xref: /openbmc/linux/sound/soc/soc-core.c (revision c12c1aad98bb75b435e79c6208b56d2018b42f8b)
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 
7059178feb4SKuninori 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 
7099178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
7101547aba9SMark Brown 		 * bias _ON and should not be suspended. */
7119178feb4SKuninori Morimoto 		if (!component->suspended) {
7124890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7131547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
714125a25daSMark Brown 				/*
7159178feb4SKuninori 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) {
7219178feb4SKuninori 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:
7279178feb4SKuninori Morimoto 				if (component->suspend)
7289178feb4SKuninori Morimoto 					component->suspend(component);
7299178feb4SKuninori Morimoto 				component->suspended = 1;
7309178feb4SKuninori Morimoto 				if (component->regmap)
7319178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
732988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
7339178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
7341547aba9SMark Brown 				break;
7351547aba9SMark Brown 			default:
7369178feb4SKuninori Morimoto 				dev_dbg(component->dev,
7379178feb4SKuninori 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) {
7989178feb4SKuninori Morimoto 		if (component->suspended) {
7999178feb4SKuninori Morimoto 			if (component->resume)
8009178feb4SKuninori Morimoto 				component->resume(component);
8019178feb4SKuninori 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 
97517fb1755SMengdong Lin 
97617fb1755SMengdong Lin /**
97717fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
97817fb1755SMengdong Lin  *
97917fb1755SMengdong Lin  * @card: soc card
98017fb1755SMengdong Lin  * @id: DAI link ID to match
98117fb1755SMengdong Lin  * @name: DAI link name to match, optional
98217fb1755SMengdong Lin  * @stream name: DAI link stream name to match, optional
98317fb1755SMengdong Lin  *
98417fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
98517fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
98617fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
98717fb1755SMengdong Lin  * specified.
98817fb1755SMengdong Lin  *
98917fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
99017fb1755SMengdong Lin  */
99117fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
99217fb1755SMengdong Lin 					       int id, const char *name,
99317fb1755SMengdong Lin 					       const char *stream_name)
99417fb1755SMengdong Lin {
99517fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
99617fb1755SMengdong Lin 
99717fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
99817fb1755SMengdong Lin 
99917fb1755SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
100017fb1755SMengdong Lin 		if (link->id != id)
100117fb1755SMengdong Lin 			continue;
100217fb1755SMengdong Lin 
100317fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
100417fb1755SMengdong Lin 			continue;
100517fb1755SMengdong Lin 
100617fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
100717fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
100817fb1755SMengdong Lin 			continue;
100917fb1755SMengdong Lin 
101017fb1755SMengdong Lin 		return link;
101117fb1755SMengdong Lin 	}
101217fb1755SMengdong Lin 
101317fb1755SMengdong Lin 	return NULL;
101417fb1755SMengdong Lin }
101517fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
101617fb1755SMengdong Lin 
101749a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
101849a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
1019db2a4165SFrank Mandarino {
102049a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
102149a5ba1cSMengdong Lin 
102249a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
102349a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
102449a5ba1cSMengdong Lin 			return true;
102549a5ba1cSMengdong Lin 	}
102649a5ba1cSMengdong Lin 
102749a5ba1cSMengdong Lin 	return false;
102849a5ba1cSMengdong Lin }
102949a5ba1cSMengdong Lin 
10306f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10316f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1032db2a4165SFrank Mandarino {
10331a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
103488bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
103514621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
10361a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
1037435c5e25SMark Brown 	struct snd_soc_platform *platform;
103806859fcaSCharles Keepax 	struct device_node *platform_of_node;
1039848dd8beSMark Brown 	const char *platform_name;
104088bd870fSBenoit Cousson 	int i;
1041db2a4165SFrank Mandarino 
10426f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10436308419aSMark Brown 
104449a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
104549a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
104649a5ba1cSMengdong Lin 			dai_link->name);
104749a5ba1cSMengdong Lin 		return 0;
104849a5ba1cSMengdong Lin 	}
1049db2a4165SFrank Mandarino 
1050513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1051513cb311SSudip Mukherjee 	if (!rtd)
1052513cb311SSudip Mukherjee 		return -ENOMEM;
1053513cb311SSudip Mukherjee 
105414621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
105514621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
105614621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
105714621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1058b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1059f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1060f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
10611a497983SMengdong Lin 		goto _err_defer;
1062c5af3a2eSMark Brown 	}
1063c5af3a2eSMark Brown 
106488bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
106588bd870fSBenoit Cousson 
106688bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
10671a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
106888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
106914621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
107088bd870fSBenoit Cousson 		if (!codec_dais[i]) {
107112023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
107288bd870fSBenoit Cousson 				codecs[i].dai_name);
10731a497983SMengdong Lin 			goto _err_defer;
107412023a9aSMisael Lopez Cruz 		}
107588bd870fSBenoit Cousson 	}
107688bd870fSBenoit Cousson 
107788bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
107888bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
107988bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
108012023a9aSMisael Lopez Cruz 
1081848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1082848dd8beSMark Brown 	platform_name = dai_link->platform_name;
10835a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1084848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1085848dd8beSMark Brown 
1086b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
1087f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
108806859fcaSCharles Keepax 		platform_of_node = platform->dev->of_node;
108906859fcaSCharles Keepax 		if (!platform_of_node && platform->dev->parent->of_node)
109006859fcaSCharles Keepax 			platform_of_node = platform->dev->parent->of_node;
109106859fcaSCharles Keepax 
10925a504963SStephen Warren 		if (dai_link->platform_of_node) {
109306859fcaSCharles Keepax 			if (platform_of_node != dai_link->platform_of_node)
10945a504963SStephen Warren 				continue;
10955a504963SStephen Warren 		} else {
1096f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
10972610ab77SStephen Warren 				continue;
10985a504963SStephen Warren 		}
10992610ab77SStephen Warren 
1100f0fba2adSLiam Girdwood 		rtd->platform = platform;
1101f0fba2adSLiam Girdwood 	}
1102b19e6e7bSMark Brown 	if (!rtd->platform) {
1103f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1104f0fba2adSLiam Girdwood 			dai_link->platform_name);
110570fcad49SCharles Keepax 		goto _err_defer;
1106f0fba2adSLiam Girdwood 	}
1107b19e6e7bSMark Brown 
11081a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1109b19e6e7bSMark Brown 	return 0;
11101a497983SMengdong Lin 
11111a497983SMengdong Lin _err_defer:
11121a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11131a497983SMengdong Lin 	return  -EPROBE_DEFER;
11146b05eda6SMark Brown }
11156b05eda6SMark Brown 
1116f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1117d12cd198SStephen Warren {
1118abd31b32SLars-Peter Clausen 	if (!component->card)
111970090bbbSLars-Peter Clausen 		return;
1120d12cd198SStephen Warren 
1121d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1122d12cd198SStephen Warren 
1123f1d45cc3SLars-Peter Clausen 	if (component->remove)
1124f1d45cc3SLars-Peter Clausen 		component->remove(component);
1125d12cd198SStephen Warren 
1126f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1127d12cd198SStephen Warren 
1128f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1129abd31b32SLars-Peter Clausen 	component->card = NULL;
1130f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1131d12cd198SStephen Warren }
1132d12cd198SStephen Warren 
1133e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1134589c3563SJarkko Nikula {
1135589c3563SJarkko Nikula 	int err;
1136589c3563SJarkko Nikula 
1137e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1138e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1139e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1140e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1141589c3563SJarkko Nikula 			if (err < 0)
1142e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1143b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1144e60cd14fSLars-Peter Clausen 					dai->name, err);
1145b0aa88afSMisael Lopez Cruz 		}
1146e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1147b0aa88afSMisael Lopez Cruz 	}
1148b0aa88afSMisael Lopez Cruz }
1149b0aa88afSMisael Lopez Cruz 
11501a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11511a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1152f0fba2adSLiam Girdwood {
1153e60cd14fSLars-Peter Clausen 	int i;
1154f0fba2adSLiam Girdwood 
1155f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1156f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
115736ae1a96SMark Brown 		device_unregister(rtd->dev);
1158f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
115902a06d30SBarry Song 	}
116002a06d30SBarry Song 
1161f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
116288bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1163e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1164f0fba2adSLiam Girdwood 
1165e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1166f0fba2adSLiam Girdwood }
1167f0fba2adSLiam Girdwood 
11681a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11691a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
117062ae68faSStephen Warren {
117162ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
117262ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
117361aca564SLars-Peter Clausen 	struct snd_soc_component *component;
117488bd870fSBenoit Cousson 	int i;
117562ae68faSStephen Warren 
117662ae68faSStephen Warren 	/* remove the platform */
117770090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1178f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
117962ae68faSStephen Warren 
118062ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
118188bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
118261aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
118370090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
118461aca564SLars-Peter Clausen 			soc_remove_component(component);
118562ae68faSStephen Warren 	}
118662ae68faSStephen Warren 
118762ae68faSStephen Warren 	/* remove any CPU-side CODEC */
118862ae68faSStephen Warren 	if (cpu_dai) {
118970090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
119061aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
119162ae68faSStephen Warren 	}
119262ae68faSStephen Warren }
119362ae68faSStephen Warren 
11940671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11950671fd8eSKuninori Morimoto {
11961a497983SMengdong Lin 	int order;
11971a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1198f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
11990671fd8eSKuninori Morimoto 
12000168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
12010168bf0dSLiam Girdwood 			order++) {
12021a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12031a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
12040168bf0dSLiam Girdwood 	}
120562ae68faSStephen Warren 
120662ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
120762ae68faSStephen Warren 			order++) {
12081a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12091a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
121062ae68faSStephen Warren 	}
121162ae68faSStephen Warren 
1212f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1213f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1214f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1215f8f80361SMengdong Lin 				link->name);
1216f8f80361SMengdong Lin 
1217f8f80361SMengdong Lin 		list_del(&link->list);
1218f8f80361SMengdong Lin 		card->num_dai_links--;
12190671fd8eSKuninori Morimoto 	}
12200671fd8eSKuninori Morimoto }
12210671fd8eSKuninori Morimoto 
1222923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1223923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1224923c5e61SMengdong Lin {
1225923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1226923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1227923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1228923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1229923c5e61SMengdong Lin 
1230923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1231923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1232923c5e61SMengdong Lin 				GFP_KERNEL);
1233923c5e61SMengdong Lin 		if (!dai_link->codecs)
1234923c5e61SMengdong Lin 			return -ENOMEM;
1235923c5e61SMengdong Lin 
1236923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1237923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1238923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1239923c5e61SMengdong Lin 	}
1240923c5e61SMengdong Lin 
1241923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1242923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1243923c5e61SMengdong Lin 		return -EINVAL;
1244923c5e61SMengdong Lin 	}
1245923c5e61SMengdong Lin 
1246923c5e61SMengdong Lin 	return 0;
1247923c5e61SMengdong Lin }
1248923c5e61SMengdong Lin 
1249923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1250923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1251923c5e61SMengdong Lin {
1252923c5e61SMengdong Lin 	int i, ret;
1253923c5e61SMengdong Lin 
1254923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1255923c5e61SMengdong Lin 	if (ret) {
1256923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1257923c5e61SMengdong Lin 		return ret;
1258923c5e61SMengdong Lin 	}
1259923c5e61SMengdong Lin 
1260923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1261923c5e61SMengdong Lin 		/*
1262923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1263923c5e61SMengdong Lin 		 * not both or neither.
1264923c5e61SMengdong Lin 		 */
1265923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1266923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1267923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1268923c5e61SMengdong Lin 				link->name);
1269923c5e61SMengdong Lin 			return -EINVAL;
1270923c5e61SMengdong Lin 		}
1271923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1272923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1273923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1274923c5e61SMengdong Lin 				link->name);
1275923c5e61SMengdong Lin 			return -EINVAL;
1276923c5e61SMengdong Lin 		}
1277923c5e61SMengdong Lin 	}
1278923c5e61SMengdong Lin 
1279923c5e61SMengdong Lin 	/*
1280923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1281923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1282923c5e61SMengdong Lin 	 */
1283923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1284923c5e61SMengdong Lin 		dev_err(card->dev,
1285923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1286923c5e61SMengdong Lin 			link->name);
1287923c5e61SMengdong Lin 		return -EINVAL;
1288923c5e61SMengdong Lin 	}
1289923c5e61SMengdong Lin 
1290923c5e61SMengdong Lin 	/*
1291923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1292923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1293923c5e61SMengdong Lin 	 * name alone..
1294923c5e61SMengdong Lin 	 */
1295923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1296923c5e61SMengdong Lin 		dev_err(card->dev,
1297923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1298923c5e61SMengdong Lin 			link->name);
1299923c5e61SMengdong Lin 		return -EINVAL;
1300923c5e61SMengdong Lin 	}
1301923c5e61SMengdong Lin 	/*
1302923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1303923c5e61SMengdong Lin 	 * specified
1304923c5e61SMengdong Lin 	 */
1305923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1306923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1307923c5e61SMengdong Lin 		dev_err(card->dev,
1308923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1309923c5e61SMengdong Lin 			link->name);
1310923c5e61SMengdong Lin 		return -EINVAL;
1311923c5e61SMengdong Lin 	}
1312923c5e61SMengdong Lin 
1313923c5e61SMengdong Lin 	return 0;
1314923c5e61SMengdong Lin }
1315923c5e61SMengdong Lin 
1316f8f80361SMengdong Lin /**
1317f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1318f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1319f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1320f8f80361SMengdong Lin  *
1321f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1322f8f80361SMengdong Lin  *
1323f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1324f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1325f8f80361SMengdong Lin  * DAI links in dai_link array.
1326f8f80361SMengdong Lin  */
1327f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1328f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1329f8f80361SMengdong Lin {
1330f8f80361SMengdong Lin 	if (dai_link->dobj.type
1331f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1332f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1333f8f80361SMengdong Lin 			dai_link->dobj.type);
1334f8f80361SMengdong Lin 		return -EINVAL;
1335f8f80361SMengdong Lin 	}
1336f8f80361SMengdong Lin 
1337f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1338d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1339d6f220eaSMengdong Lin 	 * on the link created by topology.
1340d6f220eaSMengdong Lin 	 */
1341d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1342d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1343d6f220eaSMengdong Lin 
1344f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1345f8f80361SMengdong Lin 	card->num_dai_links++;
1346f8f80361SMengdong Lin 
1347f8f80361SMengdong Lin 	return 0;
1348f8f80361SMengdong Lin }
1349f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1350f8f80361SMengdong Lin 
1351f8f80361SMengdong Lin /**
1352f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1353f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1354f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1355f8f80361SMengdong Lin  *
1356f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1357f8f80361SMengdong Lin  *
1358f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1359f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1360f8f80361SMengdong Lin  */
1361f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1362f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1363f8f80361SMengdong Lin {
1364f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1365f8f80361SMengdong Lin 
1366f8f80361SMengdong Lin 	if (dai_link->dobj.type
1367f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1368f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1369f8f80361SMengdong Lin 			dai_link->dobj.type);
1370f8f80361SMengdong Lin 		return;
1371f8f80361SMengdong Lin 	}
1372f8f80361SMengdong Lin 
1373f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1374d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1375d6f220eaSMengdong Lin 	 * on the link created by topology.
1376d6f220eaSMengdong Lin 	 */
1377d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1378d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1379d6f220eaSMengdong Lin 
1380f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1381f8f80361SMengdong Lin 		if (link == dai_link) {
1382f8f80361SMengdong Lin 			list_del(&link->list);
1383f8f80361SMengdong Lin 			card->num_dai_links--;
1384f8f80361SMengdong Lin 			return;
1385f8f80361SMengdong Lin 		}
1386f8f80361SMengdong Lin 	}
1387f8f80361SMengdong Lin }
1388f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1389f0fba2adSLiam Girdwood 
1390ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
139194f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1392ead9b919SJarkko Nikula {
1393ead9b919SJarkko Nikula 	int i;
1394ead9b919SJarkko Nikula 
1395ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1396ead9b919SJarkko Nikula 		return;
1397ead9b919SJarkko Nikula 
1398ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1399ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
140094f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
14013ca041edSSebastian Reichel 			continue;
140294f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
14033ca041edSSebastian Reichel 			continue;
140494f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1405ead9b919SJarkko Nikula 		break;
1406ead9b919SJarkko Nikula 	}
1407ead9b919SJarkko Nikula }
1408ead9b919SJarkko Nikula 
1409f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1410f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1411589c3563SJarkko Nikula {
1412f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1413888df395SMark Brown 	struct snd_soc_dai *dai;
1414f1d45cc3SLars-Peter Clausen 	int ret;
1415589c3563SJarkko Nikula 
14161b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
141770090bbbSLars-Peter Clausen 		return 0;
1418589c3563SJarkko Nikula 
1419abd31b32SLars-Peter Clausen 	if (component->card) {
14201b7c1231SLars-Peter Clausen 		if (component->card != card) {
14211b7c1231SLars-Peter Clausen 			dev_err(component->dev,
14221b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
14231b7c1231SLars-Peter Clausen 				card->name, component->card->name);
14241b7c1231SLars-Peter Clausen 			return -ENODEV;
14251b7c1231SLars-Peter Clausen 		}
14261b7c1231SLars-Peter Clausen 		return 0;
14271b7c1231SLars-Peter Clausen 	}
14281b7c1231SLars-Peter Clausen 
1429abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1430abd31b32SLars-Peter Clausen 		return -ENODEV;
1431abd31b32SLars-Peter Clausen 
1432f1d45cc3SLars-Peter Clausen 	component->card = card;
1433f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1434f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1435589c3563SJarkko Nikula 
1436f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1437d5d1e0beSLars-Peter Clausen 
1438f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1439f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1440f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
144177530150SLars-Peter Clausen 
1442b318ad50SNariman Poushin 		if (ret != 0) {
1443f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1444b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1445b318ad50SNariman Poushin 			goto err_probe;
1446b318ad50SNariman Poushin 		}
1447b318ad50SNariman Poushin 	}
1448b318ad50SNariman Poushin 
14490634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
14500634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1451261edc70SNariman Poushin 		if (ret != 0) {
1452f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1453261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1454261edc70SNariman Poushin 			goto err_probe;
1455261edc70SNariman Poushin 		}
1456261edc70SNariman Poushin 	}
1457888df395SMark Brown 
1458f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1459f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1460589c3563SJarkko Nikula 		if (ret < 0) {
1461f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1462f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
146370d29331SJarkko Nikula 			goto err_probe;
1464589c3563SJarkko Nikula 		}
1465cb2cf612SLiam Girdwood 
1466f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1467f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1468956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1469f1d45cc3SLars-Peter Clausen 			component->name);
1470956245e9SLiam Girdwood 	}
1471956245e9SLiam Girdwood 
1472f2ed6b07SMengdong Lin 	/* machine specific init */
1473f2ed6b07SMengdong Lin 	if (component->init) {
1474f2ed6b07SMengdong Lin 		ret = component->init(component);
1475f2ed6b07SMengdong Lin 		if (ret < 0) {
1476f2ed6b07SMengdong Lin 			dev_err(component->dev,
1477f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1478f2ed6b07SMengdong Lin 			goto err_probe;
1479f2ed6b07SMengdong Lin 		}
1480f2ed6b07SMengdong Lin 	}
1481f2ed6b07SMengdong Lin 
1482f1d45cc3SLars-Peter Clausen 	if (component->controls)
1483f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1484f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1485f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1486f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1487f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1488956245e9SLiam Girdwood 
1489f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1490d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1491956245e9SLiam Girdwood 
1492956245e9SLiam Girdwood 	return 0;
1493956245e9SLiam Girdwood 
1494956245e9SLiam Girdwood err_probe:
1495f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1496abd31b32SLars-Peter Clausen 	component->card = NULL;
1497f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1498956245e9SLiam Girdwood 
1499956245e9SLiam Girdwood 	return ret;
1500956245e9SLiam Girdwood }
1501956245e9SLiam Girdwood 
150236ae1a96SMark Brown static void rtd_release(struct device *dev)
150336ae1a96SMark Brown {
150436ae1a96SMark Brown 	kfree(dev);
150536ae1a96SMark Brown }
1506f0fba2adSLiam Girdwood 
15075f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
15085f3484acSLars-Peter Clausen 	const char *name)
1509503ae5e0SMisael Lopez Cruz {
1510589c3563SJarkko Nikula 	int ret = 0;
1511589c3563SJarkko Nikula 
1512589c3563SJarkko Nikula 	/* register the rtd device */
151336ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
151436ae1a96SMark Brown 	if (!rtd->dev)
151536ae1a96SMark Brown 		return -ENOMEM;
151636ae1a96SMark Brown 	device_initialize(rtd->dev);
15175f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
151836ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1519d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1520f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
152136ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1522b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
152301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
152401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
152501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
152601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
152736ae1a96SMark Brown 	ret = device_add(rtd->dev);
1528589c3563SJarkko Nikula 	if (ret < 0) {
1529865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1530865df9cbSChuansheng Liu 		put_device(rtd->dev);
15315f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1532f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1533589c3563SJarkko Nikula 		return ret;
1534589c3563SJarkko Nikula 	}
1535589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1536589c3563SJarkko Nikula 	return 0;
1537589c3563SJarkko Nikula }
1538589c3563SJarkko Nikula 
15391a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
15401a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
154162ae68faSStephen Warren 				     int order)
154262ae68faSStephen Warren {
154362ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1544f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
154588bd870fSBenoit Cousson 	int i, ret;
154662ae68faSStephen Warren 
154762ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
154861aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
154970090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1550f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
155162ae68faSStephen Warren 		if (ret < 0)
155262ae68faSStephen Warren 			return ret;
155362ae68faSStephen Warren 	}
155462ae68faSStephen Warren 
155588bd870fSBenoit Cousson 	/* probe the CODEC-side components */
155688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
155761aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
155870090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1559f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
156062ae68faSStephen Warren 			if (ret < 0)
156162ae68faSStephen Warren 				return ret;
156262ae68faSStephen Warren 		}
156388bd870fSBenoit Cousson 	}
156462ae68faSStephen Warren 
156562ae68faSStephen Warren 	/* probe the platform */
156670090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1567f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
156862ae68faSStephen Warren 		if (ret < 0)
156962ae68faSStephen Warren 			return ret;
157062ae68faSStephen Warren 	}
157162ae68faSStephen Warren 
157262ae68faSStephen Warren 	return 0;
157362ae68faSStephen Warren }
157462ae68faSStephen Warren 
15758e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1576b0aa88afSMisael Lopez Cruz {
1577b0aa88afSMisael Lopez Cruz 	int ret;
1578b0aa88afSMisael Lopez Cruz 
15798e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
15808e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
15818e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1582b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
15838e2be562SLars-Peter Clausen 				dev_err(dai->dev,
15848e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
15858e2be562SLars-Peter Clausen 					dai->name, ret);
1586b0aa88afSMisael Lopez Cruz 				return ret;
1587b0aa88afSMisael Lopez Cruz 			}
1588b0aa88afSMisael Lopez Cruz 		}
1589b0aa88afSMisael Lopez Cruz 
15908e2be562SLars-Peter Clausen 		dai->probed = 1;
1591b0aa88afSMisael Lopez Cruz 	}
1592b0aa88afSMisael Lopez Cruz 
1593b0aa88afSMisael Lopez Cruz 	return 0;
1594b0aa88afSMisael Lopez Cruz }
1595b0aa88afSMisael Lopez Cruz 
15962436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
15972436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
15983f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
15992436a723SMisael Lopez Cruz {
16003f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16013f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
16023de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
16032436a723SMisael Lopez Cruz 	int ret;
16042436a723SMisael Lopez Cruz 
160588bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
160688bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
160788bd870fSBenoit Cousson 
16082436a723SMisael Lopez Cruz 	/* link the DAI widgets */
16093de7c420SVinod Koul 	sink = codec_dai->playback_widget;
16103de7c420SVinod Koul 	source = cpu_dai->capture_widget;
16113de7c420SVinod Koul 	if (sink && source) {
16122436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16133de7c420SVinod Koul 					   dai_link->num_params,
16143de7c420SVinod Koul 					   source, sink);
16152436a723SMisael Lopez Cruz 		if (ret != 0) {
16162436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16173de7c420SVinod Koul 				sink->name, source->name, ret);
16182436a723SMisael Lopez Cruz 			return ret;
16192436a723SMisael Lopez Cruz 		}
16202436a723SMisael Lopez Cruz 	}
16212436a723SMisael Lopez Cruz 
16223de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
16233de7c420SVinod Koul 	source = codec_dai->capture_widget;
16243de7c420SVinod Koul 	if (sink && source) {
16252436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16263de7c420SVinod Koul 					   dai_link->num_params,
16273de7c420SVinod Koul 					   source, sink);
16282436a723SMisael Lopez Cruz 		if (ret != 0) {
16292436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16303de7c420SVinod Koul 				sink->name, source->name, ret);
16312436a723SMisael Lopez Cruz 			return ret;
16322436a723SMisael Lopez Cruz 		}
16332436a723SMisael Lopez Cruz 	}
16342436a723SMisael Lopez Cruz 
16352436a723SMisael Lopez Cruz 	return 0;
16362436a723SMisael Lopez Cruz }
16372436a723SMisael Lopez Cruz 
16381a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
16391a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1640f0fba2adSLiam Girdwood {
16411a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1642c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
164388bd870fSBenoit Cousson 	int i, ret;
1644f0fba2adSLiam Girdwood 
1645f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
16461a497983SMengdong Lin 			card->name, rtd->num, order);
1647f0fba2adSLiam Girdwood 
1648f0fba2adSLiam Girdwood 	/* set default power off timeout */
1649f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1650f0fba2adSLiam Girdwood 
16518e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
16528e2be562SLars-Peter Clausen 	if (ret)
1653f0fba2adSLiam Girdwood 		return ret;
1654f0fba2adSLiam Girdwood 
1655f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
165688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
16578e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1658b0aa88afSMisael Lopez Cruz 		if (ret)
1659f0fba2adSLiam Girdwood 			return ret;
166088bd870fSBenoit Cousson 	}
1661f0fba2adSLiam Girdwood 
16620168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
16630168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
16640168bf0dSLiam Girdwood 		return 0;
16650168bf0dSLiam Girdwood 
16665f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16675f3484acSLars-Peter Clausen 	if (dai_link->init) {
16685f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
16695f3484acSLars-Peter Clausen 		if (ret < 0) {
16705f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16715f3484acSLars-Peter Clausen 				dai_link->name, ret);
16725f3484acSLars-Peter Clausen 			return ret;
16735f3484acSLars-Peter Clausen 		}
16745f3484acSLars-Peter Clausen 	}
16755f3484acSLars-Peter Clausen 
1676a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1677a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1678a5053a8eSKuninori Morimoto 
16795f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1680589c3563SJarkko Nikula 	if (ret)
1681f0fba2adSLiam Girdwood 		return ret;
1682f0fba2adSLiam Girdwood 
16835f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
16845f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
16852e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
16862e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
16875f3484acSLars-Peter Clausen #endif
16885f3484acSLars-Peter Clausen 
16896f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
16901245b700SNamarta Kohli 		/*create compress_device"*/
16911a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
16921245b700SNamarta Kohli 		if (ret < 0) {
1693f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
16941245b700SNamarta Kohli 					 dai_link->stream_name);
16951245b700SNamarta Kohli 			return ret;
16961245b700SNamarta Kohli 		}
16971245b700SNamarta Kohli 	} else {
16981245b700SNamarta Kohli 
1699c74184edSMark Brown 		if (!dai_link->params) {
1700f0fba2adSLiam Girdwood 			/* create the pcm */
17011a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1702f0fba2adSLiam Girdwood 			if (ret < 0) {
1703f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
17040837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1705f0fba2adSLiam Girdwood 				return ret;
1706f0fba2adSLiam Girdwood 			}
1707c74184edSMark Brown 		} else {
17089d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
17099d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
17109d58a077SRichard Fitzgerald 
1711c74184edSMark Brown 			/* link the DAI widgets */
17123f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
17132436a723SMisael Lopez Cruz 			if (ret)
1714c74184edSMark Brown 				return ret;
1715c74184edSMark Brown 		}
1716c74184edSMark Brown 	}
1717c74184edSMark Brown 
1718f0fba2adSLiam Girdwood 	return 0;
1719f0fba2adSLiam Girdwood }
1720f0fba2adSLiam Girdwood 
172144c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1722b19e6e7bSMark Brown {
17233ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1724f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1725f2ed6b07SMengdong Lin 	const char *name;
1726f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
17273ca041edSSebastian Reichel 
1728f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1729f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1730f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1731f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1732f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1733f2ed6b07SMengdong Lin 		if (!component) {
1734f2ed6b07SMengdong Lin 			if (codec_of_node)
1735f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1736f2ed6b07SMengdong Lin 			goto err_defer;
1737f2ed6b07SMengdong Lin 		}
1738f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1739f2ed6b07SMengdong Lin 		/* generic components */
1740f2ed6b07SMengdong Lin 		name = aux_dev->name;
1741f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1742f2ed6b07SMengdong Lin 		if (!component)
1743f2ed6b07SMengdong Lin 			goto err_defer;
1744f2ed6b07SMengdong Lin 	} else {
1745f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1746f2ed6b07SMengdong Lin 		return -EINVAL;
1747f2ed6b07SMengdong Lin 	}
17483ca041edSSebastian Reichel 
1749f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
17501a653aa4SKuninori Morimoto 	component->auxiliary = 1;
17511a653aa4SKuninori Morimoto 
1752f2ed6b07SMengdong Lin 	return 0;
1753f2ed6b07SMengdong Lin 
1754f2ed6b07SMengdong Lin err_defer:
175565d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1756b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1757b19e6e7bSMark Brown }
1758b19e6e7bSMark Brown 
1759f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1760f2ed6b07SMengdong Lin {
1761f2ed6b07SMengdong Lin 	struct snd_soc_component *comp;
1762f2ed6b07SMengdong Lin 	int order;
1763f2ed6b07SMengdong Lin 	int ret;
1764f2ed6b07SMengdong Lin 
1765f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1766f2ed6b07SMengdong Lin 		order++) {
17671a653aa4SKuninori Morimoto 		list_for_each_entry(comp, &card->component_dev_list, card_list) {
17681a653aa4SKuninori Morimoto 			if (!comp->auxiliary)
17691a653aa4SKuninori Morimoto 				continue;
17701a653aa4SKuninori Morimoto 
1771f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1772f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1773f2ed6b07SMengdong Lin 				if (ret < 0) {
1774f2ed6b07SMengdong Lin 					dev_err(card->dev,
1775f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1776f2ed6b07SMengdong Lin 						comp->name, ret);
1777f2ed6b07SMengdong Lin 					return ret;
1778f2ed6b07SMengdong Lin 				}
1779f2ed6b07SMengdong Lin 			}
1780f2ed6b07SMengdong Lin 		}
1781f2ed6b07SMengdong Lin 	}
178265d9361fSLars-Peter Clausen 
178344c69bb1SLars-Peter Clausen 	return 0;
17843ca041edSSebastian Reichel }
17852eea392dSJarkko Nikula 
1786f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
178744c69bb1SLars-Peter Clausen {
1788f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1789f2ed6b07SMengdong Lin 	int order;
179044c69bb1SLars-Peter Clausen 
1791f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1792f2ed6b07SMengdong Lin 		order++) {
1793f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
17941a653aa4SKuninori Morimoto 			&card->component_dev_list, card_list) {
17951a653aa4SKuninori Morimoto 
17961a653aa4SKuninori Morimoto 			if (!comp->auxiliary)
17971a653aa4SKuninori Morimoto 				continue;
17981a653aa4SKuninori Morimoto 
1799f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1800f2ed6b07SMengdong Lin 				soc_remove_component(comp);
18011a653aa4SKuninori Morimoto 				comp->auxiliary = 0;
18022eea392dSJarkko Nikula 			}
18035f3484acSLars-Peter Clausen 		}
18042eea392dSJarkko Nikula 	}
18052eea392dSJarkko Nikula }
18062eea392dSJarkko Nikula 
1807f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1808fdf0f54dSDimitris Papastamos {
1809fdf0f54dSDimitris Papastamos 	int ret;
1810fdf0f54dSDimitris Papastamos 
1811fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1812fdf0f54dSDimitris Papastamos 		return 0;
1813fdf0f54dSDimitris Papastamos 
1814fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1815fdf0f54dSDimitris Papastamos 	if (ret < 0) {
181610e8aa9aSMichał Mirosław 		dev_err(codec->dev,
181710e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
181810e8aa9aSMichał Mirosław 			ret);
1819fdf0f54dSDimitris Papastamos 		return ret;
1820fdf0f54dSDimitris Papastamos 	}
1821fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1822fdf0f54dSDimitris Papastamos 	return 0;
1823fdf0f54dSDimitris Papastamos }
1824fdf0f54dSDimitris Papastamos 
1825ce64c8b9SLars-Peter Clausen /**
1826ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1827ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1828ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1829ce64c8b9SLars-Peter Clausen  *
1830ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1831ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1832ce64c8b9SLars-Peter Clausen  *
1833ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1834ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1835ce64c8b9SLars-Peter Clausen  *
1836ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1837ce64c8b9SLars-Peter Clausen  */
1838ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1839ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1840ce64c8b9SLars-Peter Clausen {
1841ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1842ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1843ce64c8b9SLars-Peter Clausen 	unsigned int i;
1844ce64c8b9SLars-Peter Clausen 	int ret;
1845ce64c8b9SLars-Peter Clausen 
1846ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1847ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1848ce64c8b9SLars-Peter Clausen 
1849ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1850ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1851ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1852ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1853ce64c8b9SLars-Peter Clausen 			return ret;
1854ce64c8b9SLars-Peter Clausen 		}
1855ce64c8b9SLars-Peter Clausen 	}
1856ce64c8b9SLars-Peter Clausen 
1857ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1858ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1859ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1860ce64c8b9SLars-Peter Clausen 
1861ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1862ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1863ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1864ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1865ce64c8b9SLars-Peter Clausen 			break;
1866ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1867ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1868ce64c8b9SLars-Peter Clausen 			break;
1869ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1870ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1871ce64c8b9SLars-Peter Clausen 			break;
1872ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1873ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1874ce64c8b9SLars-Peter Clausen 			break;
1875ce64c8b9SLars-Peter Clausen 		}
1876ce64c8b9SLars-Peter Clausen 
1877ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1878ce64c8b9SLars-Peter Clausen 	}
1879ce64c8b9SLars-Peter Clausen 
1880ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1881ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1882ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1883ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1884ce64c8b9SLars-Peter Clausen 		return ret;
1885ce64c8b9SLars-Peter Clausen 	}
1886ce64c8b9SLars-Peter Clausen 
1887ce64c8b9SLars-Peter Clausen 	return 0;
1888ce64c8b9SLars-Peter Clausen }
1889ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1890ce64c8b9SLars-Peter Clausen 
1891b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1892f0fba2adSLiam Girdwood {
1893fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
18941a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
189561b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1896ce64c8b9SLars-Peter Clausen 	int ret, i, order;
189796dd3622SMark Brown 
189834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
189901b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1900f0fba2adSLiam Girdwood 
1901b19e6e7bSMark Brown 	/* bind DAIs */
1902b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
19036f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
1904b19e6e7bSMark Brown 		if (ret != 0)
1905b19e6e7bSMark Brown 			goto base_error;
1906db2a4165SFrank Mandarino 	}
1907db2a4165SFrank Mandarino 
190844c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1909b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
191044c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1911b19e6e7bSMark Brown 		if (ret != 0)
1912b19e6e7bSMark Brown 			goto base_error;
1913db2a4165SFrank Mandarino 	}
1914db2a4165SFrank Mandarino 
1915f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1916f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
1917f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
1918f8f80361SMengdong Lin 
1919fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1920fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1921fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1922fdf0f54dSDimitris Papastamos 			continue;
1923f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1924b19e6e7bSMark Brown 		if (ret < 0)
1925b19e6e7bSMark Brown 			goto base_error;
1926fdf0f54dSDimitris Papastamos 	}
1927fdf0f54dSDimitris Papastamos 
1928f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1929102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1930f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1931f0fba2adSLiam Girdwood 	if (ret < 0) {
193210e8aa9aSMichał Mirosław 		dev_err(card->dev,
193310e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
193410e8aa9aSMichał Mirosław 			card->name, ret);
1935b19e6e7bSMark Brown 		goto base_error;
1936db2a4165SFrank Mandarino 	}
1937db2a4165SFrank Mandarino 
19380757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19390757d834SLars-Peter Clausen 
1940e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1941e37a4970SMark Brown 	card->dapm.dev = card->dev;
1942e37a4970SMark Brown 	card->dapm.card = card;
1943e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1944e37a4970SMark Brown 
1945d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1946d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1947d5d1e0beSLars-Peter Clausen #endif
1948d5d1e0beSLars-Peter Clausen 
194988ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
19506ed25978SAndy Green 	/* deferred resume work */
19516308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
19521301a964SRandy Dunlap #endif
19536ed25978SAndy Green 
19549a841ebbSMark Brown 	if (card->dapm_widgets)
19559a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19569a841ebbSMark Brown 					  card->num_dapm_widgets);
19579a841ebbSMark Brown 
1958f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
1959f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1960f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
1961f23e860eSNicolin Chen 
1962f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1963f0fba2adSLiam Girdwood 	if (card->probe) {
1964e7361ec4SMark Brown 		ret = card->probe(card);
1965f0fba2adSLiam Girdwood 		if (ret < 0)
1966f0fba2adSLiam Girdwood 			goto card_probe_error;
1967f0fba2adSLiam Girdwood 	}
1968f0fba2adSLiam Girdwood 
196962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
19700168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
19710168bf0dSLiam Girdwood 			order++) {
19721a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
19731a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
197462ae68faSStephen Warren 			if (ret < 0) {
1975f110bfc7SLiam Girdwood 				dev_err(card->dev,
1976f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1977f110bfc7SLiam Girdwood 					ret);
197862ae68faSStephen Warren 				goto probe_dai_err;
197962ae68faSStephen Warren 			}
198062ae68faSStephen Warren 		}
198162ae68faSStephen Warren 	}
198262ae68faSStephen Warren 
1983f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1984f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
1985f2ed6b07SMengdong Lin 	if (ret < 0)
1986f2ed6b07SMengdong Lin 		goto probe_dai_err;
1987f2ed6b07SMengdong Lin 
198861b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
198961b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
199061b0088bSMengdong Lin 	 */
199161b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
199261b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
199361b0088bSMengdong Lin 			continue;
199461b0088bSMengdong Lin 
199561b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
199661b0088bSMengdong Lin 		if (ret)
199761b0088bSMengdong Lin 			goto probe_dai_err;
199861b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
199961b0088bSMengdong Lin 		if (ret)
200061b0088bSMengdong Lin 			goto probe_dai_err;
200161b0088bSMengdong Lin 	}
200261b0088bSMengdong Lin 
200362ae68faSStephen Warren 	/* probe all DAI links on this card */
200462ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
200562ae68faSStephen Warren 			order++) {
20061a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
20071a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2008fe3e78e0SMark Brown 			if (ret < 0) {
2009f110bfc7SLiam Girdwood 				dev_err(card->dev,
2010f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2011f110bfc7SLiam Girdwood 					ret);
2012f0fba2adSLiam Girdwood 				goto probe_dai_err;
2013fe3e78e0SMark Brown 			}
2014fe3e78e0SMark Brown 		}
20150168bf0dSLiam Girdwood 	}
2016fe3e78e0SMark Brown 
2017888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2018b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2019888df395SMark Brown 
2020b7af1dafSMark Brown 	if (card->controls)
2021022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
2022b7af1dafSMark Brown 
2023b8ad29deSMark Brown 	if (card->dapm_routes)
2024b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2025b8ad29deSMark Brown 					card->num_dapm_routes);
2026b8ad29deSMark Brown 
2027f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2028f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2029f23e860eSNicolin Chen 					card->num_of_dapm_routes);
203075d9ac46SMark Brown 
2031f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2032fe3e78e0SMark Brown 		 "%s", card->name);
2033f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
203422de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2035f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2036f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2037f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2038f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2039f0e8ed85SMark Brown 		case '_':
2040f0e8ed85SMark Brown 		case '-':
2041f0e8ed85SMark Brown 		case '\0':
2042f0e8ed85SMark Brown 			break;
2043f0e8ed85SMark Brown 		default:
2044f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2045f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2046f0e8ed85SMark Brown 			break;
2047f0e8ed85SMark Brown 		}
2048f0e8ed85SMark Brown 	}
2049fe3e78e0SMark Brown 
205028e9ad92SMark Brown 	if (card->late_probe) {
205128e9ad92SMark Brown 		ret = card->late_probe(card);
205228e9ad92SMark Brown 		if (ret < 0) {
2053f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
205428e9ad92SMark Brown 				card->name, ret);
205528e9ad92SMark Brown 			goto probe_aux_dev_err;
205628e9ad92SMark Brown 		}
205728e9ad92SMark Brown 	}
205828e9ad92SMark Brown 
2059824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20608c193b8dSLars-Peter Clausen 
2061f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2062fe3e78e0SMark Brown 	if (ret < 0) {
2063f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2064f110bfc7SLiam Girdwood 				ret);
20656b3ed785SAxel Lin 		goto probe_aux_dev_err;
2066fe3e78e0SMark Brown 	}
2067fe3e78e0SMark Brown 
2068435c5e25SMark Brown 	card->instantiated = 1;
20694f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2070f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
207134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2072b19e6e7bSMark Brown 
2073b19e6e7bSMark Brown 	return 0;
2074db2a4165SFrank Mandarino 
20752eea392dSJarkko Nikula probe_aux_dev_err:
2076f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
20772eea392dSJarkko Nikula 
2078f0fba2adSLiam Girdwood probe_dai_err:
20790671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2080fe3e78e0SMark Brown 
2081f0fba2adSLiam Girdwood card_probe_error:
208287506549SMark Brown 	if (card->remove)
2083e7361ec4SMark Brown 		card->remove(card);
2084f0fba2adSLiam Girdwood 
20852210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
20860757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2087f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2088f0fba2adSLiam Girdwood 
2089b19e6e7bSMark Brown base_error:
20901a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2091f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
209234e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2093db2a4165SFrank Mandarino 
2094b19e6e7bSMark Brown 	return ret;
2095435c5e25SMark Brown }
2096435c5e25SMark Brown 
2097435c5e25SMark Brown /* probes a new socdev */
2098435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2099435c5e25SMark Brown {
2100f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2101435c5e25SMark Brown 
210270a7ca34SVinod Koul 	/*
210370a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
210470a7ca34SVinod Koul 	 * we should not be here in that case so ret error
210570a7ca34SVinod Koul 	 */
210670a7ca34SVinod Koul 	if (!card)
210770a7ca34SVinod Koul 		return -EINVAL;
210870a7ca34SVinod Koul 
2109fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2110f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2111fe4085e8SMark Brown 		 card->name);
2112fe4085e8SMark Brown 
2113435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2114435c5e25SMark Brown 	card->dev = &pdev->dev;
2115f0fba2adSLiam Girdwood 
211628d528c8SMark Brown 	return snd_soc_register_card(card);
2117435c5e25SMark Brown }
2118435c5e25SMark Brown 
2119b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2120db2a4165SFrank Mandarino {
21211a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2122db2a4165SFrank Mandarino 
2123f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
21241a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
212543829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2126db2a4165SFrank Mandarino 
2127f0fba2adSLiam Girdwood 	/* remove and free each DAI */
21280671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
21291a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2130f0fba2adSLiam Girdwood 
2131f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2132f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2133f2ed6b07SMengdong Lin 
2134d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2135a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2136a6052154SJarkko Nikula 
2137f0fba2adSLiam Girdwood 	/* remove the card */
213887506549SMark Brown 	if (card->remove)
2139e7361ec4SMark Brown 		card->remove(card);
2140f0fba2adSLiam Girdwood 
2141f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2142b0e26485SVinod Koul 	return 0;
2143b0e26485SVinod Koul 
2144b2dfa62cSGuennadi Liakhovetski }
2145b0e26485SVinod Koul 
2146b0e26485SVinod Koul /* removes a socdev */
2147b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2148b0e26485SVinod Koul {
2149b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2150b0e26485SVinod Koul 
2151c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2152db2a4165SFrank Mandarino 	return 0;
2153db2a4165SFrank Mandarino }
2154db2a4165SFrank Mandarino 
21556f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
215651737470SMark Brown {
21576f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21581a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
215951737470SMark Brown 
216051737470SMark Brown 	if (!card->instantiated)
2161416356fcSMark Brown 		return 0;
216251737470SMark Brown 
216351737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
216451737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
21651a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
216643829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
216751737470SMark Brown 
2168f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2169416356fcSMark Brown 
2170988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
21711a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
217288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21731a497983SMengdong Lin 		int i;
217488bd870fSBenoit Cousson 
2175988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21761a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
21771a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
217888bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
217988bd870fSBenoit Cousson 		}
2180988e8cc4SNicolin Chen 	}
2181988e8cc4SNicolin Chen 
2182416356fcSMark Brown 	return 0;
218351737470SMark Brown }
21846f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
218551737470SMark Brown 
21866f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2187b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2188b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2189b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2190b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21916f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2192b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2193416356fcSMark Brown };
2194deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2195416356fcSMark Brown 
2196db2a4165SFrank Mandarino /* ASoC platform driver */
2197db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2198db2a4165SFrank Mandarino 	.driver		= {
2199db2a4165SFrank Mandarino 		.name		= "soc-audio",
22006f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2201db2a4165SFrank Mandarino 	},
2202db2a4165SFrank Mandarino 	.probe		= soc_probe,
2203db2a4165SFrank Mandarino 	.remove		= soc_remove,
2204db2a4165SFrank Mandarino };
2205db2a4165SFrank Mandarino 
2206096e49d5SMark Brown /**
2207db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2208db2a4165SFrank Mandarino  * @_template: control template
2209db2a4165SFrank Mandarino  * @data: control private data
2210ac11a2b3SMark Brown  * @long_name: control long name
2211efb7ac3fSMark Brown  * @prefix: control name prefix
2212db2a4165SFrank Mandarino  *
2213db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2214db2a4165SFrank Mandarino  *
2215db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2216db2a4165SFrank Mandarino  */
2217db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22183056557fSMark Brown 				  void *data, const char *long_name,
2219efb7ac3fSMark Brown 				  const char *prefix)
2220db2a4165SFrank Mandarino {
2221db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2222efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2223efb7ac3fSMark Brown 	char *name = NULL;
2224db2a4165SFrank Mandarino 
2225db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2226db2a4165SFrank Mandarino 	template.index = 0;
2227db2a4165SFrank Mandarino 
2228efb7ac3fSMark Brown 	if (!long_name)
2229efb7ac3fSMark Brown 		long_name = template.name;
2230efb7ac3fSMark Brown 
2231efb7ac3fSMark Brown 	if (prefix) {
22322b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2233efb7ac3fSMark Brown 		if (!name)
2234efb7ac3fSMark Brown 			return NULL;
2235efb7ac3fSMark Brown 
2236efb7ac3fSMark Brown 		template.name = name;
2237efb7ac3fSMark Brown 	} else {
2238efb7ac3fSMark Brown 		template.name = long_name;
2239efb7ac3fSMark Brown 	}
2240efb7ac3fSMark Brown 
2241efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2242efb7ac3fSMark Brown 
2243efb7ac3fSMark Brown 	kfree(name);
2244efb7ac3fSMark Brown 
2245efb7ac3fSMark Brown 	return kcontrol;
2246db2a4165SFrank Mandarino }
2247db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2248db2a4165SFrank Mandarino 
2249022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2250022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2251022658beSLiam Girdwood 	const char *prefix, void *data)
2252022658beSLiam Girdwood {
2253022658beSLiam Girdwood 	int err, i;
2254022658beSLiam Girdwood 
2255022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2256022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2257022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2258022658beSLiam Girdwood 						     control->name, prefix));
2259022658beSLiam Girdwood 		if (err < 0) {
2260f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2261f110bfc7SLiam Girdwood 				control->name, err);
2262022658beSLiam Girdwood 			return err;
2263022658beSLiam Girdwood 		}
2264022658beSLiam Girdwood 	}
2265022658beSLiam Girdwood 
2266022658beSLiam Girdwood 	return 0;
2267022658beSLiam Girdwood }
2268022658beSLiam Girdwood 
22694fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22704fefd698SDimitris Papastamos 					       const char *name)
22714fefd698SDimitris Papastamos {
22724fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22734fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22744fefd698SDimitris Papastamos 
22754fefd698SDimitris Papastamos 	if (unlikely(!name))
22764fefd698SDimitris Papastamos 		return NULL;
22774fefd698SDimitris Papastamos 
22784fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22794fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22804fefd698SDimitris Papastamos 			return kctl;
22814fefd698SDimitris Papastamos 	return NULL;
22824fefd698SDimitris Papastamos }
22834fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22844fefd698SDimitris Papastamos 
2285db2a4165SFrank Mandarino /**
22860f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22870f2780adSLars-Peter Clausen  *
22880f2780adSLars-Peter Clausen  * @component: Component to add controls to
22890f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22900f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22910f2780adSLars-Peter Clausen  *
22920f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22930f2780adSLars-Peter Clausen  */
22940f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22950f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22960f2780adSLars-Peter Clausen {
22970f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22980f2780adSLars-Peter Clausen 
22990f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23000f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23010f2780adSLars-Peter Clausen }
23020f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23030f2780adSLars-Peter Clausen 
23040f2780adSLars-Peter Clausen /**
2305022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2306022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
23073e8e1952SIan Molton  * duplicating this code.
23083e8e1952SIan Molton  *
23093e8e1952SIan Molton  * @codec: codec to add controls to
23103e8e1952SIan Molton  * @controls: array of controls to add
23113e8e1952SIan Molton  * @num_controls: number of elements in the array
23123e8e1952SIan Molton  *
23133e8e1952SIan Molton  * Return 0 for success, else error.
23143e8e1952SIan Molton  */
2315022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
23160f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23173e8e1952SIan Molton {
23180f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
23190f2780adSLars-Peter Clausen 		num_controls);
23203e8e1952SIan Molton }
2321022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
23223e8e1952SIan Molton 
23233e8e1952SIan Molton /**
2324a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2325022658beSLiam Girdwood  * Convenience function to add a list of controls.
2326a491a5c8SLiam Girdwood  *
2327a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2328a491a5c8SLiam Girdwood  * @controls: array of controls to add
2329a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2330a491a5c8SLiam Girdwood  *
2331a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2332a491a5c8SLiam Girdwood  */
2333a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
23340f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2335a491a5c8SLiam Girdwood {
23360f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
23370f2780adSLars-Peter Clausen 		num_controls);
2338a491a5c8SLiam Girdwood }
2339a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2340a491a5c8SLiam Girdwood 
2341a491a5c8SLiam Girdwood /**
2342022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2343022658beSLiam Girdwood  * Convenience function to add a list of controls.
2344022658beSLiam Girdwood  *
2345022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2346022658beSLiam Girdwood  * @controls: array of controls to add
2347022658beSLiam Girdwood  * @num_controls: number of elements in the array
2348022658beSLiam Girdwood  *
2349022658beSLiam Girdwood  * Return 0 for success, else error.
2350022658beSLiam Girdwood  */
2351022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2352022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2353022658beSLiam Girdwood {
2354022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2355022658beSLiam Girdwood 
2356022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2357022658beSLiam Girdwood 			NULL, soc_card);
2358022658beSLiam Girdwood }
2359022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2360022658beSLiam Girdwood 
2361022658beSLiam Girdwood /**
2362022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2363022658beSLiam Girdwood  * Convienience function to add a list of controls.
2364022658beSLiam Girdwood  *
2365022658beSLiam Girdwood  * @dai: DAI to add controls to
2366022658beSLiam Girdwood  * @controls: array of controls to add
2367022658beSLiam Girdwood  * @num_controls: number of elements in the array
2368022658beSLiam Girdwood  *
2369022658beSLiam Girdwood  * Return 0 for success, else error.
2370022658beSLiam Girdwood  */
2371022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2372022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2373022658beSLiam Girdwood {
2374313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2375022658beSLiam Girdwood 
2376022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2377022658beSLiam Girdwood 			NULL, dai);
2378022658beSLiam Girdwood }
2379022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2380022658beSLiam Girdwood 
2381022658beSLiam Girdwood /**
23828c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23838c6529dbSLiam Girdwood  * @dai: DAI
23848c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23858c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23868c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23878c6529dbSLiam Girdwood  *
23888c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
23898c6529dbSLiam Girdwood  */
23908c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
23918c6529dbSLiam Girdwood 	unsigned int freq, int dir)
23928c6529dbSLiam Girdwood {
2393f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2394f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2395ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2396da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2397ec4ee52aSMark Brown 						      freq, dir);
23988c6529dbSLiam Girdwood 	else
23991104a9c8SMark Brown 		return -ENOTSUPP;
24008c6529dbSLiam Girdwood }
24018c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
24028c6529dbSLiam Girdwood 
24038c6529dbSLiam Girdwood /**
2404ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2405ec4ee52aSMark Brown  * @codec: CODEC
2406ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2407da1c6ea6SMark Brown  * @source: Source for the clock
2408ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2409ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2410ec4ee52aSMark Brown  *
2411ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2412ec4ee52aSMark Brown  */
2413ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2414da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2415ec4ee52aSMark Brown {
2416ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2417da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2418da1c6ea6SMark Brown 						 freq, dir);
2419ec4ee52aSMark Brown 	else
24201104a9c8SMark Brown 		return -ENOTSUPP;
2421ec4ee52aSMark Brown }
2422ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2423ec4ee52aSMark Brown 
2424ec4ee52aSMark Brown /**
24258c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
24268c6529dbSLiam Girdwood  * @dai: DAI
2427ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
24288c6529dbSLiam Girdwood  * @div: new clock divisor.
24298c6529dbSLiam Girdwood  *
24308c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
24318c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
24328c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
24338c6529dbSLiam Girdwood  */
24348c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
24358c6529dbSLiam Girdwood 	int div_id, int div)
24368c6529dbSLiam Girdwood {
2437f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2438f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
24398c6529dbSLiam Girdwood 	else
24408c6529dbSLiam Girdwood 		return -EINVAL;
24418c6529dbSLiam Girdwood }
24428c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
24438c6529dbSLiam Girdwood 
24448c6529dbSLiam Girdwood /**
24458c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
24468c6529dbSLiam Girdwood  * @dai: DAI
24478c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
244885488037SMark Brown  * @source: DAI specific source for the PLL
24498c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24508c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24518c6529dbSLiam Girdwood  *
24528c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
24538c6529dbSLiam Girdwood  */
245485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
245585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
24568c6529dbSLiam Girdwood {
2457f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2458f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
245985488037SMark Brown 					 freq_in, freq_out);
2460ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2461ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2462ec4ee52aSMark Brown 						   freq_in, freq_out);
24638c6529dbSLiam Girdwood 	else
24648c6529dbSLiam Girdwood 		return -EINVAL;
24658c6529dbSLiam Girdwood }
24668c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24678c6529dbSLiam Girdwood 
2468ec4ee52aSMark Brown /*
2469ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2470ec4ee52aSMark Brown  * @codec: CODEC
2471ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2472ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2473ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2474ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2475ec4ee52aSMark Brown  *
2476ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2477ec4ee52aSMark Brown  */
2478ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2479ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2480ec4ee52aSMark Brown {
2481ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2482ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2483ec4ee52aSMark Brown 					      freq_in, freq_out);
2484ec4ee52aSMark Brown 	else
2485ec4ee52aSMark Brown 		return -EINVAL;
2486ec4ee52aSMark Brown }
2487ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2488ec4ee52aSMark Brown 
24898c6529dbSLiam Girdwood /**
2490e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2491e54cf76bSLiam Girdwood  * @dai: DAI
2492231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2493e54cf76bSLiam Girdwood  *
2494e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2495e54cf76bSLiam Girdwood  */
2496e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2497e54cf76bSLiam Girdwood {
2498e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2499e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2500e54cf76bSLiam Girdwood 	else
2501e54cf76bSLiam Girdwood 		return -EINVAL;
2502e54cf76bSLiam Girdwood }
2503e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2504e54cf76bSLiam Girdwood 
2505e54cf76bSLiam Girdwood /**
25068c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
25078c6529dbSLiam Girdwood  * @dai: DAI
25088c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
25098c6529dbSLiam Girdwood  *
25108c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
25118c6529dbSLiam Girdwood  */
25128c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
25138c6529dbSLiam Girdwood {
25145e4ba569SShawn Guo 	if (dai->driver == NULL)
25158c6529dbSLiam Girdwood 		return -EINVAL;
25165e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
25175e4ba569SShawn Guo 		return -ENOTSUPP;
25185e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
25198c6529dbSLiam Girdwood }
25208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
25218c6529dbSLiam Girdwood 
25228c6529dbSLiam Girdwood /**
2523e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
252489c67857SXiubo Li  * @slots: Number of slots in use.
252589c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
252689c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
252789c67857SXiubo Li  *
252889c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
252989c67857SXiubo Li  */
2530e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
253189c67857SXiubo Li 					  unsigned int *tx_mask,
253289c67857SXiubo Li 					  unsigned int *rx_mask)
253389c67857SXiubo Li {
253489c67857SXiubo Li 	if (*tx_mask || *rx_mask)
253589c67857SXiubo Li 		return 0;
253689c67857SXiubo Li 
253789c67857SXiubo Li 	if (!slots)
253889c67857SXiubo Li 		return -EINVAL;
253989c67857SXiubo Li 
254089c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
254189c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
254289c67857SXiubo Li 
254389c67857SXiubo Li 	return 0;
254489c67857SXiubo Li }
254589c67857SXiubo Li 
254689c67857SXiubo Li /**
2547e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2548e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2549a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2550a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25518c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2552a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25538c6529dbSLiam Girdwood  *
2554e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2555e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2556e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2557e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2558e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2559e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2560e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2561e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2562e46c9366SLars-Peter Clausen  * channel and so on.
2563e46c9366SLars-Peter Clausen  *
2564e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2565e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2566e46c9366SLars-Peter Clausen  *
2567e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
25688c6529dbSLiam Girdwood  */
25698c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2570a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25718c6529dbSLiam Girdwood {
2572e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2573e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
257489c67857SXiubo Li 						&tx_mask, &rx_mask);
257589c67857SXiubo Li 	else
2576e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
257789c67857SXiubo Li 
257888bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
257988bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
258088bd870fSBenoit Cousson 
2581f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2582f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2583a5479e38SDaniel Ribeiro 				slots, slot_width);
25848c6529dbSLiam Girdwood 	else
2585b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25868c6529dbSLiam Girdwood }
25878c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25888c6529dbSLiam Girdwood 
25898c6529dbSLiam Girdwood /**
2590472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2591472df3cbSBarry Song  * @dai: DAI
2592472df3cbSBarry Song  * @tx_num: how many TX channels
2593472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2594472df3cbSBarry Song  *           0~num-1 uses
2595472df3cbSBarry Song  * @rx_num: how many RX channels
2596472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2597472df3cbSBarry Song  *           0~num-1 uses
2598472df3cbSBarry Song  *
2599472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2600472df3cbSBarry Song  */
2601472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2602472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2603472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2604472df3cbSBarry Song {
2605f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2606f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2607472df3cbSBarry Song 			rx_num, rx_slot);
2608472df3cbSBarry Song 	else
2609472df3cbSBarry Song 		return -EINVAL;
2610472df3cbSBarry Song }
2611472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2612472df3cbSBarry Song 
2613472df3cbSBarry Song /**
26148c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
26158c6529dbSLiam Girdwood  * @dai: DAI
26168c6529dbSLiam Girdwood  * @tristate: tristate enable
26178c6529dbSLiam Girdwood  *
26188c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
26198c6529dbSLiam Girdwood  */
26208c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
26218c6529dbSLiam Girdwood {
2622f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2623f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
26248c6529dbSLiam Girdwood 	else
26258c6529dbSLiam Girdwood 		return -EINVAL;
26268c6529dbSLiam Girdwood }
26278c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
26288c6529dbSLiam Girdwood 
26298c6529dbSLiam Girdwood /**
26308c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
26318c6529dbSLiam Girdwood  * @dai: DAI
26328c6529dbSLiam Girdwood  * @mute: mute enable
2633da18396fSMark Brown  * @direction: stream to mute
26348c6529dbSLiam Girdwood  *
26358c6529dbSLiam Girdwood  * Mutes the DAI DAC.
26368c6529dbSLiam Girdwood  */
2637da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2638da18396fSMark Brown 			     int direction)
26398c6529dbSLiam Girdwood {
2640da18396fSMark Brown 	if (!dai->driver)
2641da18396fSMark Brown 		return -ENOTSUPP;
2642da18396fSMark Brown 
2643da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2644da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2645da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2646da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2647f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
26488c6529dbSLiam Girdwood 	else
264904570c62SMark Brown 		return -ENOTSUPP;
26508c6529dbSLiam Girdwood }
26518c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26528c6529dbSLiam Girdwood 
2653c5af3a2eSMark Brown /**
2654c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2655c5af3a2eSMark Brown  *
2656ac11a2b3SMark Brown  * @card: Card to register
2657c5af3a2eSMark Brown  *
2658c5af3a2eSMark Brown  */
265970a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2660c5af3a2eSMark Brown {
2661923c5e61SMengdong Lin 	int i, ret;
26621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2663f0fba2adSLiam Girdwood 
2664c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2665c5af3a2eSMark Brown 		return -EINVAL;
2666c5af3a2eSMark Brown 
26675a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
26685a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
26695a504963SStephen Warren 
2670923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
267188bd870fSBenoit Cousson 		if (ret) {
2672923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2673923c5e61SMengdong Lin 				link->name);
267488bd870fSBenoit Cousson 			return ret;
267588bd870fSBenoit Cousson 		}
26765a504963SStephen Warren 	}
26775a504963SStephen Warren 
2678ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2679ed77cc12SMark Brown 
2680111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2681111c6419SStephen Warren 
2682f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2683f8f80361SMengdong Lin 	card->num_dai_links = 0;
2684f0fba2adSLiam Girdwood 
26851a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
26869115171aSMark Brown 	card->num_rtd = 0;
2687db2a4165SFrank Mandarino 
2688db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
26898a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2690db2a4165SFrank Mandarino 	card->instantiated = 0;
2691db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2692a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2693db2a4165SFrank Mandarino 
2694b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2695b19e6e7bSMark Brown 	if (ret != 0)
26964e2576bdSKuninori Morimoto 		return ret;
2697db2a4165SFrank Mandarino 
2698988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
26991a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
270088bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
270188bd870fSBenoit Cousson 		int j;
270288bd870fSBenoit Cousson 
270388bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
270488bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2705988e8cc4SNicolin Chen 			if (!codec_dai->active)
2706988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
270788bd870fSBenoit Cousson 		}
270888bd870fSBenoit Cousson 
2709988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2710988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2711988e8cc4SNicolin Chen 	}
2712988e8cc4SNicolin Chen 
2713b19e6e7bSMark Brown 	return ret;
2714db2a4165SFrank Mandarino }
271570a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2716db2a4165SFrank Mandarino 
2717db2a4165SFrank Mandarino /**
2718db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2719db2a4165SFrank Mandarino  *
2720db2a4165SFrank Mandarino  * @card: Card to unregister
2721db2a4165SFrank Mandarino  *
2722db2a4165SFrank Mandarino  */
272370a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2724db2a4165SFrank Mandarino {
272501e0df66SLars-Peter Clausen 	if (card->instantiated) {
272601e0df66SLars-Peter Clausen 		card->instantiated = false;
27271c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2728b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2729f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
27308f6f9b29SKuninori Morimoto 	}
2731db2a4165SFrank Mandarino 
2732db2a4165SFrank Mandarino 	return 0;
2733db2a4165SFrank Mandarino }
273470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2735db2a4165SFrank Mandarino 
2736db2a4165SFrank Mandarino /*
2737db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2738db2a4165SFrank Mandarino  * and sanitizing names.
2739db2a4165SFrank Mandarino  */
27400b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2741db2a4165SFrank Mandarino {
2742db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2743db2a4165SFrank Mandarino 	int id1, id2;
2744db2a4165SFrank Mandarino 
2745db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2746db2a4165SFrank Mandarino 		return NULL;
2747db2a4165SFrank Mandarino 
274858818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2749db2a4165SFrank Mandarino 
2750db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2751c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2752c5af3a2eSMark Brown 	if (found) {
2753c5af3a2eSMark Brown 		/* get ID */
2754c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2755c5af3a2eSMark Brown 
2756c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2757c5af3a2eSMark Brown 			if (*id == -1)
2758c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2759c5af3a2eSMark Brown 		}
2760c5af3a2eSMark Brown 
2761c5af3a2eSMark Brown 	} else {
2762c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2763c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2764c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2765c5af3a2eSMark Brown 
2766c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2767c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2768c5af3a2eSMark Brown 
2769c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2770c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
277158818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2772c5af3a2eSMark Brown 		} else
2773c5af3a2eSMark Brown 			*id = 0;
2774c5af3a2eSMark Brown 	}
2775c5af3a2eSMark Brown 
2776c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2777c5af3a2eSMark Brown }
2778c5af3a2eSMark Brown 
2779c5af3a2eSMark Brown /*
2780c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2781c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2782c5af3a2eSMark Brown  */
2783c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2784c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2785c5af3a2eSMark Brown {
2786c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
278710e8aa9aSMichał Mirosław 		dev_err(dev,
278810e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
278910e8aa9aSMichał Mirosław 			dev_name(dev));
2790c5af3a2eSMark Brown 		return NULL;
2791c5af3a2eSMark Brown 	}
2792c5af3a2eSMark Brown 
2793c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2794c5af3a2eSMark Brown }
2795c5af3a2eSMark Brown 
2796c5af3a2eSMark Brown /**
279732c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
27989115171aSMark Brown  *
279932c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
28009115171aSMark Brown  */
280132c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
28029115171aSMark Brown {
28035c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
28049115171aSMark Brown 
28055c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
280632c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
280732c9ba54SLars-Peter Clausen 			dai->name);
28089115171aSMark Brown 		list_del(&dai->list);
2809f0fba2adSLiam Girdwood 		kfree(dai->name);
2810f0fba2adSLiam Girdwood 		kfree(dai);
28119115171aSMark Brown 	}
281232c9ba54SLars-Peter Clausen }
28139115171aSMark Brown 
28145e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
28155e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
28165e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
28175e4fb372SMengdong Lin 	bool legacy_dai_naming)
28185e4fb372SMengdong Lin {
28195e4fb372SMengdong Lin 	struct device *dev = component->dev;
28205e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
28215e4fb372SMengdong Lin 
28225e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
28235e4fb372SMengdong Lin 
28245e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
28255e4fb372SMengdong Lin 	if (dai == NULL)
28265e4fb372SMengdong Lin 		return NULL;
28275e4fb372SMengdong Lin 
28285e4fb372SMengdong Lin 	/*
28295e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
28305e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
28315e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
28325e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
28335e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
28345e4fb372SMengdong Lin 	 * component-less anymore.
28355e4fb372SMengdong Lin 	 */
28365e4fb372SMengdong Lin 	if (legacy_dai_naming &&
28375e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
28385e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
28395e4fb372SMengdong Lin 	} else {
28405e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
28415e4fb372SMengdong Lin 		if (dai_drv->id)
28425e4fb372SMengdong Lin 			dai->id = dai_drv->id;
28435e4fb372SMengdong Lin 		else
28445e4fb372SMengdong Lin 			dai->id = component->num_dai;
28455e4fb372SMengdong Lin 	}
28465e4fb372SMengdong Lin 	if (dai->name == NULL) {
28475e4fb372SMengdong Lin 		kfree(dai);
28485e4fb372SMengdong Lin 		return NULL;
28495e4fb372SMengdong Lin 	}
28505e4fb372SMengdong Lin 
28515e4fb372SMengdong Lin 	dai->component = component;
28525e4fb372SMengdong Lin 	dai->dev = dev;
28535e4fb372SMengdong Lin 	dai->driver = dai_drv;
28545e4fb372SMengdong Lin 	if (!dai->driver->ops)
28555e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
28565e4fb372SMengdong Lin 
28575e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
28585e4fb372SMengdong Lin 	component->num_dai++;
28595e4fb372SMengdong Lin 
28605e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
28615e4fb372SMengdong Lin 	return dai;
28625e4fb372SMengdong Lin }
28635e4fb372SMengdong Lin 
28649115171aSMark Brown /**
286532c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
28669115171aSMark Brown  *
28676106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
28686106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2869ac11a2b3SMark Brown  * @count: Number of DAIs
287032c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
287132c9ba54SLars-Peter Clausen  *                     parent's name.
28729115171aSMark Brown  */
287332c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2874bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2875bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
28769115171aSMark Brown {
28776106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2878f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
287932c9ba54SLars-Peter Clausen 	unsigned int i;
288032c9ba54SLars-Peter Clausen 	int ret;
2881f0fba2adSLiam Girdwood 
2882f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
28839115171aSMark Brown 
2884bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2885bb13109dSLars-Peter Clausen 
28869115171aSMark Brown 	for (i = 0; i < count; i++) {
2887f0fba2adSLiam Girdwood 
28885e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
28895e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
2890c46e0079SAxel Lin 		if (dai == NULL) {
2891c46e0079SAxel Lin 			ret = -ENOMEM;
2892c46e0079SAxel Lin 			goto err;
2893c46e0079SAxel Lin 		}
2894f0fba2adSLiam Girdwood 	}
2895f0fba2adSLiam Girdwood 
28969115171aSMark Brown 	return 0;
28979115171aSMark Brown 
28989115171aSMark Brown err:
289932c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
29009115171aSMark Brown 
29019115171aSMark Brown 	return ret;
29029115171aSMark Brown }
29039115171aSMark Brown 
290468003e6cSMengdong Lin /**
290568003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
290668003e6cSMengdong Lin  *
290768003e6cSMengdong Lin  * @component: The component the DAIs are registered for
290868003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
290968003e6cSMengdong Lin  *
291068003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
291168003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
291268003e6cSMengdong Lin  * will be freed in the component cleanup.
291368003e6cSMengdong Lin  */
291468003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
291568003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
291668003e6cSMengdong Lin {
291768003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
291868003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
291968003e6cSMengdong Lin 	struct snd_soc_dai *dai;
292068003e6cSMengdong Lin 	int ret;
292168003e6cSMengdong Lin 
292268003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
292368003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
292468003e6cSMengdong Lin 			dai_drv->dobj.type);
292568003e6cSMengdong Lin 		return -EINVAL;
292668003e6cSMengdong Lin 	}
292768003e6cSMengdong Lin 
292868003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
292968003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
293068003e6cSMengdong Lin 	if (!dai)
293168003e6cSMengdong Lin 		return -ENOMEM;
293268003e6cSMengdong Lin 
293368003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
293468003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
293568003e6cSMengdong Lin 	 */
293668003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
293768003e6cSMengdong Lin 	if (ret != 0) {
293868003e6cSMengdong Lin 		dev_err(component->dev,
293968003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
294068003e6cSMengdong Lin 	}
294168003e6cSMengdong Lin 
294268003e6cSMengdong Lin 	return ret;
294368003e6cSMengdong Lin }
294468003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
294568003e6cSMengdong Lin 
294614e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
294714e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2948d191bd8dSKuninori Morimoto {
294914e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2950d191bd8dSKuninori Morimoto 
295114e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
295214e8bdebSLars-Peter Clausen }
2953d191bd8dSKuninori Morimoto 
295414e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
295514e8bdebSLars-Peter Clausen 	int event)
295614e8bdebSLars-Peter Clausen {
295714e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
295814e8bdebSLars-Peter Clausen 
295914e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
296014e8bdebSLars-Peter Clausen }
296114e8bdebSLars-Peter Clausen 
2962bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2963bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2964d191bd8dSKuninori Morimoto {
2965ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2966ce0fc93aSLars-Peter Clausen 
2967bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2968bb13109dSLars-Peter Clausen 	if (!component->name) {
2969bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2970d191bd8dSKuninori Morimoto 		return -ENOMEM;
2971d191bd8dSKuninori Morimoto 	}
2972d191bd8dSKuninori Morimoto 
2973bb13109dSLars-Peter Clausen 	component->dev = dev;
2974bb13109dSLars-Peter Clausen 	component->driver = driver;
297561aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
297661aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
29779178feb4SKuninori Morimoto 	component->suspend = component->driver->suspend;
29789178feb4SKuninori Morimoto 	component->resume = component->driver->resume;
2979e2c330b9SLars-Peter Clausen 
29804890140fSLars-Peter Clausen 	dapm = &component->dapm;
2981ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2982ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2983ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
29845819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
298514e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
298614e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
298714e8bdebSLars-Peter Clausen 	if (driver->stream_event)
298814e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2989ce0fc93aSLars-Peter Clausen 
299061aca564SLars-Peter Clausen 	component->controls = driver->controls;
299161aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
299261aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
299361aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
299461aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
299561aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
299661aca564SLars-Peter Clausen 
2997bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2998bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2999bb13109dSLars-Peter Clausen 
3000bb13109dSLars-Peter Clausen 	return 0;
3001d191bd8dSKuninori Morimoto }
3002d191bd8dSKuninori Morimoto 
300320feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3004886f5692SLars-Peter Clausen {
3005886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
300620feb881SLars-Peter Clausen 
3007886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3008886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3009886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3010886f5692SLars-Peter Clausen }
301120feb881SLars-Peter Clausen 
3012e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3013e874bf5fSLars-Peter Clausen 
301420feb881SLars-Peter Clausen /**
301520feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
301620feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
301720feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
301820feb881SLars-Peter Clausen  *
301920feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
302020feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
302120feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
302220feb881SLars-Peter Clausen  * before the first IO attempt of the component.
302320feb881SLars-Peter Clausen  */
302420feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
302520feb881SLars-Peter Clausen 	struct regmap *regmap)
302620feb881SLars-Peter Clausen {
302720feb881SLars-Peter Clausen 	component->regmap = regmap;
302820feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3029886f5692SLars-Peter Clausen }
303020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
303120feb881SLars-Peter Clausen 
303220feb881SLars-Peter Clausen /**
303320feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
303420feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
303520feb881SLars-Peter Clausen  *
303620feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
303720feb881SLars-Peter Clausen  * removes the regmap instance from the component.
303820feb881SLars-Peter Clausen  *
303920feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
304020feb881SLars-Peter Clausen  * to initialize the regmap instance.
304120feb881SLars-Peter Clausen  */
304220feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
304320feb881SLars-Peter Clausen {
304420feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
304520feb881SLars-Peter Clausen 	component->regmap = NULL;
304620feb881SLars-Peter Clausen }
304720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3048886f5692SLars-Peter Clausen 
3049e874bf5fSLars-Peter Clausen #endif
3050d191bd8dSKuninori Morimoto 
3051bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3052bb13109dSLars-Peter Clausen {
305320feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
305420feb881SLars-Peter Clausen 		if (!component->regmap)
305520feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
305620feb881SLars-Peter Clausen 		if (component->regmap)
305720feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
305820feb881SLars-Peter Clausen 	}
3059886f5692SLars-Peter Clausen 
3060bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
30618a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3062d191bd8dSKuninori Morimoto }
3063d191bd8dSKuninori Morimoto 
3064bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3065bb13109dSLars-Peter Clausen {
3066d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3067bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3068d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3069bb13109dSLars-Peter Clausen }
3070d191bd8dSKuninori Morimoto 
3071bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3072bb13109dSLars-Peter Clausen {
3073bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3074bb13109dSLars-Peter Clausen 	kfree(component->name);
3075bb13109dSLars-Peter Clausen }
3076d191bd8dSKuninori Morimoto 
3077bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3078bb13109dSLars-Peter Clausen {
3079*c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3080*c12c1aadSKuninori Morimoto 
3081*c12c1aadSKuninori Morimoto 	if (card)
3082*c12c1aadSKuninori Morimoto 		snd_soc_unregister_card(card);
3083*c12c1aadSKuninori Morimoto 
3084bb13109dSLars-Peter Clausen 	list_del(&component->list);
3085bb13109dSLars-Peter Clausen }
3086d191bd8dSKuninori Morimoto 
3087d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3088d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3089d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3090d191bd8dSKuninori Morimoto 			       int num_dai)
3091d191bd8dSKuninori Morimoto {
3092d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3093bb13109dSLars-Peter Clausen 	int ret;
3094d191bd8dSKuninori Morimoto 
3095bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3096d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3097d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3098d191bd8dSKuninori Morimoto 		return -ENOMEM;
3099d191bd8dSKuninori Morimoto 	}
3100d191bd8dSKuninori Morimoto 
3101bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3102bb13109dSLars-Peter Clausen 	if (ret)
3103bb13109dSLars-Peter Clausen 		goto err_free;
3104bb13109dSLars-Peter Clausen 
31053d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
310698e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
31073d59400fSLars-Peter Clausen 
3108bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3109bb13109dSLars-Peter Clausen 	if (ret < 0) {
3110f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3111bb13109dSLars-Peter Clausen 		goto err_cleanup;
3112bb13109dSLars-Peter Clausen 	}
3113bb13109dSLars-Peter Clausen 
3114bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3115bb13109dSLars-Peter Clausen 
3116bb13109dSLars-Peter Clausen 	return 0;
3117bb13109dSLars-Peter Clausen 
3118bb13109dSLars-Peter Clausen err_cleanup:
3119bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3120bb13109dSLars-Peter Clausen err_free:
3121bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3122bb13109dSLars-Peter Clausen 	return ret;
3123d191bd8dSKuninori Morimoto }
3124d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3125d191bd8dSKuninori Morimoto 
3126d191bd8dSKuninori Morimoto /**
3127d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3128d191bd8dSKuninori Morimoto  *
3129628536eaSJonathan Corbet  * @dev: The device to unregister
3130d191bd8dSKuninori Morimoto  */
3131d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3132d191bd8dSKuninori Morimoto {
3133d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3134d191bd8dSKuninori Morimoto 
313534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3136d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
313798e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3138d191bd8dSKuninori Morimoto 			goto found;
3139d191bd8dSKuninori Morimoto 	}
314034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3141d191bd8dSKuninori Morimoto 	return;
3142d191bd8dSKuninori Morimoto 
3143d191bd8dSKuninori Morimoto found:
31448a978234SLiam Girdwood 	snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
314534e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
314634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3147bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3148bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3149d191bd8dSKuninori Morimoto }
3150d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3151d191bd8dSKuninori Morimoto 
3152f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3153e2c330b9SLars-Peter Clausen {
3154e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3155e2c330b9SLars-Peter Clausen 
3156f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3157e2c330b9SLars-Peter Clausen }
3158e2c330b9SLars-Peter Clausen 
3159f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3160e2c330b9SLars-Peter Clausen {
3161e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3162e2c330b9SLars-Peter Clausen 
3163f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3164e2c330b9SLars-Peter Clausen }
3165e2c330b9SLars-Peter Clausen 
3166d191bd8dSKuninori Morimoto /**
316771a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
316871a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
316971a45cdaSLars-Peter Clausen  * @platform: The platform to add
31707d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
317112a48a8cSMark Brown  */
317271a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3173d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
317412a48a8cSMark Brown {
3175b37f1d12SLars-Peter Clausen 	int ret;
3176b37f1d12SLars-Peter Clausen 
3177bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3178bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3179bb13109dSLars-Peter Clausen 	if (ret)
3180bb13109dSLars-Peter Clausen 		return ret;
3181f0fba2adSLiam Girdwood 
3182f0fba2adSLiam Girdwood 	platform->dev = dev;
3183f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3184f1d45cc3SLars-Peter Clausen 
3185f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3186f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3187f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3188f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
318912a48a8cSMark Brown 
319081c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
319181c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
319281c7cfd1SLars-Peter Clausen #endif
319312a48a8cSMark Brown 
319412a48a8cSMark Brown 	mutex_lock(&client_mutex);
3195bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
319612a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
319712a48a8cSMark Brown 	mutex_unlock(&client_mutex);
319812a48a8cSMark Brown 
3199f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3200f4333203SLars-Peter Clausen 		platform->component.name);
320112a48a8cSMark Brown 
320212a48a8cSMark Brown 	return 0;
320312a48a8cSMark Brown }
320471a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
320571a45cdaSLars-Peter Clausen 
320671a45cdaSLars-Peter Clausen /**
320771a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
320871a45cdaSLars-Peter Clausen  *
3209628536eaSJonathan Corbet  * @dev: The device for the platform
3210628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
321171a45cdaSLars-Peter Clausen  */
321271a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
321371a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
321471a45cdaSLars-Peter Clausen {
321571a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
321671a45cdaSLars-Peter Clausen 	int ret;
321771a45cdaSLars-Peter Clausen 
321871a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
321971a45cdaSLars-Peter Clausen 
322071a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
322171a45cdaSLars-Peter Clausen 	if (platform == NULL)
322271a45cdaSLars-Peter Clausen 		return -ENOMEM;
322371a45cdaSLars-Peter Clausen 
322471a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
322571a45cdaSLars-Peter Clausen 	if (ret)
322671a45cdaSLars-Peter Clausen 		kfree(platform);
322771a45cdaSLars-Peter Clausen 
322871a45cdaSLars-Peter Clausen 	return ret;
322971a45cdaSLars-Peter Clausen }
323012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
323112a48a8cSMark Brown 
323212a48a8cSMark Brown /**
323371a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
323471a45cdaSLars-Peter Clausen  * @platform: the platform to remove
323571a45cdaSLars-Peter Clausen  */
323671a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
323771a45cdaSLars-Peter Clausen {
3238b37f1d12SLars-Peter Clausen 
323971a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
324071a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3241bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
324271a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
324371a45cdaSLars-Peter Clausen 
324471a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3245f4333203SLars-Peter Clausen 		platform->component.name);
3246decc27b0SDaniel Mack 
3247decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
324871a45cdaSLars-Peter Clausen }
324971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
325071a45cdaSLars-Peter Clausen 
325171a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
325271a45cdaSLars-Peter Clausen {
325371a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
325471a45cdaSLars-Peter Clausen 
325534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
325671a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
325734e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
325834e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
325971a45cdaSLars-Peter Clausen 			return platform;
326071a45cdaSLars-Peter Clausen 		}
326134e81ab4SLars-Peter Clausen 	}
326234e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
326371a45cdaSLars-Peter Clausen 
326471a45cdaSLars-Peter Clausen 	return NULL;
326571a45cdaSLars-Peter Clausen }
326671a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
326771a45cdaSLars-Peter Clausen 
326871a45cdaSLars-Peter Clausen /**
326912a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
327012a48a8cSMark Brown  *
3271628536eaSJonathan Corbet  * @dev: platform to unregister
327212a48a8cSMark Brown  */
3273f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
327412a48a8cSMark Brown {
3275f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3276f0fba2adSLiam Girdwood 
327771a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
327871a45cdaSLars-Peter Clausen 	if (!platform)
3279f0fba2adSLiam Girdwood 		return;
3280f0fba2adSLiam Girdwood 
328171a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3282f0fba2adSLiam Girdwood 	kfree(platform);
328312a48a8cSMark Brown }
328412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
328512a48a8cSMark Brown 
3286151ab22cSMark Brown static u64 codec_format_map[] = {
3287151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3288151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3289151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3290151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3291151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3292151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3293151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3294151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3295151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3296151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3297151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3298151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3299151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3300151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3301151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3302151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3303151ab22cSMark Brown };
3304151ab22cSMark Brown 
3305151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3306151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3307151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3308151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3309151ab22cSMark Brown  */
3310151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3311151ab22cSMark Brown {
3312151ab22cSMark Brown 	int i;
3313151ab22cSMark Brown 
3314151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3315151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3316151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3317151ab22cSMark Brown }
3318151ab22cSMark Brown 
3319f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3320f1d45cc3SLars-Peter Clausen {
3321f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3322f1d45cc3SLars-Peter Clausen 
3323f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3324f1d45cc3SLars-Peter Clausen }
3325f1d45cc3SLars-Peter Clausen 
3326f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3327f1d45cc3SLars-Peter Clausen {
3328f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3329f1d45cc3SLars-Peter Clausen 
3330f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3331f1d45cc3SLars-Peter Clausen }
3332f1d45cc3SLars-Peter Clausen 
33339178feb4SKuninori Morimoto static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
33349178feb4SKuninori Morimoto {
33359178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
33369178feb4SKuninori Morimoto 
33379178feb4SKuninori Morimoto 	return codec->driver->suspend(codec);
33389178feb4SKuninori Morimoto }
33399178feb4SKuninori Morimoto 
33409178feb4SKuninori Morimoto static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
33419178feb4SKuninori Morimoto {
33429178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
33439178feb4SKuninori Morimoto 
33449178feb4SKuninori Morimoto 	return codec->driver->resume(codec);
33459178feb4SKuninori Morimoto }
33469178feb4SKuninori Morimoto 
3347e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3348e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3349e2c330b9SLars-Peter Clausen {
3350e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3351e2c330b9SLars-Peter Clausen 
3352e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3353e2c330b9SLars-Peter Clausen }
3354e2c330b9SLars-Peter Clausen 
3355e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3356e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3357e2c330b9SLars-Peter Clausen {
3358e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3359e2c330b9SLars-Peter Clausen 
3360e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3361e2c330b9SLars-Peter Clausen 
3362e2c330b9SLars-Peter Clausen 	return 0;
3363e2c330b9SLars-Peter Clausen }
3364e2c330b9SLars-Peter Clausen 
336568f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
336668f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
336768f831c2SLars-Peter Clausen {
336868f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
336968f831c2SLars-Peter Clausen 
337068f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
337168f831c2SLars-Peter Clausen }
337268f831c2SLars-Peter Clausen 
33730d0cf00aSMark Brown /**
33740d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
33750d0cf00aSMark Brown  *
3376628536eaSJonathan Corbet  * @dev: The parent device for this codec
3377628536eaSJonathan Corbet  * @codec_drv: Codec driver
3378628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3379628536eaSJonathan Corbet  * @num_dai: Number of DAIs
33800d0cf00aSMark Brown  */
3381f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3382001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3383001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3384001ae4c0SMark Brown 			   int num_dai)
33850d0cf00aSMark Brown {
33864890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3387f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3388bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3389f0fba2adSLiam Girdwood 	int ret, i;
3390151ab22cSMark Brown 
3391f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
33920d0cf00aSMark Brown 
3393f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3394f0fba2adSLiam Girdwood 	if (codec == NULL)
3395f0fba2adSLiam Girdwood 		return -ENOMEM;
33960d0cf00aSMark Brown 
3397f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3398ce0fc93aSLars-Peter Clausen 
3399bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3400bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3401bb13109dSLars-Peter Clausen 	if (ret)
3402bb13109dSLars-Peter Clausen 		goto err_free;
3403151ab22cSMark Brown 
3404f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3405f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3406f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3407f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
34089178feb4SKuninori Morimoto 	if (codec_drv->suspend)
34099178feb4SKuninori Morimoto 		codec->component.suspend = snd_soc_codec_drv_suspend;
34109178feb4SKuninori Morimoto 	if (codec_drv->resume)
34119178feb4SKuninori Morimoto 		codec->component.resume = snd_soc_codec_drv_resume;
3412e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3413e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3414e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3415e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
34163d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
34174890140fSLars-Peter Clausen 
34184890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
34194890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
34204890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
342114e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
34224890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
342368f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
34244890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3425f0fba2adSLiam Girdwood 	codec->dev = dev;
3426f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3427e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3428f0fba2adSLiam Girdwood 
342981c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
343081c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
343181c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
343281c7cfd1SLars-Peter Clausen #endif
3433a39f75f7SXiubo Li 
3434a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3435886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3436a39f75f7SXiubo Li 
3437f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3438f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3439f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3440f0fba2adSLiam Girdwood 	}
3441f0fba2adSLiam Girdwood 
3442bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3443bb13109dSLars-Peter Clausen 	if (ret < 0) {
3444f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3445bb13109dSLars-Peter Clausen 		goto err_cleanup;
3446bb13109dSLars-Peter Clausen 	}
3447bb13109dSLars-Peter Clausen 
3448bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3449bb13109dSLars-Peter Clausen 		dai->codec = codec;
3450bb13109dSLars-Peter Clausen 
3451054880feSMark Brown 	mutex_lock(&client_mutex);
3452bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3453054880feSMark Brown 	list_add(&codec->list, &codec_list);
3454054880feSMark Brown 	mutex_unlock(&client_mutex);
3455054880feSMark Brown 
3456f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3457f4333203SLars-Peter Clausen 		codec->component.name);
34580d0cf00aSMark Brown 	return 0;
3459f0fba2adSLiam Girdwood 
3460bb13109dSLars-Peter Clausen err_cleanup:
3461bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3462bb13109dSLars-Peter Clausen err_free:
3463f0fba2adSLiam Girdwood 	kfree(codec);
3464f0fba2adSLiam Girdwood 	return ret;
34650d0cf00aSMark Brown }
34660d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
34670d0cf00aSMark Brown 
34680d0cf00aSMark Brown /**
34690d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
34700d0cf00aSMark Brown  *
3471628536eaSJonathan Corbet  * @dev: codec to unregister
34720d0cf00aSMark Brown  */
3473f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
34740d0cf00aSMark Brown {
3475f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3476f0fba2adSLiam Girdwood 
347734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3478f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3479f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3480f0fba2adSLiam Girdwood 			goto found;
3481f0fba2adSLiam Girdwood 	}
348234e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3483f0fba2adSLiam Girdwood 	return;
3484f0fba2adSLiam Girdwood 
3485f0fba2adSLiam Girdwood found:
34860d0cf00aSMark Brown 	list_del(&codec->list);
3487bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
34880d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
34890d0cf00aSMark Brown 
3490f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3491f4333203SLars-Peter Clausen 			codec->component.name);
3492f0fba2adSLiam Girdwood 
3493bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
34947a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3495f0fba2adSLiam Girdwood 	kfree(codec);
34960d0cf00aSMark Brown }
34970d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
34980d0cf00aSMark Brown 
3499bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
35008f5ebb1bSKuninori Morimoto int snd_soc_of_parse_card_name_from_node(struct snd_soc_card *card,
35018f5ebb1bSKuninori Morimoto 					 struct device_node *np,
3502bec4fa05SStephen Warren 					 const char *propname)
3503bec4fa05SStephen Warren {
3504bec4fa05SStephen Warren 	int ret;
3505bec4fa05SStephen Warren 
35067e07e7c0STushar Behera 	if (!card->dev) {
35077e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
35087e07e7c0STushar Behera 		return -EINVAL;
35097e07e7c0STushar Behera 	}
35107e07e7c0STushar Behera 
35118f5ebb1bSKuninori Morimoto 	if (!np)
35127e07e7c0STushar Behera 		np = card->dev->of_node;
35137e07e7c0STushar Behera 
3514bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3515bec4fa05SStephen Warren 	/*
3516bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3517bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3518bec4fa05SStephen Warren 	 * snd_soc_register_card.
3519bec4fa05SStephen Warren 	 */
3520bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3521bec4fa05SStephen Warren 		dev_err(card->dev,
3522f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3523bec4fa05SStephen Warren 			propname, ret);
3524bec4fa05SStephen Warren 		return ret;
3525bec4fa05SStephen Warren 	}
3526bec4fa05SStephen Warren 
3527bec4fa05SStephen Warren 	return 0;
3528bec4fa05SStephen Warren }
35298f5ebb1bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name_from_node);
3530bec4fa05SStephen Warren 
35319a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
35329a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
35339a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
35349a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
35359a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
35369a6d4860SXiubo Li };
35379a6d4860SXiubo Li 
35381ef5bcd5SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets_from_node(struct snd_soc_card *card,
35391ef5bcd5SKuninori Morimoto 					  struct device_node *np,
35409a6d4860SXiubo Li 					  const char *propname)
35419a6d4860SXiubo Li {
35429a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
35439a6d4860SXiubo Li 	const char *template, *wname;
35449a6d4860SXiubo Li 	int i, j, num_widgets, ret;
35459a6d4860SXiubo Li 
35461ef5bcd5SKuninori Morimoto 	if (!np)
35471ef5bcd5SKuninori Morimoto 		np = card->dev->of_node;
35481ef5bcd5SKuninori Morimoto 
35499a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
35509a6d4860SXiubo Li 	if (num_widgets < 0) {
35519a6d4860SXiubo Li 		dev_err(card->dev,
35529a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
35539a6d4860SXiubo Li 		return -EINVAL;
35549a6d4860SXiubo Li 	}
35559a6d4860SXiubo Li 	if (num_widgets & 1) {
35569a6d4860SXiubo Li 		dev_err(card->dev,
35579a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
35589a6d4860SXiubo Li 		return -EINVAL;
35599a6d4860SXiubo Li 	}
35609a6d4860SXiubo Li 
35619a6d4860SXiubo Li 	num_widgets /= 2;
35629a6d4860SXiubo Li 	if (!num_widgets) {
35639a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
35649a6d4860SXiubo Li 			propname);
35659a6d4860SXiubo Li 		return -EINVAL;
35669a6d4860SXiubo Li 	}
35679a6d4860SXiubo Li 
35689a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
35699a6d4860SXiubo Li 			       GFP_KERNEL);
35709a6d4860SXiubo Li 	if (!widgets) {
35719a6d4860SXiubo Li 		dev_err(card->dev,
35729a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
35739a6d4860SXiubo Li 		return -ENOMEM;
35749a6d4860SXiubo Li 	}
35759a6d4860SXiubo Li 
35769a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
35779a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35789a6d4860SXiubo Li 			2 * i, &template);
35799a6d4860SXiubo Li 		if (ret) {
35809a6d4860SXiubo Li 			dev_err(card->dev,
35819a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35829a6d4860SXiubo Li 				propname, 2 * i, ret);
35839a6d4860SXiubo Li 			return -EINVAL;
35849a6d4860SXiubo Li 		}
35859a6d4860SXiubo Li 
35869a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
35879a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
35889a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
35899a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
35909a6d4860SXiubo Li 				break;
35919a6d4860SXiubo Li 			}
35929a6d4860SXiubo Li 		}
35939a6d4860SXiubo Li 
35949a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
35959a6d4860SXiubo Li 			dev_err(card->dev,
35969a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
35979a6d4860SXiubo Li 				template);
35989a6d4860SXiubo Li 			return -EINVAL;
35999a6d4860SXiubo Li 		}
36009a6d4860SXiubo Li 
36019a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
36029a6d4860SXiubo Li 						    (2 * i) + 1,
36039a6d4860SXiubo Li 						    &wname);
36049a6d4860SXiubo Li 		if (ret) {
36059a6d4860SXiubo Li 			dev_err(card->dev,
36069a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
36079a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
36089a6d4860SXiubo Li 			return -EINVAL;
36099a6d4860SXiubo Li 		}
36109a6d4860SXiubo Li 
36119a6d4860SXiubo Li 		widgets[i].name = wname;
36129a6d4860SXiubo Li 	}
36139a6d4860SXiubo Li 
3614f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3615f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
36169a6d4860SXiubo Li 
36179a6d4860SXiubo Li 	return 0;
36189a6d4860SXiubo Li }
36191ef5bcd5SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets_from_node);
36209a6d4860SXiubo Li 
36216131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
36226131084aSJyri Sarha 				    const char *prop_name,
36236131084aSJyri Sarha 				    unsigned int *mask)
36246131084aSJyri Sarha {
36256131084aSJyri Sarha 	u32 val;
36266c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
36276131084aSJyri Sarha 	int i;
36286131084aSJyri Sarha 
36296131084aSJyri Sarha 	if (!of_slot_mask)
36306131084aSJyri Sarha 		return 0;
36316131084aSJyri Sarha 	val /= sizeof(u32);
36326131084aSJyri Sarha 	for (i = 0; i < val; i++)
36336131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
36346131084aSJyri Sarha 			*mask |= (1 << i);
36356131084aSJyri Sarha 
36366131084aSJyri Sarha 	return val;
36376131084aSJyri Sarha }
36386131084aSJyri Sarha 
363989c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
36406131084aSJyri Sarha 			      unsigned int *tx_mask,
36416131084aSJyri Sarha 			      unsigned int *rx_mask,
364289c67857SXiubo Li 			      unsigned int *slots,
364389c67857SXiubo Li 			      unsigned int *slot_width)
364489c67857SXiubo Li {
364589c67857SXiubo Li 	u32 val;
364689c67857SXiubo Li 	int ret;
364789c67857SXiubo Li 
36486131084aSJyri Sarha 	if (tx_mask)
36496131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
36506131084aSJyri Sarha 	if (rx_mask)
36516131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
36526131084aSJyri Sarha 
365389c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
365489c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
365589c67857SXiubo Li 		if (ret)
365689c67857SXiubo Li 			return ret;
365789c67857SXiubo Li 
365889c67857SXiubo Li 		if (slots)
365989c67857SXiubo Li 			*slots = val;
366089c67857SXiubo Li 	}
366189c67857SXiubo Li 
366289c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
366389c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
366489c67857SXiubo Li 		if (ret)
366589c67857SXiubo Li 			return ret;
366689c67857SXiubo Li 
366789c67857SXiubo Li 		if (slot_width)
366889c67857SXiubo Li 			*slot_width = val;
366989c67857SXiubo Li 	}
367089c67857SXiubo Li 
367189c67857SXiubo Li 	return 0;
367289c67857SXiubo Li }
367389c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
367489c67857SXiubo Li 
3675b6defccaSKuninori Morimoto void snd_soc_of_parse_audio_prefix_from_node(struct snd_soc_card *card,
3676b6defccaSKuninori Morimoto 				   struct device_node *np,
36775e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
36785e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
36795e3cdaa2SKuninori Morimoto 				   const char *propname)
36805e3cdaa2SKuninori Morimoto {
36815e3cdaa2SKuninori Morimoto 	const char *str;
36825e3cdaa2SKuninori Morimoto 	int ret;
36835e3cdaa2SKuninori Morimoto 
3684b6defccaSKuninori Morimoto 	if (!np)
3685b6defccaSKuninori Morimoto 		np = card->dev->of_node;
3686b6defccaSKuninori Morimoto 
36875e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
36885e3cdaa2SKuninori Morimoto 	if (ret < 0) {
36895e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
36905e3cdaa2SKuninori Morimoto 		return;
36915e3cdaa2SKuninori Morimoto 	}
36925e3cdaa2SKuninori Morimoto 
36935e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
36945e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
36955e3cdaa2SKuninori Morimoto }
3696b6defccaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix_from_node);
36975e3cdaa2SKuninori Morimoto 
36987364c8dcSKuninori Morimoto int snd_soc_of_parse_audio_routing_from_node(struct snd_soc_card *card,
36997364c8dcSKuninori Morimoto 				   struct device_node *np,
3700a4a54dd5SStephen Warren 				   const char *propname)
3701a4a54dd5SStephen Warren {
3702e3b1e6a1SMark Brown 	int num_routes;
3703a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3704a4a54dd5SStephen Warren 	int i, ret;
3705a4a54dd5SStephen Warren 
37067364c8dcSKuninori Morimoto 	if (!np)
37077364c8dcSKuninori Morimoto 		np = card->dev->of_node;
37087364c8dcSKuninori Morimoto 
3709a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3710c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
371110e8aa9aSMichał Mirosław 		dev_err(card->dev,
371210e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
371310e8aa9aSMichał Mirosław 			propname);
3714a4a54dd5SStephen Warren 		return -EINVAL;
3715a4a54dd5SStephen Warren 	}
3716a4a54dd5SStephen Warren 	num_routes /= 2;
3717a4a54dd5SStephen Warren 	if (!num_routes) {
3718f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3719a4a54dd5SStephen Warren 			propname);
3720a4a54dd5SStephen Warren 		return -EINVAL;
3721a4a54dd5SStephen Warren 	}
3722a4a54dd5SStephen Warren 
3723e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3724a4a54dd5SStephen Warren 			      GFP_KERNEL);
3725a4a54dd5SStephen Warren 	if (!routes) {
3726a4a54dd5SStephen Warren 		dev_err(card->dev,
3727f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3728a4a54dd5SStephen Warren 		return -EINVAL;
3729a4a54dd5SStephen Warren 	}
3730a4a54dd5SStephen Warren 
3731a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3732a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3733e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3734a4a54dd5SStephen Warren 		if (ret) {
3735c871bd0bSMark Brown 			dev_err(card->dev,
3736c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3737c871bd0bSMark Brown 				propname, 2 * i, ret);
3738a4a54dd5SStephen Warren 			return -EINVAL;
3739a4a54dd5SStephen Warren 		}
3740a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3741e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3742a4a54dd5SStephen Warren 		if (ret) {
3743a4a54dd5SStephen Warren 			dev_err(card->dev,
3744c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3745c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3746a4a54dd5SStephen Warren 			return -EINVAL;
3747a4a54dd5SStephen Warren 		}
3748a4a54dd5SStephen Warren 	}
3749a4a54dd5SStephen Warren 
3750f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3751f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3752a4a54dd5SStephen Warren 
3753a4a54dd5SStephen Warren 	return 0;
3754a4a54dd5SStephen Warren }
37557364c8dcSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing_from_node);
3756a4a54dd5SStephen Warren 
3757a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3758389cb834SJyri Sarha 				     const char *prefix,
3759389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3760389cb834SJyri Sarha 				     struct device_node **framemaster)
3761a7930ed4SKuninori Morimoto {
3762a7930ed4SKuninori Morimoto 	int ret, i;
3763a7930ed4SKuninori Morimoto 	char prop[128];
3764a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3765a7930ed4SKuninori Morimoto 	int bit, frame;
3766a7930ed4SKuninori Morimoto 	const char *str;
3767a7930ed4SKuninori Morimoto 	struct {
3768a7930ed4SKuninori Morimoto 		char *name;
3769a7930ed4SKuninori Morimoto 		unsigned int val;
3770a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3771a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3772a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3773a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3774a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3775a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3776a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3777a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3778a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3779a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3780a7930ed4SKuninori Morimoto 	};
3781a7930ed4SKuninori Morimoto 
3782a7930ed4SKuninori Morimoto 	if (!prefix)
3783a7930ed4SKuninori Morimoto 		prefix = "";
3784a7930ed4SKuninori Morimoto 
3785a7930ed4SKuninori Morimoto 	/*
3786a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3787a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3788a7930ed4SKuninori Morimoto 	 */
3789a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3790a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3791a7930ed4SKuninori Morimoto 	if (ret == 0) {
3792a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3793a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3794a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3795a7930ed4SKuninori Morimoto 				break;
3796a7930ed4SKuninori Morimoto 			}
3797a7930ed4SKuninori Morimoto 		}
3798a7930ed4SKuninori Morimoto 	}
3799a7930ed4SKuninori Morimoto 
3800a7930ed4SKuninori Morimoto 	/*
38018c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3802a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3803a7930ed4SKuninori Morimoto 	 */
38048c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
380551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
38068c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
38078c2d6a9fSKuninori Morimoto 	else
38088c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3809a7930ed4SKuninori Morimoto 
3810a7930ed4SKuninori Morimoto 	/*
3811a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3812a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3813a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3814a7930ed4SKuninori Morimoto 	 */
3815a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3816a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3817a7930ed4SKuninori Morimoto 
3818a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3819a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3820a7930ed4SKuninori Morimoto 
3821a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3822a7930ed4SKuninori Morimoto 	case 0x11:
3823a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3824a7930ed4SKuninori Morimoto 		break;
3825a7930ed4SKuninori Morimoto 	case 0x10:
3826a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3827a7930ed4SKuninori Morimoto 		break;
3828a7930ed4SKuninori Morimoto 	case 0x01:
3829a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3830a7930ed4SKuninori Morimoto 		break;
3831a7930ed4SKuninori Morimoto 	default:
3832a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3833a7930ed4SKuninori Morimoto 		break;
3834a7930ed4SKuninori Morimoto 	}
3835a7930ed4SKuninori Morimoto 
3836a7930ed4SKuninori Morimoto 	/*
3837a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3838a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3839a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3840a7930ed4SKuninori Morimoto 	 */
3841a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3842a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3843389cb834SJyri Sarha 	if (bit && bitclkmaster)
3844389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3845a7930ed4SKuninori Morimoto 
3846a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3847a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3848389cb834SJyri Sarha 	if (frame && framemaster)
3849389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3850a7930ed4SKuninori Morimoto 
3851a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3852a7930ed4SKuninori Morimoto 	case 0x11:
3853a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3854a7930ed4SKuninori Morimoto 		break;
3855a7930ed4SKuninori Morimoto 	case 0x10:
3856a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3857a7930ed4SKuninori Morimoto 		break;
3858a7930ed4SKuninori Morimoto 	case 0x01:
3859a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3860a7930ed4SKuninori Morimoto 		break;
3861a7930ed4SKuninori Morimoto 	default:
3862a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3863a7930ed4SKuninori Morimoto 		break;
3864a7930ed4SKuninori Morimoto 	}
3865a7930ed4SKuninori Morimoto 
3866a7930ed4SKuninori Morimoto 	return format;
3867a7930ed4SKuninori Morimoto }
3868a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3869a7930ed4SKuninori Morimoto 
38701ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3871cb470087SKuninori Morimoto 				const char **dai_name)
3872cb470087SKuninori Morimoto {
3873cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
38743e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
387593b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3876cb470087SKuninori Morimoto 
3877cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3878cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
38793e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
38803e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
38813e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
38823e0aa8d8SJyri Sarha 
38833e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3884cb470087SKuninori Morimoto 			continue;
3885cb470087SKuninori Morimoto 
38866833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
388793b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
388893b0f3eeSJean-Francois Moine 							     args,
388993b0f3eeSJean-Francois Moine 							     dai_name);
38906833c452SKuninori Morimoto 		} else {
38916833c452SKuninori Morimoto 			int id = -1;
38926833c452SKuninori Morimoto 
389393b0f3eeSJean-Francois Moine 			switch (args->args_count) {
38946833c452SKuninori Morimoto 			case 0:
38956833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
38966833c452SKuninori Morimoto 				break;
38976833c452SKuninori Morimoto 			case 1:
389893b0f3eeSJean-Francois Moine 				id = args->args[0];
38996833c452SKuninori Morimoto 				break;
39006833c452SKuninori Morimoto 			default:
39016833c452SKuninori Morimoto 				/* not supported */
3902cb470087SKuninori Morimoto 				break;
3903cb470087SKuninori Morimoto 			}
3904cb470087SKuninori Morimoto 
39056833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
39066833c452SKuninori Morimoto 				ret = -EINVAL;
39073dcba280SNicolin Chen 				continue;
39086833c452SKuninori Morimoto 			}
3909e41975edSXiubo Li 
3910e41975edSXiubo Li 			ret = 0;
3911e41975edSXiubo Li 
3912e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3913e41975edSXiubo Li 			if (!*dai_name)
3914e41975edSXiubo Li 				*dai_name = pos->name;
39156833c452SKuninori Morimoto 		}
39166833c452SKuninori Morimoto 
3917cb470087SKuninori Morimoto 		break;
3918cb470087SKuninori Morimoto 	}
3919cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
392093b0f3eeSJean-Francois Moine 	return ret;
392193b0f3eeSJean-Francois Moine }
39221ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
392393b0f3eeSJean-Francois Moine 
392493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
392593b0f3eeSJean-Francois Moine 			    const char **dai_name)
392693b0f3eeSJean-Francois Moine {
392793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
392893b0f3eeSJean-Francois Moine 	int ret;
392993b0f3eeSJean-Francois Moine 
393093b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
393193b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
393293b0f3eeSJean-Francois Moine 	if (ret)
393393b0f3eeSJean-Francois Moine 		return ret;
393493b0f3eeSJean-Francois Moine 
393593b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3936cb470087SKuninori Morimoto 
3937cb470087SKuninori Morimoto 	of_node_put(args.np);
3938cb470087SKuninori Morimoto 
3939cb470087SKuninori Morimoto 	return ret;
3940cb470087SKuninori Morimoto }
3941cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3942cb470087SKuninori Morimoto 
394393b0f3eeSJean-Francois Moine /*
394493b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
394593b0f3eeSJean-Francois Moine  * @dev: Card device
394693b0f3eeSJean-Francois Moine  * @of_node: Device node
394793b0f3eeSJean-Francois Moine  * @dai_link: DAI link
394893b0f3eeSJean-Francois Moine  *
394993b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
395093b0f3eeSJean-Francois Moine  * 'sound-dai'.
395193b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
395293b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
395393b0f3eeSJean-Francois Moine  *
395493b0f3eeSJean-Francois Moine  * Returns 0 for success
395593b0f3eeSJean-Francois Moine  */
395693b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
395793b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
395893b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
395993b0f3eeSJean-Francois Moine {
396093b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
396193b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
396293b0f3eeSJean-Francois Moine 	char *name;
396393b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
396493b0f3eeSJean-Francois Moine 
396593b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
396693b0f3eeSJean-Francois Moine 	name = "sound-dai";
396793b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
396893b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
396993b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
397093b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
397193b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
397293b0f3eeSJean-Francois Moine 		else
397393b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
397493b0f3eeSJean-Francois Moine 		return num_codecs;
397593b0f3eeSJean-Francois Moine 	}
397693b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
397793b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
397893b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
397993b0f3eeSJean-Francois Moine 	if (!component)
398093b0f3eeSJean-Francois Moine 		return -ENOMEM;
398193b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
398293b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
398393b0f3eeSJean-Francois Moine 
398493b0f3eeSJean-Francois Moine 	/* Parse the list */
398593b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
398693b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
398793b0f3eeSJean-Francois Moine 	     index++, component++) {
398893b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
398993b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
399093b0f3eeSJean-Francois Moine 						  index, &args);
399193b0f3eeSJean-Francois Moine 		if (ret)
399293b0f3eeSJean-Francois Moine 			goto err;
399393b0f3eeSJean-Francois Moine 		component->of_node = args.np;
399493b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
399593b0f3eeSJean-Francois Moine 		if (ret < 0)
399693b0f3eeSJean-Francois Moine 			goto err;
399793b0f3eeSJean-Francois Moine 	}
399893b0f3eeSJean-Francois Moine 	return 0;
399993b0f3eeSJean-Francois Moine err:
400093b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
400193b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
400293b0f3eeSJean-Francois Moine 	     index++, component++) {
400393b0f3eeSJean-Francois Moine 		if (!component->of_node)
400493b0f3eeSJean-Francois Moine 			break;
400593b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
400693b0f3eeSJean-Francois Moine 		component->of_node = NULL;
400793b0f3eeSJean-Francois Moine 	}
400893b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
400993b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
401093b0f3eeSJean-Francois Moine 	return ret;
401193b0f3eeSJean-Francois Moine }
401293b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
401393b0f3eeSJean-Francois Moine 
4014c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4015db2a4165SFrank Mandarino {
40166553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
4017fb257897SMark Brown 	snd_soc_util_init();
4018fb257897SMark Brown 
4019db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4020db2a4165SFrank Mandarino }
40214abe8e16SMark Brown module_init(snd_soc_init);
4022db2a4165SFrank Mandarino 
40237d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4024db2a4165SFrank Mandarino {
4025fb257897SMark Brown 	snd_soc_util_exit();
40266553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
4027fb257897SMark Brown 
4028384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
4029384c89e2SMark Brown #endif
4030db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4031db2a4165SFrank Mandarino }
4032db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4033db2a4165SFrank Mandarino 
4034db2a4165SFrank Mandarino /* Module information */
4035d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4036db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4037db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4038db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4039