xref: /openbmc/linux/sound/soc/soc-core.c (revision 18dd66ea84464a4bc86089276d5f75cadeb94a09)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51384c89e2SMark Brown #endif
52384c89e2SMark Brown 
53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
54030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
55e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
56c5af3a2eSMark Brown 
57368dee94SKuninori Morimoto #define for_each_component(component)			\
58368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
59368dee94SKuninori Morimoto 
60db2a4165SFrank Mandarino /*
61587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
62587c9844SKuninori Morimoto  * dai_link. see soc.h
63587c9844SKuninori Morimoto  */
64587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
65587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
66587c9844SKuninori Morimoto 
67587c9844SKuninori Morimoto /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
760faf1237SYueHaibing #ifdef CONFIG_DMI
772c7b696aSMarcel Ziswiler /*
782c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7998faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
8098faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8198faf436SMengdong Lin  */
8298faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8398faf436SMengdong Lin 	"To be filled by OEM",
8498faf436SMengdong Lin 	"TBD by OEM",
8598faf436SMengdong Lin 	"Default String",
8698faf436SMengdong Lin 	"Board Manufacturer",
8798faf436SMengdong Lin 	"Board Vendor Name",
8898faf436SMengdong Lin 	"Board Product Name",
8998faf436SMengdong Lin 	NULL,	/* terminator */
9098faf436SMengdong Lin };
910faf1237SYueHaibing #endif
9298faf436SMengdong Lin 
93dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
94dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
95dbe21408SMark Brown {
9636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
97dbe21408SMark Brown 
98f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
99dbe21408SMark Brown }
100dbe21408SMark Brown 
101dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
102dbe21408SMark Brown 			       struct device_attribute *attr,
103dbe21408SMark Brown 			       const char *buf, size_t count)
104dbe21408SMark Brown {
10536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
106c593b520SMark Brown 	int ret;
107dbe21408SMark Brown 
108b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
109c593b520SMark Brown 	if (ret)
110c593b520SMark Brown 		return ret;
111dbe21408SMark Brown 
112dbe21408SMark Brown 	return count;
113dbe21408SMark Brown }
114dbe21408SMark Brown 
115dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
116dbe21408SMark Brown 
117d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
118d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
119d29697dcSTakashi Iwai 	NULL
120d29697dcSTakashi Iwai };
121d29697dcSTakashi Iwai 
122d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
124d29697dcSTakashi Iwai {
125d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
126d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
127d29697dcSTakashi Iwai 
128d918a376SKuninori Morimoto 	if (!rtd)
129d918a376SKuninori Morimoto 		return 0;
130d918a376SKuninori Morimoto 
131d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
132d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1333b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
134d29697dcSTakashi Iwai }
135d29697dcSTakashi Iwai 
136d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
137d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
138d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
139d29697dcSTakashi Iwai };
140d29697dcSTakashi Iwai 
141f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
142d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
143d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
144d29697dcSTakashi Iwai };
145d29697dcSTakashi Iwai 
146d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
147d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
148f7e73b26SMark Brown 	&soc_dev_group,
149d29697dcSTakashi Iwai 	NULL
150d29697dcSTakashi Iwai };
151d29697dcSTakashi Iwai 
1522624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
15381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
154e73f3de5SRussell King {
1556553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1566553bf06SLars-Peter Clausen 		return;
1576553bf06SLars-Peter Clausen 
15881c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15981c7cfd1SLars-Peter Clausen 		char *name;
160e73f3de5SRussell King 
16181c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
16281c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
16381c7cfd1SLars-Peter Clausen 		if (name) {
16481c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16681c7cfd1SLars-Peter Clausen 			kfree(name);
16781c7cfd1SLars-Peter Clausen 		}
16881c7cfd1SLars-Peter Clausen 	} else {
16981c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
17081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
171e73f3de5SRussell King 	}
172e73f3de5SRussell King 
17381c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
17481c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17581c7cfd1SLars-Peter Clausen }
17681c7cfd1SLars-Peter Clausen 
17781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17881c7cfd1SLars-Peter Clausen {
179ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
180ad64bfbdSKuninori Morimoto 		return;
18181c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
182ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
18381c7cfd1SLars-Peter Clausen }
18481c7cfd1SLars-Peter Clausen 
185c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
186f3208780SMark Brown {
1871438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
188f3208780SMark Brown 	struct snd_soc_dai *dai;
189f3208780SMark Brown 
19034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
19134e81ab4SLars-Peter Clausen 
192368dee94SKuninori Morimoto 	for_each_component(component)
19315a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
194700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
195f3208780SMark Brown 
19634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19734e81ab4SLars-Peter Clausen 
198700c17caSDonglin Peng 	return 0;
199700c17caSDonglin Peng }
200c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
201f3208780SMark Brown 
202db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
203db795f9bSKuninori Morimoto {
204db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
205db795f9bSKuninori Morimoto 
206db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
207db795f9bSKuninori Morimoto 
208368dee94SKuninori Morimoto 	for_each_component(component)
209db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
210db795f9bSKuninori Morimoto 
211db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
212db795f9bSKuninori Morimoto 
213db795f9bSKuninori Morimoto 	return 0;
214db795f9bSKuninori Morimoto }
215db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
216db795f9bSKuninori Morimoto 
217a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
218a6052154SJarkko Nikula {
219a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2208a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2213a45b867SJarkko Nikula 
222fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2233a45b867SJarkko Nikula 			   &card->pop_time);
224d8ca7a0aSKuninori Morimoto 
225d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
226a6052154SJarkko Nikula }
227a6052154SJarkko Nikula 
228a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
229a6052154SJarkko Nikula {
230a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
23129040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
232a6052154SJarkko Nikula }
233a6052154SJarkko Nikula 
2346553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2356553bf06SLars-Peter Clausen {
2366553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2376553bf06SLars-Peter Clausen 
238fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
239fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
240db795f9bSKuninori Morimoto 
241fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
242fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2436553bf06SLars-Peter Clausen }
2446553bf06SLars-Peter Clausen 
2456553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2466553bf06SLars-Peter Clausen {
2476553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2486553bf06SLars-Peter Clausen }
2496553bf06SLars-Peter Clausen 
2502624d5faSMark Brown #else
2512624d5faSMark Brown 
25281c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
25381c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2542624d5faSMark Brown {
2552624d5faSMark Brown }
2562624d5faSMark Brown 
25781c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
25881c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
259731f1ab2SSebastien Guiriec {
260731f1ab2SSebastien Guiriec }
261731f1ab2SSebastien Guiriec 
262b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
263b95fccbcSAxel Lin {
264b95fccbcSAxel Lin }
265b95fccbcSAxel Lin 
266b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
267b95fccbcSAxel Lin {
268b95fccbcSAxel Lin }
2696553bf06SLars-Peter Clausen 
2706553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2716553bf06SLars-Peter Clausen {
2726553bf06SLars-Peter Clausen }
2736553bf06SLars-Peter Clausen 
2746553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2756553bf06SLars-Peter Clausen {
2766553bf06SLars-Peter Clausen }
2776553bf06SLars-Peter Clausen 
2782624d5faSMark Brown #endif
2792624d5faSMark Brown 
2808ec241c4SKuninori Morimoto /*
2818ec241c4SKuninori Morimoto  * This is glue code between snd_pcm_lib_ioctl() and
2828ec241c4SKuninori Morimoto  * snd_soc_component_driver :: ioctl
2838ec241c4SKuninori Morimoto  */
2848ec241c4SKuninori Morimoto int snd_soc_pcm_lib_ioctl(struct snd_soc_component *component,
2858ec241c4SKuninori Morimoto 			  struct snd_pcm_substream *substream,
2868ec241c4SKuninori Morimoto 			  unsigned int cmd, void *arg)
2878ec241c4SKuninori Morimoto {
2888ec241c4SKuninori Morimoto 	return snd_pcm_lib_ioctl(substream, cmd, arg);
2898ec241c4SKuninori Morimoto }
2908ec241c4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_pcm_lib_ioctl);
2918ec241c4SKuninori Morimoto 
292a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
293a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
294a0ac4411SKuninori Morimoto {
295a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
2962b544dd7SKuninori Morimoto 	struct snd_soc_component *comp;
297a0ac4411SKuninori Morimoto 
2982b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, comp) {
299a0ac4411SKuninori Morimoto 		/* already connected */
3002b544dd7SKuninori Morimoto 		if (comp == component)
301a0ac4411SKuninori Morimoto 			return 0;
302a0ac4411SKuninori Morimoto 	}
303a0ac4411SKuninori Morimoto 
304353e16bfSKuninori Morimoto 	/*
305353e16bfSKuninori Morimoto 	 * created rtdcom here will be freed when rtd->dev was freed.
306353e16bfSKuninori Morimoto 	 * see
307353e16bfSKuninori Morimoto 	 *	soc_free_pcm_runtime() :: device_unregister(rtd->dev)
308353e16bfSKuninori Morimoto 	 */
309353e16bfSKuninori Morimoto 	rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
31032d2c172SKuninori Morimoto 	if (!rtdcom)
311a0ac4411SKuninori Morimoto 		return -ENOMEM;
312a0ac4411SKuninori Morimoto 
31332d2c172SKuninori Morimoto 	rtdcom->component = component;
31432d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
315a0ac4411SKuninori Morimoto 
316353e16bfSKuninori Morimoto 	/*
317353e16bfSKuninori Morimoto 	 * When rtd was freed, created rtdcom here will be
318353e16bfSKuninori Morimoto 	 * also freed.
319353e16bfSKuninori Morimoto 	 * And we don't need to call list_del(&rtdcom->list)
320353e16bfSKuninori Morimoto 	 * when freed, because rtd is also freed.
321353e16bfSKuninori Morimoto 	 */
32232d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
323a0ac4411SKuninori Morimoto 
324a0ac4411SKuninori Morimoto 	return 0;
325a0ac4411SKuninori Morimoto }
326a0ac4411SKuninori Morimoto 
327a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
328a0ac4411SKuninori Morimoto 						const char *driver_name)
329a0ac4411SKuninori Morimoto {
330a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
3312b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
332a0ac4411SKuninori Morimoto 
333971da24cSKuninori Morimoto 	if (!driver_name)
334971da24cSKuninori Morimoto 		return NULL;
335971da24cSKuninori Morimoto 
336a33c0d16SKuninori Morimoto 	/*
337a33c0d16SKuninori Morimoto 	 * NOTE
338a33c0d16SKuninori Morimoto 	 *
339a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
340a33c0d16SKuninori Morimoto 	 * specified driver name.
341a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
342a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
343a33c0d16SKuninori Morimoto 	 */
3442b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
3452b544dd7SKuninori Morimoto 		const char *component_name = component->driver->name;
346971da24cSKuninori Morimoto 
347971da24cSKuninori Morimoto 		if (!component_name)
348971da24cSKuninori Morimoto 			continue;
349971da24cSKuninori Morimoto 
350971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
351971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
352a0ac4411SKuninori Morimoto 			return rtdcom->component;
353a0ac4411SKuninori Morimoto 	}
354a0ac4411SKuninori Morimoto 
355a0ac4411SKuninori Morimoto 	return NULL;
356a0ac4411SKuninori Morimoto }
357031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
358a0ac4411SKuninori Morimoto 
359*18dd66eaSKuninori Morimoto static struct snd_soc_component
360*18dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
361b8132657SKuninori Morimoto {
362b8132657SKuninori Morimoto 	struct snd_soc_component *component;
3635bd7e08bSKuninori Morimoto 	struct snd_soc_component *found_component;
364b8132657SKuninori Morimoto 
3655bd7e08bSKuninori Morimoto 	found_component = NULL;
366b8132657SKuninori Morimoto 	for_each_component(component) {
3675bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3685bd7e08bSKuninori Morimoto 		    (!driver_name ||
3695bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3705bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3715bd7e08bSKuninori Morimoto 			found_component = component;
372b8132657SKuninori Morimoto 			break;
373b8132657SKuninori Morimoto 		}
3745bd7e08bSKuninori Morimoto 	}
375b8132657SKuninori Morimoto 
3765bd7e08bSKuninori Morimoto 	return found_component;
377b8132657SKuninori Morimoto }
378*18dd66eaSKuninori Morimoto 
379*18dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
380*18dd66eaSKuninori Morimoto 						   const char *driver_name)
381*18dd66eaSKuninori Morimoto {
382*18dd66eaSKuninori Morimoto 	struct snd_soc_component *component;
383*18dd66eaSKuninori Morimoto 
384*18dd66eaSKuninori Morimoto 	mutex_lock(&client_mutex);
385*18dd66eaSKuninori Morimoto 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
386*18dd66eaSKuninori Morimoto 	mutex_unlock(&client_mutex);
387*18dd66eaSKuninori Morimoto 
388*18dd66eaSKuninori Morimoto 	return component;
389*18dd66eaSKuninori Morimoto }
390b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
391b8132657SKuninori Morimoto 
39247c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
39347c88fffSLiam Girdwood 		const char *dai_link, int stream)
39447c88fffSLiam Girdwood {
3951a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
39647c88fffSLiam Girdwood 
397bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3981a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3991a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
4001a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
40147c88fffSLiam Girdwood 	}
402f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
40347c88fffSLiam Girdwood 	return NULL;
40447c88fffSLiam Girdwood }
40547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
40647c88fffSLiam Girdwood 
40775ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
40875ab9eb6SKuninori Morimoto 
4096e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
4106e864344SKuninori Morimoto {
4116e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
4126e864344SKuninori Morimoto 	kfree(dev);
4136e864344SKuninori Morimoto }
4146e864344SKuninori Morimoto 
4151c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
4161c93a9e0SKuninori Morimoto {
4176e864344SKuninori Morimoto 	if (!rtd)
4186e864344SKuninori Morimoto 		return;
4196e864344SKuninori Morimoto 
420753ace0aSKuninori Morimoto 	list_del(&rtd->list);
421b7c5bc45SKuninori Morimoto 
422b7c5bc45SKuninori Morimoto 	/*
423b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
424b7c5bc45SKuninori Morimoto 	 * see
425b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
426d918a376SKuninori Morimoto 	 *
427d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
428d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
429d918a376SKuninori Morimoto 	 * see
430d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
431b7c5bc45SKuninori Morimoto 	 */
432b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4331c93a9e0SKuninori Morimoto }
4341c93a9e0SKuninori Morimoto 
4351a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4361a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4371a497983SMengdong Lin {
4381a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
439d918a376SKuninori Morimoto 	struct device *dev;
4406e864344SKuninori Morimoto 	int ret;
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 	dev->groups	= soc_dev_attr_groups;
452d918a376SKuninori Morimoto 
453d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
454d918a376SKuninori Morimoto 
455d918a376SKuninori Morimoto 	ret = device_register(dev);
456d918a376SKuninori Morimoto 	if (ret < 0) {
457d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
458d918a376SKuninori Morimoto 		return NULL;
459d918a376SKuninori Morimoto 	}
460d918a376SKuninori Morimoto 
461d918a376SKuninori Morimoto 	/*
4626e864344SKuninori Morimoto 	 * for rtd
4636e864344SKuninori Morimoto 	 */
4644dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4651a497983SMengdong Lin 	if (!rtd)
4666e864344SKuninori Morimoto 		goto free_rtd;
4671a497983SMengdong Lin 
468d918a376SKuninori Morimoto 	rtd->dev = dev;
469d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
470d918a376SKuninori Morimoto 
4716e864344SKuninori Morimoto 	/*
4726e864344SKuninori Morimoto 	 * for rtd->codec_dais
4736e864344SKuninori Morimoto 	 */
4744dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4756396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4761a497983SMengdong Lin 					GFP_KERNEL);
4776e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4786e864344SKuninori Morimoto 		goto free_rtd;
4796e864344SKuninori Morimoto 
4806e864344SKuninori Morimoto 	/*
4816e864344SKuninori Morimoto 	 * rtd remaining settings
4826e864344SKuninori Morimoto 	 */
483929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4846e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4856e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
4866e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
4876e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
4886e864344SKuninori Morimoto 
489929deb84SKuninori Morimoto 	rtd->card = card;
490929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
491929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
492929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
493929deb84SKuninori Morimoto 
4946634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4951a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4961a497983SMengdong Lin 	rtd->num = card->num_rtd;
4971a497983SMengdong Lin 	card->num_rtd++;
498a848125eSKuninori Morimoto 
499a848125eSKuninori Morimoto 	return rtd;
5006e864344SKuninori Morimoto 
5016e864344SKuninori Morimoto free_rtd:
5026e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5036e864344SKuninori Morimoto 	return NULL;
5041a497983SMengdong Lin }
5051a497983SMengdong Lin 
50647c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
50747c88fffSLiam Girdwood 		const char *dai_link)
50847c88fffSLiam Girdwood {
5091a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
51047c88fffSLiam Girdwood 
511bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5121a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
5131a497983SMengdong Lin 			return rtd;
51447c88fffSLiam Girdwood 	}
515f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
51647c88fffSLiam Girdwood 	return NULL;
51747c88fffSLiam Girdwood }
51847c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
51947c88fffSLiam Girdwood 
52065462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
52165462e44SKuninori Morimoto {
52265462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
52365462e44SKuninori Morimoto 
52465462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
52565462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
52665462e44SKuninori Morimoto }
52765462e44SKuninori Morimoto 
5286f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
529db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5306f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
531db2a4165SFrank Mandarino {
5326f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
533d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5341a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5351a497983SMengdong Lin 	int i;
536db2a4165SFrank Mandarino 
537c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
538c5599b87SLars-Peter Clausen 	if (!card->instantiated)
539e3509ff0SDaniel Mack 		return 0;
540e3509ff0SDaniel Mack 
5412c7b696aSMarcel Ziswiler 	/*
5422c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5436ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5446ed25978SAndy Green 	 */
545f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5466ed25978SAndy Green 
5476ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
548f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5496ed25978SAndy Green 
550a00f90f9SMark Brown 	/* mute any active DACs */
551bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5520b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5533efab7dcSMark Brown 
5541a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5553efab7dcSMark Brown 			continue;
5563efab7dcSMark Brown 
5570b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
55888fdffa2SKuninori Morimoto 			if (dai->playback_active)
55988fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
56088fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
561db2a4165SFrank Mandarino 		}
56288bd870fSBenoit Cousson 	}
563db2a4165SFrank Mandarino 
5644ccab3e7SLiam Girdwood 	/* suspend all pcms */
565bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5661a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5673efab7dcSMark Brown 			continue;
5683efab7dcSMark Brown 
5691a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5703efab7dcSMark Brown 	}
5714ccab3e7SLiam Girdwood 
57287506549SMark Brown 	if (card->suspend_pre)
57370b2ac12SMark Brown 		card->suspend_pre(card);
574db2a4165SFrank Mandarino 
575bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5761a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5773efab7dcSMark Brown 
5781a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5793efab7dcSMark Brown 			continue;
5803efab7dcSMark Brown 
581e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
582e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
583db2a4165SFrank Mandarino 	}
584db2a4165SFrank Mandarino 
58537660b6dSLars-Peter Clausen 	/* close any waiting streams */
58665462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
587db2a4165SFrank Mandarino 
588bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5893efab7dcSMark Brown 
5901a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5913efab7dcSMark Brown 			continue;
5923efab7dcSMark Brown 
5931a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5947bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
595db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
596f0fba2adSLiam Girdwood 
5971a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5987bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
599db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
600db2a4165SFrank Mandarino 	}
601db2a4165SFrank Mandarino 
6028be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6038be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
604e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
605e2d32ff6SMark Brown 
6069178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
607f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
6082c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
6092c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
610d9fc4063SKuninori Morimoto 
6112c7b696aSMarcel Ziswiler 		/*
6122c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
6132c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
6142c7b696aSMarcel Ziswiler 		 */
615e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
6164890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6171547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
618125a25daSMark Brown 				/*
6199178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
620125a25daSMark Brown 				 * bias off then being in STANDBY
621125a25daSMark Brown 				 * means it's doing something,
622125a25daSMark Brown 				 * otherwise fall through.
623125a25daSMark Brown 				 */
6244890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6259178feb4SKuninori Morimoto 					dev_dbg(component->dev,
62610e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
627125a25daSMark Brown 					break;
628125a25daSMark Brown 				}
6291a12d5dcSGustavo A. R. Silva 				/* fall through */
630a8093297SLars-Peter Clausen 
6311547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
63266c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6339178feb4SKuninori Morimoto 				if (component->regmap)
6349178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
635988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6369178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6371547aba9SMark Brown 				break;
6381547aba9SMark Brown 			default:
6399178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6409178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6411547aba9SMark Brown 				break;
6421547aba9SMark Brown 			}
6431547aba9SMark Brown 		}
644f0fba2adSLiam Girdwood 	}
645db2a4165SFrank Mandarino 
646bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6471a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6483efab7dcSMark Brown 
6491a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6503efab7dcSMark Brown 			continue;
6513efab7dcSMark Brown 
652e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
653e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
654988e8cc4SNicolin Chen 
655988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
656988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
657db2a4165SFrank Mandarino 	}
658db2a4165SFrank Mandarino 
65987506549SMark Brown 	if (card->suspend_post)
66070b2ac12SMark Brown 		card->suspend_post(card);
661db2a4165SFrank Mandarino 
662db2a4165SFrank Mandarino 	return 0;
663db2a4165SFrank Mandarino }
6646f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
665db2a4165SFrank Mandarino 
6662c7b696aSMarcel Ziswiler /*
6672c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6686ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6696ed25978SAndy Green  */
6706ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
671db2a4165SFrank Mandarino {
672f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6732c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6742c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6751a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
676d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6771a497983SMengdong Lin 	int i;
678db2a4165SFrank Mandarino 
6792c7b696aSMarcel Ziswiler 	/*
6802c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6816ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6826ed25978SAndy Green 	 */
6836ed25978SAndy Green 
684f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6856ed25978SAndy Green 
6869949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
687f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6889949788bSMark Brown 
68987506549SMark Brown 	if (card->resume_pre)
69070b2ac12SMark Brown 		card->resume_pre(card);
691db2a4165SFrank Mandarino 
692bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
693bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6941a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6953efab7dcSMark Brown 
6961a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6973efab7dcSMark Brown 			continue;
6983efab7dcSMark Brown 
69924b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
70024b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
701db2a4165SFrank Mandarino 	}
702db2a4165SFrank Mandarino 
703f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
704e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
7059a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
7061547aba9SMark Brown 	}
707db2a4165SFrank Mandarino 
708bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7093efab7dcSMark Brown 
7101a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7113efab7dcSMark Brown 			continue;
7123efab7dcSMark Brown 
7131a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
714d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
715db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
716f0fba2adSLiam Girdwood 
7171a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
718d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
719db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
720db2a4165SFrank Mandarino 	}
721db2a4165SFrank Mandarino 
7223ff3f64bSMark Brown 	/* unmute any active DACs */
723bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7240b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
7253efab7dcSMark Brown 
7261a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7273efab7dcSMark Brown 			continue;
7283efab7dcSMark Brown 
7290b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
73088fdffa2SKuninori Morimoto 			if (dai->playback_active)
73188fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
73288fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
733db2a4165SFrank Mandarino 		}
73488bd870fSBenoit Cousson 	}
735db2a4165SFrank Mandarino 
736bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7371a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7383efab7dcSMark Brown 
7391a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7403efab7dcSMark Brown 			continue;
7413efab7dcSMark Brown 
74224b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
74324b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
744db2a4165SFrank Mandarino 	}
745db2a4165SFrank Mandarino 
74687506549SMark Brown 	if (card->resume_post)
74770b2ac12SMark Brown 		card->resume_post(card);
748db2a4165SFrank Mandarino 
749f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7506ed25978SAndy Green 
7518be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7528be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
753e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7541a7aaa58SJeeja KP 
7551a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7561a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7576ed25978SAndy Green }
7586ed25978SAndy Green 
7596ed25978SAndy Green /* powers up audio subsystem after a suspend */
7606f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7616ed25978SAndy Green {
7626f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
763bc263214SLars-Peter Clausen 	bool bus_control = false;
7641a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
76522d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
76622d251a5SKuninori Morimoto 	int i;
767b9dd94a8SPeter Ujfalusi 
768c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
769c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7705ff1ddf2SEric Miao 		return 0;
7715ff1ddf2SEric Miao 
772988e8cc4SNicolin Chen 	/* activate pins from sleep state */
773bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
77488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
77588bd870fSBenoit Cousson 
776988e8cc4SNicolin Chen 		if (cpu_dai->active)
777988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
77888bd870fSBenoit Cousson 
77922d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
780988e8cc4SNicolin Chen 			if (codec_dai->active)
781988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
782988e8cc4SNicolin Chen 		}
78388bd870fSBenoit Cousson 	}
784988e8cc4SNicolin Chen 
785bc263214SLars-Peter Clausen 	/*
786bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
787bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
788bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
78964ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
79064ab9baaSMark Brown 	 */
791bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7921a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7932c7b696aSMarcel Ziswiler 
794bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
79582e14e8bSStephen Warren 	}
796bc263214SLars-Peter Clausen 	if (bus_control) {
797bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
79864ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
79964ab9baaSMark Brown 	} else {
800f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8016308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
802f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
803f0fba2adSLiam Girdwood 	}
8046ed25978SAndy Green 
805db2a4165SFrank Mandarino 	return 0;
806db2a4165SFrank Mandarino }
8076f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
808b3da4251SKuninori Morimoto 
809b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
810b3da4251SKuninori Morimoto {
811b3da4251SKuninori Morimoto 	/* deferred resume work */
812b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
813b3da4251SKuninori Morimoto }
814db2a4165SFrank Mandarino #else
8156f8ab4acSMark Brown #define snd_soc_suspend NULL
8166f8ab4acSMark Brown #define snd_soc_resume NULL
817b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
818b3da4251SKuninori Morimoto {
819b3da4251SKuninori Morimoto }
820db2a4165SFrank Mandarino #endif
821db2a4165SFrank Mandarino 
82285e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
82302a06d30SBarry Song };
82402a06d30SBarry Song 
825c0834440SKuninori Morimoto static struct device_node
826c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
82712023a9aSMisael Lopez Cruz {
828c0834440SKuninori Morimoto 	struct device_node *of_node;
82912023a9aSMisael Lopez Cruz 
830c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
831c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
832c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
83334e81ab4SLars-Peter Clausen 
834c0834440SKuninori Morimoto 	return of_node;
83512023a9aSMisael Lopez Cruz }
83612023a9aSMisael Lopez Cruz 
837be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
838be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
839be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
840be6ac0a9SKuninori Morimoto {
841be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
842be6ac0a9SKuninori Morimoto 
8437d7db5d3SKuninori Morimoto 	if (!dlc)
8447d7db5d3SKuninori Morimoto 		return 0;
8457d7db5d3SKuninori Morimoto 
8467d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
847be6ac0a9SKuninori Morimoto 
848be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
849be6ac0a9SKuninori Morimoto 		return 0;
850be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
851be6ac0a9SKuninori Morimoto 		return 0;
852be6ac0a9SKuninori Morimoto 
853be6ac0a9SKuninori Morimoto 	return 1;
854be6ac0a9SKuninori Morimoto }
855be6ac0a9SKuninori Morimoto 
856db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
857c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
858db2a4165SFrank Mandarino {
859db2a4165SFrank Mandarino 	struct snd_soc_component *component;
860db2a4165SFrank Mandarino 
861db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
862db2a4165SFrank Mandarino 
863e3303268SKuninori Morimoto 	/*
864e3303268SKuninori Morimoto 	 * NOTE
865e3303268SKuninori Morimoto 	 *
866e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
867e3303268SKuninori Morimoto 	 * has few components by same of_node/name
868e3303268SKuninori Morimoto 	 * ex)
869e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
870e3303268SKuninori Morimoto 	 */
871c1e230f0SKuninori Morimoto 	for_each_component(component)
872c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
873db2a4165SFrank Mandarino 			return component;
874db2a4165SFrank Mandarino 
875db2a4165SFrank Mandarino 	return NULL;
87612023a9aSMisael Lopez Cruz }
87712023a9aSMisael Lopez Cruz 
878fbb88b5cSMengdong Lin /**
879fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
880fbb88b5cSMengdong Lin  *
8814958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
882fbb88b5cSMengdong Lin  *
883ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
884fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
885fbb88b5cSMengdong Lin  * should also match if being specified.
886fbb88b5cSMengdong Lin  *
887fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
888fbb88b5cSMengdong Lin  */
889305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
89014621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
89112023a9aSMisael Lopez Cruz {
89214621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
89314621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
89412023a9aSMisael Lopez Cruz 
89534e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
89634e81ab4SLars-Peter Clausen 
89714621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
898368dee94SKuninori Morimoto 	for_each_component(component) {
899be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
90012023a9aSMisael Lopez Cruz 			continue;
90115a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
9024958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
9036a6dafdaSJeffy Chen 			    && (!dai->driver->name
9046a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
90514621c7eSLars-Peter Clausen 				continue;
90612023a9aSMisael Lopez Cruz 
90714621c7eSLars-Peter Clausen 			return dai;
90812023a9aSMisael Lopez Cruz 		}
90912023a9aSMisael Lopez Cruz 	}
91012023a9aSMisael Lopez Cruz 
91112023a9aSMisael Lopez Cruz 	return NULL;
91212023a9aSMisael Lopez Cruz }
913305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
91412023a9aSMisael Lopez Cruz 
91517fb1755SMengdong Lin /**
91617fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
91717fb1755SMengdong Lin  *
91817fb1755SMengdong Lin  * @card: soc card
91917fb1755SMengdong Lin  * @id: DAI link ID to match
92017fb1755SMengdong Lin  * @name: DAI link name to match, optional
9218abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
92217fb1755SMengdong Lin  *
92317fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
92417fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
92517fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
92617fb1755SMengdong Lin  * specified.
92717fb1755SMengdong Lin  *
92817fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
92917fb1755SMengdong Lin  */
93017fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
93117fb1755SMengdong Lin 					       int id, const char *name,
93217fb1755SMengdong Lin 					       const char *stream_name)
93317fb1755SMengdong Lin {
93442849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
93517fb1755SMengdong Lin 
93617fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
93717fb1755SMengdong Lin 
93842849064SKuninori Morimoto 	for_each_card_links(card, link) {
93917fb1755SMengdong Lin 		if (link->id != id)
94017fb1755SMengdong Lin 			continue;
94117fb1755SMengdong Lin 
94217fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
94317fb1755SMengdong Lin 			continue;
94417fb1755SMengdong Lin 
94517fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
94617fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
94717fb1755SMengdong Lin 			continue;
94817fb1755SMengdong Lin 
94917fb1755SMengdong Lin 		return link;
95017fb1755SMengdong Lin 	}
95117fb1755SMengdong Lin 
95217fb1755SMengdong Lin 	return NULL;
95317fb1755SMengdong Lin }
95417fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
95517fb1755SMengdong Lin 
95649a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
95749a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
958db2a4165SFrank Mandarino {
95949a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
96049a5ba1cSMengdong Lin 
961bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
96249a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
96349a5ba1cSMengdong Lin 			return true;
96449a5ba1cSMengdong Lin 	}
96549a5ba1cSMengdong Lin 
96649a5ba1cSMengdong Lin 	return false;
96749a5ba1cSMengdong Lin }
96849a5ba1cSMengdong Lin 
969bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
97036794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
97136794902SKuninori Morimoto {
97236794902SKuninori Morimoto 	int i;
97336794902SKuninori Morimoto 	struct snd_soc_dai_link_component *codec, *platform;
97436794902SKuninori Morimoto 
97536794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
97636794902SKuninori Morimoto 		/*
97736794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
97836794902SKuninori Morimoto 		 * not both or neither.
97936794902SKuninori Morimoto 		 */
98036794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
98136794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
98236794902SKuninori Morimoto 				link->name);
98336794902SKuninori Morimoto 			return -EINVAL;
98436794902SKuninori Morimoto 		}
98536794902SKuninori Morimoto 
98636794902SKuninori Morimoto 		/* Codec DAI name must be specified */
98736794902SKuninori Morimoto 		if (!codec->dai_name) {
98836794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
98936794902SKuninori Morimoto 				link->name);
99036794902SKuninori Morimoto 			return -EINVAL;
99136794902SKuninori Morimoto 		}
99236794902SKuninori Morimoto 
99336794902SKuninori Morimoto 		/*
99436794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
99536794902SKuninori Morimoto 		 * component list.
99636794902SKuninori Morimoto 		 */
99736794902SKuninori Morimoto 		if (!soc_find_component(codec))
99836794902SKuninori Morimoto 			return -EPROBE_DEFER;
99936794902SKuninori Morimoto 	}
100036794902SKuninori Morimoto 
100136794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
100236794902SKuninori Morimoto 		/*
100336794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
100436794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
100536794902SKuninori Morimoto 		 * in the rtdcom list
100636794902SKuninori Morimoto 		 */
100736794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
100836794902SKuninori Morimoto 			dev_err(card->dev,
100936794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
101036794902SKuninori Morimoto 				link->name);
101136794902SKuninori Morimoto 			return -EINVAL;
101236794902SKuninori Morimoto 		}
101336794902SKuninori Morimoto 
101436794902SKuninori Morimoto 		/*
101536794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
101636794902SKuninori Morimoto 		 * component list.
101736794902SKuninori Morimoto 		 */
101836794902SKuninori Morimoto 		if (!soc_find_component(platform))
101936794902SKuninori Morimoto 			return -EPROBE_DEFER;
102036794902SKuninori Morimoto 	}
102136794902SKuninori Morimoto 
102236794902SKuninori Morimoto 	/* FIXME */
102336794902SKuninori Morimoto 	if (link->num_cpus > 1) {
102436794902SKuninori Morimoto 		dev_err(card->dev,
102536794902SKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
102636794902SKuninori Morimoto 			link->name);
102736794902SKuninori Morimoto 		return -EINVAL;
102836794902SKuninori Morimoto 	}
102936794902SKuninori Morimoto 
103036794902SKuninori Morimoto 	/*
103136794902SKuninori Morimoto 	 * CPU device may be specified by either name or OF node, but
103236794902SKuninori Morimoto 	 * can be left unspecified, and will be matched based on DAI
103336794902SKuninori Morimoto 	 * name alone..
103436794902SKuninori Morimoto 	 */
103536794902SKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
103636794902SKuninori Morimoto 		dev_err(card->dev,
103736794902SKuninori Morimoto 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
103836794902SKuninori Morimoto 			link->name);
103936794902SKuninori Morimoto 		return -EINVAL;
104036794902SKuninori Morimoto 	}
104136794902SKuninori Morimoto 
104236794902SKuninori Morimoto 	/*
1043cd3c5ad7SKuninori Morimoto 	 * Defer card registration if cpu dai component is not added to
104436794902SKuninori Morimoto 	 * component list.
104536794902SKuninori Morimoto 	 */
104636794902SKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
104736794902SKuninori Morimoto 	    !soc_find_component(link->cpus))
104836794902SKuninori Morimoto 		return -EPROBE_DEFER;
104936794902SKuninori Morimoto 
105036794902SKuninori Morimoto 	/*
105136794902SKuninori Morimoto 	 * At least one of CPU DAI name or CPU device name/node must be
105236794902SKuninori Morimoto 	 * specified
105336794902SKuninori Morimoto 	 */
105436794902SKuninori Morimoto 	if (!link->cpus->dai_name &&
105536794902SKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
105636794902SKuninori Morimoto 		dev_err(card->dev,
105736794902SKuninori Morimoto 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
105836794902SKuninori Morimoto 			link->name);
105936794902SKuninori Morimoto 		return -EINVAL;
106036794902SKuninori Morimoto 	}
106136794902SKuninori Morimoto 
106236794902SKuninori Morimoto 	return 0;
106336794902SKuninori Morimoto }
106436794902SKuninori Morimoto 
1065bc7a9091SKuninori Morimoto static void soc_unbind_dai_link(struct snd_soc_card *card,
1066bc7a9091SKuninori Morimoto 				struct snd_soc_dai_link *dai_link)
1067bc7a9091SKuninori Morimoto {
1068bc7a9091SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1069bc7a9091SKuninori Morimoto 
1070bc7a9091SKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1071bc7a9091SKuninori Morimoto 	if (rtd)
1072bc7a9091SKuninori Morimoto 		soc_free_pcm_runtime(rtd);
1073bc7a9091SKuninori Morimoto }
1074bc7a9091SKuninori Morimoto 
10756f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10766f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1077db2a4165SFrank Mandarino {
10781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
107934614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
108090be711eSKuninori Morimoto 	struct snd_soc_component *component;
1081bfce78a5SKuninori Morimoto 	int i, ret;
1082db2a4165SFrank Mandarino 
1083a655de80SLiam Girdwood 	if (dai_link->ignore)
1084a655de80SLiam Girdwood 		return 0;
1085a655de80SLiam Girdwood 
10866f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10876308419aSMark Brown 
108849a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
108949a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
109049a5ba1cSMengdong Lin 			dai_link->name);
109149a5ba1cSMengdong Lin 		return 0;
109249a5ba1cSMengdong Lin 	}
1093db2a4165SFrank Mandarino 
1094bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
1095bfce78a5SKuninori Morimoto 	if (ret < 0)
1096bfce78a5SKuninori Morimoto 		return ret;
1097bfce78a5SKuninori Morimoto 
1098513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1099513cb311SSudip Mukherjee 	if (!rtd)
1100513cb311SSudip Mukherjee 		return -ENOMEM;
1101513cb311SSudip Mukherjee 
110208a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
110308a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1104b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
11056b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
110608a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
11071a497983SMengdong Lin 		goto _err_defer;
1108c5af3a2eSMark Brown 	}
110990be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1110c5af3a2eSMark Brown 
111188bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1112e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
111334614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
111434614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
11150a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
11167c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
111734614739SJerome Brunet 				 codec->dai_name);
11181a497983SMengdong Lin 			goto _err_defer;
111912023a9aSMisael Lopez Cruz 		}
112034614739SJerome Brunet 
11210a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
112288bd870fSBenoit Cousson 	}
112388bd870fSBenoit Cousson 
112488bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
11250a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
112612023a9aSMisael Lopez Cruz 
1127e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
112834614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1129368dee94SKuninori Morimoto 		for_each_component(component) {
113034614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
113190be711eSKuninori Morimoto 				continue;
113290be711eSKuninori Morimoto 
113390be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
113490be711eSKuninori Morimoto 		}
113534614739SJerome Brunet 	}
113690be711eSKuninori Morimoto 
1137b19e6e7bSMark Brown 	return 0;
11381a497983SMengdong Lin 
11391a497983SMengdong Lin _err_defer:
11401a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11411a497983SMengdong Lin 	return -EPROBE_DEFER;
11426b05eda6SMark Brown }
11436b05eda6SMark Brown 
1144ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
1145ffd60fbaSKuninori Morimoto {
1146ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1147ffd60fbaSKuninori Morimoto 	const char *str;
1148ffd60fbaSKuninori Morimoto 	int ret;
1149ffd60fbaSKuninori Morimoto 
1150ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1151ffd60fbaSKuninori Morimoto 	if (!ret)
1152ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1153ffd60fbaSKuninori Morimoto }
1154ffd60fbaSKuninori Morimoto 
1155ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1156ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1157ffd60fbaSKuninori Morimoto {
1158ffd60fbaSKuninori Morimoto 	int i;
1159ffd60fbaSKuninori Morimoto 
1160ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1161ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1162ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1163ffd60fbaSKuninori Morimoto 
1164ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1165ffd60fbaSKuninori Morimoto 			continue;
1166ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1167ffd60fbaSKuninori Morimoto 			continue;
1168ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1169ffd60fbaSKuninori Morimoto 		return;
1170ffd60fbaSKuninori Morimoto 	}
1171ffd60fbaSKuninori Morimoto 
1172ffd60fbaSKuninori Morimoto 	/*
1173ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1174ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1175ffd60fbaSKuninori Morimoto 	 */
1176ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1177ffd60fbaSKuninori Morimoto }
1178ffd60fbaSKuninori Morimoto 
117922d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
118022d14231SKuninori Morimoto {
118104f770d9SKuninori Morimoto 	/* For framework level robustness */
11823bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
118304f770d9SKuninori Morimoto 
11847a5d9815SBard liao 	list_del_init(&component->card_list);
118522d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
118622d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
118722d14231SKuninori Morimoto 	component->card = NULL;
11880e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
118922d14231SKuninori Morimoto }
119022d14231SKuninori Morimoto 
1191f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1192d12cd198SStephen Warren {
1193abd31b32SLars-Peter Clausen 	if (!component->card)
119470090bbbSLars-Peter Clausen 		return;
1195d12cd198SStephen Warren 
119603b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
11977a5d9815SBard liao 
119822d14231SKuninori Morimoto 	soc_cleanup_component(component);
1199d12cd198SStephen Warren }
1200d12cd198SStephen Warren 
1201ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1202ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1203ffd60fbaSKuninori Morimoto {
1204ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1205ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1206ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1207ffd60fbaSKuninori Morimoto 	int ret;
1208ffd60fbaSKuninori Morimoto 
1209ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1210ffd60fbaSKuninori Morimoto 		return 0;
1211ffd60fbaSKuninori Morimoto 
1212ffd60fbaSKuninori Morimoto 	if (component->card) {
1213ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1214ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1215ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1216ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1217ffd60fbaSKuninori Morimoto 			return -ENODEV;
1218ffd60fbaSKuninori Morimoto 		}
1219ffd60fbaSKuninori Morimoto 		return 0;
1220ffd60fbaSKuninori Morimoto 	}
1221ffd60fbaSKuninori Morimoto 
1222ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1223ffd60fbaSKuninori Morimoto 	if (ret < 0)
1224ffd60fbaSKuninori Morimoto 		return ret;
1225ffd60fbaSKuninori Morimoto 
1226ffd60fbaSKuninori Morimoto 	component->card = card;
1227ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1228ffd60fbaSKuninori Morimoto 
1229ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1230ffd60fbaSKuninori Morimoto 
123195c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1232b614beafSKuninori Morimoto 
1233ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1234ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1235ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1236ffd60fbaSKuninori Morimoto 
1237ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1238ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1239ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1240ffd60fbaSKuninori Morimoto 		goto err_probe;
1241ffd60fbaSKuninori Morimoto 	}
1242ffd60fbaSKuninori Morimoto 
1243ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1244ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1245ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1246ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1247ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1248ffd60fbaSKuninori Morimoto 			goto err_probe;
1249ffd60fbaSKuninori Morimoto 		}
1250ffd60fbaSKuninori Morimoto 	}
1251ffd60fbaSKuninori Morimoto 
1252ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1253ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1254ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1255ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1256ffd60fbaSKuninori Morimoto 		goto err_probe;
1257ffd60fbaSKuninori Morimoto 	}
1258ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1259ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1260ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1261ffd60fbaSKuninori Morimoto 	     component->name);
1262ffd60fbaSKuninori Morimoto 
1263ffd60fbaSKuninori Morimoto 	/* machine specific init */
1264ffd60fbaSKuninori Morimoto 	if (component->init) {
1265ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1266ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1267ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1268ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1269ffd60fbaSKuninori Morimoto 			goto err_probe;
1270ffd60fbaSKuninori Morimoto 		}
1271ffd60fbaSKuninori Morimoto 	}
1272ffd60fbaSKuninori Morimoto 
1273ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1274ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1275ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1276ffd60fbaSKuninori Morimoto 	if (ret < 0)
1277ffd60fbaSKuninori Morimoto 		goto err_probe;
1278ffd60fbaSKuninori Morimoto 
1279ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1280ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1281ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1282ffd60fbaSKuninori Morimoto 	if (ret < 0)
1283ffd60fbaSKuninori Morimoto 		goto err_probe;
1284ffd60fbaSKuninori Morimoto 
1285ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1286ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1287ffd60fbaSKuninori Morimoto 
1288ffd60fbaSKuninori Morimoto err_probe:
1289ffd60fbaSKuninori Morimoto 	if (ret < 0)
1290ffd60fbaSKuninori Morimoto 		soc_cleanup_component(component);
1291ffd60fbaSKuninori Morimoto 
1292ffd60fbaSKuninori Morimoto 	return ret;
1293ffd60fbaSKuninori Morimoto }
1294ffd60fbaSKuninori Morimoto 
1295e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1296589c3563SJarkko Nikula {
1297589c3563SJarkko Nikula 	int err;
1298589c3563SJarkko Nikula 
129952abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
13002eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
13012eda3cb1SKuninori Morimoto 		return;
13022eda3cb1SKuninori Morimoto 
1303dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1304589c3563SJarkko Nikula 	if (err < 0)
1305e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1306b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1307e60cd14fSLars-Peter Clausen 			dai->name, err);
1308dcdab582SKuninori Morimoto 
1309e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1310b0aa88afSMisael Lopez Cruz }
1311b0aa88afSMisael Lopez Cruz 
1312a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1313a7d44f78SKuninori Morimoto {
1314a7d44f78SKuninori Morimoto 	int ret;
1315a7d44f78SKuninori Morimoto 
1316a7d44f78SKuninori Morimoto 	if (dai->probed ||
1317a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1318a7d44f78SKuninori Morimoto 		return 0;
1319a7d44f78SKuninori Morimoto 
1320a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1321a7d44f78SKuninori Morimoto 	if (ret < 0) {
1322a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1323a7d44f78SKuninori Morimoto 			dai->name, ret);
1324a7d44f78SKuninori Morimoto 		return ret;
1325a7d44f78SKuninori Morimoto 	}
1326a7d44f78SKuninori Morimoto 
1327a7d44f78SKuninori Morimoto 	dai->probed = 1;
1328a7d44f78SKuninori Morimoto 
1329a7d44f78SKuninori Morimoto 	return 0;
1330a7d44f78SKuninori Morimoto }
1331a7d44f78SKuninori Morimoto 
13324ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1333f0fba2adSLiam Girdwood {
1334e60cd14fSLars-Peter Clausen 	int i;
13350b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
13364ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13374ca47d21SKuninori Morimoto 	int order;
13384ca47d21SKuninori Morimoto 
13394ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13404ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1341f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
13420b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
13430b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1344f0fba2adSLiam Girdwood 
1345e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1346f0fba2adSLiam Girdwood 		}
13474ca47d21SKuninori Morimoto 	}
13484ca47d21SKuninori Morimoto }
1349f0fba2adSLiam Girdwood 
1350bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1351bc7c16c2SKuninori Morimoto {
1352bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1353bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1354bc7c16c2SKuninori Morimoto 	int i, order, ret;
1355bc7c16c2SKuninori Morimoto 
1356bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1357bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1358bc7c16c2SKuninori Morimoto 
1359bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1360bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1361bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1362bc7c16c2SKuninori Morimoto 
1363bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1364bc7c16c2SKuninori Morimoto 			if (ret)
1365bc7c16c2SKuninori Morimoto 				return ret;
1366bc7c16c2SKuninori Morimoto 
1367bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1368bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1369bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1370bc7c16c2SKuninori Morimoto 				if (ret)
1371bc7c16c2SKuninori Morimoto 					return ret;
1372bc7c16c2SKuninori Morimoto 			}
1373bc7c16c2SKuninori Morimoto 		}
1374bc7c16c2SKuninori Morimoto 	}
1375bc7c16c2SKuninori Morimoto 
1376bc7c16c2SKuninori Morimoto 	return 0;
1377bc7c16c2SKuninori Morimoto }
1378bc7c16c2SKuninori Morimoto 
1379b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
138062ae68faSStephen Warren {
138161aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1382b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
138390be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1384b006c0c6SKuninori Morimoto 	int order;
138562ae68faSStephen Warren 
1386b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1387b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13882b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
1389b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1390b006c0c6SKuninori Morimoto 					continue;
1391b006c0c6SKuninori Morimoto 
139261aca564SLars-Peter Clausen 				soc_remove_component(component);
139362ae68faSStephen Warren 			}
139462ae68faSStephen Warren 		}
1395b006c0c6SKuninori Morimoto 	}
1396b006c0c6SKuninori Morimoto }
139762ae68faSStephen Warren 
139862f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
13996fb03550SKuninori Morimoto {
14006fb03550SKuninori Morimoto 	struct snd_soc_component *component;
140162f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
14026fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
140362f07a6bSKuninori Morimoto 	int ret, order;
14046fb03550SKuninori Morimoto 
140562f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
140662f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14072b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
140862f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
140962f07a6bSKuninori Morimoto 					continue;
141062f07a6bSKuninori Morimoto 
14116fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
14126fb03550SKuninori Morimoto 				if (ret < 0)
14136fb03550SKuninori Morimoto 					return ret;
14146fb03550SKuninori Morimoto 			}
14156fb03550SKuninori Morimoto 		}
141662f07a6bSKuninori Morimoto 	}
14176fb03550SKuninori Morimoto 
14186fb03550SKuninori Morimoto 	return 0;
14196fb03550SKuninori Morimoto }
14206fb03550SKuninori Morimoto 
1421ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1422ef2e8175SKuninori Morimoto {
14232c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
14242c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1425ef2e8175SKuninori Morimoto 
1426ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1427ef2e8175SKuninori Morimoto 		return;
1428ef2e8175SKuninori Morimoto 
1429ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1430ef2e8175SKuninori Morimoto }
1431df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1432ef2e8175SKuninori Morimoto 
1433f8f80361SMengdong Lin /**
1434f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1435f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1436f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1437f8f80361SMengdong Lin  *
1438f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1439f8f80361SMengdong Lin  *
1440f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1441f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1442f8f80361SMengdong Lin  * DAI links in dai_link array.
1443f8f80361SMengdong Lin  */
1444f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1445f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1446f8f80361SMengdong Lin {
14476b1dff02SKuninori Morimoto 	int ret;
14486b1dff02SKuninori Morimoto 
1449f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1450237d1908SKuninori Morimoto 
14512c7b696aSMarcel Ziswiler 	/*
14522c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1453d6f220eaSMengdong Lin 	 */
1454237d1908SKuninori Morimoto 	if (card->add_dai_link)
1455d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1456d6f220eaSMengdong Lin 
14576b1dff02SKuninori Morimoto 	ret = soc_bind_dai_link(card, dai_link);
14586b1dff02SKuninori Morimoto 	if (ret < 0)
14596b1dff02SKuninori Morimoto 		return ret;
14606b1dff02SKuninori Morimoto 
14616634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1462f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1463f8f80361SMengdong Lin 
1464f8f80361SMengdong Lin 	return 0;
1465f8f80361SMengdong Lin }
1466f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1467f8f80361SMengdong Lin 
1468f8f80361SMengdong Lin /**
1469f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1470f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1471f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1472f8f80361SMengdong Lin  *
1473f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1474f8f80361SMengdong Lin  *
1475f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1476f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1477f8f80361SMengdong Lin  */
1478f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1479f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1480f8f80361SMengdong Lin {
1481f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1482237d1908SKuninori Morimoto 
14832c7b696aSMarcel Ziswiler 	/*
14842c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1485d6f220eaSMengdong Lin 	 */
1486237d1908SKuninori Morimoto 	if (card->remove_dai_link)
1487d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1488d6f220eaSMengdong Lin 
1489c26a8841SKuninori Morimoto 	list_del(&dai_link->list);
1490bc7a9091SKuninori Morimoto 
1491bc7a9091SKuninori Morimoto 	soc_unbind_dai_link(card, dai_link);
1492f8f80361SMengdong Lin }
1493f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
14940671fd8eSKuninori Morimoto 
149525f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
149625f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
149725f7b701SArnaud Pouliquen {
149825f7b701SArnaud Pouliquen 	int i, ret = 0;
149925f7b701SArnaud Pouliquen 
150025f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
150125f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
150225f7b701SArnaud Pouliquen 
1503de17f14eSRohit kumar 		if (drv->pcm_new)
150425f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
150525f7b701SArnaud Pouliquen 		if (ret < 0) {
150625f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
150725f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
150825f7b701SArnaud Pouliquen 				dais[i]->name);
150925f7b701SArnaud Pouliquen 			return ret;
151025f7b701SArnaud Pouliquen 		}
151125f7b701SArnaud Pouliquen 	}
151225f7b701SArnaud Pouliquen 
151325f7b701SArnaud Pouliquen 	return 0;
151425f7b701SArnaud Pouliquen }
151525f7b701SArnaud Pouliquen 
1516c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1517c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1518c4b46982SKuninori Morimoto {
1519c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1520c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1521c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1522c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1523c4b46982SKuninori Morimoto 	int ret, num;
1524c4b46982SKuninori Morimoto 
1525c4b46982SKuninori Morimoto 	/* set default power off timeout */
1526c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
15270168bf0dSLiam Girdwood 
15285f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15295f3484acSLars-Peter Clausen 	if (dai_link->init) {
15305f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15315f3484acSLars-Peter Clausen 		if (ret < 0) {
15325f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15335f3484acSLars-Peter Clausen 				dai_link->name, ret);
15345f3484acSLars-Peter Clausen 			return ret;
15355f3484acSLars-Peter Clausen 		}
15365f3484acSLars-Peter Clausen 	}
15375f3484acSLars-Peter Clausen 
153840aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
153940aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
154040aa5383SRicard Wanderlof 		if (ret)
154140aa5383SRicard Wanderlof 			return ret;
154240aa5383SRicard Wanderlof 	}
1543a5053a8eSKuninori Morimoto 
15445f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15452e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
15465f3484acSLars-Peter Clausen 
1547a655de80SLiam Girdwood 	num = rtd->num;
1548a655de80SLiam Girdwood 
1549a655de80SLiam Girdwood 	/*
1550a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1551a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1552a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1553a655de80SLiam Girdwood 	 */
15542b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
1555a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1556a655de80SLiam Girdwood 			continue;
1557a655de80SLiam Girdwood 
1558a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1559a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1560a655de80SLiam Girdwood 		else
1561a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1562a655de80SLiam Girdwood 	}
1563a655de80SLiam Girdwood 
1564b423c420SKuninori Morimoto 	/* create compress_device if possible */
1565b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1566b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1567b423c420SKuninori Morimoto 		if (ret < 0)
1568f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15691245b700SNamarta Kohli 					 dai_link->stream_name);
15701245b700SNamarta Kohli 		return ret;
15711245b700SNamarta Kohli 	}
1572b423c420SKuninori Morimoto 
1573f0fba2adSLiam Girdwood 	/* create the pcm */
1574a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1575f0fba2adSLiam Girdwood 	if (ret < 0) {
1576f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15770837fc62SFabio Estevam 			dai_link->stream_name, ret);
1578f0fba2adSLiam Girdwood 		return ret;
1579f0fba2adSLiam Girdwood 	}
158025f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
158125f7b701SArnaud Pouliquen 	if (ret < 0)
158225f7b701SArnaud Pouliquen 		return ret;
158325f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
158425f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
158525f7b701SArnaud Pouliquen 	return ret;
1586f0fba2adSLiam Girdwood }
1587f0fba2adSLiam Girdwood 
1588e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15894893a2ebSKuninori Morimoto {
1590e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1591e8fbd250SKuninori Morimoto 
1592e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
15934893a2ebSKuninori Morimoto 		component->init = NULL;
15944893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15954893a2ebSKuninori Morimoto 	}
1596e8fbd250SKuninori Morimoto }
15974893a2ebSKuninori Morimoto 
1598bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1599b19e6e7bSMark Brown {
1600f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1601bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1602bee886f1SKuninori Morimoto 	int i;
16033ca041edSSebastian Reichel 
1604bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1605f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1606bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1607f2ed6b07SMengdong Lin 		if (!component)
16083dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
16093ca041edSSebastian Reichel 
1610bee886f1SKuninori Morimoto 		component->init = aux->init;
1611c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1612d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1613bee886f1SKuninori Morimoto 	}
1614f2ed6b07SMengdong Lin 	return 0;
1615b19e6e7bSMark Brown }
1616b19e6e7bSMark Brown 
1617f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1618f2ed6b07SMengdong Lin {
1619991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1620f2ed6b07SMengdong Lin 	int order;
1621f2ed6b07SMengdong Lin 	int ret;
1622f2ed6b07SMengdong Lin 
16231a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1624c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1625f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1626f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1627f2ed6b07SMengdong Lin 				if (ret < 0) {
1628f2ed6b07SMengdong Lin 					dev_err(card->dev,
1629f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1630f2ed6b07SMengdong Lin 						comp->name, ret);
1631f2ed6b07SMengdong Lin 					return ret;
1632f2ed6b07SMengdong Lin 				}
1633f2ed6b07SMengdong Lin 			}
1634f2ed6b07SMengdong Lin 		}
1635f2ed6b07SMengdong Lin 	}
163665d9361fSLars-Peter Clausen 
163744c69bb1SLars-Peter Clausen 	return 0;
16383ca041edSSebastian Reichel }
16392eea392dSJarkko Nikula 
1640f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
164144c69bb1SLars-Peter Clausen {
1642f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1643f2ed6b07SMengdong Lin 	int order;
164444c69bb1SLars-Peter Clausen 
16451a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1646c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1647e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1648f2ed6b07SMengdong Lin 				soc_remove_component(comp);
16495f3484acSLars-Peter Clausen 		}
16502eea392dSJarkko Nikula 	}
16512eea392dSJarkko Nikula }
16522eea392dSJarkko Nikula 
1653ce64c8b9SLars-Peter Clausen /**
1654ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1655ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1656ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1657ce64c8b9SLars-Peter Clausen  *
1658ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1659ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1660ce64c8b9SLars-Peter Clausen  *
1661ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1662ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1663ce64c8b9SLars-Peter Clausen  *
1664ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1665ce64c8b9SLars-Peter Clausen  */
1666ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1667ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1668ce64c8b9SLars-Peter Clausen {
1669ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16700b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1671ce64c8b9SLars-Peter Clausen 	unsigned int i;
1672ce64c8b9SLars-Peter Clausen 	int ret;
1673ce64c8b9SLars-Peter Clausen 
16740b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1675ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1676ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1677ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1678ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1679ce64c8b9SLars-Peter Clausen 			return ret;
1680ce64c8b9SLars-Peter Clausen 		}
1681ce64c8b9SLars-Peter Clausen 	}
1682ce64c8b9SLars-Peter Clausen 
16832c7b696aSMarcel Ziswiler 	/*
16842c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16852c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16862c7b696aSMarcel Ziswiler 	 */
1687999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1688ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1689ce64c8b9SLars-Peter Clausen 
1690ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1691ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1692ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1693ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1694ce64c8b9SLars-Peter Clausen 			break;
1695ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1696ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1697ce64c8b9SLars-Peter Clausen 			break;
1698ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1699ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1700ce64c8b9SLars-Peter Clausen 			break;
1701ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1702ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1703ce64c8b9SLars-Peter Clausen 			break;
1704ce64c8b9SLars-Peter Clausen 		}
1705ce64c8b9SLars-Peter Clausen 
1706ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1707ce64c8b9SLars-Peter Clausen 	}
1708ce64c8b9SLars-Peter Clausen 
1709ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1710ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1711ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1712ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1713ce64c8b9SLars-Peter Clausen 		return ret;
1714ce64c8b9SLars-Peter Clausen 	}
1715ce64c8b9SLars-Peter Clausen 
1716ce64c8b9SLars-Peter Clausen 	return 0;
1717ce64c8b9SLars-Peter Clausen }
1718ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1719ce64c8b9SLars-Peter Clausen 
17201f5a4535STakashi Iwai #ifdef CONFIG_DMI
17212c7b696aSMarcel Ziswiler /*
17222c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1723345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1724345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1725345233d7SLiam Girdwood  */
1726345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1727345233d7SLiam Girdwood {
1728345233d7SLiam Girdwood 	int i, j = 0;
1729345233d7SLiam Girdwood 
1730345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1731345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1732345233d7SLiam Girdwood 		    || (name[i] == '_'))
1733345233d7SLiam Girdwood 			name[j++] = name[i];
1734345233d7SLiam Girdwood 		else if (name[i] == '-')
1735345233d7SLiam Girdwood 			name[j++] = '_';
1736345233d7SLiam Girdwood 	}
1737345233d7SLiam Girdwood 
1738345233d7SLiam Girdwood 	name[j] = '\0';
1739345233d7SLiam Girdwood }
1740345233d7SLiam Girdwood 
17412c7b696aSMarcel Ziswiler /*
17422c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
174398faf436SMengdong Lin  * in the black list.
174498faf436SMengdong Lin  */
174598faf436SMengdong Lin static int is_dmi_valid(const char *field)
174698faf436SMengdong Lin {
174798faf436SMengdong Lin 	int i = 0;
174898faf436SMengdong Lin 
174998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
175098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
175198faf436SMengdong Lin 			return 0;
175298faf436SMengdong Lin 		i++;
175346b5a4d2SWu Fengguang 	}
175498faf436SMengdong Lin 
175598faf436SMengdong Lin 	return 1;
175698faf436SMengdong Lin }
175798faf436SMengdong Lin 
1758345233d7SLiam Girdwood /**
1759345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1760345233d7SLiam Girdwood  * @card: The card to register DMI names
1761345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1762345233d7SLiam Girdwood  *
1763345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1764345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1765345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1766345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1767345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1768345233d7SLiam Girdwood  * as the sound card long name, in the format of
1769345233d7SLiam Girdwood  * "vendor-product-version-board"
1770345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1771345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1772345233d7SLiam Girdwood  * (UCM) configurations for the card.
1773345233d7SLiam Girdwood  *
1774345233d7SLiam Girdwood  * Possible card long names may be:
1775345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1776345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1777345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1778345233d7SLiam Girdwood  *
1779345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1780345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1781345233d7SLiam Girdwood  *
1782345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1783345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1784345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1785345233d7SLiam Girdwood  * charactors like SPACE.
1786345233d7SLiam Girdwood  *
1787345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1788345233d7SLiam Girdwood  */
1789345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1790345233d7SLiam Girdwood {
1791345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1792345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1793345233d7SLiam Girdwood 	size_t len;
1794345233d7SLiam Girdwood 
1795345233d7SLiam Girdwood 	if (card->long_name)
1796345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1797345233d7SLiam Girdwood 
1798345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1799345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
180098faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1801345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1802345233d7SLiam Girdwood 		return 0;
1803345233d7SLiam Girdwood 	}
1804345233d7SLiam Girdwood 
1805345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1806345233d7SLiam Girdwood 			 "%s", vendor);
1807345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1808345233d7SLiam Girdwood 
1809345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
181098faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1811345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1812345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1813345233d7SLiam Girdwood 			 longname_buf_size - len,
1814345233d7SLiam Girdwood 			 "-%s", product);
1815345233d7SLiam Girdwood 
1816345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1817345233d7SLiam Girdwood 		if (len < longname_buf_size)
1818345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1819345233d7SLiam Girdwood 
18202c7b696aSMarcel Ziswiler 		/*
18212c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1822345233d7SLiam Girdwood 		 * name in the product version field
1823345233d7SLiam Girdwood 		 */
1824345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
182598faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1826345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1827345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1828345233d7SLiam Girdwood 				 longname_buf_size - len,
1829345233d7SLiam Girdwood 				 "-%s", product_version);
1830345233d7SLiam Girdwood 
1831345233d7SLiam Girdwood 			len++;
1832345233d7SLiam Girdwood 			if (len < longname_buf_size)
1833345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1834345233d7SLiam Girdwood 		}
1835345233d7SLiam Girdwood 	}
1836345233d7SLiam Girdwood 
1837345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
183898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1839345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1840345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1841345233d7SLiam Girdwood 			 longname_buf_size - len,
1842345233d7SLiam Girdwood 			 "-%s", board);
1843345233d7SLiam Girdwood 
1844345233d7SLiam Girdwood 		len++;
1845345233d7SLiam Girdwood 		if (len < longname_buf_size)
1846345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1847345233d7SLiam Girdwood 	} else if (!product) {
1848345233d7SLiam Girdwood 		/* fall back to using legacy name */
1849345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1850345233d7SLiam Girdwood 		return 0;
1851345233d7SLiam Girdwood 	}
1852345233d7SLiam Girdwood 
1853345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1854345233d7SLiam Girdwood 	if (flavour) {
1855345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1856345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1857345233d7SLiam Girdwood 			 longname_buf_size - len,
1858345233d7SLiam Girdwood 			 "-%s", flavour);
1859345233d7SLiam Girdwood 
1860345233d7SLiam Girdwood 		len++;
1861345233d7SLiam Girdwood 		if (len < longname_buf_size)
1862345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1863345233d7SLiam Girdwood 	}
1864345233d7SLiam Girdwood 
1865345233d7SLiam Girdwood 	/* set the card long name */
1866345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1867345233d7SLiam Girdwood 
1868345233d7SLiam Girdwood 	return 0;
1869345233d7SLiam Girdwood }
1870345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18711f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1872345233d7SLiam Girdwood 
1873a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1874a655de80SLiam Girdwood {
1875a655de80SLiam Girdwood 	struct snd_soc_component *component;
1876a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1877a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1878a655de80SLiam Girdwood 	int i;
1879a655de80SLiam Girdwood 
1880368dee94SKuninori Morimoto 	for_each_component(component) {
1881a655de80SLiam Girdwood 
188249f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1883a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1884a655de80SLiam Girdwood 			continue;
1885a655de80SLiam Girdwood 
1886a655de80SLiam Girdwood 		/* for this machine ? */
1887e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1888a655de80SLiam Girdwood 			    card->dev->driver->name))
1889e194098bSPierre-Louis Bossart 			goto match;
1890e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1891e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1892a655de80SLiam Girdwood 			continue;
1893e194098bSPierre-Louis Bossart match:
1894a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18957fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1896a655de80SLiam Girdwood 
1897a655de80SLiam Girdwood 			/* ignore this FE */
1898a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1899a655de80SLiam Girdwood 				dai_link->ignore = true;
1900a655de80SLiam Girdwood 				continue;
1901a655de80SLiam Girdwood 			}
1902a655de80SLiam Girdwood 
190349f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1904a655de80SLiam Girdwood 				 card->dai_link[i].name);
1905a655de80SLiam Girdwood 
1906a655de80SLiam Girdwood 			/* override platform component */
1907adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1908daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1909daecf46eSKuninori Morimoto 				continue;
1910daecf46eSKuninori Morimoto 			}
1911910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1912a655de80SLiam Girdwood 
1913a655de80SLiam Girdwood 			/* convert non BE into BE */
1914a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1915a655de80SLiam Girdwood 
1916a655de80SLiam Girdwood 			/* override any BE fixups */
1917a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1918a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1919a655de80SLiam Girdwood 
19202c7b696aSMarcel Ziswiler 			/*
19212c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1922a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1923a655de80SLiam Girdwood 			 */
1924a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1925a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1926a655de80SLiam Girdwood 		}
1927a655de80SLiam Girdwood 
1928a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1929a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1930a655de80SLiam Girdwood 
1931a655de80SLiam Girdwood 			/* topology shortname created? */
1932a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1933a655de80SLiam Girdwood 				comp_drv = component->driver;
1934a655de80SLiam Girdwood 
1935a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1936a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1937a655de80SLiam Girdwood 					 card->name);
1938a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1939a655de80SLiam Girdwood 			}
1940a655de80SLiam Girdwood 
1941a655de80SLiam Girdwood 			/* use topology shortname */
1942a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1943a655de80SLiam Girdwood 		}
1944a655de80SLiam Girdwood 	}
1945a655de80SLiam Girdwood }
1946a655de80SLiam Girdwood 
19470f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
19480f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
19490f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
19500f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
19510f23f718SKuninori Morimoto 				  int normalization)
19520f23f718SKuninori Morimoto {
19530f23f718SKuninori Morimoto 	int i;
19540f23f718SKuninori Morimoto 
19550f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
19560f23f718SKuninori Morimoto 
19570f23f718SKuninori Morimoto 	if (!normalization)
19580f23f718SKuninori Morimoto 		return;
19590f23f718SKuninori Morimoto 
19600f23f718SKuninori Morimoto 	/*
19610f23f718SKuninori Morimoto 	 * Name normalization
19620f23f718SKuninori Morimoto 	 *
19630f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
19640f23f718SKuninori Morimoto 	 * searches in the user-space.
19650f23f718SKuninori Morimoto 	 *
19660f23f718SKuninori Morimoto 	 * ex)
19670f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
19680f23f718SKuninori Morimoto 	 */
19690f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
19700f23f718SKuninori Morimoto 		switch (name[i]) {
19710f23f718SKuninori Morimoto 		case '_':
19720f23f718SKuninori Morimoto 		case '-':
19730f23f718SKuninori Morimoto 		case '\0':
19740f23f718SKuninori Morimoto 			break;
19750f23f718SKuninori Morimoto 		default:
19760f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19770f23f718SKuninori Morimoto 				name[i] = '_';
19780f23f718SKuninori Morimoto 			break;
19790f23f718SKuninori Morimoto 		}
19800f23f718SKuninori Morimoto 	}
19810f23f718SKuninori Morimoto }
19820f23f718SKuninori Morimoto 
1983a4de83a3SKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
198453e947a0SKuninori Morimoto {
19857ce6088fSKuninori Morimoto 	struct snd_soc_dai_link *link, *_link;
19867ce6088fSKuninori Morimoto 
1987df95a16dSPierre-Louis Bossart 	/* This should be called before snd_card_free() */
1988df95a16dSPierre-Louis Bossart 	soc_remove_link_components(card);
1989df95a16dSPierre-Louis Bossart 
199053e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
199129040d1aSKuninori Morimoto 	if (card->snd_card) {
199253e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
199329040d1aSKuninori Morimoto 		card->snd_card = NULL;
199429040d1aSKuninori Morimoto 	}
199553e947a0SKuninori Morimoto 
199653e947a0SKuninori Morimoto 	/* remove and free each DAI */
19977ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19987ce6088fSKuninori Morimoto 
19997ce6088fSKuninori Morimoto 	for_each_card_links_safe(card, link, _link)
20007ce6088fSKuninori Morimoto 		snd_soc_remove_dai_link(card, link);
20017ce6088fSKuninori Morimoto 
200253e947a0SKuninori Morimoto 	/* remove auxiliary devices */
200353e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
2004e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
200553e947a0SKuninori Morimoto 
200653e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
200753e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
200853e947a0SKuninori Morimoto 
200953e947a0SKuninori Morimoto 	/* remove the card */
201053e947a0SKuninori Morimoto 	if (card->remove)
201153e947a0SKuninori Morimoto 		card->remove(card);
201253e947a0SKuninori Morimoto }
201353e947a0SKuninori Morimoto 
2014b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2015f0fba2adSLiam Girdwood {
20161a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
201761b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2018c7e73774SKuninori Morimoto 	int ret, i;
201996dd3622SMark Brown 
202034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
202101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2022f0fba2adSLiam Girdwood 
202395c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
202453e947a0SKuninori Morimoto 
2025a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
2026a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
2027a655de80SLiam Girdwood 
202844c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2029bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
2030bee886f1SKuninori Morimoto 	if (ret < 0)
203153e947a0SKuninori Morimoto 		goto probe_end;
2032db2a4165SFrank Mandarino 
2033f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2034d8145989SKuninori Morimoto 	card->num_rtd = 0;
20355b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20365b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
20375b99a0aaSKuninori Morimoto 		if (ret < 0)
20385b99a0aaSKuninori Morimoto 			goto probe_end;
20395b99a0aaSKuninori Morimoto 	}
2040f8f80361SMengdong Lin 
2041f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2042102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2043f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2044f0fba2adSLiam Girdwood 	if (ret < 0) {
204510e8aa9aSMichał Mirosław 		dev_err(card->dev,
204610e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
204710e8aa9aSMichał Mirosław 			card->name, ret);
204853e947a0SKuninori Morimoto 		goto probe_end;
2049db2a4165SFrank Mandarino 	}
2050db2a4165SFrank Mandarino 
20510757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20520757d834SLars-Peter Clausen 
2053b3da4251SKuninori Morimoto 	soc_resume_init(card);
20546ed25978SAndy Green 
2055b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20569a841ebbSMark Brown 					card->num_dapm_widgets);
2057b8ba3b57SKuninori Morimoto 	if (ret < 0)
2058b8ba3b57SKuninori Morimoto 		goto probe_end;
20599a841ebbSMark Brown 
2060b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2061f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2062b8ba3b57SKuninori Morimoto 	if (ret < 0)
2063b8ba3b57SKuninori Morimoto 		goto probe_end;
2064f23e860eSNicolin Chen 
2065f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2066f0fba2adSLiam Girdwood 	if (card->probe) {
2067e7361ec4SMark Brown 		ret = card->probe(card);
2068f0fba2adSLiam Girdwood 		if (ret < 0)
206953e947a0SKuninori Morimoto 			goto probe_end;
2070f0fba2adSLiam Girdwood 	}
2071f0fba2adSLiam Girdwood 
207262ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
207362f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
207462ae68faSStephen Warren 	if (ret < 0) {
2075f110bfc7SLiam Girdwood 		dev_err(card->dev,
207662f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
207753e947a0SKuninori Morimoto 		goto probe_end;
207862ae68faSStephen Warren 	}
207962ae68faSStephen Warren 
2080f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2081f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2082f2ed6b07SMengdong Lin 	if (ret < 0)
208353e947a0SKuninori Morimoto 		goto probe_end;
2084f2ed6b07SMengdong Lin 
208562ae68faSStephen Warren 	/* probe all DAI links on this card */
2086c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2087fe3e78e0SMark Brown 	if (ret < 0) {
2088f110bfc7SLiam Girdwood 		dev_err(card->dev,
2089c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
209053e947a0SKuninori Morimoto 		goto probe_end;
2091fe3e78e0SMark Brown 	}
2092fe3e78e0SMark Brown 
2093c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2094c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2095c4b46982SKuninori Morimoto 
2096888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2097b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2098888df395SMark Brown 
20999b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
21002c7b696aSMarcel Ziswiler 					card->num_controls);
21019b98c7c2SKuninori Morimoto 	if (ret < 0)
21029b98c7c2SKuninori Morimoto 		goto probe_end;
2103b7af1dafSMark Brown 
2104daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2105b8ad29deSMark Brown 				      card->num_dapm_routes);
2106daa480bdSKuninori Morimoto 	if (ret < 0)
2107daa480bdSKuninori Morimoto 		goto probe_end;
2108b8ad29deSMark Brown 
2109daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2110f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2111daa480bdSKuninori Morimoto 	if (ret < 0)
2112daa480bdSKuninori Morimoto 		goto probe_end;
211375d9ac46SMark Brown 
2114861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2115861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2116861886d3STakashi Iwai 
21170f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
21180f23f718SKuninori Morimoto 			    card->name, NULL, 0);
21190f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
21200f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
21210f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
21220f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2123fe3e78e0SMark Brown 
212428e9ad92SMark Brown 	if (card->late_probe) {
212528e9ad92SMark Brown 		ret = card->late_probe(card);
212628e9ad92SMark Brown 		if (ret < 0) {
2127f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
212828e9ad92SMark Brown 				card->name, ret);
212953e947a0SKuninori Morimoto 			goto probe_end;
213028e9ad92SMark Brown 		}
213128e9ad92SMark Brown 	}
213228e9ad92SMark Brown 
2133824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21348c193b8dSLars-Peter Clausen 
2135f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2136fe3e78e0SMark Brown 	if (ret < 0) {
2137f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2138f110bfc7SLiam Girdwood 				ret);
213953e947a0SKuninori Morimoto 		goto probe_end;
2140fe3e78e0SMark Brown 	}
2141fe3e78e0SMark Brown 
2142435c5e25SMark Brown 	card->instantiated = 1;
2143882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21444f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2145b19e6e7bSMark Brown 
214653e947a0SKuninori Morimoto probe_end:
214753e947a0SKuninori Morimoto 	if (ret < 0)
214853e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2149db2a4165SFrank Mandarino 
2150f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
215134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2152db2a4165SFrank Mandarino 
2153b19e6e7bSMark Brown 	return ret;
2154435c5e25SMark Brown }
2155435c5e25SMark Brown 
2156435c5e25SMark Brown /* probes a new socdev */
2157435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2158435c5e25SMark Brown {
2159f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2160435c5e25SMark Brown 
216170a7ca34SVinod Koul 	/*
216270a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
216370a7ca34SVinod Koul 	 * we should not be here in that case so ret error
216470a7ca34SVinod Koul 	 */
216570a7ca34SVinod Koul 	if (!card)
216670a7ca34SVinod Koul 		return -EINVAL;
216770a7ca34SVinod Koul 
2168fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2169f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2170fe4085e8SMark Brown 		 card->name);
2171fe4085e8SMark Brown 
2172435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2173435c5e25SMark Brown 	card->dev = &pdev->dev;
2174f0fba2adSLiam Girdwood 
217528d528c8SMark Brown 	return snd_soc_register_card(card);
2176435c5e25SMark Brown }
2177435c5e25SMark Brown 
2178b0e26485SVinod Koul /* removes a socdev */
2179b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2180b0e26485SVinod Koul {
2181b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2182b0e26485SVinod Koul 
2183c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2184db2a4165SFrank Mandarino 	return 0;
2185db2a4165SFrank Mandarino }
2186db2a4165SFrank Mandarino 
21876f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
218851737470SMark Brown {
21896f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21901a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
219151737470SMark Brown 
219251737470SMark Brown 	if (!card->instantiated)
2193416356fcSMark Brown 		return 0;
219451737470SMark Brown 
21952c7b696aSMarcel Ziswiler 	/*
21962c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21972c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21982c7b696aSMarcel Ziswiler 	 */
219965462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
220051737470SMark Brown 
2201f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2202416356fcSMark Brown 
2203988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2204bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
220588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22060b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22071a497983SMengdong Lin 		int i;
220888bd870fSBenoit Cousson 
2209988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22100b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
221188bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
221288bd870fSBenoit Cousson 		}
2213988e8cc4SNicolin Chen 	}
2214988e8cc4SNicolin Chen 
2215416356fcSMark Brown 	return 0;
221651737470SMark Brown }
22176f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
221851737470SMark Brown 
22196f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2220b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2221b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2222b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2223b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22246f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2225b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2226416356fcSMark Brown };
2227deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2228416356fcSMark Brown 
2229db2a4165SFrank Mandarino /* ASoC platform driver */
2230db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2231db2a4165SFrank Mandarino 	.driver		= {
2232db2a4165SFrank Mandarino 		.name		= "soc-audio",
22336f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2234db2a4165SFrank Mandarino 	},
2235db2a4165SFrank Mandarino 	.probe		= soc_probe,
2236db2a4165SFrank Mandarino 	.remove		= soc_remove,
2237db2a4165SFrank Mandarino };
2238db2a4165SFrank Mandarino 
2239096e49d5SMark Brown /**
2240db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2241db2a4165SFrank Mandarino  * @_template: control template
2242db2a4165SFrank Mandarino  * @data: control private data
2243ac11a2b3SMark Brown  * @long_name: control long name
2244efb7ac3fSMark Brown  * @prefix: control name prefix
2245db2a4165SFrank Mandarino  *
2246db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2247db2a4165SFrank Mandarino  *
2248db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2249db2a4165SFrank Mandarino  */
2250db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22513056557fSMark Brown 				  void *data, const char *long_name,
2252efb7ac3fSMark Brown 				  const char *prefix)
2253db2a4165SFrank Mandarino {
2254db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2255efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2256efb7ac3fSMark Brown 	char *name = NULL;
2257db2a4165SFrank Mandarino 
2258db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2259db2a4165SFrank Mandarino 	template.index = 0;
2260db2a4165SFrank Mandarino 
2261efb7ac3fSMark Brown 	if (!long_name)
2262efb7ac3fSMark Brown 		long_name = template.name;
2263efb7ac3fSMark Brown 
2264efb7ac3fSMark Brown 	if (prefix) {
22652b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2266efb7ac3fSMark Brown 		if (!name)
2267efb7ac3fSMark Brown 			return NULL;
2268efb7ac3fSMark Brown 
2269efb7ac3fSMark Brown 		template.name = name;
2270efb7ac3fSMark Brown 	} else {
2271efb7ac3fSMark Brown 		template.name = long_name;
2272efb7ac3fSMark Brown 	}
2273efb7ac3fSMark Brown 
2274efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2275efb7ac3fSMark Brown 
2276efb7ac3fSMark Brown 	kfree(name);
2277efb7ac3fSMark Brown 
2278efb7ac3fSMark Brown 	return kcontrol;
2279db2a4165SFrank Mandarino }
2280db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2281db2a4165SFrank Mandarino 
2282022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2283022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2284022658beSLiam Girdwood 	const char *prefix, void *data)
2285022658beSLiam Girdwood {
2286022658beSLiam Girdwood 	int err, i;
2287022658beSLiam Girdwood 
2288022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2289022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22902c7b696aSMarcel Ziswiler 
2291022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2292022658beSLiam Girdwood 						     control->name, prefix));
2293022658beSLiam Girdwood 		if (err < 0) {
2294f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2295f110bfc7SLiam Girdwood 				control->name, err);
2296022658beSLiam Girdwood 			return err;
2297022658beSLiam Girdwood 		}
2298022658beSLiam Girdwood 	}
2299022658beSLiam Girdwood 
2300022658beSLiam Girdwood 	return 0;
2301022658beSLiam Girdwood }
2302022658beSLiam Girdwood 
23034fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23044fefd698SDimitris Papastamos 					       const char *name)
23054fefd698SDimitris Papastamos {
23064fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23074fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23084fefd698SDimitris Papastamos 
23094fefd698SDimitris Papastamos 	if (unlikely(!name))
23104fefd698SDimitris Papastamos 		return NULL;
23114fefd698SDimitris Papastamos 
23124fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23134fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23144fefd698SDimitris Papastamos 			return kctl;
23154fefd698SDimitris Papastamos 	return NULL;
23164fefd698SDimitris Papastamos }
23174fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23184fefd698SDimitris Papastamos 
2319db2a4165SFrank Mandarino /**
23200f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23210f2780adSLars-Peter Clausen  *
23220f2780adSLars-Peter Clausen  * @component: Component to add controls to
23230f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23240f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23250f2780adSLars-Peter Clausen  *
23260f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23270f2780adSLars-Peter Clausen  */
23280f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23290f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23300f2780adSLars-Peter Clausen {
23310f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23320f2780adSLars-Peter Clausen 
23330f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23340f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23350f2780adSLars-Peter Clausen }
23360f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23370f2780adSLars-Peter Clausen 
23380f2780adSLars-Peter Clausen /**
2339022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2340022658beSLiam Girdwood  * Convenience function to add a list of controls.
2341022658beSLiam Girdwood  *
2342022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2343022658beSLiam Girdwood  * @controls: array of controls to add
2344022658beSLiam Girdwood  * @num_controls: number of elements in the array
2345022658beSLiam Girdwood  *
2346022658beSLiam Girdwood  * Return 0 for success, else error.
2347022658beSLiam Girdwood  */
2348022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2349022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2350022658beSLiam Girdwood {
2351022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2352022658beSLiam Girdwood 
2353022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2354022658beSLiam Girdwood 			NULL, soc_card);
2355022658beSLiam Girdwood }
2356022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2357022658beSLiam Girdwood 
2358022658beSLiam Girdwood /**
2359022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2360022658beSLiam Girdwood  * Convienience function to add a list of controls.
2361022658beSLiam Girdwood  *
2362022658beSLiam Girdwood  * @dai: DAI to add controls to
2363022658beSLiam Girdwood  * @controls: array of controls to add
2364022658beSLiam Girdwood  * @num_controls: number of elements in the array
2365022658beSLiam Girdwood  *
2366022658beSLiam Girdwood  * Return 0 for success, else error.
2367022658beSLiam Girdwood  */
2368022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2369022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2370022658beSLiam Girdwood {
2371313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2372022658beSLiam Girdwood 
2373022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2374022658beSLiam Girdwood 			NULL, dai);
2375022658beSLiam Girdwood }
2376022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2377022658beSLiam Girdwood 
2378e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2379e894efefSSrinivas Kandagatla {
2380e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2381e894efefSSrinivas Kandagatla 	int ret;
2382e894efefSSrinivas Kandagatla 
2383e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2384e894efefSSrinivas Kandagatla 	if (ret != 0)
2385e894efefSSrinivas Kandagatla 		return ret;
2386e894efefSSrinivas Kandagatla 
2387e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2388bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2389e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2390e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2391e894efefSSrinivas Kandagatla 		int j;
2392e894efefSSrinivas Kandagatla 
2393e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2394e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2395e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2396e894efefSSrinivas Kandagatla 		}
2397e894efefSSrinivas Kandagatla 
2398e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2399e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2400e894efefSSrinivas Kandagatla 	}
2401e894efefSSrinivas Kandagatla 
2402e894efefSSrinivas Kandagatla 	return ret;
2403e894efefSSrinivas Kandagatla }
2404e894efefSSrinivas Kandagatla 
2405c5af3a2eSMark Brown /**
2406c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2407c5af3a2eSMark Brown  *
2408ac11a2b3SMark Brown  * @card: Card to register
2409c5af3a2eSMark Brown  *
2410c5af3a2eSMark Brown  */
241170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2412c5af3a2eSMark Brown {
2413c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2414c5af3a2eSMark Brown 		return -EINVAL;
2415c5af3a2eSMark Brown 
2416ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2417ed77cc12SMark Brown 
2418b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2419b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2420b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2421b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2422b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2423b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2424f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
24251a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2426db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
24278a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2428b03bfaecSKuninori Morimoto 
2429c5af3a2eSMark Brown 	card->instantiated = 0;
2430f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2431a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
243272b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2433a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2434c5af3a2eSMark Brown 
2435e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2436c5af3a2eSMark Brown }
243770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2438c5af3a2eSMark Brown 
2439e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2440e894efefSSrinivas Kandagatla {
2441e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2442e894efefSSrinivas Kandagatla 		card->instantiated = false;
2443e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
244453e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2445f96fb7d1SRanjani Sridharan 
2446e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2447e894efefSSrinivas Kandagatla 		if (!unregister)
2448e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2449e894efefSSrinivas Kandagatla 	} else {
2450e894efefSSrinivas Kandagatla 		if (unregister)
2451e894efefSSrinivas Kandagatla 			list_del(&card->list);
2452e894efefSSrinivas Kandagatla 	}
2453e894efefSSrinivas Kandagatla }
2454e894efefSSrinivas Kandagatla 
2455c5af3a2eSMark Brown /**
2456c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2457c5af3a2eSMark Brown  *
2458ac11a2b3SMark Brown  * @card: Card to unregister
2459c5af3a2eSMark Brown  *
2460c5af3a2eSMark Brown  */
246170a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2462c5af3a2eSMark Brown {
2463b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2464e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2465b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2466f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2467c5af3a2eSMark Brown 
2468c5af3a2eSMark Brown 	return 0;
2469c5af3a2eSMark Brown }
247070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2471c5af3a2eSMark Brown 
2472f0fba2adSLiam Girdwood /*
2473f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2474f0fba2adSLiam Girdwood  * and sanitizing names.
2475f0fba2adSLiam Girdwood  */
24760b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2477f0fba2adSLiam Girdwood {
2478f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2479f0fba2adSLiam Girdwood 	int id1, id2;
2480f0fba2adSLiam Girdwood 
2481f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2482f0fba2adSLiam Girdwood 		return NULL;
2483f0fba2adSLiam Girdwood 
248458818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2485f0fba2adSLiam Girdwood 
2486f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2487f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2488f0fba2adSLiam Girdwood 	if (found) {
2489f0fba2adSLiam Girdwood 		/* get ID */
2490f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2491f0fba2adSLiam Girdwood 
2492f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2493f0fba2adSLiam Girdwood 			if (*id == -1)
2494f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2495f0fba2adSLiam Girdwood 		}
2496f0fba2adSLiam Girdwood 
2497f0fba2adSLiam Girdwood 	} else {
2498f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2499f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2500f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2501f0fba2adSLiam Girdwood 
2502f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
250305899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2504f0fba2adSLiam Girdwood 
2505f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
25062c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
25072c7b696aSMarcel Ziswiler 				 name);
250858818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2509f0fba2adSLiam Girdwood 		} else
2510f0fba2adSLiam Girdwood 			*id = 0;
2511f0fba2adSLiam Girdwood 	}
2512f0fba2adSLiam Girdwood 
251350014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2514f0fba2adSLiam Girdwood }
2515f0fba2adSLiam Girdwood 
2516f0fba2adSLiam Girdwood /*
2517f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2518f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2519f0fba2adSLiam Girdwood  */
2520f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2521f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2522f0fba2adSLiam Girdwood {
2523f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
252410e8aa9aSMichał Mirosław 		dev_err(dev,
252510e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
252610e8aa9aSMichał Mirosław 			dev_name(dev));
2527f0fba2adSLiam Girdwood 		return NULL;
2528f0fba2adSLiam Girdwood 	}
2529f0fba2adSLiam Girdwood 
253050014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2531f0fba2adSLiam Girdwood }
2532f0fba2adSLiam Girdwood 
2533e11381f3SKuninori Morimoto static void soc_del_dai(struct snd_soc_dai *dai)
2534e11381f3SKuninori Morimoto {
2535e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2536e11381f3SKuninori Morimoto 	list_del(&dai->list);
2537e11381f3SKuninori Morimoto }
2538e11381f3SKuninori Morimoto 
25395e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25405e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25415e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25425e4fb372SMengdong Lin 	bool legacy_dai_naming)
25435e4fb372SMengdong Lin {
25445e4fb372SMengdong Lin 	struct device *dev = component->dev;
25455e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25465e4fb372SMengdong Lin 
25475e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25485e4fb372SMengdong Lin 
254950014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
25505e4fb372SMengdong Lin 	if (dai == NULL)
25515e4fb372SMengdong Lin 		return NULL;
25525e4fb372SMengdong Lin 
25535e4fb372SMengdong Lin 	/*
25545e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25555e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25565e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25575e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25585e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25595e4fb372SMengdong Lin 	 * component-less anymore.
25605e4fb372SMengdong Lin 	 */
25615e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25625e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25635e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25645e4fb372SMengdong Lin 	} else {
25655e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25665e4fb372SMengdong Lin 		if (dai_drv->id)
25675e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25685e4fb372SMengdong Lin 		else
25695e4fb372SMengdong Lin 			dai->id = component->num_dai;
25705e4fb372SMengdong Lin 	}
257150014499SKuninori Morimoto 	if (!dai->name)
25725e4fb372SMengdong Lin 		return NULL;
25735e4fb372SMengdong Lin 
25745e4fb372SMengdong Lin 	dai->component = component;
25755e4fb372SMengdong Lin 	dai->dev = dev;
25765e4fb372SMengdong Lin 	dai->driver = dai_drv;
25775e4fb372SMengdong Lin 	if (!dai->driver->ops)
25785e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25795e4fb372SMengdong Lin 
258015a0c645SKuninori Morimoto 	/* see for_each_component_dais */
258158bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25825e4fb372SMengdong Lin 	component->num_dai++;
25835e4fb372SMengdong Lin 
25845e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25855e4fb372SMengdong Lin 	return dai;
25865e4fb372SMengdong Lin }
25875e4fb372SMengdong Lin 
2588e11381f3SKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2589e11381f3SKuninori Morimoto {
2590e11381f3SKuninori Morimoto 	soc_del_dai(dai);
2591e11381f3SKuninori Morimoto }
2592e11381f3SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2593e11381f3SKuninori Morimoto 
25949115171aSMark Brown /**
259568003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
259668003e6cSMengdong Lin  *
259768003e6cSMengdong Lin  * @component: The component the DAIs are registered for
259868003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
259968003e6cSMengdong Lin  *
260068003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
260168003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
260268003e6cSMengdong Lin  * will be freed in the component cleanup.
260368003e6cSMengdong Lin  */
2604e443c205SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
26055d075197SKuninori Morimoto 					 struct snd_soc_dai_driver *dai_drv,
26065d075197SKuninori Morimoto 					 bool legacy_dai_naming)
260768003e6cSMengdong Lin {
260871cb85f5SKuninori Morimoto 	struct device *dev = component->dev;
260971cb85f5SKuninori Morimoto 
261071cb85f5SKuninori Morimoto 	dev_dbg(dev, "ASoC: dai register %s\n", dai_drv->name);
261171cb85f5SKuninori Morimoto 
261268003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
2613e443c205SKuninori Morimoto 	return soc_add_dai(component, dai_drv, legacy_dai_naming);
261468003e6cSMengdong Lin }
261568003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
261668003e6cSMengdong Lin 
2617daf77373SKuninori Morimoto /**
26183f6674aeSKuninori Morimoto  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
26193f6674aeSKuninori Morimoto  *
26203f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
26213f6674aeSKuninori Morimoto  */
26223f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
26233f6674aeSKuninori Morimoto {
26243f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
26253f6674aeSKuninori Morimoto 
2626e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2627e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
26283f6674aeSKuninori Morimoto }
26293f6674aeSKuninori Morimoto 
26303f6674aeSKuninori Morimoto /**
2631daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2632daf77373SKuninori Morimoto  *
2633daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2634daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2635daf77373SKuninori Morimoto  * @count: Number of DAIs
2636daf77373SKuninori Morimoto  */
2637daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2638daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2639daf77373SKuninori Morimoto 				 size_t count)
2640daf77373SKuninori Morimoto {
2641daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2642daf77373SKuninori Morimoto 	unsigned int i;
2643daf77373SKuninori Morimoto 	int ret;
2644daf77373SKuninori Morimoto 
2645daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
264671cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2647daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2648daf77373SKuninori Morimoto 		if (dai == NULL) {
2649daf77373SKuninori Morimoto 			ret = -ENOMEM;
2650daf77373SKuninori Morimoto 			goto err;
2651daf77373SKuninori Morimoto 		}
2652daf77373SKuninori Morimoto 	}
2653daf77373SKuninori Morimoto 
2654daf77373SKuninori Morimoto 	return 0;
2655daf77373SKuninori Morimoto 
2656daf77373SKuninori Morimoto err:
2657daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2658daf77373SKuninori Morimoto 
2659daf77373SKuninori Morimoto 	return ret;
2660daf77373SKuninori Morimoto }
2661daf77373SKuninori Morimoto 
2662bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2663bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2664d191bd8dSKuninori Morimoto {
26658d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2666495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2667495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26688d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26698d92bb51SKuninori Morimoto 
2670bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2671bb13109dSLars-Peter Clausen 	if (!component->name) {
2672bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2673d191bd8dSKuninori Morimoto 		return -ENOMEM;
2674d191bd8dSKuninori Morimoto 	}
2675d191bd8dSKuninori Morimoto 
2676bb13109dSLars-Peter Clausen 	component->dev = dev;
2677bb13109dSLars-Peter Clausen 	component->driver = driver;
2678e2c330b9SLars-Peter Clausen 
2679bb13109dSLars-Peter Clausen 	return 0;
2680d191bd8dSKuninori Morimoto }
2681d191bd8dSKuninori Morimoto 
268220feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2683886f5692SLars-Peter Clausen {
2684886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
268520feb881SLars-Peter Clausen 
2686886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2687886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2688886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2689886f5692SLars-Peter Clausen }
269020feb881SLars-Peter Clausen 
2691e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2692e874bf5fSLars-Peter Clausen 
269320feb881SLars-Peter Clausen /**
26942c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26952c7b696aSMarcel Ziswiler  *                                   component
269620feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
269720feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
269820feb881SLars-Peter Clausen  *
269920feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
270020feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
270120feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
270220feb881SLars-Peter Clausen  * before the first IO attempt of the component.
270320feb881SLars-Peter Clausen  */
270420feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
270520feb881SLars-Peter Clausen 	struct regmap *regmap)
270620feb881SLars-Peter Clausen {
270720feb881SLars-Peter Clausen 	component->regmap = regmap;
270820feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2709886f5692SLars-Peter Clausen }
271020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
271120feb881SLars-Peter Clausen 
271220feb881SLars-Peter Clausen /**
27132c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27142c7b696aSMarcel Ziswiler  *                                   component
271520feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
271620feb881SLars-Peter Clausen  *
271720feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
271820feb881SLars-Peter Clausen  * removes the regmap instance from the component.
271920feb881SLars-Peter Clausen  *
272020feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
272120feb881SLars-Peter Clausen  * to initialize the regmap instance.
272220feb881SLars-Peter Clausen  */
272320feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
272420feb881SLars-Peter Clausen {
272520feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
272620feb881SLars-Peter Clausen 	component->regmap = NULL;
272720feb881SLars-Peter Clausen }
272820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2729886f5692SLars-Peter Clausen 
2730e874bf5fSLars-Peter Clausen #endif
2731d191bd8dSKuninori Morimoto 
2732273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2733273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2734273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2735273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2736273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2737273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2738273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2739273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2740273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2741273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2742273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2743273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2744273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2745273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2746273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2747273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2748273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2749273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2750273d778eSKuninori Morimoto };
2751273d778eSKuninori Morimoto 
2752273d778eSKuninori Morimoto /*
2753273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2754273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2755273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2756273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2757273d778eSKuninori Morimoto  */
2758273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2759273d778eSKuninori Morimoto {
2760273d778eSKuninori Morimoto 	int i;
2761273d778eSKuninori Morimoto 
2762273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2763273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2764273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2765273d778eSKuninori Morimoto }
2766273d778eSKuninori Morimoto 
2767e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2768e894efefSSrinivas Kandagatla {
2769e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2770e894efefSSrinivas Kandagatla 
2771b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2772e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2773e894efefSSrinivas Kandagatla 			list_del(&card->list);
2774e894efefSSrinivas Kandagatla }
2775e894efefSSrinivas Kandagatla 
2776486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2777486c7978SKuninori Morimoto {
2778b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2779b18768f5SKuninori Morimoto 
2780486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2781b18768f5SKuninori Morimoto 
2782b18768f5SKuninori Morimoto 	if (card)
2783b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2784b18768f5SKuninori Morimoto 
2785b18768f5SKuninori Morimoto 	list_del(&component->list);
2786486c7978SKuninori Morimoto }
2787486c7978SKuninori Morimoto 
2788e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2789e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2790cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2791d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2792d191bd8dSKuninori Morimoto 			int num_dai)
2793d191bd8dSKuninori Morimoto {
2794bb13109dSLars-Peter Clausen 	int ret;
2795273d778eSKuninori Morimoto 	int i;
2796d191bd8dSKuninori Morimoto 
2797b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2798b18768f5SKuninori Morimoto 
2799cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2800bb13109dSLars-Peter Clausen 	if (ret)
2801bb13109dSLars-Peter Clausen 		goto err_free;
2802bb13109dSLars-Peter Clausen 
2803273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2804273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2805273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2806273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2807273d778eSKuninori Morimoto 		}
2808273d778eSKuninori Morimoto 	}
2809273d778eSKuninori Morimoto 
28100e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2811bb13109dSLars-Peter Clausen 	if (ret < 0) {
2812f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2813bb13109dSLars-Peter Clausen 		goto err_cleanup;
2814bb13109dSLars-Peter Clausen 	}
2815bb13109dSLars-Peter Clausen 
2816b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2817b18768f5SKuninori Morimoto 		if (!component->regmap)
2818b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2819b18768f5SKuninori Morimoto 							   NULL);
2820b18768f5SKuninori Morimoto 		if (component->regmap)
2821b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2822b18768f5SKuninori Morimoto 	}
2823bb13109dSLars-Peter Clausen 
2824b18768f5SKuninori Morimoto 	/* see for_each_component */
2825b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2826bb13109dSLars-Peter Clausen 
2827bb13109dSLars-Peter Clausen err_cleanup:
2828b18768f5SKuninori Morimoto 	if (ret < 0)
2829486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2830bb13109dSLars-Peter Clausen err_free:
2831b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2832b18768f5SKuninori Morimoto 
2833b18768f5SKuninori Morimoto 	if (ret == 0)
2834b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2835b18768f5SKuninori Morimoto 
2836bb13109dSLars-Peter Clausen 	return ret;
2837d191bd8dSKuninori Morimoto }
2838e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2839e0dac41bSKuninori Morimoto 
2840e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2841e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2842e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2843e0dac41bSKuninori Morimoto 			int num_dai)
2844e0dac41bSKuninori Morimoto {
2845e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2846e0dac41bSKuninori Morimoto 
28477ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
284808e61d03SKuninori Morimoto 	if (!component)
2849e0dac41bSKuninori Morimoto 		return -ENOMEM;
2850e0dac41bSKuninori Morimoto 
2851e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2852e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2853e0dac41bSKuninori Morimoto }
2854d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2855d191bd8dSKuninori Morimoto 
2856d191bd8dSKuninori Morimoto /**
28572eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28582eccea8cSKuninori Morimoto  * from the ASoC core
2859d191bd8dSKuninori Morimoto  *
2860628536eaSJonathan Corbet  * @dev: The device to unregister
2861d191bd8dSKuninori Morimoto  */
28622eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28632eccea8cSKuninori Morimoto {
2864ac6a4dd3SKuninori Morimoto 	struct snd_soc_component *component;
2865ac6a4dd3SKuninori Morimoto 
2866ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2867ac6a4dd3SKuninori Morimoto 	while (1) {
2868*18dd66eaSKuninori Morimoto 		component = snd_soc_lookup_component_nolocked(dev, NULL);
2869ac6a4dd3SKuninori Morimoto 		if (!component)
2870ac6a4dd3SKuninori Morimoto 			break;
2871ac6a4dd3SKuninori Morimoto 
2872ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2873ac6a4dd3SKuninori Morimoto 	}
2874ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2875d191bd8dSKuninori Morimoto }
2876d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2877d191bd8dSKuninori Morimoto 
2878bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2879b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2880bec4fa05SStephen Warren 			       const char *propname)
2881bec4fa05SStephen Warren {
2882b07609ceSKuninori Morimoto 	struct device_node *np;
2883bec4fa05SStephen Warren 	int ret;
2884bec4fa05SStephen Warren 
28857e07e7c0STushar Behera 	if (!card->dev) {
28867e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
28877e07e7c0STushar Behera 		return -EINVAL;
28887e07e7c0STushar Behera 	}
28897e07e7c0STushar Behera 
28907e07e7c0STushar Behera 	np = card->dev->of_node;
28917e07e7c0STushar Behera 
2892bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2893bec4fa05SStephen Warren 	/*
2894bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2895bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2896bec4fa05SStephen Warren 	 * snd_soc_register_card.
2897bec4fa05SStephen Warren 	 */
2898bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2899bec4fa05SStephen Warren 		dev_err(card->dev,
2900f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2901bec4fa05SStephen Warren 			propname, ret);
2902bec4fa05SStephen Warren 		return ret;
2903bec4fa05SStephen Warren 	}
2904bec4fa05SStephen Warren 
2905bec4fa05SStephen Warren 	return 0;
2906bec4fa05SStephen Warren }
2907b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2908bec4fa05SStephen Warren 
29099a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29109a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29119a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29129a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29139a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29149a6d4860SXiubo Li };
29159a6d4860SXiubo Li 
291621efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29179a6d4860SXiubo Li 					  const char *propname)
29189a6d4860SXiubo Li {
291921efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29209a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29219a6d4860SXiubo Li 	const char *template, *wname;
29229a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29239a6d4860SXiubo Li 
29249a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29259a6d4860SXiubo Li 	if (num_widgets < 0) {
29269a6d4860SXiubo Li 		dev_err(card->dev,
29279a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29289a6d4860SXiubo Li 		return -EINVAL;
29299a6d4860SXiubo Li 	}
29309a6d4860SXiubo Li 	if (num_widgets & 1) {
29319a6d4860SXiubo Li 		dev_err(card->dev,
29329a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29339a6d4860SXiubo Li 		return -EINVAL;
29349a6d4860SXiubo Li 	}
29359a6d4860SXiubo Li 
29369a6d4860SXiubo Li 	num_widgets /= 2;
29379a6d4860SXiubo Li 	if (!num_widgets) {
29389a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29399a6d4860SXiubo Li 			propname);
29409a6d4860SXiubo Li 		return -EINVAL;
29419a6d4860SXiubo Li 	}
29429a6d4860SXiubo Li 
29439a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
29449a6d4860SXiubo Li 			       GFP_KERNEL);
29459a6d4860SXiubo Li 	if (!widgets) {
29469a6d4860SXiubo Li 		dev_err(card->dev,
29479a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29489a6d4860SXiubo Li 		return -ENOMEM;
29499a6d4860SXiubo Li 	}
29509a6d4860SXiubo Li 
29519a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
29529a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29539a6d4860SXiubo Li 			2 * i, &template);
29549a6d4860SXiubo Li 		if (ret) {
29559a6d4860SXiubo Li 			dev_err(card->dev,
29569a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29579a6d4860SXiubo Li 				propname, 2 * i, ret);
29589a6d4860SXiubo Li 			return -EINVAL;
29599a6d4860SXiubo Li 		}
29609a6d4860SXiubo Li 
29619a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
29629a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
29639a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
29649a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
29659a6d4860SXiubo Li 				break;
29669a6d4860SXiubo Li 			}
29679a6d4860SXiubo Li 		}
29689a6d4860SXiubo Li 
29699a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
29709a6d4860SXiubo Li 			dev_err(card->dev,
29719a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
29729a6d4860SXiubo Li 				template);
29739a6d4860SXiubo Li 			return -EINVAL;
29749a6d4860SXiubo Li 		}
29759a6d4860SXiubo Li 
29769a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29779a6d4860SXiubo Li 						    (2 * i) + 1,
29789a6d4860SXiubo Li 						    &wname);
29799a6d4860SXiubo Li 		if (ret) {
29809a6d4860SXiubo Li 			dev_err(card->dev,
29819a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29829a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
29839a6d4860SXiubo Li 			return -EINVAL;
29849a6d4860SXiubo Li 		}
29859a6d4860SXiubo Li 
29869a6d4860SXiubo Li 		widgets[i].name = wname;
29879a6d4860SXiubo Li 	}
29889a6d4860SXiubo Li 
2989f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2990f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
29919a6d4860SXiubo Li 
29929a6d4860SXiubo Li 	return 0;
29939a6d4860SXiubo Li }
299421efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
29959a6d4860SXiubo Li 
2996cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
29976131084aSJyri Sarha 			     const char *prop_name,
29986131084aSJyri Sarha 			     unsigned int *mask)
29996131084aSJyri Sarha {
30006131084aSJyri Sarha 	u32 val;
30016c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30026131084aSJyri Sarha 	int i;
30036131084aSJyri Sarha 
30046131084aSJyri Sarha 	if (!of_slot_mask)
30056131084aSJyri Sarha 		return 0;
30066131084aSJyri Sarha 	val /= sizeof(u32);
30076131084aSJyri Sarha 	for (i = 0; i < val; i++)
30086131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30096131084aSJyri Sarha 			*mask |= (1 << i);
30106131084aSJyri Sarha 
30116131084aSJyri Sarha 	return val;
30126131084aSJyri Sarha }
3013cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30146131084aSJyri Sarha 
301589c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30166131084aSJyri Sarha 			      unsigned int *tx_mask,
30176131084aSJyri Sarha 			      unsigned int *rx_mask,
301889c67857SXiubo Li 			      unsigned int *slots,
301989c67857SXiubo Li 			      unsigned int *slot_width)
302089c67857SXiubo Li {
302189c67857SXiubo Li 	u32 val;
302289c67857SXiubo Li 	int ret;
302389c67857SXiubo Li 
30246131084aSJyri Sarha 	if (tx_mask)
30256131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30266131084aSJyri Sarha 	if (rx_mask)
30276131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30286131084aSJyri Sarha 
302989c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
303089c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
303189c67857SXiubo Li 		if (ret)
303289c67857SXiubo Li 			return ret;
303389c67857SXiubo Li 
303489c67857SXiubo Li 		if (slots)
303589c67857SXiubo Li 			*slots = val;
303689c67857SXiubo Li 	}
303789c67857SXiubo Li 
303889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
303989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
304089c67857SXiubo Li 		if (ret)
304189c67857SXiubo Li 			return ret;
304289c67857SXiubo Li 
304389c67857SXiubo Li 		if (slot_width)
304489c67857SXiubo Li 			*slot_width = val;
304589c67857SXiubo Li 	}
304689c67857SXiubo Li 
304789c67857SXiubo Li 	return 0;
304889c67857SXiubo Li }
304989c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
305089c67857SXiubo Li 
30513b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
30525e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
30535e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
30545e3cdaa2SKuninori Morimoto 				  const char *propname)
30555e3cdaa2SKuninori Morimoto {
30565e3cdaa2SKuninori Morimoto 	const char *str;
30575e3cdaa2SKuninori Morimoto 	int ret;
30585e3cdaa2SKuninori Morimoto 
30595e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
30605e3cdaa2SKuninori Morimoto 	if (ret < 0) {
30615e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
30625e3cdaa2SKuninori Morimoto 		return;
30635e3cdaa2SKuninori Morimoto 	}
30645e3cdaa2SKuninori Morimoto 
30655e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
30665e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
30675e3cdaa2SKuninori Morimoto }
30683b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
30695e3cdaa2SKuninori Morimoto 
30702bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3071a4a54dd5SStephen Warren 				   const char *propname)
3072a4a54dd5SStephen Warren {
30732bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3074e3b1e6a1SMark Brown 	int num_routes;
3075a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3076a4a54dd5SStephen Warren 	int i, ret;
3077a4a54dd5SStephen Warren 
3078a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3079c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
308010e8aa9aSMichał Mirosław 		dev_err(card->dev,
308110e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
308210e8aa9aSMichał Mirosław 			propname);
3083a4a54dd5SStephen Warren 		return -EINVAL;
3084a4a54dd5SStephen Warren 	}
3085a4a54dd5SStephen Warren 	num_routes /= 2;
3086a4a54dd5SStephen Warren 	if (!num_routes) {
3087f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3088a4a54dd5SStephen Warren 			propname);
3089a4a54dd5SStephen Warren 		return -EINVAL;
3090a4a54dd5SStephen Warren 	}
3091a4a54dd5SStephen Warren 
3092a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3093a4a54dd5SStephen Warren 			      GFP_KERNEL);
3094a4a54dd5SStephen Warren 	if (!routes) {
3095a4a54dd5SStephen Warren 		dev_err(card->dev,
3096f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3097a4a54dd5SStephen Warren 		return -EINVAL;
3098a4a54dd5SStephen Warren 	}
3099a4a54dd5SStephen Warren 
3100a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3101a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3102e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3103a4a54dd5SStephen Warren 		if (ret) {
3104c871bd0bSMark Brown 			dev_err(card->dev,
3105c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3106c871bd0bSMark Brown 				propname, 2 * i, ret);
3107a4a54dd5SStephen Warren 			return -EINVAL;
3108a4a54dd5SStephen Warren 		}
3109a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3110e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3111a4a54dd5SStephen Warren 		if (ret) {
3112a4a54dd5SStephen Warren 			dev_err(card->dev,
3113c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3114c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3115a4a54dd5SStephen Warren 			return -EINVAL;
3116a4a54dd5SStephen Warren 		}
3117a4a54dd5SStephen Warren 	}
3118a4a54dd5SStephen Warren 
3119f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3120f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3121a4a54dd5SStephen Warren 
3122a4a54dd5SStephen Warren 	return 0;
3123a4a54dd5SStephen Warren }
31242bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3125a4a54dd5SStephen Warren 
3126a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3127389cb834SJyri Sarha 				     const char *prefix,
3128389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3129389cb834SJyri Sarha 				     struct device_node **framemaster)
3130a7930ed4SKuninori Morimoto {
3131a7930ed4SKuninori Morimoto 	int ret, i;
3132a7930ed4SKuninori Morimoto 	char prop[128];
3133a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3134a7930ed4SKuninori Morimoto 	int bit, frame;
3135a7930ed4SKuninori Morimoto 	const char *str;
3136a7930ed4SKuninori Morimoto 	struct {
3137a7930ed4SKuninori Morimoto 		char *name;
3138a7930ed4SKuninori Morimoto 		unsigned int val;
3139a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3140a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3141a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3142a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3143a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3144a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3145a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3146a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3147a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3148a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3149a7930ed4SKuninori Morimoto 	};
3150a7930ed4SKuninori Morimoto 
3151a7930ed4SKuninori Morimoto 	if (!prefix)
3152a7930ed4SKuninori Morimoto 		prefix = "";
3153a7930ed4SKuninori Morimoto 
3154a7930ed4SKuninori Morimoto 	/*
31555711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31565711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3157a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3158a7930ed4SKuninori Morimoto 	 */
31595711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
31605711c979SKuninori Morimoto 	if (ret < 0) {
3161a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3162a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
31635711c979SKuninori Morimoto 	}
3164a7930ed4SKuninori Morimoto 	if (ret == 0) {
3165a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3166a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3167a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3168a7930ed4SKuninori Morimoto 				break;
3169a7930ed4SKuninori Morimoto 			}
3170a7930ed4SKuninori Morimoto 		}
3171a7930ed4SKuninori Morimoto 	}
3172a7930ed4SKuninori Morimoto 
3173a7930ed4SKuninori Morimoto 	/*
31748c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3175a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3176a7930ed4SKuninori Morimoto 	 */
31778c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
317851930295SJulia Lawall 	if (of_property_read_bool(np, prop))
31798c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
31808c2d6a9fSKuninori Morimoto 	else
31818c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3182a7930ed4SKuninori Morimoto 
3183a7930ed4SKuninori Morimoto 	/*
3184a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3185a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3186a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3187a7930ed4SKuninori Morimoto 	 */
3188a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3189a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3190a7930ed4SKuninori Morimoto 
3191a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3192a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3193a7930ed4SKuninori Morimoto 
3194a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3195a7930ed4SKuninori Morimoto 	case 0x11:
3196a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3197a7930ed4SKuninori Morimoto 		break;
3198a7930ed4SKuninori Morimoto 	case 0x10:
3199a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3200a7930ed4SKuninori Morimoto 		break;
3201a7930ed4SKuninori Morimoto 	case 0x01:
3202a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3203a7930ed4SKuninori Morimoto 		break;
3204a7930ed4SKuninori Morimoto 	default:
3205a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3206a7930ed4SKuninori Morimoto 		break;
3207a7930ed4SKuninori Morimoto 	}
3208a7930ed4SKuninori Morimoto 
3209a7930ed4SKuninori Morimoto 	/*
3210a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3211a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3212a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3213a7930ed4SKuninori Morimoto 	 */
3214a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3215a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3216389cb834SJyri Sarha 	if (bit && bitclkmaster)
3217389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3218a7930ed4SKuninori Morimoto 
3219a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3220a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3221389cb834SJyri Sarha 	if (frame && framemaster)
3222389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3223a7930ed4SKuninori Morimoto 
3224a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3225a7930ed4SKuninori Morimoto 	case 0x11:
3226a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3227a7930ed4SKuninori Morimoto 		break;
3228a7930ed4SKuninori Morimoto 	case 0x10:
3229a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3230a7930ed4SKuninori Morimoto 		break;
3231a7930ed4SKuninori Morimoto 	case 0x01:
3232a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3233a7930ed4SKuninori Morimoto 		break;
3234a7930ed4SKuninori Morimoto 	default:
3235a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3236a7930ed4SKuninori Morimoto 		break;
3237a7930ed4SKuninori Morimoto 	}
3238a7930ed4SKuninori Morimoto 
3239a7930ed4SKuninori Morimoto 	return format;
3240a7930ed4SKuninori Morimoto }
3241a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3242a7930ed4SKuninori Morimoto 
3243a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3244a180e8b9SKuninori Morimoto {
324509d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3246c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3247a180e8b9SKuninori Morimoto 	int ret;
3248a180e8b9SKuninori Morimoto 
3249c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3250c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3251a180e8b9SKuninori Morimoto 	/*
3252a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3253a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3254a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3255a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3256a180e8b9SKuninori Morimoto 	 */
3257a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3258a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3259c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32602c7b1704SKuninori Morimoto 	if (component)
32612c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3262a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3263a180e8b9SKuninori Morimoto 
3264c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3265c0a480d1STony Lindgren 
3266a180e8b9SKuninori Morimoto 	return ret;
3267a180e8b9SKuninori Morimoto }
3268a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3269a180e8b9SKuninori Morimoto 
32701ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3271cb470087SKuninori Morimoto 				const char **dai_name)
3272cb470087SKuninori Morimoto {
3273cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
32743e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
327593b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3276cb470087SKuninori Morimoto 
3277cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3278368dee94SKuninori Morimoto 	for_each_component(pos) {
3279c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
32803e0aa8d8SJyri Sarha 
32813e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3282cb470087SKuninori Morimoto 			continue;
3283cb470087SKuninori Morimoto 
3284a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3285a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
328658bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32876833c452SKuninori Morimoto 			int id = -1;
32886833c452SKuninori Morimoto 
328993b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32906833c452SKuninori Morimoto 			case 0:
32916833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32926833c452SKuninori Morimoto 				break;
32936833c452SKuninori Morimoto 			case 1:
329493b0f3eeSJean-Francois Moine 				id = args->args[0];
32956833c452SKuninori Morimoto 				break;
32966833c452SKuninori Morimoto 			default:
32976833c452SKuninori Morimoto 				/* not supported */
3298cb470087SKuninori Morimoto 				break;
3299cb470087SKuninori Morimoto 			}
3300cb470087SKuninori Morimoto 
33016833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33026833c452SKuninori Morimoto 				ret = -EINVAL;
33033dcba280SNicolin Chen 				continue;
33046833c452SKuninori Morimoto 			}
3305e41975edSXiubo Li 
3306e41975edSXiubo Li 			ret = 0;
3307e41975edSXiubo Li 
330858bf4179SKuninori Morimoto 			/* find target DAI */
330915a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
331058bf4179SKuninori Morimoto 				if (id == 0)
331158bf4179SKuninori Morimoto 					break;
331258bf4179SKuninori Morimoto 				id--;
331358bf4179SKuninori Morimoto 			}
331458bf4179SKuninori Morimoto 
331558bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3316e41975edSXiubo Li 			if (!*dai_name)
3317e41975edSXiubo Li 				*dai_name = pos->name;
33186833c452SKuninori Morimoto 		}
33196833c452SKuninori Morimoto 
3320cb470087SKuninori Morimoto 		break;
3321cb470087SKuninori Morimoto 	}
3322cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
332393b0f3eeSJean-Francois Moine 	return ret;
332493b0f3eeSJean-Francois Moine }
33251ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
332693b0f3eeSJean-Francois Moine 
332793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
332893b0f3eeSJean-Francois Moine 			    const char **dai_name)
332993b0f3eeSJean-Francois Moine {
333093b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
333193b0f3eeSJean-Francois Moine 	int ret;
333293b0f3eeSJean-Francois Moine 
333393b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
333493b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
333593b0f3eeSJean-Francois Moine 	if (ret)
333693b0f3eeSJean-Francois Moine 		return ret;
333793b0f3eeSJean-Francois Moine 
333893b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3339cb470087SKuninori Morimoto 
3340cb470087SKuninori Morimoto 	of_node_put(args.np);
3341cb470087SKuninori Morimoto 
3342cb470087SKuninori Morimoto 	return ret;
3343cb470087SKuninori Morimoto }
3344cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3345cb470087SKuninori Morimoto 
334693b0f3eeSJean-Francois Moine /*
334794685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
334894685763SSylwester Nawrocki  * @dai_link: DAI link
334994685763SSylwester Nawrocki  *
335094685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
335194685763SSylwester Nawrocki  */
335294685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
335394685763SSylwester Nawrocki {
33543db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
335594685763SSylwester Nawrocki 	int index;
335694685763SSylwester Nawrocki 
33573db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
335894685763SSylwester Nawrocki 		if (!component->of_node)
335994685763SSylwester Nawrocki 			break;
336094685763SSylwester Nawrocki 		of_node_put(component->of_node);
336194685763SSylwester Nawrocki 		component->of_node = NULL;
336294685763SSylwester Nawrocki 	}
336394685763SSylwester Nawrocki }
336494685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
336594685763SSylwester Nawrocki 
336694685763SSylwester Nawrocki /*
336793b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
336893b0f3eeSJean-Francois Moine  * @dev: Card device
336993b0f3eeSJean-Francois Moine  * @of_node: Device node
337093b0f3eeSJean-Francois Moine  * @dai_link: DAI link
337193b0f3eeSJean-Francois Moine  *
337293b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
337393b0f3eeSJean-Francois Moine  * 'sound-dai'.
337493b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
337594685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
337694685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
337793b0f3eeSJean-Francois Moine  *
337893b0f3eeSJean-Francois Moine  * Returns 0 for success
337993b0f3eeSJean-Francois Moine  */
338093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
338193b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
338293b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
338393b0f3eeSJean-Francois Moine {
338493b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
338593b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
338693b0f3eeSJean-Francois Moine 	char *name;
338793b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
338893b0f3eeSJean-Francois Moine 
338993b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
339093b0f3eeSJean-Francois Moine 	name = "sound-dai";
339193b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
339293b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
339393b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
339493b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
339593b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
339693b0f3eeSJean-Francois Moine 		else
339793b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
339893b0f3eeSJean-Francois Moine 		return num_codecs;
339993b0f3eeSJean-Francois Moine 	}
3400a86854d0SKees Cook 	component = devm_kcalloc(dev,
3401a86854d0SKees Cook 				 num_codecs, sizeof(*component),
340293b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
340393b0f3eeSJean-Francois Moine 	if (!component)
340493b0f3eeSJean-Francois Moine 		return -ENOMEM;
340593b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
340693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
340793b0f3eeSJean-Francois Moine 
340893b0f3eeSJean-Francois Moine 	/* Parse the list */
34093db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
341093b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
341193b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
341293b0f3eeSJean-Francois Moine 						 index, &args);
341393b0f3eeSJean-Francois Moine 		if (ret)
341493b0f3eeSJean-Francois Moine 			goto err;
341593b0f3eeSJean-Francois Moine 		component->of_node = args.np;
341693b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
341793b0f3eeSJean-Francois Moine 		if (ret < 0)
341893b0f3eeSJean-Francois Moine 			goto err;
341993b0f3eeSJean-Francois Moine 	}
342093b0f3eeSJean-Francois Moine 	return 0;
342193b0f3eeSJean-Francois Moine err:
342294685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
342393b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
342493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
342593b0f3eeSJean-Francois Moine 	return ret;
342693b0f3eeSJean-Francois Moine }
342793b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
342893b0f3eeSJean-Francois Moine 
3429c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3430db2a4165SFrank Mandarino {
34316553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3432fb257897SMark Brown 	snd_soc_util_init();
3433fb257897SMark Brown 
3434db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3435db2a4165SFrank Mandarino }
34364abe8e16SMark Brown module_init(snd_soc_init);
3437db2a4165SFrank Mandarino 
34387d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3439db2a4165SFrank Mandarino {
3440fb257897SMark Brown 	snd_soc_util_exit();
34416553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3442fb257897SMark Brown 
3443db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3444db2a4165SFrank Mandarino }
3445db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3446db2a4165SFrank Mandarino 
3447db2a4165SFrank Mandarino /* Module information */
3448d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3449db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3450db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34518b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3452