xref: /openbmc/linux/sound/soc/soc-core.c (revision 065aa861b1243704b329a6d8407d8399614df6bd)
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 
75628d0f72STakashi Iwai 	return sysfs_emit(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 */
1103989ade2SKuninori Morimoto 	return rtd->dai_link->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 
23241b1774fSKuninori Morimoto static inline void soc_init_component_debugfs(struct snd_soc_component *component) { }
23341b1774fSKuninori Morimoto static inline void soc_cleanup_component_debugfs(struct snd_soc_component *component) { }
23441b1774fSKuninori Morimoto static inline void soc_init_card_debugfs(struct snd_soc_card *card) { }
23541b1774fSKuninori Morimoto static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) { }
23641b1774fSKuninori Morimoto static inline void snd_soc_debugfs_init(void) { }
23741b1774fSKuninori Morimoto static inline void snd_soc_debugfs_exit(void) { }
2386553bf06SLars-Peter Clausen 
2392624d5faSMark Brown #endif
2402624d5faSMark Brown 
2410e66a2c6SKuninori Morimoto static inline int snd_soc_dlc_component_is_empty(struct snd_soc_dai_link_component *dlc)
2420e66a2c6SKuninori Morimoto {
2430e66a2c6SKuninori Morimoto 	return !(dlc->name || dlc->of_node);
2440e66a2c6SKuninori Morimoto }
2450e66a2c6SKuninori Morimoto 
2460e66a2c6SKuninori Morimoto static inline int snd_soc_dlc_component_is_invalid(struct snd_soc_dai_link_component *dlc)
2470e66a2c6SKuninori Morimoto {
2480e66a2c6SKuninori Morimoto 	return (dlc->name && dlc->of_node);
2490e66a2c6SKuninori Morimoto }
2500e66a2c6SKuninori Morimoto 
2510e66a2c6SKuninori Morimoto static inline int snd_soc_dlc_dai_is_empty(struct snd_soc_dai_link_component *dlc)
2520e66a2c6SKuninori Morimoto {
2530e66a2c6SKuninori Morimoto 	return !dlc->dai_name;
2540e66a2c6SKuninori Morimoto }
2550e66a2c6SKuninori Morimoto 
25612b05232SKuninori Morimoto static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
257a0ac4411SKuninori Morimoto 				     struct snd_soc_component *component)
258a0ac4411SKuninori Morimoto {
2592b544dd7SKuninori Morimoto 	struct snd_soc_component *comp;
260613fb500SKuninori Morimoto 	int i;
261a0ac4411SKuninori Morimoto 
262613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, comp) {
263a0ac4411SKuninori Morimoto 		/* already connected */
2642b544dd7SKuninori Morimoto 		if (comp == component)
265a0ac4411SKuninori Morimoto 			return 0;
266a0ac4411SKuninori Morimoto 	}
267a0ac4411SKuninori Morimoto 
268613fb500SKuninori Morimoto 	/* see for_each_rtd_components */
269613fb500SKuninori Morimoto 	rtd->components[rtd->num_components] = component;
270613fb500SKuninori Morimoto 	rtd->num_components++;
271a0ac4411SKuninori Morimoto 
272a0ac4411SKuninori Morimoto 	return 0;
273a0ac4411SKuninori Morimoto }
274a0ac4411SKuninori Morimoto 
275a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
276a0ac4411SKuninori Morimoto 						const char *driver_name)
277a0ac4411SKuninori Morimoto {
2782b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
279613fb500SKuninori Morimoto 	int i;
280a0ac4411SKuninori Morimoto 
281971da24cSKuninori Morimoto 	if (!driver_name)
282971da24cSKuninori Morimoto 		return NULL;
283971da24cSKuninori Morimoto 
284a33c0d16SKuninori Morimoto 	/*
285a33c0d16SKuninori Morimoto 	 * NOTE
286a33c0d16SKuninori Morimoto 	 *
287a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
288a33c0d16SKuninori Morimoto 	 * specified driver name.
289a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
290a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
291a33c0d16SKuninori Morimoto 	 */
292613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
2932b544dd7SKuninori Morimoto 		const char *component_name = component->driver->name;
294971da24cSKuninori Morimoto 
295971da24cSKuninori Morimoto 		if (!component_name)
296971da24cSKuninori Morimoto 			continue;
297971da24cSKuninori Morimoto 
298971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
299971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
300613fb500SKuninori Morimoto 			return component;
301a0ac4411SKuninori Morimoto 	}
302a0ac4411SKuninori Morimoto 
303a0ac4411SKuninori Morimoto 	return NULL;
304a0ac4411SKuninori Morimoto }
305031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
306a0ac4411SKuninori Morimoto 
3076fbea6b6SShengjiu Wang struct snd_soc_component
30818dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
309b8132657SKuninori Morimoto {
310b8132657SKuninori Morimoto 	struct snd_soc_component *component;
3115bd7e08bSKuninori Morimoto 	struct snd_soc_component *found_component;
312b8132657SKuninori Morimoto 
3135bd7e08bSKuninori Morimoto 	found_component = NULL;
314b8132657SKuninori Morimoto 	for_each_component(component) {
3155bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3165bd7e08bSKuninori Morimoto 		    (!driver_name ||
3175bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3185bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3195bd7e08bSKuninori Morimoto 			found_component = component;
320b8132657SKuninori Morimoto 			break;
321b8132657SKuninori Morimoto 		}
3225bd7e08bSKuninori Morimoto 	}
323b8132657SKuninori Morimoto 
3245bd7e08bSKuninori Morimoto 	return found_component;
325b8132657SKuninori Morimoto }
3266fbea6b6SShengjiu Wang EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
32718dd66eaSKuninori Morimoto 
32818dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
32918dd66eaSKuninori Morimoto 						   const char *driver_name)
33018dd66eaSKuninori Morimoto {
33118dd66eaSKuninori Morimoto 	struct snd_soc_component *component;
33218dd66eaSKuninori Morimoto 
33318dd66eaSKuninori Morimoto 	mutex_lock(&client_mutex);
33418dd66eaSKuninori Morimoto 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
33518dd66eaSKuninori Morimoto 	mutex_unlock(&client_mutex);
33618dd66eaSKuninori Morimoto 
33718dd66eaSKuninori Morimoto 	return component;
33818dd66eaSKuninori Morimoto }
339b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
340b8132657SKuninori Morimoto 
34194def8eaSKuninori Morimoto struct snd_soc_pcm_runtime
34294def8eaSKuninori Morimoto *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
3434468189fSKuninori Morimoto 			 struct snd_soc_dai_link *dai_link)
34494def8eaSKuninori Morimoto {
34594def8eaSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
34694def8eaSKuninori Morimoto 
34794def8eaSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3484468189fSKuninori Morimoto 		if (rtd->dai_link == dai_link)
34994def8eaSKuninori Morimoto 			return rtd;
35094def8eaSKuninori Morimoto 	}
3514468189fSKuninori Morimoto 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
35294def8eaSKuninori Morimoto 	return NULL;
35394def8eaSKuninori Morimoto }
35494def8eaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
35594def8eaSKuninori Morimoto 
35683f94a2eSKuninori Morimoto /*
35783f94a2eSKuninori Morimoto  * Power down the audio subsystem pmdown_time msecs after close is called.
35883f94a2eSKuninori Morimoto  * This is to ensure there are no pops or clicks in between any music tracks
35983f94a2eSKuninori Morimoto  * due to DAPM power cycling.
36083f94a2eSKuninori Morimoto  */
36183f94a2eSKuninori Morimoto void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
36283f94a2eSKuninori Morimoto {
363c2233a26SKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
3640f6011fdSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
36583f94a2eSKuninori Morimoto 
36638e42f6dSKuninori Morimoto 	snd_soc_dpcm_mutex_lock(rtd);
36783f94a2eSKuninori Morimoto 
36883f94a2eSKuninori Morimoto 	dev_dbg(rtd->dev,
36983f94a2eSKuninori Morimoto 		"ASoC: pop wq checking: %s status: %s waiting: %s\n",
37083f94a2eSKuninori Morimoto 		codec_dai->driver->playback.stream_name,
371b3dea624SKuninori Morimoto 		snd_soc_dai_stream_active(codec_dai, playback) ?
372b3dea624SKuninori Morimoto 		"active" : "inactive",
37383f94a2eSKuninori Morimoto 		rtd->pop_wait ? "yes" : "no");
37483f94a2eSKuninori Morimoto 
37583f94a2eSKuninori Morimoto 	/* are we waiting on this codec DAI stream */
37683f94a2eSKuninori Morimoto 	if (rtd->pop_wait == 1) {
37783f94a2eSKuninori Morimoto 		rtd->pop_wait = 0;
3780f6011fdSKuninori Morimoto 		snd_soc_dapm_stream_event(rtd, playback,
37983f94a2eSKuninori Morimoto 					  SND_SOC_DAPM_STREAM_STOP);
38083f94a2eSKuninori Morimoto 	}
38183f94a2eSKuninori Morimoto 
38238e42f6dSKuninori Morimoto 	snd_soc_dpcm_mutex_unlock(rtd);
38383f94a2eSKuninori Morimoto }
38483f94a2eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
38583f94a2eSKuninori Morimoto 
3866e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3876e864344SKuninori Morimoto {
3886e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3896e864344SKuninori Morimoto 	kfree(dev);
3906e864344SKuninori Morimoto }
3916e864344SKuninori Morimoto 
3921c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3931c93a9e0SKuninori Morimoto {
3946e864344SKuninori Morimoto 	if (!rtd)
3956e864344SKuninori Morimoto 		return;
3966e864344SKuninori Morimoto 
397753ace0aSKuninori Morimoto 	list_del(&rtd->list);
398b7c5bc45SKuninori Morimoto 
3999c9b6520SCurtis Malainey 	if (delayed_work_pending(&rtd->delayed_work))
4000ced7b05SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
4010ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
4020ced7b05SKuninori Morimoto 
403b7c5bc45SKuninori Morimoto 	/*
404b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
405b7c5bc45SKuninori Morimoto 	 * see
406b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
407d918a376SKuninori Morimoto 	 *
408d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
409d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
410d918a376SKuninori Morimoto 	 * see
411d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
412f1b3ee78SKuninori Morimoto 	 *
413f1b3ee78SKuninori Morimoto 	 * We don't need to mind freeing for rtd,
414f1b3ee78SKuninori Morimoto 	 * because it was created from dev (= rtd->dev)
415f1b3ee78SKuninori Morimoto 	 * see
416f1b3ee78SKuninori Morimoto 	 *	soc_new_pcm_runtime()
417f1b3ee78SKuninori Morimoto 	 *
418f1b3ee78SKuninori Morimoto 	 *		rtd = devm_kzalloc(dev, ...);
419f1b3ee78SKuninori Morimoto 	 *		rtd->dev = dev
420b7c5bc45SKuninori Morimoto 	 */
421b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4221c93a9e0SKuninori Morimoto }
4231c93a9e0SKuninori Morimoto 
4244bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) {
4254bf2e385SCurtis Malainey 	struct snd_soc_pcm_runtime *rtd =
4264bf2e385SCurtis Malainey 			container_of(work, struct snd_soc_pcm_runtime,
4274bf2e385SCurtis Malainey 				     delayed_work.work);
4284bf2e385SCurtis Malainey 
4294bf2e385SCurtis Malainey 	if (rtd->close_delayed_work_func)
4304bf2e385SCurtis Malainey 		rtd->close_delayed_work_func(rtd);
4314bf2e385SCurtis Malainey }
4324bf2e385SCurtis Malainey 
4331a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4341a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4351a497983SMengdong Lin {
4361a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
437613fb500SKuninori Morimoto 	struct snd_soc_component *component;
438d918a376SKuninori Morimoto 	struct device *dev;
4396e864344SKuninori Morimoto 	int ret;
440d74c2a15SKuninori Morimoto 	int stream;
4411a497983SMengdong Lin 
4426e864344SKuninori Morimoto 	/*
443d918a376SKuninori Morimoto 	 * for rtd->dev
444d918a376SKuninori Morimoto 	 */
445d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
446d918a376SKuninori Morimoto 	if (!dev)
447d918a376SKuninori Morimoto 		return NULL;
448d918a376SKuninori Morimoto 
449d918a376SKuninori Morimoto 	dev->parent	= card->dev;
450d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
451d918a376SKuninori Morimoto 
452d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
453d918a376SKuninori Morimoto 
454d918a376SKuninori Morimoto 	ret = device_register(dev);
455d918a376SKuninori Morimoto 	if (ret < 0) {
456d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
457d918a376SKuninori Morimoto 		return NULL;
458d918a376SKuninori Morimoto 	}
459d918a376SKuninori Morimoto 
460d918a376SKuninori Morimoto 	/*
4616e864344SKuninori Morimoto 	 * for rtd
4626e864344SKuninori Morimoto 	 */
463613fb500SKuninori Morimoto 	rtd = devm_kzalloc(dev,
464613fb500SKuninori Morimoto 			   sizeof(*rtd) +
46566a796c0Slishqchn 			   sizeof(component) * (dai_link->num_cpus +
466613fb500SKuninori Morimoto 						 dai_link->num_codecs +
467613fb500SKuninori Morimoto 						 dai_link->num_platforms),
468613fb500SKuninori Morimoto 			   GFP_KERNEL);
4695fa7553dSKuninori Morimoto 	if (!rtd) {
4705fa7553dSKuninori Morimoto 		device_unregister(dev);
4715fa7553dSKuninori Morimoto 		return NULL;
4725fa7553dSKuninori Morimoto 	}
4731a497983SMengdong Lin 
474d918a376SKuninori Morimoto 	rtd->dev = dev;
47507d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->list);
476d74c2a15SKuninori Morimoto 	for_each_pcm_streams(stream) {
477d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
478d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
479d74c2a15SKuninori Morimoto 	}
480d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
4814bf2e385SCurtis Malainey 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
482d918a376SKuninori Morimoto 
4836e864344SKuninori Morimoto 	/*
48422a2fc81SKuninori Morimoto 	 * for rtd->dais
4856e864344SKuninori Morimoto 	 */
48622a2fc81SKuninori Morimoto 	rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
4876396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4881a497983SMengdong Lin 					GFP_KERNEL);
48922a2fc81SKuninori Morimoto 	if (!rtd->dais)
4906e864344SKuninori Morimoto 		goto free_rtd;
4916e864344SKuninori Morimoto 
4926e864344SKuninori Morimoto 	/*
49322a2fc81SKuninori Morimoto 	 * dais = [][][][][][][][][][][][][][][][][][]
49422a2fc81SKuninori Morimoto 	 *	  ^cpu_dais         ^codec_dais
49522a2fc81SKuninori Morimoto 	 *	  |--- num_cpus ---|--- num_codecs --|
4961729025bSKuninori Morimoto 	 * see
4971729025bSKuninori Morimoto 	 *	asoc_rtd_to_cpu()
4981729025bSKuninori Morimoto 	 *	asoc_rtd_to_codec()
49976afa643SShreyas NC 	 */
500929deb84SKuninori Morimoto 	rtd->card	= card;
501929deb84SKuninori Morimoto 	rtd->dai_link	= dai_link;
50201faf501SKuninori Morimoto 	rtd->num	= card->num_rtd++;
503c403dcd8SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;			/* default power off timeout */
504929deb84SKuninori Morimoto 
5056634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
5061a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
507a848125eSKuninori Morimoto 
5085c74c9d3SCharles Keepax 	ret = device_add_groups(dev, soc_dev_attr_groups);
5095c74c9d3SCharles Keepax 	if (ret < 0)
5105c74c9d3SCharles Keepax 		goto free_rtd;
5115c74c9d3SCharles Keepax 
512a848125eSKuninori Morimoto 	return rtd;
5136e864344SKuninori Morimoto 
5146e864344SKuninori Morimoto free_rtd:
5156e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5166e864344SKuninori Morimoto 	return NULL;
5171a497983SMengdong Lin }
5181a497983SMengdong Lin 
51965462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
52065462e44SKuninori Morimoto {
52165462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
52265462e44SKuninori Morimoto 
52365462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
52465462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
52565462e44SKuninori Morimoto }
52665462e44SKuninori Morimoto 
5276f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
528d4c1d9ebSKuninori Morimoto static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
529d4c1d9ebSKuninori Morimoto {
530d4c1d9ebSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
531d4c1d9ebSKuninori Morimoto 	struct snd_soc_dai *dai;
532d4c1d9ebSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
533d4c1d9ebSKuninori Morimoto 	int i;
534d4c1d9ebSKuninori Morimoto 
535d4c1d9ebSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
536d4c1d9ebSKuninori Morimoto 
537d4c1d9ebSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
538d4c1d9ebSKuninori Morimoto 			continue;
539d4c1d9ebSKuninori Morimoto 
540d4c1d9ebSKuninori Morimoto 		for_each_rtd_dais(rtd, i, dai) {
541d4c1d9ebSKuninori Morimoto 			if (snd_soc_dai_stream_active(dai, playback))
542d4c1d9ebSKuninori Morimoto 				snd_soc_dai_digital_mute(dai, mute, playback);
543d4c1d9ebSKuninori Morimoto 		}
544d4c1d9ebSKuninori Morimoto 	}
545d4c1d9ebSKuninori Morimoto }
546d4c1d9ebSKuninori Morimoto 
547baed393eSKuninori Morimoto static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
548baed393eSKuninori Morimoto {
549baed393eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
550baed393eSKuninori Morimoto 	int stream;
551baed393eSKuninori Morimoto 
552baed393eSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
553baed393eSKuninori Morimoto 
554baed393eSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
555baed393eSKuninori Morimoto 			continue;
556baed393eSKuninori Morimoto 
557baed393eSKuninori Morimoto 		for_each_pcm_streams(stream)
558baed393eSKuninori Morimoto 			snd_soc_dapm_stream_event(rtd, stream, event);
559baed393eSKuninori Morimoto 	}
560baed393eSKuninori Morimoto }
561baed393eSKuninori Morimoto 
562db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5636f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
564db2a4165SFrank Mandarino {
5656f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
566d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5671a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5681a497983SMengdong Lin 	int i;
569db2a4165SFrank Mandarino 
570c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
5719024bae4SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card))
572e3509ff0SDaniel Mack 		return 0;
573e3509ff0SDaniel Mack 
5742c7b696aSMarcel Ziswiler 	/*
5752c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5766ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5776ed25978SAndy Green 	 */
578b6cc78daSTakashi Iwai 	snd_power_wait(card->snd_card);
5796ed25978SAndy Green 
5806ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
581f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5826ed25978SAndy Green 
583a00f90f9SMark Brown 	/* mute any active DACs */
584d4c1d9ebSKuninori Morimoto 	soc_playback_digital_mute(card, 1);
585db2a4165SFrank Mandarino 
5864ccab3e7SLiam Girdwood 	/* suspend all pcms */
587bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5881a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5893efab7dcSMark Brown 			continue;
5903efab7dcSMark Brown 
5911a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5923efab7dcSMark Brown 	}
5934ccab3e7SLiam Girdwood 
594130dc08cSKuninori Morimoto 	snd_soc_card_suspend_pre(card);
595db2a4165SFrank Mandarino 
59637660b6dSLars-Peter Clausen 	/* close any waiting streams */
59765462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
598db2a4165SFrank Mandarino 
599baed393eSKuninori Morimoto 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
600db2a4165SFrank Mandarino 
6018be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6028be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
603e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
604e2d32ff6SMark Brown 
6059178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
6061272063aSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6071272063aSKuninori Morimoto 
6081272063aSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
6091272063aSKuninori Morimoto 			continue;
6101272063aSKuninori Morimoto 
6111272063aSKuninori Morimoto 		for_each_rtd_components(rtd, i, component) {
6122c7b696aSMarcel Ziswiler 			struct snd_soc_dapm_context *dapm =
6132c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
614d9fc4063SKuninori Morimoto 
6152c7b696aSMarcel Ziswiler 			/*
6161272063aSKuninori Morimoto 			 * ignore if component was already suspended
6172c7b696aSMarcel Ziswiler 			 */
6181272063aSKuninori Morimoto 			if (snd_soc_component_is_suspended(component))
6191272063aSKuninori Morimoto 				continue;
6201272063aSKuninori Morimoto 
6211272063aSKuninori Morimoto 			/*
6221272063aSKuninori Morimoto 			 * If there are paths active then the COMPONENT will be
6231272063aSKuninori Morimoto 			 * held with bias _ON and should not be suspended.
6241272063aSKuninori Morimoto 			 */
6254890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6261547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
627125a25daSMark Brown 				/*
6289178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
629125a25daSMark Brown 				 * bias off then being in STANDBY
630125a25daSMark Brown 				 * means it's doing something,
631125a25daSMark Brown 				 * otherwise fall through.
632125a25daSMark Brown 				 */
6334890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6349178feb4SKuninori Morimoto 					dev_dbg(component->dev,
63510e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
636125a25daSMark Brown 					break;
637125a25daSMark Brown 				}
638df561f66SGustavo A. R. Silva 				fallthrough;
639a8093297SLars-Peter Clausen 
6401547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
64166c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6429178feb4SKuninori Morimoto 				if (component->regmap)
6439178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
644988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6459178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6461547aba9SMark Brown 				break;
6471547aba9SMark Brown 			default:
6489178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6499178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6501547aba9SMark Brown 				break;
6511547aba9SMark Brown 			}
6521547aba9SMark Brown 		}
653f0fba2adSLiam Girdwood 	}
654db2a4165SFrank Mandarino 
655d17b60b2SKuninori Morimoto 	snd_soc_card_suspend_post(card);
656db2a4165SFrank Mandarino 
657db2a4165SFrank Mandarino 	return 0;
658db2a4165SFrank Mandarino }
6596f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
660db2a4165SFrank Mandarino 
6612c7b696aSMarcel Ziswiler /*
6622c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6636ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6646ed25978SAndy Green  */
6656ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
666db2a4165SFrank Mandarino {
667f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6682c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6692c7b696aSMarcel Ziswiler 				     deferred_resume_work);
670d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
671db2a4165SFrank Mandarino 
6722c7b696aSMarcel Ziswiler 	/*
6732c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6746ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6756ed25978SAndy Green 	 */
6766ed25978SAndy Green 
677f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6786ed25978SAndy Green 
6799949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
680f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6819949788bSMark Brown 
682934c752cSKuninori Morimoto 	snd_soc_card_resume_pre(card);
683db2a4165SFrank Mandarino 
684f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
685e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6869a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6871547aba9SMark Brown 	}
688db2a4165SFrank Mandarino 
689baed393eSKuninori Morimoto 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
690db2a4165SFrank Mandarino 
6913ff3f64bSMark Brown 	/* unmute any active DACs */
692d4c1d9ebSKuninori Morimoto 	soc_playback_digital_mute(card, 0);
693db2a4165SFrank Mandarino 
694739443d1SKuninori Morimoto 	snd_soc_card_resume_post(card);
695db2a4165SFrank Mandarino 
696f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6976ed25978SAndy Green 
6988be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6998be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
700e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7011a7aaa58SJeeja KP 
7021a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7031a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7046ed25978SAndy Green }
7056ed25978SAndy Green 
7066ed25978SAndy Green /* powers up audio subsystem after a suspend */
7076f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7086ed25978SAndy Green {
7096f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
71076c39e86SKuninori Morimoto 	struct snd_soc_component *component;
711b9dd94a8SPeter Ujfalusi 
712c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
7139024bae4SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card))
7145ff1ddf2SEric Miao 		return 0;
7155ff1ddf2SEric Miao 
716988e8cc4SNicolin Chen 	/* activate pins from sleep state */
71776c39e86SKuninori Morimoto 	for_each_card_components(card, component)
718b3dea624SKuninori Morimoto 		if (snd_soc_component_active(component))
71976c39e86SKuninori Morimoto 			pinctrl_pm_select_default_state(component->dev);
720988e8cc4SNicolin Chen 
721f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Scheduling resume work\n");
7226308419aSMark Brown 	if (!schedule_work(&card->deferred_resume_work))
723f110bfc7SLiam Girdwood 		dev_err(dev, "ASoC: resume work item may be lost\n");
7246ed25978SAndy Green 
725db2a4165SFrank Mandarino 	return 0;
726db2a4165SFrank Mandarino }
7276f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
728b3da4251SKuninori Morimoto 
729b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
730b3da4251SKuninori Morimoto {
731b3da4251SKuninori Morimoto 	/* deferred resume work */
732b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
733b3da4251SKuninori Morimoto }
734db2a4165SFrank Mandarino #else
7356f8ab4acSMark Brown #define snd_soc_suspend NULL
7366f8ab4acSMark Brown #define snd_soc_resume NULL
73741b1774fSKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card) { }
738db2a4165SFrank Mandarino #endif
739db2a4165SFrank Mandarino 
740c0834440SKuninori Morimoto static struct device_node
741c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
74212023a9aSMisael Lopez Cruz {
743c0834440SKuninori Morimoto 	struct device_node *of_node;
74412023a9aSMisael Lopez Cruz 
745c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
746c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
747c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
74834e81ab4SLars-Peter Clausen 
749c0834440SKuninori Morimoto 	return of_node;
75012023a9aSMisael Lopez Cruz }
75112023a9aSMisael Lopez Cruz 
752be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
753be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
754be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
755be6ac0a9SKuninori Morimoto {
756be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
757be6ac0a9SKuninori Morimoto 
7587d7db5d3SKuninori Morimoto 	if (!dlc)
7597d7db5d3SKuninori Morimoto 		return 0;
7607d7db5d3SKuninori Morimoto 
7617d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
762be6ac0a9SKuninori Morimoto 
763be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
764be6ac0a9SKuninori Morimoto 		return 0;
765be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
766be6ac0a9SKuninori Morimoto 		return 0;
767be6ac0a9SKuninori Morimoto 
768be6ac0a9SKuninori Morimoto 	return 1;
769be6ac0a9SKuninori Morimoto }
770be6ac0a9SKuninori Morimoto 
771db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
772c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
773db2a4165SFrank Mandarino {
774db2a4165SFrank Mandarino 	struct snd_soc_component *component;
775db2a4165SFrank Mandarino 
776db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
777db2a4165SFrank Mandarino 
778e3303268SKuninori Morimoto 	/*
779e3303268SKuninori Morimoto 	 * NOTE
780e3303268SKuninori Morimoto 	 *
781e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
782e3303268SKuninori Morimoto 	 * has few components by same of_node/name
783e3303268SKuninori Morimoto 	 * ex)
784e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
785e3303268SKuninori Morimoto 	 */
786c1e230f0SKuninori Morimoto 	for_each_component(component)
787c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
788db2a4165SFrank Mandarino 			return component;
78902a06d30SBarry Song 
79002a06d30SBarry Song 	return NULL;
79112023a9aSMisael Lopez Cruz }
79212023a9aSMisael Lopez Cruz 
793fbb88b5cSMengdong Lin /**
794fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
795fbb88b5cSMengdong Lin  *
7964958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
797fbb88b5cSMengdong Lin  *
798ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
799fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
800fbb88b5cSMengdong Lin  * should also match if being specified.
801fbb88b5cSMengdong Lin  *
802fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
803fbb88b5cSMengdong Lin  */
804305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
80514621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
80612023a9aSMisael Lopez Cruz {
80714621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
80814621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
80912023a9aSMisael Lopez Cruz 
81034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
81134e81ab4SLars-Peter Clausen 
81214621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
813368dee94SKuninori Morimoto 	for_each_component(component) {
814be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
81512023a9aSMisael Lopez Cruz 			continue;
81615a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8174958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8186a6dafdaSJeffy Chen 			    && (!dai->driver->name
8196a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
82014621c7eSLars-Peter Clausen 				continue;
82112023a9aSMisael Lopez Cruz 
82214621c7eSLars-Peter Clausen 			return dai;
82312023a9aSMisael Lopez Cruz 		}
82412023a9aSMisael Lopez Cruz 	}
82512023a9aSMisael Lopez Cruz 
82612023a9aSMisael Lopez Cruz 	return NULL;
82712023a9aSMisael Lopez Cruz }
828305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
82912023a9aSMisael Lopez Cruz 
830c1c277b2SKuninori Morimoto struct snd_soc_dai *snd_soc_find_dai_with_mutex(
831c1c277b2SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
832c1c277b2SKuninori Morimoto {
833c1c277b2SKuninori Morimoto 	struct snd_soc_dai *dai;
834c1c277b2SKuninori Morimoto 
835c1c277b2SKuninori Morimoto 	mutex_lock(&client_mutex);
836c1c277b2SKuninori Morimoto 	dai = snd_soc_find_dai(dlc);
837c1c277b2SKuninori Morimoto 	mutex_unlock(&client_mutex);
838c1c277b2SKuninori Morimoto 
839c1c277b2SKuninori Morimoto 	return dai;
840c1c277b2SKuninori Morimoto }
841c1c277b2SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
842c1c277b2SKuninori Morimoto 
843bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
84436794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
84536794902SKuninori Morimoto {
84636794902SKuninori Morimoto 	int i;
8470e66a2c6SKuninori Morimoto 	struct snd_soc_dai_link_component *dlc;
84836794902SKuninori Morimoto 
8490e66a2c6SKuninori Morimoto 	/* Codec check */
8500e66a2c6SKuninori Morimoto 	for_each_link_codecs(link, i, dlc) {
85136794902SKuninori Morimoto 		/*
85236794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
85336794902SKuninori Morimoto 		 * not both or neither.
85436794902SKuninori Morimoto 		 */
8550e66a2c6SKuninori Morimoto 		if (snd_soc_dlc_component_is_invalid(dlc))
8560e66a2c6SKuninori Morimoto 			goto component_invalid;
8570e66a2c6SKuninori Morimoto 
8580e66a2c6SKuninori Morimoto 		if (snd_soc_dlc_component_is_empty(dlc))
8590e66a2c6SKuninori Morimoto 			goto component_empty;
86036794902SKuninori Morimoto 
86136794902SKuninori Morimoto 		/* Codec DAI name must be specified */
8620e66a2c6SKuninori Morimoto 		if (snd_soc_dlc_dai_is_empty(dlc))
8630e66a2c6SKuninori Morimoto 			goto dai_empty;
86436794902SKuninori Morimoto 
86536794902SKuninori Morimoto 		/*
86636794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
86736794902SKuninori Morimoto 		 * component list.
86836794902SKuninori Morimoto 		 */
8690e66a2c6SKuninori Morimoto 		if (!soc_find_component(dlc))
8700e66a2c6SKuninori Morimoto 			goto component_not_find;
871b2acc24cSRanjani Sridharan 	}
87236794902SKuninori Morimoto 
8730e66a2c6SKuninori Morimoto 	/* Platform check */
8740e66a2c6SKuninori Morimoto 	for_each_link_platforms(link, i, dlc) {
87536794902SKuninori Morimoto 		/*
87636794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
87736794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
87836794902SKuninori Morimoto 		 * in the rtdcom list
87936794902SKuninori Morimoto 		 */
8800e66a2c6SKuninori Morimoto 		if (snd_soc_dlc_component_is_invalid(dlc))
8810e66a2c6SKuninori Morimoto 			goto component_invalid;
8820e66a2c6SKuninori Morimoto 
8830e66a2c6SKuninori Morimoto 		if (snd_soc_dlc_component_is_empty(dlc))
8840e66a2c6SKuninori Morimoto 			goto component_empty;
88536794902SKuninori Morimoto 
88636794902SKuninori Morimoto 		/*
88736794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
88836794902SKuninori Morimoto 		 * component list.
88936794902SKuninori Morimoto 		 */
8900e66a2c6SKuninori Morimoto 		if (!soc_find_component(dlc))
8910e66a2c6SKuninori Morimoto 			goto component_not_find;
892b2acc24cSRanjani Sridharan 	}
89336794902SKuninori Morimoto 
8940e66a2c6SKuninori Morimoto 	/* CPU check */
8950e66a2c6SKuninori Morimoto 	for_each_link_cpus(link, i, dlc) {
89636794902SKuninori Morimoto 		/*
89736794902SKuninori Morimoto 		 * CPU device may be specified by either name or OF node, but
89836794902SKuninori Morimoto 		 * can be left unspecified, and will be matched based on DAI
89936794902SKuninori Morimoto 		 * name alone..
90036794902SKuninori Morimoto 		 */
9010e66a2c6SKuninori Morimoto 		if (snd_soc_dlc_component_is_invalid(dlc))
9020e66a2c6SKuninori Morimoto 			goto component_invalid;
90336794902SKuninori Morimoto 
90436794902SKuninori Morimoto 
9050e66a2c6SKuninori Morimoto 		if (snd_soc_dlc_component_is_empty(dlc)) {
90636794902SKuninori Morimoto 			/*
9070e66a2c6SKuninori Morimoto 			 * At least one of CPU DAI name or CPU device name/node must be specified
90836794902SKuninori Morimoto 			 */
9090e66a2c6SKuninori Morimoto 			if (snd_soc_dlc_dai_is_empty(dlc))
9100e66a2c6SKuninori Morimoto 				goto component_dai_empty;
9110e66a2c6SKuninori Morimoto 		} else {
9120e66a2c6SKuninori Morimoto 			/*
9130e66a2c6SKuninori Morimoto 			 * Defer card registration if Component is not added
9140e66a2c6SKuninori Morimoto 			 */
9150e66a2c6SKuninori Morimoto 			if (!soc_find_component(dlc))
9160e66a2c6SKuninori Morimoto 				goto component_not_find;
91736794902SKuninori Morimoto 		}
91876afa643SShreyas NC 	}
91936794902SKuninori Morimoto 
92036794902SKuninori Morimoto 	return 0;
9210e66a2c6SKuninori Morimoto 
9220e66a2c6SKuninori Morimoto component_invalid:
9230e66a2c6SKuninori Morimoto 	dev_err(card->dev, "ASoC: Both Component name/of_node are set for %s\n", link->name);
9240e66a2c6SKuninori Morimoto 	return -EINVAL;
9250e66a2c6SKuninori Morimoto 
9260e66a2c6SKuninori Morimoto component_empty:
9270e66a2c6SKuninori Morimoto 	dev_err(card->dev, "ASoC: Neither Component name/of_node are set for %s\n", link->name);
9280e66a2c6SKuninori Morimoto 	return -EINVAL;
9290e66a2c6SKuninori Morimoto 
9300e66a2c6SKuninori Morimoto component_not_find:
9310e66a2c6SKuninori Morimoto 	dev_err(card->dev, "ASoC: Component %s not found for link %s\n", dlc->name, link->name);
9320e66a2c6SKuninori Morimoto 	return -EPROBE_DEFER;
9330e66a2c6SKuninori Morimoto 
9340e66a2c6SKuninori Morimoto dai_empty:
9350e66a2c6SKuninori Morimoto 	dev_err(card->dev, "ASoC: DAI name is not set for %s\n", link->name);
9360e66a2c6SKuninori Morimoto 	return -EINVAL;
9370e66a2c6SKuninori Morimoto 
9380e66a2c6SKuninori Morimoto component_dai_empty:
9390e66a2c6SKuninori Morimoto 	dev_err(card->dev, "ASoC: Neither DAI/Component name/of_node are set for %s\n", link->name);
9400e66a2c6SKuninori Morimoto 	return -EINVAL;
94136794902SKuninori Morimoto }
94236794902SKuninori Morimoto 
943da704f26SKuninori Morimoto /**
94450cd9b53SKuninori Morimoto  * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
94550cd9b53SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime has
94650cd9b53SKuninori Morimoto  * @rtd: The pcm_runtime to remove
947da704f26SKuninori Morimoto  *
94850cd9b53SKuninori Morimoto  * This function removes a pcm_runtime from the ASoC card.
949da704f26SKuninori Morimoto  */
95050cd9b53SKuninori Morimoto void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
95150cd9b53SKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
952bc7a9091SKuninori Morimoto {
953da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
954da704f26SKuninori Morimoto 
955da704f26SKuninori Morimoto 	/*
956da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
957da704f26SKuninori Morimoto 	 */
958fcbbcc32SKuninori Morimoto 	snd_soc_card_remove_dai_link(card, rtd->dai_link);
959da704f26SKuninori Morimoto 
960bc7a9091SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
961bc7a9091SKuninori Morimoto }
96250cd9b53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
963bc7a9091SKuninori Morimoto 
96463dc47daSKuninori Morimoto /**
9650c048004SKuninori Morimoto  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
9660c048004SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime is added
9670c048004SKuninori Morimoto  * @dai_link: The DAI link to find pcm_runtime
96863dc47daSKuninori Morimoto  *
9690c048004SKuninori Morimoto  * This function adds a pcm_runtime ASoC card by using dai_link.
97063dc47daSKuninori Morimoto  *
9710c048004SKuninori Morimoto  * Note: Topology can use this API to add pcm_runtime when probing the
97263dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
97363dc47daSKuninori Morimoto  * DAI links in dai_link array.
97463dc47daSKuninori Morimoto  */
975ffaf886eSKuninori Morimoto static int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
9766f2f1ff0SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
977db2a4165SFrank Mandarino {
9781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
97976afa643SShreyas NC 	struct snd_soc_dai_link_component *codec, *platform, *cpu;
98090be711eSKuninori Morimoto 	struct snd_soc_component *component;
981bfce78a5SKuninori Morimoto 	int i, ret;
982db2a4165SFrank Mandarino 
98363dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
98463dc47daSKuninori Morimoto 
98563dc47daSKuninori Morimoto 	/*
98663dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
98763dc47daSKuninori Morimoto 	 */
988cbc7a6b5SKuninori Morimoto 	ret = snd_soc_card_add_dai_link(card, dai_link);
989cbc7a6b5SKuninori Morimoto 	if (ret < 0)
990cbc7a6b5SKuninori Morimoto 		return ret;
99163dc47daSKuninori Morimoto 
992a655de80SLiam Girdwood 	if (dai_link->ignore)
993a655de80SLiam Girdwood 		return 0;
994a655de80SLiam Girdwood 
9956f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9966308419aSMark Brown 
997bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
998bfce78a5SKuninori Morimoto 	if (ret < 0)
999bfce78a5SKuninori Morimoto 		return ret;
1000bfce78a5SKuninori Morimoto 
1001513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1002513cb311SSudip Mukherjee 	if (!rtd)
1003513cb311SSudip Mukherjee 		return -ENOMEM;
1004513cb311SSudip Mukherjee 
100576afa643SShreyas NC 	for_each_link_cpus(dai_link, i, cpu) {
1006c2233a26SKuninori Morimoto 		asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1007c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_cpu(rtd, i)) {
10086b490879SMartin Hundebøll 			dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
100976afa643SShreyas NC 				 cpu->dai_name);
10101a497983SMengdong Lin 			goto _err_defer;
1011c5af3a2eSMark Brown 		}
1012c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
101376afa643SShreyas NC 	}
101476afa643SShreyas NC 
101588bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
101634614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
1017c2233a26SKuninori Morimoto 		asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1018c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_codec(rtd, i)) {
10197c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
102034614739SJerome Brunet 				 codec->dai_name);
10211a497983SMengdong Lin 			goto _err_defer;
102212023a9aSMisael Lopez Cruz 		}
102334614739SJerome Brunet 
1024c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
102588bd870fSBenoit Cousson 	}
102688bd870fSBenoit Cousson 
1027e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
102834614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1029368dee94SKuninori Morimoto 		for_each_component(component) {
103034614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
103190be711eSKuninori Morimoto 				continue;
103290be711eSKuninori Morimoto 
103312b05232SKuninori Morimoto 			snd_soc_rtd_add_component(rtd, component);
103490be711eSKuninori Morimoto 		}
103534614739SJerome Brunet 	}
103690be711eSKuninori Morimoto 
1037b19e6e7bSMark Brown 	return 0;
10381a497983SMengdong Lin 
10391a497983SMengdong Lin _err_defer:
104050cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(card, rtd);
10411a497983SMengdong Lin 	return -EPROBE_DEFER;
10426b05eda6SMark Brown }
1043ffaf886eSKuninori Morimoto 
1044ffaf886eSKuninori Morimoto int snd_soc_add_pcm_runtimes(struct snd_soc_card *card,
1045ffaf886eSKuninori Morimoto 			     struct snd_soc_dai_link *dai_link,
1046ffaf886eSKuninori Morimoto 			     int num_dai_link)
1047ffaf886eSKuninori Morimoto {
1048ffaf886eSKuninori Morimoto 	for (int i = 0; i < num_dai_link; i++) {
1049ffaf886eSKuninori Morimoto 		int ret = snd_soc_add_pcm_runtime(card, dai_link + i);
1050ffaf886eSKuninori Morimoto 
1051ffaf886eSKuninori Morimoto 		if (ret < 0)
1052ffaf886eSKuninori Morimoto 			return ret;
1053ffaf886eSKuninori Morimoto 	}
1054ffaf886eSKuninori Morimoto 
1055ffaf886eSKuninori Morimoto 	return 0;
1056ffaf886eSKuninori Morimoto }
1057ffaf886eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtimes);
10586b05eda6SMark Brown 
1059ba9e82a1SKuninori Morimoto static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1060ba9e82a1SKuninori Morimoto {
1061ba9e82a1SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1062ba9e82a1SKuninori Morimoto 	struct snd_soc_dai *dai, *not_used;
1063ba9e82a1SKuninori Morimoto 	u64 pos, possible_fmt;
1064ba9e82a1SKuninori Morimoto 	unsigned int mask = 0, dai_fmt = 0;
1065ba9e82a1SKuninori Morimoto 	int i, j, priority, pri, until;
1066ba9e82a1SKuninori Morimoto 
1067ba9e82a1SKuninori Morimoto 	/*
1068ba9e82a1SKuninori Morimoto 	 * Get selectable format from each DAIs.
1069ba9e82a1SKuninori Morimoto 	 *
1070ba9e82a1SKuninori Morimoto 	 ****************************
1071ba9e82a1SKuninori Morimoto 	 *            NOTE
1072ba9e82a1SKuninori Morimoto 	 * Using .auto_selectable_formats is not mandatory,
1073ba9e82a1SKuninori Morimoto 	 * we can select format manually from Sound Card.
1074ba9e82a1SKuninori Morimoto 	 * When use it, driver should list well tested format only.
1075ba9e82a1SKuninori Morimoto 	 ****************************
1076ba9e82a1SKuninori Morimoto 	 *
1077ba9e82a1SKuninori Morimoto 	 * ex)
1078ba9e82a1SKuninori Morimoto 	 *	auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1079ba9e82a1SKuninori Morimoto 	 *		 (A)	 (B)	 (C)
1080ba9e82a1SKuninori Morimoto 	 *	DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1081ba9e82a1SKuninori Morimoto 	 *	DAI1 : { 0xF000, 0x0F00 };
1082ba9e82a1SKuninori Morimoto 	 *		 (X)	 (Y)
1083ba9e82a1SKuninori Morimoto 	 *
1084ba9e82a1SKuninori Morimoto 	 * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1085ba9e82a1SKuninori Morimoto 	 * Here is dev_dbg() message and comments
1086ba9e82a1SKuninori Morimoto 	 *
1087ba9e82a1SKuninori Morimoto 	 * priority = 1
1088ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1089ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (0, 0000000000000000) //               Necessary Waste
1090ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1091ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1092ba9e82a1SKuninori Morimoto 	 * priority = 2
1093ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1094ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1095ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1096ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1097ba9e82a1SKuninori Morimoto 	 * priority = 3
1098ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1099ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1100ba9e82a1SKuninori Morimoto 	 * found auto selected format: 0000000000000F00
1101ba9e82a1SKuninori Morimoto 	 */
1102ba9e82a1SKuninori Morimoto 	until = snd_soc_dai_get_fmt_max_priority(rtd);
1103ba9e82a1SKuninori Morimoto 	for (priority = 1; priority <= until; priority++) {
1104ba9e82a1SKuninori Morimoto 		for_each_rtd_dais(rtd, j, not_used) {
1105ba9e82a1SKuninori Morimoto 
1106ba9e82a1SKuninori Morimoto 			possible_fmt = ULLONG_MAX;
1107ba9e82a1SKuninori Morimoto 			for_each_rtd_dais(rtd, i, dai) {
1108ba9e82a1SKuninori Morimoto 				u64 fmt = 0;
1109ba9e82a1SKuninori Morimoto 
1110ba9e82a1SKuninori Morimoto 				pri = (j >= i) ? priority : priority - 1;
1111ba9e82a1SKuninori Morimoto 				fmt = snd_soc_dai_get_fmt(dai, pri);
1112ba9e82a1SKuninori Morimoto 				possible_fmt &= fmt;
1113ba9e82a1SKuninori Morimoto 			}
1114ba9e82a1SKuninori Morimoto 			if (possible_fmt)
1115ba9e82a1SKuninori Morimoto 				goto found;
1116ba9e82a1SKuninori Morimoto 		}
1117ba9e82a1SKuninori Morimoto 	}
1118ba9e82a1SKuninori Morimoto 	/* Not Found */
1119ba9e82a1SKuninori Morimoto 	return;
1120ba9e82a1SKuninori Morimoto found:
1121ba9e82a1SKuninori Morimoto 	/*
1122ba9e82a1SKuninori Morimoto 	 * convert POSSIBLE_DAIFMT to DAIFMT
1123ba9e82a1SKuninori Morimoto 	 *
1124ba9e82a1SKuninori Morimoto 	 * Some basic/default settings on each is defined as 0.
1125ba9e82a1SKuninori Morimoto 	 * see
1126ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_NB_NF
1127ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_GATED
1128ba9e82a1SKuninori Morimoto 	 *
1129ba9e82a1SKuninori Morimoto 	 * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1130ba9e82a1SKuninori Morimoto 	 * these value, and will be overwrite to auto selected value.
1131ba9e82a1SKuninori Morimoto 	 *
1132ba9e82a1SKuninori Morimoto 	 * To avoid such issue, loop from 63 to 0 here.
1133ba9e82a1SKuninori Morimoto 	 * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
1134ba9e82a1SKuninori Morimoto 	 * Basic/Default settings of each part and aboves are defined
1135ba9e82a1SKuninori Morimoto 	 * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1136ba9e82a1SKuninori Morimoto 	 */
1137ba9e82a1SKuninori Morimoto 	for (i = 63; i >= 0; i--) {
1138ba9e82a1SKuninori Morimoto 		pos = 1ULL << i;
1139ba9e82a1SKuninori Morimoto 		switch (possible_fmt & pos) {
1140ba9e82a1SKuninori Morimoto 		/*
1141ba9e82a1SKuninori Morimoto 		 * for format
1142ba9e82a1SKuninori Morimoto 		 */
1143ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_I2S:
1144ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1145ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1146ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1147ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1148ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_AC97:
1149ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_PDM:
1150ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1151ba9e82a1SKuninori Morimoto 			break;
1152ba9e82a1SKuninori Morimoto 		/*
1153ba9e82a1SKuninori Morimoto 		 * for clock
1154ba9e82a1SKuninori Morimoto 		 */
1155ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CONT:
1156ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1157ba9e82a1SKuninori Morimoto 			break;
1158ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_GATED:
1159ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1160ba9e82a1SKuninori Morimoto 			break;
1161ba9e82a1SKuninori Morimoto 		/*
1162ba9e82a1SKuninori Morimoto 		 * for clock invert
1163ba9e82a1SKuninori Morimoto 		 */
1164ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1165ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1166ba9e82a1SKuninori Morimoto 			break;
1167ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1168ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1169ba9e82a1SKuninori Morimoto 			break;
1170ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1171ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1172ba9e82a1SKuninori Morimoto 			break;
1173ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1174ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1175ba9e82a1SKuninori Morimoto 			break;
1176ba9e82a1SKuninori Morimoto 		/*
1177ba9e82a1SKuninori Morimoto 		 * for clock provider / consumer
1178ba9e82a1SKuninori Morimoto 		 */
1179ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1180ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1181ba9e82a1SKuninori Morimoto 			break;
1182ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1183ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1184ba9e82a1SKuninori Morimoto 			break;
1185ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1186ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1187ba9e82a1SKuninori Morimoto 			break;
1188ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1189ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1190ba9e82a1SKuninori Morimoto 			break;
1191ba9e82a1SKuninori Morimoto 		}
1192ba9e82a1SKuninori Morimoto 	}
1193ba9e82a1SKuninori Morimoto 
1194ba9e82a1SKuninori Morimoto 	/*
1195ba9e82a1SKuninori Morimoto 	 * Some driver might have very complex limitation.
1196ba9e82a1SKuninori Morimoto 	 * In such case, user want to auto-select non-limitation part,
1197ba9e82a1SKuninori Morimoto 	 * and want to manually specify complex part.
1198ba9e82a1SKuninori Morimoto 	 *
1199ba9e82a1SKuninori Morimoto 	 * Or for example, if both CPU and Codec can be clock provider,
1200ba9e82a1SKuninori Morimoto 	 * but because of its quality, user want to specify it manually.
1201ba9e82a1SKuninori Morimoto 	 *
1202ba9e82a1SKuninori Morimoto 	 * Use manually specified settings if sound card did.
1203ba9e82a1SKuninori Morimoto 	 */
1204ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1205ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1206ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1207ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1208ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1209ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_INV_MASK;
1210640eac4cSKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1211640eac4cSKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
1212ba9e82a1SKuninori Morimoto 
1213ba9e82a1SKuninori Morimoto 	dai_link->dai_fmt |= (dai_fmt & mask);
1214ba9e82a1SKuninori Morimoto }
1215ba9e82a1SKuninori Morimoto 
12164d1a98b5SKuninori Morimoto /**
12174d1a98b5SKuninori Morimoto  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
12184d1a98b5SKuninori Morimoto  * @rtd: The runtime for which the DAI link format should be changed
12194d1a98b5SKuninori Morimoto  * @dai_fmt: The new DAI link format
12204d1a98b5SKuninori Morimoto  *
12214d1a98b5SKuninori Morimoto  * This function updates the DAI link format for all DAIs connected to the DAI
12224d1a98b5SKuninori Morimoto  * link for the specified runtime.
12234d1a98b5SKuninori Morimoto  *
12244d1a98b5SKuninori Morimoto  * Note: For setups with a static format set the dai_fmt field in the
12254d1a98b5SKuninori Morimoto  * corresponding snd_dai_link struct instead of using this function.
12264d1a98b5SKuninori Morimoto  *
12274d1a98b5SKuninori Morimoto  * Returns 0 on success, otherwise a negative error code.
12284d1a98b5SKuninori Morimoto  */
12294d1a98b5SKuninori Morimoto int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
12304d1a98b5SKuninori Morimoto 				unsigned int dai_fmt)
12314d1a98b5SKuninori Morimoto {
12324d1a98b5SKuninori Morimoto 	struct snd_soc_dai *cpu_dai;
12334d1a98b5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
12344d1a98b5SKuninori Morimoto 	unsigned int i;
12354d1a98b5SKuninori Morimoto 	int ret;
12364d1a98b5SKuninori Morimoto 
12377db07e37SKuninori Morimoto 	if (!dai_fmt)
12387db07e37SKuninori Morimoto 		return 0;
12397db07e37SKuninori Morimoto 
12404d1a98b5SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
12414d1a98b5SKuninori Morimoto 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
12424d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12434d1a98b5SKuninori Morimoto 			return ret;
12444d1a98b5SKuninori Morimoto 	}
12454d1a98b5SKuninori Morimoto 
12466c076273SCharles Keepax 	/* Flip the polarity for the "CPU" end of link */
12476c076273SCharles Keepax 	dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
1248b44a67f8SKuninori Morimoto 
12494d1a98b5SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
12506c076273SCharles Keepax 		ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
12514d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12524d1a98b5SKuninori Morimoto 			return ret;
12534d1a98b5SKuninori Morimoto 	}
12544d1a98b5SKuninori Morimoto 
12554d1a98b5SKuninori Morimoto 	return 0;
12564d1a98b5SKuninori Morimoto }
12574d1a98b5SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
12584d1a98b5SKuninori Morimoto 
1259eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card,
126046496acbSKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
126146496acbSKuninori Morimoto {
126246496acbSKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1263c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
126446496acbSKuninori Morimoto 	struct snd_soc_component *component;
1265613fb500SKuninori Morimoto 	int ret, num, i;
126646496acbSKuninori Morimoto 
126746496acbSKuninori Morimoto 	/* do machine specific initialization */
126802e75636SKuninori Morimoto 	ret = snd_soc_link_init(rtd);
126902e75636SKuninori Morimoto 	if (ret < 0)
127046496acbSKuninori Morimoto 		return ret;
127146496acbSKuninori Morimoto 
1272ba9e82a1SKuninori Morimoto 	snd_soc_runtime_get_dai_fmt(rtd);
127346496acbSKuninori Morimoto 	ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
127446496acbSKuninori Morimoto 	if (ret)
127546496acbSKuninori Morimoto 		return ret;
127646496acbSKuninori Morimoto 
127746496acbSKuninori Morimoto 	/* add DPCM sysfs entries */
127846496acbSKuninori Morimoto 	soc_dpcm_debugfs_add(rtd);
127946496acbSKuninori Morimoto 
128046496acbSKuninori Morimoto 	num = rtd->num;
128146496acbSKuninori Morimoto 
128246496acbSKuninori Morimoto 	/*
128346496acbSKuninori Morimoto 	 * most drivers will register their PCMs using DAI link ordering but
128446496acbSKuninori Morimoto 	 * topology based drivers can use the DAI link id field to set PCM
128546496acbSKuninori Morimoto 	 * device number and then use rtd + a base offset of the BEs.
128646496acbSKuninori Morimoto 	 */
1287613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
128846496acbSKuninori Morimoto 		if (!component->driver->use_dai_pcm_id)
128946496acbSKuninori Morimoto 			continue;
129046496acbSKuninori Morimoto 
129146496acbSKuninori Morimoto 		if (rtd->dai_link->no_pcm)
129246496acbSKuninori Morimoto 			num += component->driver->be_pcm_base;
129346496acbSKuninori Morimoto 		else
129446496acbSKuninori Morimoto 			num = rtd->dai_link->id;
129546496acbSKuninori Morimoto 	}
129646496acbSKuninori Morimoto 
129746496acbSKuninori Morimoto 	/* create compress_device if possible */
129846496acbSKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
129962462e01SKuninori Morimoto 	if (ret != -ENOTSUPP)
130046496acbSKuninori Morimoto 		return ret;
130146496acbSKuninori Morimoto 
130246496acbSKuninori Morimoto 	/* create the pcm */
130346496acbSKuninori Morimoto 	ret = soc_new_pcm(rtd, num);
130446496acbSKuninori Morimoto 	if (ret < 0) {
130546496acbSKuninori Morimoto 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
130646496acbSKuninori Morimoto 			dai_link->stream_name, ret);
130746496acbSKuninori Morimoto 		return ret;
130846496acbSKuninori Morimoto 	}
1309d1eb6d11SKuninori Morimoto 
13100b73ba55SKuninori Morimoto 	return snd_soc_pcm_dai_new(rtd);
131146496acbSKuninori Morimoto }
131246496acbSKuninori Morimoto 
1313ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1314ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1315ffd60fbaSKuninori Morimoto {
1316aec3ff99SKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
131729d9fc7aSKuninori Morimoto 	const char *str;
131829d9fc7aSKuninori Morimoto 	int ret, i;
1319ffd60fbaSKuninori Morimoto 
13204702f991SKuninori Morimoto 	for (i = 0; i < card->num_configs; i++) {
1321ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1322ffd60fbaSKuninori Morimoto 
13233256ef98SSameer Pujar 		if (snd_soc_is_matching_component(&map->dlc, component) &&
13243256ef98SSameer Pujar 		    map->name_prefix) {
1325ffd60fbaSKuninori Morimoto 			component->name_prefix = map->name_prefix;
1326ffd60fbaSKuninori Morimoto 			return;
1327ffd60fbaSKuninori Morimoto 		}
1328c13493a2SKuninori Morimoto 	}
1329ffd60fbaSKuninori Morimoto 
1330ffd60fbaSKuninori Morimoto 	/*
1331ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1332ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1333ffd60fbaSKuninori Morimoto 	 */
133429d9fc7aSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
133529d9fc7aSKuninori Morimoto 	if (ret < 0)
133629d9fc7aSKuninori Morimoto 		return;
133729d9fc7aSKuninori Morimoto 
133829d9fc7aSKuninori Morimoto 	component->name_prefix = str;
1339ffd60fbaSKuninori Morimoto }
1340ffd60fbaSKuninori Morimoto 
1341c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1342c6619b72SKuninori Morimoto 				 int probed)
134322d14231SKuninori Morimoto {
1344c6619b72SKuninori Morimoto 
1345c6619b72SKuninori Morimoto 	if (!component->card)
1346c6619b72SKuninori Morimoto 		return;
1347c6619b72SKuninori Morimoto 
1348c6619b72SKuninori Morimoto 	if (probed)
1349c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1350c6619b72SKuninori Morimoto 
13517a5d9815SBard liao 	list_del_init(&component->card_list);
135222d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
135322d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
135422d14231SKuninori Morimoto 	component->card = NULL;
13550e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
135622d14231SKuninori Morimoto }
135722d14231SKuninori Morimoto 
1358ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1359ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1360ffd60fbaSKuninori Morimoto {
1361ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1362ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1363ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1364c6619b72SKuninori Morimoto 	int probed = 0;
1365ffd60fbaSKuninori Morimoto 	int ret;
1366ffd60fbaSKuninori Morimoto 
13678f1a1681SKuninori Morimoto 	if (snd_soc_component_is_dummy(component))
1368ffd60fbaSKuninori Morimoto 		return 0;
1369ffd60fbaSKuninori Morimoto 
1370ffd60fbaSKuninori Morimoto 	if (component->card) {
1371ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1372ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1373ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1374ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1375ffd60fbaSKuninori Morimoto 			return -ENODEV;
1376ffd60fbaSKuninori Morimoto 		}
1377ffd60fbaSKuninori Morimoto 		return 0;
1378ffd60fbaSKuninori Morimoto 	}
1379ffd60fbaSKuninori Morimoto 
1380ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1381ffd60fbaSKuninori Morimoto 	if (ret < 0)
1382ffd60fbaSKuninori Morimoto 		return ret;
1383ffd60fbaSKuninori Morimoto 
1384ffd60fbaSKuninori Morimoto 	component->card = card;
1385ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1386ffd60fbaSKuninori Morimoto 
1387ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1388ffd60fbaSKuninori Morimoto 
138995c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1390b614beafSKuninori Morimoto 
1391ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1392ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1393ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1394ffd60fbaSKuninori Morimoto 
1395ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1396ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1397ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1398ffd60fbaSKuninori Morimoto 		goto err_probe;
1399ffd60fbaSKuninori Morimoto 	}
1400ffd60fbaSKuninori Morimoto 
1401ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1402ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1403ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1404ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1405ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1406ffd60fbaSKuninori Morimoto 			goto err_probe;
1407ffd60fbaSKuninori Morimoto 		}
1408ffd60fbaSKuninori Morimoto 	}
1409ffd60fbaSKuninori Morimoto 
1410ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
141160adbd8fSKuninori Morimoto 	if (ret < 0)
1412ffd60fbaSKuninori Morimoto 		goto err_probe;
141360adbd8fSKuninori Morimoto 
1414ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1415ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1416ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1417ffd60fbaSKuninori Morimoto 	     component->name);
1418c6619b72SKuninori Morimoto 	probed = 1;
1419ffd60fbaSKuninori Morimoto 
1420257c4dacSKuninori Morimoto 	/*
1421257c4dacSKuninori Morimoto 	 * machine specific init
1422257c4dacSKuninori Morimoto 	 * see
1423257c4dacSKuninori Morimoto 	 *	snd_soc_component_set_aux()
1424257c4dacSKuninori Morimoto 	 */
1425257c4dacSKuninori Morimoto 	ret = snd_soc_component_init(component);
1426257c4dacSKuninori Morimoto 	if (ret < 0)
1427ffd60fbaSKuninori Morimoto 		goto err_probe;
1428ffd60fbaSKuninori Morimoto 
1429ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1430ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1431ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1432ffd60fbaSKuninori Morimoto 	if (ret < 0)
1433ffd60fbaSKuninori Morimoto 		goto err_probe;
1434ffd60fbaSKuninori Morimoto 
1435ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1436ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1437ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1438a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1439a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1440a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1441a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1442a22ae72bSPierre-Louis Bossart 				 __func__);
1443a22ae72bSPierre-Louis Bossart 		} else {
1444a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1445a22ae72bSPierre-Louis Bossart 				"%s: snd_soc_dapm_add_routes failed: %d\n",
1446a22ae72bSPierre-Louis Bossart 				__func__, ret);
1447ffd60fbaSKuninori Morimoto 			goto err_probe;
1448a22ae72bSPierre-Louis Bossart 		}
1449a22ae72bSPierre-Louis Bossart 	}
1450ffd60fbaSKuninori Morimoto 
1451ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1452ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1453ffd60fbaSKuninori Morimoto 
1454ffd60fbaSKuninori Morimoto err_probe:
1455ffd60fbaSKuninori Morimoto 	if (ret < 0)
1456c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1457ffd60fbaSKuninori Morimoto 
1458ffd60fbaSKuninori Morimoto 	return ret;
1459ffd60fbaSKuninori Morimoto }
1460ffd60fbaSKuninori Morimoto 
14614ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1462f0fba2adSLiam Girdwood {
14634ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
14644ca47d21SKuninori Morimoto 	int order;
14654ca47d21SKuninori Morimoto 
14664ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
14674ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14687eaa313bSKuninori Morimoto 			/* remove all rtd connected DAIs in good order */
14697eaa313bSKuninori Morimoto 			snd_soc_pcm_dai_remove(rtd, order);
1470f0fba2adSLiam Girdwood 		}
14714ca47d21SKuninori Morimoto 	}
14724ca47d21SKuninori Morimoto }
1473f0fba2adSLiam Girdwood 
1474bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1475bc7c16c2SKuninori Morimoto {
1476bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
147751801aeaSKuninori Morimoto 	int order, ret;
1478bc7c16c2SKuninori Morimoto 
1479bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1480bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
148151801aeaSKuninori Morimoto 			/* probe all rtd connected DAIs in good order */
148251801aeaSKuninori Morimoto 			ret = snd_soc_pcm_dai_probe(rtd, order);
1483bc7c16c2SKuninori Morimoto 			if (ret)
1484bc7c16c2SKuninori Morimoto 				return ret;
1485bc7c16c2SKuninori Morimoto 		}
1486bc7c16c2SKuninori Morimoto 	}
1487bc7c16c2SKuninori Morimoto 
1488bc7c16c2SKuninori Morimoto 	return 0;
1489bc7c16c2SKuninori Morimoto }
1490bc7c16c2SKuninori Morimoto 
1491b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
149262ae68faSStephen Warren {
149361aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1494b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1495613fb500SKuninori Morimoto 	int i, order;
149662ae68faSStephen Warren 
1497b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1498b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1499613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
1500b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1501b006c0c6SKuninori Morimoto 					continue;
1502b006c0c6SKuninori Morimoto 
1503c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
150462ae68faSStephen Warren 			}
150562ae68faSStephen Warren 		}
1506b006c0c6SKuninori Morimoto 	}
1507b006c0c6SKuninori Morimoto }
150862ae68faSStephen Warren 
150962f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
15106fb03550SKuninori Morimoto {
15116fb03550SKuninori Morimoto 	struct snd_soc_component *component;
151262f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1513613fb500SKuninori Morimoto 	int i, ret, order;
15146fb03550SKuninori Morimoto 
151562f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
151662f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1517613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
151862f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
151962f07a6bSKuninori Morimoto 					continue;
152062f07a6bSKuninori Morimoto 
15216fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
15226fb03550SKuninori Morimoto 				if (ret < 0)
15236fb03550SKuninori Morimoto 					return ret;
15246fb03550SKuninori Morimoto 			}
15256fb03550SKuninori Morimoto 		}
152662f07a6bSKuninori Morimoto 	}
15276fb03550SKuninori Morimoto 
15286fb03550SKuninori Morimoto 	return 0;
15296fb03550SKuninori Morimoto }
15306fb03550SKuninori Morimoto 
1531e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15324893a2ebSKuninori Morimoto {
1533e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1534e8fbd250SKuninori Morimoto 
1535e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
1536257c4dacSKuninori Morimoto 		/* for snd_soc_component_init() */
1537257c4dacSKuninori Morimoto 		snd_soc_component_set_aux(component, NULL);
15384893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15394893a2ebSKuninori Morimoto 	}
1540e8fbd250SKuninori Morimoto }
15414893a2ebSKuninori Morimoto 
1542bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1543b19e6e7bSMark Brown {
1544f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1545bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1546bee886f1SKuninori Morimoto 	int i;
15473ca041edSSebastian Reichel 
1548bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1549f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1550bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1551f2ed6b07SMengdong Lin 		if (!component)
15523dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15533ca041edSSebastian Reichel 
1554257c4dacSKuninori Morimoto 		/* for snd_soc_component_init() */
1555257c4dacSKuninori Morimoto 		snd_soc_component_set_aux(component, aux);
1556c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1557d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1558bee886f1SKuninori Morimoto 	}
1559f2ed6b07SMengdong Lin 	return 0;
1560b19e6e7bSMark Brown }
1561b19e6e7bSMark Brown 
1562f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1563f2ed6b07SMengdong Lin {
156474bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1565f2ed6b07SMengdong Lin 	int order;
1566f2ed6b07SMengdong Lin 	int ret;
1567f2ed6b07SMengdong Lin 
15681a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
156974bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
157074bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
157174bd3f92SKuninori Morimoto 				continue;
157274bd3f92SKuninori Morimoto 
157374bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
157474bd3f92SKuninori Morimoto 			if (ret < 0)
1575f2ed6b07SMengdong Lin 				return ret;
1576f2ed6b07SMengdong Lin 		}
1577f2ed6b07SMengdong Lin 	}
157865d9361fSLars-Peter Clausen 
157944c69bb1SLars-Peter Clausen 	return 0;
15803ca041edSSebastian Reichel }
15812eea392dSJarkko Nikula 
1582f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
158344c69bb1SLars-Peter Clausen {
1584f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1585f2ed6b07SMengdong Lin 	int order;
158644c69bb1SLars-Peter Clausen 
15871a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1588c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1589e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1590c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
15915f3484acSLars-Peter Clausen 		}
15922eea392dSJarkko Nikula 	}
15932eea392dSJarkko Nikula }
15942eea392dSJarkko Nikula 
15951f5a4535STakashi Iwai #ifdef CONFIG_DMI
15962c7b696aSMarcel Ziswiler /*
15978a6a6a38SKuninori Morimoto  * If a DMI filed contain strings in this blacklist (e.g.
15988a6a6a38SKuninori Morimoto  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
15998a6a6a38SKuninori Morimoto  * as invalid and dropped when setting the card long name from DMI info.
16008a6a6a38SKuninori Morimoto  */
16018a6a6a38SKuninori Morimoto static const char * const dmi_blacklist[] = {
16028a6a6a38SKuninori Morimoto 	"To be filled by OEM",
16038a6a6a38SKuninori Morimoto 	"TBD by OEM",
16048a6a6a38SKuninori Morimoto 	"Default String",
16058a6a6a38SKuninori Morimoto 	"Board Manufacturer",
16068a6a6a38SKuninori Morimoto 	"Board Vendor Name",
16078a6a6a38SKuninori Morimoto 	"Board Product Name",
16088a6a6a38SKuninori Morimoto 	NULL,	/* terminator */
16098a6a6a38SKuninori Morimoto };
16108a6a6a38SKuninori Morimoto 
16118a6a6a38SKuninori Morimoto /*
16122c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1613345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1614345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1615345233d7SLiam Girdwood  */
1616345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1617345233d7SLiam Girdwood {
1618345233d7SLiam Girdwood 	int i, j = 0;
1619345233d7SLiam Girdwood 
1620345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1621345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1622345233d7SLiam Girdwood 		    || (name[i] == '_'))
1623345233d7SLiam Girdwood 			name[j++] = name[i];
1624345233d7SLiam Girdwood 		else if (name[i] == '-')
1625345233d7SLiam Girdwood 			name[j++] = '_';
1626345233d7SLiam Girdwood 	}
1627345233d7SLiam Girdwood 
1628345233d7SLiam Girdwood 	name[j] = '\0';
1629345233d7SLiam Girdwood }
1630345233d7SLiam Girdwood 
16312c7b696aSMarcel Ziswiler /*
16322c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
163398faf436SMengdong Lin  * in the black list.
163498faf436SMengdong Lin  */
163598faf436SMengdong Lin static int is_dmi_valid(const char *field)
163698faf436SMengdong Lin {
163798faf436SMengdong Lin 	int i = 0;
163898faf436SMengdong Lin 
163998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
164098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
164198faf436SMengdong Lin 			return 0;
164298faf436SMengdong Lin 		i++;
164346b5a4d2SWu Fengguang 	}
164498faf436SMengdong Lin 
164598faf436SMengdong Lin 	return 1;
164698faf436SMengdong Lin }
164798faf436SMengdong Lin 
16484e01e5dbSJaroslav Kysela /*
16494e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
16504e01e5dbSJaroslav Kysela  */
16514e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
16524e01e5dbSJaroslav Kysela {
16534e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
16544e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
16554e01e5dbSJaroslav Kysela 	size_t len;
16564e01e5dbSJaroslav Kysela 
16574e01e5dbSJaroslav Kysela 	len = strlen(dst);
16584e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
16594e01e5dbSJaroslav Kysela 
16604e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
16614e01e5dbSJaroslav Kysela 	if (len < dst_len)
16624e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
16634e01e5dbSJaroslav Kysela }
16644e01e5dbSJaroslav Kysela 
1665345233d7SLiam Girdwood /**
1666345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1667345233d7SLiam Girdwood  * @card: The card to register DMI names
1668345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1669345233d7SLiam Girdwood  *
1670345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1671345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1672345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1673345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1674345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1675345233d7SLiam Girdwood  * as the sound card long name, in the format of
1676345233d7SLiam Girdwood  * "vendor-product-version-board"
1677345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1678345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1679345233d7SLiam Girdwood  * (UCM) configurations for the card.
1680345233d7SLiam Girdwood  *
1681345233d7SLiam Girdwood  * Possible card long names may be:
1682345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1683345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1684345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1685345233d7SLiam Girdwood  *
1686345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1687345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1688345233d7SLiam Girdwood  *
1689345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1690345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1691345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1692345233d7SLiam Girdwood  * charactors like SPACE.
1693345233d7SLiam Girdwood  *
1694345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1695345233d7SLiam Girdwood  */
1696345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1697345233d7SLiam Girdwood {
1698cdb76568SKuninori Morimoto 	const char *vendor, *product, *board;
1699345233d7SLiam Girdwood 
1700345233d7SLiam Girdwood 	if (card->long_name)
1701345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1702345233d7SLiam Girdwood 
1703c68fded7SPierre-Louis Bossart 	if (!dmi_available)
17047de14d58SJon Hunter 		return 0;
17057de14d58SJon Hunter 
17064e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1707345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
170898faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1709345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1710345233d7SLiam Girdwood 		return 0;
1711345233d7SLiam Girdwood 	}
1712345233d7SLiam Girdwood 
17134e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1714345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1715345233d7SLiam Girdwood 
1716345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
171798faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1718cdb76568SKuninori Morimoto 		const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1719cdb76568SKuninori Morimoto 
17204e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1721345233d7SLiam Girdwood 
17222c7b696aSMarcel Ziswiler 		/*
17232c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1724345233d7SLiam Girdwood 		 * name in the product version field
1725345233d7SLiam Girdwood 		 */
17264e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
17274e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1728345233d7SLiam Girdwood 	}
1729345233d7SLiam Girdwood 
1730345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
173198faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
173239870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
17334e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1734345233d7SLiam Girdwood 	} else if (!product) {
1735345233d7SLiam Girdwood 		/* fall back to using legacy name */
1736345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1737345233d7SLiam Girdwood 		return 0;
1738345233d7SLiam Girdwood 	}
1739345233d7SLiam Girdwood 
1740345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
17414e01e5dbSJaroslav Kysela 	if (flavour)
17424e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1743345233d7SLiam Girdwood 
1744345233d7SLiam Girdwood 	/* set the card long name */
1745345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1746345233d7SLiam Girdwood 
1747345233d7SLiam Girdwood 	return 0;
1748345233d7SLiam Girdwood }
1749345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
17501f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1751345233d7SLiam Girdwood 
1752a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1753a655de80SLiam Girdwood {
1754a655de80SLiam Girdwood 	struct snd_soc_component *component;
1755a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1756a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1757a655de80SLiam Girdwood 	int i;
1758a655de80SLiam Girdwood 
1759368dee94SKuninori Morimoto 	for_each_component(component) {
1760a655de80SLiam Girdwood 
176149f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1762a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1763a655de80SLiam Girdwood 			continue;
1764a655de80SLiam Girdwood 
1765a655de80SLiam Girdwood 		/* for this machine ? */
1766e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1767a655de80SLiam Girdwood 			    card->dev->driver->name))
1768e194098bSPierre-Louis Bossart 			goto match;
1769e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1770e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1771a655de80SLiam Girdwood 			continue;
1772e194098bSPierre-Louis Bossart match:
1773a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
17747fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1775a655de80SLiam Girdwood 
1776a655de80SLiam Girdwood 			/* ignore this FE */
1777a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1778a655de80SLiam Girdwood 				dai_link->ignore = true;
1779a655de80SLiam Girdwood 				continue;
1780a655de80SLiam Girdwood 			}
1781a655de80SLiam Girdwood 
17821328948fSPierre-Louis Bossart 			dev_dbg(card->dev, "info: override BE DAI link %s\n",
1783a655de80SLiam Girdwood 				card->dai_link[i].name);
1784a655de80SLiam Girdwood 
1785a655de80SLiam Girdwood 			/* override platform component */
1786adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1787daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1788daecf46eSKuninori Morimoto 				continue;
1789daecf46eSKuninori Morimoto 			}
1790d9714003SDaniel Baluta 
1791d9714003SDaniel Baluta 			if (component->dev->of_node)
1792d9714003SDaniel Baluta 				dai_link->platforms->of_node = component->dev->of_node;
1793d9714003SDaniel Baluta 			else
1794910fdcabSKuninori Morimoto 				dai_link->platforms->name = component->name;
1795a655de80SLiam Girdwood 
1796a655de80SLiam Girdwood 			/* convert non BE into BE */
1797607fa205SBard Liao 			if (!dai_link->no_pcm) {
1798a655de80SLiam Girdwood 				dai_link->no_pcm = 1;
1799607fa205SBard Liao 
1800607fa205SBard Liao 				if (dai_link->dpcm_playback)
1801607fa205SBard Liao 					dev_warn(card->dev,
1802607fa205SBard Liao 						 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1803607fa205SBard Liao 						 dai_link->name);
1804607fa205SBard Liao 				if (dai_link->dpcm_capture)
1805607fa205SBard Liao 					dev_warn(card->dev,
1806607fa205SBard Liao 						 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1807607fa205SBard Liao 						 dai_link->name);
1808607fa205SBard Liao 
1809607fa205SBard Liao 				/* convert normal link into DPCM one */
1810607fa205SBard Liao 				if (!(dai_link->dpcm_playback ||
1811607fa205SBard Liao 				      dai_link->dpcm_capture)) {
1812607fa205SBard Liao 					dai_link->dpcm_playback = !dai_link->capture_only;
1813607fa205SBard Liao 					dai_link->dpcm_capture = !dai_link->playback_only;
1814607fa205SBard Liao 				}
1815607fa205SBard Liao 			}
1816a655de80SLiam Girdwood 
18170cbbf8a0SKuninori Morimoto 			/*
18180cbbf8a0SKuninori Morimoto 			 * override any BE fixups
18190cbbf8a0SKuninori Morimoto 			 * see
18200cbbf8a0SKuninori Morimoto 			 *	snd_soc_link_be_hw_params_fixup()
18210cbbf8a0SKuninori Morimoto 			 */
1822a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1823a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1824a655de80SLiam Girdwood 
18252c7b696aSMarcel Ziswiler 			/*
18262c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1827a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1828a655de80SLiam Girdwood 			 */
1829a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1830a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1831a655de80SLiam Girdwood 		}
1832a655de80SLiam Girdwood 
1833a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1834a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1835a655de80SLiam Girdwood 
1836a655de80SLiam Girdwood 			/* topology shortname created? */
1837a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1838a655de80SLiam Girdwood 				comp_drv = component->driver;
1839a655de80SLiam Girdwood 
1840a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1841a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1842a655de80SLiam Girdwood 					 card->name);
1843a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1844a655de80SLiam Girdwood 			}
1845a655de80SLiam Girdwood 
1846a655de80SLiam Girdwood 			/* use topology shortname */
1847a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1848a655de80SLiam Girdwood 		}
1849a655de80SLiam Girdwood 	}
1850a655de80SLiam Girdwood }
1851a655de80SLiam Girdwood 
1852c8d18e44SJaroslav Kysela #define soc_setup_card_name(card, name, name1, name2) \
1853c8d18e44SJaroslav Kysela 	__soc_setup_card_name(card, name, sizeof(name), name1, name2)
1854c8d18e44SJaroslav Kysela static void __soc_setup_card_name(struct snd_soc_card *card,
1855c8d18e44SJaroslav Kysela 				  char *name, int len,
1856c8d18e44SJaroslav Kysela 				  const char *name1, const char *name2)
18570f23f718SKuninori Morimoto {
1858c8d18e44SJaroslav Kysela 	const char *src = name1 ? name1 : name2;
18590f23f718SKuninori Morimoto 	int i;
18600f23f718SKuninori Morimoto 
1861c8d18e44SJaroslav Kysela 	snprintf(name, len, "%s", src);
18620f23f718SKuninori Morimoto 
1863c8d18e44SJaroslav Kysela 	if (name != card->snd_card->driver)
18640f23f718SKuninori Morimoto 		return;
18650f23f718SKuninori Morimoto 
18660f23f718SKuninori Morimoto 	/*
1867c8d18e44SJaroslav Kysela 	 * Name normalization (driver field)
18680f23f718SKuninori Morimoto 	 *
18690f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
18700f23f718SKuninori Morimoto 	 * searches in the user-space.
18710f23f718SKuninori Morimoto 	 *
18720f23f718SKuninori Morimoto 	 * ex)
18730f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
18740f23f718SKuninori Morimoto 	 */
18750f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
18760f23f718SKuninori Morimoto 		switch (name[i]) {
18770f23f718SKuninori Morimoto 		case '_':
18780f23f718SKuninori Morimoto 		case '-':
18790f23f718SKuninori Morimoto 		case '\0':
18800f23f718SKuninori Morimoto 			break;
18810f23f718SKuninori Morimoto 		default:
18820f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
18830f23f718SKuninori Morimoto 				name[i] = '_';
18840f23f718SKuninori Morimoto 			break;
18850f23f718SKuninori Morimoto 		}
18860f23f718SKuninori Morimoto 	}
1887c8d18e44SJaroslav Kysela 
1888c8d18e44SJaroslav Kysela 	/*
1889c8d18e44SJaroslav Kysela 	 * The driver field should contain a valid string from the user view.
1890c8d18e44SJaroslav Kysela 	 * The wrapping usually does not work so well here. Set a smaller string
1891c8d18e44SJaroslav Kysela 	 * in the specific ASoC driver.
1892c8d18e44SJaroslav Kysela 	 */
1893c8d18e44SJaroslav Kysela 	if (strlen(src) > len - 1)
1894c8d18e44SJaroslav Kysela 		dev_err(card->dev, "ASoC: driver name too long '%s' -> '%s'\n", src, name);
18950f23f718SKuninori Morimoto }
18960f23f718SKuninori Morimoto 
189727f07cacSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
189853e947a0SKuninori Morimoto {
1899cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd, *n;
19007ce6088fSKuninori Morimoto 
19010ced7b05SKuninori Morimoto 	if (card->snd_card)
19020ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
190353e947a0SKuninori Morimoto 
19042a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
19052a6f0892SKuninori Morimoto 
1906c7eb967dSCezary Rojewski 	/* release machine specific resources */
1907c7eb967dSCezary Rojewski 	for_each_card_rtds(card, rtd)
1908c7eb967dSCezary Rojewski 		snd_soc_link_exit(rtd);
190953e947a0SKuninori Morimoto 	/* remove and free each DAI */
19107ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19110ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
19127ce6088fSKuninori Morimoto 
1913cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
191450cd9b53SKuninori Morimoto 		snd_soc_remove_pcm_runtime(card, rtd);
19157ce6088fSKuninori Morimoto 
191653e947a0SKuninori Morimoto 	/* remove auxiliary devices */
191753e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1918e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
191953e947a0SKuninori Morimoto 
192053e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
192153e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
192253e947a0SKuninori Morimoto 
192353e947a0SKuninori Morimoto 	/* remove the card */
1924b0275d95SKuninori Morimoto 	snd_soc_card_remove(card);
19250ced7b05SKuninori Morimoto 
19260ced7b05SKuninori Morimoto 	if (card->snd_card) {
19270ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
19280ced7b05SKuninori Morimoto 		card->snd_card = NULL;
19290ced7b05SKuninori Morimoto 	}
193053e947a0SKuninori Morimoto }
193153e947a0SKuninori Morimoto 
19322cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
19332cc1afcfSKuninori Morimoto {
19349024bae4SKuninori Morimoto 	if (snd_soc_card_is_instantiated(card)) {
19352cc1afcfSKuninori Morimoto 		card->instantiated = false;
19362cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
19372cc1afcfSKuninori Morimoto 
193827f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
19392cc1afcfSKuninori Morimoto 		if (!unregister)
19402cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
19412cc1afcfSKuninori Morimoto 	} else {
19422cc1afcfSKuninori Morimoto 		if (unregister)
19432cc1afcfSKuninori Morimoto 			list_del(&card->list);
19442cc1afcfSKuninori Morimoto 	}
19452cc1afcfSKuninori Morimoto }
19462cc1afcfSKuninori Morimoto 
1947ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1948f0fba2adSLiam Girdwood {
19491a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
195076c39e86SKuninori Morimoto 	struct snd_soc_component *component;
1951ffaf886eSKuninori Morimoto 	int ret;
195296dd3622SMark Brown 
195334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
19540f3b8184SKuninori Morimoto 	snd_soc_card_mutex_lock_root(card);
1955f0fba2adSLiam Girdwood 
195695c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
195753e947a0SKuninori Morimoto 
1958a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1959a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1960a655de80SLiam Girdwood 
196144c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1962bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1963bee886f1SKuninori Morimoto 	if (ret < 0)
196453e947a0SKuninori Morimoto 		goto probe_end;
1965db2a4165SFrank Mandarino 
1966f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1967d8145989SKuninori Morimoto 	card->num_rtd = 0;
1968ffaf886eSKuninori Morimoto 	ret = snd_soc_add_pcm_runtimes(card, card->dai_link, card->num_links);
19695b99a0aaSKuninori Morimoto 	if (ret < 0)
19705b99a0aaSKuninori Morimoto 		goto probe_end;
1971f8f80361SMengdong Lin 
1972f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1973102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1974f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1975f0fba2adSLiam Girdwood 	if (ret < 0) {
197610e8aa9aSMichał Mirosław 		dev_err(card->dev,
197710e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
197810e8aa9aSMichał Mirosław 			card->name, ret);
197953e947a0SKuninori Morimoto 		goto probe_end;
1980db2a4165SFrank Mandarino 	}
1981db2a4165SFrank Mandarino 
19820757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19830757d834SLars-Peter Clausen 
1984b3da4251SKuninori Morimoto 	soc_resume_init(card);
19856ed25978SAndy Green 
1986b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19879a841ebbSMark Brown 					card->num_dapm_widgets);
1988b8ba3b57SKuninori Morimoto 	if (ret < 0)
1989b8ba3b57SKuninori Morimoto 		goto probe_end;
19909a841ebbSMark Brown 
1991b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1992f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
1993b8ba3b57SKuninori Morimoto 	if (ret < 0)
1994b8ba3b57SKuninori Morimoto 		goto probe_end;
1995f23e860eSNicolin Chen 
1996f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
199773de4b02SKuninori Morimoto 	ret = snd_soc_card_probe(card);
1998f0fba2adSLiam Girdwood 	if (ret < 0)
199953e947a0SKuninori Morimoto 		goto probe_end;
2000f0fba2adSLiam Girdwood 
200162ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
200262f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
200362ae68faSStephen Warren 	if (ret < 0) {
2004f09b6e96SJohan Hovold 		if (ret != -EPROBE_DEFER) {
2005f110bfc7SLiam Girdwood 			dev_err(card->dev,
200662f07a6bSKuninori Morimoto 				"ASoC: failed to instantiate card %d\n", ret);
2007f09b6e96SJohan Hovold 		}
200853e947a0SKuninori Morimoto 		goto probe_end;
200962ae68faSStephen Warren 	}
201062ae68faSStephen Warren 
2011f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2012f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
201374bd3f92SKuninori Morimoto 	if (ret < 0) {
201474bd3f92SKuninori Morimoto 		dev_err(card->dev,
201574bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
201653e947a0SKuninori Morimoto 		goto probe_end;
201774bd3f92SKuninori Morimoto 	}
2018f2ed6b07SMengdong Lin 
201962ae68faSStephen Warren 	/* probe all DAI links on this card */
2020c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2021fe3e78e0SMark Brown 	if (ret < 0) {
2022f110bfc7SLiam Girdwood 		dev_err(card->dev,
2023c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
202453e947a0SKuninori Morimoto 		goto probe_end;
2025fe3e78e0SMark Brown 	}
2026fe3e78e0SMark Brown 
2027626c2e57SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2028eaffeefbSKuninori Morimoto 		ret = soc_init_pcm_runtime(card, rtd);
2029626c2e57SKuninori Morimoto 		if (ret < 0)
2030626c2e57SKuninori Morimoto 			goto probe_end;
2031626c2e57SKuninori Morimoto 	}
2032c4b46982SKuninori Morimoto 
2033888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2034b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2035888df395SMark Brown 
20369b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20372c7b696aSMarcel Ziswiler 					card->num_controls);
20389b98c7c2SKuninori Morimoto 	if (ret < 0)
20399b98c7c2SKuninori Morimoto 		goto probe_end;
2040b7af1dafSMark Brown 
2041daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2042b8ad29deSMark Brown 				      card->num_dapm_routes);
2043a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
2044a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
2045a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
2046a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
2047a22ae72bSPierre-Louis Bossart 				 __func__);
2048a22ae72bSPierre-Louis Bossart 		} else {
2049a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
2050a22ae72bSPierre-Louis Bossart 				 "%s: snd_soc_dapm_add_routes failed: %d\n",
2051a22ae72bSPierre-Louis Bossart 				 __func__, ret);
2052daa480bdSKuninori Morimoto 			goto probe_end;
2053a22ae72bSPierre-Louis Bossart 		}
2054a22ae72bSPierre-Louis Bossart 	}
2055b8ad29deSMark Brown 
2056daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2057f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2058daa480bdSKuninori Morimoto 	if (ret < 0)
2059daa480bdSKuninori Morimoto 		goto probe_end;
206075d9ac46SMark Brown 
2061861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2062861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2063861886d3STakashi Iwai 
2064c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->shortname,
2065c8d18e44SJaroslav Kysela 			    card->name, NULL);
2066c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->longname,
2067c8d18e44SJaroslav Kysela 			    card->long_name, card->name);
2068c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->driver,
2069c8d18e44SJaroslav Kysela 			    card->driver_name, card->name);
2070fe3e78e0SMark Brown 
2071dc73d73aSJaroslav Kysela 	if (card->components) {
2072dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
2073dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
2074dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
2075dc73d73aSJaroslav Kysela 		/* not work correctly */
2076dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
2077dc73d73aSJaroslav Kysela 		if (ret < 0) {
2078dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2079dc73d73aSJaroslav Kysela 				card->name, ret);
2080dc73d73aSJaroslav Kysela 			goto probe_end;
2081dc73d73aSJaroslav Kysela 		}
2082dc73d73aSJaroslav Kysela 	}
2083dc73d73aSJaroslav Kysela 
20845c0eac03SKuninori Morimoto 	ret = snd_soc_card_late_probe(card);
20855c0eac03SKuninori Morimoto 	if (ret < 0)
208653e947a0SKuninori Morimoto 		goto probe_end;
208728e9ad92SMark Brown 
2088824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
2089df4d27b1SMartin Povišer 	snd_soc_card_fixup_controls(card);
20908c193b8dSLars-Peter Clausen 
2091f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2092fe3e78e0SMark Brown 	if (ret < 0) {
2093f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2094f110bfc7SLiam Girdwood 				ret);
209553e947a0SKuninori Morimoto 		goto probe_end;
2096fe3e78e0SMark Brown 	}
2097fe3e78e0SMark Brown 
2098435c5e25SMark Brown 	card->instantiated = 1;
2099882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21004f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2101b19e6e7bSMark Brown 
2102ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
210376c39e86SKuninori Morimoto 	for_each_card_components(card, component)
2104b3dea624SKuninori Morimoto 		if (!snd_soc_component_active(component))
210576c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
2106ed90c013SKuninori Morimoto 
210753e947a0SKuninori Morimoto probe_end:
210853e947a0SKuninori Morimoto 	if (ret < 0)
210927f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
2110db2a4165SFrank Mandarino 
21110f3b8184SKuninori Morimoto 	snd_soc_card_mutex_unlock(card);
211234e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2113db2a4165SFrank Mandarino 
2114b19e6e7bSMark Brown 	return ret;
2115435c5e25SMark Brown }
2116435c5e25SMark Brown 
2117435c5e25SMark Brown /* probes a new socdev */
2118435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2119435c5e25SMark Brown {
2120f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2121435c5e25SMark Brown 
212270a7ca34SVinod Koul 	/*
212370a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
212470a7ca34SVinod Koul 	 * we should not be here in that case so ret error
212570a7ca34SVinod Koul 	 */
212670a7ca34SVinod Koul 	if (!card)
212770a7ca34SVinod Koul 		return -EINVAL;
212870a7ca34SVinod Koul 
2129fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2130f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2131fe4085e8SMark Brown 		 card->name);
2132fe4085e8SMark Brown 
2133435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2134435c5e25SMark Brown 	card->dev = &pdev->dev;
2135f0fba2adSLiam Girdwood 
2136ad61b78eSQinglang Miao 	return devm_snd_soc_register_card(&pdev->dev, card);
2137db2a4165SFrank Mandarino }
2138db2a4165SFrank Mandarino 
21396f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
214051737470SMark Brown {
21416f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
214276c39e86SKuninori Morimoto 	struct snd_soc_component *component;
214351737470SMark Brown 
21449024bae4SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card))
2145416356fcSMark Brown 		return 0;
214651737470SMark Brown 
21472c7b696aSMarcel Ziswiler 	/*
21482c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21492c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21502c7b696aSMarcel Ziswiler 	 */
215165462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
215251737470SMark Brown 
2153f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2154416356fcSMark Brown 
2155988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
215676c39e86SKuninori Morimoto 	for_each_card_components(card, component)
215776c39e86SKuninori Morimoto 		pinctrl_pm_select_sleep_state(component->dev);
2158988e8cc4SNicolin Chen 
2159416356fcSMark Brown 	return 0;
216051737470SMark Brown }
21616f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
216251737470SMark Brown 
21636f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2164b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2165b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2166b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2167b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21686f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2169b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2170416356fcSMark Brown };
2171deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2172416356fcSMark Brown 
2173db2a4165SFrank Mandarino /* ASoC platform driver */
2174db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2175db2a4165SFrank Mandarino 	.driver		= {
2176db2a4165SFrank Mandarino 		.name		= "soc-audio",
21776f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2178db2a4165SFrank Mandarino 	},
2179db2a4165SFrank Mandarino 	.probe		= soc_probe,
2180db2a4165SFrank Mandarino };
2181db2a4165SFrank Mandarino 
2182096e49d5SMark Brown /**
2183db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2184db2a4165SFrank Mandarino  * @_template: control template
2185db2a4165SFrank Mandarino  * @data: control private data
2186ac11a2b3SMark Brown  * @long_name: control long name
2187efb7ac3fSMark Brown  * @prefix: control name prefix
2188db2a4165SFrank Mandarino  *
2189db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2190db2a4165SFrank Mandarino  *
2191db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2192db2a4165SFrank Mandarino  */
2193db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21943056557fSMark Brown 				  void *data, const char *long_name,
2195efb7ac3fSMark Brown 				  const char *prefix)
2196db2a4165SFrank Mandarino {
2197db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2198efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2199efb7ac3fSMark Brown 	char *name = NULL;
2200db2a4165SFrank Mandarino 
2201db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2202db2a4165SFrank Mandarino 	template.index = 0;
2203db2a4165SFrank Mandarino 
2204efb7ac3fSMark Brown 	if (!long_name)
2205efb7ac3fSMark Brown 		long_name = template.name;
2206efb7ac3fSMark Brown 
2207efb7ac3fSMark Brown 	if (prefix) {
22082b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2209efb7ac3fSMark Brown 		if (!name)
2210efb7ac3fSMark Brown 			return NULL;
2211efb7ac3fSMark Brown 
2212efb7ac3fSMark Brown 		template.name = name;
2213efb7ac3fSMark Brown 	} else {
2214efb7ac3fSMark Brown 		template.name = long_name;
2215efb7ac3fSMark Brown 	}
2216efb7ac3fSMark Brown 
2217efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2218efb7ac3fSMark Brown 
2219efb7ac3fSMark Brown 	kfree(name);
2220efb7ac3fSMark Brown 
2221efb7ac3fSMark Brown 	return kcontrol;
2222db2a4165SFrank Mandarino }
2223db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2224db2a4165SFrank Mandarino 
2225022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2226022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2227022658beSLiam Girdwood 	const char *prefix, void *data)
2228022658beSLiam Girdwood {
2229eaf2469cSKuninori Morimoto 	int i;
2230022658beSLiam Girdwood 
2231022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2232022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2233eaf2469cSKuninori Morimoto 		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2234022658beSLiam Girdwood 							 control->name, prefix));
2235022658beSLiam Girdwood 		if (err < 0) {
2236f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2237f110bfc7SLiam Girdwood 				control->name, err);
2238022658beSLiam Girdwood 			return err;
2239022658beSLiam Girdwood 		}
2240022658beSLiam Girdwood 	}
2241022658beSLiam Girdwood 
2242022658beSLiam Girdwood 	return 0;
2243022658beSLiam Girdwood }
2244022658beSLiam Girdwood 
2245db2a4165SFrank Mandarino /**
22460f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22470f2780adSLars-Peter Clausen  *
22480f2780adSLars-Peter Clausen  * @component: Component to add controls to
22490f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22500f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22510f2780adSLars-Peter Clausen  *
22520f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22530f2780adSLars-Peter Clausen  */
22540f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22550f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22560f2780adSLars-Peter Clausen {
22570f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22580f2780adSLars-Peter Clausen 
22590f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22600f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22610f2780adSLars-Peter Clausen }
22620f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22630f2780adSLars-Peter Clausen 
22640f2780adSLars-Peter Clausen /**
2265022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2266022658beSLiam Girdwood  * Convenience function to add a list of controls.
2267022658beSLiam Girdwood  *
2268022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2269022658beSLiam Girdwood  * @controls: array of controls to add
2270022658beSLiam Girdwood  * @num_controls: number of elements in the array
2271022658beSLiam Girdwood  *
2272022658beSLiam Girdwood  * Return 0 for success, else error.
2273022658beSLiam Girdwood  */
2274022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2275022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2276022658beSLiam Girdwood {
2277022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2278022658beSLiam Girdwood 
2279022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2280022658beSLiam Girdwood 			NULL, soc_card);
2281022658beSLiam Girdwood }
2282022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2283022658beSLiam Girdwood 
2284022658beSLiam Girdwood /**
2285022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2286022658beSLiam Girdwood  * Convienience function to add a list of controls.
2287022658beSLiam Girdwood  *
2288022658beSLiam Girdwood  * @dai: DAI to add controls to
2289022658beSLiam Girdwood  * @controls: array of controls to add
2290022658beSLiam Girdwood  * @num_controls: number of elements in the array
2291022658beSLiam Girdwood  *
2292022658beSLiam Girdwood  * Return 0 for success, else error.
2293022658beSLiam Girdwood  */
2294022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2295022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2296022658beSLiam Girdwood {
2297313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2298022658beSLiam Girdwood 
2299022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2300022658beSLiam Girdwood 			NULL, dai);
2301022658beSLiam Girdwood }
2302022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2303022658beSLiam Girdwood 
2304c5af3a2eSMark Brown /**
2305c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2306c5af3a2eSMark Brown  *
2307ac11a2b3SMark Brown  * @card: Card to register
2308c5af3a2eSMark Brown  *
2309c5af3a2eSMark Brown  */
231070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2311c5af3a2eSMark Brown {
2312c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2313c5af3a2eSMark Brown 		return -EINVAL;
2314c5af3a2eSMark Brown 
2315ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2316ed77cc12SMark Brown 
2317b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2318b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2319b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2320b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2321b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2322b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
23231a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2324db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23258a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2326b03bfaecSKuninori Morimoto 
2327c5af3a2eSMark Brown 	card->instantiated = 0;
2328f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2329a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
233072b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2331c5af3a2eSMark Brown 
2332e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2333c5af3a2eSMark Brown }
233470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2335c5af3a2eSMark Brown 
2336c5af3a2eSMark Brown /**
2337c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2338c5af3a2eSMark Brown  *
2339ac11a2b3SMark Brown  * @card: Card to unregister
2340c5af3a2eSMark Brown  *
2341c5af3a2eSMark Brown  */
23421892a991SUwe Kleine-König void snd_soc_unregister_card(struct snd_soc_card *card)
2343c5af3a2eSMark Brown {
2344b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2345e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2346b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2347f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2348c5af3a2eSMark Brown }
234970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2350c5af3a2eSMark Brown 
2351f0fba2adSLiam Girdwood /*
2352f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2353f0fba2adSLiam Girdwood  * and sanitizing names.
2354f0fba2adSLiam Girdwood  */
23550b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2356f0fba2adSLiam Girdwood {
235745dd9943SDmitry Baryshkov 	const char *devname = dev_name(dev);
235845dd9943SDmitry Baryshkov 	char *found, *name;
2359d3b47a88SPierre-Louis Bossart 	unsigned int id1, id2;
2360f0fba2adSLiam Girdwood 
236145dd9943SDmitry Baryshkov 	if (devname == NULL)
2362f0fba2adSLiam Girdwood 		return NULL;
2363f0fba2adSLiam Girdwood 
236445dd9943SDmitry Baryshkov 	name = devm_kstrdup(dev, devname, GFP_KERNEL);
236541daf6baSKefeng Wang 	if (!name)
236641daf6baSKefeng Wang 		return NULL;
2367f0fba2adSLiam Girdwood 
2368f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2369f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2370f0fba2adSLiam Girdwood 	if (found) {
2371f0fba2adSLiam Girdwood 		/* get ID */
2372f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2373f0fba2adSLiam Girdwood 
2374f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2375f0fba2adSLiam Girdwood 			if (*id == -1)
2376f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2377f0fba2adSLiam Girdwood 		}
2378f0fba2adSLiam Girdwood 
2379f0fba2adSLiam Girdwood 	/* I2C component devices are named "bus-addr" */
238045dd9943SDmitry Baryshkov 	} else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2381f0fba2adSLiam Girdwood 
2382f0fba2adSLiam Girdwood 		/* create unique ID number from I2C addr and bus */
238305899446SJarkko Nikula 		*id = ((id1 & 0xffff) << 16) + id2;
2384f0fba2adSLiam Girdwood 
238545dd9943SDmitry Baryshkov 		devm_kfree(dev, name);
238645dd9943SDmitry Baryshkov 
2387f0fba2adSLiam Girdwood 		/* sanitize component name for DAI link creation */
238845dd9943SDmitry Baryshkov 		name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
238945dd9943SDmitry Baryshkov 	} else {
2390f0fba2adSLiam Girdwood 		*id = 0;
2391f0fba2adSLiam Girdwood 	}
2392f0fba2adSLiam Girdwood 
239345dd9943SDmitry Baryshkov 	return name;
2394f0fba2adSLiam Girdwood }
2395f0fba2adSLiam Girdwood 
2396f0fba2adSLiam Girdwood /*
2397f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2398f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2399f0fba2adSLiam Girdwood  */
2400f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2401f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2402f0fba2adSLiam Girdwood {
2403f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
240410e8aa9aSMichał Mirosław 		dev_err(dev,
240510e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
240610e8aa9aSMichał Mirosław 			dev_name(dev));
2407f0fba2adSLiam Girdwood 		return NULL;
2408f0fba2adSLiam Girdwood 	}
2409f0fba2adSLiam Girdwood 
241050014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2411f0fba2adSLiam Girdwood }
2412f0fba2adSLiam Girdwood 
2413ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2414e11381f3SKuninori Morimoto {
2415e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2416e11381f3SKuninori Morimoto 	list_del(&dai->list);
2417e11381f3SKuninori Morimoto }
2418ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2419e11381f3SKuninori Morimoto 
24207ca24386SKuninori Morimoto /**
24217ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
24227ca24386SKuninori Morimoto  *
24237ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
24247ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2425bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2426bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
24277ca24386SKuninori Morimoto  *
24287ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
24297ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
24307ca24386SKuninori Morimoto  * will be freed in the component cleanup.
24317ca24386SKuninori Morimoto  */
24327ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
24335e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
24345e4fb372SMengdong Lin 					 bool legacy_dai_naming)
24355e4fb372SMengdong Lin {
24365e4fb372SMengdong Lin 	struct device *dev = component->dev;
24375e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
24385e4fb372SMengdong Lin 
24397ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
24407ca24386SKuninori Morimoto 
244150014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
24425e4fb372SMengdong Lin 	if (dai == NULL)
24435e4fb372SMengdong Lin 		return NULL;
24445e4fb372SMengdong Lin 
24455e4fb372SMengdong Lin 	/*
24465e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
24475e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
24485e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
24495e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
24505e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
24515e4fb372SMengdong Lin 	 * component-less anymore.
24525e4fb372SMengdong Lin 	 */
24535e4fb372SMengdong Lin 	if (legacy_dai_naming &&
24545e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
24555e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
24565e4fb372SMengdong Lin 	} else {
24575e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
24585e4fb372SMengdong Lin 		if (dai_drv->id)
24595e4fb372SMengdong Lin 			dai->id = dai_drv->id;
24605e4fb372SMengdong Lin 		else
24615e4fb372SMengdong Lin 			dai->id = component->num_dai;
24625e4fb372SMengdong Lin 	}
246350014499SKuninori Morimoto 	if (!dai->name)
24645e4fb372SMengdong Lin 		return NULL;
24655e4fb372SMengdong Lin 
24665e4fb372SMengdong Lin 	dai->component = component;
24675e4fb372SMengdong Lin 	dai->dev = dev;
24685e4fb372SMengdong Lin 	dai->driver = dai_drv;
24695e4fb372SMengdong Lin 
247015a0c645SKuninori Morimoto 	/* see for_each_component_dais */
247158bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
24725e4fb372SMengdong Lin 	component->num_dai++;
24735e4fb372SMengdong Lin 
24745e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
24755e4fb372SMengdong Lin 	return dai;
24765e4fb372SMengdong Lin }
2477da039809SCezary Rojewski EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2478e11381f3SKuninori Morimoto 
24799115171aSMark Brown /**
2480f7b6603cSMauro Carvalho Chehab  * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
24813f6674aeSKuninori Morimoto  *
24823f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
24833f6674aeSKuninori Morimoto  */
24843f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
24853f6674aeSKuninori Morimoto {
24863f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
24873f6674aeSKuninori Morimoto 
2488e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2489e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
24903f6674aeSKuninori Morimoto }
24913f6674aeSKuninori Morimoto 
24923f6674aeSKuninori Morimoto /**
2493daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2494daf77373SKuninori Morimoto  *
2495daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2496daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2497daf77373SKuninori Morimoto  * @count: Number of DAIs
2498daf77373SKuninori Morimoto  */
2499daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2500daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2501daf77373SKuninori Morimoto 				 size_t count)
2502daf77373SKuninori Morimoto {
2503daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2504daf77373SKuninori Morimoto 	unsigned int i;
2505daf77373SKuninori Morimoto 	int ret;
2506daf77373SKuninori Morimoto 
2507daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
250871cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2509129f055aSCharles Keepax 					   component->driver->legacy_dai_naming);
2510daf77373SKuninori Morimoto 		if (dai == NULL) {
2511daf77373SKuninori Morimoto 			ret = -ENOMEM;
2512daf77373SKuninori Morimoto 			goto err;
2513daf77373SKuninori Morimoto 		}
2514daf77373SKuninori Morimoto 	}
2515daf77373SKuninori Morimoto 
2516daf77373SKuninori Morimoto 	return 0;
2517daf77373SKuninori Morimoto 
2518daf77373SKuninori Morimoto err:
2519daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2520daf77373SKuninori Morimoto 
2521daf77373SKuninori Morimoto 	return ret;
2522daf77373SKuninori Morimoto }
2523daf77373SKuninori Morimoto 
2524273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2525273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2526273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2527273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2528273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2529273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2530273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2531273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2532273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2533273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2534273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2535273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2536273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2537273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2538273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2539273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2540273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2541273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2542273d778eSKuninori Morimoto };
2543273d778eSKuninori Morimoto 
2544273d778eSKuninori Morimoto /*
2545273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2546273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2547273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2548273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2549273d778eSKuninori Morimoto  */
2550273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2551273d778eSKuninori Morimoto {
2552273d778eSKuninori Morimoto 	int i;
2553273d778eSKuninori Morimoto 
2554273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2555273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2556273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2557273d778eSKuninori Morimoto }
2558273d778eSKuninori Morimoto 
2559e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2560e894efefSSrinivas Kandagatla {
2561e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2562e894efefSSrinivas Kandagatla 
2563b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2564e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2565e894efefSSrinivas Kandagatla 			list_del(&card->list);
2566e894efefSSrinivas Kandagatla }
2567e894efefSSrinivas Kandagatla 
2568486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2569486c7978SKuninori Morimoto {
2570b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2571b18768f5SKuninori Morimoto 
2572486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2573b18768f5SKuninori Morimoto 
2574b18768f5SKuninori Morimoto 	if (card)
2575b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2576b18768f5SKuninori Morimoto 
2577b18768f5SKuninori Morimoto 	list_del(&component->list);
2578486c7978SKuninori Morimoto }
2579486c7978SKuninori Morimoto 
258008ff7209SCezary Rojewski int snd_soc_component_initialize(struct snd_soc_component *component,
258108ff7209SCezary Rojewski 				 const struct snd_soc_component_driver *driver,
25827274d4cdSCezary Rojewski 				 struct device *dev)
258308ff7209SCezary Rojewski {
258408ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->dai_list);
258508ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->dobj_list);
258608ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->card_list);
2587c448b7aaSYang Yingliang 	INIT_LIST_HEAD(&component->list);
258808ff7209SCezary Rojewski 	mutex_init(&component->io_mutex);
258908ff7209SCezary Rojewski 
25907274d4cdSCezary Rojewski 	component->name = fmt_single_name(dev, &component->id);
25917274d4cdSCezary Rojewski 	if (!component->name) {
25927274d4cdSCezary Rojewski 		dev_err(dev, "ASoC: Failed to allocate name\n");
25937274d4cdSCezary Rojewski 		return -ENOMEM;
25947274d4cdSCezary Rojewski 	}
25957274d4cdSCezary Rojewski 
259608ff7209SCezary Rojewski 	component->dev		= dev;
259708ff7209SCezary Rojewski 	component->driver	= driver;
259808ff7209SCezary Rojewski 
2599d462f6edSHeiner Kallweit #ifdef CONFIG_DEBUG_FS
2600d462f6edSHeiner Kallweit 	if (!component->debugfs_prefix)
2601d462f6edSHeiner Kallweit 		component->debugfs_prefix = driver->debugfs_prefix;
2602d462f6edSHeiner Kallweit #endif
2603d462f6edSHeiner Kallweit 
260408ff7209SCezary Rojewski 	return 0;
260508ff7209SCezary Rojewski }
260608ff7209SCezary Rojewski EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
260708ff7209SCezary Rojewski 
2608ea029dd8SCezary Rojewski int snd_soc_add_component(struct snd_soc_component *component,
2609d191bd8dSKuninori Morimoto 			  struct snd_soc_dai_driver *dai_drv,
2610d191bd8dSKuninori Morimoto 			  int num_dai)
2611d191bd8dSKuninori Morimoto {
2612bb13109dSLars-Peter Clausen 	int ret;
2613273d778eSKuninori Morimoto 	int i;
2614d191bd8dSKuninori Morimoto 
2615b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2616b18768f5SKuninori Morimoto 
2617ea029dd8SCezary Rojewski 	if (component->driver->endianness) {
2618273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2619273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2620273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2621273d778eSKuninori Morimoto 		}
2622273d778eSKuninori Morimoto 	}
2623273d778eSKuninori Morimoto 
26240e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2625bb13109dSLars-Peter Clausen 	if (ret < 0) {
2626ea029dd8SCezary Rojewski 		dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2627ea029dd8SCezary Rojewski 			ret);
2628bb13109dSLars-Peter Clausen 		goto err_cleanup;
2629bb13109dSLars-Peter Clausen 	}
2630bb13109dSLars-Peter Clausen 
2631b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2632b18768f5SKuninori Morimoto 		if (!component->regmap)
2633b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2634b18768f5SKuninori Morimoto 							   NULL);
2635b18768f5SKuninori Morimoto 		if (component->regmap)
2636b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2637b18768f5SKuninori Morimoto 	}
2638bb13109dSLars-Peter Clausen 
2639b18768f5SKuninori Morimoto 	/* see for_each_component */
2640b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2641bb13109dSLars-Peter Clausen 
2642bb13109dSLars-Peter Clausen err_cleanup:
2643b18768f5SKuninori Morimoto 	if (ret < 0)
2644486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2645ea029dd8SCezary Rojewski 
2646b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2647b18768f5SKuninori Morimoto 
2648b18768f5SKuninori Morimoto 	if (ret == 0)
2649b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2650b18768f5SKuninori Morimoto 
2651bb13109dSLars-Peter Clausen 	return ret;
2652d191bd8dSKuninori Morimoto }
2653e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2654e0dac41bSKuninori Morimoto 
2655e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2656e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2657e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2658e0dac41bSKuninori Morimoto 			int num_dai)
2659e0dac41bSKuninori Morimoto {
2660e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2661ea029dd8SCezary Rojewski 	int ret;
2662e0dac41bSKuninori Morimoto 
26637ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
266408e61d03SKuninori Morimoto 	if (!component)
2665e0dac41bSKuninori Morimoto 		return -ENOMEM;
2666e0dac41bSKuninori Morimoto 
2667ea029dd8SCezary Rojewski 	ret = snd_soc_component_initialize(component, component_driver, dev);
2668ea029dd8SCezary Rojewski 	if (ret < 0)
2669ea029dd8SCezary Rojewski 		return ret;
2670ea029dd8SCezary Rojewski 
2671ea029dd8SCezary Rojewski 	return snd_soc_add_component(component, dai_drv, num_dai);
2672e0dac41bSKuninori Morimoto }
2673d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2674d191bd8dSKuninori Morimoto 
2675d191bd8dSKuninori Morimoto /**
267658f30150SMaxime Ripard  * snd_soc_unregister_component_by_driver - Unregister component using a given driver
267758f30150SMaxime Ripard  * from the ASoC core
267858f30150SMaxime Ripard  *
267958f30150SMaxime Ripard  * @dev: The device to unregister
268058f30150SMaxime Ripard  * @component_driver: The component driver to unregister
268158f30150SMaxime Ripard  */
268258f30150SMaxime Ripard void snd_soc_unregister_component_by_driver(struct device *dev,
268358f30150SMaxime Ripard 					    const struct snd_soc_component_driver *component_driver)
268458f30150SMaxime Ripard {
268558f30150SMaxime Ripard 	struct snd_soc_component *component;
268658f30150SMaxime Ripard 
268758f30150SMaxime Ripard 	if (!component_driver)
268858f30150SMaxime Ripard 		return;
268958f30150SMaxime Ripard 
269058f30150SMaxime Ripard 	mutex_lock(&client_mutex);
269158f30150SMaxime Ripard 	component = snd_soc_lookup_component_nolocked(dev, component_driver->name);
269258f30150SMaxime Ripard 	if (!component)
269358f30150SMaxime Ripard 		goto out;
269458f30150SMaxime Ripard 
269558f30150SMaxime Ripard 	snd_soc_del_component_unlocked(component);
269658f30150SMaxime Ripard 
269758f30150SMaxime Ripard out:
269858f30150SMaxime Ripard 	mutex_unlock(&client_mutex);
269958f30150SMaxime Ripard }
270058f30150SMaxime Ripard EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
270158f30150SMaxime Ripard 
270258f30150SMaxime Ripard /**
27032eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
27042eccea8cSKuninori Morimoto  * from the ASoC core
2705d191bd8dSKuninori Morimoto  *
2706628536eaSJonathan Corbet  * @dev: The device to unregister
2707d191bd8dSKuninori Morimoto  */
27082eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
27092eccea8cSKuninori Morimoto {
2710ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2711ac6a4dd3SKuninori Morimoto 	while (1) {
27125600f3d5SKuninori Morimoto 		struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, NULL);
27135600f3d5SKuninori Morimoto 
2714ac6a4dd3SKuninori Morimoto 		if (!component)
2715ac6a4dd3SKuninori Morimoto 			break;
2716ac6a4dd3SKuninori Morimoto 
2717ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2718ac6a4dd3SKuninori Morimoto 	}
2719ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2720d191bd8dSKuninori Morimoto }
2721d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2722d191bd8dSKuninori Morimoto 
2723bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2724b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2725bec4fa05SStephen Warren 			       const char *propname)
2726bec4fa05SStephen Warren {
2727b07609ceSKuninori Morimoto 	struct device_node *np;
2728bec4fa05SStephen Warren 	int ret;
2729bec4fa05SStephen Warren 
27307e07e7c0STushar Behera 	if (!card->dev) {
27317e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
27327e07e7c0STushar Behera 		return -EINVAL;
27337e07e7c0STushar Behera 	}
27347e07e7c0STushar Behera 
27357e07e7c0STushar Behera 	np = card->dev->of_node;
27367e07e7c0STushar Behera 
2737bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2738bec4fa05SStephen Warren 	/*
2739bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2740bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2741bec4fa05SStephen Warren 	 * snd_soc_register_card.
2742bec4fa05SStephen Warren 	 */
2743bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2744bec4fa05SStephen Warren 		dev_err(card->dev,
2745f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2746bec4fa05SStephen Warren 			propname, ret);
2747bec4fa05SStephen Warren 		return ret;
2748bec4fa05SStephen Warren 	}
2749bec4fa05SStephen Warren 
2750bec4fa05SStephen Warren 	return 0;
2751bec4fa05SStephen Warren }
2752b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2753bec4fa05SStephen Warren 
27549a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
27559a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
27569a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
27579a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
27589a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
27599a6d4860SXiubo Li };
27609a6d4860SXiubo Li 
276121efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
27629a6d4860SXiubo Li 					  const char *propname)
27639a6d4860SXiubo Li {
276421efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
27659a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
27669a6d4860SXiubo Li 	const char *template, *wname;
276799c68653SKuninori Morimoto 	int i, j, num_widgets;
27689a6d4860SXiubo Li 
27699a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
27709a6d4860SXiubo Li 	if (num_widgets < 0) {
27719a6d4860SXiubo Li 		dev_err(card->dev,
27729a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
27739a6d4860SXiubo Li 		return -EINVAL;
27749a6d4860SXiubo Li 	}
27758ad0b83eSPierre-Louis Bossart 	if (!num_widgets) {
27768ad0b83eSPierre-Louis Bossart 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
27778ad0b83eSPierre-Louis Bossart 			propname);
27788ad0b83eSPierre-Louis Bossart 		return -EINVAL;
27798ad0b83eSPierre-Louis Bossart 	}
27809a6d4860SXiubo Li 	if (num_widgets & 1) {
27819a6d4860SXiubo Li 		dev_err(card->dev,
27829a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
27839a6d4860SXiubo Li 		return -EINVAL;
27849a6d4860SXiubo Li 	}
27859a6d4860SXiubo Li 
27869a6d4860SXiubo Li 	num_widgets /= 2;
27879a6d4860SXiubo Li 
27889a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
27899a6d4860SXiubo Li 			       GFP_KERNEL);
27909a6d4860SXiubo Li 	if (!widgets) {
27919a6d4860SXiubo Li 		dev_err(card->dev,
27929a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
27939a6d4860SXiubo Li 		return -ENOMEM;
27949a6d4860SXiubo Li 	}
27959a6d4860SXiubo Li 
27969a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
279799c68653SKuninori Morimoto 		int ret = of_property_read_string_index(np, propname,
27989a6d4860SXiubo Li 							2 * i, &template);
27999a6d4860SXiubo Li 		if (ret) {
28009a6d4860SXiubo Li 			dev_err(card->dev,
28019a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28029a6d4860SXiubo Li 				propname, 2 * i, ret);
28039a6d4860SXiubo Li 			return -EINVAL;
28049a6d4860SXiubo Li 		}
28059a6d4860SXiubo Li 
28069a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
28079a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
28089a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
28099a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
28109a6d4860SXiubo Li 				break;
28119a6d4860SXiubo Li 			}
28129a6d4860SXiubo Li 		}
28139a6d4860SXiubo Li 
28149a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
28159a6d4860SXiubo Li 			dev_err(card->dev,
28169a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
28179a6d4860SXiubo Li 				template);
28189a6d4860SXiubo Li 			return -EINVAL;
28199a6d4860SXiubo Li 		}
28209a6d4860SXiubo Li 
28219a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
28229a6d4860SXiubo Li 						    (2 * i) + 1,
28239a6d4860SXiubo Li 						    &wname);
28249a6d4860SXiubo Li 		if (ret) {
28259a6d4860SXiubo Li 			dev_err(card->dev,
28269a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28279a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
28289a6d4860SXiubo Li 			return -EINVAL;
28299a6d4860SXiubo Li 		}
28309a6d4860SXiubo Li 
28319a6d4860SXiubo Li 		widgets[i].name = wname;
28329a6d4860SXiubo Li 	}
28339a6d4860SXiubo Li 
2834f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2835f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
28369a6d4860SXiubo Li 
28379a6d4860SXiubo Li 	return 0;
28389a6d4860SXiubo Li }
283921efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
28409a6d4860SXiubo Li 
28413d4641a4SStephan Gerhold int snd_soc_of_parse_pin_switches(struct snd_soc_card *card, const char *prop)
28423d4641a4SStephan Gerhold {
28433d4641a4SStephan Gerhold 	const unsigned int nb_controls_max = 16;
28443d4641a4SStephan Gerhold 	const char **strings, *control_name;
28453d4641a4SStephan Gerhold 	struct snd_kcontrol_new *controls;
28463d4641a4SStephan Gerhold 	struct device *dev = card->dev;
28473d4641a4SStephan Gerhold 	unsigned int i, nb_controls;
28483d4641a4SStephan Gerhold 	int ret;
28493d4641a4SStephan Gerhold 
28503d4641a4SStephan Gerhold 	if (!of_property_read_bool(dev->of_node, prop))
28513d4641a4SStephan Gerhold 		return 0;
28523d4641a4SStephan Gerhold 
28533d4641a4SStephan Gerhold 	strings = devm_kcalloc(dev, nb_controls_max,
28543d4641a4SStephan Gerhold 			       sizeof(*strings), GFP_KERNEL);
28553d4641a4SStephan Gerhold 	if (!strings)
28563d4641a4SStephan Gerhold 		return -ENOMEM;
28573d4641a4SStephan Gerhold 
28583d4641a4SStephan Gerhold 	ret = of_property_read_string_array(dev->of_node, prop,
28593d4641a4SStephan Gerhold 					    strings, nb_controls_max);
28603d4641a4SStephan Gerhold 	if (ret < 0)
28613d4641a4SStephan Gerhold 		return ret;
28623d4641a4SStephan Gerhold 
28633d4641a4SStephan Gerhold 	nb_controls = (unsigned int)ret;
28643d4641a4SStephan Gerhold 
28653d4641a4SStephan Gerhold 	controls = devm_kcalloc(dev, nb_controls,
28663d4641a4SStephan Gerhold 				sizeof(*controls), GFP_KERNEL);
28673d4641a4SStephan Gerhold 	if (!controls)
28683d4641a4SStephan Gerhold 		return -ENOMEM;
28693d4641a4SStephan Gerhold 
28703d4641a4SStephan Gerhold 	for (i = 0; i < nb_controls; i++) {
28713d4641a4SStephan Gerhold 		control_name = devm_kasprintf(dev, GFP_KERNEL,
28723d4641a4SStephan Gerhold 					      "%s Switch", strings[i]);
28733d4641a4SStephan Gerhold 		if (!control_name)
28743d4641a4SStephan Gerhold 			return -ENOMEM;
28753d4641a4SStephan Gerhold 
28763d4641a4SStephan Gerhold 		controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
28773d4641a4SStephan Gerhold 		controls[i].name = control_name;
28783d4641a4SStephan Gerhold 		controls[i].info = snd_soc_dapm_info_pin_switch;
28793d4641a4SStephan Gerhold 		controls[i].get = snd_soc_dapm_get_pin_switch;
28803d4641a4SStephan Gerhold 		controls[i].put = snd_soc_dapm_put_pin_switch;
28813d4641a4SStephan Gerhold 		controls[i].private_value = (unsigned long)strings[i];
28823d4641a4SStephan Gerhold 	}
28833d4641a4SStephan Gerhold 
28843d4641a4SStephan Gerhold 	card->controls = controls;
28853d4641a4SStephan Gerhold 	card->num_controls = nb_controls;
28863d4641a4SStephan Gerhold 
28873d4641a4SStephan Gerhold 	return 0;
28883d4641a4SStephan Gerhold }
28893d4641a4SStephan Gerhold EXPORT_SYMBOL_GPL(snd_soc_of_parse_pin_switches);
28903d4641a4SStephan Gerhold 
2891cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
28926131084aSJyri Sarha 			     const char *prop_name,
28936131084aSJyri Sarha 			     unsigned int *mask)
28946131084aSJyri Sarha {
28956131084aSJyri Sarha 	u32 val;
28966c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
28976131084aSJyri Sarha 	int i;
28986131084aSJyri Sarha 
28996131084aSJyri Sarha 	if (!of_slot_mask)
29006131084aSJyri Sarha 		return 0;
29016131084aSJyri Sarha 	val /= sizeof(u32);
29026131084aSJyri Sarha 	for (i = 0; i < val; i++)
29036131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
29046131084aSJyri Sarha 			*mask |= (1 << i);
29056131084aSJyri Sarha 
29066131084aSJyri Sarha 	return val;
29076131084aSJyri Sarha }
2908cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
29096131084aSJyri Sarha 
291089c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
29116131084aSJyri Sarha 			      unsigned int *tx_mask,
29126131084aSJyri Sarha 			      unsigned int *rx_mask,
291389c67857SXiubo Li 			      unsigned int *slots,
291489c67857SXiubo Li 			      unsigned int *slot_width)
291589c67857SXiubo Li {
291689c67857SXiubo Li 	u32 val;
291789c67857SXiubo Li 	int ret;
291889c67857SXiubo Li 
29196131084aSJyri Sarha 	if (tx_mask)
29206131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
29216131084aSJyri Sarha 	if (rx_mask)
29226131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
29236131084aSJyri Sarha 
292489c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
292589c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
292689c67857SXiubo Li 		if (ret)
292789c67857SXiubo Li 			return ret;
292889c67857SXiubo Li 
292989c67857SXiubo Li 		if (slots)
293089c67857SXiubo Li 			*slots = val;
293189c67857SXiubo Li 	}
293289c67857SXiubo Li 
293389c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
293489c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
293589c67857SXiubo Li 		if (ret)
293689c67857SXiubo Li 			return ret;
293789c67857SXiubo Li 
293889c67857SXiubo Li 		if (slot_width)
293989c67857SXiubo Li 			*slot_width = val;
294089c67857SXiubo Li 	}
294189c67857SXiubo Li 
294289c67857SXiubo Li 	return 0;
294389c67857SXiubo Li }
294489c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
294589c67857SXiubo Li 
29463b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
29475e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
29485e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
29495e3cdaa2SKuninori Morimoto 				  const char *propname)
29505e3cdaa2SKuninori Morimoto {
29515e3cdaa2SKuninori Morimoto 	const char *str;
29525e3cdaa2SKuninori Morimoto 	int ret;
29535e3cdaa2SKuninori Morimoto 
29545e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
29555e3cdaa2SKuninori Morimoto 	if (ret < 0) {
29565e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
29575e3cdaa2SKuninori Morimoto 		return;
29585e3cdaa2SKuninori Morimoto 	}
29595e3cdaa2SKuninori Morimoto 
2960c13493a2SKuninori Morimoto 	codec_conf->dlc.of_node	= of_node;
29615e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
29625e3cdaa2SKuninori Morimoto }
29633b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
29645e3cdaa2SKuninori Morimoto 
29652bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2966a4a54dd5SStephen Warren 				   const char *propname)
2967a4a54dd5SStephen Warren {
29682bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
2969e3b1e6a1SMark Brown 	int num_routes;
2970a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
297151a3dd58SKuninori Morimoto 	int i;
2972a4a54dd5SStephen Warren 
2973a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
2974c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
297510e8aa9aSMichał Mirosław 		dev_err(card->dev,
297610e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
297710e8aa9aSMichał Mirosław 			propname);
2978a4a54dd5SStephen Warren 		return -EINVAL;
2979a4a54dd5SStephen Warren 	}
2980a4a54dd5SStephen Warren 	num_routes /= 2;
2981a4a54dd5SStephen Warren 
2982a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2983a4a54dd5SStephen Warren 			      GFP_KERNEL);
2984a4a54dd5SStephen Warren 	if (!routes) {
2985a4a54dd5SStephen Warren 		dev_err(card->dev,
2986f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
29877d3865a1SZhen Lei 		return -ENOMEM;
2988a4a54dd5SStephen Warren 	}
2989a4a54dd5SStephen Warren 
2990a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
299151a3dd58SKuninori Morimoto 		int ret = of_property_read_string_index(np, propname,
2992e3b1e6a1SMark Brown 							2 * i, &routes[i].sink);
2993a4a54dd5SStephen Warren 		if (ret) {
2994c871bd0bSMark Brown 			dev_err(card->dev,
2995c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2996c871bd0bSMark Brown 				propname, 2 * i, ret);
2997a4a54dd5SStephen Warren 			return -EINVAL;
2998a4a54dd5SStephen Warren 		}
2999a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3000e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3001a4a54dd5SStephen Warren 		if (ret) {
3002a4a54dd5SStephen Warren 			dev_err(card->dev,
3003c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3004c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3005a4a54dd5SStephen Warren 			return -EINVAL;
3006a4a54dd5SStephen Warren 		}
3007a4a54dd5SStephen Warren 	}
3008a4a54dd5SStephen Warren 
3009f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3010f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3011a4a54dd5SStephen Warren 
3012a4a54dd5SStephen Warren 	return 0;
3013a4a54dd5SStephen Warren }
30142bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3015a4a54dd5SStephen Warren 
30161ae0965dSStephan Gerhold int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
30171ae0965dSStephan Gerhold {
30181ae0965dSStephan Gerhold 	struct device_node *node = card->dev->of_node;
30191ae0965dSStephan Gerhold 	struct snd_soc_aux_dev *aux;
30201ae0965dSStephan Gerhold 	int num, i;
30211ae0965dSStephan Gerhold 
30221ae0965dSStephan Gerhold 	num = of_count_phandle_with_args(node, propname, NULL);
30231ae0965dSStephan Gerhold 	if (num == -ENOENT) {
30241ae0965dSStephan Gerhold 		return 0;
30251ae0965dSStephan Gerhold 	} else if (num < 0) {
30261ae0965dSStephan Gerhold 		dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
30271ae0965dSStephan Gerhold 			propname, num);
30281ae0965dSStephan Gerhold 		return num;
30291ae0965dSStephan Gerhold 	}
30301ae0965dSStephan Gerhold 
30311ae0965dSStephan Gerhold 	aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
30321ae0965dSStephan Gerhold 	if (!aux)
30331ae0965dSStephan Gerhold 		return -ENOMEM;
30341ae0965dSStephan Gerhold 	card->aux_dev = aux;
30351ae0965dSStephan Gerhold 	card->num_aux_devs = num;
30361ae0965dSStephan Gerhold 
30371ae0965dSStephan Gerhold 	for_each_card_pre_auxs(card, i, aux) {
30381ae0965dSStephan Gerhold 		aux->dlc.of_node = of_parse_phandle(node, propname, i);
30391ae0965dSStephan Gerhold 		if (!aux->dlc.of_node)
30401ae0965dSStephan Gerhold 			return -EINVAL;
30411ae0965dSStephan Gerhold 	}
30421ae0965dSStephan Gerhold 
30431ae0965dSStephan Gerhold 	return 0;
30441ae0965dSStephan Gerhold }
30451ae0965dSStephan Gerhold EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
30461ae0965dSStephan Gerhold 
304764c917d1SCharles Keepax unsigned int snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)
3048b44a67f8SKuninori Morimoto {
3049b44a67f8SKuninori Morimoto 	unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3050b44a67f8SKuninori Morimoto 
3051b44a67f8SKuninori Morimoto 	switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3052b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBP_CFP:
3053b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3054b44a67f8SKuninori Morimoto 		break;
3055b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBP_CFC:
3056b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3057b44a67f8SKuninori Morimoto 		break;
3058b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBC_CFP:
3059b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3060b44a67f8SKuninori Morimoto 		break;
3061b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBC_CFC:
3062b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3063b44a67f8SKuninori Morimoto 		break;
3064b44a67f8SKuninori Morimoto 	}
3065b44a67f8SKuninori Morimoto 
3066b44a67f8SKuninori Morimoto 	return inv_dai_fmt;
3067b44a67f8SKuninori Morimoto }
306864c917d1SCharles Keepax EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_flipped);
3069b44a67f8SKuninori Morimoto 
307091ae4477SKuninori Morimoto unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
307191ae4477SKuninori Morimoto {
30727766861dSKuninori Morimoto 	/*
30737766861dSKuninori Morimoto 	 * bit_frame is return value from
30747766861dSKuninori Morimoto 	 *	snd_soc_daifmt_parse_clock_provider_raw()
30757766861dSKuninori Morimoto 	 */
30767766861dSKuninori Morimoto 
307791ae4477SKuninori Morimoto 	/* Codec base */
307891ae4477SKuninori Morimoto 	switch (bit_frame) {
307991ae4477SKuninori Morimoto 	case 0x11:
308091ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBP_CFP;
308191ae4477SKuninori Morimoto 	case 0x10:
308291ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBP_CFC;
308391ae4477SKuninori Morimoto 	case 0x01:
308491ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBC_CFP;
308591ae4477SKuninori Morimoto 	default:
308691ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBC_CFC;
308791ae4477SKuninori Morimoto 	}
308891ae4477SKuninori Morimoto 
308991ae4477SKuninori Morimoto 	return 0;
309091ae4477SKuninori Morimoto }
309191ae4477SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
309291ae4477SKuninori Morimoto 
30937766861dSKuninori Morimoto unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
30947766861dSKuninori Morimoto 					 const char *prefix)
3095a7930ed4SKuninori Morimoto {
30965ad76775SKuninori Morimoto 	int ret;
3097a7930ed4SKuninori Morimoto 	char prop[128];
3098a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3099a7930ed4SKuninori Morimoto 	int bit, frame;
3100a7930ed4SKuninori Morimoto 	const char *str;
3101a7930ed4SKuninori Morimoto 	struct {
3102a7930ed4SKuninori Morimoto 		char *name;
3103a7930ed4SKuninori Morimoto 		unsigned int val;
3104a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3105a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3106a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3107a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3108a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3109a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3110a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3111a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3112a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3113a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3114a7930ed4SKuninori Morimoto 	};
3115a7930ed4SKuninori Morimoto 
3116a7930ed4SKuninori Morimoto 	if (!prefix)
3117a7930ed4SKuninori Morimoto 		prefix = "";
3118a7930ed4SKuninori Morimoto 
3119a7930ed4SKuninori Morimoto 	/*
31205711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31215711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3122a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3123a7930ed4SKuninori Morimoto 	 */
31245711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
31255711c979SKuninori Morimoto 	if (ret < 0) {
3126a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3127a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
31285711c979SKuninori Morimoto 	}
3129a7930ed4SKuninori Morimoto 	if (ret == 0) {
31305ad76775SKuninori Morimoto 		int i;
31315ad76775SKuninori Morimoto 
3132a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3133a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3134a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3135a7930ed4SKuninori Morimoto 				break;
3136a7930ed4SKuninori Morimoto 			}
3137a7930ed4SKuninori Morimoto 		}
3138a7930ed4SKuninori Morimoto 	}
3139a7930ed4SKuninori Morimoto 
3140a7930ed4SKuninori Morimoto 	/*
31418c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3142a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3143a7930ed4SKuninori Morimoto 	 */
31448c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
314551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
31468c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
31478c2d6a9fSKuninori Morimoto 	else
31488c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3149a7930ed4SKuninori Morimoto 
3150a7930ed4SKuninori Morimoto 	/*
3151a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3152a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3153a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3154a7930ed4SKuninori Morimoto 	 */
3155a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3156a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3157a7930ed4SKuninori Morimoto 
3158a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3159a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3160a7930ed4SKuninori Morimoto 
3161a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3162a7930ed4SKuninori Morimoto 	case 0x11:
3163a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3164a7930ed4SKuninori Morimoto 		break;
3165a7930ed4SKuninori Morimoto 	case 0x10:
3166a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3167a7930ed4SKuninori Morimoto 		break;
3168a7930ed4SKuninori Morimoto 	case 0x01:
3169a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3170a7930ed4SKuninori Morimoto 		break;
3171a7930ed4SKuninori Morimoto 	default:
3172a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3173a7930ed4SKuninori Morimoto 		break;
3174a7930ed4SKuninori Morimoto 	}
3175a7930ed4SKuninori Morimoto 
31767766861dSKuninori Morimoto 	return format;
31777766861dSKuninori Morimoto }
31787766861dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
31797766861dSKuninori Morimoto 
31807766861dSKuninori Morimoto unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
31817766861dSKuninori Morimoto 						     const char *prefix,
31827766861dSKuninori Morimoto 						     struct device_node **bitclkmaster,
31837766861dSKuninori Morimoto 						     struct device_node **framemaster)
31847766861dSKuninori Morimoto {
31857766861dSKuninori Morimoto 	char prop[128];
31867766861dSKuninori Morimoto 	unsigned int bit, frame;
31877766861dSKuninori Morimoto 
31887766861dSKuninori Morimoto 	if (!prefix)
31897766861dSKuninori Morimoto 		prefix = "";
31907766861dSKuninori Morimoto 
3191a7930ed4SKuninori Morimoto 	/*
3192a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3193a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3194a7930ed4SKuninori Morimoto 	 */
3195a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3196a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3197389cb834SJyri Sarha 	if (bit && bitclkmaster)
3198389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3199a7930ed4SKuninori Morimoto 
3200a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3201a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3202389cb834SJyri Sarha 	if (frame && framemaster)
3203389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3204a7930ed4SKuninori Morimoto 
32057766861dSKuninori Morimoto 	/*
32067766861dSKuninori Morimoto 	 * return bitmap.
32077766861dSKuninori Morimoto 	 * It will be parameter of
32087766861dSKuninori Morimoto 	 *	snd_soc_daifmt_clock_provider_from_bitmap()
32097766861dSKuninori Morimoto 	 */
32107766861dSKuninori Morimoto 	return (bit << 4) + frame;
3211a7930ed4SKuninori Morimoto }
32127766861dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
3213a7930ed4SKuninori Morimoto 
32141c943f60SKuninori Morimoto int snd_soc_get_stream_cpu(struct snd_soc_dai_link *dai_link, int stream)
32151c943f60SKuninori Morimoto {
32161c943f60SKuninori Morimoto 	/*
32171c943f60SKuninori Morimoto 	 * [Normal]
32181c943f60SKuninori Morimoto 	 *
32191c943f60SKuninori Morimoto 	 * Playback
32201c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
32211c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
32221c943f60SKuninori Morimoto 	 *
32231c943f60SKuninori Morimoto 	 * Capture
32241c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
32251c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
32261c943f60SKuninori Morimoto 	 */
32271c943f60SKuninori Morimoto 	if (!dai_link->c2c_params)
32281c943f60SKuninori Morimoto 		return stream;
32291c943f60SKuninori Morimoto 
32301c943f60SKuninori Morimoto 	/*
32311c943f60SKuninori Morimoto 	 * [Codec2Codec]
32321c943f60SKuninori Morimoto 	 *
32331c943f60SKuninori Morimoto 	 * Playback
32341c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
32351c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
32361c943f60SKuninori Morimoto 	 *
32371c943f60SKuninori Morimoto 	 * Capture
32381c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
32391c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
32401c943f60SKuninori Morimoto 	 */
32411c943f60SKuninori Morimoto 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
32421c943f60SKuninori Morimoto 		return SNDRV_PCM_STREAM_PLAYBACK;
32431c943f60SKuninori Morimoto 
32441c943f60SKuninori Morimoto 	return SNDRV_PCM_STREAM_CAPTURE;
32451c943f60SKuninori Morimoto }
32461c943f60SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_stream_cpu);
32471c943f60SKuninori Morimoto 
3248a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3249a180e8b9SKuninori Morimoto {
325009d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3251521d675dSKuninori Morimoto 	struct snd_soc_dai_link_component dlc = {
3252521d675dSKuninori Morimoto 		.of_node = of_graph_get_port_parent(ep),
3253521d675dSKuninori Morimoto 	};
3254a180e8b9SKuninori Morimoto 	int ret;
3255a180e8b9SKuninori Morimoto 
3256521d675dSKuninori Morimoto 
3257a180e8b9SKuninori Morimoto 	/*
3258a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3259a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3260a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3261a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3262a180e8b9SKuninori Morimoto 	 */
3263a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3264a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3265c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32662c7b1704SKuninori Morimoto 	if (component)
32672c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3268a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3269a180e8b9SKuninori Morimoto 
3270c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3271c0a480d1STony Lindgren 
3272a180e8b9SKuninori Morimoto 	return ret;
3273a180e8b9SKuninori Morimoto }
3274a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3275a180e8b9SKuninori Morimoto 
327605722a0cSKuninori Morimoto int snd_soc_get_dlc(const struct of_phandle_args *args, struct snd_soc_dai_link_component *dlc)
3277cb470087SKuninori Morimoto {
3278cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
327993b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3280cb470087SKuninori Morimoto 
3281cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3282368dee94SKuninori Morimoto 	for_each_component(pos) {
3283bce00560SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(pos);
32843e0aa8d8SJyri Sarha 
3285f7d344a2SShengjiu Wang 		if (component_of_node != args->np || !pos->num_dai)
3286cb470087SKuninori Morimoto 			continue;
3287cb470087SKuninori Morimoto 
328805722a0cSKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, &dlc->dai_name);
3289a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
329058bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32916833c452SKuninori Morimoto 			int id = -1;
32926833c452SKuninori Morimoto 
329393b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32946833c452SKuninori Morimoto 			case 0:
32956833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32966833c452SKuninori Morimoto 				break;
32976833c452SKuninori Morimoto 			case 1:
329893b0f3eeSJean-Francois Moine 				id = args->args[0];
32996833c452SKuninori Morimoto 				break;
33006833c452SKuninori Morimoto 			default:
33016833c452SKuninori Morimoto 				/* not supported */
3302cb470087SKuninori Morimoto 				break;
3303cb470087SKuninori Morimoto 			}
3304cb470087SKuninori Morimoto 
33056833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33066833c452SKuninori Morimoto 				ret = -EINVAL;
33073dcba280SNicolin Chen 				continue;
33086833c452SKuninori Morimoto 			}
3309e41975edSXiubo Li 
3310e41975edSXiubo Li 			ret = 0;
3311e41975edSXiubo Li 
331258bf4179SKuninori Morimoto 			/* find target DAI */
331315a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
331458bf4179SKuninori Morimoto 				if (id == 0)
331558bf4179SKuninori Morimoto 					break;
331658bf4179SKuninori Morimoto 				id--;
331758bf4179SKuninori Morimoto 			}
331858bf4179SKuninori Morimoto 
331905722a0cSKuninori Morimoto 			dlc->dai_name	= dai->driver->name;
332005722a0cSKuninori Morimoto 			if (!dlc->dai_name)
332105722a0cSKuninori Morimoto 				dlc->dai_name = pos->name;
33221dfa5a5aSJerome Brunet 		} else if (ret) {
33231dfa5a5aSJerome Brunet 			/*
33241dfa5a5aSJerome Brunet 			 * if another error than ENOTSUPP is returned go on and
33251dfa5a5aSJerome Brunet 			 * check if another component is provided with the same
33261dfa5a5aSJerome Brunet 			 * node. This may happen if a device provides several
33271dfa5a5aSJerome Brunet 			 * components
33281dfa5a5aSJerome Brunet 			 */
33291dfa5a5aSJerome Brunet 			continue;
33306833c452SKuninori Morimoto 		}
33316833c452SKuninori Morimoto 
3332cb470087SKuninori Morimoto 		break;
3333cb470087SKuninori Morimoto 	}
3334*065aa861SKuninori Morimoto 
3335*065aa861SKuninori Morimoto 	if (ret == 0)
3336*065aa861SKuninori Morimoto 		dlc->of_node = args->np;
3337*065aa861SKuninori Morimoto 
3338cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
333993b0f3eeSJean-Francois Moine 	return ret;
334093b0f3eeSJean-Francois Moine }
334105722a0cSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dlc);
334205722a0cSKuninori Morimoto 
334305722a0cSKuninori Morimoto int snd_soc_of_get_dlc(struct device_node *of_node,
334405722a0cSKuninori Morimoto 		       struct of_phandle_args *args,
334505722a0cSKuninori Morimoto 		       struct snd_soc_dai_link_component *dlc,
334605722a0cSKuninori Morimoto 		       int index)
334705722a0cSKuninori Morimoto {
334805722a0cSKuninori Morimoto 	struct of_phandle_args __args;
334905722a0cSKuninori Morimoto 	int ret;
335005722a0cSKuninori Morimoto 
335105722a0cSKuninori Morimoto 	if (!args)
335205722a0cSKuninori Morimoto 		args = &__args;
335305722a0cSKuninori Morimoto 
335405722a0cSKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
335505722a0cSKuninori Morimoto 					 "#sound-dai-cells", index, args);
335605722a0cSKuninori Morimoto 	if (ret)
335705722a0cSKuninori Morimoto 		return ret;
335805722a0cSKuninori Morimoto 
335905722a0cSKuninori Morimoto 	return snd_soc_get_dlc(args, dlc);
336005722a0cSKuninori Morimoto }
336105722a0cSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dlc);
336205722a0cSKuninori Morimoto 
336305722a0cSKuninori Morimoto int snd_soc_get_dai_name(const struct of_phandle_args *args,
336405722a0cSKuninori Morimoto 			 const char **dai_name)
336505722a0cSKuninori Morimoto {
336605722a0cSKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
336705722a0cSKuninori Morimoto 	int ret = snd_soc_get_dlc(args, &dlc);
336805722a0cSKuninori Morimoto 
336905722a0cSKuninori Morimoto 	if (ret == 0)
337005722a0cSKuninori Morimoto 		*dai_name = dlc.dai_name;
337105722a0cSKuninori Morimoto 
337205722a0cSKuninori Morimoto 	return ret;
337305722a0cSKuninori Morimoto }
33741ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
337593b0f3eeSJean-Francois Moine 
337693b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
33773c8b5861SKuninori Morimoto 			    const char **dai_name, int index)
337893b0f3eeSJean-Francois Moine {
337905722a0cSKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
33803c8b5861SKuninori Morimoto 	int ret = snd_soc_of_get_dlc(of_node, NULL, &dlc, index);
338193b0f3eeSJean-Francois Moine 
338205722a0cSKuninori Morimoto 	if (ret == 0)
338305722a0cSKuninori Morimoto 		*dai_name = dlc.dai_name;
3384cb470087SKuninori Morimoto 
3385cb470087SKuninori Morimoto 	return ret;
3386cb470087SKuninori Morimoto }
3387cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3388cb470087SKuninori Morimoto 
33899cc69528SKuninori Morimoto static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
33909cc69528SKuninori Morimoto {
33919cc69528SKuninori Morimoto 	if (component->of_node) {
33929cc69528SKuninori Morimoto 		of_node_put(component->of_node);
33939cc69528SKuninori Morimoto 		component->of_node = NULL;
33949cc69528SKuninori Morimoto 	}
33959cc69528SKuninori Morimoto }
33969cc69528SKuninori Morimoto 
33979cc69528SKuninori Morimoto static int __snd_soc_of_get_dai_link_component_alloc(
33989cc69528SKuninori Morimoto 	struct device *dev, struct device_node *of_node,
33999cc69528SKuninori Morimoto 	struct snd_soc_dai_link_component **ret_component,
34009cc69528SKuninori Morimoto 	int *ret_num)
34019cc69528SKuninori Morimoto {
34029cc69528SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
34039cc69528SKuninori Morimoto 	int num;
34049cc69528SKuninori Morimoto 
34059cc69528SKuninori Morimoto 	/* Count the number of CPUs/CODECs */
34069cc69528SKuninori Morimoto 	num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
34079cc69528SKuninori Morimoto 	if (num <= 0) {
34089cc69528SKuninori Morimoto 		if (num == -ENOENT)
34099cc69528SKuninori Morimoto 			dev_err(dev, "No 'sound-dai' property\n");
34109cc69528SKuninori Morimoto 		else
34119cc69528SKuninori Morimoto 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
34129cc69528SKuninori Morimoto 		return num;
34139cc69528SKuninori Morimoto 	}
34149cc69528SKuninori Morimoto 	component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
34159cc69528SKuninori Morimoto 	if (!component)
34169cc69528SKuninori Morimoto 		return -ENOMEM;
34179cc69528SKuninori Morimoto 
34189cc69528SKuninori Morimoto 	*ret_component	= component;
34199cc69528SKuninori Morimoto 	*ret_num	= num;
34209cc69528SKuninori Morimoto 
34219cc69528SKuninori Morimoto 	return 0;
34229cc69528SKuninori Morimoto }
34239cc69528SKuninori Morimoto 
342493b0f3eeSJean-Francois Moine /*
342594685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
342694685763SSylwester Nawrocki  * @dai_link: DAI link
342794685763SSylwester Nawrocki  *
342894685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
342994685763SSylwester Nawrocki  */
343094685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
343194685763SSylwester Nawrocki {
34323db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
343394685763SSylwester Nawrocki 	int index;
343494685763SSylwester Nawrocki 
34359cc69528SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component)
34369cc69528SKuninori Morimoto 		__snd_soc_of_put_component(component);
343794685763SSylwester Nawrocki }
343894685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
343994685763SSylwester Nawrocki 
344094685763SSylwester Nawrocki /*
344193b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
344293b0f3eeSJean-Francois Moine  * @dev: Card device
344393b0f3eeSJean-Francois Moine  * @of_node: Device node
344493b0f3eeSJean-Francois Moine  * @dai_link: DAI link
344593b0f3eeSJean-Francois Moine  *
344693b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
344793b0f3eeSJean-Francois Moine  * 'sound-dai'.
344893b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
344994685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
345094685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
345193b0f3eeSJean-Francois Moine  *
345293b0f3eeSJean-Francois Moine  * Returns 0 for success
345393b0f3eeSJean-Francois Moine  */
345493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
345593b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
345693b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
345793b0f3eeSJean-Francois Moine {
345893b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
34599cc69528SKuninori Morimoto 	int index, ret;
346093b0f3eeSJean-Francois Moine 
34619cc69528SKuninori Morimoto 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
34629cc69528SKuninori Morimoto 					 &dai_link->codecs, &dai_link->num_codecs);
34639cc69528SKuninori Morimoto 	if (ret < 0)
34649cc69528SKuninori Morimoto 		return ret;
346593b0f3eeSJean-Francois Moine 
346693b0f3eeSJean-Francois Moine 	/* Parse the list */
34673db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
346814c9b25fSKuninori Morimoto 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
346993b0f3eeSJean-Francois Moine 		if (ret)
347093b0f3eeSJean-Francois Moine 			goto err;
347193b0f3eeSJean-Francois Moine 	}
347293b0f3eeSJean-Francois Moine 	return 0;
347393b0f3eeSJean-Francois Moine err:
347494685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
347593b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
347693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
347793b0f3eeSJean-Francois Moine 	return ret;
347893b0f3eeSJean-Francois Moine }
347993b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
348093b0f3eeSJean-Francois Moine 
3481900dedd7SMartin Povišer /*
3482900dedd7SMartin Povišer  * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3483900dedd7SMartin Povišer  * @dai_link: DAI link
3484900dedd7SMartin Povišer  *
3485900dedd7SMartin Povišer  * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3486900dedd7SMartin Povišer  */
3487900dedd7SMartin Povišer void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3488900dedd7SMartin Povišer {
3489900dedd7SMartin Povišer 	struct snd_soc_dai_link_component *component;
3490900dedd7SMartin Povišer 	int index;
3491900dedd7SMartin Povišer 
34929cc69528SKuninori Morimoto 	for_each_link_cpus(dai_link, index, component)
34939cc69528SKuninori Morimoto 		__snd_soc_of_put_component(component);
3494900dedd7SMartin Povišer }
3495900dedd7SMartin Povišer EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3496900dedd7SMartin Povišer 
3497900dedd7SMartin Povišer /*
3498900dedd7SMartin Povišer  * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3499900dedd7SMartin Povišer  * @dev: Card device
3500900dedd7SMartin Povišer  * @of_node: Device node
3501900dedd7SMartin Povišer  * @dai_link: DAI link
3502900dedd7SMartin Povišer  *
3503900dedd7SMartin Povišer  * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3504900dedd7SMartin Povišer  * instead.
3505900dedd7SMartin Povišer  *
3506900dedd7SMartin Povišer  * Returns 0 for success
3507900dedd7SMartin Povišer  */
3508900dedd7SMartin Povišer int snd_soc_of_get_dai_link_cpus(struct device *dev,
3509900dedd7SMartin Povišer 				 struct device_node *of_node,
3510900dedd7SMartin Povišer 				 struct snd_soc_dai_link *dai_link)
3511900dedd7SMartin Povišer {
3512900dedd7SMartin Povišer 	struct snd_soc_dai_link_component *component;
35139cc69528SKuninori Morimoto 	int index, ret;
3514900dedd7SMartin Povišer 
3515586fb264SKuninori Morimoto 	/* Count the number of CPUs */
35169cc69528SKuninori Morimoto 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
35179cc69528SKuninori Morimoto 					 &dai_link->cpus, &dai_link->num_cpus);
35189cc69528SKuninori Morimoto 	if (ret < 0)
35199cc69528SKuninori Morimoto 		return ret;
3520900dedd7SMartin Povišer 
3521900dedd7SMartin Povišer 	/* Parse the list */
3522900dedd7SMartin Povišer 	for_each_link_cpus(dai_link, index, component) {
352314c9b25fSKuninori Morimoto 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3524900dedd7SMartin Povišer 		if (ret)
3525900dedd7SMartin Povišer 			goto err;
3526900dedd7SMartin Povišer 	}
3527900dedd7SMartin Povišer 	return 0;
3528900dedd7SMartin Povišer err:
3529586fb264SKuninori Morimoto 	snd_soc_of_put_dai_link_cpus(dai_link);
3530900dedd7SMartin Povišer 	dai_link->cpus = NULL;
3531900dedd7SMartin Povišer 	dai_link->num_cpus = 0;
3532900dedd7SMartin Povišer 	return ret;
3533900dedd7SMartin Povišer }
3534900dedd7SMartin Povišer EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3535900dedd7SMartin Povišer 
3536c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3537db2a4165SFrank Mandarino {
35386ec27c53SChen Zhongjin 	int ret;
3539fb257897SMark Brown 
35406ec27c53SChen Zhongjin 	snd_soc_debugfs_init();
35416ec27c53SChen Zhongjin 	ret = snd_soc_util_init();
35426ec27c53SChen Zhongjin 	if (ret)
35436ec27c53SChen Zhongjin 		goto err_util_init;
35446ec27c53SChen Zhongjin 
35456ec27c53SChen Zhongjin 	ret = platform_driver_register(&soc_driver);
35466ec27c53SChen Zhongjin 	if (ret)
35476ec27c53SChen Zhongjin 		goto err_register;
35486ec27c53SChen Zhongjin 	return 0;
35496ec27c53SChen Zhongjin 
35506ec27c53SChen Zhongjin err_register:
35516ec27c53SChen Zhongjin 	snd_soc_util_exit();
35526ec27c53SChen Zhongjin err_util_init:
35536ec27c53SChen Zhongjin 	snd_soc_debugfs_exit();
35546ec27c53SChen Zhongjin 	return ret;
3555db2a4165SFrank Mandarino }
35564abe8e16SMark Brown module_init(snd_soc_init);
3557db2a4165SFrank Mandarino 
35587d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3559db2a4165SFrank Mandarino {
3560fb257897SMark Brown 	snd_soc_util_exit();
35616553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3562fb257897SMark Brown 
3563db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3564db2a4165SFrank Mandarino }
3565db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3566db2a4165SFrank Mandarino 
3567db2a4165SFrank Mandarino /* Module information */
3568d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3569db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3570db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
35718b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3572