xref: /openbmc/linux/sound/soc/soc-core.c (revision 37660b6daf6d28bb2206c95ec75c8063f2db1606)
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 {
29581c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
29681c7cfd1SLars-Peter Clausen 		char *name;
297e73f3de5SRussell King 
29881c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
29981c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
30081c7cfd1SLars-Peter Clausen 		if (name) {
30181c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
30281c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
30381c7cfd1SLars-Peter Clausen 			kfree(name);
30481c7cfd1SLars-Peter Clausen 		}
30581c7cfd1SLars-Peter Clausen 	} else {
30681c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
30781c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
308e73f3de5SRussell King 	}
309e73f3de5SRussell King 
31081c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
31181c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
31281c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3132624d5faSMark Brown 		return;
3142624d5faSMark Brown 	}
3152624d5faSMark Brown 
31681c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
31781c7cfd1SLars-Peter Clausen 		component->debugfs_root);
31881c7cfd1SLars-Peter Clausen 
31981c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
32081c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
32181c7cfd1SLars-Peter Clausen }
32281c7cfd1SLars-Peter Clausen 
32381c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
32481c7cfd1SLars-Peter Clausen {
32581c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
32681c7cfd1SLars-Peter Clausen }
32781c7cfd1SLars-Peter Clausen 
32881c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
32981c7cfd1SLars-Peter Clausen {
33081c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
33181c7cfd1SLars-Peter Clausen 
3322624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
33381c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3342624d5faSMark Brown 						 codec, &codec_reg_fops);
3352624d5faSMark Brown 	if (!codec->debugfs_reg)
33610e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
33710e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
338731f1ab2SSebastien Guiriec }
339731f1ab2SSebastien Guiriec 
340c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
341c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
342c3c5a19aSMark Brown {
343c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3442b194f9dSMark Brown 	ssize_t len, ret = 0;
345c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
346c3c5a19aSMark Brown 
347c3c5a19aSMark Brown 	if (!buf)
348c3c5a19aSMark Brown 		return -ENOMEM;
349c3c5a19aSMark Brown 
3502b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3512b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
352f4333203SLars-Peter Clausen 			       codec->component.name);
3532b194f9dSMark Brown 		if (len >= 0)
3542b194f9dSMark Brown 			ret += len;
3552b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3562b194f9dSMark Brown 			ret = PAGE_SIZE;
3572b194f9dSMark Brown 			break;
3582b194f9dSMark Brown 		}
3592b194f9dSMark Brown 	}
360c3c5a19aSMark Brown 
361c3c5a19aSMark Brown 	if (ret >= 0)
362c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
363c3c5a19aSMark Brown 
364c3c5a19aSMark Brown 	kfree(buf);
365c3c5a19aSMark Brown 
366c3c5a19aSMark Brown 	return ret;
367c3c5a19aSMark Brown }
368c3c5a19aSMark Brown 
369c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
370c3c5a19aSMark Brown 	.read = codec_list_read_file,
371c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
372c3c5a19aSMark Brown };
373c3c5a19aSMark Brown 
374f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
375f3208780SMark Brown 				  size_t count, loff_t *ppos)
376f3208780SMark Brown {
377f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3782b194f9dSMark Brown 	ssize_t len, ret = 0;
3791438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
380f3208780SMark Brown 	struct snd_soc_dai *dai;
381f3208780SMark Brown 
382f3208780SMark Brown 	if (!buf)
383f3208780SMark Brown 		return -ENOMEM;
384f3208780SMark Brown 
3851438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3861438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3871438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3881438c2f6SLars-Peter Clausen 				dai->name);
3892b194f9dSMark Brown 			if (len >= 0)
3902b194f9dSMark Brown 				ret += len;
3912b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3922b194f9dSMark Brown 				ret = PAGE_SIZE;
3932b194f9dSMark Brown 				break;
3942b194f9dSMark Brown 			}
3952b194f9dSMark Brown 		}
3961438c2f6SLars-Peter Clausen 	}
397f3208780SMark Brown 
398f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
399f3208780SMark Brown 
400f3208780SMark Brown 	kfree(buf);
401f3208780SMark Brown 
402f3208780SMark Brown 	return ret;
403f3208780SMark Brown }
404f3208780SMark Brown 
405f3208780SMark Brown static const struct file_operations dai_list_fops = {
406f3208780SMark Brown 	.read = dai_list_read_file,
407f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
408f3208780SMark Brown };
409f3208780SMark Brown 
41019c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41119c7ac27SMark Brown 				       char __user *user_buf,
41219c7ac27SMark Brown 				       size_t count, loff_t *ppos)
41319c7ac27SMark Brown {
41419c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4152b194f9dSMark Brown 	ssize_t len, ret = 0;
41619c7ac27SMark Brown 	struct snd_soc_platform *platform;
41719c7ac27SMark Brown 
41819c7ac27SMark Brown 	if (!buf)
41919c7ac27SMark Brown 		return -ENOMEM;
42019c7ac27SMark Brown 
4212b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4222b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
423f4333203SLars-Peter Clausen 			       platform->component.name);
4242b194f9dSMark Brown 		if (len >= 0)
4252b194f9dSMark Brown 			ret += len;
4262b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4272b194f9dSMark Brown 			ret = PAGE_SIZE;
4282b194f9dSMark Brown 			break;
4292b194f9dSMark Brown 		}
4302b194f9dSMark Brown 	}
43119c7ac27SMark Brown 
43219c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
43319c7ac27SMark Brown 
43419c7ac27SMark Brown 	kfree(buf);
43519c7ac27SMark Brown 
43619c7ac27SMark Brown 	return ret;
43719c7ac27SMark Brown }
43819c7ac27SMark Brown 
43919c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44019c7ac27SMark Brown 	.read = platform_list_read_file,
44119c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
44219c7ac27SMark Brown };
44319c7ac27SMark Brown 
444a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
445a6052154SJarkko Nikula {
446a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4478a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4483a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
449a6052154SJarkko Nikula 		dev_warn(card->dev,
4507c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4513a45b867SJarkko Nikula 		return;
4523a45b867SJarkko Nikula 	}
4533a45b867SJarkko Nikula 
4543a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4553a45b867SJarkko Nikula 						    card->debugfs_card_root,
4563a45b867SJarkko Nikula 						    &card->pop_time);
4573a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4583a45b867SJarkko Nikula 		dev_warn(card->dev,
459f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
460a6052154SJarkko Nikula }
461a6052154SJarkko Nikula 
462a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
463a6052154SJarkko Nikula {
464a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
465a6052154SJarkko Nikula }
466a6052154SJarkko Nikula 
4672624d5faSMark Brown #else
4682624d5faSMark Brown 
46981c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
47081c7cfd1SLars-Peter Clausen 
47181c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
47281c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
4732624d5faSMark Brown {
4742624d5faSMark Brown }
4752624d5faSMark Brown 
47681c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
47781c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
478731f1ab2SSebastien Guiriec {
479731f1ab2SSebastien Guiriec }
480731f1ab2SSebastien Guiriec 
481b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
482b95fccbcSAxel Lin {
483b95fccbcSAxel Lin }
484b95fccbcSAxel Lin 
485b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
486b95fccbcSAxel Lin {
487b95fccbcSAxel Lin }
4882624d5faSMark Brown #endif
4892624d5faSMark Brown 
49047c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
49147c88fffSLiam Girdwood 		const char *dai_link, int stream)
49247c88fffSLiam Girdwood {
49347c88fffSLiam Girdwood 	int i;
49447c88fffSLiam Girdwood 
49547c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
49647c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
49747c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
49847c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
49947c88fffSLiam Girdwood 	}
500f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
50147c88fffSLiam Girdwood 	return NULL;
50247c88fffSLiam Girdwood }
50347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
50447c88fffSLiam Girdwood 
50547c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
50647c88fffSLiam Girdwood 		const char *dai_link)
50747c88fffSLiam Girdwood {
50847c88fffSLiam Girdwood 	int i;
50947c88fffSLiam Girdwood 
51047c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
51147c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
51247c88fffSLiam Girdwood 			return &card->rtd[i];
51347c88fffSLiam Girdwood 	}
514f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
51547c88fffSLiam Girdwood 	return NULL;
51647c88fffSLiam Girdwood }
51747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
51847c88fffSLiam Girdwood 
5199d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5209d58a077SRichard Fitzgerald {
5219d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5229d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5239d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5249d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5259d58a077SRichard Fitzgerald 	 */
5269d58a077SRichard Fitzgerald }
5279d58a077SRichard Fitzgerald 
5286f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
529db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5306f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
531db2a4165SFrank Mandarino {
5326f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5332eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
53488bd870fSBenoit Cousson 	int i, j;
535db2a4165SFrank Mandarino 
536c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
537c5599b87SLars-Peter Clausen 	if (!card->instantiated)
538e3509ff0SDaniel Mack 		return 0;
539e3509ff0SDaniel Mack 
5406ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5416ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5426ed25978SAndy Green 	 */
543f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
544f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
545f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5466ed25978SAndy Green 
5476ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
548f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5496ed25978SAndy Green 
550a00f90f9SMark Brown 	/* mute any active DACs */
551f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5523efab7dcSMark Brown 
553f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5543efab7dcSMark Brown 			continue;
5553efab7dcSMark Brown 
55688bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
55788bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
55888bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
55988bd870fSBenoit Cousson 
560f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
561f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
562db2a4165SFrank Mandarino 		}
56388bd870fSBenoit Cousson 	}
564db2a4165SFrank Mandarino 
5654ccab3e7SLiam Girdwood 	/* suspend all pcms */
566f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
567f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5683efab7dcSMark Brown 			continue;
5693efab7dcSMark Brown 
570f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5713efab7dcSMark Brown 	}
5724ccab3e7SLiam Girdwood 
57387506549SMark Brown 	if (card->suspend_pre)
57470b2ac12SMark Brown 		card->suspend_pre(card);
575db2a4165SFrank Mandarino 
576f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
577f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
5783efab7dcSMark Brown 
579f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5803efab7dcSMark Brown 			continue;
5813efab7dcSMark Brown 
582bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
583f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
584db2a4165SFrank Mandarino 	}
585db2a4165SFrank Mandarino 
586*37660b6dSLars-Peter Clausen 	/* close any waiting streams */
587*37660b6dSLars-Peter Clausen 	for (i = 0; i < card->num_rtd; i++)
58843829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
589db2a4165SFrank Mandarino 
590f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5913efab7dcSMark Brown 
592f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5933efab7dcSMark Brown 			continue;
5943efab7dcSMark Brown 
5957bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5967bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
597db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
598f0fba2adSLiam Girdwood 
5997bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6007bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
601db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
602db2a4165SFrank Mandarino 	}
603db2a4165SFrank Mandarino 
6048be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6058be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
606e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
607e2d32ff6SMark Brown 
608f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6092eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6101547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6111547aba9SMark Brown 		 * bias _ON and should not be suspended. */
612a8093297SLars-Peter Clausen 		if (!codec->suspended) {
613ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6141547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
615125a25daSMark Brown 				/*
616125a25daSMark Brown 				 * If the CODEC is capable of idle
617125a25daSMark Brown 				 * bias off then being in STANDBY
618125a25daSMark Brown 				 * means it's doing something,
619125a25daSMark Brown 				 * otherwise fall through.
620125a25daSMark Brown 				 */
621125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
622125a25daSMark Brown 					dev_dbg(codec->dev,
62310e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
624125a25daSMark Brown 					break;
625125a25daSMark Brown 				}
626a8093297SLars-Peter Clausen 
6271547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
628a8093297SLars-Peter Clausen 				if (codec->driver->suspend)
62984b315eeSLars-Peter Clausen 					codec->driver->suspend(codec);
630f0fba2adSLiam Girdwood 				codec->suspended = 1;
631e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
632e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
633988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
634988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6351547aba9SMark Brown 				break;
6361547aba9SMark Brown 			default:
63710e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
63810e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6391547aba9SMark Brown 				break;
6401547aba9SMark Brown 			}
6411547aba9SMark Brown 		}
642f0fba2adSLiam Girdwood 	}
643db2a4165SFrank Mandarino 
644f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
645f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6463efab7dcSMark Brown 
647f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6483efab7dcSMark Brown 			continue;
6493efab7dcSMark Brown 
650bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
651f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
652988e8cc4SNicolin Chen 
653988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
654988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
655db2a4165SFrank Mandarino 	}
656db2a4165SFrank Mandarino 
65787506549SMark Brown 	if (card->suspend_post)
65870b2ac12SMark Brown 		card->suspend_post(card);
659db2a4165SFrank Mandarino 
660db2a4165SFrank Mandarino 	return 0;
661db2a4165SFrank Mandarino }
6626f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
663db2a4165SFrank Mandarino 
6646ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6656ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6666ed25978SAndy Green  */
6676ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
668db2a4165SFrank Mandarino {
669f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
670f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6712eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
67288bd870fSBenoit Cousson 	int i, j;
673db2a4165SFrank Mandarino 
6746ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6756ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6766ed25978SAndy Green 	 */
6776ed25978SAndy Green 
678f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6796ed25978SAndy Green 
6809949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
681f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6829949788bSMark Brown 
68387506549SMark Brown 	if (card->resume_pre)
68470b2ac12SMark Brown 		card->resume_pre(card);
685db2a4165SFrank Mandarino 
686bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
687f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
688f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6893efab7dcSMark Brown 
690f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6913efab7dcSMark Brown 			continue;
6923efab7dcSMark Brown 
693bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
694f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
695db2a4165SFrank Mandarino 	}
696db2a4165SFrank Mandarino 
6972eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6981547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6991547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7001547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7011547aba9SMark Brown 		 */
702a8093297SLars-Peter Clausen 		if (codec->suspended) {
703ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7041547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7051547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
706a8093297SLars-Peter Clausen 				if (codec->driver->resume)
707f0fba2adSLiam Girdwood 					codec->driver->resume(codec);
708f0fba2adSLiam Girdwood 				codec->suspended = 0;
7091547aba9SMark Brown 				break;
7101547aba9SMark Brown 			default:
71110e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
71210e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7131547aba9SMark Brown 				break;
7141547aba9SMark Brown 			}
7151547aba9SMark Brown 		}
716f0fba2adSLiam Girdwood 	}
717db2a4165SFrank Mandarino 
718f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7193efab7dcSMark Brown 
720f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7213efab7dcSMark Brown 			continue;
7223efab7dcSMark Brown 
7237bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
724d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
725db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
726f0fba2adSLiam Girdwood 
7277bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
728d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
729db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
730db2a4165SFrank Mandarino 	}
731db2a4165SFrank Mandarino 
7323ff3f64bSMark Brown 	/* unmute any active DACs */
733f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7343efab7dcSMark Brown 
735f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7363efab7dcSMark Brown 			continue;
7373efab7dcSMark Brown 
73888bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
73988bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
74088bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
74188bd870fSBenoit Cousson 
742f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
743f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
744db2a4165SFrank Mandarino 		}
74588bd870fSBenoit Cousson 	}
746db2a4165SFrank Mandarino 
747f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
748f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7493efab7dcSMark Brown 
750f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7513efab7dcSMark Brown 			continue;
7523efab7dcSMark Brown 
753bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
754f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
755db2a4165SFrank Mandarino 	}
756db2a4165SFrank Mandarino 
75787506549SMark Brown 	if (card->resume_post)
75870b2ac12SMark Brown 		card->resume_post(card);
759db2a4165SFrank Mandarino 
760f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7616ed25978SAndy Green 
7626ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
763f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
764e2d32ff6SMark Brown 
7658be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7668be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
767e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7686ed25978SAndy Green }
7696ed25978SAndy Green 
7706ed25978SAndy Green /* powers up audio subsystem after a suspend */
7716f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7726ed25978SAndy Green {
7736f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
774bc263214SLars-Peter Clausen 	bool bus_control = false;
775bc263214SLars-Peter Clausen 	int i;
776b9dd94a8SPeter Ujfalusi 
777c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
778c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7795ff1ddf2SEric Miao 		return 0;
7805ff1ddf2SEric Miao 
781988e8cc4SNicolin Chen 	/* activate pins from sleep state */
782988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
78388bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
78488bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
78588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
78688bd870fSBenoit Cousson 		int j;
78788bd870fSBenoit Cousson 
788988e8cc4SNicolin Chen 		if (cpu_dai->active)
789988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
79088bd870fSBenoit Cousson 
79188bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
79288bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
793988e8cc4SNicolin Chen 			if (codec_dai->active)
794988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
795988e8cc4SNicolin Chen 		}
79688bd870fSBenoit Cousson 	}
797988e8cc4SNicolin Chen 
798bc263214SLars-Peter Clausen 	/*
799bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
800bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
801bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
80264ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
80364ab9baaSMark Brown 	 */
804f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
805f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
806bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
80782e14e8bSStephen Warren 	}
808bc263214SLars-Peter Clausen 	if (bus_control) {
809bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
81064ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
81164ab9baaSMark Brown 	} else {
812f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8136308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
814f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
815f0fba2adSLiam Girdwood 	}
8166ed25978SAndy Green 
817db2a4165SFrank Mandarino 	return 0;
818db2a4165SFrank Mandarino }
8196f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
820db2a4165SFrank Mandarino #else
8216f8ab4acSMark Brown #define snd_soc_suspend NULL
8226f8ab4acSMark Brown #define snd_soc_resume NULL
823db2a4165SFrank Mandarino #endif
824db2a4165SFrank Mandarino 
82585e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
82602a06d30SBarry Song };
82702a06d30SBarry Song 
82865d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
82965d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
83012023a9aSMisael Lopez Cruz {
83165d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
83212023a9aSMisael Lopez Cruz 
83365d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
83465d9361fSLars-Peter Clausen 		if (of_node) {
83565d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
83665d9361fSLars-Peter Clausen 				return component;
83765d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
83865d9361fSLars-Peter Clausen 			return component;
83912023a9aSMisael Lopez Cruz 		}
84012023a9aSMisael Lopez Cruz 	}
84112023a9aSMisael Lopez Cruz 
84212023a9aSMisael Lopez Cruz 	return NULL;
84312023a9aSMisael Lopez Cruz }
84412023a9aSMisael Lopez Cruz 
84514621c7eSLars-Peter Clausen static struct snd_soc_dai *snd_soc_find_dai(
84614621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
84712023a9aSMisael Lopez Cruz {
84814621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
84914621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
85012023a9aSMisael Lopez Cruz 
85114621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
85214621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
85314621c7eSLars-Peter Clausen 		if (dlc->of_node && component->dev->of_node != dlc->of_node)
85412023a9aSMisael Lopez Cruz 			continue;
8551ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
85612023a9aSMisael Lopez Cruz 			continue;
85714621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
85814621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
85914621c7eSLars-Peter Clausen 				continue;
86012023a9aSMisael Lopez Cruz 
86114621c7eSLars-Peter Clausen 			return dai;
86212023a9aSMisael Lopez Cruz 		}
86312023a9aSMisael Lopez Cruz 	}
86412023a9aSMisael Lopez Cruz 
86512023a9aSMisael Lopez Cruz 	return NULL;
86612023a9aSMisael Lopez Cruz }
86712023a9aSMisael Lopez Cruz 
868f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
869db2a4165SFrank Mandarino {
870f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
871f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
87288bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
87314621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
87488bd870fSBenoit Cousson 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
875435c5e25SMark Brown 	struct snd_soc_platform *platform;
876848dd8beSMark Brown 	const char *platform_name;
87788bd870fSBenoit Cousson 	int i;
878db2a4165SFrank Mandarino 
879f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
8806308419aSMark Brown 
88114621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
88214621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
88314621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
88414621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
885b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
886f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
887f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
888b19e6e7bSMark Brown 		return -EPROBE_DEFER;
889c5af3a2eSMark Brown 	}
890c5af3a2eSMark Brown 
89188bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
89288bd870fSBenoit Cousson 
89388bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
89488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
89514621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
89688bd870fSBenoit Cousson 		if (!codec_dais[i]) {
89712023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
89888bd870fSBenoit Cousson 				codecs[i].dai_name);
89912023a9aSMisael Lopez Cruz 			return -EPROBE_DEFER;
90012023a9aSMisael Lopez Cruz 		}
90188bd870fSBenoit Cousson 	}
90288bd870fSBenoit Cousson 
90388bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
90488bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
90588bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
90612023a9aSMisael Lopez Cruz 
907848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
908848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9095a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
910848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
911848dd8beSMark Brown 
912b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
913f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9145a504963SStephen Warren 		if (dai_link->platform_of_node) {
9155a504963SStephen Warren 			if (platform->dev->of_node !=
9165a504963SStephen Warren 			    dai_link->platform_of_node)
9175a504963SStephen Warren 				continue;
9185a504963SStephen Warren 		} else {
919f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
9202610ab77SStephen Warren 				continue;
9215a504963SStephen Warren 		}
9222610ab77SStephen Warren 
923f0fba2adSLiam Girdwood 		rtd->platform = platform;
924f0fba2adSLiam Girdwood 	}
925b19e6e7bSMark Brown 	if (!rtd->platform) {
926f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
927f0fba2adSLiam Girdwood 			dai_link->platform_name);
928b19e6e7bSMark Brown 		return -EPROBE_DEFER;
929f0fba2adSLiam Girdwood 	}
930b19e6e7bSMark Brown 
931b19e6e7bSMark Brown 	card->num_rtd++;
932b19e6e7bSMark Brown 
933b19e6e7bSMark Brown 	return 0;
9346b05eda6SMark Brown }
9356b05eda6SMark Brown 
936f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
937d12cd198SStephen Warren {
93870090bbbSLars-Peter Clausen 	if (!component->probed)
93970090bbbSLars-Peter Clausen 		return;
940d12cd198SStephen Warren 
941f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
942f1d45cc3SLars-Peter Clausen 	if (component->codec)
943f1d45cc3SLars-Peter Clausen 		list_del(&component->codec->card_list);
944d12cd198SStephen Warren 
945f1d45cc3SLars-Peter Clausen 	if (component->remove)
946f1d45cc3SLars-Peter Clausen 		component->remove(component);
947d12cd198SStephen Warren 
948f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
949d12cd198SStephen Warren 
950f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
951f1d45cc3SLars-Peter Clausen 	component->probed = 0;
952f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
953d12cd198SStephen Warren }
954d12cd198SStephen Warren 
955e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
956589c3563SJarkko Nikula {
957589c3563SJarkko Nikula 	int err;
958589c3563SJarkko Nikula 
959e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
960e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
961e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
962e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
963589c3563SJarkko Nikula 			if (err < 0)
964e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
965b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
966e60cd14fSLars-Peter Clausen 					dai->name, err);
967b0aa88afSMisael Lopez Cruz 		}
968e60cd14fSLars-Peter Clausen 		dai->probed = 0;
969b0aa88afSMisael Lopez Cruz 	}
970b0aa88afSMisael Lopez Cruz }
971b0aa88afSMisael Lopez Cruz 
97262ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
973f0fba2adSLiam Girdwood {
974f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
975e60cd14fSLars-Peter Clausen 	int i;
976f0fba2adSLiam Girdwood 
977f0fba2adSLiam Girdwood 	/* unregister the rtd device */
978f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
97936ae1a96SMark Brown 		device_unregister(rtd->dev);
980f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
98102a06d30SBarry Song 	}
98202a06d30SBarry Song 
983f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
98488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
985e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
986f0fba2adSLiam Girdwood 
987e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
988f0fba2adSLiam Girdwood }
989f0fba2adSLiam Girdwood 
99062ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
99162ae68faSStephen Warren 				       int order)
99262ae68faSStephen Warren {
99362ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
99462ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
99562ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
99661aca564SLars-Peter Clausen 	struct snd_soc_component *component;
99788bd870fSBenoit Cousson 	int i;
99862ae68faSStephen Warren 
99962ae68faSStephen Warren 	/* remove the platform */
100070090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1001f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
100262ae68faSStephen Warren 
100362ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
100488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
100561aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
100670090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
100761aca564SLars-Peter Clausen 			soc_remove_component(component);
100862ae68faSStephen Warren 	}
100962ae68faSStephen Warren 
101062ae68faSStephen Warren 	/* remove any CPU-side CODEC */
101162ae68faSStephen Warren 	if (cpu_dai) {
101270090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
101361aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
101462ae68faSStephen Warren 	}
101562ae68faSStephen Warren }
101662ae68faSStephen Warren 
10170671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10180671fd8eSKuninori Morimoto {
10190168bf0dSLiam Girdwood 	int dai, order;
10200671fd8eSKuninori Morimoto 
10210168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10220168bf0dSLiam Girdwood 			order++) {
10230168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
102462ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10250168bf0dSLiam Girdwood 	}
102662ae68faSStephen Warren 
102762ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
102862ae68faSStephen Warren 			order++) {
102962ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
103062ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
103162ae68faSStephen Warren 	}
103262ae68faSStephen Warren 
10330671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10340671fd8eSKuninori Morimoto }
10350671fd8eSKuninori Morimoto 
1036ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
103794f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1038ead9b919SJarkko Nikula {
1039ead9b919SJarkko Nikula 	int i;
1040ead9b919SJarkko Nikula 
1041ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1042ead9b919SJarkko Nikula 		return;
1043ead9b919SJarkko Nikula 
1044ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1045ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
104694f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
10473ca041edSSebastian Reichel 			continue;
104894f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
10493ca041edSSebastian Reichel 			continue;
105094f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1051ead9b919SJarkko Nikula 		break;
1052ead9b919SJarkko Nikula 	}
1053ead9b919SJarkko Nikula }
1054ead9b919SJarkko Nikula 
1055f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1056f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1057589c3563SJarkko Nikula {
1058f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1059888df395SMark Brown 	struct snd_soc_dai *dai;
1060f1d45cc3SLars-Peter Clausen 	int ret;
1061589c3563SJarkko Nikula 
106270090bbbSLars-Peter Clausen 	if (component->probed)
106370090bbbSLars-Peter Clausen 		return 0;
1064589c3563SJarkko Nikula 
1065f1d45cc3SLars-Peter Clausen 	component->card = card;
1066f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1067f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1068589c3563SJarkko Nikula 
1069f1d45cc3SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
107070d29331SJarkko Nikula 		return -ENODEV;
107170d29331SJarkko Nikula 
1072f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1073d5d1e0beSLars-Peter Clausen 
1074f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1075f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1076f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
107777530150SLars-Peter Clausen 
1078b318ad50SNariman Poushin 		if (ret != 0) {
1079f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1080b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1081b318ad50SNariman Poushin 			goto err_probe;
1082b318ad50SNariman Poushin 		}
1083b318ad50SNariman Poushin 	}
1084b318ad50SNariman Poushin 
10850634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
10860634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1087261edc70SNariman Poushin 		if (ret != 0) {
1088f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1089261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1090261edc70SNariman Poushin 			goto err_probe;
1091261edc70SNariman Poushin 		}
1092261edc70SNariman Poushin 	}
1093888df395SMark Brown 
1094f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1095f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1096589c3563SJarkko Nikula 		if (ret < 0) {
1097f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1098f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
109970d29331SJarkko Nikula 			goto err_probe;
1100589c3563SJarkko Nikula 		}
1101cb2cf612SLiam Girdwood 
1102f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1103f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1104956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1105f1d45cc3SLars-Peter Clausen 			component->name);
1106956245e9SLiam Girdwood 	}
1107956245e9SLiam Girdwood 
1108f1d45cc3SLars-Peter Clausen 	if (component->controls)
1109f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1110f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1111f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1112f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1113f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1114956245e9SLiam Girdwood 
1115f1d45cc3SLars-Peter Clausen 	component->probed = 1;
1116f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1117f1d45cc3SLars-Peter Clausen 
1118f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1119f1d45cc3SLars-Peter Clausen 	if (component->codec)
1120f1d45cc3SLars-Peter Clausen 		list_add(&component->codec->card_list, &card->codec_dev_list);
1121956245e9SLiam Girdwood 
1122956245e9SLiam Girdwood 	return 0;
1123956245e9SLiam Girdwood 
1124956245e9SLiam Girdwood err_probe:
1125f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1126f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1127956245e9SLiam Girdwood 
1128956245e9SLiam Girdwood 	return ret;
1129956245e9SLiam Girdwood }
1130956245e9SLiam Girdwood 
113136ae1a96SMark Brown static void rtd_release(struct device *dev)
113236ae1a96SMark Brown {
113336ae1a96SMark Brown 	kfree(dev);
113436ae1a96SMark Brown }
1135f0fba2adSLiam Girdwood 
11365f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
11375f3484acSLars-Peter Clausen 	const char *name)
1138503ae5e0SMisael Lopez Cruz {
1139589c3563SJarkko Nikula 	int ret = 0;
1140589c3563SJarkko Nikula 
1141589c3563SJarkko Nikula 	/* register the rtd device */
114236ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
114336ae1a96SMark Brown 	if (!rtd->dev)
114436ae1a96SMark Brown 		return -ENOMEM;
114536ae1a96SMark Brown 	device_initialize(rtd->dev);
11465f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
114736ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1148d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1149f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
115036ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1151b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
115201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
115301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
115401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
115501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
115636ae1a96SMark Brown 	ret = device_add(rtd->dev);
1157589c3563SJarkko Nikula 	if (ret < 0) {
1158865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1159865df9cbSChuansheng Liu 		put_device(rtd->dev);
11605f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1161f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1162589c3563SJarkko Nikula 		return ret;
1163589c3563SJarkko Nikula 	}
1164589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1165589c3563SJarkko Nikula 	return 0;
1166589c3563SJarkko Nikula }
1167589c3563SJarkko Nikula 
116862ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
116962ae68faSStephen Warren 				     int order)
117062ae68faSStephen Warren {
117162ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
117262ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1173f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
117488bd870fSBenoit Cousson 	int i, ret;
117562ae68faSStephen Warren 
117662ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
117761aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
117870090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1179f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
118062ae68faSStephen Warren 		if (ret < 0)
118162ae68faSStephen Warren 			return ret;
118262ae68faSStephen Warren 	}
118362ae68faSStephen Warren 
118488bd870fSBenoit Cousson 	/* probe the CODEC-side components */
118588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
118661aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
118770090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1188f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
118962ae68faSStephen Warren 			if (ret < 0)
119062ae68faSStephen Warren 				return ret;
119162ae68faSStephen Warren 		}
119288bd870fSBenoit Cousson 	}
119362ae68faSStephen Warren 
119462ae68faSStephen Warren 	/* probe the platform */
119570090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1196f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
119762ae68faSStephen Warren 		if (ret < 0)
119862ae68faSStephen Warren 			return ret;
119962ae68faSStephen Warren 	}
120062ae68faSStephen Warren 
120162ae68faSStephen Warren 	return 0;
120262ae68faSStephen Warren }
120362ae68faSStephen Warren 
12048e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1205b0aa88afSMisael Lopez Cruz {
1206b0aa88afSMisael Lopez Cruz 	int ret;
1207b0aa88afSMisael Lopez Cruz 
12088e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
12098e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
12108e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1211b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
12128e2be562SLars-Peter Clausen 				dev_err(dai->dev,
12138e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
12148e2be562SLars-Peter Clausen 					dai->name, ret);
1215b0aa88afSMisael Lopez Cruz 				return ret;
1216b0aa88afSMisael Lopez Cruz 			}
1217b0aa88afSMisael Lopez Cruz 		}
1218b0aa88afSMisael Lopez Cruz 
12198e2be562SLars-Peter Clausen 		dai->probed = 1;
1220b0aa88afSMisael Lopez Cruz 	}
1221b0aa88afSMisael Lopez Cruz 
1222b0aa88afSMisael Lopez Cruz 	return 0;
1223b0aa88afSMisael Lopez Cruz }
1224b0aa88afSMisael Lopez Cruz 
12252436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
12262436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
12273f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
12282436a723SMisael Lopez Cruz {
12293f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
12303f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
12312436a723SMisael Lopez Cruz 	struct snd_soc_dapm_widget *play_w, *capture_w;
12322436a723SMisael Lopez Cruz 	int ret;
12332436a723SMisael Lopez Cruz 
123488bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
123588bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
123688bd870fSBenoit Cousson 
12372436a723SMisael Lopez Cruz 	/* link the DAI widgets */
12382436a723SMisael Lopez Cruz 	play_w = codec_dai->playback_widget;
12392436a723SMisael Lopez Cruz 	capture_w = cpu_dai->capture_widget;
12402436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
12412436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
12422436a723SMisael Lopez Cruz 					   capture_w, play_w);
12432436a723SMisael Lopez Cruz 		if (ret != 0) {
12442436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
12452436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
12462436a723SMisael Lopez Cruz 			return ret;
12472436a723SMisael Lopez Cruz 		}
12482436a723SMisael Lopez Cruz 	}
12492436a723SMisael Lopez Cruz 
12502436a723SMisael Lopez Cruz 	play_w = cpu_dai->playback_widget;
12512436a723SMisael Lopez Cruz 	capture_w = codec_dai->capture_widget;
12522436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
12532436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
12542436a723SMisael Lopez Cruz 					   capture_w, play_w);
12552436a723SMisael Lopez Cruz 		if (ret != 0) {
12562436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
12572436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
12582436a723SMisael Lopez Cruz 			return ret;
12592436a723SMisael Lopez Cruz 		}
12602436a723SMisael Lopez Cruz 	}
12612436a723SMisael Lopez Cruz 
12622436a723SMisael Lopez Cruz 	return 0;
12632436a723SMisael Lopez Cruz }
12642436a723SMisael Lopez Cruz 
126562ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1266f0fba2adSLiam Girdwood {
1267f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1268f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1269c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
127088bd870fSBenoit Cousson 	int i, ret;
1271f0fba2adSLiam Girdwood 
1272f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
12730168bf0dSLiam Girdwood 			card->name, num, order);
1274f0fba2adSLiam Girdwood 
1275f0fba2adSLiam Girdwood 	/* set default power off timeout */
1276f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1277f0fba2adSLiam Girdwood 
12788e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
12798e2be562SLars-Peter Clausen 	if (ret)
1280f0fba2adSLiam Girdwood 		return ret;
1281f0fba2adSLiam Girdwood 
1282f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
128388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
12848e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1285b0aa88afSMisael Lopez Cruz 		if (ret)
1286f0fba2adSLiam Girdwood 			return ret;
128788bd870fSBenoit Cousson 	}
1288f0fba2adSLiam Girdwood 
12890168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
12900168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
12910168bf0dSLiam Girdwood 		return 0;
12920168bf0dSLiam Girdwood 
12935f3484acSLars-Peter Clausen 	/* do machine specific initialization */
12945f3484acSLars-Peter Clausen 	if (dai_link->init) {
12955f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
12965f3484acSLars-Peter Clausen 		if (ret < 0) {
12975f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
12985f3484acSLars-Peter Clausen 				dai_link->name, ret);
12995f3484acSLars-Peter Clausen 			return ret;
13005f3484acSLars-Peter Clausen 		}
13015f3484acSLars-Peter Clausen 	}
13025f3484acSLars-Peter Clausen 
13035f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1304589c3563SJarkko Nikula 	if (ret)
1305f0fba2adSLiam Girdwood 		return ret;
1306f0fba2adSLiam Girdwood 
13075f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
13085f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
13095f3484acSLars-Peter Clausen 	if (dai_link->dynamic) {
13105f3484acSLars-Peter Clausen 		ret = soc_dpcm_debugfs_add(rtd);
13115f3484acSLars-Peter Clausen 		if (ret < 0) {
13125f3484acSLars-Peter Clausen 			dev_err(rtd->dev,
13135f3484acSLars-Peter Clausen 				"ASoC: failed to add dpcm sysfs entries: %d\n",
13145f3484acSLars-Peter Clausen 				ret);
13155f3484acSLars-Peter Clausen 			return ret;
13165f3484acSLars-Peter Clausen 		}
13175f3484acSLars-Peter Clausen 	}
13185f3484acSLars-Peter Clausen #endif
13195f3484acSLars-Peter Clausen 
13201245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
13211245b700SNamarta Kohli 		/*create compress_device"*/
13221245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
13231245b700SNamarta Kohli 		if (ret < 0) {
1324f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
13251245b700SNamarta Kohli 					 dai_link->stream_name);
13261245b700SNamarta Kohli 			return ret;
13271245b700SNamarta Kohli 		}
13281245b700SNamarta Kohli 	} else {
13291245b700SNamarta Kohli 
1330c74184edSMark Brown 		if (!dai_link->params) {
1331f0fba2adSLiam Girdwood 			/* create the pcm */
1332f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1333f0fba2adSLiam Girdwood 			if (ret < 0) {
1334f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
13350837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1336f0fba2adSLiam Girdwood 				return ret;
1337f0fba2adSLiam Girdwood 			}
1338c74184edSMark Brown 		} else {
13399d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
13409d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
13419d58a077SRichard Fitzgerald 
1342c74184edSMark Brown 			/* link the DAI widgets */
13433f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
13442436a723SMisael Lopez Cruz 			if (ret)
1345c74184edSMark Brown 				return ret;
1346c74184edSMark Brown 		}
1347c74184edSMark Brown 	}
1348c74184edSMark Brown 
1349f0fba2adSLiam Girdwood 	return 0;
1350f0fba2adSLiam Girdwood }
1351f0fba2adSLiam Girdwood 
135244c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1353b19e6e7bSMark Brown {
135444c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
13553ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
135665d9361fSLars-Peter Clausen 	const char *name = aux_dev->codec_name;
13573ca041edSSebastian Reichel 
135865d9361fSLars-Peter Clausen 	rtd->component = soc_find_component(aux_dev->codec_of_node, name);
135965d9361fSLars-Peter Clausen 	if (!rtd->component) {
13603ca041edSSebastian Reichel 		if (aux_dev->codec_of_node)
136165d9361fSLars-Peter Clausen 			name = of_node_full_name(aux_dev->codec_of_node);
13623ca041edSSebastian Reichel 
136365d9361fSLars-Peter Clausen 		dev_err(card->dev, "ASoC: %s not registered\n", name);
1364b19e6e7bSMark Brown 		return -EPROBE_DEFER;
1365b19e6e7bSMark Brown 	}
1366b19e6e7bSMark Brown 
136765d9361fSLars-Peter Clausen 	/*
136865d9361fSLars-Peter Clausen 	 * Some places still reference rtd->codec, so we have to keep that
136965d9361fSLars-Peter Clausen 	 * initialized if the component is a CODEC. Once all those references
137065d9361fSLars-Peter Clausen 	 * have been removed, this code can be removed as well.
137165d9361fSLars-Peter Clausen 	 */
137265d9361fSLars-Peter Clausen 	 rtd->codec = rtd->component->codec;
137365d9361fSLars-Peter Clausen 
137444c69bb1SLars-Peter Clausen 	return 0;
13753ca041edSSebastian Reichel }
13762eea392dSJarkko Nikula 
137744c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
137844c69bb1SLars-Peter Clausen {
137944c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
13805f3484acSLars-Peter Clausen 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
138144c69bb1SLars-Peter Clausen 	int ret;
138244c69bb1SLars-Peter Clausen 
138365d9361fSLars-Peter Clausen 	ret = soc_probe_component(card, rtd->component);
13842eea392dSJarkko Nikula 	if (ret < 0)
1385589c3563SJarkko Nikula 		return ret;
13862eea392dSJarkko Nikula 
13875f3484acSLars-Peter Clausen 	/* do machine specific initialization */
13885f3484acSLars-Peter Clausen 	if (aux_dev->init) {
138957bf7726SLars-Peter Clausen 		ret = aux_dev->init(rtd->component);
13905f3484acSLars-Peter Clausen 		if (ret < 0) {
13915f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
13925f3484acSLars-Peter Clausen 				aux_dev->name, ret);
13932eea392dSJarkko Nikula 			return ret;
13942eea392dSJarkko Nikula 		}
13955f3484acSLars-Peter Clausen 	}
13965f3484acSLars-Peter Clausen 
13975f3484acSLars-Peter Clausen 	return soc_post_component_init(rtd, aux_dev->name);
13982eea392dSJarkko Nikula }
13992eea392dSJarkko Nikula 
14002eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
14012eea392dSJarkko Nikula {
14022eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
140365d9361fSLars-Peter Clausen 	struct snd_soc_component *component = rtd->component;
14042eea392dSJarkko Nikula 
14052eea392dSJarkko Nikula 	/* unregister the rtd device */
14062eea392dSJarkko Nikula 	if (rtd->dev_registered) {
140736ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1408d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
14092eea392dSJarkko Nikula 		rtd->dev_registered = 0;
14102eea392dSJarkko Nikula 	}
14112eea392dSJarkko Nikula 
141265d9361fSLars-Peter Clausen 	if (component && component->probed)
141365d9361fSLars-Peter Clausen 		soc_remove_component(component);
14142eea392dSJarkko Nikula }
14152eea392dSJarkko Nikula 
1416f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1417fdf0f54dSDimitris Papastamos {
1418fdf0f54dSDimitris Papastamos 	int ret;
1419fdf0f54dSDimitris Papastamos 
1420fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1421fdf0f54dSDimitris Papastamos 		return 0;
1422fdf0f54dSDimitris Papastamos 
1423fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1424fdf0f54dSDimitris Papastamos 	if (ret < 0) {
142510e8aa9aSMichał Mirosław 		dev_err(codec->dev,
142610e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
142710e8aa9aSMichał Mirosław 			ret);
1428fdf0f54dSDimitris Papastamos 		return ret;
1429fdf0f54dSDimitris Papastamos 	}
1430fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1431fdf0f54dSDimitris Papastamos 	return 0;
1432fdf0f54dSDimitris Papastamos }
1433fdf0f54dSDimitris Papastamos 
1434ce64c8b9SLars-Peter Clausen /**
1435ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1436ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1437ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1438ce64c8b9SLars-Peter Clausen  *
1439ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1440ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1441ce64c8b9SLars-Peter Clausen  *
1442ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1443ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1444ce64c8b9SLars-Peter Clausen  *
1445ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1446ce64c8b9SLars-Peter Clausen  */
1447ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1448ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1449ce64c8b9SLars-Peter Clausen {
1450ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1451ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1452ce64c8b9SLars-Peter Clausen 	unsigned int i;
1453ce64c8b9SLars-Peter Clausen 	int ret;
1454ce64c8b9SLars-Peter Clausen 
1455ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1456ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1457ce64c8b9SLars-Peter Clausen 
1458ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1459ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1460ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1461ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1462ce64c8b9SLars-Peter Clausen 			return ret;
1463ce64c8b9SLars-Peter Clausen 		}
1464ce64c8b9SLars-Peter Clausen 	}
1465ce64c8b9SLars-Peter Clausen 
1466ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1467ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1468ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1469ce64c8b9SLars-Peter Clausen 
1470ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1471ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1472ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1473ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1474ce64c8b9SLars-Peter Clausen 			break;
1475ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1476ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1477ce64c8b9SLars-Peter Clausen 			break;
1478ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1479ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1480ce64c8b9SLars-Peter Clausen 			break;
1481ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1482ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1483ce64c8b9SLars-Peter Clausen 			break;
1484ce64c8b9SLars-Peter Clausen 		}
1485ce64c8b9SLars-Peter Clausen 
1486ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1487ce64c8b9SLars-Peter Clausen 	}
1488ce64c8b9SLars-Peter Clausen 
1489ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1490ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1491ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1492ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1493ce64c8b9SLars-Peter Clausen 		return ret;
1494ce64c8b9SLars-Peter Clausen 	}
1495ce64c8b9SLars-Peter Clausen 
1496ce64c8b9SLars-Peter Clausen 	return 0;
1497ce64c8b9SLars-Peter Clausen }
1498ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1499ce64c8b9SLars-Peter Clausen 
1500b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1501f0fba2adSLiam Girdwood {
1502fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1503ce64c8b9SLars-Peter Clausen 	int ret, i, order;
150496dd3622SMark Brown 
150501b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1506f0fba2adSLiam Girdwood 
1507b19e6e7bSMark Brown 	/* bind DAIs */
1508b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1509b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1510b19e6e7bSMark Brown 		if (ret != 0)
1511b19e6e7bSMark Brown 			goto base_error;
1512db2a4165SFrank Mandarino 	}
1513db2a4165SFrank Mandarino 
151444c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1515b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
151644c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1517b19e6e7bSMark Brown 		if (ret != 0)
1518b19e6e7bSMark Brown 			goto base_error;
1519db2a4165SFrank Mandarino 	}
1520db2a4165SFrank Mandarino 
1521fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1522fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1523fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1524fdf0f54dSDimitris Papastamos 			continue;
1525f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1526b19e6e7bSMark Brown 		if (ret < 0)
1527b19e6e7bSMark Brown 			goto base_error;
1528fdf0f54dSDimitris Papastamos 	}
1529fdf0f54dSDimitris Papastamos 
1530f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1531102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1532f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1533f0fba2adSLiam Girdwood 	if (ret < 0) {
153410e8aa9aSMichał Mirosław 		dev_err(card->dev,
153510e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
153610e8aa9aSMichał Mirosław 			card->name, ret);
1537b19e6e7bSMark Brown 		goto base_error;
1538db2a4165SFrank Mandarino 	}
1539db2a4165SFrank Mandarino 
1540e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1541e37a4970SMark Brown 	card->dapm.dev = card->dev;
1542e37a4970SMark Brown 	card->dapm.card = card;
1543e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1544e37a4970SMark Brown 
1545d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1546d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1547d5d1e0beSLars-Peter Clausen #endif
1548d5d1e0beSLars-Peter Clausen 
154988ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
15506ed25978SAndy Green 	/* deferred resume work */
15516308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
15521301a964SRandy Dunlap #endif
15536ed25978SAndy Green 
15549a841ebbSMark Brown 	if (card->dapm_widgets)
15559a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
15569a841ebbSMark Brown 					  card->num_dapm_widgets);
15579a841ebbSMark Brown 
1558f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1559f0fba2adSLiam Girdwood 	if (card->probe) {
1560e7361ec4SMark Brown 		ret = card->probe(card);
1561f0fba2adSLiam Girdwood 		if (ret < 0)
1562f0fba2adSLiam Girdwood 			goto card_probe_error;
1563f0fba2adSLiam Girdwood 	}
1564f0fba2adSLiam Girdwood 
156562ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
15660168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
15670168bf0dSLiam Girdwood 			order++) {
1568fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
156962ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
157062ae68faSStephen Warren 			if (ret < 0) {
1571f110bfc7SLiam Girdwood 				dev_err(card->dev,
1572f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1573f110bfc7SLiam Girdwood 					ret);
157462ae68faSStephen Warren 				goto probe_dai_err;
157562ae68faSStephen Warren 			}
157662ae68faSStephen Warren 		}
157762ae68faSStephen Warren 	}
157862ae68faSStephen Warren 
157962ae68faSStephen Warren 	/* probe all DAI links on this card */
158062ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
158162ae68faSStephen Warren 			order++) {
158262ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
158362ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1584fe3e78e0SMark Brown 			if (ret < 0) {
1585f110bfc7SLiam Girdwood 				dev_err(card->dev,
1586f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1587f110bfc7SLiam Girdwood 					ret);
1588f0fba2adSLiam Girdwood 				goto probe_dai_err;
1589fe3e78e0SMark Brown 			}
1590fe3e78e0SMark Brown 		}
15910168bf0dSLiam Girdwood 	}
1592fe3e78e0SMark Brown 
15932eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
15942eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
15952eea392dSJarkko Nikula 		if (ret < 0) {
1596f110bfc7SLiam Girdwood 			dev_err(card->dev,
1597f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1598f110bfc7SLiam Girdwood 				ret);
15992eea392dSJarkko Nikula 			goto probe_aux_dev_err;
16002eea392dSJarkko Nikula 		}
16012eea392dSJarkko Nikula 	}
16022eea392dSJarkko Nikula 
1603888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1604b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1605888df395SMark Brown 
1606b7af1dafSMark Brown 	if (card->controls)
1607022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1608b7af1dafSMark Brown 
1609b8ad29deSMark Brown 	if (card->dapm_routes)
1610b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1611b8ad29deSMark Brown 					card->num_dapm_routes);
1612b8ad29deSMark Brown 
161375d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
1614ce64c8b9SLars-Peter Clausen 		if (card->dai_link[i].dai_fmt)
1615ce64c8b9SLars-Peter Clausen 			snd_soc_runtime_set_dai_fmt(&card->rtd[i],
1616ce64c8b9SLars-Peter Clausen 				card->dai_link[i].dai_fmt);
161775d9ac46SMark Brown 	}
161875d9ac46SMark Brown 
1619f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1620fe3e78e0SMark Brown 		 "%s", card->name);
1621f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
162222de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1623f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1624f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1625f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1626f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1627f0e8ed85SMark Brown 		case '_':
1628f0e8ed85SMark Brown 		case '-':
1629f0e8ed85SMark Brown 		case '\0':
1630f0e8ed85SMark Brown 			break;
1631f0e8ed85SMark Brown 		default:
1632f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1633f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1634f0e8ed85SMark Brown 			break;
1635f0e8ed85SMark Brown 		}
1636f0e8ed85SMark Brown 	}
1637fe3e78e0SMark Brown 
163828e9ad92SMark Brown 	if (card->late_probe) {
163928e9ad92SMark Brown 		ret = card->late_probe(card);
164028e9ad92SMark Brown 		if (ret < 0) {
1641f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
164228e9ad92SMark Brown 				card->name, ret);
164328e9ad92SMark Brown 			goto probe_aux_dev_err;
164428e9ad92SMark Brown 		}
164528e9ad92SMark Brown 	}
164628e9ad92SMark Brown 
1647824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
16488c193b8dSLars-Peter Clausen 
1649f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1650fe3e78e0SMark Brown 	if (ret < 0) {
1651f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1652f110bfc7SLiam Girdwood 				ret);
16536b3ed785SAxel Lin 		goto probe_aux_dev_err;
1654fe3e78e0SMark Brown 	}
1655fe3e78e0SMark Brown 
1656435c5e25SMark Brown 	card->instantiated = 1;
16574f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1658f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1659b19e6e7bSMark Brown 
1660b19e6e7bSMark Brown 	return 0;
1661db2a4165SFrank Mandarino 
16622eea392dSJarkko Nikula probe_aux_dev_err:
16632eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16642eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16652eea392dSJarkko Nikula 
1666f0fba2adSLiam Girdwood probe_dai_err:
16670671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1668fe3e78e0SMark Brown 
1669f0fba2adSLiam Girdwood card_probe_error:
167087506549SMark Brown 	if (card->remove)
1671e7361ec4SMark Brown 		card->remove(card);
1672f0fba2adSLiam Girdwood 
1673f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1674f0fba2adSLiam Girdwood 
1675b19e6e7bSMark Brown base_error:
1676f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1677db2a4165SFrank Mandarino 
1678b19e6e7bSMark Brown 	return ret;
1679435c5e25SMark Brown }
1680435c5e25SMark Brown 
1681435c5e25SMark Brown /* probes a new socdev */
1682435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1683435c5e25SMark Brown {
1684f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1685435c5e25SMark Brown 
168670a7ca34SVinod Koul 	/*
168770a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
168870a7ca34SVinod Koul 	 * we should not be here in that case so ret error
168970a7ca34SVinod Koul 	 */
169070a7ca34SVinod Koul 	if (!card)
169170a7ca34SVinod Koul 		return -EINVAL;
169270a7ca34SVinod Koul 
1693fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1694f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1695fe4085e8SMark Brown 		 card->name);
1696fe4085e8SMark Brown 
1697435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1698435c5e25SMark Brown 	card->dev = &pdev->dev;
1699f0fba2adSLiam Girdwood 
170028d528c8SMark Brown 	return snd_soc_register_card(card);
1701435c5e25SMark Brown }
1702435c5e25SMark Brown 
1703b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1704db2a4165SFrank Mandarino {
1705db2a4165SFrank Mandarino 	int i;
1706db2a4165SFrank Mandarino 
1707f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1708f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1709f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
171043829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1711db2a4165SFrank Mandarino 	}
1712db2a4165SFrank Mandarino 
17132eea392dSJarkko Nikula 	/* remove auxiliary devices */
17142eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17152eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17162eea392dSJarkko Nikula 
1717f0fba2adSLiam Girdwood 	/* remove and free each DAI */
17180671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1719f0fba2adSLiam Girdwood 
1720a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1721a6052154SJarkko Nikula 
1722f0fba2adSLiam Girdwood 	/* remove the card */
172387506549SMark Brown 	if (card->remove)
1724e7361ec4SMark Brown 		card->remove(card);
1725f0fba2adSLiam Girdwood 
17260aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
17270aaae527SLars-Peter Clausen 
1728f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1729b0e26485SVinod Koul 	return 0;
1730b0e26485SVinod Koul 
1731b2dfa62cSGuennadi Liakhovetski }
1732b0e26485SVinod Koul 
1733b0e26485SVinod Koul /* removes a socdev */
1734b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1735b0e26485SVinod Koul {
1736b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1737b0e26485SVinod Koul 
1738c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1739db2a4165SFrank Mandarino 	return 0;
1740db2a4165SFrank Mandarino }
1741db2a4165SFrank Mandarino 
17426f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
174351737470SMark Brown {
17446f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1745f0fba2adSLiam Girdwood 	int i;
174651737470SMark Brown 
174751737470SMark Brown 	if (!card->instantiated)
1748416356fcSMark Brown 		return 0;
174951737470SMark Brown 
175051737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
175151737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1752f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1753f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
175443829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1755f0fba2adSLiam Girdwood 	}
175651737470SMark Brown 
1757f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1758416356fcSMark Brown 
1759988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
1760988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
176188bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
176288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
176388bd870fSBenoit Cousson 		int j;
176488bd870fSBenoit Cousson 
1765988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
176688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
176788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
176888bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
176988bd870fSBenoit Cousson 		}
1770988e8cc4SNicolin Chen 	}
1771988e8cc4SNicolin Chen 
1772416356fcSMark Brown 	return 0;
177351737470SMark Brown }
17746f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
177551737470SMark Brown 
17766f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1777b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1778b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1779b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1780b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
17816f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1782b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1783416356fcSMark Brown };
1784deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1785416356fcSMark Brown 
1786db2a4165SFrank Mandarino /* ASoC platform driver */
1787db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1788db2a4165SFrank Mandarino 	.driver		= {
1789db2a4165SFrank Mandarino 		.name		= "soc-audio",
17906f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1791db2a4165SFrank Mandarino 	},
1792db2a4165SFrank Mandarino 	.probe		= soc_probe,
1793db2a4165SFrank Mandarino 	.remove		= soc_remove,
1794db2a4165SFrank Mandarino };
1795db2a4165SFrank Mandarino 
1796096e49d5SMark Brown /**
1797db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1798db2a4165SFrank Mandarino  * @_template: control template
1799db2a4165SFrank Mandarino  * @data: control private data
1800ac11a2b3SMark Brown  * @long_name: control long name
1801efb7ac3fSMark Brown  * @prefix: control name prefix
1802db2a4165SFrank Mandarino  *
1803db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1804db2a4165SFrank Mandarino  *
1805db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1806db2a4165SFrank Mandarino  */
1807db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
18083056557fSMark Brown 				  void *data, const char *long_name,
1809efb7ac3fSMark Brown 				  const char *prefix)
1810db2a4165SFrank Mandarino {
1811db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1812efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
1813efb7ac3fSMark Brown 	char *name = NULL;
1814db2a4165SFrank Mandarino 
1815db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1816db2a4165SFrank Mandarino 	template.index = 0;
1817db2a4165SFrank Mandarino 
1818efb7ac3fSMark Brown 	if (!long_name)
1819efb7ac3fSMark Brown 		long_name = template.name;
1820efb7ac3fSMark Brown 
1821efb7ac3fSMark Brown 	if (prefix) {
18222b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1823efb7ac3fSMark Brown 		if (!name)
1824efb7ac3fSMark Brown 			return NULL;
1825efb7ac3fSMark Brown 
1826efb7ac3fSMark Brown 		template.name = name;
1827efb7ac3fSMark Brown 	} else {
1828efb7ac3fSMark Brown 		template.name = long_name;
1829efb7ac3fSMark Brown 	}
1830efb7ac3fSMark Brown 
1831efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
1832efb7ac3fSMark Brown 
1833efb7ac3fSMark Brown 	kfree(name);
1834efb7ac3fSMark Brown 
1835efb7ac3fSMark Brown 	return kcontrol;
1836db2a4165SFrank Mandarino }
1837db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1838db2a4165SFrank Mandarino 
1839022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1840022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
1841022658beSLiam Girdwood 	const char *prefix, void *data)
1842022658beSLiam Girdwood {
1843022658beSLiam Girdwood 	int err, i;
1844022658beSLiam Girdwood 
1845022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
1846022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
1847022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
1848022658beSLiam Girdwood 						     control->name, prefix));
1849022658beSLiam Girdwood 		if (err < 0) {
1850f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
1851f110bfc7SLiam Girdwood 				control->name, err);
1852022658beSLiam Girdwood 			return err;
1853022658beSLiam Girdwood 		}
1854022658beSLiam Girdwood 	}
1855022658beSLiam Girdwood 
1856022658beSLiam Girdwood 	return 0;
1857022658beSLiam Girdwood }
1858022658beSLiam Girdwood 
18594fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
18604fefd698SDimitris Papastamos 					       const char *name)
18614fefd698SDimitris Papastamos {
18624fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
18634fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
18644fefd698SDimitris Papastamos 
18654fefd698SDimitris Papastamos 	if (unlikely(!name))
18664fefd698SDimitris Papastamos 		return NULL;
18674fefd698SDimitris Papastamos 
18684fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
18694fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
18704fefd698SDimitris Papastamos 			return kctl;
18714fefd698SDimitris Papastamos 	return NULL;
18724fefd698SDimitris Papastamos }
18734fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
18744fefd698SDimitris Papastamos 
1875db2a4165SFrank Mandarino /**
18760f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
18770f2780adSLars-Peter Clausen  *
18780f2780adSLars-Peter Clausen  * @component: Component to add controls to
18790f2780adSLars-Peter Clausen  * @controls: Array of controls to add
18800f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
18810f2780adSLars-Peter Clausen  *
18820f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
18830f2780adSLars-Peter Clausen  */
18840f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
18850f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
18860f2780adSLars-Peter Clausen {
18870f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
18880f2780adSLars-Peter Clausen 
18890f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
18900f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
18910f2780adSLars-Peter Clausen }
18920f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
18930f2780adSLars-Peter Clausen 
18940f2780adSLars-Peter Clausen /**
1895022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
1896022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
18973e8e1952SIan Molton  * duplicating this code.
18983e8e1952SIan Molton  *
18993e8e1952SIan Molton  * @codec: codec to add controls to
19003e8e1952SIan Molton  * @controls: array of controls to add
19013e8e1952SIan Molton  * @num_controls: number of elements in the array
19023e8e1952SIan Molton  *
19033e8e1952SIan Molton  * Return 0 for success, else error.
19043e8e1952SIan Molton  */
1905022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
19060f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
19073e8e1952SIan Molton {
19080f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
19090f2780adSLars-Peter Clausen 		num_controls);
19103e8e1952SIan Molton }
1911022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
19123e8e1952SIan Molton 
19133e8e1952SIan Molton /**
1914a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
1915022658beSLiam Girdwood  * Convenience function to add a list of controls.
1916a491a5c8SLiam Girdwood  *
1917a491a5c8SLiam Girdwood  * @platform: platform to add controls to
1918a491a5c8SLiam Girdwood  * @controls: array of controls to add
1919a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
1920a491a5c8SLiam Girdwood  *
1921a491a5c8SLiam Girdwood  * Return 0 for success, else error.
1922a491a5c8SLiam Girdwood  */
1923a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
19240f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
1925a491a5c8SLiam Girdwood {
19260f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
19270f2780adSLars-Peter Clausen 		num_controls);
1928a491a5c8SLiam Girdwood }
1929a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1930a491a5c8SLiam Girdwood 
1931a491a5c8SLiam Girdwood /**
1932022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
1933022658beSLiam Girdwood  * Convenience function to add a list of controls.
1934022658beSLiam Girdwood  *
1935022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
1936022658beSLiam Girdwood  * @controls: array of controls to add
1937022658beSLiam Girdwood  * @num_controls: number of elements in the array
1938022658beSLiam Girdwood  *
1939022658beSLiam Girdwood  * Return 0 for success, else error.
1940022658beSLiam Girdwood  */
1941022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1942022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
1943022658beSLiam Girdwood {
1944022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
1945022658beSLiam Girdwood 
1946022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1947022658beSLiam Girdwood 			NULL, soc_card);
1948022658beSLiam Girdwood }
1949022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1950022658beSLiam Girdwood 
1951022658beSLiam Girdwood /**
1952022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
1953022658beSLiam Girdwood  * Convienience function to add a list of controls.
1954022658beSLiam Girdwood  *
1955022658beSLiam Girdwood  * @dai: DAI to add controls to
1956022658beSLiam Girdwood  * @controls: array of controls to add
1957022658beSLiam Girdwood  * @num_controls: number of elements in the array
1958022658beSLiam Girdwood  *
1959022658beSLiam Girdwood  * Return 0 for success, else error.
1960022658beSLiam Girdwood  */
1961022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1962022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
1963022658beSLiam Girdwood {
1964313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
1965022658beSLiam Girdwood 
1966022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1967022658beSLiam Girdwood 			NULL, dai);
1968022658beSLiam Girdwood }
1969022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
1970022658beSLiam Girdwood 
1971022658beSLiam Girdwood /**
19728c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
19738c6529dbSLiam Girdwood  * @dai: DAI
19748c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
19758c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
19768c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
19778c6529dbSLiam Girdwood  *
19788c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
19798c6529dbSLiam Girdwood  */
19808c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
19818c6529dbSLiam Girdwood 	unsigned int freq, int dir)
19828c6529dbSLiam Girdwood {
1983f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
1984f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
1985ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
1986da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
1987ec4ee52aSMark Brown 						      freq, dir);
19888c6529dbSLiam Girdwood 	else
19891104a9c8SMark Brown 		return -ENOTSUPP;
19908c6529dbSLiam Girdwood }
19918c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
19928c6529dbSLiam Girdwood 
19938c6529dbSLiam Girdwood /**
1994ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
1995ec4ee52aSMark Brown  * @codec: CODEC
1996ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
1997da1c6ea6SMark Brown  * @source: Source for the clock
1998ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
1999ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2000ec4ee52aSMark Brown  *
2001ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2002ec4ee52aSMark Brown  */
2003ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2004da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2005ec4ee52aSMark Brown {
2006ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2007da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2008da1c6ea6SMark Brown 						 freq, dir);
2009ec4ee52aSMark Brown 	else
20101104a9c8SMark Brown 		return -ENOTSUPP;
2011ec4ee52aSMark Brown }
2012ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2013ec4ee52aSMark Brown 
2014ec4ee52aSMark Brown /**
20158c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
20168c6529dbSLiam Girdwood  * @dai: DAI
2017ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
20188c6529dbSLiam Girdwood  * @div: new clock divisor.
20198c6529dbSLiam Girdwood  *
20208c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
20218c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
20228c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
20238c6529dbSLiam Girdwood  */
20248c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
20258c6529dbSLiam Girdwood 	int div_id, int div)
20268c6529dbSLiam Girdwood {
2027f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2028f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
20298c6529dbSLiam Girdwood 	else
20308c6529dbSLiam Girdwood 		return -EINVAL;
20318c6529dbSLiam Girdwood }
20328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
20338c6529dbSLiam Girdwood 
20348c6529dbSLiam Girdwood /**
20358c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
20368c6529dbSLiam Girdwood  * @dai: DAI
20378c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
203885488037SMark Brown  * @source: DAI specific source for the PLL
20398c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
20408c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
20418c6529dbSLiam Girdwood  *
20428c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
20438c6529dbSLiam Girdwood  */
204485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
204585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
20468c6529dbSLiam Girdwood {
2047f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2048f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
204985488037SMark Brown 					 freq_in, freq_out);
2050ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2051ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2052ec4ee52aSMark Brown 						   freq_in, freq_out);
20538c6529dbSLiam Girdwood 	else
20548c6529dbSLiam Girdwood 		return -EINVAL;
20558c6529dbSLiam Girdwood }
20568c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
20578c6529dbSLiam Girdwood 
2058ec4ee52aSMark Brown /*
2059ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2060ec4ee52aSMark Brown  * @codec: CODEC
2061ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2062ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2063ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2064ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2065ec4ee52aSMark Brown  *
2066ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2067ec4ee52aSMark Brown  */
2068ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2069ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2070ec4ee52aSMark Brown {
2071ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2072ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2073ec4ee52aSMark Brown 					      freq_in, freq_out);
2074ec4ee52aSMark Brown 	else
2075ec4ee52aSMark Brown 		return -EINVAL;
2076ec4ee52aSMark Brown }
2077ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2078ec4ee52aSMark Brown 
20798c6529dbSLiam Girdwood /**
2080e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2081e54cf76bSLiam Girdwood  * @dai: DAI
2082e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
2083e54cf76bSLiam Girdwood  *
2084e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2085e54cf76bSLiam Girdwood  */
2086e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2087e54cf76bSLiam Girdwood {
2088e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2089e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2090e54cf76bSLiam Girdwood 	else
2091e54cf76bSLiam Girdwood 		return -EINVAL;
2092e54cf76bSLiam Girdwood }
2093e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2094e54cf76bSLiam Girdwood 
2095e54cf76bSLiam Girdwood /**
20968c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
20978c6529dbSLiam Girdwood  * @dai: DAI
20988c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
20998c6529dbSLiam Girdwood  *
21008c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
21018c6529dbSLiam Girdwood  */
21028c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
21038c6529dbSLiam Girdwood {
21045e4ba569SShawn Guo 	if (dai->driver == NULL)
21058c6529dbSLiam Girdwood 		return -EINVAL;
21065e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
21075e4ba569SShawn Guo 		return -ENOTSUPP;
21085e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
21098c6529dbSLiam Girdwood }
21108c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
21118c6529dbSLiam Girdwood 
21128c6529dbSLiam Girdwood /**
2113e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
211489c67857SXiubo Li  * @slots: Number of slots in use.
211589c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
211689c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
211789c67857SXiubo Li  *
211889c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
211989c67857SXiubo Li  */
2120e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
212189c67857SXiubo Li 					  unsigned int *tx_mask,
212289c67857SXiubo Li 					  unsigned int *rx_mask)
212389c67857SXiubo Li {
212489c67857SXiubo Li 	if (*tx_mask || *rx_mask)
212589c67857SXiubo Li 		return 0;
212689c67857SXiubo Li 
212789c67857SXiubo Li 	if (!slots)
212889c67857SXiubo Li 		return -EINVAL;
212989c67857SXiubo Li 
213089c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
213189c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
213289c67857SXiubo Li 
213389c67857SXiubo Li 	return 0;
213489c67857SXiubo Li }
213589c67857SXiubo Li 
213689c67857SXiubo Li /**
2137e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2138e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2139a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2140a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
21418c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2142a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
21438c6529dbSLiam Girdwood  *
2144e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2145e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2146e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2147e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2148e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2149e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2150e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2151e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2152e46c9366SLars-Peter Clausen  * channel and so on.
2153e46c9366SLars-Peter Clausen  *
2154e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2155e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2156e46c9366SLars-Peter Clausen  *
2157e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
21588c6529dbSLiam Girdwood  */
21598c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2160a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
21618c6529dbSLiam Girdwood {
2162e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2163e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
216489c67857SXiubo Li 						&tx_mask, &rx_mask);
216589c67857SXiubo Li 	else
2166e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
216789c67857SXiubo Li 
216888bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
216988bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
217088bd870fSBenoit Cousson 
2171f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2172f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2173a5479e38SDaniel Ribeiro 				slots, slot_width);
21748c6529dbSLiam Girdwood 	else
2175b2cbb6e1SXiubo Li 		return -ENOTSUPP;
21768c6529dbSLiam Girdwood }
21778c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
21788c6529dbSLiam Girdwood 
21798c6529dbSLiam Girdwood /**
2180472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2181472df3cbSBarry Song  * @dai: DAI
2182472df3cbSBarry Song  * @tx_num: how many TX channels
2183472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2184472df3cbSBarry Song  *           0~num-1 uses
2185472df3cbSBarry Song  * @rx_num: how many RX channels
2186472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2187472df3cbSBarry Song  *           0~num-1 uses
2188472df3cbSBarry Song  *
2189472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2190472df3cbSBarry Song  */
2191472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2192472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2193472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2194472df3cbSBarry Song {
2195f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2196f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2197472df3cbSBarry Song 			rx_num, rx_slot);
2198472df3cbSBarry Song 	else
2199472df3cbSBarry Song 		return -EINVAL;
2200472df3cbSBarry Song }
2201472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2202472df3cbSBarry Song 
2203472df3cbSBarry Song /**
22048c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
22058c6529dbSLiam Girdwood  * @dai: DAI
22068c6529dbSLiam Girdwood  * @tristate: tristate enable
22078c6529dbSLiam Girdwood  *
22088c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
22098c6529dbSLiam Girdwood  */
22108c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
22118c6529dbSLiam Girdwood {
2212f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2213f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
22148c6529dbSLiam Girdwood 	else
22158c6529dbSLiam Girdwood 		return -EINVAL;
22168c6529dbSLiam Girdwood }
22178c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
22188c6529dbSLiam Girdwood 
22198c6529dbSLiam Girdwood /**
22208c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
22218c6529dbSLiam Girdwood  * @dai: DAI
22228c6529dbSLiam Girdwood  * @mute: mute enable
2223da18396fSMark Brown  * @direction: stream to mute
22248c6529dbSLiam Girdwood  *
22258c6529dbSLiam Girdwood  * Mutes the DAI DAC.
22268c6529dbSLiam Girdwood  */
2227da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2228da18396fSMark Brown 			     int direction)
22298c6529dbSLiam Girdwood {
2230da18396fSMark Brown 	if (!dai->driver)
2231da18396fSMark Brown 		return -ENOTSUPP;
2232da18396fSMark Brown 
2233da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2234da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2235da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2236da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2237f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
22388c6529dbSLiam Girdwood 	else
223904570c62SMark Brown 		return -ENOTSUPP;
22408c6529dbSLiam Girdwood }
22418c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
22428c6529dbSLiam Girdwood 
224388bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card,
224488bd870fSBenoit Cousson 				   struct snd_soc_dai_link *dai_link)
224588bd870fSBenoit Cousson {
224688bd870fSBenoit Cousson 	/* Legacy codec/codec_dai link is a single entry in multicodec */
224788bd870fSBenoit Cousson 	if (dai_link->codec_name || dai_link->codec_of_node ||
224888bd870fSBenoit Cousson 	    dai_link->codec_dai_name) {
224988bd870fSBenoit Cousson 		dai_link->num_codecs = 1;
225088bd870fSBenoit Cousson 
225188bd870fSBenoit Cousson 		dai_link->codecs = devm_kzalloc(card->dev,
225288bd870fSBenoit Cousson 				sizeof(struct snd_soc_dai_link_component),
225388bd870fSBenoit Cousson 				GFP_KERNEL);
225488bd870fSBenoit Cousson 		if (!dai_link->codecs)
225588bd870fSBenoit Cousson 			return -ENOMEM;
225688bd870fSBenoit Cousson 
225788bd870fSBenoit Cousson 		dai_link->codecs[0].name = dai_link->codec_name;
225888bd870fSBenoit Cousson 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
225988bd870fSBenoit Cousson 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
226088bd870fSBenoit Cousson 	}
226188bd870fSBenoit Cousson 
226288bd870fSBenoit Cousson 	if (!dai_link->codecs) {
226388bd870fSBenoit Cousson 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
226488bd870fSBenoit Cousson 		return -EINVAL;
226588bd870fSBenoit Cousson 	}
226688bd870fSBenoit Cousson 
226788bd870fSBenoit Cousson 	return 0;
226888bd870fSBenoit Cousson }
226988bd870fSBenoit Cousson 
2270c5af3a2eSMark Brown /**
2271c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2272c5af3a2eSMark Brown  *
2273ac11a2b3SMark Brown  * @card: Card to register
2274c5af3a2eSMark Brown  *
2275c5af3a2eSMark Brown  */
227670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2277c5af3a2eSMark Brown {
227888bd870fSBenoit Cousson 	int i, j, ret;
2279f0fba2adSLiam Girdwood 
2280c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2281c5af3a2eSMark Brown 		return -EINVAL;
2282c5af3a2eSMark Brown 
22835a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
22845a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
22855a504963SStephen Warren 
228688bd870fSBenoit Cousson 		ret = snd_soc_init_multicodec(card, link);
228788bd870fSBenoit Cousson 		if (ret) {
228888bd870fSBenoit Cousson 			dev_err(card->dev, "ASoC: failed to init multicodec\n");
228988bd870fSBenoit Cousson 			return ret;
229088bd870fSBenoit Cousson 		}
229188bd870fSBenoit Cousson 
229288bd870fSBenoit Cousson 		for (j = 0; j < link->num_codecs; j++) {
22935a504963SStephen Warren 			/*
22945a504963SStephen Warren 			 * Codec must be specified by 1 of name or OF node,
22955a504963SStephen Warren 			 * not both or neither.
22965a504963SStephen Warren 			 */
229788bd870fSBenoit Cousson 			if (!!link->codecs[j].name ==
229888bd870fSBenoit Cousson 			    !!link->codecs[j].of_node) {
229988bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
230010e8aa9aSMichał Mirosław 					link->name);
23015a504963SStephen Warren 				return -EINVAL;
23025a504963SStephen Warren 			}
2303bc92657aSStephen Warren 			/* Codec DAI name must be specified */
230488bd870fSBenoit Cousson 			if (!link->codecs[j].dai_name) {
230588bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
230610e8aa9aSMichał Mirosław 					link->name);
2307bc92657aSStephen Warren 				return -EINVAL;
2308bc92657aSStephen Warren 			}
230988bd870fSBenoit Cousson 		}
23105a504963SStephen Warren 
23115a504963SStephen Warren 		/*
23125a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
23135a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
23145a504963SStephen Warren 		 */
23155a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
231610e8aa9aSMichał Mirosław 			dev_err(card->dev,
231710e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
231810e8aa9aSMichał Mirosław 				link->name);
23195a504963SStephen Warren 			return -EINVAL;
23205a504963SStephen Warren 		}
23215a504963SStephen Warren 
23225a504963SStephen Warren 		/*
2323bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
2324bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
2325bc92657aSStephen Warren 		 * name alone..
23265a504963SStephen Warren 		 */
2327bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
232810e8aa9aSMichał Mirosław 			dev_err(card->dev,
232910e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
233010e8aa9aSMichał Mirosław 				link->name);
2331bc92657aSStephen Warren 			return -EINVAL;
2332bc92657aSStephen Warren 		}
2333bc92657aSStephen Warren 		/*
2334bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
2335bc92657aSStephen Warren 		 * specified
2336bc92657aSStephen Warren 		 */
2337bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
2338bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
233910e8aa9aSMichał Mirosław 			dev_err(card->dev,
234010e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
234110e8aa9aSMichał Mirosław 				link->name);
23425a504963SStephen Warren 			return -EINVAL;
23435a504963SStephen Warren 		}
23445a504963SStephen Warren 	}
23455a504963SStephen Warren 
2346ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2347ed77cc12SMark Brown 
2348111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2349111c6419SStephen Warren 
2350150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
2351150dd2f8SVinod Koul 
2352181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
2353181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
23542eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
2355f0fba2adSLiam Girdwood 				 GFP_KERNEL);
2356f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2357f0fba2adSLiam Girdwood 		return -ENOMEM;
2358a7dbb603SLiam Girdwood 	card->num_rtd = 0;
23592eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
2360f0fba2adSLiam Girdwood 
23615f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_links; i++) {
23625f3484acSLars-Peter Clausen 		card->rtd[i].card = card;
2363f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
236488bd870fSBenoit Cousson 		card->rtd[i].codec_dais = devm_kzalloc(card->dev,
236588bd870fSBenoit Cousson 					sizeof(struct snd_soc_dai *) *
236688bd870fSBenoit Cousson 					(card->rtd[i].dai_link->num_codecs),
236788bd870fSBenoit Cousson 					GFP_KERNEL);
236888bd870fSBenoit Cousson 		if (card->rtd[i].codec_dais == NULL)
236988bd870fSBenoit Cousson 			return -ENOMEM;
23705f3484acSLars-Peter Clausen 	}
23715f3484acSLars-Peter Clausen 
23725f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_aux_devs; i++)
23735f3484acSLars-Peter Clausen 		card->rtd_aux[i].card = card;
2374f0fba2adSLiam Girdwood 
2375db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
2376c5af3a2eSMark Brown 	card->instantiated = 0;
2377f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2378a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2379c5af3a2eSMark Brown 
2380b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2381b19e6e7bSMark Brown 	if (ret != 0)
2382b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
2383c5af3a2eSMark Brown 
2384988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2385988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
238688bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
238788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
238888bd870fSBenoit Cousson 		int j;
238988bd870fSBenoit Cousson 
239088bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
239188bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2392988e8cc4SNicolin Chen 			if (!codec_dai->active)
2393988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
239488bd870fSBenoit Cousson 		}
239588bd870fSBenoit Cousson 
2396988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2397988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2398988e8cc4SNicolin Chen 	}
2399988e8cc4SNicolin Chen 
2400b19e6e7bSMark Brown 	return ret;
2401c5af3a2eSMark Brown }
240270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2403c5af3a2eSMark Brown 
2404c5af3a2eSMark Brown /**
2405c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2406c5af3a2eSMark Brown  *
2407ac11a2b3SMark Brown  * @card: Card to unregister
2408c5af3a2eSMark Brown  *
2409c5af3a2eSMark Brown  */
241070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2411c5af3a2eSMark Brown {
241201e0df66SLars-Peter Clausen 	if (card->instantiated) {
241301e0df66SLars-Peter Clausen 		card->instantiated = false;
24141c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2415b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2416f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
24178f6f9b29SKuninori Morimoto 	}
2418c5af3a2eSMark Brown 
2419c5af3a2eSMark Brown 	return 0;
2420c5af3a2eSMark Brown }
242170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2422c5af3a2eSMark Brown 
2423f0fba2adSLiam Girdwood /*
2424f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2425f0fba2adSLiam Girdwood  * and sanitizing names.
2426f0fba2adSLiam Girdwood  */
24270b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2428f0fba2adSLiam Girdwood {
2429f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2430f0fba2adSLiam Girdwood 	int id1, id2;
2431f0fba2adSLiam Girdwood 
2432f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2433f0fba2adSLiam Girdwood 		return NULL;
2434f0fba2adSLiam Girdwood 
243558818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2436f0fba2adSLiam Girdwood 
2437f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2438f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2439f0fba2adSLiam Girdwood 	if (found) {
2440f0fba2adSLiam Girdwood 		/* get ID */
2441f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2442f0fba2adSLiam Girdwood 
2443f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2444f0fba2adSLiam Girdwood 			if (*id == -1)
2445f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2446f0fba2adSLiam Girdwood 		}
2447f0fba2adSLiam Girdwood 
2448f0fba2adSLiam Girdwood 	} else {
2449f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2450f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2451f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2452f0fba2adSLiam Girdwood 
2453f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
245405899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2455f0fba2adSLiam Girdwood 
2456f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2457f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
245858818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2459f0fba2adSLiam Girdwood 		} else
2460f0fba2adSLiam Girdwood 			*id = 0;
2461f0fba2adSLiam Girdwood 	}
2462f0fba2adSLiam Girdwood 
2463f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2464f0fba2adSLiam Girdwood }
2465f0fba2adSLiam Girdwood 
2466f0fba2adSLiam Girdwood /*
2467f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2468f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2469f0fba2adSLiam Girdwood  */
2470f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2471f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2472f0fba2adSLiam Girdwood {
2473f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
247410e8aa9aSMichał Mirosław 		dev_err(dev,
247510e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
247610e8aa9aSMichał Mirosław 			dev_name(dev));
2477f0fba2adSLiam Girdwood 		return NULL;
2478f0fba2adSLiam Girdwood 	}
2479f0fba2adSLiam Girdwood 
2480f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2481f0fba2adSLiam Girdwood }
2482f0fba2adSLiam Girdwood 
24839115171aSMark Brown /**
248432c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
24859115171aSMark Brown  *
248632c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
24879115171aSMark Brown  */
248832c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
24899115171aSMark Brown {
24905c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
24919115171aSMark Brown 
24925c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
249332c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
249432c9ba54SLars-Peter Clausen 			dai->name);
24959115171aSMark Brown 		list_del(&dai->list);
2496f0fba2adSLiam Girdwood 		kfree(dai->name);
2497f0fba2adSLiam Girdwood 		kfree(dai);
24989115171aSMark Brown 	}
249932c9ba54SLars-Peter Clausen }
25009115171aSMark Brown 
25019115171aSMark Brown /**
250232c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25039115171aSMark Brown  *
25046106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25056106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2506ac11a2b3SMark Brown  * @count: Number of DAIs
250732c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
250832c9ba54SLars-Peter Clausen  *                     parent's name.
25099115171aSMark Brown  */
251032c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2511bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2512bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
25139115171aSMark Brown {
25146106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2515f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
251632c9ba54SLars-Peter Clausen 	unsigned int i;
251732c9ba54SLars-Peter Clausen 	int ret;
2518f0fba2adSLiam Girdwood 
2519f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
25209115171aSMark Brown 
2521bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2522bb13109dSLars-Peter Clausen 	component->num_dai = count;
2523bb13109dSLars-Peter Clausen 
25249115171aSMark Brown 	for (i = 0; i < count; i++) {
2525f0fba2adSLiam Girdwood 
2526f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2527c46e0079SAxel Lin 		if (dai == NULL) {
2528c46e0079SAxel Lin 			ret = -ENOMEM;
2529c46e0079SAxel Lin 			goto err;
2530c46e0079SAxel Lin 		}
2531f0fba2adSLiam Girdwood 
253232c9ba54SLars-Peter Clausen 		/*
253332c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
253432c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
253532c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
253632c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
253732c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
253832c9ba54SLars-Peter Clausen 		 * component-less anymore.
253932c9ba54SLars-Peter Clausen 		 */
254032c9ba54SLars-Peter Clausen 		if (count == 1 && legacy_dai_naming) {
2541db2a4165SFrank Mandarino 			dai->name = fmt_single_name(dev, &dai->id);
254232c9ba54SLars-Peter Clausen 		} else {
2543f0fba2adSLiam Girdwood 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
254432c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
254532c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
254632c9ba54SLars-Peter Clausen 			else
254732c9ba54SLars-Peter Clausen 				dai->id = i;
254832c9ba54SLars-Peter Clausen 		}
2549f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
2550f0fba2adSLiam Girdwood 			kfree(dai);
255132c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
25529115171aSMark Brown 			goto err;
25539115171aSMark Brown 		}
25549115171aSMark Brown 
25556106d129SLars-Peter Clausen 		dai->component = component;
2556f0fba2adSLiam Girdwood 		dai->dev = dev;
2557f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
2558f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
2559f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
2560f0fba2adSLiam Girdwood 
25611438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
2562054880feSMark Brown 
25639115171aSMark Brown 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2564f0fba2adSLiam Girdwood 	}
2565f0fba2adSLiam Girdwood 
25669115171aSMark Brown 	return 0;
25679115171aSMark Brown 
25689115171aSMark Brown err:
256932c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
25709115171aSMark Brown 
25719115171aSMark Brown 	return ret;
25729115171aSMark Brown }
25739115171aSMark Brown 
257414e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
257514e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2576d191bd8dSKuninori Morimoto {
257714e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2578d191bd8dSKuninori Morimoto 
257914e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
258014e8bdebSLars-Peter Clausen }
2581d191bd8dSKuninori Morimoto 
258214e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
258314e8bdebSLars-Peter Clausen 	int event)
258414e8bdebSLars-Peter Clausen {
258514e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
258614e8bdebSLars-Peter Clausen 
258714e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
258814e8bdebSLars-Peter Clausen }
258914e8bdebSLars-Peter Clausen 
2590bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2591bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2592d191bd8dSKuninori Morimoto {
2593ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2594ce0fc93aSLars-Peter Clausen 
2595bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2596bb13109dSLars-Peter Clausen 	if (!component->name) {
2597bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2598d191bd8dSKuninori Morimoto 		return -ENOMEM;
2599d191bd8dSKuninori Morimoto 	}
2600d191bd8dSKuninori Morimoto 
2601bb13109dSLars-Peter Clausen 	component->dev = dev;
2602bb13109dSLars-Peter Clausen 	component->driver = driver;
260361aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
260461aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
2605e2c330b9SLars-Peter Clausen 
2606ce0fc93aSLars-Peter Clausen 	if (!component->dapm_ptr)
2607ce0fc93aSLars-Peter Clausen 		component->dapm_ptr = &component->dapm;
2608ce0fc93aSLars-Peter Clausen 
2609ce0fc93aSLars-Peter Clausen 	dapm = component->dapm_ptr;
2610ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2611ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2612ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
26135819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
261414e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
261514e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
261614e8bdebSLars-Peter Clausen 	if (driver->stream_event)
261714e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2618ce0fc93aSLars-Peter Clausen 
261961aca564SLars-Peter Clausen 	component->controls = driver->controls;
262061aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
262161aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
262261aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
262361aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
262461aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
262561aca564SLars-Peter Clausen 
2626bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2627bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2628bb13109dSLars-Peter Clausen 
2629bb13109dSLars-Peter Clausen 	return 0;
2630d191bd8dSKuninori Morimoto }
2631d191bd8dSKuninori Morimoto 
263220feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2633886f5692SLars-Peter Clausen {
2634886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
263520feb881SLars-Peter Clausen 
2636886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2637886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2638886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2639886f5692SLars-Peter Clausen }
264020feb881SLars-Peter Clausen 
2641e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2642e874bf5fSLars-Peter Clausen 
264320feb881SLars-Peter Clausen /**
264420feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
264520feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
264620feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
264720feb881SLars-Peter Clausen  *
264820feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
264920feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
265020feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
265120feb881SLars-Peter Clausen  * before the first IO attempt of the component.
265220feb881SLars-Peter Clausen  */
265320feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
265420feb881SLars-Peter Clausen 	struct regmap *regmap)
265520feb881SLars-Peter Clausen {
265620feb881SLars-Peter Clausen 	component->regmap = regmap;
265720feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2658886f5692SLars-Peter Clausen }
265920feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
266020feb881SLars-Peter Clausen 
266120feb881SLars-Peter Clausen /**
266220feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
266320feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
266420feb881SLars-Peter Clausen  *
266520feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
266620feb881SLars-Peter Clausen  * removes the regmap instance from the component.
266720feb881SLars-Peter Clausen  *
266820feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
266920feb881SLars-Peter Clausen  * to initialize the regmap instance.
267020feb881SLars-Peter Clausen  */
267120feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
267220feb881SLars-Peter Clausen {
267320feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
267420feb881SLars-Peter Clausen 	component->regmap = NULL;
267520feb881SLars-Peter Clausen }
267620feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2677886f5692SLars-Peter Clausen 
2678e874bf5fSLars-Peter Clausen #endif
2679d191bd8dSKuninori Morimoto 
2680bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2681bb13109dSLars-Peter Clausen {
268220feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
268320feb881SLars-Peter Clausen 		if (!component->regmap)
268420feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
268520feb881SLars-Peter Clausen 		if (component->regmap)
268620feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
268720feb881SLars-Peter Clausen 	}
2688886f5692SLars-Peter Clausen 
2689bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2690d191bd8dSKuninori Morimoto }
2691d191bd8dSKuninori Morimoto 
2692bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
2693bb13109dSLars-Peter Clausen {
2694d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
2695bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
2696d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2697bb13109dSLars-Peter Clausen }
2698d191bd8dSKuninori Morimoto 
2699bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2700bb13109dSLars-Peter Clausen {
2701bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2702bb13109dSLars-Peter Clausen 	kfree(component->name);
2703bb13109dSLars-Peter Clausen }
2704d191bd8dSKuninori Morimoto 
2705bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2706bb13109dSLars-Peter Clausen {
2707bb13109dSLars-Peter Clausen 	list_del(&component->list);
2708bb13109dSLars-Peter Clausen }
2709d191bd8dSKuninori Morimoto 
2710bb13109dSLars-Peter Clausen static void snd_soc_component_del(struct snd_soc_component *component)
2711bb13109dSLars-Peter Clausen {
2712bb13109dSLars-Peter Clausen 	mutex_lock(&client_mutex);
2713bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(component);
2714bb13109dSLars-Peter Clausen 	mutex_unlock(&client_mutex);
2715d191bd8dSKuninori Morimoto }
2716d191bd8dSKuninori Morimoto 
2717d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2718d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
2719d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
2720d191bd8dSKuninori Morimoto 			       int num_dai)
2721d191bd8dSKuninori Morimoto {
2722d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
2723bb13109dSLars-Peter Clausen 	int ret;
2724d191bd8dSKuninori Morimoto 
2725bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
2726d191bd8dSKuninori Morimoto 	if (!cmpnt) {
2727d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
2728d191bd8dSKuninori Morimoto 		return -ENOMEM;
2729d191bd8dSKuninori Morimoto 	}
2730d191bd8dSKuninori Morimoto 
2731bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2732bb13109dSLars-Peter Clausen 	if (ret)
2733bb13109dSLars-Peter Clausen 		goto err_free;
2734bb13109dSLars-Peter Clausen 
27353d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
273698e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
27373d59400fSLars-Peter Clausen 
2738bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2739bb13109dSLars-Peter Clausen 	if (ret < 0) {
2740bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
2741bb13109dSLars-Peter Clausen 		goto err_cleanup;
2742bb13109dSLars-Peter Clausen 	}
2743bb13109dSLars-Peter Clausen 
2744bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
2745bb13109dSLars-Peter Clausen 
2746bb13109dSLars-Peter Clausen 	return 0;
2747bb13109dSLars-Peter Clausen 
2748bb13109dSLars-Peter Clausen err_cleanup:
2749bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
2750bb13109dSLars-Peter Clausen err_free:
2751bb13109dSLars-Peter Clausen 	kfree(cmpnt);
2752bb13109dSLars-Peter Clausen 	return ret;
2753d191bd8dSKuninori Morimoto }
2754d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2755d191bd8dSKuninori Morimoto 
2756d191bd8dSKuninori Morimoto /**
2757d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
2758d191bd8dSKuninori Morimoto  *
2759d191bd8dSKuninori Morimoto  */
2760d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
2761d191bd8dSKuninori Morimoto {
2762d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
2763d191bd8dSKuninori Morimoto 
2764d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
276598e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
2766d191bd8dSKuninori Morimoto 			goto found;
2767d191bd8dSKuninori Morimoto 	}
2768d191bd8dSKuninori Morimoto 	return;
2769d191bd8dSKuninori Morimoto 
2770d191bd8dSKuninori Morimoto found:
2771bb13109dSLars-Peter Clausen 	snd_soc_component_del(cmpnt);
2772bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
2773bb13109dSLars-Peter Clausen 	kfree(cmpnt);
2774d191bd8dSKuninori Morimoto }
2775d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2776d191bd8dSKuninori Morimoto 
2777f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
2778e2c330b9SLars-Peter Clausen {
2779e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2780e2c330b9SLars-Peter Clausen 
2781f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
2782e2c330b9SLars-Peter Clausen }
2783e2c330b9SLars-Peter Clausen 
2784f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
2785e2c330b9SLars-Peter Clausen {
2786e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2787e2c330b9SLars-Peter Clausen 
2788f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
2789e2c330b9SLars-Peter Clausen }
2790e2c330b9SLars-Peter Clausen 
2791d191bd8dSKuninori Morimoto /**
279271a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
279371a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
279471a45cdaSLars-Peter Clausen  * @platform: The platform to add
279571a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
279612a48a8cSMark Brown  */
279771a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
2798d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
279912a48a8cSMark Brown {
2800b37f1d12SLars-Peter Clausen 	int ret;
2801b37f1d12SLars-Peter Clausen 
2802bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
2803bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
2804bb13109dSLars-Peter Clausen 	if (ret)
2805bb13109dSLars-Peter Clausen 		return ret;
2806f0fba2adSLiam Girdwood 
2807f0fba2adSLiam Girdwood 	platform->dev = dev;
2808f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
2809f1d45cc3SLars-Peter Clausen 
2810f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
2811f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
2812f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
2813f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
281412a48a8cSMark Brown 
281581c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
281681c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
281781c7cfd1SLars-Peter Clausen #endif
281812a48a8cSMark Brown 
281912a48a8cSMark Brown 	mutex_lock(&client_mutex);
2820bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
282112a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
282212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
282312a48a8cSMark Brown 
2824f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2825f4333203SLars-Peter Clausen 		platform->component.name);
282612a48a8cSMark Brown 
282712a48a8cSMark Brown 	return 0;
282812a48a8cSMark Brown }
282971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
283071a45cdaSLars-Peter Clausen 
283171a45cdaSLars-Peter Clausen /**
283271a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
283371a45cdaSLars-Peter Clausen  *
283471a45cdaSLars-Peter Clausen  * @platform: platform to register
283571a45cdaSLars-Peter Clausen  */
283671a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
283771a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
283871a45cdaSLars-Peter Clausen {
283971a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
284071a45cdaSLars-Peter Clausen 	int ret;
284171a45cdaSLars-Peter Clausen 
284271a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
284371a45cdaSLars-Peter Clausen 
284471a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
284571a45cdaSLars-Peter Clausen 	if (platform == NULL)
284671a45cdaSLars-Peter Clausen 		return -ENOMEM;
284771a45cdaSLars-Peter Clausen 
284871a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
284971a45cdaSLars-Peter Clausen 	if (ret)
285071a45cdaSLars-Peter Clausen 		kfree(platform);
285171a45cdaSLars-Peter Clausen 
285271a45cdaSLars-Peter Clausen 	return ret;
285371a45cdaSLars-Peter Clausen }
285412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
285512a48a8cSMark Brown 
285612a48a8cSMark Brown /**
285771a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
285871a45cdaSLars-Peter Clausen  * @platform: the platform to remove
285971a45cdaSLars-Peter Clausen  */
286071a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
286171a45cdaSLars-Peter Clausen {
2862b37f1d12SLars-Peter Clausen 
286371a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
286471a45cdaSLars-Peter Clausen 	list_del(&platform->list);
2865bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
286671a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
286771a45cdaSLars-Peter Clausen 
286871a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
2869f4333203SLars-Peter Clausen 		platform->component.name);
2870decc27b0SDaniel Mack 
2871decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
287271a45cdaSLars-Peter Clausen }
287371a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
287471a45cdaSLars-Peter Clausen 
287571a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
287671a45cdaSLars-Peter Clausen {
287771a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
287871a45cdaSLars-Peter Clausen 
287971a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
288071a45cdaSLars-Peter Clausen 		if (dev == platform->dev)
288171a45cdaSLars-Peter Clausen 			return platform;
288271a45cdaSLars-Peter Clausen 	}
288371a45cdaSLars-Peter Clausen 
288471a45cdaSLars-Peter Clausen 	return NULL;
288571a45cdaSLars-Peter Clausen }
288671a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
288771a45cdaSLars-Peter Clausen 
288871a45cdaSLars-Peter Clausen /**
288912a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
289012a48a8cSMark Brown  *
2891ac11a2b3SMark Brown  * @platform: platform to unregister
289212a48a8cSMark Brown  */
2893f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
289412a48a8cSMark Brown {
2895f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
2896f0fba2adSLiam Girdwood 
289771a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
289871a45cdaSLars-Peter Clausen 	if (!platform)
2899f0fba2adSLiam Girdwood 		return;
2900f0fba2adSLiam Girdwood 
290171a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
2902f0fba2adSLiam Girdwood 	kfree(platform);
290312a48a8cSMark Brown }
290412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
290512a48a8cSMark Brown 
2906151ab22cSMark Brown static u64 codec_format_map[] = {
2907151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2908151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2909151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2910151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2911151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2912151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2913151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2914151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2915151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2916151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2917151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2918151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2919151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2920151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2921151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2922151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2923151ab22cSMark Brown };
2924151ab22cSMark Brown 
2925151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2926151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2927151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2928151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2929151ab22cSMark Brown  */
2930151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2931151ab22cSMark Brown {
2932151ab22cSMark Brown 	int i;
2933151ab22cSMark Brown 
2934151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2935151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2936151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
2937151ab22cSMark Brown }
2938151ab22cSMark Brown 
2939f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
2940f1d45cc3SLars-Peter Clausen {
2941f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2942f1d45cc3SLars-Peter Clausen 
2943f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
2944f1d45cc3SLars-Peter Clausen }
2945f1d45cc3SLars-Peter Clausen 
2946f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
2947f1d45cc3SLars-Peter Clausen {
2948f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2949f1d45cc3SLars-Peter Clausen 
2950f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
2951f1d45cc3SLars-Peter Clausen }
2952f1d45cc3SLars-Peter Clausen 
2953e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
2954e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
2955e2c330b9SLars-Peter Clausen {
2956e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2957e2c330b9SLars-Peter Clausen 
2958e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
2959e2c330b9SLars-Peter Clausen }
2960e2c330b9SLars-Peter Clausen 
2961e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
2962e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
2963e2c330b9SLars-Peter Clausen {
2964e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2965e2c330b9SLars-Peter Clausen 
2966e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
2967e2c330b9SLars-Peter Clausen 
2968e2c330b9SLars-Peter Clausen 	return 0;
2969e2c330b9SLars-Peter Clausen }
2970e2c330b9SLars-Peter Clausen 
297168f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
297268f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
297368f831c2SLars-Peter Clausen {
297468f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
297568f831c2SLars-Peter Clausen 
297668f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
297768f831c2SLars-Peter Clausen }
297868f831c2SLars-Peter Clausen 
29790d0cf00aSMark Brown /**
29800d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
29810d0cf00aSMark Brown  *
2982ac11a2b3SMark Brown  * @codec: codec to register
29830d0cf00aSMark Brown  */
2984f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
2985001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
2986001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
2987001ae4c0SMark Brown 			   int num_dai)
29880d0cf00aSMark Brown {
2989f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
2990bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
2991f0fba2adSLiam Girdwood 	int ret, i;
2992151ab22cSMark Brown 
2993f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
29940d0cf00aSMark Brown 
2995f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
2996f0fba2adSLiam Girdwood 	if (codec == NULL)
2997f0fba2adSLiam Girdwood 		return -ENOMEM;
29980d0cf00aSMark Brown 
2999ce0fc93aSLars-Peter Clausen 	codec->component.dapm_ptr = &codec->dapm;
3000f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3001ce0fc93aSLars-Peter Clausen 
3002bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3003bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3004bb13109dSLars-Peter Clausen 	if (ret)
3005bb13109dSLars-Peter Clausen 		goto err_free;
3006151ab22cSMark Brown 
3007f1d45cc3SLars-Peter Clausen 	if (codec_drv->controls) {
3008f1d45cc3SLars-Peter Clausen 		codec->component.controls = codec_drv->controls;
3009f1d45cc3SLars-Peter Clausen 		codec->component.num_controls = codec_drv->num_controls;
3010f1d45cc3SLars-Peter Clausen 	}
3011f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_widgets) {
3012f1d45cc3SLars-Peter Clausen 		codec->component.dapm_widgets = codec_drv->dapm_widgets;
3013f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3014f1d45cc3SLars-Peter Clausen 	}
3015f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_routes) {
3016f1d45cc3SLars-Peter Clausen 		codec->component.dapm_routes = codec_drv->dapm_routes;
3017f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3018f1d45cc3SLars-Peter Clausen 	}
3019f1d45cc3SLars-Peter Clausen 
3020f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3021f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3022f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3023f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
3024e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3025e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3026e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3027e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
30283d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
30295819c2faSLars-Peter Clausen 	codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
303086dbf2acSLars-Peter Clausen 	codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
303114e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
3032474b62d6SMark Brown 		codec->dapm.seq_notifier = codec_drv->seq_notifier;
303368f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
303468f831c2SLars-Peter Clausen 		codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3035f0fba2adSLiam Girdwood 	codec->dev = dev;
3036f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3037e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3038f0fba2adSLiam Girdwood 
303981c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
304081c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
304181c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
304281c7cfd1SLars-Peter Clausen #endif
3043a39f75f7SXiubo Li 
3044a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3045886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3046a39f75f7SXiubo Li 
3047f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3048f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3049f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3050f0fba2adSLiam Girdwood 	}
3051f0fba2adSLiam Girdwood 
3052bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3053bb13109dSLars-Peter Clausen 	if (ret < 0) {
3054bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3055bb13109dSLars-Peter Clausen 		goto err_cleanup;
3056bb13109dSLars-Peter Clausen 	}
3057bb13109dSLars-Peter Clausen 
3058bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3059bb13109dSLars-Peter Clausen 		dai->codec = codec;
3060bb13109dSLars-Peter Clausen 
3061054880feSMark Brown 	mutex_lock(&client_mutex);
3062bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3063054880feSMark Brown 	list_add(&codec->list, &codec_list);
3064054880feSMark Brown 	mutex_unlock(&client_mutex);
3065054880feSMark Brown 
3066f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3067f4333203SLars-Peter Clausen 		codec->component.name);
30680d0cf00aSMark Brown 	return 0;
3069f0fba2adSLiam Girdwood 
3070bb13109dSLars-Peter Clausen err_cleanup:
3071bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3072bb13109dSLars-Peter Clausen err_free:
3073f0fba2adSLiam Girdwood 	kfree(codec);
3074f0fba2adSLiam Girdwood 	return ret;
30750d0cf00aSMark Brown }
30760d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
30770d0cf00aSMark Brown 
30780d0cf00aSMark Brown /**
30790d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
30800d0cf00aSMark Brown  *
3081ac11a2b3SMark Brown  * @codec: codec to unregister
30820d0cf00aSMark Brown  */
3083f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
30840d0cf00aSMark Brown {
3085f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3086f0fba2adSLiam Girdwood 
3087f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3088f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3089f0fba2adSLiam Girdwood 			goto found;
3090f0fba2adSLiam Girdwood 	}
3091f0fba2adSLiam Girdwood 	return;
3092f0fba2adSLiam Girdwood 
3093f0fba2adSLiam Girdwood found:
3094f0fba2adSLiam Girdwood 
30950d0cf00aSMark Brown 	mutex_lock(&client_mutex);
30960d0cf00aSMark Brown 	list_del(&codec->list);
3097bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
30980d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
30990d0cf00aSMark Brown 
3100f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3101f4333203SLars-Peter Clausen 			codec->component.name);
3102f0fba2adSLiam Girdwood 
3103bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
31047a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3105f0fba2adSLiam Girdwood 	kfree(codec);
31060d0cf00aSMark Brown }
31070d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
31080d0cf00aSMark Brown 
3109bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3110bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3111bec4fa05SStephen Warren 			       const char *propname)
3112bec4fa05SStephen Warren {
31137e07e7c0STushar Behera 	struct device_node *np;
3114bec4fa05SStephen Warren 	int ret;
3115bec4fa05SStephen Warren 
31167e07e7c0STushar Behera 	if (!card->dev) {
31177e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
31187e07e7c0STushar Behera 		return -EINVAL;
31197e07e7c0STushar Behera 	}
31207e07e7c0STushar Behera 
31217e07e7c0STushar Behera 	np = card->dev->of_node;
31227e07e7c0STushar Behera 
3123bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3124bec4fa05SStephen Warren 	/*
3125bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3126bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3127bec4fa05SStephen Warren 	 * snd_soc_register_card.
3128bec4fa05SStephen Warren 	 */
3129bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3130bec4fa05SStephen Warren 		dev_err(card->dev,
3131f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3132bec4fa05SStephen Warren 			propname, ret);
3133bec4fa05SStephen Warren 		return ret;
3134bec4fa05SStephen Warren 	}
3135bec4fa05SStephen Warren 
3136bec4fa05SStephen Warren 	return 0;
3137bec4fa05SStephen Warren }
3138bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3139bec4fa05SStephen Warren 
31409a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
31419a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
31429a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
31439a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
31449a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
31459a6d4860SXiubo Li };
31469a6d4860SXiubo Li 
31479a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
31489a6d4860SXiubo Li 					  const char *propname)
31499a6d4860SXiubo Li {
31509a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
31519a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
31529a6d4860SXiubo Li 	const char *template, *wname;
31539a6d4860SXiubo Li 	int i, j, num_widgets, ret;
31549a6d4860SXiubo Li 
31559a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
31569a6d4860SXiubo Li 	if (num_widgets < 0) {
31579a6d4860SXiubo Li 		dev_err(card->dev,
31589a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
31599a6d4860SXiubo Li 		return -EINVAL;
31609a6d4860SXiubo Li 	}
31619a6d4860SXiubo Li 	if (num_widgets & 1) {
31629a6d4860SXiubo Li 		dev_err(card->dev,
31639a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
31649a6d4860SXiubo Li 		return -EINVAL;
31659a6d4860SXiubo Li 	}
31669a6d4860SXiubo Li 
31679a6d4860SXiubo Li 	num_widgets /= 2;
31689a6d4860SXiubo Li 	if (!num_widgets) {
31699a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
31709a6d4860SXiubo Li 			propname);
31719a6d4860SXiubo Li 		return -EINVAL;
31729a6d4860SXiubo Li 	}
31739a6d4860SXiubo Li 
31749a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
31759a6d4860SXiubo Li 			       GFP_KERNEL);
31769a6d4860SXiubo Li 	if (!widgets) {
31779a6d4860SXiubo Li 		dev_err(card->dev,
31789a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
31799a6d4860SXiubo Li 		return -ENOMEM;
31809a6d4860SXiubo Li 	}
31819a6d4860SXiubo Li 
31829a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
31839a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
31849a6d4860SXiubo Li 			2 * i, &template);
31859a6d4860SXiubo Li 		if (ret) {
31869a6d4860SXiubo Li 			dev_err(card->dev,
31879a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
31889a6d4860SXiubo Li 				propname, 2 * i, ret);
31899a6d4860SXiubo Li 			return -EINVAL;
31909a6d4860SXiubo Li 		}
31919a6d4860SXiubo Li 
31929a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
31939a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
31949a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
31959a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
31969a6d4860SXiubo Li 				break;
31979a6d4860SXiubo Li 			}
31989a6d4860SXiubo Li 		}
31999a6d4860SXiubo Li 
32009a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
32019a6d4860SXiubo Li 			dev_err(card->dev,
32029a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
32039a6d4860SXiubo Li 				template);
32049a6d4860SXiubo Li 			return -EINVAL;
32059a6d4860SXiubo Li 		}
32069a6d4860SXiubo Li 
32079a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
32089a6d4860SXiubo Li 						    (2 * i) + 1,
32099a6d4860SXiubo Li 						    &wname);
32109a6d4860SXiubo Li 		if (ret) {
32119a6d4860SXiubo Li 			dev_err(card->dev,
32129a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
32139a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
32149a6d4860SXiubo Li 			return -EINVAL;
32159a6d4860SXiubo Li 		}
32169a6d4860SXiubo Li 
32179a6d4860SXiubo Li 		widgets[i].name = wname;
32189a6d4860SXiubo Li 	}
32199a6d4860SXiubo Li 
32209a6d4860SXiubo Li 	card->dapm_widgets = widgets;
32219a6d4860SXiubo Li 	card->num_dapm_widgets = num_widgets;
32229a6d4860SXiubo Li 
32239a6d4860SXiubo Li 	return 0;
32249a6d4860SXiubo Li }
32259a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
32269a6d4860SXiubo Li 
322789c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
322889c67857SXiubo Li 			      unsigned int *slots,
322989c67857SXiubo Li 			      unsigned int *slot_width)
323089c67857SXiubo Li {
323189c67857SXiubo Li 	u32 val;
323289c67857SXiubo Li 	int ret;
323389c67857SXiubo Li 
323489c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
323589c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
323689c67857SXiubo Li 		if (ret)
323789c67857SXiubo Li 			return ret;
323889c67857SXiubo Li 
323989c67857SXiubo Li 		if (slots)
324089c67857SXiubo Li 			*slots = val;
324189c67857SXiubo Li 	}
324289c67857SXiubo Li 
324389c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
324489c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
324589c67857SXiubo Li 		if (ret)
324689c67857SXiubo Li 			return ret;
324789c67857SXiubo Li 
324889c67857SXiubo Li 		if (slot_width)
324989c67857SXiubo Li 			*slot_width = val;
325089c67857SXiubo Li 	}
325189c67857SXiubo Li 
325289c67857SXiubo Li 	return 0;
325389c67857SXiubo Li }
325489c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
325589c67857SXiubo Li 
3256a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3257a4a54dd5SStephen Warren 				   const char *propname)
3258a4a54dd5SStephen Warren {
3259a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3260e3b1e6a1SMark Brown 	int num_routes;
3261a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3262a4a54dd5SStephen Warren 	int i, ret;
3263a4a54dd5SStephen Warren 
3264a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3265c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
326610e8aa9aSMichał Mirosław 		dev_err(card->dev,
326710e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
326810e8aa9aSMichał Mirosław 			propname);
3269a4a54dd5SStephen Warren 		return -EINVAL;
3270a4a54dd5SStephen Warren 	}
3271a4a54dd5SStephen Warren 	num_routes /= 2;
3272a4a54dd5SStephen Warren 	if (!num_routes) {
3273f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3274a4a54dd5SStephen Warren 			propname);
3275a4a54dd5SStephen Warren 		return -EINVAL;
3276a4a54dd5SStephen Warren 	}
3277a4a54dd5SStephen Warren 
3278e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3279a4a54dd5SStephen Warren 			      GFP_KERNEL);
3280a4a54dd5SStephen Warren 	if (!routes) {
3281a4a54dd5SStephen Warren 		dev_err(card->dev,
3282f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3283a4a54dd5SStephen Warren 		return -EINVAL;
3284a4a54dd5SStephen Warren 	}
3285a4a54dd5SStephen Warren 
3286a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3287a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3288e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3289a4a54dd5SStephen Warren 		if (ret) {
3290c871bd0bSMark Brown 			dev_err(card->dev,
3291c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3292c871bd0bSMark Brown 				propname, 2 * i, ret);
3293a4a54dd5SStephen Warren 			return -EINVAL;
3294a4a54dd5SStephen Warren 		}
3295a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3296e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3297a4a54dd5SStephen Warren 		if (ret) {
3298a4a54dd5SStephen Warren 			dev_err(card->dev,
3299c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3300c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3301a4a54dd5SStephen Warren 			return -EINVAL;
3302a4a54dd5SStephen Warren 		}
3303a4a54dd5SStephen Warren 	}
3304a4a54dd5SStephen Warren 
3305e3b1e6a1SMark Brown 	card->num_dapm_routes = num_routes;
3306a4a54dd5SStephen Warren 	card->dapm_routes = routes;
3307a4a54dd5SStephen Warren 
3308a4a54dd5SStephen Warren 	return 0;
3309a4a54dd5SStephen Warren }
3310a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3311a4a54dd5SStephen Warren 
3312a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3313389cb834SJyri Sarha 				     const char *prefix,
3314389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3315389cb834SJyri Sarha 				     struct device_node **framemaster)
3316a7930ed4SKuninori Morimoto {
3317a7930ed4SKuninori Morimoto 	int ret, i;
3318a7930ed4SKuninori Morimoto 	char prop[128];
3319a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3320a7930ed4SKuninori Morimoto 	int bit, frame;
3321a7930ed4SKuninori Morimoto 	const char *str;
3322a7930ed4SKuninori Morimoto 	struct {
3323a7930ed4SKuninori Morimoto 		char *name;
3324a7930ed4SKuninori Morimoto 		unsigned int val;
3325a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3326a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3327a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3328a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3329a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3330a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3331a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3332a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3333a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3334a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3335a7930ed4SKuninori Morimoto 	};
3336a7930ed4SKuninori Morimoto 
3337a7930ed4SKuninori Morimoto 	if (!prefix)
3338a7930ed4SKuninori Morimoto 		prefix = "";
3339a7930ed4SKuninori Morimoto 
3340a7930ed4SKuninori Morimoto 	/*
3341a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3342a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3343a7930ed4SKuninori Morimoto 	 */
3344a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3345a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3346a7930ed4SKuninori Morimoto 	if (ret == 0) {
3347a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3348a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3349a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3350a7930ed4SKuninori Morimoto 				break;
3351a7930ed4SKuninori Morimoto 			}
3352a7930ed4SKuninori Morimoto 		}
3353a7930ed4SKuninori Morimoto 	}
3354a7930ed4SKuninori Morimoto 
3355a7930ed4SKuninori Morimoto 	/*
33568c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3357a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3358a7930ed4SKuninori Morimoto 	 */
33598c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
33608c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
33618c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
33628c2d6a9fSKuninori Morimoto 	else
33638c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3364a7930ed4SKuninori Morimoto 
3365a7930ed4SKuninori Morimoto 	/*
3366a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3367a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3368a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3369a7930ed4SKuninori Morimoto 	 */
3370a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3371a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3372a7930ed4SKuninori Morimoto 
3373a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3374a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3375a7930ed4SKuninori Morimoto 
3376a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3377a7930ed4SKuninori Morimoto 	case 0x11:
3378a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3379a7930ed4SKuninori Morimoto 		break;
3380a7930ed4SKuninori Morimoto 	case 0x10:
3381a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3382a7930ed4SKuninori Morimoto 		break;
3383a7930ed4SKuninori Morimoto 	case 0x01:
3384a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3385a7930ed4SKuninori Morimoto 		break;
3386a7930ed4SKuninori Morimoto 	default:
3387a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3388a7930ed4SKuninori Morimoto 		break;
3389a7930ed4SKuninori Morimoto 	}
3390a7930ed4SKuninori Morimoto 
3391a7930ed4SKuninori Morimoto 	/*
3392a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3393a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3394a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3395a7930ed4SKuninori Morimoto 	 */
3396a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3397a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3398389cb834SJyri Sarha 	if (bit && bitclkmaster)
3399389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3400a7930ed4SKuninori Morimoto 
3401a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3402a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3403389cb834SJyri Sarha 	if (frame && framemaster)
3404389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3405a7930ed4SKuninori Morimoto 
3406a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3407a7930ed4SKuninori Morimoto 	case 0x11:
3408a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3409a7930ed4SKuninori Morimoto 		break;
3410a7930ed4SKuninori Morimoto 	case 0x10:
3411a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3412a7930ed4SKuninori Morimoto 		break;
3413a7930ed4SKuninori Morimoto 	case 0x01:
3414a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3415a7930ed4SKuninori Morimoto 		break;
3416a7930ed4SKuninori Morimoto 	default:
3417a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3418a7930ed4SKuninori Morimoto 		break;
3419a7930ed4SKuninori Morimoto 	}
3420a7930ed4SKuninori Morimoto 
3421a7930ed4SKuninori Morimoto 	return format;
3422a7930ed4SKuninori Morimoto }
3423a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3424a7930ed4SKuninori Morimoto 
342593b0f3eeSJean-Francois Moine static int snd_soc_get_dai_name(struct of_phandle_args *args,
3426cb470087SKuninori Morimoto 				const char **dai_name)
3427cb470087SKuninori Morimoto {
3428cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
342993b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3430cb470087SKuninori Morimoto 
3431cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3432cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
343393b0f3eeSJean-Francois Moine 		if (pos->dev->of_node != args->np)
3434cb470087SKuninori Morimoto 			continue;
3435cb470087SKuninori Morimoto 
34366833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
343793b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
343893b0f3eeSJean-Francois Moine 							     args,
343993b0f3eeSJean-Francois Moine 							     dai_name);
34406833c452SKuninori Morimoto 		} else {
34416833c452SKuninori Morimoto 			int id = -1;
34426833c452SKuninori Morimoto 
344393b0f3eeSJean-Francois Moine 			switch (args->args_count) {
34446833c452SKuninori Morimoto 			case 0:
34456833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
34466833c452SKuninori Morimoto 				break;
34476833c452SKuninori Morimoto 			case 1:
344893b0f3eeSJean-Francois Moine 				id = args->args[0];
34496833c452SKuninori Morimoto 				break;
34506833c452SKuninori Morimoto 			default:
34516833c452SKuninori Morimoto 				/* not supported */
3452cb470087SKuninori Morimoto 				break;
3453cb470087SKuninori Morimoto 			}
3454cb470087SKuninori Morimoto 
34556833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
34566833c452SKuninori Morimoto 				ret = -EINVAL;
34573dcba280SNicolin Chen 				continue;
34586833c452SKuninori Morimoto 			}
3459e41975edSXiubo Li 
3460e41975edSXiubo Li 			ret = 0;
3461e41975edSXiubo Li 
3462e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3463e41975edSXiubo Li 			if (!*dai_name)
3464e41975edSXiubo Li 				*dai_name = pos->name;
34656833c452SKuninori Morimoto 		}
34666833c452SKuninori Morimoto 
3467cb470087SKuninori Morimoto 		break;
3468cb470087SKuninori Morimoto 	}
3469cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
347093b0f3eeSJean-Francois Moine 	return ret;
347193b0f3eeSJean-Francois Moine }
347293b0f3eeSJean-Francois Moine 
347393b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
347493b0f3eeSJean-Francois Moine 			    const char **dai_name)
347593b0f3eeSJean-Francois Moine {
347693b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
347793b0f3eeSJean-Francois Moine 	int ret;
347893b0f3eeSJean-Francois Moine 
347993b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
348093b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
348193b0f3eeSJean-Francois Moine 	if (ret)
348293b0f3eeSJean-Francois Moine 		return ret;
348393b0f3eeSJean-Francois Moine 
348493b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3485cb470087SKuninori Morimoto 
3486cb470087SKuninori Morimoto 	of_node_put(args.np);
3487cb470087SKuninori Morimoto 
3488cb470087SKuninori Morimoto 	return ret;
3489cb470087SKuninori Morimoto }
3490cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3491cb470087SKuninori Morimoto 
349293b0f3eeSJean-Francois Moine /*
349393b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
349493b0f3eeSJean-Francois Moine  * @dev: Card device
349593b0f3eeSJean-Francois Moine  * @of_node: Device node
349693b0f3eeSJean-Francois Moine  * @dai_link: DAI link
349793b0f3eeSJean-Francois Moine  *
349893b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
349993b0f3eeSJean-Francois Moine  * 'sound-dai'.
350093b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
350193b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
350293b0f3eeSJean-Francois Moine  *
350393b0f3eeSJean-Francois Moine  * Returns 0 for success
350493b0f3eeSJean-Francois Moine  */
350593b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
350693b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
350793b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
350893b0f3eeSJean-Francois Moine {
350993b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
351093b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
351193b0f3eeSJean-Francois Moine 	char *name;
351293b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
351393b0f3eeSJean-Francois Moine 
351493b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
351593b0f3eeSJean-Francois Moine 	name = "sound-dai";
351693b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
351793b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
351893b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
351993b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
352093b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
352193b0f3eeSJean-Francois Moine 		else
352293b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
352393b0f3eeSJean-Francois Moine 		return num_codecs;
352493b0f3eeSJean-Francois Moine 	}
352593b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
352693b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
352793b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
352893b0f3eeSJean-Francois Moine 	if (!component)
352993b0f3eeSJean-Francois Moine 		return -ENOMEM;
353093b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
353193b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
353293b0f3eeSJean-Francois Moine 
353393b0f3eeSJean-Francois Moine 	/* Parse the list */
353493b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
353593b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
353693b0f3eeSJean-Francois Moine 	     index++, component++) {
353793b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
353893b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
353993b0f3eeSJean-Francois Moine 						  index, &args);
354093b0f3eeSJean-Francois Moine 		if (ret)
354193b0f3eeSJean-Francois Moine 			goto err;
354293b0f3eeSJean-Francois Moine 		component->of_node = args.np;
354393b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
354493b0f3eeSJean-Francois Moine 		if (ret < 0)
354593b0f3eeSJean-Francois Moine 			goto err;
354693b0f3eeSJean-Francois Moine 	}
354793b0f3eeSJean-Francois Moine 	return 0;
354893b0f3eeSJean-Francois Moine err:
354993b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
355093b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
355193b0f3eeSJean-Francois Moine 	     index++, component++) {
355293b0f3eeSJean-Francois Moine 		if (!component->of_node)
355393b0f3eeSJean-Francois Moine 			break;
355493b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
355593b0f3eeSJean-Francois Moine 		component->of_node = NULL;
355693b0f3eeSJean-Francois Moine 	}
355793b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
355893b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
355993b0f3eeSJean-Francois Moine 	return ret;
356093b0f3eeSJean-Francois Moine }
356193b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
356293b0f3eeSJean-Francois Moine 
3563c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3564db2a4165SFrank Mandarino {
3565db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
35668a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
35678a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
35680837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
35698a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3570db2a4165SFrank Mandarino 	}
3571c3c5a19aSMark Brown 
35728a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3573c3c5a19aSMark Brown 				 &codec_list_fops))
3574c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3575c3c5a19aSMark Brown 
35768a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3577f3208780SMark Brown 				 &dai_list_fops))
3578f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
357919c7ac27SMark Brown 
35808a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
358119c7ac27SMark Brown 				 &platform_list_fops))
358219c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3583db2a4165SFrank Mandarino #endif
3584db2a4165SFrank Mandarino 
3585fb257897SMark Brown 	snd_soc_util_init();
3586fb257897SMark Brown 
3587db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3588db2a4165SFrank Mandarino }
35894abe8e16SMark Brown module_init(snd_soc_init);
3590db2a4165SFrank Mandarino 
3591db2a4165SFrank Mandarino static void __exit snd_soc_exit(void)
3592db2a4165SFrank Mandarino {
3593fb257897SMark Brown 	snd_soc_util_exit();
3594fb257897SMark Brown 
3595db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
35968a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3597db2a4165SFrank Mandarino #endif
3598db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3599db2a4165SFrank Mandarino }
3600db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3601db2a4165SFrank Mandarino 
3602db2a4165SFrank Mandarino /* Module information */
3603db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3604db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3605db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3606db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3607