xref: /openbmc/linux/sound/soc/soc-core.c (revision 353c64dd19ebc99786c3578ea0444e1b5b1bb172)
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);
342*353c64ddSFabio Estevam 	struct dentry *debugfs_reg;
34381c7cfd1SLars-Peter Clausen 
344*353c64ddSFabio Estevam 	debugfs_reg = debugfs_create_file("codec_reg", 0644,
34581c7cfd1SLars-Peter Clausen 					  codec->component.debugfs_root,
3462624d5faSMark Brown 					  codec, &codec_reg_fops);
347*353c64ddSFabio 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);
4986553bf06SLars-Peter Clausen 	if (IS_ERR(snd_soc_debugfs_root) || !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 
55447c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
55547c88fffSLiam Girdwood 		const char *dai_link, int stream)
55647c88fffSLiam Girdwood {
5571a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
55847c88fffSLiam Girdwood 
5591a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
5601a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
5611a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
5621a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
56347c88fffSLiam Girdwood 	}
564f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
56547c88fffSLiam Girdwood 	return NULL;
56647c88fffSLiam Girdwood }
56747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
56847c88fffSLiam Girdwood 
5691a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
5701a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
5711a497983SMengdong Lin {
5721a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5731a497983SMengdong Lin 
5741a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
5751a497983SMengdong Lin 	if (!rtd)
5761a497983SMengdong Lin 		return NULL;
5771a497983SMengdong Lin 
5781a497983SMengdong Lin 	rtd->card = card;
5791a497983SMengdong Lin 	rtd->dai_link = dai_link;
5801a497983SMengdong Lin 	rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
5811a497983SMengdong Lin 					dai_link->num_codecs,
5821a497983SMengdong Lin 					GFP_KERNEL);
5831a497983SMengdong Lin 	if (!rtd->codec_dais) {
5841a497983SMengdong Lin 		kfree(rtd);
5851a497983SMengdong Lin 		return NULL;
5861a497983SMengdong Lin 	}
5871a497983SMengdong Lin 
5881a497983SMengdong Lin 	return rtd;
5891a497983SMengdong Lin }
5901a497983SMengdong Lin 
5911a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
5921a497983SMengdong Lin {
5931a497983SMengdong Lin 	if (rtd && rtd->codec_dais)
5941a497983SMengdong Lin 		kfree(rtd->codec_dais);
5951a497983SMengdong Lin 	kfree(rtd);
5961a497983SMengdong Lin }
5971a497983SMengdong Lin 
5981a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
5991a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
6001a497983SMengdong Lin {
6011a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
6021a497983SMengdong Lin 	rtd->num = card->num_rtd;
6031a497983SMengdong Lin 	card->num_rtd++;
6041a497983SMengdong Lin }
6051a497983SMengdong Lin 
6061a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
6071a497983SMengdong Lin {
6081a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
6091a497983SMengdong Lin 
6101a497983SMengdong Lin 	list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
6111a497983SMengdong Lin 		list_del(&rtd->list);
6121a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
6131a497983SMengdong Lin 	}
6141a497983SMengdong Lin 
6151a497983SMengdong Lin 	card->num_rtd = 0;
6161a497983SMengdong Lin }
6171a497983SMengdong Lin 
61847c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
61947c88fffSLiam Girdwood 		const char *dai_link)
62047c88fffSLiam Girdwood {
6211a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
62247c88fffSLiam Girdwood 
6231a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6241a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
6251a497983SMengdong Lin 			return rtd;
62647c88fffSLiam Girdwood 	}
627f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
62847c88fffSLiam Girdwood 	return NULL;
62947c88fffSLiam Girdwood }
63047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
63147c88fffSLiam Girdwood 
6329d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
6339d58a077SRichard Fitzgerald {
6349d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
6359d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
6369d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
6379d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
6389d58a077SRichard Fitzgerald 	 */
6399d58a077SRichard Fitzgerald }
6409d58a077SRichard Fitzgerald 
6416f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
642db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
6436f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
644db2a4165SFrank Mandarino {
6456f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
646d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6471a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6481a497983SMengdong Lin 	int i;
649db2a4165SFrank Mandarino 
650c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
651c5599b87SLars-Peter Clausen 	if (!card->instantiated)
652e3509ff0SDaniel Mack 		return 0;
653e3509ff0SDaniel Mack 
6546ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
6556ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
6566ed25978SAndy Green 	 */
657f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
658f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
659f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
6606ed25978SAndy Green 
6616ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
662f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6636ed25978SAndy Green 
664a00f90f9SMark Brown 	/* mute any active DACs */
6651a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6663efab7dcSMark Brown 
6671a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6683efab7dcSMark Brown 			continue;
6693efab7dcSMark Brown 
6701a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
6711a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
67288bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
67388bd870fSBenoit Cousson 
674f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
675f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
676db2a4165SFrank Mandarino 		}
67788bd870fSBenoit Cousson 	}
678db2a4165SFrank Mandarino 
6794ccab3e7SLiam Girdwood 	/* suspend all pcms */
6801a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6811a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6823efab7dcSMark Brown 			continue;
6833efab7dcSMark Brown 
6841a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
6853efab7dcSMark Brown 	}
6864ccab3e7SLiam Girdwood 
68787506549SMark Brown 	if (card->suspend_pre)
68870b2ac12SMark Brown 		card->suspend_pre(card);
689db2a4165SFrank Mandarino 
6901a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6911a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6923efab7dcSMark Brown 
6931a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6943efab7dcSMark Brown 			continue;
6953efab7dcSMark Brown 
696bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
697f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
698db2a4165SFrank Mandarino 	}
699db2a4165SFrank Mandarino 
70037660b6dSLars-Peter Clausen 	/* close any waiting streams */
7011a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
7021a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
703db2a4165SFrank Mandarino 
7041a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7053efab7dcSMark Brown 
7061a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7073efab7dcSMark Brown 			continue;
7083efab7dcSMark Brown 
7091a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
7107bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
711db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
712f0fba2adSLiam Girdwood 
7131a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
7147bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
715db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
716db2a4165SFrank Mandarino 	}
717db2a4165SFrank Mandarino 
7188be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7198be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
720e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
721e2d32ff6SMark Brown 
7229178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
723d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
724d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
725d9fc4063SKuninori Morimoto 
7269178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
7271547aba9SMark Brown 		 * bias _ON and should not be suspended. */
7289178feb4SKuninori Morimoto 		if (!component->suspended) {
7294890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7301547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
731125a25daSMark Brown 				/*
7329178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
733125a25daSMark Brown 				 * bias off then being in STANDBY
734125a25daSMark Brown 				 * means it's doing something,
735125a25daSMark Brown 				 * otherwise fall through.
736125a25daSMark Brown 				 */
7374890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
7389178feb4SKuninori Morimoto 					dev_dbg(component->dev,
73910e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
740125a25daSMark Brown 					break;
741125a25daSMark Brown 				}
742a8093297SLars-Peter Clausen 
7431547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
7449178feb4SKuninori Morimoto 				if (component->suspend)
7459178feb4SKuninori Morimoto 					component->suspend(component);
7469178feb4SKuninori Morimoto 				component->suspended = 1;
7479178feb4SKuninori Morimoto 				if (component->regmap)
7489178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
749988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
7509178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
7511547aba9SMark Brown 				break;
7521547aba9SMark Brown 			default:
7539178feb4SKuninori Morimoto 				dev_dbg(component->dev,
7549178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
7551547aba9SMark Brown 				break;
7561547aba9SMark Brown 			}
7571547aba9SMark Brown 		}
758f0fba2adSLiam Girdwood 	}
759db2a4165SFrank Mandarino 
7601a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7611a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7623efab7dcSMark Brown 
7631a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7643efab7dcSMark Brown 			continue;
7653efab7dcSMark Brown 
766bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
767f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
768988e8cc4SNicolin Chen 
769988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
770988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
771db2a4165SFrank Mandarino 	}
772db2a4165SFrank Mandarino 
77387506549SMark Brown 	if (card->suspend_post)
77470b2ac12SMark Brown 		card->suspend_post(card);
775db2a4165SFrank Mandarino 
776db2a4165SFrank Mandarino 	return 0;
777db2a4165SFrank Mandarino }
7786f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
779db2a4165SFrank Mandarino 
7806ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7816ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7826ed25978SAndy Green  */
7836ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
784db2a4165SFrank Mandarino {
785f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
786f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7871a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
788d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
7891a497983SMengdong Lin 	int i;
790db2a4165SFrank Mandarino 
7916ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7926ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7936ed25978SAndy Green 	 */
7946ed25978SAndy Green 
795f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7966ed25978SAndy Green 
7979949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
798f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7999949788bSMark Brown 
80087506549SMark Brown 	if (card->resume_pre)
80170b2ac12SMark Brown 		card->resume_pre(card);
802db2a4165SFrank Mandarino 
803bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
8041a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8051a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8063efab7dcSMark Brown 
8071a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8083efab7dcSMark Brown 			continue;
8093efab7dcSMark Brown 
810bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
811f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
812db2a4165SFrank Mandarino 	}
813db2a4165SFrank Mandarino 
814d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
8159178feb4SKuninori Morimoto 		if (component->suspended) {
8169178feb4SKuninori Morimoto 			if (component->resume)
8179178feb4SKuninori Morimoto 				component->resume(component);
8189178feb4SKuninori Morimoto 			component->suspended = 0;
8191547aba9SMark Brown 		}
820f0fba2adSLiam Girdwood 	}
821db2a4165SFrank Mandarino 
8221a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8233efab7dcSMark Brown 
8241a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8253efab7dcSMark Brown 			continue;
8263efab7dcSMark Brown 
8271a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
828d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
829db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
830f0fba2adSLiam Girdwood 
8311a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
832d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
833db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
834db2a4165SFrank Mandarino 	}
835db2a4165SFrank Mandarino 
8363ff3f64bSMark Brown 	/* unmute any active DACs */
8371a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8383efab7dcSMark Brown 
8391a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8403efab7dcSMark Brown 			continue;
8413efab7dcSMark Brown 
8421a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8431a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
84488bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
84588bd870fSBenoit Cousson 
846f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
847f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
848db2a4165SFrank Mandarino 		}
84988bd870fSBenoit Cousson 	}
850db2a4165SFrank Mandarino 
8511a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8521a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8533efab7dcSMark Brown 
8541a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8553efab7dcSMark Brown 			continue;
8563efab7dcSMark Brown 
857bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
858f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
859db2a4165SFrank Mandarino 	}
860db2a4165SFrank Mandarino 
86187506549SMark Brown 	if (card->resume_post)
86270b2ac12SMark Brown 		card->resume_post(card);
863db2a4165SFrank Mandarino 
864f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8656ed25978SAndy Green 
8668be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8678be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
868e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8691a7aaa58SJeeja KP 
8701a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
8711a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
8726ed25978SAndy Green }
8736ed25978SAndy Green 
8746ed25978SAndy Green /* powers up audio subsystem after a suspend */
8756f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8766ed25978SAndy Green {
8776f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
878bc263214SLars-Peter Clausen 	bool bus_control = false;
8791a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
880b9dd94a8SPeter Ujfalusi 
881c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
882c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8835ff1ddf2SEric Miao 		return 0;
8845ff1ddf2SEric Miao 
885988e8cc4SNicolin Chen 	/* activate pins from sleep state */
8861a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
88788bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
88888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
88988bd870fSBenoit Cousson 		int j;
89088bd870fSBenoit Cousson 
891988e8cc4SNicolin Chen 		if (cpu_dai->active)
892988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
89388bd870fSBenoit Cousson 
89488bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
89588bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
896988e8cc4SNicolin Chen 			if (codec_dai->active)
897988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
898988e8cc4SNicolin Chen 		}
89988bd870fSBenoit Cousson 	}
900988e8cc4SNicolin Chen 
901bc263214SLars-Peter Clausen 	/*
902bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
903bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
904bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
90564ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
90664ab9baaSMark Brown 	 */
9071a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
9081a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
909bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
91082e14e8bSStephen Warren 	}
911bc263214SLars-Peter Clausen 	if (bus_control) {
912bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
91364ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
91464ab9baaSMark Brown 	} else {
915f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
9166308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
917f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
918f0fba2adSLiam Girdwood 	}
9196ed25978SAndy Green 
920db2a4165SFrank Mandarino 	return 0;
921db2a4165SFrank Mandarino }
9226f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
923db2a4165SFrank Mandarino #else
9246f8ab4acSMark Brown #define snd_soc_suspend NULL
9256f8ab4acSMark Brown #define snd_soc_resume NULL
926db2a4165SFrank Mandarino #endif
927db2a4165SFrank Mandarino 
92885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
92902a06d30SBarry Song };
93002a06d30SBarry Song 
93165d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
93265d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
93312023a9aSMisael Lopez Cruz {
93465d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
93512023a9aSMisael Lopez Cruz 
93634e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
93734e81ab4SLars-Peter Clausen 
93865d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
93965d9361fSLars-Peter Clausen 		if (of_node) {
94065d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
94165d9361fSLars-Peter Clausen 				return component;
94265d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
94365d9361fSLars-Peter Clausen 			return component;
94412023a9aSMisael Lopez Cruz 		}
94512023a9aSMisael Lopez Cruz 	}
94612023a9aSMisael Lopez Cruz 
94712023a9aSMisael Lopez Cruz 	return NULL;
94812023a9aSMisael Lopez Cruz }
94912023a9aSMisael Lopez Cruz 
950fbb88b5cSMengdong Lin /**
951fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
952fbb88b5cSMengdong Lin  *
953fbb88b5cSMengdong Lin  * @dlc: name of the DAI and optional component info to match
954fbb88b5cSMengdong Lin  *
955ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
956fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
957fbb88b5cSMengdong Lin  * should also match if being specified.
958fbb88b5cSMengdong Lin  *
959fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
960fbb88b5cSMengdong Lin  */
961305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
96214621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
96312023a9aSMisael Lopez Cruz {
96414621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
96514621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
9663e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
96712023a9aSMisael Lopez Cruz 
96834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
96934e81ab4SLars-Peter Clausen 
97014621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
97114621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
9723e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
9733e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
9743e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
9753e0aa8d8SJyri Sarha 
9763e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
97712023a9aSMisael Lopez Cruz 			continue;
9781ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
97912023a9aSMisael Lopez Cruz 			continue;
98014621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
98114621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
98214621c7eSLars-Peter Clausen 				continue;
98312023a9aSMisael Lopez Cruz 
98414621c7eSLars-Peter Clausen 			return dai;
98512023a9aSMisael Lopez Cruz 		}
98612023a9aSMisael Lopez Cruz 	}
98712023a9aSMisael Lopez Cruz 
98812023a9aSMisael Lopez Cruz 	return NULL;
98912023a9aSMisael Lopez Cruz }
990305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
99112023a9aSMisael Lopez Cruz 
99217fb1755SMengdong Lin 
99317fb1755SMengdong Lin /**
99417fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
99517fb1755SMengdong Lin  *
99617fb1755SMengdong Lin  * @card: soc card
99717fb1755SMengdong Lin  * @id: DAI link ID to match
99817fb1755SMengdong Lin  * @name: DAI link name to match, optional
9998abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
100017fb1755SMengdong Lin  *
100117fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
100217fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
100317fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
100417fb1755SMengdong Lin  * specified.
100517fb1755SMengdong Lin  *
100617fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
100717fb1755SMengdong Lin  */
100817fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
100917fb1755SMengdong Lin 					       int id, const char *name,
101017fb1755SMengdong Lin 					       const char *stream_name)
101117fb1755SMengdong Lin {
101217fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
101317fb1755SMengdong Lin 
101417fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
101517fb1755SMengdong Lin 
101617fb1755SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
101717fb1755SMengdong Lin 		if (link->id != id)
101817fb1755SMengdong Lin 			continue;
101917fb1755SMengdong Lin 
102017fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
102117fb1755SMengdong Lin 			continue;
102217fb1755SMengdong Lin 
102317fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
102417fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
102517fb1755SMengdong Lin 			continue;
102617fb1755SMengdong Lin 
102717fb1755SMengdong Lin 		return link;
102817fb1755SMengdong Lin 	}
102917fb1755SMengdong Lin 
103017fb1755SMengdong Lin 	return NULL;
103117fb1755SMengdong Lin }
103217fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
103317fb1755SMengdong Lin 
103449a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
103549a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
1036db2a4165SFrank Mandarino {
103749a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
103849a5ba1cSMengdong Lin 
103949a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
104049a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
104149a5ba1cSMengdong Lin 			return true;
104249a5ba1cSMengdong Lin 	}
104349a5ba1cSMengdong Lin 
104449a5ba1cSMengdong Lin 	return false;
104549a5ba1cSMengdong Lin }
104649a5ba1cSMengdong Lin 
10476f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10486f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1049db2a4165SFrank Mandarino {
10501a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
105188bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
105214621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
10531a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
1054435c5e25SMark Brown 	struct snd_soc_platform *platform;
105506859fcaSCharles Keepax 	struct device_node *platform_of_node;
1056848dd8beSMark Brown 	const char *platform_name;
105788bd870fSBenoit Cousson 	int i;
1058db2a4165SFrank Mandarino 
10596f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10606308419aSMark Brown 
106149a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
106249a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
106349a5ba1cSMengdong Lin 			dai_link->name);
106449a5ba1cSMengdong Lin 		return 0;
106549a5ba1cSMengdong Lin 	}
1066db2a4165SFrank Mandarino 
1067513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1068513cb311SSudip Mukherjee 	if (!rtd)
1069513cb311SSudip Mukherjee 		return -ENOMEM;
1070513cb311SSudip Mukherjee 
107114621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
107214621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
107314621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
107414621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1075b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1076f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1077f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
10781a497983SMengdong Lin 		goto _err_defer;
1079c5af3a2eSMark Brown 	}
1080c5af3a2eSMark Brown 
108188bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
108288bd870fSBenoit Cousson 
108388bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
10841a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
108588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
108614621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
108788bd870fSBenoit Cousson 		if (!codec_dais[i]) {
108812023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
108988bd870fSBenoit Cousson 				codecs[i].dai_name);
10901a497983SMengdong Lin 			goto _err_defer;
109112023a9aSMisael Lopez Cruz 		}
109288bd870fSBenoit Cousson 	}
109388bd870fSBenoit Cousson 
109488bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
109588bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
109688bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
109712023a9aSMisael Lopez Cruz 
1098848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1099848dd8beSMark Brown 	platform_name = dai_link->platform_name;
11005a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1101848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1102848dd8beSMark Brown 
1103b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
1104f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
110506859fcaSCharles Keepax 		platform_of_node = platform->dev->of_node;
110606859fcaSCharles Keepax 		if (!platform_of_node && platform->dev->parent->of_node)
110706859fcaSCharles Keepax 			platform_of_node = platform->dev->parent->of_node;
110806859fcaSCharles Keepax 
11095a504963SStephen Warren 		if (dai_link->platform_of_node) {
111006859fcaSCharles Keepax 			if (platform_of_node != dai_link->platform_of_node)
11115a504963SStephen Warren 				continue;
11125a504963SStephen Warren 		} else {
1113f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
11142610ab77SStephen Warren 				continue;
11155a504963SStephen Warren 		}
11162610ab77SStephen Warren 
1117f0fba2adSLiam Girdwood 		rtd->platform = platform;
1118f0fba2adSLiam Girdwood 	}
1119b19e6e7bSMark Brown 	if (!rtd->platform) {
1120f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1121f0fba2adSLiam Girdwood 			dai_link->platform_name);
112270fcad49SCharles Keepax 		goto _err_defer;
1123f0fba2adSLiam Girdwood 	}
1124b19e6e7bSMark Brown 
11251a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1126b19e6e7bSMark Brown 	return 0;
11271a497983SMengdong Lin 
11281a497983SMengdong Lin _err_defer:
11291a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11301a497983SMengdong Lin 	return  -EPROBE_DEFER;
11316b05eda6SMark Brown }
11326b05eda6SMark Brown 
1133f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1134d12cd198SStephen Warren {
1135abd31b32SLars-Peter Clausen 	if (!component->card)
113670090bbbSLars-Peter Clausen 		return;
1137d12cd198SStephen Warren 
1138d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1139d12cd198SStephen Warren 
1140f1d45cc3SLars-Peter Clausen 	if (component->remove)
1141f1d45cc3SLars-Peter Clausen 		component->remove(component);
1142d12cd198SStephen Warren 
1143f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1144d12cd198SStephen Warren 
1145f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1146abd31b32SLars-Peter Clausen 	component->card = NULL;
1147f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1148d12cd198SStephen Warren }
1149d12cd198SStephen Warren 
1150e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1151589c3563SJarkko Nikula {
1152589c3563SJarkko Nikula 	int err;
1153589c3563SJarkko Nikula 
1154e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1155e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1156e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1157e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1158589c3563SJarkko Nikula 			if (err < 0)
1159e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1160b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1161e60cd14fSLars-Peter Clausen 					dai->name, err);
1162b0aa88afSMisael Lopez Cruz 		}
1163e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1164b0aa88afSMisael Lopez Cruz 	}
1165b0aa88afSMisael Lopez Cruz }
1166b0aa88afSMisael Lopez Cruz 
11671a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11681a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1169f0fba2adSLiam Girdwood {
1170e60cd14fSLars-Peter Clausen 	int i;
1171f0fba2adSLiam Girdwood 
1172f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1173f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
117436ae1a96SMark Brown 		device_unregister(rtd->dev);
1175f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
117602a06d30SBarry Song 	}
117702a06d30SBarry Song 
1178f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
117988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1180e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1181f0fba2adSLiam Girdwood 
1182e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1183f0fba2adSLiam Girdwood }
1184f0fba2adSLiam Girdwood 
11851a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11861a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
118762ae68faSStephen Warren {
118862ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
118962ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
119061aca564SLars-Peter Clausen 	struct snd_soc_component *component;
119188bd870fSBenoit Cousson 	int i;
119262ae68faSStephen Warren 
119362ae68faSStephen Warren 	/* remove the platform */
119470090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1195f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
119662ae68faSStephen Warren 
119762ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
119888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
119961aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
120070090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
120161aca564SLars-Peter Clausen 			soc_remove_component(component);
120262ae68faSStephen Warren 	}
120362ae68faSStephen Warren 
120462ae68faSStephen Warren 	/* remove any CPU-side CODEC */
120562ae68faSStephen Warren 	if (cpu_dai) {
120670090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
120761aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
120862ae68faSStephen Warren 	}
120962ae68faSStephen Warren }
121062ae68faSStephen Warren 
12110671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
12120671fd8eSKuninori Morimoto {
12131a497983SMengdong Lin 	int order;
12141a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1215f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
12160671fd8eSKuninori Morimoto 
12170168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
12180168bf0dSLiam Girdwood 			order++) {
12191a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12201a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
12210168bf0dSLiam Girdwood 	}
122262ae68faSStephen Warren 
122362ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
122462ae68faSStephen Warren 			order++) {
12251a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12261a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
122762ae68faSStephen Warren 	}
122862ae68faSStephen Warren 
1229f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1230f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1231f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1232f8f80361SMengdong Lin 				link->name);
1233f8f80361SMengdong Lin 
1234f8f80361SMengdong Lin 		list_del(&link->list);
1235f8f80361SMengdong Lin 		card->num_dai_links--;
12360671fd8eSKuninori Morimoto 	}
12370671fd8eSKuninori Morimoto }
12380671fd8eSKuninori Morimoto 
1239923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1240923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1241923c5e61SMengdong Lin {
1242923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1243923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1244923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1245923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1246923c5e61SMengdong Lin 
1247923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1248923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1249923c5e61SMengdong Lin 				GFP_KERNEL);
1250923c5e61SMengdong Lin 		if (!dai_link->codecs)
1251923c5e61SMengdong Lin 			return -ENOMEM;
1252923c5e61SMengdong Lin 
1253923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1254923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1255923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1256923c5e61SMengdong Lin 	}
1257923c5e61SMengdong Lin 
1258923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1259923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1260923c5e61SMengdong Lin 		return -EINVAL;
1261923c5e61SMengdong Lin 	}
1262923c5e61SMengdong Lin 
1263923c5e61SMengdong Lin 	return 0;
1264923c5e61SMengdong Lin }
1265923c5e61SMengdong Lin 
1266923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1267923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1268923c5e61SMengdong Lin {
1269923c5e61SMengdong Lin 	int i, ret;
1270923c5e61SMengdong Lin 
1271923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1272923c5e61SMengdong Lin 	if (ret) {
1273923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1274923c5e61SMengdong Lin 		return ret;
1275923c5e61SMengdong Lin 	}
1276923c5e61SMengdong Lin 
1277923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1278923c5e61SMengdong Lin 		/*
1279923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1280923c5e61SMengdong Lin 		 * not both or neither.
1281923c5e61SMengdong Lin 		 */
1282923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1283923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1284923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1285923c5e61SMengdong Lin 				link->name);
1286923c5e61SMengdong Lin 			return -EINVAL;
1287923c5e61SMengdong Lin 		}
1288923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1289923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1290923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1291923c5e61SMengdong Lin 				link->name);
1292923c5e61SMengdong Lin 			return -EINVAL;
1293923c5e61SMengdong Lin 		}
1294923c5e61SMengdong Lin 	}
1295923c5e61SMengdong Lin 
1296923c5e61SMengdong Lin 	/*
1297923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1298923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1299923c5e61SMengdong Lin 	 */
1300923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1301923c5e61SMengdong Lin 		dev_err(card->dev,
1302923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1303923c5e61SMengdong Lin 			link->name);
1304923c5e61SMengdong Lin 		return -EINVAL;
1305923c5e61SMengdong Lin 	}
1306923c5e61SMengdong Lin 
1307923c5e61SMengdong Lin 	/*
1308923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1309923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1310923c5e61SMengdong Lin 	 * name alone..
1311923c5e61SMengdong Lin 	 */
1312923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1313923c5e61SMengdong Lin 		dev_err(card->dev,
1314923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1315923c5e61SMengdong Lin 			link->name);
1316923c5e61SMengdong Lin 		return -EINVAL;
1317923c5e61SMengdong Lin 	}
1318923c5e61SMengdong Lin 	/*
1319923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1320923c5e61SMengdong Lin 	 * specified
1321923c5e61SMengdong Lin 	 */
1322923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1323923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1324923c5e61SMengdong Lin 		dev_err(card->dev,
1325923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1326923c5e61SMengdong Lin 			link->name);
1327923c5e61SMengdong Lin 		return -EINVAL;
1328923c5e61SMengdong Lin 	}
1329923c5e61SMengdong Lin 
1330923c5e61SMengdong Lin 	return 0;
1331923c5e61SMengdong Lin }
1332923c5e61SMengdong Lin 
1333f8f80361SMengdong Lin /**
1334f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1335f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1336f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1337f8f80361SMengdong Lin  *
1338f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1339f8f80361SMengdong Lin  *
1340f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1341f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1342f8f80361SMengdong Lin  * DAI links in dai_link array.
1343f8f80361SMengdong Lin  */
1344f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1345f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1346f8f80361SMengdong Lin {
1347f8f80361SMengdong Lin 	if (dai_link->dobj.type
1348f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1349f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1350f8f80361SMengdong Lin 			dai_link->dobj.type);
1351f8f80361SMengdong Lin 		return -EINVAL;
1352f8f80361SMengdong Lin 	}
1353f8f80361SMengdong Lin 
1354f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1355d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1356d6f220eaSMengdong Lin 	 * on the link created by topology.
1357d6f220eaSMengdong Lin 	 */
1358d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1359d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1360d6f220eaSMengdong Lin 
1361f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1362f8f80361SMengdong Lin 	card->num_dai_links++;
1363f8f80361SMengdong Lin 
1364f8f80361SMengdong Lin 	return 0;
1365f8f80361SMengdong Lin }
1366f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1367f8f80361SMengdong Lin 
1368f8f80361SMengdong Lin /**
1369f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1370f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1371f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1372f8f80361SMengdong Lin  *
1373f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1374f8f80361SMengdong Lin  *
1375f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1376f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1377f8f80361SMengdong Lin  */
1378f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1379f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1380f8f80361SMengdong Lin {
1381f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1382f8f80361SMengdong Lin 
1383f8f80361SMengdong Lin 	if (dai_link->dobj.type
1384f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1385f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1386f8f80361SMengdong Lin 			dai_link->dobj.type);
1387f8f80361SMengdong Lin 		return;
1388f8f80361SMengdong Lin 	}
1389f8f80361SMengdong Lin 
1390f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1391d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1392d6f220eaSMengdong Lin 	 * on the link created by topology.
1393d6f220eaSMengdong Lin 	 */
1394d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1395d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1396d6f220eaSMengdong Lin 
1397f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1398f8f80361SMengdong Lin 		if (link == dai_link) {
1399f8f80361SMengdong Lin 			list_del(&link->list);
1400f8f80361SMengdong Lin 			card->num_dai_links--;
1401f8f80361SMengdong Lin 			return;
1402f8f80361SMengdong Lin 		}
1403f8f80361SMengdong Lin 	}
1404f8f80361SMengdong Lin }
1405f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1406f0fba2adSLiam Girdwood 
1407ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
140894f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1409ead9b919SJarkko Nikula {
1410ead9b919SJarkko Nikula 	int i;
1411ead9b919SJarkko Nikula 
1412ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1413ead9b919SJarkko Nikula 		return;
1414ead9b919SJarkko Nikula 
1415ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1416ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
141794f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
14183ca041edSSebastian Reichel 			continue;
141994f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
14203ca041edSSebastian Reichel 			continue;
142194f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1422ead9b919SJarkko Nikula 		break;
1423ead9b919SJarkko Nikula 	}
1424ead9b919SJarkko Nikula }
1425ead9b919SJarkko Nikula 
1426f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1427f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1428589c3563SJarkko Nikula {
1429f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1430888df395SMark Brown 	struct snd_soc_dai *dai;
1431f1d45cc3SLars-Peter Clausen 	int ret;
1432589c3563SJarkko Nikula 
14331b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
143470090bbbSLars-Peter Clausen 		return 0;
1435589c3563SJarkko Nikula 
1436abd31b32SLars-Peter Clausen 	if (component->card) {
14371b7c1231SLars-Peter Clausen 		if (component->card != card) {
14381b7c1231SLars-Peter Clausen 			dev_err(component->dev,
14391b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
14401b7c1231SLars-Peter Clausen 				card->name, component->card->name);
14411b7c1231SLars-Peter Clausen 			return -ENODEV;
14421b7c1231SLars-Peter Clausen 		}
14431b7c1231SLars-Peter Clausen 		return 0;
14441b7c1231SLars-Peter Clausen 	}
14451b7c1231SLars-Peter Clausen 
1446abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1447abd31b32SLars-Peter Clausen 		return -ENODEV;
1448abd31b32SLars-Peter Clausen 
1449f1d45cc3SLars-Peter Clausen 	component->card = card;
1450f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1451f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1452589c3563SJarkko Nikula 
1453f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1454d5d1e0beSLars-Peter Clausen 
1455f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1456f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1457f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
145877530150SLars-Peter Clausen 
1459b318ad50SNariman Poushin 		if (ret != 0) {
1460f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1461b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1462b318ad50SNariman Poushin 			goto err_probe;
1463b318ad50SNariman Poushin 		}
1464b318ad50SNariman Poushin 	}
1465b318ad50SNariman Poushin 
14660634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
14670634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1468261edc70SNariman Poushin 		if (ret != 0) {
1469f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1470261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1471261edc70SNariman Poushin 			goto err_probe;
1472261edc70SNariman Poushin 		}
1473261edc70SNariman Poushin 	}
1474888df395SMark Brown 
1475f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1476f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1477589c3563SJarkko Nikula 		if (ret < 0) {
1478f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1479f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
148070d29331SJarkko Nikula 			goto err_probe;
1481589c3563SJarkko Nikula 		}
1482cb2cf612SLiam Girdwood 
1483f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1484f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1485956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1486f1d45cc3SLars-Peter Clausen 			component->name);
1487956245e9SLiam Girdwood 	}
1488956245e9SLiam Girdwood 
1489f2ed6b07SMengdong Lin 	/* machine specific init */
1490f2ed6b07SMengdong Lin 	if (component->init) {
1491f2ed6b07SMengdong Lin 		ret = component->init(component);
1492f2ed6b07SMengdong Lin 		if (ret < 0) {
1493f2ed6b07SMengdong Lin 			dev_err(component->dev,
1494f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1495f2ed6b07SMengdong Lin 			goto err_probe;
1496f2ed6b07SMengdong Lin 		}
1497f2ed6b07SMengdong Lin 	}
1498f2ed6b07SMengdong Lin 
1499f1d45cc3SLars-Peter Clausen 	if (component->controls)
1500f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1501f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1502f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1503f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1504f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1505956245e9SLiam Girdwood 
1506f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1507d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1508956245e9SLiam Girdwood 
1509956245e9SLiam Girdwood 	return 0;
1510956245e9SLiam Girdwood 
1511956245e9SLiam Girdwood err_probe:
1512f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1513abd31b32SLars-Peter Clausen 	component->card = NULL;
1514f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1515956245e9SLiam Girdwood 
1516956245e9SLiam Girdwood 	return ret;
1517956245e9SLiam Girdwood }
1518956245e9SLiam Girdwood 
151936ae1a96SMark Brown static void rtd_release(struct device *dev)
152036ae1a96SMark Brown {
152136ae1a96SMark Brown 	kfree(dev);
152236ae1a96SMark Brown }
1523f0fba2adSLiam Girdwood 
15245f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
15255f3484acSLars-Peter Clausen 	const char *name)
1526503ae5e0SMisael Lopez Cruz {
1527589c3563SJarkko Nikula 	int ret = 0;
1528589c3563SJarkko Nikula 
1529589c3563SJarkko Nikula 	/* register the rtd device */
153036ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
153136ae1a96SMark Brown 	if (!rtd->dev)
153236ae1a96SMark Brown 		return -ENOMEM;
153336ae1a96SMark Brown 	device_initialize(rtd->dev);
15345f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
153536ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1536d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1537f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
153836ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1539b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
154001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
154101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
154201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
154301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
154436ae1a96SMark Brown 	ret = device_add(rtd->dev);
1545589c3563SJarkko Nikula 	if (ret < 0) {
1546865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1547865df9cbSChuansheng Liu 		put_device(rtd->dev);
15485f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1549f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1550589c3563SJarkko Nikula 		return ret;
1551589c3563SJarkko Nikula 	}
1552589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1553589c3563SJarkko Nikula 	return 0;
1554589c3563SJarkko Nikula }
1555589c3563SJarkko Nikula 
15561a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
15571a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
155862ae68faSStephen Warren 				     int order)
155962ae68faSStephen Warren {
156062ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1561f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
156288bd870fSBenoit Cousson 	int i, ret;
156362ae68faSStephen Warren 
156462ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
156561aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
156670090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1567f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
156862ae68faSStephen Warren 		if (ret < 0)
156962ae68faSStephen Warren 			return ret;
157062ae68faSStephen Warren 	}
157162ae68faSStephen Warren 
157288bd870fSBenoit Cousson 	/* probe the CODEC-side components */
157388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
157461aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
157570090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1576f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
157762ae68faSStephen Warren 			if (ret < 0)
157862ae68faSStephen Warren 				return ret;
157962ae68faSStephen Warren 		}
158088bd870fSBenoit Cousson 	}
158162ae68faSStephen Warren 
158262ae68faSStephen Warren 	/* probe the platform */
158370090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1584f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
158562ae68faSStephen Warren 		if (ret < 0)
158662ae68faSStephen Warren 			return ret;
158762ae68faSStephen Warren 	}
158862ae68faSStephen Warren 
158962ae68faSStephen Warren 	return 0;
159062ae68faSStephen Warren }
159162ae68faSStephen Warren 
15928e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1593b0aa88afSMisael Lopez Cruz {
1594b0aa88afSMisael Lopez Cruz 	int ret;
1595b0aa88afSMisael Lopez Cruz 
15968e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
15978e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
15988e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1599b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
16008e2be562SLars-Peter Clausen 				dev_err(dai->dev,
16018e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
16028e2be562SLars-Peter Clausen 					dai->name, ret);
1603b0aa88afSMisael Lopez Cruz 				return ret;
1604b0aa88afSMisael Lopez Cruz 			}
1605b0aa88afSMisael Lopez Cruz 		}
1606b0aa88afSMisael Lopez Cruz 
16078e2be562SLars-Peter Clausen 		dai->probed = 1;
1608b0aa88afSMisael Lopez Cruz 	}
1609b0aa88afSMisael Lopez Cruz 
1610b0aa88afSMisael Lopez Cruz 	return 0;
1611b0aa88afSMisael Lopez Cruz }
1612b0aa88afSMisael Lopez Cruz 
161325f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
161425f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
161525f7b701SArnaud Pouliquen {
161625f7b701SArnaud Pouliquen 	int i, ret = 0;
161725f7b701SArnaud Pouliquen 
161825f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
161925f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
162025f7b701SArnaud Pouliquen 
162125f7b701SArnaud Pouliquen 		if (!rtd->dai_link->no_pcm && drv->pcm_new)
162225f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
162325f7b701SArnaud Pouliquen 		if (ret < 0) {
162425f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
162525f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
162625f7b701SArnaud Pouliquen 				dais[i]->name);
162725f7b701SArnaud Pouliquen 			return ret;
162825f7b701SArnaud Pouliquen 		}
162925f7b701SArnaud Pouliquen 	}
163025f7b701SArnaud Pouliquen 
163125f7b701SArnaud Pouliquen 	return 0;
163225f7b701SArnaud Pouliquen }
163325f7b701SArnaud Pouliquen 
16342436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
16352436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
16363f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
16372436a723SMisael Lopez Cruz {
16383f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16393f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
16403de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
16412436a723SMisael Lopez Cruz 	int ret;
16422436a723SMisael Lopez Cruz 
164388bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
164488bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
164588bd870fSBenoit Cousson 
16462436a723SMisael Lopez Cruz 	/* link the DAI widgets */
16473de7c420SVinod Koul 	sink = codec_dai->playback_widget;
16483de7c420SVinod Koul 	source = cpu_dai->capture_widget;
16493de7c420SVinod Koul 	if (sink && source) {
16502436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16513de7c420SVinod Koul 					   dai_link->num_params,
16523de7c420SVinod Koul 					   source, sink);
16532436a723SMisael Lopez Cruz 		if (ret != 0) {
16542436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16553de7c420SVinod Koul 				sink->name, source->name, ret);
16562436a723SMisael Lopez Cruz 			return ret;
16572436a723SMisael Lopez Cruz 		}
16582436a723SMisael Lopez Cruz 	}
16592436a723SMisael Lopez Cruz 
16603de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
16613de7c420SVinod Koul 	source = codec_dai->capture_widget;
16623de7c420SVinod Koul 	if (sink && source) {
16632436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16643de7c420SVinod Koul 					   dai_link->num_params,
16653de7c420SVinod Koul 					   source, sink);
16662436a723SMisael Lopez Cruz 		if (ret != 0) {
16672436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16683de7c420SVinod Koul 				sink->name, source->name, ret);
16692436a723SMisael Lopez Cruz 			return ret;
16702436a723SMisael Lopez Cruz 		}
16712436a723SMisael Lopez Cruz 	}
16722436a723SMisael Lopez Cruz 
16732436a723SMisael Lopez Cruz 	return 0;
16742436a723SMisael Lopez Cruz }
16752436a723SMisael Lopez Cruz 
16761a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
16771a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1678f0fba2adSLiam Girdwood {
16791a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1680c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
168188bd870fSBenoit Cousson 	int i, ret;
1682f0fba2adSLiam Girdwood 
1683f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
16841a497983SMengdong Lin 			card->name, rtd->num, order);
1685f0fba2adSLiam Girdwood 
1686f0fba2adSLiam Girdwood 	/* set default power off timeout */
1687f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1688f0fba2adSLiam Girdwood 
16898e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
16908e2be562SLars-Peter Clausen 	if (ret)
1691f0fba2adSLiam Girdwood 		return ret;
1692f0fba2adSLiam Girdwood 
1693f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
169488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
16958e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1696b0aa88afSMisael Lopez Cruz 		if (ret)
1697f0fba2adSLiam Girdwood 			return ret;
169888bd870fSBenoit Cousson 	}
1699f0fba2adSLiam Girdwood 
17000168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
17010168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
17020168bf0dSLiam Girdwood 		return 0;
17030168bf0dSLiam Girdwood 
17045f3484acSLars-Peter Clausen 	/* do machine specific initialization */
17055f3484acSLars-Peter Clausen 	if (dai_link->init) {
17065f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
17075f3484acSLars-Peter Clausen 		if (ret < 0) {
17085f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
17095f3484acSLars-Peter Clausen 				dai_link->name, ret);
17105f3484acSLars-Peter Clausen 			return ret;
17115f3484acSLars-Peter Clausen 		}
17125f3484acSLars-Peter Clausen 	}
17135f3484acSLars-Peter Clausen 
1714a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1715a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1716a5053a8eSKuninori Morimoto 
17175f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1718589c3563SJarkko Nikula 	if (ret)
1719f0fba2adSLiam Girdwood 		return ret;
1720f0fba2adSLiam Girdwood 
17215f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
17225f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
17232e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
17242e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
17255f3484acSLars-Peter Clausen #endif
17265f3484acSLars-Peter Clausen 
17276f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
17281245b700SNamarta Kohli 		/*create compress_device"*/
17291a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
17301245b700SNamarta Kohli 		if (ret < 0) {
1731f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
17321245b700SNamarta Kohli 					 dai_link->stream_name);
17331245b700SNamarta Kohli 			return ret;
17341245b700SNamarta Kohli 		}
17351245b700SNamarta Kohli 	} else {
17361245b700SNamarta Kohli 
1737c74184edSMark Brown 		if (!dai_link->params) {
1738f0fba2adSLiam Girdwood 			/* create the pcm */
17391a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1740f0fba2adSLiam Girdwood 			if (ret < 0) {
1741f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
17420837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1743f0fba2adSLiam Girdwood 				return ret;
1744f0fba2adSLiam Girdwood 			}
174525f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
174625f7b701SArnaud Pouliquen 			if (ret < 0)
174725f7b701SArnaud Pouliquen 				return ret;
174825f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(rtd->codec_dais,
174925f7b701SArnaud Pouliquen 						   rtd->num_codecs, rtd);
175025f7b701SArnaud Pouliquen 			if (ret < 0)
175125f7b701SArnaud Pouliquen 				return ret;
1752c74184edSMark Brown 		} else {
17539d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
17549d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
17559d58a077SRichard Fitzgerald 
1756c74184edSMark Brown 			/* link the DAI widgets */
17573f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
17582436a723SMisael Lopez Cruz 			if (ret)
1759c74184edSMark Brown 				return ret;
1760c74184edSMark Brown 		}
1761c74184edSMark Brown 	}
1762c74184edSMark Brown 
1763f0fba2adSLiam Girdwood 	return 0;
1764f0fba2adSLiam Girdwood }
1765f0fba2adSLiam Girdwood 
176644c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1767b19e6e7bSMark Brown {
17683ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1769f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1770f2ed6b07SMengdong Lin 	const char *name;
1771f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
17723ca041edSSebastian Reichel 
1773f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1774f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1775f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1776f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1777f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1778f2ed6b07SMengdong Lin 		if (!component) {
1779f2ed6b07SMengdong Lin 			if (codec_of_node)
1780f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1781f2ed6b07SMengdong Lin 			goto err_defer;
1782f2ed6b07SMengdong Lin 		}
1783f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1784f2ed6b07SMengdong Lin 		/* generic components */
1785f2ed6b07SMengdong Lin 		name = aux_dev->name;
1786f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1787f2ed6b07SMengdong Lin 		if (!component)
1788f2ed6b07SMengdong Lin 			goto err_defer;
1789f2ed6b07SMengdong Lin 	} else {
1790f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1791f2ed6b07SMengdong Lin 		return -EINVAL;
1792f2ed6b07SMengdong Lin 	}
17933ca041edSSebastian Reichel 
1794f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1795d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
17961a653aa4SKuninori Morimoto 
1797f2ed6b07SMengdong Lin 	return 0;
1798f2ed6b07SMengdong Lin 
1799f2ed6b07SMengdong Lin err_defer:
180065d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1801b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1802b19e6e7bSMark Brown }
1803b19e6e7bSMark Brown 
1804f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1805f2ed6b07SMengdong Lin {
1806991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1807f2ed6b07SMengdong Lin 	int order;
1808f2ed6b07SMengdong Lin 	int ret;
1809f2ed6b07SMengdong Lin 
1810f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1811f2ed6b07SMengdong Lin 		order++) {
1812991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1813f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1814f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1815f2ed6b07SMengdong Lin 				if (ret < 0) {
1816f2ed6b07SMengdong Lin 					dev_err(card->dev,
1817f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1818f2ed6b07SMengdong Lin 						comp->name, ret);
1819f2ed6b07SMengdong Lin 					return ret;
1820f2ed6b07SMengdong Lin 				}
1821f2ed6b07SMengdong Lin 			}
1822f2ed6b07SMengdong Lin 		}
1823f2ed6b07SMengdong Lin 	}
182465d9361fSLars-Peter Clausen 
182544c69bb1SLars-Peter Clausen 	return 0;
18263ca041edSSebastian Reichel }
18272eea392dSJarkko Nikula 
1828f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
182944c69bb1SLars-Peter Clausen {
1830f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1831f2ed6b07SMengdong Lin 	int order;
183244c69bb1SLars-Peter Clausen 
1833f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1834f2ed6b07SMengdong Lin 		order++) {
1835f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1836991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
18371a653aa4SKuninori Morimoto 
1838f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1839f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1840991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1841991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
18422eea392dSJarkko Nikula 			}
18435f3484acSLars-Peter Clausen 		}
18442eea392dSJarkko Nikula 	}
18452eea392dSJarkko Nikula }
18462eea392dSJarkko Nikula 
1847f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1848fdf0f54dSDimitris Papastamos {
1849fdf0f54dSDimitris Papastamos 	int ret;
1850fdf0f54dSDimitris Papastamos 
1851fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1852fdf0f54dSDimitris Papastamos 		return 0;
1853fdf0f54dSDimitris Papastamos 
1854fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1855fdf0f54dSDimitris Papastamos 	if (ret < 0) {
185610e8aa9aSMichał Mirosław 		dev_err(codec->dev,
185710e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
185810e8aa9aSMichał Mirosław 			ret);
1859fdf0f54dSDimitris Papastamos 		return ret;
1860fdf0f54dSDimitris Papastamos 	}
1861fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1862fdf0f54dSDimitris Papastamos 	return 0;
1863fdf0f54dSDimitris Papastamos }
1864fdf0f54dSDimitris Papastamos 
1865ce64c8b9SLars-Peter Clausen /**
1866ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1867ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1868ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1869ce64c8b9SLars-Peter Clausen  *
1870ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1871ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1872ce64c8b9SLars-Peter Clausen  *
1873ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1874ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1875ce64c8b9SLars-Peter Clausen  *
1876ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1877ce64c8b9SLars-Peter Clausen  */
1878ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1879ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1880ce64c8b9SLars-Peter Clausen {
1881ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1882ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1883ce64c8b9SLars-Peter Clausen 	unsigned int i;
1884ce64c8b9SLars-Peter Clausen 	int ret;
1885ce64c8b9SLars-Peter Clausen 
1886ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1887ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1888ce64c8b9SLars-Peter Clausen 
1889ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1890ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1891ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1892ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1893ce64c8b9SLars-Peter Clausen 			return ret;
1894ce64c8b9SLars-Peter Clausen 		}
1895ce64c8b9SLars-Peter Clausen 	}
1896ce64c8b9SLars-Peter Clausen 
1897ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1898ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1899ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1900ce64c8b9SLars-Peter Clausen 
1901ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1902ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1903ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1904ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1905ce64c8b9SLars-Peter Clausen 			break;
1906ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1907ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1908ce64c8b9SLars-Peter Clausen 			break;
1909ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1910ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1911ce64c8b9SLars-Peter Clausen 			break;
1912ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1913ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1914ce64c8b9SLars-Peter Clausen 			break;
1915ce64c8b9SLars-Peter Clausen 		}
1916ce64c8b9SLars-Peter Clausen 
1917ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1918ce64c8b9SLars-Peter Clausen 	}
1919ce64c8b9SLars-Peter Clausen 
1920ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1921ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1922ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1923ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1924ce64c8b9SLars-Peter Clausen 		return ret;
1925ce64c8b9SLars-Peter Clausen 	}
1926ce64c8b9SLars-Peter Clausen 
1927ce64c8b9SLars-Peter Clausen 	return 0;
1928ce64c8b9SLars-Peter Clausen }
1929ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1930ce64c8b9SLars-Peter Clausen 
1931345233d7SLiam Girdwood 
19321f5a4535STakashi Iwai #ifdef CONFIG_DMI
1933345233d7SLiam Girdwood /* Trim special characters, and replace '-' with '_' since '-' is used to
1934345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1935345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1936345233d7SLiam Girdwood  */
1937345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1938345233d7SLiam Girdwood {
1939345233d7SLiam Girdwood 	int i, j = 0;
1940345233d7SLiam Girdwood 
1941345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1942345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1943345233d7SLiam Girdwood 		    || (name[i] == '_'))
1944345233d7SLiam Girdwood 			name[j++] = name[i];
1945345233d7SLiam Girdwood 		else if (name[i] == '-')
1946345233d7SLiam Girdwood 			name[j++] = '_';
1947345233d7SLiam Girdwood 	}
1948345233d7SLiam Girdwood 
1949345233d7SLiam Girdwood 	name[j] = '\0';
1950345233d7SLiam Girdwood }
1951345233d7SLiam Girdwood 
195298faf436SMengdong Lin /* Check if a DMI field is valid, i.e. not containing any string
195398faf436SMengdong Lin  * in the black list.
195498faf436SMengdong Lin  */
195598faf436SMengdong Lin static int is_dmi_valid(const char *field)
195698faf436SMengdong Lin {
195798faf436SMengdong Lin 	int i = 0;
195898faf436SMengdong Lin 
195998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
196098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
196198faf436SMengdong Lin 			return 0;
196298faf436SMengdong Lin 		i++;
196346b5a4d2SWu Fengguang 	}
196498faf436SMengdong Lin 
196598faf436SMengdong Lin 	return 1;
196698faf436SMengdong Lin }
196798faf436SMengdong Lin 
1968345233d7SLiam Girdwood /**
1969345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1970345233d7SLiam Girdwood  * @card: The card to register DMI names
1971345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1972345233d7SLiam Girdwood  *
1973345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1974345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1975345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1976345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1977345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1978345233d7SLiam Girdwood  * as the sound card long name, in the format of
1979345233d7SLiam Girdwood  * "vendor-product-version-board"
1980345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1981345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1982345233d7SLiam Girdwood  * (UCM) configurations for the card.
1983345233d7SLiam Girdwood  *
1984345233d7SLiam Girdwood  * Possible card long names may be:
1985345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1986345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1987345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1988345233d7SLiam Girdwood  *
1989345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1990345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1991345233d7SLiam Girdwood  *
1992345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1993345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1994345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1995345233d7SLiam Girdwood  * charactors like SPACE.
1996345233d7SLiam Girdwood  *
1997345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1998345233d7SLiam Girdwood  */
1999345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2000345233d7SLiam Girdwood {
2001345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
2002345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
2003345233d7SLiam Girdwood 	size_t len;
2004345233d7SLiam Girdwood 
2005345233d7SLiam Girdwood 	if (card->long_name)
2006345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
2007345233d7SLiam Girdwood 
2008345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
2009345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
201098faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
2011345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2012345233d7SLiam Girdwood 		return 0;
2013345233d7SLiam Girdwood 	}
2014345233d7SLiam Girdwood 
201598faf436SMengdong Lin 
2016345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2017345233d7SLiam Girdwood 			 "%s", vendor);
2018345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
2019345233d7SLiam Girdwood 
2020345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
202198faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
2022345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2023345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2024345233d7SLiam Girdwood 			 longname_buf_size - len,
2025345233d7SLiam Girdwood 			 "-%s", product);
2026345233d7SLiam Girdwood 
2027345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
2028345233d7SLiam Girdwood 		if (len < longname_buf_size)
2029345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2030345233d7SLiam Girdwood 
2031345233d7SLiam Girdwood 		/* some vendors like Lenovo may only put a self-explanatory
2032345233d7SLiam Girdwood 		 * name in the product version field
2033345233d7SLiam Girdwood 		 */
2034345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
203598faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
2036345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
2037345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
2038345233d7SLiam Girdwood 				 longname_buf_size - len,
2039345233d7SLiam Girdwood 				 "-%s", product_version);
2040345233d7SLiam Girdwood 
2041345233d7SLiam Girdwood 			len++;
2042345233d7SLiam Girdwood 			if (len < longname_buf_size)
2043345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
2044345233d7SLiam Girdwood 		}
2045345233d7SLiam Girdwood 	}
2046345233d7SLiam Girdwood 
2047345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
204898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
2049345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2050345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2051345233d7SLiam Girdwood 			 longname_buf_size - len,
2052345233d7SLiam Girdwood 			 "-%s", board);
2053345233d7SLiam Girdwood 
2054345233d7SLiam Girdwood 		len++;
2055345233d7SLiam Girdwood 		if (len < longname_buf_size)
2056345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2057345233d7SLiam Girdwood 	} else if (!product) {
2058345233d7SLiam Girdwood 		/* fall back to using legacy name */
2059345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2060345233d7SLiam Girdwood 		return 0;
2061345233d7SLiam Girdwood 	}
2062345233d7SLiam Girdwood 
2063345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
2064345233d7SLiam Girdwood 	if (flavour) {
2065345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2066345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2067345233d7SLiam Girdwood 			 longname_buf_size - len,
2068345233d7SLiam Girdwood 			 "-%s", flavour);
2069345233d7SLiam Girdwood 
2070345233d7SLiam Girdwood 		len++;
2071345233d7SLiam Girdwood 		if (len < longname_buf_size)
2072345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2073345233d7SLiam Girdwood 	}
2074345233d7SLiam Girdwood 
2075345233d7SLiam Girdwood 	/* set the card long name */
2076345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
2077345233d7SLiam Girdwood 
2078345233d7SLiam Girdwood 	return 0;
2079345233d7SLiam Girdwood }
2080345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
20811f5a4535STakashi Iwai #endif /* CONFIG_DMI */
2082345233d7SLiam Girdwood 
2083b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2084f0fba2adSLiam Girdwood {
2085fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
20861a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
208761b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2088ce64c8b9SLars-Peter Clausen 	int ret, i, order;
208996dd3622SMark Brown 
209034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
209101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2092f0fba2adSLiam Girdwood 
2093b19e6e7bSMark Brown 	/* bind DAIs */
2094b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
20956f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
2096b19e6e7bSMark Brown 		if (ret != 0)
2097b19e6e7bSMark Brown 			goto base_error;
2098db2a4165SFrank Mandarino 	}
2099db2a4165SFrank Mandarino 
210044c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2101b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
210244c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2103b19e6e7bSMark Brown 		if (ret != 0)
2104b19e6e7bSMark Brown 			goto base_error;
2105db2a4165SFrank Mandarino 	}
2106db2a4165SFrank Mandarino 
2107f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2108f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
2109f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
2110f8f80361SMengdong Lin 
2111fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
2112fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
2113fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
2114fdf0f54dSDimitris Papastamos 			continue;
2115f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
2116b19e6e7bSMark Brown 		if (ret < 0)
2117b19e6e7bSMark Brown 			goto base_error;
2118fdf0f54dSDimitris Papastamos 	}
2119fdf0f54dSDimitris Papastamos 
2120f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2121102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2122f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2123f0fba2adSLiam Girdwood 	if (ret < 0) {
212410e8aa9aSMichał Mirosław 		dev_err(card->dev,
212510e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
212610e8aa9aSMichał Mirosław 			card->name, ret);
2127b19e6e7bSMark Brown 		goto base_error;
2128db2a4165SFrank Mandarino 	}
2129db2a4165SFrank Mandarino 
21300757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
21310757d834SLars-Peter Clausen 
2132e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
2133e37a4970SMark Brown 	card->dapm.dev = card->dev;
2134e37a4970SMark Brown 	card->dapm.card = card;
2135e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
2136e37a4970SMark Brown 
2137d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2138d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2139d5d1e0beSLars-Peter Clausen #endif
2140d5d1e0beSLars-Peter Clausen 
214188ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
21426ed25978SAndy Green 	/* deferred resume work */
21436308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
21441301a964SRandy Dunlap #endif
21456ed25978SAndy Green 
21469a841ebbSMark Brown 	if (card->dapm_widgets)
21479a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
21489a841ebbSMark Brown 					  card->num_dapm_widgets);
21499a841ebbSMark Brown 
2150f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2151f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2152f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2153f23e860eSNicolin Chen 
2154f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2155f0fba2adSLiam Girdwood 	if (card->probe) {
2156e7361ec4SMark Brown 		ret = card->probe(card);
2157f0fba2adSLiam Girdwood 		if (ret < 0)
2158f0fba2adSLiam Girdwood 			goto card_probe_error;
2159f0fba2adSLiam Girdwood 	}
2160f0fba2adSLiam Girdwood 
216162ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
21620168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
21630168bf0dSLiam Girdwood 			order++) {
21641a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
21651a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
216662ae68faSStephen Warren 			if (ret < 0) {
2167f110bfc7SLiam Girdwood 				dev_err(card->dev,
2168f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2169f110bfc7SLiam Girdwood 					ret);
217062ae68faSStephen Warren 				goto probe_dai_err;
217162ae68faSStephen Warren 			}
217262ae68faSStephen Warren 		}
217362ae68faSStephen Warren 	}
217462ae68faSStephen Warren 
2175f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2176f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2177f2ed6b07SMengdong Lin 	if (ret < 0)
2178f2ed6b07SMengdong Lin 		goto probe_dai_err;
2179f2ed6b07SMengdong Lin 
218061b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
218161b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
218261b0088bSMengdong Lin 	 */
218361b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
218461b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
218561b0088bSMengdong Lin 			continue;
218661b0088bSMengdong Lin 
218761b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
218861b0088bSMengdong Lin 		if (ret)
218961b0088bSMengdong Lin 			goto probe_dai_err;
219061b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
219161b0088bSMengdong Lin 		if (ret)
219261b0088bSMengdong Lin 			goto probe_dai_err;
219361b0088bSMengdong Lin 	}
219461b0088bSMengdong Lin 
219562ae68faSStephen Warren 	/* probe all DAI links on this card */
219662ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
219762ae68faSStephen Warren 			order++) {
21981a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
21991a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2200fe3e78e0SMark Brown 			if (ret < 0) {
2201f110bfc7SLiam Girdwood 				dev_err(card->dev,
2202f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2203f110bfc7SLiam Girdwood 					ret);
2204f0fba2adSLiam Girdwood 				goto probe_dai_err;
2205fe3e78e0SMark Brown 			}
2206fe3e78e0SMark Brown 		}
22070168bf0dSLiam Girdwood 	}
2208fe3e78e0SMark Brown 
2209888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2210b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2211888df395SMark Brown 
2212b7af1dafSMark Brown 	if (card->controls)
2213022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
2214b7af1dafSMark Brown 
2215b8ad29deSMark Brown 	if (card->dapm_routes)
2216b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2217b8ad29deSMark Brown 					card->num_dapm_routes);
2218b8ad29deSMark Brown 
2219f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2220f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2221f23e860eSNicolin Chen 					card->num_of_dapm_routes);
222275d9ac46SMark Brown 
2223861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2224861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2225861886d3STakashi Iwai 
2226f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2227fe3e78e0SMark Brown 		 "%s", card->name);
2228f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
222922de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2230f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2231f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2232f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2233f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2234f0e8ed85SMark Brown 		case '_':
2235f0e8ed85SMark Brown 		case '-':
2236f0e8ed85SMark Brown 		case '\0':
2237f0e8ed85SMark Brown 			break;
2238f0e8ed85SMark Brown 		default:
2239f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2240f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2241f0e8ed85SMark Brown 			break;
2242f0e8ed85SMark Brown 		}
2243f0e8ed85SMark Brown 	}
2244fe3e78e0SMark Brown 
224528e9ad92SMark Brown 	if (card->late_probe) {
224628e9ad92SMark Brown 		ret = card->late_probe(card);
224728e9ad92SMark Brown 		if (ret < 0) {
2248f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
224928e9ad92SMark Brown 				card->name, ret);
225028e9ad92SMark Brown 			goto probe_aux_dev_err;
225128e9ad92SMark Brown 		}
225228e9ad92SMark Brown 	}
225328e9ad92SMark Brown 
2254824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
22558c193b8dSLars-Peter Clausen 
2256f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2257fe3e78e0SMark Brown 	if (ret < 0) {
2258f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2259f110bfc7SLiam Girdwood 				ret);
22606b3ed785SAxel Lin 		goto probe_aux_dev_err;
2261fe3e78e0SMark Brown 	}
2262fe3e78e0SMark Brown 
2263435c5e25SMark Brown 	card->instantiated = 1;
22644f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2265f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
226634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2267b19e6e7bSMark Brown 
2268b19e6e7bSMark Brown 	return 0;
2269db2a4165SFrank Mandarino 
22702eea392dSJarkko Nikula probe_aux_dev_err:
2271f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
22722eea392dSJarkko Nikula 
2273f0fba2adSLiam Girdwood probe_dai_err:
22740671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2275fe3e78e0SMark Brown 
2276f0fba2adSLiam Girdwood card_probe_error:
227787506549SMark Brown 	if (card->remove)
2278e7361ec4SMark Brown 		card->remove(card);
2279f0fba2adSLiam Girdwood 
22802210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
22810757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2282f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2283f0fba2adSLiam Girdwood 
2284b19e6e7bSMark Brown base_error:
22851a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2286f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
228734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2288db2a4165SFrank Mandarino 
2289b19e6e7bSMark Brown 	return ret;
2290435c5e25SMark Brown }
2291435c5e25SMark Brown 
2292435c5e25SMark Brown /* probes a new socdev */
2293435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2294435c5e25SMark Brown {
2295f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2296435c5e25SMark Brown 
229770a7ca34SVinod Koul 	/*
229870a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
229970a7ca34SVinod Koul 	 * we should not be here in that case so ret error
230070a7ca34SVinod Koul 	 */
230170a7ca34SVinod Koul 	if (!card)
230270a7ca34SVinod Koul 		return -EINVAL;
230370a7ca34SVinod Koul 
2304fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2305f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2306fe4085e8SMark Brown 		 card->name);
2307fe4085e8SMark Brown 
2308435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2309435c5e25SMark Brown 	card->dev = &pdev->dev;
2310f0fba2adSLiam Girdwood 
231128d528c8SMark Brown 	return snd_soc_register_card(card);
2312435c5e25SMark Brown }
2313435c5e25SMark Brown 
2314b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2315db2a4165SFrank Mandarino {
23161a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2317db2a4165SFrank Mandarino 
2318f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
23191a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
232043829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2321db2a4165SFrank Mandarino 
23224efda5f2STakashi Iwai 	/* free the ALSA card at first; this syncs with pending operations */
23234efda5f2STakashi Iwai 	snd_card_free(card->snd_card);
23244efda5f2STakashi Iwai 
2325f0fba2adSLiam Girdwood 	/* remove and free each DAI */
23260671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
23271a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2328f0fba2adSLiam Girdwood 
2329f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2330f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2331f2ed6b07SMengdong Lin 
2332d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2333a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2334a6052154SJarkko Nikula 
2335f0fba2adSLiam Girdwood 	/* remove the card */
233687506549SMark Brown 	if (card->remove)
2337e7361ec4SMark Brown 		card->remove(card);
2338f0fba2adSLiam Girdwood 
2339b0e26485SVinod Koul 	return 0;
2340b2dfa62cSGuennadi Liakhovetski }
2341b0e26485SVinod Koul 
2342b0e26485SVinod Koul /* removes a socdev */
2343b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2344b0e26485SVinod Koul {
2345b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2346b0e26485SVinod Koul 
2347c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2348db2a4165SFrank Mandarino 	return 0;
2349db2a4165SFrank Mandarino }
2350db2a4165SFrank Mandarino 
23516f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
235251737470SMark Brown {
23536f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
23541a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
235551737470SMark Brown 
235651737470SMark Brown 	if (!card->instantiated)
2357416356fcSMark Brown 		return 0;
235851737470SMark Brown 
235951737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
236051737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
23611a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
236243829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
236351737470SMark Brown 
2364f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2365416356fcSMark Brown 
2366988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
23671a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
236888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
23691a497983SMengdong Lin 		int i;
237088bd870fSBenoit Cousson 
2371988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
23721a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
23731a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
237488bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
237588bd870fSBenoit Cousson 		}
2376988e8cc4SNicolin Chen 	}
2377988e8cc4SNicolin Chen 
2378416356fcSMark Brown 	return 0;
237951737470SMark Brown }
23806f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
238151737470SMark Brown 
23826f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2383b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2384b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2385b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2386b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
23876f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2388b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2389416356fcSMark Brown };
2390deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2391416356fcSMark Brown 
2392db2a4165SFrank Mandarino /* ASoC platform driver */
2393db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2394db2a4165SFrank Mandarino 	.driver		= {
2395db2a4165SFrank Mandarino 		.name		= "soc-audio",
23966f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2397db2a4165SFrank Mandarino 	},
2398db2a4165SFrank Mandarino 	.probe		= soc_probe,
2399db2a4165SFrank Mandarino 	.remove		= soc_remove,
2400db2a4165SFrank Mandarino };
2401db2a4165SFrank Mandarino 
2402096e49d5SMark Brown /**
2403db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2404db2a4165SFrank Mandarino  * @_template: control template
2405db2a4165SFrank Mandarino  * @data: control private data
2406ac11a2b3SMark Brown  * @long_name: control long name
2407efb7ac3fSMark Brown  * @prefix: control name prefix
2408db2a4165SFrank Mandarino  *
2409db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2410db2a4165SFrank Mandarino  *
2411db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2412db2a4165SFrank Mandarino  */
2413db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
24143056557fSMark Brown 				  void *data, const char *long_name,
2415efb7ac3fSMark Brown 				  const char *prefix)
2416db2a4165SFrank Mandarino {
2417db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2418efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2419efb7ac3fSMark Brown 	char *name = NULL;
2420db2a4165SFrank Mandarino 
2421db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2422db2a4165SFrank Mandarino 	template.index = 0;
2423db2a4165SFrank Mandarino 
2424efb7ac3fSMark Brown 	if (!long_name)
2425efb7ac3fSMark Brown 		long_name = template.name;
2426efb7ac3fSMark Brown 
2427efb7ac3fSMark Brown 	if (prefix) {
24282b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2429efb7ac3fSMark Brown 		if (!name)
2430efb7ac3fSMark Brown 			return NULL;
2431efb7ac3fSMark Brown 
2432efb7ac3fSMark Brown 		template.name = name;
2433efb7ac3fSMark Brown 	} else {
2434efb7ac3fSMark Brown 		template.name = long_name;
2435efb7ac3fSMark Brown 	}
2436efb7ac3fSMark Brown 
2437efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2438efb7ac3fSMark Brown 
2439efb7ac3fSMark Brown 	kfree(name);
2440efb7ac3fSMark Brown 
2441efb7ac3fSMark Brown 	return kcontrol;
2442db2a4165SFrank Mandarino }
2443db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2444db2a4165SFrank Mandarino 
2445022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2446022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2447022658beSLiam Girdwood 	const char *prefix, void *data)
2448022658beSLiam Girdwood {
2449022658beSLiam Girdwood 	int err, i;
2450022658beSLiam Girdwood 
2451022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2452022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2453022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2454022658beSLiam Girdwood 						     control->name, prefix));
2455022658beSLiam Girdwood 		if (err < 0) {
2456f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2457f110bfc7SLiam Girdwood 				control->name, err);
2458022658beSLiam Girdwood 			return err;
2459022658beSLiam Girdwood 		}
2460022658beSLiam Girdwood 	}
2461022658beSLiam Girdwood 
2462022658beSLiam Girdwood 	return 0;
2463022658beSLiam Girdwood }
2464022658beSLiam Girdwood 
24654fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
24664fefd698SDimitris Papastamos 					       const char *name)
24674fefd698SDimitris Papastamos {
24684fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
24694fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
24704fefd698SDimitris Papastamos 
24714fefd698SDimitris Papastamos 	if (unlikely(!name))
24724fefd698SDimitris Papastamos 		return NULL;
24734fefd698SDimitris Papastamos 
24744fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
24754fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
24764fefd698SDimitris Papastamos 			return kctl;
24774fefd698SDimitris Papastamos 	return NULL;
24784fefd698SDimitris Papastamos }
24794fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
24804fefd698SDimitris Papastamos 
2481db2a4165SFrank Mandarino /**
24820f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
24830f2780adSLars-Peter Clausen  *
24840f2780adSLars-Peter Clausen  * @component: Component to add controls to
24850f2780adSLars-Peter Clausen  * @controls: Array of controls to add
24860f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
24870f2780adSLars-Peter Clausen  *
24880f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
24890f2780adSLars-Peter Clausen  */
24900f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
24910f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
24920f2780adSLars-Peter Clausen {
24930f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
24940f2780adSLars-Peter Clausen 
24950f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
24960f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
24970f2780adSLars-Peter Clausen }
24980f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
24990f2780adSLars-Peter Clausen 
25000f2780adSLars-Peter Clausen /**
2501022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2502022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
25033e8e1952SIan Molton  * duplicating this code.
25043e8e1952SIan Molton  *
25053e8e1952SIan Molton  * @codec: codec to add controls to
25063e8e1952SIan Molton  * @controls: array of controls to add
25073e8e1952SIan Molton  * @num_controls: number of elements in the array
25083e8e1952SIan Molton  *
25093e8e1952SIan Molton  * Return 0 for success, else error.
25103e8e1952SIan Molton  */
2511022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
25120f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
25133e8e1952SIan Molton {
25140f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
25150f2780adSLars-Peter Clausen 		num_controls);
25163e8e1952SIan Molton }
2517022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
25183e8e1952SIan Molton 
25193e8e1952SIan Molton /**
2520a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2521022658beSLiam Girdwood  * Convenience function to add a list of controls.
2522a491a5c8SLiam Girdwood  *
2523a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2524a491a5c8SLiam Girdwood  * @controls: array of controls to add
2525a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2526a491a5c8SLiam Girdwood  *
2527a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2528a491a5c8SLiam Girdwood  */
2529a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
25300f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2531a491a5c8SLiam Girdwood {
25320f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
25330f2780adSLars-Peter Clausen 		num_controls);
2534a491a5c8SLiam Girdwood }
2535a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2536a491a5c8SLiam Girdwood 
2537a491a5c8SLiam Girdwood /**
2538022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2539022658beSLiam Girdwood  * Convenience function to add a list of controls.
2540022658beSLiam Girdwood  *
2541022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2542022658beSLiam Girdwood  * @controls: array of controls to add
2543022658beSLiam Girdwood  * @num_controls: number of elements in the array
2544022658beSLiam Girdwood  *
2545022658beSLiam Girdwood  * Return 0 for success, else error.
2546022658beSLiam Girdwood  */
2547022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2548022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2549022658beSLiam Girdwood {
2550022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2551022658beSLiam Girdwood 
2552022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2553022658beSLiam Girdwood 			NULL, soc_card);
2554022658beSLiam Girdwood }
2555022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2556022658beSLiam Girdwood 
2557022658beSLiam Girdwood /**
2558022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2559022658beSLiam Girdwood  * Convienience function to add a list of controls.
2560022658beSLiam Girdwood  *
2561022658beSLiam Girdwood  * @dai: DAI to add controls to
2562022658beSLiam Girdwood  * @controls: array of controls to add
2563022658beSLiam Girdwood  * @num_controls: number of elements in the array
2564022658beSLiam Girdwood  *
2565022658beSLiam Girdwood  * Return 0 for success, else error.
2566022658beSLiam Girdwood  */
2567022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2568022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2569022658beSLiam Girdwood {
2570313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2571022658beSLiam Girdwood 
2572022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2573022658beSLiam Girdwood 			NULL, dai);
2574022658beSLiam Girdwood }
2575022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2576022658beSLiam Girdwood 
2577022658beSLiam Girdwood /**
25788c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
25798c6529dbSLiam Girdwood  * @dai: DAI
25808c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
25818c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
25828c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
25838c6529dbSLiam Girdwood  *
25848c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
25858c6529dbSLiam Girdwood  */
25868c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
25878c6529dbSLiam Girdwood 	unsigned int freq, int dir)
25888c6529dbSLiam Girdwood {
2589f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2590f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2591ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2592da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2593ec4ee52aSMark Brown 						      freq, dir);
25948c6529dbSLiam Girdwood 	else
25951104a9c8SMark Brown 		return -ENOTSUPP;
25968c6529dbSLiam Girdwood }
25978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
25988c6529dbSLiam Girdwood 
25998c6529dbSLiam Girdwood /**
2600ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2601ec4ee52aSMark Brown  * @codec: CODEC
2602ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2603da1c6ea6SMark Brown  * @source: Source for the clock
2604ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2605ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2606ec4ee52aSMark Brown  *
2607ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2608ec4ee52aSMark Brown  */
2609ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2610da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2611ec4ee52aSMark Brown {
2612ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2613da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2614da1c6ea6SMark Brown 						 freq, dir);
2615ec4ee52aSMark Brown 	else
26161104a9c8SMark Brown 		return -ENOTSUPP;
2617ec4ee52aSMark Brown }
2618ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2619ec4ee52aSMark Brown 
2620ec4ee52aSMark Brown /**
26218c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
26228c6529dbSLiam Girdwood  * @dai: DAI
2623ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
26248c6529dbSLiam Girdwood  * @div: new clock divisor.
26258c6529dbSLiam Girdwood  *
26268c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
26278c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
26288c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
26298c6529dbSLiam Girdwood  */
26308c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
26318c6529dbSLiam Girdwood 	int div_id, int div)
26328c6529dbSLiam Girdwood {
2633f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2634f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
26358c6529dbSLiam Girdwood 	else
26368c6529dbSLiam Girdwood 		return -EINVAL;
26378c6529dbSLiam Girdwood }
26388c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
26398c6529dbSLiam Girdwood 
26408c6529dbSLiam Girdwood /**
26418c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
26428c6529dbSLiam Girdwood  * @dai: DAI
26438c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
264485488037SMark Brown  * @source: DAI specific source for the PLL
26458c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
26468c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
26478c6529dbSLiam Girdwood  *
26488c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
26498c6529dbSLiam Girdwood  */
265085488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
265185488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
26528c6529dbSLiam Girdwood {
2653f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2654f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
265585488037SMark Brown 					 freq_in, freq_out);
2656ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2657ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2658ec4ee52aSMark Brown 						   freq_in, freq_out);
26598c6529dbSLiam Girdwood 	else
26608c6529dbSLiam Girdwood 		return -EINVAL;
26618c6529dbSLiam Girdwood }
26628c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
26638c6529dbSLiam Girdwood 
2664ec4ee52aSMark Brown /*
2665ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2666ec4ee52aSMark Brown  * @codec: CODEC
2667ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2668ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2669ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2670ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2671ec4ee52aSMark Brown  *
2672ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2673ec4ee52aSMark Brown  */
2674ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2675ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2676ec4ee52aSMark Brown {
2677ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2678ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2679ec4ee52aSMark Brown 					      freq_in, freq_out);
2680ec4ee52aSMark Brown 	else
2681ec4ee52aSMark Brown 		return -EINVAL;
2682ec4ee52aSMark Brown }
2683ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2684ec4ee52aSMark Brown 
26858c6529dbSLiam Girdwood /**
2686e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2687e54cf76bSLiam Girdwood  * @dai: DAI
2688231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2689e54cf76bSLiam Girdwood  *
2690e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2691e54cf76bSLiam Girdwood  */
2692e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2693e54cf76bSLiam Girdwood {
2694e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2695e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2696e54cf76bSLiam Girdwood 	else
2697e54cf76bSLiam Girdwood 		return -EINVAL;
2698e54cf76bSLiam Girdwood }
2699e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2700e54cf76bSLiam Girdwood 
2701e54cf76bSLiam Girdwood /**
27028c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
27038c6529dbSLiam Girdwood  * @dai: DAI
27048c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
27058c6529dbSLiam Girdwood  *
27068c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
27078c6529dbSLiam Girdwood  */
27088c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
27098c6529dbSLiam Girdwood {
27105e4ba569SShawn Guo 	if (dai->driver == NULL)
27118c6529dbSLiam Girdwood 		return -EINVAL;
27125e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
27135e4ba569SShawn Guo 		return -ENOTSUPP;
27145e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
27158c6529dbSLiam Girdwood }
27168c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
27178c6529dbSLiam Girdwood 
27188c6529dbSLiam Girdwood /**
2719e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
272089c67857SXiubo Li  * @slots: Number of slots in use.
272189c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
272289c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
272389c67857SXiubo Li  *
272489c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
272589c67857SXiubo Li  */
2726e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
272789c67857SXiubo Li 					  unsigned int *tx_mask,
272889c67857SXiubo Li 					  unsigned int *rx_mask)
272989c67857SXiubo Li {
273089c67857SXiubo Li 	if (*tx_mask || *rx_mask)
273189c67857SXiubo Li 		return 0;
273289c67857SXiubo Li 
273389c67857SXiubo Li 	if (!slots)
273489c67857SXiubo Li 		return -EINVAL;
273589c67857SXiubo Li 
273689c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
273789c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
273889c67857SXiubo Li 
273989c67857SXiubo Li 	return 0;
274089c67857SXiubo Li }
274189c67857SXiubo Li 
274289c67857SXiubo Li /**
2743e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2744e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2745a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2746a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
27478c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2748a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
27498c6529dbSLiam Girdwood  *
2750e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2751e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2752e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2753e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2754e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2755e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2756e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2757e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2758e46c9366SLars-Peter Clausen  * channel and so on.
2759e46c9366SLars-Peter Clausen  *
2760e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2761e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2762e46c9366SLars-Peter Clausen  *
2763e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
27648c6529dbSLiam Girdwood  */
27658c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2766a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
27678c6529dbSLiam Girdwood {
2768e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2769e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
277089c67857SXiubo Li 						&tx_mask, &rx_mask);
277189c67857SXiubo Li 	else
2772e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
277389c67857SXiubo Li 
277488bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
277588bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
277688bd870fSBenoit Cousson 
2777f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2778f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2779a5479e38SDaniel Ribeiro 				slots, slot_width);
27808c6529dbSLiam Girdwood 	else
2781b2cbb6e1SXiubo Li 		return -ENOTSUPP;
27828c6529dbSLiam Girdwood }
27838c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
27848c6529dbSLiam Girdwood 
27858c6529dbSLiam Girdwood /**
2786472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2787472df3cbSBarry Song  * @dai: DAI
2788472df3cbSBarry Song  * @tx_num: how many TX channels
2789472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2790472df3cbSBarry Song  *           0~num-1 uses
2791472df3cbSBarry Song  * @rx_num: how many RX channels
2792472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2793472df3cbSBarry Song  *           0~num-1 uses
2794472df3cbSBarry Song  *
2795472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2796472df3cbSBarry Song  */
2797472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2798472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2799472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2800472df3cbSBarry Song {
2801f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2802f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2803472df3cbSBarry Song 			rx_num, rx_slot);
2804472df3cbSBarry Song 	else
2805472df3cbSBarry Song 		return -EINVAL;
2806472df3cbSBarry Song }
2807472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2808472df3cbSBarry Song 
2809472df3cbSBarry Song /**
28108c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
28118c6529dbSLiam Girdwood  * @dai: DAI
28128c6529dbSLiam Girdwood  * @tristate: tristate enable
28138c6529dbSLiam Girdwood  *
28148c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
28158c6529dbSLiam Girdwood  */
28168c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
28178c6529dbSLiam Girdwood {
2818f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2819f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
28208c6529dbSLiam Girdwood 	else
28218c6529dbSLiam Girdwood 		return -EINVAL;
28228c6529dbSLiam Girdwood }
28238c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
28248c6529dbSLiam Girdwood 
28258c6529dbSLiam Girdwood /**
28268c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
28278c6529dbSLiam Girdwood  * @dai: DAI
28288c6529dbSLiam Girdwood  * @mute: mute enable
2829da18396fSMark Brown  * @direction: stream to mute
28308c6529dbSLiam Girdwood  *
28318c6529dbSLiam Girdwood  * Mutes the DAI DAC.
28328c6529dbSLiam Girdwood  */
2833da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2834da18396fSMark Brown 			     int direction)
28358c6529dbSLiam Girdwood {
2836da18396fSMark Brown 	if (!dai->driver)
2837da18396fSMark Brown 		return -ENOTSUPP;
2838da18396fSMark Brown 
2839da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2840da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2841da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2842da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2843f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
28448c6529dbSLiam Girdwood 	else
284504570c62SMark Brown 		return -ENOTSUPP;
28468c6529dbSLiam Girdwood }
28478c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
28488c6529dbSLiam Girdwood 
2849c5af3a2eSMark Brown /**
2850c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2851c5af3a2eSMark Brown  *
2852ac11a2b3SMark Brown  * @card: Card to register
2853c5af3a2eSMark Brown  *
2854c5af3a2eSMark Brown  */
285570a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2856c5af3a2eSMark Brown {
2857923c5e61SMengdong Lin 	int i, ret;
28581a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2859f0fba2adSLiam Girdwood 
2860c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2861c5af3a2eSMark Brown 		return -EINVAL;
2862c5af3a2eSMark Brown 
28635a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
28645a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
28655a504963SStephen Warren 
2866923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
286788bd870fSBenoit Cousson 		if (ret) {
2868923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2869923c5e61SMengdong Lin 				link->name);
287088bd870fSBenoit Cousson 			return ret;
287188bd870fSBenoit Cousson 		}
28725a504963SStephen Warren 	}
28735a504963SStephen Warren 
2874ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2875ed77cc12SMark Brown 
2876111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2877111c6419SStephen Warren 
2878f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2879f8f80361SMengdong Lin 	card->num_dai_links = 0;
2880f0fba2adSLiam Girdwood 
28811a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
28829115171aSMark Brown 	card->num_rtd = 0;
2883db2a4165SFrank Mandarino 
2884db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
28858a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2886db2a4165SFrank Mandarino 	card->instantiated = 0;
2887db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2888a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2889db2a4165SFrank Mandarino 
2890b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2891b19e6e7bSMark Brown 	if (ret != 0)
28924e2576bdSKuninori Morimoto 		return ret;
2893db2a4165SFrank Mandarino 
2894988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
28951a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
289688bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
289788bd870fSBenoit Cousson 		int j;
289888bd870fSBenoit Cousson 
289988bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
290088bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2901988e8cc4SNicolin Chen 			if (!codec_dai->active)
2902988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
290388bd870fSBenoit Cousson 		}
290488bd870fSBenoit Cousson 
2905988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2906988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2907988e8cc4SNicolin Chen 	}
2908988e8cc4SNicolin Chen 
2909b19e6e7bSMark Brown 	return ret;
2910db2a4165SFrank Mandarino }
291170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2912db2a4165SFrank Mandarino 
2913db2a4165SFrank Mandarino /**
2914db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2915db2a4165SFrank Mandarino  *
2916db2a4165SFrank Mandarino  * @card: Card to unregister
2917db2a4165SFrank Mandarino  *
2918db2a4165SFrank Mandarino  */
291970a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2920db2a4165SFrank Mandarino {
292101e0df66SLars-Peter Clausen 	if (card->instantiated) {
292201e0df66SLars-Peter Clausen 		card->instantiated = false;
29231c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2924b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2925f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
29268f6f9b29SKuninori Morimoto 	}
2927db2a4165SFrank Mandarino 
2928db2a4165SFrank Mandarino 	return 0;
2929db2a4165SFrank Mandarino }
293070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2931db2a4165SFrank Mandarino 
2932db2a4165SFrank Mandarino /*
2933db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2934db2a4165SFrank Mandarino  * and sanitizing names.
2935db2a4165SFrank Mandarino  */
29360b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2937db2a4165SFrank Mandarino {
2938db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2939db2a4165SFrank Mandarino 	int id1, id2;
2940db2a4165SFrank Mandarino 
2941db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2942db2a4165SFrank Mandarino 		return NULL;
2943db2a4165SFrank Mandarino 
294458818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2945db2a4165SFrank Mandarino 
2946db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2947c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2948c5af3a2eSMark Brown 	if (found) {
2949c5af3a2eSMark Brown 		/* get ID */
2950c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2951c5af3a2eSMark Brown 
2952c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2953c5af3a2eSMark Brown 			if (*id == -1)
2954c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2955c5af3a2eSMark Brown 		}
2956c5af3a2eSMark Brown 
2957c5af3a2eSMark Brown 	} else {
2958c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2959c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2960c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2961c5af3a2eSMark Brown 
2962c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2963c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2964c5af3a2eSMark Brown 
2965c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2966c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
296758818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2968c5af3a2eSMark Brown 		} else
2969c5af3a2eSMark Brown 			*id = 0;
2970c5af3a2eSMark Brown 	}
2971c5af3a2eSMark Brown 
2972c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2973c5af3a2eSMark Brown }
2974c5af3a2eSMark Brown 
2975c5af3a2eSMark Brown /*
2976c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2977c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2978c5af3a2eSMark Brown  */
2979c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2980c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2981c5af3a2eSMark Brown {
2982c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
298310e8aa9aSMichał Mirosław 		dev_err(dev,
298410e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
298510e8aa9aSMichał Mirosław 			dev_name(dev));
2986c5af3a2eSMark Brown 		return NULL;
2987c5af3a2eSMark Brown 	}
2988c5af3a2eSMark Brown 
2989c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2990c5af3a2eSMark Brown }
2991c5af3a2eSMark Brown 
2992c5af3a2eSMark Brown /**
299332c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
29949115171aSMark Brown  *
299532c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
29969115171aSMark Brown  */
299732c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
29989115171aSMark Brown {
29995c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
30009115171aSMark Brown 
30015c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
300232c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
300332c9ba54SLars-Peter Clausen 			dai->name);
30049115171aSMark Brown 		list_del(&dai->list);
3005f0fba2adSLiam Girdwood 		kfree(dai->name);
3006f0fba2adSLiam Girdwood 		kfree(dai);
30079115171aSMark Brown 	}
300832c9ba54SLars-Peter Clausen }
30099115171aSMark Brown 
30105e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
30115e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
30125e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
30135e4fb372SMengdong Lin 	bool legacy_dai_naming)
30145e4fb372SMengdong Lin {
30155e4fb372SMengdong Lin 	struct device *dev = component->dev;
30165e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
30175e4fb372SMengdong Lin 
30185e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
30195e4fb372SMengdong Lin 
30205e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
30215e4fb372SMengdong Lin 	if (dai == NULL)
30225e4fb372SMengdong Lin 		return NULL;
30235e4fb372SMengdong Lin 
30245e4fb372SMengdong Lin 	/*
30255e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
30265e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
30275e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
30285e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
30295e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
30305e4fb372SMengdong Lin 	 * component-less anymore.
30315e4fb372SMengdong Lin 	 */
30325e4fb372SMengdong Lin 	if (legacy_dai_naming &&
30335e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
30345e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
30355e4fb372SMengdong Lin 	} else {
30365e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
30375e4fb372SMengdong Lin 		if (dai_drv->id)
30385e4fb372SMengdong Lin 			dai->id = dai_drv->id;
30395e4fb372SMengdong Lin 		else
30405e4fb372SMengdong Lin 			dai->id = component->num_dai;
30415e4fb372SMengdong Lin 	}
30425e4fb372SMengdong Lin 	if (dai->name == NULL) {
30435e4fb372SMengdong Lin 		kfree(dai);
30445e4fb372SMengdong Lin 		return NULL;
30455e4fb372SMengdong Lin 	}
30465e4fb372SMengdong Lin 
30475e4fb372SMengdong Lin 	dai->component = component;
30485e4fb372SMengdong Lin 	dai->dev = dev;
30495e4fb372SMengdong Lin 	dai->driver = dai_drv;
30505e4fb372SMengdong Lin 	if (!dai->driver->ops)
30515e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
30525e4fb372SMengdong Lin 
30535e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
30545e4fb372SMengdong Lin 	component->num_dai++;
30555e4fb372SMengdong Lin 
30565e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
30575e4fb372SMengdong Lin 	return dai;
30585e4fb372SMengdong Lin }
30595e4fb372SMengdong Lin 
30609115171aSMark Brown /**
306132c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
30629115171aSMark Brown  *
30636106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
30646106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3065ac11a2b3SMark Brown  * @count: Number of DAIs
306632c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
306732c9ba54SLars-Peter Clausen  *                     parent's name.
30689115171aSMark Brown  */
306932c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
3070bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
3071bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
30729115171aSMark Brown {
30736106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3074f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
307532c9ba54SLars-Peter Clausen 	unsigned int i;
307632c9ba54SLars-Peter Clausen 	int ret;
3077f0fba2adSLiam Girdwood 
30785b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
30799115171aSMark Brown 
3080bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
3081bb13109dSLars-Peter Clausen 
30829115171aSMark Brown 	for (i = 0; i < count; i++) {
3083f0fba2adSLiam Girdwood 
30845e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
30855e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
3086c46e0079SAxel Lin 		if (dai == NULL) {
3087c46e0079SAxel Lin 			ret = -ENOMEM;
3088c46e0079SAxel Lin 			goto err;
3089c46e0079SAxel Lin 		}
3090f0fba2adSLiam Girdwood 	}
3091f0fba2adSLiam Girdwood 
30929115171aSMark Brown 	return 0;
30939115171aSMark Brown 
30949115171aSMark Brown err:
309532c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
30969115171aSMark Brown 
30979115171aSMark Brown 	return ret;
30989115171aSMark Brown }
30999115171aSMark Brown 
310068003e6cSMengdong Lin /**
310168003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
310268003e6cSMengdong Lin  *
310368003e6cSMengdong Lin  * @component: The component the DAIs are registered for
310468003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
310568003e6cSMengdong Lin  *
310668003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
310768003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
310868003e6cSMengdong Lin  * will be freed in the component cleanup.
310968003e6cSMengdong Lin  */
311068003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
311168003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
311268003e6cSMengdong Lin {
311368003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
311468003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
311568003e6cSMengdong Lin 	struct snd_soc_dai *dai;
311668003e6cSMengdong Lin 	int ret;
311768003e6cSMengdong Lin 
311868003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
311968003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
312068003e6cSMengdong Lin 			dai_drv->dobj.type);
312168003e6cSMengdong Lin 		return -EINVAL;
312268003e6cSMengdong Lin 	}
312368003e6cSMengdong Lin 
312468003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
312568003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
312668003e6cSMengdong Lin 	if (!dai)
312768003e6cSMengdong Lin 		return -ENOMEM;
312868003e6cSMengdong Lin 
312968003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
313068003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
313168003e6cSMengdong Lin 	 */
313268003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
313368003e6cSMengdong Lin 	if (ret != 0) {
313468003e6cSMengdong Lin 		dev_err(component->dev,
313568003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
313668003e6cSMengdong Lin 	}
313768003e6cSMengdong Lin 
313868003e6cSMengdong Lin 	return ret;
313968003e6cSMengdong Lin }
314068003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
314168003e6cSMengdong Lin 
314214e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
314314e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3144d191bd8dSKuninori Morimoto {
314514e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3146d191bd8dSKuninori Morimoto 
314714e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
314814e8bdebSLars-Peter Clausen }
3149d191bd8dSKuninori Morimoto 
315014e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
315114e8bdebSLars-Peter Clausen 	int event)
315214e8bdebSLars-Peter Clausen {
315314e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
315414e8bdebSLars-Peter Clausen 
315514e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
315614e8bdebSLars-Peter Clausen }
315714e8bdebSLars-Peter Clausen 
3158bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3159bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3160d191bd8dSKuninori Morimoto {
3161ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3162ce0fc93aSLars-Peter Clausen 
3163bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3164bb13109dSLars-Peter Clausen 	if (!component->name) {
3165bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3166d191bd8dSKuninori Morimoto 		return -ENOMEM;
3167d191bd8dSKuninori Morimoto 	}
3168d191bd8dSKuninori Morimoto 
3169bb13109dSLars-Peter Clausen 	component->dev = dev;
3170bb13109dSLars-Peter Clausen 	component->driver = driver;
317161aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
317261aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
31739178feb4SKuninori Morimoto 	component->suspend = component->driver->suspend;
31749178feb4SKuninori Morimoto 	component->resume = component->driver->resume;
317599b04f4cSKuninori Morimoto 	component->pcm_new = component->driver->pcm_new;
317699b04f4cSKuninori Morimoto 	component->pcm_free = component->driver->pcm_free;
3177e2c330b9SLars-Peter Clausen 
31784890140fSLars-Peter Clausen 	dapm = &component->dapm;
3179ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3180ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3181ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
31825819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
318314e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
318414e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
318514e8bdebSLars-Peter Clausen 	if (driver->stream_event)
318614e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
3187ce0fc93aSLars-Peter Clausen 
318861aca564SLars-Peter Clausen 	component->controls = driver->controls;
318961aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
319061aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
319161aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
319261aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
319361aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
319461aca564SLars-Peter Clausen 
3195bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3196bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3197bb13109dSLars-Peter Clausen 
3198bb13109dSLars-Peter Clausen 	return 0;
3199d191bd8dSKuninori Morimoto }
3200d191bd8dSKuninori Morimoto 
320120feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3202886f5692SLars-Peter Clausen {
3203886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
320420feb881SLars-Peter Clausen 
3205886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3206886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3207886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3208886f5692SLars-Peter Clausen }
320920feb881SLars-Peter Clausen 
3210e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3211e874bf5fSLars-Peter Clausen 
321220feb881SLars-Peter Clausen /**
321320feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
321420feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
321520feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
321620feb881SLars-Peter Clausen  *
321720feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
321820feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
321920feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
322020feb881SLars-Peter Clausen  * before the first IO attempt of the component.
322120feb881SLars-Peter Clausen  */
322220feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
322320feb881SLars-Peter Clausen 	struct regmap *regmap)
322420feb881SLars-Peter Clausen {
322520feb881SLars-Peter Clausen 	component->regmap = regmap;
322620feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3227886f5692SLars-Peter Clausen }
322820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
322920feb881SLars-Peter Clausen 
323020feb881SLars-Peter Clausen /**
323120feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
323220feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
323320feb881SLars-Peter Clausen  *
323420feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
323520feb881SLars-Peter Clausen  * removes the regmap instance from the component.
323620feb881SLars-Peter Clausen  *
323720feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
323820feb881SLars-Peter Clausen  * to initialize the regmap instance.
323920feb881SLars-Peter Clausen  */
324020feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
324120feb881SLars-Peter Clausen {
324220feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
324320feb881SLars-Peter Clausen 	component->regmap = NULL;
324420feb881SLars-Peter Clausen }
324520feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3246886f5692SLars-Peter Clausen 
3247e874bf5fSLars-Peter Clausen #endif
3248d191bd8dSKuninori Morimoto 
3249bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3250bb13109dSLars-Peter Clausen {
325120feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
325220feb881SLars-Peter Clausen 		if (!component->regmap)
325320feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
325420feb881SLars-Peter Clausen 		if (component->regmap)
325520feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
325620feb881SLars-Peter Clausen 	}
3257886f5692SLars-Peter Clausen 
3258bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
32598a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3260d191bd8dSKuninori Morimoto }
3261d191bd8dSKuninori Morimoto 
3262bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3263bb13109dSLars-Peter Clausen {
3264d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3265bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3266d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3267bb13109dSLars-Peter Clausen }
3268d191bd8dSKuninori Morimoto 
3269bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3270bb13109dSLars-Peter Clausen {
3271bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3272bb13109dSLars-Peter Clausen 	kfree(component->name);
3273bb13109dSLars-Peter Clausen }
3274d191bd8dSKuninori Morimoto 
3275bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3276bb13109dSLars-Peter Clausen {
3277c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3278c12c1aadSKuninori Morimoto 
3279c12c1aadSKuninori Morimoto 	if (card)
3280c12c1aadSKuninori Morimoto 		snd_soc_unregister_card(card);
3281c12c1aadSKuninori Morimoto 
3282bb13109dSLars-Peter Clausen 	list_del(&component->list);
3283bb13109dSLars-Peter Clausen }
3284d191bd8dSKuninori Morimoto 
3285d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3286d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3287d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3288d191bd8dSKuninori Morimoto 			       int num_dai)
3289d191bd8dSKuninori Morimoto {
3290d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3291bb13109dSLars-Peter Clausen 	int ret;
3292d191bd8dSKuninori Morimoto 
3293bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3294d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3295d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3296d191bd8dSKuninori Morimoto 		return -ENOMEM;
3297d191bd8dSKuninori Morimoto 	}
3298d191bd8dSKuninori Morimoto 
3299bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3300bb13109dSLars-Peter Clausen 	if (ret)
3301bb13109dSLars-Peter Clausen 		goto err_free;
3302bb13109dSLars-Peter Clausen 
33033d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
330498e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
33053d59400fSLars-Peter Clausen 
3306bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3307bb13109dSLars-Peter Clausen 	if (ret < 0) {
3308f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3309bb13109dSLars-Peter Clausen 		goto err_cleanup;
3310bb13109dSLars-Peter Clausen 	}
3311bb13109dSLars-Peter Clausen 
3312bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3313bb13109dSLars-Peter Clausen 
3314bb13109dSLars-Peter Clausen 	return 0;
3315bb13109dSLars-Peter Clausen 
3316bb13109dSLars-Peter Clausen err_cleanup:
3317bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3318bb13109dSLars-Peter Clausen err_free:
3319bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3320bb13109dSLars-Peter Clausen 	return ret;
3321d191bd8dSKuninori Morimoto }
3322d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3323d191bd8dSKuninori Morimoto 
3324d191bd8dSKuninori Morimoto /**
3325d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3326d191bd8dSKuninori Morimoto  *
3327628536eaSJonathan Corbet  * @dev: The device to unregister
3328d191bd8dSKuninori Morimoto  */
3329d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3330d191bd8dSKuninori Morimoto {
3331d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3332d191bd8dSKuninori Morimoto 
333334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3334d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
333598e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3336d191bd8dSKuninori Morimoto 			goto found;
3337d191bd8dSKuninori Morimoto 	}
333834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3339d191bd8dSKuninori Morimoto 	return;
3340d191bd8dSKuninori Morimoto 
3341d191bd8dSKuninori Morimoto found:
33428a978234SLiam Girdwood 	snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
334334e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
334434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3345bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3346bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3347d191bd8dSKuninori Morimoto }
3348d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3349d191bd8dSKuninori Morimoto 
3350f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3351e2c330b9SLars-Peter Clausen {
3352e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3353e2c330b9SLars-Peter Clausen 
3354f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3355e2c330b9SLars-Peter Clausen }
3356e2c330b9SLars-Peter Clausen 
3357f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3358e2c330b9SLars-Peter Clausen {
3359e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3360e2c330b9SLars-Peter Clausen 
3361f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3362e2c330b9SLars-Peter Clausen }
3363e2c330b9SLars-Peter Clausen 
336499b04f4cSKuninori Morimoto static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd)
336599b04f4cSKuninori Morimoto {
336699b04f4cSKuninori Morimoto 	struct snd_soc_platform *platform = rtd->platform;
336799b04f4cSKuninori Morimoto 
3368d6c098a1SBrian Norris 	if (platform->driver->pcm_new)
336999b04f4cSKuninori Morimoto 		return platform->driver->pcm_new(rtd);
3370d6c098a1SBrian Norris 	else
3371d6c098a1SBrian Norris 		return 0;
337299b04f4cSKuninori Morimoto }
337399b04f4cSKuninori Morimoto 
337499b04f4cSKuninori Morimoto static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm)
337599b04f4cSKuninori Morimoto {
337699b04f4cSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = pcm->private_data;
337799b04f4cSKuninori Morimoto 	struct snd_soc_platform *platform = rtd->platform;
337899b04f4cSKuninori Morimoto 
3379d6c098a1SBrian Norris 	if (platform->driver->pcm_free)
338099b04f4cSKuninori Morimoto 		platform->driver->pcm_free(pcm);
338199b04f4cSKuninori Morimoto }
338299b04f4cSKuninori Morimoto 
3383d191bd8dSKuninori Morimoto /**
338471a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
338571a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
338671a45cdaSLars-Peter Clausen  * @platform: The platform to add
33877d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
338812a48a8cSMark Brown  */
338971a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3390d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
339112a48a8cSMark Brown {
3392b37f1d12SLars-Peter Clausen 	int ret;
3393b37f1d12SLars-Peter Clausen 
3394bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3395bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3396bb13109dSLars-Peter Clausen 	if (ret)
3397bb13109dSLars-Peter Clausen 		return ret;
3398f0fba2adSLiam Girdwood 
3399f0fba2adSLiam Girdwood 	platform->dev = dev;
3400f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3401f1d45cc3SLars-Peter Clausen 
3402f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3403f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3404f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3405f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
340699b04f4cSKuninori Morimoto 	if (platform_drv->pcm_new)
340799b04f4cSKuninori Morimoto 		platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
340899b04f4cSKuninori Morimoto 	if (platform_drv->pcm_free)
340999b04f4cSKuninori Morimoto 		platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
341012a48a8cSMark Brown 
341181c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
341281c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
341381c7cfd1SLars-Peter Clausen #endif
341412a48a8cSMark Brown 
341512a48a8cSMark Brown 	mutex_lock(&client_mutex);
3416bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
341712a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
341812a48a8cSMark Brown 	mutex_unlock(&client_mutex);
341912a48a8cSMark Brown 
3420f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3421f4333203SLars-Peter Clausen 		platform->component.name);
342212a48a8cSMark Brown 
342312a48a8cSMark Brown 	return 0;
342412a48a8cSMark Brown }
342571a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
342671a45cdaSLars-Peter Clausen 
342771a45cdaSLars-Peter Clausen /**
342871a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
342971a45cdaSLars-Peter Clausen  *
3430628536eaSJonathan Corbet  * @dev: The device for the platform
3431628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
343271a45cdaSLars-Peter Clausen  */
343371a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
343471a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
343571a45cdaSLars-Peter Clausen {
343671a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
343771a45cdaSLars-Peter Clausen 	int ret;
343871a45cdaSLars-Peter Clausen 
343971a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
344071a45cdaSLars-Peter Clausen 
344171a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
344271a45cdaSLars-Peter Clausen 	if (platform == NULL)
344371a45cdaSLars-Peter Clausen 		return -ENOMEM;
344471a45cdaSLars-Peter Clausen 
344571a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
344671a45cdaSLars-Peter Clausen 	if (ret)
344771a45cdaSLars-Peter Clausen 		kfree(platform);
344871a45cdaSLars-Peter Clausen 
344971a45cdaSLars-Peter Clausen 	return ret;
345071a45cdaSLars-Peter Clausen }
345112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
345212a48a8cSMark Brown 
345312a48a8cSMark Brown /**
345471a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
345571a45cdaSLars-Peter Clausen  * @platform: the platform to remove
345671a45cdaSLars-Peter Clausen  */
345771a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
345871a45cdaSLars-Peter Clausen {
3459b37f1d12SLars-Peter Clausen 
346071a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
346171a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3462bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
346371a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
346471a45cdaSLars-Peter Clausen 
346571a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3466f4333203SLars-Peter Clausen 		platform->component.name);
3467decc27b0SDaniel Mack 
3468decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
346971a45cdaSLars-Peter Clausen }
347071a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
347171a45cdaSLars-Peter Clausen 
347271a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
347371a45cdaSLars-Peter Clausen {
347471a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
347571a45cdaSLars-Peter Clausen 
347634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
347771a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
347834e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
347934e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
348071a45cdaSLars-Peter Clausen 			return platform;
348171a45cdaSLars-Peter Clausen 		}
348234e81ab4SLars-Peter Clausen 	}
348334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
348471a45cdaSLars-Peter Clausen 
348571a45cdaSLars-Peter Clausen 	return NULL;
348671a45cdaSLars-Peter Clausen }
348771a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
348871a45cdaSLars-Peter Clausen 
348971a45cdaSLars-Peter Clausen /**
349012a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
349112a48a8cSMark Brown  *
3492628536eaSJonathan Corbet  * @dev: platform to unregister
349312a48a8cSMark Brown  */
3494f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
349512a48a8cSMark Brown {
3496f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3497f0fba2adSLiam Girdwood 
349871a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
349971a45cdaSLars-Peter Clausen 	if (!platform)
3500f0fba2adSLiam Girdwood 		return;
3501f0fba2adSLiam Girdwood 
350271a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3503f0fba2adSLiam Girdwood 	kfree(platform);
350412a48a8cSMark Brown }
350512a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
350612a48a8cSMark Brown 
3507151ab22cSMark Brown static u64 codec_format_map[] = {
3508151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3509151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3510151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3511151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3512151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3513151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3514151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3515151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3516151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3517151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3518151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3519151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3520151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3521151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3522151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3523151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3524151ab22cSMark Brown };
3525151ab22cSMark Brown 
3526151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3527151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3528151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3529151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3530151ab22cSMark Brown  */
3531151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3532151ab22cSMark Brown {
3533151ab22cSMark Brown 	int i;
3534151ab22cSMark Brown 
3535151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3536151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3537151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3538151ab22cSMark Brown }
3539151ab22cSMark Brown 
3540f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3541f1d45cc3SLars-Peter Clausen {
3542f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3543f1d45cc3SLars-Peter Clausen 
3544f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3545f1d45cc3SLars-Peter Clausen }
3546f1d45cc3SLars-Peter Clausen 
3547f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3548f1d45cc3SLars-Peter Clausen {
3549f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3550f1d45cc3SLars-Peter Clausen 
3551f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3552f1d45cc3SLars-Peter Clausen }
3553f1d45cc3SLars-Peter Clausen 
35549178feb4SKuninori Morimoto static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
35559178feb4SKuninori Morimoto {
35569178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
35579178feb4SKuninori Morimoto 
35589178feb4SKuninori Morimoto 	return codec->driver->suspend(codec);
35599178feb4SKuninori Morimoto }
35609178feb4SKuninori Morimoto 
35619178feb4SKuninori Morimoto static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
35629178feb4SKuninori Morimoto {
35639178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
35649178feb4SKuninori Morimoto 
35659178feb4SKuninori Morimoto 	return codec->driver->resume(codec);
35669178feb4SKuninori Morimoto }
35679178feb4SKuninori Morimoto 
3568e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3569e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3570e2c330b9SLars-Peter Clausen {
3571e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3572e2c330b9SLars-Peter Clausen 
3573e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3574e2c330b9SLars-Peter Clausen }
3575e2c330b9SLars-Peter Clausen 
3576e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3577e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3578e2c330b9SLars-Peter Clausen {
3579e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3580e2c330b9SLars-Peter Clausen 
3581e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3582e2c330b9SLars-Peter Clausen 
3583e2c330b9SLars-Peter Clausen 	return 0;
3584e2c330b9SLars-Peter Clausen }
3585e2c330b9SLars-Peter Clausen 
358668f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
358768f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
358868f831c2SLars-Peter Clausen {
358968f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
359068f831c2SLars-Peter Clausen 
359168f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
359268f831c2SLars-Peter Clausen }
359368f831c2SLars-Peter Clausen 
35940d0cf00aSMark Brown /**
35950d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
35960d0cf00aSMark Brown  *
3597628536eaSJonathan Corbet  * @dev: The parent device for this codec
3598628536eaSJonathan Corbet  * @codec_drv: Codec driver
3599628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3600628536eaSJonathan Corbet  * @num_dai: Number of DAIs
36010d0cf00aSMark Brown  */
3602f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3603001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3604001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3605001ae4c0SMark Brown 			   int num_dai)
36060d0cf00aSMark Brown {
36074890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3608f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3609bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3610f0fba2adSLiam Girdwood 	int ret, i;
3611151ab22cSMark Brown 
3612f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
36130d0cf00aSMark Brown 
3614f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3615f0fba2adSLiam Girdwood 	if (codec == NULL)
3616f0fba2adSLiam Girdwood 		return -ENOMEM;
36170d0cf00aSMark Brown 
3618f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3619ce0fc93aSLars-Peter Clausen 
3620bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3621bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3622bb13109dSLars-Peter Clausen 	if (ret)
3623bb13109dSLars-Peter Clausen 		goto err_free;
3624151ab22cSMark Brown 
3625f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3626f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3627f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3628f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
36299178feb4SKuninori Morimoto 	if (codec_drv->suspend)
36309178feb4SKuninori Morimoto 		codec->component.suspend = snd_soc_codec_drv_suspend;
36319178feb4SKuninori Morimoto 	if (codec_drv->resume)
36329178feb4SKuninori Morimoto 		codec->component.resume = snd_soc_codec_drv_resume;
3633e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3634e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3635e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3636e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
36373d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
36384890140fSLars-Peter Clausen 
36394890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
36404890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
36414890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
364214e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
36434890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
364468f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
36454890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3646f0fba2adSLiam Girdwood 	codec->dev = dev;
3647f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3648e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3649f0fba2adSLiam Girdwood 
365081c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
365181c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
365281c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
365381c7cfd1SLars-Peter Clausen #endif
3654a39f75f7SXiubo Li 
3655a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3656886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3657a39f75f7SXiubo Li 
3658f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3659f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3660f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3661f0fba2adSLiam Girdwood 	}
3662f0fba2adSLiam Girdwood 
3663bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3664bb13109dSLars-Peter Clausen 	if (ret < 0) {
3665f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3666bb13109dSLars-Peter Clausen 		goto err_cleanup;
3667bb13109dSLars-Peter Clausen 	}
3668bb13109dSLars-Peter Clausen 
3669bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3670bb13109dSLars-Peter Clausen 		dai->codec = codec;
3671bb13109dSLars-Peter Clausen 
3672054880feSMark Brown 	mutex_lock(&client_mutex);
3673bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3674054880feSMark Brown 	list_add(&codec->list, &codec_list);
3675054880feSMark Brown 	mutex_unlock(&client_mutex);
3676054880feSMark Brown 
3677f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3678f4333203SLars-Peter Clausen 		codec->component.name);
36790d0cf00aSMark Brown 	return 0;
3680f0fba2adSLiam Girdwood 
3681bb13109dSLars-Peter Clausen err_cleanup:
3682bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3683bb13109dSLars-Peter Clausen err_free:
3684f0fba2adSLiam Girdwood 	kfree(codec);
3685f0fba2adSLiam Girdwood 	return ret;
36860d0cf00aSMark Brown }
36870d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
36880d0cf00aSMark Brown 
36890d0cf00aSMark Brown /**
36900d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
36910d0cf00aSMark Brown  *
3692628536eaSJonathan Corbet  * @dev: codec to unregister
36930d0cf00aSMark Brown  */
3694f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
36950d0cf00aSMark Brown {
3696f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3697f0fba2adSLiam Girdwood 
369834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3699f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3700f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3701f0fba2adSLiam Girdwood 			goto found;
3702f0fba2adSLiam Girdwood 	}
370334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3704f0fba2adSLiam Girdwood 	return;
3705f0fba2adSLiam Girdwood 
3706f0fba2adSLiam Girdwood found:
37070d0cf00aSMark Brown 	list_del(&codec->list);
3708bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
37090d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
37100d0cf00aSMark Brown 
3711f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3712f4333203SLars-Peter Clausen 			codec->component.name);
3713f0fba2adSLiam Girdwood 
3714bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
37157a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3716f0fba2adSLiam Girdwood 	kfree(codec);
37170d0cf00aSMark Brown }
37180d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
37190d0cf00aSMark Brown 
3720bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3721b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3722bec4fa05SStephen Warren 			       const char *propname)
3723bec4fa05SStephen Warren {
3724b07609ceSKuninori Morimoto 	struct device_node *np;
3725bec4fa05SStephen Warren 	int ret;
3726bec4fa05SStephen Warren 
37277e07e7c0STushar Behera 	if (!card->dev) {
37287e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
37297e07e7c0STushar Behera 		return -EINVAL;
37307e07e7c0STushar Behera 	}
37317e07e7c0STushar Behera 
37327e07e7c0STushar Behera 	np = card->dev->of_node;
37337e07e7c0STushar Behera 
3734bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3735bec4fa05SStephen Warren 	/*
3736bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3737bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3738bec4fa05SStephen Warren 	 * snd_soc_register_card.
3739bec4fa05SStephen Warren 	 */
3740bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3741bec4fa05SStephen Warren 		dev_err(card->dev,
3742f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3743bec4fa05SStephen Warren 			propname, ret);
3744bec4fa05SStephen Warren 		return ret;
3745bec4fa05SStephen Warren 	}
3746bec4fa05SStephen Warren 
3747bec4fa05SStephen Warren 	return 0;
3748bec4fa05SStephen Warren }
3749b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3750bec4fa05SStephen Warren 
37519a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
37529a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
37539a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
37549a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
37559a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
37569a6d4860SXiubo Li };
37579a6d4860SXiubo Li 
375821efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
37599a6d4860SXiubo Li 					  const char *propname)
37609a6d4860SXiubo Li {
376121efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
37629a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
37639a6d4860SXiubo Li 	const char *template, *wname;
37649a6d4860SXiubo Li 	int i, j, num_widgets, ret;
37659a6d4860SXiubo Li 
37669a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
37679a6d4860SXiubo Li 	if (num_widgets < 0) {
37689a6d4860SXiubo Li 		dev_err(card->dev,
37699a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
37709a6d4860SXiubo Li 		return -EINVAL;
37719a6d4860SXiubo Li 	}
37729a6d4860SXiubo Li 	if (num_widgets & 1) {
37739a6d4860SXiubo Li 		dev_err(card->dev,
37749a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
37759a6d4860SXiubo Li 		return -EINVAL;
37769a6d4860SXiubo Li 	}
37779a6d4860SXiubo Li 
37789a6d4860SXiubo Li 	num_widgets /= 2;
37799a6d4860SXiubo Li 	if (!num_widgets) {
37809a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
37819a6d4860SXiubo Li 			propname);
37829a6d4860SXiubo Li 		return -EINVAL;
37839a6d4860SXiubo Li 	}
37849a6d4860SXiubo Li 
37859a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
37869a6d4860SXiubo Li 			       GFP_KERNEL);
37879a6d4860SXiubo Li 	if (!widgets) {
37889a6d4860SXiubo Li 		dev_err(card->dev,
37899a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
37909a6d4860SXiubo Li 		return -ENOMEM;
37919a6d4860SXiubo Li 	}
37929a6d4860SXiubo Li 
37939a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
37949a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
37959a6d4860SXiubo Li 			2 * i, &template);
37969a6d4860SXiubo Li 		if (ret) {
37979a6d4860SXiubo Li 			dev_err(card->dev,
37989a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
37999a6d4860SXiubo Li 				propname, 2 * i, ret);
38009a6d4860SXiubo Li 			return -EINVAL;
38019a6d4860SXiubo Li 		}
38029a6d4860SXiubo Li 
38039a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
38049a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
38059a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
38069a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
38079a6d4860SXiubo Li 				break;
38089a6d4860SXiubo Li 			}
38099a6d4860SXiubo Li 		}
38109a6d4860SXiubo Li 
38119a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
38129a6d4860SXiubo Li 			dev_err(card->dev,
38139a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
38149a6d4860SXiubo Li 				template);
38159a6d4860SXiubo Li 			return -EINVAL;
38169a6d4860SXiubo Li 		}
38179a6d4860SXiubo Li 
38189a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
38199a6d4860SXiubo Li 						    (2 * i) + 1,
38209a6d4860SXiubo Li 						    &wname);
38219a6d4860SXiubo Li 		if (ret) {
38229a6d4860SXiubo Li 			dev_err(card->dev,
38239a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
38249a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
38259a6d4860SXiubo Li 			return -EINVAL;
38269a6d4860SXiubo Li 		}
38279a6d4860SXiubo Li 
38289a6d4860SXiubo Li 		widgets[i].name = wname;
38299a6d4860SXiubo Li 	}
38309a6d4860SXiubo Li 
3831f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3832f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
38339a6d4860SXiubo Li 
38349a6d4860SXiubo Li 	return 0;
38359a6d4860SXiubo Li }
383621efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
38379a6d4860SXiubo Li 
38386131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
38396131084aSJyri Sarha 				    const char *prop_name,
38406131084aSJyri Sarha 				    unsigned int *mask)
38416131084aSJyri Sarha {
38426131084aSJyri Sarha 	u32 val;
38436c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
38446131084aSJyri Sarha 	int i;
38456131084aSJyri Sarha 
38466131084aSJyri Sarha 	if (!of_slot_mask)
38476131084aSJyri Sarha 		return 0;
38486131084aSJyri Sarha 	val /= sizeof(u32);
38496131084aSJyri Sarha 	for (i = 0; i < val; i++)
38506131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
38516131084aSJyri Sarha 			*mask |= (1 << i);
38526131084aSJyri Sarha 
38536131084aSJyri Sarha 	return val;
38546131084aSJyri Sarha }
38556131084aSJyri Sarha 
385689c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
38576131084aSJyri Sarha 			      unsigned int *tx_mask,
38586131084aSJyri Sarha 			      unsigned int *rx_mask,
385989c67857SXiubo Li 			      unsigned int *slots,
386089c67857SXiubo Li 			      unsigned int *slot_width)
386189c67857SXiubo Li {
386289c67857SXiubo Li 	u32 val;
386389c67857SXiubo Li 	int ret;
386489c67857SXiubo Li 
38656131084aSJyri Sarha 	if (tx_mask)
38666131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
38676131084aSJyri Sarha 	if (rx_mask)
38686131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
38696131084aSJyri Sarha 
387089c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
387189c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
387289c67857SXiubo Li 		if (ret)
387389c67857SXiubo Li 			return ret;
387489c67857SXiubo Li 
387589c67857SXiubo Li 		if (slots)
387689c67857SXiubo Li 			*slots = val;
387789c67857SXiubo Li 	}
387889c67857SXiubo Li 
387989c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
388089c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
388189c67857SXiubo Li 		if (ret)
388289c67857SXiubo Li 			return ret;
388389c67857SXiubo Li 
388489c67857SXiubo Li 		if (slot_width)
388589c67857SXiubo Li 			*slot_width = val;
388689c67857SXiubo Li 	}
388789c67857SXiubo Li 
388889c67857SXiubo Li 	return 0;
388989c67857SXiubo Li }
389089c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
389189c67857SXiubo Li 
3892440a3006SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
38935e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
38945e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
38955e3cdaa2SKuninori Morimoto 				   const char *propname)
38965e3cdaa2SKuninori Morimoto {
3897440a3006SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
38985e3cdaa2SKuninori Morimoto 	const char *str;
38995e3cdaa2SKuninori Morimoto 	int ret;
39005e3cdaa2SKuninori Morimoto 
39015e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
39025e3cdaa2SKuninori Morimoto 	if (ret < 0) {
39035e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
39045e3cdaa2SKuninori Morimoto 		return;
39055e3cdaa2SKuninori Morimoto 	}
39065e3cdaa2SKuninori Morimoto 
39075e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
39085e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
39095e3cdaa2SKuninori Morimoto }
3910440a3006SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
39115e3cdaa2SKuninori Morimoto 
39122bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3913a4a54dd5SStephen Warren 				   const char *propname)
3914a4a54dd5SStephen Warren {
39152bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3916e3b1e6a1SMark Brown 	int num_routes;
3917a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3918a4a54dd5SStephen Warren 	int i, ret;
3919a4a54dd5SStephen Warren 
3920a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3921c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
392210e8aa9aSMichał Mirosław 		dev_err(card->dev,
392310e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
392410e8aa9aSMichał Mirosław 			propname);
3925a4a54dd5SStephen Warren 		return -EINVAL;
3926a4a54dd5SStephen Warren 	}
3927a4a54dd5SStephen Warren 	num_routes /= 2;
3928a4a54dd5SStephen Warren 	if (!num_routes) {
3929f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3930a4a54dd5SStephen Warren 			propname);
3931a4a54dd5SStephen Warren 		return -EINVAL;
3932a4a54dd5SStephen Warren 	}
3933a4a54dd5SStephen Warren 
3934e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3935a4a54dd5SStephen Warren 			      GFP_KERNEL);
3936a4a54dd5SStephen Warren 	if (!routes) {
3937a4a54dd5SStephen Warren 		dev_err(card->dev,
3938f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3939a4a54dd5SStephen Warren 		return -EINVAL;
3940a4a54dd5SStephen Warren 	}
3941a4a54dd5SStephen Warren 
3942a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3943a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3944e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3945a4a54dd5SStephen Warren 		if (ret) {
3946c871bd0bSMark Brown 			dev_err(card->dev,
3947c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3948c871bd0bSMark Brown 				propname, 2 * i, ret);
3949a4a54dd5SStephen Warren 			return -EINVAL;
3950a4a54dd5SStephen Warren 		}
3951a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3952e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3953a4a54dd5SStephen Warren 		if (ret) {
3954a4a54dd5SStephen Warren 			dev_err(card->dev,
3955c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3956c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3957a4a54dd5SStephen Warren 			return -EINVAL;
3958a4a54dd5SStephen Warren 		}
3959a4a54dd5SStephen Warren 	}
3960a4a54dd5SStephen Warren 
3961f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3962f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3963a4a54dd5SStephen Warren 
3964a4a54dd5SStephen Warren 	return 0;
3965a4a54dd5SStephen Warren }
39662bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3967a4a54dd5SStephen Warren 
3968a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3969389cb834SJyri Sarha 				     const char *prefix,
3970389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3971389cb834SJyri Sarha 				     struct device_node **framemaster)
3972a7930ed4SKuninori Morimoto {
3973a7930ed4SKuninori Morimoto 	int ret, i;
3974a7930ed4SKuninori Morimoto 	char prop[128];
3975a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3976a7930ed4SKuninori Morimoto 	int bit, frame;
3977a7930ed4SKuninori Morimoto 	const char *str;
3978a7930ed4SKuninori Morimoto 	struct {
3979a7930ed4SKuninori Morimoto 		char *name;
3980a7930ed4SKuninori Morimoto 		unsigned int val;
3981a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3982a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3983a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3984a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3985a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3986a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3987a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3988a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3989a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3990a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3991a7930ed4SKuninori Morimoto 	};
3992a7930ed4SKuninori Morimoto 
3993a7930ed4SKuninori Morimoto 	if (!prefix)
3994a7930ed4SKuninori Morimoto 		prefix = "";
3995a7930ed4SKuninori Morimoto 
3996a7930ed4SKuninori Morimoto 	/*
39975711c979SKuninori Morimoto 	 * check "dai-format = xxx"
39985711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3999a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4000a7930ed4SKuninori Morimoto 	 */
40015711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
40025711c979SKuninori Morimoto 	if (ret < 0) {
4003a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
4004a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
40055711c979SKuninori Morimoto 	}
4006a7930ed4SKuninori Morimoto 	if (ret == 0) {
4007a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4008a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4009a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4010a7930ed4SKuninori Morimoto 				break;
4011a7930ed4SKuninori Morimoto 			}
4012a7930ed4SKuninori Morimoto 		}
4013a7930ed4SKuninori Morimoto 	}
4014a7930ed4SKuninori Morimoto 
4015a7930ed4SKuninori Morimoto 	/*
40168c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4017a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4018a7930ed4SKuninori Morimoto 	 */
40198c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
402051930295SJulia Lawall 	if (of_property_read_bool(np, prop))
40218c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
40228c2d6a9fSKuninori Morimoto 	else
40238c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4024a7930ed4SKuninori Morimoto 
4025a7930ed4SKuninori Morimoto 	/*
4026a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4027a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4028a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4029a7930ed4SKuninori Morimoto 	 */
4030a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4031a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4032a7930ed4SKuninori Morimoto 
4033a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4034a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4035a7930ed4SKuninori Morimoto 
4036a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4037a7930ed4SKuninori Morimoto 	case 0x11:
4038a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4039a7930ed4SKuninori Morimoto 		break;
4040a7930ed4SKuninori Morimoto 	case 0x10:
4041a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4042a7930ed4SKuninori Morimoto 		break;
4043a7930ed4SKuninori Morimoto 	case 0x01:
4044a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4045a7930ed4SKuninori Morimoto 		break;
4046a7930ed4SKuninori Morimoto 	default:
4047a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4048a7930ed4SKuninori Morimoto 		break;
4049a7930ed4SKuninori Morimoto 	}
4050a7930ed4SKuninori Morimoto 
4051a7930ed4SKuninori Morimoto 	/*
4052a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4053a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4054a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4055a7930ed4SKuninori Morimoto 	 */
4056a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4057a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4058389cb834SJyri Sarha 	if (bit && bitclkmaster)
4059389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4060a7930ed4SKuninori Morimoto 
4061a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4062a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4063389cb834SJyri Sarha 	if (frame && framemaster)
4064389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4065a7930ed4SKuninori Morimoto 
4066a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4067a7930ed4SKuninori Morimoto 	case 0x11:
4068a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4069a7930ed4SKuninori Morimoto 		break;
4070a7930ed4SKuninori Morimoto 	case 0x10:
4071a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4072a7930ed4SKuninori Morimoto 		break;
4073a7930ed4SKuninori Morimoto 	case 0x01:
4074a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4075a7930ed4SKuninori Morimoto 		break;
4076a7930ed4SKuninori Morimoto 	default:
4077a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4078a7930ed4SKuninori Morimoto 		break;
4079a7930ed4SKuninori Morimoto 	}
4080a7930ed4SKuninori Morimoto 
4081a7930ed4SKuninori Morimoto 	return format;
4082a7930ed4SKuninori Morimoto }
4083a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4084a7930ed4SKuninori Morimoto 
4085a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
4086a180e8b9SKuninori Morimoto {
4087a180e8b9SKuninori Morimoto 	struct snd_soc_component *pos;
4088a180e8b9SKuninori Morimoto 	struct device_node *node;
4089a180e8b9SKuninori Morimoto 	int ret;
4090a180e8b9SKuninori Morimoto 
4091a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
4092a180e8b9SKuninori Morimoto 
4093a180e8b9SKuninori Morimoto 	/*
4094a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
4095a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
4096a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
4097a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
4098a180e8b9SKuninori Morimoto 	 */
4099a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
4100a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
4101a180e8b9SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4102a180e8b9SKuninori Morimoto 		struct device_node *component_of_node = pos->dev->of_node;
4103a180e8b9SKuninori Morimoto 
4104a180e8b9SKuninori Morimoto 		if (!component_of_node && pos->dev->parent)
4105a180e8b9SKuninori Morimoto 			component_of_node = pos->dev->parent->of_node;
4106a180e8b9SKuninori Morimoto 
4107a180e8b9SKuninori Morimoto 		if (component_of_node != node)
4108a180e8b9SKuninori Morimoto 			continue;
4109a180e8b9SKuninori Morimoto 
4110a180e8b9SKuninori Morimoto 		if (pos->driver->of_xlate_dai_id)
4111a180e8b9SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_id(pos, ep);
4112a180e8b9SKuninori Morimoto 
4113a180e8b9SKuninori Morimoto 		break;
4114a180e8b9SKuninori Morimoto 	}
4115a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
4116a180e8b9SKuninori Morimoto 
4117a180e8b9SKuninori Morimoto 	return ret;
4118a180e8b9SKuninori Morimoto }
4119a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4120a180e8b9SKuninori Morimoto 
41211ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
4122cb470087SKuninori Morimoto 				const char **dai_name)
4123cb470087SKuninori Morimoto {
4124cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
41253e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
412693b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
4127cb470087SKuninori Morimoto 
4128cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4129cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
41303e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
41313e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
41323e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
41333e0aa8d8SJyri Sarha 
41343e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
4135cb470087SKuninori Morimoto 			continue;
4136cb470087SKuninori Morimoto 
41376833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
413893b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
413993b0f3eeSJean-Francois Moine 							     args,
414093b0f3eeSJean-Francois Moine 							     dai_name);
41416833c452SKuninori Morimoto 		} else {
41426833c452SKuninori Morimoto 			int id = -1;
41436833c452SKuninori Morimoto 
414493b0f3eeSJean-Francois Moine 			switch (args->args_count) {
41456833c452SKuninori Morimoto 			case 0:
41466833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
41476833c452SKuninori Morimoto 				break;
41486833c452SKuninori Morimoto 			case 1:
414993b0f3eeSJean-Francois Moine 				id = args->args[0];
41506833c452SKuninori Morimoto 				break;
41516833c452SKuninori Morimoto 			default:
41526833c452SKuninori Morimoto 				/* not supported */
4153cb470087SKuninori Morimoto 				break;
4154cb470087SKuninori Morimoto 			}
4155cb470087SKuninori Morimoto 
41566833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
41576833c452SKuninori Morimoto 				ret = -EINVAL;
41583dcba280SNicolin Chen 				continue;
41596833c452SKuninori Morimoto 			}
4160e41975edSXiubo Li 
4161e41975edSXiubo Li 			ret = 0;
4162e41975edSXiubo Li 
4163e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4164e41975edSXiubo Li 			if (!*dai_name)
4165e41975edSXiubo Li 				*dai_name = pos->name;
41666833c452SKuninori Morimoto 		}
41676833c452SKuninori Morimoto 
4168cb470087SKuninori Morimoto 		break;
4169cb470087SKuninori Morimoto 	}
4170cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
417193b0f3eeSJean-Francois Moine 	return ret;
417293b0f3eeSJean-Francois Moine }
41731ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
417493b0f3eeSJean-Francois Moine 
417593b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
417693b0f3eeSJean-Francois Moine 			    const char **dai_name)
417793b0f3eeSJean-Francois Moine {
417893b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
417993b0f3eeSJean-Francois Moine 	int ret;
418093b0f3eeSJean-Francois Moine 
418193b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
418293b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
418393b0f3eeSJean-Francois Moine 	if (ret)
418493b0f3eeSJean-Francois Moine 		return ret;
418593b0f3eeSJean-Francois Moine 
418693b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
4187cb470087SKuninori Morimoto 
4188cb470087SKuninori Morimoto 	of_node_put(args.np);
4189cb470087SKuninori Morimoto 
4190cb470087SKuninori Morimoto 	return ret;
4191cb470087SKuninori Morimoto }
4192cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4193cb470087SKuninori Morimoto 
419493b0f3eeSJean-Francois Moine /*
419593b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
419693b0f3eeSJean-Francois Moine  * @dev: Card device
419793b0f3eeSJean-Francois Moine  * @of_node: Device node
419893b0f3eeSJean-Francois Moine  * @dai_link: DAI link
419993b0f3eeSJean-Francois Moine  *
420093b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
420193b0f3eeSJean-Francois Moine  * 'sound-dai'.
420293b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
420393b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
420493b0f3eeSJean-Francois Moine  *
420593b0f3eeSJean-Francois Moine  * Returns 0 for success
420693b0f3eeSJean-Francois Moine  */
420793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
420893b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
420993b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
421093b0f3eeSJean-Francois Moine {
421193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
421293b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
421393b0f3eeSJean-Francois Moine 	char *name;
421493b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
421593b0f3eeSJean-Francois Moine 
421693b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
421793b0f3eeSJean-Francois Moine 	name = "sound-dai";
421893b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
421993b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
422093b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
422193b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
422293b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
422393b0f3eeSJean-Francois Moine 		else
422493b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
422593b0f3eeSJean-Francois Moine 		return num_codecs;
422693b0f3eeSJean-Francois Moine 	}
422793b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
422893b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
422993b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
423093b0f3eeSJean-Francois Moine 	if (!component)
423193b0f3eeSJean-Francois Moine 		return -ENOMEM;
423293b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
423393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
423493b0f3eeSJean-Francois Moine 
423593b0f3eeSJean-Francois Moine 	/* Parse the list */
423693b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
423793b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
423893b0f3eeSJean-Francois Moine 	     index++, component++) {
423993b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
424093b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
424193b0f3eeSJean-Francois Moine 						  index, &args);
424293b0f3eeSJean-Francois Moine 		if (ret)
424393b0f3eeSJean-Francois Moine 			goto err;
424493b0f3eeSJean-Francois Moine 		component->of_node = args.np;
424593b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
424693b0f3eeSJean-Francois Moine 		if (ret < 0)
424793b0f3eeSJean-Francois Moine 			goto err;
424893b0f3eeSJean-Francois Moine 	}
424993b0f3eeSJean-Francois Moine 	return 0;
425093b0f3eeSJean-Francois Moine err:
425193b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
425293b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
425393b0f3eeSJean-Francois Moine 	     index++, component++) {
425493b0f3eeSJean-Francois Moine 		if (!component->of_node)
425593b0f3eeSJean-Francois Moine 			break;
425693b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
425793b0f3eeSJean-Francois Moine 		component->of_node = NULL;
425893b0f3eeSJean-Francois Moine 	}
425993b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
426093b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
426193b0f3eeSJean-Francois Moine 	return ret;
426293b0f3eeSJean-Francois Moine }
426393b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
426493b0f3eeSJean-Francois Moine 
4265c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4266db2a4165SFrank Mandarino {
42676553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
4268fb257897SMark Brown 	snd_soc_util_init();
4269fb257897SMark Brown 
4270db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4271db2a4165SFrank Mandarino }
42724abe8e16SMark Brown module_init(snd_soc_init);
4273db2a4165SFrank Mandarino 
42747d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4275db2a4165SFrank Mandarino {
4276fb257897SMark Brown 	snd_soc_util_exit();
42776553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
4278fb257897SMark Brown 
4279db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4280db2a4165SFrank Mandarino }
4281db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4282db2a4165SFrank Mandarino 
4283db2a4165SFrank Mandarino /* Module information */
4284d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4285db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4286db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4287db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4288