xref: /openbmc/linux/sound/soc/soc-core.c (revision a180e8b988437b3e84a1b501ac4d073467602ca6)
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>
37*a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
38345233d7SLiam Girdwood #include <linux/dmi.h>
39db2a4165SFrank Mandarino #include <sound/core.h>
403028eb8cSMark Brown #include <sound/jack.h>
41db2a4165SFrank Mandarino #include <sound/pcm.h>
42db2a4165SFrank Mandarino #include <sound/pcm_params.h>
43db2a4165SFrank Mandarino #include <sound/soc.h>
4401d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
458a978234SLiam Girdwood #include <sound/soc-topology.h>
46db2a4165SFrank Mandarino #include <sound/initval.h>
47db2a4165SFrank Mandarino 
48a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
49a8b1d34fSMark Brown #include <trace/events/asoc.h>
50a8b1d34fSMark Brown 
51f0fba2adSLiam Girdwood #define NAME_SIZE	32
52f0fba2adSLiam Girdwood 
53384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
548a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
558a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56384c89e2SMark Brown #endif
57384c89e2SMark Brown 
58c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5912a48a8cSMark Brown static LIST_HEAD(platform_list);
600d0cf00aSMark Brown static LIST_HEAD(codec_list);
61030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
62c5af3a2eSMark Brown 
63db2a4165SFrank Mandarino /*
64db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
65db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
66db2a4165SFrank Mandarino  * between two audio tracks.
67db2a4165SFrank Mandarino  */
68db2a4165SFrank Mandarino static int pmdown_time = 5000;
69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71db2a4165SFrank Mandarino 
722bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
732bc9a81eSDimitris Papastamos  * a particular given value */
742bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
752bc9a81eSDimitris Papastamos {
762bc9a81eSDimitris Papastamos 	int c = 0;
772bc9a81eSDimitris Papastamos 	int i;
782bc9a81eSDimitris Papastamos 
792bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
802bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
812bc9a81eSDimitris Papastamos 			break;
822bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
832bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
842bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
852bc9a81eSDimitris Papastamos 	else
862bc9a81eSDimitris Papastamos 		c /= 8;
872bc9a81eSDimitris Papastamos 	return c;
882bc9a81eSDimitris Papastamos }
892bc9a81eSDimitris Papastamos 
9013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
9113fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9313fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
942624d5faSMark Brown {
9500b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9600b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9713fd179fSDimitris Papastamos 	int ret;
9813fd179fSDimitris Papastamos 
9913fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10013fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10113fd179fSDimitris Papastamos 		return -EINVAL;
10213fd179fSDimitris Papastamos 
103d6b6c2caSTakashi Iwai 	sprintf(buf, "%.*x: ", wordsize, reg);
104d6b6c2caSTakashi Iwai 	buf += wordsize + 2;
105d6b6c2caSTakashi Iwai 
10613fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
107d6b6c2caSTakashi Iwai 	if (ret < 0)
108d6b6c2caSTakashi Iwai 		memset(buf, 'X', regsize);
109d6b6c2caSTakashi Iwai 	else
110d6b6c2caSTakashi Iwai 		sprintf(buf, "%.*x", regsize, ret);
111d6b6c2caSTakashi Iwai 	buf[regsize] = '\n';
112d6b6c2caSTakashi Iwai 	/* no NUL-termination needed */
11313fd179fSDimitris Papastamos 	return 0;
11413fd179fSDimitris Papastamos }
11513fd179fSDimitris Papastamos 
11613fd179fSDimitris Papastamos /* codec register dump */
11713fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
11813fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
11913fd179fSDimitris Papastamos {
12013fd179fSDimitris Papastamos 	int i, step = 1;
1212bc9a81eSDimitris Papastamos 	int wordsize, regsize;
12213fd179fSDimitris Papastamos 	int len;
12313fd179fSDimitris Papastamos 	size_t total = 0;
12413fd179fSDimitris Papastamos 	loff_t p = 0;
1252bc9a81eSDimitris Papastamos 
12600b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
12700b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1282624d5faSMark Brown 
12913fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13013fd179fSDimitris Papastamos 
131f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1322624d5faSMark Brown 		return 0;
1332624d5faSMark Brown 
134f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
135f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1362624d5faSMark Brown 
137f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
13813fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
13913fd179fSDimitris Papastamos 		 * entries for the default case */
14013fd179fSDimitris Papastamos 		if (p >= pos) {
14113fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1422624d5faSMark Brown 				break;
14313fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
14413fd179fSDimitris Papastamos 			total += len;
14513fd179fSDimitris Papastamos 		}
14613fd179fSDimitris Papastamos 		p += len;
14713fd179fSDimitris Papastamos 	}
1482624d5faSMark Brown 
14913fd179fSDimitris Papastamos 	total = min(total, count - 1);
1502624d5faSMark Brown 
15113fd179fSDimitris Papastamos 	return total;
1522624d5faSMark Brown }
15313fd179fSDimitris Papastamos 
1542624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1552624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1562624d5faSMark Brown {
15736ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
158f0fba2adSLiam Girdwood 
15913fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1602624d5faSMark Brown }
1612624d5faSMark Brown 
1622624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1632624d5faSMark Brown 
164dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
165dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
166dbe21408SMark Brown {
16736ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
168dbe21408SMark Brown 
169f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
170dbe21408SMark Brown }
171dbe21408SMark Brown 
172dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
173dbe21408SMark Brown 			       struct device_attribute *attr,
174dbe21408SMark Brown 			       const char *buf, size_t count)
175dbe21408SMark Brown {
17636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
177c593b520SMark Brown 	int ret;
178dbe21408SMark Brown 
179b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
180c593b520SMark Brown 	if (ret)
181c593b520SMark Brown 		return ret;
182dbe21408SMark Brown 
183dbe21408SMark Brown 	return count;
184dbe21408SMark Brown }
185dbe21408SMark Brown 
186dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
187dbe21408SMark Brown 
188d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
189d29697dcSTakashi Iwai 	&dev_attr_codec_reg.attr,
190d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
191d29697dcSTakashi Iwai 	NULL
192d29697dcSTakashi Iwai };
193d29697dcSTakashi Iwai 
194d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
195d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
196d29697dcSTakashi Iwai {
197d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
198d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
199d29697dcSTakashi Iwai 
200d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
201d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
202d29697dcSTakashi Iwai 	return rtd->codec ? attr->mode : 0; /* enabled only with codec */
203d29697dcSTakashi Iwai }
204d29697dcSTakashi Iwai 
205d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
206d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
207d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
208d29697dcSTakashi Iwai };
209d29697dcSTakashi Iwai 
210d29697dcSTakashi Iwai static const struct attribute_group soc_dev_roup = {
211d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
212d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
213d29697dcSTakashi Iwai };
214d29697dcSTakashi Iwai 
215d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
216d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
217d29697dcSTakashi Iwai 	&soc_dev_roup,
218d29697dcSTakashi Iwai 	NULL
219d29697dcSTakashi Iwai };
220d29697dcSTakashi Iwai 
2212624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2222624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2232624d5faSMark Brown 				   size_t count, loff_t *ppos)
2242624d5faSMark Brown {
2252624d5faSMark Brown 	ssize_t ret;
2262624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
22713fd179fSDimitris Papastamos 	char *buf;
22813fd179fSDimitris Papastamos 
22913fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
23013fd179fSDimitris Papastamos 		return -EINVAL;
23113fd179fSDimitris Papastamos 
23213fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2332624d5faSMark Brown 	if (!buf)
2342624d5faSMark Brown 		return -ENOMEM;
23513fd179fSDimitris Papastamos 
23613fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
23713fd179fSDimitris Papastamos 	if (ret >= 0) {
23813fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23913fd179fSDimitris Papastamos 			kfree(buf);
24013fd179fSDimitris Papastamos 			return -EFAULT;
24113fd179fSDimitris Papastamos 		}
24213fd179fSDimitris Papastamos 		*ppos += ret;
24313fd179fSDimitris Papastamos 	}
24413fd179fSDimitris Papastamos 
2452624d5faSMark Brown 	kfree(buf);
2462624d5faSMark Brown 	return ret;
2472624d5faSMark Brown }
2482624d5faSMark Brown 
2492624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2502624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2512624d5faSMark Brown {
2522624d5faSMark Brown 	char buf[32];
25334e268d8SStephen Boyd 	size_t buf_size;
2542624d5faSMark Brown 	char *start = buf;
2552624d5faSMark Brown 	unsigned long reg, value;
2562624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
257b785a492SJingoo Han 	int ret;
2582624d5faSMark Brown 
2592624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2602624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2612624d5faSMark Brown 		return -EFAULT;
2622624d5faSMark Brown 	buf[buf_size] = 0;
2632624d5faSMark Brown 
2642624d5faSMark Brown 	while (*start == ' ')
2652624d5faSMark Brown 		start++;
2662624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2672624d5faSMark Brown 	while (*start == ' ')
2682624d5faSMark Brown 		start++;
269b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
270b785a492SJingoo Han 	if (ret)
271b785a492SJingoo Han 		return ret;
2720d51a9cbSMark Brown 
2730d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
274373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2750d51a9cbSMark Brown 
276e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2772624d5faSMark Brown 	return buf_size;
2782624d5faSMark Brown }
2792624d5faSMark Brown 
2802624d5faSMark Brown static const struct file_operations codec_reg_fops = {
281234e3405SStephen Boyd 	.open = simple_open,
2822624d5faSMark Brown 	.read = codec_reg_read_file,
2832624d5faSMark Brown 	.write = codec_reg_write_file,
2846038f373SArnd Bergmann 	.llseek = default_llseek,
2852624d5faSMark Brown };
2862624d5faSMark Brown 
28781c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
288e73f3de5SRussell King {
2896553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
2906553bf06SLars-Peter Clausen 		return;
2916553bf06SLars-Peter Clausen 
29281c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
29381c7cfd1SLars-Peter Clausen 		char *name;
294e73f3de5SRussell King 
29581c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
29681c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
29781c7cfd1SLars-Peter Clausen 		if (name) {
29881c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
29981c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
30081c7cfd1SLars-Peter Clausen 			kfree(name);
30181c7cfd1SLars-Peter Clausen 		}
30281c7cfd1SLars-Peter Clausen 	} else {
30381c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
30481c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
305e73f3de5SRussell King 	}
306e73f3de5SRussell King 
30781c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
30881c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
30981c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
3102624d5faSMark Brown 		return;
3112624d5faSMark Brown 	}
3122624d5faSMark Brown 
31381c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
31481c7cfd1SLars-Peter Clausen 		component->debugfs_root);
31581c7cfd1SLars-Peter Clausen 
31681c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
31781c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
31881c7cfd1SLars-Peter Clausen }
31981c7cfd1SLars-Peter Clausen 
32081c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
32181c7cfd1SLars-Peter Clausen {
32281c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
32381c7cfd1SLars-Peter Clausen }
32481c7cfd1SLars-Peter Clausen 
32581c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
32681c7cfd1SLars-Peter Clausen {
32781c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
32881c7cfd1SLars-Peter Clausen 
3292624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
33081c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3312624d5faSMark Brown 						 codec, &codec_reg_fops);
3322624d5faSMark Brown 	if (!codec->debugfs_reg)
33310e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
33410e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
335731f1ab2SSebastien Guiriec }
336731f1ab2SSebastien Guiriec 
337c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
338c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
339c3c5a19aSMark Brown {
340c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3412b194f9dSMark Brown 	ssize_t len, ret = 0;
342c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
343c3c5a19aSMark Brown 
344c3c5a19aSMark Brown 	if (!buf)
345c3c5a19aSMark Brown 		return -ENOMEM;
346c3c5a19aSMark Brown 
34734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
34834e81ab4SLars-Peter Clausen 
3492b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3502b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
351f4333203SLars-Peter Clausen 			       codec->component.name);
3522b194f9dSMark Brown 		if (len >= 0)
3532b194f9dSMark Brown 			ret += len;
3542b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3552b194f9dSMark Brown 			ret = PAGE_SIZE;
3562b194f9dSMark Brown 			break;
3572b194f9dSMark Brown 		}
3582b194f9dSMark Brown 	}
359c3c5a19aSMark Brown 
36034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
36134e81ab4SLars-Peter Clausen 
362c3c5a19aSMark Brown 	if (ret >= 0)
363c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
364c3c5a19aSMark Brown 
365c3c5a19aSMark Brown 	kfree(buf);
366c3c5a19aSMark Brown 
367c3c5a19aSMark Brown 	return ret;
368c3c5a19aSMark Brown }
369c3c5a19aSMark Brown 
370c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
371c3c5a19aSMark Brown 	.read = codec_list_read_file,
372c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
373c3c5a19aSMark Brown };
374c3c5a19aSMark Brown 
375f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
376f3208780SMark Brown 				  size_t count, loff_t *ppos)
377f3208780SMark Brown {
378f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3792b194f9dSMark Brown 	ssize_t len, ret = 0;
3801438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
381f3208780SMark Brown 	struct snd_soc_dai *dai;
382f3208780SMark Brown 
383f3208780SMark Brown 	if (!buf)
384f3208780SMark Brown 		return -ENOMEM;
385f3208780SMark Brown 
38634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
38734e81ab4SLars-Peter Clausen 
3881438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3891438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3901438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3911438c2f6SLars-Peter Clausen 				dai->name);
3922b194f9dSMark Brown 			if (len >= 0)
3932b194f9dSMark Brown 				ret += len;
3942b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3952b194f9dSMark Brown 				ret = PAGE_SIZE;
3962b194f9dSMark Brown 				break;
3972b194f9dSMark Brown 			}
3982b194f9dSMark Brown 		}
3991438c2f6SLars-Peter Clausen 	}
400f3208780SMark Brown 
40134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
40234e81ab4SLars-Peter Clausen 
403f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
404f3208780SMark Brown 
405f3208780SMark Brown 	kfree(buf);
406f3208780SMark Brown 
407f3208780SMark Brown 	return ret;
408f3208780SMark Brown }
409f3208780SMark Brown 
410f3208780SMark Brown static const struct file_operations dai_list_fops = {
411f3208780SMark Brown 	.read = dai_list_read_file,
412f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
413f3208780SMark Brown };
414f3208780SMark Brown 
41519c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41619c7ac27SMark Brown 				       char __user *user_buf,
41719c7ac27SMark Brown 				       size_t count, loff_t *ppos)
41819c7ac27SMark Brown {
41919c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4202b194f9dSMark Brown 	ssize_t len, ret = 0;
42119c7ac27SMark Brown 	struct snd_soc_platform *platform;
42219c7ac27SMark Brown 
42319c7ac27SMark Brown 	if (!buf)
42419c7ac27SMark Brown 		return -ENOMEM;
42519c7ac27SMark Brown 
42634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
42734e81ab4SLars-Peter Clausen 
4282b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4292b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
430f4333203SLars-Peter Clausen 			       platform->component.name);
4312b194f9dSMark Brown 		if (len >= 0)
4322b194f9dSMark Brown 			ret += len;
4332b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4342b194f9dSMark Brown 			ret = PAGE_SIZE;
4352b194f9dSMark Brown 			break;
4362b194f9dSMark Brown 		}
4372b194f9dSMark Brown 	}
43819c7ac27SMark Brown 
43934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
44034e81ab4SLars-Peter Clausen 
44119c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
44219c7ac27SMark Brown 
44319c7ac27SMark Brown 	kfree(buf);
44419c7ac27SMark Brown 
44519c7ac27SMark Brown 	return ret;
44619c7ac27SMark Brown }
44719c7ac27SMark Brown 
44819c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44919c7ac27SMark Brown 	.read = platform_list_read_file,
45019c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
45119c7ac27SMark Brown };
45219c7ac27SMark Brown 
453a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
454a6052154SJarkko Nikula {
4556553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
4566553bf06SLars-Peter Clausen 		return;
4576553bf06SLars-Peter Clausen 
458a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4598a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4603a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
461a6052154SJarkko Nikula 		dev_warn(card->dev,
4627c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4633a45b867SJarkko Nikula 		return;
4643a45b867SJarkko Nikula 	}
4653a45b867SJarkko Nikula 
4663a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4673a45b867SJarkko Nikula 						    card->debugfs_card_root,
4683a45b867SJarkko Nikula 						    &card->pop_time);
4693a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4703a45b867SJarkko Nikula 		dev_warn(card->dev,
471f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
472a6052154SJarkko Nikula }
473a6052154SJarkko Nikula 
474a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
475a6052154SJarkko Nikula {
476a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
477a6052154SJarkko Nikula }
478a6052154SJarkko Nikula 
4796553bf06SLars-Peter Clausen 
4806553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
4816553bf06SLars-Peter Clausen {
4826553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4836553bf06SLars-Peter Clausen 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4846553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
4856553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
4866553bf06SLars-Peter Clausen 		return;
4876553bf06SLars-Peter Clausen 	}
4886553bf06SLars-Peter Clausen 
4896553bf06SLars-Peter Clausen 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4906553bf06SLars-Peter Clausen 				 &codec_list_fops))
4916553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4926553bf06SLars-Peter Clausen 
4936553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4946553bf06SLars-Peter Clausen 				 &dai_list_fops))
4956553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4966553bf06SLars-Peter Clausen 
4976553bf06SLars-Peter Clausen 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4986553bf06SLars-Peter Clausen 				 &platform_list_fops))
4996553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
5006553bf06SLars-Peter Clausen }
5016553bf06SLars-Peter Clausen 
5026553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
5036553bf06SLars-Peter Clausen {
5046553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
5056553bf06SLars-Peter Clausen }
5066553bf06SLars-Peter Clausen 
5072624d5faSMark Brown #else
5082624d5faSMark Brown 
50981c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
51081c7cfd1SLars-Peter Clausen 
51181c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
51281c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
5132624d5faSMark Brown {
5142624d5faSMark Brown }
5152624d5faSMark Brown 
51681c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
51781c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
518731f1ab2SSebastien Guiriec {
519731f1ab2SSebastien Guiriec }
520731f1ab2SSebastien Guiriec 
521b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
522b95fccbcSAxel Lin {
523b95fccbcSAxel Lin }
524b95fccbcSAxel Lin 
525b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
526b95fccbcSAxel Lin {
527b95fccbcSAxel Lin }
5286553bf06SLars-Peter Clausen 
5296553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
5306553bf06SLars-Peter Clausen {
5316553bf06SLars-Peter Clausen }
5326553bf06SLars-Peter Clausen 
5336553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
5346553bf06SLars-Peter Clausen {
5356553bf06SLars-Peter Clausen }
5366553bf06SLars-Peter Clausen 
5372624d5faSMark Brown #endif
5382624d5faSMark Brown 
53947c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
54047c88fffSLiam Girdwood 		const char *dai_link, int stream)
54147c88fffSLiam Girdwood {
5421a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
54347c88fffSLiam Girdwood 
5441a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
5451a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
5461a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
5471a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
54847c88fffSLiam Girdwood 	}
549f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
55047c88fffSLiam Girdwood 	return NULL;
55147c88fffSLiam Girdwood }
55247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
55347c88fffSLiam Girdwood 
5541a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
5551a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
5561a497983SMengdong Lin {
5571a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5581a497983SMengdong Lin 
5591a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
5601a497983SMengdong Lin 	if (!rtd)
5611a497983SMengdong Lin 		return NULL;
5621a497983SMengdong Lin 
5631a497983SMengdong Lin 	rtd->card = card;
5641a497983SMengdong Lin 	rtd->dai_link = dai_link;
5651a497983SMengdong Lin 	rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
5661a497983SMengdong Lin 					dai_link->num_codecs,
5671a497983SMengdong Lin 					GFP_KERNEL);
5681a497983SMengdong Lin 	if (!rtd->codec_dais) {
5691a497983SMengdong Lin 		kfree(rtd);
5701a497983SMengdong Lin 		return NULL;
5711a497983SMengdong Lin 	}
5721a497983SMengdong Lin 
5731a497983SMengdong Lin 	return rtd;
5741a497983SMengdong Lin }
5751a497983SMengdong Lin 
5761a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
5771a497983SMengdong Lin {
5781a497983SMengdong Lin 	if (rtd && rtd->codec_dais)
5791a497983SMengdong Lin 		kfree(rtd->codec_dais);
5801a497983SMengdong Lin 	kfree(rtd);
5811a497983SMengdong Lin }
5821a497983SMengdong Lin 
5831a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
5841a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
5851a497983SMengdong Lin {
5861a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
5871a497983SMengdong Lin 	rtd->num = card->num_rtd;
5881a497983SMengdong Lin 	card->num_rtd++;
5891a497983SMengdong Lin }
5901a497983SMengdong Lin 
5911a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
5921a497983SMengdong Lin {
5931a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
5941a497983SMengdong Lin 
5951a497983SMengdong Lin 	list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
5961a497983SMengdong Lin 		list_del(&rtd->list);
5971a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
5981a497983SMengdong Lin 	}
5991a497983SMengdong Lin 
6001a497983SMengdong Lin 	card->num_rtd = 0;
6011a497983SMengdong Lin }
6021a497983SMengdong Lin 
60347c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
60447c88fffSLiam Girdwood 		const char *dai_link)
60547c88fffSLiam Girdwood {
6061a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
60747c88fffSLiam Girdwood 
6081a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6091a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
6101a497983SMengdong Lin 			return rtd;
61147c88fffSLiam Girdwood 	}
612f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
61347c88fffSLiam Girdwood 	return NULL;
61447c88fffSLiam Girdwood }
61547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
61647c88fffSLiam Girdwood 
6179d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
6189d58a077SRichard Fitzgerald {
6199d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
6209d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
6219d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
6229d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
6239d58a077SRichard Fitzgerald 	 */
6249d58a077SRichard Fitzgerald }
6259d58a077SRichard Fitzgerald 
6266f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
627db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
6286f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
629db2a4165SFrank Mandarino {
6306f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
631d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6321a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
6331a497983SMengdong Lin 	int i;
634db2a4165SFrank Mandarino 
635c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
636c5599b87SLars-Peter Clausen 	if (!card->instantiated)
637e3509ff0SDaniel Mack 		return 0;
638e3509ff0SDaniel Mack 
6396ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
6406ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
6416ed25978SAndy Green 	 */
642f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
643f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
644f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
6456ed25978SAndy Green 
6466ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
647f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6486ed25978SAndy Green 
649a00f90f9SMark Brown 	/* mute any active DACs */
6501a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6513efab7dcSMark Brown 
6521a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6533efab7dcSMark Brown 			continue;
6543efab7dcSMark Brown 
6551a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
6561a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
65788bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
65888bd870fSBenoit Cousson 
659f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
660f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
661db2a4165SFrank Mandarino 		}
66288bd870fSBenoit Cousson 	}
663db2a4165SFrank Mandarino 
6644ccab3e7SLiam Girdwood 	/* suspend all pcms */
6651a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6661a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6673efab7dcSMark Brown 			continue;
6683efab7dcSMark Brown 
6691a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
6703efab7dcSMark Brown 	}
6714ccab3e7SLiam Girdwood 
67287506549SMark Brown 	if (card->suspend_pre)
67370b2ac12SMark Brown 		card->suspend_pre(card);
674db2a4165SFrank Mandarino 
6751a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6761a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6773efab7dcSMark Brown 
6781a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6793efab7dcSMark Brown 			continue;
6803efab7dcSMark Brown 
681bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
682f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
683db2a4165SFrank Mandarino 	}
684db2a4165SFrank Mandarino 
68537660b6dSLars-Peter Clausen 	/* close any waiting streams */
6861a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
6871a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
688db2a4165SFrank Mandarino 
6891a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
6903efab7dcSMark Brown 
6911a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6923efab7dcSMark Brown 			continue;
6933efab7dcSMark Brown 
6941a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6957bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
696db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
697f0fba2adSLiam Girdwood 
6981a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6997bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
700db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
701db2a4165SFrank Mandarino 	}
702db2a4165SFrank Mandarino 
7038be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7048be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
705e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
706e2d32ff6SMark Brown 
7079178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
708d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
709d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
710d9fc4063SKuninori Morimoto 
7119178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
7121547aba9SMark Brown 		 * bias _ON and should not be suspended. */
7139178feb4SKuninori Morimoto 		if (!component->suspended) {
7144890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
7151547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
716125a25daSMark Brown 				/*
7179178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
718125a25daSMark Brown 				 * bias off then being in STANDBY
719125a25daSMark Brown 				 * means it's doing something,
720125a25daSMark Brown 				 * otherwise fall through.
721125a25daSMark Brown 				 */
7224890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
7239178feb4SKuninori Morimoto 					dev_dbg(component->dev,
72410e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
725125a25daSMark Brown 					break;
726125a25daSMark Brown 				}
727a8093297SLars-Peter Clausen 
7281547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
7299178feb4SKuninori Morimoto 				if (component->suspend)
7309178feb4SKuninori Morimoto 					component->suspend(component);
7319178feb4SKuninori Morimoto 				component->suspended = 1;
7329178feb4SKuninori Morimoto 				if (component->regmap)
7339178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
734988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
7359178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
7361547aba9SMark Brown 				break;
7371547aba9SMark Brown 			default:
7389178feb4SKuninori Morimoto 				dev_dbg(component->dev,
7399178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
7401547aba9SMark Brown 				break;
7411547aba9SMark Brown 			}
7421547aba9SMark Brown 		}
743f0fba2adSLiam Girdwood 	}
744db2a4165SFrank Mandarino 
7451a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7461a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7473efab7dcSMark Brown 
7481a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7493efab7dcSMark Brown 			continue;
7503efab7dcSMark Brown 
751bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
752f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
753988e8cc4SNicolin Chen 
754988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
755988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
756db2a4165SFrank Mandarino 	}
757db2a4165SFrank Mandarino 
75887506549SMark Brown 	if (card->suspend_post)
75970b2ac12SMark Brown 		card->suspend_post(card);
760db2a4165SFrank Mandarino 
761db2a4165SFrank Mandarino 	return 0;
762db2a4165SFrank Mandarino }
7636f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
764db2a4165SFrank Mandarino 
7656ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7666ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7676ed25978SAndy Green  */
7686ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
769db2a4165SFrank Mandarino {
770f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
771f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7721a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
773d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
7741a497983SMengdong Lin 	int i;
775db2a4165SFrank Mandarino 
7766ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7776ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7786ed25978SAndy Green 	 */
7796ed25978SAndy Green 
780f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7816ed25978SAndy Green 
7829949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
783f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7849949788bSMark Brown 
78587506549SMark Brown 	if (card->resume_pre)
78670b2ac12SMark Brown 		card->resume_pre(card);
787db2a4165SFrank Mandarino 
788bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
7891a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
7901a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7913efab7dcSMark Brown 
7921a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7933efab7dcSMark Brown 			continue;
7943efab7dcSMark Brown 
795bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
796f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
797db2a4165SFrank Mandarino 	}
798db2a4165SFrank Mandarino 
799d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
8009178feb4SKuninori Morimoto 		if (component->suspended) {
8019178feb4SKuninori Morimoto 			if (component->resume)
8029178feb4SKuninori Morimoto 				component->resume(component);
8039178feb4SKuninori Morimoto 			component->suspended = 0;
8041547aba9SMark Brown 		}
805f0fba2adSLiam Girdwood 	}
806db2a4165SFrank Mandarino 
8071a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8083efab7dcSMark Brown 
8091a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8103efab7dcSMark Brown 			continue;
8113efab7dcSMark Brown 
8121a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
813d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
814db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
815f0fba2adSLiam Girdwood 
8161a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
817d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
818db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
819db2a4165SFrank Mandarino 	}
820db2a4165SFrank Mandarino 
8213ff3f64bSMark Brown 	/* unmute any active DACs */
8221a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8233efab7dcSMark Brown 
8241a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8253efab7dcSMark Brown 			continue;
8263efab7dcSMark Brown 
8271a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
8281a497983SMengdong Lin 			struct snd_soc_dai *dai = rtd->codec_dais[i];
82988bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
83088bd870fSBenoit Cousson 
831f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
832f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
833db2a4165SFrank Mandarino 		}
83488bd870fSBenoit Cousson 	}
835db2a4165SFrank Mandarino 
8361a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8371a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8383efab7dcSMark Brown 
8391a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
8403efab7dcSMark Brown 			continue;
8413efab7dcSMark Brown 
842bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
843f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
844db2a4165SFrank Mandarino 	}
845db2a4165SFrank Mandarino 
84687506549SMark Brown 	if (card->resume_post)
84770b2ac12SMark Brown 		card->resume_post(card);
848db2a4165SFrank Mandarino 
849f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8506ed25978SAndy Green 
8518be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
8528be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
853e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8541a7aaa58SJeeja KP 
8551a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
8561a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
8576ed25978SAndy Green }
8586ed25978SAndy Green 
8596ed25978SAndy Green /* powers up audio subsystem after a suspend */
8606f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8616ed25978SAndy Green {
8626f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
863bc263214SLars-Peter Clausen 	bool bus_control = false;
8641a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
865b9dd94a8SPeter Ujfalusi 
866c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
867c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8685ff1ddf2SEric Miao 		return 0;
8695ff1ddf2SEric Miao 
870988e8cc4SNicolin Chen 	/* activate pins from sleep state */
8711a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
87288bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
87388bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87488bd870fSBenoit Cousson 		int j;
87588bd870fSBenoit Cousson 
876988e8cc4SNicolin Chen 		if (cpu_dai->active)
877988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
87888bd870fSBenoit Cousson 
87988bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
88088bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
881988e8cc4SNicolin Chen 			if (codec_dai->active)
882988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
883988e8cc4SNicolin Chen 		}
88488bd870fSBenoit Cousson 	}
885988e8cc4SNicolin Chen 
886bc263214SLars-Peter Clausen 	/*
887bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
888bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
889bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
89064ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
89164ab9baaSMark Brown 	 */
8921a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
8931a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
894bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
89582e14e8bSStephen Warren 	}
896bc263214SLars-Peter Clausen 	if (bus_control) {
897bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
89864ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
89964ab9baaSMark Brown 	} else {
900f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
9016308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
902f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
903f0fba2adSLiam Girdwood 	}
9046ed25978SAndy Green 
905db2a4165SFrank Mandarino 	return 0;
906db2a4165SFrank Mandarino }
9076f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
908db2a4165SFrank Mandarino #else
9096f8ab4acSMark Brown #define snd_soc_suspend NULL
9106f8ab4acSMark Brown #define snd_soc_resume NULL
911db2a4165SFrank Mandarino #endif
912db2a4165SFrank Mandarino 
91385e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
91402a06d30SBarry Song };
91502a06d30SBarry Song 
91665d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
91765d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
91812023a9aSMisael Lopez Cruz {
91965d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
92012023a9aSMisael Lopez Cruz 
92134e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
92234e81ab4SLars-Peter Clausen 
92365d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
92465d9361fSLars-Peter Clausen 		if (of_node) {
92565d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
92665d9361fSLars-Peter Clausen 				return component;
92765d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
92865d9361fSLars-Peter Clausen 			return component;
92912023a9aSMisael Lopez Cruz 		}
93012023a9aSMisael Lopez Cruz 	}
93112023a9aSMisael Lopez Cruz 
93212023a9aSMisael Lopez Cruz 	return NULL;
93312023a9aSMisael Lopez Cruz }
93412023a9aSMisael Lopez Cruz 
935fbb88b5cSMengdong Lin /**
936fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
937fbb88b5cSMengdong Lin  *
938fbb88b5cSMengdong Lin  * @dlc: name of the DAI and optional component info to match
939fbb88b5cSMengdong Lin  *
940ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
941fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
942fbb88b5cSMengdong Lin  * should also match if being specified.
943fbb88b5cSMengdong Lin  *
944fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
945fbb88b5cSMengdong Lin  */
946305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
94714621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
94812023a9aSMisael Lopez Cruz {
94914621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
95014621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
9513e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
95212023a9aSMisael Lopez Cruz 
95334e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
95434e81ab4SLars-Peter Clausen 
95514621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
95614621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
9573e0aa8d8SJyri Sarha 		component_of_node = component->dev->of_node;
9583e0aa8d8SJyri Sarha 		if (!component_of_node && component->dev->parent)
9593e0aa8d8SJyri Sarha 			component_of_node = component->dev->parent->of_node;
9603e0aa8d8SJyri Sarha 
9613e0aa8d8SJyri Sarha 		if (dlc->of_node && component_of_node != dlc->of_node)
96212023a9aSMisael Lopez Cruz 			continue;
9631ffae361SLars-Peter Clausen 		if (dlc->name && strcmp(component->name, dlc->name))
96412023a9aSMisael Lopez Cruz 			continue;
96514621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
96614621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
96714621c7eSLars-Peter Clausen 				continue;
96812023a9aSMisael Lopez Cruz 
96914621c7eSLars-Peter Clausen 			return dai;
97012023a9aSMisael Lopez Cruz 		}
97112023a9aSMisael Lopez Cruz 	}
97212023a9aSMisael Lopez Cruz 
97312023a9aSMisael Lopez Cruz 	return NULL;
97412023a9aSMisael Lopez Cruz }
975305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
97612023a9aSMisael Lopez Cruz 
97717fb1755SMengdong Lin 
97817fb1755SMengdong Lin /**
97917fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
98017fb1755SMengdong Lin  *
98117fb1755SMengdong Lin  * @card: soc card
98217fb1755SMengdong Lin  * @id: DAI link ID to match
98317fb1755SMengdong Lin  * @name: DAI link name to match, optional
9848abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
98517fb1755SMengdong Lin  *
98617fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
98717fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
98817fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
98917fb1755SMengdong Lin  * specified.
99017fb1755SMengdong Lin  *
99117fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
99217fb1755SMengdong Lin  */
99317fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
99417fb1755SMengdong Lin 					       int id, const char *name,
99517fb1755SMengdong Lin 					       const char *stream_name)
99617fb1755SMengdong Lin {
99717fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
99817fb1755SMengdong Lin 
99917fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
100017fb1755SMengdong Lin 
100117fb1755SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
100217fb1755SMengdong Lin 		if (link->id != id)
100317fb1755SMengdong Lin 			continue;
100417fb1755SMengdong Lin 
100517fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
100617fb1755SMengdong Lin 			continue;
100717fb1755SMengdong Lin 
100817fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
100917fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
101017fb1755SMengdong Lin 			continue;
101117fb1755SMengdong Lin 
101217fb1755SMengdong Lin 		return link;
101317fb1755SMengdong Lin 	}
101417fb1755SMengdong Lin 
101517fb1755SMengdong Lin 	return NULL;
101617fb1755SMengdong Lin }
101717fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
101817fb1755SMengdong Lin 
101949a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
102049a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
1021db2a4165SFrank Mandarino {
102249a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
102349a5ba1cSMengdong Lin 
102449a5ba1cSMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
102549a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
102649a5ba1cSMengdong Lin 			return true;
102749a5ba1cSMengdong Lin 	}
102849a5ba1cSMengdong Lin 
102949a5ba1cSMengdong Lin 	return false;
103049a5ba1cSMengdong Lin }
103149a5ba1cSMengdong Lin 
10326f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10336f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1034db2a4165SFrank Mandarino {
10351a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
103688bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
103714621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
10381a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
1039435c5e25SMark Brown 	struct snd_soc_platform *platform;
104006859fcaSCharles Keepax 	struct device_node *platform_of_node;
1041848dd8beSMark Brown 	const char *platform_name;
104288bd870fSBenoit Cousson 	int i;
1043db2a4165SFrank Mandarino 
10446f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10456308419aSMark Brown 
104649a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
104749a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
104849a5ba1cSMengdong Lin 			dai_link->name);
104949a5ba1cSMengdong Lin 		return 0;
105049a5ba1cSMengdong Lin 	}
1051db2a4165SFrank Mandarino 
1052513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1053513cb311SSudip Mukherjee 	if (!rtd)
1054513cb311SSudip Mukherjee 		return -ENOMEM;
1055513cb311SSudip Mukherjee 
105614621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
105714621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
105814621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
105914621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1060b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
1061f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1062f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
10631a497983SMengdong Lin 		goto _err_defer;
1064c5af3a2eSMark Brown 	}
1065c5af3a2eSMark Brown 
106688bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
106788bd870fSBenoit Cousson 
106888bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
10691a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
107088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
107114621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
107288bd870fSBenoit Cousson 		if (!codec_dais[i]) {
107312023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
107488bd870fSBenoit Cousson 				codecs[i].dai_name);
10751a497983SMengdong Lin 			goto _err_defer;
107612023a9aSMisael Lopez Cruz 		}
107788bd870fSBenoit Cousson 	}
107888bd870fSBenoit Cousson 
107988bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
108088bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
108188bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
108212023a9aSMisael Lopez Cruz 
1083848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1084848dd8beSMark Brown 	platform_name = dai_link->platform_name;
10855a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
1086848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1087848dd8beSMark Brown 
1088b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
1089f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
109006859fcaSCharles Keepax 		platform_of_node = platform->dev->of_node;
109106859fcaSCharles Keepax 		if (!platform_of_node && platform->dev->parent->of_node)
109206859fcaSCharles Keepax 			platform_of_node = platform->dev->parent->of_node;
109306859fcaSCharles Keepax 
10945a504963SStephen Warren 		if (dai_link->platform_of_node) {
109506859fcaSCharles Keepax 			if (platform_of_node != dai_link->platform_of_node)
10965a504963SStephen Warren 				continue;
10975a504963SStephen Warren 		} else {
1098f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
10992610ab77SStephen Warren 				continue;
11005a504963SStephen Warren 		}
11012610ab77SStephen Warren 
1102f0fba2adSLiam Girdwood 		rtd->platform = platform;
1103f0fba2adSLiam Girdwood 	}
1104b19e6e7bSMark Brown 	if (!rtd->platform) {
1105f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1106f0fba2adSLiam Girdwood 			dai_link->platform_name);
110770fcad49SCharles Keepax 		goto _err_defer;
1108f0fba2adSLiam Girdwood 	}
1109b19e6e7bSMark Brown 
11101a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
1111b19e6e7bSMark Brown 	return 0;
11121a497983SMengdong Lin 
11131a497983SMengdong Lin _err_defer:
11141a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11151a497983SMengdong Lin 	return  -EPROBE_DEFER;
11166b05eda6SMark Brown }
11176b05eda6SMark Brown 
1118f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1119d12cd198SStephen Warren {
1120abd31b32SLars-Peter Clausen 	if (!component->card)
112170090bbbSLars-Peter Clausen 		return;
1122d12cd198SStephen Warren 
1123d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
1124d12cd198SStephen Warren 
1125f1d45cc3SLars-Peter Clausen 	if (component->remove)
1126f1d45cc3SLars-Peter Clausen 		component->remove(component);
1127d12cd198SStephen Warren 
1128f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1129d12cd198SStephen Warren 
1130f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1131abd31b32SLars-Peter Clausen 	component->card = NULL;
1132f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1133d12cd198SStephen Warren }
1134d12cd198SStephen Warren 
1135e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1136589c3563SJarkko Nikula {
1137589c3563SJarkko Nikula 	int err;
1138589c3563SJarkko Nikula 
1139e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
1140e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
1141e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
1142e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
1143589c3563SJarkko Nikula 			if (err < 0)
1144e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
1145b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1146e60cd14fSLars-Peter Clausen 					dai->name, err);
1147b0aa88afSMisael Lopez Cruz 		}
1148e60cd14fSLars-Peter Clausen 		dai->probed = 0;
1149b0aa88afSMisael Lopez Cruz 	}
1150b0aa88afSMisael Lopez Cruz }
1151b0aa88afSMisael Lopez Cruz 
11521a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11531a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1154f0fba2adSLiam Girdwood {
1155e60cd14fSLars-Peter Clausen 	int i;
1156f0fba2adSLiam Girdwood 
1157f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1158f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
115936ae1a96SMark Brown 		device_unregister(rtd->dev);
1160f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
116102a06d30SBarry Song 	}
116202a06d30SBarry Song 
1163f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
116488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1165e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1166f0fba2adSLiam Girdwood 
1167e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1168f0fba2adSLiam Girdwood }
1169f0fba2adSLiam Girdwood 
11701a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11711a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
117262ae68faSStephen Warren {
117362ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
117462ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
117561aca564SLars-Peter Clausen 	struct snd_soc_component *component;
117688bd870fSBenoit Cousson 	int i;
117762ae68faSStephen Warren 
117862ae68faSStephen Warren 	/* remove the platform */
117970090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1180f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
118162ae68faSStephen Warren 
118262ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
118388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
118461aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
118570090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
118661aca564SLars-Peter Clausen 			soc_remove_component(component);
118762ae68faSStephen Warren 	}
118862ae68faSStephen Warren 
118962ae68faSStephen Warren 	/* remove any CPU-side CODEC */
119062ae68faSStephen Warren 	if (cpu_dai) {
119170090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
119261aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
119362ae68faSStephen Warren 	}
119462ae68faSStephen Warren }
119562ae68faSStephen Warren 
11960671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11970671fd8eSKuninori Morimoto {
11981a497983SMengdong Lin 	int order;
11991a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1200f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
12010671fd8eSKuninori Morimoto 
12020168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
12030168bf0dSLiam Girdwood 			order++) {
12041a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12051a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
12060168bf0dSLiam Girdwood 	}
120762ae68faSStephen Warren 
120862ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
120962ae68faSStephen Warren 			order++) {
12101a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list)
12111a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
121262ae68faSStephen Warren 	}
121362ae68faSStephen Warren 
1214f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1215f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1216f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1217f8f80361SMengdong Lin 				link->name);
1218f8f80361SMengdong Lin 
1219f8f80361SMengdong Lin 		list_del(&link->list);
1220f8f80361SMengdong Lin 		card->num_dai_links--;
12210671fd8eSKuninori Morimoto 	}
12220671fd8eSKuninori Morimoto }
12230671fd8eSKuninori Morimoto 
1224923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1225923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1226923c5e61SMengdong Lin {
1227923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1228923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1229923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1230923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1231923c5e61SMengdong Lin 
1232923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1233923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1234923c5e61SMengdong Lin 				GFP_KERNEL);
1235923c5e61SMengdong Lin 		if (!dai_link->codecs)
1236923c5e61SMengdong Lin 			return -ENOMEM;
1237923c5e61SMengdong Lin 
1238923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1239923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1240923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1241923c5e61SMengdong Lin 	}
1242923c5e61SMengdong Lin 
1243923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1244923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1245923c5e61SMengdong Lin 		return -EINVAL;
1246923c5e61SMengdong Lin 	}
1247923c5e61SMengdong Lin 
1248923c5e61SMengdong Lin 	return 0;
1249923c5e61SMengdong Lin }
1250923c5e61SMengdong Lin 
1251923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1252923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1253923c5e61SMengdong Lin {
1254923c5e61SMengdong Lin 	int i, ret;
1255923c5e61SMengdong Lin 
1256923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1257923c5e61SMengdong Lin 	if (ret) {
1258923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1259923c5e61SMengdong Lin 		return ret;
1260923c5e61SMengdong Lin 	}
1261923c5e61SMengdong Lin 
1262923c5e61SMengdong Lin 	for (i = 0; i < link->num_codecs; i++) {
1263923c5e61SMengdong Lin 		/*
1264923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1265923c5e61SMengdong Lin 		 * not both or neither.
1266923c5e61SMengdong Lin 		 */
1267923c5e61SMengdong Lin 		if (!!link->codecs[i].name ==
1268923c5e61SMengdong Lin 		    !!link->codecs[i].of_node) {
1269923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1270923c5e61SMengdong Lin 				link->name);
1271923c5e61SMengdong Lin 			return -EINVAL;
1272923c5e61SMengdong Lin 		}
1273923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
1274923c5e61SMengdong Lin 		if (!link->codecs[i].dai_name) {
1275923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1276923c5e61SMengdong Lin 				link->name);
1277923c5e61SMengdong Lin 			return -EINVAL;
1278923c5e61SMengdong Lin 		}
1279923c5e61SMengdong Lin 	}
1280923c5e61SMengdong Lin 
1281923c5e61SMengdong Lin 	/*
1282923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1283923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1284923c5e61SMengdong Lin 	 */
1285923c5e61SMengdong Lin 	if (link->platform_name && link->platform_of_node) {
1286923c5e61SMengdong Lin 		dev_err(card->dev,
1287923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1288923c5e61SMengdong Lin 			link->name);
1289923c5e61SMengdong Lin 		return -EINVAL;
1290923c5e61SMengdong Lin 	}
1291923c5e61SMengdong Lin 
1292923c5e61SMengdong Lin 	/*
1293923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1294923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1295923c5e61SMengdong Lin 	 * name alone..
1296923c5e61SMengdong Lin 	 */
1297923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1298923c5e61SMengdong Lin 		dev_err(card->dev,
1299923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1300923c5e61SMengdong Lin 			link->name);
1301923c5e61SMengdong Lin 		return -EINVAL;
1302923c5e61SMengdong Lin 	}
1303923c5e61SMengdong Lin 	/*
1304923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1305923c5e61SMengdong Lin 	 * specified
1306923c5e61SMengdong Lin 	 */
1307923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1308923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1309923c5e61SMengdong Lin 		dev_err(card->dev,
1310923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1311923c5e61SMengdong Lin 			link->name);
1312923c5e61SMengdong Lin 		return -EINVAL;
1313923c5e61SMengdong Lin 	}
1314923c5e61SMengdong Lin 
1315923c5e61SMengdong Lin 	return 0;
1316923c5e61SMengdong Lin }
1317923c5e61SMengdong Lin 
1318f8f80361SMengdong Lin /**
1319f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1320f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1321f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1322f8f80361SMengdong Lin  *
1323f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1324f8f80361SMengdong Lin  *
1325f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1326f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1327f8f80361SMengdong Lin  * DAI links in dai_link array.
1328f8f80361SMengdong Lin  */
1329f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1330f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1331f8f80361SMengdong Lin {
1332f8f80361SMengdong Lin 	if (dai_link->dobj.type
1333f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1334f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1335f8f80361SMengdong Lin 			dai_link->dobj.type);
1336f8f80361SMengdong Lin 		return -EINVAL;
1337f8f80361SMengdong Lin 	}
1338f8f80361SMengdong Lin 
1339f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1340d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1341d6f220eaSMengdong Lin 	 * on the link created by topology.
1342d6f220eaSMengdong Lin 	 */
1343d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1344d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1345d6f220eaSMengdong Lin 
1346f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1347f8f80361SMengdong Lin 	card->num_dai_links++;
1348f8f80361SMengdong Lin 
1349f8f80361SMengdong Lin 	return 0;
1350f8f80361SMengdong Lin }
1351f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1352f8f80361SMengdong Lin 
1353f8f80361SMengdong Lin /**
1354f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1355f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1356f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1357f8f80361SMengdong Lin  *
1358f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1359f8f80361SMengdong Lin  *
1360f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1361f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1362f8f80361SMengdong Lin  */
1363f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1364f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1365f8f80361SMengdong Lin {
1366f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1367f8f80361SMengdong Lin 
1368f8f80361SMengdong Lin 	if (dai_link->dobj.type
1369f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1370f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1371f8f80361SMengdong Lin 			dai_link->dobj.type);
1372f8f80361SMengdong Lin 		return;
1373f8f80361SMengdong Lin 	}
1374f8f80361SMengdong Lin 
1375f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1376d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1377d6f220eaSMengdong Lin 	 * on the link created by topology.
1378d6f220eaSMengdong Lin 	 */
1379d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1380d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1381d6f220eaSMengdong Lin 
1382f8f80361SMengdong Lin 	list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1383f8f80361SMengdong Lin 		if (link == dai_link) {
1384f8f80361SMengdong Lin 			list_del(&link->list);
1385f8f80361SMengdong Lin 			card->num_dai_links--;
1386f8f80361SMengdong Lin 			return;
1387f8f80361SMengdong Lin 		}
1388f8f80361SMengdong Lin 	}
1389f8f80361SMengdong Lin }
1390f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1391f0fba2adSLiam Girdwood 
1392ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
139394f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1394ead9b919SJarkko Nikula {
1395ead9b919SJarkko Nikula 	int i;
1396ead9b919SJarkko Nikula 
1397ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1398ead9b919SJarkko Nikula 		return;
1399ead9b919SJarkko Nikula 
1400ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1401ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
140294f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
14033ca041edSSebastian Reichel 			continue;
140494f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
14053ca041edSSebastian Reichel 			continue;
140694f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1407ead9b919SJarkko Nikula 		break;
1408ead9b919SJarkko Nikula 	}
1409ead9b919SJarkko Nikula }
1410ead9b919SJarkko Nikula 
1411f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1412f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1413589c3563SJarkko Nikula {
1414f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1415888df395SMark Brown 	struct snd_soc_dai *dai;
1416f1d45cc3SLars-Peter Clausen 	int ret;
1417589c3563SJarkko Nikula 
14181b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
141970090bbbSLars-Peter Clausen 		return 0;
1420589c3563SJarkko Nikula 
1421abd31b32SLars-Peter Clausen 	if (component->card) {
14221b7c1231SLars-Peter Clausen 		if (component->card != card) {
14231b7c1231SLars-Peter Clausen 			dev_err(component->dev,
14241b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
14251b7c1231SLars-Peter Clausen 				card->name, component->card->name);
14261b7c1231SLars-Peter Clausen 			return -ENODEV;
14271b7c1231SLars-Peter Clausen 		}
14281b7c1231SLars-Peter Clausen 		return 0;
14291b7c1231SLars-Peter Clausen 	}
14301b7c1231SLars-Peter Clausen 
1431abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1432abd31b32SLars-Peter Clausen 		return -ENODEV;
1433abd31b32SLars-Peter Clausen 
1434f1d45cc3SLars-Peter Clausen 	component->card = card;
1435f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1436f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1437589c3563SJarkko Nikula 
1438f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1439d5d1e0beSLars-Peter Clausen 
1440f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1441f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1442f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
144377530150SLars-Peter Clausen 
1444b318ad50SNariman Poushin 		if (ret != 0) {
1445f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1446b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1447b318ad50SNariman Poushin 			goto err_probe;
1448b318ad50SNariman Poushin 		}
1449b318ad50SNariman Poushin 	}
1450b318ad50SNariman Poushin 
14510634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
14520634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1453261edc70SNariman Poushin 		if (ret != 0) {
1454f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1455261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1456261edc70SNariman Poushin 			goto err_probe;
1457261edc70SNariman Poushin 		}
1458261edc70SNariman Poushin 	}
1459888df395SMark Brown 
1460f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1461f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1462589c3563SJarkko Nikula 		if (ret < 0) {
1463f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1464f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
146570d29331SJarkko Nikula 			goto err_probe;
1466589c3563SJarkko Nikula 		}
1467cb2cf612SLiam Girdwood 
1468f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1469f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1470956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1471f1d45cc3SLars-Peter Clausen 			component->name);
1472956245e9SLiam Girdwood 	}
1473956245e9SLiam Girdwood 
1474f2ed6b07SMengdong Lin 	/* machine specific init */
1475f2ed6b07SMengdong Lin 	if (component->init) {
1476f2ed6b07SMengdong Lin 		ret = component->init(component);
1477f2ed6b07SMengdong Lin 		if (ret < 0) {
1478f2ed6b07SMengdong Lin 			dev_err(component->dev,
1479f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1480f2ed6b07SMengdong Lin 			goto err_probe;
1481f2ed6b07SMengdong Lin 		}
1482f2ed6b07SMengdong Lin 	}
1483f2ed6b07SMengdong Lin 
1484f1d45cc3SLars-Peter Clausen 	if (component->controls)
1485f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1486f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1487f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1488f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1489f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1490956245e9SLiam Girdwood 
1491f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1492d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1493956245e9SLiam Girdwood 
1494956245e9SLiam Girdwood 	return 0;
1495956245e9SLiam Girdwood 
1496956245e9SLiam Girdwood err_probe:
1497f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1498abd31b32SLars-Peter Clausen 	component->card = NULL;
1499f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1500956245e9SLiam Girdwood 
1501956245e9SLiam Girdwood 	return ret;
1502956245e9SLiam Girdwood }
1503956245e9SLiam Girdwood 
150436ae1a96SMark Brown static void rtd_release(struct device *dev)
150536ae1a96SMark Brown {
150636ae1a96SMark Brown 	kfree(dev);
150736ae1a96SMark Brown }
1508f0fba2adSLiam Girdwood 
15095f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
15105f3484acSLars-Peter Clausen 	const char *name)
1511503ae5e0SMisael Lopez Cruz {
1512589c3563SJarkko Nikula 	int ret = 0;
1513589c3563SJarkko Nikula 
1514589c3563SJarkko Nikula 	/* register the rtd device */
151536ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
151636ae1a96SMark Brown 	if (!rtd->dev)
151736ae1a96SMark Brown 		return -ENOMEM;
151836ae1a96SMark Brown 	device_initialize(rtd->dev);
15195f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
152036ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1521d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1522f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
152336ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1524b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
152501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
152601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
152701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
152801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
152936ae1a96SMark Brown 	ret = device_add(rtd->dev);
1530589c3563SJarkko Nikula 	if (ret < 0) {
1531865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1532865df9cbSChuansheng Liu 		put_device(rtd->dev);
15335f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1534f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1535589c3563SJarkko Nikula 		return ret;
1536589c3563SJarkko Nikula 	}
1537589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1538589c3563SJarkko Nikula 	return 0;
1539589c3563SJarkko Nikula }
1540589c3563SJarkko Nikula 
15411a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
15421a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
154362ae68faSStephen Warren 				     int order)
154462ae68faSStephen Warren {
154562ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1546f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
154788bd870fSBenoit Cousson 	int i, ret;
154862ae68faSStephen Warren 
154962ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
155061aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
155170090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1552f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
155362ae68faSStephen Warren 		if (ret < 0)
155462ae68faSStephen Warren 			return ret;
155562ae68faSStephen Warren 	}
155662ae68faSStephen Warren 
155788bd870fSBenoit Cousson 	/* probe the CODEC-side components */
155888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
155961aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
156070090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1561f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
156262ae68faSStephen Warren 			if (ret < 0)
156362ae68faSStephen Warren 				return ret;
156462ae68faSStephen Warren 		}
156588bd870fSBenoit Cousson 	}
156662ae68faSStephen Warren 
156762ae68faSStephen Warren 	/* probe the platform */
156870090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1569f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
157062ae68faSStephen Warren 		if (ret < 0)
157162ae68faSStephen Warren 			return ret;
157262ae68faSStephen Warren 	}
157362ae68faSStephen Warren 
157462ae68faSStephen Warren 	return 0;
157562ae68faSStephen Warren }
157662ae68faSStephen Warren 
15778e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1578b0aa88afSMisael Lopez Cruz {
1579b0aa88afSMisael Lopez Cruz 	int ret;
1580b0aa88afSMisael Lopez Cruz 
15818e2be562SLars-Peter Clausen 	if (!dai->probed && dai->driver->probe_order == order) {
15828e2be562SLars-Peter Clausen 		if (dai->driver->probe) {
15838e2be562SLars-Peter Clausen 			ret = dai->driver->probe(dai);
1584b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
15858e2be562SLars-Peter Clausen 				dev_err(dai->dev,
15868e2be562SLars-Peter Clausen 					"ASoC: failed to probe DAI %s: %d\n",
15878e2be562SLars-Peter Clausen 					dai->name, ret);
1588b0aa88afSMisael Lopez Cruz 				return ret;
1589b0aa88afSMisael Lopez Cruz 			}
1590b0aa88afSMisael Lopez Cruz 		}
1591b0aa88afSMisael Lopez Cruz 
15928e2be562SLars-Peter Clausen 		dai->probed = 1;
1593b0aa88afSMisael Lopez Cruz 	}
1594b0aa88afSMisael Lopez Cruz 
1595b0aa88afSMisael Lopez Cruz 	return 0;
1596b0aa88afSMisael Lopez Cruz }
1597b0aa88afSMisael Lopez Cruz 
159825f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
159925f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
160025f7b701SArnaud Pouliquen {
160125f7b701SArnaud Pouliquen 	int i, ret = 0;
160225f7b701SArnaud Pouliquen 
160325f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
160425f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
160525f7b701SArnaud Pouliquen 
160625f7b701SArnaud Pouliquen 		if (!rtd->dai_link->no_pcm && drv->pcm_new)
160725f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
160825f7b701SArnaud Pouliquen 		if (ret < 0) {
160925f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
161025f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
161125f7b701SArnaud Pouliquen 				dais[i]->name);
161225f7b701SArnaud Pouliquen 			return ret;
161325f7b701SArnaud Pouliquen 		}
161425f7b701SArnaud Pouliquen 	}
161525f7b701SArnaud Pouliquen 
161625f7b701SArnaud Pouliquen 	return 0;
161725f7b701SArnaud Pouliquen }
161825f7b701SArnaud Pouliquen 
16192436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
16202436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
16213f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
16222436a723SMisael Lopez Cruz {
16233f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16243f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
16253de7c420SVinod Koul 	struct snd_soc_dapm_widget *sink, *source;
16262436a723SMisael Lopez Cruz 	int ret;
16272436a723SMisael Lopez Cruz 
162888bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
162988bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
163088bd870fSBenoit Cousson 
16312436a723SMisael Lopez Cruz 	/* link the DAI widgets */
16323de7c420SVinod Koul 	sink = codec_dai->playback_widget;
16333de7c420SVinod Koul 	source = cpu_dai->capture_widget;
16343de7c420SVinod Koul 	if (sink && source) {
16352436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16363de7c420SVinod Koul 					   dai_link->num_params,
16373de7c420SVinod Koul 					   source, sink);
16382436a723SMisael Lopez Cruz 		if (ret != 0) {
16392436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16403de7c420SVinod Koul 				sink->name, source->name, ret);
16412436a723SMisael Lopez Cruz 			return ret;
16422436a723SMisael Lopez Cruz 		}
16432436a723SMisael Lopez Cruz 	}
16442436a723SMisael Lopez Cruz 
16453de7c420SVinod Koul 	sink = cpu_dai->playback_widget;
16463de7c420SVinod Koul 	source = codec_dai->capture_widget;
16473de7c420SVinod Koul 	if (sink && source) {
16482436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
16493de7c420SVinod Koul 					   dai_link->num_params,
16503de7c420SVinod Koul 					   source, sink);
16512436a723SMisael Lopez Cruz 		if (ret != 0) {
16522436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
16533de7c420SVinod Koul 				sink->name, source->name, ret);
16542436a723SMisael Lopez Cruz 			return ret;
16552436a723SMisael Lopez Cruz 		}
16562436a723SMisael Lopez Cruz 	}
16572436a723SMisael Lopez Cruz 
16582436a723SMisael Lopez Cruz 	return 0;
16592436a723SMisael Lopez Cruz }
16602436a723SMisael Lopez Cruz 
16611a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
16621a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1663f0fba2adSLiam Girdwood {
16641a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1665c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
166688bd870fSBenoit Cousson 	int i, ret;
1667f0fba2adSLiam Girdwood 
1668f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
16691a497983SMengdong Lin 			card->name, rtd->num, order);
1670f0fba2adSLiam Girdwood 
1671f0fba2adSLiam Girdwood 	/* set default power off timeout */
1672f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1673f0fba2adSLiam Girdwood 
16748e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
16758e2be562SLars-Peter Clausen 	if (ret)
1676f0fba2adSLiam Girdwood 		return ret;
1677f0fba2adSLiam Girdwood 
1678f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
167988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
16808e2be562SLars-Peter Clausen 		ret = soc_probe_dai(rtd->codec_dais[i], order);
1681b0aa88afSMisael Lopez Cruz 		if (ret)
1682f0fba2adSLiam Girdwood 			return ret;
168388bd870fSBenoit Cousson 	}
1684f0fba2adSLiam Girdwood 
16850168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
16860168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
16870168bf0dSLiam Girdwood 		return 0;
16880168bf0dSLiam Girdwood 
16895f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16905f3484acSLars-Peter Clausen 	if (dai_link->init) {
16915f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
16925f3484acSLars-Peter Clausen 		if (ret < 0) {
16935f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16945f3484acSLars-Peter Clausen 				dai_link->name, ret);
16955f3484acSLars-Peter Clausen 			return ret;
16965f3484acSLars-Peter Clausen 		}
16975f3484acSLars-Peter Clausen 	}
16985f3484acSLars-Peter Clausen 
1699a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1700a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1701a5053a8eSKuninori Morimoto 
17025f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1703589c3563SJarkko Nikula 	if (ret)
1704f0fba2adSLiam Girdwood 		return ret;
1705f0fba2adSLiam Girdwood 
17065f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
17075f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
17082e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
17092e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
17105f3484acSLars-Peter Clausen #endif
17115f3484acSLars-Peter Clausen 
17126f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
17131245b700SNamarta Kohli 		/*create compress_device"*/
17141a497983SMengdong Lin 		ret = cpu_dai->driver->compress_new(rtd, rtd->num);
17151245b700SNamarta Kohli 		if (ret < 0) {
1716f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
17171245b700SNamarta Kohli 					 dai_link->stream_name);
17181245b700SNamarta Kohli 			return ret;
17191245b700SNamarta Kohli 		}
17201245b700SNamarta Kohli 	} else {
17211245b700SNamarta Kohli 
1722c74184edSMark Brown 		if (!dai_link->params) {
1723f0fba2adSLiam Girdwood 			/* create the pcm */
17241a497983SMengdong Lin 			ret = soc_new_pcm(rtd, rtd->num);
1725f0fba2adSLiam Girdwood 			if (ret < 0) {
1726f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
17270837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1728f0fba2adSLiam Girdwood 				return ret;
1729f0fba2adSLiam Girdwood 			}
173025f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
173125f7b701SArnaud Pouliquen 			if (ret < 0)
173225f7b701SArnaud Pouliquen 				return ret;
173325f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(rtd->codec_dais,
173425f7b701SArnaud Pouliquen 						   rtd->num_codecs, rtd);
173525f7b701SArnaud Pouliquen 			if (ret < 0)
173625f7b701SArnaud Pouliquen 				return ret;
1737c74184edSMark Brown 		} else {
17389d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
17399d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
17409d58a077SRichard Fitzgerald 
1741c74184edSMark Brown 			/* link the DAI widgets */
17423f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
17432436a723SMisael Lopez Cruz 			if (ret)
1744c74184edSMark Brown 				return ret;
1745c74184edSMark Brown 		}
1746c74184edSMark Brown 	}
1747c74184edSMark Brown 
1748f0fba2adSLiam Girdwood 	return 0;
1749f0fba2adSLiam Girdwood }
1750f0fba2adSLiam Girdwood 
175144c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1752b19e6e7bSMark Brown {
17533ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1754f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1755f2ed6b07SMengdong Lin 	const char *name;
1756f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
17573ca041edSSebastian Reichel 
1758f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1759f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1760f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1761f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1762f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1763f2ed6b07SMengdong Lin 		if (!component) {
1764f2ed6b07SMengdong Lin 			if (codec_of_node)
1765f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1766f2ed6b07SMengdong Lin 			goto err_defer;
1767f2ed6b07SMengdong Lin 		}
1768f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1769f2ed6b07SMengdong Lin 		/* generic components */
1770f2ed6b07SMengdong Lin 		name = aux_dev->name;
1771f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1772f2ed6b07SMengdong Lin 		if (!component)
1773f2ed6b07SMengdong Lin 			goto err_defer;
1774f2ed6b07SMengdong Lin 	} else {
1775f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1776f2ed6b07SMengdong Lin 		return -EINVAL;
1777f2ed6b07SMengdong Lin 	}
17783ca041edSSebastian Reichel 
1779f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1780d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
17811a653aa4SKuninori Morimoto 
1782f2ed6b07SMengdong Lin 	return 0;
1783f2ed6b07SMengdong Lin 
1784f2ed6b07SMengdong Lin err_defer:
178565d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1786b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1787b19e6e7bSMark Brown }
1788b19e6e7bSMark Brown 
1789f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1790f2ed6b07SMengdong Lin {
1791991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1792f2ed6b07SMengdong Lin 	int order;
1793f2ed6b07SMengdong Lin 	int ret;
1794f2ed6b07SMengdong Lin 
1795f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1796f2ed6b07SMengdong Lin 		order++) {
1797991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1798f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1799f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1800f2ed6b07SMengdong Lin 				if (ret < 0) {
1801f2ed6b07SMengdong Lin 					dev_err(card->dev,
1802f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1803f2ed6b07SMengdong Lin 						comp->name, ret);
1804f2ed6b07SMengdong Lin 					return ret;
1805f2ed6b07SMengdong Lin 				}
1806f2ed6b07SMengdong Lin 			}
1807f2ed6b07SMengdong Lin 		}
1808f2ed6b07SMengdong Lin 	}
180965d9361fSLars-Peter Clausen 
181044c69bb1SLars-Peter Clausen 	return 0;
18113ca041edSSebastian Reichel }
18122eea392dSJarkko Nikula 
1813f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
181444c69bb1SLars-Peter Clausen {
1815f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1816f2ed6b07SMengdong Lin 	int order;
181744c69bb1SLars-Peter Clausen 
1818f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1819f2ed6b07SMengdong Lin 		order++) {
1820f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1821991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
18221a653aa4SKuninori Morimoto 
1823f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1824f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1825991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1826991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
18272eea392dSJarkko Nikula 			}
18285f3484acSLars-Peter Clausen 		}
18292eea392dSJarkko Nikula 	}
18302eea392dSJarkko Nikula }
18312eea392dSJarkko Nikula 
1832f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1833fdf0f54dSDimitris Papastamos {
1834fdf0f54dSDimitris Papastamos 	int ret;
1835fdf0f54dSDimitris Papastamos 
1836fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1837fdf0f54dSDimitris Papastamos 		return 0;
1838fdf0f54dSDimitris Papastamos 
1839fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1840fdf0f54dSDimitris Papastamos 	if (ret < 0) {
184110e8aa9aSMichał Mirosław 		dev_err(codec->dev,
184210e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
184310e8aa9aSMichał Mirosław 			ret);
1844fdf0f54dSDimitris Papastamos 		return ret;
1845fdf0f54dSDimitris Papastamos 	}
1846fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1847fdf0f54dSDimitris Papastamos 	return 0;
1848fdf0f54dSDimitris Papastamos }
1849fdf0f54dSDimitris Papastamos 
1850ce64c8b9SLars-Peter Clausen /**
1851ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1852ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1853ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1854ce64c8b9SLars-Peter Clausen  *
1855ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1856ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1857ce64c8b9SLars-Peter Clausen  *
1858ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1859ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1860ce64c8b9SLars-Peter Clausen  *
1861ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1862ce64c8b9SLars-Peter Clausen  */
1863ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1864ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1865ce64c8b9SLars-Peter Clausen {
1866ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
1867ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1868ce64c8b9SLars-Peter Clausen 	unsigned int i;
1869ce64c8b9SLars-Peter Clausen 	int ret;
1870ce64c8b9SLars-Peter Clausen 
1871ce64c8b9SLars-Peter Clausen 	for (i = 0; i < rtd->num_codecs; i++) {
1872ce64c8b9SLars-Peter Clausen 		struct snd_soc_dai *codec_dai = codec_dais[i];
1873ce64c8b9SLars-Peter Clausen 
1874ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1875ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1876ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1877ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1878ce64c8b9SLars-Peter Clausen 			return ret;
1879ce64c8b9SLars-Peter Clausen 		}
1880ce64c8b9SLars-Peter Clausen 	}
1881ce64c8b9SLars-Peter Clausen 
1882ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1883ce64c8b9SLars-Peter Clausen 	if (cpu_dai->codec) {
1884ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1885ce64c8b9SLars-Peter Clausen 
1886ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1887ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1888ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1889ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1890ce64c8b9SLars-Peter Clausen 			break;
1891ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1892ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1893ce64c8b9SLars-Peter Clausen 			break;
1894ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1895ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1896ce64c8b9SLars-Peter Clausen 			break;
1897ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1898ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1899ce64c8b9SLars-Peter Clausen 			break;
1900ce64c8b9SLars-Peter Clausen 		}
1901ce64c8b9SLars-Peter Clausen 
1902ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1903ce64c8b9SLars-Peter Clausen 	}
1904ce64c8b9SLars-Peter Clausen 
1905ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1906ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1907ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1908ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1909ce64c8b9SLars-Peter Clausen 		return ret;
1910ce64c8b9SLars-Peter Clausen 	}
1911ce64c8b9SLars-Peter Clausen 
1912ce64c8b9SLars-Peter Clausen 	return 0;
1913ce64c8b9SLars-Peter Clausen }
1914ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1915ce64c8b9SLars-Peter Clausen 
1916345233d7SLiam Girdwood 
19171f5a4535STakashi Iwai #ifdef CONFIG_DMI
1918345233d7SLiam Girdwood /* Trim special characters, and replace '-' with '_' since '-' is used to
1919345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1920345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1921345233d7SLiam Girdwood  */
1922345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1923345233d7SLiam Girdwood {
1924345233d7SLiam Girdwood 	int i, j = 0;
1925345233d7SLiam Girdwood 
1926345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1927345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1928345233d7SLiam Girdwood 		    || (name[i] == '_'))
1929345233d7SLiam Girdwood 			name[j++] = name[i];
1930345233d7SLiam Girdwood 		else if (name[i] == '-')
1931345233d7SLiam Girdwood 			name[j++] = '_';
1932345233d7SLiam Girdwood 	}
1933345233d7SLiam Girdwood 
1934345233d7SLiam Girdwood 	name[j] = '\0';
1935345233d7SLiam Girdwood }
1936345233d7SLiam Girdwood 
1937345233d7SLiam Girdwood /**
1938345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1939345233d7SLiam Girdwood  * @card: The card to register DMI names
1940345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1941345233d7SLiam Girdwood  *
1942345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1943345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1944345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1945345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1946345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1947345233d7SLiam Girdwood  * as the sound card long name, in the format of
1948345233d7SLiam Girdwood  * "vendor-product-version-board"
1949345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1950345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1951345233d7SLiam Girdwood  * (UCM) configurations for the card.
1952345233d7SLiam Girdwood  *
1953345233d7SLiam Girdwood  * Possible card long names may be:
1954345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1955345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1956345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1957345233d7SLiam Girdwood  *
1958345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1959345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1960345233d7SLiam Girdwood  *
1961345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1962345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1963345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1964345233d7SLiam Girdwood  * charactors like SPACE.
1965345233d7SLiam Girdwood  *
1966345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1967345233d7SLiam Girdwood  */
1968345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1969345233d7SLiam Girdwood {
1970345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1971345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1972345233d7SLiam Girdwood 	size_t len;
1973345233d7SLiam Girdwood 
1974345233d7SLiam Girdwood 	if (card->long_name)
1975345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1976345233d7SLiam Girdwood 
1977345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1978345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1979345233d7SLiam Girdwood 	if (!vendor) {
1980345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1981345233d7SLiam Girdwood 		return 0;
1982345233d7SLiam Girdwood 	}
1983345233d7SLiam Girdwood 
1984345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1985345233d7SLiam Girdwood 			 "%s", vendor);
1986345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1987345233d7SLiam Girdwood 
1988345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
1989345233d7SLiam Girdwood 	if (product) {
1990345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1991345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1992345233d7SLiam Girdwood 			 longname_buf_size - len,
1993345233d7SLiam Girdwood 			 "-%s", product);
1994345233d7SLiam Girdwood 
1995345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1996345233d7SLiam Girdwood 		if (len < longname_buf_size)
1997345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1998345233d7SLiam Girdwood 
1999345233d7SLiam Girdwood 		/* some vendors like Lenovo may only put a self-explanatory
2000345233d7SLiam Girdwood 		 * name in the product version field
2001345233d7SLiam Girdwood 		 */
2002345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2003345233d7SLiam Girdwood 		if (product_version) {
2004345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
2005345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
2006345233d7SLiam Girdwood 				 longname_buf_size - len,
2007345233d7SLiam Girdwood 				 "-%s", product_version);
2008345233d7SLiam Girdwood 
2009345233d7SLiam Girdwood 			len++;
2010345233d7SLiam Girdwood 			if (len < longname_buf_size)
2011345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
2012345233d7SLiam Girdwood 		}
2013345233d7SLiam Girdwood 	}
2014345233d7SLiam Girdwood 
2015345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
2016345233d7SLiam Girdwood 	if (board) {
2017345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2018345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2019345233d7SLiam Girdwood 			 longname_buf_size - len,
2020345233d7SLiam Girdwood 			 "-%s", board);
2021345233d7SLiam Girdwood 
2022345233d7SLiam Girdwood 		len++;
2023345233d7SLiam Girdwood 		if (len < longname_buf_size)
2024345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2025345233d7SLiam Girdwood 	} else if (!product) {
2026345233d7SLiam Girdwood 		/* fall back to using legacy name */
2027345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2028345233d7SLiam Girdwood 		return 0;
2029345233d7SLiam Girdwood 	}
2030345233d7SLiam Girdwood 
2031345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
2032345233d7SLiam Girdwood 	if (flavour) {
2033345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
2034345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
2035345233d7SLiam Girdwood 			 longname_buf_size - len,
2036345233d7SLiam Girdwood 			 "-%s", flavour);
2037345233d7SLiam Girdwood 
2038345233d7SLiam Girdwood 		len++;
2039345233d7SLiam Girdwood 		if (len < longname_buf_size)
2040345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
2041345233d7SLiam Girdwood 	}
2042345233d7SLiam Girdwood 
2043345233d7SLiam Girdwood 	/* set the card long name */
2044345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
2045345233d7SLiam Girdwood 
2046345233d7SLiam Girdwood 	return 0;
2047345233d7SLiam Girdwood }
2048345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
20491f5a4535STakashi Iwai #endif /* CONFIG_DMI */
2050345233d7SLiam Girdwood 
2051b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2052f0fba2adSLiam Girdwood {
2053fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
20541a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
205561b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2056ce64c8b9SLars-Peter Clausen 	int ret, i, order;
205796dd3622SMark Brown 
205834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
205901b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2060f0fba2adSLiam Girdwood 
2061b19e6e7bSMark Brown 	/* bind DAIs */
2062b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
20636f2f1ff0SMengdong Lin 		ret = soc_bind_dai_link(card, &card->dai_link[i]);
2064b19e6e7bSMark Brown 		if (ret != 0)
2065b19e6e7bSMark Brown 			goto base_error;
2066db2a4165SFrank Mandarino 	}
2067db2a4165SFrank Mandarino 
206844c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2069b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
207044c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2071b19e6e7bSMark Brown 		if (ret != 0)
2072b19e6e7bSMark Brown 			goto base_error;
2073db2a4165SFrank Mandarino 	}
2074db2a4165SFrank Mandarino 
2075f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2076f8f80361SMengdong Lin 	for (i = 0; i < card->num_links; i++)
2077f8f80361SMengdong Lin 		snd_soc_add_dai_link(card, card->dai_link+i);
2078f8f80361SMengdong Lin 
2079fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
2080fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
2081fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
2082fdf0f54dSDimitris Papastamos 			continue;
2083f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
2084b19e6e7bSMark Brown 		if (ret < 0)
2085b19e6e7bSMark Brown 			goto base_error;
2086fdf0f54dSDimitris Papastamos 	}
2087fdf0f54dSDimitris Papastamos 
2088f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2089102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2090f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2091f0fba2adSLiam Girdwood 	if (ret < 0) {
209210e8aa9aSMichał Mirosław 		dev_err(card->dev,
209310e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
209410e8aa9aSMichał Mirosław 			card->name, ret);
2095b19e6e7bSMark Brown 		goto base_error;
2096db2a4165SFrank Mandarino 	}
2097db2a4165SFrank Mandarino 
20980757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20990757d834SLars-Peter Clausen 
2100e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
2101e37a4970SMark Brown 	card->dapm.dev = card->dev;
2102e37a4970SMark Brown 	card->dapm.card = card;
2103e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
2104e37a4970SMark Brown 
2105d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2106d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2107d5d1e0beSLars-Peter Clausen #endif
2108d5d1e0beSLars-Peter Clausen 
210988ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
21106ed25978SAndy Green 	/* deferred resume work */
21116308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
21121301a964SRandy Dunlap #endif
21136ed25978SAndy Green 
21149a841ebbSMark Brown 	if (card->dapm_widgets)
21159a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
21169a841ebbSMark Brown 					  card->num_dapm_widgets);
21179a841ebbSMark Brown 
2118f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2119f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2120f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2121f23e860eSNicolin Chen 
2122f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2123f0fba2adSLiam Girdwood 	if (card->probe) {
2124e7361ec4SMark Brown 		ret = card->probe(card);
2125f0fba2adSLiam Girdwood 		if (ret < 0)
2126f0fba2adSLiam Girdwood 			goto card_probe_error;
2127f0fba2adSLiam Girdwood 	}
2128f0fba2adSLiam Girdwood 
212962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
21300168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
21310168bf0dSLiam Girdwood 			order++) {
21321a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
21331a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
213462ae68faSStephen Warren 			if (ret < 0) {
2135f110bfc7SLiam Girdwood 				dev_err(card->dev,
2136f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2137f110bfc7SLiam Girdwood 					ret);
213862ae68faSStephen Warren 				goto probe_dai_err;
213962ae68faSStephen Warren 			}
214062ae68faSStephen Warren 		}
214162ae68faSStephen Warren 	}
214262ae68faSStephen Warren 
2143f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2144f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2145f2ed6b07SMengdong Lin 	if (ret < 0)
2146f2ed6b07SMengdong Lin 		goto probe_dai_err;
2147f2ed6b07SMengdong Lin 
214861b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
214961b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
215061b0088bSMengdong Lin 	 */
215161b0088bSMengdong Lin 	list_for_each_entry(dai_link, &card->dai_link_list, list) {
215261b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
215361b0088bSMengdong Lin 			continue;
215461b0088bSMengdong Lin 
215561b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
215661b0088bSMengdong Lin 		if (ret)
215761b0088bSMengdong Lin 			goto probe_dai_err;
215861b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
215961b0088bSMengdong Lin 		if (ret)
216061b0088bSMengdong Lin 			goto probe_dai_err;
216161b0088bSMengdong Lin 	}
216261b0088bSMengdong Lin 
216362ae68faSStephen Warren 	/* probe all DAI links on this card */
216462ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
216562ae68faSStephen Warren 			order++) {
21661a497983SMengdong Lin 		list_for_each_entry(rtd, &card->rtd_list, list) {
21671a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2168fe3e78e0SMark Brown 			if (ret < 0) {
2169f110bfc7SLiam Girdwood 				dev_err(card->dev,
2170f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2171f110bfc7SLiam Girdwood 					ret);
2172f0fba2adSLiam Girdwood 				goto probe_dai_err;
2173fe3e78e0SMark Brown 			}
2174fe3e78e0SMark Brown 		}
21750168bf0dSLiam Girdwood 	}
2176fe3e78e0SMark Brown 
2177888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2178b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2179888df395SMark Brown 
2180b7af1dafSMark Brown 	if (card->controls)
2181022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
2182b7af1dafSMark Brown 
2183b8ad29deSMark Brown 	if (card->dapm_routes)
2184b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2185b8ad29deSMark Brown 					card->num_dapm_routes);
2186b8ad29deSMark Brown 
2187f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2188f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2189f23e860eSNicolin Chen 					card->num_of_dapm_routes);
219075d9ac46SMark Brown 
2191861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2192861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2193861886d3STakashi Iwai 
2194f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2195fe3e78e0SMark Brown 		 "%s", card->name);
2196f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
219722de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2198f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2199f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2200f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2201f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2202f0e8ed85SMark Brown 		case '_':
2203f0e8ed85SMark Brown 		case '-':
2204f0e8ed85SMark Brown 		case '\0':
2205f0e8ed85SMark Brown 			break;
2206f0e8ed85SMark Brown 		default:
2207f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2208f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2209f0e8ed85SMark Brown 			break;
2210f0e8ed85SMark Brown 		}
2211f0e8ed85SMark Brown 	}
2212fe3e78e0SMark Brown 
221328e9ad92SMark Brown 	if (card->late_probe) {
221428e9ad92SMark Brown 		ret = card->late_probe(card);
221528e9ad92SMark Brown 		if (ret < 0) {
2216f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
221728e9ad92SMark Brown 				card->name, ret);
221828e9ad92SMark Brown 			goto probe_aux_dev_err;
221928e9ad92SMark Brown 		}
222028e9ad92SMark Brown 	}
222128e9ad92SMark Brown 
2222824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
22238c193b8dSLars-Peter Clausen 
2224f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2225fe3e78e0SMark Brown 	if (ret < 0) {
2226f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2227f110bfc7SLiam Girdwood 				ret);
22286b3ed785SAxel Lin 		goto probe_aux_dev_err;
2229fe3e78e0SMark Brown 	}
2230fe3e78e0SMark Brown 
2231435c5e25SMark Brown 	card->instantiated = 1;
22324f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2233f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
223434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2235b19e6e7bSMark Brown 
2236b19e6e7bSMark Brown 	return 0;
2237db2a4165SFrank Mandarino 
22382eea392dSJarkko Nikula probe_aux_dev_err:
2239f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
22402eea392dSJarkko Nikula 
2241f0fba2adSLiam Girdwood probe_dai_err:
22420671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2243fe3e78e0SMark Brown 
2244f0fba2adSLiam Girdwood card_probe_error:
224587506549SMark Brown 	if (card->remove)
2246e7361ec4SMark Brown 		card->remove(card);
2247f0fba2adSLiam Girdwood 
22482210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
22490757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2250f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2251f0fba2adSLiam Girdwood 
2252b19e6e7bSMark Brown base_error:
22531a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2254f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
225534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2256db2a4165SFrank Mandarino 
2257b19e6e7bSMark Brown 	return ret;
2258435c5e25SMark Brown }
2259435c5e25SMark Brown 
2260435c5e25SMark Brown /* probes a new socdev */
2261435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2262435c5e25SMark Brown {
2263f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2264435c5e25SMark Brown 
226570a7ca34SVinod Koul 	/*
226670a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
226770a7ca34SVinod Koul 	 * we should not be here in that case so ret error
226870a7ca34SVinod Koul 	 */
226970a7ca34SVinod Koul 	if (!card)
227070a7ca34SVinod Koul 		return -EINVAL;
227170a7ca34SVinod Koul 
2272fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2273f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2274fe4085e8SMark Brown 		 card->name);
2275fe4085e8SMark Brown 
2276435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2277435c5e25SMark Brown 	card->dev = &pdev->dev;
2278f0fba2adSLiam Girdwood 
227928d528c8SMark Brown 	return snd_soc_register_card(card);
2280435c5e25SMark Brown }
2281435c5e25SMark Brown 
2282b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2283db2a4165SFrank Mandarino {
22841a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2285db2a4165SFrank Mandarino 
2286f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
22871a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
228843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2289db2a4165SFrank Mandarino 
2290f0fba2adSLiam Girdwood 	/* remove and free each DAI */
22910671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
22921a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2293f0fba2adSLiam Girdwood 
2294f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2295f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2296f2ed6b07SMengdong Lin 
2297d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2298a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2299a6052154SJarkko Nikula 
2300f0fba2adSLiam Girdwood 	/* remove the card */
230187506549SMark Brown 	if (card->remove)
2302e7361ec4SMark Brown 		card->remove(card);
2303f0fba2adSLiam Girdwood 
2304f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2305b0e26485SVinod Koul 	return 0;
2306b0e26485SVinod Koul 
2307b2dfa62cSGuennadi Liakhovetski }
2308b0e26485SVinod Koul 
2309b0e26485SVinod Koul /* removes a socdev */
2310b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2311b0e26485SVinod Koul {
2312b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2313b0e26485SVinod Koul 
2314c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2315db2a4165SFrank Mandarino 	return 0;
2316db2a4165SFrank Mandarino }
2317db2a4165SFrank Mandarino 
23186f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
231951737470SMark Brown {
23206f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
23211a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
232251737470SMark Brown 
232351737470SMark Brown 	if (!card->instantiated)
2324416356fcSMark Brown 		return 0;
232551737470SMark Brown 
232651737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
232751737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
23281a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)
232943829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
233051737470SMark Brown 
2331f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2332416356fcSMark Brown 
2333988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
23341a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list) {
233588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
23361a497983SMengdong Lin 		int i;
233788bd870fSBenoit Cousson 
2338988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
23391a497983SMengdong Lin 		for (i = 0; i < rtd->num_codecs; i++) {
23401a497983SMengdong Lin 			struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
234188bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
234288bd870fSBenoit Cousson 		}
2343988e8cc4SNicolin Chen 	}
2344988e8cc4SNicolin Chen 
2345416356fcSMark Brown 	return 0;
234651737470SMark Brown }
23476f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
234851737470SMark Brown 
23496f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2350b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2351b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2352b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2353b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
23546f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2355b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2356416356fcSMark Brown };
2357deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2358416356fcSMark Brown 
2359db2a4165SFrank Mandarino /* ASoC platform driver */
2360db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2361db2a4165SFrank Mandarino 	.driver		= {
2362db2a4165SFrank Mandarino 		.name		= "soc-audio",
23636f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2364db2a4165SFrank Mandarino 	},
2365db2a4165SFrank Mandarino 	.probe		= soc_probe,
2366db2a4165SFrank Mandarino 	.remove		= soc_remove,
2367db2a4165SFrank Mandarino };
2368db2a4165SFrank Mandarino 
2369096e49d5SMark Brown /**
2370db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2371db2a4165SFrank Mandarino  * @_template: control template
2372db2a4165SFrank Mandarino  * @data: control private data
2373ac11a2b3SMark Brown  * @long_name: control long name
2374efb7ac3fSMark Brown  * @prefix: control name prefix
2375db2a4165SFrank Mandarino  *
2376db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2377db2a4165SFrank Mandarino  *
2378db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2379db2a4165SFrank Mandarino  */
2380db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
23813056557fSMark Brown 				  void *data, const char *long_name,
2382efb7ac3fSMark Brown 				  const char *prefix)
2383db2a4165SFrank Mandarino {
2384db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2385efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2386efb7ac3fSMark Brown 	char *name = NULL;
2387db2a4165SFrank Mandarino 
2388db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2389db2a4165SFrank Mandarino 	template.index = 0;
2390db2a4165SFrank Mandarino 
2391efb7ac3fSMark Brown 	if (!long_name)
2392efb7ac3fSMark Brown 		long_name = template.name;
2393efb7ac3fSMark Brown 
2394efb7ac3fSMark Brown 	if (prefix) {
23952b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2396efb7ac3fSMark Brown 		if (!name)
2397efb7ac3fSMark Brown 			return NULL;
2398efb7ac3fSMark Brown 
2399efb7ac3fSMark Brown 		template.name = name;
2400efb7ac3fSMark Brown 	} else {
2401efb7ac3fSMark Brown 		template.name = long_name;
2402efb7ac3fSMark Brown 	}
2403efb7ac3fSMark Brown 
2404efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2405efb7ac3fSMark Brown 
2406efb7ac3fSMark Brown 	kfree(name);
2407efb7ac3fSMark Brown 
2408efb7ac3fSMark Brown 	return kcontrol;
2409db2a4165SFrank Mandarino }
2410db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2411db2a4165SFrank Mandarino 
2412022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2413022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2414022658beSLiam Girdwood 	const char *prefix, void *data)
2415022658beSLiam Girdwood {
2416022658beSLiam Girdwood 	int err, i;
2417022658beSLiam Girdwood 
2418022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2419022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2420022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2421022658beSLiam Girdwood 						     control->name, prefix));
2422022658beSLiam Girdwood 		if (err < 0) {
2423f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2424f110bfc7SLiam Girdwood 				control->name, err);
2425022658beSLiam Girdwood 			return err;
2426022658beSLiam Girdwood 		}
2427022658beSLiam Girdwood 	}
2428022658beSLiam Girdwood 
2429022658beSLiam Girdwood 	return 0;
2430022658beSLiam Girdwood }
2431022658beSLiam Girdwood 
24324fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
24334fefd698SDimitris Papastamos 					       const char *name)
24344fefd698SDimitris Papastamos {
24354fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
24364fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
24374fefd698SDimitris Papastamos 
24384fefd698SDimitris Papastamos 	if (unlikely(!name))
24394fefd698SDimitris Papastamos 		return NULL;
24404fefd698SDimitris Papastamos 
24414fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
24424fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
24434fefd698SDimitris Papastamos 			return kctl;
24444fefd698SDimitris Papastamos 	return NULL;
24454fefd698SDimitris Papastamos }
24464fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
24474fefd698SDimitris Papastamos 
2448db2a4165SFrank Mandarino /**
24490f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
24500f2780adSLars-Peter Clausen  *
24510f2780adSLars-Peter Clausen  * @component: Component to add controls to
24520f2780adSLars-Peter Clausen  * @controls: Array of controls to add
24530f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
24540f2780adSLars-Peter Clausen  *
24550f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
24560f2780adSLars-Peter Clausen  */
24570f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
24580f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
24590f2780adSLars-Peter Clausen {
24600f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
24610f2780adSLars-Peter Clausen 
24620f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
24630f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
24640f2780adSLars-Peter Clausen }
24650f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
24660f2780adSLars-Peter Clausen 
24670f2780adSLars-Peter Clausen /**
2468022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2469022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
24703e8e1952SIan Molton  * duplicating this code.
24713e8e1952SIan Molton  *
24723e8e1952SIan Molton  * @codec: codec to add controls to
24733e8e1952SIan Molton  * @controls: array of controls to add
24743e8e1952SIan Molton  * @num_controls: number of elements in the array
24753e8e1952SIan Molton  *
24763e8e1952SIan Molton  * Return 0 for success, else error.
24773e8e1952SIan Molton  */
2478022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
24790f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
24803e8e1952SIan Molton {
24810f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
24820f2780adSLars-Peter Clausen 		num_controls);
24833e8e1952SIan Molton }
2484022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
24853e8e1952SIan Molton 
24863e8e1952SIan Molton /**
2487a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2488022658beSLiam Girdwood  * Convenience function to add a list of controls.
2489a491a5c8SLiam Girdwood  *
2490a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2491a491a5c8SLiam Girdwood  * @controls: array of controls to add
2492a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2493a491a5c8SLiam Girdwood  *
2494a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2495a491a5c8SLiam Girdwood  */
2496a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
24970f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2498a491a5c8SLiam Girdwood {
24990f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
25000f2780adSLars-Peter Clausen 		num_controls);
2501a491a5c8SLiam Girdwood }
2502a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2503a491a5c8SLiam Girdwood 
2504a491a5c8SLiam Girdwood /**
2505022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2506022658beSLiam Girdwood  * Convenience function to add a list of controls.
2507022658beSLiam Girdwood  *
2508022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2509022658beSLiam Girdwood  * @controls: array of controls to add
2510022658beSLiam Girdwood  * @num_controls: number of elements in the array
2511022658beSLiam Girdwood  *
2512022658beSLiam Girdwood  * Return 0 for success, else error.
2513022658beSLiam Girdwood  */
2514022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2515022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2516022658beSLiam Girdwood {
2517022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2518022658beSLiam Girdwood 
2519022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2520022658beSLiam Girdwood 			NULL, soc_card);
2521022658beSLiam Girdwood }
2522022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2523022658beSLiam Girdwood 
2524022658beSLiam Girdwood /**
2525022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2526022658beSLiam Girdwood  * Convienience function to add a list of controls.
2527022658beSLiam Girdwood  *
2528022658beSLiam Girdwood  * @dai: DAI to add controls to
2529022658beSLiam Girdwood  * @controls: array of controls to add
2530022658beSLiam Girdwood  * @num_controls: number of elements in the array
2531022658beSLiam Girdwood  *
2532022658beSLiam Girdwood  * Return 0 for success, else error.
2533022658beSLiam Girdwood  */
2534022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2535022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2536022658beSLiam Girdwood {
2537313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2538022658beSLiam Girdwood 
2539022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2540022658beSLiam Girdwood 			NULL, dai);
2541022658beSLiam Girdwood }
2542022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2543022658beSLiam Girdwood 
2544022658beSLiam Girdwood /**
25458c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
25468c6529dbSLiam Girdwood  * @dai: DAI
25478c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
25488c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
25498c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
25508c6529dbSLiam Girdwood  *
25518c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
25528c6529dbSLiam Girdwood  */
25538c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
25548c6529dbSLiam Girdwood 	unsigned int freq, int dir)
25558c6529dbSLiam Girdwood {
2556f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2557f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2558ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2559da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2560ec4ee52aSMark Brown 						      freq, dir);
25618c6529dbSLiam Girdwood 	else
25621104a9c8SMark Brown 		return -ENOTSUPP;
25638c6529dbSLiam Girdwood }
25648c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
25658c6529dbSLiam Girdwood 
25668c6529dbSLiam Girdwood /**
2567ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2568ec4ee52aSMark Brown  * @codec: CODEC
2569ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2570da1c6ea6SMark Brown  * @source: Source for the clock
2571ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2572ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2573ec4ee52aSMark Brown  *
2574ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2575ec4ee52aSMark Brown  */
2576ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2577da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2578ec4ee52aSMark Brown {
2579ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2580da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2581da1c6ea6SMark Brown 						 freq, dir);
2582ec4ee52aSMark Brown 	else
25831104a9c8SMark Brown 		return -ENOTSUPP;
2584ec4ee52aSMark Brown }
2585ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2586ec4ee52aSMark Brown 
2587ec4ee52aSMark Brown /**
25888c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
25898c6529dbSLiam Girdwood  * @dai: DAI
2590ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
25918c6529dbSLiam Girdwood  * @div: new clock divisor.
25928c6529dbSLiam Girdwood  *
25938c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
25948c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
25958c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
25968c6529dbSLiam Girdwood  */
25978c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
25988c6529dbSLiam Girdwood 	int div_id, int div)
25998c6529dbSLiam Girdwood {
2600f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2601f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
26028c6529dbSLiam Girdwood 	else
26038c6529dbSLiam Girdwood 		return -EINVAL;
26048c6529dbSLiam Girdwood }
26058c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
26068c6529dbSLiam Girdwood 
26078c6529dbSLiam Girdwood /**
26088c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
26098c6529dbSLiam Girdwood  * @dai: DAI
26108c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
261185488037SMark Brown  * @source: DAI specific source for the PLL
26128c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
26138c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
26148c6529dbSLiam Girdwood  *
26158c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
26168c6529dbSLiam Girdwood  */
261785488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
261885488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
26198c6529dbSLiam Girdwood {
2620f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2621f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
262285488037SMark Brown 					 freq_in, freq_out);
2623ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2624ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2625ec4ee52aSMark Brown 						   freq_in, freq_out);
26268c6529dbSLiam Girdwood 	else
26278c6529dbSLiam Girdwood 		return -EINVAL;
26288c6529dbSLiam Girdwood }
26298c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
26308c6529dbSLiam Girdwood 
2631ec4ee52aSMark Brown /*
2632ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2633ec4ee52aSMark Brown  * @codec: CODEC
2634ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2635ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2636ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2637ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2638ec4ee52aSMark Brown  *
2639ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2640ec4ee52aSMark Brown  */
2641ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2642ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2643ec4ee52aSMark Brown {
2644ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2645ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2646ec4ee52aSMark Brown 					      freq_in, freq_out);
2647ec4ee52aSMark Brown 	else
2648ec4ee52aSMark Brown 		return -EINVAL;
2649ec4ee52aSMark Brown }
2650ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2651ec4ee52aSMark Brown 
26528c6529dbSLiam Girdwood /**
2653e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2654e54cf76bSLiam Girdwood  * @dai: DAI
2655231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2656e54cf76bSLiam Girdwood  *
2657e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2658e54cf76bSLiam Girdwood  */
2659e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2660e54cf76bSLiam Girdwood {
2661e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2662e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2663e54cf76bSLiam Girdwood 	else
2664e54cf76bSLiam Girdwood 		return -EINVAL;
2665e54cf76bSLiam Girdwood }
2666e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2667e54cf76bSLiam Girdwood 
2668e54cf76bSLiam Girdwood /**
26698c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
26708c6529dbSLiam Girdwood  * @dai: DAI
26718c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
26728c6529dbSLiam Girdwood  *
26738c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
26748c6529dbSLiam Girdwood  */
26758c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
26768c6529dbSLiam Girdwood {
26775e4ba569SShawn Guo 	if (dai->driver == NULL)
26788c6529dbSLiam Girdwood 		return -EINVAL;
26795e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
26805e4ba569SShawn Guo 		return -ENOTSUPP;
26815e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
26828c6529dbSLiam Girdwood }
26838c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
26848c6529dbSLiam Girdwood 
26858c6529dbSLiam Girdwood /**
2686e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
268789c67857SXiubo Li  * @slots: Number of slots in use.
268889c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
268989c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
269089c67857SXiubo Li  *
269189c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
269289c67857SXiubo Li  */
2693e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
269489c67857SXiubo Li 					  unsigned int *tx_mask,
269589c67857SXiubo Li 					  unsigned int *rx_mask)
269689c67857SXiubo Li {
269789c67857SXiubo Li 	if (*tx_mask || *rx_mask)
269889c67857SXiubo Li 		return 0;
269989c67857SXiubo Li 
270089c67857SXiubo Li 	if (!slots)
270189c67857SXiubo Li 		return -EINVAL;
270289c67857SXiubo Li 
270389c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
270489c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
270589c67857SXiubo Li 
270689c67857SXiubo Li 	return 0;
270789c67857SXiubo Li }
270889c67857SXiubo Li 
270989c67857SXiubo Li /**
2710e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2711e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2712a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2713a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
27148c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2715a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
27168c6529dbSLiam Girdwood  *
2717e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2718e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2719e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2720e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2721e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2722e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2723e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2724e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2725e46c9366SLars-Peter Clausen  * channel and so on.
2726e46c9366SLars-Peter Clausen  *
2727e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2728e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2729e46c9366SLars-Peter Clausen  *
2730e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
27318c6529dbSLiam Girdwood  */
27328c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2733a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
27348c6529dbSLiam Girdwood {
2735e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2736e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
273789c67857SXiubo Li 						&tx_mask, &rx_mask);
273889c67857SXiubo Li 	else
2739e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
274089c67857SXiubo Li 
274188bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
274288bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
274388bd870fSBenoit Cousson 
2744f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2745f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2746a5479e38SDaniel Ribeiro 				slots, slot_width);
27478c6529dbSLiam Girdwood 	else
2748b2cbb6e1SXiubo Li 		return -ENOTSUPP;
27498c6529dbSLiam Girdwood }
27508c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
27518c6529dbSLiam Girdwood 
27528c6529dbSLiam Girdwood /**
2753472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2754472df3cbSBarry Song  * @dai: DAI
2755472df3cbSBarry Song  * @tx_num: how many TX channels
2756472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2757472df3cbSBarry Song  *           0~num-1 uses
2758472df3cbSBarry Song  * @rx_num: how many RX channels
2759472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2760472df3cbSBarry Song  *           0~num-1 uses
2761472df3cbSBarry Song  *
2762472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2763472df3cbSBarry Song  */
2764472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2765472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2766472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2767472df3cbSBarry Song {
2768f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2769f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2770472df3cbSBarry Song 			rx_num, rx_slot);
2771472df3cbSBarry Song 	else
2772472df3cbSBarry Song 		return -EINVAL;
2773472df3cbSBarry Song }
2774472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2775472df3cbSBarry Song 
2776472df3cbSBarry Song /**
27778c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
27788c6529dbSLiam Girdwood  * @dai: DAI
27798c6529dbSLiam Girdwood  * @tristate: tristate enable
27808c6529dbSLiam Girdwood  *
27818c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
27828c6529dbSLiam Girdwood  */
27838c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
27848c6529dbSLiam Girdwood {
2785f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2786f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
27878c6529dbSLiam Girdwood 	else
27888c6529dbSLiam Girdwood 		return -EINVAL;
27898c6529dbSLiam Girdwood }
27908c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
27918c6529dbSLiam Girdwood 
27928c6529dbSLiam Girdwood /**
27938c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
27948c6529dbSLiam Girdwood  * @dai: DAI
27958c6529dbSLiam Girdwood  * @mute: mute enable
2796da18396fSMark Brown  * @direction: stream to mute
27978c6529dbSLiam Girdwood  *
27988c6529dbSLiam Girdwood  * Mutes the DAI DAC.
27998c6529dbSLiam Girdwood  */
2800da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2801da18396fSMark Brown 			     int direction)
28028c6529dbSLiam Girdwood {
2803da18396fSMark Brown 	if (!dai->driver)
2804da18396fSMark Brown 		return -ENOTSUPP;
2805da18396fSMark Brown 
2806da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2807da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2808da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2809da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2810f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
28118c6529dbSLiam Girdwood 	else
281204570c62SMark Brown 		return -ENOTSUPP;
28138c6529dbSLiam Girdwood }
28148c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
28158c6529dbSLiam Girdwood 
2816c5af3a2eSMark Brown /**
2817c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2818c5af3a2eSMark Brown  *
2819ac11a2b3SMark Brown  * @card: Card to register
2820c5af3a2eSMark Brown  *
2821c5af3a2eSMark Brown  */
282270a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2823c5af3a2eSMark Brown {
2824923c5e61SMengdong Lin 	int i, ret;
28251a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2826f0fba2adSLiam Girdwood 
2827c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2828c5af3a2eSMark Brown 		return -EINVAL;
2829c5af3a2eSMark Brown 
28305a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
28315a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
28325a504963SStephen Warren 
2833923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
283488bd870fSBenoit Cousson 		if (ret) {
2835923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2836923c5e61SMengdong Lin 				link->name);
283788bd870fSBenoit Cousson 			return ret;
283888bd870fSBenoit Cousson 		}
28395a504963SStephen Warren 	}
28405a504963SStephen Warren 
2841ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2842ed77cc12SMark Brown 
2843111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2844111c6419SStephen Warren 
2845f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2846f8f80361SMengdong Lin 	card->num_dai_links = 0;
2847f0fba2adSLiam Girdwood 
28481a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
28499115171aSMark Brown 	card->num_rtd = 0;
2850db2a4165SFrank Mandarino 
2851db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
28528a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2853db2a4165SFrank Mandarino 	card->instantiated = 0;
2854db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2855a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2856db2a4165SFrank Mandarino 
2857b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2858b19e6e7bSMark Brown 	if (ret != 0)
28594e2576bdSKuninori Morimoto 		return ret;
2860db2a4165SFrank Mandarino 
2861988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
28621a497983SMengdong Lin 	list_for_each_entry(rtd, &card->rtd_list, list)  {
286388bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
286488bd870fSBenoit Cousson 		int j;
286588bd870fSBenoit Cousson 
286688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
286788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2868988e8cc4SNicolin Chen 			if (!codec_dai->active)
2869988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
287088bd870fSBenoit Cousson 		}
287188bd870fSBenoit Cousson 
2872988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2873988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2874988e8cc4SNicolin Chen 	}
2875988e8cc4SNicolin Chen 
2876b19e6e7bSMark Brown 	return ret;
2877db2a4165SFrank Mandarino }
287870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2879db2a4165SFrank Mandarino 
2880db2a4165SFrank Mandarino /**
2881db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2882db2a4165SFrank Mandarino  *
2883db2a4165SFrank Mandarino  * @card: Card to unregister
2884db2a4165SFrank Mandarino  *
2885db2a4165SFrank Mandarino  */
288670a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2887db2a4165SFrank Mandarino {
288801e0df66SLars-Peter Clausen 	if (card->instantiated) {
288901e0df66SLars-Peter Clausen 		card->instantiated = false;
28901c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2891b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2892f110bfc7SLiam Girdwood 		dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
28938f6f9b29SKuninori Morimoto 	}
2894db2a4165SFrank Mandarino 
2895db2a4165SFrank Mandarino 	return 0;
2896db2a4165SFrank Mandarino }
289770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2898db2a4165SFrank Mandarino 
2899db2a4165SFrank Mandarino /*
2900db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2901db2a4165SFrank Mandarino  * and sanitizing names.
2902db2a4165SFrank Mandarino  */
29030b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2904db2a4165SFrank Mandarino {
2905db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2906db2a4165SFrank Mandarino 	int id1, id2;
2907db2a4165SFrank Mandarino 
2908db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2909db2a4165SFrank Mandarino 		return NULL;
2910db2a4165SFrank Mandarino 
291158818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2912db2a4165SFrank Mandarino 
2913db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2914c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2915c5af3a2eSMark Brown 	if (found) {
2916c5af3a2eSMark Brown 		/* get ID */
2917c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2918c5af3a2eSMark Brown 
2919c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2920c5af3a2eSMark Brown 			if (*id == -1)
2921c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2922c5af3a2eSMark Brown 		}
2923c5af3a2eSMark Brown 
2924c5af3a2eSMark Brown 	} else {
2925c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2926c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2927c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2928c5af3a2eSMark Brown 
2929c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2930c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2931c5af3a2eSMark Brown 
2932c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2933c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
293458818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2935c5af3a2eSMark Brown 		} else
2936c5af3a2eSMark Brown 			*id = 0;
2937c5af3a2eSMark Brown 	}
2938c5af3a2eSMark Brown 
2939c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2940c5af3a2eSMark Brown }
2941c5af3a2eSMark Brown 
2942c5af3a2eSMark Brown /*
2943c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2944c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2945c5af3a2eSMark Brown  */
2946c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2947c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2948c5af3a2eSMark Brown {
2949c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
295010e8aa9aSMichał Mirosław 		dev_err(dev,
295110e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
295210e8aa9aSMichał Mirosław 			dev_name(dev));
2953c5af3a2eSMark Brown 		return NULL;
2954c5af3a2eSMark Brown 	}
2955c5af3a2eSMark Brown 
2956c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2957c5af3a2eSMark Brown }
2958c5af3a2eSMark Brown 
2959c5af3a2eSMark Brown /**
296032c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
29619115171aSMark Brown  *
296232c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
29639115171aSMark Brown  */
296432c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
29659115171aSMark Brown {
29665c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
29679115171aSMark Brown 
29685c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
296932c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
297032c9ba54SLars-Peter Clausen 			dai->name);
29719115171aSMark Brown 		list_del(&dai->list);
2972f0fba2adSLiam Girdwood 		kfree(dai->name);
2973f0fba2adSLiam Girdwood 		kfree(dai);
29749115171aSMark Brown 	}
297532c9ba54SLars-Peter Clausen }
29769115171aSMark Brown 
29775e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
29785e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
29795e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
29805e4fb372SMengdong Lin 	bool legacy_dai_naming)
29815e4fb372SMengdong Lin {
29825e4fb372SMengdong Lin 	struct device *dev = component->dev;
29835e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
29845e4fb372SMengdong Lin 
29855e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
29865e4fb372SMengdong Lin 
29875e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
29885e4fb372SMengdong Lin 	if (dai == NULL)
29895e4fb372SMengdong Lin 		return NULL;
29905e4fb372SMengdong Lin 
29915e4fb372SMengdong Lin 	/*
29925e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
29935e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
29945e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
29955e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
29965e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
29975e4fb372SMengdong Lin 	 * component-less anymore.
29985e4fb372SMengdong Lin 	 */
29995e4fb372SMengdong Lin 	if (legacy_dai_naming &&
30005e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
30015e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
30025e4fb372SMengdong Lin 	} else {
30035e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
30045e4fb372SMengdong Lin 		if (dai_drv->id)
30055e4fb372SMengdong Lin 			dai->id = dai_drv->id;
30065e4fb372SMengdong Lin 		else
30075e4fb372SMengdong Lin 			dai->id = component->num_dai;
30085e4fb372SMengdong Lin 	}
30095e4fb372SMengdong Lin 	if (dai->name == NULL) {
30105e4fb372SMengdong Lin 		kfree(dai);
30115e4fb372SMengdong Lin 		return NULL;
30125e4fb372SMengdong Lin 	}
30135e4fb372SMengdong Lin 
30145e4fb372SMengdong Lin 	dai->component = component;
30155e4fb372SMengdong Lin 	dai->dev = dev;
30165e4fb372SMengdong Lin 	dai->driver = dai_drv;
30175e4fb372SMengdong Lin 	if (!dai->driver->ops)
30185e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
30195e4fb372SMengdong Lin 
30205e4fb372SMengdong Lin 	list_add(&dai->list, &component->dai_list);
30215e4fb372SMengdong Lin 	component->num_dai++;
30225e4fb372SMengdong Lin 
30235e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
30245e4fb372SMengdong Lin 	return dai;
30255e4fb372SMengdong Lin }
30265e4fb372SMengdong Lin 
30279115171aSMark Brown /**
302832c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
30299115171aSMark Brown  *
30306106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
30316106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3032ac11a2b3SMark Brown  * @count: Number of DAIs
303332c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
303432c9ba54SLars-Peter Clausen  *                     parent's name.
30359115171aSMark Brown  */
303632c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
3037bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
3038bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
30399115171aSMark Brown {
30406106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3041f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
304232c9ba54SLars-Peter Clausen 	unsigned int i;
304332c9ba54SLars-Peter Clausen 	int ret;
3044f0fba2adSLiam Girdwood 
30455b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
30469115171aSMark Brown 
3047bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
3048bb13109dSLars-Peter Clausen 
30499115171aSMark Brown 	for (i = 0; i < count; i++) {
3050f0fba2adSLiam Girdwood 
30515e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
30525e4fb372SMengdong Lin 				count == 1 && legacy_dai_naming);
3053c46e0079SAxel Lin 		if (dai == NULL) {
3054c46e0079SAxel Lin 			ret = -ENOMEM;
3055c46e0079SAxel Lin 			goto err;
3056c46e0079SAxel Lin 		}
3057f0fba2adSLiam Girdwood 	}
3058f0fba2adSLiam Girdwood 
30599115171aSMark Brown 	return 0;
30609115171aSMark Brown 
30619115171aSMark Brown err:
306232c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
30639115171aSMark Brown 
30649115171aSMark Brown 	return ret;
30659115171aSMark Brown }
30669115171aSMark Brown 
306768003e6cSMengdong Lin /**
306868003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
306968003e6cSMengdong Lin  *
307068003e6cSMengdong Lin  * @component: The component the DAIs are registered for
307168003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
307268003e6cSMengdong Lin  *
307368003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
307468003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
307568003e6cSMengdong Lin  * will be freed in the component cleanup.
307668003e6cSMengdong Lin  */
307768003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
307868003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
307968003e6cSMengdong Lin {
308068003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
308168003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
308268003e6cSMengdong Lin 	struct snd_soc_dai *dai;
308368003e6cSMengdong Lin 	int ret;
308468003e6cSMengdong Lin 
308568003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
308668003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
308768003e6cSMengdong Lin 			dai_drv->dobj.type);
308868003e6cSMengdong Lin 		return -EINVAL;
308968003e6cSMengdong Lin 	}
309068003e6cSMengdong Lin 
309168003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
309268003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
309368003e6cSMengdong Lin 	if (!dai)
309468003e6cSMengdong Lin 		return -ENOMEM;
309568003e6cSMengdong Lin 
309668003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
309768003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
309868003e6cSMengdong Lin 	 */
309968003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
310068003e6cSMengdong Lin 	if (ret != 0) {
310168003e6cSMengdong Lin 		dev_err(component->dev,
310268003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
310368003e6cSMengdong Lin 	}
310468003e6cSMengdong Lin 
310568003e6cSMengdong Lin 	return ret;
310668003e6cSMengdong Lin }
310768003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
310868003e6cSMengdong Lin 
310914e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
311014e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3111d191bd8dSKuninori Morimoto {
311214e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3113d191bd8dSKuninori Morimoto 
311414e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
311514e8bdebSLars-Peter Clausen }
3116d191bd8dSKuninori Morimoto 
311714e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
311814e8bdebSLars-Peter Clausen 	int event)
311914e8bdebSLars-Peter Clausen {
312014e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
312114e8bdebSLars-Peter Clausen 
312214e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
312314e8bdebSLars-Peter Clausen }
312414e8bdebSLars-Peter Clausen 
3125bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3126bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3127d191bd8dSKuninori Morimoto {
3128ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3129ce0fc93aSLars-Peter Clausen 
3130bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3131bb13109dSLars-Peter Clausen 	if (!component->name) {
3132bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3133d191bd8dSKuninori Morimoto 		return -ENOMEM;
3134d191bd8dSKuninori Morimoto 	}
3135d191bd8dSKuninori Morimoto 
3136bb13109dSLars-Peter Clausen 	component->dev = dev;
3137bb13109dSLars-Peter Clausen 	component->driver = driver;
313861aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
313961aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
31409178feb4SKuninori Morimoto 	component->suspend = component->driver->suspend;
31419178feb4SKuninori Morimoto 	component->resume = component->driver->resume;
314299b04f4cSKuninori Morimoto 	component->pcm_new = component->driver->pcm_new;
314399b04f4cSKuninori Morimoto 	component->pcm_free = component->driver->pcm_free;
3144e2c330b9SLars-Peter Clausen 
31454890140fSLars-Peter Clausen 	dapm = &component->dapm;
3146ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3147ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3148ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
31495819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
315014e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
315114e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
315214e8bdebSLars-Peter Clausen 	if (driver->stream_event)
315314e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
3154ce0fc93aSLars-Peter Clausen 
315561aca564SLars-Peter Clausen 	component->controls = driver->controls;
315661aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
315761aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
315861aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
315961aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
316061aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
316161aca564SLars-Peter Clausen 
3162bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3163bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3164bb13109dSLars-Peter Clausen 
3165bb13109dSLars-Peter Clausen 	return 0;
3166d191bd8dSKuninori Morimoto }
3167d191bd8dSKuninori Morimoto 
316820feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3169886f5692SLars-Peter Clausen {
3170886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
317120feb881SLars-Peter Clausen 
3172886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3173886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3174886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3175886f5692SLars-Peter Clausen }
317620feb881SLars-Peter Clausen 
3177e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3178e874bf5fSLars-Peter Clausen 
317920feb881SLars-Peter Clausen /**
318020feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
318120feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
318220feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
318320feb881SLars-Peter Clausen  *
318420feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
318520feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
318620feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
318720feb881SLars-Peter Clausen  * before the first IO attempt of the component.
318820feb881SLars-Peter Clausen  */
318920feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
319020feb881SLars-Peter Clausen 	struct regmap *regmap)
319120feb881SLars-Peter Clausen {
319220feb881SLars-Peter Clausen 	component->regmap = regmap;
319320feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3194886f5692SLars-Peter Clausen }
319520feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
319620feb881SLars-Peter Clausen 
319720feb881SLars-Peter Clausen /**
319820feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
319920feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
320020feb881SLars-Peter Clausen  *
320120feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
320220feb881SLars-Peter Clausen  * removes the regmap instance from the component.
320320feb881SLars-Peter Clausen  *
320420feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
320520feb881SLars-Peter Clausen  * to initialize the regmap instance.
320620feb881SLars-Peter Clausen  */
320720feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
320820feb881SLars-Peter Clausen {
320920feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
321020feb881SLars-Peter Clausen 	component->regmap = NULL;
321120feb881SLars-Peter Clausen }
321220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3213886f5692SLars-Peter Clausen 
3214e874bf5fSLars-Peter Clausen #endif
3215d191bd8dSKuninori Morimoto 
3216bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3217bb13109dSLars-Peter Clausen {
321820feb881SLars-Peter Clausen 	if (!component->write && !component->read) {
321920feb881SLars-Peter Clausen 		if (!component->regmap)
322020feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
322120feb881SLars-Peter Clausen 		if (component->regmap)
322220feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
322320feb881SLars-Peter Clausen 	}
3224886f5692SLars-Peter Clausen 
3225bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
32268a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3227d191bd8dSKuninori Morimoto }
3228d191bd8dSKuninori Morimoto 
3229bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
3230bb13109dSLars-Peter Clausen {
3231d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
3232bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
3233d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3234bb13109dSLars-Peter Clausen }
3235d191bd8dSKuninori Morimoto 
3236bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3237bb13109dSLars-Peter Clausen {
3238bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3239bb13109dSLars-Peter Clausen 	kfree(component->name);
3240bb13109dSLars-Peter Clausen }
3241d191bd8dSKuninori Morimoto 
3242bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3243bb13109dSLars-Peter Clausen {
3244c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3245c12c1aadSKuninori Morimoto 
3246c12c1aadSKuninori Morimoto 	if (card)
3247c12c1aadSKuninori Morimoto 		snd_soc_unregister_card(card);
3248c12c1aadSKuninori Morimoto 
3249bb13109dSLars-Peter Clausen 	list_del(&component->list);
3250bb13109dSLars-Peter Clausen }
3251d191bd8dSKuninori Morimoto 
3252d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3253d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3254d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3255d191bd8dSKuninori Morimoto 			       int num_dai)
3256d191bd8dSKuninori Morimoto {
3257d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3258bb13109dSLars-Peter Clausen 	int ret;
3259d191bd8dSKuninori Morimoto 
3260bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3261d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3262d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3263d191bd8dSKuninori Morimoto 		return -ENOMEM;
3264d191bd8dSKuninori Morimoto 	}
3265d191bd8dSKuninori Morimoto 
3266bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3267bb13109dSLars-Peter Clausen 	if (ret)
3268bb13109dSLars-Peter Clausen 		goto err_free;
3269bb13109dSLars-Peter Clausen 
32703d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
327198e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
32723d59400fSLars-Peter Clausen 
3273bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3274bb13109dSLars-Peter Clausen 	if (ret < 0) {
3275f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3276bb13109dSLars-Peter Clausen 		goto err_cleanup;
3277bb13109dSLars-Peter Clausen 	}
3278bb13109dSLars-Peter Clausen 
3279bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3280bb13109dSLars-Peter Clausen 
3281bb13109dSLars-Peter Clausen 	return 0;
3282bb13109dSLars-Peter Clausen 
3283bb13109dSLars-Peter Clausen err_cleanup:
3284bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3285bb13109dSLars-Peter Clausen err_free:
3286bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3287bb13109dSLars-Peter Clausen 	return ret;
3288d191bd8dSKuninori Morimoto }
3289d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3290d191bd8dSKuninori Morimoto 
3291d191bd8dSKuninori Morimoto /**
3292d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3293d191bd8dSKuninori Morimoto  *
3294628536eaSJonathan Corbet  * @dev: The device to unregister
3295d191bd8dSKuninori Morimoto  */
3296d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3297d191bd8dSKuninori Morimoto {
3298d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3299d191bd8dSKuninori Morimoto 
330034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3301d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
330298e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3303d191bd8dSKuninori Morimoto 			goto found;
3304d191bd8dSKuninori Morimoto 	}
330534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3306d191bd8dSKuninori Morimoto 	return;
3307d191bd8dSKuninori Morimoto 
3308d191bd8dSKuninori Morimoto found:
33098a978234SLiam Girdwood 	snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
331034e81ab4SLars-Peter Clausen 	snd_soc_component_del_unlocked(cmpnt);
331134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3312bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3313bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3314d191bd8dSKuninori Morimoto }
3315d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3316d191bd8dSKuninori Morimoto 
3317f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3318e2c330b9SLars-Peter Clausen {
3319e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3320e2c330b9SLars-Peter Clausen 
3321f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3322e2c330b9SLars-Peter Clausen }
3323e2c330b9SLars-Peter Clausen 
3324f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3325e2c330b9SLars-Peter Clausen {
3326e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3327e2c330b9SLars-Peter Clausen 
3328f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3329e2c330b9SLars-Peter Clausen }
3330e2c330b9SLars-Peter Clausen 
333199b04f4cSKuninori Morimoto static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd)
333299b04f4cSKuninori Morimoto {
333399b04f4cSKuninori Morimoto 	struct snd_soc_platform *platform = rtd->platform;
333499b04f4cSKuninori Morimoto 
3335d6c098a1SBrian Norris 	if (platform->driver->pcm_new)
333699b04f4cSKuninori Morimoto 		return platform->driver->pcm_new(rtd);
3337d6c098a1SBrian Norris 	else
3338d6c098a1SBrian Norris 		return 0;
333999b04f4cSKuninori Morimoto }
334099b04f4cSKuninori Morimoto 
334199b04f4cSKuninori Morimoto static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm)
334299b04f4cSKuninori Morimoto {
334399b04f4cSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = pcm->private_data;
334499b04f4cSKuninori Morimoto 	struct snd_soc_platform *platform = rtd->platform;
334599b04f4cSKuninori Morimoto 
3346d6c098a1SBrian Norris 	if (platform->driver->pcm_free)
334799b04f4cSKuninori Morimoto 		platform->driver->pcm_free(pcm);
334899b04f4cSKuninori Morimoto }
334999b04f4cSKuninori Morimoto 
3350d191bd8dSKuninori Morimoto /**
335171a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
335271a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
335371a45cdaSLars-Peter Clausen  * @platform: The platform to add
33547d1442b4SMasanari Iida  * @platform_drv: The driver for the platform
335512a48a8cSMark Brown  */
335671a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3357d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
335812a48a8cSMark Brown {
3359b37f1d12SLars-Peter Clausen 	int ret;
3360b37f1d12SLars-Peter Clausen 
3361bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3362bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3363bb13109dSLars-Peter Clausen 	if (ret)
3364bb13109dSLars-Peter Clausen 		return ret;
3365f0fba2adSLiam Girdwood 
3366f0fba2adSLiam Girdwood 	platform->dev = dev;
3367f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3368f1d45cc3SLars-Peter Clausen 
3369f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3370f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3371f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3372f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
337399b04f4cSKuninori Morimoto 	if (platform_drv->pcm_new)
337499b04f4cSKuninori Morimoto 		platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
337599b04f4cSKuninori Morimoto 	if (platform_drv->pcm_free)
337699b04f4cSKuninori Morimoto 		platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
337712a48a8cSMark Brown 
337881c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
337981c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
338081c7cfd1SLars-Peter Clausen #endif
338112a48a8cSMark Brown 
338212a48a8cSMark Brown 	mutex_lock(&client_mutex);
3383bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
338412a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
338512a48a8cSMark Brown 	mutex_unlock(&client_mutex);
338612a48a8cSMark Brown 
3387f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3388f4333203SLars-Peter Clausen 		platform->component.name);
338912a48a8cSMark Brown 
339012a48a8cSMark Brown 	return 0;
339112a48a8cSMark Brown }
339271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
339371a45cdaSLars-Peter Clausen 
339471a45cdaSLars-Peter Clausen /**
339571a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
339671a45cdaSLars-Peter Clausen  *
3397628536eaSJonathan Corbet  * @dev: The device for the platform
3398628536eaSJonathan Corbet  * @platform_drv: The driver for the platform
339971a45cdaSLars-Peter Clausen  */
340071a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
340171a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
340271a45cdaSLars-Peter Clausen {
340371a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
340471a45cdaSLars-Peter Clausen 	int ret;
340571a45cdaSLars-Peter Clausen 
340671a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
340771a45cdaSLars-Peter Clausen 
340871a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
340971a45cdaSLars-Peter Clausen 	if (platform == NULL)
341071a45cdaSLars-Peter Clausen 		return -ENOMEM;
341171a45cdaSLars-Peter Clausen 
341271a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
341371a45cdaSLars-Peter Clausen 	if (ret)
341471a45cdaSLars-Peter Clausen 		kfree(platform);
341571a45cdaSLars-Peter Clausen 
341671a45cdaSLars-Peter Clausen 	return ret;
341771a45cdaSLars-Peter Clausen }
341812a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
341912a48a8cSMark Brown 
342012a48a8cSMark Brown /**
342171a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
342271a45cdaSLars-Peter Clausen  * @platform: the platform to remove
342371a45cdaSLars-Peter Clausen  */
342471a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
342571a45cdaSLars-Peter Clausen {
3426b37f1d12SLars-Peter Clausen 
342771a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
342871a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3429bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
343071a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
343171a45cdaSLars-Peter Clausen 
343271a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3433f4333203SLars-Peter Clausen 		platform->component.name);
3434decc27b0SDaniel Mack 
3435decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
343671a45cdaSLars-Peter Clausen }
343771a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
343871a45cdaSLars-Peter Clausen 
343971a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
344071a45cdaSLars-Peter Clausen {
344171a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
344271a45cdaSLars-Peter Clausen 
344334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
344471a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
344534e81ab4SLars-Peter Clausen 		if (dev == platform->dev) {
344634e81ab4SLars-Peter Clausen 			mutex_unlock(&client_mutex);
344771a45cdaSLars-Peter Clausen 			return platform;
344871a45cdaSLars-Peter Clausen 		}
344934e81ab4SLars-Peter Clausen 	}
345034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
345171a45cdaSLars-Peter Clausen 
345271a45cdaSLars-Peter Clausen 	return NULL;
345371a45cdaSLars-Peter Clausen }
345471a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
345571a45cdaSLars-Peter Clausen 
345671a45cdaSLars-Peter Clausen /**
345712a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
345812a48a8cSMark Brown  *
3459628536eaSJonathan Corbet  * @dev: platform to unregister
346012a48a8cSMark Brown  */
3461f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
346212a48a8cSMark Brown {
3463f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3464f0fba2adSLiam Girdwood 
346571a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
346671a45cdaSLars-Peter Clausen 	if (!platform)
3467f0fba2adSLiam Girdwood 		return;
3468f0fba2adSLiam Girdwood 
346971a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3470f0fba2adSLiam Girdwood 	kfree(platform);
347112a48a8cSMark Brown }
347212a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
347312a48a8cSMark Brown 
3474151ab22cSMark Brown static u64 codec_format_map[] = {
3475151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3476151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3477151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3478151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3479151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3480151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3481151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3482151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3483151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3484151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3485151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3486151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3487151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3488151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3489151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3490151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3491151ab22cSMark Brown };
3492151ab22cSMark Brown 
3493151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3494151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3495151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3496151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3497151ab22cSMark Brown  */
3498151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3499151ab22cSMark Brown {
3500151ab22cSMark Brown 	int i;
3501151ab22cSMark Brown 
3502151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3503151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3504151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3505151ab22cSMark Brown }
3506151ab22cSMark Brown 
3507f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3508f1d45cc3SLars-Peter Clausen {
3509f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3510f1d45cc3SLars-Peter Clausen 
3511f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3512f1d45cc3SLars-Peter Clausen }
3513f1d45cc3SLars-Peter Clausen 
3514f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3515f1d45cc3SLars-Peter Clausen {
3516f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3517f1d45cc3SLars-Peter Clausen 
3518f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3519f1d45cc3SLars-Peter Clausen }
3520f1d45cc3SLars-Peter Clausen 
35219178feb4SKuninori Morimoto static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
35229178feb4SKuninori Morimoto {
35239178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
35249178feb4SKuninori Morimoto 
35259178feb4SKuninori Morimoto 	return codec->driver->suspend(codec);
35269178feb4SKuninori Morimoto }
35279178feb4SKuninori Morimoto 
35289178feb4SKuninori Morimoto static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
35299178feb4SKuninori Morimoto {
35309178feb4SKuninori Morimoto 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
35319178feb4SKuninori Morimoto 
35329178feb4SKuninori Morimoto 	return codec->driver->resume(codec);
35339178feb4SKuninori Morimoto }
35349178feb4SKuninori Morimoto 
3535e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3536e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3537e2c330b9SLars-Peter Clausen {
3538e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3539e2c330b9SLars-Peter Clausen 
3540e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3541e2c330b9SLars-Peter Clausen }
3542e2c330b9SLars-Peter Clausen 
3543e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3544e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3545e2c330b9SLars-Peter Clausen {
3546e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3547e2c330b9SLars-Peter Clausen 
3548e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3549e2c330b9SLars-Peter Clausen 
3550e2c330b9SLars-Peter Clausen 	return 0;
3551e2c330b9SLars-Peter Clausen }
3552e2c330b9SLars-Peter Clausen 
355368f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
355468f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
355568f831c2SLars-Peter Clausen {
355668f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
355768f831c2SLars-Peter Clausen 
355868f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
355968f831c2SLars-Peter Clausen }
356068f831c2SLars-Peter Clausen 
35610d0cf00aSMark Brown /**
35620d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
35630d0cf00aSMark Brown  *
3564628536eaSJonathan Corbet  * @dev: The parent device for this codec
3565628536eaSJonathan Corbet  * @codec_drv: Codec driver
3566628536eaSJonathan Corbet  * @dai_drv: The associated DAI driver
3567628536eaSJonathan Corbet  * @num_dai: Number of DAIs
35680d0cf00aSMark Brown  */
3569f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3570001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3571001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3572001ae4c0SMark Brown 			   int num_dai)
35730d0cf00aSMark Brown {
35744890140fSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3575f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3576bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3577f0fba2adSLiam Girdwood 	int ret, i;
3578151ab22cSMark Brown 
3579f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
35800d0cf00aSMark Brown 
3581f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3582f0fba2adSLiam Girdwood 	if (codec == NULL)
3583f0fba2adSLiam Girdwood 		return -ENOMEM;
35840d0cf00aSMark Brown 
3585f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3586ce0fc93aSLars-Peter Clausen 
3587bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3588bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3589bb13109dSLars-Peter Clausen 	if (ret)
3590bb13109dSLars-Peter Clausen 		goto err_free;
3591151ab22cSMark Brown 
3592f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3593f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3594f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3595f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
35969178feb4SKuninori Morimoto 	if (codec_drv->suspend)
35979178feb4SKuninori Morimoto 		codec->component.suspend = snd_soc_codec_drv_suspend;
35989178feb4SKuninori Morimoto 	if (codec_drv->resume)
35999178feb4SKuninori Morimoto 		codec->component.resume = snd_soc_codec_drv_resume;
3600e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3601e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3602e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3603e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
36043d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
36054890140fSLars-Peter Clausen 
36064890140fSLars-Peter Clausen 	dapm = snd_soc_codec_get_dapm(codec);
36074890140fSLars-Peter Clausen 	dapm->idle_bias_off = codec_drv->idle_bias_off;
36084890140fSLars-Peter Clausen 	dapm->suspend_bias_off = codec_drv->suspend_bias_off;
360914e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
36104890140fSLars-Peter Clausen 		dapm->seq_notifier = codec_drv->seq_notifier;
361168f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
36124890140fSLars-Peter Clausen 		dapm->set_bias_level = snd_soc_codec_set_bias_level;
3613f0fba2adSLiam Girdwood 	codec->dev = dev;
3614f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3615e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3616f0fba2adSLiam Girdwood 
361781c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
361881c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
361981c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
362081c7cfd1SLars-Peter Clausen #endif
3621a39f75f7SXiubo Li 
3622a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3623886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3624a39f75f7SXiubo Li 
3625f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3626f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3627f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3628f0fba2adSLiam Girdwood 	}
3629f0fba2adSLiam Girdwood 
3630bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3631bb13109dSLars-Peter Clausen 	if (ret < 0) {
3632f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3633bb13109dSLars-Peter Clausen 		goto err_cleanup;
3634bb13109dSLars-Peter Clausen 	}
3635bb13109dSLars-Peter Clausen 
3636bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3637bb13109dSLars-Peter Clausen 		dai->codec = codec;
3638bb13109dSLars-Peter Clausen 
3639054880feSMark Brown 	mutex_lock(&client_mutex);
3640bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3641054880feSMark Brown 	list_add(&codec->list, &codec_list);
3642054880feSMark Brown 	mutex_unlock(&client_mutex);
3643054880feSMark Brown 
3644f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3645f4333203SLars-Peter Clausen 		codec->component.name);
36460d0cf00aSMark Brown 	return 0;
3647f0fba2adSLiam Girdwood 
3648bb13109dSLars-Peter Clausen err_cleanup:
3649bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3650bb13109dSLars-Peter Clausen err_free:
3651f0fba2adSLiam Girdwood 	kfree(codec);
3652f0fba2adSLiam Girdwood 	return ret;
36530d0cf00aSMark Brown }
36540d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
36550d0cf00aSMark Brown 
36560d0cf00aSMark Brown /**
36570d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
36580d0cf00aSMark Brown  *
3659628536eaSJonathan Corbet  * @dev: codec to unregister
36600d0cf00aSMark Brown  */
3661f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
36620d0cf00aSMark Brown {
3663f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3664f0fba2adSLiam Girdwood 
366534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3666f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3667f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3668f0fba2adSLiam Girdwood 			goto found;
3669f0fba2adSLiam Girdwood 	}
367034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
3671f0fba2adSLiam Girdwood 	return;
3672f0fba2adSLiam Girdwood 
3673f0fba2adSLiam Girdwood found:
36740d0cf00aSMark Brown 	list_del(&codec->list);
3675bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
36760d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
36770d0cf00aSMark Brown 
3678f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3679f4333203SLars-Peter Clausen 			codec->component.name);
3680f0fba2adSLiam Girdwood 
3681bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
36827a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3683f0fba2adSLiam Girdwood 	kfree(codec);
36840d0cf00aSMark Brown }
36850d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
36860d0cf00aSMark Brown 
3687bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3688b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3689bec4fa05SStephen Warren 			       const char *propname)
3690bec4fa05SStephen Warren {
3691b07609ceSKuninori Morimoto 	struct device_node *np;
3692bec4fa05SStephen Warren 	int ret;
3693bec4fa05SStephen Warren 
36947e07e7c0STushar Behera 	if (!card->dev) {
36957e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
36967e07e7c0STushar Behera 		return -EINVAL;
36977e07e7c0STushar Behera 	}
36987e07e7c0STushar Behera 
36997e07e7c0STushar Behera 	np = card->dev->of_node;
37007e07e7c0STushar Behera 
3701bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3702bec4fa05SStephen Warren 	/*
3703bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3704bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3705bec4fa05SStephen Warren 	 * snd_soc_register_card.
3706bec4fa05SStephen Warren 	 */
3707bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3708bec4fa05SStephen Warren 		dev_err(card->dev,
3709f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3710bec4fa05SStephen Warren 			propname, ret);
3711bec4fa05SStephen Warren 		return ret;
3712bec4fa05SStephen Warren 	}
3713bec4fa05SStephen Warren 
3714bec4fa05SStephen Warren 	return 0;
3715bec4fa05SStephen Warren }
3716b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3717bec4fa05SStephen Warren 
37189a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
37199a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
37209a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
37219a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
37229a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
37239a6d4860SXiubo Li };
37249a6d4860SXiubo Li 
372521efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
37269a6d4860SXiubo Li 					  const char *propname)
37279a6d4860SXiubo Li {
372821efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
37299a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
37309a6d4860SXiubo Li 	const char *template, *wname;
37319a6d4860SXiubo Li 	int i, j, num_widgets, ret;
37329a6d4860SXiubo Li 
37339a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
37349a6d4860SXiubo Li 	if (num_widgets < 0) {
37359a6d4860SXiubo Li 		dev_err(card->dev,
37369a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
37379a6d4860SXiubo Li 		return -EINVAL;
37389a6d4860SXiubo Li 	}
37399a6d4860SXiubo Li 	if (num_widgets & 1) {
37409a6d4860SXiubo Li 		dev_err(card->dev,
37419a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
37429a6d4860SXiubo Li 		return -EINVAL;
37439a6d4860SXiubo Li 	}
37449a6d4860SXiubo Li 
37459a6d4860SXiubo Li 	num_widgets /= 2;
37469a6d4860SXiubo Li 	if (!num_widgets) {
37479a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
37489a6d4860SXiubo Li 			propname);
37499a6d4860SXiubo Li 		return -EINVAL;
37509a6d4860SXiubo Li 	}
37519a6d4860SXiubo Li 
37529a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
37539a6d4860SXiubo Li 			       GFP_KERNEL);
37549a6d4860SXiubo Li 	if (!widgets) {
37559a6d4860SXiubo Li 		dev_err(card->dev,
37569a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
37579a6d4860SXiubo Li 		return -ENOMEM;
37589a6d4860SXiubo Li 	}
37599a6d4860SXiubo Li 
37609a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
37619a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
37629a6d4860SXiubo Li 			2 * i, &template);
37639a6d4860SXiubo Li 		if (ret) {
37649a6d4860SXiubo Li 			dev_err(card->dev,
37659a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
37669a6d4860SXiubo Li 				propname, 2 * i, ret);
37679a6d4860SXiubo Li 			return -EINVAL;
37689a6d4860SXiubo Li 		}
37699a6d4860SXiubo Li 
37709a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
37719a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
37729a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
37739a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
37749a6d4860SXiubo Li 				break;
37759a6d4860SXiubo Li 			}
37769a6d4860SXiubo Li 		}
37779a6d4860SXiubo Li 
37789a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
37799a6d4860SXiubo Li 			dev_err(card->dev,
37809a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
37819a6d4860SXiubo Li 				template);
37829a6d4860SXiubo Li 			return -EINVAL;
37839a6d4860SXiubo Li 		}
37849a6d4860SXiubo Li 
37859a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
37869a6d4860SXiubo Li 						    (2 * i) + 1,
37879a6d4860SXiubo Li 						    &wname);
37889a6d4860SXiubo Li 		if (ret) {
37899a6d4860SXiubo Li 			dev_err(card->dev,
37909a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
37919a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
37929a6d4860SXiubo Li 			return -EINVAL;
37939a6d4860SXiubo Li 		}
37949a6d4860SXiubo Li 
37959a6d4860SXiubo Li 		widgets[i].name = wname;
37969a6d4860SXiubo Li 	}
37979a6d4860SXiubo Li 
3798f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3799f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
38009a6d4860SXiubo Li 
38019a6d4860SXiubo Li 	return 0;
38029a6d4860SXiubo Li }
380321efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
38049a6d4860SXiubo Li 
38056131084aSJyri Sarha static int snd_soc_of_get_slot_mask(struct device_node *np,
38066131084aSJyri Sarha 				    const char *prop_name,
38076131084aSJyri Sarha 				    unsigned int *mask)
38086131084aSJyri Sarha {
38096131084aSJyri Sarha 	u32 val;
38106c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
38116131084aSJyri Sarha 	int i;
38126131084aSJyri Sarha 
38136131084aSJyri Sarha 	if (!of_slot_mask)
38146131084aSJyri Sarha 		return 0;
38156131084aSJyri Sarha 	val /= sizeof(u32);
38166131084aSJyri Sarha 	for (i = 0; i < val; i++)
38176131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
38186131084aSJyri Sarha 			*mask |= (1 << i);
38196131084aSJyri Sarha 
38206131084aSJyri Sarha 	return val;
38216131084aSJyri Sarha }
38226131084aSJyri Sarha 
382389c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
38246131084aSJyri Sarha 			      unsigned int *tx_mask,
38256131084aSJyri Sarha 			      unsigned int *rx_mask,
382689c67857SXiubo Li 			      unsigned int *slots,
382789c67857SXiubo Li 			      unsigned int *slot_width)
382889c67857SXiubo Li {
382989c67857SXiubo Li 	u32 val;
383089c67857SXiubo Li 	int ret;
383189c67857SXiubo Li 
38326131084aSJyri Sarha 	if (tx_mask)
38336131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
38346131084aSJyri Sarha 	if (rx_mask)
38356131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
38366131084aSJyri Sarha 
383789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
383889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
383989c67857SXiubo Li 		if (ret)
384089c67857SXiubo Li 			return ret;
384189c67857SXiubo Li 
384289c67857SXiubo Li 		if (slots)
384389c67857SXiubo Li 			*slots = val;
384489c67857SXiubo Li 	}
384589c67857SXiubo Li 
384689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
384789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
384889c67857SXiubo Li 		if (ret)
384989c67857SXiubo Li 			return ret;
385089c67857SXiubo Li 
385189c67857SXiubo Li 		if (slot_width)
385289c67857SXiubo Li 			*slot_width = val;
385389c67857SXiubo Li 	}
385489c67857SXiubo Li 
385589c67857SXiubo Li 	return 0;
385689c67857SXiubo Li }
385789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
385889c67857SXiubo Li 
3859440a3006SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
38605e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
38615e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
38625e3cdaa2SKuninori Morimoto 				   const char *propname)
38635e3cdaa2SKuninori Morimoto {
3864440a3006SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
38655e3cdaa2SKuninori Morimoto 	const char *str;
38665e3cdaa2SKuninori Morimoto 	int ret;
38675e3cdaa2SKuninori Morimoto 
38685e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
38695e3cdaa2SKuninori Morimoto 	if (ret < 0) {
38705e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
38715e3cdaa2SKuninori Morimoto 		return;
38725e3cdaa2SKuninori Morimoto 	}
38735e3cdaa2SKuninori Morimoto 
38745e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
38755e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
38765e3cdaa2SKuninori Morimoto }
3877440a3006SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
38785e3cdaa2SKuninori Morimoto 
38792bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3880a4a54dd5SStephen Warren 				   const char *propname)
3881a4a54dd5SStephen Warren {
38822bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3883e3b1e6a1SMark Brown 	int num_routes;
3884a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3885a4a54dd5SStephen Warren 	int i, ret;
3886a4a54dd5SStephen Warren 
3887a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3888c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
388910e8aa9aSMichał Mirosław 		dev_err(card->dev,
389010e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
389110e8aa9aSMichał Mirosław 			propname);
3892a4a54dd5SStephen Warren 		return -EINVAL;
3893a4a54dd5SStephen Warren 	}
3894a4a54dd5SStephen Warren 	num_routes /= 2;
3895a4a54dd5SStephen Warren 	if (!num_routes) {
3896f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3897a4a54dd5SStephen Warren 			propname);
3898a4a54dd5SStephen Warren 		return -EINVAL;
3899a4a54dd5SStephen Warren 	}
3900a4a54dd5SStephen Warren 
3901e3b1e6a1SMark Brown 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3902a4a54dd5SStephen Warren 			      GFP_KERNEL);
3903a4a54dd5SStephen Warren 	if (!routes) {
3904a4a54dd5SStephen Warren 		dev_err(card->dev,
3905f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3906a4a54dd5SStephen Warren 		return -EINVAL;
3907a4a54dd5SStephen Warren 	}
3908a4a54dd5SStephen Warren 
3909a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3910a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3911e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3912a4a54dd5SStephen Warren 		if (ret) {
3913c871bd0bSMark Brown 			dev_err(card->dev,
3914c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3915c871bd0bSMark Brown 				propname, 2 * i, ret);
3916a4a54dd5SStephen Warren 			return -EINVAL;
3917a4a54dd5SStephen Warren 		}
3918a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3919e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3920a4a54dd5SStephen Warren 		if (ret) {
3921a4a54dd5SStephen Warren 			dev_err(card->dev,
3922c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3923c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3924a4a54dd5SStephen Warren 			return -EINVAL;
3925a4a54dd5SStephen Warren 		}
3926a4a54dd5SStephen Warren 	}
3927a4a54dd5SStephen Warren 
3928f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3929f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3930a4a54dd5SStephen Warren 
3931a4a54dd5SStephen Warren 	return 0;
3932a4a54dd5SStephen Warren }
39332bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3934a4a54dd5SStephen Warren 
3935a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3936389cb834SJyri Sarha 				     const char *prefix,
3937389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3938389cb834SJyri Sarha 				     struct device_node **framemaster)
3939a7930ed4SKuninori Morimoto {
3940a7930ed4SKuninori Morimoto 	int ret, i;
3941a7930ed4SKuninori Morimoto 	char prop[128];
3942a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3943a7930ed4SKuninori Morimoto 	int bit, frame;
3944a7930ed4SKuninori Morimoto 	const char *str;
3945a7930ed4SKuninori Morimoto 	struct {
3946a7930ed4SKuninori Morimoto 		char *name;
3947a7930ed4SKuninori Morimoto 		unsigned int val;
3948a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3949a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3950a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3951a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3952a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3953a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3954a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3955a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3956a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3957a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3958a7930ed4SKuninori Morimoto 	};
3959a7930ed4SKuninori Morimoto 
3960a7930ed4SKuninori Morimoto 	if (!prefix)
3961a7930ed4SKuninori Morimoto 		prefix = "";
3962a7930ed4SKuninori Morimoto 
3963a7930ed4SKuninori Morimoto 	/*
3964a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3965a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3966a7930ed4SKuninori Morimoto 	 */
3967a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3968a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3969a7930ed4SKuninori Morimoto 	if (ret == 0) {
3970a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3971a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3972a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3973a7930ed4SKuninori Morimoto 				break;
3974a7930ed4SKuninori Morimoto 			}
3975a7930ed4SKuninori Morimoto 		}
3976a7930ed4SKuninori Morimoto 	}
3977a7930ed4SKuninori Morimoto 
3978a7930ed4SKuninori Morimoto 	/*
39798c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3980a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3981a7930ed4SKuninori Morimoto 	 */
39828c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
398351930295SJulia Lawall 	if (of_property_read_bool(np, prop))
39848c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
39858c2d6a9fSKuninori Morimoto 	else
39868c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3987a7930ed4SKuninori Morimoto 
3988a7930ed4SKuninori Morimoto 	/*
3989a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3990a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3991a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3992a7930ed4SKuninori Morimoto 	 */
3993a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3994a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3995a7930ed4SKuninori Morimoto 
3996a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3997a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3998a7930ed4SKuninori Morimoto 
3999a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4000a7930ed4SKuninori Morimoto 	case 0x11:
4001a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4002a7930ed4SKuninori Morimoto 		break;
4003a7930ed4SKuninori Morimoto 	case 0x10:
4004a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4005a7930ed4SKuninori Morimoto 		break;
4006a7930ed4SKuninori Morimoto 	case 0x01:
4007a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4008a7930ed4SKuninori Morimoto 		break;
4009a7930ed4SKuninori Morimoto 	default:
4010a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4011a7930ed4SKuninori Morimoto 		break;
4012a7930ed4SKuninori Morimoto 	}
4013a7930ed4SKuninori Morimoto 
4014a7930ed4SKuninori Morimoto 	/*
4015a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4016a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4017a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4018a7930ed4SKuninori Morimoto 	 */
4019a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4020a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4021389cb834SJyri Sarha 	if (bit && bitclkmaster)
4022389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4023a7930ed4SKuninori Morimoto 
4024a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4025a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4026389cb834SJyri Sarha 	if (frame && framemaster)
4027389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4028a7930ed4SKuninori Morimoto 
4029a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4030a7930ed4SKuninori Morimoto 	case 0x11:
4031a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4032a7930ed4SKuninori Morimoto 		break;
4033a7930ed4SKuninori Morimoto 	case 0x10:
4034a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4035a7930ed4SKuninori Morimoto 		break;
4036a7930ed4SKuninori Morimoto 	case 0x01:
4037a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4038a7930ed4SKuninori Morimoto 		break;
4039a7930ed4SKuninori Morimoto 	default:
4040a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4041a7930ed4SKuninori Morimoto 		break;
4042a7930ed4SKuninori Morimoto 	}
4043a7930ed4SKuninori Morimoto 
4044a7930ed4SKuninori Morimoto 	return format;
4045a7930ed4SKuninori Morimoto }
4046a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4047a7930ed4SKuninori Morimoto 
4048*a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
4049*a180e8b9SKuninori Morimoto {
4050*a180e8b9SKuninori Morimoto 	struct snd_soc_component *pos;
4051*a180e8b9SKuninori Morimoto 	struct device_node *node;
4052*a180e8b9SKuninori Morimoto 	int ret;
4053*a180e8b9SKuninori Morimoto 
4054*a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
4055*a180e8b9SKuninori Morimoto 
4056*a180e8b9SKuninori Morimoto 	/*
4057*a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
4058*a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
4059*a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
4060*a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
4061*a180e8b9SKuninori Morimoto 	 */
4062*a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
4063*a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
4064*a180e8b9SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4065*a180e8b9SKuninori Morimoto 		struct device_node *component_of_node = pos->dev->of_node;
4066*a180e8b9SKuninori Morimoto 
4067*a180e8b9SKuninori Morimoto 		if (!component_of_node && pos->dev->parent)
4068*a180e8b9SKuninori Morimoto 			component_of_node = pos->dev->parent->of_node;
4069*a180e8b9SKuninori Morimoto 
4070*a180e8b9SKuninori Morimoto 		if (component_of_node != node)
4071*a180e8b9SKuninori Morimoto 			continue;
4072*a180e8b9SKuninori Morimoto 
4073*a180e8b9SKuninori Morimoto 		if (pos->driver->of_xlate_dai_id)
4074*a180e8b9SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_id(pos, ep);
4075*a180e8b9SKuninori Morimoto 
4076*a180e8b9SKuninori Morimoto 		break;
4077*a180e8b9SKuninori Morimoto 	}
4078*a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
4079*a180e8b9SKuninori Morimoto 
4080*a180e8b9SKuninori Morimoto 	return ret;
4081*a180e8b9SKuninori Morimoto }
4082*a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4083*a180e8b9SKuninori Morimoto 
40841ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
4085cb470087SKuninori Morimoto 				const char **dai_name)
4086cb470087SKuninori Morimoto {
4087cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
40883e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
408993b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
4090cb470087SKuninori Morimoto 
4091cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4092cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
40933e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
40943e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
40953e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
40963e0aa8d8SJyri Sarha 
40973e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
4098cb470087SKuninori Morimoto 			continue;
4099cb470087SKuninori Morimoto 
41006833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
410193b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
410293b0f3eeSJean-Francois Moine 							     args,
410393b0f3eeSJean-Francois Moine 							     dai_name);
41046833c452SKuninori Morimoto 		} else {
41056833c452SKuninori Morimoto 			int id = -1;
41066833c452SKuninori Morimoto 
410793b0f3eeSJean-Francois Moine 			switch (args->args_count) {
41086833c452SKuninori Morimoto 			case 0:
41096833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
41106833c452SKuninori Morimoto 				break;
41116833c452SKuninori Morimoto 			case 1:
411293b0f3eeSJean-Francois Moine 				id = args->args[0];
41136833c452SKuninori Morimoto 				break;
41146833c452SKuninori Morimoto 			default:
41156833c452SKuninori Morimoto 				/* not supported */
4116cb470087SKuninori Morimoto 				break;
4117cb470087SKuninori Morimoto 			}
4118cb470087SKuninori Morimoto 
41196833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
41206833c452SKuninori Morimoto 				ret = -EINVAL;
41213dcba280SNicolin Chen 				continue;
41226833c452SKuninori Morimoto 			}
4123e41975edSXiubo Li 
4124e41975edSXiubo Li 			ret = 0;
4125e41975edSXiubo Li 
4126e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4127e41975edSXiubo Li 			if (!*dai_name)
4128e41975edSXiubo Li 				*dai_name = pos->name;
41296833c452SKuninori Morimoto 		}
41306833c452SKuninori Morimoto 
4131cb470087SKuninori Morimoto 		break;
4132cb470087SKuninori Morimoto 	}
4133cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
413493b0f3eeSJean-Francois Moine 	return ret;
413593b0f3eeSJean-Francois Moine }
41361ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
413793b0f3eeSJean-Francois Moine 
413893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
413993b0f3eeSJean-Francois Moine 			    const char **dai_name)
414093b0f3eeSJean-Francois Moine {
414193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
414293b0f3eeSJean-Francois Moine 	int ret;
414393b0f3eeSJean-Francois Moine 
414493b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
414593b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
414693b0f3eeSJean-Francois Moine 	if (ret)
414793b0f3eeSJean-Francois Moine 		return ret;
414893b0f3eeSJean-Francois Moine 
414993b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
4150cb470087SKuninori Morimoto 
4151cb470087SKuninori Morimoto 	of_node_put(args.np);
4152cb470087SKuninori Morimoto 
4153cb470087SKuninori Morimoto 	return ret;
4154cb470087SKuninori Morimoto }
4155cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4156cb470087SKuninori Morimoto 
415793b0f3eeSJean-Francois Moine /*
415893b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
415993b0f3eeSJean-Francois Moine  * @dev: Card device
416093b0f3eeSJean-Francois Moine  * @of_node: Device node
416193b0f3eeSJean-Francois Moine  * @dai_link: DAI link
416293b0f3eeSJean-Francois Moine  *
416393b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
416493b0f3eeSJean-Francois Moine  * 'sound-dai'.
416593b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
416693b0f3eeSJean-Francois Moine  * The device nodes in the array (of_node) must be dereferenced by the caller.
416793b0f3eeSJean-Francois Moine  *
416893b0f3eeSJean-Francois Moine  * Returns 0 for success
416993b0f3eeSJean-Francois Moine  */
417093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
417193b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
417293b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
417393b0f3eeSJean-Francois Moine {
417493b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
417593b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
417693b0f3eeSJean-Francois Moine 	char *name;
417793b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
417893b0f3eeSJean-Francois Moine 
417993b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
418093b0f3eeSJean-Francois Moine 	name = "sound-dai";
418193b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
418293b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
418393b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
418493b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
418593b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
418693b0f3eeSJean-Francois Moine 		else
418793b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
418893b0f3eeSJean-Francois Moine 		return num_codecs;
418993b0f3eeSJean-Francois Moine 	}
419093b0f3eeSJean-Francois Moine 	component = devm_kzalloc(dev,
419193b0f3eeSJean-Francois Moine 				 sizeof *component * num_codecs,
419293b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
419393b0f3eeSJean-Francois Moine 	if (!component)
419493b0f3eeSJean-Francois Moine 		return -ENOMEM;
419593b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
419693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
419793b0f3eeSJean-Francois Moine 
419893b0f3eeSJean-Francois Moine 	/* Parse the list */
419993b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
420093b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
420193b0f3eeSJean-Francois Moine 	     index++, component++) {
420293b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
420393b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
420493b0f3eeSJean-Francois Moine 						  index, &args);
420593b0f3eeSJean-Francois Moine 		if (ret)
420693b0f3eeSJean-Francois Moine 			goto err;
420793b0f3eeSJean-Francois Moine 		component->of_node = args.np;
420893b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
420993b0f3eeSJean-Francois Moine 		if (ret < 0)
421093b0f3eeSJean-Francois Moine 			goto err;
421193b0f3eeSJean-Francois Moine 	}
421293b0f3eeSJean-Francois Moine 	return 0;
421393b0f3eeSJean-Francois Moine err:
421493b0f3eeSJean-Francois Moine 	for (index = 0, component = dai_link->codecs;
421593b0f3eeSJean-Francois Moine 	     index < dai_link->num_codecs;
421693b0f3eeSJean-Francois Moine 	     index++, component++) {
421793b0f3eeSJean-Francois Moine 		if (!component->of_node)
421893b0f3eeSJean-Francois Moine 			break;
421993b0f3eeSJean-Francois Moine 		of_node_put(component->of_node);
422093b0f3eeSJean-Francois Moine 		component->of_node = NULL;
422193b0f3eeSJean-Francois Moine 	}
422293b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
422393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
422493b0f3eeSJean-Francois Moine 	return ret;
422593b0f3eeSJean-Francois Moine }
422693b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
422793b0f3eeSJean-Francois Moine 
4228c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4229db2a4165SFrank Mandarino {
42306553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
4231fb257897SMark Brown 	snd_soc_util_init();
4232fb257897SMark Brown 
4233db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4234db2a4165SFrank Mandarino }
42354abe8e16SMark Brown module_init(snd_soc_init);
4236db2a4165SFrank Mandarino 
42377d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4238db2a4165SFrank Mandarino {
4239fb257897SMark Brown 	snd_soc_util_exit();
42406553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
4241fb257897SMark Brown 
4242db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4243db2a4165SFrank Mandarino }
4244db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4245db2a4165SFrank Mandarino 
4246db2a4165SFrank Mandarino /* Module information */
4247d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4248db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4249db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4250db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4251