xref: /openbmc/linux/sound/soc/soc-core.c (revision 9e4980896c46ed84d0aa27382e18d1cacb7cb86e)
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 	char tmpbuf[len + 1];
9613fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
9713fd179fSDimitris Papastamos 
9813fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
9913fd179fSDimitris Papastamos 	 * try to abuse this function */
10013fd179fSDimitris Papastamos 	WARN_ON(len > 63);
10113fd179fSDimitris Papastamos 
10213fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10313fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10413fd179fSDimitris Papastamos 		return -EINVAL;
10513fd179fSDimitris Papastamos 
10613fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
10713fd179fSDimitris Papastamos 	if (ret < 0) {
10813fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
10913fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
11013fd179fSDimitris Papastamos 	} else {
11113fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
11213fd179fSDimitris Papastamos 	}
11313fd179fSDimitris Papastamos 
11413fd179fSDimitris Papastamos 	/* prepare the buffer */
11513fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
11613fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
11713fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
11813fd179fSDimitris Papastamos 
11913fd179fSDimitris Papastamos 	return 0;
12013fd179fSDimitris Papastamos }
12113fd179fSDimitris Papastamos 
12213fd179fSDimitris Papastamos /* codec register dump */
12313fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
12413fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
12513fd179fSDimitris Papastamos {
12613fd179fSDimitris Papastamos 	int i, step = 1;
1272bc9a81eSDimitris Papastamos 	int wordsize, regsize;
12813fd179fSDimitris Papastamos 	int len;
12913fd179fSDimitris Papastamos 	size_t total = 0;
13013fd179fSDimitris Papastamos 	loff_t p = 0;
1312bc9a81eSDimitris Papastamos 
13200b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
13300b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1342624d5faSMark Brown 
13513fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13613fd179fSDimitris Papastamos 
137f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1382624d5faSMark Brown 		return 0;
1392624d5faSMark Brown 
140f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
141f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1422624d5faSMark Brown 
143f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
14413fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
14513fd179fSDimitris Papastamos 		 * entries for the default case */
14613fd179fSDimitris Papastamos 		if (p >= pos) {
14713fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1482624d5faSMark Brown 				break;
14913fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
15013fd179fSDimitris Papastamos 			total += len;
15113fd179fSDimitris Papastamos 		}
15213fd179fSDimitris Papastamos 		p += len;
15313fd179fSDimitris Papastamos 	}
1542624d5faSMark Brown 
15513fd179fSDimitris Papastamos 	total = min(total, count - 1);
1562624d5faSMark Brown 
15713fd179fSDimitris Papastamos 	return total;
1582624d5faSMark Brown }
15913fd179fSDimitris Papastamos 
1602624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1612624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1622624d5faSMark Brown {
16336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
164f0fba2adSLiam Girdwood 
16513fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1662624d5faSMark Brown }
1672624d5faSMark Brown 
1682624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1692624d5faSMark Brown 
170dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
171dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
172dbe21408SMark Brown {
17336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
174dbe21408SMark Brown 
175f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
176dbe21408SMark Brown }
177dbe21408SMark Brown 
178dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
179dbe21408SMark Brown 			       struct device_attribute *attr,
180dbe21408SMark Brown 			       const char *buf, size_t count)
181dbe21408SMark Brown {
18236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183c593b520SMark Brown 	int ret;
184dbe21408SMark Brown 
185b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
186c593b520SMark Brown 	if (ret)
187c593b520SMark Brown 		return ret;
188dbe21408SMark Brown 
189dbe21408SMark Brown 	return count;
190dbe21408SMark Brown }
191dbe21408SMark Brown 
192dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
193dbe21408SMark Brown 
194d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
195d29697dcSTakashi Iwai 	&dev_attr_codec_reg.attr,
196d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
197d29697dcSTakashi Iwai 	NULL
198d29697dcSTakashi Iwai };
199d29697dcSTakashi Iwai 
200d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
201d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
202d29697dcSTakashi Iwai {
203d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
204d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
205d29697dcSTakashi Iwai 
206d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
207d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
208d29697dcSTakashi Iwai 	return rtd->codec ? attr->mode : 0; /* enabled only with codec */
209d29697dcSTakashi Iwai }
210d29697dcSTakashi Iwai 
211d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
212d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
213d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
214d29697dcSTakashi Iwai };
215d29697dcSTakashi Iwai 
216d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = {
217d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
218d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
219d29697dcSTakashi Iwai };
220d29697dcSTakashi Iwai 
221d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
222d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
223d29697dcSTakashi Iwai 	&soc_dev_roup,
224d29697dcSTakashi Iwai 	NULL
225d29697dcSTakashi Iwai };
226d29697dcSTakashi Iwai 
2272624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2282624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2292624d5faSMark Brown 				   size_t count, loff_t *ppos)
2302624d5faSMark Brown {
2312624d5faSMark Brown 	ssize_t ret;
2322624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
23313fd179fSDimitris Papastamos 	char *buf;
23413fd179fSDimitris Papastamos 
23513fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
23613fd179fSDimitris Papastamos 		return -EINVAL;
23713fd179fSDimitris Papastamos 
23813fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2392624d5faSMark Brown 	if (!buf)
2402624d5faSMark Brown 		return -ENOMEM;
24113fd179fSDimitris Papastamos 
24213fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
24313fd179fSDimitris Papastamos 	if (ret >= 0) {
24413fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
24513fd179fSDimitris Papastamos 			kfree(buf);
24613fd179fSDimitris Papastamos 			return -EFAULT;
24713fd179fSDimitris Papastamos 		}
24813fd179fSDimitris Papastamos 		*ppos += ret;
24913fd179fSDimitris Papastamos 	}
25013fd179fSDimitris Papastamos 
2512624d5faSMark Brown 	kfree(buf);
2522624d5faSMark Brown 	return ret;
2532624d5faSMark Brown }
2542624d5faSMark Brown 
2552624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2562624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2572624d5faSMark Brown {
2582624d5faSMark Brown 	char buf[32];
25934e268d8SStephen Boyd 	size_t buf_size;
2602624d5faSMark Brown 	char *start = buf;
2612624d5faSMark Brown 	unsigned long reg, value;
2622624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
263b785a492SJingoo Han 	int ret;
2642624d5faSMark Brown 
2652624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2662624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2672624d5faSMark Brown 		return -EFAULT;
2682624d5faSMark Brown 	buf[buf_size] = 0;
2692624d5faSMark Brown 
2702624d5faSMark Brown 	while (*start == ' ')
2712624d5faSMark Brown 		start++;
2722624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2732624d5faSMark Brown 	while (*start == ' ')
2742624d5faSMark Brown 		start++;
275b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
276b785a492SJingoo Han 	if (ret)
277b785a492SJingoo Han 		return ret;
2780d51a9cbSMark Brown 
2790d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
280373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2810d51a9cbSMark Brown 
282e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2832624d5faSMark Brown 	return buf_size;
2842624d5faSMark Brown }
2852624d5faSMark Brown 
2862624d5faSMark Brown static const struct file_operations codec_reg_fops = {
287234e3405SStephen Boyd 	.open = simple_open,
2882624d5faSMark Brown 	.read = codec_reg_read_file,
2892624d5faSMark Brown 	.write = codec_reg_write_file,
2906038f373SArnd Bergmann 	.llseek = default_llseek,
2912624d5faSMark Brown };
2922624d5faSMark Brown 
29381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
294e73f3de5SRussell King {
2956553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
2966553bf06SLars-Peter Clausen 		return;
2976553bf06SLars-Peter Clausen 
29881c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
29981c7cfd1SLars-Peter Clausen 		char *name;
300e73f3de5SRussell King 
30181c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
30281c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
30381c7cfd1SLars-Peter Clausen 		if (name) {
30481c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
30581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
30681c7cfd1SLars-Peter Clausen 			kfree(name);
30781c7cfd1SLars-Peter Clausen 		}
30881c7cfd1SLars-Peter Clausen 	} else {
30981c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
31081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
311e73f3de5SRussell King 	}
312e73f3de5SRussell King 
31381c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
31481c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
31581c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3162624d5faSMark Brown 		return;
3172624d5faSMark Brown 	}
3182624d5faSMark Brown 
31981c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
32081c7cfd1SLars-Peter Clausen 		component->debugfs_root);
32181c7cfd1SLars-Peter Clausen 
32281c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
32381c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
32481c7cfd1SLars-Peter Clausen }
32581c7cfd1SLars-Peter Clausen 
32681c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
32781c7cfd1SLars-Peter Clausen {
32881c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
32981c7cfd1SLars-Peter Clausen }
33081c7cfd1SLars-Peter Clausen 
33181c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
33281c7cfd1SLars-Peter Clausen {
33381c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
33481c7cfd1SLars-Peter Clausen 
3352624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
33681c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3372624d5faSMark Brown 						 codec, &codec_reg_fops);
3382624d5faSMark Brown 	if (!codec->debugfs_reg)
33910e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
34010e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
341731f1ab2SSebastien Guiriec }
342731f1ab2SSebastien Guiriec 
343c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
344c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
345c3c5a19aSMark Brown {
346c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3472b194f9dSMark Brown 	ssize_t len, ret = 0;
348c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
349c3c5a19aSMark Brown 
350c3c5a19aSMark Brown 	if (!buf)
351c3c5a19aSMark Brown 		return -ENOMEM;
352c3c5a19aSMark Brown 
35334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
35434e81ab4SLars-Peter Clausen 
3552b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3562b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
357f4333203SLars-Peter Clausen 			       codec->component.name);
3582b194f9dSMark Brown 		if (len >= 0)
3592b194f9dSMark Brown 			ret += len;
3602b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3612b194f9dSMark Brown 			ret = PAGE_SIZE;
3622b194f9dSMark Brown 			break;
3632b194f9dSMark Brown 		}
3642b194f9dSMark Brown 	}
365c3c5a19aSMark Brown 
36634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
36734e81ab4SLars-Peter Clausen 
368c3c5a19aSMark Brown 	if (ret >= 0)
369c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
370c3c5a19aSMark Brown 
371c3c5a19aSMark Brown 	kfree(buf);
372c3c5a19aSMark Brown 
373c3c5a19aSMark Brown 	return ret;
374c3c5a19aSMark Brown }
375c3c5a19aSMark Brown 
376c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
377c3c5a19aSMark Brown 	.read = codec_list_read_file,
378c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
379c3c5a19aSMark Brown };
380c3c5a19aSMark Brown 
381f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
382f3208780SMark Brown 				  size_t count, loff_t *ppos)
383f3208780SMark Brown {
384f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3852b194f9dSMark Brown 	ssize_t len, ret = 0;
3861438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
387f3208780SMark Brown 	struct snd_soc_dai *dai;
388f3208780SMark Brown 
389f3208780SMark Brown 	if (!buf)
390f3208780SMark Brown 		return -ENOMEM;
391f3208780SMark Brown 
39234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
39334e81ab4SLars-Peter Clausen 
3941438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3951438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3961438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3971438c2f6SLars-Peter Clausen 				dai->name);
3982b194f9dSMark Brown 			if (len >= 0)
3992b194f9dSMark Brown 				ret += len;
4002b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
4012b194f9dSMark Brown 				ret = PAGE_SIZE;
4022b194f9dSMark Brown 				break;
4032b194f9dSMark Brown 			}
4042b194f9dSMark Brown 		}
4051438c2f6SLars-Peter Clausen 	}
406f3208780SMark Brown 
40734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
40834e81ab4SLars-Peter Clausen 
409f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
410f3208780SMark Brown 
411f3208780SMark Brown 	kfree(buf);
412f3208780SMark Brown 
413f3208780SMark Brown 	return ret;
414f3208780SMark Brown }
415f3208780SMark Brown 
416f3208780SMark Brown static const struct file_operations dai_list_fops = {
417f3208780SMark Brown 	.read = dai_list_read_file,
418f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
419f3208780SMark Brown };
420f3208780SMark Brown 
42119c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
42219c7ac27SMark Brown 				       char __user *user_buf,
42319c7ac27SMark Brown 				       size_t count, loff_t *ppos)
42419c7ac27SMark Brown {
42519c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4262b194f9dSMark Brown 	ssize_t len, ret = 0;
42719c7ac27SMark Brown 	struct snd_soc_platform *platform;
42819c7ac27SMark Brown 
42919c7ac27SMark Brown 	if (!buf)
43019c7ac27SMark Brown 		return -ENOMEM;
43119c7ac27SMark Brown 
43234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
43334e81ab4SLars-Peter Clausen 
4342b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4352b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
436f4333203SLars-Peter Clausen 			       platform->component.name);
4372b194f9dSMark Brown 		if (len >= 0)
4382b194f9dSMark Brown 			ret += len;
4392b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4402b194f9dSMark Brown 			ret = PAGE_SIZE;
4412b194f9dSMark Brown 			break;
4422b194f9dSMark Brown 		}
4432b194f9dSMark Brown 	}
44419c7ac27SMark Brown 
44534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
44634e81ab4SLars-Peter Clausen 
44719c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
44819c7ac27SMark Brown 
44919c7ac27SMark Brown 	kfree(buf);
45019c7ac27SMark Brown 
45119c7ac27SMark Brown 	return ret;
45219c7ac27SMark Brown }
45319c7ac27SMark Brown 
45419c7ac27SMark Brown static const struct file_operations platform_list_fops = {
45519c7ac27SMark Brown 	.read = platform_list_read_file,
45619c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
45719c7ac27SMark Brown };
45819c7ac27SMark Brown 
459a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
460a6052154SJarkko Nikula {
4616553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
4626553bf06SLars-Peter Clausen 		return;
4636553bf06SLars-Peter Clausen 
464a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4658a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4663a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
467a6052154SJarkko Nikula 		dev_warn(card->dev,
4687c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4693a45b867SJarkko Nikula 		return;
4703a45b867SJarkko Nikula 	}
4713a45b867SJarkko Nikula 
4723a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4733a45b867SJarkko Nikula 						    card->debugfs_card_root,
4743a45b867SJarkko Nikula 						    &card->pop_time);
4753a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4763a45b867SJarkko Nikula 		dev_warn(card->dev,
477f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
478a6052154SJarkko Nikula }
479a6052154SJarkko Nikula 
480a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
481a6052154SJarkko Nikula {
482a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
483a6052154SJarkko Nikula }
484a6052154SJarkko Nikula 
4856553bf06SLars-Peter Clausen 
4866553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
4876553bf06SLars-Peter Clausen {
4886553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4896553bf06SLars-Peter Clausen 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4906553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
4916553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
4926553bf06SLars-Peter Clausen 		return;
4936553bf06SLars-Peter Clausen 	}
4946553bf06SLars-Peter Clausen 
4956553bf06SLars-Peter Clausen 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4966553bf06SLars-Peter Clausen 				 &codec_list_fops))
4976553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4986553bf06SLars-Peter Clausen 
4996553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
5006553bf06SLars-Peter Clausen 				 &dai_list_fops))
5016553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
5026553bf06SLars-Peter Clausen 
5036553bf06SLars-Peter Clausen 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
5046553bf06SLars-Peter Clausen 				 &platform_list_fops))
5056553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
5066553bf06SLars-Peter Clausen }
5076553bf06SLars-Peter Clausen 
5086553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
5096553bf06SLars-Peter Clausen {
5106553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
5116553bf06SLars-Peter Clausen }
5126553bf06SLars-Peter Clausen 
5132624d5faSMark Brown #else
5142624d5faSMark Brown 
51581c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
51681c7cfd1SLars-Peter Clausen 
51781c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
51881c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
5192624d5faSMark Brown {
5202624d5faSMark Brown }
5212624d5faSMark Brown 
52281c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
52381c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
524731f1ab2SSebastien Guiriec {
525731f1ab2SSebastien Guiriec }
526731f1ab2SSebastien Guiriec 
527b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
528b95fccbcSAxel Lin {
529b95fccbcSAxel Lin }
530b95fccbcSAxel Lin 
531b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
532b95fccbcSAxel Lin {
533b95fccbcSAxel Lin }
5346553bf06SLars-Peter Clausen 
5356553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
5366553bf06SLars-Peter Clausen {
5376553bf06SLars-Peter Clausen }
5386553bf06SLars-Peter Clausen 
5396553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
5406553bf06SLars-Peter Clausen {
5416553bf06SLars-Peter Clausen }
5426553bf06SLars-Peter Clausen 
5432624d5faSMark Brown #endif
5442624d5faSMark Brown 
54547c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
54647c88fffSLiam Girdwood 		const char *dai_link, int stream)
54747c88fffSLiam Girdwood {
54847c88fffSLiam Girdwood 	int i;
54947c88fffSLiam Girdwood 
55047c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
55147c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
55247c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
55347c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
55447c88fffSLiam Girdwood 	}
555f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
55647c88fffSLiam Girdwood 	return NULL;
55747c88fffSLiam Girdwood }
55847c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
55947c88fffSLiam Girdwood 
56047c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
56147c88fffSLiam Girdwood 		const char *dai_link)
56247c88fffSLiam Girdwood {
56347c88fffSLiam Girdwood 	int i;
56447c88fffSLiam Girdwood 
56547c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
56647c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
56747c88fffSLiam Girdwood 			return &card->rtd[i];
56847c88fffSLiam Girdwood 	}
569f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
57047c88fffSLiam Girdwood 	return NULL;
57147c88fffSLiam Girdwood }
57247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
57347c88fffSLiam Girdwood 
5749d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5759d58a077SRichard Fitzgerald {
5769d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5779d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5789d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5799d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5809d58a077SRichard Fitzgerald 	 */
5819d58a077SRichard Fitzgerald }
5829d58a077SRichard Fitzgerald 
5836f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
584db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5856f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
586db2a4165SFrank Mandarino {
5876f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5882eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
58988bd870fSBenoit Cousson 	int i, j;
590db2a4165SFrank Mandarino 
591c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
592c5599b87SLars-Peter Clausen 	if (!card->instantiated)
593e3509ff0SDaniel Mack 		return 0;
594e3509ff0SDaniel Mack 
5956ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5966ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5976ed25978SAndy Green 	 */
598f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
599f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
600f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
6016ed25978SAndy Green 
6026ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
603f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6046ed25978SAndy Green 
605a00f90f9SMark Brown 	/* mute any active DACs */
606f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6073efab7dcSMark Brown 
608f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6093efab7dcSMark Brown 			continue;
6103efab7dcSMark Brown 
61188bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
61288bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
61388bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
61488bd870fSBenoit Cousson 
615f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
616f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
617db2a4165SFrank Mandarino 		}
61888bd870fSBenoit Cousson 	}
619db2a4165SFrank Mandarino 
6204ccab3e7SLiam Girdwood 	/* suspend all pcms */
621f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
622f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6233efab7dcSMark Brown 			continue;
6243efab7dcSMark Brown 
625f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
6263efab7dcSMark Brown 	}
6274ccab3e7SLiam Girdwood 
62887506549SMark Brown 	if (card->suspend_pre)
62970b2ac12SMark Brown 		card->suspend_pre(card);
630db2a4165SFrank Mandarino 
631f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
632f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6333efab7dcSMark Brown 
634f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6353efab7dcSMark Brown 			continue;
6363efab7dcSMark Brown 
637bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
638f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
639db2a4165SFrank Mandarino 	}
640db2a4165SFrank Mandarino 
64137660b6dSLars-Peter Clausen 	/* close any waiting streams */
64237660b6dSLars-Peter Clausen 	for (i = 0; i < card->num_rtd; i++)
64343829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
644db2a4165SFrank Mandarino 
645f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6463efab7dcSMark Brown 
647f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6483efab7dcSMark Brown 			continue;
6493efab7dcSMark Brown 
6507bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6517bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
652db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
653f0fba2adSLiam Girdwood 
6547bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6557bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
656db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
657db2a4165SFrank Mandarino 	}
658db2a4165SFrank Mandarino 
6598be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6608be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
661e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
662e2d32ff6SMark Brown 
663f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6642eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6651547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6661547aba9SMark Brown 		 * bias _ON and should not be suspended. */
667a8093297SLars-Peter Clausen 		if (!codec->suspended) {
668ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6691547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
670125a25daSMark Brown 				/*
671125a25daSMark Brown 				 * If the CODEC is capable of idle
672125a25daSMark Brown 				 * bias off then being in STANDBY
673125a25daSMark Brown 				 * means it's doing something,
674125a25daSMark Brown 				 * otherwise fall through.
675125a25daSMark Brown 				 */
676125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
677125a25daSMark Brown 					dev_dbg(codec->dev,
67810e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
679125a25daSMark Brown 					break;
680125a25daSMark Brown 				}
681a8093297SLars-Peter Clausen 
6821547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
683a8093297SLars-Peter Clausen 				if (codec->driver->suspend)
68484b315eeSLars-Peter Clausen 					codec->driver->suspend(codec);
685f0fba2adSLiam Girdwood 				codec->suspended = 1;
686e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
687e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
688988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
689988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6901547aba9SMark Brown 				break;
6911547aba9SMark Brown 			default:
69210e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
69310e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6941547aba9SMark Brown 				break;
6951547aba9SMark Brown 			}
6961547aba9SMark Brown 		}
697f0fba2adSLiam Girdwood 	}
698db2a4165SFrank Mandarino 
699f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
700f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7013efab7dcSMark Brown 
702f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7033efab7dcSMark Brown 			continue;
7043efab7dcSMark Brown 
705bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
706f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
707988e8cc4SNicolin Chen 
708988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
709988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
710db2a4165SFrank Mandarino 	}
711db2a4165SFrank Mandarino 
71287506549SMark Brown 	if (card->suspend_post)
71370b2ac12SMark Brown 		card->suspend_post(card);
714db2a4165SFrank Mandarino 
715db2a4165SFrank Mandarino 	return 0;
716db2a4165SFrank Mandarino }
7176f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
718db2a4165SFrank Mandarino 
7196ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7206ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7216ed25978SAndy Green  */
7226ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
723db2a4165SFrank Mandarino {
724f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
725f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7262eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
72788bd870fSBenoit Cousson 	int i, j;
728db2a4165SFrank Mandarino 
7296ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7306ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7316ed25978SAndy Green 	 */
7326ed25978SAndy Green 
733f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7346ed25978SAndy Green 
7359949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
736f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7379949788bSMark Brown 
73887506549SMark Brown 	if (card->resume_pre)
73970b2ac12SMark Brown 		card->resume_pre(card);
740db2a4165SFrank Mandarino 
741bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
742f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
743f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7443efab7dcSMark Brown 
745f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7463efab7dcSMark Brown 			continue;
7473efab7dcSMark Brown 
748bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
749f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
750db2a4165SFrank Mandarino 	}
751db2a4165SFrank Mandarino 
7522eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7531547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7541547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7551547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7561547aba9SMark Brown 		 */
757a8093297SLars-Peter Clausen 		if (codec->suspended) {
758ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7591547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7601547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
761a8093297SLars-Peter Clausen 				if (codec->driver->resume)
762f0fba2adSLiam Girdwood 					codec->driver->resume(codec);
763f0fba2adSLiam Girdwood 				codec->suspended = 0;
7641547aba9SMark Brown 				break;
7651547aba9SMark Brown 			default:
76610e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
76710e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7681547aba9SMark Brown 				break;
7691547aba9SMark Brown 			}
7701547aba9SMark Brown 		}
771f0fba2adSLiam Girdwood 	}
772db2a4165SFrank Mandarino 
773f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7743efab7dcSMark Brown 
775f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7763efab7dcSMark Brown 			continue;
7773efab7dcSMark Brown 
7787bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
779d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
780db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
781f0fba2adSLiam Girdwood 
7827bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
783d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
784db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
785db2a4165SFrank Mandarino 	}
786db2a4165SFrank Mandarino 
7873ff3f64bSMark Brown 	/* unmute any active DACs */
788f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7893efab7dcSMark Brown 
790f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7913efab7dcSMark Brown 			continue;
7923efab7dcSMark Brown 
79388bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
79488bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
79588bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
79688bd870fSBenoit Cousson 
797f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
798f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
799db2a4165SFrank Mandarino 		}
80088bd870fSBenoit Cousson 	}
801db2a4165SFrank Mandarino 
802f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
803f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
8043efab7dcSMark Brown 
805f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
8063efab7dcSMark Brown 			continue;
8073efab7dcSMark Brown 
808bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
809f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
810db2a4165SFrank Mandarino 	}
811db2a4165SFrank Mandarino 
81287506549SMark Brown 	if (card->resume_post)
81370b2ac12SMark Brown 		card->resume_post(card);
814db2a4165SFrank Mandarino 
815f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8166ed25978SAndy Green 
8176ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
818f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
819e2d32ff6SMark Brown 
8208be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8218be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
822e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8236ed25978SAndy Green }
8246ed25978SAndy Green 
8256ed25978SAndy Green /* powers up audio subsystem after a suspend */
8266f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8276ed25978SAndy Green {
8286f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
829bc263214SLars-Peter Clausen 	bool bus_control = false;
830bc263214SLars-Peter Clausen 	int i;
831b9dd94a8SPeter Ujfalusi 
832c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
833c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8345ff1ddf2SEric Miao 		return 0;
8355ff1ddf2SEric Miao 
836988e8cc4SNicolin Chen 	/* activate pins from sleep state */
837988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
83888bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
83988bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
84088bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
84188bd870fSBenoit Cousson 		int j;
84288bd870fSBenoit Cousson 
843988e8cc4SNicolin Chen 		if (cpu_dai->active)
844988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
84588bd870fSBenoit Cousson 
84688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
84788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
848988e8cc4SNicolin Chen 			if (codec_dai->active)
849988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
850988e8cc4SNicolin Chen 		}
85188bd870fSBenoit Cousson 	}
852988e8cc4SNicolin Chen 
853bc263214SLars-Peter Clausen 	/*
854bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
855bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
856bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
85764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
85864ab9baaSMark Brown 	 */
859f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
860f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
861bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
86282e14e8bSStephen Warren 	}
863bc263214SLars-Peter Clausen 	if (bus_control) {
864bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
86564ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
86664ab9baaSMark Brown 	} else {
867f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8686308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
869f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
870f0fba2adSLiam Girdwood 	}
8716ed25978SAndy Green 
872db2a4165SFrank Mandarino 	return 0;
873db2a4165SFrank Mandarino }
8746f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
875db2a4165SFrank Mandarino #else
8766f8ab4acSMark Brown #define snd_soc_suspend NULL
8776f8ab4acSMark Brown #define snd_soc_resume NULL
878db2a4165SFrank Mandarino #endif
879db2a4165SFrank Mandarino 
88085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
88102a06d30SBarry Song };
88202a06d30SBarry Song 
88365d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
88465d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
88512023a9aSMisael Lopez Cruz {
88665d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
88712023a9aSMisael Lopez Cruz 
88834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
88934e81ab4SLars-Peter Clausen 
89065d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
89165d9361fSLars-Peter Clausen 		if (of_node) {
89265d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
89365d9361fSLars-Peter Clausen 				return component;
89465d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
89565d9361fSLars-Peter Clausen 			return component;
89612023a9aSMisael Lopez Cruz 		}
89712023a9aSMisael Lopez Cruz 	}
89812023a9aSMisael Lopez Cruz 
89912023a9aSMisael Lopez Cruz 	return NULL;
90012023a9aSMisael Lopez Cruz }
90112023a9aSMisael Lopez Cruz 
90214621c7eSLars-Peter Clausen static struct snd_soc_dai *snd_soc_find_dai(
90314621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
90412023a9aSMisael Lopez Cruz {
90514621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
90614621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
90712023a9aSMisael Lopez Cruz 
90834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
90934e81ab4SLars-Peter Clausen 
91014621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
91114621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
91214621c7eSLars-Peter Clausen 		if (dlc->of_node && component->dev->of_node != dlc->of_node)
91312023a9aSMisael Lopez Cruz 			continue;
9141ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
91512023a9aSMisael Lopez Cruz 			continue;
91614621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
91714621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
91814621c7eSLars-Peter Clausen 				continue;
91912023a9aSMisael Lopez Cruz 
92014621c7eSLars-Peter Clausen 			return dai;
92112023a9aSMisael Lopez Cruz 		}
92212023a9aSMisael Lopez Cruz 	}
92312023a9aSMisael Lopez Cruz 
92412023a9aSMisael Lopez Cruz 	return NULL;
92512023a9aSMisael Lopez Cruz }
92612023a9aSMisael Lopez Cruz 
927f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
928db2a4165SFrank Mandarino {
929f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
930f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
93188bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
93214621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
93388bd870fSBenoit Cousson 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
934435c5e25SMark Brown 	struct snd_soc_platform *platform;
935848dd8beSMark Brown 	const char *platform_name;
93688bd870fSBenoit Cousson 	int i;
937db2a4165SFrank Mandarino 
938f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
9396308419aSMark Brown 
94014621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
94114621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
94214621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
94314621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
944b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
945f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
946f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
947b19e6e7bSMark Brown 		return -EPROBE_DEFER;
948c5af3a2eSMark Brown 	}
949c5af3a2eSMark Brown 
95088bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
95188bd870fSBenoit Cousson 
95288bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
95388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
95414621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
95588bd870fSBenoit Cousson 		if (!codec_dais[i]) {
95612023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
95788bd870fSBenoit Cousson 				codecs[i].dai_name);
95812023a9aSMisael Lopez Cruz 			return -EPROBE_DEFER;
95912023a9aSMisael Lopez Cruz 		}
96088bd870fSBenoit Cousson 	}
96188bd870fSBenoit Cousson 
96288bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
96388bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
96488bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
96512023a9aSMisael Lopez Cruz 
966848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
967848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9685a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
969848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
970848dd8beSMark Brown 
971b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
972f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9735a504963SStephen Warren 		if (dai_link->platform_of_node) {
9745a504963SStephen Warren 			if (platform->dev->of_node !=
9755a504963SStephen Warren 			    dai_link->platform_of_node)
9765a504963SStephen Warren 				continue;
9775a504963SStephen Warren 		} else {
978f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
9792610ab77SStephen Warren 				continue;
9805a504963SStephen Warren 		}
9812610ab77SStephen Warren 
982f0fba2adSLiam Girdwood 		rtd->platform = platform;
983f0fba2adSLiam Girdwood 	}
984b19e6e7bSMark Brown 	if (!rtd->platform) {
985f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
986f0fba2adSLiam Girdwood 			dai_link->platform_name);
987b19e6e7bSMark Brown 		return -EPROBE_DEFER;
988f0fba2adSLiam Girdwood 	}
989b19e6e7bSMark Brown 
990b19e6e7bSMark Brown 	card->num_rtd++;
991b19e6e7bSMark Brown 
992b19e6e7bSMark Brown 	return 0;
9936b05eda6SMark Brown }
9946b05eda6SMark Brown 
995f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
996d12cd198SStephen Warren {
99770090bbbSLars-Peter Clausen 	if (!component->probed)
99870090bbbSLars-Peter Clausen 		return;
999d12cd198SStephen Warren 
1000f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1001f1d45cc3SLars-Peter Clausen 	if (component->codec)
1002f1d45cc3SLars-Peter Clausen 		list_del(&component->codec->card_list);
1003d12cd198SStephen Warren 
1004f1d45cc3SLars-Peter Clausen 	if (component->remove)
1005f1d45cc3SLars-Peter Clausen 		component->remove(component);
1006d12cd198SStephen Warren 
1007f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1008d12cd198SStephen Warren 
1009f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1010f1d45cc3SLars-Peter Clausen 	component->probed = 0;
1011f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1012d12cd198SStephen Warren }
1013d12cd198SStephen Warren 
1014e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1015589c3563SJarkko Nikula {
1016589c3563SJarkko Nikula 	int err;
1017589c3563SJarkko Nikula 
1018e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1019e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1020e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1021e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1022589c3563SJarkko Nikula 			if (err < 0)
1023e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1024b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1025e60cd14fSLars-Peter Clausen 					dai->name, err);
1026b0aa88afSMisael Lopez Cruz 		}
1027e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1028b0aa88afSMisael Lopez Cruz 	}
1029b0aa88afSMisael Lopez Cruz }
1030b0aa88afSMisael Lopez Cruz 
103162ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1032f0fba2adSLiam Girdwood {
1033f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1034e60cd14fSLars-Peter Clausen 	int i;
1035f0fba2adSLiam Girdwood 
1036f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1037f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
103836ae1a96SMark Brown 		device_unregister(rtd->dev);
1039f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
104002a06d30SBarry Song 	}
104102a06d30SBarry Song 
1042f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
104388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1044e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1045f0fba2adSLiam Girdwood 
1046e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1047f0fba2adSLiam Girdwood }
1048f0fba2adSLiam Girdwood 
104962ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
105062ae68faSStephen Warren 				       int order)
105162ae68faSStephen Warren {
105262ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
105362ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105462ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
105561aca564SLars-Peter Clausen 	struct snd_soc_component *component;
105688bd870fSBenoit Cousson 	int i;
105762ae68faSStephen Warren 
105862ae68faSStephen Warren 	/* remove the platform */
105970090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1060f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
106162ae68faSStephen Warren 
106262ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
106388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
106461aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
106570090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
106661aca564SLars-Peter Clausen 			soc_remove_component(component);
106762ae68faSStephen Warren 	}
106862ae68faSStephen Warren 
106962ae68faSStephen Warren 	/* remove any CPU-side CODEC */
107062ae68faSStephen Warren 	if (cpu_dai) {
107170090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
107261aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
107362ae68faSStephen Warren 	}
107462ae68faSStephen Warren }
107562ae68faSStephen Warren 
10760671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10770671fd8eSKuninori Morimoto {
10780168bf0dSLiam Girdwood 	int dai, order;
10790671fd8eSKuninori Morimoto 
10800168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10810168bf0dSLiam Girdwood 			order++) {
10820168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
108362ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10840168bf0dSLiam Girdwood 	}
108562ae68faSStephen Warren 
108662ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
108762ae68faSStephen Warren 			order++) {
108862ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
108962ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
109062ae68faSStephen Warren 	}
109162ae68faSStephen Warren 
10920671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10930671fd8eSKuninori Morimoto }
10940671fd8eSKuninori Morimoto 
1095ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
109694f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1097ead9b919SJarkko Nikula {
1098ead9b919SJarkko Nikula 	int i;
1099ead9b919SJarkko Nikula 
1100ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1101ead9b919SJarkko Nikula 		return;
1102ead9b919SJarkko Nikula 
1103ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1104ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
110594f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
11063ca041edSSebastian Reichel 			continue;
110794f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
11083ca041edSSebastian Reichel 			continue;
110994f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1110ead9b919SJarkko Nikula 		break;
1111ead9b919SJarkko Nikula 	}
1112ead9b919SJarkko Nikula }
1113ead9b919SJarkko Nikula 
1114f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1115f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1116589c3563SJarkko Nikula {
1117f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1118888df395SMark Brown 	struct snd_soc_dai *dai;
1119f1d45cc3SLars-Peter Clausen 	int ret;
1120589c3563SJarkko Nikula 
112170090bbbSLars-Peter Clausen 	if (component->probed)
112270090bbbSLars-Peter Clausen 		return 0;
1123589c3563SJarkko Nikula 
1124f1d45cc3SLars-Peter Clausen 	component->card = card;
1125f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1126f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1127589c3563SJarkko Nikula 
1128f1d45cc3SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
112970d29331SJarkko Nikula 		return -ENODEV;
113070d29331SJarkko Nikula 
1131f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1132d5d1e0beSLars-Peter Clausen 
1133f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1134f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1135f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
113677530150SLars-Peter Clausen 
1137b318ad50SNariman Poushin 		if (ret != 0) {
1138f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1139b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1140b318ad50SNariman Poushin 			goto err_probe;
1141b318ad50SNariman Poushin 		}
1142b318ad50SNariman Poushin 	}
1143b318ad50SNariman Poushin 
11440634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
11450634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1146261edc70SNariman Poushin 		if (ret != 0) {
1147f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1148261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1149261edc70SNariman Poushin 			goto err_probe;
1150261edc70SNariman Poushin 		}
1151261edc70SNariman Poushin 	}
1152888df395SMark Brown 
1153f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1154f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1155589c3563SJarkko Nikula 		if (ret < 0) {
1156f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1157f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
115870d29331SJarkko Nikula 			goto err_probe;
1159589c3563SJarkko Nikula 		}
1160cb2cf612SLiam Girdwood 
1161f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1162f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1163956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1164f1d45cc3SLars-Peter Clausen 			component->name);
1165956245e9SLiam Girdwood 	}
1166956245e9SLiam Girdwood 
1167f1d45cc3SLars-Peter Clausen 	if (component->controls)
1168f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1169f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1170f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1171f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1172f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1173956245e9SLiam Girdwood 
1174f1d45cc3SLars-Peter Clausen 	component->probed = 1;
1175f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1176f1d45cc3SLars-Peter Clausen 
1177f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1178f1d45cc3SLars-Peter Clausen 	if (component->codec)
1179f1d45cc3SLars-Peter Clausen 		list_add(&component->codec->card_list, &card->codec_dev_list);
1180956245e9SLiam Girdwood 
1181956245e9SLiam Girdwood 	return 0;
1182956245e9SLiam Girdwood 
1183956245e9SLiam Girdwood err_probe:
1184f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1185f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1186956245e9SLiam Girdwood 
1187956245e9SLiam Girdwood 	return ret;
1188956245e9SLiam Girdwood }
1189956245e9SLiam Girdwood 
119036ae1a96SMark Brown static void rtd_release(struct device *dev)
119136ae1a96SMark Brown {
119236ae1a96SMark Brown 	kfree(dev);
119336ae1a96SMark Brown }
1194f0fba2adSLiam Girdwood 
11955f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
11965f3484acSLars-Peter Clausen 	const char *name)
1197503ae5e0SMisael Lopez Cruz {
1198589c3563SJarkko Nikula 	int ret = 0;
1199589c3563SJarkko Nikula 
1200589c3563SJarkko Nikula 	/* register the rtd device */
120136ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
120236ae1a96SMark Brown 	if (!rtd->dev)
120336ae1a96SMark Brown 		return -ENOMEM;
120436ae1a96SMark Brown 	device_initialize(rtd->dev);
12055f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
120636ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1207d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1208f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
120936ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1210b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
121101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
121201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
121301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
121401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
121536ae1a96SMark Brown 	ret = device_add(rtd->dev);
1216589c3563SJarkko Nikula 	if (ret < 0) {
1217865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1218865df9cbSChuansheng Liu 		put_device(rtd->dev);
12195f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1220f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1221589c3563SJarkko Nikula 		return ret;
1222589c3563SJarkko Nikula 	}
1223589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1224589c3563SJarkko Nikula 	return 0;
1225589c3563SJarkko Nikula }
1226589c3563SJarkko Nikula 
122762ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
122862ae68faSStephen Warren 				     int order)
122962ae68faSStephen Warren {
123062ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
123162ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1232f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
123388bd870fSBenoit Cousson 	int i, ret;
123462ae68faSStephen Warren 
123562ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
123661aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
123770090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1238f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
123962ae68faSStephen Warren 		if (ret < 0)
124062ae68faSStephen Warren 			return ret;
124162ae68faSStephen Warren 	}
124262ae68faSStephen Warren 
124388bd870fSBenoit Cousson 	/* probe the CODEC-side components */
124488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
124561aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
124670090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1247f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
124862ae68faSStephen Warren 			if (ret < 0)
124962ae68faSStephen Warren 				return ret;
125062ae68faSStephen Warren 		}
125188bd870fSBenoit Cousson 	}
125262ae68faSStephen Warren 
125362ae68faSStephen Warren 	/* probe the platform */
125470090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1255f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
125662ae68faSStephen Warren 		if (ret < 0)
125762ae68faSStephen Warren 			return ret;
125862ae68faSStephen Warren 	}
125962ae68faSStephen Warren 
126062ae68faSStephen Warren 	return 0;
126162ae68faSStephen Warren }
126262ae68faSStephen Warren 
12638e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1264b0aa88afSMisael Lopez Cruz {
1265b0aa88afSMisael Lopez Cruz 	int ret;
1266b0aa88afSMisael Lopez Cruz 
12678e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
12688e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
12698e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1270b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
12718e2be562SLars-Peter Clausen 				dev_err(dai->dev,
12728e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
12738e2be562SLars-Peter Clausen 					dai->name, ret);
1274b0aa88afSMisael Lopez Cruz 				return ret;
1275b0aa88afSMisael Lopez Cruz 			}
1276b0aa88afSMisael Lopez Cruz 		}
1277b0aa88afSMisael Lopez Cruz 
12788e2be562SLars-Peter Clausen 		dai->probed = 1;
1279b0aa88afSMisael Lopez Cruz 	}
1280b0aa88afSMisael Lopez Cruz 
1281b0aa88afSMisael Lopez Cruz 	return 0;
1282b0aa88afSMisael Lopez Cruz }
1283b0aa88afSMisael Lopez Cruz 
12842436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
12852436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
12863f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
12872436a723SMisael Lopez Cruz {
12883f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
12893f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
12902436a723SMisael Lopez Cruz 	struct snd_soc_dapm_widget *play_w, *capture_w;
12912436a723SMisael Lopez Cruz 	int ret;
12922436a723SMisael Lopez Cruz 
129388bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
129488bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
129588bd870fSBenoit Cousson 
12962436a723SMisael Lopez Cruz 	/* link the DAI widgets */
12972436a723SMisael Lopez Cruz 	play_w = codec_dai->playback_widget;
12982436a723SMisael Lopez Cruz 	capture_w = cpu_dai->capture_widget;
12992436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
13002436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1301c6615082SNikesh Oswal 					   dai_link->num_params, capture_w,
1302c6615082SNikesh Oswal 					   play_w);
13032436a723SMisael Lopez Cruz 		if (ret != 0) {
13042436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
13052436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
13062436a723SMisael Lopez Cruz 			return ret;
13072436a723SMisael Lopez Cruz 		}
13082436a723SMisael Lopez Cruz 	}
13092436a723SMisael Lopez Cruz 
13102436a723SMisael Lopez Cruz 	play_w = cpu_dai->playback_widget;
13112436a723SMisael Lopez Cruz 	capture_w = codec_dai->capture_widget;
13122436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
13132436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1314c6615082SNikesh Oswal 					   dai_link->num_params, capture_w,
1315c6615082SNikesh Oswal 					   play_w);
13162436a723SMisael Lopez Cruz 		if (ret != 0) {
13172436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
13182436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
13192436a723SMisael Lopez Cruz 			return ret;
13202436a723SMisael Lopez Cruz 		}
13212436a723SMisael Lopez Cruz 	}
13222436a723SMisael Lopez Cruz 
13232436a723SMisael Lopez Cruz 	return 0;
13242436a723SMisael Lopez Cruz }
13252436a723SMisael Lopez Cruz 
132662ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1327f0fba2adSLiam Girdwood {
1328f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1329f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1330c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
133188bd870fSBenoit Cousson 	int i, ret;
1332f0fba2adSLiam Girdwood 
1333f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
13340168bf0dSLiam Girdwood 			card->name, num, order);
1335f0fba2adSLiam Girdwood 
1336f0fba2adSLiam Girdwood 	/* set default power off timeout */
1337f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1338f0fba2adSLiam Girdwood 
13398e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
13408e2be562SLars-Peter Clausen 	if (ret)
1341f0fba2adSLiam Girdwood 		return ret;
1342f0fba2adSLiam Girdwood 
1343f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
134488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
13458e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1346b0aa88afSMisael Lopez Cruz 		if (ret)
1347f0fba2adSLiam Girdwood 			return ret;
134888bd870fSBenoit Cousson 	}
1349f0fba2adSLiam Girdwood 
13500168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
13510168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
13520168bf0dSLiam Girdwood 		return 0;
13530168bf0dSLiam Girdwood 
13545f3484acSLars-Peter Clausen 	/* do machine specific initialization */
13555f3484acSLars-Peter Clausen 	if (dai_link->init) {
13565f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
13575f3484acSLars-Peter Clausen 		if (ret < 0) {
13585f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
13595f3484acSLars-Peter Clausen 				dai_link->name, ret);
13605f3484acSLars-Peter Clausen 			return ret;
13615f3484acSLars-Peter Clausen 		}
13625f3484acSLars-Peter Clausen 	}
13635f3484acSLars-Peter Clausen 
1364a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1365a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1366a5053a8eSKuninori Morimoto 
13675f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1368589c3563SJarkko Nikula 	if (ret)
1369f0fba2adSLiam Girdwood 		return ret;
1370f0fba2adSLiam Girdwood 
13715f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
13725f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
13732e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
13742e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
13755f3484acSLars-Peter Clausen #endif
13765f3484acSLars-Peter Clausen 
13771245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
13781245b700SNamarta Kohli 		/*create compress_device"*/
13791245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
13801245b700SNamarta Kohli 		if (ret < 0) {
1381f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
13821245b700SNamarta Kohli 					 dai_link->stream_name);
13831245b700SNamarta Kohli 			return ret;
13841245b700SNamarta Kohli 		}
13851245b700SNamarta Kohli 	} else {
13861245b700SNamarta Kohli 
1387c74184edSMark Brown 		if (!dai_link->params) {
1388f0fba2adSLiam Girdwood 			/* create the pcm */
1389f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1390f0fba2adSLiam Girdwood 			if (ret < 0) {
1391f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
13920837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1393f0fba2adSLiam Girdwood 				return ret;
1394f0fba2adSLiam Girdwood 			}
1395c74184edSMark Brown 		} else {
13969d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
13979d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
13989d58a077SRichard Fitzgerald 
1399c74184edSMark Brown 			/* link the DAI widgets */
14003f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
14012436a723SMisael Lopez Cruz 			if (ret)
1402c74184edSMark Brown 				return ret;
1403c74184edSMark Brown 		}
1404c74184edSMark Brown 	}
1405c74184edSMark Brown 
1406f0fba2adSLiam Girdwood 	return 0;
1407f0fba2adSLiam Girdwood }
1408f0fba2adSLiam Girdwood 
140944c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1410b19e6e7bSMark Brown {
141144c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14123ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
141365d9361fSLars-Peter Clausen 	const char *name = aux_dev->codec_name;
14143ca041edSSebastian Reichel 
141565d9361fSLars-Peter Clausen 	rtd->component = soc_find_component(aux_dev->codec_of_node, name);
141665d9361fSLars-Peter Clausen 	if (!rtd->component) {
14173ca041edSSebastian Reichel 		if (aux_dev->codec_of_node)
141865d9361fSLars-Peter Clausen 			name = of_node_full_name(aux_dev->codec_of_node);
14193ca041edSSebastian Reichel 
142065d9361fSLars-Peter Clausen 		dev_err(card->dev, "ASoC: %s not registered\n", name);
1421b19e6e7bSMark Brown 		return -EPROBE_DEFER;
1422b19e6e7bSMark Brown 	}
1423b19e6e7bSMark Brown 
142465d9361fSLars-Peter Clausen 	/*
142565d9361fSLars-Peter Clausen 	 * Some places still reference rtd->codec, so we have to keep that
142665d9361fSLars-Peter Clausen 	 * initialized if the component is a CODEC. Once all those references
142765d9361fSLars-Peter Clausen 	 * have been removed, this code can be removed as well.
142865d9361fSLars-Peter Clausen 	 */
142965d9361fSLars-Peter Clausen 	 rtd->codec = rtd->component->codec;
143065d9361fSLars-Peter Clausen 
143144c69bb1SLars-Peter Clausen 	return 0;
14323ca041edSSebastian Reichel }
14332eea392dSJarkko Nikula 
143444c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
143544c69bb1SLars-Peter Clausen {
143644c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14375f3484acSLars-Peter Clausen 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
143844c69bb1SLars-Peter Clausen 	int ret;
143944c69bb1SLars-Peter Clausen 
144065d9361fSLars-Peter Clausen 	ret = soc_probe_component(card, rtd->component);
14412eea392dSJarkko Nikula 	if (ret < 0)
1442589c3563SJarkko Nikula 		return ret;
14432eea392dSJarkko Nikula 
14445f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14455f3484acSLars-Peter Clausen 	if (aux_dev->init) {
144657bf7726SLars-Peter Clausen 		ret = aux_dev->init(rtd->component);
14475f3484acSLars-Peter Clausen 		if (ret < 0) {
14485f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14495f3484acSLars-Peter Clausen 				aux_dev->name, ret);
14502eea392dSJarkko Nikula 			return ret;
14512eea392dSJarkko Nikula 		}
14525f3484acSLars-Peter Clausen 	}
14535f3484acSLars-Peter Clausen 
14545f3484acSLars-Peter Clausen 	return soc_post_component_init(rtd, aux_dev->name);
14552eea392dSJarkko Nikula }
14562eea392dSJarkko Nikula 
14572eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
14582eea392dSJarkko Nikula {
14592eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
146065d9361fSLars-Peter Clausen 	struct snd_soc_component *component = rtd->component;
14612eea392dSJarkko Nikula 
14622eea392dSJarkko Nikula 	/* unregister the rtd device */
14632eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1464d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
14652eea392dSJarkko Nikula 		rtd->dev_registered = 0;
14662eea392dSJarkko Nikula 	}
14672eea392dSJarkko Nikula 
146865d9361fSLars-Peter Clausen 	if (component && component->probed)
146965d9361fSLars-Peter Clausen 		soc_remove_component(component);
14702eea392dSJarkko Nikula }
14712eea392dSJarkko Nikula 
1472f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1473fdf0f54dSDimitris Papastamos {
1474fdf0f54dSDimitris Papastamos 	int ret;
1475fdf0f54dSDimitris Papastamos 
1476fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1477fdf0f54dSDimitris Papastamos 		return 0;
1478fdf0f54dSDimitris Papastamos 
1479fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1480fdf0f54dSDimitris Papastamos 	if (ret < 0) {
148110e8aa9aSMichał Mirosław 		dev_err(codec->dev,
148210e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
148310e8aa9aSMichał Mirosław 			ret);
1484fdf0f54dSDimitris Papastamos 		return ret;
1485fdf0f54dSDimitris Papastamos 	}
1486fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1487fdf0f54dSDimitris Papastamos 	return 0;
1488fdf0f54dSDimitris Papastamos }
1489fdf0f54dSDimitris Papastamos 
1490ce64c8b9SLars-Peter Clausen /**
1491ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1492ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1493ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1494ce64c8b9SLars-Peter Clausen  *
1495ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1496ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1497ce64c8b9SLars-Peter Clausen  *
1498ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1499ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1500ce64c8b9SLars-Peter Clausen  *
1501ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1502ce64c8b9SLars-Peter Clausen  */
1503ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1504ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1505ce64c8b9SLars-Peter Clausen {
1506ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1507ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1508ce64c8b9SLars-Peter Clausen 	unsigned int i;
1509ce64c8b9SLars-Peter Clausen 	int ret;
1510ce64c8b9SLars-Peter Clausen 
1511ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1512ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1513ce64c8b9SLars-Peter Clausen 
1514ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1515ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1516ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1517ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1518ce64c8b9SLars-Peter Clausen 			return ret;
1519ce64c8b9SLars-Peter Clausen 		}
1520ce64c8b9SLars-Peter Clausen 	}
1521ce64c8b9SLars-Peter Clausen 
1522ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1523ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1524ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1525ce64c8b9SLars-Peter Clausen 
1526ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1527ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1528ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1529ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1530ce64c8b9SLars-Peter Clausen 			break;
1531ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1532ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1533ce64c8b9SLars-Peter Clausen 			break;
1534ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1535ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1536ce64c8b9SLars-Peter Clausen 			break;
1537ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1538ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1539ce64c8b9SLars-Peter Clausen 			break;
1540ce64c8b9SLars-Peter Clausen 		}
1541ce64c8b9SLars-Peter Clausen 
1542ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1543ce64c8b9SLars-Peter Clausen 	}
1544ce64c8b9SLars-Peter Clausen 
1545ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1546ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1547ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1548ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1549ce64c8b9SLars-Peter Clausen 		return ret;
1550ce64c8b9SLars-Peter Clausen 	}
1551ce64c8b9SLars-Peter Clausen 
1552ce64c8b9SLars-Peter Clausen 	return 0;
1553ce64c8b9SLars-Peter Clausen }
1554ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1555ce64c8b9SLars-Peter Clausen 
1556b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1557f0fba2adSLiam Girdwood {
1558fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1559ce64c8b9SLars-Peter Clausen 	int ret, i, order;
156096dd3622SMark Brown 
156134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
156201b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1563f0fba2adSLiam Girdwood 
1564b19e6e7bSMark Brown 	/* bind DAIs */
1565b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1566b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1567b19e6e7bSMark Brown 		if (ret != 0)
1568b19e6e7bSMark Brown 			goto base_error;
1569db2a4165SFrank Mandarino 	}
1570db2a4165SFrank Mandarino 
157144c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1572b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
157344c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1574b19e6e7bSMark Brown 		if (ret != 0)
1575b19e6e7bSMark Brown 			goto base_error;
1576db2a4165SFrank Mandarino 	}
1577db2a4165SFrank Mandarino 
1578fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1579fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1580fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1581fdf0f54dSDimitris Papastamos 			continue;
1582f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1583b19e6e7bSMark Brown 		if (ret < 0)
1584b19e6e7bSMark Brown 			goto base_error;
1585fdf0f54dSDimitris Papastamos 	}
1586fdf0f54dSDimitris Papastamos 
1587f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1588102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1589f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1590f0fba2adSLiam Girdwood 	if (ret < 0) {
159110e8aa9aSMichał Mirosław 		dev_err(card->dev,
159210e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
159310e8aa9aSMichał Mirosław 			card->name, ret);
1594b19e6e7bSMark Brown 		goto base_error;
1595db2a4165SFrank Mandarino 	}
1596db2a4165SFrank Mandarino 
15970757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
15980757d834SLars-Peter Clausen 
1599e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1600e37a4970SMark Brown 	card->dapm.dev = card->dev;
1601e37a4970SMark Brown 	card->dapm.card = card;
1602e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1603e37a4970SMark Brown 
1604d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1605d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1606d5d1e0beSLars-Peter Clausen #endif
1607d5d1e0beSLars-Peter Clausen 
160888ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
16096ed25978SAndy Green 	/* deferred resume work */
16106308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16111301a964SRandy Dunlap #endif
16126ed25978SAndy Green 
16139a841ebbSMark Brown 	if (card->dapm_widgets)
16149a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
16159a841ebbSMark Brown 					  card->num_dapm_widgets);
16169a841ebbSMark Brown 
1617f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
1618f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1619f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
1620f23e860eSNicolin Chen 
1621f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1622f0fba2adSLiam Girdwood 	if (card->probe) {
1623e7361ec4SMark Brown 		ret = card->probe(card);
1624f0fba2adSLiam Girdwood 		if (ret < 0)
1625f0fba2adSLiam Girdwood 			goto card_probe_error;
1626f0fba2adSLiam Girdwood 	}
1627f0fba2adSLiam Girdwood 
162862ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
16290168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
16300168bf0dSLiam Girdwood 			order++) {
1631fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
163262ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
163362ae68faSStephen Warren 			if (ret < 0) {
1634f110bfc7SLiam Girdwood 				dev_err(card->dev,
1635f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1636f110bfc7SLiam Girdwood 					ret);
163762ae68faSStephen Warren 				goto probe_dai_err;
163862ae68faSStephen Warren 			}
163962ae68faSStephen Warren 		}
164062ae68faSStephen Warren 	}
164162ae68faSStephen Warren 
164262ae68faSStephen Warren 	/* probe all DAI links on this card */
164362ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
164462ae68faSStephen Warren 			order++) {
164562ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
164662ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1647fe3e78e0SMark Brown 			if (ret < 0) {
1648f110bfc7SLiam Girdwood 				dev_err(card->dev,
1649f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1650f110bfc7SLiam Girdwood 					ret);
1651f0fba2adSLiam Girdwood 				goto probe_dai_err;
1652fe3e78e0SMark Brown 			}
1653fe3e78e0SMark Brown 		}
16540168bf0dSLiam Girdwood 	}
1655fe3e78e0SMark Brown 
16562eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
16572eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
16582eea392dSJarkko Nikula 		if (ret < 0) {
1659f110bfc7SLiam Girdwood 			dev_err(card->dev,
1660f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1661f110bfc7SLiam Girdwood 				ret);
16622eea392dSJarkko Nikula 			goto probe_aux_dev_err;
16632eea392dSJarkko Nikula 		}
16642eea392dSJarkko Nikula 	}
16652eea392dSJarkko Nikula 
1666888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1667b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1668888df395SMark Brown 
1669b7af1dafSMark Brown 	if (card->controls)
1670022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1671b7af1dafSMark Brown 
1672b8ad29deSMark Brown 	if (card->dapm_routes)
1673b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1674b8ad29deSMark Brown 					card->num_dapm_routes);
1675b8ad29deSMark Brown 
1676f23e860eSNicolin Chen 	if (card->of_dapm_routes)
1677f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1678f23e860eSNicolin Chen 					card->num_of_dapm_routes);
167975d9ac46SMark Brown 
1680f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1681fe3e78e0SMark Brown 		 "%s", card->name);
1682f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
168322de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1684f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1685f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1686f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1687f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1688f0e8ed85SMark Brown 		case '_':
1689f0e8ed85SMark Brown 		case '-':
1690f0e8ed85SMark Brown 		case '\0':
1691f0e8ed85SMark Brown 			break;
1692f0e8ed85SMark Brown 		default:
1693f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1694f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1695f0e8ed85SMark Brown 			break;
1696f0e8ed85SMark Brown 		}
1697f0e8ed85SMark Brown 	}
1698fe3e78e0SMark Brown 
169928e9ad92SMark Brown 	if (card->late_probe) {
170028e9ad92SMark Brown 		ret = card->late_probe(card);
170128e9ad92SMark Brown 		if (ret < 0) {
1702f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
170328e9ad92SMark Brown 				card->name, ret);
170428e9ad92SMark Brown 			goto probe_aux_dev_err;
170528e9ad92SMark Brown 		}
170628e9ad92SMark Brown 	}
170728e9ad92SMark Brown 
1708824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
17098c193b8dSLars-Peter Clausen 
1710f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1711fe3e78e0SMark Brown 	if (ret < 0) {
1712f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1713f110bfc7SLiam Girdwood 				ret);
17146b3ed785SAxel Lin 		goto probe_aux_dev_err;
1715fe3e78e0SMark Brown 	}
1716fe3e78e0SMark Brown 
1717435c5e25SMark Brown 	card->instantiated = 1;
17184f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1719f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
172034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
1721b19e6e7bSMark Brown 
1722b19e6e7bSMark Brown 	return 0;
1723db2a4165SFrank Mandarino 
17242eea392dSJarkko Nikula probe_aux_dev_err:
17252eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17262eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17272eea392dSJarkko Nikula 
1728f0fba2adSLiam Girdwood probe_dai_err:
17290671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1730fe3e78e0SMark Brown 
1731f0fba2adSLiam Girdwood card_probe_error:
173287506549SMark Brown 	if (card->remove)
1733e7361ec4SMark Brown 		card->remove(card);
1734f0fba2adSLiam Girdwood 
17350757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
1736f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1737f0fba2adSLiam Girdwood 
1738b19e6e7bSMark Brown base_error:
1739f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
174034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
1741db2a4165SFrank Mandarino 
1742b19e6e7bSMark Brown 	return ret;
1743435c5e25SMark Brown }
1744435c5e25SMark Brown 
1745435c5e25SMark Brown /* probes a new socdev */
1746435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1747435c5e25SMark Brown {
1748f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1749435c5e25SMark Brown 
175070a7ca34SVinod Koul 	/*
175170a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
175270a7ca34SVinod Koul 	 * we should not be here in that case so ret error
175370a7ca34SVinod Koul 	 */
175470a7ca34SVinod Koul 	if (!card)
175570a7ca34SVinod Koul 		return -EINVAL;
175670a7ca34SVinod Koul 
1757fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1758f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1759fe4085e8SMark Brown 		 card->name);
1760fe4085e8SMark Brown 
1761435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1762435c5e25SMark Brown 	card->dev = &pdev->dev;
1763f0fba2adSLiam Girdwood 
176428d528c8SMark Brown 	return snd_soc_register_card(card);
1765435c5e25SMark Brown }
1766435c5e25SMark Brown 
1767b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1768db2a4165SFrank Mandarino {
1769db2a4165SFrank Mandarino 	int i;
1770db2a4165SFrank Mandarino 
1771f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1772f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1773f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
177443829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1775db2a4165SFrank Mandarino 	}
1776db2a4165SFrank Mandarino 
17772eea392dSJarkko Nikula 	/* remove auxiliary devices */
17782eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17792eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17802eea392dSJarkko Nikula 
1781f0fba2adSLiam Girdwood 	/* remove and free each DAI */
17820671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1783f0fba2adSLiam Girdwood 
1784a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1785a6052154SJarkko Nikula 
1786f0fba2adSLiam Girdwood 	/* remove the card */
178787506549SMark Brown 	if (card->remove)
1788e7361ec4SMark Brown 		card->remove(card);
1789f0fba2adSLiam Girdwood 
17900aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
17910aaae527SLars-Peter Clausen 
1792f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1793b0e26485SVinod Koul 	return 0;
1794b0e26485SVinod Koul 
1795b2dfa62cSGuennadi Liakhovetski }
1796b0e26485SVinod Koul 
1797b0e26485SVinod Koul /* removes a socdev */
1798b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1799b0e26485SVinod Koul {
1800b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1801b0e26485SVinod Koul 
1802c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1803db2a4165SFrank Mandarino 	return 0;
1804db2a4165SFrank Mandarino }
1805db2a4165SFrank Mandarino 
18066f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
180751737470SMark Brown {
18086f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1809f0fba2adSLiam Girdwood 	int i;
181051737470SMark Brown 
181151737470SMark Brown 	if (!card->instantiated)
1812416356fcSMark Brown 		return 0;
181351737470SMark Brown 
181451737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
181551737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1816f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1817f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
181843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1819f0fba2adSLiam Girdwood 	}
182051737470SMark Brown 
1821f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1822416356fcSMark Brown 
1823988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
1824988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
182588bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
182688bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
182788bd870fSBenoit Cousson 		int j;
182888bd870fSBenoit Cousson 
1829988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
183088bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
183188bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
183288bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
183388bd870fSBenoit Cousson 		}
1834988e8cc4SNicolin Chen 	}
1835988e8cc4SNicolin Chen 
1836416356fcSMark Brown 	return 0;
183751737470SMark Brown }
18386f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
183951737470SMark Brown 
18406f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1841b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1842b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1843b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1844b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
18456f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1846b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1847416356fcSMark Brown };
1848deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1849416356fcSMark Brown 
1850db2a4165SFrank Mandarino /* ASoC platform driver */
1851db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1852db2a4165SFrank Mandarino 	.driver		= {
1853db2a4165SFrank Mandarino 		.name		= "soc-audio",
18546f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1855db2a4165SFrank Mandarino 	},
1856db2a4165SFrank Mandarino 	.probe		= soc_probe,
1857db2a4165SFrank Mandarino 	.remove		= soc_remove,
1858db2a4165SFrank Mandarino };
1859db2a4165SFrank Mandarino 
1860096e49d5SMark Brown /**
1861db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1862db2a4165SFrank Mandarino  * @_template: control template
1863db2a4165SFrank Mandarino  * @data: control private data
1864ac11a2b3SMark Brown  * @long_name: control long name
1865efb7ac3fSMark Brown  * @prefix: control name prefix
1866db2a4165SFrank Mandarino  *
1867db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1868db2a4165SFrank Mandarino  *
1869db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1870db2a4165SFrank Mandarino  */
1871db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
18723056557fSMark Brown 				  void *data, const char *long_name,
1873efb7ac3fSMark Brown 				  const char *prefix)
1874db2a4165SFrank Mandarino {
1875db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1876efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
1877efb7ac3fSMark Brown 	char *name = NULL;
1878db2a4165SFrank Mandarino 
1879db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1880db2a4165SFrank Mandarino 	template.index = 0;
1881db2a4165SFrank Mandarino 
1882efb7ac3fSMark Brown 	if (!long_name)
1883efb7ac3fSMark Brown 		long_name = template.name;
1884efb7ac3fSMark Brown 
1885efb7ac3fSMark Brown 	if (prefix) {
18862b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1887efb7ac3fSMark Brown 		if (!name)
1888efb7ac3fSMark Brown 			return NULL;
1889efb7ac3fSMark Brown 
1890efb7ac3fSMark Brown 		template.name = name;
1891efb7ac3fSMark Brown 	} else {
1892efb7ac3fSMark Brown 		template.name = long_name;
1893efb7ac3fSMark Brown 	}
1894efb7ac3fSMark Brown 
1895efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
1896efb7ac3fSMark Brown 
1897efb7ac3fSMark Brown 	kfree(name);
1898efb7ac3fSMark Brown 
1899efb7ac3fSMark Brown 	return kcontrol;
1900db2a4165SFrank Mandarino }
1901db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1902db2a4165SFrank Mandarino 
1903022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1904022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
1905022658beSLiam Girdwood 	const char *prefix, void *data)
1906022658beSLiam Girdwood {
1907022658beSLiam Girdwood 	int err, i;
1908022658beSLiam Girdwood 
1909022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
1910022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
1911022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
1912022658beSLiam Girdwood 						     control->name, prefix));
1913022658beSLiam Girdwood 		if (err < 0) {
1914f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
1915f110bfc7SLiam Girdwood 				control->name, err);
1916022658beSLiam Girdwood 			return err;
1917022658beSLiam Girdwood 		}
1918022658beSLiam Girdwood 	}
1919022658beSLiam Girdwood 
1920022658beSLiam Girdwood 	return 0;
1921022658beSLiam Girdwood }
1922022658beSLiam Girdwood 
19234fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
19244fefd698SDimitris Papastamos 					       const char *name)
19254fefd698SDimitris Papastamos {
19264fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
19274fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
19284fefd698SDimitris Papastamos 
19294fefd698SDimitris Papastamos 	if (unlikely(!name))
19304fefd698SDimitris Papastamos 		return NULL;
19314fefd698SDimitris Papastamos 
19324fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
19334fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
19344fefd698SDimitris Papastamos 			return kctl;
19354fefd698SDimitris Papastamos 	return NULL;
19364fefd698SDimitris Papastamos }
19374fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
19384fefd698SDimitris Papastamos 
1939db2a4165SFrank Mandarino /**
19400f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
19410f2780adSLars-Peter Clausen  *
19420f2780adSLars-Peter Clausen  * @component: Component to add controls to
19430f2780adSLars-Peter Clausen  * @controls: Array of controls to add
19440f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
19450f2780adSLars-Peter Clausen  *
19460f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
19470f2780adSLars-Peter Clausen  */
19480f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
19490f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
19500f2780adSLars-Peter Clausen {
19510f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
19520f2780adSLars-Peter Clausen 
19530f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
19540f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
19550f2780adSLars-Peter Clausen }
19560f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
19570f2780adSLars-Peter Clausen 
19580f2780adSLars-Peter Clausen /**
1959022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
1960022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
19613e8e1952SIan Molton  * duplicating this code.
19623e8e1952SIan Molton  *
19633e8e1952SIan Molton  * @codec: codec to add controls to
19643e8e1952SIan Molton  * @controls: array of controls to add
19653e8e1952SIan Molton  * @num_controls: number of elements in the array
19663e8e1952SIan Molton  *
19673e8e1952SIan Molton  * Return 0 for success, else error.
19683e8e1952SIan Molton  */
1969022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
19700f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
19713e8e1952SIan Molton {
19720f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
19730f2780adSLars-Peter Clausen 		num_controls);
19743e8e1952SIan Molton }
1975022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
19763e8e1952SIan Molton 
19773e8e1952SIan Molton /**
1978a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
1979022658beSLiam Girdwood  * Convenience function to add a list of controls.
1980a491a5c8SLiam Girdwood  *
1981a491a5c8SLiam Girdwood  * @platform: platform to add controls to
1982a491a5c8SLiam Girdwood  * @controls: array of controls to add
1983a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
1984a491a5c8SLiam Girdwood  *
1985a491a5c8SLiam Girdwood  * Return 0 for success, else error.
1986a491a5c8SLiam Girdwood  */
1987a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
19880f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
1989a491a5c8SLiam Girdwood {
19900f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
19910f2780adSLars-Peter Clausen 		num_controls);
1992a491a5c8SLiam Girdwood }
1993a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1994a491a5c8SLiam Girdwood 
1995a491a5c8SLiam Girdwood /**
1996022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
1997022658beSLiam Girdwood  * Convenience function to add a list of controls.
1998022658beSLiam Girdwood  *
1999022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2000022658beSLiam Girdwood  * @controls: array of controls to add
2001022658beSLiam Girdwood  * @num_controls: number of elements in the array
2002022658beSLiam Girdwood  *
2003022658beSLiam Girdwood  * Return 0 for success, else error.
2004022658beSLiam Girdwood  */
2005022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2006022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2007022658beSLiam Girdwood {
2008022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2009022658beSLiam Girdwood 
2010022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2011022658beSLiam Girdwood 			NULL, soc_card);
2012022658beSLiam Girdwood }
2013022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2014022658beSLiam Girdwood 
2015022658beSLiam Girdwood /**
2016022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2017022658beSLiam Girdwood  * Convienience function to add a list of controls.
2018022658beSLiam Girdwood  *
2019022658beSLiam Girdwood  * @dai: DAI to add controls to
2020022658beSLiam Girdwood  * @controls: array of controls to add
2021022658beSLiam Girdwood  * @num_controls: number of elements in the array
2022022658beSLiam Girdwood  *
2023022658beSLiam Girdwood  * Return 0 for success, else error.
2024022658beSLiam Girdwood  */
2025022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2026022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2027022658beSLiam Girdwood {
2028313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2029022658beSLiam Girdwood 
2030022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2031022658beSLiam Girdwood 			NULL, dai);
2032022658beSLiam Girdwood }
2033022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2034022658beSLiam Girdwood 
2035022658beSLiam Girdwood /**
20368c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
20378c6529dbSLiam Girdwood  * @dai: DAI
20388c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
20398c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
20408c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
20418c6529dbSLiam Girdwood  *
20428c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
20438c6529dbSLiam Girdwood  */
20448c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
20458c6529dbSLiam Girdwood 	unsigned int freq, int dir)
20468c6529dbSLiam Girdwood {
2047f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2048f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2049ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2050da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2051ec4ee52aSMark Brown 						      freq, dir);
20528c6529dbSLiam Girdwood 	else
20531104a9c8SMark Brown 		return -ENOTSUPP;
20548c6529dbSLiam Girdwood }
20558c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
20568c6529dbSLiam Girdwood 
20578c6529dbSLiam Girdwood /**
2058ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2059ec4ee52aSMark Brown  * @codec: CODEC
2060ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2061da1c6ea6SMark Brown  * @source: Source for the clock
2062ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2063ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2064ec4ee52aSMark Brown  *
2065ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2066ec4ee52aSMark Brown  */
2067ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2068da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2069ec4ee52aSMark Brown {
2070ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2071da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2072da1c6ea6SMark Brown 						 freq, dir);
2073ec4ee52aSMark Brown 	else
20741104a9c8SMark Brown 		return -ENOTSUPP;
2075ec4ee52aSMark Brown }
2076ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2077ec4ee52aSMark Brown 
2078ec4ee52aSMark Brown /**
20798c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
20808c6529dbSLiam Girdwood  * @dai: DAI
2081ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
20828c6529dbSLiam Girdwood  * @div: new clock divisor.
20838c6529dbSLiam Girdwood  *
20848c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
20858c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
20868c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
20878c6529dbSLiam Girdwood  */
20888c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
20898c6529dbSLiam Girdwood 	int div_id, int div)
20908c6529dbSLiam Girdwood {
2091f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2092f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
20938c6529dbSLiam Girdwood 	else
20948c6529dbSLiam Girdwood 		return -EINVAL;
20958c6529dbSLiam Girdwood }
20968c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
20978c6529dbSLiam Girdwood 
20988c6529dbSLiam Girdwood /**
20998c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
21008c6529dbSLiam Girdwood  * @dai: DAI
21018c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
210285488037SMark Brown  * @source: DAI specific source for the PLL
21038c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
21048c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
21058c6529dbSLiam Girdwood  *
21068c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
21078c6529dbSLiam Girdwood  */
210885488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
210985488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
21108c6529dbSLiam Girdwood {
2111f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2112f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
211385488037SMark Brown 					 freq_in, freq_out);
2114ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2115ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2116ec4ee52aSMark Brown 						   freq_in, freq_out);
21178c6529dbSLiam Girdwood 	else
21188c6529dbSLiam Girdwood 		return -EINVAL;
21198c6529dbSLiam Girdwood }
21208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
21218c6529dbSLiam Girdwood 
2122ec4ee52aSMark Brown /*
2123ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2124ec4ee52aSMark Brown  * @codec: CODEC
2125ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2126ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2127ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2128ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2129ec4ee52aSMark Brown  *
2130ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2131ec4ee52aSMark Brown  */
2132ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2133ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2134ec4ee52aSMark Brown {
2135ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2136ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2137ec4ee52aSMark Brown 					      freq_in, freq_out);
2138ec4ee52aSMark Brown 	else
2139ec4ee52aSMark Brown 		return -EINVAL;
2140ec4ee52aSMark Brown }
2141ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2142ec4ee52aSMark Brown 
21438c6529dbSLiam Girdwood /**
2144e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2145e54cf76bSLiam Girdwood  * @dai: DAI
2146e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
2147e54cf76bSLiam Girdwood  *
2148e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2149e54cf76bSLiam Girdwood  */
2150e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2151e54cf76bSLiam Girdwood {
2152e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2153e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2154e54cf76bSLiam Girdwood 	else
2155e54cf76bSLiam Girdwood 		return -EINVAL;
2156e54cf76bSLiam Girdwood }
2157e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2158e54cf76bSLiam Girdwood 
2159e54cf76bSLiam Girdwood /**
21608c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
21618c6529dbSLiam Girdwood  * @dai: DAI
21628c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
21638c6529dbSLiam Girdwood  *
21648c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
21658c6529dbSLiam Girdwood  */
21668c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
21678c6529dbSLiam Girdwood {
21685e4ba569SShawn Guo 	if (dai->driver == NULL)
21698c6529dbSLiam Girdwood 		return -EINVAL;
21705e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
21715e4ba569SShawn Guo 		return -ENOTSUPP;
21725e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
21738c6529dbSLiam Girdwood }
21748c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
21758c6529dbSLiam Girdwood 
21768c6529dbSLiam Girdwood /**
2177e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
217889c67857SXiubo Li  * @slots: Number of slots in use.
217989c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
218089c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
218189c67857SXiubo Li  *
218289c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
218389c67857SXiubo Li  */
2184e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
218589c67857SXiubo Li 					  unsigned int *tx_mask,
218689c67857SXiubo Li 					  unsigned int *rx_mask)
218789c67857SXiubo Li {
218889c67857SXiubo Li 	if (*tx_mask || *rx_mask)
218989c67857SXiubo Li 		return 0;
219089c67857SXiubo Li 
219189c67857SXiubo Li 	if (!slots)
219289c67857SXiubo Li 		return -EINVAL;
219389c67857SXiubo Li 
219489c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
219589c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
219689c67857SXiubo Li 
219789c67857SXiubo Li 	return 0;
219889c67857SXiubo Li }
219989c67857SXiubo Li 
220089c67857SXiubo Li /**
2201e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2202e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2203a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2204a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
22058c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2206a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
22078c6529dbSLiam Girdwood  *
2208e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2209e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2210e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2211e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2212e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2213e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2214e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2215e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2216e46c9366SLars-Peter Clausen  * channel and so on.
2217e46c9366SLars-Peter Clausen  *
2218e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2219e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2220e46c9366SLars-Peter Clausen  *
2221e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
22228c6529dbSLiam Girdwood  */
22238c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2224a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
22258c6529dbSLiam Girdwood {
2226e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2227e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
222889c67857SXiubo Li 						&tx_mask, &rx_mask);
222989c67857SXiubo Li 	else
2230e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
223189c67857SXiubo Li 
223288bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
223388bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
223488bd870fSBenoit Cousson 
2235f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2236f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2237a5479e38SDaniel Ribeiro 				slots, slot_width);
22388c6529dbSLiam Girdwood 	else
2239b2cbb6e1SXiubo Li 		return -ENOTSUPP;
22408c6529dbSLiam Girdwood }
22418c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
22428c6529dbSLiam Girdwood 
22438c6529dbSLiam Girdwood /**
2244472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2245472df3cbSBarry Song  * @dai: DAI
2246472df3cbSBarry Song  * @tx_num: how many TX channels
2247472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2248472df3cbSBarry Song  *           0~num-1 uses
2249472df3cbSBarry Song  * @rx_num: how many RX channels
2250472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2251472df3cbSBarry Song  *           0~num-1 uses
2252472df3cbSBarry Song  *
2253472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2254472df3cbSBarry Song  */
2255472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2256472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2257472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2258472df3cbSBarry Song {
2259f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2260f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2261472df3cbSBarry Song 			rx_num, rx_slot);
2262472df3cbSBarry Song 	else
2263472df3cbSBarry Song 		return -EINVAL;
2264472df3cbSBarry Song }
2265472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2266472df3cbSBarry Song 
2267472df3cbSBarry Song /**
22688c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
22698c6529dbSLiam Girdwood  * @dai: DAI
22708c6529dbSLiam Girdwood  * @tristate: tristate enable
22718c6529dbSLiam Girdwood  *
22728c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
22738c6529dbSLiam Girdwood  */
22748c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
22758c6529dbSLiam Girdwood {
2276f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2277f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
22788c6529dbSLiam Girdwood 	else
22798c6529dbSLiam Girdwood 		return -EINVAL;
22808c6529dbSLiam Girdwood }
22818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
22828c6529dbSLiam Girdwood 
22838c6529dbSLiam Girdwood /**
22848c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
22858c6529dbSLiam Girdwood  * @dai: DAI
22868c6529dbSLiam Girdwood  * @mute: mute enable
2287da18396fSMark Brown  * @direction: stream to mute
22888c6529dbSLiam Girdwood  *
22898c6529dbSLiam Girdwood  * Mutes the DAI DAC.
22908c6529dbSLiam Girdwood  */
2291da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2292da18396fSMark Brown 			     int direction)
22938c6529dbSLiam Girdwood {
2294da18396fSMark Brown 	if (!dai->driver)
2295da18396fSMark Brown 		return -ENOTSUPP;
2296da18396fSMark Brown 
2297da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2298da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2299da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2300da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2301f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
23028c6529dbSLiam Girdwood 	else
230304570c62SMark Brown 		return -ENOTSUPP;
23048c6529dbSLiam Girdwood }
23058c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
23068c6529dbSLiam Girdwood 
230788bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card,
230888bd870fSBenoit Cousson 				   struct snd_soc_dai_link *dai_link)
230988bd870fSBenoit Cousson {
231088bd870fSBenoit Cousson 	/* Legacy codec/codec_dai link is a single entry in multicodec */
231188bd870fSBenoit Cousson 	if (dai_link->codec_name || dai_link->codec_of_node ||
231288bd870fSBenoit Cousson 	    dai_link->codec_dai_name) {
231388bd870fSBenoit Cousson 		dai_link->num_codecs = 1;
231488bd870fSBenoit Cousson 
231588bd870fSBenoit Cousson 		dai_link->codecs = devm_kzalloc(card->dev,
231688bd870fSBenoit Cousson 				sizeof(struct snd_soc_dai_link_component),
231788bd870fSBenoit Cousson 				GFP_KERNEL);
231888bd870fSBenoit Cousson 		if (!dai_link->codecs)
231988bd870fSBenoit Cousson 			return -ENOMEM;
232088bd870fSBenoit Cousson 
232188bd870fSBenoit Cousson 		dai_link->codecs[0].name = dai_link->codec_name;
232288bd870fSBenoit Cousson 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
232388bd870fSBenoit Cousson 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
232488bd870fSBenoit Cousson 	}
232588bd870fSBenoit Cousson 
232688bd870fSBenoit Cousson 	if (!dai_link->codecs) {
232788bd870fSBenoit Cousson 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
232888bd870fSBenoit Cousson 		return -EINVAL;
232988bd870fSBenoit Cousson 	}
233088bd870fSBenoit Cousson 
233188bd870fSBenoit Cousson 	return 0;
233288bd870fSBenoit Cousson }
233388bd870fSBenoit Cousson 
2334c5af3a2eSMark Brown /**
2335c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2336c5af3a2eSMark Brown  *
2337ac11a2b3SMark Brown  * @card: Card to register
2338c5af3a2eSMark Brown  *
2339c5af3a2eSMark Brown  */
234070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2341c5af3a2eSMark Brown {
234288bd870fSBenoit Cousson 	int i, j, ret;
2343f0fba2adSLiam Girdwood 
2344c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2345c5af3a2eSMark Brown 		return -EINVAL;
2346c5af3a2eSMark Brown 
23475a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
23485a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
23495a504963SStephen Warren 
235088bd870fSBenoit Cousson 		ret = snd_soc_init_multicodec(card, link);
235188bd870fSBenoit Cousson 		if (ret) {
235288bd870fSBenoit Cousson 			dev_err(card->dev, "ASoC: failed to init multicodec\n");
235388bd870fSBenoit Cousson 			return ret;
235488bd870fSBenoit Cousson 		}
235588bd870fSBenoit Cousson 
235688bd870fSBenoit Cousson 		for (j = 0; j < link->num_codecs; j++) {
23575a504963SStephen Warren 			/*
23585a504963SStephen Warren 			 * Codec must be specified by 1 of name or OF node,
23595a504963SStephen Warren 			 * not both or neither.
23605a504963SStephen Warren 			 */
236188bd870fSBenoit Cousson 			if (!!link->codecs[j].name ==
236288bd870fSBenoit Cousson 			    !!link->codecs[j].of_node) {
236388bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
236410e8aa9aSMichał Mirosław 					link->name);
23655a504963SStephen Warren 				return -EINVAL;
23665a504963SStephen Warren 			}
2367bc92657aSStephen Warren 			/* Codec DAI name must be specified */
236888bd870fSBenoit Cousson 			if (!link->codecs[j].dai_name) {
236988bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
237010e8aa9aSMichał Mirosław 					link->name);
2371bc92657aSStephen Warren 				return -EINVAL;
2372bc92657aSStephen Warren 			}
237388bd870fSBenoit Cousson 		}
23745a504963SStephen Warren 
23755a504963SStephen Warren 		/*
23765a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
23775a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
23785a504963SStephen Warren 		 */
23795a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
238010e8aa9aSMichał Mirosław 			dev_err(card->dev,
238110e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
238210e8aa9aSMichał Mirosław 				link->name);
23835a504963SStephen Warren 			return -EINVAL;
23845a504963SStephen Warren 		}
23855a504963SStephen Warren 
23865a504963SStephen Warren 		/*
2387bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
2388bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
2389bc92657aSStephen Warren 		 * name alone..
23905a504963SStephen Warren 		 */
2391bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
239210e8aa9aSMichał Mirosław 			dev_err(card->dev,
239310e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
239410e8aa9aSMichał Mirosław 				link->name);
2395bc92657aSStephen Warren 			return -EINVAL;
2396bc92657aSStephen Warren 		}
2397bc92657aSStephen Warren 		/*
2398bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
2399bc92657aSStephen Warren 		 * specified
2400bc92657aSStephen Warren 		 */
2401bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
2402bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
240310e8aa9aSMichał Mirosław 			dev_err(card->dev,
240410e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
240510e8aa9aSMichał Mirosław 				link->name);
24065a504963SStephen Warren 			return -EINVAL;
24075a504963SStephen Warren 		}
24085a504963SStephen Warren 	}
24095a504963SStephen Warren 
2410ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2411ed77cc12SMark Brown 
2412111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2413111c6419SStephen Warren 
2414181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
2415181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
24162eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
2417f0fba2adSLiam Girdwood 				 GFP_KERNEL);
2418f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2419f0fba2adSLiam Girdwood 		return -ENOMEM;
2420a7dbb603SLiam Girdwood 	card->num_rtd = 0;
24212eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
2422f0fba2adSLiam Girdwood 
24235f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_links; i++) {
24245f3484acSLars-Peter Clausen 		card->rtd[i].card = card;
2425f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
242688bd870fSBenoit Cousson 		card->rtd[i].codec_dais = devm_kzalloc(card->dev,
242788bd870fSBenoit Cousson 					sizeof(struct snd_soc_dai *) *
242888bd870fSBenoit Cousson 					(card->rtd[i].dai_link->num_codecs),
242988bd870fSBenoit Cousson 					GFP_KERNEL);
243088bd870fSBenoit Cousson 		if (card->rtd[i].codec_dais == NULL)
243188bd870fSBenoit Cousson 			return -ENOMEM;
24325f3484acSLars-Peter Clausen 	}
24335f3484acSLars-Peter Clausen 
24345f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_aux_devs; i++)
24355f3484acSLars-Peter Clausen 		card->rtd_aux[i].card = card;
2436f0fba2adSLiam Girdwood 
2437db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
2438c5af3a2eSMark Brown 	card->instantiated = 0;
2439f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2440a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2441c5af3a2eSMark Brown 
2442b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2443b19e6e7bSMark Brown 	if (ret != 0)
24444e2576bdSKuninori Morimoto 		return ret;
2445c5af3a2eSMark Brown 
2446988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2447988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
244888bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
244988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
245088bd870fSBenoit Cousson 		int j;
245188bd870fSBenoit Cousson 
245288bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
245388bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2454988e8cc4SNicolin Chen 			if (!codec_dai->active)
2455988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
245688bd870fSBenoit Cousson 		}
245788bd870fSBenoit Cousson 
2458988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2459988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2460988e8cc4SNicolin Chen 	}
2461988e8cc4SNicolin Chen 
2462b19e6e7bSMark Brown 	return ret;
2463c5af3a2eSMark Brown }
246470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2465c5af3a2eSMark Brown 
2466c5af3a2eSMark Brown /**
2467c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2468c5af3a2eSMark Brown  *
2469ac11a2b3SMark Brown  * @card: Card to unregister
2470c5af3a2eSMark Brown  *
2471c5af3a2eSMark Brown  */
247270a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2473c5af3a2eSMark Brown {
247401e0df66SLars-Peter Clausen 	if (card->instantiated) {
247501e0df66SLars-Peter Clausen 		card->instantiated = false;
24761c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2477b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2478f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
24798f6f9b29SKuninori Morimoto 	}
2480c5af3a2eSMark Brown 
2481c5af3a2eSMark Brown 	return 0;
2482c5af3a2eSMark Brown }
248370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2484c5af3a2eSMark Brown 
2485f0fba2adSLiam Girdwood /*
2486f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2487f0fba2adSLiam Girdwood  * and sanitizing names.
2488f0fba2adSLiam Girdwood  */
24890b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2490f0fba2adSLiam Girdwood {
2491f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2492f0fba2adSLiam Girdwood 	int id1, id2;
2493f0fba2adSLiam Girdwood 
2494f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2495f0fba2adSLiam Girdwood 		return NULL;
2496f0fba2adSLiam Girdwood 
249758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2498f0fba2adSLiam Girdwood 
2499f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2500f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2501f0fba2adSLiam Girdwood 	if (found) {
2502f0fba2adSLiam Girdwood 		/* get ID */
2503f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2504f0fba2adSLiam Girdwood 
2505f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2506f0fba2adSLiam Girdwood 			if (*id == -1)
2507f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2508f0fba2adSLiam Girdwood 		}
2509f0fba2adSLiam Girdwood 
2510f0fba2adSLiam Girdwood 	} else {
2511f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2512f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2513f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2514f0fba2adSLiam Girdwood 
2515f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
251605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2517f0fba2adSLiam Girdwood 
2518f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2519f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
252058818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2521f0fba2adSLiam Girdwood 		} else
2522f0fba2adSLiam Girdwood 			*id = 0;
2523f0fba2adSLiam Girdwood 	}
2524f0fba2adSLiam Girdwood 
2525f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2526f0fba2adSLiam Girdwood }
2527f0fba2adSLiam Girdwood 
2528f0fba2adSLiam Girdwood /*
2529f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2530f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2531f0fba2adSLiam Girdwood  */
2532f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2533f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2534f0fba2adSLiam Girdwood {
2535f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
253610e8aa9aSMichał Mirosław 		dev_err(dev,
253710e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
253810e8aa9aSMichał Mirosław 			dev_name(dev));
2539f0fba2adSLiam Girdwood 		return NULL;
2540f0fba2adSLiam Girdwood 	}
2541f0fba2adSLiam Girdwood 
2542f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2543f0fba2adSLiam Girdwood }
2544f0fba2adSLiam Girdwood 
25459115171aSMark Brown /**
254632c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25479115171aSMark Brown  *
254832c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25499115171aSMark Brown  */
255032c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25519115171aSMark Brown {
25525c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25539115171aSMark Brown 
25545c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
255532c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
255632c9ba54SLars-Peter Clausen 			dai->name);
25579115171aSMark Brown 		list_del(&dai->list);
2558f0fba2adSLiam Girdwood 		kfree(dai->name);
2559f0fba2adSLiam Girdwood 		kfree(dai);
25609115171aSMark Brown 	}
256132c9ba54SLars-Peter Clausen }
25629115171aSMark Brown 
25639115171aSMark Brown /**
256432c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25659115171aSMark Brown  *
25666106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25676106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2568ac11a2b3SMark Brown  * @count: Number of DAIs
256932c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
257032c9ba54SLars-Peter Clausen  *                     parent's name.
25719115171aSMark Brown  */
257232c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2573bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2574bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
25759115171aSMark Brown {
25766106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2577f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
257832c9ba54SLars-Peter Clausen 	unsigned int i;
257932c9ba54SLars-Peter Clausen 	int ret;
2580f0fba2adSLiam Girdwood 
2581f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
25829115171aSMark Brown 
2583bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2584bb13109dSLars-Peter Clausen 	component->num_dai = count;
2585bb13109dSLars-Peter Clausen 
25869115171aSMark Brown 	for (i = 0; i < count; i++) {
2587f0fba2adSLiam Girdwood 
2588f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2589c46e0079SAxel Lin 		if (dai == NULL) {
2590c46e0079SAxel Lin 			ret = -ENOMEM;
2591c46e0079SAxel Lin 			goto err;
2592c46e0079SAxel Lin 		}
2593f0fba2adSLiam Girdwood 
259432c9ba54SLars-Peter Clausen 		/*
259532c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
259632c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
259732c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
259832c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
259932c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
260032c9ba54SLars-Peter Clausen 		 * component-less anymore.
260132c9ba54SLars-Peter Clausen 		 */
2602*9e498089SSrinivas Kandagatla 		if (count == 1 && legacy_dai_naming &&
2603*9e498089SSrinivas Kandagatla 			(dai_drv[i].id == 0 || dai_drv[i].name == NULL)) {
2604db2a4165SFrank Mandarino 			dai->name = fmt_single_name(dev, &dai->id);
260532c9ba54SLars-Peter Clausen 		} else {
2606f0fba2adSLiam Girdwood 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
260732c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
260832c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
260932c9ba54SLars-Peter Clausen 			else
261032c9ba54SLars-Peter Clausen 				dai->id = i;
261132c9ba54SLars-Peter Clausen 		}
2612f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
2613f0fba2adSLiam Girdwood 			kfree(dai);
261432c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
26159115171aSMark Brown 			goto err;
26169115171aSMark Brown 		}
26179115171aSMark Brown 
26186106d129SLars-Peter Clausen 		dai->component = component;
2619f0fba2adSLiam Girdwood 		dai->dev = dev;
2620f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
2621f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
2622f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
2623f0fba2adSLiam Girdwood 
26241438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
2625054880feSMark Brown 
26269115171aSMark Brown 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2627f0fba2adSLiam Girdwood 	}
2628f0fba2adSLiam Girdwood 
26299115171aSMark Brown 	return 0;
26309115171aSMark Brown 
26319115171aSMark Brown err:
263232c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26339115171aSMark Brown 
26349115171aSMark Brown 	return ret;
26359115171aSMark Brown }
26369115171aSMark Brown 
263714e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
263814e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2639d191bd8dSKuninori Morimoto {
264014e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2641d191bd8dSKuninori Morimoto 
264214e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
264314e8bdebSLars-Peter Clausen }
2644d191bd8dSKuninori Morimoto 
264514e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
264614e8bdebSLars-Peter Clausen 	int event)
264714e8bdebSLars-Peter Clausen {
264814e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
264914e8bdebSLars-Peter Clausen 
265014e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
265114e8bdebSLars-Peter Clausen }
265214e8bdebSLars-Peter Clausen 
2653bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2654bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2655d191bd8dSKuninori Morimoto {
2656ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2657ce0fc93aSLars-Peter Clausen 
2658bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2659bb13109dSLars-Peter Clausen 	if (!component->name) {
2660bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2661d191bd8dSKuninori Morimoto 		return -ENOMEM;
2662d191bd8dSKuninori Morimoto 	}
2663d191bd8dSKuninori Morimoto 
2664bb13109dSLars-Peter Clausen 	component->dev = dev;
2665bb13109dSLars-Peter Clausen 	component->driver = driver;
266661aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
266761aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
2668e2c330b9SLars-Peter Clausen 
2669ce0fc93aSLars-Peter Clausen 	if (!component->dapm_ptr)
2670ce0fc93aSLars-Peter Clausen 		component->dapm_ptr = &component->dapm;
2671ce0fc93aSLars-Peter Clausen 
2672ce0fc93aSLars-Peter Clausen 	dapm = component->dapm_ptr;
2673ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2674ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2675ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
26765819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
267714e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
267814e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
267914e8bdebSLars-Peter Clausen 	if (driver->stream_event)
268014e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2681ce0fc93aSLars-Peter Clausen 
268261aca564SLars-Peter Clausen 	component->controls = driver->controls;
268361aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
268461aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
268561aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
268661aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
268761aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
268861aca564SLars-Peter Clausen 
2689bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2690bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2691bb13109dSLars-Peter Clausen 
2692bb13109dSLars-Peter Clausen 	return 0;
2693d191bd8dSKuninori Morimoto }
2694d191bd8dSKuninori Morimoto 
269520feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2696886f5692SLars-Peter Clausen {
2697886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
269820feb881SLars-Peter Clausen 
2699886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2700886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2701886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2702886f5692SLars-Peter Clausen }
270320feb881SLars-Peter Clausen 
2704e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2705e874bf5fSLars-Peter Clausen 
270620feb881SLars-Peter Clausen /**
270720feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
270820feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
270920feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
271020feb881SLars-Peter Clausen  *
271120feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
271220feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
271320feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
271420feb881SLars-Peter Clausen  * before the first IO attempt of the component.
271520feb881SLars-Peter Clausen  */
271620feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
271720feb881SLars-Peter Clausen 	struct regmap *regmap)
271820feb881SLars-Peter Clausen {
271920feb881SLars-Peter Clausen 	component->regmap = regmap;
272020feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2721886f5692SLars-Peter Clausen }
272220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
272320feb881SLars-Peter Clausen 
272420feb881SLars-Peter Clausen /**
272520feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
272620feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
272720feb881SLars-Peter Clausen  *
272820feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
272920feb881SLars-Peter Clausen  * removes the regmap instance from the component.
273020feb881SLars-Peter Clausen  *
273120feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
273220feb881SLars-Peter Clausen  * to initialize the regmap instance.
273320feb881SLars-Peter Clausen  */
273420feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
273520feb881SLars-Peter Clausen {
273620feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
273720feb881SLars-Peter Clausen 	component->regmap = NULL;
273820feb881SLars-Peter Clausen }
273920feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2740886f5692SLars-Peter Clausen 
2741e874bf5fSLars-Peter Clausen #endif
2742d191bd8dSKuninori Morimoto 
2743bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2744bb13109dSLars-Peter Clausen {
274520feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
274620feb881SLars-Peter Clausen 		if (!component->regmap)
274720feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
274820feb881SLars-Peter Clausen 		if (component->regmap)
274920feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
275020feb881SLars-Peter Clausen 	}
2751886f5692SLars-Peter Clausen 
2752bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2753d191bd8dSKuninori Morimoto }
2754d191bd8dSKuninori Morimoto 
2755bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
2756bb13109dSLars-Peter Clausen {
2757d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
2758bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
2759d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2760bb13109dSLars-Peter Clausen }
2761d191bd8dSKuninori Morimoto 
2762bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2763bb13109dSLars-Peter Clausen {
2764bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2765bb13109dSLars-Peter Clausen 	kfree(component->name);
2766bb13109dSLars-Peter Clausen }
2767d191bd8dSKuninori Morimoto 
2768bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2769bb13109dSLars-Peter Clausen {
2770bb13109dSLars-Peter Clausen 	list_del(&component->list);
2771bb13109dSLars-Peter Clausen }
2772d191bd8dSKuninori Morimoto 
2773d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2774d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
2775d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
2776d191bd8dSKuninori Morimoto 			       int num_dai)
2777d191bd8dSKuninori Morimoto {
2778d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
2779bb13109dSLars-Peter Clausen 	int ret;
2780d191bd8dSKuninori Morimoto 
2781bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
2782d191bd8dSKuninori Morimoto 	if (!cmpnt) {
2783d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
2784d191bd8dSKuninori Morimoto 		return -ENOMEM;
2785d191bd8dSKuninori Morimoto 	}
2786d191bd8dSKuninori Morimoto 
2787bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2788bb13109dSLars-Peter Clausen 	if (ret)
2789bb13109dSLars-Peter Clausen 		goto err_free;
2790bb13109dSLars-Peter Clausen 
27913d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
279298e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
27933d59400fSLars-Peter Clausen 
2794bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2795bb13109dSLars-Peter Clausen 	if (ret < 0) {
2796f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2797bb13109dSLars-Peter Clausen 		goto err_cleanup;
2798bb13109dSLars-Peter Clausen 	}
2799bb13109dSLars-Peter Clausen 
2800bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
2801bb13109dSLars-Peter Clausen 
2802bb13109dSLars-Peter Clausen 	return 0;
2803bb13109dSLars-Peter Clausen 
2804bb13109dSLars-Peter Clausen err_cleanup:
2805bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
2806bb13109dSLars-Peter Clausen err_free:
2807bb13109dSLars-Peter Clausen 	kfree(cmpnt);
2808bb13109dSLars-Peter Clausen 	return ret;
2809d191bd8dSKuninori Morimoto }
2810d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2811d191bd8dSKuninori Morimoto 
2812d191bd8dSKuninori Morimoto /**
2813d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
2814d191bd8dSKuninori Morimoto  *
2815d191bd8dSKuninori Morimoto  */
2816d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
2817d191bd8dSKuninori Morimoto {
2818d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
2819d191bd8dSKuninori Morimoto 
282034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2821d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
282298e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
2823d191bd8dSKuninori Morimoto 			goto found;
2824d191bd8dSKuninori Morimoto 	}
282534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2826d191bd8dSKuninori Morimoto 	return;
2827d191bd8dSKuninori Morimoto 
2828d191bd8dSKuninori Morimoto found:
282934e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
283034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2831bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
2832bb13109dSLars-Peter Clausen 	kfree(cmpnt);
2833d191bd8dSKuninori Morimoto }
2834d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2835d191bd8dSKuninori Morimoto 
2836f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
2837e2c330b9SLars-Peter Clausen {
2838e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2839e2c330b9SLars-Peter Clausen 
2840f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
2841e2c330b9SLars-Peter Clausen }
2842e2c330b9SLars-Peter Clausen 
2843f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
2844e2c330b9SLars-Peter Clausen {
2845e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2846e2c330b9SLars-Peter Clausen 
2847f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
2848e2c330b9SLars-Peter Clausen }
2849e2c330b9SLars-Peter Clausen 
2850d191bd8dSKuninori Morimoto /**
285171a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
285271a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
285371a45cdaSLars-Peter Clausen  * @platform: The platform to add
285471a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
285512a48a8cSMark Brown  */
285671a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
2857d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
285812a48a8cSMark Brown {
2859b37f1d12SLars-Peter Clausen 	int ret;
2860b37f1d12SLars-Peter Clausen 
2861bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
2862bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
2863bb13109dSLars-Peter Clausen 	if (ret)
2864bb13109dSLars-Peter Clausen 		return ret;
2865f0fba2adSLiam Girdwood 
2866f0fba2adSLiam Girdwood 	platform->dev = dev;
2867f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
2868f1d45cc3SLars-Peter Clausen 
2869f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
2870f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
2871f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
2872f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
287312a48a8cSMark Brown 
287481c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
287581c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
287681c7cfd1SLars-Peter Clausen #endif
287712a48a8cSMark Brown 
287812a48a8cSMark Brown 	mutex_lock(&client_mutex);
2879bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
288012a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
288112a48a8cSMark Brown 	mutex_unlock(&client_mutex);
288212a48a8cSMark Brown 
2883f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2884f4333203SLars-Peter Clausen 		platform->component.name);
288512a48a8cSMark Brown 
288612a48a8cSMark Brown 	return 0;
288712a48a8cSMark Brown }
288871a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
288971a45cdaSLars-Peter Clausen 
289071a45cdaSLars-Peter Clausen /**
289171a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
289271a45cdaSLars-Peter Clausen  *
289371a45cdaSLars-Peter Clausen  * @platform: platform to register
289471a45cdaSLars-Peter Clausen  */
289571a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
289671a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
289771a45cdaSLars-Peter Clausen {
289871a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
289971a45cdaSLars-Peter Clausen 	int ret;
290071a45cdaSLars-Peter Clausen 
290171a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
290271a45cdaSLars-Peter Clausen 
290371a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
290471a45cdaSLars-Peter Clausen 	if (platform == NULL)
290571a45cdaSLars-Peter Clausen 		return -ENOMEM;
290671a45cdaSLars-Peter Clausen 
290771a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
290871a45cdaSLars-Peter Clausen 	if (ret)
290971a45cdaSLars-Peter Clausen 		kfree(platform);
291071a45cdaSLars-Peter Clausen 
291171a45cdaSLars-Peter Clausen 	return ret;
291271a45cdaSLars-Peter Clausen }
291312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
291412a48a8cSMark Brown 
291512a48a8cSMark Brown /**
291671a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
291771a45cdaSLars-Peter Clausen  * @platform: the platform to remove
291871a45cdaSLars-Peter Clausen  */
291971a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
292071a45cdaSLars-Peter Clausen {
2921b37f1d12SLars-Peter Clausen 
292271a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
292371a45cdaSLars-Peter Clausen 	list_del(&platform->list);
2924bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
292571a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
292671a45cdaSLars-Peter Clausen 
292771a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
2928f4333203SLars-Peter Clausen 		platform->component.name);
2929decc27b0SDaniel Mack 
2930decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
293171a45cdaSLars-Peter Clausen }
293271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
293371a45cdaSLars-Peter Clausen 
293471a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
293571a45cdaSLars-Peter Clausen {
293671a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
293771a45cdaSLars-Peter Clausen 
293834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
293971a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
294034e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
294134e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
294271a45cdaSLars-Peter Clausen 			return platform;
294371a45cdaSLars-Peter Clausen 		}
294434e81ab4SLars-Peter Clausen 	}
294534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
294671a45cdaSLars-Peter Clausen 
294771a45cdaSLars-Peter Clausen 	return NULL;
294871a45cdaSLars-Peter Clausen }
294971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
295071a45cdaSLars-Peter Clausen 
295171a45cdaSLars-Peter Clausen /**
295212a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
295312a48a8cSMark Brown  *
2954ac11a2b3SMark Brown  * @platform: platform to unregister
295512a48a8cSMark Brown  */
2956f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
295712a48a8cSMark Brown {
2958f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
2959f0fba2adSLiam Girdwood 
296071a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
296171a45cdaSLars-Peter Clausen 	if (!platform)
2962f0fba2adSLiam Girdwood 		return;
2963f0fba2adSLiam Girdwood 
296471a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
2965f0fba2adSLiam Girdwood 	kfree(platform);
296612a48a8cSMark Brown }
296712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
296812a48a8cSMark Brown 
2969151ab22cSMark Brown static u64 codec_format_map[] = {
2970151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2971151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2972151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2973151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2974151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2975151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2976151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2977151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2978151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2979151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2980151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2981151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2982151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2983151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2984151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2985151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2986151ab22cSMark Brown };
2987151ab22cSMark Brown 
2988151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2989151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2990151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2991151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2992151ab22cSMark Brown  */
2993151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2994151ab22cSMark Brown {
2995151ab22cSMark Brown 	int i;
2996151ab22cSMark Brown 
2997151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2998151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2999151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3000151ab22cSMark Brown }
3001151ab22cSMark Brown 
3002f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3003f1d45cc3SLars-Peter Clausen {
3004f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3005f1d45cc3SLars-Peter Clausen 
3006f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3007f1d45cc3SLars-Peter Clausen }
3008f1d45cc3SLars-Peter Clausen 
3009f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3010f1d45cc3SLars-Peter Clausen {
3011f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3012f1d45cc3SLars-Peter Clausen 
3013f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3014f1d45cc3SLars-Peter Clausen }
3015f1d45cc3SLars-Peter Clausen 
3016e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3017e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3018e2c330b9SLars-Peter Clausen {
3019e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3020e2c330b9SLars-Peter Clausen 
3021e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3022e2c330b9SLars-Peter Clausen }
3023e2c330b9SLars-Peter Clausen 
3024e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3025e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3026e2c330b9SLars-Peter Clausen {
3027e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3028e2c330b9SLars-Peter Clausen 
3029e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3030e2c330b9SLars-Peter Clausen 
3031e2c330b9SLars-Peter Clausen 	return 0;
3032e2c330b9SLars-Peter Clausen }
3033e2c330b9SLars-Peter Clausen 
303468f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
303568f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
303668f831c2SLars-Peter Clausen {
303768f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
303868f831c2SLars-Peter Clausen 
303968f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
304068f831c2SLars-Peter Clausen }
304168f831c2SLars-Peter Clausen 
30420d0cf00aSMark Brown /**
30430d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
30440d0cf00aSMark Brown  *
3045ac11a2b3SMark Brown  * @codec: codec to register
30460d0cf00aSMark Brown  */
3047f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3048001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3049001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3050001ae4c0SMark Brown 			   int num_dai)
30510d0cf00aSMark Brown {
3052f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3053bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3054f0fba2adSLiam Girdwood 	int ret, i;
3055151ab22cSMark Brown 
3056f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
30570d0cf00aSMark Brown 
3058f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3059f0fba2adSLiam Girdwood 	if (codec == NULL)
3060f0fba2adSLiam Girdwood 		return -ENOMEM;
30610d0cf00aSMark Brown 
3062ce0fc93aSLars-Peter Clausen 	codec->component.dapm_ptr = &codec->dapm;
3063f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3064ce0fc93aSLars-Peter Clausen 
3065bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3066bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3067bb13109dSLars-Peter Clausen 	if (ret)
3068bb13109dSLars-Peter Clausen 		goto err_free;
3069151ab22cSMark Brown 
3070f1d45cc3SLars-Peter Clausen 	if (codec_drv->controls) {
3071f1d45cc3SLars-Peter Clausen 		codec->component.controls = codec_drv->controls;
3072f1d45cc3SLars-Peter Clausen 		codec->component.num_controls = codec_drv->num_controls;
3073f1d45cc3SLars-Peter Clausen 	}
3074f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_widgets) {
3075f1d45cc3SLars-Peter Clausen 		codec->component.dapm_widgets = codec_drv->dapm_widgets;
3076f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3077f1d45cc3SLars-Peter Clausen 	}
3078f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_routes) {
3079f1d45cc3SLars-Peter Clausen 		codec->component.dapm_routes = codec_drv->dapm_routes;
3080f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3081f1d45cc3SLars-Peter Clausen 	}
3082f1d45cc3SLars-Peter Clausen 
3083f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3084f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3085f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3086f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
3087e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3088e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3089e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3090e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
30913d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
30925819c2faSLars-Peter Clausen 	codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
309386dbf2acSLars-Peter Clausen 	codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
309414e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
3095474b62d6SMark Brown 		codec->dapm.seq_notifier = codec_drv->seq_notifier;
309668f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
309768f831c2SLars-Peter Clausen 		codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3098f0fba2adSLiam Girdwood 	codec->dev = dev;
3099f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3100e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3101f0fba2adSLiam Girdwood 
310281c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
310381c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
310481c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
310581c7cfd1SLars-Peter Clausen #endif
3106a39f75f7SXiubo Li 
3107a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3108886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3109a39f75f7SXiubo Li 
3110f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3111f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3112f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3113f0fba2adSLiam Girdwood 	}
3114f0fba2adSLiam Girdwood 
3115bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3116bb13109dSLars-Peter Clausen 	if (ret < 0) {
3117f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3118bb13109dSLars-Peter Clausen 		goto err_cleanup;
3119bb13109dSLars-Peter Clausen 	}
3120bb13109dSLars-Peter Clausen 
3121bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3122bb13109dSLars-Peter Clausen 		dai->codec = codec;
3123bb13109dSLars-Peter Clausen 
3124054880feSMark Brown 	mutex_lock(&client_mutex);
3125bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3126054880feSMark Brown 	list_add(&codec->list, &codec_list);
3127054880feSMark Brown 	mutex_unlock(&client_mutex);
3128054880feSMark Brown 
3129f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3130f4333203SLars-Peter Clausen 		codec->component.name);
31310d0cf00aSMark Brown 	return 0;
3132f0fba2adSLiam Girdwood 
3133bb13109dSLars-Peter Clausen err_cleanup:
3134bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3135bb13109dSLars-Peter Clausen err_free:
3136f0fba2adSLiam Girdwood 	kfree(codec);
3137f0fba2adSLiam Girdwood 	return ret;
31380d0cf00aSMark Brown }
31390d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
31400d0cf00aSMark Brown 
31410d0cf00aSMark Brown /**
31420d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
31430d0cf00aSMark Brown  *
3144ac11a2b3SMark Brown  * @codec: codec to unregister
31450d0cf00aSMark Brown  */
3146f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
31470d0cf00aSMark Brown {
3148f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3149f0fba2adSLiam Girdwood 
315034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3151f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3152f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3153f0fba2adSLiam Girdwood 			goto found;
3154f0fba2adSLiam Girdwood 	}
315534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3156f0fba2adSLiam Girdwood 	return;
3157f0fba2adSLiam Girdwood 
3158f0fba2adSLiam Girdwood found:
31590d0cf00aSMark Brown 	list_del(&codec->list);
3160bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
31610d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
31620d0cf00aSMark Brown 
3163f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3164f4333203SLars-Peter Clausen 			codec->component.name);
3165f0fba2adSLiam Girdwood 
3166bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
31677a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3168f0fba2adSLiam Girdwood 	kfree(codec);
31690d0cf00aSMark Brown }
31700d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
31710d0cf00aSMark Brown 
3172bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3173bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3174bec4fa05SStephen Warren 			       const char *propname)
3175bec4fa05SStephen Warren {
31767e07e7c0STushar Behera 	struct device_node *np;
3177bec4fa05SStephen Warren 	int ret;
3178bec4fa05SStephen Warren 
31797e07e7c0STushar Behera 	if (!card->dev) {
31807e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
31817e07e7c0STushar Behera 		return -EINVAL;
31827e07e7c0STushar Behera 	}
31837e07e7c0STushar Behera 
31847e07e7c0STushar Behera 	np = card->dev->of_node;
31857e07e7c0STushar Behera 
3186bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3187bec4fa05SStephen Warren 	/*
3188bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3189bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3190bec4fa05SStephen Warren 	 * snd_soc_register_card.
3191bec4fa05SStephen Warren 	 */
3192bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3193bec4fa05SStephen Warren 		dev_err(card->dev,
3194f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3195bec4fa05SStephen Warren 			propname, ret);
3196bec4fa05SStephen Warren 		return ret;
3197bec4fa05SStephen Warren 	}
3198bec4fa05SStephen Warren 
3199bec4fa05SStephen Warren 	return 0;
3200bec4fa05SStephen Warren }
3201bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3202bec4fa05SStephen Warren 
32039a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
32049a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
32059a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
32069a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
32079a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
32089a6d4860SXiubo Li };
32099a6d4860SXiubo Li 
32109a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
32119a6d4860SXiubo Li 					  const char *propname)
32129a6d4860SXiubo Li {
32139a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
32149a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
32159a6d4860SXiubo Li 	const char *template, *wname;
32169a6d4860SXiubo Li 	int i, j, num_widgets, ret;
32179a6d4860SXiubo Li 
32189a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
32199a6d4860SXiubo Li 	if (num_widgets < 0) {
32209a6d4860SXiubo Li 		dev_err(card->dev,
32219a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
32229a6d4860SXiubo Li 		return -EINVAL;
32239a6d4860SXiubo Li 	}
32249a6d4860SXiubo Li 	if (num_widgets & 1) {
32259a6d4860SXiubo Li 		dev_err(card->dev,
32269a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
32279a6d4860SXiubo Li 		return -EINVAL;
32289a6d4860SXiubo Li 	}
32299a6d4860SXiubo Li 
32309a6d4860SXiubo Li 	num_widgets /= 2;
32319a6d4860SXiubo Li 	if (!num_widgets) {
32329a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
32339a6d4860SXiubo Li 			propname);
32349a6d4860SXiubo Li 		return -EINVAL;
32359a6d4860SXiubo Li 	}
32369a6d4860SXiubo Li 
32379a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
32389a6d4860SXiubo Li 			       GFP_KERNEL);
32399a6d4860SXiubo Li 	if (!widgets) {
32409a6d4860SXiubo Li 		dev_err(card->dev,
32419a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
32429a6d4860SXiubo Li 		return -ENOMEM;
32439a6d4860SXiubo Li 	}
32449a6d4860SXiubo Li 
32459a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
32469a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
32479a6d4860SXiubo Li 			2 * i, &template);
32489a6d4860SXiubo Li 		if (ret) {
32499a6d4860SXiubo Li 			dev_err(card->dev,
32509a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
32519a6d4860SXiubo Li 				propname, 2 * i, ret);
32529a6d4860SXiubo Li 			return -EINVAL;
32539a6d4860SXiubo Li 		}
32549a6d4860SXiubo Li 
32559a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
32569a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
32579a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
32589a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
32599a6d4860SXiubo Li 				break;
32609a6d4860SXiubo Li 			}
32619a6d4860SXiubo Li 		}
32629a6d4860SXiubo Li 
32639a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
32649a6d4860SXiubo Li 			dev_err(card->dev,
32659a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
32669a6d4860SXiubo Li 				template);
32679a6d4860SXiubo Li 			return -EINVAL;
32689a6d4860SXiubo Li 		}
32699a6d4860SXiubo Li 
32709a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
32719a6d4860SXiubo Li 						    (2 * i) + 1,
32729a6d4860SXiubo Li 						    &wname);
32739a6d4860SXiubo Li 		if (ret) {
32749a6d4860SXiubo Li 			dev_err(card->dev,
32759a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
32769a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
32779a6d4860SXiubo Li 			return -EINVAL;
32789a6d4860SXiubo Li 		}
32799a6d4860SXiubo Li 
32809a6d4860SXiubo Li 		widgets[i].name = wname;
32819a6d4860SXiubo Li 	}
32829a6d4860SXiubo Li 
3283f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3284f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
32859a6d4860SXiubo Li 
32869a6d4860SXiubo Li 	return 0;
32879a6d4860SXiubo Li }
32889a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
32899a6d4860SXiubo Li 
329089c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
329189c67857SXiubo Li 			      unsigned int *slots,
329289c67857SXiubo Li 			      unsigned int *slot_width)
329389c67857SXiubo Li {
329489c67857SXiubo Li 	u32 val;
329589c67857SXiubo Li 	int ret;
329689c67857SXiubo Li 
329789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
329889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
329989c67857SXiubo Li 		if (ret)
330089c67857SXiubo Li 			return ret;
330189c67857SXiubo Li 
330289c67857SXiubo Li 		if (slots)
330389c67857SXiubo Li 			*slots = val;
330489c67857SXiubo Li 	}
330589c67857SXiubo Li 
330689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
330789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
330889c67857SXiubo Li 		if (ret)
330989c67857SXiubo Li 			return ret;
331089c67857SXiubo Li 
331189c67857SXiubo Li 		if (slot_width)
331289c67857SXiubo Li 			*slot_width = val;
331389c67857SXiubo Li 	}
331489c67857SXiubo Li 
331589c67857SXiubo Li 	return 0;
331689c67857SXiubo Li }
331789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
331889c67857SXiubo Li 
3319a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3320a4a54dd5SStephen Warren 				   const char *propname)
3321a4a54dd5SStephen Warren {
3322a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3323e3b1e6a1SMark Brown 	int num_routes;
3324a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3325a4a54dd5SStephen Warren 	int i, ret;
3326a4a54dd5SStephen Warren 
3327a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3328c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
332910e8aa9aSMichał Mirosław 		dev_err(card->dev,
333010e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
333110e8aa9aSMichał Mirosław 			propname);
3332a4a54dd5SStephen Warren 		return -EINVAL;
3333a4a54dd5SStephen Warren 	}
3334a4a54dd5SStephen Warren 	num_routes /= 2;
3335a4a54dd5SStephen Warren 	if (!num_routes) {
3336f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3337a4a54dd5SStephen Warren 			propname);
3338a4a54dd5SStephen Warren 		return -EINVAL;
3339a4a54dd5SStephen Warren 	}
3340a4a54dd5SStephen Warren 
3341e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3342a4a54dd5SStephen Warren 			      GFP_KERNEL);
3343a4a54dd5SStephen Warren 	if (!routes) {
3344a4a54dd5SStephen Warren 		dev_err(card->dev,
3345f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3346a4a54dd5SStephen Warren 		return -EINVAL;
3347a4a54dd5SStephen Warren 	}
3348a4a54dd5SStephen Warren 
3349a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3350a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3351e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3352a4a54dd5SStephen Warren 		if (ret) {
3353c871bd0bSMark Brown 			dev_err(card->dev,
3354c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3355c871bd0bSMark Brown 				propname, 2 * i, ret);
3356a4a54dd5SStephen Warren 			return -EINVAL;
3357a4a54dd5SStephen Warren 		}
3358a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3359e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3360a4a54dd5SStephen Warren 		if (ret) {
3361a4a54dd5SStephen Warren 			dev_err(card->dev,
3362c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3363c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3364a4a54dd5SStephen Warren 			return -EINVAL;
3365a4a54dd5SStephen Warren 		}
3366a4a54dd5SStephen Warren 	}
3367a4a54dd5SStephen Warren 
3368f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3369f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3370a4a54dd5SStephen Warren 
3371a4a54dd5SStephen Warren 	return 0;
3372a4a54dd5SStephen Warren }
3373a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3374a4a54dd5SStephen Warren 
3375a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3376389cb834SJyri Sarha 				     const char *prefix,
3377389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3378389cb834SJyri Sarha 				     struct device_node **framemaster)
3379a7930ed4SKuninori Morimoto {
3380a7930ed4SKuninori Morimoto 	int ret, i;
3381a7930ed4SKuninori Morimoto 	char prop[128];
3382a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3383a7930ed4SKuninori Morimoto 	int bit, frame;
3384a7930ed4SKuninori Morimoto 	const char *str;
3385a7930ed4SKuninori Morimoto 	struct {
3386a7930ed4SKuninori Morimoto 		char *name;
3387a7930ed4SKuninori Morimoto 		unsigned int val;
3388a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3389a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3390a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3391a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3392a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3393a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3394a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3395a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3396a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3397a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3398a7930ed4SKuninori Morimoto 	};
3399a7930ed4SKuninori Morimoto 
3400a7930ed4SKuninori Morimoto 	if (!prefix)
3401a7930ed4SKuninori Morimoto 		prefix = "";
3402a7930ed4SKuninori Morimoto 
3403a7930ed4SKuninori Morimoto 	/*
3404a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3405a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3406a7930ed4SKuninori Morimoto 	 */
3407a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3408a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3409a7930ed4SKuninori Morimoto 	if (ret == 0) {
3410a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3411a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3412a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3413a7930ed4SKuninori Morimoto 				break;
3414a7930ed4SKuninori Morimoto 			}
3415a7930ed4SKuninori Morimoto 		}
3416a7930ed4SKuninori Morimoto 	}
3417a7930ed4SKuninori Morimoto 
3418a7930ed4SKuninori Morimoto 	/*
34198c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3420a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3421a7930ed4SKuninori Morimoto 	 */
34228c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
34238c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
34248c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
34258c2d6a9fSKuninori Morimoto 	else
34268c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3427a7930ed4SKuninori Morimoto 
3428a7930ed4SKuninori Morimoto 	/*
3429a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3430a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3431a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3432a7930ed4SKuninori Morimoto 	 */
3433a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3434a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3435a7930ed4SKuninori Morimoto 
3436a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3437a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3438a7930ed4SKuninori Morimoto 
3439a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3440a7930ed4SKuninori Morimoto 	case 0x11:
3441a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3442a7930ed4SKuninori Morimoto 		break;
3443a7930ed4SKuninori Morimoto 	case 0x10:
3444a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3445a7930ed4SKuninori Morimoto 		break;
3446a7930ed4SKuninori Morimoto 	case 0x01:
3447a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3448a7930ed4SKuninori Morimoto 		break;
3449a7930ed4SKuninori Morimoto 	default:
3450a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3451a7930ed4SKuninori Morimoto 		break;
3452a7930ed4SKuninori Morimoto 	}
3453a7930ed4SKuninori Morimoto 
3454a7930ed4SKuninori Morimoto 	/*
3455a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3456a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3457a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3458a7930ed4SKuninori Morimoto 	 */
3459a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3460a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3461389cb834SJyri Sarha 	if (bit && bitclkmaster)
3462389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3463a7930ed4SKuninori Morimoto 
3464a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3465a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3466389cb834SJyri Sarha 	if (frame && framemaster)
3467389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3468a7930ed4SKuninori Morimoto 
3469a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3470a7930ed4SKuninori Morimoto 	case 0x11:
3471a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3472a7930ed4SKuninori Morimoto 		break;
3473a7930ed4SKuninori Morimoto 	case 0x10:
3474a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3475a7930ed4SKuninori Morimoto 		break;
3476a7930ed4SKuninori Morimoto 	case 0x01:
3477a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3478a7930ed4SKuninori Morimoto 		break;
3479a7930ed4SKuninori Morimoto 	default:
3480a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3481a7930ed4SKuninori Morimoto 		break;
3482a7930ed4SKuninori Morimoto 	}
3483a7930ed4SKuninori Morimoto 
3484a7930ed4SKuninori Morimoto 	return format;
3485a7930ed4SKuninori Morimoto }
3486a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3487a7930ed4SKuninori Morimoto 
348893b0f3eeSJean-Francois Moine static int snd_soc_get_dai_name(struct of_phandle_args *args,
3489cb470087SKuninori Morimoto 				const char **dai_name)
3490cb470087SKuninori Morimoto {
3491cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
349293b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3493cb470087SKuninori Morimoto 
3494cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3495cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
349693b0f3eeSJean-Francois Moine 		if (pos->dev->of_node != args->np)
3497cb470087SKuninori Morimoto 			continue;
3498cb470087SKuninori Morimoto 
34996833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
350093b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
350193b0f3eeSJean-Francois Moine 							     args,
350293b0f3eeSJean-Francois Moine 							     dai_name);
35036833c452SKuninori Morimoto 		} else {
35046833c452SKuninori Morimoto 			int id = -1;
35056833c452SKuninori Morimoto 
350693b0f3eeSJean-Francois Moine 			switch (args->args_count) {
35076833c452SKuninori Morimoto 			case 0:
35086833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
35096833c452SKuninori Morimoto 				break;
35106833c452SKuninori Morimoto 			case 1:
351193b0f3eeSJean-Francois Moine 				id = args->args[0];
35126833c452SKuninori Morimoto 				break;
35136833c452SKuninori Morimoto 			default:
35146833c452SKuninori Morimoto 				/* not supported */
3515cb470087SKuninori Morimoto 				break;
3516cb470087SKuninori Morimoto 			}
3517cb470087SKuninori Morimoto 
35186833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
35196833c452SKuninori Morimoto 				ret = -EINVAL;
35203dcba280SNicolin Chen 				continue;
35216833c452SKuninori Morimoto 			}
3522e41975edSXiubo Li 
3523e41975edSXiubo Li 			ret = 0;
3524e41975edSXiubo Li 
3525e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3526e41975edSXiubo Li 			if (!*dai_name)
3527e41975edSXiubo Li 				*dai_name = pos->name;
35286833c452SKuninori Morimoto 		}
35296833c452SKuninori Morimoto 
3530cb470087SKuninori Morimoto 		break;
3531cb470087SKuninori Morimoto 	}
3532cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
353393b0f3eeSJean-Francois Moine 	return ret;
353493b0f3eeSJean-Francois Moine }
353593b0f3eeSJean-Francois Moine 
353693b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
353793b0f3eeSJean-Francois Moine 			    const char **dai_name)
353893b0f3eeSJean-Francois Moine {
353993b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
354093b0f3eeSJean-Francois Moine 	int ret;
354193b0f3eeSJean-Francois Moine 
354293b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
354393b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
354493b0f3eeSJean-Francois Moine 	if (ret)
354593b0f3eeSJean-Francois Moine 		return ret;
354693b0f3eeSJean-Francois Moine 
354793b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3548cb470087SKuninori Morimoto 
3549cb470087SKuninori Morimoto 	of_node_put(args.np);
3550cb470087SKuninori Morimoto 
3551cb470087SKuninori Morimoto 	return ret;
3552cb470087SKuninori Morimoto }
3553cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3554cb470087SKuninori Morimoto 
355593b0f3eeSJean-Francois Moine /*
355693b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
355793b0f3eeSJean-Francois Moine  * @dev: Card device
355893b0f3eeSJean-Francois Moine  * @of_node: Device node
355993b0f3eeSJean-Francois Moine  * @dai_link: DAI link
356093b0f3eeSJean-Francois Moine  *
356193b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
356293b0f3eeSJean-Francois Moine  * 'sound-dai'.
356393b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
356493b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
356593b0f3eeSJean-Francois Moine  *
356693b0f3eeSJean-Francois Moine  * Returns 0 for success
356793b0f3eeSJean-Francois Moine  */
356893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
356993b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
357093b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
357193b0f3eeSJean-Francois Moine {
357293b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
357393b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
357493b0f3eeSJean-Francois Moine 	char *name;
357593b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
357693b0f3eeSJean-Francois Moine 
357793b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
357893b0f3eeSJean-Francois Moine 	name = "sound-dai";
357993b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
358093b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
358193b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
358293b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
358393b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
358493b0f3eeSJean-Francois Moine 		else
358593b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
358693b0f3eeSJean-Francois Moine 		return num_codecs;
358793b0f3eeSJean-Francois Moine 	}
358893b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
358993b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
359093b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
359193b0f3eeSJean-Francois Moine 	if (!component)
359293b0f3eeSJean-Francois Moine 		return -ENOMEM;
359393b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
359493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
359593b0f3eeSJean-Francois Moine 
359693b0f3eeSJean-Francois Moine 	/* Parse the list */
359793b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
359893b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
359993b0f3eeSJean-Francois Moine 	     index++, component++) {
360093b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
360193b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
360293b0f3eeSJean-Francois Moine 						  index, &args);
360393b0f3eeSJean-Francois Moine 		if (ret)
360493b0f3eeSJean-Francois Moine 			goto err;
360593b0f3eeSJean-Francois Moine 		component->of_node = args.np;
360693b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
360793b0f3eeSJean-Francois Moine 		if (ret < 0)
360893b0f3eeSJean-Francois Moine 			goto err;
360993b0f3eeSJean-Francois Moine 	}
361093b0f3eeSJean-Francois Moine 	return 0;
361193b0f3eeSJean-Francois Moine err:
361293b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
361393b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
361493b0f3eeSJean-Francois Moine 	     index++, component++) {
361593b0f3eeSJean-Francois Moine 		if (!component->of_node)
361693b0f3eeSJean-Francois Moine 			break;
361793b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
361893b0f3eeSJean-Francois Moine 		component->of_node = NULL;
361993b0f3eeSJean-Francois Moine 	}
362093b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
362193b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
362293b0f3eeSJean-Francois Moine 	return ret;
362393b0f3eeSJean-Francois Moine }
362493b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
362593b0f3eeSJean-Francois Moine 
3626c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3627db2a4165SFrank Mandarino {
36286553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3629fb257897SMark Brown 	snd_soc_util_init();
3630fb257897SMark Brown 
3631db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3632db2a4165SFrank Mandarino }
36334abe8e16SMark Brown module_init(snd_soc_init);
3634db2a4165SFrank Mandarino 
3635db2a4165SFrank Mandarino static void __exit snd_soc_exit(void)
3636db2a4165SFrank Mandarino {
3637fb257897SMark Brown 	snd_soc_util_exit();
36386553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3639fb257897SMark Brown 
3640db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
3641db2a4165SFrank Mandarino #endif
3642db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3643db2a4165SFrank Mandarino }
3644db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3645db2a4165SFrank Mandarino 
3646db2a4165SFrank Mandarino /* Module information */
3647db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3648db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3649db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3650db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3651