xref: /openbmc/linux/sound/soc/soc-core.c (revision 1a653aa44725668590b36bbe2d7fe4736a69f055)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6f0fba2adSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7f0fba2adSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
80664d888SLiam Girdwood  *
9d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
100664d888SLiam Girdwood  *         with code, comments and ideas from :-
110664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
14db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
15db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
16db2a4165SFrank Mandarino  *  option) any later version.
17db2a4165SFrank Mandarino  *
18db2a4165SFrank Mandarino  *  TODO:
19db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
20db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
21db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
22db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
23db2a4165SFrank Mandarino  */
24db2a4165SFrank Mandarino 
25db2a4165SFrank Mandarino #include <linux/module.h>
26db2a4165SFrank Mandarino #include <linux/moduleparam.h>
27db2a4165SFrank Mandarino #include <linux/init.h>
28db2a4165SFrank Mandarino #include <linux/delay.h>
29db2a4165SFrank Mandarino #include <linux/pm.h>
30db2a4165SFrank Mandarino #include <linux/bitops.h>
3112ef193dSTroy Kisky #include <linux/debugfs.h>
32db2a4165SFrank Mandarino #include <linux/platform_device.h>
33741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
34f0e8ed85SMark Brown #include <linux/ctype.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
36bec4fa05SStephen Warren #include <linux/of.h>
37db2a4165SFrank Mandarino #include <sound/core.h>
383028eb8cSMark Brown #include <sound/jack.h>
39db2a4165SFrank Mandarino #include <sound/pcm.h>
40db2a4165SFrank Mandarino #include <sound/pcm_params.h>
41db2a4165SFrank Mandarino #include <sound/soc.h>
4201d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
438a978234SLiam Girdwood #include <sound/soc-topology.h>
44db2a4165SFrank Mandarino #include <sound/initval.h>
45db2a4165SFrank Mandarino 
46a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
47a8b1d34fSMark Brown #include <trace/events/asoc.h>
48a8b1d34fSMark Brown 
49f0fba2adSLiam Girdwood #define NAME_SIZE	32
50f0fba2adSLiam Girdwood 
51384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
528a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
538a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
54384c89e2SMark Brown #endif
55384c89e2SMark Brown 
56c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5712a48a8cSMark Brown static LIST_HEAD(platform_list);
580d0cf00aSMark Brown static LIST_HEAD(codec_list);
59030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
60c5af3a2eSMark Brown 
61db2a4165SFrank Mandarino /*
62db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
63db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
64db2a4165SFrank Mandarino  * between two audio tracks.
65db2a4165SFrank Mandarino  */
66db2a4165SFrank Mandarino static int pmdown_time = 5000;
67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69db2a4165SFrank Mandarino 
702bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
712bc9a81eSDimitris Papastamos  * a particular given value */
722bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
732bc9a81eSDimitris Papastamos {
742bc9a81eSDimitris Papastamos 	int c = 0;
752bc9a81eSDimitris Papastamos 	int i;
762bc9a81eSDimitris Papastamos 
772bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
782bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
792bc9a81eSDimitris Papastamos 			break;
802bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
812bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
822bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
832bc9a81eSDimitris Papastamos 	else
842bc9a81eSDimitris Papastamos 		c /= 8;
852bc9a81eSDimitris Papastamos 	return c;
862bc9a81eSDimitris Papastamos }
872bc9a81eSDimitris Papastamos 
8813fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
8913fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9013fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9113fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
922624d5faSMark Brown {
9300b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9400b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9513fd179fSDimitris Papastamos 	int ret;
9613fd179fSDimitris Papastamos 
9713fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
9813fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
9913fd179fSDimitris Papastamos 		return -EINVAL;
10013fd179fSDimitris Papastamos 
101d6b6c2caSTakashi Iwai 	sprintf(buf, "%.*x: ", wordsize, reg);
102d6b6c2caSTakashi Iwai 	buf += wordsize + 2;
103d6b6c2caSTakashi Iwai 
10413fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
105d6b6c2caSTakashi Iwai 	if (ret < 0)
106d6b6c2caSTakashi Iwai 		memset(buf, 'X', regsize);
107d6b6c2caSTakashi Iwai 	else
108d6b6c2caSTakashi Iwai 		sprintf(buf, "%.*x", regsize, ret);
109d6b6c2caSTakashi Iwai 	buf[regsize] = '\n';
110d6b6c2caSTakashi Iwai 	/* no NUL-termination needed */
11113fd179fSDimitris Papastamos 	return 0;
11213fd179fSDimitris Papastamos }
11313fd179fSDimitris Papastamos 
11413fd179fSDimitris Papastamos /* codec register dump */
11513fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
11613fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
11713fd179fSDimitris Papastamos {
11813fd179fSDimitris Papastamos 	int i, step = 1;
1192bc9a81eSDimitris Papastamos 	int wordsize, regsize;
12013fd179fSDimitris Papastamos 	int len;
12113fd179fSDimitris Papastamos 	size_t total = 0;
12213fd179fSDimitris Papastamos 	loff_t p = 0;
1232bc9a81eSDimitris Papastamos 
12400b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
12500b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1262624d5faSMark Brown 
12713fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
12813fd179fSDimitris Papastamos 
129f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1302624d5faSMark Brown 		return 0;
1312624d5faSMark Brown 
132f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
133f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1342624d5faSMark Brown 
135f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
13613fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
13713fd179fSDimitris Papastamos 		 * entries for the default case */
13813fd179fSDimitris Papastamos 		if (p >= pos) {
13913fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1402624d5faSMark Brown 				break;
14113fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
14213fd179fSDimitris Papastamos 			total += len;
14313fd179fSDimitris Papastamos 		}
14413fd179fSDimitris Papastamos 		p += len;
14513fd179fSDimitris Papastamos 	}
1462624d5faSMark Brown 
14713fd179fSDimitris Papastamos 	total = min(total, count - 1);
1482624d5faSMark Brown 
14913fd179fSDimitris Papastamos 	return total;
1502624d5faSMark Brown }
15113fd179fSDimitris Papastamos 
1522624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1532624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1542624d5faSMark Brown {
15536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
156f0fba2adSLiam Girdwood 
15713fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1582624d5faSMark Brown }
1592624d5faSMark Brown 
1602624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1612624d5faSMark Brown 
162dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
163dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
164dbe21408SMark Brown {
16536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
166dbe21408SMark Brown 
167f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
168dbe21408SMark Brown }
169dbe21408SMark Brown 
170dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
171dbe21408SMark Brown 			       struct device_attribute *attr,
172dbe21408SMark Brown 			       const char *buf, size_t count)
173dbe21408SMark Brown {
17436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
175c593b520SMark Brown 	int ret;
176dbe21408SMark Brown 
177b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
178c593b520SMark Brown 	if (ret)
179c593b520SMark Brown 		return ret;
180dbe21408SMark Brown 
181dbe21408SMark Brown 	return count;
182dbe21408SMark Brown }
183dbe21408SMark Brown 
184dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
185dbe21408SMark Brown 
186d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
187d29697dcSTakashi Iwai 	&dev_attr_codec_reg.attr,
188d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
189d29697dcSTakashi Iwai 	NULL
190d29697dcSTakashi Iwai };
191d29697dcSTakashi Iwai 
192d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
193d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
194d29697dcSTakashi Iwai {
195d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
196d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
197d29697dcSTakashi Iwai 
198d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
199d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
200d29697dcSTakashi Iwai 	return rtd->codec ? attr->mode : 0; /* enabled only with codec */
201d29697dcSTakashi Iwai }
202d29697dcSTakashi Iwai 
203d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
204d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
205d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
206d29697dcSTakashi Iwai };
207d29697dcSTakashi Iwai 
208d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = {
209d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
210d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
211d29697dcSTakashi Iwai };
212d29697dcSTakashi Iwai 
213d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
214d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
215d29697dcSTakashi Iwai 	&soc_dev_roup,
216d29697dcSTakashi Iwai 	NULL
217d29697dcSTakashi Iwai };
218d29697dcSTakashi Iwai 
2192624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2202624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2212624d5faSMark Brown 				   size_t count, loff_t *ppos)
2222624d5faSMark Brown {
2232624d5faSMark Brown 	ssize_t ret;
2242624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
22513fd179fSDimitris Papastamos 	char *buf;
22613fd179fSDimitris Papastamos 
22713fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22813fd179fSDimitris Papastamos 		return -EINVAL;
22913fd179fSDimitris Papastamos 
23013fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2312624d5faSMark Brown 	if (!buf)
2322624d5faSMark Brown 		return -ENOMEM;
23313fd179fSDimitris Papastamos 
23413fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
23513fd179fSDimitris Papastamos 	if (ret >= 0) {
23613fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23713fd179fSDimitris Papastamos 			kfree(buf);
23813fd179fSDimitris Papastamos 			return -EFAULT;
23913fd179fSDimitris Papastamos 		}
24013fd179fSDimitris Papastamos 		*ppos += ret;
24113fd179fSDimitris Papastamos 	}
24213fd179fSDimitris Papastamos 
2432624d5faSMark Brown 	kfree(buf);
2442624d5faSMark Brown 	return ret;
2452624d5faSMark Brown }
2462624d5faSMark Brown 
2472624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2482624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2492624d5faSMark Brown {
2502624d5faSMark Brown 	char buf[32];
25134e268d8SStephen Boyd 	size_t buf_size;
2522624d5faSMark Brown 	char *start = buf;
2532624d5faSMark Brown 	unsigned long reg, value;
2542624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
255b785a492SJingoo Han 	int ret;
2562624d5faSMark Brown 
2572624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2582624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2592624d5faSMark Brown 		return -EFAULT;
2602624d5faSMark Brown 	buf[buf_size] = 0;
2612624d5faSMark Brown 
2622624d5faSMark Brown 	while (*start == ' ')
2632624d5faSMark Brown 		start++;
2642624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2652624d5faSMark Brown 	while (*start == ' ')
2662624d5faSMark Brown 		start++;
267b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
268b785a492SJingoo Han 	if (ret)
269b785a492SJingoo Han 		return ret;
2700d51a9cbSMark Brown 
2710d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
272373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2730d51a9cbSMark Brown 
274e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2752624d5faSMark Brown 	return buf_size;
2762624d5faSMark Brown }
2772624d5faSMark Brown 
2782624d5faSMark Brown static const struct file_operations codec_reg_fops = {
279234e3405SStephen Boyd 	.open = simple_open,
2802624d5faSMark Brown 	.read = codec_reg_read_file,
2812624d5faSMark Brown 	.write = codec_reg_write_file,
2826038f373SArnd Bergmann 	.llseek = default_llseek,
2832624d5faSMark Brown };
2842624d5faSMark Brown 
28581c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
286e73f3de5SRussell King {
2876553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
2886553bf06SLars-Peter Clausen 		return;
2896553bf06SLars-Peter Clausen 
29081c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
29181c7cfd1SLars-Peter Clausen 		char *name;
292e73f3de5SRussell King 
29381c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
29481c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
29581c7cfd1SLars-Peter Clausen 		if (name) {
29681c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
29781c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
29881c7cfd1SLars-Peter Clausen 			kfree(name);
29981c7cfd1SLars-Peter Clausen 		}
30081c7cfd1SLars-Peter Clausen 	} else {
30181c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
30281c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
303e73f3de5SRussell King 	}
304e73f3de5SRussell King 
30581c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
30681c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
30781c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3082624d5faSMark Brown 		return;
3092624d5faSMark Brown 	}
3102624d5faSMark Brown 
31181c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
31281c7cfd1SLars-Peter Clausen 		component->debugfs_root);
31381c7cfd1SLars-Peter Clausen 
31481c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
31581c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
31681c7cfd1SLars-Peter Clausen }
31781c7cfd1SLars-Peter Clausen 
31881c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
31981c7cfd1SLars-Peter Clausen {
32081c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
32181c7cfd1SLars-Peter Clausen }
32281c7cfd1SLars-Peter Clausen 
32381c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
32481c7cfd1SLars-Peter Clausen {
32581c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
32681c7cfd1SLars-Peter Clausen 
3272624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
32881c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3292624d5faSMark Brown 						 codec, &codec_reg_fops);
3302624d5faSMark Brown 	if (!codec->debugfs_reg)
33110e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
33210e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
333731f1ab2SSebastien Guiriec }
334731f1ab2SSebastien Guiriec 
335c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
336c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
337c3c5a19aSMark Brown {
338c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3392b194f9dSMark Brown 	ssize_t len, ret = 0;
340c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
341c3c5a19aSMark Brown 
342c3c5a19aSMark Brown 	if (!buf)
343c3c5a19aSMark Brown 		return -ENOMEM;
344c3c5a19aSMark Brown 
34534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
34634e81ab4SLars-Peter Clausen 
3472b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3482b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
349f4333203SLars-Peter Clausen 			       codec->component.name);
3502b194f9dSMark Brown 		if (len >= 0)
3512b194f9dSMark Brown 			ret += len;
3522b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3532b194f9dSMark Brown 			ret = PAGE_SIZE;
3542b194f9dSMark Brown 			break;
3552b194f9dSMark Brown 		}
3562b194f9dSMark Brown 	}
357c3c5a19aSMark Brown 
35834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
35934e81ab4SLars-Peter Clausen 
360c3c5a19aSMark Brown 	if (ret >= 0)
361c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
362c3c5a19aSMark Brown 
363c3c5a19aSMark Brown 	kfree(buf);
364c3c5a19aSMark Brown 
365c3c5a19aSMark Brown 	return ret;
366c3c5a19aSMark Brown }
367c3c5a19aSMark Brown 
368c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
369c3c5a19aSMark Brown 	.read = codec_list_read_file,
370c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
371c3c5a19aSMark Brown };
372c3c5a19aSMark Brown 
373f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
374f3208780SMark Brown 				  size_t count, loff_t *ppos)
375f3208780SMark Brown {
376f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3772b194f9dSMark Brown 	ssize_t len, ret = 0;
3781438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
379f3208780SMark Brown 	struct snd_soc_dai *dai;
380f3208780SMark Brown 
381f3208780SMark Brown 	if (!buf)
382f3208780SMark Brown 		return -ENOMEM;
383f3208780SMark Brown 
38434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
38534e81ab4SLars-Peter Clausen 
3861438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3871438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3881438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3891438c2f6SLars-Peter Clausen 				dai->name);
3902b194f9dSMark Brown 			if (len >= 0)
3912b194f9dSMark Brown 				ret += len;
3922b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3932b194f9dSMark Brown 				ret = PAGE_SIZE;
3942b194f9dSMark Brown 				break;
3952b194f9dSMark Brown 			}
3962b194f9dSMark Brown 		}
3971438c2f6SLars-Peter Clausen 	}
398f3208780SMark Brown 
39934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
40034e81ab4SLars-Peter Clausen 
401f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
402f3208780SMark Brown 
403f3208780SMark Brown 	kfree(buf);
404f3208780SMark Brown 
405f3208780SMark Brown 	return ret;
406f3208780SMark Brown }
407f3208780SMark Brown 
408f3208780SMark Brown static const struct file_operations dai_list_fops = {
409f3208780SMark Brown 	.read = dai_list_read_file,
410f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
411f3208780SMark Brown };
412f3208780SMark Brown 
41319c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41419c7ac27SMark Brown 				       char __user *user_buf,
41519c7ac27SMark Brown 				       size_t count, loff_t *ppos)
41619c7ac27SMark Brown {
41719c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4182b194f9dSMark Brown 	ssize_t len, ret = 0;
41919c7ac27SMark Brown 	struct snd_soc_platform *platform;
42019c7ac27SMark Brown 
42119c7ac27SMark Brown 	if (!buf)
42219c7ac27SMark Brown 		return -ENOMEM;
42319c7ac27SMark Brown 
42434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
42534e81ab4SLars-Peter Clausen 
4262b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4272b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
428f4333203SLars-Peter Clausen 			       platform->component.name);
4292b194f9dSMark Brown 		if (len >= 0)
4302b194f9dSMark Brown 			ret += len;
4312b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4322b194f9dSMark Brown 			ret = PAGE_SIZE;
4332b194f9dSMark Brown 			break;
4342b194f9dSMark Brown 		}
4352b194f9dSMark Brown 	}
43619c7ac27SMark Brown 
43734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
43834e81ab4SLars-Peter Clausen 
43919c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
44019c7ac27SMark Brown 
44119c7ac27SMark Brown 	kfree(buf);
44219c7ac27SMark Brown 
44319c7ac27SMark Brown 	return ret;
44419c7ac27SMark Brown }
44519c7ac27SMark Brown 
44619c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44719c7ac27SMark Brown 	.read = platform_list_read_file,
44819c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
44919c7ac27SMark Brown };
45019c7ac27SMark Brown 
451a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
452a6052154SJarkko Nikula {
4536553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
4546553bf06SLars-Peter Clausen 		return;
4556553bf06SLars-Peter Clausen 
456a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4578a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4583a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
459a6052154SJarkko Nikula 		dev_warn(card->dev,
4607c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4613a45b867SJarkko Nikula 		return;
4623a45b867SJarkko Nikula 	}
4633a45b867SJarkko Nikula 
4643a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4653a45b867SJarkko Nikula 						    card->debugfs_card_root,
4663a45b867SJarkko Nikula 						    &card->pop_time);
4673a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4683a45b867SJarkko Nikula 		dev_warn(card->dev,
469f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
470a6052154SJarkko Nikula }
471a6052154SJarkko Nikula 
472a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
473a6052154SJarkko Nikula {
474a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
475a6052154SJarkko Nikula }
476a6052154SJarkko Nikula 
4776553bf06SLars-Peter Clausen 
4786553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
4796553bf06SLars-Peter Clausen {
4806553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4816553bf06SLars-Peter Clausen 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4826553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
4836553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
4846553bf06SLars-Peter Clausen 		return;
4856553bf06SLars-Peter Clausen 	}
4866553bf06SLars-Peter Clausen 
4876553bf06SLars-Peter Clausen 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4886553bf06SLars-Peter Clausen 				 &codec_list_fops))
4896553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4906553bf06SLars-Peter Clausen 
4916553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4926553bf06SLars-Peter Clausen 				 &dai_list_fops))
4936553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4946553bf06SLars-Peter Clausen 
4956553bf06SLars-Peter Clausen 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4966553bf06SLars-Peter Clausen 				 &platform_list_fops))
4976553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4986553bf06SLars-Peter Clausen }
4996553bf06SLars-Peter Clausen 
5006553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
5016553bf06SLars-Peter Clausen {
5026553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
5036553bf06SLars-Peter Clausen }
5046553bf06SLars-Peter Clausen 
5052624d5faSMark Brown #else
5062624d5faSMark Brown 
50781c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
50881c7cfd1SLars-Peter Clausen 
50981c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
51081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
5112624d5faSMark Brown {
5122624d5faSMark Brown }
5132624d5faSMark Brown 
51481c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
51581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
516731f1ab2SSebastien Guiriec {
517731f1ab2SSebastien Guiriec }
518731f1ab2SSebastien Guiriec 
519b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
520b95fccbcSAxel Lin {
521b95fccbcSAxel Lin }
522b95fccbcSAxel Lin 
523b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
524b95fccbcSAxel Lin {
525b95fccbcSAxel Lin }
5266553bf06SLars-Peter Clausen 
5276553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
5286553bf06SLars-Peter Clausen {
5296553bf06SLars-Peter Clausen }
5306553bf06SLars-Peter Clausen 
5316553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
5326553bf06SLars-Peter Clausen {
5336553bf06SLars-Peter Clausen }
5346553bf06SLars-Peter Clausen 
5352624d5faSMark Brown #endif
5362624d5faSMark Brown 
53747c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
53847c88fffSLiam Girdwood 		const char *dai_link, int stream)
53947c88fffSLiam Girdwood {
5401a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
54147c88fffSLiam Girdwood 
5421a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
5431a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
5441a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
5451a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
54647c88fffSLiam Girdwood 	}
547f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
54847c88fffSLiam Girdwood 	return NULL;
54947c88fffSLiam Girdwood }
55047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
55147c88fffSLiam Girdwood 
5521a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
5531a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
5541a497983SMengdong Lin {
5551a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5561a497983SMengdong Lin 
5571a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
5581a497983SMengdong Lin 	if (!rtd)
5591a497983SMengdong Lin 		return NULL;
5601a497983SMengdong Lin 
5611a497983SMengdong Lin 	rtd->card = card;
5621a497983SMengdong Lin 	rtd->dai_link = dai_link;
5631a497983SMengdong Lin 	rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
5641a497983SMengdong Lin 					dai_link->num_codecs,
5651a497983SMengdong Lin 					GFP_KERNEL);
5661a497983SMengdong Lin 	if (!rtd->codec_dais) {
5671a497983SMengdong Lin 		kfree(rtd);
5681a497983SMengdong Lin 		return NULL;
5691a497983SMengdong Lin 	}
5701a497983SMengdong Lin 
5711a497983SMengdong Lin 	return rtd;
5721a497983SMengdong Lin }
5731a497983SMengdong Lin 
5741a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
5751a497983SMengdong Lin {
5761a497983SMengdong Lin 	if (rtd && rtd->codec_dais)
5771a497983SMengdong Lin 		kfree(rtd->codec_dais);
5781a497983SMengdong Lin 	kfree(rtd);
5791a497983SMengdong Lin }
5801a497983SMengdong Lin 
5811a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
5821a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
5831a497983SMengdong Lin {
5841a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
5851a497983SMengdong Lin 	rtd->num = card->num_rtd;
5861a497983SMengdong Lin 	card->num_rtd++;
5871a497983SMengdong Lin }
5881a497983SMengdong Lin 
5891a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
5901a497983SMengdong Lin {
5911a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
5921a497983SMengdong Lin 
5931a497983SMengdong Lin 	list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
5941a497983SMengdong Lin 		list_del(&rtd->list);
5951a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
5961a497983SMengdong Lin 	}
5971a497983SMengdong Lin 
5981a497983SMengdong Lin 	card->num_rtd = 0;
5991a497983SMengdong Lin }
6001a497983SMengdong Lin 
60147c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
60247c88fffSLiam Girdwood 		const char *dai_link)
60347c88fffSLiam Girdwood {
6041a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
60547c88fffSLiam Girdwood 
6061a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6071a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
6081a497983SMengdong Lin 			return rtd;
60947c88fffSLiam Girdwood 	}
610f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
61147c88fffSLiam Girdwood 	return NULL;
61247c88fffSLiam Girdwood }
61347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
61447c88fffSLiam Girdwood 
6159d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
6169d58a077SRichard Fitzgerald {
6179d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
6189d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
6199d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
6209d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
6219d58a077SRichard Fitzgerald 	 */
6229d58a077SRichard Fitzgerald }
6239d58a077SRichard Fitzgerald 
6246f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
625db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
6266f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
627db2a4165SFrank Mandarino {
6286f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
629d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6301a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6311a497983SMengdong Lin 	int i;
632db2a4165SFrank Mandarino 
633c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
634c5599b87SLars-Peter Clausen 	if (!card->instantiated)
635e3509ff0SDaniel Mack 		return 0;
636e3509ff0SDaniel Mack 
6376ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
6386ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
6396ed25978SAndy Green 	 */
640f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
641f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
642f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
6436ed25978SAndy Green 
6446ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
645f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6466ed25978SAndy Green 
647a00f90f9SMark Brown 	/* mute any active DACs */
6481a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6493efab7dcSMark Brown 
6501a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6513efab7dcSMark Brown 			continue;
6523efab7dcSMark Brown 
6531a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
6541a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
65588bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
65688bd870fSBenoit Cousson 
657f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
658f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
659db2a4165SFrank Mandarino 		}
66088bd870fSBenoit Cousson 	}
661db2a4165SFrank Mandarino 
6624ccab3e7SLiam Girdwood 	/* suspend all pcms */
6631a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6641a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6653efab7dcSMark Brown 			continue;
6663efab7dcSMark Brown 
6671a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
6683efab7dcSMark Brown 	}
6694ccab3e7SLiam Girdwood 
67087506549SMark Brown 	if (card->suspend_pre)
67170b2ac12SMark Brown 		card->suspend_pre(card);
672db2a4165SFrank Mandarino 
6731a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6741a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6753efab7dcSMark Brown 
6761a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6773efab7dcSMark Brown 			continue;
6783efab7dcSMark Brown 
679bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
680f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
681db2a4165SFrank Mandarino 	}
682db2a4165SFrank Mandarino 
68337660b6dSLars-Peter Clausen 	/* close any waiting streams */
6841a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
6851a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
686db2a4165SFrank Mandarino 
6871a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6883efab7dcSMark Brown 
6891a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6903efab7dcSMark Brown 			continue;
6913efab7dcSMark Brown 
6921a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6937bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
694db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
695f0fba2adSLiam Girdwood 
6961a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6977bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
698db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
699db2a4165SFrank Mandarino 	}
700db2a4165SFrank Mandarino 
7018be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7028be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
703e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
704e2d32ff6SMark Brown 
705f0fba2adSLiam Girdwood 	/* suspend all CODECs */
706d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
707d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
708d9fc4063SKuninori Morimoto 		struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
709d9fc4063SKuninori Morimoto 
710d9fc4063SKuninori Morimoto 		if (!codec)
711d9fc4063SKuninori Morimoto 			continue;
7124890140fSLars-Peter Clausen 
7131547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
7141547aba9SMark Brown 		 * bias _ON and should not be suspended. */
715a8093297SLars-Peter Clausen 		if (!codec->suspended) {
7164890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7171547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
718125a25daSMark Brown 				/*
719125a25daSMark Brown 				 * If the CODEC is capable of idle
720125a25daSMark Brown 				 * bias off then being in STANDBY
721125a25daSMark Brown 				 * means it's doing something,
722125a25daSMark Brown 				 * otherwise fall through.
723125a25daSMark Brown 				 */
7244890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
725125a25daSMark Brown 					dev_dbg(codec->dev,
72610e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
727125a25daSMark Brown 					break;
728125a25daSMark Brown 				}
729a8093297SLars-Peter Clausen 
7301547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
731a8093297SLars-Peter Clausen 				if (codec->driver->suspend)
73284b315eeSLars-Peter Clausen 					codec->driver->suspend(codec);
733f0fba2adSLiam Girdwood 				codec->suspended = 1;
734e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
735e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
736988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
737988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
7381547aba9SMark Brown 				break;
7391547aba9SMark Brown 			default:
74010e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
74110e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
7421547aba9SMark Brown 				break;
7431547aba9SMark Brown 			}
7441547aba9SMark Brown 		}
745f0fba2adSLiam Girdwood 	}
746db2a4165SFrank Mandarino 
7471a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7481a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7493efab7dcSMark Brown 
7501a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7513efab7dcSMark Brown 			continue;
7523efab7dcSMark Brown 
753bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
754f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
755988e8cc4SNicolin Chen 
756988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
757988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
758db2a4165SFrank Mandarino 	}
759db2a4165SFrank Mandarino 
76087506549SMark Brown 	if (card->suspend_post)
76170b2ac12SMark Brown 		card->suspend_post(card);
762db2a4165SFrank Mandarino 
763db2a4165SFrank Mandarino 	return 0;
764db2a4165SFrank Mandarino }
7656f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
766db2a4165SFrank Mandarino 
7676ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7686ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7696ed25978SAndy Green  */
7706ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
771db2a4165SFrank Mandarino {
772f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
773f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7741a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
775d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
7761a497983SMengdong Lin 	int i;
777db2a4165SFrank Mandarino 
7786ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7796ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7806ed25978SAndy Green 	 */
7816ed25978SAndy Green 
782f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7836ed25978SAndy Green 
7849949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
785f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7869949788bSMark Brown 
78787506549SMark Brown 	if (card->resume_pre)
78870b2ac12SMark Brown 		card->resume_pre(card);
789db2a4165SFrank Mandarino 
790bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
7911a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7921a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7933efab7dcSMark Brown 
7941a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7953efab7dcSMark Brown 			continue;
7963efab7dcSMark Brown 
797bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
798f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
799db2a4165SFrank Mandarino 	}
800db2a4165SFrank Mandarino 
801d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
802d9fc4063SKuninori Morimoto 		struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
803d9fc4063SKuninori Morimoto 
804d9fc4063SKuninori Morimoto 		if (!codec)
805d9fc4063SKuninori Morimoto 			continue;
806d9fc4063SKuninori Morimoto 
807a8093297SLars-Peter Clausen 		if (codec->suspended) {
808a8093297SLars-Peter Clausen 			if (codec->driver->resume)
809f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
810f0fba2adSLiam Girdwood 			codec->suspended = 0;
8111547aba9SMark Brown 		}
812f0fba2adSLiam Girdwood 	}
813db2a4165SFrank Mandarino 
8141a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8153efab7dcSMark Brown 
8161a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8173efab7dcSMark Brown 			continue;
8183efab7dcSMark Brown 
8191a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
820d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
821db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
822f0fba2adSLiam Girdwood 
8231a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
824d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
825db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
826db2a4165SFrank Mandarino 	}
827db2a4165SFrank Mandarino 
8283ff3f64bSMark Brown 	/* unmute any active DACs */
8291a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8303efab7dcSMark Brown 
8311a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8323efab7dcSMark Brown 			continue;
8333efab7dcSMark Brown 
8341a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8351a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
83688bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
83788bd870fSBenoit Cousson 
838f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
839f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
840db2a4165SFrank Mandarino 		}
84188bd870fSBenoit Cousson 	}
842db2a4165SFrank Mandarino 
8431a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8441a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8453efab7dcSMark Brown 
8461a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8473efab7dcSMark Brown 			continue;
8483efab7dcSMark Brown 
849bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
850f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
851db2a4165SFrank Mandarino 	}
852db2a4165SFrank Mandarino 
85387506549SMark Brown 	if (card->resume_post)
85470b2ac12SMark Brown 		card->resume_post(card);
855db2a4165SFrank Mandarino 
856f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8576ed25978SAndy Green 
8588be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8598be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
860e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8611a7aaa58SJeeja KP 
8621a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
8631a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
8646ed25978SAndy Green }
8656ed25978SAndy Green 
8666ed25978SAndy Green /* powers up audio subsystem after a suspend */
8676f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8686ed25978SAndy Green {
8696f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
870bc263214SLars-Peter Clausen 	bool bus_control = false;
8711a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
872b9dd94a8SPeter Ujfalusi 
873c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
874c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8755ff1ddf2SEric Miao 		return 0;
8765ff1ddf2SEric Miao 
877988e8cc4SNicolin Chen 	/* activate pins from sleep state */
8781a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
87988bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
88088bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
88188bd870fSBenoit Cousson 		int j;
88288bd870fSBenoit Cousson 
883988e8cc4SNicolin Chen 		if (cpu_dai->active)
884988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
88588bd870fSBenoit Cousson 
88688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
88788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
888988e8cc4SNicolin Chen 			if (codec_dai->active)
889988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
890988e8cc4SNicolin Chen 		}
89188bd870fSBenoit Cousson 	}
892988e8cc4SNicolin Chen 
893bc263214SLars-Peter Clausen 	/*
894bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
895bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
896bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
89764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
89864ab9baaSMark Brown 	 */
8991a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
9001a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
901bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
90282e14e8bSStephen Warren 	}
903bc263214SLars-Peter Clausen 	if (bus_control) {
904bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
90564ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
90664ab9baaSMark Brown 	} else {
907f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
9086308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
909f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
910f0fba2adSLiam Girdwood 	}
9116ed25978SAndy Green 
912db2a4165SFrank Mandarino 	return 0;
913db2a4165SFrank Mandarino }
9146f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
915db2a4165SFrank Mandarino #else
9166f8ab4acSMark Brown #define snd_soc_suspend NULL
9176f8ab4acSMark Brown #define snd_soc_resume NULL
918db2a4165SFrank Mandarino #endif
919db2a4165SFrank Mandarino 
92085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
92102a06d30SBarry Song };
92202a06d30SBarry Song 
92365d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
92465d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
92512023a9aSMisael Lopez Cruz {
92665d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
92712023a9aSMisael Lopez Cruz 
92834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
92934e81ab4SLars-Peter Clausen 
93065d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
93165d9361fSLars-Peter Clausen 		if (of_node) {
93265d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
93365d9361fSLars-Peter Clausen 				return component;
93465d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
93565d9361fSLars-Peter Clausen 			return component;
93612023a9aSMisael Lopez Cruz 		}
93712023a9aSMisael Lopez Cruz 	}
93812023a9aSMisael Lopez Cruz 
93912023a9aSMisael Lopez Cruz 	return NULL;
94012023a9aSMisael Lopez Cruz }
94112023a9aSMisael Lopez Cruz 
942fbb88b5cSMengdong Lin /**
943fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
944fbb88b5cSMengdong Lin  *
945fbb88b5cSMengdong Lin  * @dlc: name of the DAI and optional component info to match
946fbb88b5cSMengdong Lin  *
947fbb88b5cSMengdong Lin  * This function will search all regsitered components and their DAIs to
948fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
949fbb88b5cSMengdong Lin  * should also match if being specified.
950fbb88b5cSMengdong Lin  *
951fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
952fbb88b5cSMengdong Lin  */
953305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
95414621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
95512023a9aSMisael Lopez Cruz {
95614621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
95714621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
9583e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
95912023a9aSMisael Lopez Cruz 
96034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
96134e81ab4SLars-Peter Clausen 
96214621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
96314621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
9643e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
9653e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
9663e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
9673e0aa8d8SJyri Sarha 
9683e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
96912023a9aSMisael Lopez Cruz 			continue;
9701ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
97112023a9aSMisael Lopez Cruz 			continue;
97214621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
97314621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
97414621c7eSLars-Peter Clausen 				continue;
97512023a9aSMisael Lopez Cruz 
97614621c7eSLars-Peter Clausen 			return dai;
97712023a9aSMisael Lopez Cruz 		}
97812023a9aSMisael Lopez Cruz 	}
97912023a9aSMisael Lopez Cruz 
98012023a9aSMisael Lopez Cruz 	return NULL;
98112023a9aSMisael Lopez Cruz }
982305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
98312023a9aSMisael Lopez Cruz 
98449a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
98549a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
986db2a4165SFrank Mandarino {
98749a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
98849a5ba1cSMengdong Lin 
98949a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
99049a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
99149a5ba1cSMengdong Lin 			return true;
99249a5ba1cSMengdong Lin 	}
99349a5ba1cSMengdong Lin 
99449a5ba1cSMengdong Lin 	return false;
99549a5ba1cSMengdong Lin }
99649a5ba1cSMengdong Lin 
9976f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
9986f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
999db2a4165SFrank Mandarino {
10001a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
100188bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
100214621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
10031a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
1004435c5e25SMark Brown 	struct snd_soc_platform *platform;
1005848dd8beSMark Brown 	const char *platform_name;
100688bd870fSBenoit Cousson 	int i;
1007db2a4165SFrank Mandarino 
10086f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10096308419aSMark Brown 
101049a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
101149a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
101249a5ba1cSMengdong Lin 			dai_link->name);
101349a5ba1cSMengdong Lin 		return 0;
101449a5ba1cSMengdong Lin 	}
1015db2a4165SFrank Mandarino 
1016513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1017513cb311SSudip Mukherjee 	if (!rtd)
1018513cb311SSudip Mukherjee 		return -ENOMEM;
1019513cb311SSudip Mukherjee 
102014621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
102114621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
102214621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
102314621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1024b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1025f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1026f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
10271a497983SMengdong Lin 		goto _err_defer;
1028c5af3a2eSMark Brown 	}
1029c5af3a2eSMark Brown 
103088bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
103188bd870fSBenoit Cousson 
103288bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
10331a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
103488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
103514621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
103688bd870fSBenoit Cousson 		if (!codec_dais[i]) {
103712023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
103888bd870fSBenoit Cousson 				codecs[i].dai_name);
10391a497983SMengdong Lin 			goto _err_defer;
104012023a9aSMisael Lopez Cruz 		}
104188bd870fSBenoit Cousson 	}
104288bd870fSBenoit Cousson 
104388bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
104488bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
104588bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
104612023a9aSMisael Lopez Cruz 
1047848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1048848dd8beSMark Brown 	platform_name = dai_link->platform_name;
10495a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1050848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1051848dd8beSMark Brown 
1052b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
1053f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
10545a504963SStephen Warren 		if (dai_link->platform_of_node) {
10555a504963SStephen Warren 			if (platform->dev->of_node !=
10565a504963SStephen Warren 			    dai_link->platform_of_node)
10575a504963SStephen Warren 				continue;
10585a504963SStephen Warren 		} else {
1059f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
10602610ab77SStephen Warren 				continue;
10615a504963SStephen Warren 		}
10622610ab77SStephen Warren 
1063f0fba2adSLiam Girdwood 		rtd->platform = platform;
1064f0fba2adSLiam Girdwood 	}
1065b19e6e7bSMark Brown 	if (!rtd->platform) {
1066f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1067f0fba2adSLiam Girdwood 			dai_link->platform_name);
106870fcad49SCharles Keepax 		goto _err_defer;
1069f0fba2adSLiam Girdwood 	}
1070b19e6e7bSMark Brown 
10711a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1072b19e6e7bSMark Brown 	return 0;
10731a497983SMengdong Lin 
10741a497983SMengdong Lin _err_defer:
10751a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
10761a497983SMengdong Lin 	return  -EPROBE_DEFER;
10776b05eda6SMark Brown }
10786b05eda6SMark Brown 
1079f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1080d12cd198SStephen Warren {
1081abd31b32SLars-Peter Clausen 	if (!component->card)
108270090bbbSLars-Peter Clausen 		return;
1083d12cd198SStephen Warren 
1084d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1085d12cd198SStephen Warren 
1086f1d45cc3SLars-Peter Clausen 	if (component->remove)
1087f1d45cc3SLars-Peter Clausen 		component->remove(component);
1088d12cd198SStephen Warren 
1089f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1090d12cd198SStephen Warren 
1091f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1092abd31b32SLars-Peter Clausen 	component->card = NULL;
1093f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1094d12cd198SStephen Warren }
1095d12cd198SStephen Warren 
1096e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1097589c3563SJarkko Nikula {
1098589c3563SJarkko Nikula 	int err;
1099589c3563SJarkko Nikula 
1100e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1101e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1102e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1103e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1104589c3563SJarkko Nikula 			if (err < 0)
1105e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1106b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1107e60cd14fSLars-Peter Clausen 					dai->name, err);
1108b0aa88afSMisael Lopez Cruz 		}
1109e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1110b0aa88afSMisael Lopez Cruz 	}
1111b0aa88afSMisael Lopez Cruz }
1112b0aa88afSMisael Lopez Cruz 
11131a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11141a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1115f0fba2adSLiam Girdwood {
1116e60cd14fSLars-Peter Clausen 	int i;
1117f0fba2adSLiam Girdwood 
1118f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1119f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
112036ae1a96SMark Brown 		device_unregister(rtd->dev);
1121f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
112202a06d30SBarry Song 	}
112302a06d30SBarry Song 
1124f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
112588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1126e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1127f0fba2adSLiam Girdwood 
1128e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1129f0fba2adSLiam Girdwood }
1130f0fba2adSLiam Girdwood 
11311a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11321a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
113362ae68faSStephen Warren {
113462ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
113562ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
113661aca564SLars-Peter Clausen 	struct snd_soc_component *component;
113788bd870fSBenoit Cousson 	int i;
113862ae68faSStephen Warren 
113962ae68faSStephen Warren 	/* remove the platform */
114070090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1141f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
114262ae68faSStephen Warren 
114362ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
114488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
114561aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
114670090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
114761aca564SLars-Peter Clausen 			soc_remove_component(component);
114862ae68faSStephen Warren 	}
114962ae68faSStephen Warren 
115062ae68faSStephen Warren 	/* remove any CPU-side CODEC */
115162ae68faSStephen Warren 	if (cpu_dai) {
115270090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
115361aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
115462ae68faSStephen Warren 	}
115562ae68faSStephen Warren }
115662ae68faSStephen Warren 
11570671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11580671fd8eSKuninori Morimoto {
11591a497983SMengdong Lin 	int order;
11601a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1161f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
11620671fd8eSKuninori Morimoto 
11630168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
11640168bf0dSLiam Girdwood 			order++) {
11651a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
11661a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
11670168bf0dSLiam Girdwood 	}
116862ae68faSStephen Warren 
116962ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
117062ae68faSStephen Warren 			order++) {
11711a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
11721a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
117362ae68faSStephen Warren 	}
117462ae68faSStephen Warren 
1175f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1176f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1177f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1178f8f80361SMengdong Lin 				link->name);
1179f8f80361SMengdong Lin 
1180f8f80361SMengdong Lin 		list_del(&link->list);
1181f8f80361SMengdong Lin 		card->num_dai_links--;
11820671fd8eSKuninori Morimoto 	}
11830671fd8eSKuninori Morimoto }
11840671fd8eSKuninori Morimoto 
1185923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1186923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1187923c5e61SMengdong Lin {
1188923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1189923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1190923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1191923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1192923c5e61SMengdong Lin 
1193923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1194923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1195923c5e61SMengdong Lin 				GFP_KERNEL);
1196923c5e61SMengdong Lin 		if (!dai_link->codecs)
1197923c5e61SMengdong Lin 			return -ENOMEM;
1198923c5e61SMengdong Lin 
1199923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1200923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1201923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1202923c5e61SMengdong Lin 	}
1203923c5e61SMengdong Lin 
1204923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1205923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1206923c5e61SMengdong Lin 		return -EINVAL;
1207923c5e61SMengdong Lin 	}
1208923c5e61SMengdong Lin 
1209923c5e61SMengdong Lin 	return 0;
1210923c5e61SMengdong Lin }
1211923c5e61SMengdong Lin 
1212923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1213923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1214923c5e61SMengdong Lin {
1215923c5e61SMengdong Lin 	int i, ret;
1216923c5e61SMengdong Lin 
1217923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1218923c5e61SMengdong Lin 	if (ret) {
1219923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1220923c5e61SMengdong Lin 		return ret;
1221923c5e61SMengdong Lin 	}
1222923c5e61SMengdong Lin 
1223923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1224923c5e61SMengdong Lin 		/*
1225923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1226923c5e61SMengdong Lin 		 * not both or neither.
1227923c5e61SMengdong Lin 		 */
1228923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1229923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1230923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1231923c5e61SMengdong Lin 				link->name);
1232923c5e61SMengdong Lin 			return -EINVAL;
1233923c5e61SMengdong Lin 		}
1234923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1235923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1236923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1237923c5e61SMengdong Lin 				link->name);
1238923c5e61SMengdong Lin 			return -EINVAL;
1239923c5e61SMengdong Lin 		}
1240923c5e61SMengdong Lin 	}
1241923c5e61SMengdong Lin 
1242923c5e61SMengdong Lin 	/*
1243923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1244923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1245923c5e61SMengdong Lin 	 */
1246923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1247923c5e61SMengdong Lin 		dev_err(card->dev,
1248923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1249923c5e61SMengdong Lin 			link->name);
1250923c5e61SMengdong Lin 		return -EINVAL;
1251923c5e61SMengdong Lin 	}
1252923c5e61SMengdong Lin 
1253923c5e61SMengdong Lin 	/*
1254923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1255923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1256923c5e61SMengdong Lin 	 * name alone..
1257923c5e61SMengdong Lin 	 */
1258923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1259923c5e61SMengdong Lin 		dev_err(card->dev,
1260923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1261923c5e61SMengdong Lin 			link->name);
1262923c5e61SMengdong Lin 		return -EINVAL;
1263923c5e61SMengdong Lin 	}
1264923c5e61SMengdong Lin 	/*
1265923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1266923c5e61SMengdong Lin 	 * specified
1267923c5e61SMengdong Lin 	 */
1268923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1269923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1270923c5e61SMengdong Lin 		dev_err(card->dev,
1271923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1272923c5e61SMengdong Lin 			link->name);
1273923c5e61SMengdong Lin 		return -EINVAL;
1274923c5e61SMengdong Lin 	}
1275923c5e61SMengdong Lin 
1276923c5e61SMengdong Lin 	return 0;
1277923c5e61SMengdong Lin }
1278923c5e61SMengdong Lin 
1279f8f80361SMengdong Lin /**
1280f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1281f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1282f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1283f8f80361SMengdong Lin  *
1284f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1285f8f80361SMengdong Lin  *
1286f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1287f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1288f8f80361SMengdong Lin  * DAI links in dai_link array.
1289f8f80361SMengdong Lin  */
1290f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1291f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1292f8f80361SMengdong Lin {
1293f8f80361SMengdong Lin 	if (dai_link->dobj.type
1294f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1295f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1296f8f80361SMengdong Lin 			dai_link->dobj.type);
1297f8f80361SMengdong Lin 		return -EINVAL;
1298f8f80361SMengdong Lin 	}
1299f8f80361SMengdong Lin 
1300f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1301d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1302d6f220eaSMengdong Lin 	 * on the link created by topology.
1303d6f220eaSMengdong Lin 	 */
1304d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1305d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1306d6f220eaSMengdong Lin 
1307f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1308f8f80361SMengdong Lin 	card->num_dai_links++;
1309f8f80361SMengdong Lin 
1310f8f80361SMengdong Lin 	return 0;
1311f8f80361SMengdong Lin }
1312f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1313f8f80361SMengdong Lin 
1314f8f80361SMengdong Lin /**
1315f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1316f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1317f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1318f8f80361SMengdong Lin  *
1319f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1320f8f80361SMengdong Lin  *
1321f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1322f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1323f8f80361SMengdong Lin  */
1324f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1325f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1326f8f80361SMengdong Lin {
1327f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1328f8f80361SMengdong Lin 
1329f8f80361SMengdong Lin 	if (dai_link->dobj.type
1330f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1331f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1332f8f80361SMengdong Lin 			dai_link->dobj.type);
1333f8f80361SMengdong Lin 		return;
1334f8f80361SMengdong Lin 	}
1335f8f80361SMengdong Lin 
1336f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1337d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1338d6f220eaSMengdong Lin 	 * on the link created by topology.
1339d6f220eaSMengdong Lin 	 */
1340d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1341d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1342d6f220eaSMengdong Lin 
1343f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1344f8f80361SMengdong Lin 		if (link == dai_link) {
1345f8f80361SMengdong Lin 			list_del(&link->list);
1346f8f80361SMengdong Lin 			card->num_dai_links--;
1347f8f80361SMengdong Lin 			return;
1348f8f80361SMengdong Lin 		}
1349f8f80361SMengdong Lin 	}
1350f8f80361SMengdong Lin }
1351f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1352f0fba2adSLiam Girdwood 
1353ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
135494f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1355ead9b919SJarkko Nikula {
1356ead9b919SJarkko Nikula 	int i;
1357ead9b919SJarkko Nikula 
1358ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1359ead9b919SJarkko Nikula 		return;
1360ead9b919SJarkko Nikula 
1361ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1362ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
136394f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
13643ca041edSSebastian Reichel 			continue;
136594f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
13663ca041edSSebastian Reichel 			continue;
136794f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1368ead9b919SJarkko Nikula 		break;
1369ead9b919SJarkko Nikula 	}
1370ead9b919SJarkko Nikula }
1371ead9b919SJarkko Nikula 
1372f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1373f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1374589c3563SJarkko Nikula {
1375f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1376888df395SMark Brown 	struct snd_soc_dai *dai;
1377f1d45cc3SLars-Peter Clausen 	int ret;
1378589c3563SJarkko Nikula 
13791b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
138070090bbbSLars-Peter Clausen 		return 0;
1381589c3563SJarkko Nikula 
1382abd31b32SLars-Peter Clausen 	if (component->card) {
13831b7c1231SLars-Peter Clausen 		if (component->card != card) {
13841b7c1231SLars-Peter Clausen 			dev_err(component->dev,
13851b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
13861b7c1231SLars-Peter Clausen 				card->name, component->card->name);
13871b7c1231SLars-Peter Clausen 			return -ENODEV;
13881b7c1231SLars-Peter Clausen 		}
13891b7c1231SLars-Peter Clausen 		return 0;
13901b7c1231SLars-Peter Clausen 	}
13911b7c1231SLars-Peter Clausen 
1392abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1393abd31b32SLars-Peter Clausen 		return -ENODEV;
1394abd31b32SLars-Peter Clausen 
1395f1d45cc3SLars-Peter Clausen 	component->card = card;
1396f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1397f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1398589c3563SJarkko Nikula 
1399f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1400d5d1e0beSLars-Peter Clausen 
1401f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1402f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1403f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
140477530150SLars-Peter Clausen 
1405b318ad50SNariman Poushin 		if (ret != 0) {
1406f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1407b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1408b318ad50SNariman Poushin 			goto err_probe;
1409b318ad50SNariman Poushin 		}
1410b318ad50SNariman Poushin 	}
1411b318ad50SNariman Poushin 
14120634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
14130634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1414261edc70SNariman Poushin 		if (ret != 0) {
1415f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1416261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1417261edc70SNariman Poushin 			goto err_probe;
1418261edc70SNariman Poushin 		}
1419261edc70SNariman Poushin 	}
1420888df395SMark Brown 
1421f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1422f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1423589c3563SJarkko Nikula 		if (ret < 0) {
1424f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1425f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
142670d29331SJarkko Nikula 			goto err_probe;
1427589c3563SJarkko Nikula 		}
1428cb2cf612SLiam Girdwood 
1429f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1430f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1431956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1432f1d45cc3SLars-Peter Clausen 			component->name);
1433956245e9SLiam Girdwood 	}
1434956245e9SLiam Girdwood 
1435f2ed6b07SMengdong Lin 	/* machine specific init */
1436f2ed6b07SMengdong Lin 	if (component->init) {
1437f2ed6b07SMengdong Lin 		ret = component->init(component);
1438f2ed6b07SMengdong Lin 		if (ret < 0) {
1439f2ed6b07SMengdong Lin 			dev_err(component->dev,
1440f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1441f2ed6b07SMengdong Lin 			goto err_probe;
1442f2ed6b07SMengdong Lin 		}
1443f2ed6b07SMengdong Lin 	}
1444f2ed6b07SMengdong Lin 
1445f1d45cc3SLars-Peter Clausen 	if (component->controls)
1446f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1447f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1448f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1449f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1450f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1451956245e9SLiam Girdwood 
1452f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1453d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1454956245e9SLiam Girdwood 
1455956245e9SLiam Girdwood 	return 0;
1456956245e9SLiam Girdwood 
1457956245e9SLiam Girdwood err_probe:
1458f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1459abd31b32SLars-Peter Clausen 	component->card = NULL;
1460f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1461956245e9SLiam Girdwood 
1462956245e9SLiam Girdwood 	return ret;
1463956245e9SLiam Girdwood }
1464956245e9SLiam Girdwood 
146536ae1a96SMark Brown static void rtd_release(struct device *dev)
146636ae1a96SMark Brown {
146736ae1a96SMark Brown 	kfree(dev);
146836ae1a96SMark Brown }
1469f0fba2adSLiam Girdwood 
14705f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
14715f3484acSLars-Peter Clausen 	const char *name)
1472503ae5e0SMisael Lopez Cruz {
1473589c3563SJarkko Nikula 	int ret = 0;
1474589c3563SJarkko Nikula 
1475589c3563SJarkko Nikula 	/* register the rtd device */
147636ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
147736ae1a96SMark Brown 	if (!rtd->dev)
147836ae1a96SMark Brown 		return -ENOMEM;
147936ae1a96SMark Brown 	device_initialize(rtd->dev);
14805f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
148136ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1482d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1483f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
148436ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1485b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
148601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
148701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
148801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
148901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
149036ae1a96SMark Brown 	ret = device_add(rtd->dev);
1491589c3563SJarkko Nikula 	if (ret < 0) {
1492865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1493865df9cbSChuansheng Liu 		put_device(rtd->dev);
14945f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1495f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1496589c3563SJarkko Nikula 		return ret;
1497589c3563SJarkko Nikula 	}
1498589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1499589c3563SJarkko Nikula 	return 0;
1500589c3563SJarkko Nikula }
1501589c3563SJarkko Nikula 
15021a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
15031a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
150462ae68faSStephen Warren 				     int order)
150562ae68faSStephen Warren {
150662ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1507f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
150888bd870fSBenoit Cousson 	int i, ret;
150962ae68faSStephen Warren 
151062ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
151161aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
151270090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1513f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
151462ae68faSStephen Warren 		if (ret < 0)
151562ae68faSStephen Warren 			return ret;
151662ae68faSStephen Warren 	}
151762ae68faSStephen Warren 
151888bd870fSBenoit Cousson 	/* probe the CODEC-side components */
151988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
152061aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
152170090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1522f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
152362ae68faSStephen Warren 			if (ret < 0)
152462ae68faSStephen Warren 				return ret;
152562ae68faSStephen Warren 		}
152688bd870fSBenoit Cousson 	}
152762ae68faSStephen Warren 
152862ae68faSStephen Warren 	/* probe the platform */
152970090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1530f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
153162ae68faSStephen Warren 		if (ret < 0)
153262ae68faSStephen Warren 			return ret;
153362ae68faSStephen Warren 	}
153462ae68faSStephen Warren 
153562ae68faSStephen Warren 	return 0;
153662ae68faSStephen Warren }
153762ae68faSStephen Warren 
15388e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1539b0aa88afSMisael Lopez Cruz {
1540b0aa88afSMisael Lopez Cruz 	int ret;
1541b0aa88afSMisael Lopez Cruz 
15428e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
15438e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
15448e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1545b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
15468e2be562SLars-Peter Clausen 				dev_err(dai->dev,
15478e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
15488e2be562SLars-Peter Clausen 					dai->name, ret);
1549b0aa88afSMisael Lopez Cruz 				return ret;
1550b0aa88afSMisael Lopez Cruz 			}
1551b0aa88afSMisael Lopez Cruz 		}
1552b0aa88afSMisael Lopez Cruz 
15538e2be562SLars-Peter Clausen 		dai->probed = 1;
1554b0aa88afSMisael Lopez Cruz 	}
1555b0aa88afSMisael Lopez Cruz 
1556b0aa88afSMisael Lopez Cruz 	return 0;
1557b0aa88afSMisael Lopez Cruz }
1558b0aa88afSMisael Lopez Cruz 
15592436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
15602436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
15613f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
15622436a723SMisael Lopez Cruz {
15633f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
15643f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
15653de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
15662436a723SMisael Lopez Cruz 	int ret;
15672436a723SMisael Lopez Cruz 
156888bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
156988bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
157088bd870fSBenoit Cousson 
15712436a723SMisael Lopez Cruz 	/* link the DAI widgets */
15723de7c420SVinod Koul 	sink = codec_dai->playback_widget;
15733de7c420SVinod Koul 	source = cpu_dai->capture_widget;
15743de7c420SVinod Koul 	if (sink && source) {
15752436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
15763de7c420SVinod Koul 					   dai_link->num_params,
15773de7c420SVinod Koul 					   source, sink);
15782436a723SMisael Lopez Cruz 		if (ret != 0) {
15792436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
15803de7c420SVinod Koul 				sink->name, source->name, ret);
15812436a723SMisael Lopez Cruz 			return ret;
15822436a723SMisael Lopez Cruz 		}
15832436a723SMisael Lopez Cruz 	}
15842436a723SMisael Lopez Cruz 
15853de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
15863de7c420SVinod Koul 	source = codec_dai->capture_widget;
15873de7c420SVinod Koul 	if (sink && source) {
15882436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
15893de7c420SVinod Koul 					   dai_link->num_params,
15903de7c420SVinod Koul 					   source, sink);
15912436a723SMisael Lopez Cruz 		if (ret != 0) {
15922436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
15933de7c420SVinod Koul 				sink->name, source->name, ret);
15942436a723SMisael Lopez Cruz 			return ret;
15952436a723SMisael Lopez Cruz 		}
15962436a723SMisael Lopez Cruz 	}
15972436a723SMisael Lopez Cruz 
15982436a723SMisael Lopez Cruz 	return 0;
15992436a723SMisael Lopez Cruz }
16002436a723SMisael Lopez Cruz 
16011a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
16021a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1603f0fba2adSLiam Girdwood {
16041a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1605c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
160688bd870fSBenoit Cousson 	int i, ret;
1607f0fba2adSLiam Girdwood 
1608f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
16091a497983SMengdong Lin 			card->name, rtd->num, order);
1610f0fba2adSLiam Girdwood 
1611f0fba2adSLiam Girdwood 	/* set default power off timeout */
1612f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1613f0fba2adSLiam Girdwood 
16148e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
16158e2be562SLars-Peter Clausen 	if (ret)
1616f0fba2adSLiam Girdwood 		return ret;
1617f0fba2adSLiam Girdwood 
1618f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
161988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
16208e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1621b0aa88afSMisael Lopez Cruz 		if (ret)
1622f0fba2adSLiam Girdwood 			return ret;
162388bd870fSBenoit Cousson 	}
1624f0fba2adSLiam Girdwood 
16250168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
16260168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
16270168bf0dSLiam Girdwood 		return 0;
16280168bf0dSLiam Girdwood 
16295f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16305f3484acSLars-Peter Clausen 	if (dai_link->init) {
16315f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
16325f3484acSLars-Peter Clausen 		if (ret < 0) {
16335f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16345f3484acSLars-Peter Clausen 				dai_link->name, ret);
16355f3484acSLars-Peter Clausen 			return ret;
16365f3484acSLars-Peter Clausen 		}
16375f3484acSLars-Peter Clausen 	}
16385f3484acSLars-Peter Clausen 
1639a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1640a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1641a5053a8eSKuninori Morimoto 
16425f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1643589c3563SJarkko Nikula 	if (ret)
1644f0fba2adSLiam Girdwood 		return ret;
1645f0fba2adSLiam Girdwood 
16465f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
16475f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
16482e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
16492e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
16505f3484acSLars-Peter Clausen #endif
16515f3484acSLars-Peter Clausen 
16526f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
16531245b700SNamarta Kohli 		/*create compress_device"*/
16541a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
16551245b700SNamarta Kohli 		if (ret < 0) {
1656f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
16571245b700SNamarta Kohli 					 dai_link->stream_name);
16581245b700SNamarta Kohli 			return ret;
16591245b700SNamarta Kohli 		}
16601245b700SNamarta Kohli 	} else {
16611245b700SNamarta Kohli 
1662c74184edSMark Brown 		if (!dai_link->params) {
1663f0fba2adSLiam Girdwood 			/* create the pcm */
16641a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1665f0fba2adSLiam Girdwood 			if (ret < 0) {
1666f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
16670837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1668f0fba2adSLiam Girdwood 				return ret;
1669f0fba2adSLiam Girdwood 			}
1670c74184edSMark Brown 		} else {
16719d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
16729d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
16739d58a077SRichard Fitzgerald 
1674c74184edSMark Brown 			/* link the DAI widgets */
16753f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
16762436a723SMisael Lopez Cruz 			if (ret)
1677c74184edSMark Brown 				return ret;
1678c74184edSMark Brown 		}
1679c74184edSMark Brown 	}
1680c74184edSMark Brown 
1681f0fba2adSLiam Girdwood 	return 0;
1682f0fba2adSLiam Girdwood }
1683f0fba2adSLiam Girdwood 
168444c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1685b19e6e7bSMark Brown {
16863ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1687f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1688f2ed6b07SMengdong Lin 	const char *name;
1689f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
16903ca041edSSebastian Reichel 
1691f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1692f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1693f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1694f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1695f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1696f2ed6b07SMengdong Lin 		if (!component) {
1697f2ed6b07SMengdong Lin 			if (codec_of_node)
1698f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1699f2ed6b07SMengdong Lin 			goto err_defer;
1700f2ed6b07SMengdong Lin 		}
1701f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1702f2ed6b07SMengdong Lin 		/* generic components */
1703f2ed6b07SMengdong Lin 		name = aux_dev->name;
1704f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1705f2ed6b07SMengdong Lin 		if (!component)
1706f2ed6b07SMengdong Lin 			goto err_defer;
1707f2ed6b07SMengdong Lin 	} else {
1708f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1709f2ed6b07SMengdong Lin 		return -EINVAL;
1710f2ed6b07SMengdong Lin 	}
17113ca041edSSebastian Reichel 
1712f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1713*1a653aa4SKuninori Morimoto 	component->auxiliary = 1;
1714*1a653aa4SKuninori Morimoto 
1715f2ed6b07SMengdong Lin 	return 0;
1716f2ed6b07SMengdong Lin 
1717f2ed6b07SMengdong Lin err_defer:
171865d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1719b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1720b19e6e7bSMark Brown }
1721b19e6e7bSMark Brown 
1722f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1723f2ed6b07SMengdong Lin {
1724f2ed6b07SMengdong Lin 	struct snd_soc_component *comp;
1725f2ed6b07SMengdong Lin 	int order;
1726f2ed6b07SMengdong Lin 	int ret;
1727f2ed6b07SMengdong Lin 
1728f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1729f2ed6b07SMengdong Lin 		order++) {
1730*1a653aa4SKuninori Morimoto 		list_for_each_entry(comp, &card->component_dev_list, card_list) {
1731*1a653aa4SKuninori Morimoto 			if (!comp->auxiliary)
1732*1a653aa4SKuninori Morimoto 				continue;
1733*1a653aa4SKuninori Morimoto 
1734f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1735f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1736f2ed6b07SMengdong Lin 				if (ret < 0) {
1737f2ed6b07SMengdong Lin 					dev_err(card->dev,
1738f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1739f2ed6b07SMengdong Lin 						comp->name, ret);
1740f2ed6b07SMengdong Lin 					return ret;
1741f2ed6b07SMengdong Lin 				}
1742f2ed6b07SMengdong Lin 			}
1743f2ed6b07SMengdong Lin 		}
1744f2ed6b07SMengdong Lin 	}
174565d9361fSLars-Peter Clausen 
174644c69bb1SLars-Peter Clausen 	return 0;
17473ca041edSSebastian Reichel }
17482eea392dSJarkko Nikula 
1749f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
175044c69bb1SLars-Peter Clausen {
1751f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1752f2ed6b07SMengdong Lin 	int order;
175344c69bb1SLars-Peter Clausen 
1754f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1755f2ed6b07SMengdong Lin 		order++) {
1756f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1757*1a653aa4SKuninori Morimoto 			&card->component_dev_list, card_list) {
1758*1a653aa4SKuninori Morimoto 
1759*1a653aa4SKuninori Morimoto 			if (!comp->auxiliary)
1760*1a653aa4SKuninori Morimoto 				continue;
1761*1a653aa4SKuninori Morimoto 
1762f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1763f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1764*1a653aa4SKuninori Morimoto 				comp->auxiliary = 0;
17652eea392dSJarkko Nikula 			}
17665f3484acSLars-Peter Clausen 		}
17672eea392dSJarkko Nikula 	}
17682eea392dSJarkko Nikula }
17692eea392dSJarkko Nikula 
1770f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1771fdf0f54dSDimitris Papastamos {
1772fdf0f54dSDimitris Papastamos 	int ret;
1773fdf0f54dSDimitris Papastamos 
1774fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1775fdf0f54dSDimitris Papastamos 		return 0;
1776fdf0f54dSDimitris Papastamos 
1777fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1778fdf0f54dSDimitris Papastamos 	if (ret < 0) {
177910e8aa9aSMichał Mirosław 		dev_err(codec->dev,
178010e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
178110e8aa9aSMichał Mirosław 			ret);
1782fdf0f54dSDimitris Papastamos 		return ret;
1783fdf0f54dSDimitris Papastamos 	}
1784fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1785fdf0f54dSDimitris Papastamos 	return 0;
1786fdf0f54dSDimitris Papastamos }
1787fdf0f54dSDimitris Papastamos 
1788ce64c8b9SLars-Peter Clausen /**
1789ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1790ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1791ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1792ce64c8b9SLars-Peter Clausen  *
1793ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1794ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1795ce64c8b9SLars-Peter Clausen  *
1796ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1797ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1798ce64c8b9SLars-Peter Clausen  *
1799ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1800ce64c8b9SLars-Peter Clausen  */
1801ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1802ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1803ce64c8b9SLars-Peter Clausen {
1804ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1805ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1806ce64c8b9SLars-Peter Clausen 	unsigned int i;
1807ce64c8b9SLars-Peter Clausen 	int ret;
1808ce64c8b9SLars-Peter Clausen 
1809ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1810ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1811ce64c8b9SLars-Peter Clausen 
1812ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1813ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1814ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1815ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1816ce64c8b9SLars-Peter Clausen 			return ret;
1817ce64c8b9SLars-Peter Clausen 		}
1818ce64c8b9SLars-Peter Clausen 	}
1819ce64c8b9SLars-Peter Clausen 
1820ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1821ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1822ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1823ce64c8b9SLars-Peter Clausen 
1824ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1825ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1826ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1827ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1828ce64c8b9SLars-Peter Clausen 			break;
1829ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1830ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1831ce64c8b9SLars-Peter Clausen 			break;
1832ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1833ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1834ce64c8b9SLars-Peter Clausen 			break;
1835ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1836ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1837ce64c8b9SLars-Peter Clausen 			break;
1838ce64c8b9SLars-Peter Clausen 		}
1839ce64c8b9SLars-Peter Clausen 
1840ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1841ce64c8b9SLars-Peter Clausen 	}
1842ce64c8b9SLars-Peter Clausen 
1843ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1844ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1845ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1846ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1847ce64c8b9SLars-Peter Clausen 		return ret;
1848ce64c8b9SLars-Peter Clausen 	}
1849ce64c8b9SLars-Peter Clausen 
1850ce64c8b9SLars-Peter Clausen 	return 0;
1851ce64c8b9SLars-Peter Clausen }
1852ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1853ce64c8b9SLars-Peter Clausen 
1854b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1855f0fba2adSLiam Girdwood {
1856fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
18571a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
185861b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1859ce64c8b9SLars-Peter Clausen 	int ret, i, order;
186096dd3622SMark Brown 
186134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
186201b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1863f0fba2adSLiam Girdwood 
1864b19e6e7bSMark Brown 	/* bind DAIs */
1865b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
18666f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
1867b19e6e7bSMark Brown 		if (ret != 0)
1868b19e6e7bSMark Brown 			goto base_error;
1869db2a4165SFrank Mandarino 	}
1870db2a4165SFrank Mandarino 
187144c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1872b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
187344c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1874b19e6e7bSMark Brown 		if (ret != 0)
1875b19e6e7bSMark Brown 			goto base_error;
1876db2a4165SFrank Mandarino 	}
1877db2a4165SFrank Mandarino 
1878f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1879f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
1880f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
1881f8f80361SMengdong Lin 
1882fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1883fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1884fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1885fdf0f54dSDimitris Papastamos 			continue;
1886f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1887b19e6e7bSMark Brown 		if (ret < 0)
1888b19e6e7bSMark Brown 			goto base_error;
1889fdf0f54dSDimitris Papastamos 	}
1890fdf0f54dSDimitris Papastamos 
1891f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1892102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1893f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1894f0fba2adSLiam Girdwood 	if (ret < 0) {
189510e8aa9aSMichał Mirosław 		dev_err(card->dev,
189610e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
189710e8aa9aSMichał Mirosław 			card->name, ret);
1898b19e6e7bSMark Brown 		goto base_error;
1899db2a4165SFrank Mandarino 	}
1900db2a4165SFrank Mandarino 
19010757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19020757d834SLars-Peter Clausen 
1903e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1904e37a4970SMark Brown 	card->dapm.dev = card->dev;
1905e37a4970SMark Brown 	card->dapm.card = card;
1906e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1907e37a4970SMark Brown 
1908d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1909d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1910d5d1e0beSLars-Peter Clausen #endif
1911d5d1e0beSLars-Peter Clausen 
191288ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
19136ed25978SAndy Green 	/* deferred resume work */
19146308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
19151301a964SRandy Dunlap #endif
19166ed25978SAndy Green 
19179a841ebbSMark Brown 	if (card->dapm_widgets)
19189a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19199a841ebbSMark Brown 					  card->num_dapm_widgets);
19209a841ebbSMark Brown 
1921f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
1922f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1923f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
1924f23e860eSNicolin Chen 
1925f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1926f0fba2adSLiam Girdwood 	if (card->probe) {
1927e7361ec4SMark Brown 		ret = card->probe(card);
1928f0fba2adSLiam Girdwood 		if (ret < 0)
1929f0fba2adSLiam Girdwood 			goto card_probe_error;
1930f0fba2adSLiam Girdwood 	}
1931f0fba2adSLiam Girdwood 
193262ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
19330168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
19340168bf0dSLiam Girdwood 			order++) {
19351a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
19361a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
193762ae68faSStephen Warren 			if (ret < 0) {
1938f110bfc7SLiam Girdwood 				dev_err(card->dev,
1939f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1940f110bfc7SLiam Girdwood 					ret);
194162ae68faSStephen Warren 				goto probe_dai_err;
194262ae68faSStephen Warren 			}
194362ae68faSStephen Warren 		}
194462ae68faSStephen Warren 	}
194562ae68faSStephen Warren 
1946f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1947f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
1948f2ed6b07SMengdong Lin 	if (ret < 0)
1949f2ed6b07SMengdong Lin 		goto probe_dai_err;
1950f2ed6b07SMengdong Lin 
195161b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
195261b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
195361b0088bSMengdong Lin 	 */
195461b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
195561b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
195661b0088bSMengdong Lin 			continue;
195761b0088bSMengdong Lin 
195861b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
195961b0088bSMengdong Lin 		if (ret)
196061b0088bSMengdong Lin 			goto probe_dai_err;
196161b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
196261b0088bSMengdong Lin 		if (ret)
196361b0088bSMengdong Lin 			goto probe_dai_err;
196461b0088bSMengdong Lin 	}
196561b0088bSMengdong Lin 
196662ae68faSStephen Warren 	/* probe all DAI links on this card */
196762ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
196862ae68faSStephen Warren 			order++) {
19691a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
19701a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
1971fe3e78e0SMark Brown 			if (ret < 0) {
1972f110bfc7SLiam Girdwood 				dev_err(card->dev,
1973f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1974f110bfc7SLiam Girdwood 					ret);
1975f0fba2adSLiam Girdwood 				goto probe_dai_err;
1976fe3e78e0SMark Brown 			}
1977fe3e78e0SMark Brown 		}
19780168bf0dSLiam Girdwood 	}
1979fe3e78e0SMark Brown 
1980888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1981b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1982888df395SMark Brown 
1983b7af1dafSMark Brown 	if (card->controls)
1984022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1985b7af1dafSMark Brown 
1986b8ad29deSMark Brown 	if (card->dapm_routes)
1987b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1988b8ad29deSMark Brown 					card->num_dapm_routes);
1989b8ad29deSMark Brown 
1990f23e860eSNicolin Chen 	if (card->of_dapm_routes)
1991f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1992f23e860eSNicolin Chen 					card->num_of_dapm_routes);
199375d9ac46SMark Brown 
1994f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1995fe3e78e0SMark Brown 		 "%s", card->name);
1996f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
199722de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1998f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1999f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2000f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2001f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2002f0e8ed85SMark Brown 		case '_':
2003f0e8ed85SMark Brown 		case '-':
2004f0e8ed85SMark Brown 		case '\0':
2005f0e8ed85SMark Brown 			break;
2006f0e8ed85SMark Brown 		default:
2007f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2008f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2009f0e8ed85SMark Brown 			break;
2010f0e8ed85SMark Brown 		}
2011f0e8ed85SMark Brown 	}
2012fe3e78e0SMark Brown 
201328e9ad92SMark Brown 	if (card->late_probe) {
201428e9ad92SMark Brown 		ret = card->late_probe(card);
201528e9ad92SMark Brown 		if (ret < 0) {
2016f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
201728e9ad92SMark Brown 				card->name, ret);
201828e9ad92SMark Brown 			goto probe_aux_dev_err;
201928e9ad92SMark Brown 		}
202028e9ad92SMark Brown 	}
202128e9ad92SMark Brown 
2022824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20238c193b8dSLars-Peter Clausen 
2024f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2025fe3e78e0SMark Brown 	if (ret < 0) {
2026f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2027f110bfc7SLiam Girdwood 				ret);
20286b3ed785SAxel Lin 		goto probe_aux_dev_err;
2029fe3e78e0SMark Brown 	}
2030fe3e78e0SMark Brown 
2031435c5e25SMark Brown 	card->instantiated = 1;
20324f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2033f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
203434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2035b19e6e7bSMark Brown 
2036b19e6e7bSMark Brown 	return 0;
2037db2a4165SFrank Mandarino 
20382eea392dSJarkko Nikula probe_aux_dev_err:
2039f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
20402eea392dSJarkko Nikula 
2041f0fba2adSLiam Girdwood probe_dai_err:
20420671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2043fe3e78e0SMark Brown 
2044f0fba2adSLiam Girdwood card_probe_error:
204587506549SMark Brown 	if (card->remove)
2046e7361ec4SMark Brown 		card->remove(card);
2047f0fba2adSLiam Girdwood 
20482210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
20490757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2050f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2051f0fba2adSLiam Girdwood 
2052b19e6e7bSMark Brown base_error:
20531a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2054f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
205534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2056db2a4165SFrank Mandarino 
2057b19e6e7bSMark Brown 	return ret;
2058435c5e25SMark Brown }
2059435c5e25SMark Brown 
2060435c5e25SMark Brown /* probes a new socdev */
2061435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2062435c5e25SMark Brown {
2063f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2064435c5e25SMark Brown 
206570a7ca34SVinod Koul 	/*
206670a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
206770a7ca34SVinod Koul 	 * we should not be here in that case so ret error
206870a7ca34SVinod Koul 	 */
206970a7ca34SVinod Koul 	if (!card)
207070a7ca34SVinod Koul 		return -EINVAL;
207170a7ca34SVinod Koul 
2072fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2073f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2074fe4085e8SMark Brown 		 card->name);
2075fe4085e8SMark Brown 
2076435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2077435c5e25SMark Brown 	card->dev = &pdev->dev;
2078f0fba2adSLiam Girdwood 
207928d528c8SMark Brown 	return snd_soc_register_card(card);
2080435c5e25SMark Brown }
2081435c5e25SMark Brown 
2082b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2083db2a4165SFrank Mandarino {
20841a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2085db2a4165SFrank Mandarino 
2086f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
20871a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
208843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2089db2a4165SFrank Mandarino 
2090f0fba2adSLiam Girdwood 	/* remove and free each DAI */
20910671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
20921a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2093f0fba2adSLiam Girdwood 
2094f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2095f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2096f2ed6b07SMengdong Lin 
2097d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2098a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2099a6052154SJarkko Nikula 
2100f0fba2adSLiam Girdwood 	/* remove the card */
210187506549SMark Brown 	if (card->remove)
2102e7361ec4SMark Brown 		card->remove(card);
2103f0fba2adSLiam Girdwood 
2104f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2105b0e26485SVinod Koul 	return 0;
2106b0e26485SVinod Koul 
2107b2dfa62cSGuennadi Liakhovetski }
2108b0e26485SVinod Koul 
2109b0e26485SVinod Koul /* removes a socdev */
2110b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2111b0e26485SVinod Koul {
2112b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2113b0e26485SVinod Koul 
2114c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2115db2a4165SFrank Mandarino 	return 0;
2116db2a4165SFrank Mandarino }
2117db2a4165SFrank Mandarino 
21186f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
211951737470SMark Brown {
21206f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21211a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
212251737470SMark Brown 
212351737470SMark Brown 	if (!card->instantiated)
2124416356fcSMark Brown 		return 0;
212551737470SMark Brown 
212651737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
212751737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
21281a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
212943829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
213051737470SMark Brown 
2131f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2132416356fcSMark Brown 
2133988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
21341a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
213588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21361a497983SMengdong Lin 		int i;
213788bd870fSBenoit Cousson 
2138988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21391a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
21401a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
214188bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
214288bd870fSBenoit Cousson 		}
2143988e8cc4SNicolin Chen 	}
2144988e8cc4SNicolin Chen 
2145416356fcSMark Brown 	return 0;
214651737470SMark Brown }
21476f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
214851737470SMark Brown 
21496f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2150b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2151b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2152b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2153b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21546f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2155b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2156416356fcSMark Brown };
2157deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2158416356fcSMark Brown 
2159db2a4165SFrank Mandarino /* ASoC platform driver */
2160db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2161db2a4165SFrank Mandarino 	.driver		= {
2162db2a4165SFrank Mandarino 		.name		= "soc-audio",
21636f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2164db2a4165SFrank Mandarino 	},
2165db2a4165SFrank Mandarino 	.probe		= soc_probe,
2166db2a4165SFrank Mandarino 	.remove		= soc_remove,
2167db2a4165SFrank Mandarino };
2168db2a4165SFrank Mandarino 
2169096e49d5SMark Brown /**
2170db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2171db2a4165SFrank Mandarino  * @_template: control template
2172db2a4165SFrank Mandarino  * @data: control private data
2173ac11a2b3SMark Brown  * @long_name: control long name
2174efb7ac3fSMark Brown  * @prefix: control name prefix
2175db2a4165SFrank Mandarino  *
2176db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2177db2a4165SFrank Mandarino  *
2178db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2179db2a4165SFrank Mandarino  */
2180db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21813056557fSMark Brown 				  void *data, const char *long_name,
2182efb7ac3fSMark Brown 				  const char *prefix)
2183db2a4165SFrank Mandarino {
2184db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2185efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2186efb7ac3fSMark Brown 	char *name = NULL;
2187db2a4165SFrank Mandarino 
2188db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2189db2a4165SFrank Mandarino 	template.index = 0;
2190db2a4165SFrank Mandarino 
2191efb7ac3fSMark Brown 	if (!long_name)
2192efb7ac3fSMark Brown 		long_name = template.name;
2193efb7ac3fSMark Brown 
2194efb7ac3fSMark Brown 	if (prefix) {
21952b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2196efb7ac3fSMark Brown 		if (!name)
2197efb7ac3fSMark Brown 			return NULL;
2198efb7ac3fSMark Brown 
2199efb7ac3fSMark Brown 		template.name = name;
2200efb7ac3fSMark Brown 	} else {
2201efb7ac3fSMark Brown 		template.name = long_name;
2202efb7ac3fSMark Brown 	}
2203efb7ac3fSMark Brown 
2204efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2205efb7ac3fSMark Brown 
2206efb7ac3fSMark Brown 	kfree(name);
2207efb7ac3fSMark Brown 
2208efb7ac3fSMark Brown 	return kcontrol;
2209db2a4165SFrank Mandarino }
2210db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2211db2a4165SFrank Mandarino 
2212022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2213022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2214022658beSLiam Girdwood 	const char *prefix, void *data)
2215022658beSLiam Girdwood {
2216022658beSLiam Girdwood 	int err, i;
2217022658beSLiam Girdwood 
2218022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2219022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2220022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2221022658beSLiam Girdwood 						     control->name, prefix));
2222022658beSLiam Girdwood 		if (err < 0) {
2223f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2224f110bfc7SLiam Girdwood 				control->name, err);
2225022658beSLiam Girdwood 			return err;
2226022658beSLiam Girdwood 		}
2227022658beSLiam Girdwood 	}
2228022658beSLiam Girdwood 
2229022658beSLiam Girdwood 	return 0;
2230022658beSLiam Girdwood }
2231022658beSLiam Girdwood 
22324fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22334fefd698SDimitris Papastamos 					       const char *name)
22344fefd698SDimitris Papastamos {
22354fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22364fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22374fefd698SDimitris Papastamos 
22384fefd698SDimitris Papastamos 	if (unlikely(!name))
22394fefd698SDimitris Papastamos 		return NULL;
22404fefd698SDimitris Papastamos 
22414fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22424fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22434fefd698SDimitris Papastamos 			return kctl;
22444fefd698SDimitris Papastamos 	return NULL;
22454fefd698SDimitris Papastamos }
22464fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22474fefd698SDimitris Papastamos 
2248db2a4165SFrank Mandarino /**
22490f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22500f2780adSLars-Peter Clausen  *
22510f2780adSLars-Peter Clausen  * @component: Component to add controls to
22520f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22530f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22540f2780adSLars-Peter Clausen  *
22550f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22560f2780adSLars-Peter Clausen  */
22570f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22580f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22590f2780adSLars-Peter Clausen {
22600f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22610f2780adSLars-Peter Clausen 
22620f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22630f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22640f2780adSLars-Peter Clausen }
22650f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22660f2780adSLars-Peter Clausen 
22670f2780adSLars-Peter Clausen /**
2268022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2269022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
22703e8e1952SIan Molton  * duplicating this code.
22713e8e1952SIan Molton  *
22723e8e1952SIan Molton  * @codec: codec to add controls to
22733e8e1952SIan Molton  * @controls: array of controls to add
22743e8e1952SIan Molton  * @num_controls: number of elements in the array
22753e8e1952SIan Molton  *
22763e8e1952SIan Molton  * Return 0 for success, else error.
22773e8e1952SIan Molton  */
2278022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
22790f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22803e8e1952SIan Molton {
22810f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
22820f2780adSLars-Peter Clausen 		num_controls);
22833e8e1952SIan Molton }
2284022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
22853e8e1952SIan Molton 
22863e8e1952SIan Molton /**
2287a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2288022658beSLiam Girdwood  * Convenience function to add a list of controls.
2289a491a5c8SLiam Girdwood  *
2290a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2291a491a5c8SLiam Girdwood  * @controls: array of controls to add
2292a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2293a491a5c8SLiam Girdwood  *
2294a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2295a491a5c8SLiam Girdwood  */
2296a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
22970f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2298a491a5c8SLiam Girdwood {
22990f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
23000f2780adSLars-Peter Clausen 		num_controls);
2301a491a5c8SLiam Girdwood }
2302a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2303a491a5c8SLiam Girdwood 
2304a491a5c8SLiam Girdwood /**
2305022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2306022658beSLiam Girdwood  * Convenience function to add a list of controls.
2307022658beSLiam Girdwood  *
2308022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2309022658beSLiam Girdwood  * @controls: array of controls to add
2310022658beSLiam Girdwood  * @num_controls: number of elements in the array
2311022658beSLiam Girdwood  *
2312022658beSLiam Girdwood  * Return 0 for success, else error.
2313022658beSLiam Girdwood  */
2314022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2315022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2316022658beSLiam Girdwood {
2317022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2318022658beSLiam Girdwood 
2319022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2320022658beSLiam Girdwood 			NULL, soc_card);
2321022658beSLiam Girdwood }
2322022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2323022658beSLiam Girdwood 
2324022658beSLiam Girdwood /**
2325022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2326022658beSLiam Girdwood  * Convienience function to add a list of controls.
2327022658beSLiam Girdwood  *
2328022658beSLiam Girdwood  * @dai: DAI to add controls to
2329022658beSLiam Girdwood  * @controls: array of controls to add
2330022658beSLiam Girdwood  * @num_controls: number of elements in the array
2331022658beSLiam Girdwood  *
2332022658beSLiam Girdwood  * Return 0 for success, else error.
2333022658beSLiam Girdwood  */
2334022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2335022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2336022658beSLiam Girdwood {
2337313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2338022658beSLiam Girdwood 
2339022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2340022658beSLiam Girdwood 			NULL, dai);
2341022658beSLiam Girdwood }
2342022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2343022658beSLiam Girdwood 
2344022658beSLiam Girdwood /**
23458c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23468c6529dbSLiam Girdwood  * @dai: DAI
23478c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23488c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23498c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23508c6529dbSLiam Girdwood  *
23518c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
23528c6529dbSLiam Girdwood  */
23538c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
23548c6529dbSLiam Girdwood 	unsigned int freq, int dir)
23558c6529dbSLiam Girdwood {
2356f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2357f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2358ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2359da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2360ec4ee52aSMark Brown 						      freq, dir);
23618c6529dbSLiam Girdwood 	else
23621104a9c8SMark Brown 		return -ENOTSUPP;
23638c6529dbSLiam Girdwood }
23648c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
23658c6529dbSLiam Girdwood 
23668c6529dbSLiam Girdwood /**
2367ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2368ec4ee52aSMark Brown  * @codec: CODEC
2369ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2370da1c6ea6SMark Brown  * @source: Source for the clock
2371ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2372ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2373ec4ee52aSMark Brown  *
2374ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2375ec4ee52aSMark Brown  */
2376ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2377da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2378ec4ee52aSMark Brown {
2379ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2380da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2381da1c6ea6SMark Brown 						 freq, dir);
2382ec4ee52aSMark Brown 	else
23831104a9c8SMark Brown 		return -ENOTSUPP;
2384ec4ee52aSMark Brown }
2385ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2386ec4ee52aSMark Brown 
2387ec4ee52aSMark Brown /**
23888c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
23898c6529dbSLiam Girdwood  * @dai: DAI
2390ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
23918c6529dbSLiam Girdwood  * @div: new clock divisor.
23928c6529dbSLiam Girdwood  *
23938c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
23948c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
23958c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
23968c6529dbSLiam Girdwood  */
23978c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
23988c6529dbSLiam Girdwood 	int div_id, int div)
23998c6529dbSLiam Girdwood {
2400f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2401f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
24028c6529dbSLiam Girdwood 	else
24038c6529dbSLiam Girdwood 		return -EINVAL;
24048c6529dbSLiam Girdwood }
24058c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
24068c6529dbSLiam Girdwood 
24078c6529dbSLiam Girdwood /**
24088c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
24098c6529dbSLiam Girdwood  * @dai: DAI
24108c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
241185488037SMark Brown  * @source: DAI specific source for the PLL
24128c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24138c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24148c6529dbSLiam Girdwood  *
24158c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
24168c6529dbSLiam Girdwood  */
241785488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
241885488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
24198c6529dbSLiam Girdwood {
2420f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2421f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
242285488037SMark Brown 					 freq_in, freq_out);
2423ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2424ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2425ec4ee52aSMark Brown 						   freq_in, freq_out);
24268c6529dbSLiam Girdwood 	else
24278c6529dbSLiam Girdwood 		return -EINVAL;
24288c6529dbSLiam Girdwood }
24298c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24308c6529dbSLiam Girdwood 
2431ec4ee52aSMark Brown /*
2432ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2433ec4ee52aSMark Brown  * @codec: CODEC
2434ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2435ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2436ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2437ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2438ec4ee52aSMark Brown  *
2439ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2440ec4ee52aSMark Brown  */
2441ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2442ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2443ec4ee52aSMark Brown {
2444ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2445ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2446ec4ee52aSMark Brown 					      freq_in, freq_out);
2447ec4ee52aSMark Brown 	else
2448ec4ee52aSMark Brown 		return -EINVAL;
2449ec4ee52aSMark Brown }
2450ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2451ec4ee52aSMark Brown 
24528c6529dbSLiam Girdwood /**
2453e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2454e54cf76bSLiam Girdwood  * @dai: DAI
2455231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2456e54cf76bSLiam Girdwood  *
2457e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2458e54cf76bSLiam Girdwood  */
2459e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2460e54cf76bSLiam Girdwood {
2461e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2462e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2463e54cf76bSLiam Girdwood 	else
2464e54cf76bSLiam Girdwood 		return -EINVAL;
2465e54cf76bSLiam Girdwood }
2466e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2467e54cf76bSLiam Girdwood 
2468e54cf76bSLiam Girdwood /**
24698c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
24708c6529dbSLiam Girdwood  * @dai: DAI
24718c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
24728c6529dbSLiam Girdwood  *
24738c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
24748c6529dbSLiam Girdwood  */
24758c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
24768c6529dbSLiam Girdwood {
24775e4ba569SShawn Guo 	if (dai->driver == NULL)
24788c6529dbSLiam Girdwood 		return -EINVAL;
24795e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
24805e4ba569SShawn Guo 		return -ENOTSUPP;
24815e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
24828c6529dbSLiam Girdwood }
24838c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
24848c6529dbSLiam Girdwood 
24858c6529dbSLiam Girdwood /**
2486e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
248789c67857SXiubo Li  * @slots: Number of slots in use.
248889c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
248989c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
249089c67857SXiubo Li  *
249189c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
249289c67857SXiubo Li  */
2493e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
249489c67857SXiubo Li 					  unsigned int *tx_mask,
249589c67857SXiubo Li 					  unsigned int *rx_mask)
249689c67857SXiubo Li {
249789c67857SXiubo Li 	if (*tx_mask || *rx_mask)
249889c67857SXiubo Li 		return 0;
249989c67857SXiubo Li 
250089c67857SXiubo Li 	if (!slots)
250189c67857SXiubo Li 		return -EINVAL;
250289c67857SXiubo Li 
250389c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
250489c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
250589c67857SXiubo Li 
250689c67857SXiubo Li 	return 0;
250789c67857SXiubo Li }
250889c67857SXiubo Li 
250989c67857SXiubo Li /**
2510e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2511e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2512a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2513a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25148c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2515a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25168c6529dbSLiam Girdwood  *
2517e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2518e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2519e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2520e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2521e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2522e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2523e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2524e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2525e46c9366SLars-Peter Clausen  * channel and so on.
2526e46c9366SLars-Peter Clausen  *
2527e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2528e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2529e46c9366SLars-Peter Clausen  *
2530e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
25318c6529dbSLiam Girdwood  */
25328c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2533a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25348c6529dbSLiam Girdwood {
2535e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2536e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
253789c67857SXiubo Li 						&tx_mask, &rx_mask);
253889c67857SXiubo Li 	else
2539e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
254089c67857SXiubo Li 
254188bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
254288bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
254388bd870fSBenoit Cousson 
2544f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2545f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2546a5479e38SDaniel Ribeiro 				slots, slot_width);
25478c6529dbSLiam Girdwood 	else
2548b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25498c6529dbSLiam Girdwood }
25508c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25518c6529dbSLiam Girdwood 
25528c6529dbSLiam Girdwood /**
2553472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2554472df3cbSBarry Song  * @dai: DAI
2555472df3cbSBarry Song  * @tx_num: how many TX channels
2556472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2557472df3cbSBarry Song  *           0~num-1 uses
2558472df3cbSBarry Song  * @rx_num: how many RX channels
2559472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2560472df3cbSBarry Song  *           0~num-1 uses
2561472df3cbSBarry Song  *
2562472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2563472df3cbSBarry Song  */
2564472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2565472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2566472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2567472df3cbSBarry Song {
2568f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2569f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2570472df3cbSBarry Song 			rx_num, rx_slot);
2571472df3cbSBarry Song 	else
2572472df3cbSBarry Song 		return -EINVAL;
2573472df3cbSBarry Song }
2574472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2575472df3cbSBarry Song 
2576472df3cbSBarry Song /**
25778c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
25788c6529dbSLiam Girdwood  * @dai: DAI
25798c6529dbSLiam Girdwood  * @tristate: tristate enable
25808c6529dbSLiam Girdwood  *
25818c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
25828c6529dbSLiam Girdwood  */
25838c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
25848c6529dbSLiam Girdwood {
2585f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2586f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
25878c6529dbSLiam Girdwood 	else
25888c6529dbSLiam Girdwood 		return -EINVAL;
25898c6529dbSLiam Girdwood }
25908c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
25918c6529dbSLiam Girdwood 
25928c6529dbSLiam Girdwood /**
25938c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
25948c6529dbSLiam Girdwood  * @dai: DAI
25958c6529dbSLiam Girdwood  * @mute: mute enable
2596da18396fSMark Brown  * @direction: stream to mute
25978c6529dbSLiam Girdwood  *
25988c6529dbSLiam Girdwood  * Mutes the DAI DAC.
25998c6529dbSLiam Girdwood  */
2600da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2601da18396fSMark Brown 			     int direction)
26028c6529dbSLiam Girdwood {
2603da18396fSMark Brown 	if (!dai->driver)
2604da18396fSMark Brown 		return -ENOTSUPP;
2605da18396fSMark Brown 
2606da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2607da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2608da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2609da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2610f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
26118c6529dbSLiam Girdwood 	else
261204570c62SMark Brown 		return -ENOTSUPP;
26138c6529dbSLiam Girdwood }
26148c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26158c6529dbSLiam Girdwood 
2616c5af3a2eSMark Brown /**
2617c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2618c5af3a2eSMark Brown  *
2619ac11a2b3SMark Brown  * @card: Card to register
2620c5af3a2eSMark Brown  *
2621c5af3a2eSMark Brown  */
262270a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2623c5af3a2eSMark Brown {
2624923c5e61SMengdong Lin 	int i, ret;
26251a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2626f0fba2adSLiam Girdwood 
2627c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2628c5af3a2eSMark Brown 		return -EINVAL;
2629c5af3a2eSMark Brown 
26305a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
26315a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
26325a504963SStephen Warren 
2633923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
263488bd870fSBenoit Cousson 		if (ret) {
2635923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2636923c5e61SMengdong Lin 				link->name);
263788bd870fSBenoit Cousson 			return ret;
263888bd870fSBenoit Cousson 		}
26395a504963SStephen Warren 	}
26405a504963SStephen Warren 
2641ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2642ed77cc12SMark Brown 
2643111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2644111c6419SStephen Warren 
2645f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2646f8f80361SMengdong Lin 	card->num_dai_links = 0;
2647f0fba2adSLiam Girdwood 
26481a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
26499115171aSMark Brown 	card->num_rtd = 0;
2650db2a4165SFrank Mandarino 
2651db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
26528a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2653db2a4165SFrank Mandarino 	card->instantiated = 0;
2654db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2655a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2656db2a4165SFrank Mandarino 
2657b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2658b19e6e7bSMark Brown 	if (ret != 0)
26594e2576bdSKuninori Morimoto 		return ret;
2660db2a4165SFrank Mandarino 
2661988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
26621a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
266388bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
266488bd870fSBenoit Cousson 		int j;
266588bd870fSBenoit Cousson 
266688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
266788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2668988e8cc4SNicolin Chen 			if (!codec_dai->active)
2669988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
267088bd870fSBenoit Cousson 		}
267188bd870fSBenoit Cousson 
2672988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2673988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2674988e8cc4SNicolin Chen 	}
2675988e8cc4SNicolin Chen 
2676b19e6e7bSMark Brown 	return ret;
2677db2a4165SFrank Mandarino }
267870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2679db2a4165SFrank Mandarino 
2680db2a4165SFrank Mandarino /**
2681db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2682db2a4165SFrank Mandarino  *
2683db2a4165SFrank Mandarino  * @card: Card to unregister
2684db2a4165SFrank Mandarino  *
2685db2a4165SFrank Mandarino  */
268670a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2687db2a4165SFrank Mandarino {
268801e0df66SLars-Peter Clausen 	if (card->instantiated) {
268901e0df66SLars-Peter Clausen 		card->instantiated = false;
26901c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2691b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2692f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
26938f6f9b29SKuninori Morimoto 	}
2694db2a4165SFrank Mandarino 
2695db2a4165SFrank Mandarino 	return 0;
2696db2a4165SFrank Mandarino }
269770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2698db2a4165SFrank Mandarino 
2699db2a4165SFrank Mandarino /*
2700db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2701db2a4165SFrank Mandarino  * and sanitizing names.
2702db2a4165SFrank Mandarino  */
27030b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2704db2a4165SFrank Mandarino {
2705db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2706db2a4165SFrank Mandarino 	int id1, id2;
2707db2a4165SFrank Mandarino 
2708db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2709db2a4165SFrank Mandarino 		return NULL;
2710db2a4165SFrank Mandarino 
271158818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2712db2a4165SFrank Mandarino 
2713db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2714c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2715c5af3a2eSMark Brown 	if (found) {
2716c5af3a2eSMark Brown 		/* get ID */
2717c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2718c5af3a2eSMark Brown 
2719c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2720c5af3a2eSMark Brown 			if (*id == -1)
2721c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2722c5af3a2eSMark Brown 		}
2723c5af3a2eSMark Brown 
2724c5af3a2eSMark Brown 	} else {
2725c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2726c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2727c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2728c5af3a2eSMark Brown 
2729c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2730c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2731c5af3a2eSMark Brown 
2732c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2733c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
273458818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2735c5af3a2eSMark Brown 		} else
2736c5af3a2eSMark Brown 			*id = 0;
2737c5af3a2eSMark Brown 	}
2738c5af3a2eSMark Brown 
2739c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2740c5af3a2eSMark Brown }
2741c5af3a2eSMark Brown 
2742c5af3a2eSMark Brown /*
2743c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2744c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2745c5af3a2eSMark Brown  */
2746c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2747c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2748c5af3a2eSMark Brown {
2749c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
275010e8aa9aSMichał Mirosław 		dev_err(dev,
275110e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
275210e8aa9aSMichał Mirosław 			dev_name(dev));
2753c5af3a2eSMark Brown 		return NULL;
2754c5af3a2eSMark Brown 	}
2755c5af3a2eSMark Brown 
2756c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2757c5af3a2eSMark Brown }
2758c5af3a2eSMark Brown 
2759c5af3a2eSMark Brown /**
276032c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
27619115171aSMark Brown  *
276232c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
27639115171aSMark Brown  */
276432c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
27659115171aSMark Brown {
27665c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
27679115171aSMark Brown 
27685c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
276932c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
277032c9ba54SLars-Peter Clausen 			dai->name);
27719115171aSMark Brown 		list_del(&dai->list);
2772f0fba2adSLiam Girdwood 		kfree(dai->name);
2773f0fba2adSLiam Girdwood 		kfree(dai);
27749115171aSMark Brown 	}
277532c9ba54SLars-Peter Clausen }
27769115171aSMark Brown 
27775e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
27785e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
27795e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
27805e4fb372SMengdong Lin 	bool legacy_dai_naming)
27815e4fb372SMengdong Lin {
27825e4fb372SMengdong Lin 	struct device *dev = component->dev;
27835e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
27845e4fb372SMengdong Lin 
27855e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
27865e4fb372SMengdong Lin 
27875e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
27885e4fb372SMengdong Lin 	if (dai == NULL)
27895e4fb372SMengdong Lin 		return NULL;
27905e4fb372SMengdong Lin 
27915e4fb372SMengdong Lin 	/*
27925e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
27935e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
27945e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
27955e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
27965e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
27975e4fb372SMengdong Lin 	 * component-less anymore.
27985e4fb372SMengdong Lin 	 */
27995e4fb372SMengdong Lin 	if (legacy_dai_naming &&
28005e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
28015e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
28025e4fb372SMengdong Lin 	} else {
28035e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
28045e4fb372SMengdong Lin 		if (dai_drv->id)
28055e4fb372SMengdong Lin 			dai->id = dai_drv->id;
28065e4fb372SMengdong Lin 		else
28075e4fb372SMengdong Lin 			dai->id = component->num_dai;
28085e4fb372SMengdong Lin 	}
28095e4fb372SMengdong Lin 	if (dai->name == NULL) {
28105e4fb372SMengdong Lin 		kfree(dai);
28115e4fb372SMengdong Lin 		return NULL;
28125e4fb372SMengdong Lin 	}
28135e4fb372SMengdong Lin 
28145e4fb372SMengdong Lin 	dai->component = component;
28155e4fb372SMengdong Lin 	dai->dev = dev;
28165e4fb372SMengdong Lin 	dai->driver = dai_drv;
28175e4fb372SMengdong Lin 	if (!dai->driver->ops)
28185e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
28195e4fb372SMengdong Lin 
28205e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
28215e4fb372SMengdong Lin 	component->num_dai++;
28225e4fb372SMengdong Lin 
28235e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
28245e4fb372SMengdong Lin 	return dai;
28255e4fb372SMengdong Lin }
28265e4fb372SMengdong Lin 
28279115171aSMark Brown /**
282832c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
28299115171aSMark Brown  *
28306106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
28316106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2832ac11a2b3SMark Brown  * @count: Number of DAIs
283332c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
283432c9ba54SLars-Peter Clausen  *                     parent's name.
28359115171aSMark Brown  */
283632c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2837bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2838bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
28399115171aSMark Brown {
28406106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2841f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
284232c9ba54SLars-Peter Clausen 	unsigned int i;
284332c9ba54SLars-Peter Clausen 	int ret;
2844f0fba2adSLiam Girdwood 
2845f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
28469115171aSMark Brown 
2847bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2848bb13109dSLars-Peter Clausen 
28499115171aSMark Brown 	for (i = 0; i < count; i++) {
2850f0fba2adSLiam Girdwood 
28515e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
28525e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
2853c46e0079SAxel Lin 		if (dai == NULL) {
2854c46e0079SAxel Lin 			ret = -ENOMEM;
2855c46e0079SAxel Lin 			goto err;
2856c46e0079SAxel Lin 		}
2857f0fba2adSLiam Girdwood 	}
2858f0fba2adSLiam Girdwood 
28599115171aSMark Brown 	return 0;
28609115171aSMark Brown 
28619115171aSMark Brown err:
286232c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
28639115171aSMark Brown 
28649115171aSMark Brown 	return ret;
28659115171aSMark Brown }
28669115171aSMark Brown 
286768003e6cSMengdong Lin /**
286868003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
286968003e6cSMengdong Lin  *
287068003e6cSMengdong Lin  * @component: The component the DAIs are registered for
287168003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
287268003e6cSMengdong Lin  *
287368003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
287468003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
287568003e6cSMengdong Lin  * will be freed in the component cleanup.
287668003e6cSMengdong Lin  */
287768003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
287868003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
287968003e6cSMengdong Lin {
288068003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
288168003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
288268003e6cSMengdong Lin 	struct snd_soc_dai *dai;
288368003e6cSMengdong Lin 	int ret;
288468003e6cSMengdong Lin 
288568003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
288668003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
288768003e6cSMengdong Lin 			dai_drv->dobj.type);
288868003e6cSMengdong Lin 		return -EINVAL;
288968003e6cSMengdong Lin 	}
289068003e6cSMengdong Lin 
289168003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
289268003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
289368003e6cSMengdong Lin 	if (!dai)
289468003e6cSMengdong Lin 		return -ENOMEM;
289568003e6cSMengdong Lin 
289668003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
289768003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
289868003e6cSMengdong Lin 	 */
289968003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
290068003e6cSMengdong Lin 	if (ret != 0) {
290168003e6cSMengdong Lin 		dev_err(component->dev,
290268003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
290368003e6cSMengdong Lin 	}
290468003e6cSMengdong Lin 
290568003e6cSMengdong Lin 	return ret;
290668003e6cSMengdong Lin }
290768003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
290868003e6cSMengdong Lin 
290914e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
291014e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2911d191bd8dSKuninori Morimoto {
291214e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2913d191bd8dSKuninori Morimoto 
291414e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
291514e8bdebSLars-Peter Clausen }
2916d191bd8dSKuninori Morimoto 
291714e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
291814e8bdebSLars-Peter Clausen 	int event)
291914e8bdebSLars-Peter Clausen {
292014e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
292114e8bdebSLars-Peter Clausen 
292214e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
292314e8bdebSLars-Peter Clausen }
292414e8bdebSLars-Peter Clausen 
2925bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2926bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2927d191bd8dSKuninori Morimoto {
2928ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2929ce0fc93aSLars-Peter Clausen 
2930bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2931bb13109dSLars-Peter Clausen 	if (!component->name) {
2932bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2933d191bd8dSKuninori Morimoto 		return -ENOMEM;
2934d191bd8dSKuninori Morimoto 	}
2935d191bd8dSKuninori Morimoto 
2936bb13109dSLars-Peter Clausen 	component->dev = dev;
2937bb13109dSLars-Peter Clausen 	component->driver = driver;
293861aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
293961aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
2940e2c330b9SLars-Peter Clausen 
29414890140fSLars-Peter Clausen 	dapm = &component->dapm;
2942ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2943ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2944ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
29455819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
294614e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
294714e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
294814e8bdebSLars-Peter Clausen 	if (driver->stream_event)
294914e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2950ce0fc93aSLars-Peter Clausen 
295161aca564SLars-Peter Clausen 	component->controls = driver->controls;
295261aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
295361aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
295461aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
295561aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
295661aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
295761aca564SLars-Peter Clausen 
2958bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2959bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2960bb13109dSLars-Peter Clausen 
2961bb13109dSLars-Peter Clausen 	return 0;
2962d191bd8dSKuninori Morimoto }
2963d191bd8dSKuninori Morimoto 
296420feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2965886f5692SLars-Peter Clausen {
2966886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
296720feb881SLars-Peter Clausen 
2968886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2969886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2970886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2971886f5692SLars-Peter Clausen }
297220feb881SLars-Peter Clausen 
2973e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2974e874bf5fSLars-Peter Clausen 
297520feb881SLars-Peter Clausen /**
297620feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
297720feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
297820feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
297920feb881SLars-Peter Clausen  *
298020feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
298120feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
298220feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
298320feb881SLars-Peter Clausen  * before the first IO attempt of the component.
298420feb881SLars-Peter Clausen  */
298520feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
298620feb881SLars-Peter Clausen 	struct regmap *regmap)
298720feb881SLars-Peter Clausen {
298820feb881SLars-Peter Clausen 	component->regmap = regmap;
298920feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2990886f5692SLars-Peter Clausen }
299120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
299220feb881SLars-Peter Clausen 
299320feb881SLars-Peter Clausen /**
299420feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
299520feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
299620feb881SLars-Peter Clausen  *
299720feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
299820feb881SLars-Peter Clausen  * removes the regmap instance from the component.
299920feb881SLars-Peter Clausen  *
300020feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
300120feb881SLars-Peter Clausen  * to initialize the regmap instance.
300220feb881SLars-Peter Clausen  */
300320feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
300420feb881SLars-Peter Clausen {
300520feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
300620feb881SLars-Peter Clausen 	component->regmap = NULL;
300720feb881SLars-Peter Clausen }
300820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3009886f5692SLars-Peter Clausen 
3010e874bf5fSLars-Peter Clausen #endif
3011d191bd8dSKuninori Morimoto 
3012bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3013bb13109dSLars-Peter Clausen {
301420feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
301520feb881SLars-Peter Clausen 		if (!component->regmap)
301620feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
301720feb881SLars-Peter Clausen 		if (component->regmap)
301820feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
301920feb881SLars-Peter Clausen 	}
3020886f5692SLars-Peter Clausen 
3021bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
30228a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3023d191bd8dSKuninori Morimoto }
3024d191bd8dSKuninori Morimoto 
3025bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3026bb13109dSLars-Peter Clausen {
3027d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3028bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3029d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3030bb13109dSLars-Peter Clausen }
3031d191bd8dSKuninori Morimoto 
3032bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3033bb13109dSLars-Peter Clausen {
3034bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3035bb13109dSLars-Peter Clausen 	kfree(component->name);
3036bb13109dSLars-Peter Clausen }
3037d191bd8dSKuninori Morimoto 
3038bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3039bb13109dSLars-Peter Clausen {
3040bb13109dSLars-Peter Clausen 	list_del(&component->list);
3041bb13109dSLars-Peter Clausen }
3042d191bd8dSKuninori Morimoto 
3043d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3044d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3045d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3046d191bd8dSKuninori Morimoto 			       int num_dai)
3047d191bd8dSKuninori Morimoto {
3048d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3049bb13109dSLars-Peter Clausen 	int ret;
3050d191bd8dSKuninori Morimoto 
3051bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3052d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3053d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3054d191bd8dSKuninori Morimoto 		return -ENOMEM;
3055d191bd8dSKuninori Morimoto 	}
3056d191bd8dSKuninori Morimoto 
3057bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3058bb13109dSLars-Peter Clausen 	if (ret)
3059bb13109dSLars-Peter Clausen 		goto err_free;
3060bb13109dSLars-Peter Clausen 
30613d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
306298e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
30633d59400fSLars-Peter Clausen 
3064bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3065bb13109dSLars-Peter Clausen 	if (ret < 0) {
3066f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3067bb13109dSLars-Peter Clausen 		goto err_cleanup;
3068bb13109dSLars-Peter Clausen 	}
3069bb13109dSLars-Peter Clausen 
3070bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3071bb13109dSLars-Peter Clausen 
3072bb13109dSLars-Peter Clausen 	return 0;
3073bb13109dSLars-Peter Clausen 
3074bb13109dSLars-Peter Clausen err_cleanup:
3075bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3076bb13109dSLars-Peter Clausen err_free:
3077bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3078bb13109dSLars-Peter Clausen 	return ret;
3079d191bd8dSKuninori Morimoto }
3080d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3081d191bd8dSKuninori Morimoto 
3082d191bd8dSKuninori Morimoto /**
3083d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3084d191bd8dSKuninori Morimoto  *
3085628536eaSJonathan Corbet  * @dev: The device to unregister
3086d191bd8dSKuninori Morimoto  */
3087d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3088d191bd8dSKuninori Morimoto {
3089d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3090d191bd8dSKuninori Morimoto 
309134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3092d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
309398e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3094d191bd8dSKuninori Morimoto 			goto found;
3095d191bd8dSKuninori Morimoto 	}
309634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3097d191bd8dSKuninori Morimoto 	return;
3098d191bd8dSKuninori Morimoto 
3099d191bd8dSKuninori Morimoto found:
31008a978234SLiam Girdwood 	snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
310134e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
310234e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3103bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3104bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3105d191bd8dSKuninori Morimoto }
3106d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3107d191bd8dSKuninori Morimoto 
3108f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3109e2c330b9SLars-Peter Clausen {
3110e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3111e2c330b9SLars-Peter Clausen 
3112f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3113e2c330b9SLars-Peter Clausen }
3114e2c330b9SLars-Peter Clausen 
3115f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3116e2c330b9SLars-Peter Clausen {
3117e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3118e2c330b9SLars-Peter Clausen 
3119f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3120e2c330b9SLars-Peter Clausen }
3121e2c330b9SLars-Peter Clausen 
3122d191bd8dSKuninori Morimoto /**
312371a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
312471a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
312571a45cdaSLars-Peter Clausen  * @platform: The platform to add
31267d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
312712a48a8cSMark Brown  */
312871a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3129d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
313012a48a8cSMark Brown {
3131b37f1d12SLars-Peter Clausen 	int ret;
3132b37f1d12SLars-Peter Clausen 
3133bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3134bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3135bb13109dSLars-Peter Clausen 	if (ret)
3136bb13109dSLars-Peter Clausen 		return ret;
3137f0fba2adSLiam Girdwood 
3138f0fba2adSLiam Girdwood 	platform->dev = dev;
3139f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3140f1d45cc3SLars-Peter Clausen 
3141f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3142f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3143f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3144f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
314512a48a8cSMark Brown 
314681c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
314781c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
314881c7cfd1SLars-Peter Clausen #endif
314912a48a8cSMark Brown 
315012a48a8cSMark Brown 	mutex_lock(&client_mutex);
3151bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
315212a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
315312a48a8cSMark Brown 	mutex_unlock(&client_mutex);
315412a48a8cSMark Brown 
3155f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3156f4333203SLars-Peter Clausen 		platform->component.name);
315712a48a8cSMark Brown 
315812a48a8cSMark Brown 	return 0;
315912a48a8cSMark Brown }
316071a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
316171a45cdaSLars-Peter Clausen 
316271a45cdaSLars-Peter Clausen /**
316371a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
316471a45cdaSLars-Peter Clausen  *
3165628536eaSJonathan Corbet  * @dev: The device for the platform
3166628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
316771a45cdaSLars-Peter Clausen  */
316871a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
316971a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
317071a45cdaSLars-Peter Clausen {
317171a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
317271a45cdaSLars-Peter Clausen 	int ret;
317371a45cdaSLars-Peter Clausen 
317471a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
317571a45cdaSLars-Peter Clausen 
317671a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
317771a45cdaSLars-Peter Clausen 	if (platform == NULL)
317871a45cdaSLars-Peter Clausen 		return -ENOMEM;
317971a45cdaSLars-Peter Clausen 
318071a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
318171a45cdaSLars-Peter Clausen 	if (ret)
318271a45cdaSLars-Peter Clausen 		kfree(platform);
318371a45cdaSLars-Peter Clausen 
318471a45cdaSLars-Peter Clausen 	return ret;
318571a45cdaSLars-Peter Clausen }
318612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
318712a48a8cSMark Brown 
318812a48a8cSMark Brown /**
318971a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
319071a45cdaSLars-Peter Clausen  * @platform: the platform to remove
319171a45cdaSLars-Peter Clausen  */
319271a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
319371a45cdaSLars-Peter Clausen {
3194b37f1d12SLars-Peter Clausen 
319571a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
319671a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3197bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
319871a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
319971a45cdaSLars-Peter Clausen 
320071a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3201f4333203SLars-Peter Clausen 		platform->component.name);
3202decc27b0SDaniel Mack 
3203decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
320471a45cdaSLars-Peter Clausen }
320571a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
320671a45cdaSLars-Peter Clausen 
320771a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
320871a45cdaSLars-Peter Clausen {
320971a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
321071a45cdaSLars-Peter Clausen 
321134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
321271a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
321334e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
321434e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
321571a45cdaSLars-Peter Clausen 			return platform;
321671a45cdaSLars-Peter Clausen 		}
321734e81ab4SLars-Peter Clausen 	}
321834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
321971a45cdaSLars-Peter Clausen 
322071a45cdaSLars-Peter Clausen 	return NULL;
322171a45cdaSLars-Peter Clausen }
322271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
322371a45cdaSLars-Peter Clausen 
322471a45cdaSLars-Peter Clausen /**
322512a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
322612a48a8cSMark Brown  *
3227628536eaSJonathan Corbet  * @dev: platform to unregister
322812a48a8cSMark Brown  */
3229f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
323012a48a8cSMark Brown {
3231f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3232f0fba2adSLiam Girdwood 
323371a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
323471a45cdaSLars-Peter Clausen 	if (!platform)
3235f0fba2adSLiam Girdwood 		return;
3236f0fba2adSLiam Girdwood 
323771a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3238f0fba2adSLiam Girdwood 	kfree(platform);
323912a48a8cSMark Brown }
324012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
324112a48a8cSMark Brown 
3242151ab22cSMark Brown static u64 codec_format_map[] = {
3243151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3244151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3245151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3246151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3247151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3248151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3249151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3250151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3251151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3252151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3253151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3254151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3255151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3256151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3257151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3258151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3259151ab22cSMark Brown };
3260151ab22cSMark Brown 
3261151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3262151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3263151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3264151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3265151ab22cSMark Brown  */
3266151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3267151ab22cSMark Brown {
3268151ab22cSMark Brown 	int i;
3269151ab22cSMark Brown 
3270151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3271151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3272151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3273151ab22cSMark Brown }
3274151ab22cSMark Brown 
3275f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3276f1d45cc3SLars-Peter Clausen {
3277f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3278f1d45cc3SLars-Peter Clausen 
3279f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3280f1d45cc3SLars-Peter Clausen }
3281f1d45cc3SLars-Peter Clausen 
3282f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3283f1d45cc3SLars-Peter Clausen {
3284f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3285f1d45cc3SLars-Peter Clausen 
3286f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3287f1d45cc3SLars-Peter Clausen }
3288f1d45cc3SLars-Peter Clausen 
3289e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3290e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3291e2c330b9SLars-Peter Clausen {
3292e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3293e2c330b9SLars-Peter Clausen 
3294e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3295e2c330b9SLars-Peter Clausen }
3296e2c330b9SLars-Peter Clausen 
3297e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3298e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3299e2c330b9SLars-Peter Clausen {
3300e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3301e2c330b9SLars-Peter Clausen 
3302e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3303e2c330b9SLars-Peter Clausen 
3304e2c330b9SLars-Peter Clausen 	return 0;
3305e2c330b9SLars-Peter Clausen }
3306e2c330b9SLars-Peter Clausen 
330768f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
330868f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
330968f831c2SLars-Peter Clausen {
331068f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
331168f831c2SLars-Peter Clausen 
331268f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
331368f831c2SLars-Peter Clausen }
331468f831c2SLars-Peter Clausen 
33150d0cf00aSMark Brown /**
33160d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
33170d0cf00aSMark Brown  *
3318628536eaSJonathan Corbet  * @dev: The parent device for this codec
3319628536eaSJonathan Corbet  * @codec_drv: Codec driver
3320628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3321628536eaSJonathan Corbet  * @num_dai: Number of DAIs
33220d0cf00aSMark Brown  */
3323f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3324001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3325001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3326001ae4c0SMark Brown 			   int num_dai)
33270d0cf00aSMark Brown {
33284890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3329f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3330bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3331f0fba2adSLiam Girdwood 	int ret, i;
3332151ab22cSMark Brown 
3333f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
33340d0cf00aSMark Brown 
3335f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3336f0fba2adSLiam Girdwood 	if (codec == NULL)
3337f0fba2adSLiam Girdwood 		return -ENOMEM;
33380d0cf00aSMark Brown 
3339f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3340ce0fc93aSLars-Peter Clausen 
3341bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3342bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3343bb13109dSLars-Peter Clausen 	if (ret)
3344bb13109dSLars-Peter Clausen 		goto err_free;
3345151ab22cSMark Brown 
3346f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3347f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3348f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3349f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
3350e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3351e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3352e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3353e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
33543d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
33554890140fSLars-Peter Clausen 
33564890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
33574890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
33584890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
335914e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
33604890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
336168f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
33624890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3363f0fba2adSLiam Girdwood 	codec->dev = dev;
3364f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3365e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3366f0fba2adSLiam Girdwood 
336781c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
336881c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
336981c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
337081c7cfd1SLars-Peter Clausen #endif
3371a39f75f7SXiubo Li 
3372a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3373886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3374a39f75f7SXiubo Li 
3375f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3376f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3377f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3378f0fba2adSLiam Girdwood 	}
3379f0fba2adSLiam Girdwood 
3380bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3381bb13109dSLars-Peter Clausen 	if (ret < 0) {
3382f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3383bb13109dSLars-Peter Clausen 		goto err_cleanup;
3384bb13109dSLars-Peter Clausen 	}
3385bb13109dSLars-Peter Clausen 
3386bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3387bb13109dSLars-Peter Clausen 		dai->codec = codec;
3388bb13109dSLars-Peter Clausen 
3389054880feSMark Brown 	mutex_lock(&client_mutex);
3390bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3391054880feSMark Brown 	list_add(&codec->list, &codec_list);
3392054880feSMark Brown 	mutex_unlock(&client_mutex);
3393054880feSMark Brown 
3394f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3395f4333203SLars-Peter Clausen 		codec->component.name);
33960d0cf00aSMark Brown 	return 0;
3397f0fba2adSLiam Girdwood 
3398bb13109dSLars-Peter Clausen err_cleanup:
3399bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3400bb13109dSLars-Peter Clausen err_free:
3401f0fba2adSLiam Girdwood 	kfree(codec);
3402f0fba2adSLiam Girdwood 	return ret;
34030d0cf00aSMark Brown }
34040d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
34050d0cf00aSMark Brown 
34060d0cf00aSMark Brown /**
34070d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
34080d0cf00aSMark Brown  *
3409628536eaSJonathan Corbet  * @dev: codec to unregister
34100d0cf00aSMark Brown  */
3411f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
34120d0cf00aSMark Brown {
3413f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3414f0fba2adSLiam Girdwood 
341534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3416f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3417f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3418f0fba2adSLiam Girdwood 			goto found;
3419f0fba2adSLiam Girdwood 	}
342034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3421f0fba2adSLiam Girdwood 	return;
3422f0fba2adSLiam Girdwood 
3423f0fba2adSLiam Girdwood found:
34240d0cf00aSMark Brown 	list_del(&codec->list);
3425bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
34260d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
34270d0cf00aSMark Brown 
3428f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3429f4333203SLars-Peter Clausen 			codec->component.name);
3430f0fba2adSLiam Girdwood 
3431bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
34327a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3433f0fba2adSLiam Girdwood 	kfree(codec);
34340d0cf00aSMark Brown }
34350d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
34360d0cf00aSMark Brown 
3437bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3438bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3439bec4fa05SStephen Warren 			       const char *propname)
3440bec4fa05SStephen Warren {
34417e07e7c0STushar Behera 	struct device_node *np;
3442bec4fa05SStephen Warren 	int ret;
3443bec4fa05SStephen Warren 
34447e07e7c0STushar Behera 	if (!card->dev) {
34457e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
34467e07e7c0STushar Behera 		return -EINVAL;
34477e07e7c0STushar Behera 	}
34487e07e7c0STushar Behera 
34497e07e7c0STushar Behera 	np = card->dev->of_node;
34507e07e7c0STushar Behera 
3451bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3452bec4fa05SStephen Warren 	/*
3453bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3454bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3455bec4fa05SStephen Warren 	 * snd_soc_register_card.
3456bec4fa05SStephen Warren 	 */
3457bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3458bec4fa05SStephen Warren 		dev_err(card->dev,
3459f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3460bec4fa05SStephen Warren 			propname, ret);
3461bec4fa05SStephen Warren 		return ret;
3462bec4fa05SStephen Warren 	}
3463bec4fa05SStephen Warren 
3464bec4fa05SStephen Warren 	return 0;
3465bec4fa05SStephen Warren }
3466bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3467bec4fa05SStephen Warren 
34689a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
34699a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
34709a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
34719a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
34729a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
34739a6d4860SXiubo Li };
34749a6d4860SXiubo Li 
34759a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
34769a6d4860SXiubo Li 					  const char *propname)
34779a6d4860SXiubo Li {
34789a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
34799a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
34809a6d4860SXiubo Li 	const char *template, *wname;
34819a6d4860SXiubo Li 	int i, j, num_widgets, ret;
34829a6d4860SXiubo Li 
34839a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
34849a6d4860SXiubo Li 	if (num_widgets < 0) {
34859a6d4860SXiubo Li 		dev_err(card->dev,
34869a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
34879a6d4860SXiubo Li 		return -EINVAL;
34889a6d4860SXiubo Li 	}
34899a6d4860SXiubo Li 	if (num_widgets & 1) {
34909a6d4860SXiubo Li 		dev_err(card->dev,
34919a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
34929a6d4860SXiubo Li 		return -EINVAL;
34939a6d4860SXiubo Li 	}
34949a6d4860SXiubo Li 
34959a6d4860SXiubo Li 	num_widgets /= 2;
34969a6d4860SXiubo Li 	if (!num_widgets) {
34979a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
34989a6d4860SXiubo Li 			propname);
34999a6d4860SXiubo Li 		return -EINVAL;
35009a6d4860SXiubo Li 	}
35019a6d4860SXiubo Li 
35029a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
35039a6d4860SXiubo Li 			       GFP_KERNEL);
35049a6d4860SXiubo Li 	if (!widgets) {
35059a6d4860SXiubo Li 		dev_err(card->dev,
35069a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
35079a6d4860SXiubo Li 		return -ENOMEM;
35089a6d4860SXiubo Li 	}
35099a6d4860SXiubo Li 
35109a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
35119a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35129a6d4860SXiubo Li 			2 * i, &template);
35139a6d4860SXiubo Li 		if (ret) {
35149a6d4860SXiubo Li 			dev_err(card->dev,
35159a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35169a6d4860SXiubo Li 				propname, 2 * i, ret);
35179a6d4860SXiubo Li 			return -EINVAL;
35189a6d4860SXiubo Li 		}
35199a6d4860SXiubo Li 
35209a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
35219a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
35229a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
35239a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
35249a6d4860SXiubo Li 				break;
35259a6d4860SXiubo Li 			}
35269a6d4860SXiubo Li 		}
35279a6d4860SXiubo Li 
35289a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
35299a6d4860SXiubo Li 			dev_err(card->dev,
35309a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
35319a6d4860SXiubo Li 				template);
35329a6d4860SXiubo Li 			return -EINVAL;
35339a6d4860SXiubo Li 		}
35349a6d4860SXiubo Li 
35359a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35369a6d4860SXiubo Li 						    (2 * i) + 1,
35379a6d4860SXiubo Li 						    &wname);
35389a6d4860SXiubo Li 		if (ret) {
35399a6d4860SXiubo Li 			dev_err(card->dev,
35409a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35419a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
35429a6d4860SXiubo Li 			return -EINVAL;
35439a6d4860SXiubo Li 		}
35449a6d4860SXiubo Li 
35459a6d4860SXiubo Li 		widgets[i].name = wname;
35469a6d4860SXiubo Li 	}
35479a6d4860SXiubo Li 
3548f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3549f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
35509a6d4860SXiubo Li 
35519a6d4860SXiubo Li 	return 0;
35529a6d4860SXiubo Li }
35539a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
35549a6d4860SXiubo Li 
35556131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
35566131084aSJyri Sarha 				    const char *prop_name,
35576131084aSJyri Sarha 				    unsigned int *mask)
35586131084aSJyri Sarha {
35596131084aSJyri Sarha 	u32 val;
35606c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
35616131084aSJyri Sarha 	int i;
35626131084aSJyri Sarha 
35636131084aSJyri Sarha 	if (!of_slot_mask)
35646131084aSJyri Sarha 		return 0;
35656131084aSJyri Sarha 	val /= sizeof(u32);
35666131084aSJyri Sarha 	for (i = 0; i < val; i++)
35676131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
35686131084aSJyri Sarha 			*mask |= (1 << i);
35696131084aSJyri Sarha 
35706131084aSJyri Sarha 	return val;
35716131084aSJyri Sarha }
35726131084aSJyri Sarha 
357389c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
35746131084aSJyri Sarha 			      unsigned int *tx_mask,
35756131084aSJyri Sarha 			      unsigned int *rx_mask,
357689c67857SXiubo Li 			      unsigned int *slots,
357789c67857SXiubo Li 			      unsigned int *slot_width)
357889c67857SXiubo Li {
357989c67857SXiubo Li 	u32 val;
358089c67857SXiubo Li 	int ret;
358189c67857SXiubo Li 
35826131084aSJyri Sarha 	if (tx_mask)
35836131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
35846131084aSJyri Sarha 	if (rx_mask)
35856131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
35866131084aSJyri Sarha 
358789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
358889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
358989c67857SXiubo Li 		if (ret)
359089c67857SXiubo Li 			return ret;
359189c67857SXiubo Li 
359289c67857SXiubo Li 		if (slots)
359389c67857SXiubo Li 			*slots = val;
359489c67857SXiubo Li 	}
359589c67857SXiubo Li 
359689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
359789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
359889c67857SXiubo Li 		if (ret)
359989c67857SXiubo Li 			return ret;
360089c67857SXiubo Li 
360189c67857SXiubo Li 		if (slot_width)
360289c67857SXiubo Li 			*slot_width = val;
360389c67857SXiubo Li 	}
360489c67857SXiubo Li 
360589c67857SXiubo Li 	return 0;
360689c67857SXiubo Li }
360789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
360889c67857SXiubo Li 
36095e3cdaa2SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
36105e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
36115e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
36125e3cdaa2SKuninori Morimoto 				   const char *propname)
36135e3cdaa2SKuninori Morimoto {
36145e3cdaa2SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
36155e3cdaa2SKuninori Morimoto 	const char *str;
36165e3cdaa2SKuninori Morimoto 	int ret;
36175e3cdaa2SKuninori Morimoto 
36185e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
36195e3cdaa2SKuninori Morimoto 	if (ret < 0) {
36205e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
36215e3cdaa2SKuninori Morimoto 		return;
36225e3cdaa2SKuninori Morimoto 	}
36235e3cdaa2SKuninori Morimoto 
36245e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
36255e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
36265e3cdaa2SKuninori Morimoto }
36275e3cdaa2SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
36285e3cdaa2SKuninori Morimoto 
3629a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3630a4a54dd5SStephen Warren 				   const char *propname)
3631a4a54dd5SStephen Warren {
3632a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3633e3b1e6a1SMark Brown 	int num_routes;
3634a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3635a4a54dd5SStephen Warren 	int i, ret;
3636a4a54dd5SStephen Warren 
3637a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3638c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
363910e8aa9aSMichał Mirosław 		dev_err(card->dev,
364010e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
364110e8aa9aSMichał Mirosław 			propname);
3642a4a54dd5SStephen Warren 		return -EINVAL;
3643a4a54dd5SStephen Warren 	}
3644a4a54dd5SStephen Warren 	num_routes /= 2;
3645a4a54dd5SStephen Warren 	if (!num_routes) {
3646f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3647a4a54dd5SStephen Warren 			propname);
3648a4a54dd5SStephen Warren 		return -EINVAL;
3649a4a54dd5SStephen Warren 	}
3650a4a54dd5SStephen Warren 
3651e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3652a4a54dd5SStephen Warren 			      GFP_KERNEL);
3653a4a54dd5SStephen Warren 	if (!routes) {
3654a4a54dd5SStephen Warren 		dev_err(card->dev,
3655f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3656a4a54dd5SStephen Warren 		return -EINVAL;
3657a4a54dd5SStephen Warren 	}
3658a4a54dd5SStephen Warren 
3659a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3660a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3661e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3662a4a54dd5SStephen Warren 		if (ret) {
3663c871bd0bSMark Brown 			dev_err(card->dev,
3664c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3665c871bd0bSMark Brown 				propname, 2 * i, ret);
3666a4a54dd5SStephen Warren 			return -EINVAL;
3667a4a54dd5SStephen Warren 		}
3668a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3669e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3670a4a54dd5SStephen Warren 		if (ret) {
3671a4a54dd5SStephen Warren 			dev_err(card->dev,
3672c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3673c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3674a4a54dd5SStephen Warren 			return -EINVAL;
3675a4a54dd5SStephen Warren 		}
3676a4a54dd5SStephen Warren 	}
3677a4a54dd5SStephen Warren 
3678f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3679f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3680a4a54dd5SStephen Warren 
3681a4a54dd5SStephen Warren 	return 0;
3682a4a54dd5SStephen Warren }
3683a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3684a4a54dd5SStephen Warren 
3685a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3686389cb834SJyri Sarha 				     const char *prefix,
3687389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3688389cb834SJyri Sarha 				     struct device_node **framemaster)
3689a7930ed4SKuninori Morimoto {
3690a7930ed4SKuninori Morimoto 	int ret, i;
3691a7930ed4SKuninori Morimoto 	char prop[128];
3692a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3693a7930ed4SKuninori Morimoto 	int bit, frame;
3694a7930ed4SKuninori Morimoto 	const char *str;
3695a7930ed4SKuninori Morimoto 	struct {
3696a7930ed4SKuninori Morimoto 		char *name;
3697a7930ed4SKuninori Morimoto 		unsigned int val;
3698a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3699a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3700a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3701a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3702a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3703a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3704a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3705a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3706a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3707a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3708a7930ed4SKuninori Morimoto 	};
3709a7930ed4SKuninori Morimoto 
3710a7930ed4SKuninori Morimoto 	if (!prefix)
3711a7930ed4SKuninori Morimoto 		prefix = "";
3712a7930ed4SKuninori Morimoto 
3713a7930ed4SKuninori Morimoto 	/*
3714a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3715a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3716a7930ed4SKuninori Morimoto 	 */
3717a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3718a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3719a7930ed4SKuninori Morimoto 	if (ret == 0) {
3720a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3721a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3722a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3723a7930ed4SKuninori Morimoto 				break;
3724a7930ed4SKuninori Morimoto 			}
3725a7930ed4SKuninori Morimoto 		}
3726a7930ed4SKuninori Morimoto 	}
3727a7930ed4SKuninori Morimoto 
3728a7930ed4SKuninori Morimoto 	/*
37298c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3730a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3731a7930ed4SKuninori Morimoto 	 */
37328c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
373351930295SJulia Lawall 	if (of_property_read_bool(np, prop))
37348c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
37358c2d6a9fSKuninori Morimoto 	else
37368c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3737a7930ed4SKuninori Morimoto 
3738a7930ed4SKuninori Morimoto 	/*
3739a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3740a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3741a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3742a7930ed4SKuninori Morimoto 	 */
3743a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3744a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3745a7930ed4SKuninori Morimoto 
3746a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3747a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3748a7930ed4SKuninori Morimoto 
3749a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3750a7930ed4SKuninori Morimoto 	case 0x11:
3751a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3752a7930ed4SKuninori Morimoto 		break;
3753a7930ed4SKuninori Morimoto 	case 0x10:
3754a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3755a7930ed4SKuninori Morimoto 		break;
3756a7930ed4SKuninori Morimoto 	case 0x01:
3757a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3758a7930ed4SKuninori Morimoto 		break;
3759a7930ed4SKuninori Morimoto 	default:
3760a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3761a7930ed4SKuninori Morimoto 		break;
3762a7930ed4SKuninori Morimoto 	}
3763a7930ed4SKuninori Morimoto 
3764a7930ed4SKuninori Morimoto 	/*
3765a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3766a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3767a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3768a7930ed4SKuninori Morimoto 	 */
3769a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3770a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3771389cb834SJyri Sarha 	if (bit && bitclkmaster)
3772389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3773a7930ed4SKuninori Morimoto 
3774a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3775a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3776389cb834SJyri Sarha 	if (frame && framemaster)
3777389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3778a7930ed4SKuninori Morimoto 
3779a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3780a7930ed4SKuninori Morimoto 	case 0x11:
3781a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3782a7930ed4SKuninori Morimoto 		break;
3783a7930ed4SKuninori Morimoto 	case 0x10:
3784a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3785a7930ed4SKuninori Morimoto 		break;
3786a7930ed4SKuninori Morimoto 	case 0x01:
3787a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3788a7930ed4SKuninori Morimoto 		break;
3789a7930ed4SKuninori Morimoto 	default:
3790a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3791a7930ed4SKuninori Morimoto 		break;
3792a7930ed4SKuninori Morimoto 	}
3793a7930ed4SKuninori Morimoto 
3794a7930ed4SKuninori Morimoto 	return format;
3795a7930ed4SKuninori Morimoto }
3796a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3797a7930ed4SKuninori Morimoto 
379893b0f3eeSJean-Francois Moine static int snd_soc_get_dai_name(struct of_phandle_args *args,
3799cb470087SKuninori Morimoto 				const char **dai_name)
3800cb470087SKuninori Morimoto {
3801cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
38023e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
380393b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3804cb470087SKuninori Morimoto 
3805cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3806cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
38073e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
38083e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
38093e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
38103e0aa8d8SJyri Sarha 
38113e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3812cb470087SKuninori Morimoto 			continue;
3813cb470087SKuninori Morimoto 
38146833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
381593b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
381693b0f3eeSJean-Francois Moine 							     args,
381793b0f3eeSJean-Francois Moine 							     dai_name);
38186833c452SKuninori Morimoto 		} else {
38196833c452SKuninori Morimoto 			int id = -1;
38206833c452SKuninori Morimoto 
382193b0f3eeSJean-Francois Moine 			switch (args->args_count) {
38226833c452SKuninori Morimoto 			case 0:
38236833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
38246833c452SKuninori Morimoto 				break;
38256833c452SKuninori Morimoto 			case 1:
382693b0f3eeSJean-Francois Moine 				id = args->args[0];
38276833c452SKuninori Morimoto 				break;
38286833c452SKuninori Morimoto 			default:
38296833c452SKuninori Morimoto 				/* not supported */
3830cb470087SKuninori Morimoto 				break;
3831cb470087SKuninori Morimoto 			}
3832cb470087SKuninori Morimoto 
38336833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
38346833c452SKuninori Morimoto 				ret = -EINVAL;
38353dcba280SNicolin Chen 				continue;
38366833c452SKuninori Morimoto 			}
3837e41975edSXiubo Li 
3838e41975edSXiubo Li 			ret = 0;
3839e41975edSXiubo Li 
3840e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3841e41975edSXiubo Li 			if (!*dai_name)
3842e41975edSXiubo Li 				*dai_name = pos->name;
38436833c452SKuninori Morimoto 		}
38446833c452SKuninori Morimoto 
3845cb470087SKuninori Morimoto 		break;
3846cb470087SKuninori Morimoto 	}
3847cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
384893b0f3eeSJean-Francois Moine 	return ret;
384993b0f3eeSJean-Francois Moine }
385093b0f3eeSJean-Francois Moine 
385193b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
385293b0f3eeSJean-Francois Moine 			    const char **dai_name)
385393b0f3eeSJean-Francois Moine {
385493b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
385593b0f3eeSJean-Francois Moine 	int ret;
385693b0f3eeSJean-Francois Moine 
385793b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
385893b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
385993b0f3eeSJean-Francois Moine 	if (ret)
386093b0f3eeSJean-Francois Moine 		return ret;
386193b0f3eeSJean-Francois Moine 
386293b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3863cb470087SKuninori Morimoto 
3864cb470087SKuninori Morimoto 	of_node_put(args.np);
3865cb470087SKuninori Morimoto 
3866cb470087SKuninori Morimoto 	return ret;
3867cb470087SKuninori Morimoto }
3868cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3869cb470087SKuninori Morimoto 
387093b0f3eeSJean-Francois Moine /*
387193b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
387293b0f3eeSJean-Francois Moine  * @dev: Card device
387393b0f3eeSJean-Francois Moine  * @of_node: Device node
387493b0f3eeSJean-Francois Moine  * @dai_link: DAI link
387593b0f3eeSJean-Francois Moine  *
387693b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
387793b0f3eeSJean-Francois Moine  * 'sound-dai'.
387893b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
387993b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
388093b0f3eeSJean-Francois Moine  *
388193b0f3eeSJean-Francois Moine  * Returns 0 for success
388293b0f3eeSJean-Francois Moine  */
388393b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
388493b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
388593b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
388693b0f3eeSJean-Francois Moine {
388793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
388893b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
388993b0f3eeSJean-Francois Moine 	char *name;
389093b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
389193b0f3eeSJean-Francois Moine 
389293b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
389393b0f3eeSJean-Francois Moine 	name = "sound-dai";
389493b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
389593b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
389693b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
389793b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
389893b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
389993b0f3eeSJean-Francois Moine 		else
390093b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
390193b0f3eeSJean-Francois Moine 		return num_codecs;
390293b0f3eeSJean-Francois Moine 	}
390393b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
390493b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
390593b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
390693b0f3eeSJean-Francois Moine 	if (!component)
390793b0f3eeSJean-Francois Moine 		return -ENOMEM;
390893b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
390993b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
391093b0f3eeSJean-Francois Moine 
391193b0f3eeSJean-Francois Moine 	/* Parse the list */
391293b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
391393b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
391493b0f3eeSJean-Francois Moine 	     index++, component++) {
391593b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
391693b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
391793b0f3eeSJean-Francois Moine 						  index, &args);
391893b0f3eeSJean-Francois Moine 		if (ret)
391993b0f3eeSJean-Francois Moine 			goto err;
392093b0f3eeSJean-Francois Moine 		component->of_node = args.np;
392193b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
392293b0f3eeSJean-Francois Moine 		if (ret < 0)
392393b0f3eeSJean-Francois Moine 			goto err;
392493b0f3eeSJean-Francois Moine 	}
392593b0f3eeSJean-Francois Moine 	return 0;
392693b0f3eeSJean-Francois Moine err:
392793b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
392893b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
392993b0f3eeSJean-Francois Moine 	     index++, component++) {
393093b0f3eeSJean-Francois Moine 		if (!component->of_node)
393193b0f3eeSJean-Francois Moine 			break;
393293b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
393393b0f3eeSJean-Francois Moine 		component->of_node = NULL;
393493b0f3eeSJean-Francois Moine 	}
393593b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
393693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
393793b0f3eeSJean-Francois Moine 	return ret;
393893b0f3eeSJean-Francois Moine }
393993b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
394093b0f3eeSJean-Francois Moine 
3941c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3942db2a4165SFrank Mandarino {
39436553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3944fb257897SMark Brown 	snd_soc_util_init();
3945fb257897SMark Brown 
3946db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3947db2a4165SFrank Mandarino }
39484abe8e16SMark Brown module_init(snd_soc_init);
3949db2a4165SFrank Mandarino 
39507d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3951db2a4165SFrank Mandarino {
3952fb257897SMark Brown 	snd_soc_util_exit();
39536553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3954fb257897SMark Brown 
3955384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3956384c89e2SMark Brown #endif
3957db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3958db2a4165SFrank Mandarino }
3959db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3960db2a4165SFrank Mandarino 
3961db2a4165SFrank Mandarino /* Module information */
3962d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3963db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3964db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3965db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3966