xref: /openbmc/linux/sound/soc/soc-core.c (revision 5711c97920377827da4bbc18233ffb08c6e3bbef)
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>
37345233d7SLiam Girdwood #include <linux/dmi.h>
38db2a4165SFrank Mandarino #include <sound/core.h>
393028eb8cSMark Brown #include <sound/jack.h>
40db2a4165SFrank Mandarino #include <sound/pcm.h>
41db2a4165SFrank Mandarino #include <sound/pcm_params.h>
42db2a4165SFrank Mandarino #include <sound/soc.h>
4301d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
448a978234SLiam Girdwood #include <sound/soc-topology.h>
45db2a4165SFrank Mandarino #include <sound/initval.h>
46db2a4165SFrank Mandarino 
47a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
48a8b1d34fSMark Brown #include <trace/events/asoc.h>
49a8b1d34fSMark Brown 
50f0fba2adSLiam Girdwood #define NAME_SIZE	32
51f0fba2adSLiam Girdwood 
52384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
538a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
548a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
55384c89e2SMark Brown #endif
56384c89e2SMark Brown 
57c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5812a48a8cSMark Brown static LIST_HEAD(platform_list);
590d0cf00aSMark Brown static LIST_HEAD(codec_list);
60030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
61c5af3a2eSMark Brown 
62db2a4165SFrank Mandarino /*
63db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
64db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
65db2a4165SFrank Mandarino  * between two audio tracks.
66db2a4165SFrank Mandarino  */
67db2a4165SFrank Mandarino static int pmdown_time = 5000;
68db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
69db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70db2a4165SFrank Mandarino 
712bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
722bc9a81eSDimitris Papastamos  * a particular given value */
732bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
742bc9a81eSDimitris Papastamos {
752bc9a81eSDimitris Papastamos 	int c = 0;
762bc9a81eSDimitris Papastamos 	int i;
772bc9a81eSDimitris Papastamos 
782bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
792bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
802bc9a81eSDimitris Papastamos 			break;
812bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
822bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
832bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
842bc9a81eSDimitris Papastamos 	else
852bc9a81eSDimitris Papastamos 		c /= 8;
862bc9a81eSDimitris Papastamos 	return c;
872bc9a81eSDimitris Papastamos }
882bc9a81eSDimitris Papastamos 
8913fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
9013fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9113fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9213fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
932624d5faSMark Brown {
9400b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9500b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9613fd179fSDimitris Papastamos 	int ret;
9713fd179fSDimitris Papastamos 
9813fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
9913fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10013fd179fSDimitris Papastamos 		return -EINVAL;
10113fd179fSDimitris Papastamos 
102d6b6c2caSTakashi Iwai 	sprintf(buf, "%.*x: ", wordsize, reg);
103d6b6c2caSTakashi Iwai 	buf += wordsize + 2;
104d6b6c2caSTakashi Iwai 
10513fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
106d6b6c2caSTakashi Iwai 	if (ret < 0)
107d6b6c2caSTakashi Iwai 		memset(buf, 'X', regsize);
108d6b6c2caSTakashi Iwai 	else
109d6b6c2caSTakashi Iwai 		sprintf(buf, "%.*x", regsize, ret);
110d6b6c2caSTakashi Iwai 	buf[regsize] = '\n';
111d6b6c2caSTakashi Iwai 	/* no NUL-termination needed */
11213fd179fSDimitris Papastamos 	return 0;
11313fd179fSDimitris Papastamos }
11413fd179fSDimitris Papastamos 
11513fd179fSDimitris Papastamos /* codec register dump */
11613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
11713fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
11813fd179fSDimitris Papastamos {
11913fd179fSDimitris Papastamos 	int i, step = 1;
1202bc9a81eSDimitris Papastamos 	int wordsize, regsize;
12113fd179fSDimitris Papastamos 	int len;
12213fd179fSDimitris Papastamos 	size_t total = 0;
12313fd179fSDimitris Papastamos 	loff_t p = 0;
1242bc9a81eSDimitris Papastamos 
12500b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
12600b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1272624d5faSMark Brown 
12813fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
12913fd179fSDimitris Papastamos 
130f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1312624d5faSMark Brown 		return 0;
1322624d5faSMark Brown 
133f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
134f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1352624d5faSMark Brown 
136f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
13713fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
13813fd179fSDimitris Papastamos 		 * entries for the default case */
13913fd179fSDimitris Papastamos 		if (p >= pos) {
14013fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1412624d5faSMark Brown 				break;
14213fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
14313fd179fSDimitris Papastamos 			total += len;
14413fd179fSDimitris Papastamos 		}
14513fd179fSDimitris Papastamos 		p += len;
14613fd179fSDimitris Papastamos 	}
1472624d5faSMark Brown 
14813fd179fSDimitris Papastamos 	total = min(total, count - 1);
1492624d5faSMark Brown 
15013fd179fSDimitris Papastamos 	return total;
1512624d5faSMark Brown }
15213fd179fSDimitris Papastamos 
1532624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1542624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1552624d5faSMark Brown {
15636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
157f0fba2adSLiam Girdwood 
15813fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1592624d5faSMark Brown }
1602624d5faSMark Brown 
1612624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1622624d5faSMark Brown 
163dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
164dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
165dbe21408SMark Brown {
16636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
167dbe21408SMark Brown 
168f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
169dbe21408SMark Brown }
170dbe21408SMark Brown 
171dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
172dbe21408SMark Brown 			       struct device_attribute *attr,
173dbe21408SMark Brown 			       const char *buf, size_t count)
174dbe21408SMark Brown {
17536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
176c593b520SMark Brown 	int ret;
177dbe21408SMark Brown 
178b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
179c593b520SMark Brown 	if (ret)
180c593b520SMark Brown 		return ret;
181dbe21408SMark Brown 
182dbe21408SMark Brown 	return count;
183dbe21408SMark Brown }
184dbe21408SMark Brown 
185dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
186dbe21408SMark Brown 
187d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
188d29697dcSTakashi Iwai 	&dev_attr_codec_reg.attr,
189d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
190d29697dcSTakashi Iwai 	NULL
191d29697dcSTakashi Iwai };
192d29697dcSTakashi Iwai 
193d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
194d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
195d29697dcSTakashi Iwai {
196d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
197d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198d29697dcSTakashi Iwai 
199d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
200d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
201d29697dcSTakashi Iwai 	return rtd->codec ? attr->mode : 0; /* enabled only with codec */
202d29697dcSTakashi Iwai }
203d29697dcSTakashi Iwai 
204d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
205d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
206d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
207d29697dcSTakashi Iwai };
208d29697dcSTakashi Iwai 
209d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = {
210d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
211d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
212d29697dcSTakashi Iwai };
213d29697dcSTakashi Iwai 
214d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
215d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
216d29697dcSTakashi Iwai 	&soc_dev_roup,
217d29697dcSTakashi Iwai 	NULL
218d29697dcSTakashi Iwai };
219d29697dcSTakashi Iwai 
2202624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2212624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2222624d5faSMark Brown 				   size_t count, loff_t *ppos)
2232624d5faSMark Brown {
2242624d5faSMark Brown 	ssize_t ret;
2252624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
22613fd179fSDimitris Papastamos 	char *buf;
22713fd179fSDimitris Papastamos 
22813fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22913fd179fSDimitris Papastamos 		return -EINVAL;
23013fd179fSDimitris Papastamos 
23113fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2322624d5faSMark Brown 	if (!buf)
2332624d5faSMark Brown 		return -ENOMEM;
23413fd179fSDimitris Papastamos 
23513fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
23613fd179fSDimitris Papastamos 	if (ret >= 0) {
23713fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23813fd179fSDimitris Papastamos 			kfree(buf);
23913fd179fSDimitris Papastamos 			return -EFAULT;
24013fd179fSDimitris Papastamos 		}
24113fd179fSDimitris Papastamos 		*ppos += ret;
24213fd179fSDimitris Papastamos 	}
24313fd179fSDimitris Papastamos 
2442624d5faSMark Brown 	kfree(buf);
2452624d5faSMark Brown 	return ret;
2462624d5faSMark Brown }
2472624d5faSMark Brown 
2482624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2492624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2502624d5faSMark Brown {
2512624d5faSMark Brown 	char buf[32];
25234e268d8SStephen Boyd 	size_t buf_size;
2532624d5faSMark Brown 	char *start = buf;
2542624d5faSMark Brown 	unsigned long reg, value;
2552624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
256b785a492SJingoo Han 	int ret;
2572624d5faSMark Brown 
2582624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2592624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2602624d5faSMark Brown 		return -EFAULT;
2612624d5faSMark Brown 	buf[buf_size] = 0;
2622624d5faSMark Brown 
2632624d5faSMark Brown 	while (*start == ' ')
2642624d5faSMark Brown 		start++;
2652624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2662624d5faSMark Brown 	while (*start == ' ')
2672624d5faSMark Brown 		start++;
268b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
269b785a492SJingoo Han 	if (ret)
270b785a492SJingoo Han 		return ret;
2710d51a9cbSMark Brown 
2720d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
273373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2740d51a9cbSMark Brown 
275e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2762624d5faSMark Brown 	return buf_size;
2772624d5faSMark Brown }
2782624d5faSMark Brown 
2792624d5faSMark Brown static const struct file_operations codec_reg_fops = {
280234e3405SStephen Boyd 	.open = simple_open,
2812624d5faSMark Brown 	.read = codec_reg_read_file,
2822624d5faSMark Brown 	.write = codec_reg_write_file,
2836038f373SArnd Bergmann 	.llseek = default_llseek,
2842624d5faSMark Brown };
2852624d5faSMark Brown 
28681c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
287e73f3de5SRussell King {
2886553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
2896553bf06SLars-Peter Clausen 		return;
2906553bf06SLars-Peter Clausen 
29181c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
29281c7cfd1SLars-Peter Clausen 		char *name;
293e73f3de5SRussell King 
29481c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
29581c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
29681c7cfd1SLars-Peter Clausen 		if (name) {
29781c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
29881c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
29981c7cfd1SLars-Peter Clausen 			kfree(name);
30081c7cfd1SLars-Peter Clausen 		}
30181c7cfd1SLars-Peter Clausen 	} else {
30281c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
30381c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
304e73f3de5SRussell King 	}
305e73f3de5SRussell King 
30681c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
30781c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
30881c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3092624d5faSMark Brown 		return;
3102624d5faSMark Brown 	}
3112624d5faSMark Brown 
31281c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
31381c7cfd1SLars-Peter Clausen 		component->debugfs_root);
31481c7cfd1SLars-Peter Clausen 
31581c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
31681c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
31781c7cfd1SLars-Peter Clausen }
31881c7cfd1SLars-Peter Clausen 
31981c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
32081c7cfd1SLars-Peter Clausen {
32181c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
32281c7cfd1SLars-Peter Clausen }
32381c7cfd1SLars-Peter Clausen 
32481c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
32581c7cfd1SLars-Peter Clausen {
32681c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
32781c7cfd1SLars-Peter Clausen 
3282624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
32981c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3302624d5faSMark Brown 						 codec, &codec_reg_fops);
3312624d5faSMark Brown 	if (!codec->debugfs_reg)
33210e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
33310e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
334731f1ab2SSebastien Guiriec }
335731f1ab2SSebastien Guiriec 
336c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
337c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
338c3c5a19aSMark Brown {
339c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3402b194f9dSMark Brown 	ssize_t len, ret = 0;
341c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
342c3c5a19aSMark Brown 
343c3c5a19aSMark Brown 	if (!buf)
344c3c5a19aSMark Brown 		return -ENOMEM;
345c3c5a19aSMark Brown 
34634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
34734e81ab4SLars-Peter Clausen 
3482b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3492b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
350f4333203SLars-Peter Clausen 			       codec->component.name);
3512b194f9dSMark Brown 		if (len >= 0)
3522b194f9dSMark Brown 			ret += len;
3532b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3542b194f9dSMark Brown 			ret = PAGE_SIZE;
3552b194f9dSMark Brown 			break;
3562b194f9dSMark Brown 		}
3572b194f9dSMark Brown 	}
358c3c5a19aSMark Brown 
35934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
36034e81ab4SLars-Peter Clausen 
361c3c5a19aSMark Brown 	if (ret >= 0)
362c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
363c3c5a19aSMark Brown 
364c3c5a19aSMark Brown 	kfree(buf);
365c3c5a19aSMark Brown 
366c3c5a19aSMark Brown 	return ret;
367c3c5a19aSMark Brown }
368c3c5a19aSMark Brown 
369c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
370c3c5a19aSMark Brown 	.read = codec_list_read_file,
371c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
372c3c5a19aSMark Brown };
373c3c5a19aSMark Brown 
374f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
375f3208780SMark Brown 				  size_t count, loff_t *ppos)
376f3208780SMark Brown {
377f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3782b194f9dSMark Brown 	ssize_t len, ret = 0;
3791438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
380f3208780SMark Brown 	struct snd_soc_dai *dai;
381f3208780SMark Brown 
382f3208780SMark Brown 	if (!buf)
383f3208780SMark Brown 		return -ENOMEM;
384f3208780SMark Brown 
38534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
38634e81ab4SLars-Peter Clausen 
3871438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3881438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3891438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3901438c2f6SLars-Peter Clausen 				dai->name);
3912b194f9dSMark Brown 			if (len >= 0)
3922b194f9dSMark Brown 				ret += len;
3932b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3942b194f9dSMark Brown 				ret = PAGE_SIZE;
3952b194f9dSMark Brown 				break;
3962b194f9dSMark Brown 			}
3972b194f9dSMark Brown 		}
3981438c2f6SLars-Peter Clausen 	}
399f3208780SMark Brown 
40034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
40134e81ab4SLars-Peter Clausen 
402f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
403f3208780SMark Brown 
404f3208780SMark Brown 	kfree(buf);
405f3208780SMark Brown 
406f3208780SMark Brown 	return ret;
407f3208780SMark Brown }
408f3208780SMark Brown 
409f3208780SMark Brown static const struct file_operations dai_list_fops = {
410f3208780SMark Brown 	.read = dai_list_read_file,
411f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
412f3208780SMark Brown };
413f3208780SMark Brown 
41419c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41519c7ac27SMark Brown 				       char __user *user_buf,
41619c7ac27SMark Brown 				       size_t count, loff_t *ppos)
41719c7ac27SMark Brown {
41819c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4192b194f9dSMark Brown 	ssize_t len, ret = 0;
42019c7ac27SMark Brown 	struct snd_soc_platform *platform;
42119c7ac27SMark Brown 
42219c7ac27SMark Brown 	if (!buf)
42319c7ac27SMark Brown 		return -ENOMEM;
42419c7ac27SMark Brown 
42534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
42634e81ab4SLars-Peter Clausen 
4272b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4282b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
429f4333203SLars-Peter Clausen 			       platform->component.name);
4302b194f9dSMark Brown 		if (len >= 0)
4312b194f9dSMark Brown 			ret += len;
4322b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4332b194f9dSMark Brown 			ret = PAGE_SIZE;
4342b194f9dSMark Brown 			break;
4352b194f9dSMark Brown 		}
4362b194f9dSMark Brown 	}
43719c7ac27SMark Brown 
43834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
43934e81ab4SLars-Peter Clausen 
44019c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
44119c7ac27SMark Brown 
44219c7ac27SMark Brown 	kfree(buf);
44319c7ac27SMark Brown 
44419c7ac27SMark Brown 	return ret;
44519c7ac27SMark Brown }
44619c7ac27SMark Brown 
44719c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44819c7ac27SMark Brown 	.read = platform_list_read_file,
44919c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
45019c7ac27SMark Brown };
45119c7ac27SMark Brown 
452a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
453a6052154SJarkko Nikula {
4546553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
4556553bf06SLars-Peter Clausen 		return;
4566553bf06SLars-Peter Clausen 
457a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4588a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4593a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
460a6052154SJarkko Nikula 		dev_warn(card->dev,
4617c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4623a45b867SJarkko Nikula 		return;
4633a45b867SJarkko Nikula 	}
4643a45b867SJarkko Nikula 
4653a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4663a45b867SJarkko Nikula 						    card->debugfs_card_root,
4673a45b867SJarkko Nikula 						    &card->pop_time);
4683a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4693a45b867SJarkko Nikula 		dev_warn(card->dev,
470f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
471a6052154SJarkko Nikula }
472a6052154SJarkko Nikula 
473a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
474a6052154SJarkko Nikula {
475a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
476a6052154SJarkko Nikula }
477a6052154SJarkko Nikula 
4786553bf06SLars-Peter Clausen 
4796553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
4806553bf06SLars-Peter Clausen {
4816553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4826553bf06SLars-Peter Clausen 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4836553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
4846553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
4856553bf06SLars-Peter Clausen 		return;
4866553bf06SLars-Peter Clausen 	}
4876553bf06SLars-Peter Clausen 
4886553bf06SLars-Peter Clausen 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4896553bf06SLars-Peter Clausen 				 &codec_list_fops))
4906553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4916553bf06SLars-Peter Clausen 
4926553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4936553bf06SLars-Peter Clausen 				 &dai_list_fops))
4946553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4956553bf06SLars-Peter Clausen 
4966553bf06SLars-Peter Clausen 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4976553bf06SLars-Peter Clausen 				 &platform_list_fops))
4986553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4996553bf06SLars-Peter Clausen }
5006553bf06SLars-Peter Clausen 
5016553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
5026553bf06SLars-Peter Clausen {
5036553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
5046553bf06SLars-Peter Clausen }
5056553bf06SLars-Peter Clausen 
5062624d5faSMark Brown #else
5072624d5faSMark Brown 
50881c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
50981c7cfd1SLars-Peter Clausen 
51081c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
51181c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
5122624d5faSMark Brown {
5132624d5faSMark Brown }
5142624d5faSMark Brown 
51581c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
51681c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
517731f1ab2SSebastien Guiriec {
518731f1ab2SSebastien Guiriec }
519731f1ab2SSebastien Guiriec 
520b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
521b95fccbcSAxel Lin {
522b95fccbcSAxel Lin }
523b95fccbcSAxel Lin 
524b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
525b95fccbcSAxel Lin {
526b95fccbcSAxel Lin }
5276553bf06SLars-Peter Clausen 
5286553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
5296553bf06SLars-Peter Clausen {
5306553bf06SLars-Peter Clausen }
5316553bf06SLars-Peter Clausen 
5326553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
5336553bf06SLars-Peter Clausen {
5346553bf06SLars-Peter Clausen }
5356553bf06SLars-Peter Clausen 
5362624d5faSMark Brown #endif
5372624d5faSMark Brown 
53847c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
53947c88fffSLiam Girdwood 		const char *dai_link, int stream)
54047c88fffSLiam Girdwood {
5411a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
54247c88fffSLiam Girdwood 
5431a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
5441a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
5451a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
5461a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
54747c88fffSLiam Girdwood 	}
548f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
54947c88fffSLiam Girdwood 	return NULL;
55047c88fffSLiam Girdwood }
55147c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
55247c88fffSLiam Girdwood 
5531a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
5541a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
5551a497983SMengdong Lin {
5561a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5571a497983SMengdong Lin 
5581a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
5591a497983SMengdong Lin 	if (!rtd)
5601a497983SMengdong Lin 		return NULL;
5611a497983SMengdong Lin 
5621a497983SMengdong Lin 	rtd->card = card;
5631a497983SMengdong Lin 	rtd->dai_link = dai_link;
5641a497983SMengdong Lin 	rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
5651a497983SMengdong Lin 					dai_link->num_codecs,
5661a497983SMengdong Lin 					GFP_KERNEL);
5671a497983SMengdong Lin 	if (!rtd->codec_dais) {
5681a497983SMengdong Lin 		kfree(rtd);
5691a497983SMengdong Lin 		return NULL;
5701a497983SMengdong Lin 	}
5711a497983SMengdong Lin 
5721a497983SMengdong Lin 	return rtd;
5731a497983SMengdong Lin }
5741a497983SMengdong Lin 
5751a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
5761a497983SMengdong Lin {
5771a497983SMengdong Lin 	if (rtd && rtd->codec_dais)
5781a497983SMengdong Lin 		kfree(rtd->codec_dais);
5791a497983SMengdong Lin 	kfree(rtd);
5801a497983SMengdong Lin }
5811a497983SMengdong Lin 
5821a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
5831a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
5841a497983SMengdong Lin {
5851a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
5861a497983SMengdong Lin 	rtd->num = card->num_rtd;
5871a497983SMengdong Lin 	card->num_rtd++;
5881a497983SMengdong Lin }
5891a497983SMengdong Lin 
5901a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
5911a497983SMengdong Lin {
5921a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
5931a497983SMengdong Lin 
5941a497983SMengdong Lin 	list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
5951a497983SMengdong Lin 		list_del(&rtd->list);
5961a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
5971a497983SMengdong Lin 	}
5981a497983SMengdong Lin 
5991a497983SMengdong Lin 	card->num_rtd = 0;
6001a497983SMengdong Lin }
6011a497983SMengdong Lin 
60247c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
60347c88fffSLiam Girdwood 		const char *dai_link)
60447c88fffSLiam Girdwood {
6051a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
60647c88fffSLiam Girdwood 
6071a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6081a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
6091a497983SMengdong Lin 			return rtd;
61047c88fffSLiam Girdwood 	}
611f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
61247c88fffSLiam Girdwood 	return NULL;
61347c88fffSLiam Girdwood }
61447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
61547c88fffSLiam Girdwood 
6169d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
6179d58a077SRichard Fitzgerald {
6189d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
6199d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
6209d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
6219d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
6229d58a077SRichard Fitzgerald 	 */
6239d58a077SRichard Fitzgerald }
6249d58a077SRichard Fitzgerald 
6256f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
626db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
6276f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
628db2a4165SFrank Mandarino {
6296f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
630d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6311a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6321a497983SMengdong Lin 	int i;
633db2a4165SFrank Mandarino 
634c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
635c5599b87SLars-Peter Clausen 	if (!card->instantiated)
636e3509ff0SDaniel Mack 		return 0;
637e3509ff0SDaniel Mack 
6386ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
6396ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
6406ed25978SAndy Green 	 */
641f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
642f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
643f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
6446ed25978SAndy Green 
6456ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
646f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6476ed25978SAndy Green 
648a00f90f9SMark Brown 	/* mute any active DACs */
6491a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6503efab7dcSMark Brown 
6511a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6523efab7dcSMark Brown 			continue;
6533efab7dcSMark Brown 
6541a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
6551a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
65688bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
65788bd870fSBenoit Cousson 
658f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
659f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
660db2a4165SFrank Mandarino 		}
66188bd870fSBenoit Cousson 	}
662db2a4165SFrank Mandarino 
6634ccab3e7SLiam Girdwood 	/* suspend all pcms */
6641a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6651a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6663efab7dcSMark Brown 			continue;
6673efab7dcSMark Brown 
6681a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
6693efab7dcSMark Brown 	}
6704ccab3e7SLiam Girdwood 
67187506549SMark Brown 	if (card->suspend_pre)
67270b2ac12SMark Brown 		card->suspend_pre(card);
673db2a4165SFrank Mandarino 
6741a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6751a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6763efab7dcSMark Brown 
6771a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6783efab7dcSMark Brown 			continue;
6793efab7dcSMark Brown 
680bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
681f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
682db2a4165SFrank Mandarino 	}
683db2a4165SFrank Mandarino 
68437660b6dSLars-Peter Clausen 	/* close any waiting streams */
6851a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
6861a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
687db2a4165SFrank Mandarino 
6881a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6893efab7dcSMark Brown 
6901a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6913efab7dcSMark Brown 			continue;
6923efab7dcSMark Brown 
6931a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6947bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
695db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
696f0fba2adSLiam Girdwood 
6971a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6987bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
699db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
700db2a4165SFrank Mandarino 	}
701db2a4165SFrank Mandarino 
7028be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7038be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
704e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
705e2d32ff6SMark Brown 
7069178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
707d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
708d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
709d9fc4063SKuninori Morimoto 
7109178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
7111547aba9SMark Brown 		 * bias _ON and should not be suspended. */
7129178feb4SKuninori Morimoto 		if (!component->suspended) {
7134890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7141547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
715125a25daSMark Brown 				/*
7169178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
717125a25daSMark Brown 				 * bias off then being in STANDBY
718125a25daSMark Brown 				 * means it's doing something,
719125a25daSMark Brown 				 * otherwise fall through.
720125a25daSMark Brown 				 */
7214890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
7229178feb4SKuninori Morimoto 					dev_dbg(component->dev,
72310e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
724125a25daSMark Brown 					break;
725125a25daSMark Brown 				}
726a8093297SLars-Peter Clausen 
7271547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
7289178feb4SKuninori Morimoto 				if (component->suspend)
7299178feb4SKuninori Morimoto 					component->suspend(component);
7309178feb4SKuninori Morimoto 				component->suspended = 1;
7319178feb4SKuninori Morimoto 				if (component->regmap)
7329178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
733988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
7349178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
7351547aba9SMark Brown 				break;
7361547aba9SMark Brown 			default:
7379178feb4SKuninori Morimoto 				dev_dbg(component->dev,
7389178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
7391547aba9SMark Brown 				break;
7401547aba9SMark Brown 			}
7411547aba9SMark Brown 		}
742f0fba2adSLiam Girdwood 	}
743db2a4165SFrank Mandarino 
7441a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7451a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7463efab7dcSMark Brown 
7471a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7483efab7dcSMark Brown 			continue;
7493efab7dcSMark Brown 
750bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
751f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
752988e8cc4SNicolin Chen 
753988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
754988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
755db2a4165SFrank Mandarino 	}
756db2a4165SFrank Mandarino 
75787506549SMark Brown 	if (card->suspend_post)
75870b2ac12SMark Brown 		card->suspend_post(card);
759db2a4165SFrank Mandarino 
760db2a4165SFrank Mandarino 	return 0;
761db2a4165SFrank Mandarino }
7626f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
763db2a4165SFrank Mandarino 
7646ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7656ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7666ed25978SAndy Green  */
7676ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
768db2a4165SFrank Mandarino {
769f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
770f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7711a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
772d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
7731a497983SMengdong Lin 	int i;
774db2a4165SFrank Mandarino 
7756ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7766ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7776ed25978SAndy Green 	 */
7786ed25978SAndy Green 
779f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7806ed25978SAndy Green 
7819949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
782f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7839949788bSMark Brown 
78487506549SMark Brown 	if (card->resume_pre)
78570b2ac12SMark Brown 		card->resume_pre(card);
786db2a4165SFrank Mandarino 
787bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
7881a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7891a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7903efab7dcSMark Brown 
7911a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7923efab7dcSMark Brown 			continue;
7933efab7dcSMark Brown 
794bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
795f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
796db2a4165SFrank Mandarino 	}
797db2a4165SFrank Mandarino 
798d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
7999178feb4SKuninori Morimoto 		if (component->suspended) {
8009178feb4SKuninori Morimoto 			if (component->resume)
8019178feb4SKuninori Morimoto 				component->resume(component);
8029178feb4SKuninori Morimoto 			component->suspended = 0;
8031547aba9SMark Brown 		}
804f0fba2adSLiam Girdwood 	}
805db2a4165SFrank Mandarino 
8061a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8073efab7dcSMark Brown 
8081a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8093efab7dcSMark Brown 			continue;
8103efab7dcSMark Brown 
8111a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
812d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
813db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
814f0fba2adSLiam Girdwood 
8151a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
816d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
817db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
818db2a4165SFrank Mandarino 	}
819db2a4165SFrank Mandarino 
8203ff3f64bSMark Brown 	/* unmute any active DACs */
8211a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8223efab7dcSMark Brown 
8231a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8243efab7dcSMark Brown 			continue;
8253efab7dcSMark Brown 
8261a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8271a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
82888bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
82988bd870fSBenoit Cousson 
830f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
831f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
832db2a4165SFrank Mandarino 		}
83388bd870fSBenoit Cousson 	}
834db2a4165SFrank Mandarino 
8351a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8361a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8373efab7dcSMark Brown 
8381a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8393efab7dcSMark Brown 			continue;
8403efab7dcSMark Brown 
841bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
842f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
843db2a4165SFrank Mandarino 	}
844db2a4165SFrank Mandarino 
84587506549SMark Brown 	if (card->resume_post)
84670b2ac12SMark Brown 		card->resume_post(card);
847db2a4165SFrank Mandarino 
848f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8496ed25978SAndy Green 
8508be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8518be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
852e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8531a7aaa58SJeeja KP 
8541a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
8551a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
8566ed25978SAndy Green }
8576ed25978SAndy Green 
8586ed25978SAndy Green /* powers up audio subsystem after a suspend */
8596f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8606ed25978SAndy Green {
8616f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
862bc263214SLars-Peter Clausen 	bool bus_control = false;
8631a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
864b9dd94a8SPeter Ujfalusi 
865c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
866c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8675ff1ddf2SEric Miao 		return 0;
8685ff1ddf2SEric Miao 
869988e8cc4SNicolin Chen 	/* activate pins from sleep state */
8701a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
87188bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
87288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87388bd870fSBenoit Cousson 		int j;
87488bd870fSBenoit Cousson 
875988e8cc4SNicolin Chen 		if (cpu_dai->active)
876988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
87788bd870fSBenoit Cousson 
87888bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
87988bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
880988e8cc4SNicolin Chen 			if (codec_dai->active)
881988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
882988e8cc4SNicolin Chen 		}
88388bd870fSBenoit Cousson 	}
884988e8cc4SNicolin Chen 
885bc263214SLars-Peter Clausen 	/*
886bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
887bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
888bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
88964ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
89064ab9baaSMark Brown 	 */
8911a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8921a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
893bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
89482e14e8bSStephen Warren 	}
895bc263214SLars-Peter Clausen 	if (bus_control) {
896bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
89764ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
89864ab9baaSMark Brown 	} else {
899f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
9006308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
901f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
902f0fba2adSLiam Girdwood 	}
9036ed25978SAndy Green 
904db2a4165SFrank Mandarino 	return 0;
905db2a4165SFrank Mandarino }
9066f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
907db2a4165SFrank Mandarino #else
9086f8ab4acSMark Brown #define snd_soc_suspend NULL
9096f8ab4acSMark Brown #define snd_soc_resume NULL
910db2a4165SFrank Mandarino #endif
911db2a4165SFrank Mandarino 
91285e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
91302a06d30SBarry Song };
91402a06d30SBarry Song 
91565d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
91665d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
91712023a9aSMisael Lopez Cruz {
91865d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
91912023a9aSMisael Lopez Cruz 
92034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
92134e81ab4SLars-Peter Clausen 
92265d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
92365d9361fSLars-Peter Clausen 		if (of_node) {
92465d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
92565d9361fSLars-Peter Clausen 				return component;
92665d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
92765d9361fSLars-Peter Clausen 			return component;
92812023a9aSMisael Lopez Cruz 		}
92912023a9aSMisael Lopez Cruz 	}
93012023a9aSMisael Lopez Cruz 
93112023a9aSMisael Lopez Cruz 	return NULL;
93212023a9aSMisael Lopez Cruz }
93312023a9aSMisael Lopez Cruz 
934fbb88b5cSMengdong Lin /**
935fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
936fbb88b5cSMengdong Lin  *
937fbb88b5cSMengdong Lin  * @dlc: name of the DAI and optional component info to match
938fbb88b5cSMengdong Lin  *
939ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
940fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
941fbb88b5cSMengdong Lin  * should also match if being specified.
942fbb88b5cSMengdong Lin  *
943fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
944fbb88b5cSMengdong Lin  */
945305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
94614621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
94712023a9aSMisael Lopez Cruz {
94814621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
94914621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
9503e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
95112023a9aSMisael Lopez Cruz 
95234e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
95334e81ab4SLars-Peter Clausen 
95414621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
95514621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
9563e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
9573e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
9583e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
9593e0aa8d8SJyri Sarha 
9603e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
96112023a9aSMisael Lopez Cruz 			continue;
9621ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
96312023a9aSMisael Lopez Cruz 			continue;
96414621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
96514621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
96614621c7eSLars-Peter Clausen 				continue;
96712023a9aSMisael Lopez Cruz 
96814621c7eSLars-Peter Clausen 			return dai;
96912023a9aSMisael Lopez Cruz 		}
97012023a9aSMisael Lopez Cruz 	}
97112023a9aSMisael Lopez Cruz 
97212023a9aSMisael Lopez Cruz 	return NULL;
97312023a9aSMisael Lopez Cruz }
974305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
97512023a9aSMisael Lopez Cruz 
97617fb1755SMengdong Lin 
97717fb1755SMengdong Lin /**
97817fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
97917fb1755SMengdong Lin  *
98017fb1755SMengdong Lin  * @card: soc card
98117fb1755SMengdong Lin  * @id: DAI link ID to match
98217fb1755SMengdong Lin  * @name: DAI link name to match, optional
9838abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
98417fb1755SMengdong Lin  *
98517fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
98617fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
98717fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
98817fb1755SMengdong Lin  * specified.
98917fb1755SMengdong Lin  *
99017fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
99117fb1755SMengdong Lin  */
99217fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
99317fb1755SMengdong Lin 					       int id, const char *name,
99417fb1755SMengdong Lin 					       const char *stream_name)
99517fb1755SMengdong Lin {
99617fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
99717fb1755SMengdong Lin 
99817fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
99917fb1755SMengdong Lin 
100017fb1755SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
100117fb1755SMengdong Lin 		if (link->id != id)
100217fb1755SMengdong Lin 			continue;
100317fb1755SMengdong Lin 
100417fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
100517fb1755SMengdong Lin 			continue;
100617fb1755SMengdong Lin 
100717fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
100817fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
100917fb1755SMengdong Lin 			continue;
101017fb1755SMengdong Lin 
101117fb1755SMengdong Lin 		return link;
101217fb1755SMengdong Lin 	}
101317fb1755SMengdong Lin 
101417fb1755SMengdong Lin 	return NULL;
101517fb1755SMengdong Lin }
101617fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
101717fb1755SMengdong Lin 
101849a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
101949a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
1020db2a4165SFrank Mandarino {
102149a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
102249a5ba1cSMengdong Lin 
102349a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
102449a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
102549a5ba1cSMengdong Lin 			return true;
102649a5ba1cSMengdong Lin 	}
102749a5ba1cSMengdong Lin 
102849a5ba1cSMengdong Lin 	return false;
102949a5ba1cSMengdong Lin }
103049a5ba1cSMengdong Lin 
10316f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10326f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1033db2a4165SFrank Mandarino {
10341a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
103588bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
103614621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
10371a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
1038435c5e25SMark Brown 	struct snd_soc_platform *platform;
103906859fcaSCharles Keepax 	struct device_node *platform_of_node;
1040848dd8beSMark Brown 	const char *platform_name;
104188bd870fSBenoit Cousson 	int i;
1042db2a4165SFrank Mandarino 
10436f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10446308419aSMark Brown 
104549a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
104649a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
104749a5ba1cSMengdong Lin 			dai_link->name);
104849a5ba1cSMengdong Lin 		return 0;
104949a5ba1cSMengdong Lin 	}
1050db2a4165SFrank Mandarino 
1051513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1052513cb311SSudip Mukherjee 	if (!rtd)
1053513cb311SSudip Mukherjee 		return -ENOMEM;
1054513cb311SSudip Mukherjee 
105514621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
105614621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
105714621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
105814621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1059b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1060f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1061f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
10621a497983SMengdong Lin 		goto _err_defer;
1063c5af3a2eSMark Brown 	}
1064c5af3a2eSMark Brown 
106588bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
106688bd870fSBenoit Cousson 
106788bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
10681a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
106988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
107014621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
107188bd870fSBenoit Cousson 		if (!codec_dais[i]) {
107212023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
107388bd870fSBenoit Cousson 				codecs[i].dai_name);
10741a497983SMengdong Lin 			goto _err_defer;
107512023a9aSMisael Lopez Cruz 		}
107688bd870fSBenoit Cousson 	}
107788bd870fSBenoit Cousson 
107888bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
107988bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
108088bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
108112023a9aSMisael Lopez Cruz 
1082848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1083848dd8beSMark Brown 	platform_name = dai_link->platform_name;
10845a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1085848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1086848dd8beSMark Brown 
1087b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
1088f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
108906859fcaSCharles Keepax 		platform_of_node = platform->dev->of_node;
109006859fcaSCharles Keepax 		if (!platform_of_node && platform->dev->parent->of_node)
109106859fcaSCharles Keepax 			platform_of_node = platform->dev->parent->of_node;
109206859fcaSCharles Keepax 
10935a504963SStephen Warren 		if (dai_link->platform_of_node) {
109406859fcaSCharles Keepax 			if (platform_of_node != dai_link->platform_of_node)
10955a504963SStephen Warren 				continue;
10965a504963SStephen Warren 		} else {
1097f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
10982610ab77SStephen Warren 				continue;
10995a504963SStephen Warren 		}
11002610ab77SStephen Warren 
1101f0fba2adSLiam Girdwood 		rtd->platform = platform;
1102f0fba2adSLiam Girdwood 	}
1103b19e6e7bSMark Brown 	if (!rtd->platform) {
1104f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1105f0fba2adSLiam Girdwood 			dai_link->platform_name);
110670fcad49SCharles Keepax 		goto _err_defer;
1107f0fba2adSLiam Girdwood 	}
1108b19e6e7bSMark Brown 
11091a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1110b19e6e7bSMark Brown 	return 0;
11111a497983SMengdong Lin 
11121a497983SMengdong Lin _err_defer:
11131a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11141a497983SMengdong Lin 	return  -EPROBE_DEFER;
11156b05eda6SMark Brown }
11166b05eda6SMark Brown 
1117f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1118d12cd198SStephen Warren {
1119abd31b32SLars-Peter Clausen 	if (!component->card)
112070090bbbSLars-Peter Clausen 		return;
1121d12cd198SStephen Warren 
1122d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1123d12cd198SStephen Warren 
1124f1d45cc3SLars-Peter Clausen 	if (component->remove)
1125f1d45cc3SLars-Peter Clausen 		component->remove(component);
1126d12cd198SStephen Warren 
1127f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1128d12cd198SStephen Warren 
1129f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1130abd31b32SLars-Peter Clausen 	component->card = NULL;
1131f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1132d12cd198SStephen Warren }
1133d12cd198SStephen Warren 
1134e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1135589c3563SJarkko Nikula {
1136589c3563SJarkko Nikula 	int err;
1137589c3563SJarkko Nikula 
1138e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1139e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1140e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1141e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1142589c3563SJarkko Nikula 			if (err < 0)
1143e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1144b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1145e60cd14fSLars-Peter Clausen 					dai->name, err);
1146b0aa88afSMisael Lopez Cruz 		}
1147e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1148b0aa88afSMisael Lopez Cruz 	}
1149b0aa88afSMisael Lopez Cruz }
1150b0aa88afSMisael Lopez Cruz 
11511a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11521a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1153f0fba2adSLiam Girdwood {
1154e60cd14fSLars-Peter Clausen 	int i;
1155f0fba2adSLiam Girdwood 
1156f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1157f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
115836ae1a96SMark Brown 		device_unregister(rtd->dev);
1159f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
116002a06d30SBarry Song 	}
116102a06d30SBarry Song 
1162f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
116388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1164e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1165f0fba2adSLiam Girdwood 
1166e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1167f0fba2adSLiam Girdwood }
1168f0fba2adSLiam Girdwood 
11691a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11701a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
117162ae68faSStephen Warren {
117262ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
117362ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
117461aca564SLars-Peter Clausen 	struct snd_soc_component *component;
117588bd870fSBenoit Cousson 	int i;
117662ae68faSStephen Warren 
117762ae68faSStephen Warren 	/* remove the platform */
117870090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1179f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
118062ae68faSStephen Warren 
118162ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
118288bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
118361aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
118470090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
118561aca564SLars-Peter Clausen 			soc_remove_component(component);
118662ae68faSStephen Warren 	}
118762ae68faSStephen Warren 
118862ae68faSStephen Warren 	/* remove any CPU-side CODEC */
118962ae68faSStephen Warren 	if (cpu_dai) {
119070090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
119161aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
119262ae68faSStephen Warren 	}
119362ae68faSStephen Warren }
119462ae68faSStephen Warren 
11950671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11960671fd8eSKuninori Morimoto {
11971a497983SMengdong Lin 	int order;
11981a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1199f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
12000671fd8eSKuninori Morimoto 
12010168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
12020168bf0dSLiam Girdwood 			order++) {
12031a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12041a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
12050168bf0dSLiam Girdwood 	}
120662ae68faSStephen Warren 
120762ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
120862ae68faSStephen Warren 			order++) {
12091a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12101a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
121162ae68faSStephen Warren 	}
121262ae68faSStephen Warren 
1213f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1214f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1215f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1216f8f80361SMengdong Lin 				link->name);
1217f8f80361SMengdong Lin 
1218f8f80361SMengdong Lin 		list_del(&link->list);
1219f8f80361SMengdong Lin 		card->num_dai_links--;
12200671fd8eSKuninori Morimoto 	}
12210671fd8eSKuninori Morimoto }
12220671fd8eSKuninori Morimoto 
1223923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1224923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1225923c5e61SMengdong Lin {
1226923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1227923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1228923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1229923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1230923c5e61SMengdong Lin 
1231923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1232923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1233923c5e61SMengdong Lin 				GFP_KERNEL);
1234923c5e61SMengdong Lin 		if (!dai_link->codecs)
1235923c5e61SMengdong Lin 			return -ENOMEM;
1236923c5e61SMengdong Lin 
1237923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1238923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1239923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1240923c5e61SMengdong Lin 	}
1241923c5e61SMengdong Lin 
1242923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1243923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1244923c5e61SMengdong Lin 		return -EINVAL;
1245923c5e61SMengdong Lin 	}
1246923c5e61SMengdong Lin 
1247923c5e61SMengdong Lin 	return 0;
1248923c5e61SMengdong Lin }
1249923c5e61SMengdong Lin 
1250923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1251923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1252923c5e61SMengdong Lin {
1253923c5e61SMengdong Lin 	int i, ret;
1254923c5e61SMengdong Lin 
1255923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1256923c5e61SMengdong Lin 	if (ret) {
1257923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1258923c5e61SMengdong Lin 		return ret;
1259923c5e61SMengdong Lin 	}
1260923c5e61SMengdong Lin 
1261923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1262923c5e61SMengdong Lin 		/*
1263923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1264923c5e61SMengdong Lin 		 * not both or neither.
1265923c5e61SMengdong Lin 		 */
1266923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1267923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1268923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1269923c5e61SMengdong Lin 				link->name);
1270923c5e61SMengdong Lin 			return -EINVAL;
1271923c5e61SMengdong Lin 		}
1272923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1273923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1274923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1275923c5e61SMengdong Lin 				link->name);
1276923c5e61SMengdong Lin 			return -EINVAL;
1277923c5e61SMengdong Lin 		}
1278923c5e61SMengdong Lin 	}
1279923c5e61SMengdong Lin 
1280923c5e61SMengdong Lin 	/*
1281923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1282923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1283923c5e61SMengdong Lin 	 */
1284923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1285923c5e61SMengdong Lin 		dev_err(card->dev,
1286923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1287923c5e61SMengdong Lin 			link->name);
1288923c5e61SMengdong Lin 		return -EINVAL;
1289923c5e61SMengdong Lin 	}
1290923c5e61SMengdong Lin 
1291923c5e61SMengdong Lin 	/*
1292923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1293923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1294923c5e61SMengdong Lin 	 * name alone..
1295923c5e61SMengdong Lin 	 */
1296923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1297923c5e61SMengdong Lin 		dev_err(card->dev,
1298923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1299923c5e61SMengdong Lin 			link->name);
1300923c5e61SMengdong Lin 		return -EINVAL;
1301923c5e61SMengdong Lin 	}
1302923c5e61SMengdong Lin 	/*
1303923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1304923c5e61SMengdong Lin 	 * specified
1305923c5e61SMengdong Lin 	 */
1306923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1307923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1308923c5e61SMengdong Lin 		dev_err(card->dev,
1309923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1310923c5e61SMengdong Lin 			link->name);
1311923c5e61SMengdong Lin 		return -EINVAL;
1312923c5e61SMengdong Lin 	}
1313923c5e61SMengdong Lin 
1314923c5e61SMengdong Lin 	return 0;
1315923c5e61SMengdong Lin }
1316923c5e61SMengdong Lin 
1317f8f80361SMengdong Lin /**
1318f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1319f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1320f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1321f8f80361SMengdong Lin  *
1322f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1323f8f80361SMengdong Lin  *
1324f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1325f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1326f8f80361SMengdong Lin  * DAI links in dai_link array.
1327f8f80361SMengdong Lin  */
1328f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1329f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1330f8f80361SMengdong Lin {
1331f8f80361SMengdong Lin 	if (dai_link->dobj.type
1332f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1333f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1334f8f80361SMengdong Lin 			dai_link->dobj.type);
1335f8f80361SMengdong Lin 		return -EINVAL;
1336f8f80361SMengdong Lin 	}
1337f8f80361SMengdong Lin 
1338f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1339d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1340d6f220eaSMengdong Lin 	 * on the link created by topology.
1341d6f220eaSMengdong Lin 	 */
1342d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1343d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1344d6f220eaSMengdong Lin 
1345f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1346f8f80361SMengdong Lin 	card->num_dai_links++;
1347f8f80361SMengdong Lin 
1348f8f80361SMengdong Lin 	return 0;
1349f8f80361SMengdong Lin }
1350f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1351f8f80361SMengdong Lin 
1352f8f80361SMengdong Lin /**
1353f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1354f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1355f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1356f8f80361SMengdong Lin  *
1357f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1358f8f80361SMengdong Lin  *
1359f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1360f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1361f8f80361SMengdong Lin  */
1362f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1363f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1364f8f80361SMengdong Lin {
1365f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1366f8f80361SMengdong Lin 
1367f8f80361SMengdong Lin 	if (dai_link->dobj.type
1368f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1369f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1370f8f80361SMengdong Lin 			dai_link->dobj.type);
1371f8f80361SMengdong Lin 		return;
1372f8f80361SMengdong Lin 	}
1373f8f80361SMengdong Lin 
1374f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1375d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1376d6f220eaSMengdong Lin 	 * on the link created by topology.
1377d6f220eaSMengdong Lin 	 */
1378d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1379d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1380d6f220eaSMengdong Lin 
1381f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1382f8f80361SMengdong Lin 		if (link == dai_link) {
1383f8f80361SMengdong Lin 			list_del(&link->list);
1384f8f80361SMengdong Lin 			card->num_dai_links--;
1385f8f80361SMengdong Lin 			return;
1386f8f80361SMengdong Lin 		}
1387f8f80361SMengdong Lin 	}
1388f8f80361SMengdong Lin }
1389f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1390f0fba2adSLiam Girdwood 
1391ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
139294f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1393ead9b919SJarkko Nikula {
1394ead9b919SJarkko Nikula 	int i;
1395ead9b919SJarkko Nikula 
1396ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1397ead9b919SJarkko Nikula 		return;
1398ead9b919SJarkko Nikula 
1399ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1400ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
140194f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
14023ca041edSSebastian Reichel 			continue;
140394f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
14043ca041edSSebastian Reichel 			continue;
140594f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1406ead9b919SJarkko Nikula 		break;
1407ead9b919SJarkko Nikula 	}
1408ead9b919SJarkko Nikula }
1409ead9b919SJarkko Nikula 
1410f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1411f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1412589c3563SJarkko Nikula {
1413f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1414888df395SMark Brown 	struct snd_soc_dai *dai;
1415f1d45cc3SLars-Peter Clausen 	int ret;
1416589c3563SJarkko Nikula 
14171b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
141870090bbbSLars-Peter Clausen 		return 0;
1419589c3563SJarkko Nikula 
1420abd31b32SLars-Peter Clausen 	if (component->card) {
14211b7c1231SLars-Peter Clausen 		if (component->card != card) {
14221b7c1231SLars-Peter Clausen 			dev_err(component->dev,
14231b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
14241b7c1231SLars-Peter Clausen 				card->name, component->card->name);
14251b7c1231SLars-Peter Clausen 			return -ENODEV;
14261b7c1231SLars-Peter Clausen 		}
14271b7c1231SLars-Peter Clausen 		return 0;
14281b7c1231SLars-Peter Clausen 	}
14291b7c1231SLars-Peter Clausen 
1430abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1431abd31b32SLars-Peter Clausen 		return -ENODEV;
1432abd31b32SLars-Peter Clausen 
1433f1d45cc3SLars-Peter Clausen 	component->card = card;
1434f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1435f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1436589c3563SJarkko Nikula 
1437f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1438d5d1e0beSLars-Peter Clausen 
1439f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1440f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1441f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
144277530150SLars-Peter Clausen 
1443b318ad50SNariman Poushin 		if (ret != 0) {
1444f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1445b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1446b318ad50SNariman Poushin 			goto err_probe;
1447b318ad50SNariman Poushin 		}
1448b318ad50SNariman Poushin 	}
1449b318ad50SNariman Poushin 
14500634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
14510634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1452261edc70SNariman Poushin 		if (ret != 0) {
1453f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1454261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1455261edc70SNariman Poushin 			goto err_probe;
1456261edc70SNariman Poushin 		}
1457261edc70SNariman Poushin 	}
1458888df395SMark Brown 
1459f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1460f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1461589c3563SJarkko Nikula 		if (ret < 0) {
1462f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1463f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
146470d29331SJarkko Nikula 			goto err_probe;
1465589c3563SJarkko Nikula 		}
1466cb2cf612SLiam Girdwood 
1467f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1468f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1469956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1470f1d45cc3SLars-Peter Clausen 			component->name);
1471956245e9SLiam Girdwood 	}
1472956245e9SLiam Girdwood 
1473f2ed6b07SMengdong Lin 	/* machine specific init */
1474f2ed6b07SMengdong Lin 	if (component->init) {
1475f2ed6b07SMengdong Lin 		ret = component->init(component);
1476f2ed6b07SMengdong Lin 		if (ret < 0) {
1477f2ed6b07SMengdong Lin 			dev_err(component->dev,
1478f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1479f2ed6b07SMengdong Lin 			goto err_probe;
1480f2ed6b07SMengdong Lin 		}
1481f2ed6b07SMengdong Lin 	}
1482f2ed6b07SMengdong Lin 
1483f1d45cc3SLars-Peter Clausen 	if (component->controls)
1484f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1485f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1486f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1487f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1488f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1489956245e9SLiam Girdwood 
1490f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1491d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1492956245e9SLiam Girdwood 
1493956245e9SLiam Girdwood 	return 0;
1494956245e9SLiam Girdwood 
1495956245e9SLiam Girdwood err_probe:
1496f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1497abd31b32SLars-Peter Clausen 	component->card = NULL;
1498f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1499956245e9SLiam Girdwood 
1500956245e9SLiam Girdwood 	return ret;
1501956245e9SLiam Girdwood }
1502956245e9SLiam Girdwood 
150336ae1a96SMark Brown static void rtd_release(struct device *dev)
150436ae1a96SMark Brown {
150536ae1a96SMark Brown 	kfree(dev);
150636ae1a96SMark Brown }
1507f0fba2adSLiam Girdwood 
15085f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
15095f3484acSLars-Peter Clausen 	const char *name)
1510503ae5e0SMisael Lopez Cruz {
1511589c3563SJarkko Nikula 	int ret = 0;
1512589c3563SJarkko Nikula 
1513589c3563SJarkko Nikula 	/* register the rtd device */
151436ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
151536ae1a96SMark Brown 	if (!rtd->dev)
151636ae1a96SMark Brown 		return -ENOMEM;
151736ae1a96SMark Brown 	device_initialize(rtd->dev);
15185f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
151936ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1520d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1521f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
152236ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1523b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
152401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
152501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
152601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
152701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
152836ae1a96SMark Brown 	ret = device_add(rtd->dev);
1529589c3563SJarkko Nikula 	if (ret < 0) {
1530865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1531865df9cbSChuansheng Liu 		put_device(rtd->dev);
15325f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1533f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1534589c3563SJarkko Nikula 		return ret;
1535589c3563SJarkko Nikula 	}
1536589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1537589c3563SJarkko Nikula 	return 0;
1538589c3563SJarkko Nikula }
1539589c3563SJarkko Nikula 
15401a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
15411a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
154262ae68faSStephen Warren 				     int order)
154362ae68faSStephen Warren {
154462ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1545f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
154688bd870fSBenoit Cousson 	int i, ret;
154762ae68faSStephen Warren 
154862ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
154961aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
155070090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1551f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
155262ae68faSStephen Warren 		if (ret < 0)
155362ae68faSStephen Warren 			return ret;
155462ae68faSStephen Warren 	}
155562ae68faSStephen Warren 
155688bd870fSBenoit Cousson 	/* probe the CODEC-side components */
155788bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
155861aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
155970090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1560f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
156162ae68faSStephen Warren 			if (ret < 0)
156262ae68faSStephen Warren 				return ret;
156362ae68faSStephen Warren 		}
156488bd870fSBenoit Cousson 	}
156562ae68faSStephen Warren 
156662ae68faSStephen Warren 	/* probe the platform */
156770090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1568f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
156962ae68faSStephen Warren 		if (ret < 0)
157062ae68faSStephen Warren 			return ret;
157162ae68faSStephen Warren 	}
157262ae68faSStephen Warren 
157362ae68faSStephen Warren 	return 0;
157462ae68faSStephen Warren }
157562ae68faSStephen Warren 
15768e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1577b0aa88afSMisael Lopez Cruz {
1578b0aa88afSMisael Lopez Cruz 	int ret;
1579b0aa88afSMisael Lopez Cruz 
15808e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
15818e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
15828e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1583b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
15848e2be562SLars-Peter Clausen 				dev_err(dai->dev,
15858e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
15868e2be562SLars-Peter Clausen 					dai->name, ret);
1587b0aa88afSMisael Lopez Cruz 				return ret;
1588b0aa88afSMisael Lopez Cruz 			}
1589b0aa88afSMisael Lopez Cruz 		}
1590b0aa88afSMisael Lopez Cruz 
15918e2be562SLars-Peter Clausen 		dai->probed = 1;
1592b0aa88afSMisael Lopez Cruz 	}
1593b0aa88afSMisael Lopez Cruz 
1594b0aa88afSMisael Lopez Cruz 	return 0;
1595b0aa88afSMisael Lopez Cruz }
1596b0aa88afSMisael Lopez Cruz 
159725f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
159825f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
159925f7b701SArnaud Pouliquen {
160025f7b701SArnaud Pouliquen 	int i, ret = 0;
160125f7b701SArnaud Pouliquen 
160225f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
160325f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
160425f7b701SArnaud Pouliquen 
160525f7b701SArnaud Pouliquen 		if (!rtd->dai_link->no_pcm && drv->pcm_new)
160625f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
160725f7b701SArnaud Pouliquen 		if (ret < 0) {
160825f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
160925f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
161025f7b701SArnaud Pouliquen 				dais[i]->name);
161125f7b701SArnaud Pouliquen 			return ret;
161225f7b701SArnaud Pouliquen 		}
161325f7b701SArnaud Pouliquen 	}
161425f7b701SArnaud Pouliquen 
161525f7b701SArnaud Pouliquen 	return 0;
161625f7b701SArnaud Pouliquen }
161725f7b701SArnaud Pouliquen 
16182436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
16192436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
16203f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
16212436a723SMisael Lopez Cruz {
16223f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16233f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
16243de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
16252436a723SMisael Lopez Cruz 	int ret;
16262436a723SMisael Lopez Cruz 
162788bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
162888bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
162988bd870fSBenoit Cousson 
16302436a723SMisael Lopez Cruz 	/* link the DAI widgets */
16313de7c420SVinod Koul 	sink = codec_dai->playback_widget;
16323de7c420SVinod Koul 	source = cpu_dai->capture_widget;
16333de7c420SVinod Koul 	if (sink && source) {
16342436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16353de7c420SVinod Koul 					   dai_link->num_params,
16363de7c420SVinod Koul 					   source, sink);
16372436a723SMisael Lopez Cruz 		if (ret != 0) {
16382436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16393de7c420SVinod Koul 				sink->name, source->name, ret);
16402436a723SMisael Lopez Cruz 			return ret;
16412436a723SMisael Lopez Cruz 		}
16422436a723SMisael Lopez Cruz 	}
16432436a723SMisael Lopez Cruz 
16443de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
16453de7c420SVinod Koul 	source = codec_dai->capture_widget;
16463de7c420SVinod Koul 	if (sink && source) {
16472436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16483de7c420SVinod Koul 					   dai_link->num_params,
16493de7c420SVinod Koul 					   source, sink);
16502436a723SMisael Lopez Cruz 		if (ret != 0) {
16512436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16523de7c420SVinod Koul 				sink->name, source->name, ret);
16532436a723SMisael Lopez Cruz 			return ret;
16542436a723SMisael Lopez Cruz 		}
16552436a723SMisael Lopez Cruz 	}
16562436a723SMisael Lopez Cruz 
16572436a723SMisael Lopez Cruz 	return 0;
16582436a723SMisael Lopez Cruz }
16592436a723SMisael Lopez Cruz 
16601a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
16611a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1662f0fba2adSLiam Girdwood {
16631a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1664c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
166588bd870fSBenoit Cousson 	int i, ret;
1666f0fba2adSLiam Girdwood 
1667f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
16681a497983SMengdong Lin 			card->name, rtd->num, order);
1669f0fba2adSLiam Girdwood 
1670f0fba2adSLiam Girdwood 	/* set default power off timeout */
1671f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1672f0fba2adSLiam Girdwood 
16738e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
16748e2be562SLars-Peter Clausen 	if (ret)
1675f0fba2adSLiam Girdwood 		return ret;
1676f0fba2adSLiam Girdwood 
1677f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
167888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
16798e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1680b0aa88afSMisael Lopez Cruz 		if (ret)
1681f0fba2adSLiam Girdwood 			return ret;
168288bd870fSBenoit Cousson 	}
1683f0fba2adSLiam Girdwood 
16840168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
16850168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
16860168bf0dSLiam Girdwood 		return 0;
16870168bf0dSLiam Girdwood 
16885f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16895f3484acSLars-Peter Clausen 	if (dai_link->init) {
16905f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
16915f3484acSLars-Peter Clausen 		if (ret < 0) {
16925f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16935f3484acSLars-Peter Clausen 				dai_link->name, ret);
16945f3484acSLars-Peter Clausen 			return ret;
16955f3484acSLars-Peter Clausen 		}
16965f3484acSLars-Peter Clausen 	}
16975f3484acSLars-Peter Clausen 
1698a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1699a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1700a5053a8eSKuninori Morimoto 
17015f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1702589c3563SJarkko Nikula 	if (ret)
1703f0fba2adSLiam Girdwood 		return ret;
1704f0fba2adSLiam Girdwood 
17055f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
17065f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
17072e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
17082e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
17095f3484acSLars-Peter Clausen #endif
17105f3484acSLars-Peter Clausen 
17116f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
17121245b700SNamarta Kohli 		/*create compress_device"*/
17131a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
17141245b700SNamarta Kohli 		if (ret < 0) {
1715f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
17161245b700SNamarta Kohli 					 dai_link->stream_name);
17171245b700SNamarta Kohli 			return ret;
17181245b700SNamarta Kohli 		}
17191245b700SNamarta Kohli 	} else {
17201245b700SNamarta Kohli 
1721c74184edSMark Brown 		if (!dai_link->params) {
1722f0fba2adSLiam Girdwood 			/* create the pcm */
17231a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1724f0fba2adSLiam Girdwood 			if (ret < 0) {
1725f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
17260837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1727f0fba2adSLiam Girdwood 				return ret;
1728f0fba2adSLiam Girdwood 			}
172925f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
173025f7b701SArnaud Pouliquen 			if (ret < 0)
173125f7b701SArnaud Pouliquen 				return ret;
173225f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(rtd->codec_dais,
173325f7b701SArnaud Pouliquen 						   rtd->num_codecs, rtd);
173425f7b701SArnaud Pouliquen 			if (ret < 0)
173525f7b701SArnaud Pouliquen 				return ret;
1736c74184edSMark Brown 		} else {
17379d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
17389d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
17399d58a077SRichard Fitzgerald 
1740c74184edSMark Brown 			/* link the DAI widgets */
17413f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
17422436a723SMisael Lopez Cruz 			if (ret)
1743c74184edSMark Brown 				return ret;
1744c74184edSMark Brown 		}
1745c74184edSMark Brown 	}
1746c74184edSMark Brown 
1747f0fba2adSLiam Girdwood 	return 0;
1748f0fba2adSLiam Girdwood }
1749f0fba2adSLiam Girdwood 
175044c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1751b19e6e7bSMark Brown {
17523ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1753f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1754f2ed6b07SMengdong Lin 	const char *name;
1755f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
17563ca041edSSebastian Reichel 
1757f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1758f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1759f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1760f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1761f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1762f2ed6b07SMengdong Lin 		if (!component) {
1763f2ed6b07SMengdong Lin 			if (codec_of_node)
1764f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1765f2ed6b07SMengdong Lin 			goto err_defer;
1766f2ed6b07SMengdong Lin 		}
1767f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1768f2ed6b07SMengdong Lin 		/* generic components */
1769f2ed6b07SMengdong Lin 		name = aux_dev->name;
1770f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1771f2ed6b07SMengdong Lin 		if (!component)
1772f2ed6b07SMengdong Lin 			goto err_defer;
1773f2ed6b07SMengdong Lin 	} else {
1774f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1775f2ed6b07SMengdong Lin 		return -EINVAL;
1776f2ed6b07SMengdong Lin 	}
17773ca041edSSebastian Reichel 
1778f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1779d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
17801a653aa4SKuninori Morimoto 
1781f2ed6b07SMengdong Lin 	return 0;
1782f2ed6b07SMengdong Lin 
1783f2ed6b07SMengdong Lin err_defer:
178465d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1785b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1786b19e6e7bSMark Brown }
1787b19e6e7bSMark Brown 
1788f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1789f2ed6b07SMengdong Lin {
1790991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1791f2ed6b07SMengdong Lin 	int order;
1792f2ed6b07SMengdong Lin 	int ret;
1793f2ed6b07SMengdong Lin 
1794f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1795f2ed6b07SMengdong Lin 		order++) {
1796991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1797f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1798f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1799f2ed6b07SMengdong Lin 				if (ret < 0) {
1800f2ed6b07SMengdong Lin 					dev_err(card->dev,
1801f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1802f2ed6b07SMengdong Lin 						comp->name, ret);
1803f2ed6b07SMengdong Lin 					return ret;
1804f2ed6b07SMengdong Lin 				}
1805f2ed6b07SMengdong Lin 			}
1806f2ed6b07SMengdong Lin 		}
1807f2ed6b07SMengdong Lin 	}
180865d9361fSLars-Peter Clausen 
180944c69bb1SLars-Peter Clausen 	return 0;
18103ca041edSSebastian Reichel }
18112eea392dSJarkko Nikula 
1812f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
181344c69bb1SLars-Peter Clausen {
1814f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1815f2ed6b07SMengdong Lin 	int order;
181644c69bb1SLars-Peter Clausen 
1817f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1818f2ed6b07SMengdong Lin 		order++) {
1819f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1820991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
18211a653aa4SKuninori Morimoto 
1822f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1823f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1824991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1825991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
18262eea392dSJarkko Nikula 			}
18275f3484acSLars-Peter Clausen 		}
18282eea392dSJarkko Nikula 	}
18292eea392dSJarkko Nikula }
18302eea392dSJarkko Nikula 
1831f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1832fdf0f54dSDimitris Papastamos {
1833fdf0f54dSDimitris Papastamos 	int ret;
1834fdf0f54dSDimitris Papastamos 
1835fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1836fdf0f54dSDimitris Papastamos 		return 0;
1837fdf0f54dSDimitris Papastamos 
1838fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1839fdf0f54dSDimitris Papastamos 	if (ret < 0) {
184010e8aa9aSMichał Mirosław 		dev_err(codec->dev,
184110e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
184210e8aa9aSMichał Mirosław 			ret);
1843fdf0f54dSDimitris Papastamos 		return ret;
1844fdf0f54dSDimitris Papastamos 	}
1845fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1846fdf0f54dSDimitris Papastamos 	return 0;
1847fdf0f54dSDimitris Papastamos }
1848fdf0f54dSDimitris Papastamos 
1849ce64c8b9SLars-Peter Clausen /**
1850ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1851ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1852ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1853ce64c8b9SLars-Peter Clausen  *
1854ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1855ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1856ce64c8b9SLars-Peter Clausen  *
1857ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1858ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1859ce64c8b9SLars-Peter Clausen  *
1860ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1861ce64c8b9SLars-Peter Clausen  */
1862ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1863ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1864ce64c8b9SLars-Peter Clausen {
1865ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1866ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1867ce64c8b9SLars-Peter Clausen 	unsigned int i;
1868ce64c8b9SLars-Peter Clausen 	int ret;
1869ce64c8b9SLars-Peter Clausen 
1870ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1871ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1872ce64c8b9SLars-Peter Clausen 
1873ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1874ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1875ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1876ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1877ce64c8b9SLars-Peter Clausen 			return ret;
1878ce64c8b9SLars-Peter Clausen 		}
1879ce64c8b9SLars-Peter Clausen 	}
1880ce64c8b9SLars-Peter Clausen 
1881ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1882ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1883ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1884ce64c8b9SLars-Peter Clausen 
1885ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1886ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1887ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1888ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1889ce64c8b9SLars-Peter Clausen 			break;
1890ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1891ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1892ce64c8b9SLars-Peter Clausen 			break;
1893ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1894ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1895ce64c8b9SLars-Peter Clausen 			break;
1896ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1897ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1898ce64c8b9SLars-Peter Clausen 			break;
1899ce64c8b9SLars-Peter Clausen 		}
1900ce64c8b9SLars-Peter Clausen 
1901ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1902ce64c8b9SLars-Peter Clausen 	}
1903ce64c8b9SLars-Peter Clausen 
1904ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1905ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1906ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1907ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1908ce64c8b9SLars-Peter Clausen 		return ret;
1909ce64c8b9SLars-Peter Clausen 	}
1910ce64c8b9SLars-Peter Clausen 
1911ce64c8b9SLars-Peter Clausen 	return 0;
1912ce64c8b9SLars-Peter Clausen }
1913ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1914ce64c8b9SLars-Peter Clausen 
1915345233d7SLiam Girdwood 
19161f5a4535STakashi Iwai #ifdef CONFIG_DMI
1917345233d7SLiam Girdwood /* Trim special characters, and replace '-' with '_' since '-' is used to
1918345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1919345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1920345233d7SLiam Girdwood  */
1921345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1922345233d7SLiam Girdwood {
1923345233d7SLiam Girdwood 	int i, j = 0;
1924345233d7SLiam Girdwood 
1925345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1926345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1927345233d7SLiam Girdwood 		    || (name[i] == '_'))
1928345233d7SLiam Girdwood 			name[j++] = name[i];
1929345233d7SLiam Girdwood 		else if (name[i] == '-')
1930345233d7SLiam Girdwood 			name[j++] = '_';
1931345233d7SLiam Girdwood 	}
1932345233d7SLiam Girdwood 
1933345233d7SLiam Girdwood 	name[j] = '\0';
1934345233d7SLiam Girdwood }
1935345233d7SLiam Girdwood 
1936345233d7SLiam Girdwood /**
1937345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1938345233d7SLiam Girdwood  * @card: The card to register DMI names
1939345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1940345233d7SLiam Girdwood  *
1941345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1942345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1943345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1944345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1945345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1946345233d7SLiam Girdwood  * as the sound card long name, in the format of
1947345233d7SLiam Girdwood  * "vendor-product-version-board"
1948345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1949345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1950345233d7SLiam Girdwood  * (UCM) configurations for the card.
1951345233d7SLiam Girdwood  *
1952345233d7SLiam Girdwood  * Possible card long names may be:
1953345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1954345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1955345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1956345233d7SLiam Girdwood  *
1957345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1958345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1959345233d7SLiam Girdwood  *
1960345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1961345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1962345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1963345233d7SLiam Girdwood  * charactors like SPACE.
1964345233d7SLiam Girdwood  *
1965345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1966345233d7SLiam Girdwood  */
1967345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1968345233d7SLiam Girdwood {
1969345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1970345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1971345233d7SLiam Girdwood 	size_t len;
1972345233d7SLiam Girdwood 
1973345233d7SLiam Girdwood 	if (card->long_name)
1974345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1975345233d7SLiam Girdwood 
1976345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1977345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1978345233d7SLiam Girdwood 	if (!vendor) {
1979345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1980345233d7SLiam Girdwood 		return 0;
1981345233d7SLiam Girdwood 	}
1982345233d7SLiam Girdwood 
1983345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1984345233d7SLiam Girdwood 			 "%s", vendor);
1985345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1986345233d7SLiam Girdwood 
1987345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
1988345233d7SLiam Girdwood 	if (product) {
1989345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1990345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1991345233d7SLiam Girdwood 			 longname_buf_size - len,
1992345233d7SLiam Girdwood 			 "-%s", product);
1993345233d7SLiam Girdwood 
1994345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1995345233d7SLiam Girdwood 		if (len < longname_buf_size)
1996345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1997345233d7SLiam Girdwood 
1998345233d7SLiam Girdwood 		/* some vendors like Lenovo may only put a self-explanatory
1999345233d7SLiam Girdwood 		 * name in the product version field
2000345233d7SLiam Girdwood 		 */
2001345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2002345233d7SLiam Girdwood 		if (product_version) {
2003345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
2004345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
2005345233d7SLiam Girdwood 				 longname_buf_size - len,
2006345233d7SLiam Girdwood 				 "-%s", product_version);
2007345233d7SLiam Girdwood 
2008345233d7SLiam Girdwood 			len++;
2009345233d7SLiam Girdwood 			if (len < longname_buf_size)
2010345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
2011345233d7SLiam Girdwood 		}
2012345233d7SLiam Girdwood 	}
2013345233d7SLiam Girdwood 
2014345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
2015345233d7SLiam Girdwood 	if (board) {
2016345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2017345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2018345233d7SLiam Girdwood 			 longname_buf_size - len,
2019345233d7SLiam Girdwood 			 "-%s", board);
2020345233d7SLiam Girdwood 
2021345233d7SLiam Girdwood 		len++;
2022345233d7SLiam Girdwood 		if (len < longname_buf_size)
2023345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2024345233d7SLiam Girdwood 	} else if (!product) {
2025345233d7SLiam Girdwood 		/* fall back to using legacy name */
2026345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2027345233d7SLiam Girdwood 		return 0;
2028345233d7SLiam Girdwood 	}
2029345233d7SLiam Girdwood 
2030345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
2031345233d7SLiam Girdwood 	if (flavour) {
2032345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2033345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2034345233d7SLiam Girdwood 			 longname_buf_size - len,
2035345233d7SLiam Girdwood 			 "-%s", flavour);
2036345233d7SLiam Girdwood 
2037345233d7SLiam Girdwood 		len++;
2038345233d7SLiam Girdwood 		if (len < longname_buf_size)
2039345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2040345233d7SLiam Girdwood 	}
2041345233d7SLiam Girdwood 
2042345233d7SLiam Girdwood 	/* set the card long name */
2043345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
2044345233d7SLiam Girdwood 
2045345233d7SLiam Girdwood 	return 0;
2046345233d7SLiam Girdwood }
2047345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
20481f5a4535STakashi Iwai #endif /* CONFIG_DMI */
2049345233d7SLiam Girdwood 
2050b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2051f0fba2adSLiam Girdwood {
2052fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
20531a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
205461b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2055ce64c8b9SLars-Peter Clausen 	int ret, i, order;
205696dd3622SMark Brown 
205734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
205801b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2059f0fba2adSLiam Girdwood 
2060b19e6e7bSMark Brown 	/* bind DAIs */
2061b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
20626f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
2063b19e6e7bSMark Brown 		if (ret != 0)
2064b19e6e7bSMark Brown 			goto base_error;
2065db2a4165SFrank Mandarino 	}
2066db2a4165SFrank Mandarino 
206744c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2068b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
206944c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2070b19e6e7bSMark Brown 		if (ret != 0)
2071b19e6e7bSMark Brown 			goto base_error;
2072db2a4165SFrank Mandarino 	}
2073db2a4165SFrank Mandarino 
2074f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2075f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
2076f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
2077f8f80361SMengdong Lin 
2078fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
2079fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
2080fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
2081fdf0f54dSDimitris Papastamos 			continue;
2082f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
2083b19e6e7bSMark Brown 		if (ret < 0)
2084b19e6e7bSMark Brown 			goto base_error;
2085fdf0f54dSDimitris Papastamos 	}
2086fdf0f54dSDimitris Papastamos 
2087f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2088102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2089f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2090f0fba2adSLiam Girdwood 	if (ret < 0) {
209110e8aa9aSMichał Mirosław 		dev_err(card->dev,
209210e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
209310e8aa9aSMichał Mirosław 			card->name, ret);
2094b19e6e7bSMark Brown 		goto base_error;
2095db2a4165SFrank Mandarino 	}
2096db2a4165SFrank Mandarino 
20970757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20980757d834SLars-Peter Clausen 
2099e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
2100e37a4970SMark Brown 	card->dapm.dev = card->dev;
2101e37a4970SMark Brown 	card->dapm.card = card;
2102e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
2103e37a4970SMark Brown 
2104d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2105d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2106d5d1e0beSLars-Peter Clausen #endif
2107d5d1e0beSLars-Peter Clausen 
210888ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
21096ed25978SAndy Green 	/* deferred resume work */
21106308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
21111301a964SRandy Dunlap #endif
21126ed25978SAndy Green 
21139a841ebbSMark Brown 	if (card->dapm_widgets)
21149a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
21159a841ebbSMark Brown 					  card->num_dapm_widgets);
21169a841ebbSMark Brown 
2117f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2118f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2119f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2120f23e860eSNicolin Chen 
2121f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2122f0fba2adSLiam Girdwood 	if (card->probe) {
2123e7361ec4SMark Brown 		ret = card->probe(card);
2124f0fba2adSLiam Girdwood 		if (ret < 0)
2125f0fba2adSLiam Girdwood 			goto card_probe_error;
2126f0fba2adSLiam Girdwood 	}
2127f0fba2adSLiam Girdwood 
212862ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
21290168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
21300168bf0dSLiam Girdwood 			order++) {
21311a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
21321a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
213362ae68faSStephen Warren 			if (ret < 0) {
2134f110bfc7SLiam Girdwood 				dev_err(card->dev,
2135f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2136f110bfc7SLiam Girdwood 					ret);
213762ae68faSStephen Warren 				goto probe_dai_err;
213862ae68faSStephen Warren 			}
213962ae68faSStephen Warren 		}
214062ae68faSStephen Warren 	}
214162ae68faSStephen Warren 
2142f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2143f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2144f2ed6b07SMengdong Lin 	if (ret < 0)
2145f2ed6b07SMengdong Lin 		goto probe_dai_err;
2146f2ed6b07SMengdong Lin 
214761b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
214861b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
214961b0088bSMengdong Lin 	 */
215061b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
215161b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
215261b0088bSMengdong Lin 			continue;
215361b0088bSMengdong Lin 
215461b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
215561b0088bSMengdong Lin 		if (ret)
215661b0088bSMengdong Lin 			goto probe_dai_err;
215761b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
215861b0088bSMengdong Lin 		if (ret)
215961b0088bSMengdong Lin 			goto probe_dai_err;
216061b0088bSMengdong Lin 	}
216161b0088bSMengdong Lin 
216262ae68faSStephen Warren 	/* probe all DAI links on this card */
216362ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
216462ae68faSStephen Warren 			order++) {
21651a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
21661a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2167fe3e78e0SMark Brown 			if (ret < 0) {
2168f110bfc7SLiam Girdwood 				dev_err(card->dev,
2169f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2170f110bfc7SLiam Girdwood 					ret);
2171f0fba2adSLiam Girdwood 				goto probe_dai_err;
2172fe3e78e0SMark Brown 			}
2173fe3e78e0SMark Brown 		}
21740168bf0dSLiam Girdwood 	}
2175fe3e78e0SMark Brown 
2176888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2177b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2178888df395SMark Brown 
2179b7af1dafSMark Brown 	if (card->controls)
2180022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
2181b7af1dafSMark Brown 
2182b8ad29deSMark Brown 	if (card->dapm_routes)
2183b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2184b8ad29deSMark Brown 					card->num_dapm_routes);
2185b8ad29deSMark Brown 
2186f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2187f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2188f23e860eSNicolin Chen 					card->num_of_dapm_routes);
218975d9ac46SMark Brown 
2190861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2191861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2192861886d3STakashi Iwai 
2193f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2194fe3e78e0SMark Brown 		 "%s", card->name);
2195f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
219622de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2197f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2198f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2199f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2200f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2201f0e8ed85SMark Brown 		case '_':
2202f0e8ed85SMark Brown 		case '-':
2203f0e8ed85SMark Brown 		case '\0':
2204f0e8ed85SMark Brown 			break;
2205f0e8ed85SMark Brown 		default:
2206f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2207f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2208f0e8ed85SMark Brown 			break;
2209f0e8ed85SMark Brown 		}
2210f0e8ed85SMark Brown 	}
2211fe3e78e0SMark Brown 
221228e9ad92SMark Brown 	if (card->late_probe) {
221328e9ad92SMark Brown 		ret = card->late_probe(card);
221428e9ad92SMark Brown 		if (ret < 0) {
2215f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
221628e9ad92SMark Brown 				card->name, ret);
221728e9ad92SMark Brown 			goto probe_aux_dev_err;
221828e9ad92SMark Brown 		}
221928e9ad92SMark Brown 	}
222028e9ad92SMark Brown 
2221824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
22228c193b8dSLars-Peter Clausen 
2223f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2224fe3e78e0SMark Brown 	if (ret < 0) {
2225f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2226f110bfc7SLiam Girdwood 				ret);
22276b3ed785SAxel Lin 		goto probe_aux_dev_err;
2228fe3e78e0SMark Brown 	}
2229fe3e78e0SMark Brown 
2230435c5e25SMark Brown 	card->instantiated = 1;
22314f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2232f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
223334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2234b19e6e7bSMark Brown 
2235b19e6e7bSMark Brown 	return 0;
2236db2a4165SFrank Mandarino 
22372eea392dSJarkko Nikula probe_aux_dev_err:
2238f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
22392eea392dSJarkko Nikula 
2240f0fba2adSLiam Girdwood probe_dai_err:
22410671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2242fe3e78e0SMark Brown 
2243f0fba2adSLiam Girdwood card_probe_error:
224487506549SMark Brown 	if (card->remove)
2245e7361ec4SMark Brown 		card->remove(card);
2246f0fba2adSLiam Girdwood 
22472210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
22480757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2249f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2250f0fba2adSLiam Girdwood 
2251b19e6e7bSMark Brown base_error:
22521a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2253f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
225434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2255db2a4165SFrank Mandarino 
2256b19e6e7bSMark Brown 	return ret;
2257435c5e25SMark Brown }
2258435c5e25SMark Brown 
2259435c5e25SMark Brown /* probes a new socdev */
2260435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2261435c5e25SMark Brown {
2262f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2263435c5e25SMark Brown 
226470a7ca34SVinod Koul 	/*
226570a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
226670a7ca34SVinod Koul 	 * we should not be here in that case so ret error
226770a7ca34SVinod Koul 	 */
226870a7ca34SVinod Koul 	if (!card)
226970a7ca34SVinod Koul 		return -EINVAL;
227070a7ca34SVinod Koul 
2271fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2272f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2273fe4085e8SMark Brown 		 card->name);
2274fe4085e8SMark Brown 
2275435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2276435c5e25SMark Brown 	card->dev = &pdev->dev;
2277f0fba2adSLiam Girdwood 
227828d528c8SMark Brown 	return snd_soc_register_card(card);
2279435c5e25SMark Brown }
2280435c5e25SMark Brown 
2281b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2282db2a4165SFrank Mandarino {
22831a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2284db2a4165SFrank Mandarino 
2285f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
22861a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
228743829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2288db2a4165SFrank Mandarino 
2289f0fba2adSLiam Girdwood 	/* remove and free each DAI */
22900671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
22911a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2292f0fba2adSLiam Girdwood 
2293f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2294f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2295f2ed6b07SMengdong Lin 
2296d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2297a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2298a6052154SJarkko Nikula 
2299f0fba2adSLiam Girdwood 	/* remove the card */
230087506549SMark Brown 	if (card->remove)
2301e7361ec4SMark Brown 		card->remove(card);
2302f0fba2adSLiam Girdwood 
2303f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2304b0e26485SVinod Koul 	return 0;
2305b0e26485SVinod Koul 
2306b2dfa62cSGuennadi Liakhovetski }
2307b0e26485SVinod Koul 
2308b0e26485SVinod Koul /* removes a socdev */
2309b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2310b0e26485SVinod Koul {
2311b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2312b0e26485SVinod Koul 
2313c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2314db2a4165SFrank Mandarino 	return 0;
2315db2a4165SFrank Mandarino }
2316db2a4165SFrank Mandarino 
23176f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
231851737470SMark Brown {
23196f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
23201a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
232151737470SMark Brown 
232251737470SMark Brown 	if (!card->instantiated)
2323416356fcSMark Brown 		return 0;
232451737470SMark Brown 
232551737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
232651737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
23271a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
232843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
232951737470SMark Brown 
2330f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2331416356fcSMark Brown 
2332988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
23331a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
233488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
23351a497983SMengdong Lin 		int i;
233688bd870fSBenoit Cousson 
2337988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
23381a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
23391a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
234088bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
234188bd870fSBenoit Cousson 		}
2342988e8cc4SNicolin Chen 	}
2343988e8cc4SNicolin Chen 
2344416356fcSMark Brown 	return 0;
234551737470SMark Brown }
23466f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
234751737470SMark Brown 
23486f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2349b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2350b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2351b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2352b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
23536f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2354b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2355416356fcSMark Brown };
2356deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2357416356fcSMark Brown 
2358db2a4165SFrank Mandarino /* ASoC platform driver */
2359db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2360db2a4165SFrank Mandarino 	.driver		= {
2361db2a4165SFrank Mandarino 		.name		= "soc-audio",
23626f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2363db2a4165SFrank Mandarino 	},
2364db2a4165SFrank Mandarino 	.probe		= soc_probe,
2365db2a4165SFrank Mandarino 	.remove		= soc_remove,
2366db2a4165SFrank Mandarino };
2367db2a4165SFrank Mandarino 
2368096e49d5SMark Brown /**
2369db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2370db2a4165SFrank Mandarino  * @_template: control template
2371db2a4165SFrank Mandarino  * @data: control private data
2372ac11a2b3SMark Brown  * @long_name: control long name
2373efb7ac3fSMark Brown  * @prefix: control name prefix
2374db2a4165SFrank Mandarino  *
2375db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2376db2a4165SFrank Mandarino  *
2377db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2378db2a4165SFrank Mandarino  */
2379db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
23803056557fSMark Brown 				  void *data, const char *long_name,
2381efb7ac3fSMark Brown 				  const char *prefix)
2382db2a4165SFrank Mandarino {
2383db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2384efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2385efb7ac3fSMark Brown 	char *name = NULL;
2386db2a4165SFrank Mandarino 
2387db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2388db2a4165SFrank Mandarino 	template.index = 0;
2389db2a4165SFrank Mandarino 
2390efb7ac3fSMark Brown 	if (!long_name)
2391efb7ac3fSMark Brown 		long_name = template.name;
2392efb7ac3fSMark Brown 
2393efb7ac3fSMark Brown 	if (prefix) {
23942b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2395efb7ac3fSMark Brown 		if (!name)
2396efb7ac3fSMark Brown 			return NULL;
2397efb7ac3fSMark Brown 
2398efb7ac3fSMark Brown 		template.name = name;
2399efb7ac3fSMark Brown 	} else {
2400efb7ac3fSMark Brown 		template.name = long_name;
2401efb7ac3fSMark Brown 	}
2402efb7ac3fSMark Brown 
2403efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2404efb7ac3fSMark Brown 
2405efb7ac3fSMark Brown 	kfree(name);
2406efb7ac3fSMark Brown 
2407efb7ac3fSMark Brown 	return kcontrol;
2408db2a4165SFrank Mandarino }
2409db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2410db2a4165SFrank Mandarino 
2411022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2412022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2413022658beSLiam Girdwood 	const char *prefix, void *data)
2414022658beSLiam Girdwood {
2415022658beSLiam Girdwood 	int err, i;
2416022658beSLiam Girdwood 
2417022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2418022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2419022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2420022658beSLiam Girdwood 						     control->name, prefix));
2421022658beSLiam Girdwood 		if (err < 0) {
2422f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2423f110bfc7SLiam Girdwood 				control->name, err);
2424022658beSLiam Girdwood 			return err;
2425022658beSLiam Girdwood 		}
2426022658beSLiam Girdwood 	}
2427022658beSLiam Girdwood 
2428022658beSLiam Girdwood 	return 0;
2429022658beSLiam Girdwood }
2430022658beSLiam Girdwood 
24314fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
24324fefd698SDimitris Papastamos 					       const char *name)
24334fefd698SDimitris Papastamos {
24344fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
24354fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
24364fefd698SDimitris Papastamos 
24374fefd698SDimitris Papastamos 	if (unlikely(!name))
24384fefd698SDimitris Papastamos 		return NULL;
24394fefd698SDimitris Papastamos 
24404fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
24414fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
24424fefd698SDimitris Papastamos 			return kctl;
24434fefd698SDimitris Papastamos 	return NULL;
24444fefd698SDimitris Papastamos }
24454fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
24464fefd698SDimitris Papastamos 
2447db2a4165SFrank Mandarino /**
24480f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
24490f2780adSLars-Peter Clausen  *
24500f2780adSLars-Peter Clausen  * @component: Component to add controls to
24510f2780adSLars-Peter Clausen  * @controls: Array of controls to add
24520f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
24530f2780adSLars-Peter Clausen  *
24540f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
24550f2780adSLars-Peter Clausen  */
24560f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
24570f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
24580f2780adSLars-Peter Clausen {
24590f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
24600f2780adSLars-Peter Clausen 
24610f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
24620f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
24630f2780adSLars-Peter Clausen }
24640f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
24650f2780adSLars-Peter Clausen 
24660f2780adSLars-Peter Clausen /**
2467022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2468022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
24693e8e1952SIan Molton  * duplicating this code.
24703e8e1952SIan Molton  *
24713e8e1952SIan Molton  * @codec: codec to add controls to
24723e8e1952SIan Molton  * @controls: array of controls to add
24733e8e1952SIan Molton  * @num_controls: number of elements in the array
24743e8e1952SIan Molton  *
24753e8e1952SIan Molton  * Return 0 for success, else error.
24763e8e1952SIan Molton  */
2477022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
24780f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
24793e8e1952SIan Molton {
24800f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
24810f2780adSLars-Peter Clausen 		num_controls);
24823e8e1952SIan Molton }
2483022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
24843e8e1952SIan Molton 
24853e8e1952SIan Molton /**
2486a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2487022658beSLiam Girdwood  * Convenience function to add a list of controls.
2488a491a5c8SLiam Girdwood  *
2489a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2490a491a5c8SLiam Girdwood  * @controls: array of controls to add
2491a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2492a491a5c8SLiam Girdwood  *
2493a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2494a491a5c8SLiam Girdwood  */
2495a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
24960f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2497a491a5c8SLiam Girdwood {
24980f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
24990f2780adSLars-Peter Clausen 		num_controls);
2500a491a5c8SLiam Girdwood }
2501a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2502a491a5c8SLiam Girdwood 
2503a491a5c8SLiam Girdwood /**
2504022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2505022658beSLiam Girdwood  * Convenience function to add a list of controls.
2506022658beSLiam Girdwood  *
2507022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2508022658beSLiam Girdwood  * @controls: array of controls to add
2509022658beSLiam Girdwood  * @num_controls: number of elements in the array
2510022658beSLiam Girdwood  *
2511022658beSLiam Girdwood  * Return 0 for success, else error.
2512022658beSLiam Girdwood  */
2513022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2514022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2515022658beSLiam Girdwood {
2516022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2517022658beSLiam Girdwood 
2518022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2519022658beSLiam Girdwood 			NULL, soc_card);
2520022658beSLiam Girdwood }
2521022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2522022658beSLiam Girdwood 
2523022658beSLiam Girdwood /**
2524022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2525022658beSLiam Girdwood  * Convienience function to add a list of controls.
2526022658beSLiam Girdwood  *
2527022658beSLiam Girdwood  * @dai: DAI to add controls to
2528022658beSLiam Girdwood  * @controls: array of controls to add
2529022658beSLiam Girdwood  * @num_controls: number of elements in the array
2530022658beSLiam Girdwood  *
2531022658beSLiam Girdwood  * Return 0 for success, else error.
2532022658beSLiam Girdwood  */
2533022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2534022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2535022658beSLiam Girdwood {
2536313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2537022658beSLiam Girdwood 
2538022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2539022658beSLiam Girdwood 			NULL, dai);
2540022658beSLiam Girdwood }
2541022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2542022658beSLiam Girdwood 
2543022658beSLiam Girdwood /**
25448c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
25458c6529dbSLiam Girdwood  * @dai: DAI
25468c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
25478c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
25488c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
25498c6529dbSLiam Girdwood  *
25508c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
25518c6529dbSLiam Girdwood  */
25528c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
25538c6529dbSLiam Girdwood 	unsigned int freq, int dir)
25548c6529dbSLiam Girdwood {
2555f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2556f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2557ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2558da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2559ec4ee52aSMark Brown 						      freq, dir);
25608c6529dbSLiam Girdwood 	else
25611104a9c8SMark Brown 		return -ENOTSUPP;
25628c6529dbSLiam Girdwood }
25638c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
25648c6529dbSLiam Girdwood 
25658c6529dbSLiam Girdwood /**
2566ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2567ec4ee52aSMark Brown  * @codec: CODEC
2568ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2569da1c6ea6SMark Brown  * @source: Source for the clock
2570ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2571ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2572ec4ee52aSMark Brown  *
2573ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2574ec4ee52aSMark Brown  */
2575ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2576da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2577ec4ee52aSMark Brown {
2578ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2579da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2580da1c6ea6SMark Brown 						 freq, dir);
2581ec4ee52aSMark Brown 	else
25821104a9c8SMark Brown 		return -ENOTSUPP;
2583ec4ee52aSMark Brown }
2584ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2585ec4ee52aSMark Brown 
2586ec4ee52aSMark Brown /**
25878c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
25888c6529dbSLiam Girdwood  * @dai: DAI
2589ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
25908c6529dbSLiam Girdwood  * @div: new clock divisor.
25918c6529dbSLiam Girdwood  *
25928c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
25938c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
25948c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
25958c6529dbSLiam Girdwood  */
25968c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
25978c6529dbSLiam Girdwood 	int div_id, int div)
25988c6529dbSLiam Girdwood {
2599f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2600f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
26018c6529dbSLiam Girdwood 	else
26028c6529dbSLiam Girdwood 		return -EINVAL;
26038c6529dbSLiam Girdwood }
26048c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
26058c6529dbSLiam Girdwood 
26068c6529dbSLiam Girdwood /**
26078c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
26088c6529dbSLiam Girdwood  * @dai: DAI
26098c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
261085488037SMark Brown  * @source: DAI specific source for the PLL
26118c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
26128c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
26138c6529dbSLiam Girdwood  *
26148c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
26158c6529dbSLiam Girdwood  */
261685488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
261785488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
26188c6529dbSLiam Girdwood {
2619f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2620f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
262185488037SMark Brown 					 freq_in, freq_out);
2622ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2623ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2624ec4ee52aSMark Brown 						   freq_in, freq_out);
26258c6529dbSLiam Girdwood 	else
26268c6529dbSLiam Girdwood 		return -EINVAL;
26278c6529dbSLiam Girdwood }
26288c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
26298c6529dbSLiam Girdwood 
2630ec4ee52aSMark Brown /*
2631ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2632ec4ee52aSMark Brown  * @codec: CODEC
2633ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2634ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2635ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2636ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2637ec4ee52aSMark Brown  *
2638ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2639ec4ee52aSMark Brown  */
2640ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2641ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2642ec4ee52aSMark Brown {
2643ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2644ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2645ec4ee52aSMark Brown 					      freq_in, freq_out);
2646ec4ee52aSMark Brown 	else
2647ec4ee52aSMark Brown 		return -EINVAL;
2648ec4ee52aSMark Brown }
2649ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2650ec4ee52aSMark Brown 
26518c6529dbSLiam Girdwood /**
2652e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2653e54cf76bSLiam Girdwood  * @dai: DAI
2654231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2655e54cf76bSLiam Girdwood  *
2656e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2657e54cf76bSLiam Girdwood  */
2658e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2659e54cf76bSLiam Girdwood {
2660e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2661e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2662e54cf76bSLiam Girdwood 	else
2663e54cf76bSLiam Girdwood 		return -EINVAL;
2664e54cf76bSLiam Girdwood }
2665e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2666e54cf76bSLiam Girdwood 
2667e54cf76bSLiam Girdwood /**
26688c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
26698c6529dbSLiam Girdwood  * @dai: DAI
26708c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
26718c6529dbSLiam Girdwood  *
26728c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
26738c6529dbSLiam Girdwood  */
26748c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
26758c6529dbSLiam Girdwood {
26765e4ba569SShawn Guo 	if (dai->driver == NULL)
26778c6529dbSLiam Girdwood 		return -EINVAL;
26785e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
26795e4ba569SShawn Guo 		return -ENOTSUPP;
26805e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
26818c6529dbSLiam Girdwood }
26828c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
26838c6529dbSLiam Girdwood 
26848c6529dbSLiam Girdwood /**
2685e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
268689c67857SXiubo Li  * @slots: Number of slots in use.
268789c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
268889c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
268989c67857SXiubo Li  *
269089c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
269189c67857SXiubo Li  */
2692e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
269389c67857SXiubo Li 					  unsigned int *tx_mask,
269489c67857SXiubo Li 					  unsigned int *rx_mask)
269589c67857SXiubo Li {
269689c67857SXiubo Li 	if (*tx_mask || *rx_mask)
269789c67857SXiubo Li 		return 0;
269889c67857SXiubo Li 
269989c67857SXiubo Li 	if (!slots)
270089c67857SXiubo Li 		return -EINVAL;
270189c67857SXiubo Li 
270289c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
270389c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
270489c67857SXiubo Li 
270589c67857SXiubo Li 	return 0;
270689c67857SXiubo Li }
270789c67857SXiubo Li 
270889c67857SXiubo Li /**
2709e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2710e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2711a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2712a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
27138c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2714a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
27158c6529dbSLiam Girdwood  *
2716e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2717e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2718e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2719e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2720e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2721e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2722e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2723e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2724e46c9366SLars-Peter Clausen  * channel and so on.
2725e46c9366SLars-Peter Clausen  *
2726e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2727e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2728e46c9366SLars-Peter Clausen  *
2729e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
27308c6529dbSLiam Girdwood  */
27318c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2732a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
27338c6529dbSLiam Girdwood {
2734e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2735e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
273689c67857SXiubo Li 						&tx_mask, &rx_mask);
273789c67857SXiubo Li 	else
2738e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
273989c67857SXiubo Li 
274088bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
274188bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
274288bd870fSBenoit Cousson 
2743f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2744f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2745a5479e38SDaniel Ribeiro 				slots, slot_width);
27468c6529dbSLiam Girdwood 	else
2747b2cbb6e1SXiubo Li 		return -ENOTSUPP;
27488c6529dbSLiam Girdwood }
27498c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
27508c6529dbSLiam Girdwood 
27518c6529dbSLiam Girdwood /**
2752472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2753472df3cbSBarry Song  * @dai: DAI
2754472df3cbSBarry Song  * @tx_num: how many TX channels
2755472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2756472df3cbSBarry Song  *           0~num-1 uses
2757472df3cbSBarry Song  * @rx_num: how many RX channels
2758472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2759472df3cbSBarry Song  *           0~num-1 uses
2760472df3cbSBarry Song  *
2761472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2762472df3cbSBarry Song  */
2763472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2764472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2765472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2766472df3cbSBarry Song {
2767f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2768f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2769472df3cbSBarry Song 			rx_num, rx_slot);
2770472df3cbSBarry Song 	else
2771472df3cbSBarry Song 		return -EINVAL;
2772472df3cbSBarry Song }
2773472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2774472df3cbSBarry Song 
2775472df3cbSBarry Song /**
27768c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
27778c6529dbSLiam Girdwood  * @dai: DAI
27788c6529dbSLiam Girdwood  * @tristate: tristate enable
27798c6529dbSLiam Girdwood  *
27808c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
27818c6529dbSLiam Girdwood  */
27828c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
27838c6529dbSLiam Girdwood {
2784f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2785f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
27868c6529dbSLiam Girdwood 	else
27878c6529dbSLiam Girdwood 		return -EINVAL;
27888c6529dbSLiam Girdwood }
27898c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
27908c6529dbSLiam Girdwood 
27918c6529dbSLiam Girdwood /**
27928c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
27938c6529dbSLiam Girdwood  * @dai: DAI
27948c6529dbSLiam Girdwood  * @mute: mute enable
2795da18396fSMark Brown  * @direction: stream to mute
27968c6529dbSLiam Girdwood  *
27978c6529dbSLiam Girdwood  * Mutes the DAI DAC.
27988c6529dbSLiam Girdwood  */
2799da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2800da18396fSMark Brown 			     int direction)
28018c6529dbSLiam Girdwood {
2802da18396fSMark Brown 	if (!dai->driver)
2803da18396fSMark Brown 		return -ENOTSUPP;
2804da18396fSMark Brown 
2805da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2806da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2807da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2808da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2809f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
28108c6529dbSLiam Girdwood 	else
281104570c62SMark Brown 		return -ENOTSUPP;
28128c6529dbSLiam Girdwood }
28138c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
28148c6529dbSLiam Girdwood 
2815c5af3a2eSMark Brown /**
2816c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2817c5af3a2eSMark Brown  *
2818ac11a2b3SMark Brown  * @card: Card to register
2819c5af3a2eSMark Brown  *
2820c5af3a2eSMark Brown  */
282170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2822c5af3a2eSMark Brown {
2823923c5e61SMengdong Lin 	int i, ret;
28241a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2825f0fba2adSLiam Girdwood 
2826c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2827c5af3a2eSMark Brown 		return -EINVAL;
2828c5af3a2eSMark Brown 
28295a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
28305a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
28315a504963SStephen Warren 
2832923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
283388bd870fSBenoit Cousson 		if (ret) {
2834923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2835923c5e61SMengdong Lin 				link->name);
283688bd870fSBenoit Cousson 			return ret;
283788bd870fSBenoit Cousson 		}
28385a504963SStephen Warren 	}
28395a504963SStephen Warren 
2840ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2841ed77cc12SMark Brown 
2842111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2843111c6419SStephen Warren 
2844f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2845f8f80361SMengdong Lin 	card->num_dai_links = 0;
2846f0fba2adSLiam Girdwood 
28471a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
28489115171aSMark Brown 	card->num_rtd = 0;
2849db2a4165SFrank Mandarino 
2850db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
28518a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2852db2a4165SFrank Mandarino 	card->instantiated = 0;
2853db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2854a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2855db2a4165SFrank Mandarino 
2856b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2857b19e6e7bSMark Brown 	if (ret != 0)
28584e2576bdSKuninori Morimoto 		return ret;
2859db2a4165SFrank Mandarino 
2860988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
28611a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
286288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
286388bd870fSBenoit Cousson 		int j;
286488bd870fSBenoit Cousson 
286588bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
286688bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2867988e8cc4SNicolin Chen 			if (!codec_dai->active)
2868988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
286988bd870fSBenoit Cousson 		}
287088bd870fSBenoit Cousson 
2871988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2872988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2873988e8cc4SNicolin Chen 	}
2874988e8cc4SNicolin Chen 
2875b19e6e7bSMark Brown 	return ret;
2876db2a4165SFrank Mandarino }
287770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2878db2a4165SFrank Mandarino 
2879db2a4165SFrank Mandarino /**
2880db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2881db2a4165SFrank Mandarino  *
2882db2a4165SFrank Mandarino  * @card: Card to unregister
2883db2a4165SFrank Mandarino  *
2884db2a4165SFrank Mandarino  */
288570a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2886db2a4165SFrank Mandarino {
288701e0df66SLars-Peter Clausen 	if (card->instantiated) {
288801e0df66SLars-Peter Clausen 		card->instantiated = false;
28891c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2890b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2891f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
28928f6f9b29SKuninori Morimoto 	}
2893db2a4165SFrank Mandarino 
2894db2a4165SFrank Mandarino 	return 0;
2895db2a4165SFrank Mandarino }
289670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2897db2a4165SFrank Mandarino 
2898db2a4165SFrank Mandarino /*
2899db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2900db2a4165SFrank Mandarino  * and sanitizing names.
2901db2a4165SFrank Mandarino  */
29020b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2903db2a4165SFrank Mandarino {
2904db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2905db2a4165SFrank Mandarino 	int id1, id2;
2906db2a4165SFrank Mandarino 
2907db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2908db2a4165SFrank Mandarino 		return NULL;
2909db2a4165SFrank Mandarino 
291058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2911db2a4165SFrank Mandarino 
2912db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2913c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2914c5af3a2eSMark Brown 	if (found) {
2915c5af3a2eSMark Brown 		/* get ID */
2916c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2917c5af3a2eSMark Brown 
2918c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2919c5af3a2eSMark Brown 			if (*id == -1)
2920c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2921c5af3a2eSMark Brown 		}
2922c5af3a2eSMark Brown 
2923c5af3a2eSMark Brown 	} else {
2924c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2925c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2926c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2927c5af3a2eSMark Brown 
2928c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2929c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2930c5af3a2eSMark Brown 
2931c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2932c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
293358818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2934c5af3a2eSMark Brown 		} else
2935c5af3a2eSMark Brown 			*id = 0;
2936c5af3a2eSMark Brown 	}
2937c5af3a2eSMark Brown 
2938c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2939c5af3a2eSMark Brown }
2940c5af3a2eSMark Brown 
2941c5af3a2eSMark Brown /*
2942c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2943c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2944c5af3a2eSMark Brown  */
2945c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2946c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2947c5af3a2eSMark Brown {
2948c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
294910e8aa9aSMichał Mirosław 		dev_err(dev,
295010e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
295110e8aa9aSMichał Mirosław 			dev_name(dev));
2952c5af3a2eSMark Brown 		return NULL;
2953c5af3a2eSMark Brown 	}
2954c5af3a2eSMark Brown 
2955c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2956c5af3a2eSMark Brown }
2957c5af3a2eSMark Brown 
2958c5af3a2eSMark Brown /**
295932c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
29609115171aSMark Brown  *
296132c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
29629115171aSMark Brown  */
296332c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
29649115171aSMark Brown {
29655c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
29669115171aSMark Brown 
29675c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
296832c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
296932c9ba54SLars-Peter Clausen 			dai->name);
29709115171aSMark Brown 		list_del(&dai->list);
2971f0fba2adSLiam Girdwood 		kfree(dai->name);
2972f0fba2adSLiam Girdwood 		kfree(dai);
29739115171aSMark Brown 	}
297432c9ba54SLars-Peter Clausen }
29759115171aSMark Brown 
29765e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
29775e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
29785e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
29795e4fb372SMengdong Lin 	bool legacy_dai_naming)
29805e4fb372SMengdong Lin {
29815e4fb372SMengdong Lin 	struct device *dev = component->dev;
29825e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
29835e4fb372SMengdong Lin 
29845e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
29855e4fb372SMengdong Lin 
29865e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
29875e4fb372SMengdong Lin 	if (dai == NULL)
29885e4fb372SMengdong Lin 		return NULL;
29895e4fb372SMengdong Lin 
29905e4fb372SMengdong Lin 	/*
29915e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
29925e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
29935e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
29945e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
29955e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
29965e4fb372SMengdong Lin 	 * component-less anymore.
29975e4fb372SMengdong Lin 	 */
29985e4fb372SMengdong Lin 	if (legacy_dai_naming &&
29995e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
30005e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
30015e4fb372SMengdong Lin 	} else {
30025e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
30035e4fb372SMengdong Lin 		if (dai_drv->id)
30045e4fb372SMengdong Lin 			dai->id = dai_drv->id;
30055e4fb372SMengdong Lin 		else
30065e4fb372SMengdong Lin 			dai->id = component->num_dai;
30075e4fb372SMengdong Lin 	}
30085e4fb372SMengdong Lin 	if (dai->name == NULL) {
30095e4fb372SMengdong Lin 		kfree(dai);
30105e4fb372SMengdong Lin 		return NULL;
30115e4fb372SMengdong Lin 	}
30125e4fb372SMengdong Lin 
30135e4fb372SMengdong Lin 	dai->component = component;
30145e4fb372SMengdong Lin 	dai->dev = dev;
30155e4fb372SMengdong Lin 	dai->driver = dai_drv;
30165e4fb372SMengdong Lin 	if (!dai->driver->ops)
30175e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
30185e4fb372SMengdong Lin 
30195e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
30205e4fb372SMengdong Lin 	component->num_dai++;
30215e4fb372SMengdong Lin 
30225e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
30235e4fb372SMengdong Lin 	return dai;
30245e4fb372SMengdong Lin }
30255e4fb372SMengdong Lin 
30269115171aSMark Brown /**
302732c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
30289115171aSMark Brown  *
30296106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
30306106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3031ac11a2b3SMark Brown  * @count: Number of DAIs
303232c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
303332c9ba54SLars-Peter Clausen  *                     parent's name.
30349115171aSMark Brown  */
303532c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
3036bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
3037bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
30389115171aSMark Brown {
30396106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3040f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
304132c9ba54SLars-Peter Clausen 	unsigned int i;
304232c9ba54SLars-Peter Clausen 	int ret;
3043f0fba2adSLiam Girdwood 
30445b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
30459115171aSMark Brown 
3046bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
3047bb13109dSLars-Peter Clausen 
30489115171aSMark Brown 	for (i = 0; i < count; i++) {
3049f0fba2adSLiam Girdwood 
30505e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
30515e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
3052c46e0079SAxel Lin 		if (dai == NULL) {
3053c46e0079SAxel Lin 			ret = -ENOMEM;
3054c46e0079SAxel Lin 			goto err;
3055c46e0079SAxel Lin 		}
3056f0fba2adSLiam Girdwood 	}
3057f0fba2adSLiam Girdwood 
30589115171aSMark Brown 	return 0;
30599115171aSMark Brown 
30609115171aSMark Brown err:
306132c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
30629115171aSMark Brown 
30639115171aSMark Brown 	return ret;
30649115171aSMark Brown }
30659115171aSMark Brown 
306668003e6cSMengdong Lin /**
306768003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
306868003e6cSMengdong Lin  *
306968003e6cSMengdong Lin  * @component: The component the DAIs are registered for
307068003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
307168003e6cSMengdong Lin  *
307268003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
307368003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
307468003e6cSMengdong Lin  * will be freed in the component cleanup.
307568003e6cSMengdong Lin  */
307668003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
307768003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
307868003e6cSMengdong Lin {
307968003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
308068003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
308168003e6cSMengdong Lin 	struct snd_soc_dai *dai;
308268003e6cSMengdong Lin 	int ret;
308368003e6cSMengdong Lin 
308468003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
308568003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
308668003e6cSMengdong Lin 			dai_drv->dobj.type);
308768003e6cSMengdong Lin 		return -EINVAL;
308868003e6cSMengdong Lin 	}
308968003e6cSMengdong Lin 
309068003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
309168003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
309268003e6cSMengdong Lin 	if (!dai)
309368003e6cSMengdong Lin 		return -ENOMEM;
309468003e6cSMengdong Lin 
309568003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
309668003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
309768003e6cSMengdong Lin 	 */
309868003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
309968003e6cSMengdong Lin 	if (ret != 0) {
310068003e6cSMengdong Lin 		dev_err(component->dev,
310168003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
310268003e6cSMengdong Lin 	}
310368003e6cSMengdong Lin 
310468003e6cSMengdong Lin 	return ret;
310568003e6cSMengdong Lin }
310668003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
310768003e6cSMengdong Lin 
310814e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
310914e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3110d191bd8dSKuninori Morimoto {
311114e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3112d191bd8dSKuninori Morimoto 
311314e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
311414e8bdebSLars-Peter Clausen }
3115d191bd8dSKuninori Morimoto 
311614e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
311714e8bdebSLars-Peter Clausen 	int event)
311814e8bdebSLars-Peter Clausen {
311914e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
312014e8bdebSLars-Peter Clausen 
312114e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
312214e8bdebSLars-Peter Clausen }
312314e8bdebSLars-Peter Clausen 
3124bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3125bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3126d191bd8dSKuninori Morimoto {
3127ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3128ce0fc93aSLars-Peter Clausen 
3129bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3130bb13109dSLars-Peter Clausen 	if (!component->name) {
3131bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3132d191bd8dSKuninori Morimoto 		return -ENOMEM;
3133d191bd8dSKuninori Morimoto 	}
3134d191bd8dSKuninori Morimoto 
3135bb13109dSLars-Peter Clausen 	component->dev = dev;
3136bb13109dSLars-Peter Clausen 	component->driver = driver;
313761aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
313861aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
31399178feb4SKuninori Morimoto 	component->suspend = component->driver->suspend;
31409178feb4SKuninori Morimoto 	component->resume = component->driver->resume;
314199b04f4cSKuninori Morimoto 	component->pcm_new = component->driver->pcm_new;
314299b04f4cSKuninori Morimoto 	component->pcm_free = component->driver->pcm_free;
3143e2c330b9SLars-Peter Clausen 
31444890140fSLars-Peter Clausen 	dapm = &component->dapm;
3145ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3146ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3147ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
31485819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
314914e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
315014e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
315114e8bdebSLars-Peter Clausen 	if (driver->stream_event)
315214e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
3153ce0fc93aSLars-Peter Clausen 
315461aca564SLars-Peter Clausen 	component->controls = driver->controls;
315561aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
315661aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
315761aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
315861aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
315961aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
316061aca564SLars-Peter Clausen 
3161bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3162bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3163bb13109dSLars-Peter Clausen 
3164bb13109dSLars-Peter Clausen 	return 0;
3165d191bd8dSKuninori Morimoto }
3166d191bd8dSKuninori Morimoto 
316720feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3168886f5692SLars-Peter Clausen {
3169886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
317020feb881SLars-Peter Clausen 
3171886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3172886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3173886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3174886f5692SLars-Peter Clausen }
317520feb881SLars-Peter Clausen 
3176e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3177e874bf5fSLars-Peter Clausen 
317820feb881SLars-Peter Clausen /**
317920feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
318020feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
318120feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
318220feb881SLars-Peter Clausen  *
318320feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
318420feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
318520feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
318620feb881SLars-Peter Clausen  * before the first IO attempt of the component.
318720feb881SLars-Peter Clausen  */
318820feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
318920feb881SLars-Peter Clausen 	struct regmap *regmap)
319020feb881SLars-Peter Clausen {
319120feb881SLars-Peter Clausen 	component->regmap = regmap;
319220feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3193886f5692SLars-Peter Clausen }
319420feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
319520feb881SLars-Peter Clausen 
319620feb881SLars-Peter Clausen /**
319720feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
319820feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
319920feb881SLars-Peter Clausen  *
320020feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
320120feb881SLars-Peter Clausen  * removes the regmap instance from the component.
320220feb881SLars-Peter Clausen  *
320320feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
320420feb881SLars-Peter Clausen  * to initialize the regmap instance.
320520feb881SLars-Peter Clausen  */
320620feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
320720feb881SLars-Peter Clausen {
320820feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
320920feb881SLars-Peter Clausen 	component->regmap = NULL;
321020feb881SLars-Peter Clausen }
321120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3212886f5692SLars-Peter Clausen 
3213e874bf5fSLars-Peter Clausen #endif
3214d191bd8dSKuninori Morimoto 
3215bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3216bb13109dSLars-Peter Clausen {
321720feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
321820feb881SLars-Peter Clausen 		if (!component->regmap)
321920feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
322020feb881SLars-Peter Clausen 		if (component->regmap)
322120feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
322220feb881SLars-Peter Clausen 	}
3223886f5692SLars-Peter Clausen 
3224bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
32258a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3226d191bd8dSKuninori Morimoto }
3227d191bd8dSKuninori Morimoto 
3228bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3229bb13109dSLars-Peter Clausen {
3230d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3231bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3232d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3233bb13109dSLars-Peter Clausen }
3234d191bd8dSKuninori Morimoto 
3235bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3236bb13109dSLars-Peter Clausen {
3237bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3238bb13109dSLars-Peter Clausen 	kfree(component->name);
3239bb13109dSLars-Peter Clausen }
3240d191bd8dSKuninori Morimoto 
3241bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3242bb13109dSLars-Peter Clausen {
3243c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3244c12c1aadSKuninori Morimoto 
3245c12c1aadSKuninori Morimoto 	if (card)
3246c12c1aadSKuninori Morimoto 		snd_soc_unregister_card(card);
3247c12c1aadSKuninori Morimoto 
3248bb13109dSLars-Peter Clausen 	list_del(&component->list);
3249bb13109dSLars-Peter Clausen }
3250d191bd8dSKuninori Morimoto 
3251d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3252d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3253d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3254d191bd8dSKuninori Morimoto 			       int num_dai)
3255d191bd8dSKuninori Morimoto {
3256d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3257bb13109dSLars-Peter Clausen 	int ret;
3258d191bd8dSKuninori Morimoto 
3259bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3260d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3261d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3262d191bd8dSKuninori Morimoto 		return -ENOMEM;
3263d191bd8dSKuninori Morimoto 	}
3264d191bd8dSKuninori Morimoto 
3265bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3266bb13109dSLars-Peter Clausen 	if (ret)
3267bb13109dSLars-Peter Clausen 		goto err_free;
3268bb13109dSLars-Peter Clausen 
32693d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
327098e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
32713d59400fSLars-Peter Clausen 
3272bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3273bb13109dSLars-Peter Clausen 	if (ret < 0) {
3274f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3275bb13109dSLars-Peter Clausen 		goto err_cleanup;
3276bb13109dSLars-Peter Clausen 	}
3277bb13109dSLars-Peter Clausen 
3278bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3279bb13109dSLars-Peter Clausen 
3280bb13109dSLars-Peter Clausen 	return 0;
3281bb13109dSLars-Peter Clausen 
3282bb13109dSLars-Peter Clausen err_cleanup:
3283bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3284bb13109dSLars-Peter Clausen err_free:
3285bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3286bb13109dSLars-Peter Clausen 	return ret;
3287d191bd8dSKuninori Morimoto }
3288d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3289d191bd8dSKuninori Morimoto 
3290d191bd8dSKuninori Morimoto /**
3291d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3292d191bd8dSKuninori Morimoto  *
3293628536eaSJonathan Corbet  * @dev: The device to unregister
3294d191bd8dSKuninori Morimoto  */
3295d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3296d191bd8dSKuninori Morimoto {
3297d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3298d191bd8dSKuninori Morimoto 
329934e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3300d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
330198e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3302d191bd8dSKuninori Morimoto 			goto found;
3303d191bd8dSKuninori Morimoto 	}
330434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3305d191bd8dSKuninori Morimoto 	return;
3306d191bd8dSKuninori Morimoto 
3307d191bd8dSKuninori Morimoto found:
33088a978234SLiam Girdwood 	snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
330934e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
331034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3311bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3312bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3313d191bd8dSKuninori Morimoto }
3314d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3315d191bd8dSKuninori Morimoto 
3316f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3317e2c330b9SLars-Peter Clausen {
3318e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3319e2c330b9SLars-Peter Clausen 
3320f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3321e2c330b9SLars-Peter Clausen }
3322e2c330b9SLars-Peter Clausen 
3323f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3324e2c330b9SLars-Peter Clausen {
3325e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3326e2c330b9SLars-Peter Clausen 
3327f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3328e2c330b9SLars-Peter Clausen }
3329e2c330b9SLars-Peter Clausen 
333099b04f4cSKuninori Morimoto static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd)
333199b04f4cSKuninori Morimoto {
333299b04f4cSKuninori Morimoto 	struct snd_soc_platform *platform = rtd->platform;
333399b04f4cSKuninori Morimoto 
3334d6c098a1SBrian Norris 	if (platform->driver->pcm_new)
333599b04f4cSKuninori Morimoto 		return platform->driver->pcm_new(rtd);
3336d6c098a1SBrian Norris 	else
3337d6c098a1SBrian Norris 		return 0;
333899b04f4cSKuninori Morimoto }
333999b04f4cSKuninori Morimoto 
334099b04f4cSKuninori Morimoto static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm)
334199b04f4cSKuninori Morimoto {
334299b04f4cSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = pcm->private_data;
334399b04f4cSKuninori Morimoto 	struct snd_soc_platform *platform = rtd->platform;
334499b04f4cSKuninori Morimoto 
3345d6c098a1SBrian Norris 	if (platform->driver->pcm_free)
334699b04f4cSKuninori Morimoto 		platform->driver->pcm_free(pcm);
334799b04f4cSKuninori Morimoto }
334899b04f4cSKuninori Morimoto 
3349d191bd8dSKuninori Morimoto /**
335071a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
335171a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
335271a45cdaSLars-Peter Clausen  * @platform: The platform to add
33537d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
335412a48a8cSMark Brown  */
335571a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3356d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
335712a48a8cSMark Brown {
3358b37f1d12SLars-Peter Clausen 	int ret;
3359b37f1d12SLars-Peter Clausen 
3360bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3361bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3362bb13109dSLars-Peter Clausen 	if (ret)
3363bb13109dSLars-Peter Clausen 		return ret;
3364f0fba2adSLiam Girdwood 
3365f0fba2adSLiam Girdwood 	platform->dev = dev;
3366f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3367f1d45cc3SLars-Peter Clausen 
3368f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3369f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3370f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3371f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
337299b04f4cSKuninori Morimoto 	if (platform_drv->pcm_new)
337399b04f4cSKuninori Morimoto 		platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
337499b04f4cSKuninori Morimoto 	if (platform_drv->pcm_free)
337599b04f4cSKuninori Morimoto 		platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
337612a48a8cSMark Brown 
337781c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
337881c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
337981c7cfd1SLars-Peter Clausen #endif
338012a48a8cSMark Brown 
338112a48a8cSMark Brown 	mutex_lock(&client_mutex);
3382bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
338312a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
338412a48a8cSMark Brown 	mutex_unlock(&client_mutex);
338512a48a8cSMark Brown 
3386f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3387f4333203SLars-Peter Clausen 		platform->component.name);
338812a48a8cSMark Brown 
338912a48a8cSMark Brown 	return 0;
339012a48a8cSMark Brown }
339171a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
339271a45cdaSLars-Peter Clausen 
339371a45cdaSLars-Peter Clausen /**
339471a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
339571a45cdaSLars-Peter Clausen  *
3396628536eaSJonathan Corbet  * @dev: The device for the platform
3397628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
339871a45cdaSLars-Peter Clausen  */
339971a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
340071a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
340171a45cdaSLars-Peter Clausen {
340271a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
340371a45cdaSLars-Peter Clausen 	int ret;
340471a45cdaSLars-Peter Clausen 
340571a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
340671a45cdaSLars-Peter Clausen 
340771a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
340871a45cdaSLars-Peter Clausen 	if (platform == NULL)
340971a45cdaSLars-Peter Clausen 		return -ENOMEM;
341071a45cdaSLars-Peter Clausen 
341171a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
341271a45cdaSLars-Peter Clausen 	if (ret)
341371a45cdaSLars-Peter Clausen 		kfree(platform);
341471a45cdaSLars-Peter Clausen 
341571a45cdaSLars-Peter Clausen 	return ret;
341671a45cdaSLars-Peter Clausen }
341712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
341812a48a8cSMark Brown 
341912a48a8cSMark Brown /**
342071a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
342171a45cdaSLars-Peter Clausen  * @platform: the platform to remove
342271a45cdaSLars-Peter Clausen  */
342371a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
342471a45cdaSLars-Peter Clausen {
3425b37f1d12SLars-Peter Clausen 
342671a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
342771a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3428bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
342971a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
343071a45cdaSLars-Peter Clausen 
343171a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3432f4333203SLars-Peter Clausen 		platform->component.name);
3433decc27b0SDaniel Mack 
3434decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
343571a45cdaSLars-Peter Clausen }
343671a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
343771a45cdaSLars-Peter Clausen 
343871a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
343971a45cdaSLars-Peter Clausen {
344071a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
344171a45cdaSLars-Peter Clausen 
344234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
344371a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
344434e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
344534e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
344671a45cdaSLars-Peter Clausen 			return platform;
344771a45cdaSLars-Peter Clausen 		}
344834e81ab4SLars-Peter Clausen 	}
344934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
345071a45cdaSLars-Peter Clausen 
345171a45cdaSLars-Peter Clausen 	return NULL;
345271a45cdaSLars-Peter Clausen }
345371a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
345471a45cdaSLars-Peter Clausen 
345571a45cdaSLars-Peter Clausen /**
345612a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
345712a48a8cSMark Brown  *
3458628536eaSJonathan Corbet  * @dev: platform to unregister
345912a48a8cSMark Brown  */
3460f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
346112a48a8cSMark Brown {
3462f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3463f0fba2adSLiam Girdwood 
346471a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
346571a45cdaSLars-Peter Clausen 	if (!platform)
3466f0fba2adSLiam Girdwood 		return;
3467f0fba2adSLiam Girdwood 
346871a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3469f0fba2adSLiam Girdwood 	kfree(platform);
347012a48a8cSMark Brown }
347112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
347212a48a8cSMark Brown 
3473151ab22cSMark Brown static u64 codec_format_map[] = {
3474151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3475151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3476151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3477151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3478151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3479151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3480151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3481151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3482151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3483151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3484151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3485151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3486151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3487151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3488151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3489151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3490151ab22cSMark Brown };
3491151ab22cSMark Brown 
3492151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3493151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3494151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3495151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3496151ab22cSMark Brown  */
3497151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3498151ab22cSMark Brown {
3499151ab22cSMark Brown 	int i;
3500151ab22cSMark Brown 
3501151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3502151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3503151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3504151ab22cSMark Brown }
3505151ab22cSMark Brown 
3506f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3507f1d45cc3SLars-Peter Clausen {
3508f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3509f1d45cc3SLars-Peter Clausen 
3510f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3511f1d45cc3SLars-Peter Clausen }
3512f1d45cc3SLars-Peter Clausen 
3513f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3514f1d45cc3SLars-Peter Clausen {
3515f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3516f1d45cc3SLars-Peter Clausen 
3517f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3518f1d45cc3SLars-Peter Clausen }
3519f1d45cc3SLars-Peter Clausen 
35209178feb4SKuninori Morimoto static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
35219178feb4SKuninori Morimoto {
35229178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
35239178feb4SKuninori Morimoto 
35249178feb4SKuninori Morimoto 	return codec->driver->suspend(codec);
35259178feb4SKuninori Morimoto }
35269178feb4SKuninori Morimoto 
35279178feb4SKuninori Morimoto static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
35289178feb4SKuninori Morimoto {
35299178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
35309178feb4SKuninori Morimoto 
35319178feb4SKuninori Morimoto 	return codec->driver->resume(codec);
35329178feb4SKuninori Morimoto }
35339178feb4SKuninori Morimoto 
3534e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3535e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3536e2c330b9SLars-Peter Clausen {
3537e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3538e2c330b9SLars-Peter Clausen 
3539e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3540e2c330b9SLars-Peter Clausen }
3541e2c330b9SLars-Peter Clausen 
3542e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3543e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3544e2c330b9SLars-Peter Clausen {
3545e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3546e2c330b9SLars-Peter Clausen 
3547e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3548e2c330b9SLars-Peter Clausen 
3549e2c330b9SLars-Peter Clausen 	return 0;
3550e2c330b9SLars-Peter Clausen }
3551e2c330b9SLars-Peter Clausen 
355268f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
355368f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
355468f831c2SLars-Peter Clausen {
355568f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
355668f831c2SLars-Peter Clausen 
355768f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
355868f831c2SLars-Peter Clausen }
355968f831c2SLars-Peter Clausen 
35600d0cf00aSMark Brown /**
35610d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
35620d0cf00aSMark Brown  *
3563628536eaSJonathan Corbet  * @dev: The parent device for this codec
3564628536eaSJonathan Corbet  * @codec_drv: Codec driver
3565628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3566628536eaSJonathan Corbet  * @num_dai: Number of DAIs
35670d0cf00aSMark Brown  */
3568f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3569001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3570001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3571001ae4c0SMark Brown 			   int num_dai)
35720d0cf00aSMark Brown {
35734890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3574f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3575bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3576f0fba2adSLiam Girdwood 	int ret, i;
3577151ab22cSMark Brown 
3578f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
35790d0cf00aSMark Brown 
3580f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3581f0fba2adSLiam Girdwood 	if (codec == NULL)
3582f0fba2adSLiam Girdwood 		return -ENOMEM;
35830d0cf00aSMark Brown 
3584f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3585ce0fc93aSLars-Peter Clausen 
3586bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3587bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3588bb13109dSLars-Peter Clausen 	if (ret)
3589bb13109dSLars-Peter Clausen 		goto err_free;
3590151ab22cSMark Brown 
3591f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3592f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3593f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3594f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
35959178feb4SKuninori Morimoto 	if (codec_drv->suspend)
35969178feb4SKuninori Morimoto 		codec->component.suspend = snd_soc_codec_drv_suspend;
35979178feb4SKuninori Morimoto 	if (codec_drv->resume)
35989178feb4SKuninori Morimoto 		codec->component.resume = snd_soc_codec_drv_resume;
3599e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3600e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3601e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3602e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
36033d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
36044890140fSLars-Peter Clausen 
36054890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
36064890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
36074890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
360814e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
36094890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
361068f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
36114890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3612f0fba2adSLiam Girdwood 	codec->dev = dev;
3613f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3614e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3615f0fba2adSLiam Girdwood 
361681c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
361781c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
361881c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
361981c7cfd1SLars-Peter Clausen #endif
3620a39f75f7SXiubo Li 
3621a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3622886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3623a39f75f7SXiubo Li 
3624f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3625f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3626f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3627f0fba2adSLiam Girdwood 	}
3628f0fba2adSLiam Girdwood 
3629bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3630bb13109dSLars-Peter Clausen 	if (ret < 0) {
3631f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3632bb13109dSLars-Peter Clausen 		goto err_cleanup;
3633bb13109dSLars-Peter Clausen 	}
3634bb13109dSLars-Peter Clausen 
3635bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3636bb13109dSLars-Peter Clausen 		dai->codec = codec;
3637bb13109dSLars-Peter Clausen 
3638054880feSMark Brown 	mutex_lock(&client_mutex);
3639bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3640054880feSMark Brown 	list_add(&codec->list, &codec_list);
3641054880feSMark Brown 	mutex_unlock(&client_mutex);
3642054880feSMark Brown 
3643f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3644f4333203SLars-Peter Clausen 		codec->component.name);
36450d0cf00aSMark Brown 	return 0;
3646f0fba2adSLiam Girdwood 
3647bb13109dSLars-Peter Clausen err_cleanup:
3648bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3649bb13109dSLars-Peter Clausen err_free:
3650f0fba2adSLiam Girdwood 	kfree(codec);
3651f0fba2adSLiam Girdwood 	return ret;
36520d0cf00aSMark Brown }
36530d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
36540d0cf00aSMark Brown 
36550d0cf00aSMark Brown /**
36560d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
36570d0cf00aSMark Brown  *
3658628536eaSJonathan Corbet  * @dev: codec to unregister
36590d0cf00aSMark Brown  */
3660f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
36610d0cf00aSMark Brown {
3662f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3663f0fba2adSLiam Girdwood 
366434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3665f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3666f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3667f0fba2adSLiam Girdwood 			goto found;
3668f0fba2adSLiam Girdwood 	}
366934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3670f0fba2adSLiam Girdwood 	return;
3671f0fba2adSLiam Girdwood 
3672f0fba2adSLiam Girdwood found:
36730d0cf00aSMark Brown 	list_del(&codec->list);
3674bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
36750d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
36760d0cf00aSMark Brown 
3677f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3678f4333203SLars-Peter Clausen 			codec->component.name);
3679f0fba2adSLiam Girdwood 
3680bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
36817a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3682f0fba2adSLiam Girdwood 	kfree(codec);
36830d0cf00aSMark Brown }
36840d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
36850d0cf00aSMark Brown 
3686bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3687b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3688bec4fa05SStephen Warren 			       const char *propname)
3689bec4fa05SStephen Warren {
3690b07609ceSKuninori Morimoto 	struct device_node *np;
3691bec4fa05SStephen Warren 	int ret;
3692bec4fa05SStephen Warren 
36937e07e7c0STushar Behera 	if (!card->dev) {
36947e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
36957e07e7c0STushar Behera 		return -EINVAL;
36967e07e7c0STushar Behera 	}
36977e07e7c0STushar Behera 
36987e07e7c0STushar Behera 	np = card->dev->of_node;
36997e07e7c0STushar Behera 
3700bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3701bec4fa05SStephen Warren 	/*
3702bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3703bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3704bec4fa05SStephen Warren 	 * snd_soc_register_card.
3705bec4fa05SStephen Warren 	 */
3706bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3707bec4fa05SStephen Warren 		dev_err(card->dev,
3708f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3709bec4fa05SStephen Warren 			propname, ret);
3710bec4fa05SStephen Warren 		return ret;
3711bec4fa05SStephen Warren 	}
3712bec4fa05SStephen Warren 
3713bec4fa05SStephen Warren 	return 0;
3714bec4fa05SStephen Warren }
3715b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3716bec4fa05SStephen Warren 
37179a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
37189a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
37199a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
37209a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
37219a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
37229a6d4860SXiubo Li };
37239a6d4860SXiubo Li 
372421efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
37259a6d4860SXiubo Li 					  const char *propname)
37269a6d4860SXiubo Li {
372721efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
37289a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
37299a6d4860SXiubo Li 	const char *template, *wname;
37309a6d4860SXiubo Li 	int i, j, num_widgets, ret;
37319a6d4860SXiubo Li 
37329a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
37339a6d4860SXiubo Li 	if (num_widgets < 0) {
37349a6d4860SXiubo Li 		dev_err(card->dev,
37359a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
37369a6d4860SXiubo Li 		return -EINVAL;
37379a6d4860SXiubo Li 	}
37389a6d4860SXiubo Li 	if (num_widgets & 1) {
37399a6d4860SXiubo Li 		dev_err(card->dev,
37409a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
37419a6d4860SXiubo Li 		return -EINVAL;
37429a6d4860SXiubo Li 	}
37439a6d4860SXiubo Li 
37449a6d4860SXiubo Li 	num_widgets /= 2;
37459a6d4860SXiubo Li 	if (!num_widgets) {
37469a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
37479a6d4860SXiubo Li 			propname);
37489a6d4860SXiubo Li 		return -EINVAL;
37499a6d4860SXiubo Li 	}
37509a6d4860SXiubo Li 
37519a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
37529a6d4860SXiubo Li 			       GFP_KERNEL);
37539a6d4860SXiubo Li 	if (!widgets) {
37549a6d4860SXiubo Li 		dev_err(card->dev,
37559a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
37569a6d4860SXiubo Li 		return -ENOMEM;
37579a6d4860SXiubo Li 	}
37589a6d4860SXiubo Li 
37599a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
37609a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
37619a6d4860SXiubo Li 			2 * i, &template);
37629a6d4860SXiubo Li 		if (ret) {
37639a6d4860SXiubo Li 			dev_err(card->dev,
37649a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
37659a6d4860SXiubo Li 				propname, 2 * i, ret);
37669a6d4860SXiubo Li 			return -EINVAL;
37679a6d4860SXiubo Li 		}
37689a6d4860SXiubo Li 
37699a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
37709a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
37719a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
37729a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
37739a6d4860SXiubo Li 				break;
37749a6d4860SXiubo Li 			}
37759a6d4860SXiubo Li 		}
37769a6d4860SXiubo Li 
37779a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
37789a6d4860SXiubo Li 			dev_err(card->dev,
37799a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
37809a6d4860SXiubo Li 				template);
37819a6d4860SXiubo Li 			return -EINVAL;
37829a6d4860SXiubo Li 		}
37839a6d4860SXiubo Li 
37849a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
37859a6d4860SXiubo Li 						    (2 * i) + 1,
37869a6d4860SXiubo Li 						    &wname);
37879a6d4860SXiubo Li 		if (ret) {
37889a6d4860SXiubo Li 			dev_err(card->dev,
37899a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
37909a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
37919a6d4860SXiubo Li 			return -EINVAL;
37929a6d4860SXiubo Li 		}
37939a6d4860SXiubo Li 
37949a6d4860SXiubo Li 		widgets[i].name = wname;
37959a6d4860SXiubo Li 	}
37969a6d4860SXiubo Li 
3797f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3798f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
37999a6d4860SXiubo Li 
38009a6d4860SXiubo Li 	return 0;
38019a6d4860SXiubo Li }
380221efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
38039a6d4860SXiubo Li 
38046131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
38056131084aSJyri Sarha 				    const char *prop_name,
38066131084aSJyri Sarha 				    unsigned int *mask)
38076131084aSJyri Sarha {
38086131084aSJyri Sarha 	u32 val;
38096c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
38106131084aSJyri Sarha 	int i;
38116131084aSJyri Sarha 
38126131084aSJyri Sarha 	if (!of_slot_mask)
38136131084aSJyri Sarha 		return 0;
38146131084aSJyri Sarha 	val /= sizeof(u32);
38156131084aSJyri Sarha 	for (i = 0; i < val; i++)
38166131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
38176131084aSJyri Sarha 			*mask |= (1 << i);
38186131084aSJyri Sarha 
38196131084aSJyri Sarha 	return val;
38206131084aSJyri Sarha }
38216131084aSJyri Sarha 
382289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
38236131084aSJyri Sarha 			      unsigned int *tx_mask,
38246131084aSJyri Sarha 			      unsigned int *rx_mask,
382589c67857SXiubo Li 			      unsigned int *slots,
382689c67857SXiubo Li 			      unsigned int *slot_width)
382789c67857SXiubo Li {
382889c67857SXiubo Li 	u32 val;
382989c67857SXiubo Li 	int ret;
383089c67857SXiubo Li 
38316131084aSJyri Sarha 	if (tx_mask)
38326131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
38336131084aSJyri Sarha 	if (rx_mask)
38346131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
38356131084aSJyri Sarha 
383689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
383789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
383889c67857SXiubo Li 		if (ret)
383989c67857SXiubo Li 			return ret;
384089c67857SXiubo Li 
384189c67857SXiubo Li 		if (slots)
384289c67857SXiubo Li 			*slots = val;
384389c67857SXiubo Li 	}
384489c67857SXiubo Li 
384589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
384689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
384789c67857SXiubo Li 		if (ret)
384889c67857SXiubo Li 			return ret;
384989c67857SXiubo Li 
385089c67857SXiubo Li 		if (slot_width)
385189c67857SXiubo Li 			*slot_width = val;
385289c67857SXiubo Li 	}
385389c67857SXiubo Li 
385489c67857SXiubo Li 	return 0;
385589c67857SXiubo Li }
385689c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
385789c67857SXiubo Li 
3858440a3006SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
38595e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
38605e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
38615e3cdaa2SKuninori Morimoto 				   const char *propname)
38625e3cdaa2SKuninori Morimoto {
3863440a3006SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
38645e3cdaa2SKuninori Morimoto 	const char *str;
38655e3cdaa2SKuninori Morimoto 	int ret;
38665e3cdaa2SKuninori Morimoto 
38675e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
38685e3cdaa2SKuninori Morimoto 	if (ret < 0) {
38695e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
38705e3cdaa2SKuninori Morimoto 		return;
38715e3cdaa2SKuninori Morimoto 	}
38725e3cdaa2SKuninori Morimoto 
38735e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
38745e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
38755e3cdaa2SKuninori Morimoto }
3876440a3006SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
38775e3cdaa2SKuninori Morimoto 
38782bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3879a4a54dd5SStephen Warren 				   const char *propname)
3880a4a54dd5SStephen Warren {
38812bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3882e3b1e6a1SMark Brown 	int num_routes;
3883a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3884a4a54dd5SStephen Warren 	int i, ret;
3885a4a54dd5SStephen Warren 
3886a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3887c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
388810e8aa9aSMichał Mirosław 		dev_err(card->dev,
388910e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
389010e8aa9aSMichał Mirosław 			propname);
3891a4a54dd5SStephen Warren 		return -EINVAL;
3892a4a54dd5SStephen Warren 	}
3893a4a54dd5SStephen Warren 	num_routes /= 2;
3894a4a54dd5SStephen Warren 	if (!num_routes) {
3895f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3896a4a54dd5SStephen Warren 			propname);
3897a4a54dd5SStephen Warren 		return -EINVAL;
3898a4a54dd5SStephen Warren 	}
3899a4a54dd5SStephen Warren 
3900e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3901a4a54dd5SStephen Warren 			      GFP_KERNEL);
3902a4a54dd5SStephen Warren 	if (!routes) {
3903a4a54dd5SStephen Warren 		dev_err(card->dev,
3904f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3905a4a54dd5SStephen Warren 		return -EINVAL;
3906a4a54dd5SStephen Warren 	}
3907a4a54dd5SStephen Warren 
3908a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3909a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3910e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3911a4a54dd5SStephen Warren 		if (ret) {
3912c871bd0bSMark Brown 			dev_err(card->dev,
3913c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3914c871bd0bSMark Brown 				propname, 2 * i, ret);
3915a4a54dd5SStephen Warren 			return -EINVAL;
3916a4a54dd5SStephen Warren 		}
3917a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3918e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3919a4a54dd5SStephen Warren 		if (ret) {
3920a4a54dd5SStephen Warren 			dev_err(card->dev,
3921c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3922c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3923a4a54dd5SStephen Warren 			return -EINVAL;
3924a4a54dd5SStephen Warren 		}
3925a4a54dd5SStephen Warren 	}
3926a4a54dd5SStephen Warren 
3927f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3928f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3929a4a54dd5SStephen Warren 
3930a4a54dd5SStephen Warren 	return 0;
3931a4a54dd5SStephen Warren }
39322bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3933a4a54dd5SStephen Warren 
3934a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3935389cb834SJyri Sarha 				     const char *prefix,
3936389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3937389cb834SJyri Sarha 				     struct device_node **framemaster)
3938a7930ed4SKuninori Morimoto {
3939a7930ed4SKuninori Morimoto 	int ret, i;
3940a7930ed4SKuninori Morimoto 	char prop[128];
3941a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3942a7930ed4SKuninori Morimoto 	int bit, frame;
3943a7930ed4SKuninori Morimoto 	const char *str;
3944a7930ed4SKuninori Morimoto 	struct {
3945a7930ed4SKuninori Morimoto 		char *name;
3946a7930ed4SKuninori Morimoto 		unsigned int val;
3947a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3948a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3949a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3950a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3951a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3952a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3953a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3954a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3955a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3956a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3957a7930ed4SKuninori Morimoto 	};
3958a7930ed4SKuninori Morimoto 
3959a7930ed4SKuninori Morimoto 	if (!prefix)
3960a7930ed4SKuninori Morimoto 		prefix = "";
3961a7930ed4SKuninori Morimoto 
3962a7930ed4SKuninori Morimoto 	/*
3963*5711c979SKuninori Morimoto 	 * check "dai-format = xxx"
3964*5711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3965a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3966a7930ed4SKuninori Morimoto 	 */
3967*5711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
3968*5711c979SKuninori Morimoto 	if (ret < 0) {
3969a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3970a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
3971*5711c979SKuninori Morimoto 	}
3972a7930ed4SKuninori Morimoto 	if (ret == 0) {
3973a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3974a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3975a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3976a7930ed4SKuninori Morimoto 				break;
3977a7930ed4SKuninori Morimoto 			}
3978a7930ed4SKuninori Morimoto 		}
3979a7930ed4SKuninori Morimoto 	}
3980a7930ed4SKuninori Morimoto 
3981a7930ed4SKuninori Morimoto 	/*
39828c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3983a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3984a7930ed4SKuninori Morimoto 	 */
39858c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
398651930295SJulia Lawall 	if (of_property_read_bool(np, prop))
39878c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
39888c2d6a9fSKuninori Morimoto 	else
39898c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3990a7930ed4SKuninori Morimoto 
3991a7930ed4SKuninori Morimoto 	/*
3992a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3993a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3994a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3995a7930ed4SKuninori Morimoto 	 */
3996a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3997a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3998a7930ed4SKuninori Morimoto 
3999a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4000a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4001a7930ed4SKuninori Morimoto 
4002a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4003a7930ed4SKuninori Morimoto 	case 0x11:
4004a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4005a7930ed4SKuninori Morimoto 		break;
4006a7930ed4SKuninori Morimoto 	case 0x10:
4007a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4008a7930ed4SKuninori Morimoto 		break;
4009a7930ed4SKuninori Morimoto 	case 0x01:
4010a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4011a7930ed4SKuninori Morimoto 		break;
4012a7930ed4SKuninori Morimoto 	default:
4013a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4014a7930ed4SKuninori Morimoto 		break;
4015a7930ed4SKuninori Morimoto 	}
4016a7930ed4SKuninori Morimoto 
4017a7930ed4SKuninori Morimoto 	/*
4018a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4019a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4020a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4021a7930ed4SKuninori Morimoto 	 */
4022a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4023a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4024389cb834SJyri Sarha 	if (bit && bitclkmaster)
4025389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4026a7930ed4SKuninori Morimoto 
4027a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4028a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4029389cb834SJyri Sarha 	if (frame && framemaster)
4030389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4031a7930ed4SKuninori Morimoto 
4032a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4033a7930ed4SKuninori Morimoto 	case 0x11:
4034a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4035a7930ed4SKuninori Morimoto 		break;
4036a7930ed4SKuninori Morimoto 	case 0x10:
4037a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4038a7930ed4SKuninori Morimoto 		break;
4039a7930ed4SKuninori Morimoto 	case 0x01:
4040a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4041a7930ed4SKuninori Morimoto 		break;
4042a7930ed4SKuninori Morimoto 	default:
4043a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4044a7930ed4SKuninori Morimoto 		break;
4045a7930ed4SKuninori Morimoto 	}
4046a7930ed4SKuninori Morimoto 
4047a7930ed4SKuninori Morimoto 	return format;
4048a7930ed4SKuninori Morimoto }
4049a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4050a7930ed4SKuninori Morimoto 
40511ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
4052cb470087SKuninori Morimoto 				const char **dai_name)
4053cb470087SKuninori Morimoto {
4054cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
40553e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
405693b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
4057cb470087SKuninori Morimoto 
4058cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4059cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
40603e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
40613e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
40623e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
40633e0aa8d8SJyri Sarha 
40643e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
4065cb470087SKuninori Morimoto 			continue;
4066cb470087SKuninori Morimoto 
40676833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
406893b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
406993b0f3eeSJean-Francois Moine 							     args,
407093b0f3eeSJean-Francois Moine 							     dai_name);
40716833c452SKuninori Morimoto 		} else {
40726833c452SKuninori Morimoto 			int id = -1;
40736833c452SKuninori Morimoto 
407493b0f3eeSJean-Francois Moine 			switch (args->args_count) {
40756833c452SKuninori Morimoto 			case 0:
40766833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
40776833c452SKuninori Morimoto 				break;
40786833c452SKuninori Morimoto 			case 1:
407993b0f3eeSJean-Francois Moine 				id = args->args[0];
40806833c452SKuninori Morimoto 				break;
40816833c452SKuninori Morimoto 			default:
40826833c452SKuninori Morimoto 				/* not supported */
4083cb470087SKuninori Morimoto 				break;
4084cb470087SKuninori Morimoto 			}
4085cb470087SKuninori Morimoto 
40866833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
40876833c452SKuninori Morimoto 				ret = -EINVAL;
40883dcba280SNicolin Chen 				continue;
40896833c452SKuninori Morimoto 			}
4090e41975edSXiubo Li 
4091e41975edSXiubo Li 			ret = 0;
4092e41975edSXiubo Li 
4093e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4094e41975edSXiubo Li 			if (!*dai_name)
4095e41975edSXiubo Li 				*dai_name = pos->name;
40966833c452SKuninori Morimoto 		}
40976833c452SKuninori Morimoto 
4098cb470087SKuninori Morimoto 		break;
4099cb470087SKuninori Morimoto 	}
4100cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
410193b0f3eeSJean-Francois Moine 	return ret;
410293b0f3eeSJean-Francois Moine }
41031ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
410493b0f3eeSJean-Francois Moine 
410593b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
410693b0f3eeSJean-Francois Moine 			    const char **dai_name)
410793b0f3eeSJean-Francois Moine {
410893b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
410993b0f3eeSJean-Francois Moine 	int ret;
411093b0f3eeSJean-Francois Moine 
411193b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
411293b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
411393b0f3eeSJean-Francois Moine 	if (ret)
411493b0f3eeSJean-Francois Moine 		return ret;
411593b0f3eeSJean-Francois Moine 
411693b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
4117cb470087SKuninori Morimoto 
4118cb470087SKuninori Morimoto 	of_node_put(args.np);
4119cb470087SKuninori Morimoto 
4120cb470087SKuninori Morimoto 	return ret;
4121cb470087SKuninori Morimoto }
4122cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4123cb470087SKuninori Morimoto 
412493b0f3eeSJean-Francois Moine /*
412593b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
412693b0f3eeSJean-Francois Moine  * @dev: Card device
412793b0f3eeSJean-Francois Moine  * @of_node: Device node
412893b0f3eeSJean-Francois Moine  * @dai_link: DAI link
412993b0f3eeSJean-Francois Moine  *
413093b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
413193b0f3eeSJean-Francois Moine  * 'sound-dai'.
413293b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
413393b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
413493b0f3eeSJean-Francois Moine  *
413593b0f3eeSJean-Francois Moine  * Returns 0 for success
413693b0f3eeSJean-Francois Moine  */
413793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
413893b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
413993b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
414093b0f3eeSJean-Francois Moine {
414193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
414293b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
414393b0f3eeSJean-Francois Moine 	char *name;
414493b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
414593b0f3eeSJean-Francois Moine 
414693b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
414793b0f3eeSJean-Francois Moine 	name = "sound-dai";
414893b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
414993b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
415093b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
415193b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
415293b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
415393b0f3eeSJean-Francois Moine 		else
415493b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
415593b0f3eeSJean-Francois Moine 		return num_codecs;
415693b0f3eeSJean-Francois Moine 	}
415793b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
415893b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
415993b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
416093b0f3eeSJean-Francois Moine 	if (!component)
416193b0f3eeSJean-Francois Moine 		return -ENOMEM;
416293b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
416393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
416493b0f3eeSJean-Francois Moine 
416593b0f3eeSJean-Francois Moine 	/* Parse the list */
416693b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
416793b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
416893b0f3eeSJean-Francois Moine 	     index++, component++) {
416993b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
417093b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
417193b0f3eeSJean-Francois Moine 						  index, &args);
417293b0f3eeSJean-Francois Moine 		if (ret)
417393b0f3eeSJean-Francois Moine 			goto err;
417493b0f3eeSJean-Francois Moine 		component->of_node = args.np;
417593b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
417693b0f3eeSJean-Francois Moine 		if (ret < 0)
417793b0f3eeSJean-Francois Moine 			goto err;
417893b0f3eeSJean-Francois Moine 	}
417993b0f3eeSJean-Francois Moine 	return 0;
418093b0f3eeSJean-Francois Moine err:
418193b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
418293b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
418393b0f3eeSJean-Francois Moine 	     index++, component++) {
418493b0f3eeSJean-Francois Moine 		if (!component->of_node)
418593b0f3eeSJean-Francois Moine 			break;
418693b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
418793b0f3eeSJean-Francois Moine 		component->of_node = NULL;
418893b0f3eeSJean-Francois Moine 	}
418993b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
419093b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
419193b0f3eeSJean-Francois Moine 	return ret;
419293b0f3eeSJean-Francois Moine }
419393b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
419493b0f3eeSJean-Francois Moine 
4195c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4196db2a4165SFrank Mandarino {
41976553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
4198fb257897SMark Brown 	snd_soc_util_init();
4199fb257897SMark Brown 
4200db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4201db2a4165SFrank Mandarino }
42024abe8e16SMark Brown module_init(snd_soc_init);
4203db2a4165SFrank Mandarino 
42047d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4205db2a4165SFrank Mandarino {
4206fb257897SMark Brown 	snd_soc_util_exit();
42076553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
4208fb257897SMark Brown 
4209db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4210db2a4165SFrank Mandarino }
4211db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4212db2a4165SFrank Mandarino 
4213db2a4165SFrank Mandarino /* Module information */
4214d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4215db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4216db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4217db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4218