xref: /openbmc/linux/sound/soc/soc-core.c (revision e0dac41b8c21d41534b04d520c1927c2b0ac9b5e)
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 
6171a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
6181a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
6191a497983SMengdong Lin {
6201a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6211a497983SMengdong Lin 
6221a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
6231a497983SMengdong Lin 	if (!rtd)
6241a497983SMengdong Lin 		return NULL;
6251a497983SMengdong Lin 
626a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
6271a497983SMengdong Lin 	rtd->card = card;
6281a497983SMengdong Lin 	rtd->dai_link = dai_link;
6291a497983SMengdong Lin 	rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
6301a497983SMengdong Lin 					dai_link->num_codecs,
6311a497983SMengdong Lin 					GFP_KERNEL);
6321a497983SMengdong Lin 	if (!rtd->codec_dais) {
6331a497983SMengdong Lin 		kfree(rtd);
6341a497983SMengdong Lin 		return NULL;
6351a497983SMengdong Lin 	}
6361a497983SMengdong Lin 
6371a497983SMengdong Lin 	return rtd;
6381a497983SMengdong Lin }
6391a497983SMengdong Lin 
6401a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
6411a497983SMengdong Lin {
6421a497983SMengdong Lin 	if (rtd && rtd->codec_dais)
6431a497983SMengdong Lin 		kfree(rtd->codec_dais);
644a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
6451a497983SMengdong Lin 	kfree(rtd);
6461a497983SMengdong Lin }
6471a497983SMengdong Lin 
6481a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
6491a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
6501a497983SMengdong Lin {
6511a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
6521a497983SMengdong Lin 	rtd->num = card->num_rtd;
6531a497983SMengdong Lin 	card->num_rtd++;
6541a497983SMengdong Lin }
6551a497983SMengdong Lin 
6561a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
6571a497983SMengdong Lin {
6581a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
6591a497983SMengdong Lin 
6601a497983SMengdong Lin 	list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
6611a497983SMengdong Lin 		list_del(&rtd->list);
6621a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
6631a497983SMengdong Lin 	}
6641a497983SMengdong Lin 
6651a497983SMengdong Lin 	card->num_rtd = 0;
6661a497983SMengdong Lin }
6671a497983SMengdong Lin 
66847c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
66947c88fffSLiam Girdwood 		const char *dai_link)
67047c88fffSLiam Girdwood {
6711a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
67247c88fffSLiam Girdwood 
6731a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6741a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
6751a497983SMengdong Lin 			return rtd;
67647c88fffSLiam Girdwood 	}
677f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
67847c88fffSLiam Girdwood 	return NULL;
67947c88fffSLiam Girdwood }
68047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
68147c88fffSLiam Girdwood 
6829d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
6839d58a077SRichard Fitzgerald {
6849d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
6859d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
6869d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
6879d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
6889d58a077SRichard Fitzgerald 	 */
6899d58a077SRichard Fitzgerald }
6909d58a077SRichard Fitzgerald 
6916f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
692db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
6936f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
694db2a4165SFrank Mandarino {
6956f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
696d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6971a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6981a497983SMengdong Lin 	int i;
699db2a4165SFrank Mandarino 
700c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
701c5599b87SLars-Peter Clausen 	if (!card->instantiated)
702e3509ff0SDaniel Mack 		return 0;
703e3509ff0SDaniel Mack 
7046ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
7056ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
7066ed25978SAndy Green 	 */
707f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
7086ed25978SAndy Green 
7096ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
710f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
7116ed25978SAndy Green 
712a00f90f9SMark Brown 	/* mute any active DACs */
7131a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7143efab7dcSMark Brown 
7151a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7163efab7dcSMark Brown 			continue;
7173efab7dcSMark Brown 
7181a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
7191a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
72088bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
72188bd870fSBenoit Cousson 
722f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
723f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
724db2a4165SFrank Mandarino 		}
72588bd870fSBenoit Cousson 	}
726db2a4165SFrank Mandarino 
7274ccab3e7SLiam Girdwood 	/* suspend all pcms */
7281a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7291a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7303efab7dcSMark Brown 			continue;
7313efab7dcSMark Brown 
7321a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
7333efab7dcSMark Brown 	}
7344ccab3e7SLiam Girdwood 
73587506549SMark Brown 	if (card->suspend_pre)
73670b2ac12SMark Brown 		card->suspend_pre(card);
737db2a4165SFrank Mandarino 
7381a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7391a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7403efab7dcSMark Brown 
7411a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7423efab7dcSMark Brown 			continue;
7433efab7dcSMark Brown 
744bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
745f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
746db2a4165SFrank Mandarino 	}
747db2a4165SFrank Mandarino 
74837660b6dSLars-Peter Clausen 	/* close any waiting streams */
7491a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
7501a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
751db2a4165SFrank Mandarino 
7521a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7533efab7dcSMark Brown 
7541a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7553efab7dcSMark Brown 			continue;
7563efab7dcSMark Brown 
7571a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
7587bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
759db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
760f0fba2adSLiam Girdwood 
7611a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
7627bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
763db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
764db2a4165SFrank Mandarino 	}
765db2a4165SFrank Mandarino 
7668be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7678be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
768e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
769e2d32ff6SMark Brown 
7709178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
771d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
772d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
773d9fc4063SKuninori Morimoto 
7749178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
7751547aba9SMark Brown 		 * bias _ON and should not be suspended. */
7769178feb4SKuninori Morimoto 		if (!component->suspended) {
7774890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7781547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
779125a25daSMark Brown 				/*
7809178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
781125a25daSMark Brown 				 * bias off then being in STANDBY
782125a25daSMark Brown 				 * means it's doing something,
783125a25daSMark Brown 				 * otherwise fall through.
784125a25daSMark Brown 				 */
7854890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
7869178feb4SKuninori Morimoto 					dev_dbg(component->dev,
78710e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
788125a25daSMark Brown 					break;
789125a25daSMark Brown 				}
790a8093297SLars-Peter Clausen 
7911547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
7929178feb4SKuninori Morimoto 				if (component->suspend)
7939178feb4SKuninori Morimoto 					component->suspend(component);
7949178feb4SKuninori Morimoto 				component->suspended = 1;
7959178feb4SKuninori Morimoto 				if (component->regmap)
7969178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
797988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
7989178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
7991547aba9SMark Brown 				break;
8001547aba9SMark Brown 			default:
8019178feb4SKuninori Morimoto 				dev_dbg(component->dev,
8029178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
8031547aba9SMark Brown 				break;
8041547aba9SMark Brown 			}
8051547aba9SMark Brown 		}
806f0fba2adSLiam Girdwood 	}
807db2a4165SFrank Mandarino 
8081a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8091a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8103efab7dcSMark Brown 
8111a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8123efab7dcSMark Brown 			continue;
8133efab7dcSMark Brown 
814bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
815f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
816988e8cc4SNicolin Chen 
817988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
818988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
819db2a4165SFrank Mandarino 	}
820db2a4165SFrank Mandarino 
82187506549SMark Brown 	if (card->suspend_post)
82270b2ac12SMark Brown 		card->suspend_post(card);
823db2a4165SFrank Mandarino 
824db2a4165SFrank Mandarino 	return 0;
825db2a4165SFrank Mandarino }
8266f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
827db2a4165SFrank Mandarino 
8286ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
8296ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
8306ed25978SAndy Green  */
8316ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
832db2a4165SFrank Mandarino {
833f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
834f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
8351a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
836d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
8371a497983SMengdong Lin 	int i;
838db2a4165SFrank Mandarino 
8396ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
8406ed25978SAndy Green 	 * so userspace apps are blocked from touching us
8416ed25978SAndy Green 	 */
8426ed25978SAndy Green 
843f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
8446ed25978SAndy Green 
8459949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
846f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
8479949788bSMark Brown 
84887506549SMark Brown 	if (card->resume_pre)
84970b2ac12SMark Brown 		card->resume_pre(card);
850db2a4165SFrank Mandarino 
851bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
8521a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8531a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8543efab7dcSMark Brown 
8551a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8563efab7dcSMark Brown 			continue;
8573efab7dcSMark Brown 
858bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
859f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
860db2a4165SFrank Mandarino 	}
861db2a4165SFrank Mandarino 
862d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
8639178feb4SKuninori Morimoto 		if (component->suspended) {
8649178feb4SKuninori Morimoto 			if (component->resume)
8659178feb4SKuninori Morimoto 				component->resume(component);
8669178feb4SKuninori Morimoto 			component->suspended = 0;
8671547aba9SMark Brown 		}
868f0fba2adSLiam Girdwood 	}
869db2a4165SFrank Mandarino 
8701a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8713efab7dcSMark Brown 
8721a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8733efab7dcSMark Brown 			continue;
8743efab7dcSMark Brown 
8751a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
876d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
877db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
878f0fba2adSLiam Girdwood 
8791a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
880d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
881db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
882db2a4165SFrank Mandarino 	}
883db2a4165SFrank Mandarino 
8843ff3f64bSMark Brown 	/* unmute any active DACs */
8851a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8863efab7dcSMark Brown 
8871a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8883efab7dcSMark Brown 			continue;
8893efab7dcSMark Brown 
8901a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8911a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
89288bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
89388bd870fSBenoit Cousson 
894f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
895f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
896db2a4165SFrank Mandarino 		}
89788bd870fSBenoit Cousson 	}
898db2a4165SFrank Mandarino 
8991a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
9001a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9013efab7dcSMark Brown 
9021a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
9033efab7dcSMark Brown 			continue;
9043efab7dcSMark Brown 
905bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
906f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
907db2a4165SFrank Mandarino 	}
908db2a4165SFrank Mandarino 
90987506549SMark Brown 	if (card->resume_post)
91070b2ac12SMark Brown 		card->resume_post(card);
911db2a4165SFrank Mandarino 
912f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
9136ed25978SAndy Green 
9148be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
9158be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
916e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
9171a7aaa58SJeeja KP 
9181a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
9191a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
9206ed25978SAndy Green }
9216ed25978SAndy Green 
9226ed25978SAndy Green /* powers up audio subsystem after a suspend */
9236f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
9246ed25978SAndy Green {
9256f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
926bc263214SLars-Peter Clausen 	bool bus_control = false;
9271a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
928b9dd94a8SPeter Ujfalusi 
929c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
930c5599b87SLars-Peter Clausen 	if (!card->instantiated)
9315ff1ddf2SEric Miao 		return 0;
9325ff1ddf2SEric Miao 
933988e8cc4SNicolin Chen 	/* activate pins from sleep state */
9341a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
93588bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
93688bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
93788bd870fSBenoit Cousson 		int j;
93888bd870fSBenoit Cousson 
939988e8cc4SNicolin Chen 		if (cpu_dai->active)
940988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
94188bd870fSBenoit Cousson 
94288bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
94388bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
944988e8cc4SNicolin Chen 			if (codec_dai->active)
945988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
946988e8cc4SNicolin Chen 		}
94788bd870fSBenoit Cousson 	}
948988e8cc4SNicolin Chen 
949bc263214SLars-Peter Clausen 	/*
950bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
951bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
952bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
95364ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
95464ab9baaSMark Brown 	 */
9551a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
9561a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
957bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
95882e14e8bSStephen Warren 	}
959bc263214SLars-Peter Clausen 	if (bus_control) {
960bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
96164ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
96264ab9baaSMark Brown 	} else {
963f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
9646308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
965f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
966f0fba2adSLiam Girdwood 	}
9676ed25978SAndy Green 
968db2a4165SFrank Mandarino 	return 0;
969db2a4165SFrank Mandarino }
9706f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
971db2a4165SFrank Mandarino #else
9726f8ab4acSMark Brown #define snd_soc_suspend NULL
9736f8ab4acSMark Brown #define snd_soc_resume NULL
974db2a4165SFrank Mandarino #endif
975db2a4165SFrank Mandarino 
97685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
97702a06d30SBarry Song };
97802a06d30SBarry Song 
97965d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
98065d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
98112023a9aSMisael Lopez Cruz {
98265d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
98312023a9aSMisael Lopez Cruz 
98434e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
98534e81ab4SLars-Peter Clausen 
98665d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
98765d9361fSLars-Peter Clausen 		if (of_node) {
98865d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
98965d9361fSLars-Peter Clausen 				return component;
99065d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
99165d9361fSLars-Peter Clausen 			return component;
99212023a9aSMisael Lopez Cruz 		}
99312023a9aSMisael Lopez Cruz 	}
99412023a9aSMisael Lopez Cruz 
99512023a9aSMisael Lopez Cruz 	return NULL;
99612023a9aSMisael Lopez Cruz }
99712023a9aSMisael Lopez Cruz 
998fbb88b5cSMengdong Lin /**
999fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
1000fbb88b5cSMengdong Lin  *
10014958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
1002fbb88b5cSMengdong Lin  *
1003ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
1004fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
1005fbb88b5cSMengdong Lin  * should also match if being specified.
1006fbb88b5cSMengdong Lin  *
1007fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
1008fbb88b5cSMengdong Lin  */
1009305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
101014621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
101112023a9aSMisael Lopez Cruz {
101214621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
101314621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
10143e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
101512023a9aSMisael Lopez Cruz 
101634e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
101734e81ab4SLars-Peter Clausen 
101814621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
101914621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
10203e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
10213e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
10223e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
10233e0aa8d8SJyri Sarha 
10243e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
102512023a9aSMisael Lopez Cruz 			continue;
10261ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
102712023a9aSMisael Lopez Cruz 			continue;
102814621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
10294958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
10306a6dafdaSJeffy Chen 			    && (!dai->driver->name
10316a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
103214621c7eSLars-Peter Clausen 				continue;
103312023a9aSMisael Lopez Cruz 
103414621c7eSLars-Peter Clausen 			return dai;
103512023a9aSMisael Lopez Cruz 		}
103612023a9aSMisael Lopez Cruz 	}
103712023a9aSMisael Lopez Cruz 
103812023a9aSMisael Lopez Cruz 	return NULL;
103912023a9aSMisael Lopez Cruz }
1040305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
104112023a9aSMisael Lopez Cruz 
104217fb1755SMengdong Lin 
104317fb1755SMengdong Lin /**
104417fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
104517fb1755SMengdong Lin  *
104617fb1755SMengdong Lin  * @card: soc card
104717fb1755SMengdong Lin  * @id: DAI link ID to match
104817fb1755SMengdong Lin  * @name: DAI link name to match, optional
10498abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
105017fb1755SMengdong Lin  *
105117fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
105217fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
105317fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
105417fb1755SMengdong Lin  * specified.
105517fb1755SMengdong Lin  *
105617fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
105717fb1755SMengdong Lin  */
105817fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
105917fb1755SMengdong Lin 					       int id, const char *name,
106017fb1755SMengdong Lin 					       const char *stream_name)
106117fb1755SMengdong Lin {
106217fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
106317fb1755SMengdong Lin 
106417fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
106517fb1755SMengdong Lin 
106617fb1755SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
106717fb1755SMengdong Lin 		if (link->id != id)
106817fb1755SMengdong Lin 			continue;
106917fb1755SMengdong Lin 
107017fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
107117fb1755SMengdong Lin 			continue;
107217fb1755SMengdong Lin 
107317fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
107417fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
107517fb1755SMengdong Lin 			continue;
107617fb1755SMengdong Lin 
107717fb1755SMengdong Lin 		return link;
107817fb1755SMengdong Lin 	}
107917fb1755SMengdong Lin 
108017fb1755SMengdong Lin 	return NULL;
108117fb1755SMengdong Lin }
108217fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
108317fb1755SMengdong Lin 
108449a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
108549a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
1086db2a4165SFrank Mandarino {
108749a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
108849a5ba1cSMengdong Lin 
108949a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
109049a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
109149a5ba1cSMengdong Lin 			return true;
109249a5ba1cSMengdong Lin 	}
109349a5ba1cSMengdong Lin 
109449a5ba1cSMengdong Lin 	return false;
109549a5ba1cSMengdong Lin }
109649a5ba1cSMengdong Lin 
10976f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10986f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1099db2a4165SFrank Mandarino {
11001a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
110188bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
110214621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
110390be711eSKuninori Morimoto 	struct snd_soc_component *component;
11041a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
1105435c5e25SMark Brown 	struct snd_soc_platform *platform;
110606859fcaSCharles Keepax 	struct device_node *platform_of_node;
1107848dd8beSMark Brown 	const char *platform_name;
110888bd870fSBenoit Cousson 	int i;
1109db2a4165SFrank Mandarino 
11106f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
11116308419aSMark Brown 
111249a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
111349a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
111449a5ba1cSMengdong Lin 			dai_link->name);
111549a5ba1cSMengdong Lin 		return 0;
111649a5ba1cSMengdong Lin 	}
1117db2a4165SFrank Mandarino 
1118513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1119513cb311SSudip Mukherjee 	if (!rtd)
1120513cb311SSudip Mukherjee 		return -ENOMEM;
1121513cb311SSudip Mukherjee 
112214621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
112314621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
112414621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
112514621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1126b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1127f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1128f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
11291a497983SMengdong Lin 		goto _err_defer;
1130c5af3a2eSMark Brown 	}
113190be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1132c5af3a2eSMark Brown 
113388bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
113488bd870fSBenoit Cousson 
113588bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
11361a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
113788bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
113814621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
113988bd870fSBenoit Cousson 		if (!codec_dais[i]) {
114012023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
114188bd870fSBenoit Cousson 				codecs[i].dai_name);
11421a497983SMengdong Lin 			goto _err_defer;
114312023a9aSMisael Lopez Cruz 		}
114490be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
114588bd870fSBenoit Cousson 	}
114688bd870fSBenoit Cousson 
114788bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
114888bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
114988bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
115012023a9aSMisael Lopez Cruz 
1151848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1152848dd8beSMark Brown 	platform_name = dai_link->platform_name;
11535a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1154848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1155848dd8beSMark Brown 
1156b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
115790be711eSKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
115890be711eSKuninori Morimoto 		platform_of_node = component->dev->of_node;
115990be711eSKuninori Morimoto 		if (!platform_of_node && component->dev->parent->of_node)
116090be711eSKuninori Morimoto 			platform_of_node = component->dev->parent->of_node;
116190be711eSKuninori Morimoto 
116290be711eSKuninori Morimoto 		if (dai_link->platform_of_node) {
116390be711eSKuninori Morimoto 			if (platform_of_node != dai_link->platform_of_node)
116490be711eSKuninori Morimoto 				continue;
116590be711eSKuninori Morimoto 		} else {
116690be711eSKuninori Morimoto 			if (strcmp(component->name, platform_name))
116790be711eSKuninori Morimoto 				continue;
116890be711eSKuninori Morimoto 		}
116990be711eSKuninori Morimoto 
117090be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, component);
117190be711eSKuninori Morimoto 	}
117290be711eSKuninori Morimoto 
117390be711eSKuninori Morimoto 	/* find one from the set of registered platforms */
1174f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
117506859fcaSCharles Keepax 		platform_of_node = platform->dev->of_node;
117606859fcaSCharles Keepax 		if (!platform_of_node && platform->dev->parent->of_node)
117706859fcaSCharles Keepax 			platform_of_node = platform->dev->parent->of_node;
117806859fcaSCharles Keepax 
11795a504963SStephen Warren 		if (dai_link->platform_of_node) {
118006859fcaSCharles Keepax 			if (platform_of_node != dai_link->platform_of_node)
11815a504963SStephen Warren 				continue;
11825a504963SStephen Warren 		} else {
1183f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
11842610ab77SStephen Warren 				continue;
11855a504963SStephen Warren 		}
11862610ab77SStephen Warren 
1187f0fba2adSLiam Girdwood 		rtd->platform = platform;
1188f0fba2adSLiam Girdwood 	}
1189b19e6e7bSMark Brown 	if (!rtd->platform) {
1190f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1191f0fba2adSLiam Girdwood 			dai_link->platform_name);
119270fcad49SCharles Keepax 		goto _err_defer;
1193f0fba2adSLiam Girdwood 	}
1194b19e6e7bSMark Brown 
11951a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1196b19e6e7bSMark Brown 	return 0;
11971a497983SMengdong Lin 
11981a497983SMengdong Lin _err_defer:
11991a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
12001a497983SMengdong Lin 	return  -EPROBE_DEFER;
12016b05eda6SMark Brown }
12026b05eda6SMark Brown 
1203f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1204d12cd198SStephen Warren {
1205abd31b32SLars-Peter Clausen 	if (!component->card)
120670090bbbSLars-Peter Clausen 		return;
1207d12cd198SStephen Warren 
1208d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1209d12cd198SStephen Warren 
1210f1d45cc3SLars-Peter Clausen 	if (component->remove)
1211f1d45cc3SLars-Peter Clausen 		component->remove(component);
1212d12cd198SStephen Warren 
1213f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1214d12cd198SStephen Warren 
1215f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1216abd31b32SLars-Peter Clausen 	component->card = NULL;
1217f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1218d12cd198SStephen Warren }
1219d12cd198SStephen Warren 
1220e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1221589c3563SJarkko Nikula {
1222589c3563SJarkko Nikula 	int err;
1223589c3563SJarkko Nikula 
1224e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1225e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1226e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1227e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1228589c3563SJarkko Nikula 			if (err < 0)
1229e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1230b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1231e60cd14fSLars-Peter Clausen 					dai->name, err);
1232b0aa88afSMisael Lopez Cruz 		}
1233e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1234b0aa88afSMisael Lopez Cruz 	}
1235b0aa88afSMisael Lopez Cruz }
1236b0aa88afSMisael Lopez Cruz 
12371a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
12381a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1239f0fba2adSLiam Girdwood {
1240e60cd14fSLars-Peter Clausen 	int i;
1241f0fba2adSLiam Girdwood 
1242f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1243f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
124436ae1a96SMark Brown 		device_unregister(rtd->dev);
1245f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
124602a06d30SBarry Song 	}
124702a06d30SBarry Song 
1248f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
124988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1250e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1251f0fba2adSLiam Girdwood 
1252e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1253f0fba2adSLiam Girdwood }
1254f0fba2adSLiam Girdwood 
12551a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
12561a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
125762ae68faSStephen Warren {
125861aca564SLars-Peter Clausen 	struct snd_soc_component *component;
125990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
126062ae68faSStephen Warren 
126190be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
126290be711eSKuninori Morimoto 		component = rtdcom->component;
126362ae68faSStephen Warren 
126470090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
126561aca564SLars-Peter Clausen 			soc_remove_component(component);
126662ae68faSStephen Warren 	}
126762ae68faSStephen Warren }
126862ae68faSStephen Warren 
12690671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
12700671fd8eSKuninori Morimoto {
12711a497983SMengdong Lin 	int order;
12721a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1273f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
12740671fd8eSKuninori Morimoto 
12750168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
12760168bf0dSLiam Girdwood 			order++) {
12771a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12781a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
12790168bf0dSLiam Girdwood 	}
128062ae68faSStephen Warren 
128162ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
128262ae68faSStephen Warren 			order++) {
12831a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12841a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
128562ae68faSStephen Warren 	}
128662ae68faSStephen Warren 
1287f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1288f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1289f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1290f8f80361SMengdong Lin 				link->name);
1291f8f80361SMengdong Lin 
1292f8f80361SMengdong Lin 		list_del(&link->list);
1293f8f80361SMengdong Lin 		card->num_dai_links--;
12940671fd8eSKuninori Morimoto 	}
12950671fd8eSKuninori Morimoto }
12960671fd8eSKuninori Morimoto 
1297923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1298923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1299923c5e61SMengdong Lin {
1300923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1301923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1302923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1303923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1304923c5e61SMengdong Lin 
1305923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1306923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1307923c5e61SMengdong Lin 				GFP_KERNEL);
1308923c5e61SMengdong Lin 		if (!dai_link->codecs)
1309923c5e61SMengdong Lin 			return -ENOMEM;
1310923c5e61SMengdong Lin 
1311923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1312923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1313923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1314923c5e61SMengdong Lin 	}
1315923c5e61SMengdong Lin 
1316923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1317923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1318923c5e61SMengdong Lin 		return -EINVAL;
1319923c5e61SMengdong Lin 	}
1320923c5e61SMengdong Lin 
1321923c5e61SMengdong Lin 	return 0;
1322923c5e61SMengdong Lin }
1323923c5e61SMengdong Lin 
1324923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1325923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1326923c5e61SMengdong Lin {
1327923c5e61SMengdong Lin 	int i, ret;
1328923c5e61SMengdong Lin 
1329923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1330923c5e61SMengdong Lin 	if (ret) {
1331923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1332923c5e61SMengdong Lin 		return ret;
1333923c5e61SMengdong Lin 	}
1334923c5e61SMengdong Lin 
1335923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1336923c5e61SMengdong Lin 		/*
1337923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1338923c5e61SMengdong Lin 		 * not both or neither.
1339923c5e61SMengdong Lin 		 */
1340923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1341923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1342923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1343923c5e61SMengdong Lin 				link->name);
1344923c5e61SMengdong Lin 			return -EINVAL;
1345923c5e61SMengdong Lin 		}
1346923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1347923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1348923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1349923c5e61SMengdong Lin 				link->name);
1350923c5e61SMengdong Lin 			return -EINVAL;
1351923c5e61SMengdong Lin 		}
1352923c5e61SMengdong Lin 	}
1353923c5e61SMengdong Lin 
1354923c5e61SMengdong Lin 	/*
1355923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1356923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1357923c5e61SMengdong Lin 	 */
1358923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1359923c5e61SMengdong Lin 		dev_err(card->dev,
1360923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1361923c5e61SMengdong Lin 			link->name);
1362923c5e61SMengdong Lin 		return -EINVAL;
1363923c5e61SMengdong Lin 	}
1364923c5e61SMengdong Lin 
1365923c5e61SMengdong Lin 	/*
1366923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1367923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1368923c5e61SMengdong Lin 	 * name alone..
1369923c5e61SMengdong Lin 	 */
1370923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1371923c5e61SMengdong Lin 		dev_err(card->dev,
1372923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1373923c5e61SMengdong Lin 			link->name);
1374923c5e61SMengdong Lin 		return -EINVAL;
1375923c5e61SMengdong Lin 	}
1376923c5e61SMengdong Lin 	/*
1377923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1378923c5e61SMengdong Lin 	 * specified
1379923c5e61SMengdong Lin 	 */
1380923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1381923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1382923c5e61SMengdong Lin 		dev_err(card->dev,
1383923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1384923c5e61SMengdong Lin 			link->name);
1385923c5e61SMengdong Lin 		return -EINVAL;
1386923c5e61SMengdong Lin 	}
1387923c5e61SMengdong Lin 
1388923c5e61SMengdong Lin 	return 0;
1389923c5e61SMengdong Lin }
1390923c5e61SMengdong Lin 
1391f8f80361SMengdong Lin /**
1392f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1393f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1394f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1395f8f80361SMengdong Lin  *
1396f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1397f8f80361SMengdong Lin  *
1398f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1399f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1400f8f80361SMengdong Lin  * DAI links in dai_link array.
1401f8f80361SMengdong Lin  */
1402f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1403f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1404f8f80361SMengdong Lin {
1405f8f80361SMengdong Lin 	if (dai_link->dobj.type
1406f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1407f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1408f8f80361SMengdong Lin 			dai_link->dobj.type);
1409f8f80361SMengdong Lin 		return -EINVAL;
1410f8f80361SMengdong Lin 	}
1411f8f80361SMengdong Lin 
1412f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1413d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1414d6f220eaSMengdong Lin 	 * on the link created by topology.
1415d6f220eaSMengdong Lin 	 */
1416d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1417d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1418d6f220eaSMengdong Lin 
1419f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1420f8f80361SMengdong Lin 	card->num_dai_links++;
1421f8f80361SMengdong Lin 
1422f8f80361SMengdong Lin 	return 0;
1423f8f80361SMengdong Lin }
1424f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1425f8f80361SMengdong Lin 
1426f8f80361SMengdong Lin /**
1427f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1428f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1429f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1430f8f80361SMengdong Lin  *
1431f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1432f8f80361SMengdong Lin  *
1433f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1434f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1435f8f80361SMengdong Lin  */
1436f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1437f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1438f8f80361SMengdong Lin {
1439f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1440f8f80361SMengdong Lin 
1441f8f80361SMengdong Lin 	if (dai_link->dobj.type
1442f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1443f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1444f8f80361SMengdong Lin 			dai_link->dobj.type);
1445f8f80361SMengdong Lin 		return;
1446f8f80361SMengdong Lin 	}
1447f8f80361SMengdong Lin 
1448f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1449d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1450d6f220eaSMengdong Lin 	 * on the link created by topology.
1451d6f220eaSMengdong Lin 	 */
1452d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1453d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1454d6f220eaSMengdong Lin 
1455f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1456f8f80361SMengdong Lin 		if (link == dai_link) {
1457f8f80361SMengdong Lin 			list_del(&link->list);
1458f8f80361SMengdong Lin 			card->num_dai_links--;
1459f8f80361SMengdong Lin 			return;
1460f8f80361SMengdong Lin 		}
1461f8f80361SMengdong Lin 	}
1462f8f80361SMengdong Lin }
1463f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1464f0fba2adSLiam Girdwood 
1465ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
146694f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1467ead9b919SJarkko Nikula {
1468ead9b919SJarkko Nikula 	int i;
1469ead9b919SJarkko Nikula 
1470ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1471ead9b919SJarkko Nikula 		return;
1472ead9b919SJarkko Nikula 
1473ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1474ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
147594f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
14763ca041edSSebastian Reichel 			continue;
147794f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
14783ca041edSSebastian Reichel 			continue;
147994f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1480ead9b919SJarkko Nikula 		break;
1481ead9b919SJarkko Nikula 	}
1482ead9b919SJarkko Nikula }
1483ead9b919SJarkko Nikula 
1484f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1485f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1486589c3563SJarkko Nikula {
1487f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1488888df395SMark Brown 	struct snd_soc_dai *dai;
1489f1d45cc3SLars-Peter Clausen 	int ret;
1490589c3563SJarkko Nikula 
14911b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
149270090bbbSLars-Peter Clausen 		return 0;
1493589c3563SJarkko Nikula 
1494abd31b32SLars-Peter Clausen 	if (component->card) {
14951b7c1231SLars-Peter Clausen 		if (component->card != card) {
14961b7c1231SLars-Peter Clausen 			dev_err(component->dev,
14971b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
14981b7c1231SLars-Peter Clausen 				card->name, component->card->name);
14991b7c1231SLars-Peter Clausen 			return -ENODEV;
15001b7c1231SLars-Peter Clausen 		}
15011b7c1231SLars-Peter Clausen 		return 0;
15021b7c1231SLars-Peter Clausen 	}
15031b7c1231SLars-Peter Clausen 
1504abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1505abd31b32SLars-Peter Clausen 		return -ENODEV;
1506abd31b32SLars-Peter Clausen 
1507f1d45cc3SLars-Peter Clausen 	component->card = card;
1508f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1509f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1510589c3563SJarkko Nikula 
1511f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1512d5d1e0beSLars-Peter Clausen 
1513688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1514688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1515688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1516688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
151777530150SLars-Peter Clausen 
1518b318ad50SNariman Poushin 		if (ret != 0) {
1519f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1520b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1521b318ad50SNariman Poushin 			goto err_probe;
1522b318ad50SNariman Poushin 		}
1523b318ad50SNariman Poushin 	}
1524b318ad50SNariman Poushin 
15250634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
15260634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1527261edc70SNariman Poushin 		if (ret != 0) {
1528f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1529261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1530261edc70SNariman Poushin 			goto err_probe;
1531261edc70SNariman Poushin 		}
1532261edc70SNariman Poushin 	}
1533888df395SMark Brown 
1534f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1535f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1536589c3563SJarkko Nikula 		if (ret < 0) {
1537f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1538f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
153970d29331SJarkko Nikula 			goto err_probe;
1540589c3563SJarkko Nikula 		}
1541cb2cf612SLiam Girdwood 
1542f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1543f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1544956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1545f1d45cc3SLars-Peter Clausen 			component->name);
1546956245e9SLiam Girdwood 	}
1547956245e9SLiam Girdwood 
1548f2ed6b07SMengdong Lin 	/* machine specific init */
1549f2ed6b07SMengdong Lin 	if (component->init) {
1550f2ed6b07SMengdong Lin 		ret = component->init(component);
1551f2ed6b07SMengdong Lin 		if (ret < 0) {
1552f2ed6b07SMengdong Lin 			dev_err(component->dev,
1553f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1554f2ed6b07SMengdong Lin 			goto err_probe;
1555f2ed6b07SMengdong Lin 		}
1556f2ed6b07SMengdong Lin 	}
1557f2ed6b07SMengdong Lin 
1558b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1559b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1560b8972bf0SKuninori Morimoto 					       component->driver->controls,
1561b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
15626969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
15636969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
15646969b2baSKuninori Morimoto 					component->driver->dapm_routes,
15656969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
1566956245e9SLiam Girdwood 
1567f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1568d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1569956245e9SLiam Girdwood 
1570956245e9SLiam Girdwood 	return 0;
1571956245e9SLiam Girdwood 
1572956245e9SLiam Girdwood err_probe:
1573f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1574abd31b32SLars-Peter Clausen 	component->card = NULL;
1575f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1576956245e9SLiam Girdwood 
1577956245e9SLiam Girdwood 	return ret;
1578956245e9SLiam Girdwood }
1579956245e9SLiam Girdwood 
158036ae1a96SMark Brown static void rtd_release(struct device *dev)
158136ae1a96SMark Brown {
158236ae1a96SMark Brown 	kfree(dev);
158336ae1a96SMark Brown }
1584f0fba2adSLiam Girdwood 
15855f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
15865f3484acSLars-Peter Clausen 	const char *name)
1587503ae5e0SMisael Lopez Cruz {
1588589c3563SJarkko Nikula 	int ret = 0;
1589589c3563SJarkko Nikula 
1590589c3563SJarkko Nikula 	/* register the rtd device */
159136ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
159236ae1a96SMark Brown 	if (!rtd->dev)
159336ae1a96SMark Brown 		return -ENOMEM;
159436ae1a96SMark Brown 	device_initialize(rtd->dev);
15955f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
159636ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1597d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1598f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
159936ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1600b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
160101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
160201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
160301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
160401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
160536ae1a96SMark Brown 	ret = device_add(rtd->dev);
1606589c3563SJarkko Nikula 	if (ret < 0) {
1607865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1608865df9cbSChuansheng Liu 		put_device(rtd->dev);
16095f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1610f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1611589c3563SJarkko Nikula 		return ret;
1612589c3563SJarkko Nikula 	}
1613589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1614589c3563SJarkko Nikula 	return 0;
1615589c3563SJarkko Nikula }
1616589c3563SJarkko Nikula 
16171a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
16181a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
161962ae68faSStephen Warren 				     int order)
162062ae68faSStephen Warren {
1621f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
162290be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
162390be711eSKuninori Morimoto 	int ret;
162462ae68faSStephen Warren 
162590be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
162690be711eSKuninori Morimoto 		component = rtdcom->component;
162790be711eSKuninori Morimoto 
162870090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1629f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
163062ae68faSStephen Warren 			if (ret < 0)
163162ae68faSStephen Warren 				return ret;
163262ae68faSStephen Warren 		}
163362ae68faSStephen Warren 	}
163462ae68faSStephen Warren 
163562ae68faSStephen Warren 	return 0;
163662ae68faSStephen Warren }
163762ae68faSStephen Warren 
16388e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1639b0aa88afSMisael Lopez Cruz {
1640b0aa88afSMisael Lopez Cruz 	int ret;
1641b0aa88afSMisael Lopez Cruz 
16428e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
16438e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
16448e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1645b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
16468e2be562SLars-Peter Clausen 				dev_err(dai->dev,
16478e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
16488e2be562SLars-Peter Clausen 					dai->name, ret);
1649b0aa88afSMisael Lopez Cruz 				return ret;
1650b0aa88afSMisael Lopez Cruz 			}
1651b0aa88afSMisael Lopez Cruz 		}
1652b0aa88afSMisael Lopez Cruz 
16538e2be562SLars-Peter Clausen 		dai->probed = 1;
1654b0aa88afSMisael Lopez Cruz 	}
1655b0aa88afSMisael Lopez Cruz 
1656b0aa88afSMisael Lopez Cruz 	return 0;
1657b0aa88afSMisael Lopez Cruz }
1658b0aa88afSMisael Lopez Cruz 
165925f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
166025f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
166125f7b701SArnaud Pouliquen {
166225f7b701SArnaud Pouliquen 	int i, ret = 0;
166325f7b701SArnaud Pouliquen 
166425f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
166525f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
166625f7b701SArnaud Pouliquen 
166725f7b701SArnaud Pouliquen 		if (!rtd->dai_link->no_pcm && drv->pcm_new)
166825f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
166925f7b701SArnaud Pouliquen 		if (ret < 0) {
167025f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
167125f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
167225f7b701SArnaud Pouliquen 				dais[i]->name);
167325f7b701SArnaud Pouliquen 			return ret;
167425f7b701SArnaud Pouliquen 		}
167525f7b701SArnaud Pouliquen 	}
167625f7b701SArnaud Pouliquen 
167725f7b701SArnaud Pouliquen 	return 0;
167825f7b701SArnaud Pouliquen }
167925f7b701SArnaud Pouliquen 
16802436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
16812436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
16823f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
16832436a723SMisael Lopez Cruz {
16843f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16853f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
16863de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
16872436a723SMisael Lopez Cruz 	int ret;
16882436a723SMisael Lopez Cruz 
168988bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
169088bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
169188bd870fSBenoit Cousson 
16922436a723SMisael Lopez Cruz 	/* link the DAI widgets */
16933de7c420SVinod Koul 	sink = codec_dai->playback_widget;
16943de7c420SVinod Koul 	source = cpu_dai->capture_widget;
16953de7c420SVinod Koul 	if (sink && source) {
16962436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16973de7c420SVinod Koul 					   dai_link->num_params,
16983de7c420SVinod Koul 					   source, sink);
16992436a723SMisael Lopez Cruz 		if (ret != 0) {
17002436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
17013de7c420SVinod Koul 				sink->name, source->name, ret);
17022436a723SMisael Lopez Cruz 			return ret;
17032436a723SMisael Lopez Cruz 		}
17042436a723SMisael Lopez Cruz 	}
17052436a723SMisael Lopez Cruz 
17063de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
17073de7c420SVinod Koul 	source = codec_dai->capture_widget;
17083de7c420SVinod Koul 	if (sink && source) {
17092436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
17103de7c420SVinod Koul 					   dai_link->num_params,
17113de7c420SVinod Koul 					   source, sink);
17122436a723SMisael Lopez Cruz 		if (ret != 0) {
17132436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
17143de7c420SVinod Koul 				sink->name, source->name, ret);
17152436a723SMisael Lopez Cruz 			return ret;
17162436a723SMisael Lopez Cruz 		}
17172436a723SMisael Lopez Cruz 	}
17182436a723SMisael Lopez Cruz 
17192436a723SMisael Lopez Cruz 	return 0;
17202436a723SMisael Lopez Cruz }
17212436a723SMisael Lopez Cruz 
17221a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
17231a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1724f0fba2adSLiam Girdwood {
17251a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1726c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
172788bd870fSBenoit Cousson 	int i, ret;
1728f0fba2adSLiam Girdwood 
1729f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
17301a497983SMengdong Lin 			card->name, rtd->num, order);
1731f0fba2adSLiam Girdwood 
1732f0fba2adSLiam Girdwood 	/* set default power off timeout */
1733f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1734f0fba2adSLiam Girdwood 
17358e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
17368e2be562SLars-Peter Clausen 	if (ret)
1737f0fba2adSLiam Girdwood 		return ret;
1738f0fba2adSLiam Girdwood 
1739f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
174088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
17418e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1742b0aa88afSMisael Lopez Cruz 		if (ret)
1743f0fba2adSLiam Girdwood 			return ret;
174488bd870fSBenoit Cousson 	}
1745f0fba2adSLiam Girdwood 
17460168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
17470168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
17480168bf0dSLiam Girdwood 		return 0;
17490168bf0dSLiam Girdwood 
17505f3484acSLars-Peter Clausen 	/* do machine specific initialization */
17515f3484acSLars-Peter Clausen 	if (dai_link->init) {
17525f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
17535f3484acSLars-Peter Clausen 		if (ret < 0) {
17545f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
17555f3484acSLars-Peter Clausen 				dai_link->name, ret);
17565f3484acSLars-Peter Clausen 			return ret;
17575f3484acSLars-Peter Clausen 		}
17585f3484acSLars-Peter Clausen 	}
17595f3484acSLars-Peter Clausen 
1760a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1761a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1762a5053a8eSKuninori Morimoto 
17635f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1764589c3563SJarkko Nikula 	if (ret)
1765f0fba2adSLiam Girdwood 		return ret;
1766f0fba2adSLiam Girdwood 
17675f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
17685f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
17692e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
17702e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
17715f3484acSLars-Peter Clausen #endif
17725f3484acSLars-Peter Clausen 
17736f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
17741245b700SNamarta Kohli 		/*create compress_device"*/
17751a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
17761245b700SNamarta Kohli 		if (ret < 0) {
1777f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
17781245b700SNamarta Kohli 					 dai_link->stream_name);
17791245b700SNamarta Kohli 			return ret;
17801245b700SNamarta Kohli 		}
17811245b700SNamarta Kohli 	} else {
17821245b700SNamarta Kohli 
1783c74184edSMark Brown 		if (!dai_link->params) {
1784f0fba2adSLiam Girdwood 			/* create the pcm */
17851a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1786f0fba2adSLiam Girdwood 			if (ret < 0) {
1787f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
17880837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1789f0fba2adSLiam Girdwood 				return ret;
1790f0fba2adSLiam Girdwood 			}
179125f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
179225f7b701SArnaud Pouliquen 			if (ret < 0)
179325f7b701SArnaud Pouliquen 				return ret;
179425f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(rtd->codec_dais,
179525f7b701SArnaud Pouliquen 						   rtd->num_codecs, rtd);
179625f7b701SArnaud Pouliquen 			if (ret < 0)
179725f7b701SArnaud Pouliquen 				return ret;
1798c74184edSMark Brown 		} else {
17999d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
18009d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
18019d58a077SRichard Fitzgerald 
1802c74184edSMark Brown 			/* link the DAI widgets */
18033f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
18042436a723SMisael Lopez Cruz 			if (ret)
1805c74184edSMark Brown 				return ret;
1806c74184edSMark Brown 		}
1807c74184edSMark Brown 	}
1808c74184edSMark Brown 
1809f0fba2adSLiam Girdwood 	return 0;
1810f0fba2adSLiam Girdwood }
1811f0fba2adSLiam Girdwood 
181244c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1813b19e6e7bSMark Brown {
18143ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1815f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1816f2ed6b07SMengdong Lin 	const char *name;
1817f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
18183ca041edSSebastian Reichel 
1819f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1820f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1821f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1822f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1823f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1824f2ed6b07SMengdong Lin 		if (!component) {
1825f2ed6b07SMengdong Lin 			if (codec_of_node)
1826f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1827f2ed6b07SMengdong Lin 			goto err_defer;
1828f2ed6b07SMengdong Lin 		}
1829f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1830f2ed6b07SMengdong Lin 		/* generic components */
1831f2ed6b07SMengdong Lin 		name = aux_dev->name;
1832f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1833f2ed6b07SMengdong Lin 		if (!component)
1834f2ed6b07SMengdong Lin 			goto err_defer;
1835f2ed6b07SMengdong Lin 	} else {
1836f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1837f2ed6b07SMengdong Lin 		return -EINVAL;
1838f2ed6b07SMengdong Lin 	}
18393ca041edSSebastian Reichel 
1840f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1841d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
18421a653aa4SKuninori Morimoto 
1843f2ed6b07SMengdong Lin 	return 0;
1844f2ed6b07SMengdong Lin 
1845f2ed6b07SMengdong Lin err_defer:
184665d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1847b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1848b19e6e7bSMark Brown }
1849b19e6e7bSMark Brown 
1850f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1851f2ed6b07SMengdong Lin {
1852991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1853f2ed6b07SMengdong Lin 	int order;
1854f2ed6b07SMengdong Lin 	int ret;
1855f2ed6b07SMengdong Lin 
1856f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1857f2ed6b07SMengdong Lin 		order++) {
1858991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1859f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1860f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1861f2ed6b07SMengdong Lin 				if (ret < 0) {
1862f2ed6b07SMengdong Lin 					dev_err(card->dev,
1863f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1864f2ed6b07SMengdong Lin 						comp->name, ret);
1865f2ed6b07SMengdong Lin 					return ret;
1866f2ed6b07SMengdong Lin 				}
1867f2ed6b07SMengdong Lin 			}
1868f2ed6b07SMengdong Lin 		}
1869f2ed6b07SMengdong Lin 	}
187065d9361fSLars-Peter Clausen 
187144c69bb1SLars-Peter Clausen 	return 0;
18723ca041edSSebastian Reichel }
18732eea392dSJarkko Nikula 
1874f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
187544c69bb1SLars-Peter Clausen {
1876f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1877f2ed6b07SMengdong Lin 	int order;
187844c69bb1SLars-Peter Clausen 
1879f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1880f2ed6b07SMengdong Lin 		order++) {
1881f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1882991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
18831a653aa4SKuninori Morimoto 
1884f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1885f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1886991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1887991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
18882eea392dSJarkko Nikula 			}
18895f3484acSLars-Peter Clausen 		}
18902eea392dSJarkko Nikula 	}
18912eea392dSJarkko Nikula }
18922eea392dSJarkko Nikula 
1893f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1894fdf0f54dSDimitris Papastamos {
1895fdf0f54dSDimitris Papastamos 	int ret;
1896fdf0f54dSDimitris Papastamos 
1897fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1898fdf0f54dSDimitris Papastamos 		return 0;
1899fdf0f54dSDimitris Papastamos 
1900fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1901fdf0f54dSDimitris Papastamos 	if (ret < 0) {
190210e8aa9aSMichał Mirosław 		dev_err(codec->dev,
190310e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
190410e8aa9aSMichał Mirosław 			ret);
1905fdf0f54dSDimitris Papastamos 		return ret;
1906fdf0f54dSDimitris Papastamos 	}
1907fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1908fdf0f54dSDimitris Papastamos 	return 0;
1909fdf0f54dSDimitris Papastamos }
1910fdf0f54dSDimitris Papastamos 
1911ce64c8b9SLars-Peter Clausen /**
1912ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1913ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1914ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1915ce64c8b9SLars-Peter Clausen  *
1916ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1917ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1918ce64c8b9SLars-Peter Clausen  *
1919ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1920ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1921ce64c8b9SLars-Peter Clausen  *
1922ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1923ce64c8b9SLars-Peter Clausen  */
1924ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1925ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1926ce64c8b9SLars-Peter Clausen {
1927ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1928ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1929ce64c8b9SLars-Peter Clausen 	unsigned int i;
1930ce64c8b9SLars-Peter Clausen 	int ret;
1931ce64c8b9SLars-Peter Clausen 
1932ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1933ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1934ce64c8b9SLars-Peter Clausen 
1935ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1936ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1937ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1938ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1939ce64c8b9SLars-Peter Clausen 			return ret;
1940ce64c8b9SLars-Peter Clausen 		}
1941ce64c8b9SLars-Peter Clausen 	}
1942ce64c8b9SLars-Peter Clausen 
1943ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1944ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1945ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1946ce64c8b9SLars-Peter Clausen 
1947ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1948ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1949ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1950ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1951ce64c8b9SLars-Peter Clausen 			break;
1952ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1953ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1954ce64c8b9SLars-Peter Clausen 			break;
1955ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1956ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1957ce64c8b9SLars-Peter Clausen 			break;
1958ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1959ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1960ce64c8b9SLars-Peter Clausen 			break;
1961ce64c8b9SLars-Peter Clausen 		}
1962ce64c8b9SLars-Peter Clausen 
1963ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1964ce64c8b9SLars-Peter Clausen 	}
1965ce64c8b9SLars-Peter Clausen 
1966ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1967ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1968ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1969ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1970ce64c8b9SLars-Peter Clausen 		return ret;
1971ce64c8b9SLars-Peter Clausen 	}
1972ce64c8b9SLars-Peter Clausen 
1973ce64c8b9SLars-Peter Clausen 	return 0;
1974ce64c8b9SLars-Peter Clausen }
1975ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1976ce64c8b9SLars-Peter Clausen 
1977345233d7SLiam Girdwood 
19781f5a4535STakashi Iwai #ifdef CONFIG_DMI
1979345233d7SLiam Girdwood /* Trim special characters, and replace '-' with '_' since '-' is used to
1980345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1981345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1982345233d7SLiam Girdwood  */
1983345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1984345233d7SLiam Girdwood {
1985345233d7SLiam Girdwood 	int i, j = 0;
1986345233d7SLiam Girdwood 
1987345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1988345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1989345233d7SLiam Girdwood 		    || (name[i] == '_'))
1990345233d7SLiam Girdwood 			name[j++] = name[i];
1991345233d7SLiam Girdwood 		else if (name[i] == '-')
1992345233d7SLiam Girdwood 			name[j++] = '_';
1993345233d7SLiam Girdwood 	}
1994345233d7SLiam Girdwood 
1995345233d7SLiam Girdwood 	name[j] = '\0';
1996345233d7SLiam Girdwood }
1997345233d7SLiam Girdwood 
199898faf436SMengdong Lin /* Check if a DMI field is valid, i.e. not containing any string
199998faf436SMengdong Lin  * in the black list.
200098faf436SMengdong Lin  */
200198faf436SMengdong Lin static int is_dmi_valid(const char *field)
200298faf436SMengdong Lin {
200398faf436SMengdong Lin 	int i = 0;
200498faf436SMengdong Lin 
200598faf436SMengdong Lin 	while (dmi_blacklist[i]) {
200698faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
200798faf436SMengdong Lin 			return 0;
200898faf436SMengdong Lin 		i++;
200946b5a4d2SWu Fengguang 	}
201098faf436SMengdong Lin 
201198faf436SMengdong Lin 	return 1;
201298faf436SMengdong Lin }
201398faf436SMengdong Lin 
2014345233d7SLiam Girdwood /**
2015345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
2016345233d7SLiam Girdwood  * @card: The card to register DMI names
2017345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
2018345233d7SLiam Girdwood  *
2019345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
2020345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
2021345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
2022345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
2023345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
2024345233d7SLiam Girdwood  * as the sound card long name, in the format of
2025345233d7SLiam Girdwood  * "vendor-product-version-board"
2026345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
2027345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
2028345233d7SLiam Girdwood  * (UCM) configurations for the card.
2029345233d7SLiam Girdwood  *
2030345233d7SLiam Girdwood  * Possible card long names may be:
2031345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
2032345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2033345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2034345233d7SLiam Girdwood  *
2035345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
2036345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
2037345233d7SLiam Girdwood  *
2038345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
2039345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
2040345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
2041345233d7SLiam Girdwood  * charactors like SPACE.
2042345233d7SLiam Girdwood  *
2043345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
2044345233d7SLiam Girdwood  */
2045345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2046345233d7SLiam Girdwood {
2047345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
2048345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
2049345233d7SLiam Girdwood 	size_t len;
2050345233d7SLiam Girdwood 
2051345233d7SLiam Girdwood 	if (card->long_name)
2052345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
2053345233d7SLiam Girdwood 
2054345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
2055345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
205698faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
2057345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2058345233d7SLiam Girdwood 		return 0;
2059345233d7SLiam Girdwood 	}
2060345233d7SLiam Girdwood 
206198faf436SMengdong Lin 
2062345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2063345233d7SLiam Girdwood 			 "%s", vendor);
2064345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
2065345233d7SLiam Girdwood 
2066345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
206798faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
2068345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2069345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2070345233d7SLiam Girdwood 			 longname_buf_size - len,
2071345233d7SLiam Girdwood 			 "-%s", product);
2072345233d7SLiam Girdwood 
2073345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
2074345233d7SLiam Girdwood 		if (len < longname_buf_size)
2075345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2076345233d7SLiam Girdwood 
2077345233d7SLiam Girdwood 		/* some vendors like Lenovo may only put a self-explanatory
2078345233d7SLiam Girdwood 		 * name in the product version field
2079345233d7SLiam Girdwood 		 */
2080345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
208198faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
2082345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
2083345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
2084345233d7SLiam Girdwood 				 longname_buf_size - len,
2085345233d7SLiam Girdwood 				 "-%s", product_version);
2086345233d7SLiam Girdwood 
2087345233d7SLiam Girdwood 			len++;
2088345233d7SLiam Girdwood 			if (len < longname_buf_size)
2089345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
2090345233d7SLiam Girdwood 		}
2091345233d7SLiam Girdwood 	}
2092345233d7SLiam Girdwood 
2093345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
209498faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
2095345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2096345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2097345233d7SLiam Girdwood 			 longname_buf_size - len,
2098345233d7SLiam Girdwood 			 "-%s", board);
2099345233d7SLiam Girdwood 
2100345233d7SLiam Girdwood 		len++;
2101345233d7SLiam Girdwood 		if (len < longname_buf_size)
2102345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2103345233d7SLiam Girdwood 	} else if (!product) {
2104345233d7SLiam Girdwood 		/* fall back to using legacy name */
2105345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2106345233d7SLiam Girdwood 		return 0;
2107345233d7SLiam Girdwood 	}
2108345233d7SLiam Girdwood 
2109345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
2110345233d7SLiam Girdwood 	if (flavour) {
2111345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2112345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2113345233d7SLiam Girdwood 			 longname_buf_size - len,
2114345233d7SLiam Girdwood 			 "-%s", flavour);
2115345233d7SLiam Girdwood 
2116345233d7SLiam Girdwood 		len++;
2117345233d7SLiam Girdwood 		if (len < longname_buf_size)
2118345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2119345233d7SLiam Girdwood 	}
2120345233d7SLiam Girdwood 
2121345233d7SLiam Girdwood 	/* set the card long name */
2122345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
2123345233d7SLiam Girdwood 
2124345233d7SLiam Girdwood 	return 0;
2125345233d7SLiam Girdwood }
2126345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
21271f5a4535STakashi Iwai #endif /* CONFIG_DMI */
2128345233d7SLiam Girdwood 
2129b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2130f0fba2adSLiam Girdwood {
2131fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
21321a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
213361b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2134ce64c8b9SLars-Peter Clausen 	int ret, i, order;
213596dd3622SMark Brown 
213634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
213701b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2138f0fba2adSLiam Girdwood 
2139b19e6e7bSMark Brown 	/* bind DAIs */
2140b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
21416f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
2142b19e6e7bSMark Brown 		if (ret != 0)
2143b19e6e7bSMark Brown 			goto base_error;
2144db2a4165SFrank Mandarino 	}
2145db2a4165SFrank Mandarino 
214644c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2147b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
214844c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2149b19e6e7bSMark Brown 		if (ret != 0)
2150b19e6e7bSMark Brown 			goto base_error;
2151db2a4165SFrank Mandarino 	}
2152db2a4165SFrank Mandarino 
2153f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2154f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
2155f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
2156f8f80361SMengdong Lin 
2157fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
2158fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
2159fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
2160fdf0f54dSDimitris Papastamos 			continue;
2161f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
2162b19e6e7bSMark Brown 		if (ret < 0)
2163b19e6e7bSMark Brown 			goto base_error;
2164fdf0f54dSDimitris Papastamos 	}
2165fdf0f54dSDimitris Papastamos 
2166f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2167102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2168f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2169f0fba2adSLiam Girdwood 	if (ret < 0) {
217010e8aa9aSMichał Mirosław 		dev_err(card->dev,
217110e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
217210e8aa9aSMichał Mirosław 			card->name, ret);
2173b19e6e7bSMark Brown 		goto base_error;
2174db2a4165SFrank Mandarino 	}
2175db2a4165SFrank Mandarino 
21760757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
21770757d834SLars-Peter Clausen 
2178e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
2179e37a4970SMark Brown 	card->dapm.dev = card->dev;
2180e37a4970SMark Brown 	card->dapm.card = card;
2181e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
2182e37a4970SMark Brown 
2183d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2184d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2185d5d1e0beSLars-Peter Clausen #endif
2186d5d1e0beSLars-Peter Clausen 
218788ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
21886ed25978SAndy Green 	/* deferred resume work */
21896308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
21901301a964SRandy Dunlap #endif
21916ed25978SAndy Green 
21929a841ebbSMark Brown 	if (card->dapm_widgets)
21939a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
21949a841ebbSMark Brown 					  card->num_dapm_widgets);
21959a841ebbSMark Brown 
2196f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2197f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2198f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2199f23e860eSNicolin Chen 
2200f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2201f0fba2adSLiam Girdwood 	if (card->probe) {
2202e7361ec4SMark Brown 		ret = card->probe(card);
2203f0fba2adSLiam Girdwood 		if (ret < 0)
2204f0fba2adSLiam Girdwood 			goto card_probe_error;
2205f0fba2adSLiam Girdwood 	}
2206f0fba2adSLiam Girdwood 
220762ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
22080168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
22090168bf0dSLiam Girdwood 			order++) {
22101a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
22111a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
221262ae68faSStephen Warren 			if (ret < 0) {
2213f110bfc7SLiam Girdwood 				dev_err(card->dev,
2214f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2215f110bfc7SLiam Girdwood 					ret);
221662ae68faSStephen Warren 				goto probe_dai_err;
221762ae68faSStephen Warren 			}
221862ae68faSStephen Warren 		}
221962ae68faSStephen Warren 	}
222062ae68faSStephen Warren 
2221f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2222f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2223f2ed6b07SMengdong Lin 	if (ret < 0)
2224f2ed6b07SMengdong Lin 		goto probe_dai_err;
2225f2ed6b07SMengdong Lin 
222661b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
222761b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
222861b0088bSMengdong Lin 	 */
222961b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
223061b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
223161b0088bSMengdong Lin 			continue;
223261b0088bSMengdong Lin 
223361b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
223461b0088bSMengdong Lin 		if (ret)
223561b0088bSMengdong Lin 			goto probe_dai_err;
223661b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
223761b0088bSMengdong Lin 		if (ret)
223861b0088bSMengdong Lin 			goto probe_dai_err;
223961b0088bSMengdong Lin 	}
224061b0088bSMengdong Lin 
224162ae68faSStephen Warren 	/* probe all DAI links on this card */
224262ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
224362ae68faSStephen Warren 			order++) {
22441a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
22451a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2246fe3e78e0SMark Brown 			if (ret < 0) {
2247f110bfc7SLiam Girdwood 				dev_err(card->dev,
2248f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2249f110bfc7SLiam Girdwood 					ret);
2250f0fba2adSLiam Girdwood 				goto probe_dai_err;
2251fe3e78e0SMark Brown 			}
2252fe3e78e0SMark Brown 		}
22530168bf0dSLiam Girdwood 	}
2254fe3e78e0SMark Brown 
2255888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2256b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2257888df395SMark Brown 
2258b7af1dafSMark Brown 	if (card->controls)
2259022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
2260b7af1dafSMark Brown 
2261b8ad29deSMark Brown 	if (card->dapm_routes)
2262b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2263b8ad29deSMark Brown 					card->num_dapm_routes);
2264b8ad29deSMark Brown 
2265f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2266f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2267f23e860eSNicolin Chen 					card->num_of_dapm_routes);
226875d9ac46SMark Brown 
2269861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2270861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2271861886d3STakashi Iwai 
2272f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2273fe3e78e0SMark Brown 		 "%s", card->name);
2274f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
227522de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2276f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2277f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2278f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2279f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2280f0e8ed85SMark Brown 		case '_':
2281f0e8ed85SMark Brown 		case '-':
2282f0e8ed85SMark Brown 		case '\0':
2283f0e8ed85SMark Brown 			break;
2284f0e8ed85SMark Brown 		default:
2285f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2286f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2287f0e8ed85SMark Brown 			break;
2288f0e8ed85SMark Brown 		}
2289f0e8ed85SMark Brown 	}
2290fe3e78e0SMark Brown 
229128e9ad92SMark Brown 	if (card->late_probe) {
229228e9ad92SMark Brown 		ret = card->late_probe(card);
229328e9ad92SMark Brown 		if (ret < 0) {
2294f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
229528e9ad92SMark Brown 				card->name, ret);
229628e9ad92SMark Brown 			goto probe_aux_dev_err;
229728e9ad92SMark Brown 		}
229828e9ad92SMark Brown 	}
229928e9ad92SMark Brown 
2300824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
23018c193b8dSLars-Peter Clausen 
2302f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2303fe3e78e0SMark Brown 	if (ret < 0) {
2304f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2305f110bfc7SLiam Girdwood 				ret);
23066b3ed785SAxel Lin 		goto probe_aux_dev_err;
2307fe3e78e0SMark Brown 	}
2308fe3e78e0SMark Brown 
2309435c5e25SMark Brown 	card->instantiated = 1;
23104f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2311f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
231234e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2313b19e6e7bSMark Brown 
2314b19e6e7bSMark Brown 	return 0;
2315db2a4165SFrank Mandarino 
23162eea392dSJarkko Nikula probe_aux_dev_err:
2317f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
23182eea392dSJarkko Nikula 
2319f0fba2adSLiam Girdwood probe_dai_err:
23200671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2321fe3e78e0SMark Brown 
2322f0fba2adSLiam Girdwood card_probe_error:
232387506549SMark Brown 	if (card->remove)
2324e7361ec4SMark Brown 		card->remove(card);
2325f0fba2adSLiam Girdwood 
23262210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
23270757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2328f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2329f0fba2adSLiam Girdwood 
2330b19e6e7bSMark Brown base_error:
23311a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2332f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
233334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2334db2a4165SFrank Mandarino 
2335b19e6e7bSMark Brown 	return ret;
2336435c5e25SMark Brown }
2337435c5e25SMark Brown 
2338435c5e25SMark Brown /* probes a new socdev */
2339435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2340435c5e25SMark Brown {
2341f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2342435c5e25SMark Brown 
234370a7ca34SVinod Koul 	/*
234470a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
234570a7ca34SVinod Koul 	 * we should not be here in that case so ret error
234670a7ca34SVinod Koul 	 */
234770a7ca34SVinod Koul 	if (!card)
234870a7ca34SVinod Koul 		return -EINVAL;
234970a7ca34SVinod Koul 
2350fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2351f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2352fe4085e8SMark Brown 		 card->name);
2353fe4085e8SMark Brown 
2354435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2355435c5e25SMark Brown 	card->dev = &pdev->dev;
2356f0fba2adSLiam Girdwood 
235728d528c8SMark Brown 	return snd_soc_register_card(card);
2358435c5e25SMark Brown }
2359435c5e25SMark Brown 
2360b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2361db2a4165SFrank Mandarino {
23621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2363db2a4165SFrank Mandarino 
2364f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
23651a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
236643829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2367db2a4165SFrank Mandarino 
23684efda5f2STakashi Iwai 	/* free the ALSA card at first; this syncs with pending operations */
23694efda5f2STakashi Iwai 	snd_card_free(card->snd_card);
23704efda5f2STakashi Iwai 
2371f0fba2adSLiam Girdwood 	/* remove and free each DAI */
23720671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
23731a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2374f0fba2adSLiam Girdwood 
2375f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2376f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2377f2ed6b07SMengdong Lin 
2378d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2379a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2380a6052154SJarkko Nikula 
2381f0fba2adSLiam Girdwood 	/* remove the card */
238287506549SMark Brown 	if (card->remove)
2383e7361ec4SMark Brown 		card->remove(card);
2384f0fba2adSLiam Girdwood 
2385b0e26485SVinod Koul 	return 0;
2386b2dfa62cSGuennadi Liakhovetski }
2387b0e26485SVinod Koul 
2388b0e26485SVinod Koul /* removes a socdev */
2389b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2390b0e26485SVinod Koul {
2391b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2392b0e26485SVinod Koul 
2393c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2394db2a4165SFrank Mandarino 	return 0;
2395db2a4165SFrank Mandarino }
2396db2a4165SFrank Mandarino 
23976f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
239851737470SMark Brown {
23996f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
24001a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
240151737470SMark Brown 
240251737470SMark Brown 	if (!card->instantiated)
2403416356fcSMark Brown 		return 0;
240451737470SMark Brown 
240551737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
240651737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
24071a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
240843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
240951737470SMark Brown 
2410f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2411416356fcSMark Brown 
2412988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
24131a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
241488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
24151a497983SMengdong Lin 		int i;
241688bd870fSBenoit Cousson 
2417988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
24181a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
24191a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
242088bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
242188bd870fSBenoit Cousson 		}
2422988e8cc4SNicolin Chen 	}
2423988e8cc4SNicolin Chen 
2424416356fcSMark Brown 	return 0;
242551737470SMark Brown }
24266f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
242751737470SMark Brown 
24286f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2429b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2430b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2431b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2432b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
24336f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2434b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2435416356fcSMark Brown };
2436deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2437416356fcSMark Brown 
2438db2a4165SFrank Mandarino /* ASoC platform driver */
2439db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2440db2a4165SFrank Mandarino 	.driver		= {
2441db2a4165SFrank Mandarino 		.name		= "soc-audio",
24426f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2443db2a4165SFrank Mandarino 	},
2444db2a4165SFrank Mandarino 	.probe		= soc_probe,
2445db2a4165SFrank Mandarino 	.remove		= soc_remove,
2446db2a4165SFrank Mandarino };
2447db2a4165SFrank Mandarino 
2448096e49d5SMark Brown /**
2449db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2450db2a4165SFrank Mandarino  * @_template: control template
2451db2a4165SFrank Mandarino  * @data: control private data
2452ac11a2b3SMark Brown  * @long_name: control long name
2453efb7ac3fSMark Brown  * @prefix: control name prefix
2454db2a4165SFrank Mandarino  *
2455db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2456db2a4165SFrank Mandarino  *
2457db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2458db2a4165SFrank Mandarino  */
2459db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
24603056557fSMark Brown 				  void *data, const char *long_name,
2461efb7ac3fSMark Brown 				  const char *prefix)
2462db2a4165SFrank Mandarino {
2463db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2464efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2465efb7ac3fSMark Brown 	char *name = NULL;
2466db2a4165SFrank Mandarino 
2467db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2468db2a4165SFrank Mandarino 	template.index = 0;
2469db2a4165SFrank Mandarino 
2470efb7ac3fSMark Brown 	if (!long_name)
2471efb7ac3fSMark Brown 		long_name = template.name;
2472efb7ac3fSMark Brown 
2473efb7ac3fSMark Brown 	if (prefix) {
24742b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2475efb7ac3fSMark Brown 		if (!name)
2476efb7ac3fSMark Brown 			return NULL;
2477efb7ac3fSMark Brown 
2478efb7ac3fSMark Brown 		template.name = name;
2479efb7ac3fSMark Brown 	} else {
2480efb7ac3fSMark Brown 		template.name = long_name;
2481efb7ac3fSMark Brown 	}
2482efb7ac3fSMark Brown 
2483efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2484efb7ac3fSMark Brown 
2485efb7ac3fSMark Brown 	kfree(name);
2486efb7ac3fSMark Brown 
2487efb7ac3fSMark Brown 	return kcontrol;
2488db2a4165SFrank Mandarino }
2489db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2490db2a4165SFrank Mandarino 
2491022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2492022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2493022658beSLiam Girdwood 	const char *prefix, void *data)
2494022658beSLiam Girdwood {
2495022658beSLiam Girdwood 	int err, i;
2496022658beSLiam Girdwood 
2497022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2498022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2499022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2500022658beSLiam Girdwood 						     control->name, prefix));
2501022658beSLiam Girdwood 		if (err < 0) {
2502f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2503f110bfc7SLiam Girdwood 				control->name, err);
2504022658beSLiam Girdwood 			return err;
2505022658beSLiam Girdwood 		}
2506022658beSLiam Girdwood 	}
2507022658beSLiam Girdwood 
2508022658beSLiam Girdwood 	return 0;
2509022658beSLiam Girdwood }
2510022658beSLiam Girdwood 
25114fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
25124fefd698SDimitris Papastamos 					       const char *name)
25134fefd698SDimitris Papastamos {
25144fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
25154fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
25164fefd698SDimitris Papastamos 
25174fefd698SDimitris Papastamos 	if (unlikely(!name))
25184fefd698SDimitris Papastamos 		return NULL;
25194fefd698SDimitris Papastamos 
25204fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
25214fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
25224fefd698SDimitris Papastamos 			return kctl;
25234fefd698SDimitris Papastamos 	return NULL;
25244fefd698SDimitris Papastamos }
25254fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
25264fefd698SDimitris Papastamos 
2527db2a4165SFrank Mandarino /**
25280f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
25290f2780adSLars-Peter Clausen  *
25300f2780adSLars-Peter Clausen  * @component: Component to add controls to
25310f2780adSLars-Peter Clausen  * @controls: Array of controls to add
25320f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
25330f2780adSLars-Peter Clausen  *
25340f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
25350f2780adSLars-Peter Clausen  */
25360f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
25370f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
25380f2780adSLars-Peter Clausen {
25390f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
25400f2780adSLars-Peter Clausen 
25410f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
25420f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
25430f2780adSLars-Peter Clausen }
25440f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
25450f2780adSLars-Peter Clausen 
25460f2780adSLars-Peter Clausen /**
2547022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2548022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
25493e8e1952SIan Molton  * duplicating this code.
25503e8e1952SIan Molton  *
25513e8e1952SIan Molton  * @codec: codec to add controls to
25523e8e1952SIan Molton  * @controls: array of controls to add
25533e8e1952SIan Molton  * @num_controls: number of elements in the array
25543e8e1952SIan Molton  *
25553e8e1952SIan Molton  * Return 0 for success, else error.
25563e8e1952SIan Molton  */
2557022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
25580f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
25593e8e1952SIan Molton {
25600f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
25610f2780adSLars-Peter Clausen 		num_controls);
25623e8e1952SIan Molton }
2563022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
25643e8e1952SIan Molton 
25653e8e1952SIan Molton /**
2566a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2567022658beSLiam Girdwood  * Convenience function to add a list of controls.
2568a491a5c8SLiam Girdwood  *
2569a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2570a491a5c8SLiam Girdwood  * @controls: array of controls to add
2571a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2572a491a5c8SLiam Girdwood  *
2573a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2574a491a5c8SLiam Girdwood  */
2575a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
25760f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2577a491a5c8SLiam Girdwood {
25780f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
25790f2780adSLars-Peter Clausen 		num_controls);
2580a491a5c8SLiam Girdwood }
2581a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2582a491a5c8SLiam Girdwood 
2583a491a5c8SLiam Girdwood /**
2584022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2585022658beSLiam Girdwood  * Convenience function to add a list of controls.
2586022658beSLiam Girdwood  *
2587022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2588022658beSLiam Girdwood  * @controls: array of controls to add
2589022658beSLiam Girdwood  * @num_controls: number of elements in the array
2590022658beSLiam Girdwood  *
2591022658beSLiam Girdwood  * Return 0 for success, else error.
2592022658beSLiam Girdwood  */
2593022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2594022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2595022658beSLiam Girdwood {
2596022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2597022658beSLiam Girdwood 
2598022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2599022658beSLiam Girdwood 			NULL, soc_card);
2600022658beSLiam Girdwood }
2601022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2602022658beSLiam Girdwood 
2603022658beSLiam Girdwood /**
2604022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2605022658beSLiam Girdwood  * Convienience function to add a list of controls.
2606022658beSLiam Girdwood  *
2607022658beSLiam Girdwood  * @dai: DAI to add controls to
2608022658beSLiam Girdwood  * @controls: array of controls to add
2609022658beSLiam Girdwood  * @num_controls: number of elements in the array
2610022658beSLiam Girdwood  *
2611022658beSLiam Girdwood  * Return 0 for success, else error.
2612022658beSLiam Girdwood  */
2613022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2614022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2615022658beSLiam Girdwood {
2616313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2617022658beSLiam Girdwood 
2618022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2619022658beSLiam Girdwood 			NULL, dai);
2620022658beSLiam Girdwood }
2621022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2622022658beSLiam Girdwood 
2623022658beSLiam Girdwood /**
26248c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
26258c6529dbSLiam Girdwood  * @dai: DAI
26268c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
26278c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
26288c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
26298c6529dbSLiam Girdwood  *
26308c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
26318c6529dbSLiam Girdwood  */
26328c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
26338c6529dbSLiam Girdwood 	unsigned int freq, int dir)
26348c6529dbSLiam Girdwood {
2635f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2636f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
263771ccef0dSKuninori Morimoto 
263871ccef0dSKuninori Morimoto 	return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2639ec4ee52aSMark Brown 					    freq, dir);
26408c6529dbSLiam Girdwood }
26418c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
26428c6529dbSLiam Girdwood 
26438c6529dbSLiam Girdwood /**
2644ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2645ec4ee52aSMark Brown  * @codec: CODEC
2646ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2647da1c6ea6SMark Brown  * @source: Source for the clock
2648ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2649ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2650ec4ee52aSMark Brown  *
2651ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2652ec4ee52aSMark Brown  */
2653ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2654da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2655ec4ee52aSMark Brown {
2656ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2657da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2658da1c6ea6SMark Brown 						 freq, dir);
2659ec4ee52aSMark Brown 	else
26601104a9c8SMark Brown 		return -ENOTSUPP;
2661ec4ee52aSMark Brown }
2662ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2663ec4ee52aSMark Brown 
2664ec4ee52aSMark Brown /**
266571ccef0dSKuninori Morimoto  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
266671ccef0dSKuninori Morimoto  * @component: COMPONENT
266771ccef0dSKuninori Morimoto  * @clk_id: DAI specific clock ID
266871ccef0dSKuninori Morimoto  * @source: Source for the clock
266971ccef0dSKuninori Morimoto  * @freq: new clock frequency in Hz
267071ccef0dSKuninori Morimoto  * @dir: new clock direction - input/output.
267171ccef0dSKuninori Morimoto  *
267271ccef0dSKuninori Morimoto  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
267371ccef0dSKuninori Morimoto  */
267471ccef0dSKuninori Morimoto int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
267571ccef0dSKuninori Morimoto 			     int source, unsigned int freq, int dir)
267671ccef0dSKuninori Morimoto {
267771ccef0dSKuninori Morimoto 	/* will be removed */
267871ccef0dSKuninori Morimoto 	if (component->set_sysclk)
267971ccef0dSKuninori Morimoto 		return component->set_sysclk(component, clk_id, source,
268071ccef0dSKuninori Morimoto 					     freq, dir);
268171ccef0dSKuninori Morimoto 
268271ccef0dSKuninori Morimoto 	if (component->driver->set_sysclk)
268371ccef0dSKuninori Morimoto 		return component->driver->set_sysclk(component, clk_id, source,
268471ccef0dSKuninori Morimoto 						 freq, dir);
268571ccef0dSKuninori Morimoto 
268671ccef0dSKuninori Morimoto 	return -ENOTSUPP;
268771ccef0dSKuninori Morimoto }
268871ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
268971ccef0dSKuninori Morimoto 
269071ccef0dSKuninori Morimoto /**
26918c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
26928c6529dbSLiam Girdwood  * @dai: DAI
2693ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
26948c6529dbSLiam Girdwood  * @div: new clock divisor.
26958c6529dbSLiam Girdwood  *
26968c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
26978c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
26988c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
26998c6529dbSLiam Girdwood  */
27008c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
27018c6529dbSLiam Girdwood 	int div_id, int div)
27028c6529dbSLiam Girdwood {
2703f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2704f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
27058c6529dbSLiam Girdwood 	else
27068c6529dbSLiam Girdwood 		return -EINVAL;
27078c6529dbSLiam Girdwood }
27088c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
27098c6529dbSLiam Girdwood 
27108c6529dbSLiam Girdwood /**
27118c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
27128c6529dbSLiam Girdwood  * @dai: DAI
27138c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
271485488037SMark Brown  * @source: DAI specific source for the PLL
27158c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
27168c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
27178c6529dbSLiam Girdwood  *
27188c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
27198c6529dbSLiam Girdwood  */
272085488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
272185488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
27228c6529dbSLiam Girdwood {
2723f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2724f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
272585488037SMark Brown 					 freq_in, freq_out);
2726ef641e5dSKuninori Morimoto 
2727ef641e5dSKuninori Morimoto 	return snd_soc_component_set_pll(dai->component, pll_id, source,
2728ec4ee52aSMark Brown 					 freq_in, freq_out);
27298c6529dbSLiam Girdwood }
27308c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
27318c6529dbSLiam Girdwood 
2732ec4ee52aSMark Brown /*
2733ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2734ec4ee52aSMark Brown  * @codec: CODEC
2735ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2736ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2737ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2738ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2739ec4ee52aSMark Brown  *
2740ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2741ec4ee52aSMark Brown  */
2742ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2743ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2744ec4ee52aSMark Brown {
2745ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2746ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2747ec4ee52aSMark Brown 					      freq_in, freq_out);
2748ec4ee52aSMark Brown 	else
2749ec4ee52aSMark Brown 		return -EINVAL;
2750ec4ee52aSMark Brown }
2751ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2752ec4ee52aSMark Brown 
2753ef641e5dSKuninori Morimoto /*
2754ef641e5dSKuninori Morimoto  * snd_soc_component_set_pll - configure component PLL.
2755ef641e5dSKuninori Morimoto  * @component: COMPONENT
2756ef641e5dSKuninori Morimoto  * @pll_id: DAI specific PLL ID
2757ef641e5dSKuninori Morimoto  * @source: DAI specific source for the PLL
2758ef641e5dSKuninori Morimoto  * @freq_in: PLL input clock frequency in Hz
2759ef641e5dSKuninori Morimoto  * @freq_out: requested PLL output clock frequency in Hz
2760ef641e5dSKuninori Morimoto  *
2761ef641e5dSKuninori Morimoto  * Configures and enables PLL to generate output clock based on input clock.
2762ef641e5dSKuninori Morimoto  */
2763ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2764ef641e5dSKuninori Morimoto 			      int source, unsigned int freq_in,
2765ef641e5dSKuninori Morimoto 			      unsigned int freq_out)
2766ef641e5dSKuninori Morimoto {
2767ef641e5dSKuninori Morimoto 	/* will be removed */
2768ef641e5dSKuninori Morimoto 	if (component->set_pll)
2769ef641e5dSKuninori Morimoto 		return component->set_pll(component, pll_id, source,
2770ef641e5dSKuninori Morimoto 					      freq_in, freq_out);
2771ef641e5dSKuninori Morimoto 
2772ef641e5dSKuninori Morimoto 	if (component->driver->set_pll)
2773ef641e5dSKuninori Morimoto 		return component->driver->set_pll(component, pll_id, source,
2774ef641e5dSKuninori Morimoto 					      freq_in, freq_out);
2775ef641e5dSKuninori Morimoto 
2776ef641e5dSKuninori Morimoto 	return -EINVAL;
2777ef641e5dSKuninori Morimoto }
2778ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2779ef641e5dSKuninori Morimoto 
27808c6529dbSLiam Girdwood /**
2781e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2782e54cf76bSLiam Girdwood  * @dai: DAI
2783231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2784e54cf76bSLiam Girdwood  *
2785e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2786e54cf76bSLiam Girdwood  */
2787e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2788e54cf76bSLiam Girdwood {
2789e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2790e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2791e54cf76bSLiam Girdwood 	else
2792e54cf76bSLiam Girdwood 		return -EINVAL;
2793e54cf76bSLiam Girdwood }
2794e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2795e54cf76bSLiam Girdwood 
2796e54cf76bSLiam Girdwood /**
27978c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
27988c6529dbSLiam Girdwood  * @dai: DAI
27998c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
28008c6529dbSLiam Girdwood  *
28018c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
28028c6529dbSLiam Girdwood  */
28038c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
28048c6529dbSLiam Girdwood {
28055e4ba569SShawn Guo 	if (dai->driver == NULL)
28068c6529dbSLiam Girdwood 		return -EINVAL;
28075e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
28085e4ba569SShawn Guo 		return -ENOTSUPP;
28095e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
28108c6529dbSLiam Girdwood }
28118c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
28128c6529dbSLiam Girdwood 
28138c6529dbSLiam Girdwood /**
2814e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
281589c67857SXiubo Li  * @slots: Number of slots in use.
281689c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
281789c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
281889c67857SXiubo Li  *
281989c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
282089c67857SXiubo Li  */
2821e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
282289c67857SXiubo Li 					  unsigned int *tx_mask,
282389c67857SXiubo Li 					  unsigned int *rx_mask)
282489c67857SXiubo Li {
282589c67857SXiubo Li 	if (*tx_mask || *rx_mask)
282689c67857SXiubo Li 		return 0;
282789c67857SXiubo Li 
282889c67857SXiubo Li 	if (!slots)
282989c67857SXiubo Li 		return -EINVAL;
283089c67857SXiubo Li 
283189c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
283289c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
283389c67857SXiubo Li 
283489c67857SXiubo Li 	return 0;
283589c67857SXiubo Li }
283689c67857SXiubo Li 
283789c67857SXiubo Li /**
2838e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2839e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2840a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2841a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
28428c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2843a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
28448c6529dbSLiam Girdwood  *
2845e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2846e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2847e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2848e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2849e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2850e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2851e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2852e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2853e46c9366SLars-Peter Clausen  * channel and so on.
2854e46c9366SLars-Peter Clausen  *
2855e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2856e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2857e46c9366SLars-Peter Clausen  *
2858e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
28598c6529dbSLiam Girdwood  */
28608c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2861a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
28628c6529dbSLiam Girdwood {
2863e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2864e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
286589c67857SXiubo Li 						&tx_mask, &rx_mask);
286689c67857SXiubo Li 	else
2867e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
286889c67857SXiubo Li 
286988bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
287088bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
287188bd870fSBenoit Cousson 
2872f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2873f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2874a5479e38SDaniel Ribeiro 				slots, slot_width);
28758c6529dbSLiam Girdwood 	else
2876b2cbb6e1SXiubo Li 		return -ENOTSUPP;
28778c6529dbSLiam Girdwood }
28788c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
28798c6529dbSLiam Girdwood 
28808c6529dbSLiam Girdwood /**
2881472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2882472df3cbSBarry Song  * @dai: DAI
2883472df3cbSBarry Song  * @tx_num: how many TX channels
2884472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2885472df3cbSBarry Song  *           0~num-1 uses
2886472df3cbSBarry Song  * @rx_num: how many RX channels
2887472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2888472df3cbSBarry Song  *           0~num-1 uses
2889472df3cbSBarry Song  *
2890472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2891472df3cbSBarry Song  */
2892472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2893472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2894472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2895472df3cbSBarry Song {
2896f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2897f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2898472df3cbSBarry Song 			rx_num, rx_slot);
2899472df3cbSBarry Song 	else
2900472df3cbSBarry Song 		return -EINVAL;
2901472df3cbSBarry Song }
2902472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2903472df3cbSBarry Song 
2904472df3cbSBarry Song /**
29058c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
29068c6529dbSLiam Girdwood  * @dai: DAI
29078c6529dbSLiam Girdwood  * @tristate: tristate enable
29088c6529dbSLiam Girdwood  *
29098c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
29108c6529dbSLiam Girdwood  */
29118c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
29128c6529dbSLiam Girdwood {
2913f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2914f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
29158c6529dbSLiam Girdwood 	else
29168c6529dbSLiam Girdwood 		return -EINVAL;
29178c6529dbSLiam Girdwood }
29188c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
29198c6529dbSLiam Girdwood 
29208c6529dbSLiam Girdwood /**
29218c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
29228c6529dbSLiam Girdwood  * @dai: DAI
29238c6529dbSLiam Girdwood  * @mute: mute enable
2924da18396fSMark Brown  * @direction: stream to mute
29258c6529dbSLiam Girdwood  *
29268c6529dbSLiam Girdwood  * Mutes the DAI DAC.
29278c6529dbSLiam Girdwood  */
2928da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2929da18396fSMark Brown 			     int direction)
29308c6529dbSLiam Girdwood {
2931da18396fSMark Brown 	if (!dai->driver)
2932da18396fSMark Brown 		return -ENOTSUPP;
2933da18396fSMark Brown 
2934da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2935da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2936da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2937da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2938f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
29398c6529dbSLiam Girdwood 	else
294004570c62SMark Brown 		return -ENOTSUPP;
29418c6529dbSLiam Girdwood }
29428c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
29438c6529dbSLiam Girdwood 
2944c5af3a2eSMark Brown /**
2945c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2946c5af3a2eSMark Brown  *
2947ac11a2b3SMark Brown  * @card: Card to register
2948c5af3a2eSMark Brown  *
2949c5af3a2eSMark Brown  */
295070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2951c5af3a2eSMark Brown {
2952923c5e61SMengdong Lin 	int i, ret;
29531a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2954f0fba2adSLiam Girdwood 
2955c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2956c5af3a2eSMark Brown 		return -EINVAL;
2957c5af3a2eSMark Brown 
29585a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
29595a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
29605a504963SStephen Warren 
2961923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
296288bd870fSBenoit Cousson 		if (ret) {
2963923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2964923c5e61SMengdong Lin 				link->name);
296588bd870fSBenoit Cousson 			return ret;
296688bd870fSBenoit Cousson 		}
29675a504963SStephen Warren 	}
29685a504963SStephen Warren 
2969ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2970ed77cc12SMark Brown 
2971111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2972111c6419SStephen Warren 
2973f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2974f8f80361SMengdong Lin 	card->num_dai_links = 0;
2975f0fba2adSLiam Girdwood 
29761a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
29779115171aSMark Brown 	card->num_rtd = 0;
2978db2a4165SFrank Mandarino 
2979db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
29808a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2981db2a4165SFrank Mandarino 	card->instantiated = 0;
2982db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2983a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2984db2a4165SFrank Mandarino 
2985b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2986b19e6e7bSMark Brown 	if (ret != 0)
29874e2576bdSKuninori Morimoto 		return ret;
2988db2a4165SFrank Mandarino 
2989988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
29901a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
299188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
299288bd870fSBenoit Cousson 		int j;
299388bd870fSBenoit Cousson 
299488bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
299588bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2996988e8cc4SNicolin Chen 			if (!codec_dai->active)
2997988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
299888bd870fSBenoit Cousson 		}
299988bd870fSBenoit Cousson 
3000988e8cc4SNicolin Chen 		if (!cpu_dai->active)
3001988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
3002988e8cc4SNicolin Chen 	}
3003988e8cc4SNicolin Chen 
3004b19e6e7bSMark Brown 	return ret;
3005db2a4165SFrank Mandarino }
300670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3007db2a4165SFrank Mandarino 
3008db2a4165SFrank Mandarino /**
3009db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
3010db2a4165SFrank Mandarino  *
3011db2a4165SFrank Mandarino  * @card: Card to unregister
3012db2a4165SFrank Mandarino  *
3013db2a4165SFrank Mandarino  */
301470a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3015db2a4165SFrank Mandarino {
301601e0df66SLars-Peter Clausen 	if (card->instantiated) {
301701e0df66SLars-Peter Clausen 		card->instantiated = false;
30181c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
3019b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3020f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
30218f6f9b29SKuninori Morimoto 	}
3022db2a4165SFrank Mandarino 
3023db2a4165SFrank Mandarino 	return 0;
3024db2a4165SFrank Mandarino }
302570a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3026db2a4165SFrank Mandarino 
3027db2a4165SFrank Mandarino /*
3028db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
3029db2a4165SFrank Mandarino  * and sanitizing names.
3030db2a4165SFrank Mandarino  */
30310b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3032db2a4165SFrank Mandarino {
3033db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
3034db2a4165SFrank Mandarino 	int id1, id2;
3035db2a4165SFrank Mandarino 
3036db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
3037db2a4165SFrank Mandarino 		return NULL;
3038db2a4165SFrank Mandarino 
303958818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3040db2a4165SFrank Mandarino 
3041db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
3042c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
3043c5af3a2eSMark Brown 	if (found) {
3044c5af3a2eSMark Brown 		/* get ID */
3045c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3046c5af3a2eSMark Brown 
3047c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
3048c5af3a2eSMark Brown 			if (*id == -1)
3049c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
3050c5af3a2eSMark Brown 		}
3051c5af3a2eSMark Brown 
3052c5af3a2eSMark Brown 	} else {
3053c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
3054c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3055c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
3056c5af3a2eSMark Brown 
3057c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
3058c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
3059c5af3a2eSMark Brown 
3060c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
3061c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
306258818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3063c5af3a2eSMark Brown 		} else
3064c5af3a2eSMark Brown 			*id = 0;
3065c5af3a2eSMark Brown 	}
3066c5af3a2eSMark Brown 
3067c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
3068c5af3a2eSMark Brown }
3069c5af3a2eSMark Brown 
3070c5af3a2eSMark Brown /*
3071c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
3072c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
3073c5af3a2eSMark Brown  */
3074c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
3075c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
3076c5af3a2eSMark Brown {
3077c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
307810e8aa9aSMichał Mirosław 		dev_err(dev,
307910e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
308010e8aa9aSMichał Mirosław 			dev_name(dev));
3081c5af3a2eSMark Brown 		return NULL;
3082c5af3a2eSMark Brown 	}
3083c5af3a2eSMark Brown 
3084c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
3085c5af3a2eSMark Brown }
3086c5af3a2eSMark Brown 
3087c5af3a2eSMark Brown /**
308832c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
30899115171aSMark Brown  *
309032c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
30919115171aSMark Brown  */
309232c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
30939115171aSMark Brown {
30945c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
30959115171aSMark Brown 
30965c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
309732c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
309832c9ba54SLars-Peter Clausen 			dai->name);
30999115171aSMark Brown 		list_del(&dai->list);
3100f0fba2adSLiam Girdwood 		kfree(dai->name);
3101f0fba2adSLiam Girdwood 		kfree(dai);
31029115171aSMark Brown 	}
310332c9ba54SLars-Peter Clausen }
31049115171aSMark Brown 
31055e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
31065e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
31075e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
31085e4fb372SMengdong Lin 	bool legacy_dai_naming)
31095e4fb372SMengdong Lin {
31105e4fb372SMengdong Lin 	struct device *dev = component->dev;
31115e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
31125e4fb372SMengdong Lin 
31135e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
31145e4fb372SMengdong Lin 
31155e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
31165e4fb372SMengdong Lin 	if (dai == NULL)
31175e4fb372SMengdong Lin 		return NULL;
31185e4fb372SMengdong Lin 
31195e4fb372SMengdong Lin 	/*
31205e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
31215e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
31225e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
31235e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
31245e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
31255e4fb372SMengdong Lin 	 * component-less anymore.
31265e4fb372SMengdong Lin 	 */
31275e4fb372SMengdong Lin 	if (legacy_dai_naming &&
31285e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
31295e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
31305e4fb372SMengdong Lin 	} else {
31315e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
31325e4fb372SMengdong Lin 		if (dai_drv->id)
31335e4fb372SMengdong Lin 			dai->id = dai_drv->id;
31345e4fb372SMengdong Lin 		else
31355e4fb372SMengdong Lin 			dai->id = component->num_dai;
31365e4fb372SMengdong Lin 	}
31375e4fb372SMengdong Lin 	if (dai->name == NULL) {
31385e4fb372SMengdong Lin 		kfree(dai);
31395e4fb372SMengdong Lin 		return NULL;
31405e4fb372SMengdong Lin 	}
31415e4fb372SMengdong Lin 
31425e4fb372SMengdong Lin 	dai->component = component;
31435e4fb372SMengdong Lin 	dai->dev = dev;
31445e4fb372SMengdong Lin 	dai->driver = dai_drv;
31455e4fb372SMengdong Lin 	if (!dai->driver->ops)
31465e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
31475e4fb372SMengdong Lin 
31485e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
31495e4fb372SMengdong Lin 	component->num_dai++;
31505e4fb372SMengdong Lin 
31515e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
31525e4fb372SMengdong Lin 	return dai;
31535e4fb372SMengdong Lin }
31545e4fb372SMengdong Lin 
31559115171aSMark Brown /**
315632c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
31579115171aSMark Brown  *
31586106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
31596106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3160ac11a2b3SMark Brown  * @count: Number of DAIs
316132c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
316232c9ba54SLars-Peter Clausen  *                     parent's name.
31639115171aSMark Brown  */
316432c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
3165bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
3166bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
31679115171aSMark Brown {
31686106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3169f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
317032c9ba54SLars-Peter Clausen 	unsigned int i;
317132c9ba54SLars-Peter Clausen 	int ret;
3172f0fba2adSLiam Girdwood 
31735b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
31749115171aSMark Brown 
3175bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
3176bb13109dSLars-Peter Clausen 
31779115171aSMark Brown 	for (i = 0; i < count; i++) {
3178f0fba2adSLiam Girdwood 
31795e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
31805e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
3181c46e0079SAxel Lin 		if (dai == NULL) {
3182c46e0079SAxel Lin 			ret = -ENOMEM;
3183c46e0079SAxel Lin 			goto err;
3184c46e0079SAxel Lin 		}
3185f0fba2adSLiam Girdwood 	}
3186f0fba2adSLiam Girdwood 
31879115171aSMark Brown 	return 0;
31889115171aSMark Brown 
31899115171aSMark Brown err:
319032c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
31919115171aSMark Brown 
31929115171aSMark Brown 	return ret;
31939115171aSMark Brown }
31949115171aSMark Brown 
319568003e6cSMengdong Lin /**
319668003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
319768003e6cSMengdong Lin  *
319868003e6cSMengdong Lin  * @component: The component the DAIs are registered for
319968003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
320068003e6cSMengdong Lin  *
320168003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
320268003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
320368003e6cSMengdong Lin  * will be freed in the component cleanup.
320468003e6cSMengdong Lin  */
320568003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
320668003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
320768003e6cSMengdong Lin {
320868003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
320968003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
321068003e6cSMengdong Lin 	struct snd_soc_dai *dai;
321168003e6cSMengdong Lin 	int ret;
321268003e6cSMengdong Lin 
321368003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
321468003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
321568003e6cSMengdong Lin 			dai_drv->dobj.type);
321668003e6cSMengdong Lin 		return -EINVAL;
321768003e6cSMengdong Lin 	}
321868003e6cSMengdong Lin 
321968003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
322068003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
322168003e6cSMengdong Lin 	if (!dai)
322268003e6cSMengdong Lin 		return -ENOMEM;
322368003e6cSMengdong Lin 
322468003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
322568003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
322668003e6cSMengdong Lin 	 */
322768003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
322868003e6cSMengdong Lin 	if (ret != 0) {
322968003e6cSMengdong Lin 		dev_err(component->dev,
323068003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
323168003e6cSMengdong Lin 	}
323268003e6cSMengdong Lin 
323368003e6cSMengdong Lin 	return ret;
323468003e6cSMengdong Lin }
323568003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
323668003e6cSMengdong Lin 
323714e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
323814e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3239d191bd8dSKuninori Morimoto {
324014e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3241d191bd8dSKuninori Morimoto 
324214e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
324314e8bdebSLars-Peter Clausen }
3244d191bd8dSKuninori Morimoto 
324514e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
324614e8bdebSLars-Peter Clausen 	int event)
324714e8bdebSLars-Peter Clausen {
324814e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
324914e8bdebSLars-Peter Clausen 
325014e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
325114e8bdebSLars-Peter Clausen }
325214e8bdebSLars-Peter Clausen 
3253bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3254bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3255d191bd8dSKuninori Morimoto {
3256ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3257ce0fc93aSLars-Peter Clausen 
3258bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3259bb13109dSLars-Peter Clausen 	if (!component->name) {
3260bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3261d191bd8dSKuninori Morimoto 		return -ENOMEM;
3262d191bd8dSKuninori Morimoto 	}
3263d191bd8dSKuninori Morimoto 
3264bb13109dSLars-Peter Clausen 	component->dev = dev;
3265bb13109dSLars-Peter Clausen 	component->driver = driver;
326661aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
326761aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
32689178feb4SKuninori Morimoto 	component->suspend = component->driver->suspend;
32699178feb4SKuninori Morimoto 	component->resume = component->driver->resume;
327071ccef0dSKuninori Morimoto 	component->set_sysclk = component->driver->set_sysclk;
3271ef641e5dSKuninori Morimoto 	component->set_pll = component->driver->set_pll;
327244c07365SKuninori Morimoto 	component->set_jack = component->driver->set_jack;
3273e2c330b9SLars-Peter Clausen 
327488c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
3275ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3276ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3277ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
32785819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
327914e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
328014e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
328114e8bdebSLars-Peter Clausen 	if (driver->stream_event)
328214e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
3283ce0fc93aSLars-Peter Clausen 
3284bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3285bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3286bb13109dSLars-Peter Clausen 
3287bb13109dSLars-Peter Clausen 	return 0;
3288d191bd8dSKuninori Morimoto }
3289d191bd8dSKuninori Morimoto 
329020feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3291886f5692SLars-Peter Clausen {
3292886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
329320feb881SLars-Peter Clausen 
3294886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3295886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3296886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3297886f5692SLars-Peter Clausen }
329820feb881SLars-Peter Clausen 
3299e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3300e874bf5fSLars-Peter Clausen 
330120feb881SLars-Peter Clausen /**
330220feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
330320feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
330420feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
330520feb881SLars-Peter Clausen  *
330620feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
330720feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
330820feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
330920feb881SLars-Peter Clausen  * before the first IO attempt of the component.
331020feb881SLars-Peter Clausen  */
331120feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
331220feb881SLars-Peter Clausen 	struct regmap *regmap)
331320feb881SLars-Peter Clausen {
331420feb881SLars-Peter Clausen 	component->regmap = regmap;
331520feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3316886f5692SLars-Peter Clausen }
331720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
331820feb881SLars-Peter Clausen 
331920feb881SLars-Peter Clausen /**
332020feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
332120feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
332220feb881SLars-Peter Clausen  *
332320feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
332420feb881SLars-Peter Clausen  * removes the regmap instance from the component.
332520feb881SLars-Peter Clausen  *
332620feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
332720feb881SLars-Peter Clausen  * to initialize the regmap instance.
332820feb881SLars-Peter Clausen  */
332920feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
333020feb881SLars-Peter Clausen {
333120feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
333220feb881SLars-Peter Clausen 	component->regmap = NULL;
333320feb881SLars-Peter Clausen }
333420feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3335886f5692SLars-Peter Clausen 
3336e874bf5fSLars-Peter Clausen #endif
3337d191bd8dSKuninori Morimoto 
3338bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3339bb13109dSLars-Peter Clausen {
334020feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
334120feb881SLars-Peter Clausen 		if (!component->regmap)
334220feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
334320feb881SLars-Peter Clausen 		if (component->regmap)
334420feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
334520feb881SLars-Peter Clausen 	}
3346886f5692SLars-Peter Clausen 
3347bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
33488a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3349d191bd8dSKuninori Morimoto }
3350d191bd8dSKuninori Morimoto 
3351bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3352bb13109dSLars-Peter Clausen {
3353d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3354bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3355d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3356bb13109dSLars-Peter Clausen }
3357d191bd8dSKuninori Morimoto 
3358bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3359bb13109dSLars-Peter Clausen {
3360bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3361bb13109dSLars-Peter Clausen 	kfree(component->name);
3362bb13109dSLars-Peter Clausen }
3363d191bd8dSKuninori Morimoto 
3364bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3365bb13109dSLars-Peter Clausen {
3366c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3367c12c1aadSKuninori Morimoto 
3368c12c1aadSKuninori Morimoto 	if (card)
3369c12c1aadSKuninori Morimoto 		snd_soc_unregister_card(card);
3370c12c1aadSKuninori Morimoto 
3371bb13109dSLars-Peter Clausen 	list_del(&component->list);
3372bb13109dSLars-Peter Clausen }
3373d191bd8dSKuninori Morimoto 
3374*e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
3375*e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
3376cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3377d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3378d191bd8dSKuninori Morimoto 			int num_dai)
3379d191bd8dSKuninori Morimoto {
3380bb13109dSLars-Peter Clausen 	int ret;
3381d191bd8dSKuninori Morimoto 
3382cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
3383bb13109dSLars-Peter Clausen 	if (ret)
3384bb13109dSLars-Peter Clausen 		goto err_free;
3385bb13109dSLars-Peter Clausen 
3386cf9e829eSKuninori Morimoto 	component->ignore_pmdown_time = true;
3387cf9e829eSKuninori Morimoto 	component->registered_as_component = true;
33883d59400fSLars-Peter Clausen 
3389cf9e829eSKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai, true);
3390bb13109dSLars-Peter Clausen 	if (ret < 0) {
3391f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3392bb13109dSLars-Peter Clausen 		goto err_cleanup;
3393bb13109dSLars-Peter Clausen 	}
3394bb13109dSLars-Peter Clausen 
3395cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
3396bb13109dSLars-Peter Clausen 
3397bb13109dSLars-Peter Clausen 	return 0;
3398bb13109dSLars-Peter Clausen 
3399bb13109dSLars-Peter Clausen err_cleanup:
3400cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
3401bb13109dSLars-Peter Clausen err_free:
3402cf9e829eSKuninori Morimoto 	kfree(component);
3403bb13109dSLars-Peter Clausen 	return ret;
3404d191bd8dSKuninori Morimoto }
3405*e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
3406*e0dac41bSKuninori Morimoto 
3407*e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3408*e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3409*e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3410*e0dac41bSKuninori Morimoto 			int num_dai)
3411*e0dac41bSKuninori Morimoto {
3412*e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
3413*e0dac41bSKuninori Morimoto 
3414*e0dac41bSKuninori Morimoto 	component = kzalloc(sizeof(*component), GFP_KERNEL);
3415*e0dac41bSKuninori Morimoto 	if (!component) {
3416*e0dac41bSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3417*e0dac41bSKuninori Morimoto 		return -ENOMEM;
3418*e0dac41bSKuninori Morimoto 	}
3419*e0dac41bSKuninori Morimoto 
3420*e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
3421*e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
3422*e0dac41bSKuninori Morimoto }
3423d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3424d191bd8dSKuninori Morimoto 
3425d191bd8dSKuninori Morimoto /**
34262eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
34272eccea8cSKuninori Morimoto  * from the ASoC core
3428d191bd8dSKuninori Morimoto  *
3429628536eaSJonathan Corbet  * @dev: The device to unregister
3430d191bd8dSKuninori Morimoto  */
34312eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
3432d191bd8dSKuninori Morimoto {
3433cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
343421a03528SKuninori Morimoto 	int found = 0;
3435d191bd8dSKuninori Morimoto 
343634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3437cf9e829eSKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
343821a03528SKuninori Morimoto 		if (dev != component->dev ||
343921a03528SKuninori Morimoto 		    !component->registered_as_component)
344021a03528SKuninori Morimoto 			continue;
3441d191bd8dSKuninori Morimoto 
3442cf9e829eSKuninori Morimoto 		snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3443cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
344421a03528SKuninori Morimoto 		found = 1;
344521a03528SKuninori Morimoto 		break;
3446d191bd8dSKuninori Morimoto 	}
3447d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3448d191bd8dSKuninori Morimoto 
344921a03528SKuninori Morimoto 	if (found) {
3450cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
3451cf9e829eSKuninori Morimoto 		kfree(component);
3452d191bd8dSKuninori Morimoto 	}
34532eccea8cSKuninori Morimoto 
34542eccea8cSKuninori Morimoto 	return found;
34552eccea8cSKuninori Morimoto }
34562eccea8cSKuninori Morimoto 
34572eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
34582eccea8cSKuninori Morimoto {
34592eccea8cSKuninori Morimoto 	while (__snd_soc_unregister_component(dev));
3460d191bd8dSKuninori Morimoto }
3461d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3462d191bd8dSKuninori Morimoto 
3463f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3464e2c330b9SLars-Peter Clausen {
3465e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3466e2c330b9SLars-Peter Clausen 
3467f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3468e2c330b9SLars-Peter Clausen }
3469e2c330b9SLars-Peter Clausen 
3470f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3471e2c330b9SLars-Peter Clausen {
3472e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3473e2c330b9SLars-Peter Clausen 
3474f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3475e2c330b9SLars-Peter Clausen }
3476e2c330b9SLars-Peter Clausen 
3477d191bd8dSKuninori Morimoto /**
347871a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
347971a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
348071a45cdaSLars-Peter Clausen  * @platform: The platform to add
34817d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
348212a48a8cSMark Brown  */
348371a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3484d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
348512a48a8cSMark Brown {
3486b37f1d12SLars-Peter Clausen 	int ret;
3487b37f1d12SLars-Peter Clausen 
3488bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3489bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3490bb13109dSLars-Peter Clausen 	if (ret)
3491bb13109dSLars-Peter Clausen 		return ret;
3492f0fba2adSLiam Girdwood 
3493f0fba2adSLiam Girdwood 	platform->dev = dev;
3494f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3495f1d45cc3SLars-Peter Clausen 
3496f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3497f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3498f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3499f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
350012a48a8cSMark Brown 
350181c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
350281c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
350381c7cfd1SLars-Peter Clausen #endif
350412a48a8cSMark Brown 
350512a48a8cSMark Brown 	mutex_lock(&client_mutex);
3506bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
350712a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
350812a48a8cSMark Brown 	mutex_unlock(&client_mutex);
350912a48a8cSMark Brown 
3510f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3511f4333203SLars-Peter Clausen 		platform->component.name);
351212a48a8cSMark Brown 
351312a48a8cSMark Brown 	return 0;
351412a48a8cSMark Brown }
351571a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
351671a45cdaSLars-Peter Clausen 
351771a45cdaSLars-Peter Clausen /**
351871a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
351971a45cdaSLars-Peter Clausen  *
3520628536eaSJonathan Corbet  * @dev: The device for the platform
3521628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
352271a45cdaSLars-Peter Clausen  */
352371a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
352471a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
352571a45cdaSLars-Peter Clausen {
352671a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
352771a45cdaSLars-Peter Clausen 	int ret;
352871a45cdaSLars-Peter Clausen 
352971a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
353071a45cdaSLars-Peter Clausen 
353171a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
353271a45cdaSLars-Peter Clausen 	if (platform == NULL)
353371a45cdaSLars-Peter Clausen 		return -ENOMEM;
353471a45cdaSLars-Peter Clausen 
353571a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
353671a45cdaSLars-Peter Clausen 	if (ret)
353771a45cdaSLars-Peter Clausen 		kfree(platform);
353871a45cdaSLars-Peter Clausen 
353971a45cdaSLars-Peter Clausen 	return ret;
354071a45cdaSLars-Peter Clausen }
354112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
354212a48a8cSMark Brown 
354312a48a8cSMark Brown /**
354471a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
354571a45cdaSLars-Peter Clausen  * @platform: the platform to remove
354671a45cdaSLars-Peter Clausen  */
354771a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
354871a45cdaSLars-Peter Clausen {
3549b37f1d12SLars-Peter Clausen 
355071a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
355171a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3552bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
355371a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
355471a45cdaSLars-Peter Clausen 
355571a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3556f4333203SLars-Peter Clausen 		platform->component.name);
3557decc27b0SDaniel Mack 
3558decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
355971a45cdaSLars-Peter Clausen }
356071a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
356171a45cdaSLars-Peter Clausen 
356271a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
356371a45cdaSLars-Peter Clausen {
356471a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
356571a45cdaSLars-Peter Clausen 
356634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
356771a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
356834e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
356934e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
357071a45cdaSLars-Peter Clausen 			return platform;
357171a45cdaSLars-Peter Clausen 		}
357234e81ab4SLars-Peter Clausen 	}
357334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
357471a45cdaSLars-Peter Clausen 
357571a45cdaSLars-Peter Clausen 	return NULL;
357671a45cdaSLars-Peter Clausen }
357771a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
357871a45cdaSLars-Peter Clausen 
357971a45cdaSLars-Peter Clausen /**
358012a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
358112a48a8cSMark Brown  *
3582628536eaSJonathan Corbet  * @dev: platform to unregister
358312a48a8cSMark Brown  */
3584f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
358512a48a8cSMark Brown {
3586f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3587f0fba2adSLiam Girdwood 
358871a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
358971a45cdaSLars-Peter Clausen 	if (!platform)
3590f0fba2adSLiam Girdwood 		return;
3591f0fba2adSLiam Girdwood 
359271a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3593f0fba2adSLiam Girdwood 	kfree(platform);
359412a48a8cSMark Brown }
359512a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
359612a48a8cSMark Brown 
3597151ab22cSMark Brown static u64 codec_format_map[] = {
3598151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3599151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3600151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3601151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3602151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3603151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3604151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3605151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3606151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3607151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3608151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3609151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3610151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3611151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3612151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3613151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3614151ab22cSMark Brown };
3615151ab22cSMark Brown 
3616151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3617151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3618151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3619151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3620151ab22cSMark Brown  */
3621151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3622151ab22cSMark Brown {
3623151ab22cSMark Brown 	int i;
3624151ab22cSMark Brown 
3625151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3626151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3627151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3628151ab22cSMark Brown }
3629151ab22cSMark Brown 
3630f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3631f1d45cc3SLars-Peter Clausen {
3632f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3633f1d45cc3SLars-Peter Clausen 
3634f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3635f1d45cc3SLars-Peter Clausen }
3636f1d45cc3SLars-Peter Clausen 
3637f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3638f1d45cc3SLars-Peter Clausen {
3639f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3640f1d45cc3SLars-Peter Clausen 
3641f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3642f1d45cc3SLars-Peter Clausen }
3643f1d45cc3SLars-Peter Clausen 
36449178feb4SKuninori Morimoto static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
36459178feb4SKuninori Morimoto {
36469178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
36479178feb4SKuninori Morimoto 
36489178feb4SKuninori Morimoto 	return codec->driver->suspend(codec);
36499178feb4SKuninori Morimoto }
36509178feb4SKuninori Morimoto 
36519178feb4SKuninori Morimoto static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
36529178feb4SKuninori Morimoto {
36539178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
36549178feb4SKuninori Morimoto 
36559178feb4SKuninori Morimoto 	return codec->driver->resume(codec);
36569178feb4SKuninori Morimoto }
36579178feb4SKuninori Morimoto 
3658e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3659e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3660e2c330b9SLars-Peter Clausen {
3661e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3662e2c330b9SLars-Peter Clausen 
3663e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3664e2c330b9SLars-Peter Clausen }
3665e2c330b9SLars-Peter Clausen 
3666e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3667e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3668e2c330b9SLars-Peter Clausen {
3669e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3670e2c330b9SLars-Peter Clausen 
3671e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3672e2c330b9SLars-Peter Clausen 
3673e2c330b9SLars-Peter Clausen 	return 0;
3674e2c330b9SLars-Peter Clausen }
3675e2c330b9SLars-Peter Clausen 
367671ccef0dSKuninori Morimoto static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
367771ccef0dSKuninori Morimoto 			  int clk_id, int source, unsigned int freq, int dir)
367871ccef0dSKuninori Morimoto {
367971ccef0dSKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
368071ccef0dSKuninori Morimoto 
368171ccef0dSKuninori Morimoto 	return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
368271ccef0dSKuninori Morimoto }
368371ccef0dSKuninori Morimoto 
3684ef641e5dSKuninori Morimoto static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3685ef641e5dSKuninori Morimoto 				  int pll_id, int source, unsigned int freq_in,
3686ef641e5dSKuninori Morimoto 				  unsigned int freq_out)
3687ef641e5dSKuninori Morimoto {
3688ef641e5dSKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3689ef641e5dSKuninori Morimoto 
3690ef641e5dSKuninori Morimoto 	return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3691ef641e5dSKuninori Morimoto }
3692ef641e5dSKuninori Morimoto 
369344c07365SKuninori Morimoto static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
369444c07365SKuninori Morimoto 			       struct snd_soc_jack *jack, void *data)
369544c07365SKuninori Morimoto {
369644c07365SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
369744c07365SKuninori Morimoto 
369844c07365SKuninori Morimoto 	return snd_soc_codec_set_jack(codec, jack, data);
369944c07365SKuninori Morimoto }
370044c07365SKuninori Morimoto 
370168f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
370268f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
370368f831c2SLars-Peter Clausen {
370468f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
370568f831c2SLars-Peter Clausen 
370668f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
370768f831c2SLars-Peter Clausen }
370868f831c2SLars-Peter Clausen 
37090d0cf00aSMark Brown /**
37100d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
37110d0cf00aSMark Brown  *
3712628536eaSJonathan Corbet  * @dev: The parent device for this codec
3713628536eaSJonathan Corbet  * @codec_drv: Codec driver
3714628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3715628536eaSJonathan Corbet  * @num_dai: Number of DAIs
37160d0cf00aSMark Brown  */
3717f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3718001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3719001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3720001ae4c0SMark Brown 			   int num_dai)
37210d0cf00aSMark Brown {
37224890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3723f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3724bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3725f0fba2adSLiam Girdwood 	int ret, i;
3726151ab22cSMark Brown 
3727f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
37280d0cf00aSMark Brown 
3729f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3730f0fba2adSLiam Girdwood 	if (codec == NULL)
3731f0fba2adSLiam Girdwood 		return -ENOMEM;
37320d0cf00aSMark Brown 
3733f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3734ce0fc93aSLars-Peter Clausen 
3735bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3736bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3737bb13109dSLars-Peter Clausen 	if (ret)
3738bb13109dSLars-Peter Clausen 		goto err_free;
3739151ab22cSMark Brown 
3740f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3741f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3742f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3743f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
37449178feb4SKuninori Morimoto 	if (codec_drv->suspend)
37459178feb4SKuninori Morimoto 		codec->component.suspend = snd_soc_codec_drv_suspend;
37469178feb4SKuninori Morimoto 	if (codec_drv->resume)
37479178feb4SKuninori Morimoto 		codec->component.resume = snd_soc_codec_drv_resume;
3748e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3749e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3750e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3751e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
375271ccef0dSKuninori Morimoto 	if (codec_drv->set_sysclk)
375371ccef0dSKuninori Morimoto 		codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
3754ef641e5dSKuninori Morimoto 	if (codec_drv->set_pll)
3755ef641e5dSKuninori Morimoto 		codec->component.set_pll = snd_soc_codec_set_pll_;
375644c07365SKuninori Morimoto 	if (codec_drv->set_jack)
375744c07365SKuninori Morimoto 		codec->component.set_jack = snd_soc_codec_set_jack_;
37583d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
37594890140fSLars-Peter Clausen 
37604890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
37614890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
37624890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
376314e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
37644890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
376568f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
37664890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3767f0fba2adSLiam Girdwood 	codec->dev = dev;
3768f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3769e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3770f0fba2adSLiam Girdwood 
377181c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
377281c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
377381c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
377481c7cfd1SLars-Peter Clausen #endif
3775a39f75f7SXiubo Li 
3776a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3777886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3778a39f75f7SXiubo Li 
3779f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3780f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3781f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3782f0fba2adSLiam Girdwood 	}
3783f0fba2adSLiam Girdwood 
3784bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3785bb13109dSLars-Peter Clausen 	if (ret < 0) {
3786f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3787bb13109dSLars-Peter Clausen 		goto err_cleanup;
3788bb13109dSLars-Peter Clausen 	}
3789bb13109dSLars-Peter Clausen 
3790bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3791bb13109dSLars-Peter Clausen 		dai->codec = codec;
3792bb13109dSLars-Peter Clausen 
3793054880feSMark Brown 	mutex_lock(&client_mutex);
3794bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3795054880feSMark Brown 	list_add(&codec->list, &codec_list);
3796054880feSMark Brown 	mutex_unlock(&client_mutex);
3797054880feSMark Brown 
3798f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3799f4333203SLars-Peter Clausen 		codec->component.name);
38000d0cf00aSMark Brown 	return 0;
3801f0fba2adSLiam Girdwood 
3802bb13109dSLars-Peter Clausen err_cleanup:
3803bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3804bb13109dSLars-Peter Clausen err_free:
3805f0fba2adSLiam Girdwood 	kfree(codec);
3806f0fba2adSLiam Girdwood 	return ret;
38070d0cf00aSMark Brown }
38080d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
38090d0cf00aSMark Brown 
38100d0cf00aSMark Brown /**
38110d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
38120d0cf00aSMark Brown  *
3813628536eaSJonathan Corbet  * @dev: codec to unregister
38140d0cf00aSMark Brown  */
3815f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
38160d0cf00aSMark Brown {
3817f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3818f0fba2adSLiam Girdwood 
381934e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3820f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3821f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3822f0fba2adSLiam Girdwood 			goto found;
3823f0fba2adSLiam Girdwood 	}
382434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3825f0fba2adSLiam Girdwood 	return;
3826f0fba2adSLiam Girdwood 
3827f0fba2adSLiam Girdwood found:
38280d0cf00aSMark Brown 	list_del(&codec->list);
3829bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
38300d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
38310d0cf00aSMark Brown 
3832f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3833f4333203SLars-Peter Clausen 			codec->component.name);
3834f0fba2adSLiam Girdwood 
3835bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
38367a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3837f0fba2adSLiam Girdwood 	kfree(codec);
38380d0cf00aSMark Brown }
38390d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
38400d0cf00aSMark Brown 
3841bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3842b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3843bec4fa05SStephen Warren 			       const char *propname)
3844bec4fa05SStephen Warren {
3845b07609ceSKuninori Morimoto 	struct device_node *np;
3846bec4fa05SStephen Warren 	int ret;
3847bec4fa05SStephen Warren 
38487e07e7c0STushar Behera 	if (!card->dev) {
38497e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
38507e07e7c0STushar Behera 		return -EINVAL;
38517e07e7c0STushar Behera 	}
38527e07e7c0STushar Behera 
38537e07e7c0STushar Behera 	np = card->dev->of_node;
38547e07e7c0STushar Behera 
3855bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3856bec4fa05SStephen Warren 	/*
3857bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3858bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3859bec4fa05SStephen Warren 	 * snd_soc_register_card.
3860bec4fa05SStephen Warren 	 */
3861bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3862bec4fa05SStephen Warren 		dev_err(card->dev,
3863f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3864bec4fa05SStephen Warren 			propname, ret);
3865bec4fa05SStephen Warren 		return ret;
3866bec4fa05SStephen Warren 	}
3867bec4fa05SStephen Warren 
3868bec4fa05SStephen Warren 	return 0;
3869bec4fa05SStephen Warren }
3870b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3871bec4fa05SStephen Warren 
38729a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
38739a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
38749a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
38759a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
38769a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
38779a6d4860SXiubo Li };
38789a6d4860SXiubo Li 
387921efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
38809a6d4860SXiubo Li 					  const char *propname)
38819a6d4860SXiubo Li {
388221efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
38839a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
38849a6d4860SXiubo Li 	const char *template, *wname;
38859a6d4860SXiubo Li 	int i, j, num_widgets, ret;
38869a6d4860SXiubo Li 
38879a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
38889a6d4860SXiubo Li 	if (num_widgets < 0) {
38899a6d4860SXiubo Li 		dev_err(card->dev,
38909a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
38919a6d4860SXiubo Li 		return -EINVAL;
38929a6d4860SXiubo Li 	}
38939a6d4860SXiubo Li 	if (num_widgets & 1) {
38949a6d4860SXiubo Li 		dev_err(card->dev,
38959a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
38969a6d4860SXiubo Li 		return -EINVAL;
38979a6d4860SXiubo Li 	}
38989a6d4860SXiubo Li 
38999a6d4860SXiubo Li 	num_widgets /= 2;
39009a6d4860SXiubo Li 	if (!num_widgets) {
39019a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
39029a6d4860SXiubo Li 			propname);
39039a6d4860SXiubo Li 		return -EINVAL;
39049a6d4860SXiubo Li 	}
39059a6d4860SXiubo Li 
39069a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
39079a6d4860SXiubo Li 			       GFP_KERNEL);
39089a6d4860SXiubo Li 	if (!widgets) {
39099a6d4860SXiubo Li 		dev_err(card->dev,
39109a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
39119a6d4860SXiubo Li 		return -ENOMEM;
39129a6d4860SXiubo Li 	}
39139a6d4860SXiubo Li 
39149a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
39159a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
39169a6d4860SXiubo Li 			2 * i, &template);
39179a6d4860SXiubo Li 		if (ret) {
39189a6d4860SXiubo Li 			dev_err(card->dev,
39199a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
39209a6d4860SXiubo Li 				propname, 2 * i, ret);
39219a6d4860SXiubo Li 			return -EINVAL;
39229a6d4860SXiubo Li 		}
39239a6d4860SXiubo Li 
39249a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
39259a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
39269a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
39279a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
39289a6d4860SXiubo Li 				break;
39299a6d4860SXiubo Li 			}
39309a6d4860SXiubo Li 		}
39319a6d4860SXiubo Li 
39329a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
39339a6d4860SXiubo Li 			dev_err(card->dev,
39349a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
39359a6d4860SXiubo Li 				template);
39369a6d4860SXiubo Li 			return -EINVAL;
39379a6d4860SXiubo Li 		}
39389a6d4860SXiubo Li 
39399a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
39409a6d4860SXiubo Li 						    (2 * i) + 1,
39419a6d4860SXiubo Li 						    &wname);
39429a6d4860SXiubo Li 		if (ret) {
39439a6d4860SXiubo Li 			dev_err(card->dev,
39449a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
39459a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
39469a6d4860SXiubo Li 			return -EINVAL;
39479a6d4860SXiubo Li 		}
39489a6d4860SXiubo Li 
39499a6d4860SXiubo Li 		widgets[i].name = wname;
39509a6d4860SXiubo Li 	}
39519a6d4860SXiubo Li 
3952f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3953f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
39549a6d4860SXiubo Li 
39559a6d4860SXiubo Li 	return 0;
39569a6d4860SXiubo Li }
395721efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
39589a6d4860SXiubo Li 
39596131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
39606131084aSJyri Sarha 				    const char *prop_name,
39616131084aSJyri Sarha 				    unsigned int *mask)
39626131084aSJyri Sarha {
39636131084aSJyri Sarha 	u32 val;
39646c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
39656131084aSJyri Sarha 	int i;
39666131084aSJyri Sarha 
39676131084aSJyri Sarha 	if (!of_slot_mask)
39686131084aSJyri Sarha 		return 0;
39696131084aSJyri Sarha 	val /= sizeof(u32);
39706131084aSJyri Sarha 	for (i = 0; i < val; i++)
39716131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
39726131084aSJyri Sarha 			*mask |= (1 << i);
39736131084aSJyri Sarha 
39746131084aSJyri Sarha 	return val;
39756131084aSJyri Sarha }
39766131084aSJyri Sarha 
397789c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
39786131084aSJyri Sarha 			      unsigned int *tx_mask,
39796131084aSJyri Sarha 			      unsigned int *rx_mask,
398089c67857SXiubo Li 			      unsigned int *slots,
398189c67857SXiubo Li 			      unsigned int *slot_width)
398289c67857SXiubo Li {
398389c67857SXiubo Li 	u32 val;
398489c67857SXiubo Li 	int ret;
398589c67857SXiubo Li 
39866131084aSJyri Sarha 	if (tx_mask)
39876131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
39886131084aSJyri Sarha 	if (rx_mask)
39896131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
39906131084aSJyri Sarha 
399189c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
399289c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
399389c67857SXiubo Li 		if (ret)
399489c67857SXiubo Li 			return ret;
399589c67857SXiubo Li 
399689c67857SXiubo Li 		if (slots)
399789c67857SXiubo Li 			*slots = val;
399889c67857SXiubo Li 	}
399989c67857SXiubo Li 
400089c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
400189c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
400289c67857SXiubo Li 		if (ret)
400389c67857SXiubo Li 			return ret;
400489c67857SXiubo Li 
400589c67857SXiubo Li 		if (slot_width)
400689c67857SXiubo Li 			*slot_width = val;
400789c67857SXiubo Li 	}
400889c67857SXiubo Li 
400989c67857SXiubo Li 	return 0;
401089c67857SXiubo Li }
401189c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
401289c67857SXiubo Li 
4013440a3006SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
40145e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
40155e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
40165e3cdaa2SKuninori Morimoto 				   const char *propname)
40175e3cdaa2SKuninori Morimoto {
4018440a3006SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
40195e3cdaa2SKuninori Morimoto 	const char *str;
40205e3cdaa2SKuninori Morimoto 	int ret;
40215e3cdaa2SKuninori Morimoto 
40225e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
40235e3cdaa2SKuninori Morimoto 	if (ret < 0) {
40245e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
40255e3cdaa2SKuninori Morimoto 		return;
40265e3cdaa2SKuninori Morimoto 	}
40275e3cdaa2SKuninori Morimoto 
40285e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
40295e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
40305e3cdaa2SKuninori Morimoto }
4031440a3006SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
40325e3cdaa2SKuninori Morimoto 
40332bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4034a4a54dd5SStephen Warren 				   const char *propname)
4035a4a54dd5SStephen Warren {
40362bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
4037e3b1e6a1SMark Brown 	int num_routes;
4038a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4039a4a54dd5SStephen Warren 	int i, ret;
4040a4a54dd5SStephen Warren 
4041a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4042c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
404310e8aa9aSMichał Mirosław 		dev_err(card->dev,
404410e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
404510e8aa9aSMichał Mirosław 			propname);
4046a4a54dd5SStephen Warren 		return -EINVAL;
4047a4a54dd5SStephen Warren 	}
4048a4a54dd5SStephen Warren 	num_routes /= 2;
4049a4a54dd5SStephen Warren 	if (!num_routes) {
4050f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4051a4a54dd5SStephen Warren 			propname);
4052a4a54dd5SStephen Warren 		return -EINVAL;
4053a4a54dd5SStephen Warren 	}
4054a4a54dd5SStephen Warren 
4055e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4056a4a54dd5SStephen Warren 			      GFP_KERNEL);
4057a4a54dd5SStephen Warren 	if (!routes) {
4058a4a54dd5SStephen Warren 		dev_err(card->dev,
4059f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4060a4a54dd5SStephen Warren 		return -EINVAL;
4061a4a54dd5SStephen Warren 	}
4062a4a54dd5SStephen Warren 
4063a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4064a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4065e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
4066a4a54dd5SStephen Warren 		if (ret) {
4067c871bd0bSMark Brown 			dev_err(card->dev,
4068c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4069c871bd0bSMark Brown 				propname, 2 * i, ret);
4070a4a54dd5SStephen Warren 			return -EINVAL;
4071a4a54dd5SStephen Warren 		}
4072a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4073e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
4074a4a54dd5SStephen Warren 		if (ret) {
4075a4a54dd5SStephen Warren 			dev_err(card->dev,
4076c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4077c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4078a4a54dd5SStephen Warren 			return -EINVAL;
4079a4a54dd5SStephen Warren 		}
4080a4a54dd5SStephen Warren 	}
4081a4a54dd5SStephen Warren 
4082f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
4083f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
4084a4a54dd5SStephen Warren 
4085a4a54dd5SStephen Warren 	return 0;
4086a4a54dd5SStephen Warren }
40872bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4088a4a54dd5SStephen Warren 
4089a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4090389cb834SJyri Sarha 				     const char *prefix,
4091389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
4092389cb834SJyri Sarha 				     struct device_node **framemaster)
4093a7930ed4SKuninori Morimoto {
4094a7930ed4SKuninori Morimoto 	int ret, i;
4095a7930ed4SKuninori Morimoto 	char prop[128];
4096a7930ed4SKuninori Morimoto 	unsigned int format = 0;
4097a7930ed4SKuninori Morimoto 	int bit, frame;
4098a7930ed4SKuninori Morimoto 	const char *str;
4099a7930ed4SKuninori Morimoto 	struct {
4100a7930ed4SKuninori Morimoto 		char *name;
4101a7930ed4SKuninori Morimoto 		unsigned int val;
4102a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
4103a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
4104a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
4105a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
4106a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
4107a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
4108a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
4109a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
4110a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
4111a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
4112a7930ed4SKuninori Morimoto 	};
4113a7930ed4SKuninori Morimoto 
4114a7930ed4SKuninori Morimoto 	if (!prefix)
4115a7930ed4SKuninori Morimoto 		prefix = "";
4116a7930ed4SKuninori Morimoto 
4117a7930ed4SKuninori Morimoto 	/*
41185711c979SKuninori Morimoto 	 * check "dai-format = xxx"
41195711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
4120a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4121a7930ed4SKuninori Morimoto 	 */
41225711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
41235711c979SKuninori Morimoto 	if (ret < 0) {
4124a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
4125a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
41265711c979SKuninori Morimoto 	}
4127a7930ed4SKuninori Morimoto 	if (ret == 0) {
4128a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4129a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4130a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4131a7930ed4SKuninori Morimoto 				break;
4132a7930ed4SKuninori Morimoto 			}
4133a7930ed4SKuninori Morimoto 		}
4134a7930ed4SKuninori Morimoto 	}
4135a7930ed4SKuninori Morimoto 
4136a7930ed4SKuninori Morimoto 	/*
41378c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4138a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4139a7930ed4SKuninori Morimoto 	 */
41408c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
414151930295SJulia Lawall 	if (of_property_read_bool(np, prop))
41428c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
41438c2d6a9fSKuninori Morimoto 	else
41448c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4145a7930ed4SKuninori Morimoto 
4146a7930ed4SKuninori Morimoto 	/*
4147a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4148a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4149a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4150a7930ed4SKuninori Morimoto 	 */
4151a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4152a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4153a7930ed4SKuninori Morimoto 
4154a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4155a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4156a7930ed4SKuninori Morimoto 
4157a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4158a7930ed4SKuninori Morimoto 	case 0x11:
4159a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4160a7930ed4SKuninori Morimoto 		break;
4161a7930ed4SKuninori Morimoto 	case 0x10:
4162a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4163a7930ed4SKuninori Morimoto 		break;
4164a7930ed4SKuninori Morimoto 	case 0x01:
4165a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4166a7930ed4SKuninori Morimoto 		break;
4167a7930ed4SKuninori Morimoto 	default:
4168a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4169a7930ed4SKuninori Morimoto 		break;
4170a7930ed4SKuninori Morimoto 	}
4171a7930ed4SKuninori Morimoto 
4172a7930ed4SKuninori Morimoto 	/*
4173a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4174a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4175a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4176a7930ed4SKuninori Morimoto 	 */
4177a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4178a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4179389cb834SJyri Sarha 	if (bit && bitclkmaster)
4180389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4181a7930ed4SKuninori Morimoto 
4182a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4183a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4184389cb834SJyri Sarha 	if (frame && framemaster)
4185389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4186a7930ed4SKuninori Morimoto 
4187a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4188a7930ed4SKuninori Morimoto 	case 0x11:
4189a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4190a7930ed4SKuninori Morimoto 		break;
4191a7930ed4SKuninori Morimoto 	case 0x10:
4192a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4193a7930ed4SKuninori Morimoto 		break;
4194a7930ed4SKuninori Morimoto 	case 0x01:
4195a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4196a7930ed4SKuninori Morimoto 		break;
4197a7930ed4SKuninori Morimoto 	default:
4198a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4199a7930ed4SKuninori Morimoto 		break;
4200a7930ed4SKuninori Morimoto 	}
4201a7930ed4SKuninori Morimoto 
4202a7930ed4SKuninori Morimoto 	return format;
4203a7930ed4SKuninori Morimoto }
4204a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4205a7930ed4SKuninori Morimoto 
4206a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
4207a180e8b9SKuninori Morimoto {
4208a180e8b9SKuninori Morimoto 	struct snd_soc_component *pos;
4209a180e8b9SKuninori Morimoto 	struct device_node *node;
4210a180e8b9SKuninori Morimoto 	int ret;
4211a180e8b9SKuninori Morimoto 
4212a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
4213a180e8b9SKuninori Morimoto 
4214a180e8b9SKuninori Morimoto 	/*
4215a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
4216a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
4217a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
4218a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
4219a180e8b9SKuninori Morimoto 	 */
4220a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
4221a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
4222a180e8b9SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4223a180e8b9SKuninori Morimoto 		struct device_node *component_of_node = pos->dev->of_node;
4224a180e8b9SKuninori Morimoto 
4225a180e8b9SKuninori Morimoto 		if (!component_of_node && pos->dev->parent)
4226a180e8b9SKuninori Morimoto 			component_of_node = pos->dev->parent->of_node;
4227a180e8b9SKuninori Morimoto 
4228a180e8b9SKuninori Morimoto 		if (component_of_node != node)
4229a180e8b9SKuninori Morimoto 			continue;
4230a180e8b9SKuninori Morimoto 
4231a180e8b9SKuninori Morimoto 		if (pos->driver->of_xlate_dai_id)
4232a180e8b9SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_id(pos, ep);
4233a180e8b9SKuninori Morimoto 
4234a180e8b9SKuninori Morimoto 		break;
4235a180e8b9SKuninori Morimoto 	}
4236a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
4237a180e8b9SKuninori Morimoto 
4238c0a480d1STony Lindgren 	of_node_put(node);
4239c0a480d1STony Lindgren 
4240a180e8b9SKuninori Morimoto 	return ret;
4241a180e8b9SKuninori Morimoto }
4242a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4243a180e8b9SKuninori Morimoto 
42441ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
4245cb470087SKuninori Morimoto 				const char **dai_name)
4246cb470087SKuninori Morimoto {
4247cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
42483e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
424993b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
4250cb470087SKuninori Morimoto 
4251cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4252cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
42533e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
42543e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
42553e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
42563e0aa8d8SJyri Sarha 
42573e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
4258cb470087SKuninori Morimoto 			continue;
4259cb470087SKuninori Morimoto 
42606833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
426193b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
426293b0f3eeSJean-Francois Moine 							     args,
426393b0f3eeSJean-Francois Moine 							     dai_name);
42646833c452SKuninori Morimoto 		} else {
42656833c452SKuninori Morimoto 			int id = -1;
42666833c452SKuninori Morimoto 
426793b0f3eeSJean-Francois Moine 			switch (args->args_count) {
42686833c452SKuninori Morimoto 			case 0:
42696833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
42706833c452SKuninori Morimoto 				break;
42716833c452SKuninori Morimoto 			case 1:
427293b0f3eeSJean-Francois Moine 				id = args->args[0];
42736833c452SKuninori Morimoto 				break;
42746833c452SKuninori Morimoto 			default:
42756833c452SKuninori Morimoto 				/* not supported */
4276cb470087SKuninori Morimoto 				break;
4277cb470087SKuninori Morimoto 			}
4278cb470087SKuninori Morimoto 
42796833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
42806833c452SKuninori Morimoto 				ret = -EINVAL;
42813dcba280SNicolin Chen 				continue;
42826833c452SKuninori Morimoto 			}
4283e41975edSXiubo Li 
4284e41975edSXiubo Li 			ret = 0;
4285e41975edSXiubo Li 
4286e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4287e41975edSXiubo Li 			if (!*dai_name)
4288e41975edSXiubo Li 				*dai_name = pos->name;
42896833c452SKuninori Morimoto 		}
42906833c452SKuninori Morimoto 
4291cb470087SKuninori Morimoto 		break;
4292cb470087SKuninori Morimoto 	}
4293cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
429493b0f3eeSJean-Francois Moine 	return ret;
429593b0f3eeSJean-Francois Moine }
42961ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
429793b0f3eeSJean-Francois Moine 
429893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
429993b0f3eeSJean-Francois Moine 			    const char **dai_name)
430093b0f3eeSJean-Francois Moine {
430193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
430293b0f3eeSJean-Francois Moine 	int ret;
430393b0f3eeSJean-Francois Moine 
430493b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
430593b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
430693b0f3eeSJean-Francois Moine 	if (ret)
430793b0f3eeSJean-Francois Moine 		return ret;
430893b0f3eeSJean-Francois Moine 
430993b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
4310cb470087SKuninori Morimoto 
4311cb470087SKuninori Morimoto 	of_node_put(args.np);
4312cb470087SKuninori Morimoto 
4313cb470087SKuninori Morimoto 	return ret;
4314cb470087SKuninori Morimoto }
4315cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4316cb470087SKuninori Morimoto 
431793b0f3eeSJean-Francois Moine /*
431893b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
431993b0f3eeSJean-Francois Moine  * @dev: Card device
432093b0f3eeSJean-Francois Moine  * @of_node: Device node
432193b0f3eeSJean-Francois Moine  * @dai_link: DAI link
432293b0f3eeSJean-Francois Moine  *
432393b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
432493b0f3eeSJean-Francois Moine  * 'sound-dai'.
432593b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
432693b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
432793b0f3eeSJean-Francois Moine  *
432893b0f3eeSJean-Francois Moine  * Returns 0 for success
432993b0f3eeSJean-Francois Moine  */
433093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
433193b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
433293b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
433393b0f3eeSJean-Francois Moine {
433493b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
433593b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
433693b0f3eeSJean-Francois Moine 	char *name;
433793b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
433893b0f3eeSJean-Francois Moine 
433993b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
434093b0f3eeSJean-Francois Moine 	name = "sound-dai";
434193b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
434293b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
434393b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
434493b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
434593b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
434693b0f3eeSJean-Francois Moine 		else
434793b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
434893b0f3eeSJean-Francois Moine 		return num_codecs;
434993b0f3eeSJean-Francois Moine 	}
435093b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
435193b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
435293b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
435393b0f3eeSJean-Francois Moine 	if (!component)
435493b0f3eeSJean-Francois Moine 		return -ENOMEM;
435593b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
435693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
435793b0f3eeSJean-Francois Moine 
435893b0f3eeSJean-Francois Moine 	/* Parse the list */
435993b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
436093b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
436193b0f3eeSJean-Francois Moine 	     index++, component++) {
436293b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
436393b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
436493b0f3eeSJean-Francois Moine 						  index, &args);
436593b0f3eeSJean-Francois Moine 		if (ret)
436693b0f3eeSJean-Francois Moine 			goto err;
436793b0f3eeSJean-Francois Moine 		component->of_node = args.np;
436893b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
436993b0f3eeSJean-Francois Moine 		if (ret < 0)
437093b0f3eeSJean-Francois Moine 			goto err;
437193b0f3eeSJean-Francois Moine 	}
437293b0f3eeSJean-Francois Moine 	return 0;
437393b0f3eeSJean-Francois Moine err:
437493b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
437593b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
437693b0f3eeSJean-Francois Moine 	     index++, component++) {
437793b0f3eeSJean-Francois Moine 		if (!component->of_node)
437893b0f3eeSJean-Francois Moine 			break;
437993b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
438093b0f3eeSJean-Francois Moine 		component->of_node = NULL;
438193b0f3eeSJean-Francois Moine 	}
438293b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
438393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
438493b0f3eeSJean-Francois Moine 	return ret;
438593b0f3eeSJean-Francois Moine }
438693b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
438793b0f3eeSJean-Francois Moine 
4388c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4389db2a4165SFrank Mandarino {
43906553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
4391fb257897SMark Brown 	snd_soc_util_init();
4392fb257897SMark Brown 
4393db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4394db2a4165SFrank Mandarino }
43954abe8e16SMark Brown module_init(snd_soc_init);
4396db2a4165SFrank Mandarino 
43977d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4398db2a4165SFrank Mandarino {
4399fb257897SMark Brown 	snd_soc_util_exit();
44006553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
4401fb257897SMark Brown 
4402db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4403db2a4165SFrank Mandarino }
4404db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4405db2a4165SFrank Mandarino 
4406db2a4165SFrank Mandarino /* Module information */
4407d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4408db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4409db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4410db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4411