xref: /openbmc/linux/sound/soc/soc-core.c (revision 3e0aa8d83bf8e6d414e538cf1046a3a7b48017bc)
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>
37db2a4165SFrank Mandarino #include <sound/core.h>
383028eb8cSMark Brown #include <sound/jack.h>
39db2a4165SFrank Mandarino #include <sound/pcm.h>
40db2a4165SFrank Mandarino #include <sound/pcm_params.h>
41db2a4165SFrank Mandarino #include <sound/soc.h>
4201d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
43db2a4165SFrank Mandarino #include <sound/initval.h>
44db2a4165SFrank Mandarino 
45a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
46a8b1d34fSMark Brown #include <trace/events/asoc.h>
47a8b1d34fSMark Brown 
48f0fba2adSLiam Girdwood #define NAME_SIZE	32
49f0fba2adSLiam Girdwood 
50384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
518a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
528a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53384c89e2SMark Brown #endif
54384c89e2SMark Brown 
55c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5612a48a8cSMark Brown static LIST_HEAD(platform_list);
570d0cf00aSMark Brown static LIST_HEAD(codec_list);
58030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
59c5af3a2eSMark Brown 
60db2a4165SFrank Mandarino /*
61db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
62db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
63db2a4165SFrank Mandarino  * between two audio tracks.
64db2a4165SFrank Mandarino  */
65db2a4165SFrank Mandarino static int pmdown_time = 5000;
66db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
67db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68db2a4165SFrank Mandarino 
692bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
702bc9a81eSDimitris Papastamos  * a particular given value */
712bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
722bc9a81eSDimitris Papastamos {
732bc9a81eSDimitris Papastamos 	int c = 0;
742bc9a81eSDimitris Papastamos 	int i;
752bc9a81eSDimitris Papastamos 
762bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
772bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
782bc9a81eSDimitris Papastamos 			break;
792bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
802bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
812bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
822bc9a81eSDimitris Papastamos 	else
832bc9a81eSDimitris Papastamos 		c /= 8;
842bc9a81eSDimitris Papastamos 	return c;
852bc9a81eSDimitris Papastamos }
862bc9a81eSDimitris Papastamos 
8713fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
8813fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
8913fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9013fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
912624d5faSMark Brown {
9200b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9300b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9413fd179fSDimitris Papastamos 	int ret;
9513fd179fSDimitris Papastamos 
9613fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
9713fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
9813fd179fSDimitris Papastamos 		return -EINVAL;
9913fd179fSDimitris Papastamos 
100d6b6c2caSTakashi Iwai 	sprintf(buf, "%.*x: ", wordsize, reg);
101d6b6c2caSTakashi Iwai 	buf += wordsize + 2;
102d6b6c2caSTakashi Iwai 
10313fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
104d6b6c2caSTakashi Iwai 	if (ret < 0)
105d6b6c2caSTakashi Iwai 		memset(buf, 'X', regsize);
106d6b6c2caSTakashi Iwai 	else
107d6b6c2caSTakashi Iwai 		sprintf(buf, "%.*x", regsize, ret);
108d6b6c2caSTakashi Iwai 	buf[regsize] = '\n';
109d6b6c2caSTakashi Iwai 	/* no NUL-termination needed */
11013fd179fSDimitris Papastamos 	return 0;
11113fd179fSDimitris Papastamos }
11213fd179fSDimitris Papastamos 
11313fd179fSDimitris Papastamos /* codec register dump */
11413fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
11513fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
11613fd179fSDimitris Papastamos {
11713fd179fSDimitris Papastamos 	int i, step = 1;
1182bc9a81eSDimitris Papastamos 	int wordsize, regsize;
11913fd179fSDimitris Papastamos 	int len;
12013fd179fSDimitris Papastamos 	size_t total = 0;
12113fd179fSDimitris Papastamos 	loff_t p = 0;
1222bc9a81eSDimitris Papastamos 
12300b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
12400b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1252624d5faSMark Brown 
12613fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
12713fd179fSDimitris Papastamos 
128f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1292624d5faSMark Brown 		return 0;
1302624d5faSMark Brown 
131f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
132f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1332624d5faSMark Brown 
134f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
13513fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
13613fd179fSDimitris Papastamos 		 * entries for the default case */
13713fd179fSDimitris Papastamos 		if (p >= pos) {
13813fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1392624d5faSMark Brown 				break;
14013fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
14113fd179fSDimitris Papastamos 			total += len;
14213fd179fSDimitris Papastamos 		}
14313fd179fSDimitris Papastamos 		p += len;
14413fd179fSDimitris Papastamos 	}
1452624d5faSMark Brown 
14613fd179fSDimitris Papastamos 	total = min(total, count - 1);
1472624d5faSMark Brown 
14813fd179fSDimitris Papastamos 	return total;
1492624d5faSMark Brown }
15013fd179fSDimitris Papastamos 
1512624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1522624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1532624d5faSMark Brown {
15436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
155f0fba2adSLiam Girdwood 
15613fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1572624d5faSMark Brown }
1582624d5faSMark Brown 
1592624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1602624d5faSMark Brown 
161dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
162dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
163dbe21408SMark Brown {
16436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
165dbe21408SMark Brown 
166f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
167dbe21408SMark Brown }
168dbe21408SMark Brown 
169dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
170dbe21408SMark Brown 			       struct device_attribute *attr,
171dbe21408SMark Brown 			       const char *buf, size_t count)
172dbe21408SMark Brown {
17336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
174c593b520SMark Brown 	int ret;
175dbe21408SMark Brown 
176b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
177c593b520SMark Brown 	if (ret)
178c593b520SMark Brown 		return ret;
179dbe21408SMark Brown 
180dbe21408SMark Brown 	return count;
181dbe21408SMark Brown }
182dbe21408SMark Brown 
183dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
184dbe21408SMark Brown 
185d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
186d29697dcSTakashi Iwai 	&dev_attr_codec_reg.attr,
187d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
188d29697dcSTakashi Iwai 	NULL
189d29697dcSTakashi Iwai };
190d29697dcSTakashi Iwai 
191d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
192d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
193d29697dcSTakashi Iwai {
194d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
195d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
196d29697dcSTakashi Iwai 
197d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
198d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
199d29697dcSTakashi Iwai 	return rtd->codec ? attr->mode : 0; /* enabled only with codec */
200d29697dcSTakashi Iwai }
201d29697dcSTakashi Iwai 
202d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
203d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
204d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
205d29697dcSTakashi Iwai };
206d29697dcSTakashi Iwai 
207d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = {
208d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
209d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
210d29697dcSTakashi Iwai };
211d29697dcSTakashi Iwai 
212d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
213d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
214d29697dcSTakashi Iwai 	&soc_dev_roup,
215d29697dcSTakashi Iwai 	NULL
216d29697dcSTakashi Iwai };
217d29697dcSTakashi Iwai 
2182624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2192624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2202624d5faSMark Brown 				   size_t count, loff_t *ppos)
2212624d5faSMark Brown {
2222624d5faSMark Brown 	ssize_t ret;
2232624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
22413fd179fSDimitris Papastamos 	char *buf;
22513fd179fSDimitris Papastamos 
22613fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22713fd179fSDimitris Papastamos 		return -EINVAL;
22813fd179fSDimitris Papastamos 
22913fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2302624d5faSMark Brown 	if (!buf)
2312624d5faSMark Brown 		return -ENOMEM;
23213fd179fSDimitris Papastamos 
23313fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
23413fd179fSDimitris Papastamos 	if (ret >= 0) {
23513fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23613fd179fSDimitris Papastamos 			kfree(buf);
23713fd179fSDimitris Papastamos 			return -EFAULT;
23813fd179fSDimitris Papastamos 		}
23913fd179fSDimitris Papastamos 		*ppos += ret;
24013fd179fSDimitris Papastamos 	}
24113fd179fSDimitris Papastamos 
2422624d5faSMark Brown 	kfree(buf);
2432624d5faSMark Brown 	return ret;
2442624d5faSMark Brown }
2452624d5faSMark Brown 
2462624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2472624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2482624d5faSMark Brown {
2492624d5faSMark Brown 	char buf[32];
25034e268d8SStephen Boyd 	size_t buf_size;
2512624d5faSMark Brown 	char *start = buf;
2522624d5faSMark Brown 	unsigned long reg, value;
2532624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
254b785a492SJingoo Han 	int ret;
2552624d5faSMark Brown 
2562624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2572624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2582624d5faSMark Brown 		return -EFAULT;
2592624d5faSMark Brown 	buf[buf_size] = 0;
2602624d5faSMark Brown 
2612624d5faSMark Brown 	while (*start == ' ')
2622624d5faSMark Brown 		start++;
2632624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2642624d5faSMark Brown 	while (*start == ' ')
2652624d5faSMark Brown 		start++;
266b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
267b785a492SJingoo Han 	if (ret)
268b785a492SJingoo Han 		return ret;
2690d51a9cbSMark Brown 
2700d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
271373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2720d51a9cbSMark Brown 
273e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2742624d5faSMark Brown 	return buf_size;
2752624d5faSMark Brown }
2762624d5faSMark Brown 
2772624d5faSMark Brown static const struct file_operations codec_reg_fops = {
278234e3405SStephen Boyd 	.open = simple_open,
2792624d5faSMark Brown 	.read = codec_reg_read_file,
2802624d5faSMark Brown 	.write = codec_reg_write_file,
2816038f373SArnd Bergmann 	.llseek = default_llseek,
2822624d5faSMark Brown };
2832624d5faSMark Brown 
28481c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
285e73f3de5SRussell King {
2866553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
2876553bf06SLars-Peter Clausen 		return;
2886553bf06SLars-Peter Clausen 
28981c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
29081c7cfd1SLars-Peter Clausen 		char *name;
291e73f3de5SRussell King 
29281c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
29381c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
29481c7cfd1SLars-Peter Clausen 		if (name) {
29581c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
29681c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
29781c7cfd1SLars-Peter Clausen 			kfree(name);
29881c7cfd1SLars-Peter Clausen 		}
29981c7cfd1SLars-Peter Clausen 	} else {
30081c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
30181c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
302e73f3de5SRussell King 	}
303e73f3de5SRussell King 
30481c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
30581c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
30681c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3072624d5faSMark Brown 		return;
3082624d5faSMark Brown 	}
3092624d5faSMark Brown 
31081c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
31181c7cfd1SLars-Peter Clausen 		component->debugfs_root);
31281c7cfd1SLars-Peter Clausen 
31381c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
31481c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
31581c7cfd1SLars-Peter Clausen }
31681c7cfd1SLars-Peter Clausen 
31781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
31881c7cfd1SLars-Peter Clausen {
31981c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
32081c7cfd1SLars-Peter Clausen }
32181c7cfd1SLars-Peter Clausen 
32281c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
32381c7cfd1SLars-Peter Clausen {
32481c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
32581c7cfd1SLars-Peter Clausen 
3262624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
32781c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3282624d5faSMark Brown 						 codec, &codec_reg_fops);
3292624d5faSMark Brown 	if (!codec->debugfs_reg)
33010e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
33110e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
332731f1ab2SSebastien Guiriec }
333731f1ab2SSebastien Guiriec 
334c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
335c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
336c3c5a19aSMark Brown {
337c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3382b194f9dSMark Brown 	ssize_t len, ret = 0;
339c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
340c3c5a19aSMark Brown 
341c3c5a19aSMark Brown 	if (!buf)
342c3c5a19aSMark Brown 		return -ENOMEM;
343c3c5a19aSMark Brown 
34434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
34534e81ab4SLars-Peter Clausen 
3462b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3472b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
348f4333203SLars-Peter Clausen 			       codec->component.name);
3492b194f9dSMark Brown 		if (len >= 0)
3502b194f9dSMark Brown 			ret += len;
3512b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3522b194f9dSMark Brown 			ret = PAGE_SIZE;
3532b194f9dSMark Brown 			break;
3542b194f9dSMark Brown 		}
3552b194f9dSMark Brown 	}
356c3c5a19aSMark Brown 
35734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
35834e81ab4SLars-Peter Clausen 
359c3c5a19aSMark Brown 	if (ret >= 0)
360c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
361c3c5a19aSMark Brown 
362c3c5a19aSMark Brown 	kfree(buf);
363c3c5a19aSMark Brown 
364c3c5a19aSMark Brown 	return ret;
365c3c5a19aSMark Brown }
366c3c5a19aSMark Brown 
367c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
368c3c5a19aSMark Brown 	.read = codec_list_read_file,
369c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
370c3c5a19aSMark Brown };
371c3c5a19aSMark Brown 
372f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
373f3208780SMark Brown 				  size_t count, loff_t *ppos)
374f3208780SMark Brown {
375f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3762b194f9dSMark Brown 	ssize_t len, ret = 0;
3771438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
378f3208780SMark Brown 	struct snd_soc_dai *dai;
379f3208780SMark Brown 
380f3208780SMark Brown 	if (!buf)
381f3208780SMark Brown 		return -ENOMEM;
382f3208780SMark Brown 
38334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
38434e81ab4SLars-Peter Clausen 
3851438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3861438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3871438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3881438c2f6SLars-Peter Clausen 				dai->name);
3892b194f9dSMark Brown 			if (len >= 0)
3902b194f9dSMark Brown 				ret += len;
3912b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3922b194f9dSMark Brown 				ret = PAGE_SIZE;
3932b194f9dSMark Brown 				break;
3942b194f9dSMark Brown 			}
3952b194f9dSMark Brown 		}
3961438c2f6SLars-Peter Clausen 	}
397f3208780SMark Brown 
39834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
39934e81ab4SLars-Peter Clausen 
400f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
401f3208780SMark Brown 
402f3208780SMark Brown 	kfree(buf);
403f3208780SMark Brown 
404f3208780SMark Brown 	return ret;
405f3208780SMark Brown }
406f3208780SMark Brown 
407f3208780SMark Brown static const struct file_operations dai_list_fops = {
408f3208780SMark Brown 	.read = dai_list_read_file,
409f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
410f3208780SMark Brown };
411f3208780SMark Brown 
41219c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41319c7ac27SMark Brown 				       char __user *user_buf,
41419c7ac27SMark Brown 				       size_t count, loff_t *ppos)
41519c7ac27SMark Brown {
41619c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4172b194f9dSMark Brown 	ssize_t len, ret = 0;
41819c7ac27SMark Brown 	struct snd_soc_platform *platform;
41919c7ac27SMark Brown 
42019c7ac27SMark Brown 	if (!buf)
42119c7ac27SMark Brown 		return -ENOMEM;
42219c7ac27SMark Brown 
42334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
42434e81ab4SLars-Peter Clausen 
4252b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4262b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
427f4333203SLars-Peter Clausen 			       platform->component.name);
4282b194f9dSMark Brown 		if (len >= 0)
4292b194f9dSMark Brown 			ret += len;
4302b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4312b194f9dSMark Brown 			ret = PAGE_SIZE;
4322b194f9dSMark Brown 			break;
4332b194f9dSMark Brown 		}
4342b194f9dSMark Brown 	}
43519c7ac27SMark Brown 
43634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
43734e81ab4SLars-Peter Clausen 
43819c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
43919c7ac27SMark Brown 
44019c7ac27SMark Brown 	kfree(buf);
44119c7ac27SMark Brown 
44219c7ac27SMark Brown 	return ret;
44319c7ac27SMark Brown }
44419c7ac27SMark Brown 
44519c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44619c7ac27SMark Brown 	.read = platform_list_read_file,
44719c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
44819c7ac27SMark Brown };
44919c7ac27SMark Brown 
450a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
451a6052154SJarkko Nikula {
4526553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
4536553bf06SLars-Peter Clausen 		return;
4546553bf06SLars-Peter Clausen 
455a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4568a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4573a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
458a6052154SJarkko Nikula 		dev_warn(card->dev,
4597c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4603a45b867SJarkko Nikula 		return;
4613a45b867SJarkko Nikula 	}
4623a45b867SJarkko Nikula 
4633a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4643a45b867SJarkko Nikula 						    card->debugfs_card_root,
4653a45b867SJarkko Nikula 						    &card->pop_time);
4663a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4673a45b867SJarkko Nikula 		dev_warn(card->dev,
468f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
469a6052154SJarkko Nikula }
470a6052154SJarkko Nikula 
471a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
472a6052154SJarkko Nikula {
473a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
474a6052154SJarkko Nikula }
475a6052154SJarkko Nikula 
4766553bf06SLars-Peter Clausen 
4776553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
4786553bf06SLars-Peter Clausen {
4796553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4806553bf06SLars-Peter Clausen 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4816553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
4826553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
4836553bf06SLars-Peter Clausen 		return;
4846553bf06SLars-Peter Clausen 	}
4856553bf06SLars-Peter Clausen 
4866553bf06SLars-Peter Clausen 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4876553bf06SLars-Peter Clausen 				 &codec_list_fops))
4886553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4896553bf06SLars-Peter Clausen 
4906553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4916553bf06SLars-Peter Clausen 				 &dai_list_fops))
4926553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4936553bf06SLars-Peter Clausen 
4946553bf06SLars-Peter Clausen 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4956553bf06SLars-Peter Clausen 				 &platform_list_fops))
4966553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4976553bf06SLars-Peter Clausen }
4986553bf06SLars-Peter Clausen 
4996553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
5006553bf06SLars-Peter Clausen {
5016553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
5026553bf06SLars-Peter Clausen }
5036553bf06SLars-Peter Clausen 
5042624d5faSMark Brown #else
5052624d5faSMark Brown 
50681c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
50781c7cfd1SLars-Peter Clausen 
50881c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
50981c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
5102624d5faSMark Brown {
5112624d5faSMark Brown }
5122624d5faSMark Brown 
51381c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
51481c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
515731f1ab2SSebastien Guiriec {
516731f1ab2SSebastien Guiriec }
517731f1ab2SSebastien Guiriec 
518b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
519b95fccbcSAxel Lin {
520b95fccbcSAxel Lin }
521b95fccbcSAxel Lin 
522b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
523b95fccbcSAxel Lin {
524b95fccbcSAxel Lin }
5256553bf06SLars-Peter Clausen 
5266553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
5276553bf06SLars-Peter Clausen {
5286553bf06SLars-Peter Clausen }
5296553bf06SLars-Peter Clausen 
5306553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
5316553bf06SLars-Peter Clausen {
5326553bf06SLars-Peter Clausen }
5336553bf06SLars-Peter Clausen 
5342624d5faSMark Brown #endif
5352624d5faSMark Brown 
53647c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
53747c88fffSLiam Girdwood 		const char *dai_link, int stream)
53847c88fffSLiam Girdwood {
53947c88fffSLiam Girdwood 	int i;
54047c88fffSLiam Girdwood 
54147c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
54247c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
54347c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
54447c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
54547c88fffSLiam Girdwood 	}
546f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
54747c88fffSLiam Girdwood 	return NULL;
54847c88fffSLiam Girdwood }
54947c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
55047c88fffSLiam Girdwood 
55147c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
55247c88fffSLiam Girdwood 		const char *dai_link)
55347c88fffSLiam Girdwood {
55447c88fffSLiam Girdwood 	int i;
55547c88fffSLiam Girdwood 
55647c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
55747c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
55847c88fffSLiam Girdwood 			return &card->rtd[i];
55947c88fffSLiam Girdwood 	}
560f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
56147c88fffSLiam Girdwood 	return NULL;
56247c88fffSLiam Girdwood }
56347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
56447c88fffSLiam Girdwood 
5659d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5669d58a077SRichard Fitzgerald {
5679d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5689d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5699d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5709d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5719d58a077SRichard Fitzgerald 	 */
5729d58a077SRichard Fitzgerald }
5739d58a077SRichard Fitzgerald 
5746f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
575db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5766f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
577db2a4165SFrank Mandarino {
5786f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5792eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
58088bd870fSBenoit Cousson 	int i, j;
581db2a4165SFrank Mandarino 
582c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
583c5599b87SLars-Peter Clausen 	if (!card->instantiated)
584e3509ff0SDaniel Mack 		return 0;
585e3509ff0SDaniel Mack 
5866ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5876ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5886ed25978SAndy Green 	 */
589f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
590f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
591f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5926ed25978SAndy Green 
5936ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
594f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5956ed25978SAndy Green 
596a00f90f9SMark Brown 	/* mute any active DACs */
597f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5983efab7dcSMark Brown 
599f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6003efab7dcSMark Brown 			continue;
6013efab7dcSMark Brown 
60288bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
60388bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
60488bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
60588bd870fSBenoit Cousson 
606f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
607f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
608db2a4165SFrank Mandarino 		}
60988bd870fSBenoit Cousson 	}
610db2a4165SFrank Mandarino 
6114ccab3e7SLiam Girdwood 	/* suspend all pcms */
612f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
613f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6143efab7dcSMark Brown 			continue;
6153efab7dcSMark Brown 
616f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
6173efab7dcSMark Brown 	}
6184ccab3e7SLiam Girdwood 
61987506549SMark Brown 	if (card->suspend_pre)
62070b2ac12SMark Brown 		card->suspend_pre(card);
621db2a4165SFrank Mandarino 
622f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
623f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6243efab7dcSMark Brown 
625f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6263efab7dcSMark Brown 			continue;
6273efab7dcSMark Brown 
628bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
629f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
630db2a4165SFrank Mandarino 	}
631db2a4165SFrank Mandarino 
63237660b6dSLars-Peter Clausen 	/* close any waiting streams */
63337660b6dSLars-Peter Clausen 	for (i = 0; i < card->num_rtd; i++)
63443829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
635db2a4165SFrank Mandarino 
636f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6373efab7dcSMark Brown 
638f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6393efab7dcSMark Brown 			continue;
6403efab7dcSMark Brown 
6417bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6427bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
643db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
644f0fba2adSLiam Girdwood 
6457bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6467bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
647db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
648db2a4165SFrank Mandarino 	}
649db2a4165SFrank Mandarino 
6508be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6518be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
652e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
653e2d32ff6SMark Brown 
654f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6552eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6561547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6571547aba9SMark Brown 		 * bias _ON and should not be suspended. */
658a8093297SLars-Peter Clausen 		if (!codec->suspended) {
659ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6601547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
661125a25daSMark Brown 				/*
662125a25daSMark Brown 				 * If the CODEC is capable of idle
663125a25daSMark Brown 				 * bias off then being in STANDBY
664125a25daSMark Brown 				 * means it's doing something,
665125a25daSMark Brown 				 * otherwise fall through.
666125a25daSMark Brown 				 */
667125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
668125a25daSMark Brown 					dev_dbg(codec->dev,
66910e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
670125a25daSMark Brown 					break;
671125a25daSMark Brown 				}
672a8093297SLars-Peter Clausen 
6731547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
674a8093297SLars-Peter Clausen 				if (codec->driver->suspend)
67584b315eeSLars-Peter Clausen 					codec->driver->suspend(codec);
676f0fba2adSLiam Girdwood 				codec->suspended = 1;
677e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
678e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
679988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
680988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6811547aba9SMark Brown 				break;
6821547aba9SMark Brown 			default:
68310e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
68410e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6851547aba9SMark Brown 				break;
6861547aba9SMark Brown 			}
6871547aba9SMark Brown 		}
688f0fba2adSLiam Girdwood 	}
689db2a4165SFrank Mandarino 
690f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
691f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6923efab7dcSMark Brown 
693f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6943efab7dcSMark Brown 			continue;
6953efab7dcSMark Brown 
696bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
697f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
698988e8cc4SNicolin Chen 
699988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
700988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
701db2a4165SFrank Mandarino 	}
702db2a4165SFrank Mandarino 
70387506549SMark Brown 	if (card->suspend_post)
70470b2ac12SMark Brown 		card->suspend_post(card);
705db2a4165SFrank Mandarino 
706db2a4165SFrank Mandarino 	return 0;
707db2a4165SFrank Mandarino }
7086f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
709db2a4165SFrank Mandarino 
7106ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7116ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7126ed25978SAndy Green  */
7136ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
714db2a4165SFrank Mandarino {
715f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
716f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7172eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
71888bd870fSBenoit Cousson 	int i, j;
719db2a4165SFrank Mandarino 
7206ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7216ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7226ed25978SAndy Green 	 */
7236ed25978SAndy Green 
724f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7256ed25978SAndy Green 
7269949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
727f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7289949788bSMark Brown 
72987506549SMark Brown 	if (card->resume_pre)
73070b2ac12SMark Brown 		card->resume_pre(card);
731db2a4165SFrank Mandarino 
732bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
733f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
734f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7353efab7dcSMark Brown 
736f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7373efab7dcSMark Brown 			continue;
7383efab7dcSMark Brown 
739bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
740f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
741db2a4165SFrank Mandarino 	}
742db2a4165SFrank Mandarino 
7432eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7441547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7451547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7461547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7471547aba9SMark Brown 		 */
748a8093297SLars-Peter Clausen 		if (codec->suspended) {
749ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7501547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7511547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
752a8093297SLars-Peter Clausen 				if (codec->driver->resume)
753f0fba2adSLiam Girdwood 					codec->driver->resume(codec);
754f0fba2adSLiam Girdwood 				codec->suspended = 0;
7551547aba9SMark Brown 				break;
7561547aba9SMark Brown 			default:
75710e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
75810e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7591547aba9SMark Brown 				break;
7601547aba9SMark Brown 			}
7611547aba9SMark Brown 		}
762f0fba2adSLiam Girdwood 	}
763db2a4165SFrank Mandarino 
764f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7653efab7dcSMark Brown 
766f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7673efab7dcSMark Brown 			continue;
7683efab7dcSMark Brown 
7697bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
770d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
771db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
772f0fba2adSLiam Girdwood 
7737bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
774d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
775db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
776db2a4165SFrank Mandarino 	}
777db2a4165SFrank Mandarino 
7783ff3f64bSMark Brown 	/* unmute any active DACs */
779f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7803efab7dcSMark Brown 
781f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7823efab7dcSMark Brown 			continue;
7833efab7dcSMark Brown 
78488bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
78588bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
78688bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
78788bd870fSBenoit Cousson 
788f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
789f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
790db2a4165SFrank Mandarino 		}
79188bd870fSBenoit Cousson 	}
792db2a4165SFrank Mandarino 
793f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
794f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7953efab7dcSMark Brown 
796f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7973efab7dcSMark Brown 			continue;
7983efab7dcSMark Brown 
799bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
800f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
801db2a4165SFrank Mandarino 	}
802db2a4165SFrank Mandarino 
80387506549SMark Brown 	if (card->resume_post)
80470b2ac12SMark Brown 		card->resume_post(card);
805db2a4165SFrank Mandarino 
806f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8076ed25978SAndy Green 
8086ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
809f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
810e2d32ff6SMark Brown 
8118be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8128be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
813e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8146ed25978SAndy Green }
8156ed25978SAndy Green 
8166ed25978SAndy Green /* powers up audio subsystem after a suspend */
8176f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8186ed25978SAndy Green {
8196f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
820bc263214SLars-Peter Clausen 	bool bus_control = false;
821bc263214SLars-Peter Clausen 	int i;
822b9dd94a8SPeter Ujfalusi 
823c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
824c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8255ff1ddf2SEric Miao 		return 0;
8265ff1ddf2SEric Miao 
827988e8cc4SNicolin Chen 	/* activate pins from sleep state */
828988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
82988bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
83088bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
83188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
83288bd870fSBenoit Cousson 		int j;
83388bd870fSBenoit Cousson 
834988e8cc4SNicolin Chen 		if (cpu_dai->active)
835988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
83688bd870fSBenoit Cousson 
83788bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
83888bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
839988e8cc4SNicolin Chen 			if (codec_dai->active)
840988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
841988e8cc4SNicolin Chen 		}
84288bd870fSBenoit Cousson 	}
843988e8cc4SNicolin Chen 
844bc263214SLars-Peter Clausen 	/*
845bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
846bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
847bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
84864ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
84964ab9baaSMark Brown 	 */
850f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
851f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
852bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
85382e14e8bSStephen Warren 	}
854bc263214SLars-Peter Clausen 	if (bus_control) {
855bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
85664ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
85764ab9baaSMark Brown 	} else {
858f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8596308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
860f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
861f0fba2adSLiam Girdwood 	}
8626ed25978SAndy Green 
863db2a4165SFrank Mandarino 	return 0;
864db2a4165SFrank Mandarino }
8656f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
866db2a4165SFrank Mandarino #else
8676f8ab4acSMark Brown #define snd_soc_suspend NULL
8686f8ab4acSMark Brown #define snd_soc_resume NULL
869db2a4165SFrank Mandarino #endif
870db2a4165SFrank Mandarino 
87185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
87202a06d30SBarry Song };
87302a06d30SBarry Song 
87465d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
87565d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
87612023a9aSMisael Lopez Cruz {
87765d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
87812023a9aSMisael Lopez Cruz 
87934e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
88034e81ab4SLars-Peter Clausen 
88165d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
88265d9361fSLars-Peter Clausen 		if (of_node) {
88365d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
88465d9361fSLars-Peter Clausen 				return component;
88565d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
88665d9361fSLars-Peter Clausen 			return component;
88712023a9aSMisael Lopez Cruz 		}
88812023a9aSMisael Lopez Cruz 	}
88912023a9aSMisael Lopez Cruz 
89012023a9aSMisael Lopez Cruz 	return NULL;
89112023a9aSMisael Lopez Cruz }
89212023a9aSMisael Lopez Cruz 
89314621c7eSLars-Peter Clausen static struct snd_soc_dai *snd_soc_find_dai(
89414621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
89512023a9aSMisael Lopez Cruz {
89614621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
89714621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
898*3e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
89912023a9aSMisael Lopez Cruz 
90034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
90134e81ab4SLars-Peter Clausen 
90214621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
90314621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
904*3e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
905*3e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
906*3e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
907*3e0aa8d8SJyri Sarha 
908*3e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
90912023a9aSMisael Lopez Cruz 			continue;
9101ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
91112023a9aSMisael Lopez Cruz 			continue;
91214621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
91314621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
91414621c7eSLars-Peter Clausen 				continue;
91512023a9aSMisael Lopez Cruz 
91614621c7eSLars-Peter Clausen 			return dai;
91712023a9aSMisael Lopez Cruz 		}
91812023a9aSMisael Lopez Cruz 	}
91912023a9aSMisael Lopez Cruz 
92012023a9aSMisael Lopez Cruz 	return NULL;
92112023a9aSMisael Lopez Cruz }
92212023a9aSMisael Lopez Cruz 
923f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
924db2a4165SFrank Mandarino {
925f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
926f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
92788bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
92814621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
92988bd870fSBenoit Cousson 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
930435c5e25SMark Brown 	struct snd_soc_platform *platform;
931848dd8beSMark Brown 	const char *platform_name;
93288bd870fSBenoit Cousson 	int i;
933db2a4165SFrank Mandarino 
934f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
9356308419aSMark Brown 
93614621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
93714621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
93814621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
93914621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
940b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
941f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
942f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
943b19e6e7bSMark Brown 		return -EPROBE_DEFER;
944c5af3a2eSMark Brown 	}
945c5af3a2eSMark Brown 
94688bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
94788bd870fSBenoit Cousson 
94888bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
94988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
95014621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
95188bd870fSBenoit Cousson 		if (!codec_dais[i]) {
95212023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
95388bd870fSBenoit Cousson 				codecs[i].dai_name);
95412023a9aSMisael Lopez Cruz 			return -EPROBE_DEFER;
95512023a9aSMisael Lopez Cruz 		}
95688bd870fSBenoit Cousson 	}
95788bd870fSBenoit Cousson 
95888bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
95988bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
96088bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
96112023a9aSMisael Lopez Cruz 
962848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
963848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9645a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
965848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
966848dd8beSMark Brown 
967b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
968f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9695a504963SStephen Warren 		if (dai_link->platform_of_node) {
9705a504963SStephen Warren 			if (platform->dev->of_node !=
9715a504963SStephen Warren 			    dai_link->platform_of_node)
9725a504963SStephen Warren 				continue;
9735a504963SStephen Warren 		} else {
974f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
9752610ab77SStephen Warren 				continue;
9765a504963SStephen Warren 		}
9772610ab77SStephen Warren 
978f0fba2adSLiam Girdwood 		rtd->platform = platform;
979f0fba2adSLiam Girdwood 	}
980b19e6e7bSMark Brown 	if (!rtd->platform) {
981f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
982f0fba2adSLiam Girdwood 			dai_link->platform_name);
983b19e6e7bSMark Brown 		return -EPROBE_DEFER;
984f0fba2adSLiam Girdwood 	}
985b19e6e7bSMark Brown 
986b19e6e7bSMark Brown 	card->num_rtd++;
987b19e6e7bSMark Brown 
988b19e6e7bSMark Brown 	return 0;
9896b05eda6SMark Brown }
9906b05eda6SMark Brown 
991f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
992d12cd198SStephen Warren {
99370090bbbSLars-Peter Clausen 	if (!component->probed)
99470090bbbSLars-Peter Clausen 		return;
995d12cd198SStephen Warren 
996f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
997f1d45cc3SLars-Peter Clausen 	if (component->codec)
998f1d45cc3SLars-Peter Clausen 		list_del(&component->codec->card_list);
999d12cd198SStephen Warren 
1000f1d45cc3SLars-Peter Clausen 	if (component->remove)
1001f1d45cc3SLars-Peter Clausen 		component->remove(component);
1002d12cd198SStephen Warren 
1003f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1004d12cd198SStephen Warren 
1005f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1006f1d45cc3SLars-Peter Clausen 	component->probed = 0;
1007f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1008d12cd198SStephen Warren }
1009d12cd198SStephen Warren 
1010e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1011589c3563SJarkko Nikula {
1012589c3563SJarkko Nikula 	int err;
1013589c3563SJarkko Nikula 
1014e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1015e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1016e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1017e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1018589c3563SJarkko Nikula 			if (err < 0)
1019e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1020b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1021e60cd14fSLars-Peter Clausen 					dai->name, err);
1022b0aa88afSMisael Lopez Cruz 		}
1023e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1024b0aa88afSMisael Lopez Cruz 	}
1025b0aa88afSMisael Lopez Cruz }
1026b0aa88afSMisael Lopez Cruz 
102762ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1028f0fba2adSLiam Girdwood {
1029f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1030e60cd14fSLars-Peter Clausen 	int i;
1031f0fba2adSLiam Girdwood 
1032f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1033f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
103436ae1a96SMark Brown 		device_unregister(rtd->dev);
1035f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
103602a06d30SBarry Song 	}
103702a06d30SBarry Song 
1038f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
103988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1040e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1041f0fba2adSLiam Girdwood 
1042e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1043f0fba2adSLiam Girdwood }
1044f0fba2adSLiam Girdwood 
104562ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
104662ae68faSStephen Warren 				       int order)
104762ae68faSStephen Warren {
104862ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
104962ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105062ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
105161aca564SLars-Peter Clausen 	struct snd_soc_component *component;
105288bd870fSBenoit Cousson 	int i;
105362ae68faSStephen Warren 
105462ae68faSStephen Warren 	/* remove the platform */
105570090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1056f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
105762ae68faSStephen Warren 
105862ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
105988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
106061aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
106170090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
106261aca564SLars-Peter Clausen 			soc_remove_component(component);
106362ae68faSStephen Warren 	}
106462ae68faSStephen Warren 
106562ae68faSStephen Warren 	/* remove any CPU-side CODEC */
106662ae68faSStephen Warren 	if (cpu_dai) {
106770090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
106861aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
106962ae68faSStephen Warren 	}
107062ae68faSStephen Warren }
107162ae68faSStephen Warren 
10720671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10730671fd8eSKuninori Morimoto {
10740168bf0dSLiam Girdwood 	int dai, order;
10750671fd8eSKuninori Morimoto 
10760168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10770168bf0dSLiam Girdwood 			order++) {
10780168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
107962ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10800168bf0dSLiam Girdwood 	}
108162ae68faSStephen Warren 
108262ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
108362ae68faSStephen Warren 			order++) {
108462ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
108562ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
108662ae68faSStephen Warren 	}
108762ae68faSStephen Warren 
10880671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10890671fd8eSKuninori Morimoto }
10900671fd8eSKuninori Morimoto 
1091ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
109294f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1093ead9b919SJarkko Nikula {
1094ead9b919SJarkko Nikula 	int i;
1095ead9b919SJarkko Nikula 
1096ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1097ead9b919SJarkko Nikula 		return;
1098ead9b919SJarkko Nikula 
1099ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1100ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
110194f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
11023ca041edSSebastian Reichel 			continue;
110394f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
11043ca041edSSebastian Reichel 			continue;
110594f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1106ead9b919SJarkko Nikula 		break;
1107ead9b919SJarkko Nikula 	}
1108ead9b919SJarkko Nikula }
1109ead9b919SJarkko Nikula 
1110f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1111f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1112589c3563SJarkko Nikula {
1113f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1114888df395SMark Brown 	struct snd_soc_dai *dai;
1115f1d45cc3SLars-Peter Clausen 	int ret;
1116589c3563SJarkko Nikula 
111770090bbbSLars-Peter Clausen 	if (component->probed)
111870090bbbSLars-Peter Clausen 		return 0;
1119589c3563SJarkko Nikula 
1120f1d45cc3SLars-Peter Clausen 	component->card = card;
1121f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1122f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1123589c3563SJarkko Nikula 
1124f1d45cc3SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
112570d29331SJarkko Nikula 		return -ENODEV;
112670d29331SJarkko Nikula 
1127f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1128d5d1e0beSLars-Peter Clausen 
1129f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1130f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1131f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
113277530150SLars-Peter Clausen 
1133b318ad50SNariman Poushin 		if (ret != 0) {
1134f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1135b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1136b318ad50SNariman Poushin 			goto err_probe;
1137b318ad50SNariman Poushin 		}
1138b318ad50SNariman Poushin 	}
1139b318ad50SNariman Poushin 
11400634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
11410634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1142261edc70SNariman Poushin 		if (ret != 0) {
1143f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1144261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1145261edc70SNariman Poushin 			goto err_probe;
1146261edc70SNariman Poushin 		}
1147261edc70SNariman Poushin 	}
1148888df395SMark Brown 
1149f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1150f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1151589c3563SJarkko Nikula 		if (ret < 0) {
1152f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1153f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
115470d29331SJarkko Nikula 			goto err_probe;
1155589c3563SJarkko Nikula 		}
1156cb2cf612SLiam Girdwood 
1157f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1158f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1159956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1160f1d45cc3SLars-Peter Clausen 			component->name);
1161956245e9SLiam Girdwood 	}
1162956245e9SLiam Girdwood 
1163f1d45cc3SLars-Peter Clausen 	if (component->controls)
1164f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1165f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1166f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1167f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1168f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1169956245e9SLiam Girdwood 
1170f1d45cc3SLars-Peter Clausen 	component->probed = 1;
1171f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1172f1d45cc3SLars-Peter Clausen 
1173f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1174f1d45cc3SLars-Peter Clausen 	if (component->codec)
1175f1d45cc3SLars-Peter Clausen 		list_add(&component->codec->card_list, &card->codec_dev_list);
1176956245e9SLiam Girdwood 
1177956245e9SLiam Girdwood 	return 0;
1178956245e9SLiam Girdwood 
1179956245e9SLiam Girdwood err_probe:
1180f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1181f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1182956245e9SLiam Girdwood 
1183956245e9SLiam Girdwood 	return ret;
1184956245e9SLiam Girdwood }
1185956245e9SLiam Girdwood 
118636ae1a96SMark Brown static void rtd_release(struct device *dev)
118736ae1a96SMark Brown {
118836ae1a96SMark Brown 	kfree(dev);
118936ae1a96SMark Brown }
1190f0fba2adSLiam Girdwood 
11915f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
11925f3484acSLars-Peter Clausen 	const char *name)
1193503ae5e0SMisael Lopez Cruz {
1194589c3563SJarkko Nikula 	int ret = 0;
1195589c3563SJarkko Nikula 
1196589c3563SJarkko Nikula 	/* register the rtd device */
119736ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
119836ae1a96SMark Brown 	if (!rtd->dev)
119936ae1a96SMark Brown 		return -ENOMEM;
120036ae1a96SMark Brown 	device_initialize(rtd->dev);
12015f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
120236ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1203d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1204f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
120536ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1206b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
120701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
120801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
120901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
121001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
121136ae1a96SMark Brown 	ret = device_add(rtd->dev);
1212589c3563SJarkko Nikula 	if (ret < 0) {
1213865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1214865df9cbSChuansheng Liu 		put_device(rtd->dev);
12155f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1216f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1217589c3563SJarkko Nikula 		return ret;
1218589c3563SJarkko Nikula 	}
1219589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1220589c3563SJarkko Nikula 	return 0;
1221589c3563SJarkko Nikula }
1222589c3563SJarkko Nikula 
122362ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
122462ae68faSStephen Warren 				     int order)
122562ae68faSStephen Warren {
122662ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
122762ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1228f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
122988bd870fSBenoit Cousson 	int i, ret;
123062ae68faSStephen Warren 
123162ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
123261aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
123370090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1234f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
123562ae68faSStephen Warren 		if (ret < 0)
123662ae68faSStephen Warren 			return ret;
123762ae68faSStephen Warren 	}
123862ae68faSStephen Warren 
123988bd870fSBenoit Cousson 	/* probe the CODEC-side components */
124088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
124161aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
124270090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1243f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
124462ae68faSStephen Warren 			if (ret < 0)
124562ae68faSStephen Warren 				return ret;
124662ae68faSStephen Warren 		}
124788bd870fSBenoit Cousson 	}
124862ae68faSStephen Warren 
124962ae68faSStephen Warren 	/* probe the platform */
125070090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1251f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
125262ae68faSStephen Warren 		if (ret < 0)
125362ae68faSStephen Warren 			return ret;
125462ae68faSStephen Warren 	}
125562ae68faSStephen Warren 
125662ae68faSStephen Warren 	return 0;
125762ae68faSStephen Warren }
125862ae68faSStephen Warren 
12598e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1260b0aa88afSMisael Lopez Cruz {
1261b0aa88afSMisael Lopez Cruz 	int ret;
1262b0aa88afSMisael Lopez Cruz 
12638e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
12648e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
12658e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1266b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
12678e2be562SLars-Peter Clausen 				dev_err(dai->dev,
12688e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
12698e2be562SLars-Peter Clausen 					dai->name, ret);
1270b0aa88afSMisael Lopez Cruz 				return ret;
1271b0aa88afSMisael Lopez Cruz 			}
1272b0aa88afSMisael Lopez Cruz 		}
1273b0aa88afSMisael Lopez Cruz 
12748e2be562SLars-Peter Clausen 		dai->probed = 1;
1275b0aa88afSMisael Lopez Cruz 	}
1276b0aa88afSMisael Lopez Cruz 
1277b0aa88afSMisael Lopez Cruz 	return 0;
1278b0aa88afSMisael Lopez Cruz }
1279b0aa88afSMisael Lopez Cruz 
12802436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
12812436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
12823f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
12832436a723SMisael Lopez Cruz {
12843f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
12853f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
12862436a723SMisael Lopez Cruz 	struct snd_soc_dapm_widget *play_w, *capture_w;
12872436a723SMisael Lopez Cruz 	int ret;
12882436a723SMisael Lopez Cruz 
128988bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
129088bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
129188bd870fSBenoit Cousson 
12922436a723SMisael Lopez Cruz 	/* link the DAI widgets */
12932436a723SMisael Lopez Cruz 	play_w = codec_dai->playback_widget;
12942436a723SMisael Lopez Cruz 	capture_w = cpu_dai->capture_widget;
12952436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
12962436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1297c6615082SNikesh Oswal 					   dai_link->num_params, capture_w,
1298c6615082SNikesh Oswal 					   play_w);
12992436a723SMisael Lopez Cruz 		if (ret != 0) {
13002436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
13012436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
13022436a723SMisael Lopez Cruz 			return ret;
13032436a723SMisael Lopez Cruz 		}
13042436a723SMisael Lopez Cruz 	}
13052436a723SMisael Lopez Cruz 
13062436a723SMisael Lopez Cruz 	play_w = cpu_dai->playback_widget;
13072436a723SMisael Lopez Cruz 	capture_w = codec_dai->capture_widget;
13082436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
13092436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1310c6615082SNikesh Oswal 					   dai_link->num_params, capture_w,
1311c6615082SNikesh Oswal 					   play_w);
13122436a723SMisael Lopez Cruz 		if (ret != 0) {
13132436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
13142436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
13152436a723SMisael Lopez Cruz 			return ret;
13162436a723SMisael Lopez Cruz 		}
13172436a723SMisael Lopez Cruz 	}
13182436a723SMisael Lopez Cruz 
13192436a723SMisael Lopez Cruz 	return 0;
13202436a723SMisael Lopez Cruz }
13212436a723SMisael Lopez Cruz 
132262ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1323f0fba2adSLiam Girdwood {
1324f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1325f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1326c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
132788bd870fSBenoit Cousson 	int i, ret;
1328f0fba2adSLiam Girdwood 
1329f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
13300168bf0dSLiam Girdwood 			card->name, num, order);
1331f0fba2adSLiam Girdwood 
1332f0fba2adSLiam Girdwood 	/* set default power off timeout */
1333f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1334f0fba2adSLiam Girdwood 
13358e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
13368e2be562SLars-Peter Clausen 	if (ret)
1337f0fba2adSLiam Girdwood 		return ret;
1338f0fba2adSLiam Girdwood 
1339f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
134088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
13418e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1342b0aa88afSMisael Lopez Cruz 		if (ret)
1343f0fba2adSLiam Girdwood 			return ret;
134488bd870fSBenoit Cousson 	}
1345f0fba2adSLiam Girdwood 
13460168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
13470168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
13480168bf0dSLiam Girdwood 		return 0;
13490168bf0dSLiam Girdwood 
13505f3484acSLars-Peter Clausen 	/* do machine specific initialization */
13515f3484acSLars-Peter Clausen 	if (dai_link->init) {
13525f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
13535f3484acSLars-Peter Clausen 		if (ret < 0) {
13545f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
13555f3484acSLars-Peter Clausen 				dai_link->name, ret);
13565f3484acSLars-Peter Clausen 			return ret;
13575f3484acSLars-Peter Clausen 		}
13585f3484acSLars-Peter Clausen 	}
13595f3484acSLars-Peter Clausen 
1360a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1361a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1362a5053a8eSKuninori Morimoto 
13635f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1364589c3563SJarkko Nikula 	if (ret)
1365f0fba2adSLiam Girdwood 		return ret;
1366f0fba2adSLiam Girdwood 
13675f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
13685f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
13692e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
13702e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
13715f3484acSLars-Peter Clausen #endif
13725f3484acSLars-Peter Clausen 
13731245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
13741245b700SNamarta Kohli 		/*create compress_device"*/
13751245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
13761245b700SNamarta Kohli 		if (ret < 0) {
1377f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
13781245b700SNamarta Kohli 					 dai_link->stream_name);
13791245b700SNamarta Kohli 			return ret;
13801245b700SNamarta Kohli 		}
13811245b700SNamarta Kohli 	} else {
13821245b700SNamarta Kohli 
1383c74184edSMark Brown 		if (!dai_link->params) {
1384f0fba2adSLiam Girdwood 			/* create the pcm */
1385f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1386f0fba2adSLiam Girdwood 			if (ret < 0) {
1387f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
13880837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1389f0fba2adSLiam Girdwood 				return ret;
1390f0fba2adSLiam Girdwood 			}
1391c74184edSMark Brown 		} else {
13929d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
13939d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
13949d58a077SRichard Fitzgerald 
1395c74184edSMark Brown 			/* link the DAI widgets */
13963f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
13972436a723SMisael Lopez Cruz 			if (ret)
1398c74184edSMark Brown 				return ret;
1399c74184edSMark Brown 		}
1400c74184edSMark Brown 	}
1401c74184edSMark Brown 
1402f0fba2adSLiam Girdwood 	return 0;
1403f0fba2adSLiam Girdwood }
1404f0fba2adSLiam Girdwood 
140544c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1406b19e6e7bSMark Brown {
140744c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14083ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
140965d9361fSLars-Peter Clausen 	const char *name = aux_dev->codec_name;
14103ca041edSSebastian Reichel 
141165d9361fSLars-Peter Clausen 	rtd->component = soc_find_component(aux_dev->codec_of_node, name);
141265d9361fSLars-Peter Clausen 	if (!rtd->component) {
14133ca041edSSebastian Reichel 		if (aux_dev->codec_of_node)
141465d9361fSLars-Peter Clausen 			name = of_node_full_name(aux_dev->codec_of_node);
14153ca041edSSebastian Reichel 
141665d9361fSLars-Peter Clausen 		dev_err(card->dev, "ASoC: %s not registered\n", name);
1417b19e6e7bSMark Brown 		return -EPROBE_DEFER;
1418b19e6e7bSMark Brown 	}
1419b19e6e7bSMark Brown 
142065d9361fSLars-Peter Clausen 	/*
142165d9361fSLars-Peter Clausen 	 * Some places still reference rtd->codec, so we have to keep that
142265d9361fSLars-Peter Clausen 	 * initialized if the component is a CODEC. Once all those references
142365d9361fSLars-Peter Clausen 	 * have been removed, this code can be removed as well.
142465d9361fSLars-Peter Clausen 	 */
142565d9361fSLars-Peter Clausen 	 rtd->codec = rtd->component->codec;
142665d9361fSLars-Peter Clausen 
142744c69bb1SLars-Peter Clausen 	return 0;
14283ca041edSSebastian Reichel }
14292eea392dSJarkko Nikula 
143044c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
143144c69bb1SLars-Peter Clausen {
143244c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14335f3484acSLars-Peter Clausen 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
143444c69bb1SLars-Peter Clausen 	int ret;
143544c69bb1SLars-Peter Clausen 
143665d9361fSLars-Peter Clausen 	ret = soc_probe_component(card, rtd->component);
14372eea392dSJarkko Nikula 	if (ret < 0)
1438589c3563SJarkko Nikula 		return ret;
14392eea392dSJarkko Nikula 
14405f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14415f3484acSLars-Peter Clausen 	if (aux_dev->init) {
144257bf7726SLars-Peter Clausen 		ret = aux_dev->init(rtd->component);
14435f3484acSLars-Peter Clausen 		if (ret < 0) {
14445f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14455f3484acSLars-Peter Clausen 				aux_dev->name, ret);
14462eea392dSJarkko Nikula 			return ret;
14472eea392dSJarkko Nikula 		}
14485f3484acSLars-Peter Clausen 	}
14495f3484acSLars-Peter Clausen 
14505f3484acSLars-Peter Clausen 	return soc_post_component_init(rtd, aux_dev->name);
14512eea392dSJarkko Nikula }
14522eea392dSJarkko Nikula 
14532eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
14542eea392dSJarkko Nikula {
14552eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
145665d9361fSLars-Peter Clausen 	struct snd_soc_component *component = rtd->component;
14572eea392dSJarkko Nikula 
14582eea392dSJarkko Nikula 	/* unregister the rtd device */
14592eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1460d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
14612eea392dSJarkko Nikula 		rtd->dev_registered = 0;
14622eea392dSJarkko Nikula 	}
14632eea392dSJarkko Nikula 
146465d9361fSLars-Peter Clausen 	if (component && component->probed)
146565d9361fSLars-Peter Clausen 		soc_remove_component(component);
14662eea392dSJarkko Nikula }
14672eea392dSJarkko Nikula 
1468f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1469fdf0f54dSDimitris Papastamos {
1470fdf0f54dSDimitris Papastamos 	int ret;
1471fdf0f54dSDimitris Papastamos 
1472fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1473fdf0f54dSDimitris Papastamos 		return 0;
1474fdf0f54dSDimitris Papastamos 
1475fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1476fdf0f54dSDimitris Papastamos 	if (ret < 0) {
147710e8aa9aSMichał Mirosław 		dev_err(codec->dev,
147810e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
147910e8aa9aSMichał Mirosław 			ret);
1480fdf0f54dSDimitris Papastamos 		return ret;
1481fdf0f54dSDimitris Papastamos 	}
1482fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1483fdf0f54dSDimitris Papastamos 	return 0;
1484fdf0f54dSDimitris Papastamos }
1485fdf0f54dSDimitris Papastamos 
1486ce64c8b9SLars-Peter Clausen /**
1487ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1488ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1489ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1490ce64c8b9SLars-Peter Clausen  *
1491ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1492ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1493ce64c8b9SLars-Peter Clausen  *
1494ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1495ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1496ce64c8b9SLars-Peter Clausen  *
1497ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1498ce64c8b9SLars-Peter Clausen  */
1499ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1500ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1501ce64c8b9SLars-Peter Clausen {
1502ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1503ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1504ce64c8b9SLars-Peter Clausen 	unsigned int i;
1505ce64c8b9SLars-Peter Clausen 	int ret;
1506ce64c8b9SLars-Peter Clausen 
1507ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1508ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1509ce64c8b9SLars-Peter Clausen 
1510ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1511ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1512ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1513ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1514ce64c8b9SLars-Peter Clausen 			return ret;
1515ce64c8b9SLars-Peter Clausen 		}
1516ce64c8b9SLars-Peter Clausen 	}
1517ce64c8b9SLars-Peter Clausen 
1518ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1519ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1520ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1521ce64c8b9SLars-Peter Clausen 
1522ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1523ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1524ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1525ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1526ce64c8b9SLars-Peter Clausen 			break;
1527ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1528ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1529ce64c8b9SLars-Peter Clausen 			break;
1530ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1531ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1532ce64c8b9SLars-Peter Clausen 			break;
1533ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1534ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1535ce64c8b9SLars-Peter Clausen 			break;
1536ce64c8b9SLars-Peter Clausen 		}
1537ce64c8b9SLars-Peter Clausen 
1538ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1539ce64c8b9SLars-Peter Clausen 	}
1540ce64c8b9SLars-Peter Clausen 
1541ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1542ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1543ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1544ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1545ce64c8b9SLars-Peter Clausen 		return ret;
1546ce64c8b9SLars-Peter Clausen 	}
1547ce64c8b9SLars-Peter Clausen 
1548ce64c8b9SLars-Peter Clausen 	return 0;
1549ce64c8b9SLars-Peter Clausen }
1550ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1551ce64c8b9SLars-Peter Clausen 
1552b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1553f0fba2adSLiam Girdwood {
1554fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1555ce64c8b9SLars-Peter Clausen 	int ret, i, order;
155696dd3622SMark Brown 
155734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
155801b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1559f0fba2adSLiam Girdwood 
1560b19e6e7bSMark Brown 	/* bind DAIs */
1561b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1562b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1563b19e6e7bSMark Brown 		if (ret != 0)
1564b19e6e7bSMark Brown 			goto base_error;
1565db2a4165SFrank Mandarino 	}
1566db2a4165SFrank Mandarino 
156744c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1568b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
156944c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1570b19e6e7bSMark Brown 		if (ret != 0)
1571b19e6e7bSMark Brown 			goto base_error;
1572db2a4165SFrank Mandarino 	}
1573db2a4165SFrank Mandarino 
1574fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1575fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1576fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1577fdf0f54dSDimitris Papastamos 			continue;
1578f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1579b19e6e7bSMark Brown 		if (ret < 0)
1580b19e6e7bSMark Brown 			goto base_error;
1581fdf0f54dSDimitris Papastamos 	}
1582fdf0f54dSDimitris Papastamos 
1583f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1584102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1585f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1586f0fba2adSLiam Girdwood 	if (ret < 0) {
158710e8aa9aSMichał Mirosław 		dev_err(card->dev,
158810e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
158910e8aa9aSMichał Mirosław 			card->name, ret);
1590b19e6e7bSMark Brown 		goto base_error;
1591db2a4165SFrank Mandarino 	}
1592db2a4165SFrank Mandarino 
15930757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
15940757d834SLars-Peter Clausen 
1595e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1596e37a4970SMark Brown 	card->dapm.dev = card->dev;
1597e37a4970SMark Brown 	card->dapm.card = card;
1598e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1599e37a4970SMark Brown 
1600d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1601d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1602d5d1e0beSLars-Peter Clausen #endif
1603d5d1e0beSLars-Peter Clausen 
160488ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
16056ed25978SAndy Green 	/* deferred resume work */
16066308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16071301a964SRandy Dunlap #endif
16086ed25978SAndy Green 
16099a841ebbSMark Brown 	if (card->dapm_widgets)
16109a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
16119a841ebbSMark Brown 					  card->num_dapm_widgets);
16129a841ebbSMark Brown 
1613f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
1614f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1615f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
1616f23e860eSNicolin Chen 
1617f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1618f0fba2adSLiam Girdwood 	if (card->probe) {
1619e7361ec4SMark Brown 		ret = card->probe(card);
1620f0fba2adSLiam Girdwood 		if (ret < 0)
1621f0fba2adSLiam Girdwood 			goto card_probe_error;
1622f0fba2adSLiam Girdwood 	}
1623f0fba2adSLiam Girdwood 
162462ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
16250168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
16260168bf0dSLiam Girdwood 			order++) {
1627fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
162862ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
162962ae68faSStephen Warren 			if (ret < 0) {
1630f110bfc7SLiam Girdwood 				dev_err(card->dev,
1631f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1632f110bfc7SLiam Girdwood 					ret);
163362ae68faSStephen Warren 				goto probe_dai_err;
163462ae68faSStephen Warren 			}
163562ae68faSStephen Warren 		}
163662ae68faSStephen Warren 	}
163762ae68faSStephen Warren 
163862ae68faSStephen Warren 	/* probe all DAI links on this card */
163962ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
164062ae68faSStephen Warren 			order++) {
164162ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
164262ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1643fe3e78e0SMark Brown 			if (ret < 0) {
1644f110bfc7SLiam Girdwood 				dev_err(card->dev,
1645f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1646f110bfc7SLiam Girdwood 					ret);
1647f0fba2adSLiam Girdwood 				goto probe_dai_err;
1648fe3e78e0SMark Brown 			}
1649fe3e78e0SMark Brown 		}
16500168bf0dSLiam Girdwood 	}
1651fe3e78e0SMark Brown 
16522eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
16532eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
16542eea392dSJarkko Nikula 		if (ret < 0) {
1655f110bfc7SLiam Girdwood 			dev_err(card->dev,
1656f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1657f110bfc7SLiam Girdwood 				ret);
16582eea392dSJarkko Nikula 			goto probe_aux_dev_err;
16592eea392dSJarkko Nikula 		}
16602eea392dSJarkko Nikula 	}
16612eea392dSJarkko Nikula 
1662888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1663b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1664888df395SMark Brown 
1665b7af1dafSMark Brown 	if (card->controls)
1666022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1667b7af1dafSMark Brown 
1668b8ad29deSMark Brown 	if (card->dapm_routes)
1669b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1670b8ad29deSMark Brown 					card->num_dapm_routes);
1671b8ad29deSMark Brown 
1672f23e860eSNicolin Chen 	if (card->of_dapm_routes)
1673f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1674f23e860eSNicolin Chen 					card->num_of_dapm_routes);
167575d9ac46SMark Brown 
1676f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1677fe3e78e0SMark Brown 		 "%s", card->name);
1678f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
167922de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1680f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1681f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1682f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1683f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1684f0e8ed85SMark Brown 		case '_':
1685f0e8ed85SMark Brown 		case '-':
1686f0e8ed85SMark Brown 		case '\0':
1687f0e8ed85SMark Brown 			break;
1688f0e8ed85SMark Brown 		default:
1689f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1690f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1691f0e8ed85SMark Brown 			break;
1692f0e8ed85SMark Brown 		}
1693f0e8ed85SMark Brown 	}
1694fe3e78e0SMark Brown 
169528e9ad92SMark Brown 	if (card->late_probe) {
169628e9ad92SMark Brown 		ret = card->late_probe(card);
169728e9ad92SMark Brown 		if (ret < 0) {
1698f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
169928e9ad92SMark Brown 				card->name, ret);
170028e9ad92SMark Brown 			goto probe_aux_dev_err;
170128e9ad92SMark Brown 		}
170228e9ad92SMark Brown 	}
170328e9ad92SMark Brown 
1704824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
17058c193b8dSLars-Peter Clausen 
1706f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1707fe3e78e0SMark Brown 	if (ret < 0) {
1708f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1709f110bfc7SLiam Girdwood 				ret);
17106b3ed785SAxel Lin 		goto probe_aux_dev_err;
1711fe3e78e0SMark Brown 	}
1712fe3e78e0SMark Brown 
1713435c5e25SMark Brown 	card->instantiated = 1;
17144f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1715f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
171634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
1717b19e6e7bSMark Brown 
1718b19e6e7bSMark Brown 	return 0;
1719db2a4165SFrank Mandarino 
17202eea392dSJarkko Nikula probe_aux_dev_err:
17212eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17222eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17232eea392dSJarkko Nikula 
1724f0fba2adSLiam Girdwood probe_dai_err:
17250671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1726fe3e78e0SMark Brown 
1727f0fba2adSLiam Girdwood card_probe_error:
172887506549SMark Brown 	if (card->remove)
1729e7361ec4SMark Brown 		card->remove(card);
1730f0fba2adSLiam Girdwood 
17310757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
1732f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1733f0fba2adSLiam Girdwood 
1734b19e6e7bSMark Brown base_error:
1735f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
173634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
1737db2a4165SFrank Mandarino 
1738b19e6e7bSMark Brown 	return ret;
1739435c5e25SMark Brown }
1740435c5e25SMark Brown 
1741435c5e25SMark Brown /* probes a new socdev */
1742435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1743435c5e25SMark Brown {
1744f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1745435c5e25SMark Brown 
174670a7ca34SVinod Koul 	/*
174770a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
174870a7ca34SVinod Koul 	 * we should not be here in that case so ret error
174970a7ca34SVinod Koul 	 */
175070a7ca34SVinod Koul 	if (!card)
175170a7ca34SVinod Koul 		return -EINVAL;
175270a7ca34SVinod Koul 
1753fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1754f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1755fe4085e8SMark Brown 		 card->name);
1756fe4085e8SMark Brown 
1757435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1758435c5e25SMark Brown 	card->dev = &pdev->dev;
1759f0fba2adSLiam Girdwood 
176028d528c8SMark Brown 	return snd_soc_register_card(card);
1761435c5e25SMark Brown }
1762435c5e25SMark Brown 
1763b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1764db2a4165SFrank Mandarino {
1765db2a4165SFrank Mandarino 	int i;
1766db2a4165SFrank Mandarino 
1767f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1768f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1769f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
177043829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1771db2a4165SFrank Mandarino 	}
1772db2a4165SFrank Mandarino 
17732eea392dSJarkko Nikula 	/* remove auxiliary devices */
17742eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17752eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17762eea392dSJarkko Nikula 
1777f0fba2adSLiam Girdwood 	/* remove and free each DAI */
17780671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1779f0fba2adSLiam Girdwood 
1780a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1781a6052154SJarkko Nikula 
1782f0fba2adSLiam Girdwood 	/* remove the card */
178387506549SMark Brown 	if (card->remove)
1784e7361ec4SMark Brown 		card->remove(card);
1785f0fba2adSLiam Girdwood 
17860aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
17870aaae527SLars-Peter Clausen 
1788f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1789b0e26485SVinod Koul 	return 0;
1790b0e26485SVinod Koul 
1791b2dfa62cSGuennadi Liakhovetski }
1792b0e26485SVinod Koul 
1793b0e26485SVinod Koul /* removes a socdev */
1794b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1795b0e26485SVinod Koul {
1796b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1797b0e26485SVinod Koul 
1798c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1799db2a4165SFrank Mandarino 	return 0;
1800db2a4165SFrank Mandarino }
1801db2a4165SFrank Mandarino 
18026f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
180351737470SMark Brown {
18046f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1805f0fba2adSLiam Girdwood 	int i;
180651737470SMark Brown 
180751737470SMark Brown 	if (!card->instantiated)
1808416356fcSMark Brown 		return 0;
180951737470SMark Brown 
181051737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
181151737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1812f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1813f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
181443829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1815f0fba2adSLiam Girdwood 	}
181651737470SMark Brown 
1817f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1818416356fcSMark Brown 
1819988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
1820988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
182188bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
182288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
182388bd870fSBenoit Cousson 		int j;
182488bd870fSBenoit Cousson 
1825988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
182688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
182788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
182888bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
182988bd870fSBenoit Cousson 		}
1830988e8cc4SNicolin Chen 	}
1831988e8cc4SNicolin Chen 
1832416356fcSMark Brown 	return 0;
183351737470SMark Brown }
18346f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
183551737470SMark Brown 
18366f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1837b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1838b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1839b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1840b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
18416f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1842b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1843416356fcSMark Brown };
1844deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1845416356fcSMark Brown 
1846db2a4165SFrank Mandarino /* ASoC platform driver */
1847db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1848db2a4165SFrank Mandarino 	.driver		= {
1849db2a4165SFrank Mandarino 		.name		= "soc-audio",
18506f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1851db2a4165SFrank Mandarino 	},
1852db2a4165SFrank Mandarino 	.probe		= soc_probe,
1853db2a4165SFrank Mandarino 	.remove		= soc_remove,
1854db2a4165SFrank Mandarino };
1855db2a4165SFrank Mandarino 
1856096e49d5SMark Brown /**
1857db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1858db2a4165SFrank Mandarino  * @_template: control template
1859db2a4165SFrank Mandarino  * @data: control private data
1860ac11a2b3SMark Brown  * @long_name: control long name
1861efb7ac3fSMark Brown  * @prefix: control name prefix
1862db2a4165SFrank Mandarino  *
1863db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1864db2a4165SFrank Mandarino  *
1865db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1866db2a4165SFrank Mandarino  */
1867db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
18683056557fSMark Brown 				  void *data, const char *long_name,
1869efb7ac3fSMark Brown 				  const char *prefix)
1870db2a4165SFrank Mandarino {
1871db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1872efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
1873efb7ac3fSMark Brown 	char *name = NULL;
1874db2a4165SFrank Mandarino 
1875db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1876db2a4165SFrank Mandarino 	template.index = 0;
1877db2a4165SFrank Mandarino 
1878efb7ac3fSMark Brown 	if (!long_name)
1879efb7ac3fSMark Brown 		long_name = template.name;
1880efb7ac3fSMark Brown 
1881efb7ac3fSMark Brown 	if (prefix) {
18822b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1883efb7ac3fSMark Brown 		if (!name)
1884efb7ac3fSMark Brown 			return NULL;
1885efb7ac3fSMark Brown 
1886efb7ac3fSMark Brown 		template.name = name;
1887efb7ac3fSMark Brown 	} else {
1888efb7ac3fSMark Brown 		template.name = long_name;
1889efb7ac3fSMark Brown 	}
1890efb7ac3fSMark Brown 
1891efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
1892efb7ac3fSMark Brown 
1893efb7ac3fSMark Brown 	kfree(name);
1894efb7ac3fSMark Brown 
1895efb7ac3fSMark Brown 	return kcontrol;
1896db2a4165SFrank Mandarino }
1897db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1898db2a4165SFrank Mandarino 
1899022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1900022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
1901022658beSLiam Girdwood 	const char *prefix, void *data)
1902022658beSLiam Girdwood {
1903022658beSLiam Girdwood 	int err, i;
1904022658beSLiam Girdwood 
1905022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
1906022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
1907022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
1908022658beSLiam Girdwood 						     control->name, prefix));
1909022658beSLiam Girdwood 		if (err < 0) {
1910f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
1911f110bfc7SLiam Girdwood 				control->name, err);
1912022658beSLiam Girdwood 			return err;
1913022658beSLiam Girdwood 		}
1914022658beSLiam Girdwood 	}
1915022658beSLiam Girdwood 
1916022658beSLiam Girdwood 	return 0;
1917022658beSLiam Girdwood }
1918022658beSLiam Girdwood 
19194fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
19204fefd698SDimitris Papastamos 					       const char *name)
19214fefd698SDimitris Papastamos {
19224fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
19234fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
19244fefd698SDimitris Papastamos 
19254fefd698SDimitris Papastamos 	if (unlikely(!name))
19264fefd698SDimitris Papastamos 		return NULL;
19274fefd698SDimitris Papastamos 
19284fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
19294fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
19304fefd698SDimitris Papastamos 			return kctl;
19314fefd698SDimitris Papastamos 	return NULL;
19324fefd698SDimitris Papastamos }
19334fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
19344fefd698SDimitris Papastamos 
1935db2a4165SFrank Mandarino /**
19360f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
19370f2780adSLars-Peter Clausen  *
19380f2780adSLars-Peter Clausen  * @component: Component to add controls to
19390f2780adSLars-Peter Clausen  * @controls: Array of controls to add
19400f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
19410f2780adSLars-Peter Clausen  *
19420f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
19430f2780adSLars-Peter Clausen  */
19440f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
19450f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
19460f2780adSLars-Peter Clausen {
19470f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
19480f2780adSLars-Peter Clausen 
19490f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
19500f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
19510f2780adSLars-Peter Clausen }
19520f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
19530f2780adSLars-Peter Clausen 
19540f2780adSLars-Peter Clausen /**
1955022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
1956022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
19573e8e1952SIan Molton  * duplicating this code.
19583e8e1952SIan Molton  *
19593e8e1952SIan Molton  * @codec: codec to add controls to
19603e8e1952SIan Molton  * @controls: array of controls to add
19613e8e1952SIan Molton  * @num_controls: number of elements in the array
19623e8e1952SIan Molton  *
19633e8e1952SIan Molton  * Return 0 for success, else error.
19643e8e1952SIan Molton  */
1965022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
19660f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
19673e8e1952SIan Molton {
19680f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
19690f2780adSLars-Peter Clausen 		num_controls);
19703e8e1952SIan Molton }
1971022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
19723e8e1952SIan Molton 
19733e8e1952SIan Molton /**
1974a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
1975022658beSLiam Girdwood  * Convenience function to add a list of controls.
1976a491a5c8SLiam Girdwood  *
1977a491a5c8SLiam Girdwood  * @platform: platform to add controls to
1978a491a5c8SLiam Girdwood  * @controls: array of controls to add
1979a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
1980a491a5c8SLiam Girdwood  *
1981a491a5c8SLiam Girdwood  * Return 0 for success, else error.
1982a491a5c8SLiam Girdwood  */
1983a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
19840f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
1985a491a5c8SLiam Girdwood {
19860f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
19870f2780adSLars-Peter Clausen 		num_controls);
1988a491a5c8SLiam Girdwood }
1989a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1990a491a5c8SLiam Girdwood 
1991a491a5c8SLiam Girdwood /**
1992022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
1993022658beSLiam Girdwood  * Convenience function to add a list of controls.
1994022658beSLiam Girdwood  *
1995022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
1996022658beSLiam Girdwood  * @controls: array of controls to add
1997022658beSLiam Girdwood  * @num_controls: number of elements in the array
1998022658beSLiam Girdwood  *
1999022658beSLiam Girdwood  * Return 0 for success, else error.
2000022658beSLiam Girdwood  */
2001022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2002022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2003022658beSLiam Girdwood {
2004022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2005022658beSLiam Girdwood 
2006022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2007022658beSLiam Girdwood 			NULL, soc_card);
2008022658beSLiam Girdwood }
2009022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2010022658beSLiam Girdwood 
2011022658beSLiam Girdwood /**
2012022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2013022658beSLiam Girdwood  * Convienience function to add a list of controls.
2014022658beSLiam Girdwood  *
2015022658beSLiam Girdwood  * @dai: DAI to add controls to
2016022658beSLiam Girdwood  * @controls: array of controls to add
2017022658beSLiam Girdwood  * @num_controls: number of elements in the array
2018022658beSLiam Girdwood  *
2019022658beSLiam Girdwood  * Return 0 for success, else error.
2020022658beSLiam Girdwood  */
2021022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2022022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2023022658beSLiam Girdwood {
2024313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2025022658beSLiam Girdwood 
2026022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2027022658beSLiam Girdwood 			NULL, dai);
2028022658beSLiam Girdwood }
2029022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2030022658beSLiam Girdwood 
2031022658beSLiam Girdwood /**
20328c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
20338c6529dbSLiam Girdwood  * @dai: DAI
20348c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
20358c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
20368c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
20378c6529dbSLiam Girdwood  *
20388c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
20398c6529dbSLiam Girdwood  */
20408c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
20418c6529dbSLiam Girdwood 	unsigned int freq, int dir)
20428c6529dbSLiam Girdwood {
2043f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2044f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2045ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2046da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2047ec4ee52aSMark Brown 						      freq, dir);
20488c6529dbSLiam Girdwood 	else
20491104a9c8SMark Brown 		return -ENOTSUPP;
20508c6529dbSLiam Girdwood }
20518c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
20528c6529dbSLiam Girdwood 
20538c6529dbSLiam Girdwood /**
2054ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2055ec4ee52aSMark Brown  * @codec: CODEC
2056ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2057da1c6ea6SMark Brown  * @source: Source for the clock
2058ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2059ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2060ec4ee52aSMark Brown  *
2061ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2062ec4ee52aSMark Brown  */
2063ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2064da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2065ec4ee52aSMark Brown {
2066ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2067da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2068da1c6ea6SMark Brown 						 freq, dir);
2069ec4ee52aSMark Brown 	else
20701104a9c8SMark Brown 		return -ENOTSUPP;
2071ec4ee52aSMark Brown }
2072ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2073ec4ee52aSMark Brown 
2074ec4ee52aSMark Brown /**
20758c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
20768c6529dbSLiam Girdwood  * @dai: DAI
2077ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
20788c6529dbSLiam Girdwood  * @div: new clock divisor.
20798c6529dbSLiam Girdwood  *
20808c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
20818c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
20828c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
20838c6529dbSLiam Girdwood  */
20848c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
20858c6529dbSLiam Girdwood 	int div_id, int div)
20868c6529dbSLiam Girdwood {
2087f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2088f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
20898c6529dbSLiam Girdwood 	else
20908c6529dbSLiam Girdwood 		return -EINVAL;
20918c6529dbSLiam Girdwood }
20928c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
20938c6529dbSLiam Girdwood 
20948c6529dbSLiam Girdwood /**
20958c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
20968c6529dbSLiam Girdwood  * @dai: DAI
20978c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
209885488037SMark Brown  * @source: DAI specific source for the PLL
20998c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
21008c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
21018c6529dbSLiam Girdwood  *
21028c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
21038c6529dbSLiam Girdwood  */
210485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
210585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
21068c6529dbSLiam Girdwood {
2107f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2108f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
210985488037SMark Brown 					 freq_in, freq_out);
2110ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2111ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2112ec4ee52aSMark Brown 						   freq_in, freq_out);
21138c6529dbSLiam Girdwood 	else
21148c6529dbSLiam Girdwood 		return -EINVAL;
21158c6529dbSLiam Girdwood }
21168c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
21178c6529dbSLiam Girdwood 
2118ec4ee52aSMark Brown /*
2119ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2120ec4ee52aSMark Brown  * @codec: CODEC
2121ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2122ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2123ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2124ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2125ec4ee52aSMark Brown  *
2126ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2127ec4ee52aSMark Brown  */
2128ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2129ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2130ec4ee52aSMark Brown {
2131ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2132ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2133ec4ee52aSMark Brown 					      freq_in, freq_out);
2134ec4ee52aSMark Brown 	else
2135ec4ee52aSMark Brown 		return -EINVAL;
2136ec4ee52aSMark Brown }
2137ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2138ec4ee52aSMark Brown 
21398c6529dbSLiam Girdwood /**
2140e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2141e54cf76bSLiam Girdwood  * @dai: DAI
2142e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
2143e54cf76bSLiam Girdwood  *
2144e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2145e54cf76bSLiam Girdwood  */
2146e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2147e54cf76bSLiam Girdwood {
2148e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2149e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2150e54cf76bSLiam Girdwood 	else
2151e54cf76bSLiam Girdwood 		return -EINVAL;
2152e54cf76bSLiam Girdwood }
2153e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2154e54cf76bSLiam Girdwood 
2155e54cf76bSLiam Girdwood /**
21568c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
21578c6529dbSLiam Girdwood  * @dai: DAI
21588c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
21598c6529dbSLiam Girdwood  *
21608c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
21618c6529dbSLiam Girdwood  */
21628c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
21638c6529dbSLiam Girdwood {
21645e4ba569SShawn Guo 	if (dai->driver == NULL)
21658c6529dbSLiam Girdwood 		return -EINVAL;
21665e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
21675e4ba569SShawn Guo 		return -ENOTSUPP;
21685e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
21698c6529dbSLiam Girdwood }
21708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
21718c6529dbSLiam Girdwood 
21728c6529dbSLiam Girdwood /**
2173e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
217489c67857SXiubo Li  * @slots: Number of slots in use.
217589c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
217689c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
217789c67857SXiubo Li  *
217889c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
217989c67857SXiubo Li  */
2180e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
218189c67857SXiubo Li 					  unsigned int *tx_mask,
218289c67857SXiubo Li 					  unsigned int *rx_mask)
218389c67857SXiubo Li {
218489c67857SXiubo Li 	if (*tx_mask || *rx_mask)
218589c67857SXiubo Li 		return 0;
218689c67857SXiubo Li 
218789c67857SXiubo Li 	if (!slots)
218889c67857SXiubo Li 		return -EINVAL;
218989c67857SXiubo Li 
219089c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
219189c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
219289c67857SXiubo Li 
219389c67857SXiubo Li 	return 0;
219489c67857SXiubo Li }
219589c67857SXiubo Li 
219689c67857SXiubo Li /**
2197e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2198e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2199a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2200a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
22018c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2202a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
22038c6529dbSLiam Girdwood  *
2204e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2205e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2206e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2207e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2208e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2209e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2210e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2211e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2212e46c9366SLars-Peter Clausen  * channel and so on.
2213e46c9366SLars-Peter Clausen  *
2214e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2215e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2216e46c9366SLars-Peter Clausen  *
2217e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
22188c6529dbSLiam Girdwood  */
22198c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2220a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
22218c6529dbSLiam Girdwood {
2222e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2223e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
222489c67857SXiubo Li 						&tx_mask, &rx_mask);
222589c67857SXiubo Li 	else
2226e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
222789c67857SXiubo Li 
222888bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
222988bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
223088bd870fSBenoit Cousson 
2231f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2232f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2233a5479e38SDaniel Ribeiro 				slots, slot_width);
22348c6529dbSLiam Girdwood 	else
2235b2cbb6e1SXiubo Li 		return -ENOTSUPP;
22368c6529dbSLiam Girdwood }
22378c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
22388c6529dbSLiam Girdwood 
22398c6529dbSLiam Girdwood /**
2240472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2241472df3cbSBarry Song  * @dai: DAI
2242472df3cbSBarry Song  * @tx_num: how many TX channels
2243472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2244472df3cbSBarry Song  *           0~num-1 uses
2245472df3cbSBarry Song  * @rx_num: how many RX channels
2246472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2247472df3cbSBarry Song  *           0~num-1 uses
2248472df3cbSBarry Song  *
2249472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2250472df3cbSBarry Song  */
2251472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2252472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2253472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2254472df3cbSBarry Song {
2255f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2256f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2257472df3cbSBarry Song 			rx_num, rx_slot);
2258472df3cbSBarry Song 	else
2259472df3cbSBarry Song 		return -EINVAL;
2260472df3cbSBarry Song }
2261472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2262472df3cbSBarry Song 
2263472df3cbSBarry Song /**
22648c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
22658c6529dbSLiam Girdwood  * @dai: DAI
22668c6529dbSLiam Girdwood  * @tristate: tristate enable
22678c6529dbSLiam Girdwood  *
22688c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
22698c6529dbSLiam Girdwood  */
22708c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
22718c6529dbSLiam Girdwood {
2272f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2273f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
22748c6529dbSLiam Girdwood 	else
22758c6529dbSLiam Girdwood 		return -EINVAL;
22768c6529dbSLiam Girdwood }
22778c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
22788c6529dbSLiam Girdwood 
22798c6529dbSLiam Girdwood /**
22808c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
22818c6529dbSLiam Girdwood  * @dai: DAI
22828c6529dbSLiam Girdwood  * @mute: mute enable
2283da18396fSMark Brown  * @direction: stream to mute
22848c6529dbSLiam Girdwood  *
22858c6529dbSLiam Girdwood  * Mutes the DAI DAC.
22868c6529dbSLiam Girdwood  */
2287da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2288da18396fSMark Brown 			     int direction)
22898c6529dbSLiam Girdwood {
2290da18396fSMark Brown 	if (!dai->driver)
2291da18396fSMark Brown 		return -ENOTSUPP;
2292da18396fSMark Brown 
2293da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2294da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2295da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2296da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2297f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
22988c6529dbSLiam Girdwood 	else
229904570c62SMark Brown 		return -ENOTSUPP;
23008c6529dbSLiam Girdwood }
23018c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
23028c6529dbSLiam Girdwood 
230388bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card,
230488bd870fSBenoit Cousson 				   struct snd_soc_dai_link *dai_link)
230588bd870fSBenoit Cousson {
230688bd870fSBenoit Cousson 	/* Legacy codec/codec_dai link is a single entry in multicodec */
230788bd870fSBenoit Cousson 	if (dai_link->codec_name || dai_link->codec_of_node ||
230888bd870fSBenoit Cousson 	    dai_link->codec_dai_name) {
230988bd870fSBenoit Cousson 		dai_link->num_codecs = 1;
231088bd870fSBenoit Cousson 
231188bd870fSBenoit Cousson 		dai_link->codecs = devm_kzalloc(card->dev,
231288bd870fSBenoit Cousson 				sizeof(struct snd_soc_dai_link_component),
231388bd870fSBenoit Cousson 				GFP_KERNEL);
231488bd870fSBenoit Cousson 		if (!dai_link->codecs)
231588bd870fSBenoit Cousson 			return -ENOMEM;
231688bd870fSBenoit Cousson 
231788bd870fSBenoit Cousson 		dai_link->codecs[0].name = dai_link->codec_name;
231888bd870fSBenoit Cousson 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
231988bd870fSBenoit Cousson 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
232088bd870fSBenoit Cousson 	}
232188bd870fSBenoit Cousson 
232288bd870fSBenoit Cousson 	if (!dai_link->codecs) {
232388bd870fSBenoit Cousson 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
232488bd870fSBenoit Cousson 		return -EINVAL;
232588bd870fSBenoit Cousson 	}
232688bd870fSBenoit Cousson 
232788bd870fSBenoit Cousson 	return 0;
232888bd870fSBenoit Cousson }
232988bd870fSBenoit Cousson 
2330c5af3a2eSMark Brown /**
2331c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2332c5af3a2eSMark Brown  *
2333ac11a2b3SMark Brown  * @card: Card to register
2334c5af3a2eSMark Brown  *
2335c5af3a2eSMark Brown  */
233670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2337c5af3a2eSMark Brown {
233888bd870fSBenoit Cousson 	int i, j, ret;
2339f0fba2adSLiam Girdwood 
2340c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2341c5af3a2eSMark Brown 		return -EINVAL;
2342c5af3a2eSMark Brown 
23435a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
23445a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
23455a504963SStephen Warren 
234688bd870fSBenoit Cousson 		ret = snd_soc_init_multicodec(card, link);
234788bd870fSBenoit Cousson 		if (ret) {
234888bd870fSBenoit Cousson 			dev_err(card->dev, "ASoC: failed to init multicodec\n");
234988bd870fSBenoit Cousson 			return ret;
235088bd870fSBenoit Cousson 		}
235188bd870fSBenoit Cousson 
235288bd870fSBenoit Cousson 		for (j = 0; j < link->num_codecs; j++) {
23535a504963SStephen Warren 			/*
23545a504963SStephen Warren 			 * Codec must be specified by 1 of name or OF node,
23555a504963SStephen Warren 			 * not both or neither.
23565a504963SStephen Warren 			 */
235788bd870fSBenoit Cousson 			if (!!link->codecs[j].name ==
235888bd870fSBenoit Cousson 			    !!link->codecs[j].of_node) {
235988bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
236010e8aa9aSMichał Mirosław 					link->name);
23615a504963SStephen Warren 				return -EINVAL;
23625a504963SStephen Warren 			}
2363bc92657aSStephen Warren 			/* Codec DAI name must be specified */
236488bd870fSBenoit Cousson 			if (!link->codecs[j].dai_name) {
236588bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
236610e8aa9aSMichał Mirosław 					link->name);
2367bc92657aSStephen Warren 				return -EINVAL;
2368bc92657aSStephen Warren 			}
236988bd870fSBenoit Cousson 		}
23705a504963SStephen Warren 
23715a504963SStephen Warren 		/*
23725a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
23735a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
23745a504963SStephen Warren 		 */
23755a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
237610e8aa9aSMichał Mirosław 			dev_err(card->dev,
237710e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
237810e8aa9aSMichał Mirosław 				link->name);
23795a504963SStephen Warren 			return -EINVAL;
23805a504963SStephen Warren 		}
23815a504963SStephen Warren 
23825a504963SStephen Warren 		/*
2383bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
2384bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
2385bc92657aSStephen Warren 		 * name alone..
23865a504963SStephen Warren 		 */
2387bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
238810e8aa9aSMichał Mirosław 			dev_err(card->dev,
238910e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
239010e8aa9aSMichał Mirosław 				link->name);
2391bc92657aSStephen Warren 			return -EINVAL;
2392bc92657aSStephen Warren 		}
2393bc92657aSStephen Warren 		/*
2394bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
2395bc92657aSStephen Warren 		 * specified
2396bc92657aSStephen Warren 		 */
2397bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
2398bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
239910e8aa9aSMichał Mirosław 			dev_err(card->dev,
240010e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
240110e8aa9aSMichał Mirosław 				link->name);
24025a504963SStephen Warren 			return -EINVAL;
24035a504963SStephen Warren 		}
24045a504963SStephen Warren 	}
24055a504963SStephen Warren 
2406ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2407ed77cc12SMark Brown 
2408111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2409111c6419SStephen Warren 
2410181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
2411181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
24122eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
2413f0fba2adSLiam Girdwood 				 GFP_KERNEL);
2414f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2415f0fba2adSLiam Girdwood 		return -ENOMEM;
2416a7dbb603SLiam Girdwood 	card->num_rtd = 0;
24172eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
2418f0fba2adSLiam Girdwood 
24195f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_links; i++) {
24205f3484acSLars-Peter Clausen 		card->rtd[i].card = card;
2421f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
242288bd870fSBenoit Cousson 		card->rtd[i].codec_dais = devm_kzalloc(card->dev,
242388bd870fSBenoit Cousson 					sizeof(struct snd_soc_dai *) *
242488bd870fSBenoit Cousson 					(card->rtd[i].dai_link->num_codecs),
242588bd870fSBenoit Cousson 					GFP_KERNEL);
242688bd870fSBenoit Cousson 		if (card->rtd[i].codec_dais == NULL)
242788bd870fSBenoit Cousson 			return -ENOMEM;
24285f3484acSLars-Peter Clausen 	}
24295f3484acSLars-Peter Clausen 
24305f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_aux_devs; i++)
24315f3484acSLars-Peter Clausen 		card->rtd_aux[i].card = card;
2432f0fba2adSLiam Girdwood 
2433db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
2434c5af3a2eSMark Brown 	card->instantiated = 0;
2435f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2436a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2437c5af3a2eSMark Brown 
2438b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2439b19e6e7bSMark Brown 	if (ret != 0)
24404e2576bdSKuninori Morimoto 		return ret;
2441c5af3a2eSMark Brown 
2442988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2443988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
244488bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
244588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
244688bd870fSBenoit Cousson 		int j;
244788bd870fSBenoit Cousson 
244888bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
244988bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2450988e8cc4SNicolin Chen 			if (!codec_dai->active)
2451988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
245288bd870fSBenoit Cousson 		}
245388bd870fSBenoit Cousson 
2454988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2455988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2456988e8cc4SNicolin Chen 	}
2457988e8cc4SNicolin Chen 
2458b19e6e7bSMark Brown 	return ret;
2459c5af3a2eSMark Brown }
246070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2461c5af3a2eSMark Brown 
2462c5af3a2eSMark Brown /**
2463c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2464c5af3a2eSMark Brown  *
2465ac11a2b3SMark Brown  * @card: Card to unregister
2466c5af3a2eSMark Brown  *
2467c5af3a2eSMark Brown  */
246870a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2469c5af3a2eSMark Brown {
247001e0df66SLars-Peter Clausen 	if (card->instantiated) {
247101e0df66SLars-Peter Clausen 		card->instantiated = false;
24721c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2473b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2474f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
24758f6f9b29SKuninori Morimoto 	}
2476c5af3a2eSMark Brown 
2477c5af3a2eSMark Brown 	return 0;
2478c5af3a2eSMark Brown }
247970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2480c5af3a2eSMark Brown 
2481f0fba2adSLiam Girdwood /*
2482f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2483f0fba2adSLiam Girdwood  * and sanitizing names.
2484f0fba2adSLiam Girdwood  */
24850b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2486f0fba2adSLiam Girdwood {
2487f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2488f0fba2adSLiam Girdwood 	int id1, id2;
2489f0fba2adSLiam Girdwood 
2490f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2491f0fba2adSLiam Girdwood 		return NULL;
2492f0fba2adSLiam Girdwood 
249358818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2494f0fba2adSLiam Girdwood 
2495f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2496f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2497f0fba2adSLiam Girdwood 	if (found) {
2498f0fba2adSLiam Girdwood 		/* get ID */
2499f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2500f0fba2adSLiam Girdwood 
2501f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2502f0fba2adSLiam Girdwood 			if (*id == -1)
2503f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2504f0fba2adSLiam Girdwood 		}
2505f0fba2adSLiam Girdwood 
2506f0fba2adSLiam Girdwood 	} else {
2507f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2508f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2509f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2510f0fba2adSLiam Girdwood 
2511f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
251205899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2513f0fba2adSLiam Girdwood 
2514f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2515f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
251658818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2517f0fba2adSLiam Girdwood 		} else
2518f0fba2adSLiam Girdwood 			*id = 0;
2519f0fba2adSLiam Girdwood 	}
2520f0fba2adSLiam Girdwood 
2521f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2522f0fba2adSLiam Girdwood }
2523f0fba2adSLiam Girdwood 
2524f0fba2adSLiam Girdwood /*
2525f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2526f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2527f0fba2adSLiam Girdwood  */
2528f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2529f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2530f0fba2adSLiam Girdwood {
2531f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
253210e8aa9aSMichał Mirosław 		dev_err(dev,
253310e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
253410e8aa9aSMichał Mirosław 			dev_name(dev));
2535f0fba2adSLiam Girdwood 		return NULL;
2536f0fba2adSLiam Girdwood 	}
2537f0fba2adSLiam Girdwood 
2538f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2539f0fba2adSLiam Girdwood }
2540f0fba2adSLiam Girdwood 
25419115171aSMark Brown /**
254232c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25439115171aSMark Brown  *
254432c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25459115171aSMark Brown  */
254632c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25479115171aSMark Brown {
25485c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25499115171aSMark Brown 
25505c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
255132c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
255232c9ba54SLars-Peter Clausen 			dai->name);
25539115171aSMark Brown 		list_del(&dai->list);
2554f0fba2adSLiam Girdwood 		kfree(dai->name);
2555f0fba2adSLiam Girdwood 		kfree(dai);
25569115171aSMark Brown 	}
255732c9ba54SLars-Peter Clausen }
25589115171aSMark Brown 
25599115171aSMark Brown /**
256032c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25619115171aSMark Brown  *
25626106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25636106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2564ac11a2b3SMark Brown  * @count: Number of DAIs
256532c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
256632c9ba54SLars-Peter Clausen  *                     parent's name.
25679115171aSMark Brown  */
256832c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2569bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2570bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
25719115171aSMark Brown {
25726106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2573f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
257432c9ba54SLars-Peter Clausen 	unsigned int i;
257532c9ba54SLars-Peter Clausen 	int ret;
2576f0fba2adSLiam Girdwood 
2577f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
25789115171aSMark Brown 
2579bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2580bb13109dSLars-Peter Clausen 	component->num_dai = count;
2581bb13109dSLars-Peter Clausen 
25829115171aSMark Brown 	for (i = 0; i < count; i++) {
2583f0fba2adSLiam Girdwood 
2584f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2585c46e0079SAxel Lin 		if (dai == NULL) {
2586c46e0079SAxel Lin 			ret = -ENOMEM;
2587c46e0079SAxel Lin 			goto err;
2588c46e0079SAxel Lin 		}
2589f0fba2adSLiam Girdwood 
259032c9ba54SLars-Peter Clausen 		/*
259132c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
259232c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
259332c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
259432c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
259532c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
259632c9ba54SLars-Peter Clausen 		 * component-less anymore.
259732c9ba54SLars-Peter Clausen 		 */
25989e498089SSrinivas Kandagatla 		if (count == 1 && legacy_dai_naming &&
25999e498089SSrinivas Kandagatla 			(dai_drv[i].id == 0 || dai_drv[i].name == NULL)) {
2600db2a4165SFrank Mandarino 			dai->name = fmt_single_name(dev, &dai->id);
260132c9ba54SLars-Peter Clausen 		} else {
2602f0fba2adSLiam Girdwood 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
260332c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
260432c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
260532c9ba54SLars-Peter Clausen 			else
260632c9ba54SLars-Peter Clausen 				dai->id = i;
260732c9ba54SLars-Peter Clausen 		}
2608f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
2609f0fba2adSLiam Girdwood 			kfree(dai);
261032c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
26119115171aSMark Brown 			goto err;
26129115171aSMark Brown 		}
26139115171aSMark Brown 
26146106d129SLars-Peter Clausen 		dai->component = component;
2615f0fba2adSLiam Girdwood 		dai->dev = dev;
2616f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
2617f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
2618f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
2619f0fba2adSLiam Girdwood 
26201438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
2621054880feSMark Brown 
26229115171aSMark Brown 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2623f0fba2adSLiam Girdwood 	}
2624f0fba2adSLiam Girdwood 
26259115171aSMark Brown 	return 0;
26269115171aSMark Brown 
26279115171aSMark Brown err:
262832c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26299115171aSMark Brown 
26309115171aSMark Brown 	return ret;
26319115171aSMark Brown }
26329115171aSMark Brown 
263314e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
263414e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2635d191bd8dSKuninori Morimoto {
263614e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2637d191bd8dSKuninori Morimoto 
263814e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
263914e8bdebSLars-Peter Clausen }
2640d191bd8dSKuninori Morimoto 
264114e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
264214e8bdebSLars-Peter Clausen 	int event)
264314e8bdebSLars-Peter Clausen {
264414e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
264514e8bdebSLars-Peter Clausen 
264614e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
264714e8bdebSLars-Peter Clausen }
264814e8bdebSLars-Peter Clausen 
2649bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2650bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2651d191bd8dSKuninori Morimoto {
2652ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2653ce0fc93aSLars-Peter Clausen 
2654bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2655bb13109dSLars-Peter Clausen 	if (!component->name) {
2656bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2657d191bd8dSKuninori Morimoto 		return -ENOMEM;
2658d191bd8dSKuninori Morimoto 	}
2659d191bd8dSKuninori Morimoto 
2660bb13109dSLars-Peter Clausen 	component->dev = dev;
2661bb13109dSLars-Peter Clausen 	component->driver = driver;
266261aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
266361aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
2664e2c330b9SLars-Peter Clausen 
2665ce0fc93aSLars-Peter Clausen 	if (!component->dapm_ptr)
2666ce0fc93aSLars-Peter Clausen 		component->dapm_ptr = &component->dapm;
2667ce0fc93aSLars-Peter Clausen 
2668ce0fc93aSLars-Peter Clausen 	dapm = component->dapm_ptr;
2669ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2670ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2671ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
26725819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
267314e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
267414e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
267514e8bdebSLars-Peter Clausen 	if (driver->stream_event)
267614e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2677ce0fc93aSLars-Peter Clausen 
267861aca564SLars-Peter Clausen 	component->controls = driver->controls;
267961aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
268061aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
268161aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
268261aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
268361aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
268461aca564SLars-Peter Clausen 
2685bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2686bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2687bb13109dSLars-Peter Clausen 
2688bb13109dSLars-Peter Clausen 	return 0;
2689d191bd8dSKuninori Morimoto }
2690d191bd8dSKuninori Morimoto 
269120feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2692886f5692SLars-Peter Clausen {
2693886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
269420feb881SLars-Peter Clausen 
2695886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2696886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2697886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2698886f5692SLars-Peter Clausen }
269920feb881SLars-Peter Clausen 
2700e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2701e874bf5fSLars-Peter Clausen 
270220feb881SLars-Peter Clausen /**
270320feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
270420feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
270520feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
270620feb881SLars-Peter Clausen  *
270720feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
270820feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
270920feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
271020feb881SLars-Peter Clausen  * before the first IO attempt of the component.
271120feb881SLars-Peter Clausen  */
271220feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
271320feb881SLars-Peter Clausen 	struct regmap *regmap)
271420feb881SLars-Peter Clausen {
271520feb881SLars-Peter Clausen 	component->regmap = regmap;
271620feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2717886f5692SLars-Peter Clausen }
271820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
271920feb881SLars-Peter Clausen 
272020feb881SLars-Peter Clausen /**
272120feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
272220feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
272320feb881SLars-Peter Clausen  *
272420feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
272520feb881SLars-Peter Clausen  * removes the regmap instance from the component.
272620feb881SLars-Peter Clausen  *
272720feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
272820feb881SLars-Peter Clausen  * to initialize the regmap instance.
272920feb881SLars-Peter Clausen  */
273020feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
273120feb881SLars-Peter Clausen {
273220feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
273320feb881SLars-Peter Clausen 	component->regmap = NULL;
273420feb881SLars-Peter Clausen }
273520feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2736886f5692SLars-Peter Clausen 
2737e874bf5fSLars-Peter Clausen #endif
2738d191bd8dSKuninori Morimoto 
2739bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2740bb13109dSLars-Peter Clausen {
274120feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
274220feb881SLars-Peter Clausen 		if (!component->regmap)
274320feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
274420feb881SLars-Peter Clausen 		if (component->regmap)
274520feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
274620feb881SLars-Peter Clausen 	}
2747886f5692SLars-Peter Clausen 
2748bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2749d191bd8dSKuninori Morimoto }
2750d191bd8dSKuninori Morimoto 
2751bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
2752bb13109dSLars-Peter Clausen {
2753d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
2754bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
2755d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2756bb13109dSLars-Peter Clausen }
2757d191bd8dSKuninori Morimoto 
2758bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2759bb13109dSLars-Peter Clausen {
2760bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2761bb13109dSLars-Peter Clausen 	kfree(component->name);
2762bb13109dSLars-Peter Clausen }
2763d191bd8dSKuninori Morimoto 
2764bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2765bb13109dSLars-Peter Clausen {
2766bb13109dSLars-Peter Clausen 	list_del(&component->list);
2767bb13109dSLars-Peter Clausen }
2768d191bd8dSKuninori Morimoto 
2769d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2770d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
2771d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
2772d191bd8dSKuninori Morimoto 			       int num_dai)
2773d191bd8dSKuninori Morimoto {
2774d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
2775bb13109dSLars-Peter Clausen 	int ret;
2776d191bd8dSKuninori Morimoto 
2777bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
2778d191bd8dSKuninori Morimoto 	if (!cmpnt) {
2779d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
2780d191bd8dSKuninori Morimoto 		return -ENOMEM;
2781d191bd8dSKuninori Morimoto 	}
2782d191bd8dSKuninori Morimoto 
2783bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2784bb13109dSLars-Peter Clausen 	if (ret)
2785bb13109dSLars-Peter Clausen 		goto err_free;
2786bb13109dSLars-Peter Clausen 
27873d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
278898e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
27893d59400fSLars-Peter Clausen 
2790bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2791bb13109dSLars-Peter Clausen 	if (ret < 0) {
2792f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2793bb13109dSLars-Peter Clausen 		goto err_cleanup;
2794bb13109dSLars-Peter Clausen 	}
2795bb13109dSLars-Peter Clausen 
2796bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
2797bb13109dSLars-Peter Clausen 
2798bb13109dSLars-Peter Clausen 	return 0;
2799bb13109dSLars-Peter Clausen 
2800bb13109dSLars-Peter Clausen err_cleanup:
2801bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
2802bb13109dSLars-Peter Clausen err_free:
2803bb13109dSLars-Peter Clausen 	kfree(cmpnt);
2804bb13109dSLars-Peter Clausen 	return ret;
2805d191bd8dSKuninori Morimoto }
2806d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2807d191bd8dSKuninori Morimoto 
2808d191bd8dSKuninori Morimoto /**
2809d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
2810d191bd8dSKuninori Morimoto  *
2811d191bd8dSKuninori Morimoto  */
2812d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
2813d191bd8dSKuninori Morimoto {
2814d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
2815d191bd8dSKuninori Morimoto 
281634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2817d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
281898e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
2819d191bd8dSKuninori Morimoto 			goto found;
2820d191bd8dSKuninori Morimoto 	}
282134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2822d191bd8dSKuninori Morimoto 	return;
2823d191bd8dSKuninori Morimoto 
2824d191bd8dSKuninori Morimoto found:
282534e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
282634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2827bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
2828bb13109dSLars-Peter Clausen 	kfree(cmpnt);
2829d191bd8dSKuninori Morimoto }
2830d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2831d191bd8dSKuninori Morimoto 
2832f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
2833e2c330b9SLars-Peter Clausen {
2834e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2835e2c330b9SLars-Peter Clausen 
2836f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
2837e2c330b9SLars-Peter Clausen }
2838e2c330b9SLars-Peter Clausen 
2839f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
2840e2c330b9SLars-Peter Clausen {
2841e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2842e2c330b9SLars-Peter Clausen 
2843f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
2844e2c330b9SLars-Peter Clausen }
2845e2c330b9SLars-Peter Clausen 
2846d191bd8dSKuninori Morimoto /**
284771a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
284871a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
284971a45cdaSLars-Peter Clausen  * @platform: The platform to add
285071a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
285112a48a8cSMark Brown  */
285271a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
2853d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
285412a48a8cSMark Brown {
2855b37f1d12SLars-Peter Clausen 	int ret;
2856b37f1d12SLars-Peter Clausen 
2857bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
2858bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
2859bb13109dSLars-Peter Clausen 	if (ret)
2860bb13109dSLars-Peter Clausen 		return ret;
2861f0fba2adSLiam Girdwood 
2862f0fba2adSLiam Girdwood 	platform->dev = dev;
2863f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
2864f1d45cc3SLars-Peter Clausen 
2865f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
2866f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
2867f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
2868f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
286912a48a8cSMark Brown 
287081c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
287181c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
287281c7cfd1SLars-Peter Clausen #endif
287312a48a8cSMark Brown 
287412a48a8cSMark Brown 	mutex_lock(&client_mutex);
2875bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
287612a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
287712a48a8cSMark Brown 	mutex_unlock(&client_mutex);
287812a48a8cSMark Brown 
2879f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2880f4333203SLars-Peter Clausen 		platform->component.name);
288112a48a8cSMark Brown 
288212a48a8cSMark Brown 	return 0;
288312a48a8cSMark Brown }
288471a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
288571a45cdaSLars-Peter Clausen 
288671a45cdaSLars-Peter Clausen /**
288771a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
288871a45cdaSLars-Peter Clausen  *
288971a45cdaSLars-Peter Clausen  * @platform: platform to register
289071a45cdaSLars-Peter Clausen  */
289171a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
289271a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
289371a45cdaSLars-Peter Clausen {
289471a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
289571a45cdaSLars-Peter Clausen 	int ret;
289671a45cdaSLars-Peter Clausen 
289771a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
289871a45cdaSLars-Peter Clausen 
289971a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
290071a45cdaSLars-Peter Clausen 	if (platform == NULL)
290171a45cdaSLars-Peter Clausen 		return -ENOMEM;
290271a45cdaSLars-Peter Clausen 
290371a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
290471a45cdaSLars-Peter Clausen 	if (ret)
290571a45cdaSLars-Peter Clausen 		kfree(platform);
290671a45cdaSLars-Peter Clausen 
290771a45cdaSLars-Peter Clausen 	return ret;
290871a45cdaSLars-Peter Clausen }
290912a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
291012a48a8cSMark Brown 
291112a48a8cSMark Brown /**
291271a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
291371a45cdaSLars-Peter Clausen  * @platform: the platform to remove
291471a45cdaSLars-Peter Clausen  */
291571a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
291671a45cdaSLars-Peter Clausen {
2917b37f1d12SLars-Peter Clausen 
291871a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
291971a45cdaSLars-Peter Clausen 	list_del(&platform->list);
2920bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
292171a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
292271a45cdaSLars-Peter Clausen 
292371a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
2924f4333203SLars-Peter Clausen 		platform->component.name);
2925decc27b0SDaniel Mack 
2926decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
292771a45cdaSLars-Peter Clausen }
292871a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
292971a45cdaSLars-Peter Clausen 
293071a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
293171a45cdaSLars-Peter Clausen {
293271a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
293371a45cdaSLars-Peter Clausen 
293434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
293571a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
293634e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
293734e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
293871a45cdaSLars-Peter Clausen 			return platform;
293971a45cdaSLars-Peter Clausen 		}
294034e81ab4SLars-Peter Clausen 	}
294134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
294271a45cdaSLars-Peter Clausen 
294371a45cdaSLars-Peter Clausen 	return NULL;
294471a45cdaSLars-Peter Clausen }
294571a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
294671a45cdaSLars-Peter Clausen 
294771a45cdaSLars-Peter Clausen /**
294812a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
294912a48a8cSMark Brown  *
2950ac11a2b3SMark Brown  * @platform: platform to unregister
295112a48a8cSMark Brown  */
2952f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
295312a48a8cSMark Brown {
2954f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
2955f0fba2adSLiam Girdwood 
295671a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
295771a45cdaSLars-Peter Clausen 	if (!platform)
2958f0fba2adSLiam Girdwood 		return;
2959f0fba2adSLiam Girdwood 
296071a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
2961f0fba2adSLiam Girdwood 	kfree(platform);
296212a48a8cSMark Brown }
296312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
296412a48a8cSMark Brown 
2965151ab22cSMark Brown static u64 codec_format_map[] = {
2966151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2967151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2968151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2969151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2970151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2971151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2972151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2973151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2974151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2975151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2976151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2977151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2978151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2979151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2980151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2981151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2982151ab22cSMark Brown };
2983151ab22cSMark Brown 
2984151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2985151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2986151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2987151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2988151ab22cSMark Brown  */
2989151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2990151ab22cSMark Brown {
2991151ab22cSMark Brown 	int i;
2992151ab22cSMark Brown 
2993151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2994151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2995151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
2996151ab22cSMark Brown }
2997151ab22cSMark Brown 
2998f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
2999f1d45cc3SLars-Peter Clausen {
3000f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3001f1d45cc3SLars-Peter Clausen 
3002f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3003f1d45cc3SLars-Peter Clausen }
3004f1d45cc3SLars-Peter Clausen 
3005f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3006f1d45cc3SLars-Peter Clausen {
3007f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3008f1d45cc3SLars-Peter Clausen 
3009f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3010f1d45cc3SLars-Peter Clausen }
3011f1d45cc3SLars-Peter Clausen 
3012e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3013e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3014e2c330b9SLars-Peter Clausen {
3015e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3016e2c330b9SLars-Peter Clausen 
3017e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3018e2c330b9SLars-Peter Clausen }
3019e2c330b9SLars-Peter Clausen 
3020e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3021e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3022e2c330b9SLars-Peter Clausen {
3023e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3024e2c330b9SLars-Peter Clausen 
3025e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3026e2c330b9SLars-Peter Clausen 
3027e2c330b9SLars-Peter Clausen 	return 0;
3028e2c330b9SLars-Peter Clausen }
3029e2c330b9SLars-Peter Clausen 
303068f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
303168f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
303268f831c2SLars-Peter Clausen {
303368f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
303468f831c2SLars-Peter Clausen 
303568f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
303668f831c2SLars-Peter Clausen }
303768f831c2SLars-Peter Clausen 
30380d0cf00aSMark Brown /**
30390d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
30400d0cf00aSMark Brown  *
3041ac11a2b3SMark Brown  * @codec: codec to register
30420d0cf00aSMark Brown  */
3043f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3044001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3045001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3046001ae4c0SMark Brown 			   int num_dai)
30470d0cf00aSMark Brown {
3048f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3049bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3050f0fba2adSLiam Girdwood 	int ret, i;
3051151ab22cSMark Brown 
3052f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
30530d0cf00aSMark Brown 
3054f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3055f0fba2adSLiam Girdwood 	if (codec == NULL)
3056f0fba2adSLiam Girdwood 		return -ENOMEM;
30570d0cf00aSMark Brown 
3058ce0fc93aSLars-Peter Clausen 	codec->component.dapm_ptr = &codec->dapm;
3059f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3060ce0fc93aSLars-Peter Clausen 
3061bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3062bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3063bb13109dSLars-Peter Clausen 	if (ret)
3064bb13109dSLars-Peter Clausen 		goto err_free;
3065151ab22cSMark Brown 
3066f1d45cc3SLars-Peter Clausen 	if (codec_drv->controls) {
3067f1d45cc3SLars-Peter Clausen 		codec->component.controls = codec_drv->controls;
3068f1d45cc3SLars-Peter Clausen 		codec->component.num_controls = codec_drv->num_controls;
3069f1d45cc3SLars-Peter Clausen 	}
3070f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_widgets) {
3071f1d45cc3SLars-Peter Clausen 		codec->component.dapm_widgets = codec_drv->dapm_widgets;
3072f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3073f1d45cc3SLars-Peter Clausen 	}
3074f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_routes) {
3075f1d45cc3SLars-Peter Clausen 		codec->component.dapm_routes = codec_drv->dapm_routes;
3076f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3077f1d45cc3SLars-Peter Clausen 	}
3078f1d45cc3SLars-Peter Clausen 
3079f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3080f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3081f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3082f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
3083e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3084e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3085e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3086e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
30873d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
30885819c2faSLars-Peter Clausen 	codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
308986dbf2acSLars-Peter Clausen 	codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
309014e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
3091474b62d6SMark Brown 		codec->dapm.seq_notifier = codec_drv->seq_notifier;
309268f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
309368f831c2SLars-Peter Clausen 		codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3094f0fba2adSLiam Girdwood 	codec->dev = dev;
3095f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3096e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3097f0fba2adSLiam Girdwood 
309881c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
309981c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
310081c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
310181c7cfd1SLars-Peter Clausen #endif
3102a39f75f7SXiubo Li 
3103a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3104886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3105a39f75f7SXiubo Li 
3106f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3107f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3108f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3109f0fba2adSLiam Girdwood 	}
3110f0fba2adSLiam Girdwood 
3111bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3112bb13109dSLars-Peter Clausen 	if (ret < 0) {
3113f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3114bb13109dSLars-Peter Clausen 		goto err_cleanup;
3115bb13109dSLars-Peter Clausen 	}
3116bb13109dSLars-Peter Clausen 
3117bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3118bb13109dSLars-Peter Clausen 		dai->codec = codec;
3119bb13109dSLars-Peter Clausen 
3120054880feSMark Brown 	mutex_lock(&client_mutex);
3121bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3122054880feSMark Brown 	list_add(&codec->list, &codec_list);
3123054880feSMark Brown 	mutex_unlock(&client_mutex);
3124054880feSMark Brown 
3125f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3126f4333203SLars-Peter Clausen 		codec->component.name);
31270d0cf00aSMark Brown 	return 0;
3128f0fba2adSLiam Girdwood 
3129bb13109dSLars-Peter Clausen err_cleanup:
3130bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3131bb13109dSLars-Peter Clausen err_free:
3132f0fba2adSLiam Girdwood 	kfree(codec);
3133f0fba2adSLiam Girdwood 	return ret;
31340d0cf00aSMark Brown }
31350d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
31360d0cf00aSMark Brown 
31370d0cf00aSMark Brown /**
31380d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
31390d0cf00aSMark Brown  *
3140ac11a2b3SMark Brown  * @codec: codec to unregister
31410d0cf00aSMark Brown  */
3142f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
31430d0cf00aSMark Brown {
3144f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3145f0fba2adSLiam Girdwood 
314634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3147f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3148f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3149f0fba2adSLiam Girdwood 			goto found;
3150f0fba2adSLiam Girdwood 	}
315134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3152f0fba2adSLiam Girdwood 	return;
3153f0fba2adSLiam Girdwood 
3154f0fba2adSLiam Girdwood found:
31550d0cf00aSMark Brown 	list_del(&codec->list);
3156bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
31570d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
31580d0cf00aSMark Brown 
3159f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3160f4333203SLars-Peter Clausen 			codec->component.name);
3161f0fba2adSLiam Girdwood 
3162bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
31637a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3164f0fba2adSLiam Girdwood 	kfree(codec);
31650d0cf00aSMark Brown }
31660d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
31670d0cf00aSMark Brown 
3168bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3169bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3170bec4fa05SStephen Warren 			       const char *propname)
3171bec4fa05SStephen Warren {
31727e07e7c0STushar Behera 	struct device_node *np;
3173bec4fa05SStephen Warren 	int ret;
3174bec4fa05SStephen Warren 
31757e07e7c0STushar Behera 	if (!card->dev) {
31767e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
31777e07e7c0STushar Behera 		return -EINVAL;
31787e07e7c0STushar Behera 	}
31797e07e7c0STushar Behera 
31807e07e7c0STushar Behera 	np = card->dev->of_node;
31817e07e7c0STushar Behera 
3182bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3183bec4fa05SStephen Warren 	/*
3184bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3185bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3186bec4fa05SStephen Warren 	 * snd_soc_register_card.
3187bec4fa05SStephen Warren 	 */
3188bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3189bec4fa05SStephen Warren 		dev_err(card->dev,
3190f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3191bec4fa05SStephen Warren 			propname, ret);
3192bec4fa05SStephen Warren 		return ret;
3193bec4fa05SStephen Warren 	}
3194bec4fa05SStephen Warren 
3195bec4fa05SStephen Warren 	return 0;
3196bec4fa05SStephen Warren }
3197bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3198bec4fa05SStephen Warren 
31999a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
32009a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
32019a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
32029a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
32039a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
32049a6d4860SXiubo Li };
32059a6d4860SXiubo Li 
32069a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
32079a6d4860SXiubo Li 					  const char *propname)
32089a6d4860SXiubo Li {
32099a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
32109a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
32119a6d4860SXiubo Li 	const char *template, *wname;
32129a6d4860SXiubo Li 	int i, j, num_widgets, ret;
32139a6d4860SXiubo Li 
32149a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
32159a6d4860SXiubo Li 	if (num_widgets < 0) {
32169a6d4860SXiubo Li 		dev_err(card->dev,
32179a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
32189a6d4860SXiubo Li 		return -EINVAL;
32199a6d4860SXiubo Li 	}
32209a6d4860SXiubo Li 	if (num_widgets & 1) {
32219a6d4860SXiubo Li 		dev_err(card->dev,
32229a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
32239a6d4860SXiubo Li 		return -EINVAL;
32249a6d4860SXiubo Li 	}
32259a6d4860SXiubo Li 
32269a6d4860SXiubo Li 	num_widgets /= 2;
32279a6d4860SXiubo Li 	if (!num_widgets) {
32289a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
32299a6d4860SXiubo Li 			propname);
32309a6d4860SXiubo Li 		return -EINVAL;
32319a6d4860SXiubo Li 	}
32329a6d4860SXiubo Li 
32339a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
32349a6d4860SXiubo Li 			       GFP_KERNEL);
32359a6d4860SXiubo Li 	if (!widgets) {
32369a6d4860SXiubo Li 		dev_err(card->dev,
32379a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
32389a6d4860SXiubo Li 		return -ENOMEM;
32399a6d4860SXiubo Li 	}
32409a6d4860SXiubo Li 
32419a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
32429a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
32439a6d4860SXiubo Li 			2 * i, &template);
32449a6d4860SXiubo Li 		if (ret) {
32459a6d4860SXiubo Li 			dev_err(card->dev,
32469a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
32479a6d4860SXiubo Li 				propname, 2 * i, ret);
32489a6d4860SXiubo Li 			return -EINVAL;
32499a6d4860SXiubo Li 		}
32509a6d4860SXiubo Li 
32519a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
32529a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
32539a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
32549a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
32559a6d4860SXiubo Li 				break;
32569a6d4860SXiubo Li 			}
32579a6d4860SXiubo Li 		}
32589a6d4860SXiubo Li 
32599a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
32609a6d4860SXiubo Li 			dev_err(card->dev,
32619a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
32629a6d4860SXiubo Li 				template);
32639a6d4860SXiubo Li 			return -EINVAL;
32649a6d4860SXiubo Li 		}
32659a6d4860SXiubo Li 
32669a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
32679a6d4860SXiubo Li 						    (2 * i) + 1,
32689a6d4860SXiubo Li 						    &wname);
32699a6d4860SXiubo Li 		if (ret) {
32709a6d4860SXiubo Li 			dev_err(card->dev,
32719a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
32729a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
32739a6d4860SXiubo Li 			return -EINVAL;
32749a6d4860SXiubo Li 		}
32759a6d4860SXiubo Li 
32769a6d4860SXiubo Li 		widgets[i].name = wname;
32779a6d4860SXiubo Li 	}
32789a6d4860SXiubo Li 
3279f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3280f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
32819a6d4860SXiubo Li 
32829a6d4860SXiubo Li 	return 0;
32839a6d4860SXiubo Li }
32849a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
32859a6d4860SXiubo Li 
328689c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
328789c67857SXiubo Li 			      unsigned int *slots,
328889c67857SXiubo Li 			      unsigned int *slot_width)
328989c67857SXiubo Li {
329089c67857SXiubo Li 	u32 val;
329189c67857SXiubo Li 	int ret;
329289c67857SXiubo Li 
329389c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
329489c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
329589c67857SXiubo Li 		if (ret)
329689c67857SXiubo Li 			return ret;
329789c67857SXiubo Li 
329889c67857SXiubo Li 		if (slots)
329989c67857SXiubo Li 			*slots = val;
330089c67857SXiubo Li 	}
330189c67857SXiubo Li 
330289c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
330389c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
330489c67857SXiubo Li 		if (ret)
330589c67857SXiubo Li 			return ret;
330689c67857SXiubo Li 
330789c67857SXiubo Li 		if (slot_width)
330889c67857SXiubo Li 			*slot_width = val;
330989c67857SXiubo Li 	}
331089c67857SXiubo Li 
331189c67857SXiubo Li 	return 0;
331289c67857SXiubo Li }
331389c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
331489c67857SXiubo Li 
3315a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3316a4a54dd5SStephen Warren 				   const char *propname)
3317a4a54dd5SStephen Warren {
3318a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3319e3b1e6a1SMark Brown 	int num_routes;
3320a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3321a4a54dd5SStephen Warren 	int i, ret;
3322a4a54dd5SStephen Warren 
3323a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3324c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
332510e8aa9aSMichał Mirosław 		dev_err(card->dev,
332610e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
332710e8aa9aSMichał Mirosław 			propname);
3328a4a54dd5SStephen Warren 		return -EINVAL;
3329a4a54dd5SStephen Warren 	}
3330a4a54dd5SStephen Warren 	num_routes /= 2;
3331a4a54dd5SStephen Warren 	if (!num_routes) {
3332f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3333a4a54dd5SStephen Warren 			propname);
3334a4a54dd5SStephen Warren 		return -EINVAL;
3335a4a54dd5SStephen Warren 	}
3336a4a54dd5SStephen Warren 
3337e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3338a4a54dd5SStephen Warren 			      GFP_KERNEL);
3339a4a54dd5SStephen Warren 	if (!routes) {
3340a4a54dd5SStephen Warren 		dev_err(card->dev,
3341f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3342a4a54dd5SStephen Warren 		return -EINVAL;
3343a4a54dd5SStephen Warren 	}
3344a4a54dd5SStephen Warren 
3345a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3346a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3347e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3348a4a54dd5SStephen Warren 		if (ret) {
3349c871bd0bSMark Brown 			dev_err(card->dev,
3350c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3351c871bd0bSMark Brown 				propname, 2 * i, ret);
3352a4a54dd5SStephen Warren 			return -EINVAL;
3353a4a54dd5SStephen Warren 		}
3354a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3355e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3356a4a54dd5SStephen Warren 		if (ret) {
3357a4a54dd5SStephen Warren 			dev_err(card->dev,
3358c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3359c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3360a4a54dd5SStephen Warren 			return -EINVAL;
3361a4a54dd5SStephen Warren 		}
3362a4a54dd5SStephen Warren 	}
3363a4a54dd5SStephen Warren 
3364f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3365f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3366a4a54dd5SStephen Warren 
3367a4a54dd5SStephen Warren 	return 0;
3368a4a54dd5SStephen Warren }
3369a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3370a4a54dd5SStephen Warren 
3371a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3372389cb834SJyri Sarha 				     const char *prefix,
3373389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3374389cb834SJyri Sarha 				     struct device_node **framemaster)
3375a7930ed4SKuninori Morimoto {
3376a7930ed4SKuninori Morimoto 	int ret, i;
3377a7930ed4SKuninori Morimoto 	char prop[128];
3378a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3379a7930ed4SKuninori Morimoto 	int bit, frame;
3380a7930ed4SKuninori Morimoto 	const char *str;
3381a7930ed4SKuninori Morimoto 	struct {
3382a7930ed4SKuninori Morimoto 		char *name;
3383a7930ed4SKuninori Morimoto 		unsigned int val;
3384a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3385a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3386a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3387a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3388a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3389a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3390a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3391a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3392a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3393a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3394a7930ed4SKuninori Morimoto 	};
3395a7930ed4SKuninori Morimoto 
3396a7930ed4SKuninori Morimoto 	if (!prefix)
3397a7930ed4SKuninori Morimoto 		prefix = "";
3398a7930ed4SKuninori Morimoto 
3399a7930ed4SKuninori Morimoto 	/*
3400a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3401a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3402a7930ed4SKuninori Morimoto 	 */
3403a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3404a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3405a7930ed4SKuninori Morimoto 	if (ret == 0) {
3406a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3407a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3408a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3409a7930ed4SKuninori Morimoto 				break;
3410a7930ed4SKuninori Morimoto 			}
3411a7930ed4SKuninori Morimoto 		}
3412a7930ed4SKuninori Morimoto 	}
3413a7930ed4SKuninori Morimoto 
3414a7930ed4SKuninori Morimoto 	/*
34158c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3416a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3417a7930ed4SKuninori Morimoto 	 */
34188c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
34198c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
34208c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
34218c2d6a9fSKuninori Morimoto 	else
34228c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3423a7930ed4SKuninori Morimoto 
3424a7930ed4SKuninori Morimoto 	/*
3425a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3426a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3427a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3428a7930ed4SKuninori Morimoto 	 */
3429a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3430a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3431a7930ed4SKuninori Morimoto 
3432a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3433a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3434a7930ed4SKuninori Morimoto 
3435a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3436a7930ed4SKuninori Morimoto 	case 0x11:
3437a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3438a7930ed4SKuninori Morimoto 		break;
3439a7930ed4SKuninori Morimoto 	case 0x10:
3440a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3441a7930ed4SKuninori Morimoto 		break;
3442a7930ed4SKuninori Morimoto 	case 0x01:
3443a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3444a7930ed4SKuninori Morimoto 		break;
3445a7930ed4SKuninori Morimoto 	default:
3446a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3447a7930ed4SKuninori Morimoto 		break;
3448a7930ed4SKuninori Morimoto 	}
3449a7930ed4SKuninori Morimoto 
3450a7930ed4SKuninori Morimoto 	/*
3451a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3452a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3453a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3454a7930ed4SKuninori Morimoto 	 */
3455a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3456a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3457389cb834SJyri Sarha 	if (bit && bitclkmaster)
3458389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3459a7930ed4SKuninori Morimoto 
3460a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3461a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3462389cb834SJyri Sarha 	if (frame && framemaster)
3463389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3464a7930ed4SKuninori Morimoto 
3465a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3466a7930ed4SKuninori Morimoto 	case 0x11:
3467a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3468a7930ed4SKuninori Morimoto 		break;
3469a7930ed4SKuninori Morimoto 	case 0x10:
3470a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3471a7930ed4SKuninori Morimoto 		break;
3472a7930ed4SKuninori Morimoto 	case 0x01:
3473a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3474a7930ed4SKuninori Morimoto 		break;
3475a7930ed4SKuninori Morimoto 	default:
3476a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3477a7930ed4SKuninori Morimoto 		break;
3478a7930ed4SKuninori Morimoto 	}
3479a7930ed4SKuninori Morimoto 
3480a7930ed4SKuninori Morimoto 	return format;
3481a7930ed4SKuninori Morimoto }
3482a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3483a7930ed4SKuninori Morimoto 
348493b0f3eeSJean-Francois Moine static int snd_soc_get_dai_name(struct of_phandle_args *args,
3485cb470087SKuninori Morimoto 				const char **dai_name)
3486cb470087SKuninori Morimoto {
3487cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
3488*3e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
348993b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3490cb470087SKuninori Morimoto 
3491cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3492cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
3493*3e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
3494*3e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
3495*3e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
3496*3e0aa8d8SJyri Sarha 
3497*3e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3498cb470087SKuninori Morimoto 			continue;
3499cb470087SKuninori Morimoto 
35006833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
350193b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
350293b0f3eeSJean-Francois Moine 							     args,
350393b0f3eeSJean-Francois Moine 							     dai_name);
35046833c452SKuninori Morimoto 		} else {
35056833c452SKuninori Morimoto 			int id = -1;
35066833c452SKuninori Morimoto 
350793b0f3eeSJean-Francois Moine 			switch (args->args_count) {
35086833c452SKuninori Morimoto 			case 0:
35096833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
35106833c452SKuninori Morimoto 				break;
35116833c452SKuninori Morimoto 			case 1:
351293b0f3eeSJean-Francois Moine 				id = args->args[0];
35136833c452SKuninori Morimoto 				break;
35146833c452SKuninori Morimoto 			default:
35156833c452SKuninori Morimoto 				/* not supported */
3516cb470087SKuninori Morimoto 				break;
3517cb470087SKuninori Morimoto 			}
3518cb470087SKuninori Morimoto 
35196833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
35206833c452SKuninori Morimoto 				ret = -EINVAL;
35213dcba280SNicolin Chen 				continue;
35226833c452SKuninori Morimoto 			}
3523e41975edSXiubo Li 
3524e41975edSXiubo Li 			ret = 0;
3525e41975edSXiubo Li 
3526e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3527e41975edSXiubo Li 			if (!*dai_name)
3528e41975edSXiubo Li 				*dai_name = pos->name;
35296833c452SKuninori Morimoto 		}
35306833c452SKuninori Morimoto 
3531cb470087SKuninori Morimoto 		break;
3532cb470087SKuninori Morimoto 	}
3533cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
353493b0f3eeSJean-Francois Moine 	return ret;
353593b0f3eeSJean-Francois Moine }
353693b0f3eeSJean-Francois Moine 
353793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
353893b0f3eeSJean-Francois Moine 			    const char **dai_name)
353993b0f3eeSJean-Francois Moine {
354093b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
354193b0f3eeSJean-Francois Moine 	int ret;
354293b0f3eeSJean-Francois Moine 
354393b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
354493b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
354593b0f3eeSJean-Francois Moine 	if (ret)
354693b0f3eeSJean-Francois Moine 		return ret;
354793b0f3eeSJean-Francois Moine 
354893b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3549cb470087SKuninori Morimoto 
3550cb470087SKuninori Morimoto 	of_node_put(args.np);
3551cb470087SKuninori Morimoto 
3552cb470087SKuninori Morimoto 	return ret;
3553cb470087SKuninori Morimoto }
3554cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3555cb470087SKuninori Morimoto 
355693b0f3eeSJean-Francois Moine /*
355793b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
355893b0f3eeSJean-Francois Moine  * @dev: Card device
355993b0f3eeSJean-Francois Moine  * @of_node: Device node
356093b0f3eeSJean-Francois Moine  * @dai_link: DAI link
356193b0f3eeSJean-Francois Moine  *
356293b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
356393b0f3eeSJean-Francois Moine  * 'sound-dai'.
356493b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
356593b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
356693b0f3eeSJean-Francois Moine  *
356793b0f3eeSJean-Francois Moine  * Returns 0 for success
356893b0f3eeSJean-Francois Moine  */
356993b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
357093b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
357193b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
357293b0f3eeSJean-Francois Moine {
357393b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
357493b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
357593b0f3eeSJean-Francois Moine 	char *name;
357693b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
357793b0f3eeSJean-Francois Moine 
357893b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
357993b0f3eeSJean-Francois Moine 	name = "sound-dai";
358093b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
358193b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
358293b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
358393b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
358493b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
358593b0f3eeSJean-Francois Moine 		else
358693b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
358793b0f3eeSJean-Francois Moine 		return num_codecs;
358893b0f3eeSJean-Francois Moine 	}
358993b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
359093b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
359193b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
359293b0f3eeSJean-Francois Moine 	if (!component)
359393b0f3eeSJean-Francois Moine 		return -ENOMEM;
359493b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
359593b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
359693b0f3eeSJean-Francois Moine 
359793b0f3eeSJean-Francois Moine 	/* Parse the list */
359893b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
359993b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
360093b0f3eeSJean-Francois Moine 	     index++, component++) {
360193b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
360293b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
360393b0f3eeSJean-Francois Moine 						  index, &args);
360493b0f3eeSJean-Francois Moine 		if (ret)
360593b0f3eeSJean-Francois Moine 			goto err;
360693b0f3eeSJean-Francois Moine 		component->of_node = args.np;
360793b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
360893b0f3eeSJean-Francois Moine 		if (ret < 0)
360993b0f3eeSJean-Francois Moine 			goto err;
361093b0f3eeSJean-Francois Moine 	}
361193b0f3eeSJean-Francois Moine 	return 0;
361293b0f3eeSJean-Francois Moine err:
361393b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
361493b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
361593b0f3eeSJean-Francois Moine 	     index++, component++) {
361693b0f3eeSJean-Francois Moine 		if (!component->of_node)
361793b0f3eeSJean-Francois Moine 			break;
361893b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
361993b0f3eeSJean-Francois Moine 		component->of_node = NULL;
362093b0f3eeSJean-Francois Moine 	}
362193b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
362293b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
362393b0f3eeSJean-Francois Moine 	return ret;
362493b0f3eeSJean-Francois Moine }
362593b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
362693b0f3eeSJean-Francois Moine 
3627c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3628db2a4165SFrank Mandarino {
36296553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3630fb257897SMark Brown 	snd_soc_util_init();
3631fb257897SMark Brown 
3632db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3633db2a4165SFrank Mandarino }
36344abe8e16SMark Brown module_init(snd_soc_init);
3635db2a4165SFrank Mandarino 
3636db2a4165SFrank Mandarino static void __exit snd_soc_exit(void)
3637db2a4165SFrank Mandarino {
3638fb257897SMark Brown 	snd_soc_util_exit();
36396553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3640fb257897SMark Brown 
3641db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
3642db2a4165SFrank Mandarino #endif
3643db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3644db2a4165SFrank Mandarino }
3645db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3646db2a4165SFrank Mandarino 
3647db2a4165SFrank Mandarino /* Module information */
3648db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3649db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3650db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3651db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3652