xref: /openbmc/linux/sound/soc/soc-core.c (revision 9178feb4538e055bf22be44c38b90cc31d2baf99)
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>
438a978234SLiam Girdwood #include <sound/soc-topology.h>
44db2a4165SFrank Mandarino #include <sound/initval.h>
45db2a4165SFrank Mandarino 
46a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
47a8b1d34fSMark Brown #include <trace/events/asoc.h>
48a8b1d34fSMark Brown 
49f0fba2adSLiam Girdwood #define NAME_SIZE	32
50f0fba2adSLiam Girdwood 
51384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
528a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
538a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
54384c89e2SMark Brown #endif
55384c89e2SMark Brown 
56c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5712a48a8cSMark Brown static LIST_HEAD(platform_list);
580d0cf00aSMark Brown static LIST_HEAD(codec_list);
59030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
60c5af3a2eSMark Brown 
61db2a4165SFrank Mandarino /*
62db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
63db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
64db2a4165SFrank Mandarino  * between two audio tracks.
65db2a4165SFrank Mandarino  */
66db2a4165SFrank Mandarino static int pmdown_time = 5000;
67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69db2a4165SFrank Mandarino 
702bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
712bc9a81eSDimitris Papastamos  * a particular given value */
722bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
732bc9a81eSDimitris Papastamos {
742bc9a81eSDimitris Papastamos 	int c = 0;
752bc9a81eSDimitris Papastamos 	int i;
762bc9a81eSDimitris Papastamos 
772bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
782bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
792bc9a81eSDimitris Papastamos 			break;
802bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
812bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
822bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
832bc9a81eSDimitris Papastamos 	else
842bc9a81eSDimitris Papastamos 		c /= 8;
852bc9a81eSDimitris Papastamos 	return c;
862bc9a81eSDimitris Papastamos }
872bc9a81eSDimitris Papastamos 
8813fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
8913fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9013fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9113fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
922624d5faSMark Brown {
9300b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9400b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9513fd179fSDimitris Papastamos 	int ret;
9613fd179fSDimitris Papastamos 
9713fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
9813fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
9913fd179fSDimitris Papastamos 		return -EINVAL;
10013fd179fSDimitris Papastamos 
101d6b6c2caSTakashi Iwai 	sprintf(buf, "%.*x: ", wordsize, reg);
102d6b6c2caSTakashi Iwai 	buf += wordsize + 2;
103d6b6c2caSTakashi Iwai 
10413fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
105d6b6c2caSTakashi Iwai 	if (ret < 0)
106d6b6c2caSTakashi Iwai 		memset(buf, 'X', regsize);
107d6b6c2caSTakashi Iwai 	else
108d6b6c2caSTakashi Iwai 		sprintf(buf, "%.*x", regsize, ret);
109d6b6c2caSTakashi Iwai 	buf[regsize] = '\n';
110d6b6c2caSTakashi Iwai 	/* no NUL-termination needed */
11113fd179fSDimitris Papastamos 	return 0;
11213fd179fSDimitris Papastamos }
11313fd179fSDimitris Papastamos 
11413fd179fSDimitris Papastamos /* codec register dump */
11513fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
11613fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
11713fd179fSDimitris Papastamos {
11813fd179fSDimitris Papastamos 	int i, step = 1;
1192bc9a81eSDimitris Papastamos 	int wordsize, regsize;
12013fd179fSDimitris Papastamos 	int len;
12113fd179fSDimitris Papastamos 	size_t total = 0;
12213fd179fSDimitris Papastamos 	loff_t p = 0;
1232bc9a81eSDimitris Papastamos 
12400b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
12500b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1262624d5faSMark Brown 
12713fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
12813fd179fSDimitris Papastamos 
129f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1302624d5faSMark Brown 		return 0;
1312624d5faSMark Brown 
132f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
133f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1342624d5faSMark Brown 
135f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
13613fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
13713fd179fSDimitris Papastamos 		 * entries for the default case */
13813fd179fSDimitris Papastamos 		if (p >= pos) {
13913fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1402624d5faSMark Brown 				break;
14113fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
14213fd179fSDimitris Papastamos 			total += len;
14313fd179fSDimitris Papastamos 		}
14413fd179fSDimitris Papastamos 		p += len;
14513fd179fSDimitris Papastamos 	}
1462624d5faSMark Brown 
14713fd179fSDimitris Papastamos 	total = min(total, count - 1);
1482624d5faSMark Brown 
14913fd179fSDimitris Papastamos 	return total;
1502624d5faSMark Brown }
15113fd179fSDimitris Papastamos 
1522624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1532624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1542624d5faSMark Brown {
15536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
156f0fba2adSLiam Girdwood 
15713fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1582624d5faSMark Brown }
1592624d5faSMark Brown 
1602624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1612624d5faSMark Brown 
162dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
163dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
164dbe21408SMark Brown {
16536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
166dbe21408SMark Brown 
167f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
168dbe21408SMark Brown }
169dbe21408SMark Brown 
170dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
171dbe21408SMark Brown 			       struct device_attribute *attr,
172dbe21408SMark Brown 			       const char *buf, size_t count)
173dbe21408SMark Brown {
17436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
175c593b520SMark Brown 	int ret;
176dbe21408SMark Brown 
177b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
178c593b520SMark Brown 	if (ret)
179c593b520SMark Brown 		return ret;
180dbe21408SMark Brown 
181dbe21408SMark Brown 	return count;
182dbe21408SMark Brown }
183dbe21408SMark Brown 
184dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
185dbe21408SMark Brown 
186d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
187d29697dcSTakashi Iwai 	&dev_attr_codec_reg.attr,
188d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
189d29697dcSTakashi Iwai 	NULL
190d29697dcSTakashi Iwai };
191d29697dcSTakashi Iwai 
192d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
193d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
194d29697dcSTakashi Iwai {
195d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
196d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
197d29697dcSTakashi Iwai 
198d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
199d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
200d29697dcSTakashi Iwai 	return rtd->codec ? attr->mode : 0; /* enabled only with codec */
201d29697dcSTakashi Iwai }
202d29697dcSTakashi Iwai 
203d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
204d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
205d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
206d29697dcSTakashi Iwai };
207d29697dcSTakashi Iwai 
208d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = {
209d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
210d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
211d29697dcSTakashi Iwai };
212d29697dcSTakashi Iwai 
213d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
214d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
215d29697dcSTakashi Iwai 	&soc_dev_roup,
216d29697dcSTakashi Iwai 	NULL
217d29697dcSTakashi Iwai };
218d29697dcSTakashi Iwai 
2192624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2202624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2212624d5faSMark Brown 				   size_t count, loff_t *ppos)
2222624d5faSMark Brown {
2232624d5faSMark Brown 	ssize_t ret;
2242624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
22513fd179fSDimitris Papastamos 	char *buf;
22613fd179fSDimitris Papastamos 
22713fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22813fd179fSDimitris Papastamos 		return -EINVAL;
22913fd179fSDimitris Papastamos 
23013fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2312624d5faSMark Brown 	if (!buf)
2322624d5faSMark Brown 		return -ENOMEM;
23313fd179fSDimitris Papastamos 
23413fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
23513fd179fSDimitris Papastamos 	if (ret >= 0) {
23613fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23713fd179fSDimitris Papastamos 			kfree(buf);
23813fd179fSDimitris Papastamos 			return -EFAULT;
23913fd179fSDimitris Papastamos 		}
24013fd179fSDimitris Papastamos 		*ppos += ret;
24113fd179fSDimitris Papastamos 	}
24213fd179fSDimitris Papastamos 
2432624d5faSMark Brown 	kfree(buf);
2442624d5faSMark Brown 	return ret;
2452624d5faSMark Brown }
2462624d5faSMark Brown 
2472624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2482624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2492624d5faSMark Brown {
2502624d5faSMark Brown 	char buf[32];
25134e268d8SStephen Boyd 	size_t buf_size;
2522624d5faSMark Brown 	char *start = buf;
2532624d5faSMark Brown 	unsigned long reg, value;
2542624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
255b785a492SJingoo Han 	int ret;
2562624d5faSMark Brown 
2572624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2582624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2592624d5faSMark Brown 		return -EFAULT;
2602624d5faSMark Brown 	buf[buf_size] = 0;
2612624d5faSMark Brown 
2622624d5faSMark Brown 	while (*start == ' ')
2632624d5faSMark Brown 		start++;
2642624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2652624d5faSMark Brown 	while (*start == ' ')
2662624d5faSMark Brown 		start++;
267b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
268b785a492SJingoo Han 	if (ret)
269b785a492SJingoo Han 		return ret;
2700d51a9cbSMark Brown 
2710d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
272373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2730d51a9cbSMark Brown 
274e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2752624d5faSMark Brown 	return buf_size;
2762624d5faSMark Brown }
2772624d5faSMark Brown 
2782624d5faSMark Brown static const struct file_operations codec_reg_fops = {
279234e3405SStephen Boyd 	.open = simple_open,
2802624d5faSMark Brown 	.read = codec_reg_read_file,
2812624d5faSMark Brown 	.write = codec_reg_write_file,
2826038f373SArnd Bergmann 	.llseek = default_llseek,
2832624d5faSMark Brown };
2842624d5faSMark Brown 
28581c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
286e73f3de5SRussell King {
2876553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
2886553bf06SLars-Peter Clausen 		return;
2896553bf06SLars-Peter Clausen 
29081c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
29181c7cfd1SLars-Peter Clausen 		char *name;
292e73f3de5SRussell King 
29381c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
29481c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
29581c7cfd1SLars-Peter Clausen 		if (name) {
29681c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
29781c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
29881c7cfd1SLars-Peter Clausen 			kfree(name);
29981c7cfd1SLars-Peter Clausen 		}
30081c7cfd1SLars-Peter Clausen 	} else {
30181c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
30281c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
303e73f3de5SRussell King 	}
304e73f3de5SRussell King 
30581c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
30681c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
30781c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3082624d5faSMark Brown 		return;
3092624d5faSMark Brown 	}
3102624d5faSMark Brown 
31181c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
31281c7cfd1SLars-Peter Clausen 		component->debugfs_root);
31381c7cfd1SLars-Peter Clausen 
31481c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
31581c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
31681c7cfd1SLars-Peter Clausen }
31781c7cfd1SLars-Peter Clausen 
31881c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
31981c7cfd1SLars-Peter Clausen {
32081c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
32181c7cfd1SLars-Peter Clausen }
32281c7cfd1SLars-Peter Clausen 
32381c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
32481c7cfd1SLars-Peter Clausen {
32581c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
32681c7cfd1SLars-Peter Clausen 
3272624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
32881c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3292624d5faSMark Brown 						 codec, &codec_reg_fops);
3302624d5faSMark Brown 	if (!codec->debugfs_reg)
33110e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
33210e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
333731f1ab2SSebastien Guiriec }
334731f1ab2SSebastien Guiriec 
335c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
336c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
337c3c5a19aSMark Brown {
338c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3392b194f9dSMark Brown 	ssize_t len, ret = 0;
340c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
341c3c5a19aSMark Brown 
342c3c5a19aSMark Brown 	if (!buf)
343c3c5a19aSMark Brown 		return -ENOMEM;
344c3c5a19aSMark Brown 
34534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
34634e81ab4SLars-Peter Clausen 
3472b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3482b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
349f4333203SLars-Peter Clausen 			       codec->component.name);
3502b194f9dSMark Brown 		if (len >= 0)
3512b194f9dSMark Brown 			ret += len;
3522b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3532b194f9dSMark Brown 			ret = PAGE_SIZE;
3542b194f9dSMark Brown 			break;
3552b194f9dSMark Brown 		}
3562b194f9dSMark Brown 	}
357c3c5a19aSMark Brown 
35834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
35934e81ab4SLars-Peter Clausen 
360c3c5a19aSMark Brown 	if (ret >= 0)
361c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
362c3c5a19aSMark Brown 
363c3c5a19aSMark Brown 	kfree(buf);
364c3c5a19aSMark Brown 
365c3c5a19aSMark Brown 	return ret;
366c3c5a19aSMark Brown }
367c3c5a19aSMark Brown 
368c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
369c3c5a19aSMark Brown 	.read = codec_list_read_file,
370c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
371c3c5a19aSMark Brown };
372c3c5a19aSMark Brown 
373f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
374f3208780SMark Brown 				  size_t count, loff_t *ppos)
375f3208780SMark Brown {
376f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3772b194f9dSMark Brown 	ssize_t len, ret = 0;
3781438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
379f3208780SMark Brown 	struct snd_soc_dai *dai;
380f3208780SMark Brown 
381f3208780SMark Brown 	if (!buf)
382f3208780SMark Brown 		return -ENOMEM;
383f3208780SMark Brown 
38434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
38534e81ab4SLars-Peter Clausen 
3861438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3871438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3881438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3891438c2f6SLars-Peter Clausen 				dai->name);
3902b194f9dSMark Brown 			if (len >= 0)
3912b194f9dSMark Brown 				ret += len;
3922b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3932b194f9dSMark Brown 				ret = PAGE_SIZE;
3942b194f9dSMark Brown 				break;
3952b194f9dSMark Brown 			}
3962b194f9dSMark Brown 		}
3971438c2f6SLars-Peter Clausen 	}
398f3208780SMark Brown 
39934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
40034e81ab4SLars-Peter Clausen 
401f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
402f3208780SMark Brown 
403f3208780SMark Brown 	kfree(buf);
404f3208780SMark Brown 
405f3208780SMark Brown 	return ret;
406f3208780SMark Brown }
407f3208780SMark Brown 
408f3208780SMark Brown static const struct file_operations dai_list_fops = {
409f3208780SMark Brown 	.read = dai_list_read_file,
410f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
411f3208780SMark Brown };
412f3208780SMark Brown 
41319c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41419c7ac27SMark Brown 				       char __user *user_buf,
41519c7ac27SMark Brown 				       size_t count, loff_t *ppos)
41619c7ac27SMark Brown {
41719c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4182b194f9dSMark Brown 	ssize_t len, ret = 0;
41919c7ac27SMark Brown 	struct snd_soc_platform *platform;
42019c7ac27SMark Brown 
42119c7ac27SMark Brown 	if (!buf)
42219c7ac27SMark Brown 		return -ENOMEM;
42319c7ac27SMark Brown 
42434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
42534e81ab4SLars-Peter Clausen 
4262b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4272b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
428f4333203SLars-Peter Clausen 			       platform->component.name);
4292b194f9dSMark Brown 		if (len >= 0)
4302b194f9dSMark Brown 			ret += len;
4312b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4322b194f9dSMark Brown 			ret = PAGE_SIZE;
4332b194f9dSMark Brown 			break;
4342b194f9dSMark Brown 		}
4352b194f9dSMark Brown 	}
43619c7ac27SMark Brown 
43734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
43834e81ab4SLars-Peter Clausen 
43919c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
44019c7ac27SMark Brown 
44119c7ac27SMark Brown 	kfree(buf);
44219c7ac27SMark Brown 
44319c7ac27SMark Brown 	return ret;
44419c7ac27SMark Brown }
44519c7ac27SMark Brown 
44619c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44719c7ac27SMark Brown 	.read = platform_list_read_file,
44819c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
44919c7ac27SMark Brown };
45019c7ac27SMark Brown 
451a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
452a6052154SJarkko Nikula {
4536553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
4546553bf06SLars-Peter Clausen 		return;
4556553bf06SLars-Peter Clausen 
456a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4578a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4583a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
459a6052154SJarkko Nikula 		dev_warn(card->dev,
4607c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4613a45b867SJarkko Nikula 		return;
4623a45b867SJarkko Nikula 	}
4633a45b867SJarkko Nikula 
4643a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4653a45b867SJarkko Nikula 						    card->debugfs_card_root,
4663a45b867SJarkko Nikula 						    &card->pop_time);
4673a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4683a45b867SJarkko Nikula 		dev_warn(card->dev,
469f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
470a6052154SJarkko Nikula }
471a6052154SJarkko Nikula 
472a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
473a6052154SJarkko Nikula {
474a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
475a6052154SJarkko Nikula }
476a6052154SJarkko Nikula 
4776553bf06SLars-Peter Clausen 
4786553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
4796553bf06SLars-Peter Clausen {
4806553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4816553bf06SLars-Peter Clausen 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4826553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
4836553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
4846553bf06SLars-Peter Clausen 		return;
4856553bf06SLars-Peter Clausen 	}
4866553bf06SLars-Peter Clausen 
4876553bf06SLars-Peter Clausen 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4886553bf06SLars-Peter Clausen 				 &codec_list_fops))
4896553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4906553bf06SLars-Peter Clausen 
4916553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4926553bf06SLars-Peter Clausen 				 &dai_list_fops))
4936553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4946553bf06SLars-Peter Clausen 
4956553bf06SLars-Peter Clausen 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4966553bf06SLars-Peter Clausen 				 &platform_list_fops))
4976553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4986553bf06SLars-Peter Clausen }
4996553bf06SLars-Peter Clausen 
5006553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
5016553bf06SLars-Peter Clausen {
5026553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
5036553bf06SLars-Peter Clausen }
5046553bf06SLars-Peter Clausen 
5052624d5faSMark Brown #else
5062624d5faSMark Brown 
50781c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
50881c7cfd1SLars-Peter Clausen 
50981c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
51081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
5112624d5faSMark Brown {
5122624d5faSMark Brown }
5132624d5faSMark Brown 
51481c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
51581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
516731f1ab2SSebastien Guiriec {
517731f1ab2SSebastien Guiriec }
518731f1ab2SSebastien Guiriec 
519b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
520b95fccbcSAxel Lin {
521b95fccbcSAxel Lin }
522b95fccbcSAxel Lin 
523b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
524b95fccbcSAxel Lin {
525b95fccbcSAxel Lin }
5266553bf06SLars-Peter Clausen 
5276553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
5286553bf06SLars-Peter Clausen {
5296553bf06SLars-Peter Clausen }
5306553bf06SLars-Peter Clausen 
5316553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
5326553bf06SLars-Peter Clausen {
5336553bf06SLars-Peter Clausen }
5346553bf06SLars-Peter Clausen 
5352624d5faSMark Brown #endif
5362624d5faSMark Brown 
53747c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
53847c88fffSLiam Girdwood 		const char *dai_link, int stream)
53947c88fffSLiam Girdwood {
5401a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
54147c88fffSLiam Girdwood 
5421a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
5431a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
5441a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
5451a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
54647c88fffSLiam Girdwood 	}
547f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
54847c88fffSLiam Girdwood 	return NULL;
54947c88fffSLiam Girdwood }
55047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
55147c88fffSLiam Girdwood 
5521a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
5531a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
5541a497983SMengdong Lin {
5551a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5561a497983SMengdong Lin 
5571a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
5581a497983SMengdong Lin 	if (!rtd)
5591a497983SMengdong Lin 		return NULL;
5601a497983SMengdong Lin 
5611a497983SMengdong Lin 	rtd->card = card;
5621a497983SMengdong Lin 	rtd->dai_link = dai_link;
5631a497983SMengdong Lin 	rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
5641a497983SMengdong Lin 					dai_link->num_codecs,
5651a497983SMengdong Lin 					GFP_KERNEL);
5661a497983SMengdong Lin 	if (!rtd->codec_dais) {
5671a497983SMengdong Lin 		kfree(rtd);
5681a497983SMengdong Lin 		return NULL;
5691a497983SMengdong Lin 	}
5701a497983SMengdong Lin 
5711a497983SMengdong Lin 	return rtd;
5721a497983SMengdong Lin }
5731a497983SMengdong Lin 
5741a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
5751a497983SMengdong Lin {
5761a497983SMengdong Lin 	if (rtd && rtd->codec_dais)
5771a497983SMengdong Lin 		kfree(rtd->codec_dais);
5781a497983SMengdong Lin 	kfree(rtd);
5791a497983SMengdong Lin }
5801a497983SMengdong Lin 
5811a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
5821a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
5831a497983SMengdong Lin {
5841a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
5851a497983SMengdong Lin 	rtd->num = card->num_rtd;
5861a497983SMengdong Lin 	card->num_rtd++;
5871a497983SMengdong Lin }
5881a497983SMengdong Lin 
5891a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
5901a497983SMengdong Lin {
5911a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
5921a497983SMengdong Lin 
5931a497983SMengdong Lin 	list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
5941a497983SMengdong Lin 		list_del(&rtd->list);
5951a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
5961a497983SMengdong Lin 	}
5971a497983SMengdong Lin 
5981a497983SMengdong Lin 	card->num_rtd = 0;
5991a497983SMengdong Lin }
6001a497983SMengdong Lin 
60147c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
60247c88fffSLiam Girdwood 		const char *dai_link)
60347c88fffSLiam Girdwood {
6041a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
60547c88fffSLiam Girdwood 
6061a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6071a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
6081a497983SMengdong Lin 			return rtd;
60947c88fffSLiam Girdwood 	}
610f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
61147c88fffSLiam Girdwood 	return NULL;
61247c88fffSLiam Girdwood }
61347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
61447c88fffSLiam Girdwood 
6159d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
6169d58a077SRichard Fitzgerald {
6179d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
6189d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
6199d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
6209d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
6219d58a077SRichard Fitzgerald 	 */
6229d58a077SRichard Fitzgerald }
6239d58a077SRichard Fitzgerald 
6246f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
625db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
6266f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
627db2a4165SFrank Mandarino {
6286f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
629d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6301a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6311a497983SMengdong Lin 	int i;
632db2a4165SFrank Mandarino 
633c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
634c5599b87SLars-Peter Clausen 	if (!card->instantiated)
635e3509ff0SDaniel Mack 		return 0;
636e3509ff0SDaniel Mack 
6376ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
6386ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
6396ed25978SAndy Green 	 */
640f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
641f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
642f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
6436ed25978SAndy Green 
6446ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
645f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6466ed25978SAndy Green 
647a00f90f9SMark Brown 	/* mute any active DACs */
6481a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6493efab7dcSMark Brown 
6501a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6513efab7dcSMark Brown 			continue;
6523efab7dcSMark Brown 
6531a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
6541a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
65588bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
65688bd870fSBenoit Cousson 
657f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
658f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
659db2a4165SFrank Mandarino 		}
66088bd870fSBenoit Cousson 	}
661db2a4165SFrank Mandarino 
6624ccab3e7SLiam Girdwood 	/* suspend all pcms */
6631a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6641a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6653efab7dcSMark Brown 			continue;
6663efab7dcSMark Brown 
6671a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
6683efab7dcSMark Brown 	}
6694ccab3e7SLiam Girdwood 
67087506549SMark Brown 	if (card->suspend_pre)
67170b2ac12SMark Brown 		card->suspend_pre(card);
672db2a4165SFrank Mandarino 
6731a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6741a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6753efab7dcSMark Brown 
6761a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6773efab7dcSMark Brown 			continue;
6783efab7dcSMark Brown 
679bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
680f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
681db2a4165SFrank Mandarino 	}
682db2a4165SFrank Mandarino 
68337660b6dSLars-Peter Clausen 	/* close any waiting streams */
6841a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
6851a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
686db2a4165SFrank Mandarino 
6871a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6883efab7dcSMark Brown 
6891a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6903efab7dcSMark Brown 			continue;
6913efab7dcSMark Brown 
6921a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6937bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
694db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
695f0fba2adSLiam Girdwood 
6961a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6977bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
698db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
699db2a4165SFrank Mandarino 	}
700db2a4165SFrank Mandarino 
7018be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7028be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
703e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
704e2d32ff6SMark Brown 
705*9178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
706d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
707d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
708d9fc4063SKuninori Morimoto 
709*9178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
7101547aba9SMark Brown 		 * bias _ON and should not be suspended. */
711*9178feb4SKuninori Morimoto 		if (!component->suspended) {
7124890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7131547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
714125a25daSMark Brown 				/*
715*9178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
716125a25daSMark Brown 				 * bias off then being in STANDBY
717125a25daSMark Brown 				 * means it's doing something,
718125a25daSMark Brown 				 * otherwise fall through.
719125a25daSMark Brown 				 */
7204890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
721*9178feb4SKuninori Morimoto 					dev_dbg(component->dev,
72210e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
723125a25daSMark Brown 					break;
724125a25daSMark Brown 				}
725a8093297SLars-Peter Clausen 
7261547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
727*9178feb4SKuninori Morimoto 				if (component->suspend)
728*9178feb4SKuninori Morimoto 					component->suspend(component);
729*9178feb4SKuninori Morimoto 				component->suspended = 1;
730*9178feb4SKuninori Morimoto 				if (component->regmap)
731*9178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
732988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
733*9178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
7341547aba9SMark Brown 				break;
7351547aba9SMark Brown 			default:
736*9178feb4SKuninori Morimoto 				dev_dbg(component->dev,
737*9178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
7381547aba9SMark Brown 				break;
7391547aba9SMark Brown 			}
7401547aba9SMark Brown 		}
741f0fba2adSLiam Girdwood 	}
742db2a4165SFrank Mandarino 
7431a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7441a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7453efab7dcSMark Brown 
7461a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7473efab7dcSMark Brown 			continue;
7483efab7dcSMark Brown 
749bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
750f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
751988e8cc4SNicolin Chen 
752988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
753988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
754db2a4165SFrank Mandarino 	}
755db2a4165SFrank Mandarino 
75687506549SMark Brown 	if (card->suspend_post)
75770b2ac12SMark Brown 		card->suspend_post(card);
758db2a4165SFrank Mandarino 
759db2a4165SFrank Mandarino 	return 0;
760db2a4165SFrank Mandarino }
7616f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
762db2a4165SFrank Mandarino 
7636ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7646ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7656ed25978SAndy Green  */
7666ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
767db2a4165SFrank Mandarino {
768f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
769f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7701a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
771d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
7721a497983SMengdong Lin 	int i;
773db2a4165SFrank Mandarino 
7746ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7756ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7766ed25978SAndy Green 	 */
7776ed25978SAndy Green 
778f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7796ed25978SAndy Green 
7809949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
781f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7829949788bSMark Brown 
78387506549SMark Brown 	if (card->resume_pre)
78470b2ac12SMark Brown 		card->resume_pre(card);
785db2a4165SFrank Mandarino 
786bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
7871a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7881a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7893efab7dcSMark Brown 
7901a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7913efab7dcSMark Brown 			continue;
7923efab7dcSMark Brown 
793bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
794f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
795db2a4165SFrank Mandarino 	}
796db2a4165SFrank Mandarino 
797d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
798*9178feb4SKuninori Morimoto 		if (component->suspended) {
799*9178feb4SKuninori Morimoto 			if (component->resume)
800*9178feb4SKuninori Morimoto 				component->resume(component);
801*9178feb4SKuninori Morimoto 			component->suspended = 0;
8021547aba9SMark Brown 		}
803f0fba2adSLiam Girdwood 	}
804db2a4165SFrank Mandarino 
8051a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8063efab7dcSMark Brown 
8071a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8083efab7dcSMark Brown 			continue;
8093efab7dcSMark Brown 
8101a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
811d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
812db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
813f0fba2adSLiam Girdwood 
8141a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
815d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
816db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
817db2a4165SFrank Mandarino 	}
818db2a4165SFrank Mandarino 
8193ff3f64bSMark Brown 	/* unmute any active DACs */
8201a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8213efab7dcSMark Brown 
8221a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8233efab7dcSMark Brown 			continue;
8243efab7dcSMark Brown 
8251a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8261a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
82788bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
82888bd870fSBenoit Cousson 
829f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
830f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
831db2a4165SFrank Mandarino 		}
83288bd870fSBenoit Cousson 	}
833db2a4165SFrank Mandarino 
8341a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8351a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8363efab7dcSMark Brown 
8371a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8383efab7dcSMark Brown 			continue;
8393efab7dcSMark Brown 
840bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
841f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
842db2a4165SFrank Mandarino 	}
843db2a4165SFrank Mandarino 
84487506549SMark Brown 	if (card->resume_post)
84570b2ac12SMark Brown 		card->resume_post(card);
846db2a4165SFrank Mandarino 
847f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8486ed25978SAndy Green 
8498be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8508be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
851e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8521a7aaa58SJeeja KP 
8531a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
8541a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
8556ed25978SAndy Green }
8566ed25978SAndy Green 
8576ed25978SAndy Green /* powers up audio subsystem after a suspend */
8586f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8596ed25978SAndy Green {
8606f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
861bc263214SLars-Peter Clausen 	bool bus_control = false;
8621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
863b9dd94a8SPeter Ujfalusi 
864c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
865c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8665ff1ddf2SEric Miao 		return 0;
8675ff1ddf2SEric Miao 
868988e8cc4SNicolin Chen 	/* activate pins from sleep state */
8691a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
87088bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
87188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87288bd870fSBenoit Cousson 		int j;
87388bd870fSBenoit Cousson 
874988e8cc4SNicolin Chen 		if (cpu_dai->active)
875988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
87688bd870fSBenoit Cousson 
87788bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
87888bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
879988e8cc4SNicolin Chen 			if (codec_dai->active)
880988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
881988e8cc4SNicolin Chen 		}
88288bd870fSBenoit Cousson 	}
883988e8cc4SNicolin Chen 
884bc263214SLars-Peter Clausen 	/*
885bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
886bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
887bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
88864ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
88964ab9baaSMark Brown 	 */
8901a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8911a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
892bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
89382e14e8bSStephen Warren 	}
894bc263214SLars-Peter Clausen 	if (bus_control) {
895bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
89664ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
89764ab9baaSMark Brown 	} else {
898f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8996308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
900f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
901f0fba2adSLiam Girdwood 	}
9026ed25978SAndy Green 
903db2a4165SFrank Mandarino 	return 0;
904db2a4165SFrank Mandarino }
9056f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
906db2a4165SFrank Mandarino #else
9076f8ab4acSMark Brown #define snd_soc_suspend NULL
9086f8ab4acSMark Brown #define snd_soc_resume NULL
909db2a4165SFrank Mandarino #endif
910db2a4165SFrank Mandarino 
91185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
91202a06d30SBarry Song };
91302a06d30SBarry Song 
91465d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
91565d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
91612023a9aSMisael Lopez Cruz {
91765d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
91812023a9aSMisael Lopez Cruz 
91934e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
92034e81ab4SLars-Peter Clausen 
92165d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
92265d9361fSLars-Peter Clausen 		if (of_node) {
92365d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
92465d9361fSLars-Peter Clausen 				return component;
92565d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
92665d9361fSLars-Peter Clausen 			return component;
92712023a9aSMisael Lopez Cruz 		}
92812023a9aSMisael Lopez Cruz 	}
92912023a9aSMisael Lopez Cruz 
93012023a9aSMisael Lopez Cruz 	return NULL;
93112023a9aSMisael Lopez Cruz }
93212023a9aSMisael Lopez Cruz 
933fbb88b5cSMengdong Lin /**
934fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
935fbb88b5cSMengdong Lin  *
936fbb88b5cSMengdong Lin  * @dlc: name of the DAI and optional component info to match
937fbb88b5cSMengdong Lin  *
938fbb88b5cSMengdong Lin  * This function will search all regsitered components and their DAIs to
939fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
940fbb88b5cSMengdong Lin  * should also match if being specified.
941fbb88b5cSMengdong Lin  *
942fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
943fbb88b5cSMengdong Lin  */
944305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
94514621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
94612023a9aSMisael Lopez Cruz {
94714621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
94814621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
9493e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
95012023a9aSMisael Lopez Cruz 
95134e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
95234e81ab4SLars-Peter Clausen 
95314621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
95414621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
9553e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
9563e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
9573e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
9583e0aa8d8SJyri Sarha 
9593e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
96012023a9aSMisael Lopez Cruz 			continue;
9611ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
96212023a9aSMisael Lopez Cruz 			continue;
96314621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
96414621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
96514621c7eSLars-Peter Clausen 				continue;
96612023a9aSMisael Lopez Cruz 
96714621c7eSLars-Peter Clausen 			return dai;
96812023a9aSMisael Lopez Cruz 		}
96912023a9aSMisael Lopez Cruz 	}
97012023a9aSMisael Lopez Cruz 
97112023a9aSMisael Lopez Cruz 	return NULL;
97212023a9aSMisael Lopez Cruz }
973305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
97412023a9aSMisael Lopez Cruz 
97549a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
97649a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
977db2a4165SFrank Mandarino {
97849a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
97949a5ba1cSMengdong Lin 
98049a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
98149a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
98249a5ba1cSMengdong Lin 			return true;
98349a5ba1cSMengdong Lin 	}
98449a5ba1cSMengdong Lin 
98549a5ba1cSMengdong Lin 	return false;
98649a5ba1cSMengdong Lin }
98749a5ba1cSMengdong Lin 
9886f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
9896f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
990db2a4165SFrank Mandarino {
9911a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
99288bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
99314621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
9941a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
995435c5e25SMark Brown 	struct snd_soc_platform *platform;
996848dd8beSMark Brown 	const char *platform_name;
99788bd870fSBenoit Cousson 	int i;
998db2a4165SFrank Mandarino 
9996f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10006308419aSMark Brown 
100149a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
100249a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
100349a5ba1cSMengdong Lin 			dai_link->name);
100449a5ba1cSMengdong Lin 		return 0;
100549a5ba1cSMengdong Lin 	}
1006db2a4165SFrank Mandarino 
1007513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1008513cb311SSudip Mukherjee 	if (!rtd)
1009513cb311SSudip Mukherjee 		return -ENOMEM;
1010513cb311SSudip Mukherjee 
101114621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
101214621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
101314621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
101414621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1015b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1016f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1017f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
10181a497983SMengdong Lin 		goto _err_defer;
1019c5af3a2eSMark Brown 	}
1020c5af3a2eSMark Brown 
102188bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
102288bd870fSBenoit Cousson 
102388bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
10241a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
102588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
102614621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
102788bd870fSBenoit Cousson 		if (!codec_dais[i]) {
102812023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
102988bd870fSBenoit Cousson 				codecs[i].dai_name);
10301a497983SMengdong Lin 			goto _err_defer;
103112023a9aSMisael Lopez Cruz 		}
103288bd870fSBenoit Cousson 	}
103388bd870fSBenoit Cousson 
103488bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
103588bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
103688bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
103712023a9aSMisael Lopez Cruz 
1038848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1039848dd8beSMark Brown 	platform_name = dai_link->platform_name;
10405a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1041848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1042848dd8beSMark Brown 
1043b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
1044f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
10455a504963SStephen Warren 		if (dai_link->platform_of_node) {
10465a504963SStephen Warren 			if (platform->dev->of_node !=
10475a504963SStephen Warren 			    dai_link->platform_of_node)
10485a504963SStephen Warren 				continue;
10495a504963SStephen Warren 		} else {
1050f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
10512610ab77SStephen Warren 				continue;
10525a504963SStephen Warren 		}
10532610ab77SStephen Warren 
1054f0fba2adSLiam Girdwood 		rtd->platform = platform;
1055f0fba2adSLiam Girdwood 	}
1056b19e6e7bSMark Brown 	if (!rtd->platform) {
1057f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1058f0fba2adSLiam Girdwood 			dai_link->platform_name);
105970fcad49SCharles Keepax 		goto _err_defer;
1060f0fba2adSLiam Girdwood 	}
1061b19e6e7bSMark Brown 
10621a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1063b19e6e7bSMark Brown 	return 0;
10641a497983SMengdong Lin 
10651a497983SMengdong Lin _err_defer:
10661a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
10671a497983SMengdong Lin 	return  -EPROBE_DEFER;
10686b05eda6SMark Brown }
10696b05eda6SMark Brown 
1070f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1071d12cd198SStephen Warren {
1072abd31b32SLars-Peter Clausen 	if (!component->card)
107370090bbbSLars-Peter Clausen 		return;
1074d12cd198SStephen Warren 
1075d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1076d12cd198SStephen Warren 
1077f1d45cc3SLars-Peter Clausen 	if (component->remove)
1078f1d45cc3SLars-Peter Clausen 		component->remove(component);
1079d12cd198SStephen Warren 
1080f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1081d12cd198SStephen Warren 
1082f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1083abd31b32SLars-Peter Clausen 	component->card = NULL;
1084f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1085d12cd198SStephen Warren }
1086d12cd198SStephen Warren 
1087e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1088589c3563SJarkko Nikula {
1089589c3563SJarkko Nikula 	int err;
1090589c3563SJarkko Nikula 
1091e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1092e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1093e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1094e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1095589c3563SJarkko Nikula 			if (err < 0)
1096e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1097b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1098e60cd14fSLars-Peter Clausen 					dai->name, err);
1099b0aa88afSMisael Lopez Cruz 		}
1100e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1101b0aa88afSMisael Lopez Cruz 	}
1102b0aa88afSMisael Lopez Cruz }
1103b0aa88afSMisael Lopez Cruz 
11041a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11051a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1106f0fba2adSLiam Girdwood {
1107e60cd14fSLars-Peter Clausen 	int i;
1108f0fba2adSLiam Girdwood 
1109f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1110f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
111136ae1a96SMark Brown 		device_unregister(rtd->dev);
1112f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
111302a06d30SBarry Song 	}
111402a06d30SBarry Song 
1115f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
111688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1117e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1118f0fba2adSLiam Girdwood 
1119e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1120f0fba2adSLiam Girdwood }
1121f0fba2adSLiam Girdwood 
11221a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11231a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
112462ae68faSStephen Warren {
112562ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
112662ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
112761aca564SLars-Peter Clausen 	struct snd_soc_component *component;
112888bd870fSBenoit Cousson 	int i;
112962ae68faSStephen Warren 
113062ae68faSStephen Warren 	/* remove the platform */
113170090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1132f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
113362ae68faSStephen Warren 
113462ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
113588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
113661aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
113770090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
113861aca564SLars-Peter Clausen 			soc_remove_component(component);
113962ae68faSStephen Warren 	}
114062ae68faSStephen Warren 
114162ae68faSStephen Warren 	/* remove any CPU-side CODEC */
114262ae68faSStephen Warren 	if (cpu_dai) {
114370090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
114461aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
114562ae68faSStephen Warren 	}
114662ae68faSStephen Warren }
114762ae68faSStephen Warren 
11480671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11490671fd8eSKuninori Morimoto {
11501a497983SMengdong Lin 	int order;
11511a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1152f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
11530671fd8eSKuninori Morimoto 
11540168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
11550168bf0dSLiam Girdwood 			order++) {
11561a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
11571a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
11580168bf0dSLiam Girdwood 	}
115962ae68faSStephen Warren 
116062ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
116162ae68faSStephen Warren 			order++) {
11621a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
11631a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
116462ae68faSStephen Warren 	}
116562ae68faSStephen Warren 
1166f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1167f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1168f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1169f8f80361SMengdong Lin 				link->name);
1170f8f80361SMengdong Lin 
1171f8f80361SMengdong Lin 		list_del(&link->list);
1172f8f80361SMengdong Lin 		card->num_dai_links--;
11730671fd8eSKuninori Morimoto 	}
11740671fd8eSKuninori Morimoto }
11750671fd8eSKuninori Morimoto 
1176923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1177923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1178923c5e61SMengdong Lin {
1179923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1180923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1181923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1182923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1183923c5e61SMengdong Lin 
1184923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1185923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1186923c5e61SMengdong Lin 				GFP_KERNEL);
1187923c5e61SMengdong Lin 		if (!dai_link->codecs)
1188923c5e61SMengdong Lin 			return -ENOMEM;
1189923c5e61SMengdong Lin 
1190923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1191923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1192923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1193923c5e61SMengdong Lin 	}
1194923c5e61SMengdong Lin 
1195923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1196923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1197923c5e61SMengdong Lin 		return -EINVAL;
1198923c5e61SMengdong Lin 	}
1199923c5e61SMengdong Lin 
1200923c5e61SMengdong Lin 	return 0;
1201923c5e61SMengdong Lin }
1202923c5e61SMengdong Lin 
1203923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1204923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1205923c5e61SMengdong Lin {
1206923c5e61SMengdong Lin 	int i, ret;
1207923c5e61SMengdong Lin 
1208923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1209923c5e61SMengdong Lin 	if (ret) {
1210923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1211923c5e61SMengdong Lin 		return ret;
1212923c5e61SMengdong Lin 	}
1213923c5e61SMengdong Lin 
1214923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1215923c5e61SMengdong Lin 		/*
1216923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1217923c5e61SMengdong Lin 		 * not both or neither.
1218923c5e61SMengdong Lin 		 */
1219923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1220923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1221923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1222923c5e61SMengdong Lin 				link->name);
1223923c5e61SMengdong Lin 			return -EINVAL;
1224923c5e61SMengdong Lin 		}
1225923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1226923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1227923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1228923c5e61SMengdong Lin 				link->name);
1229923c5e61SMengdong Lin 			return -EINVAL;
1230923c5e61SMengdong Lin 		}
1231923c5e61SMengdong Lin 	}
1232923c5e61SMengdong Lin 
1233923c5e61SMengdong Lin 	/*
1234923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1235923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1236923c5e61SMengdong Lin 	 */
1237923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1238923c5e61SMengdong Lin 		dev_err(card->dev,
1239923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1240923c5e61SMengdong Lin 			link->name);
1241923c5e61SMengdong Lin 		return -EINVAL;
1242923c5e61SMengdong Lin 	}
1243923c5e61SMengdong Lin 
1244923c5e61SMengdong Lin 	/*
1245923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1246923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1247923c5e61SMengdong Lin 	 * name alone..
1248923c5e61SMengdong Lin 	 */
1249923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1250923c5e61SMengdong Lin 		dev_err(card->dev,
1251923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1252923c5e61SMengdong Lin 			link->name);
1253923c5e61SMengdong Lin 		return -EINVAL;
1254923c5e61SMengdong Lin 	}
1255923c5e61SMengdong Lin 	/*
1256923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1257923c5e61SMengdong Lin 	 * specified
1258923c5e61SMengdong Lin 	 */
1259923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1260923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1261923c5e61SMengdong Lin 		dev_err(card->dev,
1262923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1263923c5e61SMengdong Lin 			link->name);
1264923c5e61SMengdong Lin 		return -EINVAL;
1265923c5e61SMengdong Lin 	}
1266923c5e61SMengdong Lin 
1267923c5e61SMengdong Lin 	return 0;
1268923c5e61SMengdong Lin }
1269923c5e61SMengdong Lin 
1270f8f80361SMengdong Lin /**
1271f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1272f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1273f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1274f8f80361SMengdong Lin  *
1275f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1276f8f80361SMengdong Lin  *
1277f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1278f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1279f8f80361SMengdong Lin  * DAI links in dai_link array.
1280f8f80361SMengdong Lin  */
1281f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1282f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1283f8f80361SMengdong Lin {
1284f8f80361SMengdong Lin 	if (dai_link->dobj.type
1285f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1286f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1287f8f80361SMengdong Lin 			dai_link->dobj.type);
1288f8f80361SMengdong Lin 		return -EINVAL;
1289f8f80361SMengdong Lin 	}
1290f8f80361SMengdong Lin 
1291f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1292d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1293d6f220eaSMengdong Lin 	 * on the link created by topology.
1294d6f220eaSMengdong Lin 	 */
1295d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1296d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1297d6f220eaSMengdong Lin 
1298f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1299f8f80361SMengdong Lin 	card->num_dai_links++;
1300f8f80361SMengdong Lin 
1301f8f80361SMengdong Lin 	return 0;
1302f8f80361SMengdong Lin }
1303f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1304f8f80361SMengdong Lin 
1305f8f80361SMengdong Lin /**
1306f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1307f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1308f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1309f8f80361SMengdong Lin  *
1310f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1311f8f80361SMengdong Lin  *
1312f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1313f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1314f8f80361SMengdong Lin  */
1315f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1316f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1317f8f80361SMengdong Lin {
1318f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1319f8f80361SMengdong Lin 
1320f8f80361SMengdong Lin 	if (dai_link->dobj.type
1321f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1322f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1323f8f80361SMengdong Lin 			dai_link->dobj.type);
1324f8f80361SMengdong Lin 		return;
1325f8f80361SMengdong Lin 	}
1326f8f80361SMengdong Lin 
1327f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1328d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1329d6f220eaSMengdong Lin 	 * on the link created by topology.
1330d6f220eaSMengdong Lin 	 */
1331d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1332d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1333d6f220eaSMengdong Lin 
1334f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1335f8f80361SMengdong Lin 		if (link == dai_link) {
1336f8f80361SMengdong Lin 			list_del(&link->list);
1337f8f80361SMengdong Lin 			card->num_dai_links--;
1338f8f80361SMengdong Lin 			return;
1339f8f80361SMengdong Lin 		}
1340f8f80361SMengdong Lin 	}
1341f8f80361SMengdong Lin }
1342f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1343f0fba2adSLiam Girdwood 
1344ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
134594f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1346ead9b919SJarkko Nikula {
1347ead9b919SJarkko Nikula 	int i;
1348ead9b919SJarkko Nikula 
1349ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1350ead9b919SJarkko Nikula 		return;
1351ead9b919SJarkko Nikula 
1352ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1353ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
135494f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
13553ca041edSSebastian Reichel 			continue;
135694f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
13573ca041edSSebastian Reichel 			continue;
135894f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1359ead9b919SJarkko Nikula 		break;
1360ead9b919SJarkko Nikula 	}
1361ead9b919SJarkko Nikula }
1362ead9b919SJarkko Nikula 
1363f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1364f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1365589c3563SJarkko Nikula {
1366f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1367888df395SMark Brown 	struct snd_soc_dai *dai;
1368f1d45cc3SLars-Peter Clausen 	int ret;
1369589c3563SJarkko Nikula 
13701b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
137170090bbbSLars-Peter Clausen 		return 0;
1372589c3563SJarkko Nikula 
1373abd31b32SLars-Peter Clausen 	if (component->card) {
13741b7c1231SLars-Peter Clausen 		if (component->card != card) {
13751b7c1231SLars-Peter Clausen 			dev_err(component->dev,
13761b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
13771b7c1231SLars-Peter Clausen 				card->name, component->card->name);
13781b7c1231SLars-Peter Clausen 			return -ENODEV;
13791b7c1231SLars-Peter Clausen 		}
13801b7c1231SLars-Peter Clausen 		return 0;
13811b7c1231SLars-Peter Clausen 	}
13821b7c1231SLars-Peter Clausen 
1383abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1384abd31b32SLars-Peter Clausen 		return -ENODEV;
1385abd31b32SLars-Peter Clausen 
1386f1d45cc3SLars-Peter Clausen 	component->card = card;
1387f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1388f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1389589c3563SJarkko Nikula 
1390f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1391d5d1e0beSLars-Peter Clausen 
1392f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1393f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1394f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
139577530150SLars-Peter Clausen 
1396b318ad50SNariman Poushin 		if (ret != 0) {
1397f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1398b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1399b318ad50SNariman Poushin 			goto err_probe;
1400b318ad50SNariman Poushin 		}
1401b318ad50SNariman Poushin 	}
1402b318ad50SNariman Poushin 
14030634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
14040634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1405261edc70SNariman Poushin 		if (ret != 0) {
1406f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1407261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1408261edc70SNariman Poushin 			goto err_probe;
1409261edc70SNariman Poushin 		}
1410261edc70SNariman Poushin 	}
1411888df395SMark Brown 
1412f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1413f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1414589c3563SJarkko Nikula 		if (ret < 0) {
1415f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1416f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
141770d29331SJarkko Nikula 			goto err_probe;
1418589c3563SJarkko Nikula 		}
1419cb2cf612SLiam Girdwood 
1420f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1421f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1422956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1423f1d45cc3SLars-Peter Clausen 			component->name);
1424956245e9SLiam Girdwood 	}
1425956245e9SLiam Girdwood 
1426f2ed6b07SMengdong Lin 	/* machine specific init */
1427f2ed6b07SMengdong Lin 	if (component->init) {
1428f2ed6b07SMengdong Lin 		ret = component->init(component);
1429f2ed6b07SMengdong Lin 		if (ret < 0) {
1430f2ed6b07SMengdong Lin 			dev_err(component->dev,
1431f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1432f2ed6b07SMengdong Lin 			goto err_probe;
1433f2ed6b07SMengdong Lin 		}
1434f2ed6b07SMengdong Lin 	}
1435f2ed6b07SMengdong Lin 
1436f1d45cc3SLars-Peter Clausen 	if (component->controls)
1437f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1438f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1439f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1440f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1441f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1442956245e9SLiam Girdwood 
1443f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1444d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1445956245e9SLiam Girdwood 
1446956245e9SLiam Girdwood 	return 0;
1447956245e9SLiam Girdwood 
1448956245e9SLiam Girdwood err_probe:
1449f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1450abd31b32SLars-Peter Clausen 	component->card = NULL;
1451f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1452956245e9SLiam Girdwood 
1453956245e9SLiam Girdwood 	return ret;
1454956245e9SLiam Girdwood }
1455956245e9SLiam Girdwood 
145636ae1a96SMark Brown static void rtd_release(struct device *dev)
145736ae1a96SMark Brown {
145836ae1a96SMark Brown 	kfree(dev);
145936ae1a96SMark Brown }
1460f0fba2adSLiam Girdwood 
14615f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
14625f3484acSLars-Peter Clausen 	const char *name)
1463503ae5e0SMisael Lopez Cruz {
1464589c3563SJarkko Nikula 	int ret = 0;
1465589c3563SJarkko Nikula 
1466589c3563SJarkko Nikula 	/* register the rtd device */
146736ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
146836ae1a96SMark Brown 	if (!rtd->dev)
146936ae1a96SMark Brown 		return -ENOMEM;
147036ae1a96SMark Brown 	device_initialize(rtd->dev);
14715f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
147236ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1473d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1474f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
147536ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1476b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
147701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
147801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
147901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
148001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
148136ae1a96SMark Brown 	ret = device_add(rtd->dev);
1482589c3563SJarkko Nikula 	if (ret < 0) {
1483865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1484865df9cbSChuansheng Liu 		put_device(rtd->dev);
14855f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1486f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1487589c3563SJarkko Nikula 		return ret;
1488589c3563SJarkko Nikula 	}
1489589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1490589c3563SJarkko Nikula 	return 0;
1491589c3563SJarkko Nikula }
1492589c3563SJarkko Nikula 
14931a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
14941a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
149562ae68faSStephen Warren 				     int order)
149662ae68faSStephen Warren {
149762ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1498f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
149988bd870fSBenoit Cousson 	int i, ret;
150062ae68faSStephen Warren 
150162ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
150261aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
150370090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1504f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
150562ae68faSStephen Warren 		if (ret < 0)
150662ae68faSStephen Warren 			return ret;
150762ae68faSStephen Warren 	}
150862ae68faSStephen Warren 
150988bd870fSBenoit Cousson 	/* probe the CODEC-side components */
151088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
151161aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
151270090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1513f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
151462ae68faSStephen Warren 			if (ret < 0)
151562ae68faSStephen Warren 				return ret;
151662ae68faSStephen Warren 		}
151788bd870fSBenoit Cousson 	}
151862ae68faSStephen Warren 
151962ae68faSStephen Warren 	/* probe the platform */
152070090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1521f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
152262ae68faSStephen Warren 		if (ret < 0)
152362ae68faSStephen Warren 			return ret;
152462ae68faSStephen Warren 	}
152562ae68faSStephen Warren 
152662ae68faSStephen Warren 	return 0;
152762ae68faSStephen Warren }
152862ae68faSStephen Warren 
15298e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1530b0aa88afSMisael Lopez Cruz {
1531b0aa88afSMisael Lopez Cruz 	int ret;
1532b0aa88afSMisael Lopez Cruz 
15338e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
15348e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
15358e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1536b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
15378e2be562SLars-Peter Clausen 				dev_err(dai->dev,
15388e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
15398e2be562SLars-Peter Clausen 					dai->name, ret);
1540b0aa88afSMisael Lopez Cruz 				return ret;
1541b0aa88afSMisael Lopez Cruz 			}
1542b0aa88afSMisael Lopez Cruz 		}
1543b0aa88afSMisael Lopez Cruz 
15448e2be562SLars-Peter Clausen 		dai->probed = 1;
1545b0aa88afSMisael Lopez Cruz 	}
1546b0aa88afSMisael Lopez Cruz 
1547b0aa88afSMisael Lopez Cruz 	return 0;
1548b0aa88afSMisael Lopez Cruz }
1549b0aa88afSMisael Lopez Cruz 
15502436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
15512436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
15523f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
15532436a723SMisael Lopez Cruz {
15543f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
15553f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
15563de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
15572436a723SMisael Lopez Cruz 	int ret;
15582436a723SMisael Lopez Cruz 
155988bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
156088bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
156188bd870fSBenoit Cousson 
15622436a723SMisael Lopez Cruz 	/* link the DAI widgets */
15633de7c420SVinod Koul 	sink = codec_dai->playback_widget;
15643de7c420SVinod Koul 	source = cpu_dai->capture_widget;
15653de7c420SVinod Koul 	if (sink && source) {
15662436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
15673de7c420SVinod Koul 					   dai_link->num_params,
15683de7c420SVinod Koul 					   source, sink);
15692436a723SMisael Lopez Cruz 		if (ret != 0) {
15702436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
15713de7c420SVinod Koul 				sink->name, source->name, ret);
15722436a723SMisael Lopez Cruz 			return ret;
15732436a723SMisael Lopez Cruz 		}
15742436a723SMisael Lopez Cruz 	}
15752436a723SMisael Lopez Cruz 
15763de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
15773de7c420SVinod Koul 	source = codec_dai->capture_widget;
15783de7c420SVinod Koul 	if (sink && source) {
15792436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
15803de7c420SVinod Koul 					   dai_link->num_params,
15813de7c420SVinod Koul 					   source, sink);
15822436a723SMisael Lopez Cruz 		if (ret != 0) {
15832436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
15843de7c420SVinod Koul 				sink->name, source->name, ret);
15852436a723SMisael Lopez Cruz 			return ret;
15862436a723SMisael Lopez Cruz 		}
15872436a723SMisael Lopez Cruz 	}
15882436a723SMisael Lopez Cruz 
15892436a723SMisael Lopez Cruz 	return 0;
15902436a723SMisael Lopez Cruz }
15912436a723SMisael Lopez Cruz 
15921a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
15931a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1594f0fba2adSLiam Girdwood {
15951a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1596c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
159788bd870fSBenoit Cousson 	int i, ret;
1598f0fba2adSLiam Girdwood 
1599f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
16001a497983SMengdong Lin 			card->name, rtd->num, order);
1601f0fba2adSLiam Girdwood 
1602f0fba2adSLiam Girdwood 	/* set default power off timeout */
1603f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1604f0fba2adSLiam Girdwood 
16058e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
16068e2be562SLars-Peter Clausen 	if (ret)
1607f0fba2adSLiam Girdwood 		return ret;
1608f0fba2adSLiam Girdwood 
1609f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
161088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
16118e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1612b0aa88afSMisael Lopez Cruz 		if (ret)
1613f0fba2adSLiam Girdwood 			return ret;
161488bd870fSBenoit Cousson 	}
1615f0fba2adSLiam Girdwood 
16160168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
16170168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
16180168bf0dSLiam Girdwood 		return 0;
16190168bf0dSLiam Girdwood 
16205f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16215f3484acSLars-Peter Clausen 	if (dai_link->init) {
16225f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
16235f3484acSLars-Peter Clausen 		if (ret < 0) {
16245f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16255f3484acSLars-Peter Clausen 				dai_link->name, ret);
16265f3484acSLars-Peter Clausen 			return ret;
16275f3484acSLars-Peter Clausen 		}
16285f3484acSLars-Peter Clausen 	}
16295f3484acSLars-Peter Clausen 
1630a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1631a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1632a5053a8eSKuninori Morimoto 
16335f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1634589c3563SJarkko Nikula 	if (ret)
1635f0fba2adSLiam Girdwood 		return ret;
1636f0fba2adSLiam Girdwood 
16375f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
16385f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
16392e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
16402e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
16415f3484acSLars-Peter Clausen #endif
16425f3484acSLars-Peter Clausen 
16436f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
16441245b700SNamarta Kohli 		/*create compress_device"*/
16451a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
16461245b700SNamarta Kohli 		if (ret < 0) {
1647f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
16481245b700SNamarta Kohli 					 dai_link->stream_name);
16491245b700SNamarta Kohli 			return ret;
16501245b700SNamarta Kohli 		}
16511245b700SNamarta Kohli 	} else {
16521245b700SNamarta Kohli 
1653c74184edSMark Brown 		if (!dai_link->params) {
1654f0fba2adSLiam Girdwood 			/* create the pcm */
16551a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1656f0fba2adSLiam Girdwood 			if (ret < 0) {
1657f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
16580837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1659f0fba2adSLiam Girdwood 				return ret;
1660f0fba2adSLiam Girdwood 			}
1661c74184edSMark Brown 		} else {
16629d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
16639d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
16649d58a077SRichard Fitzgerald 
1665c74184edSMark Brown 			/* link the DAI widgets */
16663f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
16672436a723SMisael Lopez Cruz 			if (ret)
1668c74184edSMark Brown 				return ret;
1669c74184edSMark Brown 		}
1670c74184edSMark Brown 	}
1671c74184edSMark Brown 
1672f0fba2adSLiam Girdwood 	return 0;
1673f0fba2adSLiam Girdwood }
1674f0fba2adSLiam Girdwood 
167544c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1676b19e6e7bSMark Brown {
16773ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1678f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1679f2ed6b07SMengdong Lin 	const char *name;
1680f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
16813ca041edSSebastian Reichel 
1682f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1683f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1684f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1685f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1686f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1687f2ed6b07SMengdong Lin 		if (!component) {
1688f2ed6b07SMengdong Lin 			if (codec_of_node)
1689f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1690f2ed6b07SMengdong Lin 			goto err_defer;
1691f2ed6b07SMengdong Lin 		}
1692f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1693f2ed6b07SMengdong Lin 		/* generic components */
1694f2ed6b07SMengdong Lin 		name = aux_dev->name;
1695f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1696f2ed6b07SMengdong Lin 		if (!component)
1697f2ed6b07SMengdong Lin 			goto err_defer;
1698f2ed6b07SMengdong Lin 	} else {
1699f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1700f2ed6b07SMengdong Lin 		return -EINVAL;
1701f2ed6b07SMengdong Lin 	}
17023ca041edSSebastian Reichel 
1703f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
17041a653aa4SKuninori Morimoto 	component->auxiliary = 1;
17051a653aa4SKuninori Morimoto 
1706f2ed6b07SMengdong Lin 	return 0;
1707f2ed6b07SMengdong Lin 
1708f2ed6b07SMengdong Lin err_defer:
170965d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1710b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1711b19e6e7bSMark Brown }
1712b19e6e7bSMark Brown 
1713f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1714f2ed6b07SMengdong Lin {
1715f2ed6b07SMengdong Lin 	struct snd_soc_component *comp;
1716f2ed6b07SMengdong Lin 	int order;
1717f2ed6b07SMengdong Lin 	int ret;
1718f2ed6b07SMengdong Lin 
1719f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1720f2ed6b07SMengdong Lin 		order++) {
17211a653aa4SKuninori Morimoto 		list_for_each_entry(comp, &card->component_dev_list, card_list) {
17221a653aa4SKuninori Morimoto 			if (!comp->auxiliary)
17231a653aa4SKuninori Morimoto 				continue;
17241a653aa4SKuninori Morimoto 
1725f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1726f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1727f2ed6b07SMengdong Lin 				if (ret < 0) {
1728f2ed6b07SMengdong Lin 					dev_err(card->dev,
1729f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1730f2ed6b07SMengdong Lin 						comp->name, ret);
1731f2ed6b07SMengdong Lin 					return ret;
1732f2ed6b07SMengdong Lin 				}
1733f2ed6b07SMengdong Lin 			}
1734f2ed6b07SMengdong Lin 		}
1735f2ed6b07SMengdong Lin 	}
173665d9361fSLars-Peter Clausen 
173744c69bb1SLars-Peter Clausen 	return 0;
17383ca041edSSebastian Reichel }
17392eea392dSJarkko Nikula 
1740f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
174144c69bb1SLars-Peter Clausen {
1742f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1743f2ed6b07SMengdong Lin 	int order;
174444c69bb1SLars-Peter Clausen 
1745f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1746f2ed6b07SMengdong Lin 		order++) {
1747f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
17481a653aa4SKuninori Morimoto 			&card->component_dev_list, card_list) {
17491a653aa4SKuninori Morimoto 
17501a653aa4SKuninori Morimoto 			if (!comp->auxiliary)
17511a653aa4SKuninori Morimoto 				continue;
17521a653aa4SKuninori Morimoto 
1753f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1754f2ed6b07SMengdong Lin 				soc_remove_component(comp);
17551a653aa4SKuninori Morimoto 				comp->auxiliary = 0;
17562eea392dSJarkko Nikula 			}
17575f3484acSLars-Peter Clausen 		}
17582eea392dSJarkko Nikula 	}
17592eea392dSJarkko Nikula }
17602eea392dSJarkko Nikula 
1761f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1762fdf0f54dSDimitris Papastamos {
1763fdf0f54dSDimitris Papastamos 	int ret;
1764fdf0f54dSDimitris Papastamos 
1765fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1766fdf0f54dSDimitris Papastamos 		return 0;
1767fdf0f54dSDimitris Papastamos 
1768fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1769fdf0f54dSDimitris Papastamos 	if (ret < 0) {
177010e8aa9aSMichał Mirosław 		dev_err(codec->dev,
177110e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
177210e8aa9aSMichał Mirosław 			ret);
1773fdf0f54dSDimitris Papastamos 		return ret;
1774fdf0f54dSDimitris Papastamos 	}
1775fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1776fdf0f54dSDimitris Papastamos 	return 0;
1777fdf0f54dSDimitris Papastamos }
1778fdf0f54dSDimitris Papastamos 
1779ce64c8b9SLars-Peter Clausen /**
1780ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1781ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1782ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1783ce64c8b9SLars-Peter Clausen  *
1784ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1785ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1786ce64c8b9SLars-Peter Clausen  *
1787ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1788ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1789ce64c8b9SLars-Peter Clausen  *
1790ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1791ce64c8b9SLars-Peter Clausen  */
1792ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1793ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1794ce64c8b9SLars-Peter Clausen {
1795ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1796ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1797ce64c8b9SLars-Peter Clausen 	unsigned int i;
1798ce64c8b9SLars-Peter Clausen 	int ret;
1799ce64c8b9SLars-Peter Clausen 
1800ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1801ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1802ce64c8b9SLars-Peter Clausen 
1803ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1804ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1805ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1806ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1807ce64c8b9SLars-Peter Clausen 			return ret;
1808ce64c8b9SLars-Peter Clausen 		}
1809ce64c8b9SLars-Peter Clausen 	}
1810ce64c8b9SLars-Peter Clausen 
1811ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1812ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1813ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1814ce64c8b9SLars-Peter Clausen 
1815ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1816ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1817ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1818ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1819ce64c8b9SLars-Peter Clausen 			break;
1820ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1821ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1822ce64c8b9SLars-Peter Clausen 			break;
1823ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1824ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1825ce64c8b9SLars-Peter Clausen 			break;
1826ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1827ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1828ce64c8b9SLars-Peter Clausen 			break;
1829ce64c8b9SLars-Peter Clausen 		}
1830ce64c8b9SLars-Peter Clausen 
1831ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1832ce64c8b9SLars-Peter Clausen 	}
1833ce64c8b9SLars-Peter Clausen 
1834ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1835ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1836ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1837ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1838ce64c8b9SLars-Peter Clausen 		return ret;
1839ce64c8b9SLars-Peter Clausen 	}
1840ce64c8b9SLars-Peter Clausen 
1841ce64c8b9SLars-Peter Clausen 	return 0;
1842ce64c8b9SLars-Peter Clausen }
1843ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1844ce64c8b9SLars-Peter Clausen 
1845b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1846f0fba2adSLiam Girdwood {
1847fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
18481a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
184961b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1850ce64c8b9SLars-Peter Clausen 	int ret, i, order;
185196dd3622SMark Brown 
185234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
185301b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1854f0fba2adSLiam Girdwood 
1855b19e6e7bSMark Brown 	/* bind DAIs */
1856b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
18576f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
1858b19e6e7bSMark Brown 		if (ret != 0)
1859b19e6e7bSMark Brown 			goto base_error;
1860db2a4165SFrank Mandarino 	}
1861db2a4165SFrank Mandarino 
186244c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1863b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
186444c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1865b19e6e7bSMark Brown 		if (ret != 0)
1866b19e6e7bSMark Brown 			goto base_error;
1867db2a4165SFrank Mandarino 	}
1868db2a4165SFrank Mandarino 
1869f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1870f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
1871f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
1872f8f80361SMengdong Lin 
1873fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1874fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1875fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1876fdf0f54dSDimitris Papastamos 			continue;
1877f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1878b19e6e7bSMark Brown 		if (ret < 0)
1879b19e6e7bSMark Brown 			goto base_error;
1880fdf0f54dSDimitris Papastamos 	}
1881fdf0f54dSDimitris Papastamos 
1882f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1883102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1884f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1885f0fba2adSLiam Girdwood 	if (ret < 0) {
188610e8aa9aSMichał Mirosław 		dev_err(card->dev,
188710e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
188810e8aa9aSMichał Mirosław 			card->name, ret);
1889b19e6e7bSMark Brown 		goto base_error;
1890db2a4165SFrank Mandarino 	}
1891db2a4165SFrank Mandarino 
18920757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
18930757d834SLars-Peter Clausen 
1894e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1895e37a4970SMark Brown 	card->dapm.dev = card->dev;
1896e37a4970SMark Brown 	card->dapm.card = card;
1897e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1898e37a4970SMark Brown 
1899d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1900d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1901d5d1e0beSLars-Peter Clausen #endif
1902d5d1e0beSLars-Peter Clausen 
190388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
19046ed25978SAndy Green 	/* deferred resume work */
19056308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
19061301a964SRandy Dunlap #endif
19076ed25978SAndy Green 
19089a841ebbSMark Brown 	if (card->dapm_widgets)
19099a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19109a841ebbSMark Brown 					  card->num_dapm_widgets);
19119a841ebbSMark Brown 
1912f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
1913f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1914f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
1915f23e860eSNicolin Chen 
1916f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1917f0fba2adSLiam Girdwood 	if (card->probe) {
1918e7361ec4SMark Brown 		ret = card->probe(card);
1919f0fba2adSLiam Girdwood 		if (ret < 0)
1920f0fba2adSLiam Girdwood 			goto card_probe_error;
1921f0fba2adSLiam Girdwood 	}
1922f0fba2adSLiam Girdwood 
192362ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
19240168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
19250168bf0dSLiam Girdwood 			order++) {
19261a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
19271a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
192862ae68faSStephen Warren 			if (ret < 0) {
1929f110bfc7SLiam Girdwood 				dev_err(card->dev,
1930f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1931f110bfc7SLiam Girdwood 					ret);
193262ae68faSStephen Warren 				goto probe_dai_err;
193362ae68faSStephen Warren 			}
193462ae68faSStephen Warren 		}
193562ae68faSStephen Warren 	}
193662ae68faSStephen Warren 
1937f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1938f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
1939f2ed6b07SMengdong Lin 	if (ret < 0)
1940f2ed6b07SMengdong Lin 		goto probe_dai_err;
1941f2ed6b07SMengdong Lin 
194261b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
194361b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
194461b0088bSMengdong Lin 	 */
194561b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
194661b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
194761b0088bSMengdong Lin 			continue;
194861b0088bSMengdong Lin 
194961b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
195061b0088bSMengdong Lin 		if (ret)
195161b0088bSMengdong Lin 			goto probe_dai_err;
195261b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
195361b0088bSMengdong Lin 		if (ret)
195461b0088bSMengdong Lin 			goto probe_dai_err;
195561b0088bSMengdong Lin 	}
195661b0088bSMengdong Lin 
195762ae68faSStephen Warren 	/* probe all DAI links on this card */
195862ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
195962ae68faSStephen Warren 			order++) {
19601a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
19611a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
1962fe3e78e0SMark Brown 			if (ret < 0) {
1963f110bfc7SLiam Girdwood 				dev_err(card->dev,
1964f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1965f110bfc7SLiam Girdwood 					ret);
1966f0fba2adSLiam Girdwood 				goto probe_dai_err;
1967fe3e78e0SMark Brown 			}
1968fe3e78e0SMark Brown 		}
19690168bf0dSLiam Girdwood 	}
1970fe3e78e0SMark Brown 
1971888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1972b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1973888df395SMark Brown 
1974b7af1dafSMark Brown 	if (card->controls)
1975022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1976b7af1dafSMark Brown 
1977b8ad29deSMark Brown 	if (card->dapm_routes)
1978b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1979b8ad29deSMark Brown 					card->num_dapm_routes);
1980b8ad29deSMark Brown 
1981f23e860eSNicolin Chen 	if (card->of_dapm_routes)
1982f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1983f23e860eSNicolin Chen 					card->num_of_dapm_routes);
198475d9ac46SMark Brown 
1985f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1986fe3e78e0SMark Brown 		 "%s", card->name);
1987f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
198822de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1989f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1990f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1991f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1992f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1993f0e8ed85SMark Brown 		case '_':
1994f0e8ed85SMark Brown 		case '-':
1995f0e8ed85SMark Brown 		case '\0':
1996f0e8ed85SMark Brown 			break;
1997f0e8ed85SMark Brown 		default:
1998f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1999f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2000f0e8ed85SMark Brown 			break;
2001f0e8ed85SMark Brown 		}
2002f0e8ed85SMark Brown 	}
2003fe3e78e0SMark Brown 
200428e9ad92SMark Brown 	if (card->late_probe) {
200528e9ad92SMark Brown 		ret = card->late_probe(card);
200628e9ad92SMark Brown 		if (ret < 0) {
2007f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
200828e9ad92SMark Brown 				card->name, ret);
200928e9ad92SMark Brown 			goto probe_aux_dev_err;
201028e9ad92SMark Brown 		}
201128e9ad92SMark Brown 	}
201228e9ad92SMark Brown 
2013824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20148c193b8dSLars-Peter Clausen 
2015f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2016fe3e78e0SMark Brown 	if (ret < 0) {
2017f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2018f110bfc7SLiam Girdwood 				ret);
20196b3ed785SAxel Lin 		goto probe_aux_dev_err;
2020fe3e78e0SMark Brown 	}
2021fe3e78e0SMark Brown 
2022435c5e25SMark Brown 	card->instantiated = 1;
20234f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2024f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
202534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2026b19e6e7bSMark Brown 
2027b19e6e7bSMark Brown 	return 0;
2028db2a4165SFrank Mandarino 
20292eea392dSJarkko Nikula probe_aux_dev_err:
2030f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
20312eea392dSJarkko Nikula 
2032f0fba2adSLiam Girdwood probe_dai_err:
20330671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2034fe3e78e0SMark Brown 
2035f0fba2adSLiam Girdwood card_probe_error:
203687506549SMark Brown 	if (card->remove)
2037e7361ec4SMark Brown 		card->remove(card);
2038f0fba2adSLiam Girdwood 
20392210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
20400757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2041f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2042f0fba2adSLiam Girdwood 
2043b19e6e7bSMark Brown base_error:
20441a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2045f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
204634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2047db2a4165SFrank Mandarino 
2048b19e6e7bSMark Brown 	return ret;
2049435c5e25SMark Brown }
2050435c5e25SMark Brown 
2051435c5e25SMark Brown /* probes a new socdev */
2052435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2053435c5e25SMark Brown {
2054f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2055435c5e25SMark Brown 
205670a7ca34SVinod Koul 	/*
205770a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
205870a7ca34SVinod Koul 	 * we should not be here in that case so ret error
205970a7ca34SVinod Koul 	 */
206070a7ca34SVinod Koul 	if (!card)
206170a7ca34SVinod Koul 		return -EINVAL;
206270a7ca34SVinod Koul 
2063fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2064f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2065fe4085e8SMark Brown 		 card->name);
2066fe4085e8SMark Brown 
2067435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2068435c5e25SMark Brown 	card->dev = &pdev->dev;
2069f0fba2adSLiam Girdwood 
207028d528c8SMark Brown 	return snd_soc_register_card(card);
2071435c5e25SMark Brown }
2072435c5e25SMark Brown 
2073b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2074db2a4165SFrank Mandarino {
20751a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2076db2a4165SFrank Mandarino 
2077f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
20781a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
207943829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2080db2a4165SFrank Mandarino 
2081f0fba2adSLiam Girdwood 	/* remove and free each DAI */
20820671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
20831a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2084f0fba2adSLiam Girdwood 
2085f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2086f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2087f2ed6b07SMengdong Lin 
2088d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2089a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2090a6052154SJarkko Nikula 
2091f0fba2adSLiam Girdwood 	/* remove the card */
209287506549SMark Brown 	if (card->remove)
2093e7361ec4SMark Brown 		card->remove(card);
2094f0fba2adSLiam Girdwood 
2095f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2096b0e26485SVinod Koul 	return 0;
2097b0e26485SVinod Koul 
2098b2dfa62cSGuennadi Liakhovetski }
2099b0e26485SVinod Koul 
2100b0e26485SVinod Koul /* removes a socdev */
2101b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2102b0e26485SVinod Koul {
2103b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2104b0e26485SVinod Koul 
2105c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2106db2a4165SFrank Mandarino 	return 0;
2107db2a4165SFrank Mandarino }
2108db2a4165SFrank Mandarino 
21096f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
211051737470SMark Brown {
21116f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21121a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
211351737470SMark Brown 
211451737470SMark Brown 	if (!card->instantiated)
2115416356fcSMark Brown 		return 0;
211651737470SMark Brown 
211751737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
211851737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
21191a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
212043829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
212151737470SMark Brown 
2122f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2123416356fcSMark Brown 
2124988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
21251a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
212688bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21271a497983SMengdong Lin 		int i;
212888bd870fSBenoit Cousson 
2129988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21301a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
21311a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
213288bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
213388bd870fSBenoit Cousson 		}
2134988e8cc4SNicolin Chen 	}
2135988e8cc4SNicolin Chen 
2136416356fcSMark Brown 	return 0;
213751737470SMark Brown }
21386f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
213951737470SMark Brown 
21406f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2141b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2142b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2143b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2144b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21456f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2146b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2147416356fcSMark Brown };
2148deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2149416356fcSMark Brown 
2150db2a4165SFrank Mandarino /* ASoC platform driver */
2151db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2152db2a4165SFrank Mandarino 	.driver		= {
2153db2a4165SFrank Mandarino 		.name		= "soc-audio",
21546f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2155db2a4165SFrank Mandarino 	},
2156db2a4165SFrank Mandarino 	.probe		= soc_probe,
2157db2a4165SFrank Mandarino 	.remove		= soc_remove,
2158db2a4165SFrank Mandarino };
2159db2a4165SFrank Mandarino 
2160096e49d5SMark Brown /**
2161db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2162db2a4165SFrank Mandarino  * @_template: control template
2163db2a4165SFrank Mandarino  * @data: control private data
2164ac11a2b3SMark Brown  * @long_name: control long name
2165efb7ac3fSMark Brown  * @prefix: control name prefix
2166db2a4165SFrank Mandarino  *
2167db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2168db2a4165SFrank Mandarino  *
2169db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2170db2a4165SFrank Mandarino  */
2171db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21723056557fSMark Brown 				  void *data, const char *long_name,
2173efb7ac3fSMark Brown 				  const char *prefix)
2174db2a4165SFrank Mandarino {
2175db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2176efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2177efb7ac3fSMark Brown 	char *name = NULL;
2178db2a4165SFrank Mandarino 
2179db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2180db2a4165SFrank Mandarino 	template.index = 0;
2181db2a4165SFrank Mandarino 
2182efb7ac3fSMark Brown 	if (!long_name)
2183efb7ac3fSMark Brown 		long_name = template.name;
2184efb7ac3fSMark Brown 
2185efb7ac3fSMark Brown 	if (prefix) {
21862b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2187efb7ac3fSMark Brown 		if (!name)
2188efb7ac3fSMark Brown 			return NULL;
2189efb7ac3fSMark Brown 
2190efb7ac3fSMark Brown 		template.name = name;
2191efb7ac3fSMark Brown 	} else {
2192efb7ac3fSMark Brown 		template.name = long_name;
2193efb7ac3fSMark Brown 	}
2194efb7ac3fSMark Brown 
2195efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2196efb7ac3fSMark Brown 
2197efb7ac3fSMark Brown 	kfree(name);
2198efb7ac3fSMark Brown 
2199efb7ac3fSMark Brown 	return kcontrol;
2200db2a4165SFrank Mandarino }
2201db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2202db2a4165SFrank Mandarino 
2203022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2204022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2205022658beSLiam Girdwood 	const char *prefix, void *data)
2206022658beSLiam Girdwood {
2207022658beSLiam Girdwood 	int err, i;
2208022658beSLiam Girdwood 
2209022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2210022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2211022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2212022658beSLiam Girdwood 						     control->name, prefix));
2213022658beSLiam Girdwood 		if (err < 0) {
2214f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2215f110bfc7SLiam Girdwood 				control->name, err);
2216022658beSLiam Girdwood 			return err;
2217022658beSLiam Girdwood 		}
2218022658beSLiam Girdwood 	}
2219022658beSLiam Girdwood 
2220022658beSLiam Girdwood 	return 0;
2221022658beSLiam Girdwood }
2222022658beSLiam Girdwood 
22234fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22244fefd698SDimitris Papastamos 					       const char *name)
22254fefd698SDimitris Papastamos {
22264fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22274fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22284fefd698SDimitris Papastamos 
22294fefd698SDimitris Papastamos 	if (unlikely(!name))
22304fefd698SDimitris Papastamos 		return NULL;
22314fefd698SDimitris Papastamos 
22324fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22334fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22344fefd698SDimitris Papastamos 			return kctl;
22354fefd698SDimitris Papastamos 	return NULL;
22364fefd698SDimitris Papastamos }
22374fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22384fefd698SDimitris Papastamos 
2239db2a4165SFrank Mandarino /**
22400f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22410f2780adSLars-Peter Clausen  *
22420f2780adSLars-Peter Clausen  * @component: Component to add controls to
22430f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22440f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22450f2780adSLars-Peter Clausen  *
22460f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22470f2780adSLars-Peter Clausen  */
22480f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22490f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22500f2780adSLars-Peter Clausen {
22510f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22520f2780adSLars-Peter Clausen 
22530f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22540f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22550f2780adSLars-Peter Clausen }
22560f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22570f2780adSLars-Peter Clausen 
22580f2780adSLars-Peter Clausen /**
2259022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2260022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
22613e8e1952SIan Molton  * duplicating this code.
22623e8e1952SIan Molton  *
22633e8e1952SIan Molton  * @codec: codec to add controls to
22643e8e1952SIan Molton  * @controls: array of controls to add
22653e8e1952SIan Molton  * @num_controls: number of elements in the array
22663e8e1952SIan Molton  *
22673e8e1952SIan Molton  * Return 0 for success, else error.
22683e8e1952SIan Molton  */
2269022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
22700f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22713e8e1952SIan Molton {
22720f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
22730f2780adSLars-Peter Clausen 		num_controls);
22743e8e1952SIan Molton }
2275022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
22763e8e1952SIan Molton 
22773e8e1952SIan Molton /**
2278a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2279022658beSLiam Girdwood  * Convenience function to add a list of controls.
2280a491a5c8SLiam Girdwood  *
2281a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2282a491a5c8SLiam Girdwood  * @controls: array of controls to add
2283a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2284a491a5c8SLiam Girdwood  *
2285a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2286a491a5c8SLiam Girdwood  */
2287a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
22880f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2289a491a5c8SLiam Girdwood {
22900f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
22910f2780adSLars-Peter Clausen 		num_controls);
2292a491a5c8SLiam Girdwood }
2293a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2294a491a5c8SLiam Girdwood 
2295a491a5c8SLiam Girdwood /**
2296022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2297022658beSLiam Girdwood  * Convenience function to add a list of controls.
2298022658beSLiam Girdwood  *
2299022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2300022658beSLiam Girdwood  * @controls: array of controls to add
2301022658beSLiam Girdwood  * @num_controls: number of elements in the array
2302022658beSLiam Girdwood  *
2303022658beSLiam Girdwood  * Return 0 for success, else error.
2304022658beSLiam Girdwood  */
2305022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2306022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2307022658beSLiam Girdwood {
2308022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2309022658beSLiam Girdwood 
2310022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2311022658beSLiam Girdwood 			NULL, soc_card);
2312022658beSLiam Girdwood }
2313022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2314022658beSLiam Girdwood 
2315022658beSLiam Girdwood /**
2316022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2317022658beSLiam Girdwood  * Convienience function to add a list of controls.
2318022658beSLiam Girdwood  *
2319022658beSLiam Girdwood  * @dai: DAI to add controls to
2320022658beSLiam Girdwood  * @controls: array of controls to add
2321022658beSLiam Girdwood  * @num_controls: number of elements in the array
2322022658beSLiam Girdwood  *
2323022658beSLiam Girdwood  * Return 0 for success, else error.
2324022658beSLiam Girdwood  */
2325022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2326022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2327022658beSLiam Girdwood {
2328313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2329022658beSLiam Girdwood 
2330022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2331022658beSLiam Girdwood 			NULL, dai);
2332022658beSLiam Girdwood }
2333022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2334022658beSLiam Girdwood 
2335022658beSLiam Girdwood /**
23368c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23378c6529dbSLiam Girdwood  * @dai: DAI
23388c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23398c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23408c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23418c6529dbSLiam Girdwood  *
23428c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
23438c6529dbSLiam Girdwood  */
23448c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
23458c6529dbSLiam Girdwood 	unsigned int freq, int dir)
23468c6529dbSLiam Girdwood {
2347f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2348f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2349ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2350da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2351ec4ee52aSMark Brown 						      freq, dir);
23528c6529dbSLiam Girdwood 	else
23531104a9c8SMark Brown 		return -ENOTSUPP;
23548c6529dbSLiam Girdwood }
23558c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
23568c6529dbSLiam Girdwood 
23578c6529dbSLiam Girdwood /**
2358ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2359ec4ee52aSMark Brown  * @codec: CODEC
2360ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2361da1c6ea6SMark Brown  * @source: Source for the clock
2362ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2363ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2364ec4ee52aSMark Brown  *
2365ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2366ec4ee52aSMark Brown  */
2367ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2368da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2369ec4ee52aSMark Brown {
2370ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2371da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2372da1c6ea6SMark Brown 						 freq, dir);
2373ec4ee52aSMark Brown 	else
23741104a9c8SMark Brown 		return -ENOTSUPP;
2375ec4ee52aSMark Brown }
2376ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2377ec4ee52aSMark Brown 
2378ec4ee52aSMark Brown /**
23798c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
23808c6529dbSLiam Girdwood  * @dai: DAI
2381ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
23828c6529dbSLiam Girdwood  * @div: new clock divisor.
23838c6529dbSLiam Girdwood  *
23848c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
23858c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
23868c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
23878c6529dbSLiam Girdwood  */
23888c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
23898c6529dbSLiam Girdwood 	int div_id, int div)
23908c6529dbSLiam Girdwood {
2391f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2392f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
23938c6529dbSLiam Girdwood 	else
23948c6529dbSLiam Girdwood 		return -EINVAL;
23958c6529dbSLiam Girdwood }
23968c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
23978c6529dbSLiam Girdwood 
23988c6529dbSLiam Girdwood /**
23998c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
24008c6529dbSLiam Girdwood  * @dai: DAI
24018c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
240285488037SMark Brown  * @source: DAI specific source for the PLL
24038c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24048c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24058c6529dbSLiam Girdwood  *
24068c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
24078c6529dbSLiam Girdwood  */
240885488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
240985488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
24108c6529dbSLiam Girdwood {
2411f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2412f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
241385488037SMark Brown 					 freq_in, freq_out);
2414ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2415ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2416ec4ee52aSMark Brown 						   freq_in, freq_out);
24178c6529dbSLiam Girdwood 	else
24188c6529dbSLiam Girdwood 		return -EINVAL;
24198c6529dbSLiam Girdwood }
24208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24218c6529dbSLiam Girdwood 
2422ec4ee52aSMark Brown /*
2423ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2424ec4ee52aSMark Brown  * @codec: CODEC
2425ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2426ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2427ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2428ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2429ec4ee52aSMark Brown  *
2430ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2431ec4ee52aSMark Brown  */
2432ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2433ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2434ec4ee52aSMark Brown {
2435ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2436ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2437ec4ee52aSMark Brown 					      freq_in, freq_out);
2438ec4ee52aSMark Brown 	else
2439ec4ee52aSMark Brown 		return -EINVAL;
2440ec4ee52aSMark Brown }
2441ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2442ec4ee52aSMark Brown 
24438c6529dbSLiam Girdwood /**
2444e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2445e54cf76bSLiam Girdwood  * @dai: DAI
2446231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2447e54cf76bSLiam Girdwood  *
2448e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2449e54cf76bSLiam Girdwood  */
2450e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2451e54cf76bSLiam Girdwood {
2452e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2453e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2454e54cf76bSLiam Girdwood 	else
2455e54cf76bSLiam Girdwood 		return -EINVAL;
2456e54cf76bSLiam Girdwood }
2457e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2458e54cf76bSLiam Girdwood 
2459e54cf76bSLiam Girdwood /**
24608c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
24618c6529dbSLiam Girdwood  * @dai: DAI
24628c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
24638c6529dbSLiam Girdwood  *
24648c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
24658c6529dbSLiam Girdwood  */
24668c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
24678c6529dbSLiam Girdwood {
24685e4ba569SShawn Guo 	if (dai->driver == NULL)
24698c6529dbSLiam Girdwood 		return -EINVAL;
24705e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
24715e4ba569SShawn Guo 		return -ENOTSUPP;
24725e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
24738c6529dbSLiam Girdwood }
24748c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
24758c6529dbSLiam Girdwood 
24768c6529dbSLiam Girdwood /**
2477e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
247889c67857SXiubo Li  * @slots: Number of slots in use.
247989c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
248089c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
248189c67857SXiubo Li  *
248289c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
248389c67857SXiubo Li  */
2484e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
248589c67857SXiubo Li 					  unsigned int *tx_mask,
248689c67857SXiubo Li 					  unsigned int *rx_mask)
248789c67857SXiubo Li {
248889c67857SXiubo Li 	if (*tx_mask || *rx_mask)
248989c67857SXiubo Li 		return 0;
249089c67857SXiubo Li 
249189c67857SXiubo Li 	if (!slots)
249289c67857SXiubo Li 		return -EINVAL;
249389c67857SXiubo Li 
249489c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
249589c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
249689c67857SXiubo Li 
249789c67857SXiubo Li 	return 0;
249889c67857SXiubo Li }
249989c67857SXiubo Li 
250089c67857SXiubo Li /**
2501e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2502e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2503a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2504a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25058c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2506a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25078c6529dbSLiam Girdwood  *
2508e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2509e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2510e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2511e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2512e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2513e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2514e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2515e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2516e46c9366SLars-Peter Clausen  * channel and so on.
2517e46c9366SLars-Peter Clausen  *
2518e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2519e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2520e46c9366SLars-Peter Clausen  *
2521e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
25228c6529dbSLiam Girdwood  */
25238c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2524a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25258c6529dbSLiam Girdwood {
2526e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2527e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
252889c67857SXiubo Li 						&tx_mask, &rx_mask);
252989c67857SXiubo Li 	else
2530e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
253189c67857SXiubo Li 
253288bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
253388bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
253488bd870fSBenoit Cousson 
2535f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2536f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2537a5479e38SDaniel Ribeiro 				slots, slot_width);
25388c6529dbSLiam Girdwood 	else
2539b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25408c6529dbSLiam Girdwood }
25418c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25428c6529dbSLiam Girdwood 
25438c6529dbSLiam Girdwood /**
2544472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2545472df3cbSBarry Song  * @dai: DAI
2546472df3cbSBarry Song  * @tx_num: how many TX channels
2547472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2548472df3cbSBarry Song  *           0~num-1 uses
2549472df3cbSBarry Song  * @rx_num: how many RX channels
2550472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2551472df3cbSBarry Song  *           0~num-1 uses
2552472df3cbSBarry Song  *
2553472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2554472df3cbSBarry Song  */
2555472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2556472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2557472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2558472df3cbSBarry Song {
2559f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2560f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2561472df3cbSBarry Song 			rx_num, rx_slot);
2562472df3cbSBarry Song 	else
2563472df3cbSBarry Song 		return -EINVAL;
2564472df3cbSBarry Song }
2565472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2566472df3cbSBarry Song 
2567472df3cbSBarry Song /**
25688c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
25698c6529dbSLiam Girdwood  * @dai: DAI
25708c6529dbSLiam Girdwood  * @tristate: tristate enable
25718c6529dbSLiam Girdwood  *
25728c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
25738c6529dbSLiam Girdwood  */
25748c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
25758c6529dbSLiam Girdwood {
2576f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2577f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
25788c6529dbSLiam Girdwood 	else
25798c6529dbSLiam Girdwood 		return -EINVAL;
25808c6529dbSLiam Girdwood }
25818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
25828c6529dbSLiam Girdwood 
25838c6529dbSLiam Girdwood /**
25848c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
25858c6529dbSLiam Girdwood  * @dai: DAI
25868c6529dbSLiam Girdwood  * @mute: mute enable
2587da18396fSMark Brown  * @direction: stream to mute
25888c6529dbSLiam Girdwood  *
25898c6529dbSLiam Girdwood  * Mutes the DAI DAC.
25908c6529dbSLiam Girdwood  */
2591da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2592da18396fSMark Brown 			     int direction)
25938c6529dbSLiam Girdwood {
2594da18396fSMark Brown 	if (!dai->driver)
2595da18396fSMark Brown 		return -ENOTSUPP;
2596da18396fSMark Brown 
2597da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2598da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2599da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2600da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2601f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
26028c6529dbSLiam Girdwood 	else
260304570c62SMark Brown 		return -ENOTSUPP;
26048c6529dbSLiam Girdwood }
26058c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26068c6529dbSLiam Girdwood 
2607c5af3a2eSMark Brown /**
2608c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2609c5af3a2eSMark Brown  *
2610ac11a2b3SMark Brown  * @card: Card to register
2611c5af3a2eSMark Brown  *
2612c5af3a2eSMark Brown  */
261370a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2614c5af3a2eSMark Brown {
2615923c5e61SMengdong Lin 	int i, ret;
26161a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2617f0fba2adSLiam Girdwood 
2618c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2619c5af3a2eSMark Brown 		return -EINVAL;
2620c5af3a2eSMark Brown 
26215a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
26225a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
26235a504963SStephen Warren 
2624923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
262588bd870fSBenoit Cousson 		if (ret) {
2626923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2627923c5e61SMengdong Lin 				link->name);
262888bd870fSBenoit Cousson 			return ret;
262988bd870fSBenoit Cousson 		}
26305a504963SStephen Warren 	}
26315a504963SStephen Warren 
2632ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2633ed77cc12SMark Brown 
2634111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2635111c6419SStephen Warren 
2636f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2637f8f80361SMengdong Lin 	card->num_dai_links = 0;
2638f0fba2adSLiam Girdwood 
26391a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
26409115171aSMark Brown 	card->num_rtd = 0;
2641db2a4165SFrank Mandarino 
2642db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
26438a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2644db2a4165SFrank Mandarino 	card->instantiated = 0;
2645db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2646a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2647db2a4165SFrank Mandarino 
2648b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2649b19e6e7bSMark Brown 	if (ret != 0)
26504e2576bdSKuninori Morimoto 		return ret;
2651db2a4165SFrank Mandarino 
2652988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
26531a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
265488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
265588bd870fSBenoit Cousson 		int j;
265688bd870fSBenoit Cousson 
265788bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
265888bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2659988e8cc4SNicolin Chen 			if (!codec_dai->active)
2660988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
266188bd870fSBenoit Cousson 		}
266288bd870fSBenoit Cousson 
2663988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2664988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2665988e8cc4SNicolin Chen 	}
2666988e8cc4SNicolin Chen 
2667b19e6e7bSMark Brown 	return ret;
2668db2a4165SFrank Mandarino }
266970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2670db2a4165SFrank Mandarino 
2671db2a4165SFrank Mandarino /**
2672db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2673db2a4165SFrank Mandarino  *
2674db2a4165SFrank Mandarino  * @card: Card to unregister
2675db2a4165SFrank Mandarino  *
2676db2a4165SFrank Mandarino  */
267770a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2678db2a4165SFrank Mandarino {
267901e0df66SLars-Peter Clausen 	if (card->instantiated) {
268001e0df66SLars-Peter Clausen 		card->instantiated = false;
26811c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2682b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2683f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
26848f6f9b29SKuninori Morimoto 	}
2685db2a4165SFrank Mandarino 
2686db2a4165SFrank Mandarino 	return 0;
2687db2a4165SFrank Mandarino }
268870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2689db2a4165SFrank Mandarino 
2690db2a4165SFrank Mandarino /*
2691db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2692db2a4165SFrank Mandarino  * and sanitizing names.
2693db2a4165SFrank Mandarino  */
26940b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2695db2a4165SFrank Mandarino {
2696db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2697db2a4165SFrank Mandarino 	int id1, id2;
2698db2a4165SFrank Mandarino 
2699db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2700db2a4165SFrank Mandarino 		return NULL;
2701db2a4165SFrank Mandarino 
270258818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2703db2a4165SFrank Mandarino 
2704db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2705c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2706c5af3a2eSMark Brown 	if (found) {
2707c5af3a2eSMark Brown 		/* get ID */
2708c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2709c5af3a2eSMark Brown 
2710c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2711c5af3a2eSMark Brown 			if (*id == -1)
2712c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2713c5af3a2eSMark Brown 		}
2714c5af3a2eSMark Brown 
2715c5af3a2eSMark Brown 	} else {
2716c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2717c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2718c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2719c5af3a2eSMark Brown 
2720c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2721c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2722c5af3a2eSMark Brown 
2723c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2724c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
272558818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2726c5af3a2eSMark Brown 		} else
2727c5af3a2eSMark Brown 			*id = 0;
2728c5af3a2eSMark Brown 	}
2729c5af3a2eSMark Brown 
2730c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2731c5af3a2eSMark Brown }
2732c5af3a2eSMark Brown 
2733c5af3a2eSMark Brown /*
2734c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2735c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2736c5af3a2eSMark Brown  */
2737c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2738c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2739c5af3a2eSMark Brown {
2740c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
274110e8aa9aSMichał Mirosław 		dev_err(dev,
274210e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
274310e8aa9aSMichał Mirosław 			dev_name(dev));
2744c5af3a2eSMark Brown 		return NULL;
2745c5af3a2eSMark Brown 	}
2746c5af3a2eSMark Brown 
2747c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2748c5af3a2eSMark Brown }
2749c5af3a2eSMark Brown 
2750c5af3a2eSMark Brown /**
275132c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
27529115171aSMark Brown  *
275332c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
27549115171aSMark Brown  */
275532c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
27569115171aSMark Brown {
27575c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
27589115171aSMark Brown 
27595c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
276032c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
276132c9ba54SLars-Peter Clausen 			dai->name);
27629115171aSMark Brown 		list_del(&dai->list);
2763f0fba2adSLiam Girdwood 		kfree(dai->name);
2764f0fba2adSLiam Girdwood 		kfree(dai);
27659115171aSMark Brown 	}
276632c9ba54SLars-Peter Clausen }
27679115171aSMark Brown 
27685e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
27695e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
27705e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
27715e4fb372SMengdong Lin 	bool legacy_dai_naming)
27725e4fb372SMengdong Lin {
27735e4fb372SMengdong Lin 	struct device *dev = component->dev;
27745e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
27755e4fb372SMengdong Lin 
27765e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
27775e4fb372SMengdong Lin 
27785e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
27795e4fb372SMengdong Lin 	if (dai == NULL)
27805e4fb372SMengdong Lin 		return NULL;
27815e4fb372SMengdong Lin 
27825e4fb372SMengdong Lin 	/*
27835e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
27845e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
27855e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
27865e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
27875e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
27885e4fb372SMengdong Lin 	 * component-less anymore.
27895e4fb372SMengdong Lin 	 */
27905e4fb372SMengdong Lin 	if (legacy_dai_naming &&
27915e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
27925e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
27935e4fb372SMengdong Lin 	} else {
27945e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
27955e4fb372SMengdong Lin 		if (dai_drv->id)
27965e4fb372SMengdong Lin 			dai->id = dai_drv->id;
27975e4fb372SMengdong Lin 		else
27985e4fb372SMengdong Lin 			dai->id = component->num_dai;
27995e4fb372SMengdong Lin 	}
28005e4fb372SMengdong Lin 	if (dai->name == NULL) {
28015e4fb372SMengdong Lin 		kfree(dai);
28025e4fb372SMengdong Lin 		return NULL;
28035e4fb372SMengdong Lin 	}
28045e4fb372SMengdong Lin 
28055e4fb372SMengdong Lin 	dai->component = component;
28065e4fb372SMengdong Lin 	dai->dev = dev;
28075e4fb372SMengdong Lin 	dai->driver = dai_drv;
28085e4fb372SMengdong Lin 	if (!dai->driver->ops)
28095e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
28105e4fb372SMengdong Lin 
28115e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
28125e4fb372SMengdong Lin 	component->num_dai++;
28135e4fb372SMengdong Lin 
28145e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
28155e4fb372SMengdong Lin 	return dai;
28165e4fb372SMengdong Lin }
28175e4fb372SMengdong Lin 
28189115171aSMark Brown /**
281932c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
28209115171aSMark Brown  *
28216106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
28226106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2823ac11a2b3SMark Brown  * @count: Number of DAIs
282432c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
282532c9ba54SLars-Peter Clausen  *                     parent's name.
28269115171aSMark Brown  */
282732c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2828bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2829bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
28309115171aSMark Brown {
28316106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2832f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
283332c9ba54SLars-Peter Clausen 	unsigned int i;
283432c9ba54SLars-Peter Clausen 	int ret;
2835f0fba2adSLiam Girdwood 
2836f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
28379115171aSMark Brown 
2838bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2839bb13109dSLars-Peter Clausen 
28409115171aSMark Brown 	for (i = 0; i < count; i++) {
2841f0fba2adSLiam Girdwood 
28425e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
28435e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
2844c46e0079SAxel Lin 		if (dai == NULL) {
2845c46e0079SAxel Lin 			ret = -ENOMEM;
2846c46e0079SAxel Lin 			goto err;
2847c46e0079SAxel Lin 		}
2848f0fba2adSLiam Girdwood 	}
2849f0fba2adSLiam Girdwood 
28509115171aSMark Brown 	return 0;
28519115171aSMark Brown 
28529115171aSMark Brown err:
285332c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
28549115171aSMark Brown 
28559115171aSMark Brown 	return ret;
28569115171aSMark Brown }
28579115171aSMark Brown 
285868003e6cSMengdong Lin /**
285968003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
286068003e6cSMengdong Lin  *
286168003e6cSMengdong Lin  * @component: The component the DAIs are registered for
286268003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
286368003e6cSMengdong Lin  *
286468003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
286568003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
286668003e6cSMengdong Lin  * will be freed in the component cleanup.
286768003e6cSMengdong Lin  */
286868003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
286968003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
287068003e6cSMengdong Lin {
287168003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
287268003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
287368003e6cSMengdong Lin 	struct snd_soc_dai *dai;
287468003e6cSMengdong Lin 	int ret;
287568003e6cSMengdong Lin 
287668003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
287768003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
287868003e6cSMengdong Lin 			dai_drv->dobj.type);
287968003e6cSMengdong Lin 		return -EINVAL;
288068003e6cSMengdong Lin 	}
288168003e6cSMengdong Lin 
288268003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
288368003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
288468003e6cSMengdong Lin 	if (!dai)
288568003e6cSMengdong Lin 		return -ENOMEM;
288668003e6cSMengdong Lin 
288768003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
288868003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
288968003e6cSMengdong Lin 	 */
289068003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
289168003e6cSMengdong Lin 	if (ret != 0) {
289268003e6cSMengdong Lin 		dev_err(component->dev,
289368003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
289468003e6cSMengdong Lin 	}
289568003e6cSMengdong Lin 
289668003e6cSMengdong Lin 	return ret;
289768003e6cSMengdong Lin }
289868003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
289968003e6cSMengdong Lin 
290014e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
290114e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2902d191bd8dSKuninori Morimoto {
290314e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2904d191bd8dSKuninori Morimoto 
290514e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
290614e8bdebSLars-Peter Clausen }
2907d191bd8dSKuninori Morimoto 
290814e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
290914e8bdebSLars-Peter Clausen 	int event)
291014e8bdebSLars-Peter Clausen {
291114e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
291214e8bdebSLars-Peter Clausen 
291314e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
291414e8bdebSLars-Peter Clausen }
291514e8bdebSLars-Peter Clausen 
2916bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2917bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2918d191bd8dSKuninori Morimoto {
2919ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2920ce0fc93aSLars-Peter Clausen 
2921bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2922bb13109dSLars-Peter Clausen 	if (!component->name) {
2923bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2924d191bd8dSKuninori Morimoto 		return -ENOMEM;
2925d191bd8dSKuninori Morimoto 	}
2926d191bd8dSKuninori Morimoto 
2927bb13109dSLars-Peter Clausen 	component->dev = dev;
2928bb13109dSLars-Peter Clausen 	component->driver = driver;
292961aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
293061aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
2931*9178feb4SKuninori Morimoto 	component->suspend = component->driver->suspend;
2932*9178feb4SKuninori Morimoto 	component->resume = component->driver->resume;
2933e2c330b9SLars-Peter Clausen 
29344890140fSLars-Peter Clausen 	dapm = &component->dapm;
2935ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2936ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2937ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
29385819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
293914e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
294014e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
294114e8bdebSLars-Peter Clausen 	if (driver->stream_event)
294214e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2943ce0fc93aSLars-Peter Clausen 
294461aca564SLars-Peter Clausen 	component->controls = driver->controls;
294561aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
294661aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
294761aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
294861aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
294961aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
295061aca564SLars-Peter Clausen 
2951bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2952bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2953bb13109dSLars-Peter Clausen 
2954bb13109dSLars-Peter Clausen 	return 0;
2955d191bd8dSKuninori Morimoto }
2956d191bd8dSKuninori Morimoto 
295720feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2958886f5692SLars-Peter Clausen {
2959886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
296020feb881SLars-Peter Clausen 
2961886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2962886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2963886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2964886f5692SLars-Peter Clausen }
296520feb881SLars-Peter Clausen 
2966e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2967e874bf5fSLars-Peter Clausen 
296820feb881SLars-Peter Clausen /**
296920feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
297020feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
297120feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
297220feb881SLars-Peter Clausen  *
297320feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
297420feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
297520feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
297620feb881SLars-Peter Clausen  * before the first IO attempt of the component.
297720feb881SLars-Peter Clausen  */
297820feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
297920feb881SLars-Peter Clausen 	struct regmap *regmap)
298020feb881SLars-Peter Clausen {
298120feb881SLars-Peter Clausen 	component->regmap = regmap;
298220feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2983886f5692SLars-Peter Clausen }
298420feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
298520feb881SLars-Peter Clausen 
298620feb881SLars-Peter Clausen /**
298720feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
298820feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
298920feb881SLars-Peter Clausen  *
299020feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
299120feb881SLars-Peter Clausen  * removes the regmap instance from the component.
299220feb881SLars-Peter Clausen  *
299320feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
299420feb881SLars-Peter Clausen  * to initialize the regmap instance.
299520feb881SLars-Peter Clausen  */
299620feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
299720feb881SLars-Peter Clausen {
299820feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
299920feb881SLars-Peter Clausen 	component->regmap = NULL;
300020feb881SLars-Peter Clausen }
300120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3002886f5692SLars-Peter Clausen 
3003e874bf5fSLars-Peter Clausen #endif
3004d191bd8dSKuninori Morimoto 
3005bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3006bb13109dSLars-Peter Clausen {
300720feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
300820feb881SLars-Peter Clausen 		if (!component->regmap)
300920feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
301020feb881SLars-Peter Clausen 		if (component->regmap)
301120feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
301220feb881SLars-Peter Clausen 	}
3013886f5692SLars-Peter Clausen 
3014bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
30158a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3016d191bd8dSKuninori Morimoto }
3017d191bd8dSKuninori Morimoto 
3018bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3019bb13109dSLars-Peter Clausen {
3020d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3021bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3022d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3023bb13109dSLars-Peter Clausen }
3024d191bd8dSKuninori Morimoto 
3025bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3026bb13109dSLars-Peter Clausen {
3027bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3028bb13109dSLars-Peter Clausen 	kfree(component->name);
3029bb13109dSLars-Peter Clausen }
3030d191bd8dSKuninori Morimoto 
3031bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3032bb13109dSLars-Peter Clausen {
3033bb13109dSLars-Peter Clausen 	list_del(&component->list);
3034bb13109dSLars-Peter Clausen }
3035d191bd8dSKuninori Morimoto 
3036d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3037d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3038d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3039d191bd8dSKuninori Morimoto 			       int num_dai)
3040d191bd8dSKuninori Morimoto {
3041d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3042bb13109dSLars-Peter Clausen 	int ret;
3043d191bd8dSKuninori Morimoto 
3044bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3045d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3046d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3047d191bd8dSKuninori Morimoto 		return -ENOMEM;
3048d191bd8dSKuninori Morimoto 	}
3049d191bd8dSKuninori Morimoto 
3050bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3051bb13109dSLars-Peter Clausen 	if (ret)
3052bb13109dSLars-Peter Clausen 		goto err_free;
3053bb13109dSLars-Peter Clausen 
30543d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
305598e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
30563d59400fSLars-Peter Clausen 
3057bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3058bb13109dSLars-Peter Clausen 	if (ret < 0) {
3059f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3060bb13109dSLars-Peter Clausen 		goto err_cleanup;
3061bb13109dSLars-Peter Clausen 	}
3062bb13109dSLars-Peter Clausen 
3063bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3064bb13109dSLars-Peter Clausen 
3065bb13109dSLars-Peter Clausen 	return 0;
3066bb13109dSLars-Peter Clausen 
3067bb13109dSLars-Peter Clausen err_cleanup:
3068bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3069bb13109dSLars-Peter Clausen err_free:
3070bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3071bb13109dSLars-Peter Clausen 	return ret;
3072d191bd8dSKuninori Morimoto }
3073d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3074d191bd8dSKuninori Morimoto 
3075d191bd8dSKuninori Morimoto /**
3076d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3077d191bd8dSKuninori Morimoto  *
3078628536eaSJonathan Corbet  * @dev: The device to unregister
3079d191bd8dSKuninori Morimoto  */
3080d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3081d191bd8dSKuninori Morimoto {
3082d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3083d191bd8dSKuninori Morimoto 
308434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3085d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
308698e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3087d191bd8dSKuninori Morimoto 			goto found;
3088d191bd8dSKuninori Morimoto 	}
308934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3090d191bd8dSKuninori Morimoto 	return;
3091d191bd8dSKuninori Morimoto 
3092d191bd8dSKuninori Morimoto found:
30938a978234SLiam Girdwood 	snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
309434e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
309534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3096bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3097bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3098d191bd8dSKuninori Morimoto }
3099d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3100d191bd8dSKuninori Morimoto 
3101f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3102e2c330b9SLars-Peter Clausen {
3103e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3104e2c330b9SLars-Peter Clausen 
3105f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3106e2c330b9SLars-Peter Clausen }
3107e2c330b9SLars-Peter Clausen 
3108f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3109e2c330b9SLars-Peter Clausen {
3110e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3111e2c330b9SLars-Peter Clausen 
3112f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3113e2c330b9SLars-Peter Clausen }
3114e2c330b9SLars-Peter Clausen 
3115d191bd8dSKuninori Morimoto /**
311671a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
311771a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
311871a45cdaSLars-Peter Clausen  * @platform: The platform to add
31197d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
312012a48a8cSMark Brown  */
312171a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3122d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
312312a48a8cSMark Brown {
3124b37f1d12SLars-Peter Clausen 	int ret;
3125b37f1d12SLars-Peter Clausen 
3126bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3127bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3128bb13109dSLars-Peter Clausen 	if (ret)
3129bb13109dSLars-Peter Clausen 		return ret;
3130f0fba2adSLiam Girdwood 
3131f0fba2adSLiam Girdwood 	platform->dev = dev;
3132f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3133f1d45cc3SLars-Peter Clausen 
3134f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3135f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3136f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3137f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
313812a48a8cSMark Brown 
313981c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
314081c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
314181c7cfd1SLars-Peter Clausen #endif
314212a48a8cSMark Brown 
314312a48a8cSMark Brown 	mutex_lock(&client_mutex);
3144bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
314512a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
314612a48a8cSMark Brown 	mutex_unlock(&client_mutex);
314712a48a8cSMark Brown 
3148f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3149f4333203SLars-Peter Clausen 		platform->component.name);
315012a48a8cSMark Brown 
315112a48a8cSMark Brown 	return 0;
315212a48a8cSMark Brown }
315371a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
315471a45cdaSLars-Peter Clausen 
315571a45cdaSLars-Peter Clausen /**
315671a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
315771a45cdaSLars-Peter Clausen  *
3158628536eaSJonathan Corbet  * @dev: The device for the platform
3159628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
316071a45cdaSLars-Peter Clausen  */
316171a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
316271a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
316371a45cdaSLars-Peter Clausen {
316471a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
316571a45cdaSLars-Peter Clausen 	int ret;
316671a45cdaSLars-Peter Clausen 
316771a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
316871a45cdaSLars-Peter Clausen 
316971a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
317071a45cdaSLars-Peter Clausen 	if (platform == NULL)
317171a45cdaSLars-Peter Clausen 		return -ENOMEM;
317271a45cdaSLars-Peter Clausen 
317371a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
317471a45cdaSLars-Peter Clausen 	if (ret)
317571a45cdaSLars-Peter Clausen 		kfree(platform);
317671a45cdaSLars-Peter Clausen 
317771a45cdaSLars-Peter Clausen 	return ret;
317871a45cdaSLars-Peter Clausen }
317912a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
318012a48a8cSMark Brown 
318112a48a8cSMark Brown /**
318271a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
318371a45cdaSLars-Peter Clausen  * @platform: the platform to remove
318471a45cdaSLars-Peter Clausen  */
318571a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
318671a45cdaSLars-Peter Clausen {
3187b37f1d12SLars-Peter Clausen 
318871a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
318971a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3190bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
319171a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
319271a45cdaSLars-Peter Clausen 
319371a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3194f4333203SLars-Peter Clausen 		platform->component.name);
3195decc27b0SDaniel Mack 
3196decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
319771a45cdaSLars-Peter Clausen }
319871a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
319971a45cdaSLars-Peter Clausen 
320071a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
320171a45cdaSLars-Peter Clausen {
320271a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
320371a45cdaSLars-Peter Clausen 
320434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
320571a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
320634e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
320734e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
320871a45cdaSLars-Peter Clausen 			return platform;
320971a45cdaSLars-Peter Clausen 		}
321034e81ab4SLars-Peter Clausen 	}
321134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
321271a45cdaSLars-Peter Clausen 
321371a45cdaSLars-Peter Clausen 	return NULL;
321471a45cdaSLars-Peter Clausen }
321571a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
321671a45cdaSLars-Peter Clausen 
321771a45cdaSLars-Peter Clausen /**
321812a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
321912a48a8cSMark Brown  *
3220628536eaSJonathan Corbet  * @dev: platform to unregister
322112a48a8cSMark Brown  */
3222f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
322312a48a8cSMark Brown {
3224f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3225f0fba2adSLiam Girdwood 
322671a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
322771a45cdaSLars-Peter Clausen 	if (!platform)
3228f0fba2adSLiam Girdwood 		return;
3229f0fba2adSLiam Girdwood 
323071a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3231f0fba2adSLiam Girdwood 	kfree(platform);
323212a48a8cSMark Brown }
323312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
323412a48a8cSMark Brown 
3235151ab22cSMark Brown static u64 codec_format_map[] = {
3236151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3237151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3238151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3239151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3240151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3241151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3242151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3243151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3244151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3245151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3246151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3247151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3248151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3249151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3250151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3251151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3252151ab22cSMark Brown };
3253151ab22cSMark Brown 
3254151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3255151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3256151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3257151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3258151ab22cSMark Brown  */
3259151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3260151ab22cSMark Brown {
3261151ab22cSMark Brown 	int i;
3262151ab22cSMark Brown 
3263151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3264151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3265151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3266151ab22cSMark Brown }
3267151ab22cSMark Brown 
3268f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3269f1d45cc3SLars-Peter Clausen {
3270f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3271f1d45cc3SLars-Peter Clausen 
3272f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3273f1d45cc3SLars-Peter Clausen }
3274f1d45cc3SLars-Peter Clausen 
3275f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3276f1d45cc3SLars-Peter Clausen {
3277f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3278f1d45cc3SLars-Peter Clausen 
3279f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3280f1d45cc3SLars-Peter Clausen }
3281f1d45cc3SLars-Peter Clausen 
3282*9178feb4SKuninori Morimoto static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3283*9178feb4SKuninori Morimoto {
3284*9178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3285*9178feb4SKuninori Morimoto 
3286*9178feb4SKuninori Morimoto 	return codec->driver->suspend(codec);
3287*9178feb4SKuninori Morimoto }
3288*9178feb4SKuninori Morimoto 
3289*9178feb4SKuninori Morimoto static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3290*9178feb4SKuninori Morimoto {
3291*9178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3292*9178feb4SKuninori Morimoto 
3293*9178feb4SKuninori Morimoto 	return codec->driver->resume(codec);
3294*9178feb4SKuninori Morimoto }
3295*9178feb4SKuninori Morimoto 
3296e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3297e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3298e2c330b9SLars-Peter Clausen {
3299e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3300e2c330b9SLars-Peter Clausen 
3301e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3302e2c330b9SLars-Peter Clausen }
3303e2c330b9SLars-Peter Clausen 
3304e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3305e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3306e2c330b9SLars-Peter Clausen {
3307e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3308e2c330b9SLars-Peter Clausen 
3309e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3310e2c330b9SLars-Peter Clausen 
3311e2c330b9SLars-Peter Clausen 	return 0;
3312e2c330b9SLars-Peter Clausen }
3313e2c330b9SLars-Peter Clausen 
331468f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
331568f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
331668f831c2SLars-Peter Clausen {
331768f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
331868f831c2SLars-Peter Clausen 
331968f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
332068f831c2SLars-Peter Clausen }
332168f831c2SLars-Peter Clausen 
33220d0cf00aSMark Brown /**
33230d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
33240d0cf00aSMark Brown  *
3325628536eaSJonathan Corbet  * @dev: The parent device for this codec
3326628536eaSJonathan Corbet  * @codec_drv: Codec driver
3327628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3328628536eaSJonathan Corbet  * @num_dai: Number of DAIs
33290d0cf00aSMark Brown  */
3330f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3331001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3332001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3333001ae4c0SMark Brown 			   int num_dai)
33340d0cf00aSMark Brown {
33354890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3336f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3337bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3338f0fba2adSLiam Girdwood 	int ret, i;
3339151ab22cSMark Brown 
3340f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
33410d0cf00aSMark Brown 
3342f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3343f0fba2adSLiam Girdwood 	if (codec == NULL)
3344f0fba2adSLiam Girdwood 		return -ENOMEM;
33450d0cf00aSMark Brown 
3346f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3347ce0fc93aSLars-Peter Clausen 
3348bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3349bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3350bb13109dSLars-Peter Clausen 	if (ret)
3351bb13109dSLars-Peter Clausen 		goto err_free;
3352151ab22cSMark Brown 
3353f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3354f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3355f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3356f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
3357*9178feb4SKuninori Morimoto 	if (codec_drv->suspend)
3358*9178feb4SKuninori Morimoto 		codec->component.suspend = snd_soc_codec_drv_suspend;
3359*9178feb4SKuninori Morimoto 	if (codec_drv->resume)
3360*9178feb4SKuninori Morimoto 		codec->component.resume = snd_soc_codec_drv_resume;
3361e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3362e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3363e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3364e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
33653d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
33664890140fSLars-Peter Clausen 
33674890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
33684890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
33694890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
337014e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
33714890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
337268f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
33734890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3374f0fba2adSLiam Girdwood 	codec->dev = dev;
3375f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3376e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3377f0fba2adSLiam Girdwood 
337881c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
337981c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
338081c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
338181c7cfd1SLars-Peter Clausen #endif
3382a39f75f7SXiubo Li 
3383a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3384886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3385a39f75f7SXiubo Li 
3386f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3387f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3388f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3389f0fba2adSLiam Girdwood 	}
3390f0fba2adSLiam Girdwood 
3391bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3392bb13109dSLars-Peter Clausen 	if (ret < 0) {
3393f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3394bb13109dSLars-Peter Clausen 		goto err_cleanup;
3395bb13109dSLars-Peter Clausen 	}
3396bb13109dSLars-Peter Clausen 
3397bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3398bb13109dSLars-Peter Clausen 		dai->codec = codec;
3399bb13109dSLars-Peter Clausen 
3400054880feSMark Brown 	mutex_lock(&client_mutex);
3401bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3402054880feSMark Brown 	list_add(&codec->list, &codec_list);
3403054880feSMark Brown 	mutex_unlock(&client_mutex);
3404054880feSMark Brown 
3405f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3406f4333203SLars-Peter Clausen 		codec->component.name);
34070d0cf00aSMark Brown 	return 0;
3408f0fba2adSLiam Girdwood 
3409bb13109dSLars-Peter Clausen err_cleanup:
3410bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3411bb13109dSLars-Peter Clausen err_free:
3412f0fba2adSLiam Girdwood 	kfree(codec);
3413f0fba2adSLiam Girdwood 	return ret;
34140d0cf00aSMark Brown }
34150d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
34160d0cf00aSMark Brown 
34170d0cf00aSMark Brown /**
34180d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
34190d0cf00aSMark Brown  *
3420628536eaSJonathan Corbet  * @dev: codec to unregister
34210d0cf00aSMark Brown  */
3422f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
34230d0cf00aSMark Brown {
3424f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3425f0fba2adSLiam Girdwood 
342634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3427f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3428f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3429f0fba2adSLiam Girdwood 			goto found;
3430f0fba2adSLiam Girdwood 	}
343134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3432f0fba2adSLiam Girdwood 	return;
3433f0fba2adSLiam Girdwood 
3434f0fba2adSLiam Girdwood found:
34350d0cf00aSMark Brown 	list_del(&codec->list);
3436bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
34370d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
34380d0cf00aSMark Brown 
3439f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3440f4333203SLars-Peter Clausen 			codec->component.name);
3441f0fba2adSLiam Girdwood 
3442bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
34437a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3444f0fba2adSLiam Girdwood 	kfree(codec);
34450d0cf00aSMark Brown }
34460d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
34470d0cf00aSMark Brown 
3448bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3449bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3450bec4fa05SStephen Warren 			       const char *propname)
3451bec4fa05SStephen Warren {
34527e07e7c0STushar Behera 	struct device_node *np;
3453bec4fa05SStephen Warren 	int ret;
3454bec4fa05SStephen Warren 
34557e07e7c0STushar Behera 	if (!card->dev) {
34567e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
34577e07e7c0STushar Behera 		return -EINVAL;
34587e07e7c0STushar Behera 	}
34597e07e7c0STushar Behera 
34607e07e7c0STushar Behera 	np = card->dev->of_node;
34617e07e7c0STushar Behera 
3462bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3463bec4fa05SStephen Warren 	/*
3464bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3465bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3466bec4fa05SStephen Warren 	 * snd_soc_register_card.
3467bec4fa05SStephen Warren 	 */
3468bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3469bec4fa05SStephen Warren 		dev_err(card->dev,
3470f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3471bec4fa05SStephen Warren 			propname, ret);
3472bec4fa05SStephen Warren 		return ret;
3473bec4fa05SStephen Warren 	}
3474bec4fa05SStephen Warren 
3475bec4fa05SStephen Warren 	return 0;
3476bec4fa05SStephen Warren }
3477bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3478bec4fa05SStephen Warren 
34799a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
34809a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
34819a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
34829a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
34839a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
34849a6d4860SXiubo Li };
34859a6d4860SXiubo Li 
34869a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
34879a6d4860SXiubo Li 					  const char *propname)
34889a6d4860SXiubo Li {
34899a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
34909a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
34919a6d4860SXiubo Li 	const char *template, *wname;
34929a6d4860SXiubo Li 	int i, j, num_widgets, ret;
34939a6d4860SXiubo Li 
34949a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
34959a6d4860SXiubo Li 	if (num_widgets < 0) {
34969a6d4860SXiubo Li 		dev_err(card->dev,
34979a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
34989a6d4860SXiubo Li 		return -EINVAL;
34999a6d4860SXiubo Li 	}
35009a6d4860SXiubo Li 	if (num_widgets & 1) {
35019a6d4860SXiubo Li 		dev_err(card->dev,
35029a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
35039a6d4860SXiubo Li 		return -EINVAL;
35049a6d4860SXiubo Li 	}
35059a6d4860SXiubo Li 
35069a6d4860SXiubo Li 	num_widgets /= 2;
35079a6d4860SXiubo Li 	if (!num_widgets) {
35089a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
35099a6d4860SXiubo Li 			propname);
35109a6d4860SXiubo Li 		return -EINVAL;
35119a6d4860SXiubo Li 	}
35129a6d4860SXiubo Li 
35139a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
35149a6d4860SXiubo Li 			       GFP_KERNEL);
35159a6d4860SXiubo Li 	if (!widgets) {
35169a6d4860SXiubo Li 		dev_err(card->dev,
35179a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
35189a6d4860SXiubo Li 		return -ENOMEM;
35199a6d4860SXiubo Li 	}
35209a6d4860SXiubo Li 
35219a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
35229a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35239a6d4860SXiubo Li 			2 * i, &template);
35249a6d4860SXiubo Li 		if (ret) {
35259a6d4860SXiubo Li 			dev_err(card->dev,
35269a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35279a6d4860SXiubo Li 				propname, 2 * i, ret);
35289a6d4860SXiubo Li 			return -EINVAL;
35299a6d4860SXiubo Li 		}
35309a6d4860SXiubo Li 
35319a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
35329a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
35339a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
35349a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
35359a6d4860SXiubo Li 				break;
35369a6d4860SXiubo Li 			}
35379a6d4860SXiubo Li 		}
35389a6d4860SXiubo Li 
35399a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
35409a6d4860SXiubo Li 			dev_err(card->dev,
35419a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
35429a6d4860SXiubo Li 				template);
35439a6d4860SXiubo Li 			return -EINVAL;
35449a6d4860SXiubo Li 		}
35459a6d4860SXiubo Li 
35469a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35479a6d4860SXiubo Li 						    (2 * i) + 1,
35489a6d4860SXiubo Li 						    &wname);
35499a6d4860SXiubo Li 		if (ret) {
35509a6d4860SXiubo Li 			dev_err(card->dev,
35519a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35529a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
35539a6d4860SXiubo Li 			return -EINVAL;
35549a6d4860SXiubo Li 		}
35559a6d4860SXiubo Li 
35569a6d4860SXiubo Li 		widgets[i].name = wname;
35579a6d4860SXiubo Li 	}
35589a6d4860SXiubo Li 
3559f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3560f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
35619a6d4860SXiubo Li 
35629a6d4860SXiubo Li 	return 0;
35639a6d4860SXiubo Li }
35649a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
35659a6d4860SXiubo Li 
35666131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
35676131084aSJyri Sarha 				    const char *prop_name,
35686131084aSJyri Sarha 				    unsigned int *mask)
35696131084aSJyri Sarha {
35706131084aSJyri Sarha 	u32 val;
35716c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
35726131084aSJyri Sarha 	int i;
35736131084aSJyri Sarha 
35746131084aSJyri Sarha 	if (!of_slot_mask)
35756131084aSJyri Sarha 		return 0;
35766131084aSJyri Sarha 	val /= sizeof(u32);
35776131084aSJyri Sarha 	for (i = 0; i < val; i++)
35786131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
35796131084aSJyri Sarha 			*mask |= (1 << i);
35806131084aSJyri Sarha 
35816131084aSJyri Sarha 	return val;
35826131084aSJyri Sarha }
35836131084aSJyri Sarha 
358489c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
35856131084aSJyri Sarha 			      unsigned int *tx_mask,
35866131084aSJyri Sarha 			      unsigned int *rx_mask,
358789c67857SXiubo Li 			      unsigned int *slots,
358889c67857SXiubo Li 			      unsigned int *slot_width)
358989c67857SXiubo Li {
359089c67857SXiubo Li 	u32 val;
359189c67857SXiubo Li 	int ret;
359289c67857SXiubo Li 
35936131084aSJyri Sarha 	if (tx_mask)
35946131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
35956131084aSJyri Sarha 	if (rx_mask)
35966131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
35976131084aSJyri Sarha 
359889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
359989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
360089c67857SXiubo Li 		if (ret)
360189c67857SXiubo Li 			return ret;
360289c67857SXiubo Li 
360389c67857SXiubo Li 		if (slots)
360489c67857SXiubo Li 			*slots = val;
360589c67857SXiubo Li 	}
360689c67857SXiubo Li 
360789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
360889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
360989c67857SXiubo Li 		if (ret)
361089c67857SXiubo Li 			return ret;
361189c67857SXiubo Li 
361289c67857SXiubo Li 		if (slot_width)
361389c67857SXiubo Li 			*slot_width = val;
361489c67857SXiubo Li 	}
361589c67857SXiubo Li 
361689c67857SXiubo Li 	return 0;
361789c67857SXiubo Li }
361889c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
361989c67857SXiubo Li 
36205e3cdaa2SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
36215e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
36225e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
36235e3cdaa2SKuninori Morimoto 				   const char *propname)
36245e3cdaa2SKuninori Morimoto {
36255e3cdaa2SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
36265e3cdaa2SKuninori Morimoto 	const char *str;
36275e3cdaa2SKuninori Morimoto 	int ret;
36285e3cdaa2SKuninori Morimoto 
36295e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
36305e3cdaa2SKuninori Morimoto 	if (ret < 0) {
36315e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
36325e3cdaa2SKuninori Morimoto 		return;
36335e3cdaa2SKuninori Morimoto 	}
36345e3cdaa2SKuninori Morimoto 
36355e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
36365e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
36375e3cdaa2SKuninori Morimoto }
36385e3cdaa2SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
36395e3cdaa2SKuninori Morimoto 
3640a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3641a4a54dd5SStephen Warren 				   const char *propname)
3642a4a54dd5SStephen Warren {
3643a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3644e3b1e6a1SMark Brown 	int num_routes;
3645a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3646a4a54dd5SStephen Warren 	int i, ret;
3647a4a54dd5SStephen Warren 
3648a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3649c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
365010e8aa9aSMichał Mirosław 		dev_err(card->dev,
365110e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
365210e8aa9aSMichał Mirosław 			propname);
3653a4a54dd5SStephen Warren 		return -EINVAL;
3654a4a54dd5SStephen Warren 	}
3655a4a54dd5SStephen Warren 	num_routes /= 2;
3656a4a54dd5SStephen Warren 	if (!num_routes) {
3657f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3658a4a54dd5SStephen Warren 			propname);
3659a4a54dd5SStephen Warren 		return -EINVAL;
3660a4a54dd5SStephen Warren 	}
3661a4a54dd5SStephen Warren 
3662e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3663a4a54dd5SStephen Warren 			      GFP_KERNEL);
3664a4a54dd5SStephen Warren 	if (!routes) {
3665a4a54dd5SStephen Warren 		dev_err(card->dev,
3666f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3667a4a54dd5SStephen Warren 		return -EINVAL;
3668a4a54dd5SStephen Warren 	}
3669a4a54dd5SStephen Warren 
3670a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3671a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3672e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3673a4a54dd5SStephen Warren 		if (ret) {
3674c871bd0bSMark Brown 			dev_err(card->dev,
3675c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3676c871bd0bSMark Brown 				propname, 2 * i, ret);
3677a4a54dd5SStephen Warren 			return -EINVAL;
3678a4a54dd5SStephen Warren 		}
3679a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3680e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3681a4a54dd5SStephen Warren 		if (ret) {
3682a4a54dd5SStephen Warren 			dev_err(card->dev,
3683c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3684c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3685a4a54dd5SStephen Warren 			return -EINVAL;
3686a4a54dd5SStephen Warren 		}
3687a4a54dd5SStephen Warren 	}
3688a4a54dd5SStephen Warren 
3689f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3690f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3691a4a54dd5SStephen Warren 
3692a4a54dd5SStephen Warren 	return 0;
3693a4a54dd5SStephen Warren }
3694a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3695a4a54dd5SStephen Warren 
3696a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3697389cb834SJyri Sarha 				     const char *prefix,
3698389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3699389cb834SJyri Sarha 				     struct device_node **framemaster)
3700a7930ed4SKuninori Morimoto {
3701a7930ed4SKuninori Morimoto 	int ret, i;
3702a7930ed4SKuninori Morimoto 	char prop[128];
3703a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3704a7930ed4SKuninori Morimoto 	int bit, frame;
3705a7930ed4SKuninori Morimoto 	const char *str;
3706a7930ed4SKuninori Morimoto 	struct {
3707a7930ed4SKuninori Morimoto 		char *name;
3708a7930ed4SKuninori Morimoto 		unsigned int val;
3709a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3710a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3711a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3712a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3713a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3714a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3715a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3716a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3717a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3718a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3719a7930ed4SKuninori Morimoto 	};
3720a7930ed4SKuninori Morimoto 
3721a7930ed4SKuninori Morimoto 	if (!prefix)
3722a7930ed4SKuninori Morimoto 		prefix = "";
3723a7930ed4SKuninori Morimoto 
3724a7930ed4SKuninori Morimoto 	/*
3725a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3726a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3727a7930ed4SKuninori Morimoto 	 */
3728a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3729a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3730a7930ed4SKuninori Morimoto 	if (ret == 0) {
3731a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3732a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3733a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3734a7930ed4SKuninori Morimoto 				break;
3735a7930ed4SKuninori Morimoto 			}
3736a7930ed4SKuninori Morimoto 		}
3737a7930ed4SKuninori Morimoto 	}
3738a7930ed4SKuninori Morimoto 
3739a7930ed4SKuninori Morimoto 	/*
37408c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3741a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3742a7930ed4SKuninori Morimoto 	 */
37438c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
374451930295SJulia Lawall 	if (of_property_read_bool(np, prop))
37458c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
37468c2d6a9fSKuninori Morimoto 	else
37478c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3748a7930ed4SKuninori Morimoto 
3749a7930ed4SKuninori Morimoto 	/*
3750a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3751a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3752a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3753a7930ed4SKuninori Morimoto 	 */
3754a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3755a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3756a7930ed4SKuninori Morimoto 
3757a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3758a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3759a7930ed4SKuninori Morimoto 
3760a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3761a7930ed4SKuninori Morimoto 	case 0x11:
3762a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3763a7930ed4SKuninori Morimoto 		break;
3764a7930ed4SKuninori Morimoto 	case 0x10:
3765a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3766a7930ed4SKuninori Morimoto 		break;
3767a7930ed4SKuninori Morimoto 	case 0x01:
3768a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3769a7930ed4SKuninori Morimoto 		break;
3770a7930ed4SKuninori Morimoto 	default:
3771a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3772a7930ed4SKuninori Morimoto 		break;
3773a7930ed4SKuninori Morimoto 	}
3774a7930ed4SKuninori Morimoto 
3775a7930ed4SKuninori Morimoto 	/*
3776a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3777a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3778a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3779a7930ed4SKuninori Morimoto 	 */
3780a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3781a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3782389cb834SJyri Sarha 	if (bit && bitclkmaster)
3783389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3784a7930ed4SKuninori Morimoto 
3785a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3786a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3787389cb834SJyri Sarha 	if (frame && framemaster)
3788389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3789a7930ed4SKuninori Morimoto 
3790a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3791a7930ed4SKuninori Morimoto 	case 0x11:
3792a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3793a7930ed4SKuninori Morimoto 		break;
3794a7930ed4SKuninori Morimoto 	case 0x10:
3795a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3796a7930ed4SKuninori Morimoto 		break;
3797a7930ed4SKuninori Morimoto 	case 0x01:
3798a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3799a7930ed4SKuninori Morimoto 		break;
3800a7930ed4SKuninori Morimoto 	default:
3801a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3802a7930ed4SKuninori Morimoto 		break;
3803a7930ed4SKuninori Morimoto 	}
3804a7930ed4SKuninori Morimoto 
3805a7930ed4SKuninori Morimoto 	return format;
3806a7930ed4SKuninori Morimoto }
3807a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3808a7930ed4SKuninori Morimoto 
380993b0f3eeSJean-Francois Moine static int snd_soc_get_dai_name(struct of_phandle_args *args,
3810cb470087SKuninori Morimoto 				const char **dai_name)
3811cb470087SKuninori Morimoto {
3812cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
38133e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
381493b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3815cb470087SKuninori Morimoto 
3816cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3817cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
38183e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
38193e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
38203e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
38213e0aa8d8SJyri Sarha 
38223e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3823cb470087SKuninori Morimoto 			continue;
3824cb470087SKuninori Morimoto 
38256833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
382693b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
382793b0f3eeSJean-Francois Moine 							     args,
382893b0f3eeSJean-Francois Moine 							     dai_name);
38296833c452SKuninori Morimoto 		} else {
38306833c452SKuninori Morimoto 			int id = -1;
38316833c452SKuninori Morimoto 
383293b0f3eeSJean-Francois Moine 			switch (args->args_count) {
38336833c452SKuninori Morimoto 			case 0:
38346833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
38356833c452SKuninori Morimoto 				break;
38366833c452SKuninori Morimoto 			case 1:
383793b0f3eeSJean-Francois Moine 				id = args->args[0];
38386833c452SKuninori Morimoto 				break;
38396833c452SKuninori Morimoto 			default:
38406833c452SKuninori Morimoto 				/* not supported */
3841cb470087SKuninori Morimoto 				break;
3842cb470087SKuninori Morimoto 			}
3843cb470087SKuninori Morimoto 
38446833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
38456833c452SKuninori Morimoto 				ret = -EINVAL;
38463dcba280SNicolin Chen 				continue;
38476833c452SKuninori Morimoto 			}
3848e41975edSXiubo Li 
3849e41975edSXiubo Li 			ret = 0;
3850e41975edSXiubo Li 
3851e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3852e41975edSXiubo Li 			if (!*dai_name)
3853e41975edSXiubo Li 				*dai_name = pos->name;
38546833c452SKuninori Morimoto 		}
38556833c452SKuninori Morimoto 
3856cb470087SKuninori Morimoto 		break;
3857cb470087SKuninori Morimoto 	}
3858cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
385993b0f3eeSJean-Francois Moine 	return ret;
386093b0f3eeSJean-Francois Moine }
386193b0f3eeSJean-Francois Moine 
386293b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
386393b0f3eeSJean-Francois Moine 			    const char **dai_name)
386493b0f3eeSJean-Francois Moine {
386593b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
386693b0f3eeSJean-Francois Moine 	int ret;
386793b0f3eeSJean-Francois Moine 
386893b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
386993b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
387093b0f3eeSJean-Francois Moine 	if (ret)
387193b0f3eeSJean-Francois Moine 		return ret;
387293b0f3eeSJean-Francois Moine 
387393b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3874cb470087SKuninori Morimoto 
3875cb470087SKuninori Morimoto 	of_node_put(args.np);
3876cb470087SKuninori Morimoto 
3877cb470087SKuninori Morimoto 	return ret;
3878cb470087SKuninori Morimoto }
3879cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3880cb470087SKuninori Morimoto 
388193b0f3eeSJean-Francois Moine /*
388293b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
388393b0f3eeSJean-Francois Moine  * @dev: Card device
388493b0f3eeSJean-Francois Moine  * @of_node: Device node
388593b0f3eeSJean-Francois Moine  * @dai_link: DAI link
388693b0f3eeSJean-Francois Moine  *
388793b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
388893b0f3eeSJean-Francois Moine  * 'sound-dai'.
388993b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
389093b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
389193b0f3eeSJean-Francois Moine  *
389293b0f3eeSJean-Francois Moine  * Returns 0 for success
389393b0f3eeSJean-Francois Moine  */
389493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
389593b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
389693b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
389793b0f3eeSJean-Francois Moine {
389893b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
389993b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
390093b0f3eeSJean-Francois Moine 	char *name;
390193b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
390293b0f3eeSJean-Francois Moine 
390393b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
390493b0f3eeSJean-Francois Moine 	name = "sound-dai";
390593b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
390693b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
390793b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
390893b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
390993b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
391093b0f3eeSJean-Francois Moine 		else
391193b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
391293b0f3eeSJean-Francois Moine 		return num_codecs;
391393b0f3eeSJean-Francois Moine 	}
391493b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
391593b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
391693b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
391793b0f3eeSJean-Francois Moine 	if (!component)
391893b0f3eeSJean-Francois Moine 		return -ENOMEM;
391993b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
392093b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
392193b0f3eeSJean-Francois Moine 
392293b0f3eeSJean-Francois Moine 	/* Parse the list */
392393b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
392493b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
392593b0f3eeSJean-Francois Moine 	     index++, component++) {
392693b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
392793b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
392893b0f3eeSJean-Francois Moine 						  index, &args);
392993b0f3eeSJean-Francois Moine 		if (ret)
393093b0f3eeSJean-Francois Moine 			goto err;
393193b0f3eeSJean-Francois Moine 		component->of_node = args.np;
393293b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
393393b0f3eeSJean-Francois Moine 		if (ret < 0)
393493b0f3eeSJean-Francois Moine 			goto err;
393593b0f3eeSJean-Francois Moine 	}
393693b0f3eeSJean-Francois Moine 	return 0;
393793b0f3eeSJean-Francois Moine err:
393893b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
393993b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
394093b0f3eeSJean-Francois Moine 	     index++, component++) {
394193b0f3eeSJean-Francois Moine 		if (!component->of_node)
394293b0f3eeSJean-Francois Moine 			break;
394393b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
394493b0f3eeSJean-Francois Moine 		component->of_node = NULL;
394593b0f3eeSJean-Francois Moine 	}
394693b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
394793b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
394893b0f3eeSJean-Francois Moine 	return ret;
394993b0f3eeSJean-Francois Moine }
395093b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
395193b0f3eeSJean-Francois Moine 
3952c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3953db2a4165SFrank Mandarino {
39546553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3955fb257897SMark Brown 	snd_soc_util_init();
3956fb257897SMark Brown 
3957db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3958db2a4165SFrank Mandarino }
39594abe8e16SMark Brown module_init(snd_soc_init);
3960db2a4165SFrank Mandarino 
39617d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3962db2a4165SFrank Mandarino {
3963fb257897SMark Brown 	snd_soc_util_exit();
39646553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3965fb257897SMark Brown 
3966384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3967384c89e2SMark Brown #endif
3968db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3969db2a4165SFrank Mandarino }
3970db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3971db2a4165SFrank Mandarino 
3972db2a4165SFrank Mandarino /* Module information */
3973d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3974db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3975db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3976db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3977