xref: /openbmc/linux/sound/soc/soc-core.c (revision 273d778ef38a8861f880e9df5799029dc82bd55d)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6f0fba2adSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7f0fba2adSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
80664d888SLiam Girdwood  *
9d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
100664d888SLiam Girdwood  *         with code, comments and ideas from :-
110664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
14db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
15db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
16db2a4165SFrank Mandarino  *  option) any later version.
17db2a4165SFrank Mandarino  *
18db2a4165SFrank Mandarino  *  TODO:
19db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
20db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
21db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
22db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
23db2a4165SFrank Mandarino  */
24db2a4165SFrank Mandarino 
25db2a4165SFrank Mandarino #include <linux/module.h>
26db2a4165SFrank Mandarino #include <linux/moduleparam.h>
27db2a4165SFrank Mandarino #include <linux/init.h>
28db2a4165SFrank Mandarino #include <linux/delay.h>
29db2a4165SFrank Mandarino #include <linux/pm.h>
30db2a4165SFrank Mandarino #include <linux/bitops.h>
3112ef193dSTroy Kisky #include <linux/debugfs.h>
32db2a4165SFrank Mandarino #include <linux/platform_device.h>
33741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
34f0e8ed85SMark Brown #include <linux/ctype.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
36bec4fa05SStephen Warren #include <linux/of.h>
37a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
38345233d7SLiam Girdwood #include <linux/dmi.h>
39db2a4165SFrank Mandarino #include <sound/core.h>
403028eb8cSMark Brown #include <sound/jack.h>
41db2a4165SFrank Mandarino #include <sound/pcm.h>
42db2a4165SFrank Mandarino #include <sound/pcm_params.h>
43db2a4165SFrank Mandarino #include <sound/soc.h>
4401d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
458a978234SLiam Girdwood #include <sound/soc-topology.h>
46db2a4165SFrank Mandarino #include <sound/initval.h>
47db2a4165SFrank Mandarino 
48a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
49a8b1d34fSMark Brown #include <trace/events/asoc.h>
50a8b1d34fSMark Brown 
51f0fba2adSLiam Girdwood #define NAME_SIZE	32
52f0fba2adSLiam Girdwood 
53384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
548a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
558a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56384c89e2SMark Brown #endif
57384c89e2SMark Brown 
58c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5912a48a8cSMark Brown static LIST_HEAD(platform_list);
600d0cf00aSMark Brown static LIST_HEAD(codec_list);
61030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
62c5af3a2eSMark Brown 
63db2a4165SFrank Mandarino /*
64db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
65db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
66db2a4165SFrank Mandarino  * between two audio tracks.
67db2a4165SFrank Mandarino  */
68db2a4165SFrank Mandarino static int pmdown_time = 5000;
69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71db2a4165SFrank Mandarino 
7298faf436SMengdong Lin /* If a DMI filed contain strings in this blacklist (e.g.
7398faf436SMengdong Lin  * "Type2 - Board Manufacturer" or  "Type1 - TBD by OEM"), it will be taken
7498faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
7598faf436SMengdong Lin  */
7698faf436SMengdong Lin static const char * const dmi_blacklist[] = {
7798faf436SMengdong Lin 	"To be filled by OEM",
7898faf436SMengdong Lin 	"TBD by OEM",
7998faf436SMengdong Lin 	"Default String",
8098faf436SMengdong Lin 	"Board Manufacturer",
8198faf436SMengdong Lin 	"Board Vendor Name",
8298faf436SMengdong Lin 	"Board Product Name",
8398faf436SMengdong Lin 	NULL,	/* terminator */
8498faf436SMengdong Lin };
8598faf436SMengdong Lin 
862bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
872bc9a81eSDimitris Papastamos  * a particular given value */
882bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
892bc9a81eSDimitris Papastamos {
902bc9a81eSDimitris Papastamos 	int c = 0;
912bc9a81eSDimitris Papastamos 	int i;
922bc9a81eSDimitris Papastamos 
932bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
942bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
952bc9a81eSDimitris Papastamos 			break;
962bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
972bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
982bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
992bc9a81eSDimitris Papastamos 	else
1002bc9a81eSDimitris Papastamos 		c /= 8;
1012bc9a81eSDimitris Papastamos 	return c;
1022bc9a81eSDimitris Papastamos }
1032bc9a81eSDimitris Papastamos 
10413fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
10513fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
10613fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
10713fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
1082624d5faSMark Brown {
10900b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
11000b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
11113fd179fSDimitris Papastamos 	int ret;
11213fd179fSDimitris Papastamos 
11313fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
11413fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
11513fd179fSDimitris Papastamos 		return -EINVAL;
11613fd179fSDimitris Papastamos 
117d6b6c2caSTakashi Iwai 	sprintf(buf, "%.*x: ", wordsize, reg);
118d6b6c2caSTakashi Iwai 	buf += wordsize + 2;
119d6b6c2caSTakashi Iwai 
12013fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
121d6b6c2caSTakashi Iwai 	if (ret < 0)
122d6b6c2caSTakashi Iwai 		memset(buf, 'X', regsize);
123d6b6c2caSTakashi Iwai 	else
124d6b6c2caSTakashi Iwai 		sprintf(buf, "%.*x", regsize, ret);
125d6b6c2caSTakashi Iwai 	buf[regsize] = '\n';
126d6b6c2caSTakashi Iwai 	/* no NUL-termination needed */
12713fd179fSDimitris Papastamos 	return 0;
12813fd179fSDimitris Papastamos }
12913fd179fSDimitris Papastamos 
13013fd179fSDimitris Papastamos /* codec register dump */
13113fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
13213fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
13313fd179fSDimitris Papastamos {
13413fd179fSDimitris Papastamos 	int i, step = 1;
1352bc9a81eSDimitris Papastamos 	int wordsize, regsize;
13613fd179fSDimitris Papastamos 	int len;
13713fd179fSDimitris Papastamos 	size_t total = 0;
13813fd179fSDimitris Papastamos 	loff_t p = 0;
1392bc9a81eSDimitris Papastamos 
14000b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
14100b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1422624d5faSMark Brown 
14313fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
14413fd179fSDimitris Papastamos 
145f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1462624d5faSMark Brown 		return 0;
1472624d5faSMark Brown 
148f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
149f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1502624d5faSMark Brown 
151f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
15213fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
15313fd179fSDimitris Papastamos 		 * entries for the default case */
15413fd179fSDimitris Papastamos 		if (p >= pos) {
15513fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1562624d5faSMark Brown 				break;
15713fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
15813fd179fSDimitris Papastamos 			total += len;
15913fd179fSDimitris Papastamos 		}
16013fd179fSDimitris Papastamos 		p += len;
16113fd179fSDimitris Papastamos 	}
1622624d5faSMark Brown 
16313fd179fSDimitris Papastamos 	total = min(total, count - 1);
1642624d5faSMark Brown 
16513fd179fSDimitris Papastamos 	return total;
1662624d5faSMark Brown }
16713fd179fSDimitris Papastamos 
1682624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1692624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1702624d5faSMark Brown {
17136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
172f0fba2adSLiam Girdwood 
17313fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1742624d5faSMark Brown }
1752624d5faSMark Brown 
1762624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1772624d5faSMark Brown 
178dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
179dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
180dbe21408SMark Brown {
18136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
182dbe21408SMark Brown 
183f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
184dbe21408SMark Brown }
185dbe21408SMark Brown 
186dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
187dbe21408SMark Brown 			       struct device_attribute *attr,
188dbe21408SMark Brown 			       const char *buf, size_t count)
189dbe21408SMark Brown {
19036ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
191c593b520SMark Brown 	int ret;
192dbe21408SMark Brown 
193b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
194c593b520SMark Brown 	if (ret)
195c593b520SMark Brown 		return ret;
196dbe21408SMark Brown 
197dbe21408SMark Brown 	return count;
198dbe21408SMark Brown }
199dbe21408SMark Brown 
200dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
201dbe21408SMark Brown 
202d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
203d29697dcSTakashi Iwai 	&dev_attr_codec_reg.attr,
204d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
205d29697dcSTakashi Iwai 	NULL
206d29697dcSTakashi Iwai };
207d29697dcSTakashi Iwai 
208d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
209d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
210d29697dcSTakashi Iwai {
211d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
212d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
213d29697dcSTakashi Iwai 
214d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
215d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
216d29697dcSTakashi Iwai 	return rtd->codec ? attr->mode : 0; /* enabled only with codec */
217d29697dcSTakashi Iwai }
218d29697dcSTakashi Iwai 
219d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
220d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
221d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
222d29697dcSTakashi Iwai };
223d29697dcSTakashi Iwai 
224d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = {
225d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
226d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
227d29697dcSTakashi Iwai };
228d29697dcSTakashi Iwai 
229d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
230d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
231d29697dcSTakashi Iwai 	&soc_dev_roup,
232d29697dcSTakashi Iwai 	NULL
233d29697dcSTakashi Iwai };
234d29697dcSTakashi Iwai 
2352624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2362624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2372624d5faSMark Brown 				   size_t count, loff_t *ppos)
2382624d5faSMark Brown {
2392624d5faSMark Brown 	ssize_t ret;
2402624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
24113fd179fSDimitris Papastamos 	char *buf;
24213fd179fSDimitris Papastamos 
24313fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
24413fd179fSDimitris Papastamos 		return -EINVAL;
24513fd179fSDimitris Papastamos 
24613fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2472624d5faSMark Brown 	if (!buf)
2482624d5faSMark Brown 		return -ENOMEM;
24913fd179fSDimitris Papastamos 
25013fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
25113fd179fSDimitris Papastamos 	if (ret >= 0) {
25213fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
25313fd179fSDimitris Papastamos 			kfree(buf);
25413fd179fSDimitris Papastamos 			return -EFAULT;
25513fd179fSDimitris Papastamos 		}
25613fd179fSDimitris Papastamos 		*ppos += ret;
25713fd179fSDimitris Papastamos 	}
25813fd179fSDimitris Papastamos 
2592624d5faSMark Brown 	kfree(buf);
2602624d5faSMark Brown 	return ret;
2612624d5faSMark Brown }
2622624d5faSMark Brown 
2632624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2642624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2652624d5faSMark Brown {
2662624d5faSMark Brown 	char buf[32];
26734e268d8SStephen Boyd 	size_t buf_size;
2682624d5faSMark Brown 	char *start = buf;
2692624d5faSMark Brown 	unsigned long reg, value;
2702624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
271b785a492SJingoo Han 	int ret;
2722624d5faSMark Brown 
2732624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2742624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2752624d5faSMark Brown 		return -EFAULT;
2762624d5faSMark Brown 	buf[buf_size] = 0;
2772624d5faSMark Brown 
2782624d5faSMark Brown 	while (*start == ' ')
2792624d5faSMark Brown 		start++;
2802624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2812624d5faSMark Brown 	while (*start == ' ')
2822624d5faSMark Brown 		start++;
283b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
284b785a492SJingoo Han 	if (ret)
285b785a492SJingoo Han 		return ret;
2860d51a9cbSMark Brown 
2870d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
288373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2890d51a9cbSMark Brown 
290e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2912624d5faSMark Brown 	return buf_size;
2922624d5faSMark Brown }
2932624d5faSMark Brown 
2942624d5faSMark Brown static const struct file_operations codec_reg_fops = {
295234e3405SStephen Boyd 	.open = simple_open,
2962624d5faSMark Brown 	.read = codec_reg_read_file,
2972624d5faSMark Brown 	.write = codec_reg_write_file,
2986038f373SArnd Bergmann 	.llseek = default_llseek,
2992624d5faSMark Brown };
3002624d5faSMark Brown 
30181c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
302e73f3de5SRussell King {
3036553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
3046553bf06SLars-Peter Clausen 		return;
3056553bf06SLars-Peter Clausen 
30681c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
30781c7cfd1SLars-Peter Clausen 		char *name;
308e73f3de5SRussell King 
30981c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
31081c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
31181c7cfd1SLars-Peter Clausen 		if (name) {
31281c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
31381c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
31481c7cfd1SLars-Peter Clausen 			kfree(name);
31581c7cfd1SLars-Peter Clausen 		}
31681c7cfd1SLars-Peter Clausen 	} else {
31781c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
31881c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
319e73f3de5SRussell King 	}
320e73f3de5SRussell King 
32181c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
32281c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
32381c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3242624d5faSMark Brown 		return;
3252624d5faSMark Brown 	}
3262624d5faSMark Brown 
32781c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
32881c7cfd1SLars-Peter Clausen 		component->debugfs_root);
32981c7cfd1SLars-Peter Clausen 
33081c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
33181c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
33281c7cfd1SLars-Peter Clausen }
33381c7cfd1SLars-Peter Clausen 
33481c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
33581c7cfd1SLars-Peter Clausen {
33681c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
33781c7cfd1SLars-Peter Clausen }
33881c7cfd1SLars-Peter Clausen 
33981c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
34081c7cfd1SLars-Peter Clausen {
34181c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
342353c64ddSFabio Estevam 	struct dentry *debugfs_reg;
34381c7cfd1SLars-Peter Clausen 
344353c64ddSFabio Estevam 	debugfs_reg = debugfs_create_file("codec_reg", 0644,
34581c7cfd1SLars-Peter Clausen 					  codec->component.debugfs_root,
3462624d5faSMark Brown 					  codec, &codec_reg_fops);
347353c64ddSFabio Estevam 	if (!debugfs_reg)
34810e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
34910e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
350731f1ab2SSebastien Guiriec }
351731f1ab2SSebastien Guiriec 
352c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
353c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
354c3c5a19aSMark Brown {
355c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3562b194f9dSMark Brown 	ssize_t len, ret = 0;
357c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
358c3c5a19aSMark Brown 
359c3c5a19aSMark Brown 	if (!buf)
360c3c5a19aSMark Brown 		return -ENOMEM;
361c3c5a19aSMark Brown 
36234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
36334e81ab4SLars-Peter Clausen 
3642b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3652b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
366f4333203SLars-Peter Clausen 			       codec->component.name);
3672b194f9dSMark Brown 		if (len >= 0)
3682b194f9dSMark Brown 			ret += len;
3692b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3702b194f9dSMark Brown 			ret = PAGE_SIZE;
3712b194f9dSMark Brown 			break;
3722b194f9dSMark Brown 		}
3732b194f9dSMark Brown 	}
374c3c5a19aSMark Brown 
37534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
37634e81ab4SLars-Peter Clausen 
377c3c5a19aSMark Brown 	if (ret >= 0)
378c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
379c3c5a19aSMark Brown 
380c3c5a19aSMark Brown 	kfree(buf);
381c3c5a19aSMark Brown 
382c3c5a19aSMark Brown 	return ret;
383c3c5a19aSMark Brown }
384c3c5a19aSMark Brown 
385c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
386c3c5a19aSMark Brown 	.read = codec_list_read_file,
387c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
388c3c5a19aSMark Brown };
389c3c5a19aSMark Brown 
390f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
391f3208780SMark Brown 				  size_t count, loff_t *ppos)
392f3208780SMark Brown {
393f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3942b194f9dSMark Brown 	ssize_t len, ret = 0;
3951438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
396f3208780SMark Brown 	struct snd_soc_dai *dai;
397f3208780SMark Brown 
398f3208780SMark Brown 	if (!buf)
399f3208780SMark Brown 		return -ENOMEM;
400f3208780SMark Brown 
40134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
40234e81ab4SLars-Peter Clausen 
4031438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
4041438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
4051438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
4061438c2f6SLars-Peter Clausen 				dai->name);
4072b194f9dSMark Brown 			if (len >= 0)
4082b194f9dSMark Brown 				ret += len;
4092b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
4102b194f9dSMark Brown 				ret = PAGE_SIZE;
4112b194f9dSMark Brown 				break;
4122b194f9dSMark Brown 			}
4132b194f9dSMark Brown 		}
4141438c2f6SLars-Peter Clausen 	}
415f3208780SMark Brown 
41634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
41734e81ab4SLars-Peter Clausen 
418f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
419f3208780SMark Brown 
420f3208780SMark Brown 	kfree(buf);
421f3208780SMark Brown 
422f3208780SMark Brown 	return ret;
423f3208780SMark Brown }
424f3208780SMark Brown 
425f3208780SMark Brown static const struct file_operations dai_list_fops = {
426f3208780SMark Brown 	.read = dai_list_read_file,
427f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
428f3208780SMark Brown };
429f3208780SMark Brown 
43019c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
43119c7ac27SMark Brown 				       char __user *user_buf,
43219c7ac27SMark Brown 				       size_t count, loff_t *ppos)
43319c7ac27SMark Brown {
43419c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4352b194f9dSMark Brown 	ssize_t len, ret = 0;
43619c7ac27SMark Brown 	struct snd_soc_platform *platform;
43719c7ac27SMark Brown 
43819c7ac27SMark Brown 	if (!buf)
43919c7ac27SMark Brown 		return -ENOMEM;
44019c7ac27SMark Brown 
44134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
44234e81ab4SLars-Peter Clausen 
4432b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4442b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
445f4333203SLars-Peter Clausen 			       platform->component.name);
4462b194f9dSMark Brown 		if (len >= 0)
4472b194f9dSMark Brown 			ret += len;
4482b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4492b194f9dSMark Brown 			ret = PAGE_SIZE;
4502b194f9dSMark Brown 			break;
4512b194f9dSMark Brown 		}
4522b194f9dSMark Brown 	}
45319c7ac27SMark Brown 
45434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
45534e81ab4SLars-Peter Clausen 
45619c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
45719c7ac27SMark Brown 
45819c7ac27SMark Brown 	kfree(buf);
45919c7ac27SMark Brown 
46019c7ac27SMark Brown 	return ret;
46119c7ac27SMark Brown }
46219c7ac27SMark Brown 
46319c7ac27SMark Brown static const struct file_operations platform_list_fops = {
46419c7ac27SMark Brown 	.read = platform_list_read_file,
46519c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
46619c7ac27SMark Brown };
46719c7ac27SMark Brown 
468a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
469a6052154SJarkko Nikula {
4706553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
4716553bf06SLars-Peter Clausen 		return;
4726553bf06SLars-Peter Clausen 
473a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4748a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4753a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
476a6052154SJarkko Nikula 		dev_warn(card->dev,
4777c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4783a45b867SJarkko Nikula 		return;
4793a45b867SJarkko Nikula 	}
4803a45b867SJarkko Nikula 
4813a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4823a45b867SJarkko Nikula 						    card->debugfs_card_root,
4833a45b867SJarkko Nikula 						    &card->pop_time);
4843a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4853a45b867SJarkko Nikula 		dev_warn(card->dev,
486f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
487a6052154SJarkko Nikula }
488a6052154SJarkko Nikula 
489a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
490a6052154SJarkko Nikula {
491a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
492a6052154SJarkko Nikula }
493a6052154SJarkko Nikula 
4946553bf06SLars-Peter Clausen 
4956553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
4966553bf06SLars-Peter Clausen {
4976553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
498d9a02c55SFabio Estevam 	if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
4996553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
5006553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
5016553bf06SLars-Peter Clausen 		return;
5026553bf06SLars-Peter Clausen 	}
5036553bf06SLars-Peter Clausen 
5046553bf06SLars-Peter Clausen 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
5056553bf06SLars-Peter Clausen 				 &codec_list_fops))
5066553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
5076553bf06SLars-Peter Clausen 
5086553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
5096553bf06SLars-Peter Clausen 				 &dai_list_fops))
5106553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
5116553bf06SLars-Peter Clausen 
5126553bf06SLars-Peter Clausen 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
5136553bf06SLars-Peter Clausen 				 &platform_list_fops))
5146553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
5156553bf06SLars-Peter Clausen }
5166553bf06SLars-Peter Clausen 
5176553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
5186553bf06SLars-Peter Clausen {
5196553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
5206553bf06SLars-Peter Clausen }
5216553bf06SLars-Peter Clausen 
5222624d5faSMark Brown #else
5232624d5faSMark Brown 
52481c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
52581c7cfd1SLars-Peter Clausen 
52681c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
52781c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
5282624d5faSMark Brown {
5292624d5faSMark Brown }
5302624d5faSMark Brown 
53181c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
53281c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
533731f1ab2SSebastien Guiriec {
534731f1ab2SSebastien Guiriec }
535731f1ab2SSebastien Guiriec 
536b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
537b95fccbcSAxel Lin {
538b95fccbcSAxel Lin }
539b95fccbcSAxel Lin 
540b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
541b95fccbcSAxel Lin {
542b95fccbcSAxel Lin }
5436553bf06SLars-Peter Clausen 
5446553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
5456553bf06SLars-Peter Clausen {
5466553bf06SLars-Peter Clausen }
5476553bf06SLars-Peter Clausen 
5486553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
5496553bf06SLars-Peter Clausen {
5506553bf06SLars-Peter Clausen }
5516553bf06SLars-Peter Clausen 
5522624d5faSMark Brown #endif
5532624d5faSMark Brown 
554a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
555a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
556a0ac4411SKuninori Morimoto {
557a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
558a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *new_rtdcom;
559a0ac4411SKuninori Morimoto 
560a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
561a0ac4411SKuninori Morimoto 		/* already connected */
562a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
563a0ac4411SKuninori Morimoto 			return 0;
564a0ac4411SKuninori Morimoto 	}
565a0ac4411SKuninori Morimoto 
566a0ac4411SKuninori Morimoto 	new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
567a0ac4411SKuninori Morimoto 	if (!new_rtdcom)
568a0ac4411SKuninori Morimoto 		return -ENOMEM;
569a0ac4411SKuninori Morimoto 
570a0ac4411SKuninori Morimoto 	new_rtdcom->component = component;
571a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&new_rtdcom->list);
572a0ac4411SKuninori Morimoto 
573a0ac4411SKuninori Morimoto 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
574a0ac4411SKuninori Morimoto 
575a0ac4411SKuninori Morimoto 	return 0;
576a0ac4411SKuninori Morimoto }
577a0ac4411SKuninori Morimoto 
578a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
579a0ac4411SKuninori Morimoto {
580a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
581a0ac4411SKuninori Morimoto 
582a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
583a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
584a0ac4411SKuninori Morimoto 
585a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
586a0ac4411SKuninori Morimoto }
587a0ac4411SKuninori Morimoto 
588a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
589a0ac4411SKuninori Morimoto 						const char *driver_name)
590a0ac4411SKuninori Morimoto {
591a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
592a0ac4411SKuninori Morimoto 
593a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
594a0ac4411SKuninori Morimoto 		if ((rtdcom->component->driver->name == driver_name) ||
595a0ac4411SKuninori Morimoto 		    strcmp(rtdcom->component->driver->name, driver_name) == 0)
596a0ac4411SKuninori Morimoto 			return rtdcom->component;
597a0ac4411SKuninori Morimoto 	}
598a0ac4411SKuninori Morimoto 
599a0ac4411SKuninori Morimoto 	return NULL;
600a0ac4411SKuninori Morimoto }
601a0ac4411SKuninori Morimoto 
60247c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
60347c88fffSLiam Girdwood 		const char *dai_link, int stream)
60447c88fffSLiam Girdwood {
6051a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
60647c88fffSLiam Girdwood 
6071a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6081a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
6091a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
6101a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
61147c88fffSLiam Girdwood 	}
612f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
61347c88fffSLiam Girdwood 	return NULL;
61447c88fffSLiam Girdwood }
61547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
61647c88fffSLiam Girdwood 
61775ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
61875ab9eb6SKuninori Morimoto 
6191a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
6201a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
6211a497983SMengdong Lin {
6221a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6231a497983SMengdong Lin 
6241a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
6251a497983SMengdong Lin 	if (!rtd)
6261a497983SMengdong Lin 		return NULL;
6271a497983SMengdong Lin 
628a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
6291a497983SMengdong Lin 	rtd->card = card;
6301a497983SMengdong Lin 	rtd->dai_link = dai_link;
63175ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
63275ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
63375ab9eb6SKuninori Morimoto 
6341a497983SMengdong Lin 	rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
6351a497983SMengdong Lin 					dai_link->num_codecs,
6361a497983SMengdong Lin 					GFP_KERNEL);
6371a497983SMengdong Lin 	if (!rtd->codec_dais) {
6381a497983SMengdong Lin 		kfree(rtd);
6391a497983SMengdong Lin 		return NULL;
6401a497983SMengdong Lin 	}
6411a497983SMengdong Lin 
6421a497983SMengdong Lin 	return rtd;
6431a497983SMengdong Lin }
6441a497983SMengdong Lin 
6451a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
6461a497983SMengdong Lin {
6471a497983SMengdong Lin 	kfree(rtd->codec_dais);
648a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
6491a497983SMengdong Lin 	kfree(rtd);
6501a497983SMengdong Lin }
6511a497983SMengdong Lin 
6521a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
6531a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
6541a497983SMengdong Lin {
6551a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
6561a497983SMengdong Lin 	rtd->num = card->num_rtd;
6571a497983SMengdong Lin 	card->num_rtd++;
6581a497983SMengdong Lin }
6591a497983SMengdong Lin 
6601a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
6611a497983SMengdong Lin {
6621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
6631a497983SMengdong Lin 
6641a497983SMengdong Lin 	list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
6651a497983SMengdong Lin 		list_del(&rtd->list);
6661a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
6671a497983SMengdong Lin 	}
6681a497983SMengdong Lin 
6691a497983SMengdong Lin 	card->num_rtd = 0;
6701a497983SMengdong Lin }
6711a497983SMengdong Lin 
67247c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
67347c88fffSLiam Girdwood 		const char *dai_link)
67447c88fffSLiam Girdwood {
6751a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
67647c88fffSLiam Girdwood 
6771a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6781a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
6791a497983SMengdong Lin 			return rtd;
68047c88fffSLiam Girdwood 	}
681f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
68247c88fffSLiam Girdwood 	return NULL;
68347c88fffSLiam Girdwood }
68447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
68547c88fffSLiam Girdwood 
6869d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
6879d58a077SRichard Fitzgerald {
6889d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
6899d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
6909d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
6919d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
6929d58a077SRichard Fitzgerald 	 */
6939d58a077SRichard Fitzgerald }
6949d58a077SRichard Fitzgerald 
6956f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
696db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
6976f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
698db2a4165SFrank Mandarino {
6996f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
700d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
7011a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
7021a497983SMengdong Lin 	int i;
703db2a4165SFrank Mandarino 
704c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
705c5599b87SLars-Peter Clausen 	if (!card->instantiated)
706e3509ff0SDaniel Mack 		return 0;
707e3509ff0SDaniel Mack 
7086ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
7096ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
7106ed25978SAndy Green 	 */
711f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
7126ed25978SAndy Green 
7136ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
714f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
7156ed25978SAndy Green 
716a00f90f9SMark Brown 	/* mute any active DACs */
7171a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7183efab7dcSMark Brown 
7191a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7203efab7dcSMark Brown 			continue;
7213efab7dcSMark Brown 
7221a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
7231a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
72488bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
72588bd870fSBenoit Cousson 
726f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
727f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
728db2a4165SFrank Mandarino 		}
72988bd870fSBenoit Cousson 	}
730db2a4165SFrank Mandarino 
7314ccab3e7SLiam Girdwood 	/* suspend all pcms */
7321a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7331a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7343efab7dcSMark Brown 			continue;
7353efab7dcSMark Brown 
7361a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
7373efab7dcSMark Brown 	}
7384ccab3e7SLiam Girdwood 
73987506549SMark Brown 	if (card->suspend_pre)
74070b2ac12SMark Brown 		card->suspend_pre(card);
741db2a4165SFrank Mandarino 
7421a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7431a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7443efab7dcSMark Brown 
7451a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7463efab7dcSMark Brown 			continue;
7473efab7dcSMark Brown 
748bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
749f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
750db2a4165SFrank Mandarino 	}
751db2a4165SFrank Mandarino 
75237660b6dSLars-Peter Clausen 	/* close any waiting streams */
7531a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
7541a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
755db2a4165SFrank Mandarino 
7561a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7573efab7dcSMark Brown 
7581a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7593efab7dcSMark Brown 			continue;
7603efab7dcSMark Brown 
7611a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
7627bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
763db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
764f0fba2adSLiam Girdwood 
7651a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
7667bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
767db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
768db2a4165SFrank Mandarino 	}
769db2a4165SFrank Mandarino 
7708be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7718be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
772e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
773e2d32ff6SMark Brown 
7749178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
775d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
776d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
777d9fc4063SKuninori Morimoto 
7789178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
7791547aba9SMark Brown 		 * bias _ON and should not be suspended. */
7809178feb4SKuninori Morimoto 		if (!component->suspended) {
7814890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7821547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
783125a25daSMark Brown 				/*
7849178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
785125a25daSMark Brown 				 * bias off then being in STANDBY
786125a25daSMark Brown 				 * means it's doing something,
787125a25daSMark Brown 				 * otherwise fall through.
788125a25daSMark Brown 				 */
7894890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
7909178feb4SKuninori Morimoto 					dev_dbg(component->dev,
79110e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
792125a25daSMark Brown 					break;
793125a25daSMark Brown 				}
794a8093297SLars-Peter Clausen 
7951547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
7969178feb4SKuninori Morimoto 				if (component->suspend)
7979178feb4SKuninori Morimoto 					component->suspend(component);
7989178feb4SKuninori Morimoto 				component->suspended = 1;
7999178feb4SKuninori Morimoto 				if (component->regmap)
8009178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
801988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
8029178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
8031547aba9SMark Brown 				break;
8041547aba9SMark Brown 			default:
8059178feb4SKuninori Morimoto 				dev_dbg(component->dev,
8069178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
8071547aba9SMark Brown 				break;
8081547aba9SMark Brown 			}
8091547aba9SMark Brown 		}
810f0fba2adSLiam Girdwood 	}
811db2a4165SFrank Mandarino 
8121a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8131a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8143efab7dcSMark Brown 
8151a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8163efab7dcSMark Brown 			continue;
8173efab7dcSMark Brown 
818bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
819f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
820988e8cc4SNicolin Chen 
821988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
822988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
823db2a4165SFrank Mandarino 	}
824db2a4165SFrank Mandarino 
82587506549SMark Brown 	if (card->suspend_post)
82670b2ac12SMark Brown 		card->suspend_post(card);
827db2a4165SFrank Mandarino 
828db2a4165SFrank Mandarino 	return 0;
829db2a4165SFrank Mandarino }
8306f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
831db2a4165SFrank Mandarino 
8326ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
8336ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
8346ed25978SAndy Green  */
8356ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
836db2a4165SFrank Mandarino {
837f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
838f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
8391a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
840d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
8411a497983SMengdong Lin 	int i;
842db2a4165SFrank Mandarino 
8436ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
8446ed25978SAndy Green 	 * so userspace apps are blocked from touching us
8456ed25978SAndy Green 	 */
8466ed25978SAndy Green 
847f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
8486ed25978SAndy Green 
8499949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
850f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
8519949788bSMark Brown 
85287506549SMark Brown 	if (card->resume_pre)
85370b2ac12SMark Brown 		card->resume_pre(card);
854db2a4165SFrank Mandarino 
855bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
8561a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8571a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8583efab7dcSMark Brown 
8591a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8603efab7dcSMark Brown 			continue;
8613efab7dcSMark Brown 
862bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
863f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
864db2a4165SFrank Mandarino 	}
865db2a4165SFrank Mandarino 
866d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
8679178feb4SKuninori Morimoto 		if (component->suspended) {
8689178feb4SKuninori Morimoto 			if (component->resume)
8699178feb4SKuninori Morimoto 				component->resume(component);
8709178feb4SKuninori Morimoto 			component->suspended = 0;
8711547aba9SMark Brown 		}
872f0fba2adSLiam Girdwood 	}
873db2a4165SFrank Mandarino 
8741a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8753efab7dcSMark Brown 
8761a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8773efab7dcSMark Brown 			continue;
8783efab7dcSMark Brown 
8791a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
880d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
881db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
882f0fba2adSLiam Girdwood 
8831a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
884d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
885db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
886db2a4165SFrank Mandarino 	}
887db2a4165SFrank Mandarino 
8883ff3f64bSMark Brown 	/* unmute any active DACs */
8891a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8903efab7dcSMark Brown 
8911a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8923efab7dcSMark Brown 			continue;
8933efab7dcSMark Brown 
8941a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8951a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
89688bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
89788bd870fSBenoit Cousson 
898f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
899f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
900db2a4165SFrank Mandarino 		}
90188bd870fSBenoit Cousson 	}
902db2a4165SFrank Mandarino 
9031a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
9041a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9053efab7dcSMark Brown 
9061a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
9073efab7dcSMark Brown 			continue;
9083efab7dcSMark Brown 
909bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
910f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
911db2a4165SFrank Mandarino 	}
912db2a4165SFrank Mandarino 
91387506549SMark Brown 	if (card->resume_post)
91470b2ac12SMark Brown 		card->resume_post(card);
915db2a4165SFrank Mandarino 
916f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
9176ed25978SAndy Green 
9188be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
9198be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
920e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
9211a7aaa58SJeeja KP 
9221a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
9231a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
9246ed25978SAndy Green }
9256ed25978SAndy Green 
9266ed25978SAndy Green /* powers up audio subsystem after a suspend */
9276f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
9286ed25978SAndy Green {
9296f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
930bc263214SLars-Peter Clausen 	bool bus_control = false;
9311a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
932b9dd94a8SPeter Ujfalusi 
933c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
934c5599b87SLars-Peter Clausen 	if (!card->instantiated)
9355ff1ddf2SEric Miao 		return 0;
9365ff1ddf2SEric Miao 
937988e8cc4SNicolin Chen 	/* activate pins from sleep state */
9381a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
93988bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
94088bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
94188bd870fSBenoit Cousson 		int j;
94288bd870fSBenoit Cousson 
943988e8cc4SNicolin Chen 		if (cpu_dai->active)
944988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
94588bd870fSBenoit Cousson 
94688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
94788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
948988e8cc4SNicolin Chen 			if (codec_dai->active)
949988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
950988e8cc4SNicolin Chen 		}
95188bd870fSBenoit Cousson 	}
952988e8cc4SNicolin Chen 
953bc263214SLars-Peter Clausen 	/*
954bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
955bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
956bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
95764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
95864ab9baaSMark Brown 	 */
9591a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
9601a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
961bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
96282e14e8bSStephen Warren 	}
963bc263214SLars-Peter Clausen 	if (bus_control) {
964bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
96564ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
96664ab9baaSMark Brown 	} else {
967f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
9686308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
969f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
970f0fba2adSLiam Girdwood 	}
9716ed25978SAndy Green 
972db2a4165SFrank Mandarino 	return 0;
973db2a4165SFrank Mandarino }
9746f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
975db2a4165SFrank Mandarino #else
9766f8ab4acSMark Brown #define snd_soc_suspend NULL
9776f8ab4acSMark Brown #define snd_soc_resume NULL
978db2a4165SFrank Mandarino #endif
979db2a4165SFrank Mandarino 
98085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
98102a06d30SBarry Song };
98202a06d30SBarry Song 
98365d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
98465d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
98512023a9aSMisael Lopez Cruz {
98665d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
98712023a9aSMisael Lopez Cruz 
98834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
98934e81ab4SLars-Peter Clausen 
99065d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
99165d9361fSLars-Peter Clausen 		if (of_node) {
99265d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
99365d9361fSLars-Peter Clausen 				return component;
99465d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
99565d9361fSLars-Peter Clausen 			return component;
99612023a9aSMisael Lopez Cruz 		}
99712023a9aSMisael Lopez Cruz 	}
99812023a9aSMisael Lopez Cruz 
99912023a9aSMisael Lopez Cruz 	return NULL;
100012023a9aSMisael Lopez Cruz }
100112023a9aSMisael Lopez Cruz 
1002fbb88b5cSMengdong Lin /**
1003fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
1004fbb88b5cSMengdong Lin  *
10054958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
1006fbb88b5cSMengdong Lin  *
1007ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
1008fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
1009fbb88b5cSMengdong Lin  * should also match if being specified.
1010fbb88b5cSMengdong Lin  *
1011fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
1012fbb88b5cSMengdong Lin  */
1013305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
101414621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
101512023a9aSMisael Lopez Cruz {
101614621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
101714621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
10183e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
101912023a9aSMisael Lopez Cruz 
102034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
102134e81ab4SLars-Peter Clausen 
102214621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
102314621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
10243e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
10253e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
10263e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
10273e0aa8d8SJyri Sarha 
10283e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
102912023a9aSMisael Lopez Cruz 			continue;
10301ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
103112023a9aSMisael Lopez Cruz 			continue;
103214621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
10334958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
10346a6dafdaSJeffy Chen 			    && (!dai->driver->name
10356a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
103614621c7eSLars-Peter Clausen 				continue;
103712023a9aSMisael Lopez Cruz 
103814621c7eSLars-Peter Clausen 			return dai;
103912023a9aSMisael Lopez Cruz 		}
104012023a9aSMisael Lopez Cruz 	}
104112023a9aSMisael Lopez Cruz 
104212023a9aSMisael Lopez Cruz 	return NULL;
104312023a9aSMisael Lopez Cruz }
1044305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
104512023a9aSMisael Lopez Cruz 
104617fb1755SMengdong Lin 
104717fb1755SMengdong Lin /**
104817fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
104917fb1755SMengdong Lin  *
105017fb1755SMengdong Lin  * @card: soc card
105117fb1755SMengdong Lin  * @id: DAI link ID to match
105217fb1755SMengdong Lin  * @name: DAI link name to match, optional
10538abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
105417fb1755SMengdong Lin  *
105517fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
105617fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
105717fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
105817fb1755SMengdong Lin  * specified.
105917fb1755SMengdong Lin  *
106017fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
106117fb1755SMengdong Lin  */
106217fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
106317fb1755SMengdong Lin 					       int id, const char *name,
106417fb1755SMengdong Lin 					       const char *stream_name)
106517fb1755SMengdong Lin {
106617fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
106717fb1755SMengdong Lin 
106817fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
106917fb1755SMengdong Lin 
107017fb1755SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
107117fb1755SMengdong Lin 		if (link->id != id)
107217fb1755SMengdong Lin 			continue;
107317fb1755SMengdong Lin 
107417fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
107517fb1755SMengdong Lin 			continue;
107617fb1755SMengdong Lin 
107717fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
107817fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
107917fb1755SMengdong Lin 			continue;
108017fb1755SMengdong Lin 
108117fb1755SMengdong Lin 		return link;
108217fb1755SMengdong Lin 	}
108317fb1755SMengdong Lin 
108417fb1755SMengdong Lin 	return NULL;
108517fb1755SMengdong Lin }
108617fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
108717fb1755SMengdong Lin 
108849a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
108949a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
1090db2a4165SFrank Mandarino {
109149a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
109249a5ba1cSMengdong Lin 
109349a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
109449a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
109549a5ba1cSMengdong Lin 			return true;
109649a5ba1cSMengdong Lin 	}
109749a5ba1cSMengdong Lin 
109849a5ba1cSMengdong Lin 	return false;
109949a5ba1cSMengdong Lin }
110049a5ba1cSMengdong Lin 
11016f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
11026f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1103db2a4165SFrank Mandarino {
11041a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
110588bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
110614621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
110790be711eSKuninori Morimoto 	struct snd_soc_component *component;
11081a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
1109435c5e25SMark Brown 	struct snd_soc_platform *platform;
111006859fcaSCharles Keepax 	struct device_node *platform_of_node;
1111848dd8beSMark Brown 	const char *platform_name;
111288bd870fSBenoit Cousson 	int i;
1113db2a4165SFrank Mandarino 
11146f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
11156308419aSMark Brown 
111649a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
111749a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
111849a5ba1cSMengdong Lin 			dai_link->name);
111949a5ba1cSMengdong Lin 		return 0;
112049a5ba1cSMengdong Lin 	}
1121db2a4165SFrank Mandarino 
1122513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1123513cb311SSudip Mukherjee 	if (!rtd)
1124513cb311SSudip Mukherjee 		return -ENOMEM;
1125513cb311SSudip Mukherjee 
112614621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
112714621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
112814621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
112914621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1130b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1131f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1132f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
11331a497983SMengdong Lin 		goto _err_defer;
1134c5af3a2eSMark Brown 	}
113590be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1136c5af3a2eSMark Brown 
113788bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
113888bd870fSBenoit Cousson 
113988bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
11401a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
114188bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
114214621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
114388bd870fSBenoit Cousson 		if (!codec_dais[i]) {
114412023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
114588bd870fSBenoit Cousson 				codecs[i].dai_name);
11461a497983SMengdong Lin 			goto _err_defer;
114712023a9aSMisael Lopez Cruz 		}
114890be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
114988bd870fSBenoit Cousson 	}
115088bd870fSBenoit Cousson 
115188bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
115288bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
115388bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
115412023a9aSMisael Lopez Cruz 
1155848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1156848dd8beSMark Brown 	platform_name = dai_link->platform_name;
11575a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1158848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1159848dd8beSMark Brown 
1160b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
116190be711eSKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
116290be711eSKuninori Morimoto 		platform_of_node = component->dev->of_node;
116390be711eSKuninori Morimoto 		if (!platform_of_node && component->dev->parent->of_node)
116490be711eSKuninori Morimoto 			platform_of_node = component->dev->parent->of_node;
116590be711eSKuninori Morimoto 
116690be711eSKuninori Morimoto 		if (dai_link->platform_of_node) {
116790be711eSKuninori Morimoto 			if (platform_of_node != dai_link->platform_of_node)
116890be711eSKuninori Morimoto 				continue;
116990be711eSKuninori Morimoto 		} else {
117090be711eSKuninori Morimoto 			if (strcmp(component->name, platform_name))
117190be711eSKuninori Morimoto 				continue;
117290be711eSKuninori Morimoto 		}
117390be711eSKuninori Morimoto 
117490be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, component);
117590be711eSKuninori Morimoto 	}
117690be711eSKuninori Morimoto 
117790be711eSKuninori Morimoto 	/* find one from the set of registered platforms */
1178f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
117906859fcaSCharles Keepax 		platform_of_node = platform->dev->of_node;
118006859fcaSCharles Keepax 		if (!platform_of_node && platform->dev->parent->of_node)
118106859fcaSCharles Keepax 			platform_of_node = platform->dev->parent->of_node;
118206859fcaSCharles Keepax 
11835a504963SStephen Warren 		if (dai_link->platform_of_node) {
118406859fcaSCharles Keepax 			if (platform_of_node != dai_link->platform_of_node)
11855a504963SStephen Warren 				continue;
11865a504963SStephen Warren 		} else {
1187f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
11882610ab77SStephen Warren 				continue;
11895a504963SStephen Warren 		}
11902610ab77SStephen Warren 
1191f0fba2adSLiam Girdwood 		rtd->platform = platform;
1192f0fba2adSLiam Girdwood 	}
1193b19e6e7bSMark Brown 	if (!rtd->platform) {
1194f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1195f0fba2adSLiam Girdwood 			dai_link->platform_name);
119670fcad49SCharles Keepax 		goto _err_defer;
1197f0fba2adSLiam Girdwood 	}
1198b19e6e7bSMark Brown 
11991a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1200b19e6e7bSMark Brown 	return 0;
12011a497983SMengdong Lin 
12021a497983SMengdong Lin _err_defer:
12031a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
12041a497983SMengdong Lin 	return  -EPROBE_DEFER;
12056b05eda6SMark Brown }
12066b05eda6SMark Brown 
1207f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1208d12cd198SStephen Warren {
1209abd31b32SLars-Peter Clausen 	if (!component->card)
121070090bbbSLars-Peter Clausen 		return;
1211d12cd198SStephen Warren 
1212d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1213d12cd198SStephen Warren 
1214f1d45cc3SLars-Peter Clausen 	if (component->remove)
1215f1d45cc3SLars-Peter Clausen 		component->remove(component);
1216d12cd198SStephen Warren 
1217f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1218d12cd198SStephen Warren 
1219f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1220abd31b32SLars-Peter Clausen 	component->card = NULL;
1221f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1222d12cd198SStephen Warren }
1223d12cd198SStephen Warren 
1224e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1225589c3563SJarkko Nikula {
1226589c3563SJarkko Nikula 	int err;
1227589c3563SJarkko Nikula 
1228e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1229e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1230e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1231e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1232589c3563SJarkko Nikula 			if (err < 0)
1233e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1234b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1235e60cd14fSLars-Peter Clausen 					dai->name, err);
1236b0aa88afSMisael Lopez Cruz 		}
1237e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1238b0aa88afSMisael Lopez Cruz 	}
1239b0aa88afSMisael Lopez Cruz }
1240b0aa88afSMisael Lopez Cruz 
12411a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
12421a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1243f0fba2adSLiam Girdwood {
1244e60cd14fSLars-Peter Clausen 	int i;
1245f0fba2adSLiam Girdwood 
1246f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1247f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
124836ae1a96SMark Brown 		device_unregister(rtd->dev);
1249f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
125002a06d30SBarry Song 	}
125102a06d30SBarry Song 
1252f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
125388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1254e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1255f0fba2adSLiam Girdwood 
1256e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1257f0fba2adSLiam Girdwood }
1258f0fba2adSLiam Girdwood 
12591a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
12601a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
126162ae68faSStephen Warren {
126261aca564SLars-Peter Clausen 	struct snd_soc_component *component;
126390be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
126462ae68faSStephen Warren 
126590be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
126690be711eSKuninori Morimoto 		component = rtdcom->component;
126762ae68faSStephen Warren 
126870090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
126961aca564SLars-Peter Clausen 			soc_remove_component(component);
127062ae68faSStephen Warren 	}
127162ae68faSStephen Warren }
127262ae68faSStephen Warren 
12730671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
12740671fd8eSKuninori Morimoto {
12751a497983SMengdong Lin 	int order;
12761a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1277f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
12780671fd8eSKuninori Morimoto 
12790168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
12800168bf0dSLiam Girdwood 			order++) {
12811a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12821a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
12830168bf0dSLiam Girdwood 	}
128462ae68faSStephen Warren 
128562ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
128662ae68faSStephen Warren 			order++) {
12871a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12881a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
128962ae68faSStephen Warren 	}
129062ae68faSStephen Warren 
1291f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1292f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1293f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1294f8f80361SMengdong Lin 				link->name);
1295f8f80361SMengdong Lin 
1296f8f80361SMengdong Lin 		list_del(&link->list);
1297f8f80361SMengdong Lin 		card->num_dai_links--;
12980671fd8eSKuninori Morimoto 	}
12990671fd8eSKuninori Morimoto }
13000671fd8eSKuninori Morimoto 
1301923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1302923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1303923c5e61SMengdong Lin {
1304923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1305923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1306923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1307923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1308923c5e61SMengdong Lin 
1309923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1310923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1311923c5e61SMengdong Lin 				GFP_KERNEL);
1312923c5e61SMengdong Lin 		if (!dai_link->codecs)
1313923c5e61SMengdong Lin 			return -ENOMEM;
1314923c5e61SMengdong Lin 
1315923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1316923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1317923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1318923c5e61SMengdong Lin 	}
1319923c5e61SMengdong Lin 
1320923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1321923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1322923c5e61SMengdong Lin 		return -EINVAL;
1323923c5e61SMengdong Lin 	}
1324923c5e61SMengdong Lin 
1325923c5e61SMengdong Lin 	return 0;
1326923c5e61SMengdong Lin }
1327923c5e61SMengdong Lin 
1328923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1329923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1330923c5e61SMengdong Lin {
1331923c5e61SMengdong Lin 	int i, ret;
1332923c5e61SMengdong Lin 
1333923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1334923c5e61SMengdong Lin 	if (ret) {
1335923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1336923c5e61SMengdong Lin 		return ret;
1337923c5e61SMengdong Lin 	}
1338923c5e61SMengdong Lin 
1339923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1340923c5e61SMengdong Lin 		/*
1341923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1342923c5e61SMengdong Lin 		 * not both or neither.
1343923c5e61SMengdong Lin 		 */
1344923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1345923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1346923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1347923c5e61SMengdong Lin 				link->name);
1348923c5e61SMengdong Lin 			return -EINVAL;
1349923c5e61SMengdong Lin 		}
1350923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1351923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1352923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1353923c5e61SMengdong Lin 				link->name);
1354923c5e61SMengdong Lin 			return -EINVAL;
1355923c5e61SMengdong Lin 		}
1356923c5e61SMengdong Lin 	}
1357923c5e61SMengdong Lin 
1358923c5e61SMengdong Lin 	/*
1359923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1360923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1361923c5e61SMengdong Lin 	 */
1362923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1363923c5e61SMengdong Lin 		dev_err(card->dev,
1364923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1365923c5e61SMengdong Lin 			link->name);
1366923c5e61SMengdong Lin 		return -EINVAL;
1367923c5e61SMengdong Lin 	}
1368923c5e61SMengdong Lin 
1369923c5e61SMengdong Lin 	/*
1370923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1371923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1372923c5e61SMengdong Lin 	 * name alone..
1373923c5e61SMengdong Lin 	 */
1374923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1375923c5e61SMengdong Lin 		dev_err(card->dev,
1376923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1377923c5e61SMengdong Lin 			link->name);
1378923c5e61SMengdong Lin 		return -EINVAL;
1379923c5e61SMengdong Lin 	}
1380923c5e61SMengdong Lin 	/*
1381923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1382923c5e61SMengdong Lin 	 * specified
1383923c5e61SMengdong Lin 	 */
1384923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1385923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1386923c5e61SMengdong Lin 		dev_err(card->dev,
1387923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1388923c5e61SMengdong Lin 			link->name);
1389923c5e61SMengdong Lin 		return -EINVAL;
1390923c5e61SMengdong Lin 	}
1391923c5e61SMengdong Lin 
1392923c5e61SMengdong Lin 	return 0;
1393923c5e61SMengdong Lin }
1394923c5e61SMengdong Lin 
1395f8f80361SMengdong Lin /**
1396f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1397f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1398f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1399f8f80361SMengdong Lin  *
1400f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1401f8f80361SMengdong Lin  *
1402f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1403f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1404f8f80361SMengdong Lin  * DAI links in dai_link array.
1405f8f80361SMengdong Lin  */
1406f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1407f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1408f8f80361SMengdong Lin {
1409f8f80361SMengdong Lin 	if (dai_link->dobj.type
1410f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1411f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1412f8f80361SMengdong Lin 			dai_link->dobj.type);
1413f8f80361SMengdong Lin 		return -EINVAL;
1414f8f80361SMengdong Lin 	}
1415f8f80361SMengdong Lin 
1416f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1417d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1418d6f220eaSMengdong Lin 	 * on the link created by topology.
1419d6f220eaSMengdong Lin 	 */
1420d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1421d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1422d6f220eaSMengdong Lin 
1423f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1424f8f80361SMengdong Lin 	card->num_dai_links++;
1425f8f80361SMengdong Lin 
1426f8f80361SMengdong Lin 	return 0;
1427f8f80361SMengdong Lin }
1428f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1429f8f80361SMengdong Lin 
1430f8f80361SMengdong Lin /**
1431f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1432f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1433f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1434f8f80361SMengdong Lin  *
1435f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1436f8f80361SMengdong Lin  *
1437f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1438f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1439f8f80361SMengdong Lin  */
1440f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1441f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1442f8f80361SMengdong Lin {
1443f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1444f8f80361SMengdong Lin 
1445f8f80361SMengdong Lin 	if (dai_link->dobj.type
1446f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1447f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1448f8f80361SMengdong Lin 			dai_link->dobj.type);
1449f8f80361SMengdong Lin 		return;
1450f8f80361SMengdong Lin 	}
1451f8f80361SMengdong Lin 
1452f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1453d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1454d6f220eaSMengdong Lin 	 * on the link created by topology.
1455d6f220eaSMengdong Lin 	 */
1456d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1457d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1458d6f220eaSMengdong Lin 
1459f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1460f8f80361SMengdong Lin 		if (link == dai_link) {
1461f8f80361SMengdong Lin 			list_del(&link->list);
1462f8f80361SMengdong Lin 			card->num_dai_links--;
1463f8f80361SMengdong Lin 			return;
1464f8f80361SMengdong Lin 		}
1465f8f80361SMengdong Lin 	}
1466f8f80361SMengdong Lin }
1467f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1468f0fba2adSLiam Girdwood 
1469ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
147094f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1471ead9b919SJarkko Nikula {
1472ead9b919SJarkko Nikula 	int i;
1473ead9b919SJarkko Nikula 
1474ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1475ead9b919SJarkko Nikula 		return;
1476ead9b919SJarkko Nikula 
1477ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1478ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
147994f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
14803ca041edSSebastian Reichel 			continue;
148194f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
14823ca041edSSebastian Reichel 			continue;
148394f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1484ead9b919SJarkko Nikula 		break;
1485ead9b919SJarkko Nikula 	}
1486ead9b919SJarkko Nikula }
1487ead9b919SJarkko Nikula 
1488f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1489f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1490589c3563SJarkko Nikula {
1491f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1492888df395SMark Brown 	struct snd_soc_dai *dai;
1493f1d45cc3SLars-Peter Clausen 	int ret;
1494589c3563SJarkko Nikula 
14951b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
149670090bbbSLars-Peter Clausen 		return 0;
1497589c3563SJarkko Nikula 
1498abd31b32SLars-Peter Clausen 	if (component->card) {
14991b7c1231SLars-Peter Clausen 		if (component->card != card) {
15001b7c1231SLars-Peter Clausen 			dev_err(component->dev,
15011b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
15021b7c1231SLars-Peter Clausen 				card->name, component->card->name);
15031b7c1231SLars-Peter Clausen 			return -ENODEV;
15041b7c1231SLars-Peter Clausen 		}
15051b7c1231SLars-Peter Clausen 		return 0;
15061b7c1231SLars-Peter Clausen 	}
15071b7c1231SLars-Peter Clausen 
1508abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1509abd31b32SLars-Peter Clausen 		return -ENODEV;
1510abd31b32SLars-Peter Clausen 
1511f1d45cc3SLars-Peter Clausen 	component->card = card;
1512f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1513f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1514589c3563SJarkko Nikula 
1515f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1516d5d1e0beSLars-Peter Clausen 
1517688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1518688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1519688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1520688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
152177530150SLars-Peter Clausen 
1522b318ad50SNariman Poushin 		if (ret != 0) {
1523f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1524b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1525b318ad50SNariman Poushin 			goto err_probe;
1526b318ad50SNariman Poushin 		}
1527b318ad50SNariman Poushin 	}
1528b318ad50SNariman Poushin 
15290634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
15300634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1531261edc70SNariman Poushin 		if (ret != 0) {
1532f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1533261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1534261edc70SNariman Poushin 			goto err_probe;
1535261edc70SNariman Poushin 		}
1536261edc70SNariman Poushin 	}
1537888df395SMark Brown 
1538f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1539f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1540589c3563SJarkko Nikula 		if (ret < 0) {
1541f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1542f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
154370d29331SJarkko Nikula 			goto err_probe;
1544589c3563SJarkko Nikula 		}
1545cb2cf612SLiam Girdwood 
1546f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1547f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1548956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1549f1d45cc3SLars-Peter Clausen 			component->name);
1550956245e9SLiam Girdwood 	}
1551956245e9SLiam Girdwood 
1552f2ed6b07SMengdong Lin 	/* machine specific init */
1553f2ed6b07SMengdong Lin 	if (component->init) {
1554f2ed6b07SMengdong Lin 		ret = component->init(component);
1555f2ed6b07SMengdong Lin 		if (ret < 0) {
1556f2ed6b07SMengdong Lin 			dev_err(component->dev,
1557f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1558f2ed6b07SMengdong Lin 			goto err_probe;
1559f2ed6b07SMengdong Lin 		}
1560f2ed6b07SMengdong Lin 	}
1561f2ed6b07SMengdong Lin 
1562b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1563b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1564b8972bf0SKuninori Morimoto 					       component->driver->controls,
1565b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
15666969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
15676969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
15686969b2baSKuninori Morimoto 					component->driver->dapm_routes,
15696969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
1570956245e9SLiam Girdwood 
1571f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1572d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1573956245e9SLiam Girdwood 
1574956245e9SLiam Girdwood 	return 0;
1575956245e9SLiam Girdwood 
1576956245e9SLiam Girdwood err_probe:
1577f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1578abd31b32SLars-Peter Clausen 	component->card = NULL;
1579f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1580956245e9SLiam Girdwood 
1581956245e9SLiam Girdwood 	return ret;
1582956245e9SLiam Girdwood }
1583956245e9SLiam Girdwood 
158436ae1a96SMark Brown static void rtd_release(struct device *dev)
158536ae1a96SMark Brown {
158636ae1a96SMark Brown 	kfree(dev);
158736ae1a96SMark Brown }
1588f0fba2adSLiam Girdwood 
15895f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
15905f3484acSLars-Peter Clausen 	const char *name)
1591503ae5e0SMisael Lopez Cruz {
1592589c3563SJarkko Nikula 	int ret = 0;
1593589c3563SJarkko Nikula 
1594589c3563SJarkko Nikula 	/* register the rtd device */
159536ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
159636ae1a96SMark Brown 	if (!rtd->dev)
159736ae1a96SMark Brown 		return -ENOMEM;
159836ae1a96SMark Brown 	device_initialize(rtd->dev);
15995f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
160036ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1601d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1602f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
160336ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1604b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
160501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
160601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
160701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
160801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
160936ae1a96SMark Brown 	ret = device_add(rtd->dev);
1610589c3563SJarkko Nikula 	if (ret < 0) {
1611865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1612865df9cbSChuansheng Liu 		put_device(rtd->dev);
16135f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1614f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1615589c3563SJarkko Nikula 		return ret;
1616589c3563SJarkko Nikula 	}
1617589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1618589c3563SJarkko Nikula 	return 0;
1619589c3563SJarkko Nikula }
1620589c3563SJarkko Nikula 
16211a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
16221a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
162362ae68faSStephen Warren 				     int order)
162462ae68faSStephen Warren {
1625f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
162690be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
162790be711eSKuninori Morimoto 	int ret;
162862ae68faSStephen Warren 
162990be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
163090be711eSKuninori Morimoto 		component = rtdcom->component;
163190be711eSKuninori Morimoto 
163270090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1633f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
163462ae68faSStephen Warren 			if (ret < 0)
163562ae68faSStephen Warren 				return ret;
163662ae68faSStephen Warren 		}
163762ae68faSStephen Warren 	}
163862ae68faSStephen Warren 
163962ae68faSStephen Warren 	return 0;
164062ae68faSStephen Warren }
164162ae68faSStephen Warren 
16428e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1643b0aa88afSMisael Lopez Cruz {
1644b0aa88afSMisael Lopez Cruz 	int ret;
1645b0aa88afSMisael Lopez Cruz 
16468e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
16478e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
16488e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1649b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
16508e2be562SLars-Peter Clausen 				dev_err(dai->dev,
16518e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
16528e2be562SLars-Peter Clausen 					dai->name, ret);
1653b0aa88afSMisael Lopez Cruz 				return ret;
1654b0aa88afSMisael Lopez Cruz 			}
1655b0aa88afSMisael Lopez Cruz 		}
1656b0aa88afSMisael Lopez Cruz 
16578e2be562SLars-Peter Clausen 		dai->probed = 1;
1658b0aa88afSMisael Lopez Cruz 	}
1659b0aa88afSMisael Lopez Cruz 
1660b0aa88afSMisael Lopez Cruz 	return 0;
1661b0aa88afSMisael Lopez Cruz }
1662b0aa88afSMisael Lopez Cruz 
166325f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
166425f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
166525f7b701SArnaud Pouliquen {
166625f7b701SArnaud Pouliquen 	int i, ret = 0;
166725f7b701SArnaud Pouliquen 
166825f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
166925f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
167025f7b701SArnaud Pouliquen 
167125f7b701SArnaud Pouliquen 		if (!rtd->dai_link->no_pcm && drv->pcm_new)
167225f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
167325f7b701SArnaud Pouliquen 		if (ret < 0) {
167425f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
167525f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
167625f7b701SArnaud Pouliquen 				dais[i]->name);
167725f7b701SArnaud Pouliquen 			return ret;
167825f7b701SArnaud Pouliquen 		}
167925f7b701SArnaud Pouliquen 	}
168025f7b701SArnaud Pouliquen 
168125f7b701SArnaud Pouliquen 	return 0;
168225f7b701SArnaud Pouliquen }
168325f7b701SArnaud Pouliquen 
16842436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
16852436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
16863f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
16872436a723SMisael Lopez Cruz {
16883f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16893f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
16903de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
16912436a723SMisael Lopez Cruz 	int ret;
16922436a723SMisael Lopez Cruz 
169388bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
169488bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
169588bd870fSBenoit Cousson 
16962436a723SMisael Lopez Cruz 	/* link the DAI widgets */
16973de7c420SVinod Koul 	sink = codec_dai->playback_widget;
16983de7c420SVinod Koul 	source = cpu_dai->capture_widget;
16993de7c420SVinod Koul 	if (sink && source) {
17002436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
17013de7c420SVinod Koul 					   dai_link->num_params,
17023de7c420SVinod Koul 					   source, sink);
17032436a723SMisael Lopez Cruz 		if (ret != 0) {
17042436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
17053de7c420SVinod Koul 				sink->name, source->name, ret);
17062436a723SMisael Lopez Cruz 			return ret;
17072436a723SMisael Lopez Cruz 		}
17082436a723SMisael Lopez Cruz 	}
17092436a723SMisael Lopez Cruz 
17103de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
17113de7c420SVinod Koul 	source = codec_dai->capture_widget;
17123de7c420SVinod Koul 	if (sink && source) {
17132436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
17143de7c420SVinod Koul 					   dai_link->num_params,
17153de7c420SVinod Koul 					   source, sink);
17162436a723SMisael Lopez Cruz 		if (ret != 0) {
17172436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
17183de7c420SVinod Koul 				sink->name, source->name, ret);
17192436a723SMisael Lopez Cruz 			return ret;
17202436a723SMisael Lopez Cruz 		}
17212436a723SMisael Lopez Cruz 	}
17222436a723SMisael Lopez Cruz 
17232436a723SMisael Lopez Cruz 	return 0;
17242436a723SMisael Lopez Cruz }
17252436a723SMisael Lopez Cruz 
17261a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
17271a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1728f0fba2adSLiam Girdwood {
17291a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1730c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
173188bd870fSBenoit Cousson 	int i, ret;
1732f0fba2adSLiam Girdwood 
1733f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
17341a497983SMengdong Lin 			card->name, rtd->num, order);
1735f0fba2adSLiam Girdwood 
1736f0fba2adSLiam Girdwood 	/* set default power off timeout */
1737f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1738f0fba2adSLiam Girdwood 
17398e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
17408e2be562SLars-Peter Clausen 	if (ret)
1741f0fba2adSLiam Girdwood 		return ret;
1742f0fba2adSLiam Girdwood 
1743f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
174488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
17458e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1746b0aa88afSMisael Lopez Cruz 		if (ret)
1747f0fba2adSLiam Girdwood 			return ret;
174888bd870fSBenoit Cousson 	}
1749f0fba2adSLiam Girdwood 
17500168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
17510168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
17520168bf0dSLiam Girdwood 		return 0;
17530168bf0dSLiam Girdwood 
17545f3484acSLars-Peter Clausen 	/* do machine specific initialization */
17555f3484acSLars-Peter Clausen 	if (dai_link->init) {
17565f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
17575f3484acSLars-Peter Clausen 		if (ret < 0) {
17585f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
17595f3484acSLars-Peter Clausen 				dai_link->name, ret);
17605f3484acSLars-Peter Clausen 			return ret;
17615f3484acSLars-Peter Clausen 		}
17625f3484acSLars-Peter Clausen 	}
17635f3484acSLars-Peter Clausen 
1764a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1765a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1766a5053a8eSKuninori Morimoto 
17675f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1768589c3563SJarkko Nikula 	if (ret)
1769f0fba2adSLiam Girdwood 		return ret;
1770f0fba2adSLiam Girdwood 
17715f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
17725f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
17732e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
17742e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
17755f3484acSLars-Peter Clausen #endif
17765f3484acSLars-Peter Clausen 
17776f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
17781245b700SNamarta Kohli 		/*create compress_device"*/
17791a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
17801245b700SNamarta Kohli 		if (ret < 0) {
1781f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
17821245b700SNamarta Kohli 					 dai_link->stream_name);
17831245b700SNamarta Kohli 			return ret;
17841245b700SNamarta Kohli 		}
17851245b700SNamarta Kohli 	} else {
17861245b700SNamarta Kohli 
1787c74184edSMark Brown 		if (!dai_link->params) {
1788f0fba2adSLiam Girdwood 			/* create the pcm */
17891a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1790f0fba2adSLiam Girdwood 			if (ret < 0) {
1791f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
17920837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1793f0fba2adSLiam Girdwood 				return ret;
1794f0fba2adSLiam Girdwood 			}
179525f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
179625f7b701SArnaud Pouliquen 			if (ret < 0)
179725f7b701SArnaud Pouliquen 				return ret;
179825f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(rtd->codec_dais,
179925f7b701SArnaud Pouliquen 						   rtd->num_codecs, rtd);
180025f7b701SArnaud Pouliquen 			if (ret < 0)
180125f7b701SArnaud Pouliquen 				return ret;
1802c74184edSMark Brown 		} else {
18039d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
18049d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
18059d58a077SRichard Fitzgerald 
1806c74184edSMark Brown 			/* link the DAI widgets */
18073f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
18082436a723SMisael Lopez Cruz 			if (ret)
1809c74184edSMark Brown 				return ret;
1810c74184edSMark Brown 		}
1811c74184edSMark Brown 	}
1812c74184edSMark Brown 
1813f0fba2adSLiam Girdwood 	return 0;
1814f0fba2adSLiam Girdwood }
1815f0fba2adSLiam Girdwood 
181644c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1817b19e6e7bSMark Brown {
18183ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1819f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1820f2ed6b07SMengdong Lin 	const char *name;
1821f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
18223ca041edSSebastian Reichel 
1823f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1824f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1825f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1826f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1827f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1828f2ed6b07SMengdong Lin 		if (!component) {
1829f2ed6b07SMengdong Lin 			if (codec_of_node)
1830f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1831f2ed6b07SMengdong Lin 			goto err_defer;
1832f2ed6b07SMengdong Lin 		}
1833f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1834f2ed6b07SMengdong Lin 		/* generic components */
1835f2ed6b07SMengdong Lin 		name = aux_dev->name;
1836f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1837f2ed6b07SMengdong Lin 		if (!component)
1838f2ed6b07SMengdong Lin 			goto err_defer;
1839f2ed6b07SMengdong Lin 	} else {
1840f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1841f2ed6b07SMengdong Lin 		return -EINVAL;
1842f2ed6b07SMengdong Lin 	}
18433ca041edSSebastian Reichel 
1844f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1845d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
18461a653aa4SKuninori Morimoto 
1847f2ed6b07SMengdong Lin 	return 0;
1848f2ed6b07SMengdong Lin 
1849f2ed6b07SMengdong Lin err_defer:
185065d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1851b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1852b19e6e7bSMark Brown }
1853b19e6e7bSMark Brown 
1854f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1855f2ed6b07SMengdong Lin {
1856991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1857f2ed6b07SMengdong Lin 	int order;
1858f2ed6b07SMengdong Lin 	int ret;
1859f2ed6b07SMengdong Lin 
1860f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1861f2ed6b07SMengdong Lin 		order++) {
1862991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1863f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1864f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1865f2ed6b07SMengdong Lin 				if (ret < 0) {
1866f2ed6b07SMengdong Lin 					dev_err(card->dev,
1867f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1868f2ed6b07SMengdong Lin 						comp->name, ret);
1869f2ed6b07SMengdong Lin 					return ret;
1870f2ed6b07SMengdong Lin 				}
1871f2ed6b07SMengdong Lin 			}
1872f2ed6b07SMengdong Lin 		}
1873f2ed6b07SMengdong Lin 	}
187465d9361fSLars-Peter Clausen 
187544c69bb1SLars-Peter Clausen 	return 0;
18763ca041edSSebastian Reichel }
18772eea392dSJarkko Nikula 
1878f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
187944c69bb1SLars-Peter Clausen {
1880f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1881f2ed6b07SMengdong Lin 	int order;
188244c69bb1SLars-Peter Clausen 
1883f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1884f2ed6b07SMengdong Lin 		order++) {
1885f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1886991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
18871a653aa4SKuninori Morimoto 
1888f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1889f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1890991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1891991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
18922eea392dSJarkko Nikula 			}
18935f3484acSLars-Peter Clausen 		}
18942eea392dSJarkko Nikula 	}
18952eea392dSJarkko Nikula }
18962eea392dSJarkko Nikula 
1897f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1898fdf0f54dSDimitris Papastamos {
1899fdf0f54dSDimitris Papastamos 	int ret;
1900fdf0f54dSDimitris Papastamos 
1901fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1902fdf0f54dSDimitris Papastamos 		return 0;
1903fdf0f54dSDimitris Papastamos 
1904fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1905fdf0f54dSDimitris Papastamos 	if (ret < 0) {
190610e8aa9aSMichał Mirosław 		dev_err(codec->dev,
190710e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
190810e8aa9aSMichał Mirosław 			ret);
1909fdf0f54dSDimitris Papastamos 		return ret;
1910fdf0f54dSDimitris Papastamos 	}
1911fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1912fdf0f54dSDimitris Papastamos 	return 0;
1913fdf0f54dSDimitris Papastamos }
1914fdf0f54dSDimitris Papastamos 
1915ce64c8b9SLars-Peter Clausen /**
1916ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1917ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1918ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1919ce64c8b9SLars-Peter Clausen  *
1920ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1921ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1922ce64c8b9SLars-Peter Clausen  *
1923ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1924ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1925ce64c8b9SLars-Peter Clausen  *
1926ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1927ce64c8b9SLars-Peter Clausen  */
1928ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1929ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1930ce64c8b9SLars-Peter Clausen {
1931ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1932ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1933ce64c8b9SLars-Peter Clausen 	unsigned int i;
1934ce64c8b9SLars-Peter Clausen 	int ret;
1935ce64c8b9SLars-Peter Clausen 
1936ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1937ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1938ce64c8b9SLars-Peter Clausen 
1939ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1940ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1941ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1942ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1943ce64c8b9SLars-Peter Clausen 			return ret;
1944ce64c8b9SLars-Peter Clausen 		}
1945ce64c8b9SLars-Peter Clausen 	}
1946ce64c8b9SLars-Peter Clausen 
1947ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1948ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1949ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1950ce64c8b9SLars-Peter Clausen 
1951ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1952ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1953ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1954ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1955ce64c8b9SLars-Peter Clausen 			break;
1956ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1957ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1958ce64c8b9SLars-Peter Clausen 			break;
1959ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1960ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1961ce64c8b9SLars-Peter Clausen 			break;
1962ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1963ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1964ce64c8b9SLars-Peter Clausen 			break;
1965ce64c8b9SLars-Peter Clausen 		}
1966ce64c8b9SLars-Peter Clausen 
1967ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1968ce64c8b9SLars-Peter Clausen 	}
1969ce64c8b9SLars-Peter Clausen 
1970ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1971ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1972ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1973ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1974ce64c8b9SLars-Peter Clausen 		return ret;
1975ce64c8b9SLars-Peter Clausen 	}
1976ce64c8b9SLars-Peter Clausen 
1977ce64c8b9SLars-Peter Clausen 	return 0;
1978ce64c8b9SLars-Peter Clausen }
1979ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1980ce64c8b9SLars-Peter Clausen 
1981345233d7SLiam Girdwood 
19821f5a4535STakashi Iwai #ifdef CONFIG_DMI
1983345233d7SLiam Girdwood /* Trim special characters, and replace '-' with '_' since '-' is used to
1984345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1985345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1986345233d7SLiam Girdwood  */
1987345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1988345233d7SLiam Girdwood {
1989345233d7SLiam Girdwood 	int i, j = 0;
1990345233d7SLiam Girdwood 
1991345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1992345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1993345233d7SLiam Girdwood 		    || (name[i] == '_'))
1994345233d7SLiam Girdwood 			name[j++] = name[i];
1995345233d7SLiam Girdwood 		else if (name[i] == '-')
1996345233d7SLiam Girdwood 			name[j++] = '_';
1997345233d7SLiam Girdwood 	}
1998345233d7SLiam Girdwood 
1999345233d7SLiam Girdwood 	name[j] = '\0';
2000345233d7SLiam Girdwood }
2001345233d7SLiam Girdwood 
200298faf436SMengdong Lin /* Check if a DMI field is valid, i.e. not containing any string
200398faf436SMengdong Lin  * in the black list.
200498faf436SMengdong Lin  */
200598faf436SMengdong Lin static int is_dmi_valid(const char *field)
200698faf436SMengdong Lin {
200798faf436SMengdong Lin 	int i = 0;
200898faf436SMengdong Lin 
200998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
201098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
201198faf436SMengdong Lin 			return 0;
201298faf436SMengdong Lin 		i++;
201346b5a4d2SWu Fengguang 	}
201498faf436SMengdong Lin 
201598faf436SMengdong Lin 	return 1;
201698faf436SMengdong Lin }
201798faf436SMengdong Lin 
2018345233d7SLiam Girdwood /**
2019345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
2020345233d7SLiam Girdwood  * @card: The card to register DMI names
2021345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
2022345233d7SLiam Girdwood  *
2023345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
2024345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
2025345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
2026345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
2027345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
2028345233d7SLiam Girdwood  * as the sound card long name, in the format of
2029345233d7SLiam Girdwood  * "vendor-product-version-board"
2030345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
2031345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
2032345233d7SLiam Girdwood  * (UCM) configurations for the card.
2033345233d7SLiam Girdwood  *
2034345233d7SLiam Girdwood  * Possible card long names may be:
2035345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
2036345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2037345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2038345233d7SLiam Girdwood  *
2039345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
2040345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
2041345233d7SLiam Girdwood  *
2042345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
2043345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
2044345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
2045345233d7SLiam Girdwood  * charactors like SPACE.
2046345233d7SLiam Girdwood  *
2047345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
2048345233d7SLiam Girdwood  */
2049345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2050345233d7SLiam Girdwood {
2051345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
2052345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
2053345233d7SLiam Girdwood 	size_t len;
2054345233d7SLiam Girdwood 
2055345233d7SLiam Girdwood 	if (card->long_name)
2056345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
2057345233d7SLiam Girdwood 
2058345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
2059345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
206098faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
2061345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2062345233d7SLiam Girdwood 		return 0;
2063345233d7SLiam Girdwood 	}
2064345233d7SLiam Girdwood 
206598faf436SMengdong Lin 
2066345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2067345233d7SLiam Girdwood 			 "%s", vendor);
2068345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
2069345233d7SLiam Girdwood 
2070345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
207198faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
2072345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2073345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2074345233d7SLiam Girdwood 			 longname_buf_size - len,
2075345233d7SLiam Girdwood 			 "-%s", product);
2076345233d7SLiam Girdwood 
2077345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
2078345233d7SLiam Girdwood 		if (len < longname_buf_size)
2079345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2080345233d7SLiam Girdwood 
2081345233d7SLiam Girdwood 		/* some vendors like Lenovo may only put a self-explanatory
2082345233d7SLiam Girdwood 		 * name in the product version field
2083345233d7SLiam Girdwood 		 */
2084345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
208598faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
2086345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
2087345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
2088345233d7SLiam Girdwood 				 longname_buf_size - len,
2089345233d7SLiam Girdwood 				 "-%s", product_version);
2090345233d7SLiam Girdwood 
2091345233d7SLiam Girdwood 			len++;
2092345233d7SLiam Girdwood 			if (len < longname_buf_size)
2093345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
2094345233d7SLiam Girdwood 		}
2095345233d7SLiam Girdwood 	}
2096345233d7SLiam Girdwood 
2097345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
209898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
2099345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2100345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2101345233d7SLiam Girdwood 			 longname_buf_size - len,
2102345233d7SLiam Girdwood 			 "-%s", board);
2103345233d7SLiam Girdwood 
2104345233d7SLiam Girdwood 		len++;
2105345233d7SLiam Girdwood 		if (len < longname_buf_size)
2106345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2107345233d7SLiam Girdwood 	} else if (!product) {
2108345233d7SLiam Girdwood 		/* fall back to using legacy name */
2109345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2110345233d7SLiam Girdwood 		return 0;
2111345233d7SLiam Girdwood 	}
2112345233d7SLiam Girdwood 
2113345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
2114345233d7SLiam Girdwood 	if (flavour) {
2115345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2116345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2117345233d7SLiam Girdwood 			 longname_buf_size - len,
2118345233d7SLiam Girdwood 			 "-%s", flavour);
2119345233d7SLiam Girdwood 
2120345233d7SLiam Girdwood 		len++;
2121345233d7SLiam Girdwood 		if (len < longname_buf_size)
2122345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2123345233d7SLiam Girdwood 	}
2124345233d7SLiam Girdwood 
2125345233d7SLiam Girdwood 	/* set the card long name */
2126345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
2127345233d7SLiam Girdwood 
2128345233d7SLiam Girdwood 	return 0;
2129345233d7SLiam Girdwood }
2130345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
21311f5a4535STakashi Iwai #endif /* CONFIG_DMI */
2132345233d7SLiam Girdwood 
2133b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2134f0fba2adSLiam Girdwood {
2135fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
21361a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
213761b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2138ce64c8b9SLars-Peter Clausen 	int ret, i, order;
213996dd3622SMark Brown 
214034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
214101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2142f0fba2adSLiam Girdwood 
2143b19e6e7bSMark Brown 	/* bind DAIs */
2144b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
21456f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
2146b19e6e7bSMark Brown 		if (ret != 0)
2147b19e6e7bSMark Brown 			goto base_error;
2148db2a4165SFrank Mandarino 	}
2149db2a4165SFrank Mandarino 
215044c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2151b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
215244c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2153b19e6e7bSMark Brown 		if (ret != 0)
2154b19e6e7bSMark Brown 			goto base_error;
2155db2a4165SFrank Mandarino 	}
2156db2a4165SFrank Mandarino 
2157f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2158f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
2159f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
2160f8f80361SMengdong Lin 
2161fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
2162fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
2163fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
2164fdf0f54dSDimitris Papastamos 			continue;
2165f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
2166b19e6e7bSMark Brown 		if (ret < 0)
2167b19e6e7bSMark Brown 			goto base_error;
2168fdf0f54dSDimitris Papastamos 	}
2169fdf0f54dSDimitris Papastamos 
2170f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2171102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2172f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2173f0fba2adSLiam Girdwood 	if (ret < 0) {
217410e8aa9aSMichał Mirosław 		dev_err(card->dev,
217510e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
217610e8aa9aSMichał Mirosław 			card->name, ret);
2177b19e6e7bSMark Brown 		goto base_error;
2178db2a4165SFrank Mandarino 	}
2179db2a4165SFrank Mandarino 
21800757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
21810757d834SLars-Peter Clausen 
2182e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
2183e37a4970SMark Brown 	card->dapm.dev = card->dev;
2184e37a4970SMark Brown 	card->dapm.card = card;
2185e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
2186e37a4970SMark Brown 
2187d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2188d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2189d5d1e0beSLars-Peter Clausen #endif
2190d5d1e0beSLars-Peter Clausen 
219188ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
21926ed25978SAndy Green 	/* deferred resume work */
21936308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
21941301a964SRandy Dunlap #endif
21956ed25978SAndy Green 
21969a841ebbSMark Brown 	if (card->dapm_widgets)
21979a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
21989a841ebbSMark Brown 					  card->num_dapm_widgets);
21999a841ebbSMark Brown 
2200f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2201f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2202f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2203f23e860eSNicolin Chen 
2204f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2205f0fba2adSLiam Girdwood 	if (card->probe) {
2206e7361ec4SMark Brown 		ret = card->probe(card);
2207f0fba2adSLiam Girdwood 		if (ret < 0)
2208f0fba2adSLiam Girdwood 			goto card_probe_error;
2209f0fba2adSLiam Girdwood 	}
2210f0fba2adSLiam Girdwood 
221162ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
22120168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
22130168bf0dSLiam Girdwood 			order++) {
22141a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
22151a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
221662ae68faSStephen Warren 			if (ret < 0) {
2217f110bfc7SLiam Girdwood 				dev_err(card->dev,
2218f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2219f110bfc7SLiam Girdwood 					ret);
222062ae68faSStephen Warren 				goto probe_dai_err;
222162ae68faSStephen Warren 			}
222262ae68faSStephen Warren 		}
222362ae68faSStephen Warren 	}
222462ae68faSStephen Warren 
2225f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2226f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2227f2ed6b07SMengdong Lin 	if (ret < 0)
2228f2ed6b07SMengdong Lin 		goto probe_dai_err;
2229f2ed6b07SMengdong Lin 
223061b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
223161b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
223261b0088bSMengdong Lin 	 */
223361b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
223461b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
223561b0088bSMengdong Lin 			continue;
223661b0088bSMengdong Lin 
223761b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
223861b0088bSMengdong Lin 		if (ret)
223961b0088bSMengdong Lin 			goto probe_dai_err;
224061b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
224161b0088bSMengdong Lin 		if (ret)
224261b0088bSMengdong Lin 			goto probe_dai_err;
224361b0088bSMengdong Lin 	}
224461b0088bSMengdong Lin 
224562ae68faSStephen Warren 	/* probe all DAI links on this card */
224662ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
224762ae68faSStephen Warren 			order++) {
22481a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
22491a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2250fe3e78e0SMark Brown 			if (ret < 0) {
2251f110bfc7SLiam Girdwood 				dev_err(card->dev,
2252f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2253f110bfc7SLiam Girdwood 					ret);
2254f0fba2adSLiam Girdwood 				goto probe_dai_err;
2255fe3e78e0SMark Brown 			}
2256fe3e78e0SMark Brown 		}
22570168bf0dSLiam Girdwood 	}
2258fe3e78e0SMark Brown 
2259888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2260b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2261888df395SMark Brown 
2262b7af1dafSMark Brown 	if (card->controls)
2263022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
2264b7af1dafSMark Brown 
2265b8ad29deSMark Brown 	if (card->dapm_routes)
2266b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2267b8ad29deSMark Brown 					card->num_dapm_routes);
2268b8ad29deSMark Brown 
2269f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2270f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2271f23e860eSNicolin Chen 					card->num_of_dapm_routes);
227275d9ac46SMark Brown 
2273861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2274861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2275861886d3STakashi Iwai 
2276f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2277fe3e78e0SMark Brown 		 "%s", card->name);
2278f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
227922de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2280f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2281f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2282f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2283f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2284f0e8ed85SMark Brown 		case '_':
2285f0e8ed85SMark Brown 		case '-':
2286f0e8ed85SMark Brown 		case '\0':
2287f0e8ed85SMark Brown 			break;
2288f0e8ed85SMark Brown 		default:
2289f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2290f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2291f0e8ed85SMark Brown 			break;
2292f0e8ed85SMark Brown 		}
2293f0e8ed85SMark Brown 	}
2294fe3e78e0SMark Brown 
229528e9ad92SMark Brown 	if (card->late_probe) {
229628e9ad92SMark Brown 		ret = card->late_probe(card);
229728e9ad92SMark Brown 		if (ret < 0) {
2298f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
229928e9ad92SMark Brown 				card->name, ret);
230028e9ad92SMark Brown 			goto probe_aux_dev_err;
230128e9ad92SMark Brown 		}
230228e9ad92SMark Brown 	}
230328e9ad92SMark Brown 
2304824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
23058c193b8dSLars-Peter Clausen 
2306f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2307fe3e78e0SMark Brown 	if (ret < 0) {
2308f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2309f110bfc7SLiam Girdwood 				ret);
23106b3ed785SAxel Lin 		goto probe_aux_dev_err;
2311fe3e78e0SMark Brown 	}
2312fe3e78e0SMark Brown 
2313435c5e25SMark Brown 	card->instantiated = 1;
23144f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2315f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
231634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2317b19e6e7bSMark Brown 
2318b19e6e7bSMark Brown 	return 0;
2319db2a4165SFrank Mandarino 
23202eea392dSJarkko Nikula probe_aux_dev_err:
2321f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
23222eea392dSJarkko Nikula 
2323f0fba2adSLiam Girdwood probe_dai_err:
23240671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2325fe3e78e0SMark Brown 
2326f0fba2adSLiam Girdwood card_probe_error:
232787506549SMark Brown 	if (card->remove)
2328e7361ec4SMark Brown 		card->remove(card);
2329f0fba2adSLiam Girdwood 
23302210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
23310757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2332f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2333f0fba2adSLiam Girdwood 
2334b19e6e7bSMark Brown base_error:
23351a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2336f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
233734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2338db2a4165SFrank Mandarino 
2339b19e6e7bSMark Brown 	return ret;
2340435c5e25SMark Brown }
2341435c5e25SMark Brown 
2342435c5e25SMark Brown /* probes a new socdev */
2343435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2344435c5e25SMark Brown {
2345f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2346435c5e25SMark Brown 
234770a7ca34SVinod Koul 	/*
234870a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
234970a7ca34SVinod Koul 	 * we should not be here in that case so ret error
235070a7ca34SVinod Koul 	 */
235170a7ca34SVinod Koul 	if (!card)
235270a7ca34SVinod Koul 		return -EINVAL;
235370a7ca34SVinod Koul 
2354fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2355f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2356fe4085e8SMark Brown 		 card->name);
2357fe4085e8SMark Brown 
2358435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2359435c5e25SMark Brown 	card->dev = &pdev->dev;
2360f0fba2adSLiam Girdwood 
236128d528c8SMark Brown 	return snd_soc_register_card(card);
2362435c5e25SMark Brown }
2363435c5e25SMark Brown 
2364b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2365db2a4165SFrank Mandarino {
23661a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2367db2a4165SFrank Mandarino 
2368f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
23691a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
237043829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2371db2a4165SFrank Mandarino 
23724efda5f2STakashi Iwai 	/* free the ALSA card at first; this syncs with pending operations */
23734efda5f2STakashi Iwai 	snd_card_free(card->snd_card);
23744efda5f2STakashi Iwai 
2375f0fba2adSLiam Girdwood 	/* remove and free each DAI */
23760671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
23771a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2378f0fba2adSLiam Girdwood 
2379f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2380f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2381f2ed6b07SMengdong Lin 
2382d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2383a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2384a6052154SJarkko Nikula 
2385f0fba2adSLiam Girdwood 	/* remove the card */
238687506549SMark Brown 	if (card->remove)
2387e7361ec4SMark Brown 		card->remove(card);
2388f0fba2adSLiam Girdwood 
2389b0e26485SVinod Koul 	return 0;
2390b2dfa62cSGuennadi Liakhovetski }
2391b0e26485SVinod Koul 
2392b0e26485SVinod Koul /* removes a socdev */
2393b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2394b0e26485SVinod Koul {
2395b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2396b0e26485SVinod Koul 
2397c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2398db2a4165SFrank Mandarino 	return 0;
2399db2a4165SFrank Mandarino }
2400db2a4165SFrank Mandarino 
24016f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
240251737470SMark Brown {
24036f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
24041a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
240551737470SMark Brown 
240651737470SMark Brown 	if (!card->instantiated)
2407416356fcSMark Brown 		return 0;
240851737470SMark Brown 
240951737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
241051737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
24111a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
241243829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
241351737470SMark Brown 
2414f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2415416356fcSMark Brown 
2416988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
24171a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
241888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
24191a497983SMengdong Lin 		int i;
242088bd870fSBenoit Cousson 
2421988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
24221a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
24231a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
242488bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
242588bd870fSBenoit Cousson 		}
2426988e8cc4SNicolin Chen 	}
2427988e8cc4SNicolin Chen 
2428416356fcSMark Brown 	return 0;
242951737470SMark Brown }
24306f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
243151737470SMark Brown 
24326f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2433b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2434b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2435b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2436b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
24376f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2438b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2439416356fcSMark Brown };
2440deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2441416356fcSMark Brown 
2442db2a4165SFrank Mandarino /* ASoC platform driver */
2443db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2444db2a4165SFrank Mandarino 	.driver		= {
2445db2a4165SFrank Mandarino 		.name		= "soc-audio",
24466f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2447db2a4165SFrank Mandarino 	},
2448db2a4165SFrank Mandarino 	.probe		= soc_probe,
2449db2a4165SFrank Mandarino 	.remove		= soc_remove,
2450db2a4165SFrank Mandarino };
2451db2a4165SFrank Mandarino 
2452096e49d5SMark Brown /**
2453db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2454db2a4165SFrank Mandarino  * @_template: control template
2455db2a4165SFrank Mandarino  * @data: control private data
2456ac11a2b3SMark Brown  * @long_name: control long name
2457efb7ac3fSMark Brown  * @prefix: control name prefix
2458db2a4165SFrank Mandarino  *
2459db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2460db2a4165SFrank Mandarino  *
2461db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2462db2a4165SFrank Mandarino  */
2463db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
24643056557fSMark Brown 				  void *data, const char *long_name,
2465efb7ac3fSMark Brown 				  const char *prefix)
2466db2a4165SFrank Mandarino {
2467db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2468efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2469efb7ac3fSMark Brown 	char *name = NULL;
2470db2a4165SFrank Mandarino 
2471db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2472db2a4165SFrank Mandarino 	template.index = 0;
2473db2a4165SFrank Mandarino 
2474efb7ac3fSMark Brown 	if (!long_name)
2475efb7ac3fSMark Brown 		long_name = template.name;
2476efb7ac3fSMark Brown 
2477efb7ac3fSMark Brown 	if (prefix) {
24782b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2479efb7ac3fSMark Brown 		if (!name)
2480efb7ac3fSMark Brown 			return NULL;
2481efb7ac3fSMark Brown 
2482efb7ac3fSMark Brown 		template.name = name;
2483efb7ac3fSMark Brown 	} else {
2484efb7ac3fSMark Brown 		template.name = long_name;
2485efb7ac3fSMark Brown 	}
2486efb7ac3fSMark Brown 
2487efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2488efb7ac3fSMark Brown 
2489efb7ac3fSMark Brown 	kfree(name);
2490efb7ac3fSMark Brown 
2491efb7ac3fSMark Brown 	return kcontrol;
2492db2a4165SFrank Mandarino }
2493db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2494db2a4165SFrank Mandarino 
2495022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2496022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2497022658beSLiam Girdwood 	const char *prefix, void *data)
2498022658beSLiam Girdwood {
2499022658beSLiam Girdwood 	int err, i;
2500022658beSLiam Girdwood 
2501022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2502022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2503022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2504022658beSLiam Girdwood 						     control->name, prefix));
2505022658beSLiam Girdwood 		if (err < 0) {
2506f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2507f110bfc7SLiam Girdwood 				control->name, err);
2508022658beSLiam Girdwood 			return err;
2509022658beSLiam Girdwood 		}
2510022658beSLiam Girdwood 	}
2511022658beSLiam Girdwood 
2512022658beSLiam Girdwood 	return 0;
2513022658beSLiam Girdwood }
2514022658beSLiam Girdwood 
25154fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
25164fefd698SDimitris Papastamos 					       const char *name)
25174fefd698SDimitris Papastamos {
25184fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
25194fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
25204fefd698SDimitris Papastamos 
25214fefd698SDimitris Papastamos 	if (unlikely(!name))
25224fefd698SDimitris Papastamos 		return NULL;
25234fefd698SDimitris Papastamos 
25244fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
25254fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
25264fefd698SDimitris Papastamos 			return kctl;
25274fefd698SDimitris Papastamos 	return NULL;
25284fefd698SDimitris Papastamos }
25294fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
25304fefd698SDimitris Papastamos 
2531db2a4165SFrank Mandarino /**
25320f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
25330f2780adSLars-Peter Clausen  *
25340f2780adSLars-Peter Clausen  * @component: Component to add controls to
25350f2780adSLars-Peter Clausen  * @controls: Array of controls to add
25360f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
25370f2780adSLars-Peter Clausen  *
25380f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
25390f2780adSLars-Peter Clausen  */
25400f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
25410f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
25420f2780adSLars-Peter Clausen {
25430f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
25440f2780adSLars-Peter Clausen 
25450f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
25460f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
25470f2780adSLars-Peter Clausen }
25480f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
25490f2780adSLars-Peter Clausen 
25500f2780adSLars-Peter Clausen /**
2551022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2552022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
25533e8e1952SIan Molton  * duplicating this code.
25543e8e1952SIan Molton  *
25553e8e1952SIan Molton  * @codec: codec to add controls to
25563e8e1952SIan Molton  * @controls: array of controls to add
25573e8e1952SIan Molton  * @num_controls: number of elements in the array
25583e8e1952SIan Molton  *
25593e8e1952SIan Molton  * Return 0 for success, else error.
25603e8e1952SIan Molton  */
2561022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
25620f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
25633e8e1952SIan Molton {
25640f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
25650f2780adSLars-Peter Clausen 		num_controls);
25663e8e1952SIan Molton }
2567022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
25683e8e1952SIan Molton 
25693e8e1952SIan Molton /**
2570a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2571022658beSLiam Girdwood  * Convenience function to add a list of controls.
2572a491a5c8SLiam Girdwood  *
2573a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2574a491a5c8SLiam Girdwood  * @controls: array of controls to add
2575a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2576a491a5c8SLiam Girdwood  *
2577a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2578a491a5c8SLiam Girdwood  */
2579a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
25800f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2581a491a5c8SLiam Girdwood {
25820f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
25830f2780adSLars-Peter Clausen 		num_controls);
2584a491a5c8SLiam Girdwood }
2585a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2586a491a5c8SLiam Girdwood 
2587a491a5c8SLiam Girdwood /**
2588022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2589022658beSLiam Girdwood  * Convenience function to add a list of controls.
2590022658beSLiam Girdwood  *
2591022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2592022658beSLiam Girdwood  * @controls: array of controls to add
2593022658beSLiam Girdwood  * @num_controls: number of elements in the array
2594022658beSLiam Girdwood  *
2595022658beSLiam Girdwood  * Return 0 for success, else error.
2596022658beSLiam Girdwood  */
2597022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2598022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2599022658beSLiam Girdwood {
2600022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2601022658beSLiam Girdwood 
2602022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2603022658beSLiam Girdwood 			NULL, soc_card);
2604022658beSLiam Girdwood }
2605022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2606022658beSLiam Girdwood 
2607022658beSLiam Girdwood /**
2608022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2609022658beSLiam Girdwood  * Convienience function to add a list of controls.
2610022658beSLiam Girdwood  *
2611022658beSLiam Girdwood  * @dai: DAI to add controls to
2612022658beSLiam Girdwood  * @controls: array of controls to add
2613022658beSLiam Girdwood  * @num_controls: number of elements in the array
2614022658beSLiam Girdwood  *
2615022658beSLiam Girdwood  * Return 0 for success, else error.
2616022658beSLiam Girdwood  */
2617022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2618022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2619022658beSLiam Girdwood {
2620313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2621022658beSLiam Girdwood 
2622022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2623022658beSLiam Girdwood 			NULL, dai);
2624022658beSLiam Girdwood }
2625022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2626022658beSLiam Girdwood 
2627022658beSLiam Girdwood /**
26288c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
26298c6529dbSLiam Girdwood  * @dai: DAI
26308c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
26318c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
26328c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
26338c6529dbSLiam Girdwood  *
26348c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
26358c6529dbSLiam Girdwood  */
26368c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
26378c6529dbSLiam Girdwood 	unsigned int freq, int dir)
26388c6529dbSLiam Girdwood {
263946471925SKuninori Morimoto 	if (dai->driver->ops->set_sysclk)
2640f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
264171ccef0dSKuninori Morimoto 
264271ccef0dSKuninori Morimoto 	return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2643ec4ee52aSMark Brown 					    freq, dir);
26448c6529dbSLiam Girdwood }
26458c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
26468c6529dbSLiam Girdwood 
26478c6529dbSLiam Girdwood /**
2648ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2649ec4ee52aSMark Brown  * @codec: CODEC
2650ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2651da1c6ea6SMark Brown  * @source: Source for the clock
2652ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2653ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2654ec4ee52aSMark Brown  *
2655ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2656ec4ee52aSMark Brown  */
2657ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2658da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2659ec4ee52aSMark Brown {
2660ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2661da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2662da1c6ea6SMark Brown 						 freq, dir);
2663ec4ee52aSMark Brown 	else
26641104a9c8SMark Brown 		return -ENOTSUPP;
2665ec4ee52aSMark Brown }
2666ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2667ec4ee52aSMark Brown 
2668ec4ee52aSMark Brown /**
266971ccef0dSKuninori Morimoto  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
267071ccef0dSKuninori Morimoto  * @component: COMPONENT
267171ccef0dSKuninori Morimoto  * @clk_id: DAI specific clock ID
267271ccef0dSKuninori Morimoto  * @source: Source for the clock
267371ccef0dSKuninori Morimoto  * @freq: new clock frequency in Hz
267471ccef0dSKuninori Morimoto  * @dir: new clock direction - input/output.
267571ccef0dSKuninori Morimoto  *
267671ccef0dSKuninori Morimoto  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
267771ccef0dSKuninori Morimoto  */
267871ccef0dSKuninori Morimoto int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
267971ccef0dSKuninori Morimoto 			     int source, unsigned int freq, int dir)
268071ccef0dSKuninori Morimoto {
268171ccef0dSKuninori Morimoto 	/* will be removed */
268271ccef0dSKuninori Morimoto 	if (component->set_sysclk)
268371ccef0dSKuninori Morimoto 		return component->set_sysclk(component, clk_id, source,
268471ccef0dSKuninori Morimoto 					     freq, dir);
268571ccef0dSKuninori Morimoto 
268671ccef0dSKuninori Morimoto 	if (component->driver->set_sysclk)
268771ccef0dSKuninori Morimoto 		return component->driver->set_sysclk(component, clk_id, source,
268871ccef0dSKuninori Morimoto 						 freq, dir);
268971ccef0dSKuninori Morimoto 
269071ccef0dSKuninori Morimoto 	return -ENOTSUPP;
269171ccef0dSKuninori Morimoto }
269271ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
269371ccef0dSKuninori Morimoto 
269471ccef0dSKuninori Morimoto /**
26958c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
26968c6529dbSLiam Girdwood  * @dai: DAI
2697ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
26988c6529dbSLiam Girdwood  * @div: new clock divisor.
26998c6529dbSLiam Girdwood  *
27008c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
27018c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
27028c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
27038c6529dbSLiam Girdwood  */
27048c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
27058c6529dbSLiam Girdwood 	int div_id, int div)
27068c6529dbSLiam Girdwood {
270746471925SKuninori Morimoto 	if (dai->driver->ops->set_clkdiv)
2708f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
27098c6529dbSLiam Girdwood 	else
27108c6529dbSLiam Girdwood 		return -EINVAL;
27118c6529dbSLiam Girdwood }
27128c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
27138c6529dbSLiam Girdwood 
27148c6529dbSLiam Girdwood /**
27158c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
27168c6529dbSLiam Girdwood  * @dai: DAI
27178c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
271885488037SMark Brown  * @source: DAI specific source for the PLL
27198c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
27208c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
27218c6529dbSLiam Girdwood  *
27228c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
27238c6529dbSLiam Girdwood  */
272485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
272585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
27268c6529dbSLiam Girdwood {
272746471925SKuninori Morimoto 	if (dai->driver->ops->set_pll)
2728f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
272985488037SMark Brown 					 freq_in, freq_out);
2730ef641e5dSKuninori Morimoto 
2731ef641e5dSKuninori Morimoto 	return snd_soc_component_set_pll(dai->component, pll_id, source,
2732ec4ee52aSMark Brown 					 freq_in, freq_out);
27338c6529dbSLiam Girdwood }
27348c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
27358c6529dbSLiam Girdwood 
2736ec4ee52aSMark Brown /*
2737ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2738ec4ee52aSMark Brown  * @codec: CODEC
2739ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2740ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2741ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2742ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2743ec4ee52aSMark Brown  *
2744ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2745ec4ee52aSMark Brown  */
2746ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2747ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2748ec4ee52aSMark Brown {
2749ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2750ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2751ec4ee52aSMark Brown 					      freq_in, freq_out);
2752ec4ee52aSMark Brown 	else
2753ec4ee52aSMark Brown 		return -EINVAL;
2754ec4ee52aSMark Brown }
2755ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2756ec4ee52aSMark Brown 
2757ef641e5dSKuninori Morimoto /*
2758ef641e5dSKuninori Morimoto  * snd_soc_component_set_pll - configure component PLL.
2759ef641e5dSKuninori Morimoto  * @component: COMPONENT
2760ef641e5dSKuninori Morimoto  * @pll_id: DAI specific PLL ID
2761ef641e5dSKuninori Morimoto  * @source: DAI specific source for the PLL
2762ef641e5dSKuninori Morimoto  * @freq_in: PLL input clock frequency in Hz
2763ef641e5dSKuninori Morimoto  * @freq_out: requested PLL output clock frequency in Hz
2764ef641e5dSKuninori Morimoto  *
2765ef641e5dSKuninori Morimoto  * Configures and enables PLL to generate output clock based on input clock.
2766ef641e5dSKuninori Morimoto  */
2767ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2768ef641e5dSKuninori Morimoto 			      int source, unsigned int freq_in,
2769ef641e5dSKuninori Morimoto 			      unsigned int freq_out)
2770ef641e5dSKuninori Morimoto {
2771ef641e5dSKuninori Morimoto 	/* will be removed */
2772ef641e5dSKuninori Morimoto 	if (component->set_pll)
2773ef641e5dSKuninori Morimoto 		return component->set_pll(component, pll_id, source,
2774ef641e5dSKuninori Morimoto 					      freq_in, freq_out);
2775ef641e5dSKuninori Morimoto 
2776ef641e5dSKuninori Morimoto 	if (component->driver->set_pll)
2777ef641e5dSKuninori Morimoto 		return component->driver->set_pll(component, pll_id, source,
2778ef641e5dSKuninori Morimoto 					      freq_in, freq_out);
2779ef641e5dSKuninori Morimoto 
2780ef641e5dSKuninori Morimoto 	return -EINVAL;
2781ef641e5dSKuninori Morimoto }
2782ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2783ef641e5dSKuninori Morimoto 
27848c6529dbSLiam Girdwood /**
2785e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2786e54cf76bSLiam Girdwood  * @dai: DAI
2787231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2788e54cf76bSLiam Girdwood  *
2789e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2790e54cf76bSLiam Girdwood  */
2791e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2792e54cf76bSLiam Girdwood {
279346471925SKuninori Morimoto 	if (dai->driver->ops->set_bclk_ratio)
2794e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2795e54cf76bSLiam Girdwood 	else
2796e54cf76bSLiam Girdwood 		return -EINVAL;
2797e54cf76bSLiam Girdwood }
2798e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2799e54cf76bSLiam Girdwood 
2800e54cf76bSLiam Girdwood /**
28018c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
28028c6529dbSLiam Girdwood  * @dai: DAI
28038c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
28048c6529dbSLiam Girdwood  *
28058c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
28068c6529dbSLiam Girdwood  */
28078c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
28088c6529dbSLiam Girdwood {
28095e4ba569SShawn Guo 	if (dai->driver == NULL)
28108c6529dbSLiam Girdwood 		return -EINVAL;
28115e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
28125e4ba569SShawn Guo 		return -ENOTSUPP;
28135e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
28148c6529dbSLiam Girdwood }
28158c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
28168c6529dbSLiam Girdwood 
28178c6529dbSLiam Girdwood /**
2818e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
281989c67857SXiubo Li  * @slots: Number of slots in use.
282089c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
282189c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
282289c67857SXiubo Li  *
282389c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
282489c67857SXiubo Li  */
2825e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
282689c67857SXiubo Li 					  unsigned int *tx_mask,
282789c67857SXiubo Li 					  unsigned int *rx_mask)
282889c67857SXiubo Li {
282989c67857SXiubo Li 	if (*tx_mask || *rx_mask)
283089c67857SXiubo Li 		return 0;
283189c67857SXiubo Li 
283289c67857SXiubo Li 	if (!slots)
283389c67857SXiubo Li 		return -EINVAL;
283489c67857SXiubo Li 
283589c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
283689c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
283789c67857SXiubo Li 
283889c67857SXiubo Li 	return 0;
283989c67857SXiubo Li }
284089c67857SXiubo Li 
284189c67857SXiubo Li /**
2842e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2843e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2844a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2845a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
28468c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2847a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
28488c6529dbSLiam Girdwood  *
2849e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2850e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2851e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2852e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2853e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2854e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2855e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2856e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2857e46c9366SLars-Peter Clausen  * channel and so on.
2858e46c9366SLars-Peter Clausen  *
2859e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2860e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2861e46c9366SLars-Peter Clausen  *
2862e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
28638c6529dbSLiam Girdwood  */
28648c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2865a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
28668c6529dbSLiam Girdwood {
286746471925SKuninori Morimoto 	if (dai->driver->ops->xlate_tdm_slot_mask)
2868e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
286989c67857SXiubo Li 						&tx_mask, &rx_mask);
287089c67857SXiubo Li 	else
2871e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
287289c67857SXiubo Li 
287388bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
287488bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
287588bd870fSBenoit Cousson 
287646471925SKuninori Morimoto 	if (dai->driver->ops->set_tdm_slot)
2877f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2878a5479e38SDaniel Ribeiro 				slots, slot_width);
28798c6529dbSLiam Girdwood 	else
2880b2cbb6e1SXiubo Li 		return -ENOTSUPP;
28818c6529dbSLiam Girdwood }
28828c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
28838c6529dbSLiam Girdwood 
28848c6529dbSLiam Girdwood /**
2885472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2886472df3cbSBarry Song  * @dai: DAI
2887472df3cbSBarry Song  * @tx_num: how many TX channels
2888472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2889472df3cbSBarry Song  *           0~num-1 uses
2890472df3cbSBarry Song  * @rx_num: how many RX channels
2891472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2892472df3cbSBarry Song  *           0~num-1 uses
2893472df3cbSBarry Song  *
2894472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2895472df3cbSBarry Song  */
2896472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2897472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2898472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2899472df3cbSBarry Song {
290046471925SKuninori Morimoto 	if (dai->driver->ops->set_channel_map)
2901f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2902472df3cbSBarry Song 			rx_num, rx_slot);
2903472df3cbSBarry Song 	else
2904472df3cbSBarry Song 		return -EINVAL;
2905472df3cbSBarry Song }
2906472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2907472df3cbSBarry Song 
2908472df3cbSBarry Song /**
29098c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
29108c6529dbSLiam Girdwood  * @dai: DAI
29118c6529dbSLiam Girdwood  * @tristate: tristate enable
29128c6529dbSLiam Girdwood  *
29138c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
29148c6529dbSLiam Girdwood  */
29158c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
29168c6529dbSLiam Girdwood {
291746471925SKuninori Morimoto 	if (dai->driver->ops->set_tristate)
2918f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
29198c6529dbSLiam Girdwood 	else
29208c6529dbSLiam Girdwood 		return -EINVAL;
29218c6529dbSLiam Girdwood }
29228c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
29238c6529dbSLiam Girdwood 
29248c6529dbSLiam Girdwood /**
29258c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
29268c6529dbSLiam Girdwood  * @dai: DAI
29278c6529dbSLiam Girdwood  * @mute: mute enable
2928da18396fSMark Brown  * @direction: stream to mute
29298c6529dbSLiam Girdwood  *
29308c6529dbSLiam Girdwood  * Mutes the DAI DAC.
29318c6529dbSLiam Girdwood  */
2932da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2933da18396fSMark Brown 			     int direction)
29348c6529dbSLiam Girdwood {
2935da18396fSMark Brown 	if (!dai->driver)
2936da18396fSMark Brown 		return -ENOTSUPP;
2937da18396fSMark Brown 
2938da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2939da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2940da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2941da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2942f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
29438c6529dbSLiam Girdwood 	else
294404570c62SMark Brown 		return -ENOTSUPP;
29458c6529dbSLiam Girdwood }
29468c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
29478c6529dbSLiam Girdwood 
2948c5af3a2eSMark Brown /**
2949c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2950c5af3a2eSMark Brown  *
2951ac11a2b3SMark Brown  * @card: Card to register
2952c5af3a2eSMark Brown  *
2953c5af3a2eSMark Brown  */
295470a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2955c5af3a2eSMark Brown {
2956923c5e61SMengdong Lin 	int i, ret;
29571a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2958f0fba2adSLiam Girdwood 
2959c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2960c5af3a2eSMark Brown 		return -EINVAL;
2961c5af3a2eSMark Brown 
29625a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
29635a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
29645a504963SStephen Warren 
2965923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
296688bd870fSBenoit Cousson 		if (ret) {
2967923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2968923c5e61SMengdong Lin 				link->name);
296988bd870fSBenoit Cousson 			return ret;
297088bd870fSBenoit Cousson 		}
29715a504963SStephen Warren 	}
29725a504963SStephen Warren 
2973ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2974ed77cc12SMark Brown 
2975111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2976111c6419SStephen Warren 
2977f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2978f8f80361SMengdong Lin 	card->num_dai_links = 0;
2979f0fba2adSLiam Girdwood 
29801a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
29819115171aSMark Brown 	card->num_rtd = 0;
2982db2a4165SFrank Mandarino 
2983db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
29848a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2985db2a4165SFrank Mandarino 	card->instantiated = 0;
2986db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2987a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2988db2a4165SFrank Mandarino 
2989b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2990b19e6e7bSMark Brown 	if (ret != 0)
29914e2576bdSKuninori Morimoto 		return ret;
2992db2a4165SFrank Mandarino 
2993988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
29941a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
299588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
299688bd870fSBenoit Cousson 		int j;
299788bd870fSBenoit Cousson 
299888bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
299988bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3000988e8cc4SNicolin Chen 			if (!codec_dai->active)
3001988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
300288bd870fSBenoit Cousson 		}
300388bd870fSBenoit Cousson 
3004988e8cc4SNicolin Chen 		if (!cpu_dai->active)
3005988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
3006988e8cc4SNicolin Chen 	}
3007988e8cc4SNicolin Chen 
3008b19e6e7bSMark Brown 	return ret;
3009db2a4165SFrank Mandarino }
301070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3011db2a4165SFrank Mandarino 
3012db2a4165SFrank Mandarino /**
3013db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
3014db2a4165SFrank Mandarino  *
3015db2a4165SFrank Mandarino  * @card: Card to unregister
3016db2a4165SFrank Mandarino  *
3017db2a4165SFrank Mandarino  */
301870a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3019db2a4165SFrank Mandarino {
302001e0df66SLars-Peter Clausen 	if (card->instantiated) {
302101e0df66SLars-Peter Clausen 		card->instantiated = false;
30221c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
3023b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3024f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
30258f6f9b29SKuninori Morimoto 	}
3026db2a4165SFrank Mandarino 
3027db2a4165SFrank Mandarino 	return 0;
3028db2a4165SFrank Mandarino }
302970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3030db2a4165SFrank Mandarino 
3031db2a4165SFrank Mandarino /*
3032db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
3033db2a4165SFrank Mandarino  * and sanitizing names.
3034db2a4165SFrank Mandarino  */
30350b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3036db2a4165SFrank Mandarino {
3037db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
3038db2a4165SFrank Mandarino 	int id1, id2;
3039db2a4165SFrank Mandarino 
3040db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
3041db2a4165SFrank Mandarino 		return NULL;
3042db2a4165SFrank Mandarino 
304358818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3044db2a4165SFrank Mandarino 
3045db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
3046c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
3047c5af3a2eSMark Brown 	if (found) {
3048c5af3a2eSMark Brown 		/* get ID */
3049c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3050c5af3a2eSMark Brown 
3051c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
3052c5af3a2eSMark Brown 			if (*id == -1)
3053c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
3054c5af3a2eSMark Brown 		}
3055c5af3a2eSMark Brown 
3056c5af3a2eSMark Brown 	} else {
3057c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
3058c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3059c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
3060c5af3a2eSMark Brown 
3061c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
3062c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
3063c5af3a2eSMark Brown 
3064c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
3065c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
306658818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3067c5af3a2eSMark Brown 		} else
3068c5af3a2eSMark Brown 			*id = 0;
3069c5af3a2eSMark Brown 	}
3070c5af3a2eSMark Brown 
3071c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
3072c5af3a2eSMark Brown }
3073c5af3a2eSMark Brown 
3074c5af3a2eSMark Brown /*
3075c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
3076c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
3077c5af3a2eSMark Brown  */
3078c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
3079c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
3080c5af3a2eSMark Brown {
3081c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
308210e8aa9aSMichał Mirosław 		dev_err(dev,
308310e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
308410e8aa9aSMichał Mirosław 			dev_name(dev));
3085c5af3a2eSMark Brown 		return NULL;
3086c5af3a2eSMark Brown 	}
3087c5af3a2eSMark Brown 
3088c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
3089c5af3a2eSMark Brown }
3090c5af3a2eSMark Brown 
3091c5af3a2eSMark Brown /**
309232c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
30939115171aSMark Brown  *
309432c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
30959115171aSMark Brown  */
309632c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
30979115171aSMark Brown {
30985c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
30999115171aSMark Brown 
31005c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
310132c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
310232c9ba54SLars-Peter Clausen 			dai->name);
31039115171aSMark Brown 		list_del(&dai->list);
3104f0fba2adSLiam Girdwood 		kfree(dai->name);
3105f0fba2adSLiam Girdwood 		kfree(dai);
31069115171aSMark Brown 	}
310732c9ba54SLars-Peter Clausen }
31089115171aSMark Brown 
31095e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
31105e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
31115e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
31125e4fb372SMengdong Lin 	bool legacy_dai_naming)
31135e4fb372SMengdong Lin {
31145e4fb372SMengdong Lin 	struct device *dev = component->dev;
31155e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
31165e4fb372SMengdong Lin 
31175e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
31185e4fb372SMengdong Lin 
31195e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
31205e4fb372SMengdong Lin 	if (dai == NULL)
31215e4fb372SMengdong Lin 		return NULL;
31225e4fb372SMengdong Lin 
31235e4fb372SMengdong Lin 	/*
31245e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
31255e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
31265e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
31275e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
31285e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
31295e4fb372SMengdong Lin 	 * component-less anymore.
31305e4fb372SMengdong Lin 	 */
31315e4fb372SMengdong Lin 	if (legacy_dai_naming &&
31325e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
31335e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
31345e4fb372SMengdong Lin 	} else {
31355e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
31365e4fb372SMengdong Lin 		if (dai_drv->id)
31375e4fb372SMengdong Lin 			dai->id = dai_drv->id;
31385e4fb372SMengdong Lin 		else
31395e4fb372SMengdong Lin 			dai->id = component->num_dai;
31405e4fb372SMengdong Lin 	}
31415e4fb372SMengdong Lin 	if (dai->name == NULL) {
31425e4fb372SMengdong Lin 		kfree(dai);
31435e4fb372SMengdong Lin 		return NULL;
31445e4fb372SMengdong Lin 	}
31455e4fb372SMengdong Lin 
31465e4fb372SMengdong Lin 	dai->component = component;
31475e4fb372SMengdong Lin 	dai->dev = dev;
31485e4fb372SMengdong Lin 	dai->driver = dai_drv;
31495e4fb372SMengdong Lin 	if (!dai->driver->ops)
31505e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
31515e4fb372SMengdong Lin 
31525e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
31535e4fb372SMengdong Lin 	component->num_dai++;
31545e4fb372SMengdong Lin 
31555e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
31565e4fb372SMengdong Lin 	return dai;
31575e4fb372SMengdong Lin }
31585e4fb372SMengdong Lin 
31599115171aSMark Brown /**
316032c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
31619115171aSMark Brown  *
31626106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
31636106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3164ac11a2b3SMark Brown  * @count: Number of DAIs
316532c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
316632c9ba54SLars-Peter Clausen  *                     parent's name.
31679115171aSMark Brown  */
316832c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
3169bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
3170bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
31719115171aSMark Brown {
31726106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3173f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
317432c9ba54SLars-Peter Clausen 	unsigned int i;
317532c9ba54SLars-Peter Clausen 	int ret;
3176f0fba2adSLiam Girdwood 
31775b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
31789115171aSMark Brown 
3179bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
3180bb13109dSLars-Peter Clausen 
31819115171aSMark Brown 	for (i = 0; i < count; i++) {
3182f0fba2adSLiam Girdwood 
31835e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
31845e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
3185c46e0079SAxel Lin 		if (dai == NULL) {
3186c46e0079SAxel Lin 			ret = -ENOMEM;
3187c46e0079SAxel Lin 			goto err;
3188c46e0079SAxel Lin 		}
3189f0fba2adSLiam Girdwood 	}
3190f0fba2adSLiam Girdwood 
31919115171aSMark Brown 	return 0;
31929115171aSMark Brown 
31939115171aSMark Brown err:
319432c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
31959115171aSMark Brown 
31969115171aSMark Brown 	return ret;
31979115171aSMark Brown }
31989115171aSMark Brown 
319968003e6cSMengdong Lin /**
320068003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
320168003e6cSMengdong Lin  *
320268003e6cSMengdong Lin  * @component: The component the DAIs are registered for
320368003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
320468003e6cSMengdong Lin  *
320568003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
320668003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
320768003e6cSMengdong Lin  * will be freed in the component cleanup.
320868003e6cSMengdong Lin  */
320968003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
321068003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
321168003e6cSMengdong Lin {
321268003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
321368003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
321468003e6cSMengdong Lin 	struct snd_soc_dai *dai;
321568003e6cSMengdong Lin 	int ret;
321668003e6cSMengdong Lin 
321768003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
321868003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
321968003e6cSMengdong Lin 			dai_drv->dobj.type);
322068003e6cSMengdong Lin 		return -EINVAL;
322168003e6cSMengdong Lin 	}
322268003e6cSMengdong Lin 
322368003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
322468003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
322568003e6cSMengdong Lin 	if (!dai)
322668003e6cSMengdong Lin 		return -ENOMEM;
322768003e6cSMengdong Lin 
322868003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
322968003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
323068003e6cSMengdong Lin 	 */
323168003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
323268003e6cSMengdong Lin 	if (ret != 0) {
323368003e6cSMengdong Lin 		dev_err(component->dev,
323468003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
323568003e6cSMengdong Lin 	}
323668003e6cSMengdong Lin 
323768003e6cSMengdong Lin 	return ret;
323868003e6cSMengdong Lin }
323968003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
324068003e6cSMengdong Lin 
324114e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
324214e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3243d191bd8dSKuninori Morimoto {
324414e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3245d191bd8dSKuninori Morimoto 
324614e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
324714e8bdebSLars-Peter Clausen }
3248d191bd8dSKuninori Morimoto 
324914e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
325014e8bdebSLars-Peter Clausen 	int event)
325114e8bdebSLars-Peter Clausen {
325214e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
325314e8bdebSLars-Peter Clausen 
325414e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
325514e8bdebSLars-Peter Clausen }
325614e8bdebSLars-Peter Clausen 
3257f523acebSKuninori Morimoto static int snd_soc_component_drv_pcm_new(struct snd_soc_component *component,
3258f523acebSKuninori Morimoto 					struct snd_soc_pcm_runtime *rtd)
3259f523acebSKuninori Morimoto {
3260f523acebSKuninori Morimoto 	if (component->driver->pcm_new)
3261f523acebSKuninori Morimoto 		return component->driver->pcm_new(rtd);
3262f523acebSKuninori Morimoto 
3263f523acebSKuninori Morimoto 	return 0;
3264f523acebSKuninori Morimoto }
3265f523acebSKuninori Morimoto 
3266f523acebSKuninori Morimoto static void snd_soc_component_drv_pcm_free(struct snd_soc_component *component,
3267f523acebSKuninori Morimoto 					  struct snd_pcm *pcm)
3268f523acebSKuninori Morimoto {
3269f523acebSKuninori Morimoto 	if (component->driver->pcm_free)
3270f523acebSKuninori Morimoto 		component->driver->pcm_free(pcm);
3271f523acebSKuninori Morimoto }
3272f523acebSKuninori Morimoto 
32737ba236ceSKuninori Morimoto static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
32747ba236ceSKuninori Morimoto 					enum snd_soc_bias_level level)
32757ba236ceSKuninori Morimoto {
32767ba236ceSKuninori Morimoto 	struct snd_soc_component *component = dapm->component;
32777ba236ceSKuninori Morimoto 
32787ba236ceSKuninori Morimoto 	return component->driver->set_bias_level(component, level);
32797ba236ceSKuninori Morimoto }
32807ba236ceSKuninori Morimoto 
3281bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3282bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3283d191bd8dSKuninori Morimoto {
3284ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3285ce0fc93aSLars-Peter Clausen 
3286bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3287bb13109dSLars-Peter Clausen 	if (!component->name) {
3288bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3289d191bd8dSKuninori Morimoto 		return -ENOMEM;
3290d191bd8dSKuninori Morimoto 	}
3291d191bd8dSKuninori Morimoto 
3292bb13109dSLars-Peter Clausen 	component->dev = dev;
3293bb13109dSLars-Peter Clausen 	component->driver = driver;
329461aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
329561aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
32969178feb4SKuninori Morimoto 	component->suspend = component->driver->suspend;
32979178feb4SKuninori Morimoto 	component->resume = component->driver->resume;
329871ccef0dSKuninori Morimoto 	component->set_sysclk = component->driver->set_sysclk;
3299ef641e5dSKuninori Morimoto 	component->set_pll = component->driver->set_pll;
330044c07365SKuninori Morimoto 	component->set_jack = component->driver->set_jack;
3301f523acebSKuninori Morimoto 	component->pcm_new = snd_soc_component_drv_pcm_new;
3302f523acebSKuninori Morimoto 	component->pcm_free = snd_soc_component_drv_pcm_free;
3303e2c330b9SLars-Peter Clausen 
330488c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
3305ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3306ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3307ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
33087ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
33097ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
331014e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
331114e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
331214e8bdebSLars-Peter Clausen 	if (driver->stream_event)
331314e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
33147ba236ceSKuninori Morimoto 	if (driver->set_bias_level)
33157ba236ceSKuninori Morimoto 		dapm->set_bias_level = snd_soc_component_set_bias_level;
3316ce0fc93aSLars-Peter Clausen 
3317bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3318bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3319bb13109dSLars-Peter Clausen 
3320bb13109dSLars-Peter Clausen 	return 0;
3321d191bd8dSKuninori Morimoto }
3322d191bd8dSKuninori Morimoto 
332320feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3324886f5692SLars-Peter Clausen {
3325886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
332620feb881SLars-Peter Clausen 
3327886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3328886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3329886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3330886f5692SLars-Peter Clausen }
333120feb881SLars-Peter Clausen 
3332e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3333e874bf5fSLars-Peter Clausen 
333420feb881SLars-Peter Clausen /**
333520feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
333620feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
333720feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
333820feb881SLars-Peter Clausen  *
333920feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
334020feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
334120feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
334220feb881SLars-Peter Clausen  * before the first IO attempt of the component.
334320feb881SLars-Peter Clausen  */
334420feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
334520feb881SLars-Peter Clausen 	struct regmap *regmap)
334620feb881SLars-Peter Clausen {
334720feb881SLars-Peter Clausen 	component->regmap = regmap;
334820feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3349886f5692SLars-Peter Clausen }
335020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
335120feb881SLars-Peter Clausen 
335220feb881SLars-Peter Clausen /**
335320feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
335420feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
335520feb881SLars-Peter Clausen  *
335620feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
335720feb881SLars-Peter Clausen  * removes the regmap instance from the component.
335820feb881SLars-Peter Clausen  *
335920feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
336020feb881SLars-Peter Clausen  * to initialize the regmap instance.
336120feb881SLars-Peter Clausen  */
336220feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
336320feb881SLars-Peter Clausen {
336420feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
336520feb881SLars-Peter Clausen 	component->regmap = NULL;
336620feb881SLars-Peter Clausen }
336720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3368886f5692SLars-Peter Clausen 
3369e874bf5fSLars-Peter Clausen #endif
3370d191bd8dSKuninori Morimoto 
3371bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3372bb13109dSLars-Peter Clausen {
337320feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
337420feb881SLars-Peter Clausen 		if (!component->regmap)
337520feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
337620feb881SLars-Peter Clausen 		if (component->regmap)
337720feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
337820feb881SLars-Peter Clausen 	}
3379886f5692SLars-Peter Clausen 
3380bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
33818a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3382d191bd8dSKuninori Morimoto }
3383d191bd8dSKuninori Morimoto 
3384bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3385bb13109dSLars-Peter Clausen {
3386d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3387bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3388d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3389bb13109dSLars-Peter Clausen }
3390d191bd8dSKuninori Morimoto 
3391bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3392bb13109dSLars-Peter Clausen {
3393bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3394bb13109dSLars-Peter Clausen 	kfree(component->name);
3395bb13109dSLars-Peter Clausen }
3396d191bd8dSKuninori Morimoto 
3397bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3398bb13109dSLars-Peter Clausen {
3399c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3400c12c1aadSKuninori Morimoto 
3401c12c1aadSKuninori Morimoto 	if (card)
3402c12c1aadSKuninori Morimoto 		snd_soc_unregister_card(card);
3403c12c1aadSKuninori Morimoto 
3404bb13109dSLars-Peter Clausen 	list_del(&component->list);
3405bb13109dSLars-Peter Clausen }
3406d191bd8dSKuninori Morimoto 
3407*273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
3408*273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3409*273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
3410*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
3411*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
3412*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
3413*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
3414*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
3415*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
3416*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
3417*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
3418*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
3419*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
3420*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
3421*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
3422*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
3423*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
3424*273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
3425*273d778eSKuninori Morimoto };
3426*273d778eSKuninori Morimoto 
3427*273d778eSKuninori Morimoto /*
3428*273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
3429*273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
3430*273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
3431*273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
3432*273d778eSKuninori Morimoto  */
3433*273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3434*273d778eSKuninori Morimoto {
3435*273d778eSKuninori Morimoto 	int i;
3436*273d778eSKuninori Morimoto 
3437*273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3438*273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
3439*273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
3440*273d778eSKuninori Morimoto }
3441*273d778eSKuninori Morimoto 
3442e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
3443e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
3444cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3445d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3446d191bd8dSKuninori Morimoto 			int num_dai)
3447d191bd8dSKuninori Morimoto {
3448bb13109dSLars-Peter Clausen 	int ret;
3449*273d778eSKuninori Morimoto 	int i;
3450d191bd8dSKuninori Morimoto 
3451cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
3452bb13109dSLars-Peter Clausen 	if (ret)
3453bb13109dSLars-Peter Clausen 		goto err_free;
3454bb13109dSLars-Peter Clausen 
3455cf9e829eSKuninori Morimoto 	component->ignore_pmdown_time = true;
3456cf9e829eSKuninori Morimoto 	component->registered_as_component = true;
34573d59400fSLars-Peter Clausen 
3458*273d778eSKuninori Morimoto 	if (component_driver->endianness) {
3459*273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
3460*273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
3461*273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
3462*273d778eSKuninori Morimoto 		}
3463*273d778eSKuninori Morimoto 	}
3464*273d778eSKuninori Morimoto 
3465cf9e829eSKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai, true);
3466bb13109dSLars-Peter Clausen 	if (ret < 0) {
3467f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3468bb13109dSLars-Peter Clausen 		goto err_cleanup;
3469bb13109dSLars-Peter Clausen 	}
3470bb13109dSLars-Peter Clausen 
3471cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
3472bb13109dSLars-Peter Clausen 
3473bb13109dSLars-Peter Clausen 	return 0;
3474bb13109dSLars-Peter Clausen 
3475bb13109dSLars-Peter Clausen err_cleanup:
3476cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
3477bb13109dSLars-Peter Clausen err_free:
3478cf9e829eSKuninori Morimoto 	kfree(component);
3479bb13109dSLars-Peter Clausen 	return ret;
3480d191bd8dSKuninori Morimoto }
3481e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
3482e0dac41bSKuninori Morimoto 
3483e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3484e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3485e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3486e0dac41bSKuninori Morimoto 			int num_dai)
3487e0dac41bSKuninori Morimoto {
3488e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
3489e0dac41bSKuninori Morimoto 
3490e0dac41bSKuninori Morimoto 	component = kzalloc(sizeof(*component), GFP_KERNEL);
349108e61d03SKuninori Morimoto 	if (!component)
3492e0dac41bSKuninori Morimoto 		return -ENOMEM;
3493e0dac41bSKuninori Morimoto 
3494e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
3495e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
3496e0dac41bSKuninori Morimoto }
3497d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3498d191bd8dSKuninori Morimoto 
3499d191bd8dSKuninori Morimoto /**
35002eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
35012eccea8cSKuninori Morimoto  * from the ASoC core
3502d191bd8dSKuninori Morimoto  *
3503628536eaSJonathan Corbet  * @dev: The device to unregister
3504d191bd8dSKuninori Morimoto  */
35052eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
3506d191bd8dSKuninori Morimoto {
3507cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
350821a03528SKuninori Morimoto 	int found = 0;
3509d191bd8dSKuninori Morimoto 
351034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3511cf9e829eSKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
351221a03528SKuninori Morimoto 		if (dev != component->dev ||
351321a03528SKuninori Morimoto 		    !component->registered_as_component)
351421a03528SKuninori Morimoto 			continue;
3515d191bd8dSKuninori Morimoto 
3516cf9e829eSKuninori Morimoto 		snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3517cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
351821a03528SKuninori Morimoto 		found = 1;
351921a03528SKuninori Morimoto 		break;
3520d191bd8dSKuninori Morimoto 	}
3521d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3522d191bd8dSKuninori Morimoto 
352321a03528SKuninori Morimoto 	if (found) {
3524cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
3525cf9e829eSKuninori Morimoto 		kfree(component);
3526d191bd8dSKuninori Morimoto 	}
35272eccea8cSKuninori Morimoto 
35282eccea8cSKuninori Morimoto 	return found;
35292eccea8cSKuninori Morimoto }
35302eccea8cSKuninori Morimoto 
35312eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
35322eccea8cSKuninori Morimoto {
35332eccea8cSKuninori Morimoto 	while (__snd_soc_unregister_component(dev));
3534d191bd8dSKuninori Morimoto }
3535d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3536d191bd8dSKuninori Morimoto 
35377dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
35387dd5d0d9SKuninori Morimoto 						   const char *driver_name)
35397dd5d0d9SKuninori Morimoto {
35407dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
35417dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
35427dd5d0d9SKuninori Morimoto 
35437dd5d0d9SKuninori Morimoto 	ret = NULL;
35447dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
35457dd5d0d9SKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
35467dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
35477dd5d0d9SKuninori Morimoto 			continue;
35487dd5d0d9SKuninori Morimoto 
35497dd5d0d9SKuninori Morimoto 		if (driver_name &&
35507dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
35517dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
35527dd5d0d9SKuninori Morimoto 			continue;
35537dd5d0d9SKuninori Morimoto 
35547dd5d0d9SKuninori Morimoto 		ret = component;
35557dd5d0d9SKuninori Morimoto 		break;
35567dd5d0d9SKuninori Morimoto 	}
35577dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
35587dd5d0d9SKuninori Morimoto 
35597dd5d0d9SKuninori Morimoto 	return ret;
35607dd5d0d9SKuninori Morimoto }
35617dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
35627dd5d0d9SKuninori Morimoto 
3563f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3564e2c330b9SLars-Peter Clausen {
3565e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3566e2c330b9SLars-Peter Clausen 
3567f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3568e2c330b9SLars-Peter Clausen }
3569e2c330b9SLars-Peter Clausen 
3570f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3571e2c330b9SLars-Peter Clausen {
3572e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3573e2c330b9SLars-Peter Clausen 
3574f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3575e2c330b9SLars-Peter Clausen }
3576e2c330b9SLars-Peter Clausen 
3577f523acebSKuninori Morimoto static int snd_soc_platform_drv_pcm_new(struct snd_soc_component *component,
3578f523acebSKuninori Morimoto 					struct snd_soc_pcm_runtime *rtd)
3579f523acebSKuninori Morimoto {
3580f523acebSKuninori Morimoto 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3581f523acebSKuninori Morimoto 
3582f523acebSKuninori Morimoto 	if (platform->driver->pcm_new)
3583f523acebSKuninori Morimoto 		return platform->driver->pcm_new(rtd);
3584f523acebSKuninori Morimoto 
3585f523acebSKuninori Morimoto 	return 0;
3586f523acebSKuninori Morimoto }
3587f523acebSKuninori Morimoto 
3588f523acebSKuninori Morimoto static void snd_soc_platform_drv_pcm_free(struct snd_soc_component *component,
3589f523acebSKuninori Morimoto 					  struct snd_pcm *pcm)
3590f523acebSKuninori Morimoto {
3591f523acebSKuninori Morimoto 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3592f523acebSKuninori Morimoto 
3593f523acebSKuninori Morimoto 	if (platform->driver->pcm_free)
3594f523acebSKuninori Morimoto 		platform->driver->pcm_free(pcm);
3595f523acebSKuninori Morimoto }
3596f523acebSKuninori Morimoto 
3597d191bd8dSKuninori Morimoto /**
359871a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
359971a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
360071a45cdaSLars-Peter Clausen  * @platform: The platform to add
36017d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
360212a48a8cSMark Brown  */
360371a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3604d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
360512a48a8cSMark Brown {
3606b37f1d12SLars-Peter Clausen 	int ret;
3607b37f1d12SLars-Peter Clausen 
3608bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3609bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3610bb13109dSLars-Peter Clausen 	if (ret)
3611bb13109dSLars-Peter Clausen 		return ret;
3612f0fba2adSLiam Girdwood 
3613f0fba2adSLiam Girdwood 	platform->dev = dev;
3614f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3615f1d45cc3SLars-Peter Clausen 
3616f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3617f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3618f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3619f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
3620f523acebSKuninori Morimoto 	if (platform_drv->pcm_new)
3621f523acebSKuninori Morimoto 		platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3622f523acebSKuninori Morimoto 	if (platform_drv->pcm_free)
3623f523acebSKuninori Morimoto 		platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
362412a48a8cSMark Brown 
362581c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
362681c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
362781c7cfd1SLars-Peter Clausen #endif
362812a48a8cSMark Brown 
362912a48a8cSMark Brown 	mutex_lock(&client_mutex);
3630bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
363112a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
363212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
363312a48a8cSMark Brown 
3634f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3635f4333203SLars-Peter Clausen 		platform->component.name);
363612a48a8cSMark Brown 
363712a48a8cSMark Brown 	return 0;
363812a48a8cSMark Brown }
363971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
364071a45cdaSLars-Peter Clausen 
364171a45cdaSLars-Peter Clausen /**
364271a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
364371a45cdaSLars-Peter Clausen  *
3644628536eaSJonathan Corbet  * @dev: The device for the platform
3645628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
364671a45cdaSLars-Peter Clausen  */
364771a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
364871a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
364971a45cdaSLars-Peter Clausen {
365071a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
365171a45cdaSLars-Peter Clausen 	int ret;
365271a45cdaSLars-Peter Clausen 
365371a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
365471a45cdaSLars-Peter Clausen 
365571a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
365671a45cdaSLars-Peter Clausen 	if (platform == NULL)
365771a45cdaSLars-Peter Clausen 		return -ENOMEM;
365871a45cdaSLars-Peter Clausen 
365971a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
366071a45cdaSLars-Peter Clausen 	if (ret)
366171a45cdaSLars-Peter Clausen 		kfree(platform);
366271a45cdaSLars-Peter Clausen 
366371a45cdaSLars-Peter Clausen 	return ret;
366471a45cdaSLars-Peter Clausen }
366512a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
366612a48a8cSMark Brown 
366712a48a8cSMark Brown /**
366871a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
366971a45cdaSLars-Peter Clausen  * @platform: the platform to remove
367071a45cdaSLars-Peter Clausen  */
367171a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
367271a45cdaSLars-Peter Clausen {
3673b37f1d12SLars-Peter Clausen 
367471a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
367571a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3676bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
367771a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
367871a45cdaSLars-Peter Clausen 
367971a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3680f4333203SLars-Peter Clausen 		platform->component.name);
3681decc27b0SDaniel Mack 
3682decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
368371a45cdaSLars-Peter Clausen }
368471a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
368571a45cdaSLars-Peter Clausen 
368671a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
368771a45cdaSLars-Peter Clausen {
368871a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
368971a45cdaSLars-Peter Clausen 
369034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
369171a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
369234e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
369334e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
369471a45cdaSLars-Peter Clausen 			return platform;
369571a45cdaSLars-Peter Clausen 		}
369634e81ab4SLars-Peter Clausen 	}
369734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
369871a45cdaSLars-Peter Clausen 
369971a45cdaSLars-Peter Clausen 	return NULL;
370071a45cdaSLars-Peter Clausen }
370171a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
370271a45cdaSLars-Peter Clausen 
370371a45cdaSLars-Peter Clausen /**
370412a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
370512a48a8cSMark Brown  *
3706628536eaSJonathan Corbet  * @dev: platform to unregister
370712a48a8cSMark Brown  */
3708f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
370912a48a8cSMark Brown {
3710f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3711f0fba2adSLiam Girdwood 
371271a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
371371a45cdaSLars-Peter Clausen 	if (!platform)
3714f0fba2adSLiam Girdwood 		return;
3715f0fba2adSLiam Girdwood 
371671a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3717f0fba2adSLiam Girdwood 	kfree(platform);
371812a48a8cSMark Brown }
371912a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
372012a48a8cSMark Brown 
3721f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3722f1d45cc3SLars-Peter Clausen {
3723f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3724f1d45cc3SLars-Peter Clausen 
3725f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3726f1d45cc3SLars-Peter Clausen }
3727f1d45cc3SLars-Peter Clausen 
3728f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3729f1d45cc3SLars-Peter Clausen {
3730f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3731f1d45cc3SLars-Peter Clausen 
3732f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3733f1d45cc3SLars-Peter Clausen }
3734f1d45cc3SLars-Peter Clausen 
37359178feb4SKuninori Morimoto static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
37369178feb4SKuninori Morimoto {
37379178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
37389178feb4SKuninori Morimoto 
37399178feb4SKuninori Morimoto 	return codec->driver->suspend(codec);
37409178feb4SKuninori Morimoto }
37419178feb4SKuninori Morimoto 
37429178feb4SKuninori Morimoto static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
37439178feb4SKuninori Morimoto {
37449178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
37459178feb4SKuninori Morimoto 
37469178feb4SKuninori Morimoto 	return codec->driver->resume(codec);
37479178feb4SKuninori Morimoto }
37489178feb4SKuninori Morimoto 
3749e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3750e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3751e2c330b9SLars-Peter Clausen {
3752e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3753e2c330b9SLars-Peter Clausen 
3754e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3755e2c330b9SLars-Peter Clausen }
3756e2c330b9SLars-Peter Clausen 
3757e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3758e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3759e2c330b9SLars-Peter Clausen {
3760e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3761e2c330b9SLars-Peter Clausen 
3762e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3763e2c330b9SLars-Peter Clausen 
3764e2c330b9SLars-Peter Clausen 	return 0;
3765e2c330b9SLars-Peter Clausen }
3766e2c330b9SLars-Peter Clausen 
376771ccef0dSKuninori Morimoto static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
376871ccef0dSKuninori Morimoto 			  int clk_id, int source, unsigned int freq, int dir)
376971ccef0dSKuninori Morimoto {
377071ccef0dSKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
377171ccef0dSKuninori Morimoto 
377271ccef0dSKuninori Morimoto 	return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
377371ccef0dSKuninori Morimoto }
377471ccef0dSKuninori Morimoto 
3775ef641e5dSKuninori Morimoto static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3776ef641e5dSKuninori Morimoto 				  int pll_id, int source, unsigned int freq_in,
3777ef641e5dSKuninori Morimoto 				  unsigned int freq_out)
3778ef641e5dSKuninori Morimoto {
3779ef641e5dSKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3780ef641e5dSKuninori Morimoto 
3781ef641e5dSKuninori Morimoto 	return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3782ef641e5dSKuninori Morimoto }
3783ef641e5dSKuninori Morimoto 
378444c07365SKuninori Morimoto static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
378544c07365SKuninori Morimoto 			       struct snd_soc_jack *jack, void *data)
378644c07365SKuninori Morimoto {
378744c07365SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
378844c07365SKuninori Morimoto 
378944c07365SKuninori Morimoto 	return snd_soc_codec_set_jack(codec, jack, data);
379044c07365SKuninori Morimoto }
379144c07365SKuninori Morimoto 
379268f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
379368f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
379468f831c2SLars-Peter Clausen {
379568f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
379668f831c2SLars-Peter Clausen 
379768f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
379868f831c2SLars-Peter Clausen }
379968f831c2SLars-Peter Clausen 
38000d0cf00aSMark Brown /**
38010d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
38020d0cf00aSMark Brown  *
3803628536eaSJonathan Corbet  * @dev: The parent device for this codec
3804628536eaSJonathan Corbet  * @codec_drv: Codec driver
3805628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3806628536eaSJonathan Corbet  * @num_dai: Number of DAIs
38070d0cf00aSMark Brown  */
3808f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3809001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3810001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3811001ae4c0SMark Brown 			   int num_dai)
38120d0cf00aSMark Brown {
38134890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3814f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3815bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3816f0fba2adSLiam Girdwood 	int ret, i;
3817151ab22cSMark Brown 
3818f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
38190d0cf00aSMark Brown 
3820f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3821f0fba2adSLiam Girdwood 	if (codec == NULL)
3822f0fba2adSLiam Girdwood 		return -ENOMEM;
38230d0cf00aSMark Brown 
3824f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3825ce0fc93aSLars-Peter Clausen 
3826bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3827bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3828bb13109dSLars-Peter Clausen 	if (ret)
3829bb13109dSLars-Peter Clausen 		goto err_free;
3830151ab22cSMark Brown 
3831f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3832f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3833f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3834f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
38359178feb4SKuninori Morimoto 	if (codec_drv->suspend)
38369178feb4SKuninori Morimoto 		codec->component.suspend = snd_soc_codec_drv_suspend;
38379178feb4SKuninori Morimoto 	if (codec_drv->resume)
38389178feb4SKuninori Morimoto 		codec->component.resume = snd_soc_codec_drv_resume;
3839e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3840e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3841e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3842e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
384371ccef0dSKuninori Morimoto 	if (codec_drv->set_sysclk)
384471ccef0dSKuninori Morimoto 		codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
3845ef641e5dSKuninori Morimoto 	if (codec_drv->set_pll)
3846ef641e5dSKuninori Morimoto 		codec->component.set_pll = snd_soc_codec_set_pll_;
384744c07365SKuninori Morimoto 	if (codec_drv->set_jack)
384844c07365SKuninori Morimoto 		codec->component.set_jack = snd_soc_codec_set_jack_;
38493d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
38504890140fSLars-Peter Clausen 
38514890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
38524890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
38534890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
385414e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
38554890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
385668f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
38574890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3858f0fba2adSLiam Girdwood 	codec->dev = dev;
3859f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3860e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3861f0fba2adSLiam Girdwood 
386281c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
386381c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
386481c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
386581c7cfd1SLars-Peter Clausen #endif
3866a39f75f7SXiubo Li 
3867a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3868886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3869a39f75f7SXiubo Li 
3870f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3871*273d778eSKuninori Morimoto 		convert_endianness_formats(&dai_drv[i].playback);
3872*273d778eSKuninori Morimoto 		convert_endianness_formats(&dai_drv[i].capture);
3873f0fba2adSLiam Girdwood 	}
3874f0fba2adSLiam Girdwood 
3875bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3876bb13109dSLars-Peter Clausen 	if (ret < 0) {
3877f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3878bb13109dSLars-Peter Clausen 		goto err_cleanup;
3879bb13109dSLars-Peter Clausen 	}
3880bb13109dSLars-Peter Clausen 
3881bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3882bb13109dSLars-Peter Clausen 		dai->codec = codec;
3883bb13109dSLars-Peter Clausen 
3884054880feSMark Brown 	mutex_lock(&client_mutex);
3885bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3886054880feSMark Brown 	list_add(&codec->list, &codec_list);
3887054880feSMark Brown 	mutex_unlock(&client_mutex);
3888054880feSMark Brown 
3889f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3890f4333203SLars-Peter Clausen 		codec->component.name);
38910d0cf00aSMark Brown 	return 0;
3892f0fba2adSLiam Girdwood 
3893bb13109dSLars-Peter Clausen err_cleanup:
3894bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3895bb13109dSLars-Peter Clausen err_free:
3896f0fba2adSLiam Girdwood 	kfree(codec);
3897f0fba2adSLiam Girdwood 	return ret;
38980d0cf00aSMark Brown }
38990d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
39000d0cf00aSMark Brown 
39010d0cf00aSMark Brown /**
39020d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
39030d0cf00aSMark Brown  *
3904628536eaSJonathan Corbet  * @dev: codec to unregister
39050d0cf00aSMark Brown  */
3906f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
39070d0cf00aSMark Brown {
3908f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3909f0fba2adSLiam Girdwood 
391034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3911f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3912f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3913f0fba2adSLiam Girdwood 			goto found;
3914f0fba2adSLiam Girdwood 	}
391534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3916f0fba2adSLiam Girdwood 	return;
3917f0fba2adSLiam Girdwood 
3918f0fba2adSLiam Girdwood found:
39190d0cf00aSMark Brown 	list_del(&codec->list);
3920bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
39210d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
39220d0cf00aSMark Brown 
3923f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3924f4333203SLars-Peter Clausen 			codec->component.name);
3925f0fba2adSLiam Girdwood 
3926bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
39277a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3928f0fba2adSLiam Girdwood 	kfree(codec);
39290d0cf00aSMark Brown }
39300d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
39310d0cf00aSMark Brown 
3932bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3933b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3934bec4fa05SStephen Warren 			       const char *propname)
3935bec4fa05SStephen Warren {
3936b07609ceSKuninori Morimoto 	struct device_node *np;
3937bec4fa05SStephen Warren 	int ret;
3938bec4fa05SStephen Warren 
39397e07e7c0STushar Behera 	if (!card->dev) {
39407e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
39417e07e7c0STushar Behera 		return -EINVAL;
39427e07e7c0STushar Behera 	}
39437e07e7c0STushar Behera 
39447e07e7c0STushar Behera 	np = card->dev->of_node;
39457e07e7c0STushar Behera 
3946bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3947bec4fa05SStephen Warren 	/*
3948bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3949bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3950bec4fa05SStephen Warren 	 * snd_soc_register_card.
3951bec4fa05SStephen Warren 	 */
3952bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3953bec4fa05SStephen Warren 		dev_err(card->dev,
3954f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3955bec4fa05SStephen Warren 			propname, ret);
3956bec4fa05SStephen Warren 		return ret;
3957bec4fa05SStephen Warren 	}
3958bec4fa05SStephen Warren 
3959bec4fa05SStephen Warren 	return 0;
3960bec4fa05SStephen Warren }
3961b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3962bec4fa05SStephen Warren 
39639a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
39649a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
39659a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
39669a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
39679a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
39689a6d4860SXiubo Li };
39699a6d4860SXiubo Li 
397021efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
39719a6d4860SXiubo Li 					  const char *propname)
39729a6d4860SXiubo Li {
397321efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
39749a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
39759a6d4860SXiubo Li 	const char *template, *wname;
39769a6d4860SXiubo Li 	int i, j, num_widgets, ret;
39779a6d4860SXiubo Li 
39789a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
39799a6d4860SXiubo Li 	if (num_widgets < 0) {
39809a6d4860SXiubo Li 		dev_err(card->dev,
39819a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
39829a6d4860SXiubo Li 		return -EINVAL;
39839a6d4860SXiubo Li 	}
39849a6d4860SXiubo Li 	if (num_widgets & 1) {
39859a6d4860SXiubo Li 		dev_err(card->dev,
39869a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
39879a6d4860SXiubo Li 		return -EINVAL;
39889a6d4860SXiubo Li 	}
39899a6d4860SXiubo Li 
39909a6d4860SXiubo Li 	num_widgets /= 2;
39919a6d4860SXiubo Li 	if (!num_widgets) {
39929a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
39939a6d4860SXiubo Li 			propname);
39949a6d4860SXiubo Li 		return -EINVAL;
39959a6d4860SXiubo Li 	}
39969a6d4860SXiubo Li 
39979a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
39989a6d4860SXiubo Li 			       GFP_KERNEL);
39999a6d4860SXiubo Li 	if (!widgets) {
40009a6d4860SXiubo Li 		dev_err(card->dev,
40019a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
40029a6d4860SXiubo Li 		return -ENOMEM;
40039a6d4860SXiubo Li 	}
40049a6d4860SXiubo Li 
40059a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
40069a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
40079a6d4860SXiubo Li 			2 * i, &template);
40089a6d4860SXiubo Li 		if (ret) {
40099a6d4860SXiubo Li 			dev_err(card->dev,
40109a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
40119a6d4860SXiubo Li 				propname, 2 * i, ret);
40129a6d4860SXiubo Li 			return -EINVAL;
40139a6d4860SXiubo Li 		}
40149a6d4860SXiubo Li 
40159a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
40169a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
40179a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
40189a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
40199a6d4860SXiubo Li 				break;
40209a6d4860SXiubo Li 			}
40219a6d4860SXiubo Li 		}
40229a6d4860SXiubo Li 
40239a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
40249a6d4860SXiubo Li 			dev_err(card->dev,
40259a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
40269a6d4860SXiubo Li 				template);
40279a6d4860SXiubo Li 			return -EINVAL;
40289a6d4860SXiubo Li 		}
40299a6d4860SXiubo Li 
40309a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
40319a6d4860SXiubo Li 						    (2 * i) + 1,
40329a6d4860SXiubo Li 						    &wname);
40339a6d4860SXiubo Li 		if (ret) {
40349a6d4860SXiubo Li 			dev_err(card->dev,
40359a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
40369a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
40379a6d4860SXiubo Li 			return -EINVAL;
40389a6d4860SXiubo Li 		}
40399a6d4860SXiubo Li 
40409a6d4860SXiubo Li 		widgets[i].name = wname;
40419a6d4860SXiubo Li 	}
40429a6d4860SXiubo Li 
4043f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
4044f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
40459a6d4860SXiubo Li 
40469a6d4860SXiubo Li 	return 0;
40479a6d4860SXiubo Li }
404821efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
40499a6d4860SXiubo Li 
40506131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
40516131084aSJyri Sarha 				    const char *prop_name,
40526131084aSJyri Sarha 				    unsigned int *mask)
40536131084aSJyri Sarha {
40546131084aSJyri Sarha 	u32 val;
40556c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
40566131084aSJyri Sarha 	int i;
40576131084aSJyri Sarha 
40586131084aSJyri Sarha 	if (!of_slot_mask)
40596131084aSJyri Sarha 		return 0;
40606131084aSJyri Sarha 	val /= sizeof(u32);
40616131084aSJyri Sarha 	for (i = 0; i < val; i++)
40626131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
40636131084aSJyri Sarha 			*mask |= (1 << i);
40646131084aSJyri Sarha 
40656131084aSJyri Sarha 	return val;
40666131084aSJyri Sarha }
40676131084aSJyri Sarha 
406889c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
40696131084aSJyri Sarha 			      unsigned int *tx_mask,
40706131084aSJyri Sarha 			      unsigned int *rx_mask,
407189c67857SXiubo Li 			      unsigned int *slots,
407289c67857SXiubo Li 			      unsigned int *slot_width)
407389c67857SXiubo Li {
407489c67857SXiubo Li 	u32 val;
407589c67857SXiubo Li 	int ret;
407689c67857SXiubo Li 
40776131084aSJyri Sarha 	if (tx_mask)
40786131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
40796131084aSJyri Sarha 	if (rx_mask)
40806131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
40816131084aSJyri Sarha 
408289c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
408389c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
408489c67857SXiubo Li 		if (ret)
408589c67857SXiubo Li 			return ret;
408689c67857SXiubo Li 
408789c67857SXiubo Li 		if (slots)
408889c67857SXiubo Li 			*slots = val;
408989c67857SXiubo Li 	}
409089c67857SXiubo Li 
409189c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
409289c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
409389c67857SXiubo Li 		if (ret)
409489c67857SXiubo Li 			return ret;
409589c67857SXiubo Li 
409689c67857SXiubo Li 		if (slot_width)
409789c67857SXiubo Li 			*slot_width = val;
409889c67857SXiubo Li 	}
409989c67857SXiubo Li 
410089c67857SXiubo Li 	return 0;
410189c67857SXiubo Li }
410289c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
410389c67857SXiubo Li 
4104440a3006SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
41055e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
41065e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
41075e3cdaa2SKuninori Morimoto 				   const char *propname)
41085e3cdaa2SKuninori Morimoto {
4109440a3006SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
41105e3cdaa2SKuninori Morimoto 	const char *str;
41115e3cdaa2SKuninori Morimoto 	int ret;
41125e3cdaa2SKuninori Morimoto 
41135e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
41145e3cdaa2SKuninori Morimoto 	if (ret < 0) {
41155e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
41165e3cdaa2SKuninori Morimoto 		return;
41175e3cdaa2SKuninori Morimoto 	}
41185e3cdaa2SKuninori Morimoto 
41195e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
41205e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
41215e3cdaa2SKuninori Morimoto }
4122440a3006SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
41235e3cdaa2SKuninori Morimoto 
41242bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4125a4a54dd5SStephen Warren 				   const char *propname)
4126a4a54dd5SStephen Warren {
41272bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
4128e3b1e6a1SMark Brown 	int num_routes;
4129a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4130a4a54dd5SStephen Warren 	int i, ret;
4131a4a54dd5SStephen Warren 
4132a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4133c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
413410e8aa9aSMichał Mirosław 		dev_err(card->dev,
413510e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
413610e8aa9aSMichał Mirosław 			propname);
4137a4a54dd5SStephen Warren 		return -EINVAL;
4138a4a54dd5SStephen Warren 	}
4139a4a54dd5SStephen Warren 	num_routes /= 2;
4140a4a54dd5SStephen Warren 	if (!num_routes) {
4141f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4142a4a54dd5SStephen Warren 			propname);
4143a4a54dd5SStephen Warren 		return -EINVAL;
4144a4a54dd5SStephen Warren 	}
4145a4a54dd5SStephen Warren 
4146e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4147a4a54dd5SStephen Warren 			      GFP_KERNEL);
4148a4a54dd5SStephen Warren 	if (!routes) {
4149a4a54dd5SStephen Warren 		dev_err(card->dev,
4150f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4151a4a54dd5SStephen Warren 		return -EINVAL;
4152a4a54dd5SStephen Warren 	}
4153a4a54dd5SStephen Warren 
4154a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4155a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4156e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
4157a4a54dd5SStephen Warren 		if (ret) {
4158c871bd0bSMark Brown 			dev_err(card->dev,
4159c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4160c871bd0bSMark Brown 				propname, 2 * i, ret);
4161a4a54dd5SStephen Warren 			return -EINVAL;
4162a4a54dd5SStephen Warren 		}
4163a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4164e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
4165a4a54dd5SStephen Warren 		if (ret) {
4166a4a54dd5SStephen Warren 			dev_err(card->dev,
4167c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4168c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4169a4a54dd5SStephen Warren 			return -EINVAL;
4170a4a54dd5SStephen Warren 		}
4171a4a54dd5SStephen Warren 	}
4172a4a54dd5SStephen Warren 
4173f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
4174f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
4175a4a54dd5SStephen Warren 
4176a4a54dd5SStephen Warren 	return 0;
4177a4a54dd5SStephen Warren }
41782bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4179a4a54dd5SStephen Warren 
4180a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4181389cb834SJyri Sarha 				     const char *prefix,
4182389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
4183389cb834SJyri Sarha 				     struct device_node **framemaster)
4184a7930ed4SKuninori Morimoto {
4185a7930ed4SKuninori Morimoto 	int ret, i;
4186a7930ed4SKuninori Morimoto 	char prop[128];
4187a7930ed4SKuninori Morimoto 	unsigned int format = 0;
4188a7930ed4SKuninori Morimoto 	int bit, frame;
4189a7930ed4SKuninori Morimoto 	const char *str;
4190a7930ed4SKuninori Morimoto 	struct {
4191a7930ed4SKuninori Morimoto 		char *name;
4192a7930ed4SKuninori Morimoto 		unsigned int val;
4193a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
4194a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
4195a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
4196a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
4197a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
4198a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
4199a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
4200a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
4201a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
4202a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
4203a7930ed4SKuninori Morimoto 	};
4204a7930ed4SKuninori Morimoto 
4205a7930ed4SKuninori Morimoto 	if (!prefix)
4206a7930ed4SKuninori Morimoto 		prefix = "";
4207a7930ed4SKuninori Morimoto 
4208a7930ed4SKuninori Morimoto 	/*
42095711c979SKuninori Morimoto 	 * check "dai-format = xxx"
42105711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
4211a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4212a7930ed4SKuninori Morimoto 	 */
42135711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
42145711c979SKuninori Morimoto 	if (ret < 0) {
4215a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
4216a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
42175711c979SKuninori Morimoto 	}
4218a7930ed4SKuninori Morimoto 	if (ret == 0) {
4219a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4220a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4221a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4222a7930ed4SKuninori Morimoto 				break;
4223a7930ed4SKuninori Morimoto 			}
4224a7930ed4SKuninori Morimoto 		}
4225a7930ed4SKuninori Morimoto 	}
4226a7930ed4SKuninori Morimoto 
4227a7930ed4SKuninori Morimoto 	/*
42288c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4229a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4230a7930ed4SKuninori Morimoto 	 */
42318c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
423251930295SJulia Lawall 	if (of_property_read_bool(np, prop))
42338c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
42348c2d6a9fSKuninori Morimoto 	else
42358c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4236a7930ed4SKuninori Morimoto 
4237a7930ed4SKuninori Morimoto 	/*
4238a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4239a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4240a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4241a7930ed4SKuninori Morimoto 	 */
4242a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4243a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4244a7930ed4SKuninori Morimoto 
4245a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4246a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4247a7930ed4SKuninori Morimoto 
4248a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4249a7930ed4SKuninori Morimoto 	case 0x11:
4250a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4251a7930ed4SKuninori Morimoto 		break;
4252a7930ed4SKuninori Morimoto 	case 0x10:
4253a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4254a7930ed4SKuninori Morimoto 		break;
4255a7930ed4SKuninori Morimoto 	case 0x01:
4256a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4257a7930ed4SKuninori Morimoto 		break;
4258a7930ed4SKuninori Morimoto 	default:
4259a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4260a7930ed4SKuninori Morimoto 		break;
4261a7930ed4SKuninori Morimoto 	}
4262a7930ed4SKuninori Morimoto 
4263a7930ed4SKuninori Morimoto 	/*
4264a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4265a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4266a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4267a7930ed4SKuninori Morimoto 	 */
4268a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4269a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4270389cb834SJyri Sarha 	if (bit && bitclkmaster)
4271389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4272a7930ed4SKuninori Morimoto 
4273a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4274a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4275389cb834SJyri Sarha 	if (frame && framemaster)
4276389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4277a7930ed4SKuninori Morimoto 
4278a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4279a7930ed4SKuninori Morimoto 	case 0x11:
4280a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4281a7930ed4SKuninori Morimoto 		break;
4282a7930ed4SKuninori Morimoto 	case 0x10:
4283a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4284a7930ed4SKuninori Morimoto 		break;
4285a7930ed4SKuninori Morimoto 	case 0x01:
4286a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4287a7930ed4SKuninori Morimoto 		break;
4288a7930ed4SKuninori Morimoto 	default:
4289a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4290a7930ed4SKuninori Morimoto 		break;
4291a7930ed4SKuninori Morimoto 	}
4292a7930ed4SKuninori Morimoto 
4293a7930ed4SKuninori Morimoto 	return format;
4294a7930ed4SKuninori Morimoto }
4295a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4296a7930ed4SKuninori Morimoto 
4297a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
4298a180e8b9SKuninori Morimoto {
4299a180e8b9SKuninori Morimoto 	struct snd_soc_component *pos;
4300a180e8b9SKuninori Morimoto 	struct device_node *node;
4301a180e8b9SKuninori Morimoto 	int ret;
4302a180e8b9SKuninori Morimoto 
4303a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
4304a180e8b9SKuninori Morimoto 
4305a180e8b9SKuninori Morimoto 	/*
4306a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
4307a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
4308a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
4309a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
4310a180e8b9SKuninori Morimoto 	 */
4311a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
4312a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
4313a180e8b9SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4314a180e8b9SKuninori Morimoto 		struct device_node *component_of_node = pos->dev->of_node;
4315a180e8b9SKuninori Morimoto 
4316a180e8b9SKuninori Morimoto 		if (!component_of_node && pos->dev->parent)
4317a180e8b9SKuninori Morimoto 			component_of_node = pos->dev->parent->of_node;
4318a180e8b9SKuninori Morimoto 
4319a180e8b9SKuninori Morimoto 		if (component_of_node != node)
4320a180e8b9SKuninori Morimoto 			continue;
4321a180e8b9SKuninori Morimoto 
4322a180e8b9SKuninori Morimoto 		if (pos->driver->of_xlate_dai_id)
4323a180e8b9SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_id(pos, ep);
4324a180e8b9SKuninori Morimoto 
4325a180e8b9SKuninori Morimoto 		break;
4326a180e8b9SKuninori Morimoto 	}
4327a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
4328a180e8b9SKuninori Morimoto 
4329c0a480d1STony Lindgren 	of_node_put(node);
4330c0a480d1STony Lindgren 
4331a180e8b9SKuninori Morimoto 	return ret;
4332a180e8b9SKuninori Morimoto }
4333a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4334a180e8b9SKuninori Morimoto 
43351ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
4336cb470087SKuninori Morimoto 				const char **dai_name)
4337cb470087SKuninori Morimoto {
4338cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
43393e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
434093b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
4341cb470087SKuninori Morimoto 
4342cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4343cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
43443e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
43453e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
43463e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
43473e0aa8d8SJyri Sarha 
43483e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
4349cb470087SKuninori Morimoto 			continue;
4350cb470087SKuninori Morimoto 
43516833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
435293b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
435393b0f3eeSJean-Francois Moine 							     args,
435493b0f3eeSJean-Francois Moine 							     dai_name);
43556833c452SKuninori Morimoto 		} else {
43566833c452SKuninori Morimoto 			int id = -1;
43576833c452SKuninori Morimoto 
435893b0f3eeSJean-Francois Moine 			switch (args->args_count) {
43596833c452SKuninori Morimoto 			case 0:
43606833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
43616833c452SKuninori Morimoto 				break;
43626833c452SKuninori Morimoto 			case 1:
436393b0f3eeSJean-Francois Moine 				id = args->args[0];
43646833c452SKuninori Morimoto 				break;
43656833c452SKuninori Morimoto 			default:
43666833c452SKuninori Morimoto 				/* not supported */
4367cb470087SKuninori Morimoto 				break;
4368cb470087SKuninori Morimoto 			}
4369cb470087SKuninori Morimoto 
43706833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
43716833c452SKuninori Morimoto 				ret = -EINVAL;
43723dcba280SNicolin Chen 				continue;
43736833c452SKuninori Morimoto 			}
4374e41975edSXiubo Li 
4375e41975edSXiubo Li 			ret = 0;
4376e41975edSXiubo Li 
4377e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4378e41975edSXiubo Li 			if (!*dai_name)
4379e41975edSXiubo Li 				*dai_name = pos->name;
43806833c452SKuninori Morimoto 		}
43816833c452SKuninori Morimoto 
4382cb470087SKuninori Morimoto 		break;
4383cb470087SKuninori Morimoto 	}
4384cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
438593b0f3eeSJean-Francois Moine 	return ret;
438693b0f3eeSJean-Francois Moine }
43871ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
438893b0f3eeSJean-Francois Moine 
438993b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
439093b0f3eeSJean-Francois Moine 			    const char **dai_name)
439193b0f3eeSJean-Francois Moine {
439293b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
439393b0f3eeSJean-Francois Moine 	int ret;
439493b0f3eeSJean-Francois Moine 
439593b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
439693b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
439793b0f3eeSJean-Francois Moine 	if (ret)
439893b0f3eeSJean-Francois Moine 		return ret;
439993b0f3eeSJean-Francois Moine 
440093b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
4401cb470087SKuninori Morimoto 
4402cb470087SKuninori Morimoto 	of_node_put(args.np);
4403cb470087SKuninori Morimoto 
4404cb470087SKuninori Morimoto 	return ret;
4405cb470087SKuninori Morimoto }
4406cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4407cb470087SKuninori Morimoto 
440893b0f3eeSJean-Francois Moine /*
440993b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
441093b0f3eeSJean-Francois Moine  * @dev: Card device
441193b0f3eeSJean-Francois Moine  * @of_node: Device node
441293b0f3eeSJean-Francois Moine  * @dai_link: DAI link
441393b0f3eeSJean-Francois Moine  *
441493b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
441593b0f3eeSJean-Francois Moine  * 'sound-dai'.
441693b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
441793b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
441893b0f3eeSJean-Francois Moine  *
441993b0f3eeSJean-Francois Moine  * Returns 0 for success
442093b0f3eeSJean-Francois Moine  */
442193b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
442293b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
442393b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
442493b0f3eeSJean-Francois Moine {
442593b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
442693b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
442793b0f3eeSJean-Francois Moine 	char *name;
442893b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
442993b0f3eeSJean-Francois Moine 
443093b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
443193b0f3eeSJean-Francois Moine 	name = "sound-dai";
443293b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
443393b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
443493b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
443593b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
443693b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
443793b0f3eeSJean-Francois Moine 		else
443893b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
443993b0f3eeSJean-Francois Moine 		return num_codecs;
444093b0f3eeSJean-Francois Moine 	}
444193b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
444293b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
444393b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
444493b0f3eeSJean-Francois Moine 	if (!component)
444593b0f3eeSJean-Francois Moine 		return -ENOMEM;
444693b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
444793b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
444893b0f3eeSJean-Francois Moine 
444993b0f3eeSJean-Francois Moine 	/* Parse the list */
445093b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
445193b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
445293b0f3eeSJean-Francois Moine 	     index++, component++) {
445393b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
445493b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
445593b0f3eeSJean-Francois Moine 						  index, &args);
445693b0f3eeSJean-Francois Moine 		if (ret)
445793b0f3eeSJean-Francois Moine 			goto err;
445893b0f3eeSJean-Francois Moine 		component->of_node = args.np;
445993b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
446093b0f3eeSJean-Francois Moine 		if (ret < 0)
446193b0f3eeSJean-Francois Moine 			goto err;
446293b0f3eeSJean-Francois Moine 	}
446393b0f3eeSJean-Francois Moine 	return 0;
446493b0f3eeSJean-Francois Moine err:
446593b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
446693b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
446793b0f3eeSJean-Francois Moine 	     index++, component++) {
446893b0f3eeSJean-Francois Moine 		if (!component->of_node)
446993b0f3eeSJean-Francois Moine 			break;
447093b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
447193b0f3eeSJean-Francois Moine 		component->of_node = NULL;
447293b0f3eeSJean-Francois Moine 	}
447393b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
447493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
447593b0f3eeSJean-Francois Moine 	return ret;
447693b0f3eeSJean-Francois Moine }
447793b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
447893b0f3eeSJean-Francois Moine 
4479c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4480db2a4165SFrank Mandarino {
44816553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
4482fb257897SMark Brown 	snd_soc_util_init();
4483fb257897SMark Brown 
4484db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4485db2a4165SFrank Mandarino }
44864abe8e16SMark Brown module_init(snd_soc_init);
4487db2a4165SFrank Mandarino 
44887d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4489db2a4165SFrank Mandarino {
4490fb257897SMark Brown 	snd_soc_util_exit();
44916553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
4492fb257897SMark Brown 
4493db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4494db2a4165SFrank Mandarino }
4495db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4496db2a4165SFrank Mandarino 
4497db2a4165SFrank Mandarino /* Module information */
4498d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4499db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4500db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4501db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4502