xref: /openbmc/linux/sound/soc/soc-core.c (revision e73f3de5c5eb5285a1b1d8eed111eb229702b20f)
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>
37741a509fSMarkus Pargmann #include <linux/gpio.h>
38741a509fSMarkus Pargmann #include <linux/of_gpio.h>
39474828a4SMarek Vasut #include <sound/ac97_codec.h>
40db2a4165SFrank Mandarino #include <sound/core.h>
413028eb8cSMark Brown #include <sound/jack.h>
42db2a4165SFrank Mandarino #include <sound/pcm.h>
43db2a4165SFrank Mandarino #include <sound/pcm_params.h>
44db2a4165SFrank Mandarino #include <sound/soc.h>
4501d7584cSLiam Girdwood #include <sound/soc-dpcm.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 
72741a509fSMarkus Pargmann struct snd_ac97_reset_cfg {
73741a509fSMarkus Pargmann 	struct pinctrl *pctl;
74741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_reset;
75741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_warm_reset;
76741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_run;
77741a509fSMarkus Pargmann 	int gpio_sdata;
78741a509fSMarkus Pargmann 	int gpio_sync;
79741a509fSMarkus Pargmann 	int gpio_reset;
80741a509fSMarkus Pargmann };
81741a509fSMarkus Pargmann 
822bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
832bc9a81eSDimitris Papastamos  * a particular given value */
842bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
852bc9a81eSDimitris Papastamos {
862bc9a81eSDimitris Papastamos 	int c = 0;
872bc9a81eSDimitris Papastamos 	int i;
882bc9a81eSDimitris Papastamos 
892bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
902bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
912bc9a81eSDimitris Papastamos 			break;
922bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
932bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
942bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
952bc9a81eSDimitris Papastamos 	else
962bc9a81eSDimitris Papastamos 		c /= 8;
972bc9a81eSDimitris Papastamos 	return c;
982bc9a81eSDimitris Papastamos }
992bc9a81eSDimitris Papastamos 
10013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
10113fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
10213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
10313fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
1042624d5faSMark Brown {
10500b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
10600b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
10713fd179fSDimitris Papastamos 	int ret;
10813fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
10913fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
11013fd179fSDimitris Papastamos 
11113fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
11213fd179fSDimitris Papastamos 	 * try to abuse this function */
11313fd179fSDimitris Papastamos 	WARN_ON(len > 63);
11413fd179fSDimitris Papastamos 
11513fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
11613fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
11713fd179fSDimitris Papastamos 		return -EINVAL;
11813fd179fSDimitris Papastamos 
11913fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
12013fd179fSDimitris Papastamos 	if (ret < 0) {
12113fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
12213fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
12313fd179fSDimitris Papastamos 	} else {
12413fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
12513fd179fSDimitris Papastamos 	}
12613fd179fSDimitris Papastamos 
12713fd179fSDimitris Papastamos 	/* prepare the buffer */
12813fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
12913fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
13013fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
13113fd179fSDimitris Papastamos 
13213fd179fSDimitris Papastamos 	return 0;
13313fd179fSDimitris Papastamos }
13413fd179fSDimitris Papastamos 
13513fd179fSDimitris Papastamos /* codec register dump */
13613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
13713fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
13813fd179fSDimitris Papastamos {
13913fd179fSDimitris Papastamos 	int i, step = 1;
1402bc9a81eSDimitris Papastamos 	int wordsize, regsize;
14113fd179fSDimitris Papastamos 	int len;
14213fd179fSDimitris Papastamos 	size_t total = 0;
14313fd179fSDimitris Papastamos 	loff_t p = 0;
1442bc9a81eSDimitris Papastamos 
14500b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
14600b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1472624d5faSMark Brown 
14813fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
14913fd179fSDimitris Papastamos 
150f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1512624d5faSMark Brown 		return 0;
1522624d5faSMark Brown 
153f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
154f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1552624d5faSMark Brown 
156f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
15713fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
15813fd179fSDimitris Papastamos 		 * entries for the default case */
15913fd179fSDimitris Papastamos 		if (p >= pos) {
16013fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1612624d5faSMark Brown 				break;
16213fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
16313fd179fSDimitris Papastamos 			total += len;
16413fd179fSDimitris Papastamos 		}
16513fd179fSDimitris Papastamos 		p += len;
16613fd179fSDimitris Papastamos 	}
1672624d5faSMark Brown 
16813fd179fSDimitris Papastamos 	total = min(total, count - 1);
1692624d5faSMark Brown 
17013fd179fSDimitris Papastamos 	return total;
1712624d5faSMark Brown }
17213fd179fSDimitris Papastamos 
1732624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1742624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1752624d5faSMark Brown {
17636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
177f0fba2adSLiam Girdwood 
17813fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1792624d5faSMark Brown }
1802624d5faSMark Brown 
1812624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1822624d5faSMark Brown 
183dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
184dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
185dbe21408SMark Brown {
18636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
187dbe21408SMark Brown 
188f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
189dbe21408SMark Brown }
190dbe21408SMark Brown 
191dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
192dbe21408SMark Brown 			       struct device_attribute *attr,
193dbe21408SMark Brown 			       const char *buf, size_t count)
194dbe21408SMark Brown {
19536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
196c593b520SMark Brown 	int ret;
197dbe21408SMark Brown 
198b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
199c593b520SMark Brown 	if (ret)
200c593b520SMark Brown 		return ret;
201dbe21408SMark Brown 
202dbe21408SMark Brown 	return count;
203dbe21408SMark Brown }
204dbe21408SMark Brown 
205dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206dbe21408SMark Brown 
2072624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2082624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2092624d5faSMark Brown 				   size_t count, loff_t *ppos)
2102624d5faSMark Brown {
2112624d5faSMark Brown 	ssize_t ret;
2122624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
21313fd179fSDimitris Papastamos 	char *buf;
21413fd179fSDimitris Papastamos 
21513fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
21613fd179fSDimitris Papastamos 		return -EINVAL;
21713fd179fSDimitris Papastamos 
21813fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2192624d5faSMark Brown 	if (!buf)
2202624d5faSMark Brown 		return -ENOMEM;
22113fd179fSDimitris Papastamos 
22213fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
22313fd179fSDimitris Papastamos 	if (ret >= 0) {
22413fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
22513fd179fSDimitris Papastamos 			kfree(buf);
22613fd179fSDimitris Papastamos 			return -EFAULT;
22713fd179fSDimitris Papastamos 		}
22813fd179fSDimitris Papastamos 		*ppos += ret;
22913fd179fSDimitris Papastamos 	}
23013fd179fSDimitris Papastamos 
2312624d5faSMark Brown 	kfree(buf);
2322624d5faSMark Brown 	return ret;
2332624d5faSMark Brown }
2342624d5faSMark Brown 
2352624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2362624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2372624d5faSMark Brown {
2382624d5faSMark Brown 	char buf[32];
23934e268d8SStephen Boyd 	size_t buf_size;
2402624d5faSMark Brown 	char *start = buf;
2412624d5faSMark Brown 	unsigned long reg, value;
2422624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
243b785a492SJingoo Han 	int ret;
2442624d5faSMark Brown 
2452624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2462624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2472624d5faSMark Brown 		return -EFAULT;
2482624d5faSMark Brown 	buf[buf_size] = 0;
2492624d5faSMark Brown 
2502624d5faSMark Brown 	while (*start == ' ')
2512624d5faSMark Brown 		start++;
2522624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2532624d5faSMark Brown 	while (*start == ' ')
2542624d5faSMark Brown 		start++;
255b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
256b785a492SJingoo Han 	if (ret)
257b785a492SJingoo Han 		return ret;
2580d51a9cbSMark Brown 
2590d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
260373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2610d51a9cbSMark Brown 
262e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2632624d5faSMark Brown 	return buf_size;
2642624d5faSMark Brown }
2652624d5faSMark Brown 
2662624d5faSMark Brown static const struct file_operations codec_reg_fops = {
267234e3405SStephen Boyd 	.open = simple_open,
2682624d5faSMark Brown 	.read = codec_reg_read_file,
2692624d5faSMark Brown 	.write = codec_reg_write_file,
2706038f373SArnd Bergmann 	.llseek = default_llseek,
2712624d5faSMark Brown };
2722624d5faSMark Brown 
273*e73f3de5SRussell King static struct dentry *soc_debugfs_create_dir(struct dentry *parent,
274*e73f3de5SRussell King 	const char *fmt, ...)
275*e73f3de5SRussell King {
276*e73f3de5SRussell King 	struct dentry *de;
277*e73f3de5SRussell King 	va_list ap;
278*e73f3de5SRussell King 	char *s;
279*e73f3de5SRussell King 
280*e73f3de5SRussell King 	va_start(ap, fmt);
281*e73f3de5SRussell King 	s = kvasprintf(GFP_KERNEL, fmt, ap);
282*e73f3de5SRussell King 	va_end(ap);
283*e73f3de5SRussell King 
284*e73f3de5SRussell King 	if (!s)
285*e73f3de5SRussell King 		return NULL;
286*e73f3de5SRussell King 
287*e73f3de5SRussell King 	de = debugfs_create_dir(s, parent);
288*e73f3de5SRussell King 	kfree(s);
289*e73f3de5SRussell King 
290*e73f3de5SRussell King 	return de;
291*e73f3de5SRussell King }
292*e73f3de5SRussell King 
2932624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2942624d5faSMark Brown {
295d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
296d6ce4cf3SJarkko Nikula 
297*e73f3de5SRussell King 	codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root,
298*e73f3de5SRussell King 						"codec:%s", codec->name);
2992624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
30010e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
30110e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec debugfs directory\n");
3022624d5faSMark Brown 		return;
3032624d5faSMark Brown 	}
3042624d5faSMark Brown 
305aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
306aaee8ef1SMark Brown 			    &codec->cache_sync);
307aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
308aaee8ef1SMark Brown 			    &codec->cache_only);
309aaee8ef1SMark Brown 
3102624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
3112624d5faSMark Brown 						 codec->debugfs_codec_root,
3122624d5faSMark Brown 						 codec, &codec_reg_fops);
3132624d5faSMark Brown 	if (!codec->debugfs_reg)
31410e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
31510e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
3162624d5faSMark Brown 
3178eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
3182624d5faSMark Brown }
3192624d5faSMark Brown 
3202624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3212624d5faSMark Brown {
3222624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3232624d5faSMark Brown }
3242624d5faSMark Brown 
325731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
326731f1ab2SSebastien Guiriec {
327731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
328731f1ab2SSebastien Guiriec 
329*e73f3de5SRussell King 	platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root,
330*e73f3de5SRussell King 						"platform:%s", platform->name);
331731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
332731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
333f110bfc7SLiam Girdwood 			"ASoC: Failed to create platform debugfs directory\n");
334731f1ab2SSebastien Guiriec 		return;
335731f1ab2SSebastien Guiriec 	}
336731f1ab2SSebastien Guiriec 
337731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
338731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
339731f1ab2SSebastien Guiriec }
340731f1ab2SSebastien Guiriec 
341731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
342731f1ab2SSebastien Guiriec {
343731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
344731f1ab2SSebastien Guiriec }
345731f1ab2SSebastien Guiriec 
346c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
347c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
348c3c5a19aSMark Brown {
349c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3502b194f9dSMark Brown 	ssize_t len, ret = 0;
351c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
352c3c5a19aSMark Brown 
353c3c5a19aSMark Brown 	if (!buf)
354c3c5a19aSMark Brown 		return -ENOMEM;
355c3c5a19aSMark Brown 
3562b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3572b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
358c3c5a19aSMark Brown 			       codec->name);
3592b194f9dSMark Brown 		if (len >= 0)
3602b194f9dSMark Brown 			ret += len;
3612b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3622b194f9dSMark Brown 			ret = PAGE_SIZE;
3632b194f9dSMark Brown 			break;
3642b194f9dSMark Brown 		}
3652b194f9dSMark Brown 	}
366c3c5a19aSMark Brown 
367c3c5a19aSMark Brown 	if (ret >= 0)
368c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
369c3c5a19aSMark Brown 
370c3c5a19aSMark Brown 	kfree(buf);
371c3c5a19aSMark Brown 
372c3c5a19aSMark Brown 	return ret;
373c3c5a19aSMark Brown }
374c3c5a19aSMark Brown 
375c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
376c3c5a19aSMark Brown 	.read = codec_list_read_file,
377c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
378c3c5a19aSMark Brown };
379c3c5a19aSMark Brown 
380f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
381f3208780SMark Brown 				  size_t count, loff_t *ppos)
382f3208780SMark Brown {
383f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3842b194f9dSMark Brown 	ssize_t len, ret = 0;
3851438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
386f3208780SMark Brown 	struct snd_soc_dai *dai;
387f3208780SMark Brown 
388f3208780SMark Brown 	if (!buf)
389f3208780SMark Brown 		return -ENOMEM;
390f3208780SMark Brown 
3911438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3921438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3931438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3941438c2f6SLars-Peter Clausen 				dai->name);
3952b194f9dSMark Brown 			if (len >= 0)
3962b194f9dSMark Brown 				ret += len;
3972b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3982b194f9dSMark Brown 				ret = PAGE_SIZE;
3992b194f9dSMark Brown 				break;
4002b194f9dSMark Brown 			}
4012b194f9dSMark Brown 		}
4021438c2f6SLars-Peter Clausen 	}
403f3208780SMark Brown 
404f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
405f3208780SMark Brown 
406f3208780SMark Brown 	kfree(buf);
407f3208780SMark Brown 
408f3208780SMark Brown 	return ret;
409f3208780SMark Brown }
410f3208780SMark Brown 
411f3208780SMark Brown static const struct file_operations dai_list_fops = {
412f3208780SMark Brown 	.read = dai_list_read_file,
413f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
414f3208780SMark Brown };
415f3208780SMark Brown 
41619c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41719c7ac27SMark Brown 				       char __user *user_buf,
41819c7ac27SMark Brown 				       size_t count, loff_t *ppos)
41919c7ac27SMark Brown {
42019c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4212b194f9dSMark Brown 	ssize_t len, ret = 0;
42219c7ac27SMark Brown 	struct snd_soc_platform *platform;
42319c7ac27SMark Brown 
42419c7ac27SMark Brown 	if (!buf)
42519c7ac27SMark Brown 		return -ENOMEM;
42619c7ac27SMark Brown 
4272b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4282b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
42919c7ac27SMark Brown 			       platform->name);
4302b194f9dSMark Brown 		if (len >= 0)
4312b194f9dSMark Brown 			ret += len;
4322b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4332b194f9dSMark Brown 			ret = PAGE_SIZE;
4342b194f9dSMark Brown 			break;
4352b194f9dSMark Brown 		}
4362b194f9dSMark Brown 	}
43719c7ac27SMark Brown 
43819c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
43919c7ac27SMark Brown 
44019c7ac27SMark Brown 	kfree(buf);
44119c7ac27SMark Brown 
44219c7ac27SMark Brown 	return ret;
44319c7ac27SMark Brown }
44419c7ac27SMark Brown 
44519c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44619c7ac27SMark Brown 	.read = platform_list_read_file,
44719c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
44819c7ac27SMark Brown };
44919c7ac27SMark Brown 
450a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
451a6052154SJarkko Nikula {
452a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4538a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4543a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
455a6052154SJarkko Nikula 		dev_warn(card->dev,
4567c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4573a45b867SJarkko Nikula 		return;
4583a45b867SJarkko Nikula 	}
4593a45b867SJarkko Nikula 
4603a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4613a45b867SJarkko Nikula 						    card->debugfs_card_root,
4623a45b867SJarkko Nikula 						    &card->pop_time);
4633a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4643a45b867SJarkko Nikula 		dev_warn(card->dev,
465f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
466a6052154SJarkko Nikula }
467a6052154SJarkko Nikula 
468a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469a6052154SJarkko Nikula {
470a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
471a6052154SJarkko Nikula }
472a6052154SJarkko Nikula 
4732624d5faSMark Brown #else
4742624d5faSMark Brown 
4752624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4762624d5faSMark Brown {
4772624d5faSMark Brown }
4782624d5faSMark Brown 
4792624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4802624d5faSMark Brown {
4812624d5faSMark Brown }
482b95fccbcSAxel Lin 
483731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
484731f1ab2SSebastien Guiriec {
485731f1ab2SSebastien Guiriec }
486731f1ab2SSebastien Guiriec 
487731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
488731f1ab2SSebastien Guiriec {
489731f1ab2SSebastien Guiriec }
490731f1ab2SSebastien Guiriec 
491b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
492b95fccbcSAxel Lin {
493b95fccbcSAxel Lin }
494b95fccbcSAxel Lin 
495b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
496b95fccbcSAxel Lin {
497b95fccbcSAxel Lin }
4982624d5faSMark Brown #endif
4992624d5faSMark Brown 
50047c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
50147c88fffSLiam Girdwood 		const char *dai_link, int stream)
50247c88fffSLiam Girdwood {
50347c88fffSLiam Girdwood 	int i;
50447c88fffSLiam Girdwood 
50547c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
50647c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
50747c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
50847c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
50947c88fffSLiam Girdwood 	}
510f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
51147c88fffSLiam Girdwood 	return NULL;
51247c88fffSLiam Girdwood }
51347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
51447c88fffSLiam Girdwood 
51547c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
51647c88fffSLiam Girdwood 		const char *dai_link)
51747c88fffSLiam Girdwood {
51847c88fffSLiam Girdwood 	int i;
51947c88fffSLiam Girdwood 
52047c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
52147c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
52247c88fffSLiam Girdwood 			return &card->rtd[i];
52347c88fffSLiam Girdwood 	}
524f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
52547c88fffSLiam Girdwood 	return NULL;
52647c88fffSLiam Girdwood }
52747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
52847c88fffSLiam Girdwood 
529db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
530db2a4165SFrank Mandarino /* unregister ac97 codec */
531db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
532db2a4165SFrank Mandarino {
533db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
534db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
535db2a4165SFrank Mandarino 	return 0;
536db2a4165SFrank Mandarino }
537db2a4165SFrank Mandarino 
538db2a4165SFrank Mandarino /* stop no dev release warning */
539db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
540db2a4165SFrank Mandarino 
541db2a4165SFrank Mandarino /* register ac97 codec to bus */
542db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
543db2a4165SFrank Mandarino {
544db2a4165SFrank Mandarino 	int err;
545db2a4165SFrank Mandarino 
546db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
5474ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
548db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
549db2a4165SFrank Mandarino 
550bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
551f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
552db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
553db2a4165SFrank Mandarino 	if (err < 0) {
554f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
555db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
556db2a4165SFrank Mandarino 		return err;
557db2a4165SFrank Mandarino 	}
558db2a4165SFrank Mandarino 	return 0;
559db2a4165SFrank Mandarino }
560db2a4165SFrank Mandarino #endif
561db2a4165SFrank Mandarino 
5629d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5639d58a077SRichard Fitzgerald {
5649d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5659d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5669d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5679d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5689d58a077SRichard Fitzgerald 	 */
5699d58a077SRichard Fitzgerald }
5709d58a077SRichard Fitzgerald 
5716f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
572db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5736f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
574db2a4165SFrank Mandarino {
5756f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5762eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
577db2a4165SFrank Mandarino 	int i;
578db2a4165SFrank Mandarino 
579e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
580e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
581e3509ff0SDaniel Mack 	 */
582f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
583e3509ff0SDaniel Mack 		return 0;
584e3509ff0SDaniel Mack 
5856ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5866ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5876ed25978SAndy Green 	 */
588f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
589f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
590f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5916ed25978SAndy Green 
5926ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
593f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5946ed25978SAndy Green 
595a00f90f9SMark Brown 	/* mute any active DACs */
596f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
597f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
598f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5993efab7dcSMark Brown 
600f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6013efab7dcSMark Brown 			continue;
6023efab7dcSMark Brown 
603f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
604f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
605db2a4165SFrank Mandarino 	}
606db2a4165SFrank Mandarino 
6074ccab3e7SLiam Girdwood 	/* suspend all pcms */
608f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
609f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6103efab7dcSMark Brown 			continue;
6113efab7dcSMark Brown 
612f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
6133efab7dcSMark Brown 	}
6144ccab3e7SLiam Girdwood 
61587506549SMark Brown 	if (card->suspend_pre)
61670b2ac12SMark Brown 		card->suspend_pre(card);
617db2a4165SFrank Mandarino 
618f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
619f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
620f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
6213efab7dcSMark Brown 
622f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6233efab7dcSMark Brown 			continue;
6243efab7dcSMark Brown 
625f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
626f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
627f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
628f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
629f0fba2adSLiam Girdwood 			platform->suspended = 1;
630f0fba2adSLiam Girdwood 		}
631db2a4165SFrank Mandarino 	}
632db2a4165SFrank Mandarino 
633db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
634f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
63543829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
636ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
637f0fba2adSLiam Girdwood 	}
638db2a4165SFrank Mandarino 
639f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6403efab7dcSMark Brown 
641f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6423efab7dcSMark Brown 			continue;
6433efab7dcSMark Brown 
6447bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6457bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
646db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
647f0fba2adSLiam Girdwood 
6487bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6497bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
650db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
651db2a4165SFrank Mandarino 	}
652db2a4165SFrank Mandarino 
653e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
654e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
655e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
656e2d32ff6SMark Brown 
657f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6582eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6591547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6601547aba9SMark Brown 		 * bias _ON and should not be suspended. */
661f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
662ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6631547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
664125a25daSMark Brown 				/*
665125a25daSMark Brown 				 * If the CODEC is capable of idle
666125a25daSMark Brown 				 * bias off then being in STANDBY
667125a25daSMark Brown 				 * means it's doing something,
668125a25daSMark Brown 				 * otherwise fall through.
669125a25daSMark Brown 				 */
670125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
671125a25daSMark Brown 					dev_dbg(codec->dev,
67210e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
673125a25daSMark Brown 					break;
674125a25daSMark Brown 				}
6751547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
67684b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
677f0fba2adSLiam Girdwood 				codec->suspended = 1;
6787be4ba24SMark Brown 				codec->cache_sync = 1;
679e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
680e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
681988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
682988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6831547aba9SMark Brown 				break;
6841547aba9SMark Brown 			default:
68510e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
68610e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6871547aba9SMark Brown 				break;
6881547aba9SMark Brown 			}
6891547aba9SMark Brown 		}
690f0fba2adSLiam Girdwood 	}
691db2a4165SFrank Mandarino 
692f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
693f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6943efab7dcSMark Brown 
695f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6963efab7dcSMark Brown 			continue;
6973efab7dcSMark Brown 
698f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
699f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
700988e8cc4SNicolin Chen 
701988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
702988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
703db2a4165SFrank Mandarino 	}
704db2a4165SFrank Mandarino 
70587506549SMark Brown 	if (card->suspend_post)
70670b2ac12SMark Brown 		card->suspend_post(card);
707db2a4165SFrank Mandarino 
708db2a4165SFrank Mandarino 	return 0;
709db2a4165SFrank Mandarino }
7106f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
711db2a4165SFrank Mandarino 
7126ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7136ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7146ed25978SAndy Green  */
7156ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
716db2a4165SFrank Mandarino {
717f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
718f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7192eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
720db2a4165SFrank Mandarino 	int i;
721db2a4165SFrank Mandarino 
7226ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7236ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7246ed25978SAndy Green 	 */
7256ed25978SAndy Green 
726f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7276ed25978SAndy Green 
7289949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
729f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7309949788bSMark Brown 
73187506549SMark Brown 	if (card->resume_pre)
73270b2ac12SMark Brown 		card->resume_pre(card);
733db2a4165SFrank Mandarino 
734f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
735f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
736f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7373efab7dcSMark Brown 
738f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7393efab7dcSMark Brown 			continue;
7403efab7dcSMark Brown 
741f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
742f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
743db2a4165SFrank Mandarino 	}
744db2a4165SFrank Mandarino 
7452eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7461547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7471547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7481547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7491547aba9SMark Brown 		 */
750f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
751ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7521547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7531547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
754f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
755f0fba2adSLiam Girdwood 				codec->suspended = 0;
7561547aba9SMark Brown 				break;
7571547aba9SMark Brown 			default:
75810e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
75910e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7601547aba9SMark Brown 				break;
7611547aba9SMark Brown 			}
7621547aba9SMark Brown 		}
763f0fba2adSLiam Girdwood 	}
764db2a4165SFrank Mandarino 
765f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7663efab7dcSMark Brown 
767f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7683efab7dcSMark Brown 			continue;
7693efab7dcSMark Brown 
7707bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
771d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
772db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
773f0fba2adSLiam Girdwood 
7747bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
775d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
776db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
777db2a4165SFrank Mandarino 	}
778db2a4165SFrank Mandarino 
7793ff3f64bSMark Brown 	/* unmute any active DACs */
780f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
781f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
782f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7833efab7dcSMark Brown 
784f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7853efab7dcSMark Brown 			continue;
7863efab7dcSMark Brown 
787f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
788f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
789db2a4165SFrank Mandarino 	}
790db2a4165SFrank Mandarino 
791f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
792f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
793f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7943efab7dcSMark Brown 
795f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7963efab7dcSMark Brown 			continue;
7973efab7dcSMark Brown 
798f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
799f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
800f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
801f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
802f0fba2adSLiam Girdwood 			platform->suspended = 0;
803f0fba2adSLiam Girdwood 		}
804db2a4165SFrank Mandarino 	}
805db2a4165SFrank Mandarino 
80687506549SMark Brown 	if (card->resume_post)
80770b2ac12SMark Brown 		card->resume_post(card);
808db2a4165SFrank Mandarino 
809f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8106ed25978SAndy Green 
8116ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
812f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
813e2d32ff6SMark Brown 
814e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
815e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
816e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8176ed25978SAndy Green }
8186ed25978SAndy Green 
8196ed25978SAndy Green /* powers up audio subsystem after a suspend */
8206f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8216ed25978SAndy Green {
8226f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
82382e14e8bSStephen Warren 	int i, ac97_control = 0;
824b9dd94a8SPeter Ujfalusi 
8255ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
8265ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
8275ff1ddf2SEric Miao 	 */
8285ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
8295ff1ddf2SEric Miao 		return 0;
8305ff1ddf2SEric Miao 
831988e8cc4SNicolin Chen 	/* activate pins from sleep state */
832988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
833988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
834988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
835988e8cc4SNicolin Chen 		if (cpu_dai->active)
836988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
837988e8cc4SNicolin Chen 		if (codec_dai->active)
838988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(codec_dai->dev);
839988e8cc4SNicolin Chen 	}
840988e8cc4SNicolin Chen 
84164ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
84264ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
84364ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
84464ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
84564ab9baaSMark Brown 	 */
846f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
847f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
84882e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
84982e14e8bSStephen Warren 	}
85082e14e8bSStephen Warren 	if (ac97_control) {
851f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
85264ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
85364ab9baaSMark Brown 	} else {
854f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8556308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
856f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
857f0fba2adSLiam Girdwood 	}
8586ed25978SAndy Green 
859db2a4165SFrank Mandarino 	return 0;
860db2a4165SFrank Mandarino }
8616f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
862db2a4165SFrank Mandarino #else
8636f8ab4acSMark Brown #define snd_soc_suspend NULL
8646f8ab4acSMark Brown #define snd_soc_resume NULL
865db2a4165SFrank Mandarino #endif
866db2a4165SFrank Mandarino 
86785e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
86802a06d30SBarry Song };
86902a06d30SBarry Song 
87012023a9aSMisael Lopez Cruz static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
87112023a9aSMisael Lopez Cruz 					    const char *codec_name)
87212023a9aSMisael Lopez Cruz {
87312023a9aSMisael Lopez Cruz 	struct snd_soc_codec *codec;
87412023a9aSMisael Lopez Cruz 
87512023a9aSMisael Lopez Cruz 	list_for_each_entry(codec, &codec_list, list) {
87612023a9aSMisael Lopez Cruz 		if (codec_of_node) {
87712023a9aSMisael Lopez Cruz 			if (codec->dev->of_node != codec_of_node)
87812023a9aSMisael Lopez Cruz 				continue;
87912023a9aSMisael Lopez Cruz 		} else {
88012023a9aSMisael Lopez Cruz 			if (strcmp(codec->name, codec_name))
88112023a9aSMisael Lopez Cruz 				continue;
88212023a9aSMisael Lopez Cruz 		}
88312023a9aSMisael Lopez Cruz 
88412023a9aSMisael Lopez Cruz 		return codec;
88512023a9aSMisael Lopez Cruz 	}
88612023a9aSMisael Lopez Cruz 
88712023a9aSMisael Lopez Cruz 	return NULL;
88812023a9aSMisael Lopez Cruz }
88912023a9aSMisael Lopez Cruz 
89012023a9aSMisael Lopez Cruz static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
89112023a9aSMisael Lopez Cruz 					      const char *codec_dai_name)
89212023a9aSMisael Lopez Cruz {
89312023a9aSMisael Lopez Cruz 	struct snd_soc_dai *codec_dai;
89412023a9aSMisael Lopez Cruz 
89512023a9aSMisael Lopez Cruz 	list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
89612023a9aSMisael Lopez Cruz 		if (!strcmp(codec_dai->name, codec_dai_name)) {
89712023a9aSMisael Lopez Cruz 			return codec_dai;
89812023a9aSMisael Lopez Cruz 		}
89912023a9aSMisael Lopez Cruz 	}
90012023a9aSMisael Lopez Cruz 
90112023a9aSMisael Lopez Cruz 	return NULL;
90212023a9aSMisael Lopez Cruz }
90312023a9aSMisael Lopez Cruz 
904f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
905db2a4165SFrank Mandarino {
906f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
907f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
9081438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
909435c5e25SMark Brown 	struct snd_soc_platform *platform;
91012023a9aSMisael Lopez Cruz 	struct snd_soc_dai *cpu_dai;
911848dd8beSMark Brown 	const char *platform_name;
912db2a4165SFrank Mandarino 
913f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
9146308419aSMark Brown 
915b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
9161438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
917bc92657aSStephen Warren 		if (dai_link->cpu_of_node &&
9181438c2f6SLars-Peter Clausen 			component->dev->of_node != dai_link->cpu_of_node)
9195a504963SStephen Warren 			continue;
920bc92657aSStephen Warren 		if (dai_link->cpu_name &&
9211438c2f6SLars-Peter Clausen 			strcmp(dev_name(component->dev), dai_link->cpu_name))
9222610ab77SStephen Warren 			continue;
9231438c2f6SLars-Peter Clausen 		list_for_each_entry(cpu_dai, &component->dai_list, list) {
924bc92657aSStephen Warren 			if (dai_link->cpu_dai_name &&
925bc92657aSStephen Warren 				strcmp(cpu_dai->name, dai_link->cpu_dai_name))
926bc92657aSStephen Warren 				continue;
9272610ab77SStephen Warren 
928f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
929f0fba2adSLiam Girdwood 		}
9301438c2f6SLars-Peter Clausen 	}
931b19e6e7bSMark Brown 
932b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
933f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
934f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
935b19e6e7bSMark Brown 		return -EPROBE_DEFER;
936c5af3a2eSMark Brown 	}
937c5af3a2eSMark Brown 
93812023a9aSMisael Lopez Cruz 	/* Find CODEC from registered list */
93912023a9aSMisael Lopez Cruz 	rtd->codec = soc_find_codec(dai_link->codec_of_node,
94012023a9aSMisael Lopez Cruz 				    dai_link->codec_name);
941b19e6e7bSMark Brown 	if (!rtd->codec) {
942f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CODEC %s not registered\n",
943f0fba2adSLiam Girdwood 			dai_link->codec_name);
944b19e6e7bSMark Brown 		return -EPROBE_DEFER;
945b19e6e7bSMark Brown 	}
946848dd8beSMark Brown 
94712023a9aSMisael Lopez Cruz 	/* Find CODEC DAI from registered list */
94812023a9aSMisael Lopez Cruz 	rtd->codec_dai = soc_find_codec_dai(rtd->codec,
94912023a9aSMisael Lopez Cruz 					    dai_link->codec_dai_name);
95012023a9aSMisael Lopez Cruz 	if (!rtd->codec_dai) {
95112023a9aSMisael Lopez Cruz 		dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
95212023a9aSMisael Lopez Cruz 			dai_link->codec_dai_name);
95312023a9aSMisael Lopez Cruz 		return -EPROBE_DEFER;
95412023a9aSMisael Lopez Cruz 	}
95512023a9aSMisael Lopez Cruz 
956848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
957848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9585a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
959848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
960848dd8beSMark Brown 
961b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
962f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9635a504963SStephen Warren 		if (dai_link->platform_of_node) {
9645a504963SStephen Warren 			if (platform->dev->of_node !=
9655a504963SStephen Warren 			    dai_link->platform_of_node)
9665a504963SStephen Warren 				continue;
9675a504963SStephen Warren 		} else {
9682610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
9692610ab77SStephen Warren 				continue;
9705a504963SStephen Warren 		}
9712610ab77SStephen Warren 
972f0fba2adSLiam Girdwood 		rtd->platform = platform;
973f0fba2adSLiam Girdwood 	}
974b19e6e7bSMark Brown 	if (!rtd->platform) {
975f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
976f0fba2adSLiam Girdwood 			dai_link->platform_name);
977b19e6e7bSMark Brown 		return -EPROBE_DEFER;
978f0fba2adSLiam Girdwood 	}
979b19e6e7bSMark Brown 
980b19e6e7bSMark Brown 	card->num_rtd++;
981b19e6e7bSMark Brown 
982b19e6e7bSMark Brown 	return 0;
9836b05eda6SMark Brown }
9846b05eda6SMark Brown 
985d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform)
986d12cd198SStephen Warren {
987d12cd198SStephen Warren 	int ret;
988d12cd198SStephen Warren 
989d12cd198SStephen Warren 	if (platform->driver->remove) {
990d12cd198SStephen Warren 		ret = platform->driver->remove(platform);
991d12cd198SStephen Warren 		if (ret < 0)
992f110bfc7SLiam Girdwood 			dev_err(platform->dev, "ASoC: failed to remove %d\n",
993f110bfc7SLiam Girdwood 				ret);
994d12cd198SStephen Warren 	}
995d12cd198SStephen Warren 
996d12cd198SStephen Warren 	/* Make sure all DAPM widgets are freed */
997d12cd198SStephen Warren 	snd_soc_dapm_free(&platform->dapm);
998d12cd198SStephen Warren 
999d12cd198SStephen Warren 	soc_cleanup_platform_debugfs(platform);
1000d12cd198SStephen Warren 	platform->probed = 0;
1001d12cd198SStephen Warren 	list_del(&platform->card_list);
1002d12cd198SStephen Warren 	module_put(platform->dev->driver->owner);
1003d12cd198SStephen Warren 
1004d12cd198SStephen Warren 	return 0;
1005d12cd198SStephen Warren }
1006d12cd198SStephen Warren 
1007589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
1008589c3563SJarkko Nikula {
1009589c3563SJarkko Nikula 	int err;
1010589c3563SJarkko Nikula 
1011589c3563SJarkko Nikula 	if (codec->driver->remove) {
1012589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
1013589c3563SJarkko Nikula 		if (err < 0)
1014f110bfc7SLiam Girdwood 			dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
1015589c3563SJarkko Nikula 	}
1016589c3563SJarkko Nikula 
1017589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
1018589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
1019589c3563SJarkko Nikula 
1020589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
1021589c3563SJarkko Nikula 	codec->probed = 0;
1022589c3563SJarkko Nikula 	list_del(&codec->card_list);
1023589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
1024589c3563SJarkko Nikula }
1025589c3563SJarkko Nikula 
1026b0aa88afSMisael Lopez Cruz static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1027b0aa88afSMisael Lopez Cruz {
1028b0aa88afSMisael Lopez Cruz 	int err;
1029b0aa88afSMisael Lopez Cruz 
1030b0aa88afSMisael Lopez Cruz 	if (codec_dai && codec_dai->probed &&
1031b0aa88afSMisael Lopez Cruz 			codec_dai->driver->remove_order == order) {
1032b0aa88afSMisael Lopez Cruz 		if (codec_dai->driver->remove) {
1033b0aa88afSMisael Lopez Cruz 			err = codec_dai->driver->remove(codec_dai);
1034b0aa88afSMisael Lopez Cruz 			if (err < 0)
1035b0aa88afSMisael Lopez Cruz 				dev_err(codec_dai->dev,
1036b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1037b0aa88afSMisael Lopez Cruz 					codec_dai->name, err);
1038b0aa88afSMisael Lopez Cruz 		}
1039b0aa88afSMisael Lopez Cruz 		codec_dai->probed = 0;
1040b0aa88afSMisael Lopez Cruz 	}
1041b0aa88afSMisael Lopez Cruz }
1042b0aa88afSMisael Lopez Cruz 
104362ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1044f0fba2adSLiam Girdwood {
1045f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1046f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1047f0fba2adSLiam Girdwood 	int err;
1048f0fba2adSLiam Girdwood 
1049f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1050f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
105136ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
105236ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
105336ae1a96SMark Brown 		device_unregister(rtd->dev);
1054f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
105502a06d30SBarry Song 	}
105602a06d30SBarry Song 
1057f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
1058b0aa88afSMisael Lopez Cruz 	soc_remove_codec_dai(codec_dai, order);
1059f0fba2adSLiam Girdwood 
1060f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
10610168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
10620168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
1063f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1064f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1065f0fba2adSLiam Girdwood 			if (err < 0)
1066f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1067f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
10680837fc62SFabio Estevam 					cpu_dai->name, err);
1069f0fba2adSLiam Girdwood 		}
1070f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1071a9db7dbeSStephen Warren 
107218d75644SStephen Warren 		if (!cpu_dai->codec) {
107318d75644SStephen Warren 			snd_soc_dapm_free(&cpu_dai->dapm);
1074f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
1075f0fba2adSLiam Girdwood 		}
1076f0fba2adSLiam Girdwood 	}
107718d75644SStephen Warren }
1078f0fba2adSLiam Girdwood 
107962ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
108062ae68faSStephen Warren 				       int order)
108162ae68faSStephen Warren {
108262ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
108362ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
108462ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
108562ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
108662ae68faSStephen Warren 	struct snd_soc_codec *codec;
108762ae68faSStephen Warren 
108862ae68faSStephen Warren 	/* remove the platform */
108962ae68faSStephen Warren 	if (platform && platform->probed &&
109062ae68faSStephen Warren 	    platform->driver->remove_order == order) {
109162ae68faSStephen Warren 		soc_remove_platform(platform);
109262ae68faSStephen Warren 	}
109362ae68faSStephen Warren 
109462ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
109562ae68faSStephen Warren 	if (codec_dai) {
109662ae68faSStephen Warren 		codec = codec_dai->codec;
109762ae68faSStephen Warren 		if (codec && codec->probed &&
109862ae68faSStephen Warren 		    codec->driver->remove_order == order)
109962ae68faSStephen Warren 			soc_remove_codec(codec);
110062ae68faSStephen Warren 	}
110162ae68faSStephen Warren 
110262ae68faSStephen Warren 	/* remove any CPU-side CODEC */
110362ae68faSStephen Warren 	if (cpu_dai) {
110462ae68faSStephen Warren 		codec = cpu_dai->codec;
110562ae68faSStephen Warren 		if (codec && codec->probed &&
110662ae68faSStephen Warren 		    codec->driver->remove_order == order)
110762ae68faSStephen Warren 			soc_remove_codec(codec);
110862ae68faSStephen Warren 	}
110962ae68faSStephen Warren }
111062ae68faSStephen Warren 
11110671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11120671fd8eSKuninori Morimoto {
11130168bf0dSLiam Girdwood 	int dai, order;
11140671fd8eSKuninori Morimoto 
11150168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
11160168bf0dSLiam Girdwood 			order++) {
11170168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
111862ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
11190168bf0dSLiam Girdwood 	}
112062ae68faSStephen Warren 
112162ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
112262ae68faSStephen Warren 			order++) {
112362ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
112462ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
112562ae68faSStephen Warren 	}
112662ae68faSStephen Warren 
11270671fd8eSKuninori Morimoto 	card->num_rtd = 0;
11280671fd8eSKuninori Morimoto }
11290671fd8eSKuninori Morimoto 
1130ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1131ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1132ead9b919SJarkko Nikula {
1133ead9b919SJarkko Nikula 	int i;
1134ead9b919SJarkko Nikula 
1135ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1136ead9b919SJarkko Nikula 		return;
1137ead9b919SJarkko Nikula 
1138ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1139ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
11403ca041edSSebastian Reichel 		if (map->of_node && codec->dev->of_node != map->of_node)
11413ca041edSSebastian Reichel 			continue;
11423ca041edSSebastian Reichel 		if (map->dev_name && strcmp(codec->name, map->dev_name))
11433ca041edSSebastian Reichel 			continue;
1144ead9b919SJarkko Nikula 		codec->name_prefix = map->name_prefix;
1145ead9b919SJarkko Nikula 		break;
1146ead9b919SJarkko Nikula 	}
1147ead9b919SJarkko Nikula }
1148ead9b919SJarkko Nikula 
1149589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1150589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1151589c3563SJarkko Nikula {
1152589c3563SJarkko Nikula 	int ret = 0;
115389b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1154888df395SMark Brown 	struct snd_soc_dai *dai;
1155589c3563SJarkko Nikula 
1156589c3563SJarkko Nikula 	codec->card = card;
1157589c3563SJarkko Nikula 	codec->dapm.card = card;
1158589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1159589c3563SJarkko Nikula 
116070d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
116170d29331SJarkko Nikula 		return -ENODEV;
116270d29331SJarkko Nikula 
1163d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1164d5d1e0beSLars-Peter Clausen 
1165b318ad50SNariman Poushin 	if (driver->dapm_widgets) {
1166b318ad50SNariman Poushin 		ret = snd_soc_dapm_new_controls(&codec->dapm,
1167b318ad50SNariman Poushin 						driver->dapm_widgets,
116877530150SLars-Peter Clausen 					 	driver->num_dapm_widgets);
116977530150SLars-Peter Clausen 
1170b318ad50SNariman Poushin 		if (ret != 0) {
1171b318ad50SNariman Poushin 			dev_err(codec->dev,
1172b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1173b318ad50SNariman Poushin 			goto err_probe;
1174b318ad50SNariman Poushin 		}
1175b318ad50SNariman Poushin 	}
1176b318ad50SNariman Poushin 
1177888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1178261edc70SNariman Poushin 	list_for_each_entry(dai, &codec->component.dai_list, list) {
1179261edc70SNariman Poushin 		ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1180261edc70SNariman Poushin 
1181261edc70SNariman Poushin 		if (ret != 0) {
1182261edc70SNariman Poushin 			dev_err(codec->dev,
1183261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1184261edc70SNariman Poushin 			goto err_probe;
1185261edc70SNariman Poushin 		}
1186261edc70SNariman Poushin 	}
1187888df395SMark Brown 
118833c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
118933c5f969SMark Brown 
119089b95ac0SMark Brown 	if (driver->probe) {
119189b95ac0SMark Brown 		ret = driver->probe(codec);
1192589c3563SJarkko Nikula 		if (ret < 0) {
1193589c3563SJarkko Nikula 			dev_err(codec->dev,
1194f110bfc7SLiam Girdwood 				"ASoC: failed to probe CODEC %d\n", ret);
119570d29331SJarkko Nikula 			goto err_probe;
1196589c3563SJarkko Nikula 		}
1197ff541f4bSChuansheng Liu 		WARN(codec->dapm.idle_bias_off &&
1198ff541f4bSChuansheng Liu 			codec->dapm.bias_level != SND_SOC_BIAS_OFF,
119910e8aa9aSMichał Mirosław 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
120010e8aa9aSMichał Mirosław 			codec->name);
1201589c3563SJarkko Nikula 	}
1202589c3563SJarkko Nikula 
1203b7af1dafSMark Brown 	if (driver->controls)
1204022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1205b7af1dafSMark Brown 				     driver->num_controls);
120689b95ac0SMark Brown 	if (driver->dapm_routes)
120789b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
120889b95ac0SMark Brown 					driver->num_dapm_routes);
120989b95ac0SMark Brown 
1210589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1211589c3563SJarkko Nikula 	codec->probed = 1;
1212589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
12137be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1214589c3563SJarkko Nikula 
121570d29331SJarkko Nikula 	return 0;
121670d29331SJarkko Nikula 
121770d29331SJarkko Nikula err_probe:
1218d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
121970d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
122070d29331SJarkko Nikula 
1221589c3563SJarkko Nikula 	return ret;
1222589c3563SJarkko Nikula }
1223589c3563SJarkko Nikula 
1224956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1225956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1226956245e9SLiam Girdwood {
1227956245e9SLiam Girdwood 	int ret = 0;
1228956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
12291438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
1230be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1231956245e9SLiam Girdwood 
1232956245e9SLiam Girdwood 	platform->card = card;
1233b7950641SLiam Girdwood 	platform->dapm.card = card;
1234956245e9SLiam Girdwood 
1235956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1236956245e9SLiam Girdwood 		return -ENODEV;
1237956245e9SLiam Girdwood 
1238731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1239731f1ab2SSebastien Guiriec 
1240cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1241cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1242cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1243cb2cf612SLiam Girdwood 
1244be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
12451438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
12461438c2f6SLars-Peter Clausen 		if (component->dev != platform->dev)
1247be09ad90SLiam Girdwood 			continue;
12481438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list)
1249be09ad90SLiam Girdwood 			snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1250be09ad90SLiam Girdwood 	}
1251be09ad90SLiam Girdwood 
12523fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
12533fec6b6dSStephen Warren 
1254956245e9SLiam Girdwood 	if (driver->probe) {
1255956245e9SLiam Girdwood 		ret = driver->probe(platform);
1256956245e9SLiam Girdwood 		if (ret < 0) {
1257956245e9SLiam Girdwood 			dev_err(platform->dev,
1258f110bfc7SLiam Girdwood 				"ASoC: failed to probe platform %d\n", ret);
1259956245e9SLiam Girdwood 			goto err_probe;
1260956245e9SLiam Girdwood 		}
1261956245e9SLiam Girdwood 	}
1262956245e9SLiam Girdwood 
1263cb2cf612SLiam Girdwood 	if (driver->controls)
1264cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1265cb2cf612SLiam Girdwood 				     driver->num_controls);
1266cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1267cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1268cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1269cb2cf612SLiam Girdwood 
1270956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1271956245e9SLiam Girdwood 	platform->probed = 1;
1272956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1273b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1274956245e9SLiam Girdwood 
1275956245e9SLiam Girdwood 	return 0;
1276956245e9SLiam Girdwood 
1277956245e9SLiam Girdwood err_probe:
127802db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1279956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1280956245e9SLiam Girdwood 
1281956245e9SLiam Girdwood 	return ret;
1282956245e9SLiam Girdwood }
1283956245e9SLiam Girdwood 
128436ae1a96SMark Brown static void rtd_release(struct device *dev)
128536ae1a96SMark Brown {
128636ae1a96SMark Brown 	kfree(dev);
128736ae1a96SMark Brown }
1288f0fba2adSLiam Girdwood 
1289503ae5e0SMisael Lopez Cruz static int soc_aux_dev_init(struct snd_soc_card *card,
1290503ae5e0SMisael Lopez Cruz 			    struct snd_soc_codec *codec,
1291503ae5e0SMisael Lopez Cruz 			    int num)
1292503ae5e0SMisael Lopez Cruz {
1293503ae5e0SMisael Lopez Cruz 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1294503ae5e0SMisael Lopez Cruz 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1295503ae5e0SMisael Lopez Cruz 	int ret;
1296503ae5e0SMisael Lopez Cruz 
1297503ae5e0SMisael Lopez Cruz 	rtd->card = card;
1298503ae5e0SMisael Lopez Cruz 
1299503ae5e0SMisael Lopez Cruz 	/* do machine specific initialization */
1300503ae5e0SMisael Lopez Cruz 	if (aux_dev->init) {
1301503ae5e0SMisael Lopez Cruz 		ret = aux_dev->init(&codec->dapm);
1302503ae5e0SMisael Lopez Cruz 		if (ret < 0)
1303503ae5e0SMisael Lopez Cruz 			return ret;
1304503ae5e0SMisael Lopez Cruz 	}
1305503ae5e0SMisael Lopez Cruz 
1306503ae5e0SMisael Lopez Cruz 	rtd->codec = codec;
1307503ae5e0SMisael Lopez Cruz 
1308503ae5e0SMisael Lopez Cruz 	return 0;
1309503ae5e0SMisael Lopez Cruz }
1310503ae5e0SMisael Lopez Cruz 
1311503ae5e0SMisael Lopez Cruz static int soc_dai_link_init(struct snd_soc_card *card,
1312503ae5e0SMisael Lopez Cruz 			     struct snd_soc_codec *codec,
1313503ae5e0SMisael Lopez Cruz 			     int num)
1314503ae5e0SMisael Lopez Cruz {
1315503ae5e0SMisael Lopez Cruz 	struct snd_soc_dai_link *dai_link =  &card->dai_link[num];
1316503ae5e0SMisael Lopez Cruz 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1317503ae5e0SMisael Lopez Cruz 	int ret;
1318503ae5e0SMisael Lopez Cruz 
1319503ae5e0SMisael Lopez Cruz 	rtd->card = card;
1320503ae5e0SMisael Lopez Cruz 
1321503ae5e0SMisael Lopez Cruz 	/* do machine specific initialization */
1322503ae5e0SMisael Lopez Cruz 	if (dai_link->init) {
1323503ae5e0SMisael Lopez Cruz 		ret = dai_link->init(rtd);
1324503ae5e0SMisael Lopez Cruz 		if (ret < 0)
1325503ae5e0SMisael Lopez Cruz 			return ret;
1326503ae5e0SMisael Lopez Cruz 	}
1327503ae5e0SMisael Lopez Cruz 
1328503ae5e0SMisael Lopez Cruz 	rtd->codec = codec;
1329503ae5e0SMisael Lopez Cruz 
1330503ae5e0SMisael Lopez Cruz 	return 0;
1331503ae5e0SMisael Lopez Cruz }
1332503ae5e0SMisael Lopez Cruz 
1333589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1334589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1335589c3563SJarkko Nikula 				   int num, int dailess)
1336589c3563SJarkko Nikula {
1337589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1338589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1339589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
13406479f15aSLars-Peter Clausen 	const char *name;
1341589c3563SJarkko Nikula 	int ret = 0;
1342589c3563SJarkko Nikula 
1343589c3563SJarkko Nikula 	if (!dailess) {
1344589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1345589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1346589c3563SJarkko Nikula 		name = dai_link->name;
1347503ae5e0SMisael Lopez Cruz 		ret = soc_dai_link_init(card, codec, num);
1348589c3563SJarkko Nikula 	} else {
1349589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1350589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1351589c3563SJarkko Nikula 		name = aux_dev->name;
1352503ae5e0SMisael Lopez Cruz 		ret = soc_aux_dev_init(card, codec, num);
1353589c3563SJarkko Nikula 	}
1354589c3563SJarkko Nikula 
1355589c3563SJarkko Nikula 	if (ret < 0) {
1356f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1357589c3563SJarkko Nikula 		return ret;
1358589c3563SJarkko Nikula 	}
1359589c3563SJarkko Nikula 
1360589c3563SJarkko Nikula 	/* register the rtd device */
136136ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
136236ae1a96SMark Brown 	if (!rtd->dev)
136336ae1a96SMark Brown 		return -ENOMEM;
136436ae1a96SMark Brown 	device_initialize(rtd->dev);
136536ae1a96SMark Brown 	rtd->dev->parent = card->dev;
136636ae1a96SMark Brown 	rtd->dev->release = rtd_release;
136736ae1a96SMark Brown 	rtd->dev->init_name = name;
136836ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1369b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
137001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
137101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
137201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
137301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
137436ae1a96SMark Brown 	ret = device_add(rtd->dev);
1375589c3563SJarkko Nikula 	if (ret < 0) {
1376865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1377865df9cbSChuansheng Liu 		put_device(rtd->dev);
1378589c3563SJarkko Nikula 		dev_err(card->dev,
1379f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1380589c3563SJarkko Nikula 		return ret;
1381589c3563SJarkko Nikula 	}
1382589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1383589c3563SJarkko Nikula 
1384589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
138536ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1386589c3563SJarkko Nikula 	if (ret < 0)
1387589c3563SJarkko Nikula 		dev_err(codec->dev,
1388f110bfc7SLiam Girdwood 			"ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1389589c3563SJarkko Nikula 
1390589c3563SJarkko Nikula 	/* add codec sysfs entries */
139136ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1392589c3563SJarkko Nikula 	if (ret < 0)
1393589c3563SJarkko Nikula 		dev_err(codec->dev,
1394f110bfc7SLiam Girdwood 			"ASoC: failed to add codec sysfs files: %d\n", ret);
1395589c3563SJarkko Nikula 
1396f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1397f86dcef8SLiam Girdwood 	/* add DPCM sysfs entries */
1398cd0f8911SLiam Girdwood 	if (!dailess && !dai_link->dynamic)
1399f86dcef8SLiam Girdwood 		goto out;
1400f86dcef8SLiam Girdwood 
1401f86dcef8SLiam Girdwood 	ret = soc_dpcm_debugfs_add(rtd);
1402f86dcef8SLiam Girdwood 	if (ret < 0)
1403f110bfc7SLiam Girdwood 		dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1404f86dcef8SLiam Girdwood 
1405f86dcef8SLiam Girdwood out:
1406f86dcef8SLiam Girdwood #endif
1407589c3563SJarkko Nikula 	return 0;
1408589c3563SJarkko Nikula }
1409589c3563SJarkko Nikula 
141062ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
141162ae68faSStephen Warren 				     int order)
141262ae68faSStephen Warren {
141362ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
141462ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
141562ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
141662ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
141762ae68faSStephen Warren 	int ret;
141862ae68faSStephen Warren 
141962ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
142062ae68faSStephen Warren 	if (cpu_dai->codec &&
142162ae68faSStephen Warren 	    !cpu_dai->codec->probed &&
142262ae68faSStephen Warren 	    cpu_dai->codec->driver->probe_order == order) {
142362ae68faSStephen Warren 		ret = soc_probe_codec(card, cpu_dai->codec);
142462ae68faSStephen Warren 		if (ret < 0)
142562ae68faSStephen Warren 			return ret;
142662ae68faSStephen Warren 	}
142762ae68faSStephen Warren 
142862ae68faSStephen Warren 	/* probe the CODEC-side component */
142962ae68faSStephen Warren 	if (!codec_dai->codec->probed &&
143062ae68faSStephen Warren 	    codec_dai->codec->driver->probe_order == order) {
143162ae68faSStephen Warren 		ret = soc_probe_codec(card, codec_dai->codec);
143262ae68faSStephen Warren 		if (ret < 0)
143362ae68faSStephen Warren 			return ret;
143462ae68faSStephen Warren 	}
143562ae68faSStephen Warren 
143662ae68faSStephen Warren 	/* probe the platform */
143762ae68faSStephen Warren 	if (!platform->probed &&
143862ae68faSStephen Warren 	    platform->driver->probe_order == order) {
143962ae68faSStephen Warren 		ret = soc_probe_platform(card, platform);
144062ae68faSStephen Warren 		if (ret < 0)
144162ae68faSStephen Warren 			return ret;
144262ae68faSStephen Warren 	}
144362ae68faSStephen Warren 
144462ae68faSStephen Warren 	return 0;
144562ae68faSStephen Warren }
144662ae68faSStephen Warren 
1447b0aa88afSMisael Lopez Cruz static int soc_probe_codec_dai(struct snd_soc_card *card,
1448b0aa88afSMisael Lopez Cruz 			       struct snd_soc_dai *codec_dai,
1449b0aa88afSMisael Lopez Cruz 			       int order)
1450b0aa88afSMisael Lopez Cruz {
1451b0aa88afSMisael Lopez Cruz 	int ret;
1452b0aa88afSMisael Lopez Cruz 
1453b0aa88afSMisael Lopez Cruz 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1454b0aa88afSMisael Lopez Cruz 		if (codec_dai->driver->probe) {
1455b0aa88afSMisael Lopez Cruz 			ret = codec_dai->driver->probe(codec_dai);
1456b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
1457b0aa88afSMisael Lopez Cruz 				dev_err(codec_dai->dev,
1458b0aa88afSMisael Lopez Cruz 					"ASoC: failed to probe CODEC DAI %s: %d\n",
1459b0aa88afSMisael Lopez Cruz 					codec_dai->name, ret);
1460b0aa88afSMisael Lopez Cruz 				return ret;
1461b0aa88afSMisael Lopez Cruz 			}
1462b0aa88afSMisael Lopez Cruz 		}
1463b0aa88afSMisael Lopez Cruz 
1464b0aa88afSMisael Lopez Cruz 		/* mark codec_dai as probed and add to card dai list */
1465b0aa88afSMisael Lopez Cruz 		codec_dai->probed = 1;
1466b0aa88afSMisael Lopez Cruz 	}
1467b0aa88afSMisael Lopez Cruz 
1468b0aa88afSMisael Lopez Cruz 	return 0;
1469b0aa88afSMisael Lopez Cruz }
1470b0aa88afSMisael Lopez Cruz 
14712436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
14722436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
14732436a723SMisael Lopez Cruz 				struct snd_soc_dai *cpu_dai,
14742436a723SMisael Lopez Cruz 				struct snd_soc_dai *codec_dai)
14752436a723SMisael Lopez Cruz {
14762436a723SMisael Lopez Cruz 	struct snd_soc_dapm_widget *play_w, *capture_w;
14772436a723SMisael Lopez Cruz 	int ret;
14782436a723SMisael Lopez Cruz 
14792436a723SMisael Lopez Cruz 	/* link the DAI widgets */
14802436a723SMisael Lopez Cruz 	play_w = codec_dai->playback_widget;
14812436a723SMisael Lopez Cruz 	capture_w = cpu_dai->capture_widget;
14822436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
14832436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
14842436a723SMisael Lopez Cruz 					   capture_w, play_w);
14852436a723SMisael Lopez Cruz 		if (ret != 0) {
14862436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
14872436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
14882436a723SMisael Lopez Cruz 			return ret;
14892436a723SMisael Lopez Cruz 		}
14902436a723SMisael Lopez Cruz 	}
14912436a723SMisael Lopez Cruz 
14922436a723SMisael Lopez Cruz 	play_w = cpu_dai->playback_widget;
14932436a723SMisael Lopez Cruz 	capture_w = codec_dai->capture_widget;
14942436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
14952436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
14962436a723SMisael Lopez Cruz 					   capture_w, play_w);
14972436a723SMisael Lopez Cruz 		if (ret != 0) {
14982436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
14992436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
15002436a723SMisael Lopez Cruz 			return ret;
15012436a723SMisael Lopez Cruz 		}
15022436a723SMisael Lopez Cruz 	}
15032436a723SMisael Lopez Cruz 
15042436a723SMisael Lopez Cruz 	return 0;
15052436a723SMisael Lopez Cruz }
15062436a723SMisael Lopez Cruz 
150762ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1508f0fba2adSLiam Girdwood {
1509f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1510f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1511f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1512f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1513c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1514c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1515f0fba2adSLiam Girdwood 	int ret;
1516f0fba2adSLiam Girdwood 
1517f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
15180168bf0dSLiam Girdwood 			card->name, num, order);
1519f0fba2adSLiam Girdwood 
1520f0fba2adSLiam Girdwood 	/* config components */
1521f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1522f0fba2adSLiam Girdwood 	codec_dai->card = card;
1523f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1524f0fba2adSLiam Girdwood 
1525f0fba2adSLiam Girdwood 	/* set default power off timeout */
1526f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1527f0fba2adSLiam Girdwood 
1528f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
15290168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
15300168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1531a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
1532be09ad90SLiam Girdwood 			cpu_dai->dapm.card = card;
153361b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
153461b61e3cSLiam Girdwood 				return -ENODEV;
153561b61e3cSLiam Girdwood 
153618d75644SStephen Warren 			list_add(&cpu_dai->dapm.list, &card->dapm_list);
1537a9db7dbeSStephen Warren 		}
1538be09ad90SLiam Girdwood 
1539f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1540f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1541f0fba2adSLiam Girdwood 			if (ret < 0) {
1542f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1543f110bfc7SLiam Girdwood 					"ASoC: failed to probe CPU DAI %s: %d\n",
15440837fc62SFabio Estevam 					cpu_dai->name, ret);
154561b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1546f0fba2adSLiam Girdwood 				return ret;
1547f0fba2adSLiam Girdwood 			}
1548f0fba2adSLiam Girdwood 		}
1549f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1550f0fba2adSLiam Girdwood 	}
1551f0fba2adSLiam Girdwood 
1552f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
1553b0aa88afSMisael Lopez Cruz 	ret = soc_probe_codec_dai(card, codec_dai, order);
1554b0aa88afSMisael Lopez Cruz 	if (ret)
1555f0fba2adSLiam Girdwood 		return ret;
1556f0fba2adSLiam Girdwood 
15570168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
15580168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
15590168bf0dSLiam Girdwood 		return 0;
15600168bf0dSLiam Girdwood 
1561589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1562589c3563SJarkko Nikula 	if (ret)
1563f0fba2adSLiam Girdwood 		return ret;
1564f0fba2adSLiam Girdwood 
156536ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1566f0fba2adSLiam Girdwood 	if (ret < 0)
1567f110bfc7SLiam Girdwood 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1568f110bfc7SLiam Girdwood 			ret);
1569f0fba2adSLiam Girdwood 
15701245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
15711245b700SNamarta Kohli 		/*create compress_device"*/
15721245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
15731245b700SNamarta Kohli 		if (ret < 0) {
1574f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15751245b700SNamarta Kohli 					 dai_link->stream_name);
15761245b700SNamarta Kohli 			return ret;
15771245b700SNamarta Kohli 		}
15781245b700SNamarta Kohli 	} else {
15791245b700SNamarta Kohli 
1580c74184edSMark Brown 		if (!dai_link->params) {
1581f0fba2adSLiam Girdwood 			/* create the pcm */
1582f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1583f0fba2adSLiam Girdwood 			if (ret < 0) {
1584f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15850837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1586f0fba2adSLiam Girdwood 				return ret;
1587f0fba2adSLiam Girdwood 			}
1588c74184edSMark Brown 		} else {
15899d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
15909d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
15919d58a077SRichard Fitzgerald 
1592c74184edSMark Brown 			/* link the DAI widgets */
15932436a723SMisael Lopez Cruz 			ret = soc_link_dai_widgets(card, dai_link,
15942436a723SMisael Lopez Cruz 					cpu_dai, codec_dai);
15952436a723SMisael Lopez Cruz 			if (ret)
1596c74184edSMark Brown 				return ret;
1597c74184edSMark Brown 		}
1598c74184edSMark Brown 	}
1599c74184edSMark Brown 
1600f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1601f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1602f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1603f0fba2adSLiam Girdwood 
1604f0fba2adSLiam Girdwood 	return 0;
1605f0fba2adSLiam Girdwood }
1606f0fba2adSLiam Girdwood 
1607f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
160802c9c7b9SMisael Lopez Cruz static int soc_register_ac97_codec(struct snd_soc_codec *codec,
160902c9c7b9SMisael Lopez Cruz 				   struct snd_soc_dai *codec_dai)
1610f0fba2adSLiam Girdwood {
1611f0fba2adSLiam Girdwood 	int ret;
1612f0fba2adSLiam Girdwood 
1613f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1614f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
16156b05eda6SMark Brown 	 */
161602c9c7b9SMisael Lopez Cruz 	if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
16170562f788SMika Westerberg 		/*
16180562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
16190562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
16200562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
16210562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
16220562f788SMika Westerberg 		 *
16230562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
16240562f788SMika Westerberg 		 */
162502c9c7b9SMisael Lopez Cruz 		if (!codec->ac97_created)
16260562f788SMika Westerberg 			return 0;
1627f0fba2adSLiam Girdwood 
162802c9c7b9SMisael Lopez Cruz 		ret = soc_ac97_dev_register(codec);
1629f0fba2adSLiam Girdwood 		if (ret < 0) {
163002c9c7b9SMisael Lopez Cruz 			dev_err(codec->dev,
1631f110bfc7SLiam Girdwood 				"ASoC: AC97 device register failed: %d\n", ret);
1632f0fba2adSLiam Girdwood 			return ret;
1633435c5e25SMark Brown 		}
1634435c5e25SMark Brown 
163502c9c7b9SMisael Lopez Cruz 		codec->ac97_registered = 1;
1636f0fba2adSLiam Girdwood 	}
1637f0fba2adSLiam Girdwood 	return 0;
1638f0fba2adSLiam Girdwood }
1639435c5e25SMark Brown 
164002c9c7b9SMisael Lopez Cruz static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
164102c9c7b9SMisael Lopez Cruz {
164202c9c7b9SMisael Lopez Cruz 	return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
164302c9c7b9SMisael Lopez Cruz }
164402c9c7b9SMisael Lopez Cruz 
164502c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1646f0fba2adSLiam Girdwood {
1647f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1648f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1649f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1650f0fba2adSLiam Girdwood 	}
1651f0fba2adSLiam Girdwood }
165202c9c7b9SMisael Lopez Cruz 
165302c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
165402c9c7b9SMisael Lopez Cruz {
165502c9c7b9SMisael Lopez Cruz 	soc_unregister_ac97_codec(rtd->codec);
165602c9c7b9SMisael Lopez Cruz }
1657f0fba2adSLiam Girdwood #endif
1658435c5e25SMark Brown 
16596b0a0b3bSLars-Peter Clausen static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card,
16606b0a0b3bSLars-Peter Clausen 	int num)
1661b19e6e7bSMark Brown {
1662b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1663b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1664b19e6e7bSMark Brown 
1665b19e6e7bSMark Brown 	/* find CODEC from registered CODECs */
1666b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
16673ca041edSSebastian Reichel 		if (aux_dev->codec_of_node &&
16683ca041edSSebastian Reichel 		   (codec->dev->of_node != aux_dev->codec_of_node))
16693ca041edSSebastian Reichel 			continue;
16703ca041edSSebastian Reichel 		if (aux_dev->codec_name && strcmp(codec->name, aux_dev->codec_name))
16713ca041edSSebastian Reichel 			continue;
16723ca041edSSebastian Reichel 		return codec;
1673b19e6e7bSMark Brown 	}
1674b19e6e7bSMark Brown 
16753ca041edSSebastian Reichel 	return NULL;
16763ca041edSSebastian Reichel }
1677fb099cb7SMark Brown 
16783ca041edSSebastian Reichel static int soc_check_aux_dev(struct snd_soc_card *card, int num)
16793ca041edSSebastian Reichel {
16803ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
16813ca041edSSebastian Reichel 	const char *codecname = aux_dev->codec_name;
16823ca041edSSebastian Reichel 	struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
16833ca041edSSebastian Reichel 
16843ca041edSSebastian Reichel 	if (codec)
16853ca041edSSebastian Reichel 		return 0;
16863ca041edSSebastian Reichel 	if (aux_dev->codec_of_node)
16873ca041edSSebastian Reichel 		codecname = of_node_full_name(aux_dev->codec_of_node);
16883ca041edSSebastian Reichel 
16893ca041edSSebastian Reichel 	dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1690b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1691b19e6e7bSMark Brown }
1692b19e6e7bSMark Brown 
16932eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
16942eea392dSJarkko Nikula {
16952eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
16963ca041edSSebastian Reichel 	const char *codecname = aux_dev->codec_name;
1697676ad98aSJarkko Nikula 	int ret = -ENODEV;
16983ca041edSSebastian Reichel 	struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
16992eea392dSJarkko Nikula 
17003ca041edSSebastian Reichel 	if (!codec) {
17013ca041edSSebastian Reichel 		if (aux_dev->codec_of_node)
17023ca041edSSebastian Reichel 			codecname = of_node_full_name(aux_dev->codec_of_node);
17033ca041edSSebastian Reichel 
1704676ad98aSJarkko Nikula 		/* codec not found */
17053ca041edSSebastian Reichel 		dev_err(card->dev, "ASoC: codec %s not found", codecname);
1706b19e6e7bSMark Brown 		return -EPROBE_DEFER;
17073ca041edSSebastian Reichel 	}
17082eea392dSJarkko Nikula 
17093ca041edSSebastian Reichel 	if (codec->probed) {
17103ca041edSSebastian Reichel 		dev_err(codec->dev, "ASoC: codec already probed");
17113ca041edSSebastian Reichel 		return -EBUSY;
17123ca041edSSebastian Reichel 	}
17133ca041edSSebastian Reichel 
1714589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
17152eea392dSJarkko Nikula 	if (ret < 0)
1716589c3563SJarkko Nikula 		return ret;
17172eea392dSJarkko Nikula 
1718589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
17192eea392dSJarkko Nikula 
17202eea392dSJarkko Nikula 	return ret;
17212eea392dSJarkko Nikula }
17222eea392dSJarkko Nikula 
17232eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
17242eea392dSJarkko Nikula {
17252eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
17262eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
17272eea392dSJarkko Nikula 
17282eea392dSJarkko Nikula 	/* unregister the rtd device */
17292eea392dSJarkko Nikula 	if (rtd->dev_registered) {
173036ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1731d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
17322eea392dSJarkko Nikula 		rtd->dev_registered = 0;
17332eea392dSJarkko Nikula 	}
17342eea392dSJarkko Nikula 
1735589c3563SJarkko Nikula 	if (codec && codec->probed)
1736589c3563SJarkko Nikula 		soc_remove_codec(codec);
17372eea392dSJarkko Nikula }
17382eea392dSJarkko Nikula 
1739f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1740fdf0f54dSDimitris Papastamos {
1741fdf0f54dSDimitris Papastamos 	int ret;
1742fdf0f54dSDimitris Papastamos 
1743fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1744fdf0f54dSDimitris Papastamos 		return 0;
1745fdf0f54dSDimitris Papastamos 
1746fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1747fdf0f54dSDimitris Papastamos 	if (ret < 0) {
174810e8aa9aSMichał Mirosław 		dev_err(codec->dev,
174910e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
175010e8aa9aSMichał Mirosław 			ret);
1751fdf0f54dSDimitris Papastamos 		return ret;
1752fdf0f54dSDimitris Papastamos 	}
1753fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1754fdf0f54dSDimitris Papastamos 	return 0;
1755fdf0f54dSDimitris Papastamos }
1756fdf0f54dSDimitris Papastamos 
1757b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1758f0fba2adSLiam Girdwood {
1759fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
176075d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1761f04209a7SMark Brown 	int ret, i, order, dai_fmt;
176296dd3622SMark Brown 
176301b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1764f0fba2adSLiam Girdwood 
1765b19e6e7bSMark Brown 	/* bind DAIs */
1766b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1767b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1768b19e6e7bSMark Brown 		if (ret != 0)
1769b19e6e7bSMark Brown 			goto base_error;
1770db2a4165SFrank Mandarino 	}
1771db2a4165SFrank Mandarino 
1772b19e6e7bSMark Brown 	/* check aux_devs too */
1773b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1774b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1775b19e6e7bSMark Brown 		if (ret != 0)
1776b19e6e7bSMark Brown 			goto base_error;
1777db2a4165SFrank Mandarino 	}
1778db2a4165SFrank Mandarino 
1779fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1780fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1781fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1782fdf0f54dSDimitris Papastamos 			continue;
1783f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1784b19e6e7bSMark Brown 		if (ret < 0)
1785b19e6e7bSMark Brown 			goto base_error;
1786fdf0f54dSDimitris Papastamos 	}
1787fdf0f54dSDimitris Papastamos 
1788f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1789102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1790f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1791f0fba2adSLiam Girdwood 	if (ret < 0) {
179210e8aa9aSMichał Mirosław 		dev_err(card->dev,
179310e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
179410e8aa9aSMichał Mirosław 			card->name, ret);
1795b19e6e7bSMark Brown 		goto base_error;
1796db2a4165SFrank Mandarino 	}
1797db2a4165SFrank Mandarino 
1798e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1799e37a4970SMark Brown 	card->dapm.dev = card->dev;
1800e37a4970SMark Brown 	card->dapm.card = card;
1801e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1802e37a4970SMark Brown 
1803d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1804d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1805d5d1e0beSLars-Peter Clausen #endif
1806d5d1e0beSLars-Peter Clausen 
180788ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
18086ed25978SAndy Green 	/* deferred resume work */
18096308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
18101301a964SRandy Dunlap #endif
18116ed25978SAndy Green 
18129a841ebbSMark Brown 	if (card->dapm_widgets)
18139a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
18149a841ebbSMark Brown 					  card->num_dapm_widgets);
18159a841ebbSMark Brown 
1816f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1817f0fba2adSLiam Girdwood 	if (card->probe) {
1818e7361ec4SMark Brown 		ret = card->probe(card);
1819f0fba2adSLiam Girdwood 		if (ret < 0)
1820f0fba2adSLiam Girdwood 			goto card_probe_error;
1821f0fba2adSLiam Girdwood 	}
1822f0fba2adSLiam Girdwood 
182362ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
18240168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
18250168bf0dSLiam Girdwood 			order++) {
1826fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
182762ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
182862ae68faSStephen Warren 			if (ret < 0) {
1829f110bfc7SLiam Girdwood 				dev_err(card->dev,
1830f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1831f110bfc7SLiam Girdwood 					ret);
183262ae68faSStephen Warren 				goto probe_dai_err;
183362ae68faSStephen Warren 			}
183462ae68faSStephen Warren 		}
183562ae68faSStephen Warren 	}
183662ae68faSStephen Warren 
183762ae68faSStephen Warren 	/* probe all DAI links on this card */
183862ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
183962ae68faSStephen Warren 			order++) {
184062ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
184162ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1842fe3e78e0SMark Brown 			if (ret < 0) {
1843f110bfc7SLiam Girdwood 				dev_err(card->dev,
1844f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1845f110bfc7SLiam Girdwood 					ret);
1846f0fba2adSLiam Girdwood 				goto probe_dai_err;
1847fe3e78e0SMark Brown 			}
1848fe3e78e0SMark Brown 		}
18490168bf0dSLiam Girdwood 	}
1850fe3e78e0SMark Brown 
18512eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
18522eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
18532eea392dSJarkko Nikula 		if (ret < 0) {
1854f110bfc7SLiam Girdwood 			dev_err(card->dev,
1855f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1856f110bfc7SLiam Girdwood 				ret);
18572eea392dSJarkko Nikula 			goto probe_aux_dev_err;
18582eea392dSJarkko Nikula 		}
18592eea392dSJarkko Nikula 	}
18602eea392dSJarkko Nikula 
1861888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1862b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1863888df395SMark Brown 
1864b7af1dafSMark Brown 	if (card->controls)
1865022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1866b7af1dafSMark Brown 
1867b8ad29deSMark Brown 	if (card->dapm_routes)
1868b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1869b8ad29deSMark Brown 					card->num_dapm_routes);
1870b8ad29deSMark Brown 
187175d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
187275d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1873f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
187475d9ac46SMark Brown 
1875f04209a7SMark Brown 		if (dai_fmt) {
187675d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1877f04209a7SMark Brown 						  dai_fmt);
18785e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
187975d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
1880f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
188175d9ac46SMark Brown 					 ret);
1882f04209a7SMark Brown 		}
1883f04209a7SMark Brown 
1884f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1885fe33d4c5SStephen Warren 		if (dai_fmt &&
1886fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1887f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1888f04209a7SMark Brown 						  dai_fmt);
1889f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1890f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1891f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
1892f04209a7SMark Brown 					 ret);
1893f04209a7SMark Brown 		} else if (dai_fmt) {
1894f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1895f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1896f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1897f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1898f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1899f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1900f04209a7SMark Brown 				break;
1901f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1902f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1903f04209a7SMark Brown 				break;
1904f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1905f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1906f04209a7SMark Brown 				break;
1907f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1908f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1909f04209a7SMark Brown 				break;
1910f04209a7SMark Brown 			}
191175d9ac46SMark Brown 
191275d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1913f04209a7SMark Brown 						  dai_fmt);
19145e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
191575d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1916f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
191775d9ac46SMark Brown 					 ret);
191875d9ac46SMark Brown 		}
191975d9ac46SMark Brown 	}
192075d9ac46SMark Brown 
1921f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1922fe3e78e0SMark Brown 		 "%s", card->name);
1923f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
192422de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1925f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1926f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1927f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1928f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1929f0e8ed85SMark Brown 		case '_':
1930f0e8ed85SMark Brown 		case '-':
1931f0e8ed85SMark Brown 		case '\0':
1932f0e8ed85SMark Brown 			break;
1933f0e8ed85SMark Brown 		default:
1934f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1935f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1936f0e8ed85SMark Brown 			break;
1937f0e8ed85SMark Brown 		}
1938f0e8ed85SMark Brown 	}
1939fe3e78e0SMark Brown 
194028e9ad92SMark Brown 	if (card->late_probe) {
194128e9ad92SMark Brown 		ret = card->late_probe(card);
194228e9ad92SMark Brown 		if (ret < 0) {
1943f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
194428e9ad92SMark Brown 				card->name, ret);
194528e9ad92SMark Brown 			goto probe_aux_dev_err;
194628e9ad92SMark Brown 		}
194728e9ad92SMark Brown 	}
194828e9ad92SMark Brown 
19491633281bSStephen Warren 	if (card->fully_routed)
1950b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
19511633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
19521633281bSStephen Warren 
1953824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
19548c193b8dSLars-Peter Clausen 
1955f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1956fe3e78e0SMark Brown 	if (ret < 0) {
1957f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1958f110bfc7SLiam Girdwood 				ret);
19596b3ed785SAxel Lin 		goto probe_aux_dev_err;
1960fe3e78e0SMark Brown 	}
1961fe3e78e0SMark Brown 
1962fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1963f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1964f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1965f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1966fe3e78e0SMark Brown 		if (ret < 0) {
196710e8aa9aSMichał Mirosław 			dev_err(card->dev,
196810e8aa9aSMichał Mirosław 				"ASoC: failed to register AC97: %d\n", ret);
19696b3ed785SAxel Lin 			while (--i >= 0)
197002c9c7b9SMisael Lopez Cruz 				soc_unregister_ac97_dai_link(&card->rtd[i]);
19716b3ed785SAxel Lin 			goto probe_aux_dev_err;
1972fe3e78e0SMark Brown 		}
1973fe3e78e0SMark Brown 	}
1974fe3e78e0SMark Brown #endif
1975fe3e78e0SMark Brown 
1976435c5e25SMark Brown 	card->instantiated = 1;
19774f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1978f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1979b19e6e7bSMark Brown 
1980b19e6e7bSMark Brown 	return 0;
1981db2a4165SFrank Mandarino 
19822eea392dSJarkko Nikula probe_aux_dev_err:
19832eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
19842eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
19852eea392dSJarkko Nikula 
1986f0fba2adSLiam Girdwood probe_dai_err:
19870671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1988fe3e78e0SMark Brown 
1989f0fba2adSLiam Girdwood card_probe_error:
199087506549SMark Brown 	if (card->remove)
1991e7361ec4SMark Brown 		card->remove(card);
1992f0fba2adSLiam Girdwood 
1993f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1994f0fba2adSLiam Girdwood 
1995b19e6e7bSMark Brown base_error:
1996f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1997db2a4165SFrank Mandarino 
1998b19e6e7bSMark Brown 	return ret;
1999435c5e25SMark Brown }
2000435c5e25SMark Brown 
2001435c5e25SMark Brown /* probes a new socdev */
2002435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2003435c5e25SMark Brown {
2004f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2005435c5e25SMark Brown 
200670a7ca34SVinod Koul 	/*
200770a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
200870a7ca34SVinod Koul 	 * we should not be here in that case so ret error
200970a7ca34SVinod Koul 	 */
201070a7ca34SVinod Koul 	if (!card)
201170a7ca34SVinod Koul 		return -EINVAL;
201270a7ca34SVinod Koul 
2013fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2014f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2015fe4085e8SMark Brown 		 card->name);
2016fe4085e8SMark Brown 
2017435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2018435c5e25SMark Brown 	card->dev = &pdev->dev;
2019f0fba2adSLiam Girdwood 
202028d528c8SMark Brown 	return snd_soc_register_card(card);
2021435c5e25SMark Brown }
2022435c5e25SMark Brown 
2023b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2024db2a4165SFrank Mandarino {
2025db2a4165SFrank Mandarino 	int i;
2026db2a4165SFrank Mandarino 
2027f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
2028f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2029f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
203043829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2031db2a4165SFrank Mandarino 	}
2032db2a4165SFrank Mandarino 
20332eea392dSJarkko Nikula 	/* remove auxiliary devices */
20342eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
20352eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
20362eea392dSJarkko Nikula 
2037f0fba2adSLiam Girdwood 	/* remove and free each DAI */
20380671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2039f0fba2adSLiam Girdwood 
2040a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2041a6052154SJarkko Nikula 
2042f0fba2adSLiam Girdwood 	/* remove the card */
204387506549SMark Brown 	if (card->remove)
2044e7361ec4SMark Brown 		card->remove(card);
2045f0fba2adSLiam Girdwood 
20460aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
20470aaae527SLars-Peter Clausen 
2048f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2049b0e26485SVinod Koul 	return 0;
2050b0e26485SVinod Koul 
2051b2dfa62cSGuennadi Liakhovetski }
2052b0e26485SVinod Koul 
2053b0e26485SVinod Koul /* removes a socdev */
2054b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2055b0e26485SVinod Koul {
2056b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2057b0e26485SVinod Koul 
2058c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2059db2a4165SFrank Mandarino 	return 0;
2060db2a4165SFrank Mandarino }
2061db2a4165SFrank Mandarino 
20626f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
206351737470SMark Brown {
20646f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
2065f0fba2adSLiam Girdwood 	int i;
206651737470SMark Brown 
206751737470SMark Brown 	if (!card->instantiated)
2068416356fcSMark Brown 		return 0;
206951737470SMark Brown 
207051737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
207151737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
2072f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2073f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
207443829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2075f0fba2adSLiam Girdwood 	}
207651737470SMark Brown 
2077f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2078416356fcSMark Brown 
2079988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2080988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
2081988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2082988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2083988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(codec_dai->dev);
2084988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
2085988e8cc4SNicolin Chen 	}
2086988e8cc4SNicolin Chen 
2087416356fcSMark Brown 	return 0;
208851737470SMark Brown }
20896f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
209051737470SMark Brown 
20916f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2092b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2093b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2094b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2095b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
20966f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2097b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2098416356fcSMark Brown };
2099deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2100416356fcSMark Brown 
2101db2a4165SFrank Mandarino /* ASoC platform driver */
2102db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2103db2a4165SFrank Mandarino 	.driver		= {
2104db2a4165SFrank Mandarino 		.name		= "soc-audio",
21058b45a209SKay Sievers 		.owner		= THIS_MODULE,
21066f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2107db2a4165SFrank Mandarino 	},
2108db2a4165SFrank Mandarino 	.probe		= soc_probe,
2109db2a4165SFrank Mandarino 	.remove		= soc_remove,
2110db2a4165SFrank Mandarino };
2111db2a4165SFrank Mandarino 
2112096e49d5SMark Brown /**
2113db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2114db2a4165SFrank Mandarino  * @codec: audio codec
2115db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2116db2a4165SFrank Mandarino  * @num: AC97 codec number
2117db2a4165SFrank Mandarino  *
2118db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2119db2a4165SFrank Mandarino  */
2120db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2121db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2122db2a4165SFrank Mandarino {
2123db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2124db2a4165SFrank Mandarino 
2125db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2126db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2127db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2128db2a4165SFrank Mandarino 		return -ENOMEM;
2129db2a4165SFrank Mandarino 	}
2130db2a4165SFrank Mandarino 
2131db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2132db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2133db2a4165SFrank Mandarino 		kfree(codec->ac97);
2134db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2135db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2136db2a4165SFrank Mandarino 		return -ENOMEM;
2137db2a4165SFrank Mandarino 	}
2138db2a4165SFrank Mandarino 
2139db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2140db2a4165SFrank Mandarino 	codec->ac97->num = num;
21410562f788SMika Westerberg 
21420562f788SMika Westerberg 	/*
21430562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
21440562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
21450562f788SMika Westerberg 	 */
21460562f788SMika Westerberg 	codec->ac97_created = 1;
21470562f788SMika Westerberg 
2148db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2149db2a4165SFrank Mandarino 	return 0;
2150db2a4165SFrank Mandarino }
2151db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2152db2a4165SFrank Mandarino 
2153741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2154741a509fSMarkus Pargmann 
2155741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2156741a509fSMarkus Pargmann {
2157741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2158741a509fSMarkus Pargmann 
2159741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2160741a509fSMarkus Pargmann 
2161741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2162741a509fSMarkus Pargmann 
2163741a509fSMarkus Pargmann 	udelay(10);
2164741a509fSMarkus Pargmann 
2165741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2166741a509fSMarkus Pargmann 
2167741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2168741a509fSMarkus Pargmann 	msleep(2);
2169741a509fSMarkus Pargmann }
2170741a509fSMarkus Pargmann 
2171741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2172741a509fSMarkus Pargmann {
2173741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2174741a509fSMarkus Pargmann 
2175741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2176741a509fSMarkus Pargmann 
2177741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2178741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2179741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2180741a509fSMarkus Pargmann 
2181741a509fSMarkus Pargmann 	udelay(10);
2182741a509fSMarkus Pargmann 
2183741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2184741a509fSMarkus Pargmann 
2185741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2186741a509fSMarkus Pargmann 	msleep(2);
2187741a509fSMarkus Pargmann }
2188741a509fSMarkus Pargmann 
2189741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev,
2190741a509fSMarkus Pargmann 		struct snd_ac97_reset_cfg *cfg)
2191741a509fSMarkus Pargmann {
2192741a509fSMarkus Pargmann 	struct pinctrl *p;
2193741a509fSMarkus Pargmann 	struct pinctrl_state *state;
2194741a509fSMarkus Pargmann 	int gpio;
2195741a509fSMarkus Pargmann 	int ret;
2196741a509fSMarkus Pargmann 
2197741a509fSMarkus Pargmann 	p = devm_pinctrl_get(dev);
2198741a509fSMarkus Pargmann 	if (IS_ERR(p)) {
2199741a509fSMarkus Pargmann 		dev_err(dev, "Failed to get pinctrl\n");
2200b7580cdeSChristoph Jaeger 		return PTR_ERR(p);
2201741a509fSMarkus Pargmann 	}
2202741a509fSMarkus Pargmann 	cfg->pctl = p;
2203741a509fSMarkus Pargmann 
2204741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-reset");
2205741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2206741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2207b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2208741a509fSMarkus Pargmann 	}
2209741a509fSMarkus Pargmann 	cfg->pstate_reset = state;
2210741a509fSMarkus Pargmann 
2211741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
2212741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2213741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2214b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2215741a509fSMarkus Pargmann 	}
2216741a509fSMarkus Pargmann 	cfg->pstate_warm_reset = state;
2217741a509fSMarkus Pargmann 
2218741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-running");
2219741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2220741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
2221b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2222741a509fSMarkus Pargmann 	}
2223741a509fSMarkus Pargmann 	cfg->pstate_run = state;
2224741a509fSMarkus Pargmann 
2225741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2226741a509fSMarkus Pargmann 	if (gpio < 0) {
2227741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sync gpio\n");
2228741a509fSMarkus Pargmann 		return gpio;
2229741a509fSMarkus Pargmann 	}
2230741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2231741a509fSMarkus Pargmann 	if (ret) {
2232741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
2233741a509fSMarkus Pargmann 		return ret;
2234741a509fSMarkus Pargmann 	}
2235741a509fSMarkus Pargmann 	cfg->gpio_sync = gpio;
2236741a509fSMarkus Pargmann 
2237741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2238741a509fSMarkus Pargmann 	if (gpio < 0) {
2239741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2240741a509fSMarkus Pargmann 		return gpio;
2241741a509fSMarkus Pargmann 	}
2242741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2243741a509fSMarkus Pargmann 	if (ret) {
2244741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2245741a509fSMarkus Pargmann 		return ret;
2246741a509fSMarkus Pargmann 	}
2247741a509fSMarkus Pargmann 	cfg->gpio_sdata = gpio;
2248741a509fSMarkus Pargmann 
2249741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2250741a509fSMarkus Pargmann 	if (gpio < 0) {
2251741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-reset gpio\n");
2252741a509fSMarkus Pargmann 		return gpio;
2253741a509fSMarkus Pargmann 	}
2254741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2255741a509fSMarkus Pargmann 	if (ret) {
2256741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
2257741a509fSMarkus Pargmann 		return ret;
2258741a509fSMarkus Pargmann 	}
2259741a509fSMarkus Pargmann 	cfg->gpio_reset = gpio;
2260741a509fSMarkus Pargmann 
2261741a509fSMarkus Pargmann 	return 0;
2262741a509fSMarkus Pargmann }
2263741a509fSMarkus Pargmann 
2264b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops;
2265f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops);
2266b047e1ccSMark Brown 
2267b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2268b047e1ccSMark Brown {
2269b047e1ccSMark Brown 	if (ops == soc_ac97_ops)
2270b047e1ccSMark Brown 		return 0;
2271b047e1ccSMark Brown 
2272b047e1ccSMark Brown 	if (soc_ac97_ops && ops)
2273b047e1ccSMark Brown 		return -EBUSY;
2274b047e1ccSMark Brown 
2275b047e1ccSMark Brown 	soc_ac97_ops = ops;
2276b047e1ccSMark Brown 
2277b047e1ccSMark Brown 	return 0;
2278b047e1ccSMark Brown }
2279b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2280b047e1ccSMark Brown 
2281db2a4165SFrank Mandarino /**
2282741a509fSMarkus Pargmann  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2283741a509fSMarkus Pargmann  *
2284741a509fSMarkus Pargmann  * This function sets the reset and warm_reset properties of ops and parses
2285741a509fSMarkus Pargmann  * the device node of pdev to get pinctrl states and gpio numbers to use.
2286741a509fSMarkus Pargmann  */
2287741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2288741a509fSMarkus Pargmann 		struct platform_device *pdev)
2289741a509fSMarkus Pargmann {
2290741a509fSMarkus Pargmann 	struct device *dev = &pdev->dev;
2291741a509fSMarkus Pargmann 	struct snd_ac97_reset_cfg cfg;
2292741a509fSMarkus Pargmann 	int ret;
2293741a509fSMarkus Pargmann 
2294741a509fSMarkus Pargmann 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2295741a509fSMarkus Pargmann 	if (ret)
2296741a509fSMarkus Pargmann 		return ret;
2297741a509fSMarkus Pargmann 
2298741a509fSMarkus Pargmann 	ret = snd_soc_set_ac97_ops(ops);
2299741a509fSMarkus Pargmann 	if (ret)
2300741a509fSMarkus Pargmann 		return ret;
2301741a509fSMarkus Pargmann 
2302741a509fSMarkus Pargmann 	ops->warm_reset = snd_soc_ac97_warm_reset;
2303741a509fSMarkus Pargmann 	ops->reset = snd_soc_ac97_reset;
2304741a509fSMarkus Pargmann 
2305741a509fSMarkus Pargmann 	snd_ac97_rst_cfg = cfg;
2306741a509fSMarkus Pargmann 	return 0;
2307741a509fSMarkus Pargmann }
2308741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2309741a509fSMarkus Pargmann 
2310741a509fSMarkus Pargmann /**
2311db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2312db2a4165SFrank Mandarino  * @codec: audio codec
2313db2a4165SFrank Mandarino  *
2314db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2315db2a4165SFrank Mandarino  */
2316db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2317db2a4165SFrank Mandarino {
2318db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2319f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
232002c9c7b9SMisael Lopez Cruz 	soc_unregister_ac97_codec(codec);
2321f0fba2adSLiam Girdwood #endif
2322db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2323db2a4165SFrank Mandarino 	kfree(codec->ac97);
2324db2a4165SFrank Mandarino 	codec->ac97 = NULL;
23250562f788SMika Westerberg 	codec->ac97_created = 0;
2326db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2327db2a4165SFrank Mandarino }
2328db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2329db2a4165SFrank Mandarino 
2330db2a4165SFrank Mandarino /**
2331db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2332db2a4165SFrank Mandarino  * @_template: control template
2333db2a4165SFrank Mandarino  * @data: control private data
2334ac11a2b3SMark Brown  * @long_name: control long name
2335efb7ac3fSMark Brown  * @prefix: control name prefix
2336db2a4165SFrank Mandarino  *
2337db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2338db2a4165SFrank Mandarino  *
2339db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2340db2a4165SFrank Mandarino  */
2341db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
23423056557fSMark Brown 				  void *data, const char *long_name,
2343efb7ac3fSMark Brown 				  const char *prefix)
2344db2a4165SFrank Mandarino {
2345db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2346efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2347efb7ac3fSMark Brown 	char *name = NULL;
2348db2a4165SFrank Mandarino 
2349db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2350db2a4165SFrank Mandarino 	template.index = 0;
2351db2a4165SFrank Mandarino 
2352efb7ac3fSMark Brown 	if (!long_name)
2353efb7ac3fSMark Brown 		long_name = template.name;
2354efb7ac3fSMark Brown 
2355efb7ac3fSMark Brown 	if (prefix) {
23562b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2357efb7ac3fSMark Brown 		if (!name)
2358efb7ac3fSMark Brown 			return NULL;
2359efb7ac3fSMark Brown 
2360efb7ac3fSMark Brown 		template.name = name;
2361efb7ac3fSMark Brown 	} else {
2362efb7ac3fSMark Brown 		template.name = long_name;
2363efb7ac3fSMark Brown 	}
2364efb7ac3fSMark Brown 
2365efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2366efb7ac3fSMark Brown 
2367efb7ac3fSMark Brown 	kfree(name);
2368efb7ac3fSMark Brown 
2369efb7ac3fSMark Brown 	return kcontrol;
2370db2a4165SFrank Mandarino }
2371db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2372db2a4165SFrank Mandarino 
2373022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2374022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2375022658beSLiam Girdwood 	const char *prefix, void *data)
2376022658beSLiam Girdwood {
2377022658beSLiam Girdwood 	int err, i;
2378022658beSLiam Girdwood 
2379022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2380022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2381022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2382022658beSLiam Girdwood 						     control->name, prefix));
2383022658beSLiam Girdwood 		if (err < 0) {
2384f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2385f110bfc7SLiam Girdwood 				control->name, err);
2386022658beSLiam Girdwood 			return err;
2387022658beSLiam Girdwood 		}
2388022658beSLiam Girdwood 	}
2389022658beSLiam Girdwood 
2390022658beSLiam Girdwood 	return 0;
2391022658beSLiam Girdwood }
2392022658beSLiam Girdwood 
23934fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23944fefd698SDimitris Papastamos 					       const char *name)
23954fefd698SDimitris Papastamos {
23964fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23974fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23984fefd698SDimitris Papastamos 
23994fefd698SDimitris Papastamos 	if (unlikely(!name))
24004fefd698SDimitris Papastamos 		return NULL;
24014fefd698SDimitris Papastamos 
24024fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
24034fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
24044fefd698SDimitris Papastamos 			return kctl;
24054fefd698SDimitris Papastamos 	return NULL;
24064fefd698SDimitris Papastamos }
24074fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
24084fefd698SDimitris Papastamos 
2409db2a4165SFrank Mandarino /**
2410022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2411022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
24123e8e1952SIan Molton  * duplicating this code.
24133e8e1952SIan Molton  *
24143e8e1952SIan Molton  * @codec: codec to add controls to
24153e8e1952SIan Molton  * @controls: array of controls to add
24163e8e1952SIan Molton  * @num_controls: number of elements in the array
24173e8e1952SIan Molton  *
24183e8e1952SIan Molton  * Return 0 for success, else error.
24193e8e1952SIan Molton  */
2420022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
24213e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
24223e8e1952SIan Molton {
2423f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
24243e8e1952SIan Molton 
2425022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2426907fe36aSLars-Peter Clausen 			codec->name_prefix, &codec->component);
24273e8e1952SIan Molton }
2428022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
24293e8e1952SIan Molton 
24303e8e1952SIan Molton /**
2431a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2432022658beSLiam Girdwood  * Convenience function to add a list of controls.
2433a491a5c8SLiam Girdwood  *
2434a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2435a491a5c8SLiam Girdwood  * @controls: array of controls to add
2436a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2437a491a5c8SLiam Girdwood  *
2438a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2439a491a5c8SLiam Girdwood  */
2440a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2441a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2442a491a5c8SLiam Girdwood {
2443a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2444a491a5c8SLiam Girdwood 
2445022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2446907fe36aSLars-Peter Clausen 			NULL, &platform->component);
2447a491a5c8SLiam Girdwood }
2448a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2449a491a5c8SLiam Girdwood 
2450a491a5c8SLiam Girdwood /**
2451022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2452022658beSLiam Girdwood  * Convenience function to add a list of controls.
2453022658beSLiam Girdwood  *
2454022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2455022658beSLiam Girdwood  * @controls: array of controls to add
2456022658beSLiam Girdwood  * @num_controls: number of elements in the array
2457022658beSLiam Girdwood  *
2458022658beSLiam Girdwood  * Return 0 for success, else error.
2459022658beSLiam Girdwood  */
2460022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2461022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2462022658beSLiam Girdwood {
2463022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2464022658beSLiam Girdwood 
2465022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2466022658beSLiam Girdwood 			NULL, soc_card);
2467022658beSLiam Girdwood }
2468022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2469022658beSLiam Girdwood 
2470022658beSLiam Girdwood /**
2471022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2472022658beSLiam Girdwood  * Convienience function to add a list of controls.
2473022658beSLiam Girdwood  *
2474022658beSLiam Girdwood  * @dai: DAI to add controls to
2475022658beSLiam Girdwood  * @controls: array of controls to add
2476022658beSLiam Girdwood  * @num_controls: number of elements in the array
2477022658beSLiam Girdwood  *
2478022658beSLiam Girdwood  * Return 0 for success, else error.
2479022658beSLiam Girdwood  */
2480022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2481022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2482022658beSLiam Girdwood {
2483022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2484022658beSLiam Girdwood 
2485022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2486022658beSLiam Girdwood 			NULL, dai);
2487022658beSLiam Girdwood }
2488022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2489022658beSLiam Girdwood 
2490022658beSLiam Girdwood /**
2491db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2492db2a4165SFrank Mandarino  * @kcontrol: mixer control
2493db2a4165SFrank Mandarino  * @uinfo: control element information
2494db2a4165SFrank Mandarino  *
2495db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2496db2a4165SFrank Mandarino  * mixer control.
2497db2a4165SFrank Mandarino  *
2498db2a4165SFrank Mandarino  * Returns 0 for success.
2499db2a4165SFrank Mandarino  */
2500db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2501db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2502db2a4165SFrank Mandarino {
2503db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2504db2a4165SFrank Mandarino 
2505db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2506db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
25079a8d38dbSTakashi Iwai 	uinfo->value.enumerated.items = e->items;
2508db2a4165SFrank Mandarino 
25099a8d38dbSTakashi Iwai 	if (uinfo->value.enumerated.item >= e->items)
25109a8d38dbSTakashi Iwai 		uinfo->value.enumerated.item = e->items - 1;
2511a19685cbSTakashi Iwai 	strlcpy(uinfo->value.enumerated.name,
2512a19685cbSTakashi Iwai 		e->texts[uinfo->value.enumerated.item],
2513a19685cbSTakashi Iwai 		sizeof(uinfo->value.enumerated.name));
2514db2a4165SFrank Mandarino 	return 0;
2515db2a4165SFrank Mandarino }
2516db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2517db2a4165SFrank Mandarino 
2518db2a4165SFrank Mandarino /**
2519db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2520db2a4165SFrank Mandarino  * @kcontrol: mixer control
2521ac11a2b3SMark Brown  * @ucontrol: control element information
2522db2a4165SFrank Mandarino  *
2523db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2524db2a4165SFrank Mandarino  *
2525db2a4165SFrank Mandarino  * Returns 0 for success.
2526db2a4165SFrank Mandarino  */
2527db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2528db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2529db2a4165SFrank Mandarino {
2530907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2531db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
253229ae2fa5SLars-Peter Clausen 	unsigned int val, item;
253329ae2fa5SLars-Peter Clausen 	unsigned int reg_val;
2534907fe36aSLars-Peter Clausen 	int ret;
2535db2a4165SFrank Mandarino 
2536907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, e->reg, &reg_val);
2537907fe36aSLars-Peter Clausen 	if (ret)
2538907fe36aSLars-Peter Clausen 		return ret;
253929ae2fa5SLars-Peter Clausen 	val = (reg_val >> e->shift_l) & e->mask;
254029ae2fa5SLars-Peter Clausen 	item = snd_soc_enum_val_to_item(e, val);
254129ae2fa5SLars-Peter Clausen 	ucontrol->value.enumerated.item[0] = item;
254229ae2fa5SLars-Peter Clausen 	if (e->shift_l != e->shift_r) {
254329ae2fa5SLars-Peter Clausen 		val = (reg_val >> e->shift_l) & e->mask;
254429ae2fa5SLars-Peter Clausen 		item = snd_soc_enum_val_to_item(e, val);
254529ae2fa5SLars-Peter Clausen 		ucontrol->value.enumerated.item[1] = item;
254629ae2fa5SLars-Peter Clausen 	}
2547db2a4165SFrank Mandarino 
2548db2a4165SFrank Mandarino 	return 0;
2549db2a4165SFrank Mandarino }
2550db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2551db2a4165SFrank Mandarino 
2552db2a4165SFrank Mandarino /**
2553db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2554db2a4165SFrank Mandarino  * @kcontrol: mixer control
2555ac11a2b3SMark Brown  * @ucontrol: control element information
2556db2a4165SFrank Mandarino  *
2557db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2558db2a4165SFrank Mandarino  *
2559db2a4165SFrank Mandarino  * Returns 0 for success.
2560db2a4165SFrank Mandarino  */
2561db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2562db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2563db2a4165SFrank Mandarino {
2564907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2565db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
256629ae2fa5SLars-Peter Clausen 	unsigned int *item = ucontrol->value.enumerated.item;
256746f5822fSDaniel Ribeiro 	unsigned int val;
256886767b7dSLars-Peter Clausen 	unsigned int mask;
2569db2a4165SFrank Mandarino 
257029ae2fa5SLars-Peter Clausen 	if (item[0] >= e->items)
2571db2a4165SFrank Mandarino 		return -EINVAL;
257229ae2fa5SLars-Peter Clausen 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
257386767b7dSLars-Peter Clausen 	mask = e->mask << e->shift_l;
2574db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
257529ae2fa5SLars-Peter Clausen 		if (item[1] >= e->items)
2576db2a4165SFrank Mandarino 			return -EINVAL;
257729ae2fa5SLars-Peter Clausen 		val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
257886767b7dSLars-Peter Clausen 		mask |= e->mask << e->shift_r;
2579db2a4165SFrank Mandarino 	}
2580db2a4165SFrank Mandarino 
2581907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, e->reg, mask, val);
2582db2a4165SFrank Mandarino }
2583db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2584db2a4165SFrank Mandarino 
2585db2a4165SFrank Mandarino /**
2586f227b88fSMarkus Pargmann  * snd_soc_read_signed - Read a codec register and interprete as signed value
2587907fe36aSLars-Peter Clausen  * @component: component
2588f227b88fSMarkus Pargmann  * @reg: Register to read
2589f227b88fSMarkus Pargmann  * @mask: Mask to use after shifting the register value
2590f227b88fSMarkus Pargmann  * @shift: Right shift of register value
2591f227b88fSMarkus Pargmann  * @sign_bit: Bit that describes if a number is negative or not.
2592907fe36aSLars-Peter Clausen  * @signed_val: Pointer to where the read value should be stored
25932e72f8e3SPeter Ujfalusi  *
2594f227b88fSMarkus Pargmann  * This functions reads a codec register. The register value is shifted right
2595f227b88fSMarkus Pargmann  * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2596f227b88fSMarkus Pargmann  * the given registervalue into a signed integer if sign_bit is non-zero.
25972e72f8e3SPeter Ujfalusi  *
2598907fe36aSLars-Peter Clausen  * Returns 0 on sucess, otherwise an error value
25992e72f8e3SPeter Ujfalusi  */
2600907fe36aSLars-Peter Clausen static int snd_soc_read_signed(struct snd_soc_component *component,
2601907fe36aSLars-Peter Clausen 	unsigned int reg, unsigned int mask, unsigned int shift,
2602907fe36aSLars-Peter Clausen 	unsigned int sign_bit, int *signed_val)
26032e72f8e3SPeter Ujfalusi {
2604f227b88fSMarkus Pargmann 	int ret;
260546f5822fSDaniel Ribeiro 	unsigned int val;
26062e72f8e3SPeter Ujfalusi 
2607907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2608907fe36aSLars-Peter Clausen 	if (ret < 0)
2609907fe36aSLars-Peter Clausen 		return ret;
26102e72f8e3SPeter Ujfalusi 
2611907fe36aSLars-Peter Clausen 	val = (val >> shift) & mask;
2612907fe36aSLars-Peter Clausen 
2613907fe36aSLars-Peter Clausen 	if (!sign_bit) {
2614907fe36aSLars-Peter Clausen 		*signed_val = val;
2615907fe36aSLars-Peter Clausen 		return 0;
2616907fe36aSLars-Peter Clausen 	}
2617f227b88fSMarkus Pargmann 
2618f227b88fSMarkus Pargmann 	/* non-negative number */
2619907fe36aSLars-Peter Clausen 	if (!(val & BIT(sign_bit))) {
2620907fe36aSLars-Peter Clausen 		*signed_val = val;
2621907fe36aSLars-Peter Clausen 		return 0;
2622907fe36aSLars-Peter Clausen 	}
2623f227b88fSMarkus Pargmann 
2624f227b88fSMarkus Pargmann 	ret = val;
2625f227b88fSMarkus Pargmann 
2626f227b88fSMarkus Pargmann 	/*
2627f227b88fSMarkus Pargmann 	 * The register most probably does not contain a full-sized int.
2628f227b88fSMarkus Pargmann 	 * Instead we have an arbitrary number of bits in a signed
2629f227b88fSMarkus Pargmann 	 * representation which has to be translated into a full-sized int.
2630f227b88fSMarkus Pargmann 	 * This is done by filling up all bits above the sign-bit.
2631f227b88fSMarkus Pargmann 	 */
2632f227b88fSMarkus Pargmann 	ret |= ~((int)(BIT(sign_bit) - 1));
2633f227b88fSMarkus Pargmann 
2634907fe36aSLars-Peter Clausen 	*signed_val = ret;
2635907fe36aSLars-Peter Clausen 
2636907fe36aSLars-Peter Clausen 	return 0;
26372e72f8e3SPeter Ujfalusi }
26382e72f8e3SPeter Ujfalusi 
26392e72f8e3SPeter Ujfalusi /**
2640db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2641db2a4165SFrank Mandarino  * @kcontrol: mixer control
2642db2a4165SFrank Mandarino  * @uinfo: control element information
2643db2a4165SFrank Mandarino  *
2644e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2645e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2646db2a4165SFrank Mandarino  *
2647db2a4165SFrank Mandarino  * Returns 0 for success.
2648db2a4165SFrank Mandarino  */
2649db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2650db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2651db2a4165SFrank Mandarino {
26524eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26534eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2654d11bb4a9SPeter Ujfalusi 	int platform_max;
2655db2a4165SFrank Mandarino 
2656d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2657d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2658d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2659d11bb4a9SPeter Ujfalusi 
2660d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2661a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2662a7a4ac86SPhilipp Zabel 	else
2663a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2664a7a4ac86SPhilipp Zabel 
2665e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2666db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2667f227b88fSMarkus Pargmann 	uinfo->value.integer.max = platform_max - mc->min;
2668db2a4165SFrank Mandarino 	return 0;
2669db2a4165SFrank Mandarino }
2670db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2671db2a4165SFrank Mandarino 
2672db2a4165SFrank Mandarino /**
2673db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2674db2a4165SFrank Mandarino  * @kcontrol: mixer control
2675ac11a2b3SMark Brown  * @ucontrol: control element information
2676db2a4165SFrank Mandarino  *
2677f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2678f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2679db2a4165SFrank Mandarino  *
2680db2a4165SFrank Mandarino  * Returns 0 for success.
2681db2a4165SFrank Mandarino  */
2682db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2683db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2684db2a4165SFrank Mandarino {
2685907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
26864eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26874eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2688815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2689f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2690815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2691815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26924eaa9819SJon Smirl 	int max = mc->max;
2693f227b88fSMarkus Pargmann 	int min = mc->min;
2694f227b88fSMarkus Pargmann 	int sign_bit = mc->sign_bit;
2695815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2696815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2697907fe36aSLars-Peter Clausen 	int val;
2698907fe36aSLars-Peter Clausen 	int ret;
2699db2a4165SFrank Mandarino 
2700f227b88fSMarkus Pargmann 	if (sign_bit)
2701f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2702f227b88fSMarkus Pargmann 
2703907fe36aSLars-Peter Clausen 	ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2704907fe36aSLars-Peter Clausen 	if (ret)
2705907fe36aSLars-Peter Clausen 		return ret;
2706907fe36aSLars-Peter Clausen 
2707907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = val - min;
2708f7915d99SPeter Ujfalusi 	if (invert)
2709db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2710a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2711f7915d99SPeter Ujfalusi 
2712f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2713f7915d99SPeter Ujfalusi 		if (reg == reg2)
2714907fe36aSLars-Peter Clausen 			ret = snd_soc_read_signed(component, reg, mask, rshift,
2715907fe36aSLars-Peter Clausen 				sign_bit, &val);
2716f7915d99SPeter Ujfalusi 		else
2717907fe36aSLars-Peter Clausen 			ret = snd_soc_read_signed(component, reg2, mask, shift,
2718907fe36aSLars-Peter Clausen 				sign_bit, &val);
2719907fe36aSLars-Peter Clausen 		if (ret)
2720907fe36aSLars-Peter Clausen 			return ret;
2721907fe36aSLars-Peter Clausen 
2722907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = val - min;
2723f7915d99SPeter Ujfalusi 		if (invert)
2724db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2725a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2726db2a4165SFrank Mandarino 	}
2727db2a4165SFrank Mandarino 
2728db2a4165SFrank Mandarino 	return 0;
2729db2a4165SFrank Mandarino }
2730db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2731db2a4165SFrank Mandarino 
2732db2a4165SFrank Mandarino /**
2733db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2734db2a4165SFrank Mandarino  * @kcontrol: mixer control
2735ac11a2b3SMark Brown  * @ucontrol: control element information
2736db2a4165SFrank Mandarino  *
2737974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2738974815baSPeter Ujfalusi  * control that spans 2 registers.
2739db2a4165SFrank Mandarino  *
2740db2a4165SFrank Mandarino  * Returns 0 for success.
2741db2a4165SFrank Mandarino  */
2742db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2743db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2744db2a4165SFrank Mandarino {
2745907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
27464eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27474eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2748815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2749974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2750815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2751815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
27524eaa9819SJon Smirl 	int max = mc->max;
2753f227b88fSMarkus Pargmann 	int min = mc->min;
2754f227b88fSMarkus Pargmann 	unsigned int sign_bit = mc->sign_bit;
2755815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2756815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2757974815baSPeter Ujfalusi 	int err;
2758939d9f16SNenghua Cao 	bool type_2r = false;
2759974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2760974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2761db2a4165SFrank Mandarino 
2762f227b88fSMarkus Pargmann 	if (sign_bit)
2763f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2764f227b88fSMarkus Pargmann 
2765f227b88fSMarkus Pargmann 	val = ((ucontrol->value.integer.value[0] + min) & mask);
2766db2a4165SFrank Mandarino 	if (invert)
2767a7a4ac86SPhilipp Zabel 		val = max - val;
2768db2a4165SFrank Mandarino 	val_mask = mask << shift;
2769db2a4165SFrank Mandarino 	val = val << shift;
2770974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2771f227b88fSMarkus Pargmann 		val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2772db2a4165SFrank Mandarino 		if (invert)
2773a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2774974815baSPeter Ujfalusi 		if (reg == reg2) {
2775db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2776db2a4165SFrank Mandarino 			val |= val2 << rshift;
2777974815baSPeter Ujfalusi 		} else {
2778db2a4165SFrank Mandarino 			val2 = val2 << shift;
2779939d9f16SNenghua Cao 			type_2r = true;
2780974815baSPeter Ujfalusi 		}
2781974815baSPeter Ujfalusi 	}
2782907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
27833ff3f64bSMark Brown 	if (err < 0)
2784db2a4165SFrank Mandarino 		return err;
2785db2a4165SFrank Mandarino 
2786974815baSPeter Ujfalusi 	if (type_2r)
2787907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, reg2, val_mask,
2788907fe36aSLars-Peter Clausen 			val2);
2789974815baSPeter Ujfalusi 
2790db2a4165SFrank Mandarino 	return err;
2791db2a4165SFrank Mandarino }
2792974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2793db2a4165SFrank Mandarino 
2794e13ac2e9SMark Brown /**
27951d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
27961d99f243SBrian Austin  * @kcontrol: mixer control
27971d99f243SBrian Austin  * @ucontrol: control element information
27981d99f243SBrian Austin  *
27991d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
28001d99f243SBrian Austin  * control that spans 2 registers.
28011d99f243SBrian Austin  *
28021d99f243SBrian Austin  * Returns 0 for success.
28031d99f243SBrian Austin  */
28041d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
28051d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
28061d99f243SBrian Austin {
2807907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
28081d99f243SBrian Austin 	struct soc_mixer_control *mc =
28091d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
28101d99f243SBrian Austin 	unsigned int reg = mc->reg;
28111d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28121d99f243SBrian Austin 	unsigned int shift = mc->shift;
28131d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28141d99f243SBrian Austin 	int max = mc->max;
28151d99f243SBrian Austin 	int min = mc->min;
28161d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
2817907fe36aSLars-Peter Clausen 	unsigned int val;
2818907fe36aSLars-Peter Clausen 	int ret;
28191d99f243SBrian Austin 
2820907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2821907fe36aSLars-Peter Clausen 	if (ret < 0)
2822907fe36aSLars-Peter Clausen 		return ret;
28231d99f243SBrian Austin 
2824907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2825907fe36aSLars-Peter Clausen 
2826907fe36aSLars-Peter Clausen 	if (snd_soc_volsw_is_stereo(mc)) {
2827907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, reg2, &val);
2828907fe36aSLars-Peter Clausen 		if (ret < 0)
2829907fe36aSLars-Peter Clausen 			return ret;
2830907fe36aSLars-Peter Clausen 
2831907fe36aSLars-Peter Clausen 		val = ((val >> rshift) - min) & mask;
2832907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = val;
2833907fe36aSLars-Peter Clausen 	}
28341d99f243SBrian Austin 
28351d99f243SBrian Austin 	return 0;
28361d99f243SBrian Austin }
28371d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
28381d99f243SBrian Austin 
28391d99f243SBrian Austin /**
28401d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
28411d99f243SBrian Austin  * @kcontrol: mixer control
28421d99f243SBrian Austin  * @uinfo: control element information
28431d99f243SBrian Austin  *
28441d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
28451d99f243SBrian Austin  *
28461d99f243SBrian Austin  * Returns 0 for success.
28471d99f243SBrian Austin  */
28481d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
28491d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
28501d99f243SBrian Austin {
2851907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
28521d99f243SBrian Austin 	struct soc_mixer_control *mc =
28531d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
28541d99f243SBrian Austin 
28551d99f243SBrian Austin 	unsigned int reg = mc->reg;
28561d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28571d99f243SBrian Austin 	unsigned int shift = mc->shift;
28581d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28591d99f243SBrian Austin 	int max = mc->max;
28601d99f243SBrian Austin 	int min = mc->min;
28611d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
286227f1d759SBrian Austin 	int err = 0;
28631a39019eSBard Liao 	unsigned int val, val_mask, val2 = 0;
28641d99f243SBrian Austin 
28651d99f243SBrian Austin 	val_mask = mask << shift;
28661d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
28671d99f243SBrian Austin 	val = val << shift;
28681d99f243SBrian Austin 
2869907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
2870d055852eSMukund Navada 	if (err < 0)
28711d99f243SBrian Austin 		return err;
28721d99f243SBrian Austin 
28731d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
28741d99f243SBrian Austin 		val_mask = mask << rshift;
28751d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
28761d99f243SBrian Austin 		val2 = val2 << rshift;
28771d99f243SBrian Austin 
2878907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, reg2, val_mask,
2879907fe36aSLars-Peter Clausen 			val2);
28801d99f243SBrian Austin 	}
2881907fe36aSLars-Peter Clausen 	return err;
28821d99f243SBrian Austin }
28831d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
28841d99f243SBrian Austin 
28851d99f243SBrian Austin /**
2886e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2887e13ac2e9SMark Brown  * @kcontrol: mixer control
2888e13ac2e9SMark Brown  * @uinfo: control element information
2889e13ac2e9SMark Brown  *
2890e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2891e13ac2e9SMark Brown  *
2892e13ac2e9SMark Brown  * Returns 0 for success.
2893e13ac2e9SMark Brown  */
2894e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2895e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2896e13ac2e9SMark Brown {
28974eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28984eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2899d11bb4a9SPeter Ujfalusi 	int platform_max;
29004eaa9819SJon Smirl 	int min = mc->min;
2901e13ac2e9SMark Brown 
2902d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2903d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2904d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2905d11bb4a9SPeter Ujfalusi 
2906e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2907e13ac2e9SMark Brown 	uinfo->count = 2;
2908e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2909d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2910e13ac2e9SMark Brown 	return 0;
2911e13ac2e9SMark Brown }
2912e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2913e13ac2e9SMark Brown 
2914e13ac2e9SMark Brown /**
2915e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2916e13ac2e9SMark Brown  * @kcontrol: mixer control
2917ac11a2b3SMark Brown  * @ucontrol: control element information
2918e13ac2e9SMark Brown  *
2919e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2920e13ac2e9SMark Brown  *
2921e13ac2e9SMark Brown  * Returns 0 for success.
2922e13ac2e9SMark Brown  */
2923e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2924e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2925e13ac2e9SMark Brown {
29264eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29274eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2928907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2929815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2930907fe36aSLars-Peter Clausen 	unsigned int val;
29314eaa9819SJon Smirl 	int min = mc->min;
2932907fe36aSLars-Peter Clausen 	int ret;
2933907fe36aSLars-Peter Clausen 
2934907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2935907fe36aSLars-Peter Clausen 	if (ret)
2936907fe36aSLars-Peter Clausen 		return ret;
2937e13ac2e9SMark Brown 
2938e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2939e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2940e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2941e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2942e13ac2e9SMark Brown 	return 0;
2943e13ac2e9SMark Brown }
2944e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2945e13ac2e9SMark Brown 
2946e13ac2e9SMark Brown /**
2947e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2948e13ac2e9SMark Brown  * @kcontrol: mixer control
2949ac11a2b3SMark Brown  * @ucontrol: control element information
2950e13ac2e9SMark Brown  *
2951e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2952e13ac2e9SMark Brown  *
2953e13ac2e9SMark Brown  * Returns 0 for success.
2954e13ac2e9SMark Brown  */
2955e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2956e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2957e13ac2e9SMark Brown {
29584eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29594eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2960907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2961815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
29624eaa9819SJon Smirl 	int min = mc->min;
296346f5822fSDaniel Ribeiro 	unsigned int val;
2964e13ac2e9SMark Brown 
2965e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2966e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2967e13ac2e9SMark Brown 
2968907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, reg, 0xffff, val);
2969e13ac2e9SMark Brown }
2970e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2971e13ac2e9SMark Brown 
29728c6529dbSLiam Girdwood /**
29736c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
29746c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29756c9d8cf6SAdam Thomson  * @uinfo: control element information
29766c9d8cf6SAdam Thomson  *
29776c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
29786c9d8cf6SAdam Thomson  * mixer control.
29796c9d8cf6SAdam Thomson  *
29806c9d8cf6SAdam Thomson  * returns 0 for success.
29816c9d8cf6SAdam Thomson  */
29826c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
29836c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
29846c9d8cf6SAdam Thomson {
29856c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29866c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29876c9d8cf6SAdam Thomson 	int platform_max;
29886c9d8cf6SAdam Thomson 	int min = mc->min;
29896c9d8cf6SAdam Thomson 
29906c9d8cf6SAdam Thomson 	if (!mc->platform_max)
29916c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
29926c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
29936c9d8cf6SAdam Thomson 
29946c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29959bde4f0bSMark Brown 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
29966c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
29976c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
29986c9d8cf6SAdam Thomson 
29996c9d8cf6SAdam Thomson 	return 0;
30006c9d8cf6SAdam Thomson }
30016c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
30026c9d8cf6SAdam Thomson 
30036c9d8cf6SAdam Thomson /**
30046c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
30056c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30066c9d8cf6SAdam Thomson  * @ucontrol: control element information
30076c9d8cf6SAdam Thomson  *
30086c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
30096c9d8cf6SAdam Thomson  *
30106c9d8cf6SAdam Thomson  * Returns 0 for success.
30116c9d8cf6SAdam Thomson  */
30126c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
30136c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30146c9d8cf6SAdam Thomson {
30156c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30166c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
3017907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
30186c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
30199bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
30206c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
30216c9d8cf6SAdam Thomson 	int min = mc->min;
30226c9d8cf6SAdam Thomson 	int max = mc->max;
30236c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
30246c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
30256c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
30269bde4f0bSMark Brown 	int ret;
30276c9d8cf6SAdam Thomson 
30286c9d8cf6SAdam Thomson 	val = ((ucontrol->value.integer.value[0] + min) & mask);
30296c9d8cf6SAdam Thomson 	if (invert)
30306c9d8cf6SAdam Thomson 		val = max - val;
30316c9d8cf6SAdam Thomson 	val_mask = mask << shift;
30326c9d8cf6SAdam Thomson 	val = val << shift;
30336c9d8cf6SAdam Thomson 
3034907fe36aSLars-Peter Clausen 	ret = snd_soc_component_update_bits(component, reg, val_mask, val);
30350eaa6ccaSJoonyoung Shim 	if (ret < 0)
30369bde4f0bSMark Brown 		return ret;
30379bde4f0bSMark Brown 
30389bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
30399bde4f0bSMark Brown 		val = ((ucontrol->value.integer.value[1] + min) & mask);
30409bde4f0bSMark Brown 		if (invert)
30419bde4f0bSMark Brown 			val = max - val;
30429bde4f0bSMark Brown 		val_mask = mask << shift;
30439bde4f0bSMark Brown 		val = val << shift;
30449bde4f0bSMark Brown 
3045907fe36aSLars-Peter Clausen 		ret = snd_soc_component_update_bits(component, rreg, val_mask,
3046907fe36aSLars-Peter Clausen 			val);
30479bde4f0bSMark Brown 	}
30489bde4f0bSMark Brown 
30499bde4f0bSMark Brown 	return ret;
30506c9d8cf6SAdam Thomson }
30516c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
30526c9d8cf6SAdam Thomson 
30536c9d8cf6SAdam Thomson /**
30546c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
30556c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30566c9d8cf6SAdam Thomson  * @ucontrol: control element information
30576c9d8cf6SAdam Thomson  *
30586c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
30596c9d8cf6SAdam Thomson  *
30606c9d8cf6SAdam Thomson  * Returns 0 for success.
30616c9d8cf6SAdam Thomson  */
30626c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
30636c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30646c9d8cf6SAdam Thomson {
3065907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
30666c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30676c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
30686c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
30699bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
30706c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
30716c9d8cf6SAdam Thomson 	int min = mc->min;
30726c9d8cf6SAdam Thomson 	int max = mc->max;
30736c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
30746c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
3075907fe36aSLars-Peter Clausen 	unsigned int val;
3076907fe36aSLars-Peter Clausen 	int ret;
30776c9d8cf6SAdam Thomson 
3078907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
3079907fe36aSLars-Peter Clausen 	if (ret)
3080907fe36aSLars-Peter Clausen 		return ret;
3081907fe36aSLars-Peter Clausen 
3082907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = (val >> shift) & mask;
30836c9d8cf6SAdam Thomson 	if (invert)
30846c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
30856c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
30866c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
30876c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] - min;
30886c9d8cf6SAdam Thomson 
30899bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
3090907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, rreg, &val);
3091907fe36aSLars-Peter Clausen 		if (ret)
3092907fe36aSLars-Peter Clausen 			return ret;
3093907fe36aSLars-Peter Clausen 
3094907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = (val >> shift) & mask;
30959bde4f0bSMark Brown 		if (invert)
30969bde4f0bSMark Brown 			ucontrol->value.integer.value[1] =
30979bde4f0bSMark Brown 				max - ucontrol->value.integer.value[1];
30989bde4f0bSMark Brown 		ucontrol->value.integer.value[1] =
30999bde4f0bSMark Brown 			ucontrol->value.integer.value[1] - min;
31009bde4f0bSMark Brown 	}
31019bde4f0bSMark Brown 
31026c9d8cf6SAdam Thomson 	return 0;
31036c9d8cf6SAdam Thomson }
31046c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
31056c9d8cf6SAdam Thomson 
31066c9d8cf6SAdam Thomson /**
3107637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
3108637d3847SPeter Ujfalusi  *
3109637d3847SPeter Ujfalusi  * @codec: where to look for the control
3110637d3847SPeter Ujfalusi  * @name: Name of the control
3111637d3847SPeter Ujfalusi  * @max: new maximum limit
3112637d3847SPeter Ujfalusi  *
3113637d3847SPeter Ujfalusi  * Return 0 for success, else error.
3114637d3847SPeter Ujfalusi  */
3115637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
3116637d3847SPeter Ujfalusi 	const char *name, int max)
3117637d3847SPeter Ujfalusi {
3118f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
3119637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
3120637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
3121637d3847SPeter Ujfalusi 	int found = 0;
3122637d3847SPeter Ujfalusi 	int ret = -EINVAL;
3123637d3847SPeter Ujfalusi 
3124637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
3125637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
3126637d3847SPeter Ujfalusi 		return -EINVAL;
3127637d3847SPeter Ujfalusi 
3128637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
3129637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3130637d3847SPeter Ujfalusi 			found = 1;
3131637d3847SPeter Ujfalusi 			break;
3132637d3847SPeter Ujfalusi 		}
3133637d3847SPeter Ujfalusi 	}
3134637d3847SPeter Ujfalusi 	if (found) {
3135637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3136637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3137d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3138637d3847SPeter Ujfalusi 			ret = 0;
3139637d3847SPeter Ujfalusi 		}
3140637d3847SPeter Ujfalusi 	}
3141637d3847SPeter Ujfalusi 	return ret;
3142637d3847SPeter Ujfalusi }
3143637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3144637d3847SPeter Ujfalusi 
314571d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
314671d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
314771d08516SMark Brown {
3148907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
314971d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
315071d08516SMark Brown 
315171d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3152907fe36aSLars-Peter Clausen 	uinfo->count = params->num_regs * component->val_bytes;
315371d08516SMark Brown 
315471d08516SMark Brown 	return 0;
315571d08516SMark Brown }
315671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
315771d08516SMark Brown 
315871d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
315971d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
316071d08516SMark Brown {
3161907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
316271d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
316371d08516SMark Brown 	int ret;
316471d08516SMark Brown 
3165907fe36aSLars-Peter Clausen 	if (component->regmap)
3166907fe36aSLars-Peter Clausen 		ret = regmap_raw_read(component->regmap, params->base,
316771d08516SMark Brown 				      ucontrol->value.bytes.data,
3168907fe36aSLars-Peter Clausen 				      params->num_regs * component->val_bytes);
316971d08516SMark Brown 	else
317071d08516SMark Brown 		ret = -EINVAL;
317171d08516SMark Brown 
3172f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
3173f831b055SMark Brown 	if (ret == 0 && params->mask) {
3174907fe36aSLars-Peter Clausen 		switch (component->val_bytes) {
3175f831b055SMark Brown 		case 1:
3176f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
3177f831b055SMark Brown 			break;
3178f831b055SMark Brown 		case 2:
3179f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
31808f1ec93aSCharles Keepax 				&= cpu_to_be16(~params->mask);
3181f831b055SMark Brown 			break;
3182f831b055SMark Brown 		case 4:
3183f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
31848f1ec93aSCharles Keepax 				&= cpu_to_be32(~params->mask);
3185f831b055SMark Brown 			break;
3186f831b055SMark Brown 		default:
3187f831b055SMark Brown 			return -EINVAL;
3188f831b055SMark Brown 		}
3189f831b055SMark Brown 	}
3190f831b055SMark Brown 
319171d08516SMark Brown 	return ret;
319271d08516SMark Brown }
319371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
319471d08516SMark Brown 
319571d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
319671d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
319771d08516SMark Brown {
3198907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
319971d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
3200f831b055SMark Brown 	int ret, len;
3201a1a564edSNenghua Cao 	unsigned int val, mask;
3202f831b055SMark Brown 	void *data;
320371d08516SMark Brown 
3204907fe36aSLars-Peter Clausen 	if (!component->regmap)
3205f831b055SMark Brown 		return -EINVAL;
3206f831b055SMark Brown 
3207907fe36aSLars-Peter Clausen 	len = params->num_regs * component->val_bytes;
3208f831b055SMark Brown 
3209b5a8fe43SMark Brown 	data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3210b5a8fe43SMark Brown 	if (!data)
3211b5a8fe43SMark Brown 		return -ENOMEM;
3212b5a8fe43SMark Brown 
3213f831b055SMark Brown 	/*
3214f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3215f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3216f831b055SMark Brown 	 * copy.
3217f831b055SMark Brown 	 */
3218f831b055SMark Brown 	if (params->mask) {
3219907fe36aSLars-Peter Clausen 		ret = regmap_read(component->regmap, params->base, &val);
3220f831b055SMark Brown 		if (ret != 0)
3221e8b18addSWei Yongjun 			goto out;
3222f831b055SMark Brown 
3223f831b055SMark Brown 		val &= params->mask;
3224f831b055SMark Brown 
3225907fe36aSLars-Peter Clausen 		switch (component->val_bytes) {
3226f831b055SMark Brown 		case 1:
3227f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3228f831b055SMark Brown 			((u8 *)data)[0] |= val;
3229f831b055SMark Brown 			break;
3230f831b055SMark Brown 		case 2:
3231a1a564edSNenghua Cao 			mask = ~params->mask;
3232907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3233a1a564edSNenghua Cao 							&mask, &mask);
3234a1a564edSNenghua Cao 			if (ret != 0)
3235a1a564edSNenghua Cao 				goto out;
3236a1a564edSNenghua Cao 
3237a1a564edSNenghua Cao 			((u16 *)data)[0] &= mask;
3238a1a564edSNenghua Cao 
3239907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3240a1a564edSNenghua Cao 							&val, &val);
3241a1a564edSNenghua Cao 			if (ret != 0)
3242a1a564edSNenghua Cao 				goto out;
3243a1a564edSNenghua Cao 
3244a1a564edSNenghua Cao 			((u16 *)data)[0] |= val;
3245f831b055SMark Brown 			break;
3246f831b055SMark Brown 		case 4:
3247a1a564edSNenghua Cao 			mask = ~params->mask;
3248907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3249a1a564edSNenghua Cao 							&mask, &mask);
3250a1a564edSNenghua Cao 			if (ret != 0)
3251a1a564edSNenghua Cao 				goto out;
3252a1a564edSNenghua Cao 
3253a1a564edSNenghua Cao 			((u32 *)data)[0] &= mask;
3254a1a564edSNenghua Cao 
3255907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3256a1a564edSNenghua Cao 							&val, &val);
3257a1a564edSNenghua Cao 			if (ret != 0)
3258a1a564edSNenghua Cao 				goto out;
3259a1a564edSNenghua Cao 
3260a1a564edSNenghua Cao 			((u32 *)data)[0] |= val;
3261f831b055SMark Brown 			break;
3262f831b055SMark Brown 		default:
3263e8b18addSWei Yongjun 			ret = -EINVAL;
3264e8b18addSWei Yongjun 			goto out;
3265f831b055SMark Brown 		}
3266f831b055SMark Brown 	}
3267f831b055SMark Brown 
3268907fe36aSLars-Peter Clausen 	ret = regmap_raw_write(component->regmap, params->base,
3269f831b055SMark Brown 			       data, len);
3270f831b055SMark Brown 
3271e8b18addSWei Yongjun out:
3272f831b055SMark Brown 	kfree(data);
327371d08516SMark Brown 
327471d08516SMark Brown 	return ret;
327571d08516SMark Brown }
327671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
327771d08516SMark Brown 
3278d9881208SVinod Koul int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3279d9881208SVinod Koul 			struct snd_ctl_elem_info *ucontrol)
3280d9881208SVinod Koul {
3281d9881208SVinod Koul 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3282d9881208SVinod Koul 
3283d9881208SVinod Koul 	ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3284d9881208SVinod Koul 	ucontrol->count = params->max;
3285d9881208SVinod Koul 
3286d9881208SVinod Koul 	return 0;
3287d9881208SVinod Koul }
3288d9881208SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3289d9881208SVinod Koul 
3290b6f4bb38Sapatard@mandriva.com /**
32914183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
32924183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
32934183eed2SKristoffer KARLSSON  * @uinfo: control element information
32944183eed2SKristoffer KARLSSON  *
32954183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
32964183eed2SKristoffer KARLSSON  * span multiple codec registers which together
32974183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
32984183eed2SKristoffer KARLSSON  *
32994183eed2SKristoffer KARLSSON  * Returns 0 for success.
33004183eed2SKristoffer KARLSSON  */
33014183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
33024183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
33034183eed2SKristoffer KARLSSON {
33044183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33054183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33064183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
33074183eed2SKristoffer KARLSSON 	uinfo->count = 1;
33084183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
33094183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
33104183eed2SKristoffer KARLSSON 
33114183eed2SKristoffer KARLSSON 	return 0;
33124183eed2SKristoffer KARLSSON }
33134183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
33144183eed2SKristoffer KARLSSON 
33154183eed2SKristoffer KARLSSON /**
33164183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
33174183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33184183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33194183eed2SKristoffer KARLSSON  *
33204183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
33214183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33224183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33234183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33244183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33254183eed2SKristoffer KARLSSON  *
33264183eed2SKristoffer KARLSSON  * Returns 0 for success.
33274183eed2SKristoffer KARLSSON  */
33284183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
33294183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33304183eed2SKristoffer KARLSSON {
3331907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
33324183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33334183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33344183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33354183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
3336907fe36aSLars-Peter Clausen 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
33374183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33384183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33394183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33404183eed2SKristoffer KARLSSON 	long min = mc->min;
33414183eed2SKristoffer KARLSSON 	long max = mc->max;
33424183eed2SKristoffer KARLSSON 	long val = 0;
3343907fe36aSLars-Peter Clausen 	unsigned int regval;
33444183eed2SKristoffer KARLSSON 	unsigned int i;
3345907fe36aSLars-Peter Clausen 	int ret;
33464183eed2SKristoffer KARLSSON 
33474183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
3348907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, regbase+i, &regval);
3349907fe36aSLars-Peter Clausen 		if (ret)
3350907fe36aSLars-Peter Clausen 			return ret;
3351907fe36aSLars-Peter Clausen 		val |= (regval & regwmask) << (regwshift*(regcount-i-1));
33524183eed2SKristoffer KARLSSON 	}
33534183eed2SKristoffer KARLSSON 	val &= mask;
33544183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
33554183eed2SKristoffer KARLSSON 		val |= ~mask;
33564183eed2SKristoffer KARLSSON 	if (invert)
33574183eed2SKristoffer KARLSSON 		val = max - val;
33584183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
33594183eed2SKristoffer KARLSSON 
33604183eed2SKristoffer KARLSSON 	return 0;
33614183eed2SKristoffer KARLSSON }
33624183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
33634183eed2SKristoffer KARLSSON 
33644183eed2SKristoffer KARLSSON /**
33654183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
33664183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33674183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33684183eed2SKristoffer KARLSSON  *
33694183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
33704183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33714183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33724183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33734183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33744183eed2SKristoffer KARLSSON  *
33754183eed2SKristoffer KARLSSON  * Returns 0 for success.
33764183eed2SKristoffer KARLSSON  */
33774183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
33784183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33794183eed2SKristoffer KARLSSON {
3380907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
33814183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33824183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33834183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33844183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
3385907fe36aSLars-Peter Clausen 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
33864183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33874183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33884183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33894183eed2SKristoffer KARLSSON 	long max = mc->max;
33904183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
33914183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
33924183eed2SKristoffer KARLSSON 	int err;
33934183eed2SKristoffer KARLSSON 
33944183eed2SKristoffer KARLSSON 	if (invert)
33954183eed2SKristoffer KARLSSON 		val = max - val;
33964183eed2SKristoffer KARLSSON 	val &= mask;
33974183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
33984183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
33994183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3400907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, regbase+i,
34014183eed2SKristoffer KARLSSON 				regmask, regval);
34024183eed2SKristoffer KARLSSON 		if (err < 0)
34034183eed2SKristoffer KARLSSON 			return err;
34044183eed2SKristoffer KARLSSON 	}
34054183eed2SKristoffer KARLSSON 
34064183eed2SKristoffer KARLSSON 	return 0;
34074183eed2SKristoffer KARLSSON }
34084183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
34094183eed2SKristoffer KARLSSON 
34104183eed2SKristoffer KARLSSON /**
3411dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3412dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3413dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3414dd7b10b3SKristoffer KARLSSON  *
3415dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3416dd7b10b3SKristoffer KARLSSON  *
3417dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3418dd7b10b3SKristoffer KARLSSON  */
3419dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3420dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3421dd7b10b3SKristoffer KARLSSON {
3422907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3423dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3424dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3425dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3426dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3427dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3428dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3429907fe36aSLars-Peter Clausen 	unsigned int val;
3430907fe36aSLars-Peter Clausen 	int ret;
3431907fe36aSLars-Peter Clausen 
3432907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
3433907fe36aSLars-Peter Clausen 	if (ret)
3434907fe36aSLars-Peter Clausen 		return ret;
3435907fe36aSLars-Peter Clausen 
3436907fe36aSLars-Peter Clausen 	val &= mask;
3437dd7b10b3SKristoffer KARLSSON 
3438dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3439dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3440dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3441dd7b10b3SKristoffer KARLSSON 
3442dd7b10b3SKristoffer KARLSSON 	return 0;
3443dd7b10b3SKristoffer KARLSSON }
3444dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3445dd7b10b3SKristoffer KARLSSON 
3446dd7b10b3SKristoffer KARLSSON /**
3447dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3448dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3449dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3450dd7b10b3SKristoffer KARLSSON  *
3451dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3452dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3453dd7b10b3SKristoffer KARLSSON  *
3454dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3455dd7b10b3SKristoffer KARLSSON  */
3456dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3457dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3458dd7b10b3SKristoffer KARLSSON {
3459907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3460dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3461dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3462dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3463dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3464dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3465dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3466dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3467dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3468dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3469dd7b10b3SKristoffer KARLSSON 	int err;
3470dd7b10b3SKristoffer KARLSSON 
3471907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, mask, val1);
3472dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3473dd7b10b3SKristoffer KARLSSON 		return err;
3474dd7b10b3SKristoffer KARLSSON 
3475907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, reg, mask, val2);
3476dd7b10b3SKristoffer KARLSSON }
3477dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3478dd7b10b3SKristoffer KARLSSON 
3479dd7b10b3SKristoffer KARLSSON /**
34808c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
34818c6529dbSLiam Girdwood  * @dai: DAI
34828c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
34838c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
34848c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
34858c6529dbSLiam Girdwood  *
34868c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
34878c6529dbSLiam Girdwood  */
34888c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
34898c6529dbSLiam Girdwood 	unsigned int freq, int dir)
34908c6529dbSLiam Girdwood {
3491f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3492f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3493ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3494da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3495ec4ee52aSMark Brown 						      freq, dir);
34968c6529dbSLiam Girdwood 	else
34971104a9c8SMark Brown 		return -ENOTSUPP;
34988c6529dbSLiam Girdwood }
34998c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
35008c6529dbSLiam Girdwood 
35018c6529dbSLiam Girdwood /**
3502ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3503ec4ee52aSMark Brown  * @codec: CODEC
3504ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3505da1c6ea6SMark Brown  * @source: Source for the clock
3506ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3507ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3508ec4ee52aSMark Brown  *
3509ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3510ec4ee52aSMark Brown  */
3511ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3512da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3513ec4ee52aSMark Brown {
3514ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3515da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3516da1c6ea6SMark Brown 						 freq, dir);
3517ec4ee52aSMark Brown 	else
35181104a9c8SMark Brown 		return -ENOTSUPP;
3519ec4ee52aSMark Brown }
3520ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3521ec4ee52aSMark Brown 
3522ec4ee52aSMark Brown /**
35238c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
35248c6529dbSLiam Girdwood  * @dai: DAI
3525ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
35268c6529dbSLiam Girdwood  * @div: new clock divisor.
35278c6529dbSLiam Girdwood  *
35288c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
35298c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
35308c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
35318c6529dbSLiam Girdwood  */
35328c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
35338c6529dbSLiam Girdwood 	int div_id, int div)
35348c6529dbSLiam Girdwood {
3535f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3536f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
35378c6529dbSLiam Girdwood 	else
35388c6529dbSLiam Girdwood 		return -EINVAL;
35398c6529dbSLiam Girdwood }
35408c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
35418c6529dbSLiam Girdwood 
35428c6529dbSLiam Girdwood /**
35438c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
35448c6529dbSLiam Girdwood  * @dai: DAI
35458c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
354685488037SMark Brown  * @source: DAI specific source for the PLL
35478c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
35488c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
35498c6529dbSLiam Girdwood  *
35508c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
35518c6529dbSLiam Girdwood  */
355285488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
355385488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
35548c6529dbSLiam Girdwood {
3555f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3556f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
355785488037SMark Brown 					 freq_in, freq_out);
3558ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3559ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3560ec4ee52aSMark Brown 						   freq_in, freq_out);
35618c6529dbSLiam Girdwood 	else
35628c6529dbSLiam Girdwood 		return -EINVAL;
35638c6529dbSLiam Girdwood }
35648c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
35658c6529dbSLiam Girdwood 
3566ec4ee52aSMark Brown /*
3567ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3568ec4ee52aSMark Brown  * @codec: CODEC
3569ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3570ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3571ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3572ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3573ec4ee52aSMark Brown  *
3574ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3575ec4ee52aSMark Brown  */
3576ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3577ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3578ec4ee52aSMark Brown {
3579ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3580ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3581ec4ee52aSMark Brown 					      freq_in, freq_out);
3582ec4ee52aSMark Brown 	else
3583ec4ee52aSMark Brown 		return -EINVAL;
3584ec4ee52aSMark Brown }
3585ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3586ec4ee52aSMark Brown 
35878c6529dbSLiam Girdwood /**
3588e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3589e54cf76bSLiam Girdwood  * @dai: DAI
3590e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
3591e54cf76bSLiam Girdwood  *
3592e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
3593e54cf76bSLiam Girdwood  */
3594e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3595e54cf76bSLiam Girdwood {
3596e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
3597e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
3598e54cf76bSLiam Girdwood 	else
3599e54cf76bSLiam Girdwood 		return -EINVAL;
3600e54cf76bSLiam Girdwood }
3601e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3602e54cf76bSLiam Girdwood 
3603e54cf76bSLiam Girdwood /**
36048c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
36058c6529dbSLiam Girdwood  * @dai: DAI
36068c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
36078c6529dbSLiam Girdwood  *
36088c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
36098c6529dbSLiam Girdwood  */
36108c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
36118c6529dbSLiam Girdwood {
36125e4ba569SShawn Guo 	if (dai->driver == NULL)
36138c6529dbSLiam Girdwood 		return -EINVAL;
36145e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
36155e4ba569SShawn Guo 		return -ENOTSUPP;
36165e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
36178c6529dbSLiam Girdwood }
36188c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
36198c6529dbSLiam Girdwood 
36208c6529dbSLiam Girdwood /**
3621e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
362289c67857SXiubo Li  * @slots: Number of slots in use.
362389c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
362489c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
362589c67857SXiubo Li  *
362689c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
362789c67857SXiubo Li  */
3628e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
362989c67857SXiubo Li 					  unsigned int *tx_mask,
363089c67857SXiubo Li 					  unsigned int *rx_mask)
363189c67857SXiubo Li {
363289c67857SXiubo Li 	if (*tx_mask || *rx_mask)
363389c67857SXiubo Li 		return 0;
363489c67857SXiubo Li 
363589c67857SXiubo Li 	if (!slots)
363689c67857SXiubo Li 		return -EINVAL;
363789c67857SXiubo Li 
363889c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
363989c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
364089c67857SXiubo Li 
364189c67857SXiubo Li 	return 0;
364289c67857SXiubo Li }
364389c67857SXiubo Li 
364489c67857SXiubo Li /**
36458c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
36468c6529dbSLiam Girdwood  * @dai: DAI
3647a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3648a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
36498c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3650a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
36518c6529dbSLiam Girdwood  *
36528c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
36538c6529dbSLiam Girdwood  * specific.
36548c6529dbSLiam Girdwood  */
36558c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3656a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
36578c6529dbSLiam Girdwood {
3658e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3659e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
366089c67857SXiubo Li 						&tx_mask, &rx_mask);
366189c67857SXiubo Li 	else
3662e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
366389c67857SXiubo Li 
3664f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3665f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3666a5479e38SDaniel Ribeiro 				slots, slot_width);
36678c6529dbSLiam Girdwood 	else
3668b2cbb6e1SXiubo Li 		return -ENOTSUPP;
36698c6529dbSLiam Girdwood }
36708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
36718c6529dbSLiam Girdwood 
36728c6529dbSLiam Girdwood /**
3673472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3674472df3cbSBarry Song  * @dai: DAI
3675472df3cbSBarry Song  * @tx_num: how many TX channels
3676472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3677472df3cbSBarry Song  *           0~num-1 uses
3678472df3cbSBarry Song  * @rx_num: how many RX channels
3679472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3680472df3cbSBarry Song  *           0~num-1 uses
3681472df3cbSBarry Song  *
3682472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3683472df3cbSBarry Song  */
3684472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3685472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3686472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3687472df3cbSBarry Song {
3688f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3689f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3690472df3cbSBarry Song 			rx_num, rx_slot);
3691472df3cbSBarry Song 	else
3692472df3cbSBarry Song 		return -EINVAL;
3693472df3cbSBarry Song }
3694472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3695472df3cbSBarry Song 
3696472df3cbSBarry Song /**
36978c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
36988c6529dbSLiam Girdwood  * @dai: DAI
36998c6529dbSLiam Girdwood  * @tristate: tristate enable
37008c6529dbSLiam Girdwood  *
37018c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
37028c6529dbSLiam Girdwood  */
37038c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
37048c6529dbSLiam Girdwood {
3705f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3706f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
37078c6529dbSLiam Girdwood 	else
37088c6529dbSLiam Girdwood 		return -EINVAL;
37098c6529dbSLiam Girdwood }
37108c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
37118c6529dbSLiam Girdwood 
37128c6529dbSLiam Girdwood /**
37138c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
37148c6529dbSLiam Girdwood  * @dai: DAI
37158c6529dbSLiam Girdwood  * @mute: mute enable
3716da18396fSMark Brown  * @direction: stream to mute
37178c6529dbSLiam Girdwood  *
37188c6529dbSLiam Girdwood  * Mutes the DAI DAC.
37198c6529dbSLiam Girdwood  */
3720da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3721da18396fSMark Brown 			     int direction)
37228c6529dbSLiam Girdwood {
3723da18396fSMark Brown 	if (!dai->driver)
3724da18396fSMark Brown 		return -ENOTSUPP;
3725da18396fSMark Brown 
3726da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
3727da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
3728da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3729da18396fSMark Brown 		 dai->driver->ops->digital_mute)
3730f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
37318c6529dbSLiam Girdwood 	else
373204570c62SMark Brown 		return -ENOTSUPP;
37338c6529dbSLiam Girdwood }
37348c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
37358c6529dbSLiam Girdwood 
3736c5af3a2eSMark Brown /**
3737c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3738c5af3a2eSMark Brown  *
3739ac11a2b3SMark Brown  * @card: Card to register
3740c5af3a2eSMark Brown  *
3741c5af3a2eSMark Brown  */
374270a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3743c5af3a2eSMark Brown {
3744b19e6e7bSMark Brown 	int i, ret;
3745f0fba2adSLiam Girdwood 
3746c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3747c5af3a2eSMark Brown 		return -EINVAL;
3748c5af3a2eSMark Brown 
37495a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
37505a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
37515a504963SStephen Warren 
37525a504963SStephen Warren 		/*
37535a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
37545a504963SStephen Warren 		 * not both or neither.
37555a504963SStephen Warren 		 */
37565a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
375710e8aa9aSMichał Mirosław 			dev_err(card->dev,
375810e8aa9aSMichał Mirosław 				"ASoC: Neither/both codec name/of_node are set for %s\n",
375910e8aa9aSMichał Mirosław 				link->name);
37605a504963SStephen Warren 			return -EINVAL;
37615a504963SStephen Warren 		}
3762bc92657aSStephen Warren 		/* Codec DAI name must be specified */
3763bc92657aSStephen Warren 		if (!link->codec_dai_name) {
376410e8aa9aSMichał Mirosław 			dev_err(card->dev,
376510e8aa9aSMichał Mirosław 				"ASoC: codec_dai_name not set for %s\n",
376610e8aa9aSMichał Mirosław 				link->name);
3767bc92657aSStephen Warren 			return -EINVAL;
3768bc92657aSStephen Warren 		}
37695a504963SStephen Warren 
37705a504963SStephen Warren 		/*
37715a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
37725a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
37735a504963SStephen Warren 		 */
37745a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
377510e8aa9aSMichał Mirosław 			dev_err(card->dev,
377610e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
377710e8aa9aSMichał Mirosław 				link->name);
37785a504963SStephen Warren 			return -EINVAL;
37795a504963SStephen Warren 		}
37805a504963SStephen Warren 
37815a504963SStephen Warren 		/*
3782bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3783bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3784bc92657aSStephen Warren 		 * name alone..
37855a504963SStephen Warren 		 */
3786bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
378710e8aa9aSMichał Mirosław 			dev_err(card->dev,
378810e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
378910e8aa9aSMichał Mirosław 				link->name);
3790bc92657aSStephen Warren 			return -EINVAL;
3791bc92657aSStephen Warren 		}
3792bc92657aSStephen Warren 		/*
3793bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3794bc92657aSStephen Warren 		 * specified
3795bc92657aSStephen Warren 		 */
3796bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3797bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
379810e8aa9aSMichał Mirosław 			dev_err(card->dev,
379910e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
380010e8aa9aSMichał Mirosław 				link->name);
38015a504963SStephen Warren 			return -EINVAL;
38025a504963SStephen Warren 		}
38035a504963SStephen Warren 	}
38045a504963SStephen Warren 
3805ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3806ed77cc12SMark Brown 
3807111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3808111c6419SStephen Warren 
3809150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3810150dd2f8SVinod Koul 
3811181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3812181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
38132eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3814f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3815f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3816f0fba2adSLiam Girdwood 		return -ENOMEM;
3817a7dbb603SLiam Girdwood 	card->num_rtd = 0;
38182eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3819f0fba2adSLiam Girdwood 
3820f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3821f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3822f0fba2adSLiam Girdwood 
3823db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3824c5af3a2eSMark Brown 	card->instantiated = 0;
3825f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3826a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3827c5af3a2eSMark Brown 
3828b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3829b19e6e7bSMark Brown 	if (ret != 0)
3830b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3831c5af3a2eSMark Brown 
3832988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
3833988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
3834988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3835988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3836988e8cc4SNicolin Chen 		if (!codec_dai->active)
3837988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(codec_dai->dev);
3838988e8cc4SNicolin Chen 		if (!cpu_dai->active)
3839988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
3840988e8cc4SNicolin Chen 	}
3841988e8cc4SNicolin Chen 
3842b19e6e7bSMark Brown 	return ret;
3843c5af3a2eSMark Brown }
384470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3845c5af3a2eSMark Brown 
3846c5af3a2eSMark Brown /**
3847c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3848c5af3a2eSMark Brown  *
3849ac11a2b3SMark Brown  * @card: Card to unregister
3850c5af3a2eSMark Brown  *
3851c5af3a2eSMark Brown  */
385270a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3853c5af3a2eSMark Brown {
3854b0e26485SVinod Koul 	if (card->instantiated)
3855b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3856f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3857c5af3a2eSMark Brown 
3858c5af3a2eSMark Brown 	return 0;
3859c5af3a2eSMark Brown }
386070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3861c5af3a2eSMark Brown 
3862f0fba2adSLiam Girdwood /*
3863f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3864f0fba2adSLiam Girdwood  * and sanitizing names.
3865f0fba2adSLiam Girdwood  */
38660b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3867f0fba2adSLiam Girdwood {
3868f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3869f0fba2adSLiam Girdwood 	int id1, id2;
3870f0fba2adSLiam Girdwood 
3871f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3872f0fba2adSLiam Girdwood 		return NULL;
3873f0fba2adSLiam Girdwood 
387458818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3875f0fba2adSLiam Girdwood 
3876f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3877f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3878f0fba2adSLiam Girdwood 	if (found) {
3879f0fba2adSLiam Girdwood 		/* get ID */
3880f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3881f0fba2adSLiam Girdwood 
3882f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3883f0fba2adSLiam Girdwood 			if (*id == -1)
3884f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3885f0fba2adSLiam Girdwood 		}
3886f0fba2adSLiam Girdwood 
3887f0fba2adSLiam Girdwood 	} else {
3888f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3889f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3890f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3891f0fba2adSLiam Girdwood 
3892f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
389305899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3894f0fba2adSLiam Girdwood 
3895f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3896f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
389758818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3898f0fba2adSLiam Girdwood 		} else
3899f0fba2adSLiam Girdwood 			*id = 0;
3900f0fba2adSLiam Girdwood 	}
3901f0fba2adSLiam Girdwood 
3902f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3903f0fba2adSLiam Girdwood }
3904f0fba2adSLiam Girdwood 
3905f0fba2adSLiam Girdwood /*
3906f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3907f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3908f0fba2adSLiam Girdwood  */
3909f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3910f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3911f0fba2adSLiam Girdwood {
3912f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
391310e8aa9aSMichał Mirosław 		dev_err(dev,
391410e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
391510e8aa9aSMichał Mirosław 			dev_name(dev));
3916f0fba2adSLiam Girdwood 		return NULL;
3917f0fba2adSLiam Girdwood 	}
3918f0fba2adSLiam Girdwood 
3919f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3920f0fba2adSLiam Girdwood }
3921f0fba2adSLiam Girdwood 
39229115171aSMark Brown /**
392332c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
39249115171aSMark Brown  *
392532c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
39269115171aSMark Brown  */
392732c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
39289115171aSMark Brown {
39295c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
39309115171aSMark Brown 
39315c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
393232c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
393332c9ba54SLars-Peter Clausen 			dai->name);
39349115171aSMark Brown 		list_del(&dai->list);
3935f0fba2adSLiam Girdwood 		kfree(dai->name);
3936f0fba2adSLiam Girdwood 		kfree(dai);
39379115171aSMark Brown 	}
393832c9ba54SLars-Peter Clausen }
39399115171aSMark Brown 
39409115171aSMark Brown /**
394132c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
39429115171aSMark Brown  *
39436106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
39446cc240f3SLars-Peter Clausen  * @codec: The CODEC that the DAIs are registered for, NULL if the component is
39456cc240f3SLars-Peter Clausen  *         not a CODEC.
39466106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3947ac11a2b3SMark Brown  * @count: Number of DAIs
394832c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
394932c9ba54SLars-Peter Clausen  *                     parent's name.
39509115171aSMark Brown  */
395132c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
39526cc240f3SLars-Peter Clausen 	struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
39536cc240f3SLars-Peter Clausen 	size_t count, bool legacy_dai_naming)
39549115171aSMark Brown {
39556106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3956f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
395732c9ba54SLars-Peter Clausen 	unsigned int i;
395832c9ba54SLars-Peter Clausen 	int ret;
3959f0fba2adSLiam Girdwood 
3960f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
39619115171aSMark Brown 
39629115171aSMark Brown 	for (i = 0; i < count; i++) {
3963f0fba2adSLiam Girdwood 
3964f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3965c46e0079SAxel Lin 		if (dai == NULL) {
3966c46e0079SAxel Lin 			ret = -ENOMEM;
3967c46e0079SAxel Lin 			goto err;
3968c46e0079SAxel Lin 		}
3969f0fba2adSLiam Girdwood 
397032c9ba54SLars-Peter Clausen 		/*
397132c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
397232c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
397332c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
397432c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
397532c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
397632c9ba54SLars-Peter Clausen 		 * component-less anymore.
397732c9ba54SLars-Peter Clausen 		 */
397832c9ba54SLars-Peter Clausen 		if (count == 1 && legacy_dai_naming) {
3979db2a4165SFrank Mandarino 			dai->name = fmt_single_name(dev, &dai->id);
398032c9ba54SLars-Peter Clausen 		} else {
3981f0fba2adSLiam Girdwood 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
398232c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
398332c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
398432c9ba54SLars-Peter Clausen 			else
398532c9ba54SLars-Peter Clausen 				dai->id = i;
398632c9ba54SLars-Peter Clausen 		}
3987f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3988f0fba2adSLiam Girdwood 			kfree(dai);
398932c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
39909115171aSMark Brown 			goto err;
39919115171aSMark Brown 		}
39929115171aSMark Brown 
39936106d129SLars-Peter Clausen 		dai->component = component;
39946cc240f3SLars-Peter Clausen 		dai->codec = codec;
3995f0fba2adSLiam Girdwood 		dai->dev = dev;
3996f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
3997be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3998f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3999f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
4000f0fba2adSLiam Girdwood 
40015f800080SPeter Ujfalusi 		if (!dai->codec)
40025f800080SPeter Ujfalusi 			dai->dapm.idle_bias_off = 1;
40035f800080SPeter Ujfalusi 
40041438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
4005054880feSMark Brown 
40069115171aSMark Brown 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
4007f0fba2adSLiam Girdwood 	}
4008f0fba2adSLiam Girdwood 
40099115171aSMark Brown 	return 0;
40109115171aSMark Brown 
40119115171aSMark Brown err:
401232c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
40139115171aSMark Brown 
40149115171aSMark Brown 	return ret;
40159115171aSMark Brown }
40169115171aSMark Brown 
40179115171aSMark Brown /**
4018d191bd8dSKuninori Morimoto  * snd_soc_register_component - Register a component with the ASoC core
4019d191bd8dSKuninori Morimoto  *
4020d191bd8dSKuninori Morimoto  */
4021d191bd8dSKuninori Morimoto static int
4022d191bd8dSKuninori Morimoto __snd_soc_register_component(struct device *dev,
4023d191bd8dSKuninori Morimoto 			     struct snd_soc_component *cmpnt,
4024d191bd8dSKuninori Morimoto 			     const struct snd_soc_component_driver *cmpnt_drv,
40256cc240f3SLars-Peter Clausen 			     struct snd_soc_codec *codec,
4026d191bd8dSKuninori Morimoto 			     struct snd_soc_dai_driver *dai_drv,
4027d191bd8dSKuninori Morimoto 			     int num_dai, bool allow_single_dai)
4028d191bd8dSKuninori Morimoto {
4029d191bd8dSKuninori Morimoto 	int ret;
4030d191bd8dSKuninori Morimoto 
4031d191bd8dSKuninori Morimoto 	dev_dbg(dev, "component register %s\n", dev_name(dev));
4032d191bd8dSKuninori Morimoto 
4033d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4034d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to connecting component\n");
4035d191bd8dSKuninori Morimoto 		return -ENOMEM;
4036d191bd8dSKuninori Morimoto 	}
4037d191bd8dSKuninori Morimoto 
4038e2c330b9SLars-Peter Clausen 	mutex_init(&cmpnt->io_mutex);
4039e2c330b9SLars-Peter Clausen 
4040d191bd8dSKuninori Morimoto 	cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4041d191bd8dSKuninori Morimoto 	if (!cmpnt->name) {
4042d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to simplifying name\n");
4043d191bd8dSKuninori Morimoto 		return -ENOMEM;
4044d191bd8dSKuninori Morimoto 	}
4045d191bd8dSKuninori Morimoto 
4046d191bd8dSKuninori Morimoto 	cmpnt->dev	= dev;
4047d191bd8dSKuninori Morimoto 	cmpnt->driver	= cmpnt_drv;
40486833c452SKuninori Morimoto 	cmpnt->dai_drv	= dai_drv;
4049d191bd8dSKuninori Morimoto 	cmpnt->num_dai	= num_dai;
40501438c2f6SLars-Peter Clausen 	INIT_LIST_HEAD(&cmpnt->dai_list);
4051d191bd8dSKuninori Morimoto 
40526cc240f3SLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
40536cc240f3SLars-Peter Clausen 		allow_single_dai);
4054d191bd8dSKuninori Morimoto 	if (ret < 0) {
4055d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4056d191bd8dSKuninori Morimoto 		goto error_component_name;
4057d191bd8dSKuninori Morimoto 	}
4058d191bd8dSKuninori Morimoto 
4059d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
4060d191bd8dSKuninori Morimoto 	list_add(&cmpnt->list, &component_list);
4061d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
4062d191bd8dSKuninori Morimoto 
4063d191bd8dSKuninori Morimoto 	dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4064d191bd8dSKuninori Morimoto 
4065d191bd8dSKuninori Morimoto 	return ret;
4066d191bd8dSKuninori Morimoto 
4067d191bd8dSKuninori Morimoto error_component_name:
4068d191bd8dSKuninori Morimoto 	kfree(cmpnt->name);
4069d191bd8dSKuninori Morimoto 
4070d191bd8dSKuninori Morimoto 	return ret;
4071d191bd8dSKuninori Morimoto }
4072d191bd8dSKuninori Morimoto 
4073d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
4074d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
4075d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
4076d191bd8dSKuninori Morimoto 			       int num_dai)
4077d191bd8dSKuninori Morimoto {
4078d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4079d191bd8dSKuninori Morimoto 
4080d191bd8dSKuninori Morimoto 	cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4081d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4082d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
4083d191bd8dSKuninori Morimoto 		return -ENOMEM;
4084d191bd8dSKuninori Morimoto 	}
4085d191bd8dSKuninori Morimoto 
40863d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
408798e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
40883d59400fSLars-Peter Clausen 
40896cc240f3SLars-Peter Clausen 	return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
4090d191bd8dSKuninori Morimoto 					    dai_drv, num_dai, true);
4091d191bd8dSKuninori Morimoto }
4092d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
4093d191bd8dSKuninori Morimoto 
40944da53393SJarkko Nikula static void __snd_soc_unregister_component(struct snd_soc_component *cmpnt)
40954da53393SJarkko Nikula {
40964da53393SJarkko Nikula 	snd_soc_unregister_dais(cmpnt);
40974da53393SJarkko Nikula 
40984da53393SJarkko Nikula 	mutex_lock(&client_mutex);
40994da53393SJarkko Nikula 	list_del(&cmpnt->list);
41004da53393SJarkko Nikula 	mutex_unlock(&client_mutex);
41014da53393SJarkko Nikula 
41024da53393SJarkko Nikula 	dev_dbg(cmpnt->dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
41034da53393SJarkko Nikula 	kfree(cmpnt->name);
41044da53393SJarkko Nikula }
41054da53393SJarkko Nikula 
4106d191bd8dSKuninori Morimoto /**
4107d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
4108d191bd8dSKuninori Morimoto  *
4109d191bd8dSKuninori Morimoto  */
4110d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
4111d191bd8dSKuninori Morimoto {
4112d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4113d191bd8dSKuninori Morimoto 
4114d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
411598e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
4116d191bd8dSKuninori Morimoto 			goto found;
4117d191bd8dSKuninori Morimoto 	}
4118d191bd8dSKuninori Morimoto 	return;
4119d191bd8dSKuninori Morimoto 
4120d191bd8dSKuninori Morimoto found:
41214da53393SJarkko Nikula 	__snd_soc_unregister_component(cmpnt);
4122d191bd8dSKuninori Morimoto }
4123d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4124d191bd8dSKuninori Morimoto 
4125e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4126e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
4127e2c330b9SLars-Peter Clausen {
4128e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4129e2c330b9SLars-Peter Clausen 
4130e2c330b9SLars-Peter Clausen 	return platform->driver->write(platform, reg, val);
4131e2c330b9SLars-Peter Clausen }
4132e2c330b9SLars-Peter Clausen 
4133e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4134e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
4135e2c330b9SLars-Peter Clausen {
4136e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4137e2c330b9SLars-Peter Clausen 
4138e2c330b9SLars-Peter Clausen 	*val = platform->driver->read(platform, reg);
4139e2c330b9SLars-Peter Clausen 
4140e2c330b9SLars-Peter Clausen 	return 0;
4141e2c330b9SLars-Peter Clausen }
4142e2c330b9SLars-Peter Clausen 
4143d191bd8dSKuninori Morimoto /**
414471a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
414571a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
414671a45cdaSLars-Peter Clausen  * @platform: The platform to add
414771a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
414812a48a8cSMark Brown  */
414971a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4150d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
415112a48a8cSMark Brown {
4152b37f1d12SLars-Peter Clausen 	int ret;
4153b37f1d12SLars-Peter Clausen 
4154f0fba2adSLiam Girdwood 	/* create platform component name */
4155f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
4156b5c745fbSDan Carpenter 	if (platform->name == NULL)
4157f0fba2adSLiam Girdwood 		return -ENOMEM;
4158f0fba2adSLiam Girdwood 
4159f0fba2adSLiam Girdwood 	platform->dev = dev;
4160f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
4161b7950641SLiam Girdwood 	platform->dapm.dev = dev;
4162b7950641SLiam Girdwood 	platform->dapm.platform = platform;
4163e2c330b9SLars-Peter Clausen 	platform->dapm.component = &platform->component;
416464a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
4165e2c330b9SLars-Peter Clausen 	if (platform_drv->write)
4166e2c330b9SLars-Peter Clausen 		platform->component.write = snd_soc_platform_drv_write;
4167e2c330b9SLars-Peter Clausen 	if (platform_drv->read)
4168e2c330b9SLars-Peter Clausen 		platform->component.read = snd_soc_platform_drv_read;
416912a48a8cSMark Brown 
4170b37f1d12SLars-Peter Clausen 	/* register component */
4171b37f1d12SLars-Peter Clausen 	ret = __snd_soc_register_component(dev, &platform->component,
4172b37f1d12SLars-Peter Clausen 					   &platform_drv->component_driver,
4173b37f1d12SLars-Peter Clausen 					   NULL, NULL, 0, false);
4174b37f1d12SLars-Peter Clausen 	if (ret < 0) {
4175b37f1d12SLars-Peter Clausen 		dev_err(platform->component.dev,
4176b37f1d12SLars-Peter Clausen 			"ASoC: Failed to register component: %d\n", ret);
4177b37f1d12SLars-Peter Clausen 		return ret;
4178b37f1d12SLars-Peter Clausen 	}
4179b37f1d12SLars-Peter Clausen 
418012a48a8cSMark Brown 	mutex_lock(&client_mutex);
418112a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
418212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
418312a48a8cSMark Brown 
4184f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
418512a48a8cSMark Brown 
418612a48a8cSMark Brown 	return 0;
418712a48a8cSMark Brown }
418871a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
418971a45cdaSLars-Peter Clausen 
419071a45cdaSLars-Peter Clausen /**
419171a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
419271a45cdaSLars-Peter Clausen  *
419371a45cdaSLars-Peter Clausen  * @platform: platform to register
419471a45cdaSLars-Peter Clausen  */
419571a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
419671a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
419771a45cdaSLars-Peter Clausen {
419871a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
419971a45cdaSLars-Peter Clausen 	int ret;
420071a45cdaSLars-Peter Clausen 
420171a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
420271a45cdaSLars-Peter Clausen 
420371a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
420471a45cdaSLars-Peter Clausen 	if (platform == NULL)
420571a45cdaSLars-Peter Clausen 		return -ENOMEM;
420671a45cdaSLars-Peter Clausen 
420771a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
420871a45cdaSLars-Peter Clausen 	if (ret)
420971a45cdaSLars-Peter Clausen 		kfree(platform);
421071a45cdaSLars-Peter Clausen 
421171a45cdaSLars-Peter Clausen 	return ret;
421271a45cdaSLars-Peter Clausen }
421312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
421412a48a8cSMark Brown 
421512a48a8cSMark Brown /**
421671a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
421771a45cdaSLars-Peter Clausen  * @platform: the platform to remove
421871a45cdaSLars-Peter Clausen  */
421971a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
422071a45cdaSLars-Peter Clausen {
42214da53393SJarkko Nikula 	__snd_soc_unregister_component(&platform->component);
4222b37f1d12SLars-Peter Clausen 
422371a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
422471a45cdaSLars-Peter Clausen 	list_del(&platform->list);
422571a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
422671a45cdaSLars-Peter Clausen 
422771a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
422871a45cdaSLars-Peter Clausen 		platform->name);
422971a45cdaSLars-Peter Clausen 	kfree(platform->name);
423071a45cdaSLars-Peter Clausen }
423171a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
423271a45cdaSLars-Peter Clausen 
423371a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
423471a45cdaSLars-Peter Clausen {
423571a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
423671a45cdaSLars-Peter Clausen 
423771a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
423871a45cdaSLars-Peter Clausen 		if (dev == platform->dev)
423971a45cdaSLars-Peter Clausen 			return platform;
424071a45cdaSLars-Peter Clausen 	}
424171a45cdaSLars-Peter Clausen 
424271a45cdaSLars-Peter Clausen 	return NULL;
424371a45cdaSLars-Peter Clausen }
424471a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
424571a45cdaSLars-Peter Clausen 
424671a45cdaSLars-Peter Clausen /**
424712a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
424812a48a8cSMark Brown  *
4249ac11a2b3SMark Brown  * @platform: platform to unregister
425012a48a8cSMark Brown  */
4251f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
425212a48a8cSMark Brown {
4253f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
4254f0fba2adSLiam Girdwood 
425571a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
425671a45cdaSLars-Peter Clausen 	if (!platform)
4257f0fba2adSLiam Girdwood 		return;
4258f0fba2adSLiam Girdwood 
425971a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
4260f0fba2adSLiam Girdwood 	kfree(platform);
426112a48a8cSMark Brown }
426212a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
426312a48a8cSMark Brown 
4264151ab22cSMark Brown static u64 codec_format_map[] = {
4265151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4266151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4267151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4268151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4269151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4270151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4271151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4272151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4273151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4274151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4275151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4276151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4277151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4278151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4279151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4280151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4281151ab22cSMark Brown };
4282151ab22cSMark Brown 
4283151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
4284151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
4285151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
4286151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
4287151ab22cSMark Brown  */
4288151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4289151ab22cSMark Brown {
4290151ab22cSMark Brown 	int i;
4291151ab22cSMark Brown 
4292151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4293151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
4294151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
4295151ab22cSMark Brown }
4296151ab22cSMark Brown 
4297e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4298e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
4299e2c330b9SLars-Peter Clausen {
4300e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4301e2c330b9SLars-Peter Clausen 
4302e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
4303e2c330b9SLars-Peter Clausen }
4304e2c330b9SLars-Peter Clausen 
4305e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4306e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
4307e2c330b9SLars-Peter Clausen {
4308e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4309e2c330b9SLars-Peter Clausen 
4310e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
4311e2c330b9SLars-Peter Clausen 
4312e2c330b9SLars-Peter Clausen 	return 0;
4313e2c330b9SLars-Peter Clausen }
4314e2c330b9SLars-Peter Clausen 
43150d0cf00aSMark Brown /**
43160d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
43170d0cf00aSMark Brown  *
4318ac11a2b3SMark Brown  * @codec: codec to register
43190d0cf00aSMark Brown  */
4320f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
4321001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
4322001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
4323001ae4c0SMark Brown 			   int num_dai)
43240d0cf00aSMark Brown {
4325f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4326a39f75f7SXiubo Li 	struct regmap *regmap;
4327f0fba2adSLiam Girdwood 	int ret, i;
4328151ab22cSMark Brown 
4329f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
43300d0cf00aSMark Brown 
4331f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4332f0fba2adSLiam Girdwood 	if (codec == NULL)
4333f0fba2adSLiam Girdwood 		return -ENOMEM;
43340d0cf00aSMark Brown 
4335f0fba2adSLiam Girdwood 	/* create CODEC component name */
4336f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
4337f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
4338f790b94dSKuninori Morimoto 		ret = -ENOMEM;
4339f790b94dSKuninori Morimoto 		goto fail_codec;
4340151ab22cSMark Brown 	}
4341151ab22cSMark Brown 
4342e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
4343e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
4344e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
4345e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
43463d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4347ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4348ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
4349ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
4350e2c330b9SLars-Peter Clausen 	codec->dapm.component = &codec->component;
4351474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
435264a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
4353f0fba2adSLiam Girdwood 	codec->dev = dev;
4354f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
4355e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
4356f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
4357f0fba2adSLiam Girdwood 
4358e2c330b9SLars-Peter Clausen 	if (!codec->component.write) {
4359a39f75f7SXiubo Li 		if (codec_drv->get_regmap)
4360a39f75f7SXiubo Li 			regmap = codec_drv->get_regmap(dev);
4361a39f75f7SXiubo Li 		else
4362a39f75f7SXiubo Li 			regmap = dev_get_regmap(dev, NULL);
4363a39f75f7SXiubo Li 
4364a39f75f7SXiubo Li 		if (regmap) {
4365e2c330b9SLars-Peter Clausen 			ret = snd_soc_component_init_io(&codec->component,
4366e2c330b9SLars-Peter Clausen 				regmap);
4367e2c330b9SLars-Peter Clausen 			if (ret) {
4368a39f75f7SXiubo Li 				dev_err(codec->dev,
4369a39f75f7SXiubo Li 						"Failed to set cache I/O:%d\n",
4370a39f75f7SXiubo Li 						ret);
4371a39f75f7SXiubo Li 				return ret;
4372a39f75f7SXiubo Li 			}
4373a39f75f7SXiubo Li 		}
4374a39f75f7SXiubo Li 	}
4375a39f75f7SXiubo Li 
4376f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
4377f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
4378f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
4379f0fba2adSLiam Girdwood 	}
4380f0fba2adSLiam Girdwood 
4381054880feSMark Brown 	mutex_lock(&client_mutex);
4382054880feSMark Brown 	list_add(&codec->list, &codec_list);
4383054880feSMark Brown 	mutex_unlock(&client_mutex);
4384054880feSMark Brown 
4385d191bd8dSKuninori Morimoto 	/* register component */
4386d191bd8dSKuninori Morimoto 	ret = __snd_soc_register_component(dev, &codec->component,
4387d191bd8dSKuninori Morimoto 					   &codec_drv->component_driver,
43886cc240f3SLars-Peter Clausen 					   codec, dai_drv, num_dai, false);
43895acd7dfbSKuninori Morimoto 	if (ret < 0) {
4390d191bd8dSKuninori Morimoto 		dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
43915acd7dfbSKuninori Morimoto 		goto fail_codec_name;
439226b01ccdSMark Brown 	}
4393f0fba2adSLiam Girdwood 
4394f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
43950d0cf00aSMark Brown 	return 0;
4396f0fba2adSLiam Girdwood 
4397f790b94dSKuninori Morimoto fail_codec_name:
4398e1328a83SKuninori Morimoto 	mutex_lock(&client_mutex);
4399e1328a83SKuninori Morimoto 	list_del(&codec->list);
4400e1328a83SKuninori Morimoto 	mutex_unlock(&client_mutex);
4401e1328a83SKuninori Morimoto 
4402f0fba2adSLiam Girdwood 	kfree(codec->name);
4403f790b94dSKuninori Morimoto fail_codec:
4404f0fba2adSLiam Girdwood 	kfree(codec);
4405f0fba2adSLiam Girdwood 	return ret;
44060d0cf00aSMark Brown }
44070d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
44080d0cf00aSMark Brown 
44090d0cf00aSMark Brown /**
44100d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
44110d0cf00aSMark Brown  *
4412ac11a2b3SMark Brown  * @codec: codec to unregister
44130d0cf00aSMark Brown  */
4414f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
44150d0cf00aSMark Brown {
4416f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4417f0fba2adSLiam Girdwood 
4418f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
4419f0fba2adSLiam Girdwood 		if (dev == codec->dev)
4420f0fba2adSLiam Girdwood 			goto found;
4421f0fba2adSLiam Girdwood 	}
4422f0fba2adSLiam Girdwood 	return;
4423f0fba2adSLiam Girdwood 
4424f0fba2adSLiam Girdwood found:
44254da53393SJarkko Nikula 	__snd_soc_unregister_component(&codec->component);
4426f0fba2adSLiam Girdwood 
44270d0cf00aSMark Brown 	mutex_lock(&client_mutex);
44280d0cf00aSMark Brown 	list_del(&codec->list);
44290d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
44300d0cf00aSMark Brown 
4431f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4432f0fba2adSLiam Girdwood 
44337a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
44341aafcd4dSDimitris Papastamos 	kfree(codec->name);
4435f0fba2adSLiam Girdwood 	kfree(codec);
44360d0cf00aSMark Brown }
44370d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
44380d0cf00aSMark Brown 
4439bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4440bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4441bec4fa05SStephen Warren 			       const char *propname)
4442bec4fa05SStephen Warren {
4443bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
4444bec4fa05SStephen Warren 	int ret;
4445bec4fa05SStephen Warren 
4446bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4447bec4fa05SStephen Warren 	/*
4448bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4449bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4450bec4fa05SStephen Warren 	 * snd_soc_register_card.
4451bec4fa05SStephen Warren 	 */
4452bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4453bec4fa05SStephen Warren 		dev_err(card->dev,
4454f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
4455bec4fa05SStephen Warren 			propname, ret);
4456bec4fa05SStephen Warren 		return ret;
4457bec4fa05SStephen Warren 	}
4458bec4fa05SStephen Warren 
4459bec4fa05SStephen Warren 	return 0;
4460bec4fa05SStephen Warren }
4461bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4462bec4fa05SStephen Warren 
44639a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
44649a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
44659a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
44669a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
44679a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
44689a6d4860SXiubo Li };
44699a6d4860SXiubo Li 
44709a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
44719a6d4860SXiubo Li 					  const char *propname)
44729a6d4860SXiubo Li {
44739a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
44749a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
44759a6d4860SXiubo Li 	const char *template, *wname;
44769a6d4860SXiubo Li 	int i, j, num_widgets, ret;
44779a6d4860SXiubo Li 
44789a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
44799a6d4860SXiubo Li 	if (num_widgets < 0) {
44809a6d4860SXiubo Li 		dev_err(card->dev,
44819a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
44829a6d4860SXiubo Li 		return -EINVAL;
44839a6d4860SXiubo Li 	}
44849a6d4860SXiubo Li 	if (num_widgets & 1) {
44859a6d4860SXiubo Li 		dev_err(card->dev,
44869a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
44879a6d4860SXiubo Li 		return -EINVAL;
44889a6d4860SXiubo Li 	}
44899a6d4860SXiubo Li 
44909a6d4860SXiubo Li 	num_widgets /= 2;
44919a6d4860SXiubo Li 	if (!num_widgets) {
44929a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
44939a6d4860SXiubo Li 			propname);
44949a6d4860SXiubo Li 		return -EINVAL;
44959a6d4860SXiubo Li 	}
44969a6d4860SXiubo Li 
44979a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
44989a6d4860SXiubo Li 			       GFP_KERNEL);
44999a6d4860SXiubo Li 	if (!widgets) {
45009a6d4860SXiubo Li 		dev_err(card->dev,
45019a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
45029a6d4860SXiubo Li 		return -ENOMEM;
45039a6d4860SXiubo Li 	}
45049a6d4860SXiubo Li 
45059a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
45069a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
45079a6d4860SXiubo Li 			2 * i, &template);
45089a6d4860SXiubo Li 		if (ret) {
45099a6d4860SXiubo Li 			dev_err(card->dev,
45109a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
45119a6d4860SXiubo Li 				propname, 2 * i, ret);
45129a6d4860SXiubo Li 			return -EINVAL;
45139a6d4860SXiubo Li 		}
45149a6d4860SXiubo Li 
45159a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
45169a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
45179a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
45189a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
45199a6d4860SXiubo Li 				break;
45209a6d4860SXiubo Li 			}
45219a6d4860SXiubo Li 		}
45229a6d4860SXiubo Li 
45239a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
45249a6d4860SXiubo Li 			dev_err(card->dev,
45259a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
45269a6d4860SXiubo Li 				template);
45279a6d4860SXiubo Li 			return -EINVAL;
45289a6d4860SXiubo Li 		}
45299a6d4860SXiubo Li 
45309a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
45319a6d4860SXiubo Li 						    (2 * i) + 1,
45329a6d4860SXiubo Li 						    &wname);
45339a6d4860SXiubo Li 		if (ret) {
45349a6d4860SXiubo Li 			dev_err(card->dev,
45359a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
45369a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
45379a6d4860SXiubo Li 			return -EINVAL;
45389a6d4860SXiubo Li 		}
45399a6d4860SXiubo Li 
45409a6d4860SXiubo Li 		widgets[i].name = wname;
45419a6d4860SXiubo Li 	}
45429a6d4860SXiubo Li 
45439a6d4860SXiubo Li 	card->dapm_widgets = widgets;
45449a6d4860SXiubo Li 	card->num_dapm_widgets = num_widgets;
45459a6d4860SXiubo Li 
45469a6d4860SXiubo Li 	return 0;
45479a6d4860SXiubo Li }
45489a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
45499a6d4860SXiubo Li 
455089c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
455189c67857SXiubo Li 			      unsigned int *slots,
455289c67857SXiubo Li 			      unsigned int *slot_width)
455389c67857SXiubo Li {
455489c67857SXiubo Li 	u32 val;
455589c67857SXiubo Li 	int ret;
455689c67857SXiubo Li 
455789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
455889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
455989c67857SXiubo Li 		if (ret)
456089c67857SXiubo Li 			return ret;
456189c67857SXiubo Li 
456289c67857SXiubo Li 		if (slots)
456389c67857SXiubo Li 			*slots = val;
456489c67857SXiubo Li 	}
456589c67857SXiubo Li 
456689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
456789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
456889c67857SXiubo Li 		if (ret)
456989c67857SXiubo Li 			return ret;
457089c67857SXiubo Li 
457189c67857SXiubo Li 		if (slot_width)
457289c67857SXiubo Li 			*slot_width = val;
457389c67857SXiubo Li 	}
457489c67857SXiubo Li 
457589c67857SXiubo Li 	return 0;
457689c67857SXiubo Li }
457789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
457889c67857SXiubo Li 
4579a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4580a4a54dd5SStephen Warren 				   const char *propname)
4581a4a54dd5SStephen Warren {
4582a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4583a4a54dd5SStephen Warren 	int num_routes;
4584a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4585a4a54dd5SStephen Warren 	int i, ret;
4586a4a54dd5SStephen Warren 
4587a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4588c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
458910e8aa9aSMichał Mirosław 		dev_err(card->dev,
459010e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
459110e8aa9aSMichał Mirosław 			propname);
4592a4a54dd5SStephen Warren 		return -EINVAL;
4593a4a54dd5SStephen Warren 	}
4594a4a54dd5SStephen Warren 	num_routes /= 2;
4595a4a54dd5SStephen Warren 	if (!num_routes) {
4596f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4597a4a54dd5SStephen Warren 			propname);
4598a4a54dd5SStephen Warren 		return -EINVAL;
4599a4a54dd5SStephen Warren 	}
4600a4a54dd5SStephen Warren 
4601a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4602a4a54dd5SStephen Warren 			      GFP_KERNEL);
4603a4a54dd5SStephen Warren 	if (!routes) {
4604a4a54dd5SStephen Warren 		dev_err(card->dev,
4605f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4606a4a54dd5SStephen Warren 		return -EINVAL;
4607a4a54dd5SStephen Warren 	}
4608a4a54dd5SStephen Warren 
4609a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4610a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4611a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4612a4a54dd5SStephen Warren 		if (ret) {
4613c871bd0bSMark Brown 			dev_err(card->dev,
4614c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4615c871bd0bSMark Brown 				propname, 2 * i, ret);
4616a4a54dd5SStephen Warren 			return -EINVAL;
4617a4a54dd5SStephen Warren 		}
4618a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4619a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4620a4a54dd5SStephen Warren 		if (ret) {
4621a4a54dd5SStephen Warren 			dev_err(card->dev,
4622c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4623c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4624a4a54dd5SStephen Warren 			return -EINVAL;
4625a4a54dd5SStephen Warren 		}
4626a4a54dd5SStephen Warren 	}
4627a4a54dd5SStephen Warren 
4628a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4629a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4630a4a54dd5SStephen Warren 
4631a4a54dd5SStephen Warren 	return 0;
4632a4a54dd5SStephen Warren }
4633a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4634a4a54dd5SStephen Warren 
4635a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4636389cb834SJyri Sarha 				     const char *prefix,
4637389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
4638389cb834SJyri Sarha 				     struct device_node **framemaster)
4639a7930ed4SKuninori Morimoto {
4640a7930ed4SKuninori Morimoto 	int ret, i;
4641a7930ed4SKuninori Morimoto 	char prop[128];
4642a7930ed4SKuninori Morimoto 	unsigned int format = 0;
4643a7930ed4SKuninori Morimoto 	int bit, frame;
4644a7930ed4SKuninori Morimoto 	const char *str;
4645a7930ed4SKuninori Morimoto 	struct {
4646a7930ed4SKuninori Morimoto 		char *name;
4647a7930ed4SKuninori Morimoto 		unsigned int val;
4648a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
4649a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
4650a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
4651a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
4652a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
4653a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
4654a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
4655a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
4656a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
4657a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
4658a7930ed4SKuninori Morimoto 	};
4659a7930ed4SKuninori Morimoto 
4660a7930ed4SKuninori Morimoto 	if (!prefix)
4661a7930ed4SKuninori Morimoto 		prefix = "";
4662a7930ed4SKuninori Morimoto 
4663a7930ed4SKuninori Morimoto 	/*
4664a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
4665a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4666a7930ed4SKuninori Morimoto 	 */
4667a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
4668a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
4669a7930ed4SKuninori Morimoto 	if (ret == 0) {
4670a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4671a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4672a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4673a7930ed4SKuninori Morimoto 				break;
4674a7930ed4SKuninori Morimoto 			}
4675a7930ed4SKuninori Morimoto 		}
4676a7930ed4SKuninori Morimoto 	}
4677a7930ed4SKuninori Morimoto 
4678a7930ed4SKuninori Morimoto 	/*
46798c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4680a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4681a7930ed4SKuninori Morimoto 	 */
46828c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
46838c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
46848c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
46858c2d6a9fSKuninori Morimoto 	else
46868c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4687a7930ed4SKuninori Morimoto 
4688a7930ed4SKuninori Morimoto 	/*
4689a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4690a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4691a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4692a7930ed4SKuninori Morimoto 	 */
4693a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4694a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4695a7930ed4SKuninori Morimoto 
4696a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4697a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4698a7930ed4SKuninori Morimoto 
4699a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4700a7930ed4SKuninori Morimoto 	case 0x11:
4701a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4702a7930ed4SKuninori Morimoto 		break;
4703a7930ed4SKuninori Morimoto 	case 0x10:
4704a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4705a7930ed4SKuninori Morimoto 		break;
4706a7930ed4SKuninori Morimoto 	case 0x01:
4707a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4708a7930ed4SKuninori Morimoto 		break;
4709a7930ed4SKuninori Morimoto 	default:
4710a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4711a7930ed4SKuninori Morimoto 		break;
4712a7930ed4SKuninori Morimoto 	}
4713a7930ed4SKuninori Morimoto 
4714a7930ed4SKuninori Morimoto 	/*
4715a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4716a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4717a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4718a7930ed4SKuninori Morimoto 	 */
4719a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4720a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4721389cb834SJyri Sarha 	if (bit && bitclkmaster)
4722389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4723a7930ed4SKuninori Morimoto 
4724a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4725a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4726389cb834SJyri Sarha 	if (frame && framemaster)
4727389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4728a7930ed4SKuninori Morimoto 
4729a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4730a7930ed4SKuninori Morimoto 	case 0x11:
4731a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4732a7930ed4SKuninori Morimoto 		break;
4733a7930ed4SKuninori Morimoto 	case 0x10:
4734a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4735a7930ed4SKuninori Morimoto 		break;
4736a7930ed4SKuninori Morimoto 	case 0x01:
4737a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4738a7930ed4SKuninori Morimoto 		break;
4739a7930ed4SKuninori Morimoto 	default:
4740a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4741a7930ed4SKuninori Morimoto 		break;
4742a7930ed4SKuninori Morimoto 	}
4743a7930ed4SKuninori Morimoto 
4744a7930ed4SKuninori Morimoto 	return format;
4745a7930ed4SKuninori Morimoto }
4746a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4747a7930ed4SKuninori Morimoto 
4748cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node,
4749cb470087SKuninori Morimoto 			    const char **dai_name)
4750cb470087SKuninori Morimoto {
4751cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
4752cb470087SKuninori Morimoto 	struct of_phandle_args args;
4753cb470087SKuninori Morimoto 	int ret;
4754cb470087SKuninori Morimoto 
4755cb470087SKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
4756cb470087SKuninori Morimoto 					 "#sound-dai-cells", 0, &args);
4757cb470087SKuninori Morimoto 	if (ret)
4758cb470087SKuninori Morimoto 		return ret;
4759cb470087SKuninori Morimoto 
4760cb470087SKuninori Morimoto 	ret = -EPROBE_DEFER;
4761cb470087SKuninori Morimoto 
4762cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4763cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4764cb470087SKuninori Morimoto 		if (pos->dev->of_node != args.np)
4765cb470087SKuninori Morimoto 			continue;
4766cb470087SKuninori Morimoto 
47676833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
47686833c452SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
47696833c452SKuninori Morimoto 		} else {
47706833c452SKuninori Morimoto 			int id = -1;
47716833c452SKuninori Morimoto 
47726833c452SKuninori Morimoto 			switch (args.args_count) {
47736833c452SKuninori Morimoto 			case 0:
47746833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
47756833c452SKuninori Morimoto 				break;
47766833c452SKuninori Morimoto 			case 1:
47776833c452SKuninori Morimoto 				id = args.args[0];
47786833c452SKuninori Morimoto 				break;
47796833c452SKuninori Morimoto 			default:
47806833c452SKuninori Morimoto 				/* not supported */
4781cb470087SKuninori Morimoto 				break;
4782cb470087SKuninori Morimoto 			}
4783cb470087SKuninori Morimoto 
47846833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
47856833c452SKuninori Morimoto 				ret = -EINVAL;
47863dcba280SNicolin Chen 				continue;
47876833c452SKuninori Morimoto 			}
4788e41975edSXiubo Li 
4789e41975edSXiubo Li 			ret = 0;
4790e41975edSXiubo Li 
4791e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4792e41975edSXiubo Li 			if (!*dai_name)
4793e41975edSXiubo Li 				*dai_name = pos->name;
47946833c452SKuninori Morimoto 		}
47956833c452SKuninori Morimoto 
4796cb470087SKuninori Morimoto 		break;
4797cb470087SKuninori Morimoto 	}
4798cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
4799cb470087SKuninori Morimoto 
4800cb470087SKuninori Morimoto 	of_node_put(args.np);
4801cb470087SKuninori Morimoto 
4802cb470087SKuninori Morimoto 	return ret;
4803cb470087SKuninori Morimoto }
4804cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4805cb470087SKuninori Morimoto 
4806c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4807db2a4165SFrank Mandarino {
4808db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
48098a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
48108a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
48110837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
48128a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4813db2a4165SFrank Mandarino 	}
4814c3c5a19aSMark Brown 
48158a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4816c3c5a19aSMark Brown 				 &codec_list_fops))
4817c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4818c3c5a19aSMark Brown 
48198a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4820f3208780SMark Brown 				 &dai_list_fops))
4821f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
482219c7ac27SMark Brown 
48238a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
482419c7ac27SMark Brown 				 &platform_list_fops))
482519c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4826db2a4165SFrank Mandarino #endif
4827db2a4165SFrank Mandarino 
4828fb257897SMark Brown 	snd_soc_util_init();
4829fb257897SMark Brown 
4830db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4831db2a4165SFrank Mandarino }
48324abe8e16SMark Brown module_init(snd_soc_init);
4833db2a4165SFrank Mandarino 
4834db2a4165SFrank Mandarino static void __exit snd_soc_exit(void)
4835db2a4165SFrank Mandarino {
4836fb257897SMark Brown 	snd_soc_util_exit();
4837fb257897SMark Brown 
4838db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
48398a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4840db2a4165SFrank Mandarino #endif
4841db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4842db2a4165SFrank Mandarino }
4843db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4844db2a4165SFrank Mandarino 
4845db2a4165SFrank Mandarino /* Module information */
4846db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4847db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4848db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4849db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4850