xref: /openbmc/linux/sound/soc/soc-core.c (revision 1ad8ec535b997ed36c0f32c2616206725258dd30)
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);
6292eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
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 
705f0fba2adSLiam Girdwood 	/* suspend all CODECs */
7062eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7074890140fSLars-Peter Clausen 		struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
7084890140fSLars-Peter Clausen 
7091547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
7101547aba9SMark Brown 		 * bias _ON and should not be suspended. */
711a8093297SLars-Peter Clausen 		if (!codec->suspended) {
7124890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7131547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
714125a25daSMark Brown 				/*
715125a25daSMark Brown 				 * If the CODEC 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) {
721125a25daSMark Brown 					dev_dbg(codec->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:
727a8093297SLars-Peter Clausen 				if (codec->driver->suspend)
72884b315eeSLars-Peter Clausen 					codec->driver->suspend(codec);
729f0fba2adSLiam Girdwood 				codec->suspended = 1;
730e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
731e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
732988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
733988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
7341547aba9SMark Brown 				break;
7351547aba9SMark Brown 			default:
73610e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
73710e8aa9aSMichał Mirosław 					"ASoC: CODEC 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;
7712eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
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 
7972eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
798a8093297SLars-Peter Clausen 		if (codec->suspended) {
799a8093297SLars-Peter Clausen 			if (codec->driver->resume)
800f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
801f0fba2adSLiam Girdwood 			codec->suspended = 0;
8021547aba9SMark Brown 		}
803f0fba2adSLiam Girdwood 	}
804db2a4165SFrank Mandarino 
8051a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8063efab7dcSMark Brown 
8071a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8083efab7dcSMark Brown 			continue;
8093efab7dcSMark Brown 
8101a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
811d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
812db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
813f0fba2adSLiam Girdwood 
8141a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
815d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
816db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
817db2a4165SFrank Mandarino 	}
818db2a4165SFrank Mandarino 
8193ff3f64bSMark Brown 	/* unmute any active DACs */
8201a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8213efab7dcSMark Brown 
8221a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8233efab7dcSMark Brown 			continue;
8243efab7dcSMark Brown 
8251a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8261a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
82788bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
82888bd870fSBenoit Cousson 
829f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
830f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
831db2a4165SFrank Mandarino 		}
83288bd870fSBenoit Cousson 	}
833db2a4165SFrank Mandarino 
8341a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8351a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8363efab7dcSMark Brown 
8371a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8383efab7dcSMark Brown 			continue;
8393efab7dcSMark Brown 
840bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
841f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
842db2a4165SFrank Mandarino 	}
843db2a4165SFrank Mandarino 
84487506549SMark Brown 	if (card->resume_post)
84570b2ac12SMark Brown 		card->resume_post(card);
846db2a4165SFrank Mandarino 
847f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8486ed25978SAndy Green 
8498be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8508be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
851e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8521a7aaa58SJeeja KP 
8531a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
8541a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
8556ed25978SAndy Green }
8566ed25978SAndy Green 
8576ed25978SAndy Green /* powers up audio subsystem after a suspend */
8586f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8596ed25978SAndy Green {
8606f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
861bc263214SLars-Peter Clausen 	bool bus_control = false;
8621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
863b9dd94a8SPeter Ujfalusi 
864c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
865c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8665ff1ddf2SEric Miao 		return 0;
8675ff1ddf2SEric Miao 
868988e8cc4SNicolin Chen 	/* activate pins from sleep state */
8691a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
87088bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
87188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87288bd870fSBenoit Cousson 		int j;
87388bd870fSBenoit Cousson 
874988e8cc4SNicolin Chen 		if (cpu_dai->active)
875988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
87688bd870fSBenoit Cousson 
87788bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
87888bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
879988e8cc4SNicolin Chen 			if (codec_dai->active)
880988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
881988e8cc4SNicolin Chen 		}
88288bd870fSBenoit Cousson 	}
883988e8cc4SNicolin Chen 
884bc263214SLars-Peter Clausen 	/*
885bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
886bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
887bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
88864ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
88964ab9baaSMark Brown 	 */
8901a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8911a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
892bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
89382e14e8bSStephen Warren 	}
894bc263214SLars-Peter Clausen 	if (bus_control) {
895bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
89664ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
89764ab9baaSMark Brown 	} else {
898f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8996308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
900f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
901f0fba2adSLiam Girdwood 	}
9026ed25978SAndy Green 
903db2a4165SFrank Mandarino 	return 0;
904db2a4165SFrank Mandarino }
9056f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
906db2a4165SFrank Mandarino #else
9076f8ab4acSMark Brown #define snd_soc_suspend NULL
9086f8ab4acSMark Brown #define snd_soc_resume NULL
909db2a4165SFrank Mandarino #endif
910db2a4165SFrank Mandarino 
91185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
91202a06d30SBarry Song };
91302a06d30SBarry Song 
91465d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
91565d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
91612023a9aSMisael Lopez Cruz {
91765d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
91812023a9aSMisael Lopez Cruz 
91934e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
92034e81ab4SLars-Peter Clausen 
92165d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
92265d9361fSLars-Peter Clausen 		if (of_node) {
92365d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
92465d9361fSLars-Peter Clausen 				return component;
92565d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
92665d9361fSLars-Peter Clausen 			return component;
92712023a9aSMisael Lopez Cruz 		}
92812023a9aSMisael Lopez Cruz 	}
92912023a9aSMisael Lopez Cruz 
93012023a9aSMisael Lopez Cruz 	return NULL;
93112023a9aSMisael Lopez Cruz }
93212023a9aSMisael Lopez Cruz 
933fbb88b5cSMengdong Lin /**
934fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
935fbb88b5cSMengdong Lin  *
936fbb88b5cSMengdong Lin  * @dlc: name of the DAI and optional component info to match
937fbb88b5cSMengdong Lin  *
938fbb88b5cSMengdong Lin  * This function will search all regsitered components and their DAIs to
939fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
940fbb88b5cSMengdong Lin  * should also match if being specified.
941fbb88b5cSMengdong Lin  *
942fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
943fbb88b5cSMengdong Lin  */
944305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
94514621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
94612023a9aSMisael Lopez Cruz {
94714621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
94814621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
9493e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
95012023a9aSMisael Lopez Cruz 
95134e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
95234e81ab4SLars-Peter Clausen 
95314621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
95414621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
9553e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
9563e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
9573e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
9583e0aa8d8SJyri Sarha 
9593e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
96012023a9aSMisael Lopez Cruz 			continue;
9611ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
96212023a9aSMisael Lopez Cruz 			continue;
96314621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
96414621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
96514621c7eSLars-Peter Clausen 				continue;
96612023a9aSMisael Lopez Cruz 
96714621c7eSLars-Peter Clausen 			return dai;
96812023a9aSMisael Lopez Cruz 		}
96912023a9aSMisael Lopez Cruz 	}
97012023a9aSMisael Lopez Cruz 
97112023a9aSMisael Lopez Cruz 	return NULL;
97212023a9aSMisael Lopez Cruz }
973305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
97412023a9aSMisael Lopez Cruz 
97549a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
97649a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
977db2a4165SFrank Mandarino {
97849a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
97949a5ba1cSMengdong Lin 
98049a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
98149a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
98249a5ba1cSMengdong Lin 			return true;
98349a5ba1cSMengdong Lin 	}
98449a5ba1cSMengdong Lin 
98549a5ba1cSMengdong Lin 	return false;
98649a5ba1cSMengdong Lin }
98749a5ba1cSMengdong Lin 
9886f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
9896f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
990db2a4165SFrank Mandarino {
9911a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
99288bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
99314621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
9941a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
995435c5e25SMark Brown 	struct snd_soc_platform *platform;
996848dd8beSMark Brown 	const char *platform_name;
99788bd870fSBenoit Cousson 	int i;
998db2a4165SFrank Mandarino 
9996f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10006308419aSMark Brown 
100149a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
100249a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
100349a5ba1cSMengdong Lin 			dai_link->name);
100449a5ba1cSMengdong Lin 		return 0;
100549a5ba1cSMengdong Lin 	}
1006db2a4165SFrank Mandarino 
1007513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1008513cb311SSudip Mukherjee 	if (!rtd)
1009513cb311SSudip Mukherjee 		return -ENOMEM;
1010513cb311SSudip Mukherjee 
101114621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
101214621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
101314621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
101414621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1015b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1016f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1017f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
10181a497983SMengdong Lin 		goto _err_defer;
1019c5af3a2eSMark Brown 	}
1020c5af3a2eSMark Brown 
102188bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
102288bd870fSBenoit Cousson 
102388bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
10241a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
102588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
102614621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
102788bd870fSBenoit Cousson 		if (!codec_dais[i]) {
102812023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
102988bd870fSBenoit Cousson 				codecs[i].dai_name);
10301a497983SMengdong Lin 			goto _err_defer;
103112023a9aSMisael Lopez Cruz 		}
103288bd870fSBenoit Cousson 	}
103388bd870fSBenoit Cousson 
103488bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
103588bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
103688bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
103712023a9aSMisael Lopez Cruz 
1038848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1039848dd8beSMark Brown 	platform_name = dai_link->platform_name;
10405a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1041848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1042848dd8beSMark Brown 
1043b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
1044f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
10455a504963SStephen Warren 		if (dai_link->platform_of_node) {
10465a504963SStephen Warren 			if (platform->dev->of_node !=
10475a504963SStephen Warren 			    dai_link->platform_of_node)
10485a504963SStephen Warren 				continue;
10495a504963SStephen Warren 		} else {
1050f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
10512610ab77SStephen Warren 				continue;
10525a504963SStephen Warren 		}
10532610ab77SStephen Warren 
1054f0fba2adSLiam Girdwood 		rtd->platform = platform;
1055f0fba2adSLiam Girdwood 	}
1056b19e6e7bSMark Brown 	if (!rtd->platform) {
1057f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1058f0fba2adSLiam Girdwood 			dai_link->platform_name);
105970fcad49SCharles Keepax 		goto _err_defer;
1060f0fba2adSLiam Girdwood 	}
1061b19e6e7bSMark Brown 
10621a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1063b19e6e7bSMark Brown 	return 0;
10641a497983SMengdong Lin 
10651a497983SMengdong Lin _err_defer:
10661a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
10671a497983SMengdong Lin 	return  -EPROBE_DEFER;
10686b05eda6SMark Brown }
10696b05eda6SMark Brown 
1070f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1071d12cd198SStephen Warren {
1072abd31b32SLars-Peter Clausen 	if (!component->card)
107370090bbbSLars-Peter Clausen 		return;
1074d12cd198SStephen Warren 
1075f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1076f1d45cc3SLars-Peter Clausen 	if (component->codec)
1077f1d45cc3SLars-Peter Clausen 		list_del(&component->codec->card_list);
1078d12cd198SStephen Warren 
1079f1d45cc3SLars-Peter Clausen 	if (component->remove)
1080f1d45cc3SLars-Peter Clausen 		component->remove(component);
1081d12cd198SStephen Warren 
1082f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1083d12cd198SStephen Warren 
1084f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1085abd31b32SLars-Peter Clausen 	component->card = NULL;
1086f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1087d12cd198SStephen Warren }
1088d12cd198SStephen Warren 
1089e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1090589c3563SJarkko Nikula {
1091589c3563SJarkko Nikula 	int err;
1092589c3563SJarkko Nikula 
1093e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1094e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1095e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1096e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1097589c3563SJarkko Nikula 			if (err < 0)
1098e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1099b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1100e60cd14fSLars-Peter Clausen 					dai->name, err);
1101b0aa88afSMisael Lopez Cruz 		}
1102e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1103b0aa88afSMisael Lopez Cruz 	}
1104b0aa88afSMisael Lopez Cruz }
1105b0aa88afSMisael Lopez Cruz 
11061a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11071a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1108f0fba2adSLiam Girdwood {
1109e60cd14fSLars-Peter Clausen 	int i;
1110f0fba2adSLiam Girdwood 
1111f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1112f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
111336ae1a96SMark Brown 		device_unregister(rtd->dev);
1114f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
111502a06d30SBarry Song 	}
111602a06d30SBarry Song 
1117f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
111888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1119e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1120f0fba2adSLiam Girdwood 
1121e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1122f0fba2adSLiam Girdwood }
1123f0fba2adSLiam Girdwood 
11241a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11251a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
112662ae68faSStephen Warren {
112762ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
112862ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
112961aca564SLars-Peter Clausen 	struct snd_soc_component *component;
113088bd870fSBenoit Cousson 	int i;
113162ae68faSStephen Warren 
113262ae68faSStephen Warren 	/* remove the platform */
113370090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1134f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
113562ae68faSStephen Warren 
113662ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
113788bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
113861aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
113970090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
114061aca564SLars-Peter Clausen 			soc_remove_component(component);
114162ae68faSStephen Warren 	}
114262ae68faSStephen Warren 
114362ae68faSStephen Warren 	/* remove any CPU-side CODEC */
114462ae68faSStephen Warren 	if (cpu_dai) {
114570090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
114661aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
114762ae68faSStephen Warren 	}
114862ae68faSStephen Warren }
114962ae68faSStephen Warren 
11500671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11510671fd8eSKuninori Morimoto {
11521a497983SMengdong Lin 	int order;
11531a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1154f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
11550671fd8eSKuninori Morimoto 
11560168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
11570168bf0dSLiam Girdwood 			order++) {
11581a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
11591a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
11600168bf0dSLiam Girdwood 	}
116162ae68faSStephen Warren 
116262ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
116362ae68faSStephen Warren 			order++) {
11641a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
11651a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
116662ae68faSStephen Warren 	}
116762ae68faSStephen Warren 
1168f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1169f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1170f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1171f8f80361SMengdong Lin 				link->name);
1172f8f80361SMengdong Lin 
1173f8f80361SMengdong Lin 		list_del(&link->list);
1174f8f80361SMengdong Lin 		card->num_dai_links--;
11750671fd8eSKuninori Morimoto 	}
11760671fd8eSKuninori Morimoto }
11770671fd8eSKuninori Morimoto 
1178923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1179923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1180923c5e61SMengdong Lin {
1181923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1182923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1183923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1184923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1185923c5e61SMengdong Lin 
1186923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1187923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1188923c5e61SMengdong Lin 				GFP_KERNEL);
1189923c5e61SMengdong Lin 		if (!dai_link->codecs)
1190923c5e61SMengdong Lin 			return -ENOMEM;
1191923c5e61SMengdong Lin 
1192923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1193923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1194923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1195923c5e61SMengdong Lin 	}
1196923c5e61SMengdong Lin 
1197923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1198923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1199923c5e61SMengdong Lin 		return -EINVAL;
1200923c5e61SMengdong Lin 	}
1201923c5e61SMengdong Lin 
1202923c5e61SMengdong Lin 	return 0;
1203923c5e61SMengdong Lin }
1204923c5e61SMengdong Lin 
1205923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1206923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1207923c5e61SMengdong Lin {
1208923c5e61SMengdong Lin 	int i, ret;
1209923c5e61SMengdong Lin 
1210923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1211923c5e61SMengdong Lin 	if (ret) {
1212923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1213923c5e61SMengdong Lin 		return ret;
1214923c5e61SMengdong Lin 	}
1215923c5e61SMengdong Lin 
1216923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1217923c5e61SMengdong Lin 		/*
1218923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1219923c5e61SMengdong Lin 		 * not both or neither.
1220923c5e61SMengdong Lin 		 */
1221923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1222923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1223923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1224923c5e61SMengdong Lin 				link->name);
1225923c5e61SMengdong Lin 			return -EINVAL;
1226923c5e61SMengdong Lin 		}
1227923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1228923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1229923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1230923c5e61SMengdong Lin 				link->name);
1231923c5e61SMengdong Lin 			return -EINVAL;
1232923c5e61SMengdong Lin 		}
1233923c5e61SMengdong Lin 	}
1234923c5e61SMengdong Lin 
1235923c5e61SMengdong Lin 	/*
1236923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1237923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1238923c5e61SMengdong Lin 	 */
1239923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1240923c5e61SMengdong Lin 		dev_err(card->dev,
1241923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1242923c5e61SMengdong Lin 			link->name);
1243923c5e61SMengdong Lin 		return -EINVAL;
1244923c5e61SMengdong Lin 	}
1245923c5e61SMengdong Lin 
1246923c5e61SMengdong Lin 	/*
1247923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1248923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1249923c5e61SMengdong Lin 	 * name alone..
1250923c5e61SMengdong Lin 	 */
1251923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1252923c5e61SMengdong Lin 		dev_err(card->dev,
1253923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1254923c5e61SMengdong Lin 			link->name);
1255923c5e61SMengdong Lin 		return -EINVAL;
1256923c5e61SMengdong Lin 	}
1257923c5e61SMengdong Lin 	/*
1258923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1259923c5e61SMengdong Lin 	 * specified
1260923c5e61SMengdong Lin 	 */
1261923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1262923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1263923c5e61SMengdong Lin 		dev_err(card->dev,
1264923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1265923c5e61SMengdong Lin 			link->name);
1266923c5e61SMengdong Lin 		return -EINVAL;
1267923c5e61SMengdong Lin 	}
1268923c5e61SMengdong Lin 
1269923c5e61SMengdong Lin 	return 0;
1270923c5e61SMengdong Lin }
1271923c5e61SMengdong Lin 
1272f8f80361SMengdong Lin /**
1273f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1274f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1275f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1276f8f80361SMengdong Lin  *
1277f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1278f8f80361SMengdong Lin  *
1279f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1280f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1281f8f80361SMengdong Lin  * DAI links in dai_link array.
1282f8f80361SMengdong Lin  */
1283f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1284f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1285f8f80361SMengdong Lin {
1286f8f80361SMengdong Lin 	if (dai_link->dobj.type
1287f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1288f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1289f8f80361SMengdong Lin 			dai_link->dobj.type);
1290f8f80361SMengdong Lin 		return -EINVAL;
1291f8f80361SMengdong Lin 	}
1292f8f80361SMengdong Lin 
1293f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1294d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1295d6f220eaSMengdong Lin 	 * on the link created by topology.
1296d6f220eaSMengdong Lin 	 */
1297d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1298d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1299d6f220eaSMengdong Lin 
1300f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1301f8f80361SMengdong Lin 	card->num_dai_links++;
1302f8f80361SMengdong Lin 
1303f8f80361SMengdong Lin 	return 0;
1304f8f80361SMengdong Lin }
1305f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1306f8f80361SMengdong Lin 
1307f8f80361SMengdong Lin /**
1308f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1309f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1310f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1311f8f80361SMengdong Lin  *
1312f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1313f8f80361SMengdong Lin  *
1314f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1315f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1316f8f80361SMengdong Lin  */
1317f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1318f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1319f8f80361SMengdong Lin {
1320f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1321f8f80361SMengdong Lin 
1322f8f80361SMengdong Lin 	if (dai_link->dobj.type
1323f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1324f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1325f8f80361SMengdong Lin 			dai_link->dobj.type);
1326f8f80361SMengdong Lin 		return;
1327f8f80361SMengdong Lin 	}
1328f8f80361SMengdong Lin 
1329f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1330d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1331d6f220eaSMengdong Lin 	 * on the link created by topology.
1332d6f220eaSMengdong Lin 	 */
1333d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1334d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1335d6f220eaSMengdong Lin 
1336f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1337f8f80361SMengdong Lin 		if (link == dai_link) {
1338f8f80361SMengdong Lin 			list_del(&link->list);
1339f8f80361SMengdong Lin 			card->num_dai_links--;
1340f8f80361SMengdong Lin 			return;
1341f8f80361SMengdong Lin 		}
1342f8f80361SMengdong Lin 	}
1343f8f80361SMengdong Lin }
1344f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1345f0fba2adSLiam Girdwood 
1346ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
134794f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1348ead9b919SJarkko Nikula {
1349ead9b919SJarkko Nikula 	int i;
1350ead9b919SJarkko Nikula 
1351ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1352ead9b919SJarkko Nikula 		return;
1353ead9b919SJarkko Nikula 
1354ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1355ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
135694f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
13573ca041edSSebastian Reichel 			continue;
135894f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
13593ca041edSSebastian Reichel 			continue;
136094f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1361ead9b919SJarkko Nikula 		break;
1362ead9b919SJarkko Nikula 	}
1363ead9b919SJarkko Nikula }
1364ead9b919SJarkko Nikula 
1365f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1366f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1367589c3563SJarkko Nikula {
1368f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1369888df395SMark Brown 	struct snd_soc_dai *dai;
1370f1d45cc3SLars-Peter Clausen 	int ret;
1371589c3563SJarkko Nikula 
13721b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
137370090bbbSLars-Peter Clausen 		return 0;
1374589c3563SJarkko Nikula 
1375abd31b32SLars-Peter Clausen 	if (component->card) {
13761b7c1231SLars-Peter Clausen 		if (component->card != card) {
13771b7c1231SLars-Peter Clausen 			dev_err(component->dev,
13781b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
13791b7c1231SLars-Peter Clausen 				card->name, component->card->name);
13801b7c1231SLars-Peter Clausen 			return -ENODEV;
13811b7c1231SLars-Peter Clausen 		}
13821b7c1231SLars-Peter Clausen 		return 0;
13831b7c1231SLars-Peter Clausen 	}
13841b7c1231SLars-Peter Clausen 
1385abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1386abd31b32SLars-Peter Clausen 		return -ENODEV;
1387abd31b32SLars-Peter Clausen 
1388f1d45cc3SLars-Peter Clausen 	component->card = card;
1389f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1390f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1391589c3563SJarkko Nikula 
1392f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1393d5d1e0beSLars-Peter Clausen 
1394f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1395f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1396f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
139777530150SLars-Peter Clausen 
1398b318ad50SNariman Poushin 		if (ret != 0) {
1399f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1400b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1401b318ad50SNariman Poushin 			goto err_probe;
1402b318ad50SNariman Poushin 		}
1403b318ad50SNariman Poushin 	}
1404b318ad50SNariman Poushin 
14050634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
14060634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1407261edc70SNariman Poushin 		if (ret != 0) {
1408f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1409261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1410261edc70SNariman Poushin 			goto err_probe;
1411261edc70SNariman Poushin 		}
1412261edc70SNariman Poushin 	}
1413888df395SMark Brown 
1414f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1415f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1416589c3563SJarkko Nikula 		if (ret < 0) {
1417f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1418f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
141970d29331SJarkko Nikula 			goto err_probe;
1420589c3563SJarkko Nikula 		}
1421cb2cf612SLiam Girdwood 
1422f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1423f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1424956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1425f1d45cc3SLars-Peter Clausen 			component->name);
1426956245e9SLiam Girdwood 	}
1427956245e9SLiam Girdwood 
1428f2ed6b07SMengdong Lin 	/* machine specific init */
1429f2ed6b07SMengdong Lin 	if (component->init) {
1430f2ed6b07SMengdong Lin 		ret = component->init(component);
1431f2ed6b07SMengdong Lin 		if (ret < 0) {
1432f2ed6b07SMengdong Lin 			dev_err(component->dev,
1433f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1434f2ed6b07SMengdong Lin 			goto err_probe;
1435f2ed6b07SMengdong Lin 		}
1436f2ed6b07SMengdong Lin 	}
1437f2ed6b07SMengdong Lin 
1438f1d45cc3SLars-Peter Clausen 	if (component->controls)
1439f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1440f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1441f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1442f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1443f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1444956245e9SLiam Girdwood 
1445f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1446f1d45cc3SLars-Peter Clausen 
1447f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1448f1d45cc3SLars-Peter Clausen 	if (component->codec)
1449f1d45cc3SLars-Peter Clausen 		list_add(&component->codec->card_list, &card->codec_dev_list);
1450956245e9SLiam Girdwood 
1451956245e9SLiam Girdwood 	return 0;
1452956245e9SLiam Girdwood 
1453956245e9SLiam Girdwood err_probe:
1454f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1455abd31b32SLars-Peter Clausen 	component->card = NULL;
1456f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1457956245e9SLiam Girdwood 
1458956245e9SLiam Girdwood 	return ret;
1459956245e9SLiam Girdwood }
1460956245e9SLiam Girdwood 
146136ae1a96SMark Brown static void rtd_release(struct device *dev)
146236ae1a96SMark Brown {
146336ae1a96SMark Brown 	kfree(dev);
146436ae1a96SMark Brown }
1465f0fba2adSLiam Girdwood 
14665f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
14675f3484acSLars-Peter Clausen 	const char *name)
1468503ae5e0SMisael Lopez Cruz {
1469589c3563SJarkko Nikula 	int ret = 0;
1470589c3563SJarkko Nikula 
1471589c3563SJarkko Nikula 	/* register the rtd device */
147236ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
147336ae1a96SMark Brown 	if (!rtd->dev)
147436ae1a96SMark Brown 		return -ENOMEM;
147536ae1a96SMark Brown 	device_initialize(rtd->dev);
14765f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
147736ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1478d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1479f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
148036ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1481b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
148201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
148301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
148401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
148501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
148636ae1a96SMark Brown 	ret = device_add(rtd->dev);
1487589c3563SJarkko Nikula 	if (ret < 0) {
1488865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1489865df9cbSChuansheng Liu 		put_device(rtd->dev);
14905f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1491f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1492589c3563SJarkko Nikula 		return ret;
1493589c3563SJarkko Nikula 	}
1494589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1495589c3563SJarkko Nikula 	return 0;
1496589c3563SJarkko Nikula }
1497589c3563SJarkko Nikula 
14981a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
14991a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
150062ae68faSStephen Warren 				     int order)
150162ae68faSStephen Warren {
150262ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1503f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
150488bd870fSBenoit Cousson 	int i, ret;
150562ae68faSStephen Warren 
150662ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
150761aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
150870090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1509f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
151062ae68faSStephen Warren 		if (ret < 0)
151162ae68faSStephen Warren 			return ret;
151262ae68faSStephen Warren 	}
151362ae68faSStephen Warren 
151488bd870fSBenoit Cousson 	/* probe the CODEC-side components */
151588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
151661aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
151770090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1518f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
151962ae68faSStephen Warren 			if (ret < 0)
152062ae68faSStephen Warren 				return ret;
152162ae68faSStephen Warren 		}
152288bd870fSBenoit Cousson 	}
152362ae68faSStephen Warren 
152462ae68faSStephen Warren 	/* probe the platform */
152570090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1526f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
152762ae68faSStephen Warren 		if (ret < 0)
152862ae68faSStephen Warren 			return ret;
152962ae68faSStephen Warren 	}
153062ae68faSStephen Warren 
153162ae68faSStephen Warren 	return 0;
153262ae68faSStephen Warren }
153362ae68faSStephen Warren 
15348e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1535b0aa88afSMisael Lopez Cruz {
1536b0aa88afSMisael Lopez Cruz 	int ret;
1537b0aa88afSMisael Lopez Cruz 
15388e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
15398e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
15408e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1541b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
15428e2be562SLars-Peter Clausen 				dev_err(dai->dev,
15438e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
15448e2be562SLars-Peter Clausen 					dai->name, ret);
1545b0aa88afSMisael Lopez Cruz 				return ret;
1546b0aa88afSMisael Lopez Cruz 			}
1547b0aa88afSMisael Lopez Cruz 		}
1548b0aa88afSMisael Lopez Cruz 
15498e2be562SLars-Peter Clausen 		dai->probed = 1;
1550b0aa88afSMisael Lopez Cruz 	}
1551b0aa88afSMisael Lopez Cruz 
1552b0aa88afSMisael Lopez Cruz 	return 0;
1553b0aa88afSMisael Lopez Cruz }
1554b0aa88afSMisael Lopez Cruz 
15552436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
15562436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
15573f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
15582436a723SMisael Lopez Cruz {
15593f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
15603f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
15613de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
15622436a723SMisael Lopez Cruz 	int ret;
15632436a723SMisael Lopez Cruz 
156488bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
156588bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
156688bd870fSBenoit Cousson 
15672436a723SMisael Lopez Cruz 	/* link the DAI widgets */
15683de7c420SVinod Koul 	sink = codec_dai->playback_widget;
15693de7c420SVinod Koul 	source = cpu_dai->capture_widget;
15703de7c420SVinod Koul 	if (sink && source) {
15712436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
15723de7c420SVinod Koul 					   dai_link->num_params,
15733de7c420SVinod Koul 					   source, sink);
15742436a723SMisael Lopez Cruz 		if (ret != 0) {
15752436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
15763de7c420SVinod Koul 				sink->name, source->name, ret);
15772436a723SMisael Lopez Cruz 			return ret;
15782436a723SMisael Lopez Cruz 		}
15792436a723SMisael Lopez Cruz 	}
15802436a723SMisael Lopez Cruz 
15813de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
15823de7c420SVinod Koul 	source = codec_dai->capture_widget;
15833de7c420SVinod Koul 	if (sink && source) {
15842436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
15853de7c420SVinod Koul 					   dai_link->num_params,
15863de7c420SVinod Koul 					   source, sink);
15872436a723SMisael Lopez Cruz 		if (ret != 0) {
15882436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
15893de7c420SVinod Koul 				sink->name, source->name, ret);
15902436a723SMisael Lopez Cruz 			return ret;
15912436a723SMisael Lopez Cruz 		}
15922436a723SMisael Lopez Cruz 	}
15932436a723SMisael Lopez Cruz 
15942436a723SMisael Lopez Cruz 	return 0;
15952436a723SMisael Lopez Cruz }
15962436a723SMisael Lopez Cruz 
15971a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
15981a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1599f0fba2adSLiam Girdwood {
16001a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1601c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
160288bd870fSBenoit Cousson 	int i, ret;
1603f0fba2adSLiam Girdwood 
1604f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
16051a497983SMengdong Lin 			card->name, rtd->num, order);
1606f0fba2adSLiam Girdwood 
1607f0fba2adSLiam Girdwood 	/* set default power off timeout */
1608f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1609f0fba2adSLiam Girdwood 
16108e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
16118e2be562SLars-Peter Clausen 	if (ret)
1612f0fba2adSLiam Girdwood 		return ret;
1613f0fba2adSLiam Girdwood 
1614f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
161588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
16168e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1617b0aa88afSMisael Lopez Cruz 		if (ret)
1618f0fba2adSLiam Girdwood 			return ret;
161988bd870fSBenoit Cousson 	}
1620f0fba2adSLiam Girdwood 
16210168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
16220168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
16230168bf0dSLiam Girdwood 		return 0;
16240168bf0dSLiam Girdwood 
16255f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16265f3484acSLars-Peter Clausen 	if (dai_link->init) {
16275f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
16285f3484acSLars-Peter Clausen 		if (ret < 0) {
16295f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16305f3484acSLars-Peter Clausen 				dai_link->name, ret);
16315f3484acSLars-Peter Clausen 			return ret;
16325f3484acSLars-Peter Clausen 		}
16335f3484acSLars-Peter Clausen 	}
16345f3484acSLars-Peter Clausen 
1635a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1636a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1637a5053a8eSKuninori Morimoto 
16385f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1639589c3563SJarkko Nikula 	if (ret)
1640f0fba2adSLiam Girdwood 		return ret;
1641f0fba2adSLiam Girdwood 
16425f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
16435f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
16442e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
16452e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
16465f3484acSLars-Peter Clausen #endif
16475f3484acSLars-Peter Clausen 
16486f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
16491245b700SNamarta Kohli 		/*create compress_device"*/
16501a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
16511245b700SNamarta Kohli 		if (ret < 0) {
1652f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
16531245b700SNamarta Kohli 					 dai_link->stream_name);
16541245b700SNamarta Kohli 			return ret;
16551245b700SNamarta Kohli 		}
16561245b700SNamarta Kohli 	} else {
16571245b700SNamarta Kohli 
1658c74184edSMark Brown 		if (!dai_link->params) {
1659f0fba2adSLiam Girdwood 			/* create the pcm */
16601a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1661f0fba2adSLiam Girdwood 			if (ret < 0) {
1662f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
16630837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1664f0fba2adSLiam Girdwood 				return ret;
1665f0fba2adSLiam Girdwood 			}
1666c74184edSMark Brown 		} else {
16679d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
16689d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
16699d58a077SRichard Fitzgerald 
1670c74184edSMark Brown 			/* link the DAI widgets */
16713f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
16722436a723SMisael Lopez Cruz 			if (ret)
1673c74184edSMark Brown 				return ret;
1674c74184edSMark Brown 		}
1675c74184edSMark Brown 	}
1676c74184edSMark Brown 
1677f0fba2adSLiam Girdwood 	return 0;
1678f0fba2adSLiam Girdwood }
1679f0fba2adSLiam Girdwood 
168044c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1681b19e6e7bSMark Brown {
16823ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1683f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1684f2ed6b07SMengdong Lin 	const char *name;
1685f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
16863ca041edSSebastian Reichel 
1687f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1688f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1689f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1690f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1691f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1692f2ed6b07SMengdong Lin 		if (!component) {
1693f2ed6b07SMengdong Lin 			if (codec_of_node)
1694f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1695f2ed6b07SMengdong Lin 			goto err_defer;
1696f2ed6b07SMengdong Lin 		}
1697f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1698f2ed6b07SMengdong Lin 		/* generic components */
1699f2ed6b07SMengdong Lin 		name = aux_dev->name;
1700f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1701f2ed6b07SMengdong Lin 		if (!component)
1702f2ed6b07SMengdong Lin 			goto err_defer;
1703f2ed6b07SMengdong Lin 	} else {
1704f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1705f2ed6b07SMengdong Lin 		return -EINVAL;
1706f2ed6b07SMengdong Lin 	}
17073ca041edSSebastian Reichel 
1708f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1709f2ed6b07SMengdong Lin 	list_add(&component->list_aux, &card->aux_comp_list);
1710f2ed6b07SMengdong Lin 	return 0;
1711f2ed6b07SMengdong Lin 
1712f2ed6b07SMengdong Lin err_defer:
171365d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1714b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1715b19e6e7bSMark Brown }
1716b19e6e7bSMark Brown 
1717f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1718f2ed6b07SMengdong Lin {
1719f2ed6b07SMengdong Lin 	struct snd_soc_component *comp;
1720f2ed6b07SMengdong Lin 	int order;
1721f2ed6b07SMengdong Lin 	int ret;
1722f2ed6b07SMengdong Lin 
1723f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1724f2ed6b07SMengdong Lin 		order++) {
1725f2ed6b07SMengdong Lin 		list_for_each_entry(comp, &card->aux_comp_list, list_aux) {
1726f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1727f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1728f2ed6b07SMengdong Lin 				if (ret < 0) {
1729f2ed6b07SMengdong Lin 					dev_err(card->dev,
1730f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1731f2ed6b07SMengdong Lin 						comp->name, ret);
1732f2ed6b07SMengdong Lin 					return ret;
1733f2ed6b07SMengdong Lin 				}
1734f2ed6b07SMengdong Lin 			}
1735f2ed6b07SMengdong Lin 		}
1736f2ed6b07SMengdong Lin 	}
173765d9361fSLars-Peter Clausen 
173844c69bb1SLars-Peter Clausen 	return 0;
17393ca041edSSebastian Reichel }
17402eea392dSJarkko Nikula 
1741f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
174244c69bb1SLars-Peter Clausen {
1743f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1744f2ed6b07SMengdong Lin 	int order;
174544c69bb1SLars-Peter Clausen 
1746f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1747f2ed6b07SMengdong Lin 		order++) {
1748f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1749f2ed6b07SMengdong Lin 			&card->aux_comp_list, list_aux) {
1750f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1751f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1752f2ed6b07SMengdong Lin 				/* remove it from the card's aux_comp_list */
1753f2ed6b07SMengdong Lin 				list_del(&comp->list_aux);
17542eea392dSJarkko Nikula 			}
17555f3484acSLars-Peter Clausen 		}
17562eea392dSJarkko Nikula 	}
17572eea392dSJarkko Nikula }
17582eea392dSJarkko Nikula 
1759f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1760fdf0f54dSDimitris Papastamos {
1761fdf0f54dSDimitris Papastamos 	int ret;
1762fdf0f54dSDimitris Papastamos 
1763fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1764fdf0f54dSDimitris Papastamos 		return 0;
1765fdf0f54dSDimitris Papastamos 
1766fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1767fdf0f54dSDimitris Papastamos 	if (ret < 0) {
176810e8aa9aSMichał Mirosław 		dev_err(codec->dev,
176910e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
177010e8aa9aSMichał Mirosław 			ret);
1771fdf0f54dSDimitris Papastamos 		return ret;
1772fdf0f54dSDimitris Papastamos 	}
1773fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1774fdf0f54dSDimitris Papastamos 	return 0;
1775fdf0f54dSDimitris Papastamos }
1776fdf0f54dSDimitris Papastamos 
1777ce64c8b9SLars-Peter Clausen /**
1778ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1779ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1780ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1781ce64c8b9SLars-Peter Clausen  *
1782ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1783ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1784ce64c8b9SLars-Peter Clausen  *
1785ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1786ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1787ce64c8b9SLars-Peter Clausen  *
1788ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1789ce64c8b9SLars-Peter Clausen  */
1790ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1791ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1792ce64c8b9SLars-Peter Clausen {
1793ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1794ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1795ce64c8b9SLars-Peter Clausen 	unsigned int i;
1796ce64c8b9SLars-Peter Clausen 	int ret;
1797ce64c8b9SLars-Peter Clausen 
1798ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1799ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1800ce64c8b9SLars-Peter Clausen 
1801ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1802ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1803ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1804ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1805ce64c8b9SLars-Peter Clausen 			return ret;
1806ce64c8b9SLars-Peter Clausen 		}
1807ce64c8b9SLars-Peter Clausen 	}
1808ce64c8b9SLars-Peter Clausen 
1809ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1810ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1811ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1812ce64c8b9SLars-Peter Clausen 
1813ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1814ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1815ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1816ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1817ce64c8b9SLars-Peter Clausen 			break;
1818ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1819ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1820ce64c8b9SLars-Peter Clausen 			break;
1821ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1822ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1823ce64c8b9SLars-Peter Clausen 			break;
1824ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1825ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1826ce64c8b9SLars-Peter Clausen 			break;
1827ce64c8b9SLars-Peter Clausen 		}
1828ce64c8b9SLars-Peter Clausen 
1829ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1830ce64c8b9SLars-Peter Clausen 	}
1831ce64c8b9SLars-Peter Clausen 
1832ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1833ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1834ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1835ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1836ce64c8b9SLars-Peter Clausen 		return ret;
1837ce64c8b9SLars-Peter Clausen 	}
1838ce64c8b9SLars-Peter Clausen 
1839ce64c8b9SLars-Peter Clausen 	return 0;
1840ce64c8b9SLars-Peter Clausen }
1841ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1842ce64c8b9SLars-Peter Clausen 
1843b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1844f0fba2adSLiam Girdwood {
1845fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
18461a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
184761b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1848ce64c8b9SLars-Peter Clausen 	int ret, i, order;
184996dd3622SMark Brown 
185034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
185101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1852f0fba2adSLiam Girdwood 
1853b19e6e7bSMark Brown 	/* bind DAIs */
1854b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
18556f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
1856b19e6e7bSMark Brown 		if (ret != 0)
1857b19e6e7bSMark Brown 			goto base_error;
1858db2a4165SFrank Mandarino 	}
1859db2a4165SFrank Mandarino 
186044c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1861b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
186244c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1863b19e6e7bSMark Brown 		if (ret != 0)
1864b19e6e7bSMark Brown 			goto base_error;
1865db2a4165SFrank Mandarino 	}
1866db2a4165SFrank Mandarino 
1867f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1868f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
1869f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
1870f8f80361SMengdong Lin 
1871fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1872fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1873fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1874fdf0f54dSDimitris Papastamos 			continue;
1875f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1876b19e6e7bSMark Brown 		if (ret < 0)
1877b19e6e7bSMark Brown 			goto base_error;
1878fdf0f54dSDimitris Papastamos 	}
1879fdf0f54dSDimitris Papastamos 
1880f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1881102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1882f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1883f0fba2adSLiam Girdwood 	if (ret < 0) {
188410e8aa9aSMichał Mirosław 		dev_err(card->dev,
188510e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
188610e8aa9aSMichał Mirosław 			card->name, ret);
1887b19e6e7bSMark Brown 		goto base_error;
1888db2a4165SFrank Mandarino 	}
1889db2a4165SFrank Mandarino 
18900757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
18910757d834SLars-Peter Clausen 
1892e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1893e37a4970SMark Brown 	card->dapm.dev = card->dev;
1894e37a4970SMark Brown 	card->dapm.card = card;
1895e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1896e37a4970SMark Brown 
1897d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1898d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1899d5d1e0beSLars-Peter Clausen #endif
1900d5d1e0beSLars-Peter Clausen 
190188ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
19026ed25978SAndy Green 	/* deferred resume work */
19036308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
19041301a964SRandy Dunlap #endif
19056ed25978SAndy Green 
19069a841ebbSMark Brown 	if (card->dapm_widgets)
19079a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19089a841ebbSMark Brown 					  card->num_dapm_widgets);
19099a841ebbSMark Brown 
1910f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
1911f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1912f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
1913f23e860eSNicolin Chen 
1914f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1915f0fba2adSLiam Girdwood 	if (card->probe) {
1916e7361ec4SMark Brown 		ret = card->probe(card);
1917f0fba2adSLiam Girdwood 		if (ret < 0)
1918f0fba2adSLiam Girdwood 			goto card_probe_error;
1919f0fba2adSLiam Girdwood 	}
1920f0fba2adSLiam Girdwood 
192162ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
19220168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
19230168bf0dSLiam Girdwood 			order++) {
19241a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
19251a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
192662ae68faSStephen Warren 			if (ret < 0) {
1927f110bfc7SLiam Girdwood 				dev_err(card->dev,
1928f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1929f110bfc7SLiam Girdwood 					ret);
193062ae68faSStephen Warren 				goto probe_dai_err;
193162ae68faSStephen Warren 			}
193262ae68faSStephen Warren 		}
193362ae68faSStephen Warren 	}
193462ae68faSStephen Warren 
1935f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1936f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
1937f2ed6b07SMengdong Lin 	if (ret < 0)
1938f2ed6b07SMengdong Lin 		goto probe_dai_err;
1939f2ed6b07SMengdong Lin 
194061b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
194161b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
194261b0088bSMengdong Lin 	 */
194361b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
194461b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
194561b0088bSMengdong Lin 			continue;
194661b0088bSMengdong Lin 
194761b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
194861b0088bSMengdong Lin 		if (ret)
194961b0088bSMengdong Lin 			goto probe_dai_err;
195061b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
195161b0088bSMengdong Lin 		if (ret)
195261b0088bSMengdong Lin 			goto probe_dai_err;
195361b0088bSMengdong Lin 	}
195461b0088bSMengdong Lin 
195562ae68faSStephen Warren 	/* probe all DAI links on this card */
195662ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
195762ae68faSStephen Warren 			order++) {
19581a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
19591a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
1960fe3e78e0SMark Brown 			if (ret < 0) {
1961f110bfc7SLiam Girdwood 				dev_err(card->dev,
1962f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1963f110bfc7SLiam Girdwood 					ret);
1964f0fba2adSLiam Girdwood 				goto probe_dai_err;
1965fe3e78e0SMark Brown 			}
1966fe3e78e0SMark Brown 		}
19670168bf0dSLiam Girdwood 	}
1968fe3e78e0SMark Brown 
1969888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1970b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1971888df395SMark Brown 
1972b7af1dafSMark Brown 	if (card->controls)
1973022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1974b7af1dafSMark Brown 
1975b8ad29deSMark Brown 	if (card->dapm_routes)
1976b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1977b8ad29deSMark Brown 					card->num_dapm_routes);
1978b8ad29deSMark Brown 
1979f23e860eSNicolin Chen 	if (card->of_dapm_routes)
1980f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1981f23e860eSNicolin Chen 					card->num_of_dapm_routes);
198275d9ac46SMark Brown 
1983f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1984fe3e78e0SMark Brown 		 "%s", card->name);
1985f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
198622de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1987f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1988f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1989f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1990f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1991f0e8ed85SMark Brown 		case '_':
1992f0e8ed85SMark Brown 		case '-':
1993f0e8ed85SMark Brown 		case '\0':
1994f0e8ed85SMark Brown 			break;
1995f0e8ed85SMark Brown 		default:
1996f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1997f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1998f0e8ed85SMark Brown 			break;
1999f0e8ed85SMark Brown 		}
2000f0e8ed85SMark Brown 	}
2001fe3e78e0SMark Brown 
200228e9ad92SMark Brown 	if (card->late_probe) {
200328e9ad92SMark Brown 		ret = card->late_probe(card);
200428e9ad92SMark Brown 		if (ret < 0) {
2005f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
200628e9ad92SMark Brown 				card->name, ret);
200728e9ad92SMark Brown 			goto probe_aux_dev_err;
200828e9ad92SMark Brown 		}
200928e9ad92SMark Brown 	}
201028e9ad92SMark Brown 
2011824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20128c193b8dSLars-Peter Clausen 
2013f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2014fe3e78e0SMark Brown 	if (ret < 0) {
2015f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2016f110bfc7SLiam Girdwood 				ret);
20176b3ed785SAxel Lin 		goto probe_aux_dev_err;
2018fe3e78e0SMark Brown 	}
2019fe3e78e0SMark Brown 
2020435c5e25SMark Brown 	card->instantiated = 1;
20214f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2022f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
202334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2024b19e6e7bSMark Brown 
2025b19e6e7bSMark Brown 	return 0;
2026db2a4165SFrank Mandarino 
20272eea392dSJarkko Nikula probe_aux_dev_err:
2028f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
20292eea392dSJarkko Nikula 
2030f0fba2adSLiam Girdwood probe_dai_err:
20310671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2032fe3e78e0SMark Brown 
2033f0fba2adSLiam Girdwood card_probe_error:
203487506549SMark Brown 	if (card->remove)
2035e7361ec4SMark Brown 		card->remove(card);
2036f0fba2adSLiam Girdwood 
20372210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
20380757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2039f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2040f0fba2adSLiam Girdwood 
2041b19e6e7bSMark Brown base_error:
20421a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2043f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
204434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2045db2a4165SFrank Mandarino 
2046b19e6e7bSMark Brown 	return ret;
2047435c5e25SMark Brown }
2048435c5e25SMark Brown 
2049435c5e25SMark Brown /* probes a new socdev */
2050435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2051435c5e25SMark Brown {
2052f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2053435c5e25SMark Brown 
205470a7ca34SVinod Koul 	/*
205570a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
205670a7ca34SVinod Koul 	 * we should not be here in that case so ret error
205770a7ca34SVinod Koul 	 */
205870a7ca34SVinod Koul 	if (!card)
205970a7ca34SVinod Koul 		return -EINVAL;
206070a7ca34SVinod Koul 
2061fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2062f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2063fe4085e8SMark Brown 		 card->name);
2064fe4085e8SMark Brown 
2065435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2066435c5e25SMark Brown 	card->dev = &pdev->dev;
2067f0fba2adSLiam Girdwood 
206828d528c8SMark Brown 	return snd_soc_register_card(card);
2069435c5e25SMark Brown }
2070435c5e25SMark Brown 
2071b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2072db2a4165SFrank Mandarino {
20731a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2074db2a4165SFrank Mandarino 
2075f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
20761a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
207743829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2078db2a4165SFrank Mandarino 
2079f0fba2adSLiam Girdwood 	/* remove and free each DAI */
20800671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
20811a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2082f0fba2adSLiam Girdwood 
2083f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2084f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2085f2ed6b07SMengdong Lin 
2086d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2087a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2088a6052154SJarkko Nikula 
2089f0fba2adSLiam Girdwood 	/* remove the card */
209087506549SMark Brown 	if (card->remove)
2091e7361ec4SMark Brown 		card->remove(card);
2092f0fba2adSLiam Girdwood 
2093f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2094b0e26485SVinod Koul 	return 0;
2095b0e26485SVinod Koul 
2096b2dfa62cSGuennadi Liakhovetski }
2097b0e26485SVinod Koul 
2098b0e26485SVinod Koul /* removes a socdev */
2099b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2100b0e26485SVinod Koul {
2101b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2102b0e26485SVinod Koul 
2103c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2104db2a4165SFrank Mandarino 	return 0;
2105db2a4165SFrank Mandarino }
2106db2a4165SFrank Mandarino 
21076f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
210851737470SMark Brown {
21096f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21101a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
211151737470SMark Brown 
211251737470SMark Brown 	if (!card->instantiated)
2113416356fcSMark Brown 		return 0;
211451737470SMark Brown 
211551737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
211651737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
21171a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
211843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
211951737470SMark Brown 
2120f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2121416356fcSMark Brown 
2122988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
21231a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
212488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21251a497983SMengdong Lin 		int i;
212688bd870fSBenoit Cousson 
2127988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21281a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
21291a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
213088bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
213188bd870fSBenoit Cousson 		}
2132988e8cc4SNicolin Chen 	}
2133988e8cc4SNicolin Chen 
2134416356fcSMark Brown 	return 0;
213551737470SMark Brown }
21366f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
213751737470SMark Brown 
21386f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2139b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2140b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2141b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2142b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21436f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2144b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2145416356fcSMark Brown };
2146deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2147416356fcSMark Brown 
2148db2a4165SFrank Mandarino /* ASoC platform driver */
2149db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2150db2a4165SFrank Mandarino 	.driver		= {
2151db2a4165SFrank Mandarino 		.name		= "soc-audio",
21526f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2153db2a4165SFrank Mandarino 	},
2154db2a4165SFrank Mandarino 	.probe		= soc_probe,
2155db2a4165SFrank Mandarino 	.remove		= soc_remove,
2156db2a4165SFrank Mandarino };
2157db2a4165SFrank Mandarino 
2158096e49d5SMark Brown /**
2159db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2160db2a4165SFrank Mandarino  * @_template: control template
2161db2a4165SFrank Mandarino  * @data: control private data
2162ac11a2b3SMark Brown  * @long_name: control long name
2163efb7ac3fSMark Brown  * @prefix: control name prefix
2164db2a4165SFrank Mandarino  *
2165db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2166db2a4165SFrank Mandarino  *
2167db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2168db2a4165SFrank Mandarino  */
2169db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21703056557fSMark Brown 				  void *data, const char *long_name,
2171efb7ac3fSMark Brown 				  const char *prefix)
2172db2a4165SFrank Mandarino {
2173db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2174efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2175efb7ac3fSMark Brown 	char *name = NULL;
2176db2a4165SFrank Mandarino 
2177db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2178db2a4165SFrank Mandarino 	template.index = 0;
2179db2a4165SFrank Mandarino 
2180efb7ac3fSMark Brown 	if (!long_name)
2181efb7ac3fSMark Brown 		long_name = template.name;
2182efb7ac3fSMark Brown 
2183efb7ac3fSMark Brown 	if (prefix) {
21842b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2185efb7ac3fSMark Brown 		if (!name)
2186efb7ac3fSMark Brown 			return NULL;
2187efb7ac3fSMark Brown 
2188efb7ac3fSMark Brown 		template.name = name;
2189efb7ac3fSMark Brown 	} else {
2190efb7ac3fSMark Brown 		template.name = long_name;
2191efb7ac3fSMark Brown 	}
2192efb7ac3fSMark Brown 
2193efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2194efb7ac3fSMark Brown 
2195efb7ac3fSMark Brown 	kfree(name);
2196efb7ac3fSMark Brown 
2197efb7ac3fSMark Brown 	return kcontrol;
2198db2a4165SFrank Mandarino }
2199db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2200db2a4165SFrank Mandarino 
2201022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2202022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2203022658beSLiam Girdwood 	const char *prefix, void *data)
2204022658beSLiam Girdwood {
2205022658beSLiam Girdwood 	int err, i;
2206022658beSLiam Girdwood 
2207022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2208022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2209022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2210022658beSLiam Girdwood 						     control->name, prefix));
2211022658beSLiam Girdwood 		if (err < 0) {
2212f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2213f110bfc7SLiam Girdwood 				control->name, err);
2214022658beSLiam Girdwood 			return err;
2215022658beSLiam Girdwood 		}
2216022658beSLiam Girdwood 	}
2217022658beSLiam Girdwood 
2218022658beSLiam Girdwood 	return 0;
2219022658beSLiam Girdwood }
2220022658beSLiam Girdwood 
22214fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22224fefd698SDimitris Papastamos 					       const char *name)
22234fefd698SDimitris Papastamos {
22244fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22254fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22264fefd698SDimitris Papastamos 
22274fefd698SDimitris Papastamos 	if (unlikely(!name))
22284fefd698SDimitris Papastamos 		return NULL;
22294fefd698SDimitris Papastamos 
22304fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22314fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22324fefd698SDimitris Papastamos 			return kctl;
22334fefd698SDimitris Papastamos 	return NULL;
22344fefd698SDimitris Papastamos }
22354fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22364fefd698SDimitris Papastamos 
2237db2a4165SFrank Mandarino /**
22380f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22390f2780adSLars-Peter Clausen  *
22400f2780adSLars-Peter Clausen  * @component: Component to add controls to
22410f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22420f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22430f2780adSLars-Peter Clausen  *
22440f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22450f2780adSLars-Peter Clausen  */
22460f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22470f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22480f2780adSLars-Peter Clausen {
22490f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22500f2780adSLars-Peter Clausen 
22510f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22520f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22530f2780adSLars-Peter Clausen }
22540f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22550f2780adSLars-Peter Clausen 
22560f2780adSLars-Peter Clausen /**
2257022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2258022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
22593e8e1952SIan Molton  * duplicating this code.
22603e8e1952SIan Molton  *
22613e8e1952SIan Molton  * @codec: codec to add controls to
22623e8e1952SIan Molton  * @controls: array of controls to add
22633e8e1952SIan Molton  * @num_controls: number of elements in the array
22643e8e1952SIan Molton  *
22653e8e1952SIan Molton  * Return 0 for success, else error.
22663e8e1952SIan Molton  */
2267022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
22680f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22693e8e1952SIan Molton {
22700f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
22710f2780adSLars-Peter Clausen 		num_controls);
22723e8e1952SIan Molton }
2273022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
22743e8e1952SIan Molton 
22753e8e1952SIan Molton /**
2276a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2277022658beSLiam Girdwood  * Convenience function to add a list of controls.
2278a491a5c8SLiam Girdwood  *
2279a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2280a491a5c8SLiam Girdwood  * @controls: array of controls to add
2281a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2282a491a5c8SLiam Girdwood  *
2283a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2284a491a5c8SLiam Girdwood  */
2285a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
22860f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2287a491a5c8SLiam Girdwood {
22880f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
22890f2780adSLars-Peter Clausen 		num_controls);
2290a491a5c8SLiam Girdwood }
2291a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2292a491a5c8SLiam Girdwood 
2293a491a5c8SLiam Girdwood /**
2294022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2295022658beSLiam Girdwood  * Convenience function to add a list of controls.
2296022658beSLiam Girdwood  *
2297022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2298022658beSLiam Girdwood  * @controls: array of controls to add
2299022658beSLiam Girdwood  * @num_controls: number of elements in the array
2300022658beSLiam Girdwood  *
2301022658beSLiam Girdwood  * Return 0 for success, else error.
2302022658beSLiam Girdwood  */
2303022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2304022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2305022658beSLiam Girdwood {
2306022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2307022658beSLiam Girdwood 
2308022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2309022658beSLiam Girdwood 			NULL, soc_card);
2310022658beSLiam Girdwood }
2311022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2312022658beSLiam Girdwood 
2313022658beSLiam Girdwood /**
2314022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2315022658beSLiam Girdwood  * Convienience function to add a list of controls.
2316022658beSLiam Girdwood  *
2317022658beSLiam Girdwood  * @dai: DAI to add controls to
2318022658beSLiam Girdwood  * @controls: array of controls to add
2319022658beSLiam Girdwood  * @num_controls: number of elements in the array
2320022658beSLiam Girdwood  *
2321022658beSLiam Girdwood  * Return 0 for success, else error.
2322022658beSLiam Girdwood  */
2323022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2324022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2325022658beSLiam Girdwood {
2326313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2327022658beSLiam Girdwood 
2328022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2329022658beSLiam Girdwood 			NULL, dai);
2330022658beSLiam Girdwood }
2331022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2332022658beSLiam Girdwood 
2333022658beSLiam Girdwood /**
23348c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23358c6529dbSLiam Girdwood  * @dai: DAI
23368c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23378c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23388c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23398c6529dbSLiam Girdwood  *
23408c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
23418c6529dbSLiam Girdwood  */
23428c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
23438c6529dbSLiam Girdwood 	unsigned int freq, int dir)
23448c6529dbSLiam Girdwood {
2345f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2346f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2347ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2348da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2349ec4ee52aSMark Brown 						      freq, dir);
23508c6529dbSLiam Girdwood 	else
23511104a9c8SMark Brown 		return -ENOTSUPP;
23528c6529dbSLiam Girdwood }
23538c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
23548c6529dbSLiam Girdwood 
23558c6529dbSLiam Girdwood /**
2356ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2357ec4ee52aSMark Brown  * @codec: CODEC
2358ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2359da1c6ea6SMark Brown  * @source: Source for the clock
2360ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2361ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2362ec4ee52aSMark Brown  *
2363ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2364ec4ee52aSMark Brown  */
2365ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2366da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2367ec4ee52aSMark Brown {
2368ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2369da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2370da1c6ea6SMark Brown 						 freq, dir);
2371ec4ee52aSMark Brown 	else
23721104a9c8SMark Brown 		return -ENOTSUPP;
2373ec4ee52aSMark Brown }
2374ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2375ec4ee52aSMark Brown 
2376ec4ee52aSMark Brown /**
23778c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
23788c6529dbSLiam Girdwood  * @dai: DAI
2379ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
23808c6529dbSLiam Girdwood  * @div: new clock divisor.
23818c6529dbSLiam Girdwood  *
23828c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
23838c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
23848c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
23858c6529dbSLiam Girdwood  */
23868c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
23878c6529dbSLiam Girdwood 	int div_id, int div)
23888c6529dbSLiam Girdwood {
2389f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2390f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
23918c6529dbSLiam Girdwood 	else
23928c6529dbSLiam Girdwood 		return -EINVAL;
23938c6529dbSLiam Girdwood }
23948c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
23958c6529dbSLiam Girdwood 
23968c6529dbSLiam Girdwood /**
23978c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
23988c6529dbSLiam Girdwood  * @dai: DAI
23998c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
240085488037SMark Brown  * @source: DAI specific source for the PLL
24018c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24028c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24038c6529dbSLiam Girdwood  *
24048c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
24058c6529dbSLiam Girdwood  */
240685488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
240785488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
24088c6529dbSLiam Girdwood {
2409f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2410f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
241185488037SMark Brown 					 freq_in, freq_out);
2412ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2413ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2414ec4ee52aSMark Brown 						   freq_in, freq_out);
24158c6529dbSLiam Girdwood 	else
24168c6529dbSLiam Girdwood 		return -EINVAL;
24178c6529dbSLiam Girdwood }
24188c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24198c6529dbSLiam Girdwood 
2420ec4ee52aSMark Brown /*
2421ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2422ec4ee52aSMark Brown  * @codec: CODEC
2423ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2424ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2425ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2426ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2427ec4ee52aSMark Brown  *
2428ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2429ec4ee52aSMark Brown  */
2430ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2431ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2432ec4ee52aSMark Brown {
2433ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2434ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2435ec4ee52aSMark Brown 					      freq_in, freq_out);
2436ec4ee52aSMark Brown 	else
2437ec4ee52aSMark Brown 		return -EINVAL;
2438ec4ee52aSMark Brown }
2439ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2440ec4ee52aSMark Brown 
24418c6529dbSLiam Girdwood /**
2442e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2443e54cf76bSLiam Girdwood  * @dai: DAI
2444231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2445e54cf76bSLiam Girdwood  *
2446e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2447e54cf76bSLiam Girdwood  */
2448e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2449e54cf76bSLiam Girdwood {
2450e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2451e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2452e54cf76bSLiam Girdwood 	else
2453e54cf76bSLiam Girdwood 		return -EINVAL;
2454e54cf76bSLiam Girdwood }
2455e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2456e54cf76bSLiam Girdwood 
2457e54cf76bSLiam Girdwood /**
24588c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
24598c6529dbSLiam Girdwood  * @dai: DAI
24608c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
24618c6529dbSLiam Girdwood  *
24628c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
24638c6529dbSLiam Girdwood  */
24648c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
24658c6529dbSLiam Girdwood {
24665e4ba569SShawn Guo 	if (dai->driver == NULL)
24678c6529dbSLiam Girdwood 		return -EINVAL;
24685e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
24695e4ba569SShawn Guo 		return -ENOTSUPP;
24705e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
24718c6529dbSLiam Girdwood }
24728c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
24738c6529dbSLiam Girdwood 
24748c6529dbSLiam Girdwood /**
2475e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
247689c67857SXiubo Li  * @slots: Number of slots in use.
247789c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
247889c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
247989c67857SXiubo Li  *
248089c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
248189c67857SXiubo Li  */
2482e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
248389c67857SXiubo Li 					  unsigned int *tx_mask,
248489c67857SXiubo Li 					  unsigned int *rx_mask)
248589c67857SXiubo Li {
248689c67857SXiubo Li 	if (*tx_mask || *rx_mask)
248789c67857SXiubo Li 		return 0;
248889c67857SXiubo Li 
248989c67857SXiubo Li 	if (!slots)
249089c67857SXiubo Li 		return -EINVAL;
249189c67857SXiubo Li 
249289c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
249389c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
249489c67857SXiubo Li 
249589c67857SXiubo Li 	return 0;
249689c67857SXiubo Li }
249789c67857SXiubo Li 
249889c67857SXiubo Li /**
2499e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2500e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2501a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2502a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25038c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2504a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25058c6529dbSLiam Girdwood  *
2506e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2507e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2508e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2509e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2510e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2511e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2512e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2513e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2514e46c9366SLars-Peter Clausen  * channel and so on.
2515e46c9366SLars-Peter Clausen  *
2516e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2517e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2518e46c9366SLars-Peter Clausen  *
2519e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
25208c6529dbSLiam Girdwood  */
25218c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2522a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25238c6529dbSLiam Girdwood {
2524e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2525e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
252689c67857SXiubo Li 						&tx_mask, &rx_mask);
252789c67857SXiubo Li 	else
2528e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
252989c67857SXiubo Li 
253088bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
253188bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
253288bd870fSBenoit Cousson 
2533f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2534f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2535a5479e38SDaniel Ribeiro 				slots, slot_width);
25368c6529dbSLiam Girdwood 	else
2537b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25388c6529dbSLiam Girdwood }
25398c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25408c6529dbSLiam Girdwood 
25418c6529dbSLiam Girdwood /**
2542472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2543472df3cbSBarry Song  * @dai: DAI
2544472df3cbSBarry Song  * @tx_num: how many TX channels
2545472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2546472df3cbSBarry Song  *           0~num-1 uses
2547472df3cbSBarry Song  * @rx_num: how many RX channels
2548472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2549472df3cbSBarry Song  *           0~num-1 uses
2550472df3cbSBarry Song  *
2551472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2552472df3cbSBarry Song  */
2553472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2554472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2555472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2556472df3cbSBarry Song {
2557f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2558f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2559472df3cbSBarry Song 			rx_num, rx_slot);
2560472df3cbSBarry Song 	else
2561472df3cbSBarry Song 		return -EINVAL;
2562472df3cbSBarry Song }
2563472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2564472df3cbSBarry Song 
2565472df3cbSBarry Song /**
25668c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
25678c6529dbSLiam Girdwood  * @dai: DAI
25688c6529dbSLiam Girdwood  * @tristate: tristate enable
25698c6529dbSLiam Girdwood  *
25708c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
25718c6529dbSLiam Girdwood  */
25728c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
25738c6529dbSLiam Girdwood {
2574f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2575f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
25768c6529dbSLiam Girdwood 	else
25778c6529dbSLiam Girdwood 		return -EINVAL;
25788c6529dbSLiam Girdwood }
25798c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
25808c6529dbSLiam Girdwood 
25818c6529dbSLiam Girdwood /**
25828c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
25838c6529dbSLiam Girdwood  * @dai: DAI
25848c6529dbSLiam Girdwood  * @mute: mute enable
2585da18396fSMark Brown  * @direction: stream to mute
25868c6529dbSLiam Girdwood  *
25878c6529dbSLiam Girdwood  * Mutes the DAI DAC.
25888c6529dbSLiam Girdwood  */
2589da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2590da18396fSMark Brown 			     int direction)
25918c6529dbSLiam Girdwood {
2592da18396fSMark Brown 	if (!dai->driver)
2593da18396fSMark Brown 		return -ENOTSUPP;
2594da18396fSMark Brown 
2595da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2596da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2597da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2598da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2599f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
26008c6529dbSLiam Girdwood 	else
260104570c62SMark Brown 		return -ENOTSUPP;
26028c6529dbSLiam Girdwood }
26038c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26048c6529dbSLiam Girdwood 
2605c5af3a2eSMark Brown /**
2606c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2607c5af3a2eSMark Brown  *
2608ac11a2b3SMark Brown  * @card: Card to register
2609c5af3a2eSMark Brown  *
2610c5af3a2eSMark Brown  */
261170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2612c5af3a2eSMark Brown {
2613923c5e61SMengdong Lin 	int i, ret;
26141a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2615f0fba2adSLiam Girdwood 
2616c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2617c5af3a2eSMark Brown 		return -EINVAL;
2618c5af3a2eSMark Brown 
26195a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
26205a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
26215a504963SStephen Warren 
2622923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
262388bd870fSBenoit Cousson 		if (ret) {
2624923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2625923c5e61SMengdong Lin 				link->name);
262688bd870fSBenoit Cousson 			return ret;
262788bd870fSBenoit Cousson 		}
26285a504963SStephen Warren 	}
26295a504963SStephen Warren 
2630ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2631ed77cc12SMark Brown 
2632111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2633111c6419SStephen Warren 
2634f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2635f8f80361SMengdong Lin 	card->num_dai_links = 0;
2636f0fba2adSLiam Girdwood 
26371a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
26389115171aSMark Brown 	card->num_rtd = 0;
2639db2a4165SFrank Mandarino 
2640db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
26418a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2642db2a4165SFrank Mandarino 	card->instantiated = 0;
2643db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2644a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2645db2a4165SFrank Mandarino 
2646b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2647b19e6e7bSMark Brown 	if (ret != 0)
26484e2576bdSKuninori Morimoto 		return ret;
2649db2a4165SFrank Mandarino 
2650988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
26511a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
265288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
265388bd870fSBenoit Cousson 		int j;
265488bd870fSBenoit Cousson 
265588bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
265688bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2657988e8cc4SNicolin Chen 			if (!codec_dai->active)
2658988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
265988bd870fSBenoit Cousson 		}
266088bd870fSBenoit Cousson 
2661988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2662988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2663988e8cc4SNicolin Chen 	}
2664988e8cc4SNicolin Chen 
2665b19e6e7bSMark Brown 	return ret;
2666db2a4165SFrank Mandarino }
266770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2668db2a4165SFrank Mandarino 
2669db2a4165SFrank Mandarino /**
2670db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2671db2a4165SFrank Mandarino  *
2672db2a4165SFrank Mandarino  * @card: Card to unregister
2673db2a4165SFrank Mandarino  *
2674db2a4165SFrank Mandarino  */
267570a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2676db2a4165SFrank Mandarino {
267701e0df66SLars-Peter Clausen 	if (card->instantiated) {
267801e0df66SLars-Peter Clausen 		card->instantiated = false;
26791c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2680b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2681f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
26828f6f9b29SKuninori Morimoto 	}
2683db2a4165SFrank Mandarino 
2684db2a4165SFrank Mandarino 	return 0;
2685db2a4165SFrank Mandarino }
268670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2687db2a4165SFrank Mandarino 
2688db2a4165SFrank Mandarino /*
2689db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2690db2a4165SFrank Mandarino  * and sanitizing names.
2691db2a4165SFrank Mandarino  */
26920b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2693db2a4165SFrank Mandarino {
2694db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2695db2a4165SFrank Mandarino 	int id1, id2;
2696db2a4165SFrank Mandarino 
2697db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2698db2a4165SFrank Mandarino 		return NULL;
2699db2a4165SFrank Mandarino 
270058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2701db2a4165SFrank Mandarino 
2702db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2703c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2704c5af3a2eSMark Brown 	if (found) {
2705c5af3a2eSMark Brown 		/* get ID */
2706c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2707c5af3a2eSMark Brown 
2708c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2709c5af3a2eSMark Brown 			if (*id == -1)
2710c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2711c5af3a2eSMark Brown 		}
2712c5af3a2eSMark Brown 
2713c5af3a2eSMark Brown 	} else {
2714c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2715c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2716c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2717c5af3a2eSMark Brown 
2718c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2719c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2720c5af3a2eSMark Brown 
2721c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2722c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
272358818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2724c5af3a2eSMark Brown 		} else
2725c5af3a2eSMark Brown 			*id = 0;
2726c5af3a2eSMark Brown 	}
2727c5af3a2eSMark Brown 
2728c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2729c5af3a2eSMark Brown }
2730c5af3a2eSMark Brown 
2731c5af3a2eSMark Brown /*
2732c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2733c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2734c5af3a2eSMark Brown  */
2735c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2736c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2737c5af3a2eSMark Brown {
2738c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
273910e8aa9aSMichał Mirosław 		dev_err(dev,
274010e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
274110e8aa9aSMichał Mirosław 			dev_name(dev));
2742c5af3a2eSMark Brown 		return NULL;
2743c5af3a2eSMark Brown 	}
2744c5af3a2eSMark Brown 
2745c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2746c5af3a2eSMark Brown }
2747c5af3a2eSMark Brown 
2748c5af3a2eSMark Brown /**
274932c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
27509115171aSMark Brown  *
275132c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
27529115171aSMark Brown  */
275332c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
27549115171aSMark Brown {
27555c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
27569115171aSMark Brown 
27575c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
275832c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
275932c9ba54SLars-Peter Clausen 			dai->name);
27609115171aSMark Brown 		list_del(&dai->list);
2761f0fba2adSLiam Girdwood 		kfree(dai->name);
2762f0fba2adSLiam Girdwood 		kfree(dai);
27639115171aSMark Brown 	}
276432c9ba54SLars-Peter Clausen }
27659115171aSMark Brown 
27665e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
27675e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
27685e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
27695e4fb372SMengdong Lin 	bool legacy_dai_naming)
27705e4fb372SMengdong Lin {
27715e4fb372SMengdong Lin 	struct device *dev = component->dev;
27725e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
27735e4fb372SMengdong Lin 
27745e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
27755e4fb372SMengdong Lin 
27765e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
27775e4fb372SMengdong Lin 	if (dai == NULL)
27785e4fb372SMengdong Lin 		return NULL;
27795e4fb372SMengdong Lin 
27805e4fb372SMengdong Lin 	/*
27815e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
27825e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
27835e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
27845e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
27855e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
27865e4fb372SMengdong Lin 	 * component-less anymore.
27875e4fb372SMengdong Lin 	 */
27885e4fb372SMengdong Lin 	if (legacy_dai_naming &&
27895e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
27905e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
27915e4fb372SMengdong Lin 	} else {
27925e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
27935e4fb372SMengdong Lin 		if (dai_drv->id)
27945e4fb372SMengdong Lin 			dai->id = dai_drv->id;
27955e4fb372SMengdong Lin 		else
27965e4fb372SMengdong Lin 			dai->id = component->num_dai;
27975e4fb372SMengdong Lin 	}
27985e4fb372SMengdong Lin 	if (dai->name == NULL) {
27995e4fb372SMengdong Lin 		kfree(dai);
28005e4fb372SMengdong Lin 		return NULL;
28015e4fb372SMengdong Lin 	}
28025e4fb372SMengdong Lin 
28035e4fb372SMengdong Lin 	dai->component = component;
28045e4fb372SMengdong Lin 	dai->dev = dev;
28055e4fb372SMengdong Lin 	dai->driver = dai_drv;
28065e4fb372SMengdong Lin 	if (!dai->driver->ops)
28075e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
28085e4fb372SMengdong Lin 
28095e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
28105e4fb372SMengdong Lin 	component->num_dai++;
28115e4fb372SMengdong Lin 
28125e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
28135e4fb372SMengdong Lin 	return dai;
28145e4fb372SMengdong Lin }
28155e4fb372SMengdong Lin 
28169115171aSMark Brown /**
281732c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
28189115171aSMark Brown  *
28196106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
28206106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2821ac11a2b3SMark Brown  * @count: Number of DAIs
282232c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
282332c9ba54SLars-Peter Clausen  *                     parent's name.
28249115171aSMark Brown  */
282532c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2826bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2827bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
28289115171aSMark Brown {
28296106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2830f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
283132c9ba54SLars-Peter Clausen 	unsigned int i;
283232c9ba54SLars-Peter Clausen 	int ret;
2833f0fba2adSLiam Girdwood 
2834f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
28359115171aSMark Brown 
2836bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2837bb13109dSLars-Peter Clausen 
28389115171aSMark Brown 	for (i = 0; i < count; i++) {
2839f0fba2adSLiam Girdwood 
28405e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
28415e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
2842c46e0079SAxel Lin 		if (dai == NULL) {
2843c46e0079SAxel Lin 			ret = -ENOMEM;
2844c46e0079SAxel Lin 			goto err;
2845c46e0079SAxel Lin 		}
2846f0fba2adSLiam Girdwood 	}
2847f0fba2adSLiam Girdwood 
28489115171aSMark Brown 	return 0;
28499115171aSMark Brown 
28509115171aSMark Brown err:
285132c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
28529115171aSMark Brown 
28539115171aSMark Brown 	return ret;
28549115171aSMark Brown }
28559115171aSMark Brown 
285668003e6cSMengdong Lin /**
285768003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
285868003e6cSMengdong Lin  *
285968003e6cSMengdong Lin  * @component: The component the DAIs are registered for
286068003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
286168003e6cSMengdong Lin  *
286268003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
286368003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
286468003e6cSMengdong Lin  * will be freed in the component cleanup.
286568003e6cSMengdong Lin  */
286668003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
286768003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
286868003e6cSMengdong Lin {
286968003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
287068003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
287168003e6cSMengdong Lin 	struct snd_soc_dai *dai;
287268003e6cSMengdong Lin 	int ret;
287368003e6cSMengdong Lin 
287468003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
287568003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
287668003e6cSMengdong Lin 			dai_drv->dobj.type);
287768003e6cSMengdong Lin 		return -EINVAL;
287868003e6cSMengdong Lin 	}
287968003e6cSMengdong Lin 
288068003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
288168003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
288268003e6cSMengdong Lin 	if (!dai)
288368003e6cSMengdong Lin 		return -ENOMEM;
288468003e6cSMengdong Lin 
288568003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
288668003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
288768003e6cSMengdong Lin 	 */
288868003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
288968003e6cSMengdong Lin 	if (ret != 0) {
289068003e6cSMengdong Lin 		dev_err(component->dev,
289168003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
289268003e6cSMengdong Lin 	}
289368003e6cSMengdong Lin 
289468003e6cSMengdong Lin 	return ret;
289568003e6cSMengdong Lin }
289668003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
289768003e6cSMengdong Lin 
289814e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
289914e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2900d191bd8dSKuninori Morimoto {
290114e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2902d191bd8dSKuninori Morimoto 
290314e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
290414e8bdebSLars-Peter Clausen }
2905d191bd8dSKuninori Morimoto 
290614e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
290714e8bdebSLars-Peter Clausen 	int event)
290814e8bdebSLars-Peter Clausen {
290914e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
291014e8bdebSLars-Peter Clausen 
291114e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
291214e8bdebSLars-Peter Clausen }
291314e8bdebSLars-Peter Clausen 
2914bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2915bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2916d191bd8dSKuninori Morimoto {
2917ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2918ce0fc93aSLars-Peter Clausen 
2919bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2920bb13109dSLars-Peter Clausen 	if (!component->name) {
2921bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2922d191bd8dSKuninori Morimoto 		return -ENOMEM;
2923d191bd8dSKuninori Morimoto 	}
2924d191bd8dSKuninori Morimoto 
2925bb13109dSLars-Peter Clausen 	component->dev = dev;
2926bb13109dSLars-Peter Clausen 	component->driver = driver;
292761aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
292861aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
2929e2c330b9SLars-Peter Clausen 
29304890140fSLars-Peter Clausen 	dapm = &component->dapm;
2931ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2932ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2933ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
29345819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
293514e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
293614e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
293714e8bdebSLars-Peter Clausen 	if (driver->stream_event)
293814e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2939ce0fc93aSLars-Peter Clausen 
294061aca564SLars-Peter Clausen 	component->controls = driver->controls;
294161aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
294261aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
294361aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
294461aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
294561aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
294661aca564SLars-Peter Clausen 
2947bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2948bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2949bb13109dSLars-Peter Clausen 
2950bb13109dSLars-Peter Clausen 	return 0;
2951d191bd8dSKuninori Morimoto }
2952d191bd8dSKuninori Morimoto 
295320feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2954886f5692SLars-Peter Clausen {
2955886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
295620feb881SLars-Peter Clausen 
2957886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2958886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2959886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2960886f5692SLars-Peter Clausen }
296120feb881SLars-Peter Clausen 
2962e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2963e874bf5fSLars-Peter Clausen 
296420feb881SLars-Peter Clausen /**
296520feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
296620feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
296720feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
296820feb881SLars-Peter Clausen  *
296920feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
297020feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
297120feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
297220feb881SLars-Peter Clausen  * before the first IO attempt of the component.
297320feb881SLars-Peter Clausen  */
297420feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
297520feb881SLars-Peter Clausen 	struct regmap *regmap)
297620feb881SLars-Peter Clausen {
297720feb881SLars-Peter Clausen 	component->regmap = regmap;
297820feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2979886f5692SLars-Peter Clausen }
298020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
298120feb881SLars-Peter Clausen 
298220feb881SLars-Peter Clausen /**
298320feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
298420feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
298520feb881SLars-Peter Clausen  *
298620feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
298720feb881SLars-Peter Clausen  * removes the regmap instance from the component.
298820feb881SLars-Peter Clausen  *
298920feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
299020feb881SLars-Peter Clausen  * to initialize the regmap instance.
299120feb881SLars-Peter Clausen  */
299220feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
299320feb881SLars-Peter Clausen {
299420feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
299520feb881SLars-Peter Clausen 	component->regmap = NULL;
299620feb881SLars-Peter Clausen }
299720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2998886f5692SLars-Peter Clausen 
2999e874bf5fSLars-Peter Clausen #endif
3000d191bd8dSKuninori Morimoto 
3001bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3002bb13109dSLars-Peter Clausen {
300320feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
300420feb881SLars-Peter Clausen 		if (!component->regmap)
300520feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
300620feb881SLars-Peter Clausen 		if (component->regmap)
300720feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
300820feb881SLars-Peter Clausen 	}
3009886f5692SLars-Peter Clausen 
3010bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
30118a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3012d191bd8dSKuninori Morimoto }
3013d191bd8dSKuninori Morimoto 
3014bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3015bb13109dSLars-Peter Clausen {
3016d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3017bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3018d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3019bb13109dSLars-Peter Clausen }
3020d191bd8dSKuninori Morimoto 
3021bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3022bb13109dSLars-Peter Clausen {
3023bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3024bb13109dSLars-Peter Clausen 	kfree(component->name);
3025bb13109dSLars-Peter Clausen }
3026d191bd8dSKuninori Morimoto 
3027bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3028bb13109dSLars-Peter Clausen {
3029bb13109dSLars-Peter Clausen 	list_del(&component->list);
3030bb13109dSLars-Peter Clausen }
3031d191bd8dSKuninori Morimoto 
3032d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3033d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3034d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3035d191bd8dSKuninori Morimoto 			       int num_dai)
3036d191bd8dSKuninori Morimoto {
3037d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3038bb13109dSLars-Peter Clausen 	int ret;
3039d191bd8dSKuninori Morimoto 
3040bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3041d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3042d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3043d191bd8dSKuninori Morimoto 		return -ENOMEM;
3044d191bd8dSKuninori Morimoto 	}
3045d191bd8dSKuninori Morimoto 
3046bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3047bb13109dSLars-Peter Clausen 	if (ret)
3048bb13109dSLars-Peter Clausen 		goto err_free;
3049bb13109dSLars-Peter Clausen 
30503d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
305198e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
30523d59400fSLars-Peter Clausen 
3053bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3054bb13109dSLars-Peter Clausen 	if (ret < 0) {
3055f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3056bb13109dSLars-Peter Clausen 		goto err_cleanup;
3057bb13109dSLars-Peter Clausen 	}
3058bb13109dSLars-Peter Clausen 
3059bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3060bb13109dSLars-Peter Clausen 
3061bb13109dSLars-Peter Clausen 	return 0;
3062bb13109dSLars-Peter Clausen 
3063bb13109dSLars-Peter Clausen err_cleanup:
3064bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3065bb13109dSLars-Peter Clausen err_free:
3066bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3067bb13109dSLars-Peter Clausen 	return ret;
3068d191bd8dSKuninori Morimoto }
3069d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3070d191bd8dSKuninori Morimoto 
3071d191bd8dSKuninori Morimoto /**
3072d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3073d191bd8dSKuninori Morimoto  *
3074628536eaSJonathan Corbet  * @dev: The device to unregister
3075d191bd8dSKuninori Morimoto  */
3076d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3077d191bd8dSKuninori Morimoto {
3078d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3079d191bd8dSKuninori Morimoto 
308034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3081d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
308298e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3083d191bd8dSKuninori Morimoto 			goto found;
3084d191bd8dSKuninori Morimoto 	}
308534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3086d191bd8dSKuninori Morimoto 	return;
3087d191bd8dSKuninori Morimoto 
3088d191bd8dSKuninori Morimoto found:
30898a978234SLiam Girdwood 	snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
309034e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
309134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3092bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3093bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3094d191bd8dSKuninori Morimoto }
3095d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3096d191bd8dSKuninori Morimoto 
3097f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3098e2c330b9SLars-Peter Clausen {
3099e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3100e2c330b9SLars-Peter Clausen 
3101f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3102e2c330b9SLars-Peter Clausen }
3103e2c330b9SLars-Peter Clausen 
3104f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3105e2c330b9SLars-Peter Clausen {
3106e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3107e2c330b9SLars-Peter Clausen 
3108f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3109e2c330b9SLars-Peter Clausen }
3110e2c330b9SLars-Peter Clausen 
3111d191bd8dSKuninori Morimoto /**
311271a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
311371a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
311471a45cdaSLars-Peter Clausen  * @platform: The platform to add
31157d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
311612a48a8cSMark Brown  */
311771a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3118d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
311912a48a8cSMark Brown {
3120b37f1d12SLars-Peter Clausen 	int ret;
3121b37f1d12SLars-Peter Clausen 
3122bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3123bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3124bb13109dSLars-Peter Clausen 	if (ret)
3125bb13109dSLars-Peter Clausen 		return ret;
3126f0fba2adSLiam Girdwood 
3127f0fba2adSLiam Girdwood 	platform->dev = dev;
3128f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3129f1d45cc3SLars-Peter Clausen 
3130f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3131f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3132f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3133f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
313412a48a8cSMark Brown 
313581c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
313681c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
313781c7cfd1SLars-Peter Clausen #endif
313812a48a8cSMark Brown 
313912a48a8cSMark Brown 	mutex_lock(&client_mutex);
3140bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
314112a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
314212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
314312a48a8cSMark Brown 
3144f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3145f4333203SLars-Peter Clausen 		platform->component.name);
314612a48a8cSMark Brown 
314712a48a8cSMark Brown 	return 0;
314812a48a8cSMark Brown }
314971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
315071a45cdaSLars-Peter Clausen 
315171a45cdaSLars-Peter Clausen /**
315271a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
315371a45cdaSLars-Peter Clausen  *
3154628536eaSJonathan Corbet  * @dev: The device for the platform
3155628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
315671a45cdaSLars-Peter Clausen  */
315771a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
315871a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
315971a45cdaSLars-Peter Clausen {
316071a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
316171a45cdaSLars-Peter Clausen 	int ret;
316271a45cdaSLars-Peter Clausen 
316371a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
316471a45cdaSLars-Peter Clausen 
316571a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
316671a45cdaSLars-Peter Clausen 	if (platform == NULL)
316771a45cdaSLars-Peter Clausen 		return -ENOMEM;
316871a45cdaSLars-Peter Clausen 
316971a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
317071a45cdaSLars-Peter Clausen 	if (ret)
317171a45cdaSLars-Peter Clausen 		kfree(platform);
317271a45cdaSLars-Peter Clausen 
317371a45cdaSLars-Peter Clausen 	return ret;
317471a45cdaSLars-Peter Clausen }
317512a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
317612a48a8cSMark Brown 
317712a48a8cSMark Brown /**
317871a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
317971a45cdaSLars-Peter Clausen  * @platform: the platform to remove
318071a45cdaSLars-Peter Clausen  */
318171a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
318271a45cdaSLars-Peter Clausen {
3183b37f1d12SLars-Peter Clausen 
318471a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
318571a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3186bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
318771a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
318871a45cdaSLars-Peter Clausen 
318971a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3190f4333203SLars-Peter Clausen 		platform->component.name);
3191decc27b0SDaniel Mack 
3192decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
319371a45cdaSLars-Peter Clausen }
319471a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
319571a45cdaSLars-Peter Clausen 
319671a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
319771a45cdaSLars-Peter Clausen {
319871a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
319971a45cdaSLars-Peter Clausen 
320034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
320171a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
320234e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
320334e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
320471a45cdaSLars-Peter Clausen 			return platform;
320571a45cdaSLars-Peter Clausen 		}
320634e81ab4SLars-Peter Clausen 	}
320734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
320871a45cdaSLars-Peter Clausen 
320971a45cdaSLars-Peter Clausen 	return NULL;
321071a45cdaSLars-Peter Clausen }
321171a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
321271a45cdaSLars-Peter Clausen 
321371a45cdaSLars-Peter Clausen /**
321412a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
321512a48a8cSMark Brown  *
3216628536eaSJonathan Corbet  * @dev: platform to unregister
321712a48a8cSMark Brown  */
3218f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
321912a48a8cSMark Brown {
3220f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3221f0fba2adSLiam Girdwood 
322271a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
322371a45cdaSLars-Peter Clausen 	if (!platform)
3224f0fba2adSLiam Girdwood 		return;
3225f0fba2adSLiam Girdwood 
322671a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3227f0fba2adSLiam Girdwood 	kfree(platform);
322812a48a8cSMark Brown }
322912a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
323012a48a8cSMark Brown 
3231151ab22cSMark Brown static u64 codec_format_map[] = {
3232151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3233151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3234151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3235151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3236151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3237151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3238151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3239151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3240151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3241151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3242151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3243151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3244151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3245151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3246151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3247151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3248151ab22cSMark Brown };
3249151ab22cSMark Brown 
3250151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3251151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3252151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3253151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3254151ab22cSMark Brown  */
3255151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3256151ab22cSMark Brown {
3257151ab22cSMark Brown 	int i;
3258151ab22cSMark Brown 
3259151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3260151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3261151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3262151ab22cSMark Brown }
3263151ab22cSMark Brown 
3264f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3265f1d45cc3SLars-Peter Clausen {
3266f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3267f1d45cc3SLars-Peter Clausen 
3268f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3269f1d45cc3SLars-Peter Clausen }
3270f1d45cc3SLars-Peter Clausen 
3271f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3272f1d45cc3SLars-Peter Clausen {
3273f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3274f1d45cc3SLars-Peter Clausen 
3275f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3276f1d45cc3SLars-Peter Clausen }
3277f1d45cc3SLars-Peter Clausen 
3278e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3279e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3280e2c330b9SLars-Peter Clausen {
3281e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3282e2c330b9SLars-Peter Clausen 
3283e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3284e2c330b9SLars-Peter Clausen }
3285e2c330b9SLars-Peter Clausen 
3286e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3287e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3288e2c330b9SLars-Peter Clausen {
3289e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3290e2c330b9SLars-Peter Clausen 
3291e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3292e2c330b9SLars-Peter Clausen 
3293e2c330b9SLars-Peter Clausen 	return 0;
3294e2c330b9SLars-Peter Clausen }
3295e2c330b9SLars-Peter Clausen 
329668f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
329768f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
329868f831c2SLars-Peter Clausen {
329968f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
330068f831c2SLars-Peter Clausen 
330168f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
330268f831c2SLars-Peter Clausen }
330368f831c2SLars-Peter Clausen 
33040d0cf00aSMark Brown /**
33050d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
33060d0cf00aSMark Brown  *
3307628536eaSJonathan Corbet  * @dev: The parent device for this codec
3308628536eaSJonathan Corbet  * @codec_drv: Codec driver
3309628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3310628536eaSJonathan Corbet  * @num_dai: Number of DAIs
33110d0cf00aSMark Brown  */
3312f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3313001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3314001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3315001ae4c0SMark Brown 			   int num_dai)
33160d0cf00aSMark Brown {
33174890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3318f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3319bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3320f0fba2adSLiam Girdwood 	int ret, i;
3321151ab22cSMark Brown 
3322f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
33230d0cf00aSMark Brown 
3324f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3325f0fba2adSLiam Girdwood 	if (codec == NULL)
3326f0fba2adSLiam Girdwood 		return -ENOMEM;
33270d0cf00aSMark Brown 
3328f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3329ce0fc93aSLars-Peter Clausen 
3330bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3331bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3332bb13109dSLars-Peter Clausen 	if (ret)
3333bb13109dSLars-Peter Clausen 		goto err_free;
3334151ab22cSMark Brown 
3335f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3336f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3337f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3338f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
3339e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3340e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3341e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3342e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
33433d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
33444890140fSLars-Peter Clausen 
33454890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
33464890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
33474890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
334814e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
33494890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
335068f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
33514890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3352f0fba2adSLiam Girdwood 	codec->dev = dev;
3353f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3354e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3355f0fba2adSLiam Girdwood 
335681c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
335781c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
335881c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
335981c7cfd1SLars-Peter Clausen #endif
3360a39f75f7SXiubo Li 
3361a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3362886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3363a39f75f7SXiubo Li 
3364f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3365f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3366f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3367f0fba2adSLiam Girdwood 	}
3368f0fba2adSLiam Girdwood 
3369bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3370bb13109dSLars-Peter Clausen 	if (ret < 0) {
3371f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3372bb13109dSLars-Peter Clausen 		goto err_cleanup;
3373bb13109dSLars-Peter Clausen 	}
3374bb13109dSLars-Peter Clausen 
3375bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3376bb13109dSLars-Peter Clausen 		dai->codec = codec;
3377bb13109dSLars-Peter Clausen 
3378054880feSMark Brown 	mutex_lock(&client_mutex);
3379bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3380054880feSMark Brown 	list_add(&codec->list, &codec_list);
3381054880feSMark Brown 	mutex_unlock(&client_mutex);
3382054880feSMark Brown 
3383f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3384f4333203SLars-Peter Clausen 		codec->component.name);
33850d0cf00aSMark Brown 	return 0;
3386f0fba2adSLiam Girdwood 
3387bb13109dSLars-Peter Clausen err_cleanup:
3388bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3389bb13109dSLars-Peter Clausen err_free:
3390f0fba2adSLiam Girdwood 	kfree(codec);
3391f0fba2adSLiam Girdwood 	return ret;
33920d0cf00aSMark Brown }
33930d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
33940d0cf00aSMark Brown 
33950d0cf00aSMark Brown /**
33960d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
33970d0cf00aSMark Brown  *
3398628536eaSJonathan Corbet  * @dev: codec to unregister
33990d0cf00aSMark Brown  */
3400f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
34010d0cf00aSMark Brown {
3402f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3403f0fba2adSLiam Girdwood 
340434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3405f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3406f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3407f0fba2adSLiam Girdwood 			goto found;
3408f0fba2adSLiam Girdwood 	}
340934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3410f0fba2adSLiam Girdwood 	return;
3411f0fba2adSLiam Girdwood 
3412f0fba2adSLiam Girdwood found:
34130d0cf00aSMark Brown 	list_del(&codec->list);
3414bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
34150d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
34160d0cf00aSMark Brown 
3417f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3418f4333203SLars-Peter Clausen 			codec->component.name);
3419f0fba2adSLiam Girdwood 
3420bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
34217a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3422f0fba2adSLiam Girdwood 	kfree(codec);
34230d0cf00aSMark Brown }
34240d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
34250d0cf00aSMark Brown 
3426bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3427bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3428bec4fa05SStephen Warren 			       const char *propname)
3429bec4fa05SStephen Warren {
34307e07e7c0STushar Behera 	struct device_node *np;
3431bec4fa05SStephen Warren 	int ret;
3432bec4fa05SStephen Warren 
34337e07e7c0STushar Behera 	if (!card->dev) {
34347e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
34357e07e7c0STushar Behera 		return -EINVAL;
34367e07e7c0STushar Behera 	}
34377e07e7c0STushar Behera 
34387e07e7c0STushar Behera 	np = card->dev->of_node;
34397e07e7c0STushar Behera 
3440bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3441bec4fa05SStephen Warren 	/*
3442bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3443bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3444bec4fa05SStephen Warren 	 * snd_soc_register_card.
3445bec4fa05SStephen Warren 	 */
3446bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3447bec4fa05SStephen Warren 		dev_err(card->dev,
3448f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3449bec4fa05SStephen Warren 			propname, ret);
3450bec4fa05SStephen Warren 		return ret;
3451bec4fa05SStephen Warren 	}
3452bec4fa05SStephen Warren 
3453bec4fa05SStephen Warren 	return 0;
3454bec4fa05SStephen Warren }
3455bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3456bec4fa05SStephen Warren 
34579a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
34589a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
34599a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
34609a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
34619a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
34629a6d4860SXiubo Li };
34639a6d4860SXiubo Li 
34649a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
34659a6d4860SXiubo Li 					  const char *propname)
34669a6d4860SXiubo Li {
34679a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
34689a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
34699a6d4860SXiubo Li 	const char *template, *wname;
34709a6d4860SXiubo Li 	int i, j, num_widgets, ret;
34719a6d4860SXiubo Li 
34729a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
34739a6d4860SXiubo Li 	if (num_widgets < 0) {
34749a6d4860SXiubo Li 		dev_err(card->dev,
34759a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
34769a6d4860SXiubo Li 		return -EINVAL;
34779a6d4860SXiubo Li 	}
34789a6d4860SXiubo Li 	if (num_widgets & 1) {
34799a6d4860SXiubo Li 		dev_err(card->dev,
34809a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
34819a6d4860SXiubo Li 		return -EINVAL;
34829a6d4860SXiubo Li 	}
34839a6d4860SXiubo Li 
34849a6d4860SXiubo Li 	num_widgets /= 2;
34859a6d4860SXiubo Li 	if (!num_widgets) {
34869a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
34879a6d4860SXiubo Li 			propname);
34889a6d4860SXiubo Li 		return -EINVAL;
34899a6d4860SXiubo Li 	}
34909a6d4860SXiubo Li 
34919a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
34929a6d4860SXiubo Li 			       GFP_KERNEL);
34939a6d4860SXiubo Li 	if (!widgets) {
34949a6d4860SXiubo Li 		dev_err(card->dev,
34959a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
34969a6d4860SXiubo Li 		return -ENOMEM;
34979a6d4860SXiubo Li 	}
34989a6d4860SXiubo Li 
34999a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
35009a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35019a6d4860SXiubo Li 			2 * i, &template);
35029a6d4860SXiubo Li 		if (ret) {
35039a6d4860SXiubo Li 			dev_err(card->dev,
35049a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35059a6d4860SXiubo Li 				propname, 2 * i, ret);
35069a6d4860SXiubo Li 			return -EINVAL;
35079a6d4860SXiubo Li 		}
35089a6d4860SXiubo Li 
35099a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
35109a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
35119a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
35129a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
35139a6d4860SXiubo Li 				break;
35149a6d4860SXiubo Li 			}
35159a6d4860SXiubo Li 		}
35169a6d4860SXiubo Li 
35179a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
35189a6d4860SXiubo Li 			dev_err(card->dev,
35199a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
35209a6d4860SXiubo Li 				template);
35219a6d4860SXiubo Li 			return -EINVAL;
35229a6d4860SXiubo Li 		}
35239a6d4860SXiubo Li 
35249a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35259a6d4860SXiubo Li 						    (2 * i) + 1,
35269a6d4860SXiubo Li 						    &wname);
35279a6d4860SXiubo Li 		if (ret) {
35289a6d4860SXiubo Li 			dev_err(card->dev,
35299a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35309a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
35319a6d4860SXiubo Li 			return -EINVAL;
35329a6d4860SXiubo Li 		}
35339a6d4860SXiubo Li 
35349a6d4860SXiubo Li 		widgets[i].name = wname;
35359a6d4860SXiubo Li 	}
35369a6d4860SXiubo Li 
3537f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3538f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
35399a6d4860SXiubo Li 
35409a6d4860SXiubo Li 	return 0;
35419a6d4860SXiubo Li }
35429a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
35439a6d4860SXiubo Li 
35446131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
35456131084aSJyri Sarha 				    const char *prop_name,
35466131084aSJyri Sarha 				    unsigned int *mask)
35476131084aSJyri Sarha {
35486131084aSJyri Sarha 	u32 val;
35496c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
35506131084aSJyri Sarha 	int i;
35516131084aSJyri Sarha 
35526131084aSJyri Sarha 	if (!of_slot_mask)
35536131084aSJyri Sarha 		return 0;
35546131084aSJyri Sarha 	val /= sizeof(u32);
35556131084aSJyri Sarha 	for (i = 0; i < val; i++)
35566131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
35576131084aSJyri Sarha 			*mask |= (1 << i);
35586131084aSJyri Sarha 
35596131084aSJyri Sarha 	return val;
35606131084aSJyri Sarha }
35616131084aSJyri Sarha 
356289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
35636131084aSJyri Sarha 			      unsigned int *tx_mask,
35646131084aSJyri Sarha 			      unsigned int *rx_mask,
356589c67857SXiubo Li 			      unsigned int *slots,
356689c67857SXiubo Li 			      unsigned int *slot_width)
356789c67857SXiubo Li {
356889c67857SXiubo Li 	u32 val;
356989c67857SXiubo Li 	int ret;
357089c67857SXiubo Li 
35716131084aSJyri Sarha 	if (tx_mask)
35726131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
35736131084aSJyri Sarha 	if (rx_mask)
35746131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
35756131084aSJyri Sarha 
357689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
357789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
357889c67857SXiubo Li 		if (ret)
357989c67857SXiubo Li 			return ret;
358089c67857SXiubo Li 
358189c67857SXiubo Li 		if (slots)
358289c67857SXiubo Li 			*slots = val;
358389c67857SXiubo Li 	}
358489c67857SXiubo Li 
358589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
358689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
358789c67857SXiubo Li 		if (ret)
358889c67857SXiubo Li 			return ret;
358989c67857SXiubo Li 
359089c67857SXiubo Li 		if (slot_width)
359189c67857SXiubo Li 			*slot_width = val;
359289c67857SXiubo Li 	}
359389c67857SXiubo Li 
359489c67857SXiubo Li 	return 0;
359589c67857SXiubo Li }
359689c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
359789c67857SXiubo Li 
35985e3cdaa2SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
35995e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
36005e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
36015e3cdaa2SKuninori Morimoto 				   const char *propname)
36025e3cdaa2SKuninori Morimoto {
36035e3cdaa2SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
36045e3cdaa2SKuninori Morimoto 	const char *str;
36055e3cdaa2SKuninori Morimoto 	int ret;
36065e3cdaa2SKuninori Morimoto 
36075e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
36085e3cdaa2SKuninori Morimoto 	if (ret < 0) {
36095e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
36105e3cdaa2SKuninori Morimoto 		return;
36115e3cdaa2SKuninori Morimoto 	}
36125e3cdaa2SKuninori Morimoto 
36135e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
36145e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
36155e3cdaa2SKuninori Morimoto }
36165e3cdaa2SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
36175e3cdaa2SKuninori Morimoto 
36187364c8dcSKuninori Morimoto int snd_soc_of_parse_audio_routing_from_node(struct snd_soc_card *card,
36197364c8dcSKuninori Morimoto 				   struct device_node *np,
3620a4a54dd5SStephen Warren 				   const char *propname)
3621a4a54dd5SStephen Warren {
3622e3b1e6a1SMark Brown 	int num_routes;
3623a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3624a4a54dd5SStephen Warren 	int i, ret;
3625a4a54dd5SStephen Warren 
36267364c8dcSKuninori Morimoto 	if (!np)
36277364c8dcSKuninori Morimoto 		np = card->dev->of_node;
36287364c8dcSKuninori Morimoto 
3629a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3630c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
363110e8aa9aSMichał Mirosław 		dev_err(card->dev,
363210e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
363310e8aa9aSMichał Mirosław 			propname);
3634a4a54dd5SStephen Warren 		return -EINVAL;
3635a4a54dd5SStephen Warren 	}
3636a4a54dd5SStephen Warren 	num_routes /= 2;
3637a4a54dd5SStephen Warren 	if (!num_routes) {
3638f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3639a4a54dd5SStephen Warren 			propname);
3640a4a54dd5SStephen Warren 		return -EINVAL;
3641a4a54dd5SStephen Warren 	}
3642a4a54dd5SStephen Warren 
3643e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3644a4a54dd5SStephen Warren 			      GFP_KERNEL);
3645a4a54dd5SStephen Warren 	if (!routes) {
3646a4a54dd5SStephen Warren 		dev_err(card->dev,
3647f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3648a4a54dd5SStephen Warren 		return -EINVAL;
3649a4a54dd5SStephen Warren 	}
3650a4a54dd5SStephen Warren 
3651a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3652a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3653e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3654a4a54dd5SStephen Warren 		if (ret) {
3655c871bd0bSMark Brown 			dev_err(card->dev,
3656c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3657c871bd0bSMark Brown 				propname, 2 * i, ret);
3658a4a54dd5SStephen Warren 			return -EINVAL;
3659a4a54dd5SStephen Warren 		}
3660a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3661e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3662a4a54dd5SStephen Warren 		if (ret) {
3663a4a54dd5SStephen Warren 			dev_err(card->dev,
3664c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3665c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3666a4a54dd5SStephen Warren 			return -EINVAL;
3667a4a54dd5SStephen Warren 		}
3668a4a54dd5SStephen Warren 	}
3669a4a54dd5SStephen Warren 
3670f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3671f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3672a4a54dd5SStephen Warren 
3673a4a54dd5SStephen Warren 	return 0;
3674a4a54dd5SStephen Warren }
36757364c8dcSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing_from_node);
3676a4a54dd5SStephen Warren 
3677a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3678389cb834SJyri Sarha 				     const char *prefix,
3679389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3680389cb834SJyri Sarha 				     struct device_node **framemaster)
3681a7930ed4SKuninori Morimoto {
3682a7930ed4SKuninori Morimoto 	int ret, i;
3683a7930ed4SKuninori Morimoto 	char prop[128];
3684a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3685a7930ed4SKuninori Morimoto 	int bit, frame;
3686a7930ed4SKuninori Morimoto 	const char *str;
3687a7930ed4SKuninori Morimoto 	struct {
3688a7930ed4SKuninori Morimoto 		char *name;
3689a7930ed4SKuninori Morimoto 		unsigned int val;
3690a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3691a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3692a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3693a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3694a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3695a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3696a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3697a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3698a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3699a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3700a7930ed4SKuninori Morimoto 	};
3701a7930ed4SKuninori Morimoto 
3702a7930ed4SKuninori Morimoto 	if (!prefix)
3703a7930ed4SKuninori Morimoto 		prefix = "";
3704a7930ed4SKuninori Morimoto 
3705a7930ed4SKuninori Morimoto 	/*
3706a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3707a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3708a7930ed4SKuninori Morimoto 	 */
3709a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3710a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3711a7930ed4SKuninori Morimoto 	if (ret == 0) {
3712a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3713a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3714a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3715a7930ed4SKuninori Morimoto 				break;
3716a7930ed4SKuninori Morimoto 			}
3717a7930ed4SKuninori Morimoto 		}
3718a7930ed4SKuninori Morimoto 	}
3719a7930ed4SKuninori Morimoto 
3720a7930ed4SKuninori Morimoto 	/*
37218c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3722a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3723a7930ed4SKuninori Morimoto 	 */
37248c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
372551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
37268c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
37278c2d6a9fSKuninori Morimoto 	else
37288c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3729a7930ed4SKuninori Morimoto 
3730a7930ed4SKuninori Morimoto 	/*
3731a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3732a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3733a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3734a7930ed4SKuninori Morimoto 	 */
3735a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3736a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3737a7930ed4SKuninori Morimoto 
3738a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3739a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3740a7930ed4SKuninori Morimoto 
3741a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3742a7930ed4SKuninori Morimoto 	case 0x11:
3743a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3744a7930ed4SKuninori Morimoto 		break;
3745a7930ed4SKuninori Morimoto 	case 0x10:
3746a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3747a7930ed4SKuninori Morimoto 		break;
3748a7930ed4SKuninori Morimoto 	case 0x01:
3749a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3750a7930ed4SKuninori Morimoto 		break;
3751a7930ed4SKuninori Morimoto 	default:
3752a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3753a7930ed4SKuninori Morimoto 		break;
3754a7930ed4SKuninori Morimoto 	}
3755a7930ed4SKuninori Morimoto 
3756a7930ed4SKuninori Morimoto 	/*
3757a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3758a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3759a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3760a7930ed4SKuninori Morimoto 	 */
3761a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3762a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3763389cb834SJyri Sarha 	if (bit && bitclkmaster)
3764389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3765a7930ed4SKuninori Morimoto 
3766a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3767a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3768389cb834SJyri Sarha 	if (frame && framemaster)
3769389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3770a7930ed4SKuninori Morimoto 
3771a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3772a7930ed4SKuninori Morimoto 	case 0x11:
3773a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3774a7930ed4SKuninori Morimoto 		break;
3775a7930ed4SKuninori Morimoto 	case 0x10:
3776a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3777a7930ed4SKuninori Morimoto 		break;
3778a7930ed4SKuninori Morimoto 	case 0x01:
3779a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3780a7930ed4SKuninori Morimoto 		break;
3781a7930ed4SKuninori Morimoto 	default:
3782a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3783a7930ed4SKuninori Morimoto 		break;
3784a7930ed4SKuninori Morimoto 	}
3785a7930ed4SKuninori Morimoto 
3786a7930ed4SKuninori Morimoto 	return format;
3787a7930ed4SKuninori Morimoto }
3788a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3789a7930ed4SKuninori Morimoto 
3790*1ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3791cb470087SKuninori Morimoto 				const char **dai_name)
3792cb470087SKuninori Morimoto {
3793cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
37943e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
379593b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3796cb470087SKuninori Morimoto 
3797cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3798cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
37993e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
38003e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
38013e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
38023e0aa8d8SJyri Sarha 
38033e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3804cb470087SKuninori Morimoto 			continue;
3805cb470087SKuninori Morimoto 
38066833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
380793b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
380893b0f3eeSJean-Francois Moine 							     args,
380993b0f3eeSJean-Francois Moine 							     dai_name);
38106833c452SKuninori Morimoto 		} else {
38116833c452SKuninori Morimoto 			int id = -1;
38126833c452SKuninori Morimoto 
381393b0f3eeSJean-Francois Moine 			switch (args->args_count) {
38146833c452SKuninori Morimoto 			case 0:
38156833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
38166833c452SKuninori Morimoto 				break;
38176833c452SKuninori Morimoto 			case 1:
381893b0f3eeSJean-Francois Moine 				id = args->args[0];
38196833c452SKuninori Morimoto 				break;
38206833c452SKuninori Morimoto 			default:
38216833c452SKuninori Morimoto 				/* not supported */
3822cb470087SKuninori Morimoto 				break;
3823cb470087SKuninori Morimoto 			}
3824cb470087SKuninori Morimoto 
38256833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
38266833c452SKuninori Morimoto 				ret = -EINVAL;
38273dcba280SNicolin Chen 				continue;
38286833c452SKuninori Morimoto 			}
3829e41975edSXiubo Li 
3830e41975edSXiubo Li 			ret = 0;
3831e41975edSXiubo Li 
3832e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3833e41975edSXiubo Li 			if (!*dai_name)
3834e41975edSXiubo Li 				*dai_name = pos->name;
38356833c452SKuninori Morimoto 		}
38366833c452SKuninori Morimoto 
3837cb470087SKuninori Morimoto 		break;
3838cb470087SKuninori Morimoto 	}
3839cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
384093b0f3eeSJean-Francois Moine 	return ret;
384193b0f3eeSJean-Francois Moine }
3842*1ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
384393b0f3eeSJean-Francois Moine 
384493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
384593b0f3eeSJean-Francois Moine 			    const char **dai_name)
384693b0f3eeSJean-Francois Moine {
384793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
384893b0f3eeSJean-Francois Moine 	int ret;
384993b0f3eeSJean-Francois Moine 
385093b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
385193b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
385293b0f3eeSJean-Francois Moine 	if (ret)
385393b0f3eeSJean-Francois Moine 		return ret;
385493b0f3eeSJean-Francois Moine 
385593b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3856cb470087SKuninori Morimoto 
3857cb470087SKuninori Morimoto 	of_node_put(args.np);
3858cb470087SKuninori Morimoto 
3859cb470087SKuninori Morimoto 	return ret;
3860cb470087SKuninori Morimoto }
3861cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3862cb470087SKuninori Morimoto 
386393b0f3eeSJean-Francois Moine /*
386493b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
386593b0f3eeSJean-Francois Moine  * @dev: Card device
386693b0f3eeSJean-Francois Moine  * @of_node: Device node
386793b0f3eeSJean-Francois Moine  * @dai_link: DAI link
386893b0f3eeSJean-Francois Moine  *
386993b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
387093b0f3eeSJean-Francois Moine  * 'sound-dai'.
387193b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
387293b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
387393b0f3eeSJean-Francois Moine  *
387493b0f3eeSJean-Francois Moine  * Returns 0 for success
387593b0f3eeSJean-Francois Moine  */
387693b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
387793b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
387893b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
387993b0f3eeSJean-Francois Moine {
388093b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
388193b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
388293b0f3eeSJean-Francois Moine 	char *name;
388393b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
388493b0f3eeSJean-Francois Moine 
388593b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
388693b0f3eeSJean-Francois Moine 	name = "sound-dai";
388793b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
388893b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
388993b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
389093b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
389193b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
389293b0f3eeSJean-Francois Moine 		else
389393b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
389493b0f3eeSJean-Francois Moine 		return num_codecs;
389593b0f3eeSJean-Francois Moine 	}
389693b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
389793b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
389893b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
389993b0f3eeSJean-Francois Moine 	if (!component)
390093b0f3eeSJean-Francois Moine 		return -ENOMEM;
390193b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
390293b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
390393b0f3eeSJean-Francois Moine 
390493b0f3eeSJean-Francois Moine 	/* Parse the list */
390593b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
390693b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
390793b0f3eeSJean-Francois Moine 	     index++, component++) {
390893b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
390993b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
391093b0f3eeSJean-Francois Moine 						  index, &args);
391193b0f3eeSJean-Francois Moine 		if (ret)
391293b0f3eeSJean-Francois Moine 			goto err;
391393b0f3eeSJean-Francois Moine 		component->of_node = args.np;
391493b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
391593b0f3eeSJean-Francois Moine 		if (ret < 0)
391693b0f3eeSJean-Francois Moine 			goto err;
391793b0f3eeSJean-Francois Moine 	}
391893b0f3eeSJean-Francois Moine 	return 0;
391993b0f3eeSJean-Francois Moine err:
392093b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
392193b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
392293b0f3eeSJean-Francois Moine 	     index++, component++) {
392393b0f3eeSJean-Francois Moine 		if (!component->of_node)
392493b0f3eeSJean-Francois Moine 			break;
392593b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
392693b0f3eeSJean-Francois Moine 		component->of_node = NULL;
392793b0f3eeSJean-Francois Moine 	}
392893b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
392993b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
393093b0f3eeSJean-Francois Moine 	return ret;
393193b0f3eeSJean-Francois Moine }
393293b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
393393b0f3eeSJean-Francois Moine 
3934c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3935db2a4165SFrank Mandarino {
39366553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3937fb257897SMark Brown 	snd_soc_util_init();
3938fb257897SMark Brown 
3939db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3940db2a4165SFrank Mandarino }
39414abe8e16SMark Brown module_init(snd_soc_init);
3942db2a4165SFrank Mandarino 
39437d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3944db2a4165SFrank Mandarino {
3945fb257897SMark Brown 	snd_soc_util_exit();
39466553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3947fb257897SMark Brown 
3948384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3949384c89e2SMark Brown #endif
3950db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3951db2a4165SFrank Mandarino }
3952db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3953db2a4165SFrank Mandarino 
3954db2a4165SFrank Mandarino /* Module information */
3955d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3956db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3957db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3958db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3959