xref: /openbmc/linux/sound/soc/soc-core.c (revision 5c7c343a1159d1cb7604b6137cf547b2c1e2375d)
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 
273e73f3de5SRussell King static struct dentry *soc_debugfs_create_dir(struct dentry *parent,
274e73f3de5SRussell King 	const char *fmt, ...)
275e73f3de5SRussell King {
276e73f3de5SRussell King 	struct dentry *de;
277e73f3de5SRussell King 	va_list ap;
278e73f3de5SRussell King 	char *s;
279e73f3de5SRussell King 
280e73f3de5SRussell King 	va_start(ap, fmt);
281e73f3de5SRussell King 	s = kvasprintf(GFP_KERNEL, fmt, ap);
282e73f3de5SRussell King 	va_end(ap);
283e73f3de5SRussell King 
284e73f3de5SRussell King 	if (!s)
285e73f3de5SRussell King 		return NULL;
286e73f3de5SRussell King 
287e73f3de5SRussell King 	de = debugfs_create_dir(s, parent);
288e73f3de5SRussell King 	kfree(s);
289e73f3de5SRussell King 
290e73f3de5SRussell King 	return de;
291e73f3de5SRussell King }
292e73f3de5SRussell King 
2932624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2942624d5faSMark Brown {
29500200107SLars-Peter Clausen 	struct dentry *debugfs_card_root = codec->component.card->debugfs_card_root;
296d6ce4cf3SJarkko Nikula 
297e73f3de5SRussell King 	codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root,
298211bcc6cSMark Brown 						"codec:%s",
299211bcc6cSMark Brown 						codec->component.name);
3002624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
30110e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
30210e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec debugfs directory\n");
3032624d5faSMark Brown 		return;
3042624d5faSMark Brown 	}
3052624d5faSMark Brown 
306aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
307aaee8ef1SMark Brown 			    &codec->cache_sync);
308aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
309aaee8ef1SMark Brown 			    &codec->cache_only);
310aaee8ef1SMark Brown 
3112624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
3122624d5faSMark Brown 						 codec->debugfs_codec_root,
3132624d5faSMark Brown 						 codec, &codec_reg_fops);
3142624d5faSMark Brown 	if (!codec->debugfs_reg)
31510e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
31610e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
3172624d5faSMark Brown 
3188eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
3192624d5faSMark Brown }
3202624d5faSMark Brown 
3212624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3222624d5faSMark Brown {
3232624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3242624d5faSMark Brown }
3252624d5faSMark Brown 
326731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
327731f1ab2SSebastien Guiriec {
32800200107SLars-Peter Clausen 	struct dentry *debugfs_card_root = platform->component.card->debugfs_card_root;
329731f1ab2SSebastien Guiriec 
330e73f3de5SRussell King 	platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root,
331211bcc6cSMark Brown 						"platform:%s",
332211bcc6cSMark Brown 						platform->component.name);
333731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
334731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
335f110bfc7SLiam Girdwood 			"ASoC: Failed to create platform debugfs directory\n");
336731f1ab2SSebastien Guiriec 		return;
337731f1ab2SSebastien Guiriec 	}
338731f1ab2SSebastien Guiriec 
339bc9af9faSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&platform->component.dapm,
340731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
341731f1ab2SSebastien Guiriec }
342731f1ab2SSebastien Guiriec 
343731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
344731f1ab2SSebastien Guiriec {
345731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
346731f1ab2SSebastien Guiriec }
347731f1ab2SSebastien Guiriec 
348c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
349c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
350c3c5a19aSMark Brown {
351c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3522b194f9dSMark Brown 	ssize_t len, ret = 0;
353c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
354c3c5a19aSMark Brown 
355c3c5a19aSMark Brown 	if (!buf)
356c3c5a19aSMark Brown 		return -ENOMEM;
357c3c5a19aSMark Brown 
3582b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3592b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
360f4333203SLars-Peter Clausen 			       codec->component.name);
3612b194f9dSMark Brown 		if (len >= 0)
3622b194f9dSMark Brown 			ret += len;
3632b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3642b194f9dSMark Brown 			ret = PAGE_SIZE;
3652b194f9dSMark Brown 			break;
3662b194f9dSMark Brown 		}
3672b194f9dSMark Brown 	}
368c3c5a19aSMark Brown 
369c3c5a19aSMark Brown 	if (ret >= 0)
370c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
371c3c5a19aSMark Brown 
372c3c5a19aSMark Brown 	kfree(buf);
373c3c5a19aSMark Brown 
374c3c5a19aSMark Brown 	return ret;
375c3c5a19aSMark Brown }
376c3c5a19aSMark Brown 
377c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
378c3c5a19aSMark Brown 	.read = codec_list_read_file,
379c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
380c3c5a19aSMark Brown };
381c3c5a19aSMark Brown 
382f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
383f3208780SMark Brown 				  size_t count, loff_t *ppos)
384f3208780SMark Brown {
385f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3862b194f9dSMark Brown 	ssize_t len, ret = 0;
3871438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
388f3208780SMark Brown 	struct snd_soc_dai *dai;
389f3208780SMark Brown 
390f3208780SMark Brown 	if (!buf)
391f3208780SMark Brown 		return -ENOMEM;
392f3208780SMark Brown 
3931438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3941438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3951438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3961438c2f6SLars-Peter Clausen 				dai->name);
3972b194f9dSMark Brown 			if (len >= 0)
3982b194f9dSMark Brown 				ret += len;
3992b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
4002b194f9dSMark Brown 				ret = PAGE_SIZE;
4012b194f9dSMark Brown 				break;
4022b194f9dSMark Brown 			}
4032b194f9dSMark Brown 		}
4041438c2f6SLars-Peter Clausen 	}
405f3208780SMark Brown 
406f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
407f3208780SMark Brown 
408f3208780SMark Brown 	kfree(buf);
409f3208780SMark Brown 
410f3208780SMark Brown 	return ret;
411f3208780SMark Brown }
412f3208780SMark Brown 
413f3208780SMark Brown static const struct file_operations dai_list_fops = {
414f3208780SMark Brown 	.read = dai_list_read_file,
415f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
416f3208780SMark Brown };
417f3208780SMark Brown 
41819c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
41919c7ac27SMark Brown 				       char __user *user_buf,
42019c7ac27SMark Brown 				       size_t count, loff_t *ppos)
42119c7ac27SMark Brown {
42219c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4232b194f9dSMark Brown 	ssize_t len, ret = 0;
42419c7ac27SMark Brown 	struct snd_soc_platform *platform;
42519c7ac27SMark Brown 
42619c7ac27SMark Brown 	if (!buf)
42719c7ac27SMark Brown 		return -ENOMEM;
42819c7ac27SMark Brown 
4292b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4302b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
431f4333203SLars-Peter Clausen 			       platform->component.name);
4322b194f9dSMark Brown 		if (len >= 0)
4332b194f9dSMark Brown 			ret += len;
4342b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4352b194f9dSMark Brown 			ret = PAGE_SIZE;
4362b194f9dSMark Brown 			break;
4372b194f9dSMark Brown 		}
4382b194f9dSMark Brown 	}
43919c7ac27SMark Brown 
44019c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
44119c7ac27SMark Brown 
44219c7ac27SMark Brown 	kfree(buf);
44319c7ac27SMark Brown 
44419c7ac27SMark Brown 	return ret;
44519c7ac27SMark Brown }
44619c7ac27SMark Brown 
44719c7ac27SMark Brown static const struct file_operations platform_list_fops = {
44819c7ac27SMark Brown 	.read = platform_list_read_file,
44919c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
45019c7ac27SMark Brown };
45119c7ac27SMark Brown 
452a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
453a6052154SJarkko Nikula {
454a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4558a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4563a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
457a6052154SJarkko Nikula 		dev_warn(card->dev,
4587c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4593a45b867SJarkko Nikula 		return;
4603a45b867SJarkko Nikula 	}
4613a45b867SJarkko Nikula 
4623a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4633a45b867SJarkko Nikula 						    card->debugfs_card_root,
4643a45b867SJarkko Nikula 						    &card->pop_time);
4653a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4663a45b867SJarkko Nikula 		dev_warn(card->dev,
467f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
468a6052154SJarkko Nikula }
469a6052154SJarkko Nikula 
470a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471a6052154SJarkko Nikula {
472a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
473a6052154SJarkko Nikula }
474a6052154SJarkko Nikula 
4752624d5faSMark Brown #else
4762624d5faSMark Brown 
4772624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4782624d5faSMark Brown {
4792624d5faSMark Brown }
4802624d5faSMark Brown 
4812624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4822624d5faSMark Brown {
4832624d5faSMark Brown }
484b95fccbcSAxel Lin 
485731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
486731f1ab2SSebastien Guiriec {
487731f1ab2SSebastien Guiriec }
488731f1ab2SSebastien Guiriec 
489731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
490731f1ab2SSebastien Guiriec {
491731f1ab2SSebastien Guiriec }
492731f1ab2SSebastien Guiriec 
493b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
494b95fccbcSAxel Lin {
495b95fccbcSAxel Lin }
496b95fccbcSAxel Lin 
497b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
498b95fccbcSAxel Lin {
499b95fccbcSAxel Lin }
5002624d5faSMark Brown #endif
5012624d5faSMark Brown 
50247c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
50347c88fffSLiam Girdwood 		const char *dai_link, int stream)
50447c88fffSLiam Girdwood {
50547c88fffSLiam Girdwood 	int i;
50647c88fffSLiam Girdwood 
50747c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
50847c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
50947c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
51047c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
51147c88fffSLiam Girdwood 	}
512f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
51347c88fffSLiam Girdwood 	return NULL;
51447c88fffSLiam Girdwood }
51547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
51647c88fffSLiam Girdwood 
51747c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
51847c88fffSLiam Girdwood 		const char *dai_link)
51947c88fffSLiam Girdwood {
52047c88fffSLiam Girdwood 	int i;
52147c88fffSLiam Girdwood 
52247c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
52347c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
52447c88fffSLiam Girdwood 			return &card->rtd[i];
52547c88fffSLiam Girdwood 	}
526f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
52747c88fffSLiam Girdwood 	return NULL;
52847c88fffSLiam Girdwood }
52947c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
53047c88fffSLiam Girdwood 
531db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
532db2a4165SFrank Mandarino /* unregister ac97 codec */
533db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
534db2a4165SFrank Mandarino {
535db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
536db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
537db2a4165SFrank Mandarino 	return 0;
538db2a4165SFrank Mandarino }
539db2a4165SFrank Mandarino 
540db2a4165SFrank Mandarino /* stop no dev release warning */
541db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
542db2a4165SFrank Mandarino 
543db2a4165SFrank Mandarino /* register ac97 codec to bus */
544db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
545db2a4165SFrank Mandarino {
546db2a4165SFrank Mandarino 	int err;
547db2a4165SFrank Mandarino 
548db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
54900200107SLars-Peter Clausen 	codec->ac97->dev.parent = codec->component.card->dev;
550db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
551db2a4165SFrank Mandarino 
552bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
55300200107SLars-Peter Clausen 		     codec->component.card->snd_card->number, 0,
55400200107SLars-Peter Clausen 		     codec->component.name);
555db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
556db2a4165SFrank Mandarino 	if (err < 0) {
557f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
558db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
559db2a4165SFrank Mandarino 		return err;
560db2a4165SFrank Mandarino 	}
561db2a4165SFrank Mandarino 	return 0;
562db2a4165SFrank Mandarino }
563db2a4165SFrank Mandarino #endif
564db2a4165SFrank Mandarino 
5659d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5669d58a077SRichard Fitzgerald {
5679d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5689d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5699d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5709d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5719d58a077SRichard Fitzgerald 	 */
5729d58a077SRichard Fitzgerald }
5739d58a077SRichard Fitzgerald 
5746f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
575db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5766f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
577db2a4165SFrank Mandarino {
5786f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5792eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
58088bd870fSBenoit Cousson 	int i, j;
581db2a4165SFrank Mandarino 
582e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
583e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
584e3509ff0SDaniel Mack 	 */
585f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
586e3509ff0SDaniel Mack 		return 0;
587e3509ff0SDaniel Mack 
5886ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5896ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5906ed25978SAndy Green 	 */
591f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
592f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
593f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5946ed25978SAndy Green 
5956ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
596f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5976ed25978SAndy Green 
598a00f90f9SMark Brown 	/* mute any active DACs */
599f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6003efab7dcSMark Brown 
601f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6023efab7dcSMark Brown 			continue;
6033efab7dcSMark Brown 
60488bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
60588bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
60688bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
60788bd870fSBenoit Cousson 
608f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
609f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
610db2a4165SFrank Mandarino 		}
61188bd870fSBenoit Cousson 	}
612db2a4165SFrank Mandarino 
6134ccab3e7SLiam Girdwood 	/* suspend all pcms */
614f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
615f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6163efab7dcSMark Brown 			continue;
6173efab7dcSMark Brown 
618f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
6193efab7dcSMark Brown 	}
6204ccab3e7SLiam Girdwood 
62187506549SMark Brown 	if (card->suspend_pre)
62270b2ac12SMark Brown 		card->suspend_pre(card);
623db2a4165SFrank Mandarino 
624f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
625f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
626f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
6273efab7dcSMark Brown 
628f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6293efab7dcSMark Brown 			continue;
6303efab7dcSMark Brown 
631f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
632f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
633f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
634f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
635f0fba2adSLiam Girdwood 			platform->suspended = 1;
636f0fba2adSLiam Girdwood 		}
637db2a4165SFrank Mandarino 	}
638db2a4165SFrank Mandarino 
639db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
640f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
64188bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
64243829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
64388bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
64488bd870fSBenoit Cousson 			codec_dais[j]->codec->dapm.suspend_bias_level =
64588bd870fSBenoit Cousson 					codec_dais[j]->codec->dapm.bias_level;
64688bd870fSBenoit Cousson 		}
647f0fba2adSLiam Girdwood 	}
648db2a4165SFrank Mandarino 
649f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6503efab7dcSMark Brown 
651f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6523efab7dcSMark Brown 			continue;
6533efab7dcSMark Brown 
6547bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6557bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
656db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
657f0fba2adSLiam Girdwood 
6587bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6597bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
660db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
661db2a4165SFrank Mandarino 	}
662db2a4165SFrank Mandarino 
663e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
664e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
665e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
666e2d32ff6SMark Brown 
667f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6682eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6691547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6701547aba9SMark Brown 		 * bias _ON and should not be suspended. */
671f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
672ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6731547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
674125a25daSMark Brown 				/*
675125a25daSMark Brown 				 * If the CODEC is capable of idle
676125a25daSMark Brown 				 * bias off then being in STANDBY
677125a25daSMark Brown 				 * means it's doing something,
678125a25daSMark Brown 				 * otherwise fall through.
679125a25daSMark Brown 				 */
680125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
681125a25daSMark Brown 					dev_dbg(codec->dev,
68210e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
683125a25daSMark Brown 					break;
684125a25daSMark Brown 				}
6851547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
68684b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
687f0fba2adSLiam Girdwood 				codec->suspended = 1;
6887be4ba24SMark Brown 				codec->cache_sync = 1;
689e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
690e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
691988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
692988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6931547aba9SMark Brown 				break;
6941547aba9SMark Brown 			default:
69510e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
69610e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6971547aba9SMark Brown 				break;
6981547aba9SMark Brown 			}
6991547aba9SMark Brown 		}
700f0fba2adSLiam Girdwood 	}
701db2a4165SFrank Mandarino 
702f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
703f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7043efab7dcSMark Brown 
705f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7063efab7dcSMark Brown 			continue;
7073efab7dcSMark Brown 
708f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
709f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
710988e8cc4SNicolin Chen 
711988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
712988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
713db2a4165SFrank Mandarino 	}
714db2a4165SFrank Mandarino 
71587506549SMark Brown 	if (card->suspend_post)
71670b2ac12SMark Brown 		card->suspend_post(card);
717db2a4165SFrank Mandarino 
718db2a4165SFrank Mandarino 	return 0;
719db2a4165SFrank Mandarino }
7206f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
721db2a4165SFrank Mandarino 
7226ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7236ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7246ed25978SAndy Green  */
7256ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
726db2a4165SFrank Mandarino {
727f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
728f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7292eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
73088bd870fSBenoit Cousson 	int i, j;
731db2a4165SFrank Mandarino 
7326ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7336ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7346ed25978SAndy Green 	 */
7356ed25978SAndy Green 
736f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7376ed25978SAndy Green 
7389949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
739f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7409949788bSMark Brown 
74187506549SMark Brown 	if (card->resume_pre)
74270b2ac12SMark Brown 		card->resume_pre(card);
743db2a4165SFrank Mandarino 
744f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
745f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
746f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7473efab7dcSMark Brown 
748f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7493efab7dcSMark Brown 			continue;
7503efab7dcSMark Brown 
751f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
752f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
753db2a4165SFrank Mandarino 	}
754db2a4165SFrank Mandarino 
7552eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7561547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7571547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7581547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7591547aba9SMark Brown 		 */
760f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
761ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7621547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7631547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
764f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
765f0fba2adSLiam Girdwood 				codec->suspended = 0;
7661547aba9SMark Brown 				break;
7671547aba9SMark Brown 			default:
76810e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
76910e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7701547aba9SMark Brown 				break;
7711547aba9SMark Brown 			}
7721547aba9SMark Brown 		}
773f0fba2adSLiam Girdwood 	}
774db2a4165SFrank Mandarino 
775f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7763efab7dcSMark Brown 
777f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7783efab7dcSMark Brown 			continue;
7793efab7dcSMark Brown 
7807bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
781d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
782db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
783f0fba2adSLiam Girdwood 
7847bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
785d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
786db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
787db2a4165SFrank Mandarino 	}
788db2a4165SFrank Mandarino 
7893ff3f64bSMark Brown 	/* unmute any active DACs */
790f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7913efab7dcSMark Brown 
792f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7933efab7dcSMark Brown 			continue;
7943efab7dcSMark Brown 
79588bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
79688bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
79788bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
79888bd870fSBenoit Cousson 
799f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
800f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
801db2a4165SFrank Mandarino 		}
80288bd870fSBenoit Cousson 	}
803db2a4165SFrank Mandarino 
804f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
805f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
806f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
8073efab7dcSMark Brown 
808f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
8093efab7dcSMark Brown 			continue;
8103efab7dcSMark Brown 
811f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
812f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
813f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
814f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
815f0fba2adSLiam Girdwood 			platform->suspended = 0;
816f0fba2adSLiam Girdwood 		}
817db2a4165SFrank Mandarino 	}
818db2a4165SFrank Mandarino 
81987506549SMark Brown 	if (card->resume_post)
82070b2ac12SMark Brown 		card->resume_post(card);
821db2a4165SFrank Mandarino 
822f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
8236ed25978SAndy Green 
8246ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
825f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
826e2d32ff6SMark Brown 
827e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
828e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
829e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8306ed25978SAndy Green }
8316ed25978SAndy Green 
8326ed25978SAndy Green /* powers up audio subsystem after a suspend */
8336f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8346ed25978SAndy Green {
8356f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
83682e14e8bSStephen Warren 	int i, ac97_control = 0;
837b9dd94a8SPeter Ujfalusi 
8385ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
8395ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
8405ff1ddf2SEric Miao 	 */
8415ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
8425ff1ddf2SEric Miao 		return 0;
8435ff1ddf2SEric Miao 
844988e8cc4SNicolin Chen 	/* activate pins from sleep state */
845988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
84688bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
84788bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
84888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
84988bd870fSBenoit Cousson 		int j;
85088bd870fSBenoit Cousson 
851988e8cc4SNicolin Chen 		if (cpu_dai->active)
852988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
85388bd870fSBenoit Cousson 
85488bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
85588bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
856988e8cc4SNicolin Chen 			if (codec_dai->active)
857988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
858988e8cc4SNicolin Chen 		}
85988bd870fSBenoit Cousson 	}
860988e8cc4SNicolin Chen 
86164ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
86264ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
86364ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
86464ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
86564ab9baaSMark Brown 	 */
866f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
867f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
86882e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
86982e14e8bSStephen Warren 	}
87082e14e8bSStephen Warren 	if (ac97_control) {
871f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
87264ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
87364ab9baaSMark Brown 	} else {
874f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8756308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
876f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
877f0fba2adSLiam Girdwood 	}
8786ed25978SAndy Green 
879db2a4165SFrank Mandarino 	return 0;
880db2a4165SFrank Mandarino }
8816f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
882db2a4165SFrank Mandarino #else
8836f8ab4acSMark Brown #define snd_soc_suspend NULL
8846f8ab4acSMark Brown #define snd_soc_resume NULL
885db2a4165SFrank Mandarino #endif
886db2a4165SFrank Mandarino 
88785e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
88802a06d30SBarry Song };
88902a06d30SBarry Song 
89088bd870fSBenoit Cousson static struct snd_soc_codec *soc_find_codec(
89188bd870fSBenoit Cousson 					const struct device_node *codec_of_node,
89212023a9aSMisael Lopez Cruz 					const char *codec_name)
89312023a9aSMisael Lopez Cruz {
89412023a9aSMisael Lopez Cruz 	struct snd_soc_codec *codec;
89512023a9aSMisael Lopez Cruz 
89612023a9aSMisael Lopez Cruz 	list_for_each_entry(codec, &codec_list, list) {
89712023a9aSMisael Lopez Cruz 		if (codec_of_node) {
89812023a9aSMisael Lopez Cruz 			if (codec->dev->of_node != codec_of_node)
89912023a9aSMisael Lopez Cruz 				continue;
90012023a9aSMisael Lopez Cruz 		} else {
901f4333203SLars-Peter Clausen 			if (strcmp(codec->component.name, codec_name))
90212023a9aSMisael Lopez Cruz 				continue;
90312023a9aSMisael Lopez Cruz 		}
90412023a9aSMisael Lopez Cruz 
90512023a9aSMisael Lopez Cruz 		return codec;
90612023a9aSMisael Lopez Cruz 	}
90712023a9aSMisael Lopez Cruz 
90812023a9aSMisael Lopez Cruz 	return NULL;
90912023a9aSMisael Lopez Cruz }
91012023a9aSMisael Lopez Cruz 
91112023a9aSMisael Lopez Cruz static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
91212023a9aSMisael Lopez Cruz 					      const char *codec_dai_name)
91312023a9aSMisael Lopez Cruz {
91412023a9aSMisael Lopez Cruz 	struct snd_soc_dai *codec_dai;
91512023a9aSMisael Lopez Cruz 
91612023a9aSMisael Lopez Cruz 	list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
91712023a9aSMisael Lopez Cruz 		if (!strcmp(codec_dai->name, codec_dai_name)) {
91812023a9aSMisael Lopez Cruz 			return codec_dai;
91912023a9aSMisael Lopez Cruz 		}
92012023a9aSMisael Lopez Cruz 	}
92112023a9aSMisael Lopez Cruz 
92212023a9aSMisael Lopez Cruz 	return NULL;
92312023a9aSMisael Lopez Cruz }
92412023a9aSMisael Lopez Cruz 
925f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
926db2a4165SFrank Mandarino {
927f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
928f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
9291438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
93088bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
93188bd870fSBenoit Cousson 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
932435c5e25SMark Brown 	struct snd_soc_platform *platform;
93312023a9aSMisael Lopez Cruz 	struct snd_soc_dai *cpu_dai;
934848dd8beSMark Brown 	const char *platform_name;
93588bd870fSBenoit Cousson 	int i;
936db2a4165SFrank Mandarino 
937f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
9386308419aSMark Brown 
939b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
9401438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
941bc92657aSStephen Warren 		if (dai_link->cpu_of_node &&
9421438c2f6SLars-Peter Clausen 			component->dev->of_node != dai_link->cpu_of_node)
9435a504963SStephen Warren 			continue;
944bc92657aSStephen Warren 		if (dai_link->cpu_name &&
9451438c2f6SLars-Peter Clausen 			strcmp(dev_name(component->dev), dai_link->cpu_name))
9462610ab77SStephen Warren 			continue;
9471438c2f6SLars-Peter Clausen 		list_for_each_entry(cpu_dai, &component->dai_list, list) {
948bc92657aSStephen Warren 			if (dai_link->cpu_dai_name &&
949bc92657aSStephen Warren 				strcmp(cpu_dai->name, dai_link->cpu_dai_name))
950bc92657aSStephen Warren 				continue;
9512610ab77SStephen Warren 
952f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
953f0fba2adSLiam Girdwood 		}
9541438c2f6SLars-Peter Clausen 	}
955b19e6e7bSMark Brown 
956b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
957f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
958f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
959b19e6e7bSMark Brown 		return -EPROBE_DEFER;
960c5af3a2eSMark Brown 	}
961c5af3a2eSMark Brown 
96288bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
96388bd870fSBenoit Cousson 
96488bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
96588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
96688bd870fSBenoit Cousson 		struct snd_soc_codec *codec;
96788bd870fSBenoit Cousson 		codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
96888bd870fSBenoit Cousson 		if (!codec) {
969f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: CODEC %s not registered\n",
97088bd870fSBenoit Cousson 				codecs[i].name);
971b19e6e7bSMark Brown 			return -EPROBE_DEFER;
972b19e6e7bSMark Brown 		}
973848dd8beSMark Brown 
97488bd870fSBenoit Cousson 		codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
97588bd870fSBenoit Cousson 		if (!codec_dais[i]) {
97612023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
97788bd870fSBenoit Cousson 				codecs[i].dai_name);
97812023a9aSMisael Lopez Cruz 			return -EPROBE_DEFER;
97912023a9aSMisael Lopez Cruz 		}
98088bd870fSBenoit Cousson 	}
98188bd870fSBenoit Cousson 
98288bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
98388bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
98488bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
98512023a9aSMisael Lopez Cruz 
986848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
987848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9885a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
989848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
990848dd8beSMark Brown 
991b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
992f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9935a504963SStephen Warren 		if (dai_link->platform_of_node) {
9945a504963SStephen Warren 			if (platform->dev->of_node !=
9955a504963SStephen Warren 			    dai_link->platform_of_node)
9965a504963SStephen Warren 				continue;
9975a504963SStephen Warren 		} else {
998f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
9992610ab77SStephen Warren 				continue;
10005a504963SStephen Warren 		}
10012610ab77SStephen Warren 
1002f0fba2adSLiam Girdwood 		rtd->platform = platform;
1003f0fba2adSLiam Girdwood 	}
1004b19e6e7bSMark Brown 	if (!rtd->platform) {
1005f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
1006f0fba2adSLiam Girdwood 			dai_link->platform_name);
1007b19e6e7bSMark Brown 		return -EPROBE_DEFER;
1008f0fba2adSLiam Girdwood 	}
1009b19e6e7bSMark Brown 
1010b19e6e7bSMark Brown 	card->num_rtd++;
1011b19e6e7bSMark Brown 
1012b19e6e7bSMark Brown 	return 0;
10136b05eda6SMark Brown }
10146b05eda6SMark Brown 
1015d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform)
1016d12cd198SStephen Warren {
1017d12cd198SStephen Warren 	int ret;
1018d12cd198SStephen Warren 
1019d12cd198SStephen Warren 	if (platform->driver->remove) {
1020d12cd198SStephen Warren 		ret = platform->driver->remove(platform);
1021d12cd198SStephen Warren 		if (ret < 0)
1022f110bfc7SLiam Girdwood 			dev_err(platform->dev, "ASoC: failed to remove %d\n",
1023f110bfc7SLiam Girdwood 				ret);
1024d12cd198SStephen Warren 	}
1025d12cd198SStephen Warren 
1026d12cd198SStephen Warren 	/* Make sure all DAPM widgets are freed */
1027bc9af9faSLars-Peter Clausen 	snd_soc_dapm_free(&platform->component.dapm);
1028d12cd198SStephen Warren 
1029d12cd198SStephen Warren 	soc_cleanup_platform_debugfs(platform);
1030d12cd198SStephen Warren 	platform->probed = 0;
1031d12cd198SStephen Warren 	module_put(platform->dev->driver->owner);
1032d12cd198SStephen Warren 
1033d12cd198SStephen Warren 	return 0;
1034d12cd198SStephen Warren }
1035d12cd198SStephen Warren 
1036589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
1037589c3563SJarkko Nikula {
1038589c3563SJarkko Nikula 	int err;
1039589c3563SJarkko Nikula 
1040589c3563SJarkko Nikula 	if (codec->driver->remove) {
1041589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
1042589c3563SJarkko Nikula 		if (err < 0)
1043f110bfc7SLiam Girdwood 			dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
1044589c3563SJarkko Nikula 	}
1045589c3563SJarkko Nikula 
1046589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
1047589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
1048589c3563SJarkko Nikula 
1049589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
1050589c3563SJarkko Nikula 	codec->probed = 0;
1051589c3563SJarkko Nikula 	list_del(&codec->card_list);
1052589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
1053589c3563SJarkko Nikula }
1054589c3563SJarkko Nikula 
1055b0aa88afSMisael Lopez Cruz static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1056b0aa88afSMisael Lopez Cruz {
1057b0aa88afSMisael Lopez Cruz 	int err;
1058b0aa88afSMisael Lopez Cruz 
1059b0aa88afSMisael Lopez Cruz 	if (codec_dai && codec_dai->probed &&
1060b0aa88afSMisael Lopez Cruz 			codec_dai->driver->remove_order == order) {
1061b0aa88afSMisael Lopez Cruz 		if (codec_dai->driver->remove) {
1062b0aa88afSMisael Lopez Cruz 			err = codec_dai->driver->remove(codec_dai);
1063b0aa88afSMisael Lopez Cruz 			if (err < 0)
1064b0aa88afSMisael Lopez Cruz 				dev_err(codec_dai->dev,
1065b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1066b0aa88afSMisael Lopez Cruz 					codec_dai->name, err);
1067b0aa88afSMisael Lopez Cruz 		}
1068b0aa88afSMisael Lopez Cruz 		codec_dai->probed = 0;
1069b0aa88afSMisael Lopez Cruz 	}
1070b0aa88afSMisael Lopez Cruz }
1071b0aa88afSMisael Lopez Cruz 
107262ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1073f0fba2adSLiam Girdwood {
1074f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
107588bd870fSBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
107688bd870fSBenoit Cousson 	int i, err;
1077f0fba2adSLiam Girdwood 
1078f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1079f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
108036ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
108136ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
108236ae1a96SMark Brown 		device_unregister(rtd->dev);
1083f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
108402a06d30SBarry Song 	}
108502a06d30SBarry Song 
1086f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
108788bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
108888bd870fSBenoit Cousson 		soc_remove_codec_dai(rtd->codec_dais[i], order);
1089f0fba2adSLiam Girdwood 
1090f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
10910168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
10920168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
1093f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1094f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1095f0fba2adSLiam Girdwood 			if (err < 0)
1096f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1097f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
10980837fc62SFabio Estevam 					cpu_dai->name, err);
1099f0fba2adSLiam Girdwood 		}
1100f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
11019420d97bSLars-Peter Clausen 		if (!cpu_dai->codec)
1102f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
1103f0fba2adSLiam Girdwood 	}
1104f0fba2adSLiam Girdwood }
1105f0fba2adSLiam Girdwood 
110662ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
110762ae68faSStephen Warren 				       int order)
110862ae68faSStephen Warren {
110962ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
111062ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
111162ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
111262ae68faSStephen Warren 	struct snd_soc_codec *codec;
111388bd870fSBenoit Cousson 	int i;
111462ae68faSStephen Warren 
111562ae68faSStephen Warren 	/* remove the platform */
111662ae68faSStephen Warren 	if (platform && platform->probed &&
111762ae68faSStephen Warren 	    platform->driver->remove_order == order) {
111862ae68faSStephen Warren 		soc_remove_platform(platform);
111962ae68faSStephen Warren 	}
112062ae68faSStephen Warren 
112162ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
112288bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
112388bd870fSBenoit Cousson 		codec = rtd->codec_dais[i]->codec;
112462ae68faSStephen Warren 		if (codec && codec->probed &&
112562ae68faSStephen Warren 		    codec->driver->remove_order == order)
112662ae68faSStephen Warren 			soc_remove_codec(codec);
112762ae68faSStephen Warren 	}
112862ae68faSStephen Warren 
112962ae68faSStephen Warren 	/* remove any CPU-side CODEC */
113062ae68faSStephen Warren 	if (cpu_dai) {
113162ae68faSStephen Warren 		codec = cpu_dai->codec;
113262ae68faSStephen Warren 		if (codec && codec->probed &&
113362ae68faSStephen Warren 		    codec->driver->remove_order == order)
113462ae68faSStephen Warren 			soc_remove_codec(codec);
113562ae68faSStephen Warren 	}
113662ae68faSStephen Warren }
113762ae68faSStephen Warren 
11380671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11390671fd8eSKuninori Morimoto {
11400168bf0dSLiam Girdwood 	int dai, order;
11410671fd8eSKuninori Morimoto 
11420168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
11430168bf0dSLiam Girdwood 			order++) {
11440168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
114562ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
11460168bf0dSLiam Girdwood 	}
114762ae68faSStephen Warren 
114862ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
114962ae68faSStephen Warren 			order++) {
115062ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
115162ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
115262ae68faSStephen Warren 	}
115362ae68faSStephen Warren 
11540671fd8eSKuninori Morimoto 	card->num_rtd = 0;
11550671fd8eSKuninori Morimoto }
11560671fd8eSKuninori Morimoto 
1157ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
115894f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1159ead9b919SJarkko Nikula {
1160ead9b919SJarkko Nikula 	int i;
1161ead9b919SJarkko Nikula 
1162ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1163ead9b919SJarkko Nikula 		return;
1164ead9b919SJarkko Nikula 
1165ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1166ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
116794f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
11683ca041edSSebastian Reichel 			continue;
116994f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
11703ca041edSSebastian Reichel 			continue;
117194f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1172ead9b919SJarkko Nikula 		break;
1173ead9b919SJarkko Nikula 	}
1174ead9b919SJarkko Nikula }
1175ead9b919SJarkko Nikula 
1176589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1177589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1178589c3563SJarkko Nikula {
1179589c3563SJarkko Nikula 	int ret = 0;
118089b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1181888df395SMark Brown 	struct snd_soc_dai *dai;
1182589c3563SJarkko Nikula 
118300200107SLars-Peter Clausen 	codec->component.card = card;
1184589c3563SJarkko Nikula 	codec->dapm.card = card;
118594f99c87SLars-Peter Clausen 	soc_set_name_prefix(card, &codec->component);
1186589c3563SJarkko Nikula 
118770d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
118870d29331SJarkko Nikula 		return -ENODEV;
118970d29331SJarkko Nikula 
1190d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1191d5d1e0beSLars-Peter Clausen 
1192b318ad50SNariman Poushin 	if (driver->dapm_widgets) {
1193b318ad50SNariman Poushin 		ret = snd_soc_dapm_new_controls(&codec->dapm,
1194b318ad50SNariman Poushin 						driver->dapm_widgets,
119577530150SLars-Peter Clausen 					 	driver->num_dapm_widgets);
119677530150SLars-Peter Clausen 
1197b318ad50SNariman Poushin 		if (ret != 0) {
1198b318ad50SNariman Poushin 			dev_err(codec->dev,
1199b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1200b318ad50SNariman Poushin 			goto err_probe;
1201b318ad50SNariman Poushin 		}
1202b318ad50SNariman Poushin 	}
1203b318ad50SNariman Poushin 
1204888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1205261edc70SNariman Poushin 	list_for_each_entry(dai, &codec->component.dai_list, list) {
1206261edc70SNariman Poushin 		ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1207261edc70SNariman Poushin 
1208261edc70SNariman Poushin 		if (ret != 0) {
1209261edc70SNariman Poushin 			dev_err(codec->dev,
1210261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1211261edc70SNariman Poushin 			goto err_probe;
1212261edc70SNariman Poushin 		}
1213261edc70SNariman Poushin 	}
1214888df395SMark Brown 
121533c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
121633c5f969SMark Brown 
121789b95ac0SMark Brown 	if (driver->probe) {
121889b95ac0SMark Brown 		ret = driver->probe(codec);
1219589c3563SJarkko Nikula 		if (ret < 0) {
1220589c3563SJarkko Nikula 			dev_err(codec->dev,
1221f110bfc7SLiam Girdwood 				"ASoC: failed to probe CODEC %d\n", ret);
122270d29331SJarkko Nikula 			goto err_probe;
1223589c3563SJarkko Nikula 		}
1224ff541f4bSChuansheng Liu 		WARN(codec->dapm.idle_bias_off &&
1225ff541f4bSChuansheng Liu 			codec->dapm.bias_level != SND_SOC_BIAS_OFF,
122610e8aa9aSMichał Mirosław 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1227f4333203SLars-Peter Clausen 			codec->component.name);
1228589c3563SJarkko Nikula 	}
1229589c3563SJarkko Nikula 
1230b7af1dafSMark Brown 	if (driver->controls)
1231022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1232b7af1dafSMark Brown 				     driver->num_controls);
123389b95ac0SMark Brown 	if (driver->dapm_routes)
123489b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
123589b95ac0SMark Brown 					driver->num_dapm_routes);
123689b95ac0SMark Brown 
1237589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1238589c3563SJarkko Nikula 	codec->probed = 1;
1239589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
12407be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1241589c3563SJarkko Nikula 
124270d29331SJarkko Nikula 	return 0;
124370d29331SJarkko Nikula 
124470d29331SJarkko Nikula err_probe:
1245d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
124670d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
124770d29331SJarkko Nikula 
1248589c3563SJarkko Nikula 	return ret;
1249589c3563SJarkko Nikula }
1250589c3563SJarkko Nikula 
1251956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1252956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1253956245e9SLiam Girdwood {
1254956245e9SLiam Girdwood 	int ret = 0;
1255956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
12561438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
1257be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1258956245e9SLiam Girdwood 
125900200107SLars-Peter Clausen 	platform->component.card = card;
1260bc9af9faSLars-Peter Clausen 	platform->component.dapm.card = card;
1261956245e9SLiam Girdwood 
1262956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1263956245e9SLiam Girdwood 		return -ENODEV;
1264956245e9SLiam Girdwood 
1265731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1266731f1ab2SSebastien Guiriec 
1267cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1268bc9af9faSLars-Peter Clausen 		snd_soc_dapm_new_controls(&platform->component.dapm,
1269cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1270cb2cf612SLiam Girdwood 
1271be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
12721438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
12731438c2f6SLars-Peter Clausen 		if (component->dev != platform->dev)
1274be09ad90SLiam Girdwood 			continue;
12751438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list)
1276bc9af9faSLars-Peter Clausen 			snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1277bc9af9faSLars-Peter Clausen 				dai);
1278be09ad90SLiam Girdwood 	}
1279be09ad90SLiam Girdwood 
1280bc9af9faSLars-Peter Clausen 	platform->component.dapm.idle_bias_off = 1;
12813fec6b6dSStephen Warren 
1282956245e9SLiam Girdwood 	if (driver->probe) {
1283956245e9SLiam Girdwood 		ret = driver->probe(platform);
1284956245e9SLiam Girdwood 		if (ret < 0) {
1285956245e9SLiam Girdwood 			dev_err(platform->dev,
1286f110bfc7SLiam Girdwood 				"ASoC: failed to probe platform %d\n", ret);
1287956245e9SLiam Girdwood 			goto err_probe;
1288956245e9SLiam Girdwood 		}
1289956245e9SLiam Girdwood 	}
1290956245e9SLiam Girdwood 
1291cb2cf612SLiam Girdwood 	if (driver->controls)
1292cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1293cb2cf612SLiam Girdwood 				     driver->num_controls);
1294cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1295bc9af9faSLars-Peter Clausen 		snd_soc_dapm_add_routes(&platform->component.dapm,
1296bc9af9faSLars-Peter Clausen 			driver->dapm_routes, driver->num_dapm_routes);
1297cb2cf612SLiam Girdwood 
1298956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1299956245e9SLiam Girdwood 	platform->probed = 1;
1300bc9af9faSLars-Peter Clausen 	list_add(&platform->component.dapm.list, &card->dapm_list);
1301956245e9SLiam Girdwood 
1302956245e9SLiam Girdwood 	return 0;
1303956245e9SLiam Girdwood 
1304956245e9SLiam Girdwood err_probe:
130502db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1306956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1307956245e9SLiam Girdwood 
1308956245e9SLiam Girdwood 	return ret;
1309956245e9SLiam Girdwood }
1310956245e9SLiam Girdwood 
131136ae1a96SMark Brown static void rtd_release(struct device *dev)
131236ae1a96SMark Brown {
131336ae1a96SMark Brown 	kfree(dev);
131436ae1a96SMark Brown }
1315f0fba2adSLiam Girdwood 
13165f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
13175f3484acSLars-Peter Clausen 	const char *name)
1318503ae5e0SMisael Lopez Cruz {
1319589c3563SJarkko Nikula 	int ret = 0;
1320589c3563SJarkko Nikula 
1321589c3563SJarkko Nikula 	/* register the rtd device */
132236ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
132336ae1a96SMark Brown 	if (!rtd->dev)
132436ae1a96SMark Brown 		return -ENOMEM;
132536ae1a96SMark Brown 	device_initialize(rtd->dev);
13265f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
132736ae1a96SMark Brown 	rtd->dev->release = rtd_release;
132836ae1a96SMark Brown 	rtd->dev->init_name = name;
132936ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1330b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
133101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
133201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
133301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
133401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
133536ae1a96SMark Brown 	ret = device_add(rtd->dev);
1336589c3563SJarkko Nikula 	if (ret < 0) {
1337865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1338865df9cbSChuansheng Liu 		put_device(rtd->dev);
13395f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1340f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1341589c3563SJarkko Nikula 		return ret;
1342589c3563SJarkko Nikula 	}
1343589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1344589c3563SJarkko Nikula 
1345589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
134636ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1347589c3563SJarkko Nikula 	if (ret < 0)
134844c69bb1SLars-Peter Clausen 		dev_err(rtd->dev,
1349f110bfc7SLiam Girdwood 			"ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1350589c3563SJarkko Nikula 
1351589c3563SJarkko Nikula 	/* add codec sysfs entries */
135236ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1353589c3563SJarkko Nikula 	if (ret < 0)
135444c69bb1SLars-Peter Clausen 		dev_err(rtd->dev,
1355f110bfc7SLiam Girdwood 			"ASoC: failed to add codec sysfs files: %d\n", ret);
1356589c3563SJarkko Nikula 
1357589c3563SJarkko Nikula 	return 0;
1358589c3563SJarkko Nikula }
1359589c3563SJarkko Nikula 
136062ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
136162ae68faSStephen Warren 				     int order)
136262ae68faSStephen Warren {
136362ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
136462ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
136562ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
136688bd870fSBenoit Cousson 	int i, ret;
136762ae68faSStephen Warren 
136862ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
136962ae68faSStephen Warren 	if (cpu_dai->codec &&
137062ae68faSStephen Warren 	    !cpu_dai->codec->probed &&
137162ae68faSStephen Warren 	    cpu_dai->codec->driver->probe_order == order) {
137262ae68faSStephen Warren 		ret = soc_probe_codec(card, cpu_dai->codec);
137362ae68faSStephen Warren 		if (ret < 0)
137462ae68faSStephen Warren 			return ret;
137562ae68faSStephen Warren 	}
137662ae68faSStephen Warren 
137788bd870fSBenoit Cousson 	/* probe the CODEC-side components */
137888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
137988bd870fSBenoit Cousson 		if (!rtd->codec_dais[i]->codec->probed &&
138088bd870fSBenoit Cousson 		    rtd->codec_dais[i]->codec->driver->probe_order == order) {
138188bd870fSBenoit Cousson 			ret = soc_probe_codec(card, rtd->codec_dais[i]->codec);
138262ae68faSStephen Warren 			if (ret < 0)
138362ae68faSStephen Warren 				return ret;
138462ae68faSStephen Warren 		}
138588bd870fSBenoit Cousson 	}
138662ae68faSStephen Warren 
138762ae68faSStephen Warren 	/* probe the platform */
138862ae68faSStephen Warren 	if (!platform->probed &&
138962ae68faSStephen Warren 	    platform->driver->probe_order == order) {
139062ae68faSStephen Warren 		ret = soc_probe_platform(card, platform);
139162ae68faSStephen Warren 		if (ret < 0)
139262ae68faSStephen Warren 			return ret;
139362ae68faSStephen Warren 	}
139462ae68faSStephen Warren 
139562ae68faSStephen Warren 	return 0;
139662ae68faSStephen Warren }
139762ae68faSStephen Warren 
1398b0aa88afSMisael Lopez Cruz static int soc_probe_codec_dai(struct snd_soc_card *card,
1399b0aa88afSMisael Lopez Cruz 			       struct snd_soc_dai *codec_dai,
1400b0aa88afSMisael Lopez Cruz 			       int order)
1401b0aa88afSMisael Lopez Cruz {
1402b0aa88afSMisael Lopez Cruz 	int ret;
1403b0aa88afSMisael Lopez Cruz 
1404b0aa88afSMisael Lopez Cruz 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1405b0aa88afSMisael Lopez Cruz 		if (codec_dai->driver->probe) {
1406b0aa88afSMisael Lopez Cruz 			ret = codec_dai->driver->probe(codec_dai);
1407b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
1408b0aa88afSMisael Lopez Cruz 				dev_err(codec_dai->dev,
1409b0aa88afSMisael Lopez Cruz 					"ASoC: failed to probe CODEC DAI %s: %d\n",
1410b0aa88afSMisael Lopez Cruz 					codec_dai->name, ret);
1411b0aa88afSMisael Lopez Cruz 				return ret;
1412b0aa88afSMisael Lopez Cruz 			}
1413b0aa88afSMisael Lopez Cruz 		}
1414b0aa88afSMisael Lopez Cruz 
1415b0aa88afSMisael Lopez Cruz 		/* mark codec_dai as probed and add to card dai list */
1416b0aa88afSMisael Lopez Cruz 		codec_dai->probed = 1;
1417b0aa88afSMisael Lopez Cruz 	}
1418b0aa88afSMisael Lopez Cruz 
1419b0aa88afSMisael Lopez Cruz 	return 0;
1420b0aa88afSMisael Lopez Cruz }
1421b0aa88afSMisael Lopez Cruz 
14222436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
14232436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
14243f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
14252436a723SMisael Lopez Cruz {
14263f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
14273f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
14282436a723SMisael Lopez Cruz 	struct snd_soc_dapm_widget *play_w, *capture_w;
14292436a723SMisael Lopez Cruz 	int ret;
14302436a723SMisael Lopez Cruz 
143188bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
143288bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
143388bd870fSBenoit Cousson 
14342436a723SMisael Lopez Cruz 	/* link the DAI widgets */
14352436a723SMisael Lopez Cruz 	play_w = codec_dai->playback_widget;
14362436a723SMisael Lopez Cruz 	capture_w = cpu_dai->capture_widget;
14372436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
14382436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
14392436a723SMisael Lopez Cruz 					   capture_w, play_w);
14402436a723SMisael Lopez Cruz 		if (ret != 0) {
14412436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
14422436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
14432436a723SMisael Lopez Cruz 			return ret;
14442436a723SMisael Lopez Cruz 		}
14452436a723SMisael Lopez Cruz 	}
14462436a723SMisael Lopez Cruz 
14472436a723SMisael Lopez Cruz 	play_w = cpu_dai->playback_widget;
14482436a723SMisael Lopez Cruz 	capture_w = codec_dai->capture_widget;
14492436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
14502436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
14512436a723SMisael Lopez Cruz 					   capture_w, play_w);
14522436a723SMisael Lopez Cruz 		if (ret != 0) {
14532436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
14542436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
14552436a723SMisael Lopez Cruz 			return ret;
14562436a723SMisael Lopez Cruz 		}
14572436a723SMisael Lopez Cruz 	}
14582436a723SMisael Lopez Cruz 
14592436a723SMisael Lopez Cruz 	return 0;
14602436a723SMisael Lopez Cruz }
14612436a723SMisael Lopez Cruz 
146262ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1463f0fba2adSLiam Girdwood {
1464f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1465f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1466f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1467c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
146888bd870fSBenoit Cousson 	int i, ret;
1469f0fba2adSLiam Girdwood 
1470f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14710168bf0dSLiam Girdwood 			card->name, num, order);
1472f0fba2adSLiam Girdwood 
1473f0fba2adSLiam Girdwood 	/* config components */
1474f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1475f0fba2adSLiam Girdwood 	cpu_dai->card = card;
147688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
147788bd870fSBenoit Cousson 		rtd->codec_dais[i]->card = card;
1478f0fba2adSLiam Girdwood 
1479f0fba2adSLiam Girdwood 	/* set default power off timeout */
1480f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1481f0fba2adSLiam Girdwood 
1482f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
14830168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
14840168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1485a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
148661b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
148761b61e3cSLiam Girdwood 				return -ENODEV;
1488a9db7dbeSStephen Warren 		}
1489be09ad90SLiam Girdwood 
1490f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1491f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1492f0fba2adSLiam Girdwood 			if (ret < 0) {
1493f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1494f110bfc7SLiam Girdwood 					"ASoC: failed to probe CPU DAI %s: %d\n",
14950837fc62SFabio Estevam 					cpu_dai->name, ret);
149661b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1497f0fba2adSLiam Girdwood 				return ret;
1498f0fba2adSLiam Girdwood 			}
1499f0fba2adSLiam Girdwood 		}
1500f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1501f0fba2adSLiam Girdwood 	}
1502f0fba2adSLiam Girdwood 
1503f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
150488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
150588bd870fSBenoit Cousson 		ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1506b0aa88afSMisael Lopez Cruz 		if (ret)
1507f0fba2adSLiam Girdwood 			return ret;
150888bd870fSBenoit Cousson 	}
1509f0fba2adSLiam Girdwood 
15100168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
15110168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
15120168bf0dSLiam Girdwood 		return 0;
15130168bf0dSLiam Girdwood 
15145f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15155f3484acSLars-Peter Clausen 	if (dai_link->init) {
15165f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15175f3484acSLars-Peter Clausen 		if (ret < 0) {
15185f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15195f3484acSLars-Peter Clausen 				dai_link->name, ret);
15205f3484acSLars-Peter Clausen 			return ret;
15215f3484acSLars-Peter Clausen 		}
15225f3484acSLars-Peter Clausen 	}
15235f3484acSLars-Peter Clausen 
15245f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1525589c3563SJarkko Nikula 	if (ret)
1526f0fba2adSLiam Girdwood 		return ret;
1527f0fba2adSLiam Girdwood 
15285f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
15295f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15305f3484acSLars-Peter Clausen 	if (dai_link->dynamic) {
15315f3484acSLars-Peter Clausen 		ret = soc_dpcm_debugfs_add(rtd);
15325f3484acSLars-Peter Clausen 		if (ret < 0) {
15335f3484acSLars-Peter Clausen 			dev_err(rtd->dev,
15345f3484acSLars-Peter Clausen 				"ASoC: failed to add dpcm sysfs entries: %d\n",
15355f3484acSLars-Peter Clausen 				ret);
15365f3484acSLars-Peter Clausen 			return ret;
15375f3484acSLars-Peter Clausen 		}
15385f3484acSLars-Peter Clausen 	}
15395f3484acSLars-Peter Clausen #endif
15405f3484acSLars-Peter Clausen 
154136ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1542f0fba2adSLiam Girdwood 	if (ret < 0)
1543f110bfc7SLiam Girdwood 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1544f110bfc7SLiam Girdwood 			ret);
1545f0fba2adSLiam Girdwood 
15461245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
15471245b700SNamarta Kohli 		/*create compress_device"*/
15481245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
15491245b700SNamarta Kohli 		if (ret < 0) {
1550f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15511245b700SNamarta Kohli 					 dai_link->stream_name);
15521245b700SNamarta Kohli 			return ret;
15531245b700SNamarta Kohli 		}
15541245b700SNamarta Kohli 	} else {
15551245b700SNamarta Kohli 
1556c74184edSMark Brown 		if (!dai_link->params) {
1557f0fba2adSLiam Girdwood 			/* create the pcm */
1558f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1559f0fba2adSLiam Girdwood 			if (ret < 0) {
1560f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15610837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1562f0fba2adSLiam Girdwood 				return ret;
1563f0fba2adSLiam Girdwood 			}
1564c74184edSMark Brown 		} else {
15659d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
15669d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
15679d58a077SRichard Fitzgerald 
1568c74184edSMark Brown 			/* link the DAI widgets */
15693f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
15702436a723SMisael Lopez Cruz 			if (ret)
1571c74184edSMark Brown 				return ret;
1572c74184edSMark Brown 		}
1573c74184edSMark Brown 	}
1574c74184edSMark Brown 
1575f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
157688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
157788bd870fSBenoit Cousson 		if (rtd->codec_dais[i]->driver->ac97_control)
157888bd870fSBenoit Cousson 			snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
157988bd870fSBenoit Cousson 					       rtd->cpu_dai->ac97_pdata);
158088bd870fSBenoit Cousson 	}
1581f0fba2adSLiam Girdwood 
1582f0fba2adSLiam Girdwood 	return 0;
1583f0fba2adSLiam Girdwood }
1584f0fba2adSLiam Girdwood 
1585f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
158602c9c7b9SMisael Lopez Cruz static int soc_register_ac97_codec(struct snd_soc_codec *codec,
158702c9c7b9SMisael Lopez Cruz 				   struct snd_soc_dai *codec_dai)
1588f0fba2adSLiam Girdwood {
1589f0fba2adSLiam Girdwood 	int ret;
1590f0fba2adSLiam Girdwood 
1591f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1592f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
15936b05eda6SMark Brown 	 */
159402c9c7b9SMisael Lopez Cruz 	if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
15950562f788SMika Westerberg 		/*
15960562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
15970562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
15980562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
15990562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
16000562f788SMika Westerberg 		 *
16010562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
16020562f788SMika Westerberg 		 */
160302c9c7b9SMisael Lopez Cruz 		if (!codec->ac97_created)
16040562f788SMika Westerberg 			return 0;
1605f0fba2adSLiam Girdwood 
160602c9c7b9SMisael Lopez Cruz 		ret = soc_ac97_dev_register(codec);
1607f0fba2adSLiam Girdwood 		if (ret < 0) {
160802c9c7b9SMisael Lopez Cruz 			dev_err(codec->dev,
1609f110bfc7SLiam Girdwood 				"ASoC: AC97 device register failed: %d\n", ret);
1610f0fba2adSLiam Girdwood 			return ret;
1611435c5e25SMark Brown 		}
1612435c5e25SMark Brown 
161302c9c7b9SMisael Lopez Cruz 		codec->ac97_registered = 1;
1614f0fba2adSLiam Girdwood 	}
1615f0fba2adSLiam Girdwood 	return 0;
1616f0fba2adSLiam Girdwood }
1617435c5e25SMark Brown 
161802c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1619f0fba2adSLiam Girdwood {
1620f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1621f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1622f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1623f0fba2adSLiam Girdwood 	}
1624f0fba2adSLiam Girdwood }
162502c9c7b9SMisael Lopez Cruz 
162688bd870fSBenoit Cousson static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
162788bd870fSBenoit Cousson {
162888bd870fSBenoit Cousson 	int i, ret;
162988bd870fSBenoit Cousson 
163088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
163188bd870fSBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
163288bd870fSBenoit Cousson 
163388bd870fSBenoit Cousson 		ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
163488bd870fSBenoit Cousson 		if (ret) {
163588bd870fSBenoit Cousson 			while (--i >= 0)
163688bd870fSBenoit Cousson 				soc_unregister_ac97_codec(codec_dai->codec);
163788bd870fSBenoit Cousson 			return ret;
163888bd870fSBenoit Cousson 		}
163988bd870fSBenoit Cousson 	}
164088bd870fSBenoit Cousson 
164188bd870fSBenoit Cousson 	return 0;
164288bd870fSBenoit Cousson }
164388bd870fSBenoit Cousson 
164402c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
164502c9c7b9SMisael Lopez Cruz {
164688bd870fSBenoit Cousson 	int i;
164788bd870fSBenoit Cousson 
164888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
164988bd870fSBenoit Cousson 		soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
165002c9c7b9SMisael Lopez Cruz }
1651f0fba2adSLiam Girdwood #endif
1652435c5e25SMark Brown 
165344c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1654b19e6e7bSMark Brown {
165544c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
16563ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
16573ca041edSSebastian Reichel 	const char *codecname = aux_dev->codec_name;
16583ca041edSSebastian Reichel 
165944c69bb1SLars-Peter Clausen 	rtd->codec = soc_find_codec(aux_dev->codec_of_node, codecname);
166044c69bb1SLars-Peter Clausen 	if (!rtd->codec) {
16613ca041edSSebastian Reichel 		if (aux_dev->codec_of_node)
16623ca041edSSebastian Reichel 			codecname = of_node_full_name(aux_dev->codec_of_node);
16633ca041edSSebastian Reichel 
16643ca041edSSebastian Reichel 		dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1665b19e6e7bSMark Brown 		return -EPROBE_DEFER;
1666b19e6e7bSMark Brown 	}
1667b19e6e7bSMark Brown 
166844c69bb1SLars-Peter Clausen 	return 0;
16693ca041edSSebastian Reichel }
16702eea392dSJarkko Nikula 
167144c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
167244c69bb1SLars-Peter Clausen {
167344c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
16745f3484acSLars-Peter Clausen 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
167544c69bb1SLars-Peter Clausen 	int ret;
167644c69bb1SLars-Peter Clausen 
167744c69bb1SLars-Peter Clausen 	if (rtd->codec->probed) {
167844c69bb1SLars-Peter Clausen 		dev_err(rtd->codec->dev, "ASoC: codec already probed\n");
16793ca041edSSebastian Reichel 		return -EBUSY;
16803ca041edSSebastian Reichel 	}
16813ca041edSSebastian Reichel 
168244c69bb1SLars-Peter Clausen 	ret = soc_probe_codec(card, rtd->codec);
16832eea392dSJarkko Nikula 	if (ret < 0)
1684589c3563SJarkko Nikula 		return ret;
16852eea392dSJarkko Nikula 
16865f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16875f3484acSLars-Peter Clausen 	if (aux_dev->init) {
16885f3484acSLars-Peter Clausen 		ret = aux_dev->init(&rtd->codec->dapm);
16895f3484acSLars-Peter Clausen 		if (ret < 0) {
16905f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16915f3484acSLars-Peter Clausen 				aux_dev->name, ret);
16922eea392dSJarkko Nikula 			return ret;
16932eea392dSJarkko Nikula 		}
16945f3484acSLars-Peter Clausen 	}
16955f3484acSLars-Peter Clausen 
16965f3484acSLars-Peter Clausen 	return soc_post_component_init(rtd, aux_dev->name);
16972eea392dSJarkko Nikula }
16982eea392dSJarkko Nikula 
16992eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
17002eea392dSJarkko Nikula {
17012eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
17022eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
17032eea392dSJarkko Nikula 
17042eea392dSJarkko Nikula 	/* unregister the rtd device */
17052eea392dSJarkko Nikula 	if (rtd->dev_registered) {
170636ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1707d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
17082eea392dSJarkko Nikula 		rtd->dev_registered = 0;
17092eea392dSJarkko Nikula 	}
17102eea392dSJarkko Nikula 
1711589c3563SJarkko Nikula 	if (codec && codec->probed)
1712589c3563SJarkko Nikula 		soc_remove_codec(codec);
17132eea392dSJarkko Nikula }
17142eea392dSJarkko Nikula 
1715f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1716fdf0f54dSDimitris Papastamos {
1717fdf0f54dSDimitris Papastamos 	int ret;
1718fdf0f54dSDimitris Papastamos 
1719fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1720fdf0f54dSDimitris Papastamos 		return 0;
1721fdf0f54dSDimitris Papastamos 
1722fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1723fdf0f54dSDimitris Papastamos 	if (ret < 0) {
172410e8aa9aSMichał Mirosław 		dev_err(codec->dev,
172510e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
172610e8aa9aSMichał Mirosław 			ret);
1727fdf0f54dSDimitris Papastamos 		return ret;
1728fdf0f54dSDimitris Papastamos 	}
1729fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1730fdf0f54dSDimitris Papastamos 	return 0;
1731fdf0f54dSDimitris Papastamos }
1732fdf0f54dSDimitris Papastamos 
1733b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1734f0fba2adSLiam Girdwood {
1735fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
173675d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1737f04209a7SMark Brown 	int ret, i, order, dai_fmt;
173896dd3622SMark Brown 
173901b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1740f0fba2adSLiam Girdwood 
1741b19e6e7bSMark Brown 	/* bind DAIs */
1742b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1743b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1744b19e6e7bSMark Brown 		if (ret != 0)
1745b19e6e7bSMark Brown 			goto base_error;
1746db2a4165SFrank Mandarino 	}
1747db2a4165SFrank Mandarino 
174844c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1749b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
175044c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1751b19e6e7bSMark Brown 		if (ret != 0)
1752b19e6e7bSMark Brown 			goto base_error;
1753db2a4165SFrank Mandarino 	}
1754db2a4165SFrank Mandarino 
1755fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1756fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1757fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1758fdf0f54dSDimitris Papastamos 			continue;
1759f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1760b19e6e7bSMark Brown 		if (ret < 0)
1761b19e6e7bSMark Brown 			goto base_error;
1762fdf0f54dSDimitris Papastamos 	}
1763fdf0f54dSDimitris Papastamos 
1764f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1765102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1766f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1767f0fba2adSLiam Girdwood 	if (ret < 0) {
176810e8aa9aSMichał Mirosław 		dev_err(card->dev,
176910e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
177010e8aa9aSMichał Mirosław 			card->name, ret);
1771b19e6e7bSMark Brown 		goto base_error;
1772db2a4165SFrank Mandarino 	}
1773db2a4165SFrank Mandarino 
1774e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1775e37a4970SMark Brown 	card->dapm.dev = card->dev;
1776e37a4970SMark Brown 	card->dapm.card = card;
1777e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1778e37a4970SMark Brown 
1779d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1780d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1781d5d1e0beSLars-Peter Clausen #endif
1782d5d1e0beSLars-Peter Clausen 
178388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
17846ed25978SAndy Green 	/* deferred resume work */
17856308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
17861301a964SRandy Dunlap #endif
17876ed25978SAndy Green 
17889a841ebbSMark Brown 	if (card->dapm_widgets)
17899a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
17909a841ebbSMark Brown 					  card->num_dapm_widgets);
17919a841ebbSMark Brown 
1792f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1793f0fba2adSLiam Girdwood 	if (card->probe) {
1794e7361ec4SMark Brown 		ret = card->probe(card);
1795f0fba2adSLiam Girdwood 		if (ret < 0)
1796f0fba2adSLiam Girdwood 			goto card_probe_error;
1797f0fba2adSLiam Girdwood 	}
1798f0fba2adSLiam Girdwood 
179962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
18000168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
18010168bf0dSLiam Girdwood 			order++) {
1802fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
180362ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
180462ae68faSStephen Warren 			if (ret < 0) {
1805f110bfc7SLiam Girdwood 				dev_err(card->dev,
1806f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1807f110bfc7SLiam Girdwood 					ret);
180862ae68faSStephen Warren 				goto probe_dai_err;
180962ae68faSStephen Warren 			}
181062ae68faSStephen Warren 		}
181162ae68faSStephen Warren 	}
181262ae68faSStephen Warren 
181362ae68faSStephen Warren 	/* probe all DAI links on this card */
181462ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
181562ae68faSStephen Warren 			order++) {
181662ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
181762ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1818fe3e78e0SMark Brown 			if (ret < 0) {
1819f110bfc7SLiam Girdwood 				dev_err(card->dev,
1820f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1821f110bfc7SLiam Girdwood 					ret);
1822f0fba2adSLiam Girdwood 				goto probe_dai_err;
1823fe3e78e0SMark Brown 			}
1824fe3e78e0SMark Brown 		}
18250168bf0dSLiam Girdwood 	}
1826fe3e78e0SMark Brown 
18272eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
18282eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
18292eea392dSJarkko Nikula 		if (ret < 0) {
1830f110bfc7SLiam Girdwood 			dev_err(card->dev,
1831f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1832f110bfc7SLiam Girdwood 				ret);
18332eea392dSJarkko Nikula 			goto probe_aux_dev_err;
18342eea392dSJarkko Nikula 		}
18352eea392dSJarkko Nikula 	}
18362eea392dSJarkko Nikula 
1837888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1838b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1839888df395SMark Brown 
1840b7af1dafSMark Brown 	if (card->controls)
1841022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1842b7af1dafSMark Brown 
1843b8ad29deSMark Brown 	if (card->dapm_routes)
1844b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1845b8ad29deSMark Brown 					card->num_dapm_routes);
1846b8ad29deSMark Brown 
184775d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
184888bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
184975d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1850f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
185175d9ac46SMark Brown 
1852f04209a7SMark Brown 		if (dai_fmt) {
185388bd870fSBenoit Cousson 			struct snd_soc_dai **codec_dais = rtd->codec_dais;
185488bd870fSBenoit Cousson 			int j;
185588bd870fSBenoit Cousson 
185688bd870fSBenoit Cousson 			for (j = 0; j < rtd->num_codecs; j++) {
185788bd870fSBenoit Cousson 				struct snd_soc_dai *codec_dai = codec_dais[j];
185888bd870fSBenoit Cousson 
185988bd870fSBenoit Cousson 				ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
18605e4ba569SShawn Guo 				if (ret != 0 && ret != -ENOTSUPP)
186188bd870fSBenoit Cousson 					dev_warn(codec_dai->dev,
1862f110bfc7SLiam Girdwood 						 "ASoC: Failed to set DAI format: %d\n",
186375d9ac46SMark Brown 						 ret);
1864f04209a7SMark Brown 			}
186588bd870fSBenoit Cousson 		}
1866f04209a7SMark Brown 
1867f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1868fe33d4c5SStephen Warren 		if (dai_fmt &&
1869fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1870f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1871f04209a7SMark Brown 						  dai_fmt);
1872f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1873f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1874f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
1875f04209a7SMark Brown 					 ret);
1876f04209a7SMark Brown 		} else if (dai_fmt) {
1877f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1878f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1879f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1880f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1881f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1882f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1883f04209a7SMark Brown 				break;
1884f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1885f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1886f04209a7SMark Brown 				break;
1887f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1888f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1889f04209a7SMark Brown 				break;
1890f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1891f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1892f04209a7SMark Brown 				break;
1893f04209a7SMark Brown 			}
189475d9ac46SMark Brown 
189575d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1896f04209a7SMark Brown 						  dai_fmt);
18975e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
189875d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1899f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
190075d9ac46SMark Brown 					 ret);
190175d9ac46SMark Brown 		}
190275d9ac46SMark Brown 	}
190375d9ac46SMark Brown 
1904f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1905fe3e78e0SMark Brown 		 "%s", card->name);
1906f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
190722de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1908f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1909f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1910f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1911f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1912f0e8ed85SMark Brown 		case '_':
1913f0e8ed85SMark Brown 		case '-':
1914f0e8ed85SMark Brown 		case '\0':
1915f0e8ed85SMark Brown 			break;
1916f0e8ed85SMark Brown 		default:
1917f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1918f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1919f0e8ed85SMark Brown 			break;
1920f0e8ed85SMark Brown 		}
1921f0e8ed85SMark Brown 	}
1922fe3e78e0SMark Brown 
192328e9ad92SMark Brown 	if (card->late_probe) {
192428e9ad92SMark Brown 		ret = card->late_probe(card);
192528e9ad92SMark Brown 		if (ret < 0) {
1926f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
192728e9ad92SMark Brown 				card->name, ret);
192828e9ad92SMark Brown 			goto probe_aux_dev_err;
192928e9ad92SMark Brown 		}
193028e9ad92SMark Brown 	}
193128e9ad92SMark Brown 
19321633281bSStephen Warren 	if (card->fully_routed)
19337df37884SLars-Peter Clausen 		snd_soc_dapm_auto_nc_pins(card);
19341633281bSStephen Warren 
1935824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
19368c193b8dSLars-Peter Clausen 
1937f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1938fe3e78e0SMark Brown 	if (ret < 0) {
1939f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1940f110bfc7SLiam Girdwood 				ret);
19416b3ed785SAxel Lin 		goto probe_aux_dev_err;
1942fe3e78e0SMark Brown 	}
1943fe3e78e0SMark Brown 
1944fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1945f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1946f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1947f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1948fe3e78e0SMark Brown 		if (ret < 0) {
194910e8aa9aSMichał Mirosław 			dev_err(card->dev,
195010e8aa9aSMichał Mirosław 				"ASoC: failed to register AC97: %d\n", ret);
19516b3ed785SAxel Lin 			while (--i >= 0)
195202c9c7b9SMisael Lopez Cruz 				soc_unregister_ac97_dai_link(&card->rtd[i]);
19536b3ed785SAxel Lin 			goto probe_aux_dev_err;
1954fe3e78e0SMark Brown 		}
1955fe3e78e0SMark Brown 	}
1956fe3e78e0SMark Brown #endif
1957fe3e78e0SMark Brown 
1958435c5e25SMark Brown 	card->instantiated = 1;
19594f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1960f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1961b19e6e7bSMark Brown 
1962b19e6e7bSMark Brown 	return 0;
1963db2a4165SFrank Mandarino 
19642eea392dSJarkko Nikula probe_aux_dev_err:
19652eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
19662eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
19672eea392dSJarkko Nikula 
1968f0fba2adSLiam Girdwood probe_dai_err:
19690671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1970fe3e78e0SMark Brown 
1971f0fba2adSLiam Girdwood card_probe_error:
197287506549SMark Brown 	if (card->remove)
1973e7361ec4SMark Brown 		card->remove(card);
1974f0fba2adSLiam Girdwood 
1975f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1976f0fba2adSLiam Girdwood 
1977b19e6e7bSMark Brown base_error:
1978f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1979db2a4165SFrank Mandarino 
1980b19e6e7bSMark Brown 	return ret;
1981435c5e25SMark Brown }
1982435c5e25SMark Brown 
1983435c5e25SMark Brown /* probes a new socdev */
1984435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1985435c5e25SMark Brown {
1986f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1987435c5e25SMark Brown 
198870a7ca34SVinod Koul 	/*
198970a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
199070a7ca34SVinod Koul 	 * we should not be here in that case so ret error
199170a7ca34SVinod Koul 	 */
199270a7ca34SVinod Koul 	if (!card)
199370a7ca34SVinod Koul 		return -EINVAL;
199470a7ca34SVinod Koul 
1995fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1996f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1997fe4085e8SMark Brown 		 card->name);
1998fe4085e8SMark Brown 
1999435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2000435c5e25SMark Brown 	card->dev = &pdev->dev;
2001f0fba2adSLiam Girdwood 
200228d528c8SMark Brown 	return snd_soc_register_card(card);
2003435c5e25SMark Brown }
2004435c5e25SMark Brown 
2005b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2006db2a4165SFrank Mandarino {
2007db2a4165SFrank Mandarino 	int i;
2008db2a4165SFrank Mandarino 
2009f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
2010f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2011f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
201243829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2013db2a4165SFrank Mandarino 	}
2014db2a4165SFrank Mandarino 
20152eea392dSJarkko Nikula 	/* remove auxiliary devices */
20162eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
20172eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
20182eea392dSJarkko Nikula 
2019f0fba2adSLiam Girdwood 	/* remove and free each DAI */
20200671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2021f0fba2adSLiam Girdwood 
2022a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2023a6052154SJarkko Nikula 
2024f0fba2adSLiam Girdwood 	/* remove the card */
202587506549SMark Brown 	if (card->remove)
2026e7361ec4SMark Brown 		card->remove(card);
2027f0fba2adSLiam Girdwood 
20280aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
20290aaae527SLars-Peter Clausen 
2030f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2031b0e26485SVinod Koul 	return 0;
2032b0e26485SVinod Koul 
2033b2dfa62cSGuennadi Liakhovetski }
2034b0e26485SVinod Koul 
2035b0e26485SVinod Koul /* removes a socdev */
2036b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2037b0e26485SVinod Koul {
2038b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2039b0e26485SVinod Koul 
2040c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2041db2a4165SFrank Mandarino 	return 0;
2042db2a4165SFrank Mandarino }
2043db2a4165SFrank Mandarino 
20446f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
204551737470SMark Brown {
20466f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
2047f0fba2adSLiam Girdwood 	int i;
204851737470SMark Brown 
204951737470SMark Brown 	if (!card->instantiated)
2050416356fcSMark Brown 		return 0;
205151737470SMark Brown 
205251737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
205351737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
2054f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2055f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
205643829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2057f0fba2adSLiam Girdwood 	}
205851737470SMark Brown 
2059f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2060416356fcSMark Brown 
2061988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2062988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
206388bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
206488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
206588bd870fSBenoit Cousson 		int j;
206688bd870fSBenoit Cousson 
2067988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
206888bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
206988bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
207088bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
207188bd870fSBenoit Cousson 		}
2072988e8cc4SNicolin Chen 	}
2073988e8cc4SNicolin Chen 
2074416356fcSMark Brown 	return 0;
207551737470SMark Brown }
20766f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
207751737470SMark Brown 
20786f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2079b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2080b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2081b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2082b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
20836f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2084b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2085416356fcSMark Brown };
2086deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2087416356fcSMark Brown 
2088db2a4165SFrank Mandarino /* ASoC platform driver */
2089db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2090db2a4165SFrank Mandarino 	.driver		= {
2091db2a4165SFrank Mandarino 		.name		= "soc-audio",
20928b45a209SKay Sievers 		.owner		= THIS_MODULE,
20936f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2094db2a4165SFrank Mandarino 	},
2095db2a4165SFrank Mandarino 	.probe		= soc_probe,
2096db2a4165SFrank Mandarino 	.remove		= soc_remove,
2097db2a4165SFrank Mandarino };
2098db2a4165SFrank Mandarino 
2099096e49d5SMark Brown /**
2100db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2101db2a4165SFrank Mandarino  * @codec: audio codec
2102db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2103db2a4165SFrank Mandarino  * @num: AC97 codec number
2104db2a4165SFrank Mandarino  *
2105db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2106db2a4165SFrank Mandarino  */
2107db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2108db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2109db2a4165SFrank Mandarino {
2110db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2111e3f205a7SLars-Peter Clausen 	if (codec->ac97 == NULL)
2112db2a4165SFrank Mandarino 		return -ENOMEM;
2113db2a4165SFrank Mandarino 
2114db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2115db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2116db2a4165SFrank Mandarino 		kfree(codec->ac97);
2117db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2118db2a4165SFrank Mandarino 		return -ENOMEM;
2119db2a4165SFrank Mandarino 	}
2120db2a4165SFrank Mandarino 
2121db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2122db2a4165SFrank Mandarino 	codec->ac97->num = num;
21230562f788SMika Westerberg 
21240562f788SMika Westerberg 	/*
21250562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
21260562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
21270562f788SMika Westerberg 	 */
21280562f788SMika Westerberg 	codec->ac97_created = 1;
21290562f788SMika Westerberg 
2130db2a4165SFrank Mandarino 	return 0;
2131db2a4165SFrank Mandarino }
2132db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2133db2a4165SFrank Mandarino 
2134741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2135741a509fSMarkus Pargmann 
2136741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2137741a509fSMarkus Pargmann {
2138741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2139741a509fSMarkus Pargmann 
2140741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2141741a509fSMarkus Pargmann 
2142741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2143741a509fSMarkus Pargmann 
2144741a509fSMarkus Pargmann 	udelay(10);
2145741a509fSMarkus Pargmann 
2146741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2147741a509fSMarkus Pargmann 
2148741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2149741a509fSMarkus Pargmann 	msleep(2);
2150741a509fSMarkus Pargmann }
2151741a509fSMarkus Pargmann 
2152741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2153741a509fSMarkus Pargmann {
2154741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2155741a509fSMarkus Pargmann 
2156741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2157741a509fSMarkus Pargmann 
2158741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2159741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2160741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2161741a509fSMarkus Pargmann 
2162741a509fSMarkus Pargmann 	udelay(10);
2163741a509fSMarkus Pargmann 
2164741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2165741a509fSMarkus Pargmann 
2166741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2167741a509fSMarkus Pargmann 	msleep(2);
2168741a509fSMarkus Pargmann }
2169741a509fSMarkus Pargmann 
2170741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev,
2171741a509fSMarkus Pargmann 		struct snd_ac97_reset_cfg *cfg)
2172741a509fSMarkus Pargmann {
2173741a509fSMarkus Pargmann 	struct pinctrl *p;
2174741a509fSMarkus Pargmann 	struct pinctrl_state *state;
2175741a509fSMarkus Pargmann 	int gpio;
2176741a509fSMarkus Pargmann 	int ret;
2177741a509fSMarkus Pargmann 
2178741a509fSMarkus Pargmann 	p = devm_pinctrl_get(dev);
2179741a509fSMarkus Pargmann 	if (IS_ERR(p)) {
2180741a509fSMarkus Pargmann 		dev_err(dev, "Failed to get pinctrl\n");
2181b7580cdeSChristoph Jaeger 		return PTR_ERR(p);
2182741a509fSMarkus Pargmann 	}
2183741a509fSMarkus Pargmann 	cfg->pctl = p;
2184741a509fSMarkus Pargmann 
2185741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-reset");
2186741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2187741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2188b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2189741a509fSMarkus Pargmann 	}
2190741a509fSMarkus Pargmann 	cfg->pstate_reset = state;
2191741a509fSMarkus Pargmann 
2192741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
2193741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2194741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2195b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2196741a509fSMarkus Pargmann 	}
2197741a509fSMarkus Pargmann 	cfg->pstate_warm_reset = state;
2198741a509fSMarkus Pargmann 
2199741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-running");
2200741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2201741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
2202b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2203741a509fSMarkus Pargmann 	}
2204741a509fSMarkus Pargmann 	cfg->pstate_run = state;
2205741a509fSMarkus Pargmann 
2206741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2207741a509fSMarkus Pargmann 	if (gpio < 0) {
2208741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sync gpio\n");
2209741a509fSMarkus Pargmann 		return gpio;
2210741a509fSMarkus Pargmann 	}
2211741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2212741a509fSMarkus Pargmann 	if (ret) {
2213741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
2214741a509fSMarkus Pargmann 		return ret;
2215741a509fSMarkus Pargmann 	}
2216741a509fSMarkus Pargmann 	cfg->gpio_sync = gpio;
2217741a509fSMarkus Pargmann 
2218741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2219741a509fSMarkus Pargmann 	if (gpio < 0) {
2220741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2221741a509fSMarkus Pargmann 		return gpio;
2222741a509fSMarkus Pargmann 	}
2223741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2224741a509fSMarkus Pargmann 	if (ret) {
2225741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2226741a509fSMarkus Pargmann 		return ret;
2227741a509fSMarkus Pargmann 	}
2228741a509fSMarkus Pargmann 	cfg->gpio_sdata = gpio;
2229741a509fSMarkus Pargmann 
2230741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2231741a509fSMarkus Pargmann 	if (gpio < 0) {
2232741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-reset gpio\n");
2233741a509fSMarkus Pargmann 		return gpio;
2234741a509fSMarkus Pargmann 	}
2235741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2236741a509fSMarkus Pargmann 	if (ret) {
2237741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
2238741a509fSMarkus Pargmann 		return ret;
2239741a509fSMarkus Pargmann 	}
2240741a509fSMarkus Pargmann 	cfg->gpio_reset = gpio;
2241741a509fSMarkus Pargmann 
2242741a509fSMarkus Pargmann 	return 0;
2243741a509fSMarkus Pargmann }
2244741a509fSMarkus Pargmann 
2245b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops;
2246f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops);
2247b047e1ccSMark Brown 
2248b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2249b047e1ccSMark Brown {
2250b047e1ccSMark Brown 	if (ops == soc_ac97_ops)
2251b047e1ccSMark Brown 		return 0;
2252b047e1ccSMark Brown 
2253b047e1ccSMark Brown 	if (soc_ac97_ops && ops)
2254b047e1ccSMark Brown 		return -EBUSY;
2255b047e1ccSMark Brown 
2256b047e1ccSMark Brown 	soc_ac97_ops = ops;
2257b047e1ccSMark Brown 
2258b047e1ccSMark Brown 	return 0;
2259b047e1ccSMark Brown }
2260b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2261b047e1ccSMark Brown 
2262db2a4165SFrank Mandarino /**
2263741a509fSMarkus Pargmann  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2264741a509fSMarkus Pargmann  *
2265741a509fSMarkus Pargmann  * This function sets the reset and warm_reset properties of ops and parses
2266741a509fSMarkus Pargmann  * the device node of pdev to get pinctrl states and gpio numbers to use.
2267741a509fSMarkus Pargmann  */
2268741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2269741a509fSMarkus Pargmann 		struct platform_device *pdev)
2270741a509fSMarkus Pargmann {
2271741a509fSMarkus Pargmann 	struct device *dev = &pdev->dev;
2272741a509fSMarkus Pargmann 	struct snd_ac97_reset_cfg cfg;
2273741a509fSMarkus Pargmann 	int ret;
2274741a509fSMarkus Pargmann 
2275741a509fSMarkus Pargmann 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2276741a509fSMarkus Pargmann 	if (ret)
2277741a509fSMarkus Pargmann 		return ret;
2278741a509fSMarkus Pargmann 
2279741a509fSMarkus Pargmann 	ret = snd_soc_set_ac97_ops(ops);
2280741a509fSMarkus Pargmann 	if (ret)
2281741a509fSMarkus Pargmann 		return ret;
2282741a509fSMarkus Pargmann 
2283741a509fSMarkus Pargmann 	ops->warm_reset = snd_soc_ac97_warm_reset;
2284741a509fSMarkus Pargmann 	ops->reset = snd_soc_ac97_reset;
2285741a509fSMarkus Pargmann 
2286741a509fSMarkus Pargmann 	snd_ac97_rst_cfg = cfg;
2287741a509fSMarkus Pargmann 	return 0;
2288741a509fSMarkus Pargmann }
2289741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2290741a509fSMarkus Pargmann 
2291741a509fSMarkus Pargmann /**
2292db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2293db2a4165SFrank Mandarino  * @codec: audio codec
2294db2a4165SFrank Mandarino  *
2295db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2296db2a4165SFrank Mandarino  */
2297db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2298db2a4165SFrank Mandarino {
2299f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
230002c9c7b9SMisael Lopez Cruz 	soc_unregister_ac97_codec(codec);
2301f0fba2adSLiam Girdwood #endif
2302db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2303db2a4165SFrank Mandarino 	kfree(codec->ac97);
2304db2a4165SFrank Mandarino 	codec->ac97 = NULL;
23050562f788SMika Westerberg 	codec->ac97_created = 0;
2306db2a4165SFrank Mandarino }
2307db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2308db2a4165SFrank Mandarino 
2309db2a4165SFrank Mandarino /**
2310db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2311db2a4165SFrank Mandarino  * @_template: control template
2312db2a4165SFrank Mandarino  * @data: control private data
2313ac11a2b3SMark Brown  * @long_name: control long name
2314efb7ac3fSMark Brown  * @prefix: control name prefix
2315db2a4165SFrank Mandarino  *
2316db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2317db2a4165SFrank Mandarino  *
2318db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2319db2a4165SFrank Mandarino  */
2320db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
23213056557fSMark Brown 				  void *data, const char *long_name,
2322efb7ac3fSMark Brown 				  const char *prefix)
2323db2a4165SFrank Mandarino {
2324db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2325efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2326efb7ac3fSMark Brown 	char *name = NULL;
2327db2a4165SFrank Mandarino 
2328db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2329db2a4165SFrank Mandarino 	template.index = 0;
2330db2a4165SFrank Mandarino 
2331efb7ac3fSMark Brown 	if (!long_name)
2332efb7ac3fSMark Brown 		long_name = template.name;
2333efb7ac3fSMark Brown 
2334efb7ac3fSMark Brown 	if (prefix) {
23352b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2336efb7ac3fSMark Brown 		if (!name)
2337efb7ac3fSMark Brown 			return NULL;
2338efb7ac3fSMark Brown 
2339efb7ac3fSMark Brown 		template.name = name;
2340efb7ac3fSMark Brown 	} else {
2341efb7ac3fSMark Brown 		template.name = long_name;
2342efb7ac3fSMark Brown 	}
2343efb7ac3fSMark Brown 
2344efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2345efb7ac3fSMark Brown 
2346efb7ac3fSMark Brown 	kfree(name);
2347efb7ac3fSMark Brown 
2348efb7ac3fSMark Brown 	return kcontrol;
2349db2a4165SFrank Mandarino }
2350db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2351db2a4165SFrank Mandarino 
2352022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2353022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2354022658beSLiam Girdwood 	const char *prefix, void *data)
2355022658beSLiam Girdwood {
2356022658beSLiam Girdwood 	int err, i;
2357022658beSLiam Girdwood 
2358022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2359022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2360022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2361022658beSLiam Girdwood 						     control->name, prefix));
2362022658beSLiam Girdwood 		if (err < 0) {
2363f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2364f110bfc7SLiam Girdwood 				control->name, err);
2365022658beSLiam Girdwood 			return err;
2366022658beSLiam Girdwood 		}
2367022658beSLiam Girdwood 	}
2368022658beSLiam Girdwood 
2369022658beSLiam Girdwood 	return 0;
2370022658beSLiam Girdwood }
2371022658beSLiam Girdwood 
23724fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23734fefd698SDimitris Papastamos 					       const char *name)
23744fefd698SDimitris Papastamos {
23754fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23764fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23774fefd698SDimitris Papastamos 
23784fefd698SDimitris Papastamos 	if (unlikely(!name))
23794fefd698SDimitris Papastamos 		return NULL;
23804fefd698SDimitris Papastamos 
23814fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23824fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23834fefd698SDimitris Papastamos 			return kctl;
23844fefd698SDimitris Papastamos 	return NULL;
23854fefd698SDimitris Papastamos }
23864fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23874fefd698SDimitris Papastamos 
2388db2a4165SFrank Mandarino /**
23890f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23900f2780adSLars-Peter Clausen  *
23910f2780adSLars-Peter Clausen  * @component: Component to add controls to
23920f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23930f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23940f2780adSLars-Peter Clausen  *
23950f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23960f2780adSLars-Peter Clausen  */
23970f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23980f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23990f2780adSLars-Peter Clausen {
24000f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
24010f2780adSLars-Peter Clausen 
24020f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
24030f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
24040f2780adSLars-Peter Clausen }
24050f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
24060f2780adSLars-Peter Clausen 
24070f2780adSLars-Peter Clausen /**
2408022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2409022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
24103e8e1952SIan Molton  * duplicating this code.
24113e8e1952SIan Molton  *
24123e8e1952SIan Molton  * @codec: codec to add controls to
24133e8e1952SIan Molton  * @controls: array of controls to add
24143e8e1952SIan Molton  * @num_controls: number of elements in the array
24153e8e1952SIan Molton  *
24163e8e1952SIan Molton  * Return 0 for success, else error.
24173e8e1952SIan Molton  */
2418022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
24190f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
24203e8e1952SIan Molton {
24210f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
24220f2780adSLars-Peter Clausen 		num_controls);
24233e8e1952SIan Molton }
2424022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
24253e8e1952SIan Molton 
24263e8e1952SIan Molton /**
2427a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2428022658beSLiam Girdwood  * Convenience function to add a list of controls.
2429a491a5c8SLiam Girdwood  *
2430a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2431a491a5c8SLiam Girdwood  * @controls: array of controls to add
2432a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2433a491a5c8SLiam Girdwood  *
2434a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2435a491a5c8SLiam Girdwood  */
2436a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
24370f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2438a491a5c8SLiam Girdwood {
24390f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
24400f2780adSLars-Peter Clausen 		num_controls);
2441a491a5c8SLiam Girdwood }
2442a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2443a491a5c8SLiam Girdwood 
2444a491a5c8SLiam Girdwood /**
2445022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2446022658beSLiam Girdwood  * Convenience function to add a list of controls.
2447022658beSLiam Girdwood  *
2448022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2449022658beSLiam Girdwood  * @controls: array of controls to add
2450022658beSLiam Girdwood  * @num_controls: number of elements in the array
2451022658beSLiam Girdwood  *
2452022658beSLiam Girdwood  * Return 0 for success, else error.
2453022658beSLiam Girdwood  */
2454022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2455022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2456022658beSLiam Girdwood {
2457022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2458022658beSLiam Girdwood 
2459022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2460022658beSLiam Girdwood 			NULL, soc_card);
2461022658beSLiam Girdwood }
2462022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2463022658beSLiam Girdwood 
2464022658beSLiam Girdwood /**
2465022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2466022658beSLiam Girdwood  * Convienience function to add a list of controls.
2467022658beSLiam Girdwood  *
2468022658beSLiam Girdwood  * @dai: DAI to add controls to
2469022658beSLiam Girdwood  * @controls: array of controls to add
2470022658beSLiam Girdwood  * @num_controls: number of elements in the array
2471022658beSLiam Girdwood  *
2472022658beSLiam Girdwood  * Return 0 for success, else error.
2473022658beSLiam Girdwood  */
2474022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2475022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2476022658beSLiam Girdwood {
2477022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2478022658beSLiam Girdwood 
2479022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2480022658beSLiam Girdwood 			NULL, dai);
2481022658beSLiam Girdwood }
2482022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2483022658beSLiam Girdwood 
2484022658beSLiam Girdwood /**
2485db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2486db2a4165SFrank Mandarino  * @kcontrol: mixer control
2487db2a4165SFrank Mandarino  * @uinfo: control element information
2488db2a4165SFrank Mandarino  *
2489db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2490db2a4165SFrank Mandarino  * mixer control.
2491db2a4165SFrank Mandarino  *
2492db2a4165SFrank Mandarino  * Returns 0 for success.
2493db2a4165SFrank Mandarino  */
2494db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2495db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2496db2a4165SFrank Mandarino {
2497db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2498db2a4165SFrank Mandarino 
2499db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2500db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
25019a8d38dbSTakashi Iwai 	uinfo->value.enumerated.items = e->items;
2502db2a4165SFrank Mandarino 
25039a8d38dbSTakashi Iwai 	if (uinfo->value.enumerated.item >= e->items)
25049a8d38dbSTakashi Iwai 		uinfo->value.enumerated.item = e->items - 1;
2505a19685cbSTakashi Iwai 	strlcpy(uinfo->value.enumerated.name,
2506a19685cbSTakashi Iwai 		e->texts[uinfo->value.enumerated.item],
2507a19685cbSTakashi Iwai 		sizeof(uinfo->value.enumerated.name));
2508db2a4165SFrank Mandarino 	return 0;
2509db2a4165SFrank Mandarino }
2510db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2511db2a4165SFrank Mandarino 
2512db2a4165SFrank Mandarino /**
2513db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2514db2a4165SFrank Mandarino  * @kcontrol: mixer control
2515ac11a2b3SMark Brown  * @ucontrol: control element information
2516db2a4165SFrank Mandarino  *
2517db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2518db2a4165SFrank Mandarino  *
2519db2a4165SFrank Mandarino  * Returns 0 for success.
2520db2a4165SFrank Mandarino  */
2521db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2522db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2523db2a4165SFrank Mandarino {
2524907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2525db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
252629ae2fa5SLars-Peter Clausen 	unsigned int val, item;
252729ae2fa5SLars-Peter Clausen 	unsigned int reg_val;
2528907fe36aSLars-Peter Clausen 	int ret;
2529db2a4165SFrank Mandarino 
2530907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, e->reg, &reg_val);
2531907fe36aSLars-Peter Clausen 	if (ret)
2532907fe36aSLars-Peter Clausen 		return ret;
253329ae2fa5SLars-Peter Clausen 	val = (reg_val >> e->shift_l) & e->mask;
253429ae2fa5SLars-Peter Clausen 	item = snd_soc_enum_val_to_item(e, val);
253529ae2fa5SLars-Peter Clausen 	ucontrol->value.enumerated.item[0] = item;
253629ae2fa5SLars-Peter Clausen 	if (e->shift_l != e->shift_r) {
253729ae2fa5SLars-Peter Clausen 		val = (reg_val >> e->shift_l) & e->mask;
253829ae2fa5SLars-Peter Clausen 		item = snd_soc_enum_val_to_item(e, val);
253929ae2fa5SLars-Peter Clausen 		ucontrol->value.enumerated.item[1] = item;
254029ae2fa5SLars-Peter Clausen 	}
2541db2a4165SFrank Mandarino 
2542db2a4165SFrank Mandarino 	return 0;
2543db2a4165SFrank Mandarino }
2544db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2545db2a4165SFrank Mandarino 
2546db2a4165SFrank Mandarino /**
2547db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2548db2a4165SFrank Mandarino  * @kcontrol: mixer control
2549ac11a2b3SMark Brown  * @ucontrol: control element information
2550db2a4165SFrank Mandarino  *
2551db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2552db2a4165SFrank Mandarino  *
2553db2a4165SFrank Mandarino  * Returns 0 for success.
2554db2a4165SFrank Mandarino  */
2555db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2556db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2557db2a4165SFrank Mandarino {
2558907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2559db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
256029ae2fa5SLars-Peter Clausen 	unsigned int *item = ucontrol->value.enumerated.item;
256146f5822fSDaniel Ribeiro 	unsigned int val;
256286767b7dSLars-Peter Clausen 	unsigned int mask;
2563db2a4165SFrank Mandarino 
256429ae2fa5SLars-Peter Clausen 	if (item[0] >= e->items)
2565db2a4165SFrank Mandarino 		return -EINVAL;
256629ae2fa5SLars-Peter Clausen 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
256786767b7dSLars-Peter Clausen 	mask = e->mask << e->shift_l;
2568db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
256929ae2fa5SLars-Peter Clausen 		if (item[1] >= e->items)
2570db2a4165SFrank Mandarino 			return -EINVAL;
257129ae2fa5SLars-Peter Clausen 		val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
257286767b7dSLars-Peter Clausen 		mask |= e->mask << e->shift_r;
2573db2a4165SFrank Mandarino 	}
2574db2a4165SFrank Mandarino 
2575907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, e->reg, mask, val);
2576db2a4165SFrank Mandarino }
2577db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2578db2a4165SFrank Mandarino 
2579db2a4165SFrank Mandarino /**
2580f227b88fSMarkus Pargmann  * snd_soc_read_signed - Read a codec register and interprete as signed value
2581907fe36aSLars-Peter Clausen  * @component: component
2582f227b88fSMarkus Pargmann  * @reg: Register to read
2583f227b88fSMarkus Pargmann  * @mask: Mask to use after shifting the register value
2584f227b88fSMarkus Pargmann  * @shift: Right shift of register value
2585f227b88fSMarkus Pargmann  * @sign_bit: Bit that describes if a number is negative or not.
2586907fe36aSLars-Peter Clausen  * @signed_val: Pointer to where the read value should be stored
25872e72f8e3SPeter Ujfalusi  *
2588f227b88fSMarkus Pargmann  * This functions reads a codec register. The register value is shifted right
2589f227b88fSMarkus Pargmann  * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2590f227b88fSMarkus Pargmann  * the given registervalue into a signed integer if sign_bit is non-zero.
25912e72f8e3SPeter Ujfalusi  *
2592907fe36aSLars-Peter Clausen  * Returns 0 on sucess, otherwise an error value
25932e72f8e3SPeter Ujfalusi  */
2594907fe36aSLars-Peter Clausen static int snd_soc_read_signed(struct snd_soc_component *component,
2595907fe36aSLars-Peter Clausen 	unsigned int reg, unsigned int mask, unsigned int shift,
2596907fe36aSLars-Peter Clausen 	unsigned int sign_bit, int *signed_val)
25972e72f8e3SPeter Ujfalusi {
2598f227b88fSMarkus Pargmann 	int ret;
259946f5822fSDaniel Ribeiro 	unsigned int val;
26002e72f8e3SPeter Ujfalusi 
2601907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2602907fe36aSLars-Peter Clausen 	if (ret < 0)
2603907fe36aSLars-Peter Clausen 		return ret;
26042e72f8e3SPeter Ujfalusi 
2605907fe36aSLars-Peter Clausen 	val = (val >> shift) & mask;
2606907fe36aSLars-Peter Clausen 
2607907fe36aSLars-Peter Clausen 	if (!sign_bit) {
2608907fe36aSLars-Peter Clausen 		*signed_val = val;
2609907fe36aSLars-Peter Clausen 		return 0;
2610907fe36aSLars-Peter Clausen 	}
2611f227b88fSMarkus Pargmann 
2612f227b88fSMarkus Pargmann 	/* non-negative number */
2613907fe36aSLars-Peter Clausen 	if (!(val & BIT(sign_bit))) {
2614907fe36aSLars-Peter Clausen 		*signed_val = val;
2615907fe36aSLars-Peter Clausen 		return 0;
2616907fe36aSLars-Peter Clausen 	}
2617f227b88fSMarkus Pargmann 
2618f227b88fSMarkus Pargmann 	ret = val;
2619f227b88fSMarkus Pargmann 
2620f227b88fSMarkus Pargmann 	/*
2621f227b88fSMarkus Pargmann 	 * The register most probably does not contain a full-sized int.
2622f227b88fSMarkus Pargmann 	 * Instead we have an arbitrary number of bits in a signed
2623f227b88fSMarkus Pargmann 	 * representation which has to be translated into a full-sized int.
2624f227b88fSMarkus Pargmann 	 * This is done by filling up all bits above the sign-bit.
2625f227b88fSMarkus Pargmann 	 */
2626f227b88fSMarkus Pargmann 	ret |= ~((int)(BIT(sign_bit) - 1));
2627f227b88fSMarkus Pargmann 
2628907fe36aSLars-Peter Clausen 	*signed_val = ret;
2629907fe36aSLars-Peter Clausen 
2630907fe36aSLars-Peter Clausen 	return 0;
26312e72f8e3SPeter Ujfalusi }
26322e72f8e3SPeter Ujfalusi 
26332e72f8e3SPeter Ujfalusi /**
2634db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2635db2a4165SFrank Mandarino  * @kcontrol: mixer control
2636db2a4165SFrank Mandarino  * @uinfo: control element information
2637db2a4165SFrank Mandarino  *
2638e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2639e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2640db2a4165SFrank Mandarino  *
2641db2a4165SFrank Mandarino  * Returns 0 for success.
2642db2a4165SFrank Mandarino  */
2643db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2644db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2645db2a4165SFrank Mandarino {
26464eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26474eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2648d11bb4a9SPeter Ujfalusi 	int platform_max;
2649db2a4165SFrank Mandarino 
2650d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2651d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2652d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2653d11bb4a9SPeter Ujfalusi 
2654d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2655a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2656a7a4ac86SPhilipp Zabel 	else
2657a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2658a7a4ac86SPhilipp Zabel 
2659e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2660db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2661f227b88fSMarkus Pargmann 	uinfo->value.integer.max = platform_max - mc->min;
2662db2a4165SFrank Mandarino 	return 0;
2663db2a4165SFrank Mandarino }
2664db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2665db2a4165SFrank Mandarino 
2666db2a4165SFrank Mandarino /**
2667db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2668db2a4165SFrank Mandarino  * @kcontrol: mixer control
2669ac11a2b3SMark Brown  * @ucontrol: control element information
2670db2a4165SFrank Mandarino  *
2671f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2672f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2673db2a4165SFrank Mandarino  *
2674db2a4165SFrank Mandarino  * Returns 0 for success.
2675db2a4165SFrank Mandarino  */
2676db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2677db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2678db2a4165SFrank Mandarino {
2679907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
26804eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26814eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2682815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2683f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2684815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2685815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26864eaa9819SJon Smirl 	int max = mc->max;
2687f227b88fSMarkus Pargmann 	int min = mc->min;
2688f227b88fSMarkus Pargmann 	int sign_bit = mc->sign_bit;
2689815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2690815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2691907fe36aSLars-Peter Clausen 	int val;
2692907fe36aSLars-Peter Clausen 	int ret;
2693db2a4165SFrank Mandarino 
2694f227b88fSMarkus Pargmann 	if (sign_bit)
2695f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2696f227b88fSMarkus Pargmann 
2697907fe36aSLars-Peter Clausen 	ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2698907fe36aSLars-Peter Clausen 	if (ret)
2699907fe36aSLars-Peter Clausen 		return ret;
2700907fe36aSLars-Peter Clausen 
2701907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = val - min;
2702f7915d99SPeter Ujfalusi 	if (invert)
2703db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2704a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2705f7915d99SPeter Ujfalusi 
2706f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2707f7915d99SPeter Ujfalusi 		if (reg == reg2)
2708907fe36aSLars-Peter Clausen 			ret = snd_soc_read_signed(component, reg, mask, rshift,
2709907fe36aSLars-Peter Clausen 				sign_bit, &val);
2710f7915d99SPeter Ujfalusi 		else
2711907fe36aSLars-Peter Clausen 			ret = snd_soc_read_signed(component, reg2, mask, shift,
2712907fe36aSLars-Peter Clausen 				sign_bit, &val);
2713907fe36aSLars-Peter Clausen 		if (ret)
2714907fe36aSLars-Peter Clausen 			return ret;
2715907fe36aSLars-Peter Clausen 
2716907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = val - min;
2717f7915d99SPeter Ujfalusi 		if (invert)
2718db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2719a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2720db2a4165SFrank Mandarino 	}
2721db2a4165SFrank Mandarino 
2722db2a4165SFrank Mandarino 	return 0;
2723db2a4165SFrank Mandarino }
2724db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2725db2a4165SFrank Mandarino 
2726db2a4165SFrank Mandarino /**
2727db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2728db2a4165SFrank Mandarino  * @kcontrol: mixer control
2729ac11a2b3SMark Brown  * @ucontrol: control element information
2730db2a4165SFrank Mandarino  *
2731974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2732974815baSPeter Ujfalusi  * control that spans 2 registers.
2733db2a4165SFrank Mandarino  *
2734db2a4165SFrank Mandarino  * Returns 0 for success.
2735db2a4165SFrank Mandarino  */
2736db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2737db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2738db2a4165SFrank Mandarino {
2739907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
27404eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27414eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2742815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2743974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2744815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2745815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
27464eaa9819SJon Smirl 	int max = mc->max;
2747f227b88fSMarkus Pargmann 	int min = mc->min;
2748f227b88fSMarkus Pargmann 	unsigned int sign_bit = mc->sign_bit;
2749815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2750815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2751974815baSPeter Ujfalusi 	int err;
2752939d9f16SNenghua Cao 	bool type_2r = false;
2753974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2754974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2755db2a4165SFrank Mandarino 
2756f227b88fSMarkus Pargmann 	if (sign_bit)
2757f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2758f227b88fSMarkus Pargmann 
2759f227b88fSMarkus Pargmann 	val = ((ucontrol->value.integer.value[0] + min) & mask);
2760db2a4165SFrank Mandarino 	if (invert)
2761a7a4ac86SPhilipp Zabel 		val = max - val;
2762db2a4165SFrank Mandarino 	val_mask = mask << shift;
2763db2a4165SFrank Mandarino 	val = val << shift;
2764974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2765f227b88fSMarkus Pargmann 		val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2766db2a4165SFrank Mandarino 		if (invert)
2767a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2768974815baSPeter Ujfalusi 		if (reg == reg2) {
2769db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2770db2a4165SFrank Mandarino 			val |= val2 << rshift;
2771974815baSPeter Ujfalusi 		} else {
2772db2a4165SFrank Mandarino 			val2 = val2 << shift;
2773939d9f16SNenghua Cao 			type_2r = true;
2774974815baSPeter Ujfalusi 		}
2775974815baSPeter Ujfalusi 	}
2776907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
27773ff3f64bSMark Brown 	if (err < 0)
2778db2a4165SFrank Mandarino 		return err;
2779db2a4165SFrank Mandarino 
2780974815baSPeter Ujfalusi 	if (type_2r)
2781907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, reg2, val_mask,
2782907fe36aSLars-Peter Clausen 			val2);
2783974815baSPeter Ujfalusi 
2784db2a4165SFrank Mandarino 	return err;
2785db2a4165SFrank Mandarino }
2786974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2787db2a4165SFrank Mandarino 
2788e13ac2e9SMark Brown /**
27891d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
27901d99f243SBrian Austin  * @kcontrol: mixer control
27911d99f243SBrian Austin  * @ucontrol: control element information
27921d99f243SBrian Austin  *
27931d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
27941d99f243SBrian Austin  * control that spans 2 registers.
27951d99f243SBrian Austin  *
27961d99f243SBrian Austin  * Returns 0 for success.
27971d99f243SBrian Austin  */
27981d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
27991d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
28001d99f243SBrian Austin {
2801907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
28021d99f243SBrian Austin 	struct soc_mixer_control *mc =
28031d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
28041d99f243SBrian Austin 	unsigned int reg = mc->reg;
28051d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28061d99f243SBrian Austin 	unsigned int shift = mc->shift;
28071d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28081d99f243SBrian Austin 	int max = mc->max;
28091d99f243SBrian Austin 	int min = mc->min;
28101d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
2811907fe36aSLars-Peter Clausen 	unsigned int val;
2812907fe36aSLars-Peter Clausen 	int ret;
28131d99f243SBrian Austin 
2814907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2815907fe36aSLars-Peter Clausen 	if (ret < 0)
2816907fe36aSLars-Peter Clausen 		return ret;
28171d99f243SBrian Austin 
2818907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2819907fe36aSLars-Peter Clausen 
2820907fe36aSLars-Peter Clausen 	if (snd_soc_volsw_is_stereo(mc)) {
2821907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, reg2, &val);
2822907fe36aSLars-Peter Clausen 		if (ret < 0)
2823907fe36aSLars-Peter Clausen 			return ret;
2824907fe36aSLars-Peter Clausen 
2825907fe36aSLars-Peter Clausen 		val = ((val >> rshift) - min) & mask;
2826907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = val;
2827907fe36aSLars-Peter Clausen 	}
28281d99f243SBrian Austin 
28291d99f243SBrian Austin 	return 0;
28301d99f243SBrian Austin }
28311d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
28321d99f243SBrian Austin 
28331d99f243SBrian Austin /**
28341d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
28351d99f243SBrian Austin  * @kcontrol: mixer control
28361d99f243SBrian Austin  * @uinfo: control element information
28371d99f243SBrian Austin  *
28381d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
28391d99f243SBrian Austin  *
28401d99f243SBrian Austin  * Returns 0 for success.
28411d99f243SBrian Austin  */
28421d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
28431d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
28441d99f243SBrian Austin {
2845907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
28461d99f243SBrian Austin 	struct soc_mixer_control *mc =
28471d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
28481d99f243SBrian Austin 
28491d99f243SBrian Austin 	unsigned int reg = mc->reg;
28501d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28511d99f243SBrian Austin 	unsigned int shift = mc->shift;
28521d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28531d99f243SBrian Austin 	int max = mc->max;
28541d99f243SBrian Austin 	int min = mc->min;
28551d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
285627f1d759SBrian Austin 	int err = 0;
28571a39019eSBard Liao 	unsigned int val, val_mask, val2 = 0;
28581d99f243SBrian Austin 
28591d99f243SBrian Austin 	val_mask = mask << shift;
28601d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
28611d99f243SBrian Austin 	val = val << shift;
28621d99f243SBrian Austin 
2863907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
2864d055852eSMukund Navada 	if (err < 0)
28651d99f243SBrian Austin 		return err;
28661d99f243SBrian Austin 
28671d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
28681d99f243SBrian Austin 		val_mask = mask << rshift;
28691d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
28701d99f243SBrian Austin 		val2 = val2 << rshift;
28711d99f243SBrian Austin 
2872907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, reg2, val_mask,
2873907fe36aSLars-Peter Clausen 			val2);
28741d99f243SBrian Austin 	}
2875907fe36aSLars-Peter Clausen 	return err;
28761d99f243SBrian Austin }
28771d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
28781d99f243SBrian Austin 
28791d99f243SBrian Austin /**
2880e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2881e13ac2e9SMark Brown  * @kcontrol: mixer control
2882e13ac2e9SMark Brown  * @uinfo: control element information
2883e13ac2e9SMark Brown  *
2884e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2885e13ac2e9SMark Brown  *
2886e13ac2e9SMark Brown  * Returns 0 for success.
2887e13ac2e9SMark Brown  */
2888e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2889e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2890e13ac2e9SMark Brown {
28914eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28924eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2893d11bb4a9SPeter Ujfalusi 	int platform_max;
28944eaa9819SJon Smirl 	int min = mc->min;
2895e13ac2e9SMark Brown 
2896d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2897d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2898d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2899d11bb4a9SPeter Ujfalusi 
2900e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2901e13ac2e9SMark Brown 	uinfo->count = 2;
2902e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2903d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2904e13ac2e9SMark Brown 	return 0;
2905e13ac2e9SMark Brown }
2906e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2907e13ac2e9SMark Brown 
2908e13ac2e9SMark Brown /**
2909e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2910e13ac2e9SMark Brown  * @kcontrol: mixer control
2911ac11a2b3SMark Brown  * @ucontrol: control element information
2912e13ac2e9SMark Brown  *
2913e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2914e13ac2e9SMark Brown  *
2915e13ac2e9SMark Brown  * Returns 0 for success.
2916e13ac2e9SMark Brown  */
2917e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2918e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2919e13ac2e9SMark Brown {
29204eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29214eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2922907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2923815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2924907fe36aSLars-Peter Clausen 	unsigned int val;
29254eaa9819SJon Smirl 	int min = mc->min;
2926907fe36aSLars-Peter Clausen 	int ret;
2927907fe36aSLars-Peter Clausen 
2928907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2929907fe36aSLars-Peter Clausen 	if (ret)
2930907fe36aSLars-Peter Clausen 		return ret;
2931e13ac2e9SMark Brown 
2932e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2933e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2934e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2935e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2936e13ac2e9SMark Brown 	return 0;
2937e13ac2e9SMark Brown }
2938e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2939e13ac2e9SMark Brown 
2940e13ac2e9SMark Brown /**
2941e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2942e13ac2e9SMark Brown  * @kcontrol: mixer control
2943ac11a2b3SMark Brown  * @ucontrol: control element information
2944e13ac2e9SMark Brown  *
2945e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2946e13ac2e9SMark Brown  *
2947e13ac2e9SMark Brown  * Returns 0 for success.
2948e13ac2e9SMark Brown  */
2949e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2950e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2951e13ac2e9SMark Brown {
29524eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29534eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2954907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2955815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
29564eaa9819SJon Smirl 	int min = mc->min;
295746f5822fSDaniel Ribeiro 	unsigned int val;
2958e13ac2e9SMark Brown 
2959e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2960e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2961e13ac2e9SMark Brown 
2962907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, reg, 0xffff, val);
2963e13ac2e9SMark Brown }
2964e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2965e13ac2e9SMark Brown 
29668c6529dbSLiam Girdwood /**
29676c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
29686c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29696c9d8cf6SAdam Thomson  * @uinfo: control element information
29706c9d8cf6SAdam Thomson  *
29716c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
29726c9d8cf6SAdam Thomson  * mixer control.
29736c9d8cf6SAdam Thomson  *
29746c9d8cf6SAdam Thomson  * returns 0 for success.
29756c9d8cf6SAdam Thomson  */
29766c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
29776c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
29786c9d8cf6SAdam Thomson {
29796c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29806c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29816c9d8cf6SAdam Thomson 	int platform_max;
29826c9d8cf6SAdam Thomson 	int min = mc->min;
29836c9d8cf6SAdam Thomson 
29846c9d8cf6SAdam Thomson 	if (!mc->platform_max)
29856c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
29866c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
29876c9d8cf6SAdam Thomson 
29886c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29899bde4f0bSMark Brown 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
29906c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
29916c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
29926c9d8cf6SAdam Thomson 
29936c9d8cf6SAdam Thomson 	return 0;
29946c9d8cf6SAdam Thomson }
29956c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
29966c9d8cf6SAdam Thomson 
29976c9d8cf6SAdam Thomson /**
29986c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
29996c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30006c9d8cf6SAdam Thomson  * @ucontrol: control element information
30016c9d8cf6SAdam Thomson  *
30026c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
30036c9d8cf6SAdam Thomson  *
30046c9d8cf6SAdam Thomson  * Returns 0 for success.
30056c9d8cf6SAdam Thomson  */
30066c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
30076c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30086c9d8cf6SAdam Thomson {
30096c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30106c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
3011907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
30126c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
30139bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
30146c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
30156c9d8cf6SAdam Thomson 	int min = mc->min;
30166c9d8cf6SAdam Thomson 	int max = mc->max;
30176c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
30186c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
30196c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
30209bde4f0bSMark Brown 	int ret;
30216c9d8cf6SAdam Thomson 
30226c9d8cf6SAdam Thomson 	if (invert)
3023*5c7c343aSHoward Mitchell 		val = (max - ucontrol->value.integer.value[0]) & mask;
3024*5c7c343aSHoward Mitchell 	else
3025*5c7c343aSHoward Mitchell 		val = ((ucontrol->value.integer.value[0] + min) & mask);
30266c9d8cf6SAdam Thomson 	val_mask = mask << shift;
30276c9d8cf6SAdam Thomson 	val = val << shift;
30286c9d8cf6SAdam Thomson 
3029907fe36aSLars-Peter Clausen 	ret = snd_soc_component_update_bits(component, reg, val_mask, val);
30300eaa6ccaSJoonyoung Shim 	if (ret < 0)
30319bde4f0bSMark Brown 		return ret;
30329bde4f0bSMark Brown 
30339bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
30349bde4f0bSMark Brown 		if (invert)
3035*5c7c343aSHoward Mitchell 			val = (max - ucontrol->value.integer.value[1]) & mask;
3036*5c7c343aSHoward Mitchell 		else
3037*5c7c343aSHoward Mitchell 			val = ((ucontrol->value.integer.value[1] + min) & mask);
30389bde4f0bSMark Brown 		val_mask = mask << shift;
30399bde4f0bSMark Brown 		val = val << shift;
30409bde4f0bSMark Brown 
3041907fe36aSLars-Peter Clausen 		ret = snd_soc_component_update_bits(component, rreg, val_mask,
3042907fe36aSLars-Peter Clausen 			val);
30439bde4f0bSMark Brown 	}
30449bde4f0bSMark Brown 
30459bde4f0bSMark Brown 	return ret;
30466c9d8cf6SAdam Thomson }
30476c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
30486c9d8cf6SAdam Thomson 
30496c9d8cf6SAdam Thomson /**
30506c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
30516c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30526c9d8cf6SAdam Thomson  * @ucontrol: control element information
30536c9d8cf6SAdam Thomson  *
30546c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
30556c9d8cf6SAdam Thomson  *
30566c9d8cf6SAdam Thomson  * Returns 0 for success.
30576c9d8cf6SAdam Thomson  */
30586c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
30596c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30606c9d8cf6SAdam Thomson {
3061907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
30626c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30636c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
30646c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
30659bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
30666c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
30676c9d8cf6SAdam Thomson 	int min = mc->min;
30686c9d8cf6SAdam Thomson 	int max = mc->max;
30696c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
30706c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
3071907fe36aSLars-Peter Clausen 	unsigned int val;
3072907fe36aSLars-Peter Clausen 	int ret;
30736c9d8cf6SAdam Thomson 
3074907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
3075907fe36aSLars-Peter Clausen 	if (ret)
3076907fe36aSLars-Peter Clausen 		return ret;
3077907fe36aSLars-Peter Clausen 
3078907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = (val >> shift) & mask;
30796c9d8cf6SAdam Thomson 	if (invert)
30806c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
30816c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
3082*5c7c343aSHoward Mitchell 	else
30836c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
30846c9d8cf6SAdam Thomson 			ucontrol->value.integer.value[0] - min;
30856c9d8cf6SAdam Thomson 
30869bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
3087907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, rreg, &val);
3088907fe36aSLars-Peter Clausen 		if (ret)
3089907fe36aSLars-Peter Clausen 			return ret;
3090907fe36aSLars-Peter Clausen 
3091907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = (val >> shift) & mask;
30929bde4f0bSMark Brown 		if (invert)
30939bde4f0bSMark Brown 			ucontrol->value.integer.value[1] =
30949bde4f0bSMark Brown 				max - ucontrol->value.integer.value[1];
3095*5c7c343aSHoward Mitchell 		else
30969bde4f0bSMark Brown 			ucontrol->value.integer.value[1] =
30979bde4f0bSMark Brown 				ucontrol->value.integer.value[1] - min;
30989bde4f0bSMark Brown 	}
30999bde4f0bSMark Brown 
31006c9d8cf6SAdam Thomson 	return 0;
31016c9d8cf6SAdam Thomson }
31026c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
31036c9d8cf6SAdam Thomson 
31046c9d8cf6SAdam Thomson /**
3105637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
3106637d3847SPeter Ujfalusi  *
3107637d3847SPeter Ujfalusi  * @codec: where to look for the control
3108637d3847SPeter Ujfalusi  * @name: Name of the control
3109637d3847SPeter Ujfalusi  * @max: new maximum limit
3110637d3847SPeter Ujfalusi  *
3111637d3847SPeter Ujfalusi  * Return 0 for success, else error.
3112637d3847SPeter Ujfalusi  */
3113637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
3114637d3847SPeter Ujfalusi 	const char *name, int max)
3115637d3847SPeter Ujfalusi {
311600200107SLars-Peter Clausen 	struct snd_card *card = codec->component.card->snd_card;
3117637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
3118637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
3119637d3847SPeter Ujfalusi 	int found = 0;
3120637d3847SPeter Ujfalusi 	int ret = -EINVAL;
3121637d3847SPeter Ujfalusi 
3122637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
3123637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
3124637d3847SPeter Ujfalusi 		return -EINVAL;
3125637d3847SPeter Ujfalusi 
3126637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
3127637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3128637d3847SPeter Ujfalusi 			found = 1;
3129637d3847SPeter Ujfalusi 			break;
3130637d3847SPeter Ujfalusi 		}
3131637d3847SPeter Ujfalusi 	}
3132637d3847SPeter Ujfalusi 	if (found) {
3133637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3134637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3135d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3136637d3847SPeter Ujfalusi 			ret = 0;
3137637d3847SPeter Ujfalusi 		}
3138637d3847SPeter Ujfalusi 	}
3139637d3847SPeter Ujfalusi 	return ret;
3140637d3847SPeter Ujfalusi }
3141637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3142637d3847SPeter Ujfalusi 
314371d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
314471d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
314571d08516SMark Brown {
3146907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
314771d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
314871d08516SMark Brown 
314971d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3150907fe36aSLars-Peter Clausen 	uinfo->count = params->num_regs * component->val_bytes;
315171d08516SMark Brown 
315271d08516SMark Brown 	return 0;
315371d08516SMark Brown }
315471d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
315571d08516SMark Brown 
315671d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
315771d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
315871d08516SMark Brown {
3159907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
316071d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
316171d08516SMark Brown 	int ret;
316271d08516SMark Brown 
3163907fe36aSLars-Peter Clausen 	if (component->regmap)
3164907fe36aSLars-Peter Clausen 		ret = regmap_raw_read(component->regmap, params->base,
316571d08516SMark Brown 				      ucontrol->value.bytes.data,
3166907fe36aSLars-Peter Clausen 				      params->num_regs * component->val_bytes);
316771d08516SMark Brown 	else
316871d08516SMark Brown 		ret = -EINVAL;
316971d08516SMark Brown 
3170f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
3171f831b055SMark Brown 	if (ret == 0 && params->mask) {
3172907fe36aSLars-Peter Clausen 		switch (component->val_bytes) {
3173f831b055SMark Brown 		case 1:
3174f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
3175f831b055SMark Brown 			break;
3176f831b055SMark Brown 		case 2:
3177f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
31788f1ec93aSCharles Keepax 				&= cpu_to_be16(~params->mask);
3179f831b055SMark Brown 			break;
3180f831b055SMark Brown 		case 4:
3181f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
31828f1ec93aSCharles Keepax 				&= cpu_to_be32(~params->mask);
3183f831b055SMark Brown 			break;
3184f831b055SMark Brown 		default:
3185f831b055SMark Brown 			return -EINVAL;
3186f831b055SMark Brown 		}
3187f831b055SMark Brown 	}
3188f831b055SMark Brown 
318971d08516SMark Brown 	return ret;
319071d08516SMark Brown }
319171d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
319271d08516SMark Brown 
319371d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
319471d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
319571d08516SMark Brown {
3196907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
319771d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
3198f831b055SMark Brown 	int ret, len;
3199a1a564edSNenghua Cao 	unsigned int val, mask;
3200f831b055SMark Brown 	void *data;
320171d08516SMark Brown 
3202907fe36aSLars-Peter Clausen 	if (!component->regmap)
3203f831b055SMark Brown 		return -EINVAL;
3204f831b055SMark Brown 
3205907fe36aSLars-Peter Clausen 	len = params->num_regs * component->val_bytes;
3206f831b055SMark Brown 
3207b5a8fe43SMark Brown 	data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3208b5a8fe43SMark Brown 	if (!data)
3209b5a8fe43SMark Brown 		return -ENOMEM;
3210b5a8fe43SMark Brown 
3211f831b055SMark Brown 	/*
3212f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3213f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3214f831b055SMark Brown 	 * copy.
3215f831b055SMark Brown 	 */
3216f831b055SMark Brown 	if (params->mask) {
3217907fe36aSLars-Peter Clausen 		ret = regmap_read(component->regmap, params->base, &val);
3218f831b055SMark Brown 		if (ret != 0)
3219e8b18addSWei Yongjun 			goto out;
3220f831b055SMark Brown 
3221f831b055SMark Brown 		val &= params->mask;
3222f831b055SMark Brown 
3223907fe36aSLars-Peter Clausen 		switch (component->val_bytes) {
3224f831b055SMark Brown 		case 1:
3225f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3226f831b055SMark Brown 			((u8 *)data)[0] |= val;
3227f831b055SMark Brown 			break;
3228f831b055SMark Brown 		case 2:
3229a1a564edSNenghua Cao 			mask = ~params->mask;
3230907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3231a1a564edSNenghua Cao 							&mask, &mask);
3232a1a564edSNenghua Cao 			if (ret != 0)
3233a1a564edSNenghua Cao 				goto out;
3234a1a564edSNenghua Cao 
3235a1a564edSNenghua Cao 			((u16 *)data)[0] &= mask;
3236a1a564edSNenghua Cao 
3237907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3238a1a564edSNenghua Cao 							&val, &val);
3239a1a564edSNenghua Cao 			if (ret != 0)
3240a1a564edSNenghua Cao 				goto out;
3241a1a564edSNenghua Cao 
3242a1a564edSNenghua Cao 			((u16 *)data)[0] |= val;
3243f831b055SMark Brown 			break;
3244f831b055SMark Brown 		case 4:
3245a1a564edSNenghua Cao 			mask = ~params->mask;
3246907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3247a1a564edSNenghua Cao 							&mask, &mask);
3248a1a564edSNenghua Cao 			if (ret != 0)
3249a1a564edSNenghua Cao 				goto out;
3250a1a564edSNenghua Cao 
3251a1a564edSNenghua Cao 			((u32 *)data)[0] &= mask;
3252a1a564edSNenghua Cao 
3253907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3254a1a564edSNenghua Cao 							&val, &val);
3255a1a564edSNenghua Cao 			if (ret != 0)
3256a1a564edSNenghua Cao 				goto out;
3257a1a564edSNenghua Cao 
3258a1a564edSNenghua Cao 			((u32 *)data)[0] |= val;
3259f831b055SMark Brown 			break;
3260f831b055SMark Brown 		default:
3261e8b18addSWei Yongjun 			ret = -EINVAL;
3262e8b18addSWei Yongjun 			goto out;
3263f831b055SMark Brown 		}
3264f831b055SMark Brown 	}
3265f831b055SMark Brown 
3266907fe36aSLars-Peter Clausen 	ret = regmap_raw_write(component->regmap, params->base,
3267f831b055SMark Brown 			       data, len);
3268f831b055SMark Brown 
3269e8b18addSWei Yongjun out:
3270f831b055SMark Brown 	kfree(data);
327171d08516SMark Brown 
327271d08516SMark Brown 	return ret;
327371d08516SMark Brown }
327471d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
327571d08516SMark Brown 
3276d9881208SVinod Koul int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3277d9881208SVinod Koul 			struct snd_ctl_elem_info *ucontrol)
3278d9881208SVinod Koul {
3279d9881208SVinod Koul 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3280d9881208SVinod Koul 
3281d9881208SVinod Koul 	ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3282d9881208SVinod Koul 	ucontrol->count = params->max;
3283d9881208SVinod Koul 
3284d9881208SVinod Koul 	return 0;
3285d9881208SVinod Koul }
3286d9881208SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3287d9881208SVinod Koul 
32887523a271SOmair Mohammed Abdullah int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
32897523a271SOmair Mohammed Abdullah 				unsigned int size, unsigned int __user *tlv)
32907523a271SOmair Mohammed Abdullah {
32917523a271SOmair Mohammed Abdullah 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
32927523a271SOmair Mohammed Abdullah 	unsigned int count = size < params->max ? size : params->max;
32937523a271SOmair Mohammed Abdullah 	int ret = -ENXIO;
32947523a271SOmair Mohammed Abdullah 
32957523a271SOmair Mohammed Abdullah 	switch (op_flag) {
32967523a271SOmair Mohammed Abdullah 	case SNDRV_CTL_TLV_OP_READ:
32977523a271SOmair Mohammed Abdullah 		if (params->get)
32987523a271SOmair Mohammed Abdullah 			ret = params->get(tlv, count);
32997523a271SOmair Mohammed Abdullah 		break;
33007523a271SOmair Mohammed Abdullah 	case SNDRV_CTL_TLV_OP_WRITE:
33017523a271SOmair Mohammed Abdullah 		if (params->put)
33027523a271SOmair Mohammed Abdullah 			ret = params->put(tlv, count);
33037523a271SOmair Mohammed Abdullah 		break;
33047523a271SOmair Mohammed Abdullah 	}
33057523a271SOmair Mohammed Abdullah 	return ret;
33067523a271SOmair Mohammed Abdullah }
33077523a271SOmair Mohammed Abdullah EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
33087523a271SOmair Mohammed Abdullah 
3309b6f4bb38Sapatard@mandriva.com /**
33104183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
33114183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33124183eed2SKristoffer KARLSSON  * @uinfo: control element information
33134183eed2SKristoffer KARLSSON  *
33144183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
33154183eed2SKristoffer KARLSSON  * span multiple codec registers which together
33164183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
33174183eed2SKristoffer KARLSSON  *
33184183eed2SKristoffer KARLSSON  * Returns 0 for success.
33194183eed2SKristoffer KARLSSON  */
33204183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
33214183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
33224183eed2SKristoffer KARLSSON {
33234183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33244183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33254183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
33264183eed2SKristoffer KARLSSON 	uinfo->count = 1;
33274183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
33284183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
33294183eed2SKristoffer KARLSSON 
33304183eed2SKristoffer KARLSSON 	return 0;
33314183eed2SKristoffer KARLSSON }
33324183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
33334183eed2SKristoffer KARLSSON 
33344183eed2SKristoffer KARLSSON /**
33354183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
33364183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33374183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33384183eed2SKristoffer KARLSSON  *
33394183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
33404183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33414183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33424183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33434183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33444183eed2SKristoffer KARLSSON  *
33454183eed2SKristoffer KARLSSON  * Returns 0 for success.
33464183eed2SKristoffer KARLSSON  */
33474183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
33484183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33494183eed2SKristoffer KARLSSON {
3350907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
33514183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33524183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33534183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33544183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
3355907fe36aSLars-Peter Clausen 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
33564183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33574183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33584183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33594183eed2SKristoffer KARLSSON 	long min = mc->min;
33604183eed2SKristoffer KARLSSON 	long max = mc->max;
33614183eed2SKristoffer KARLSSON 	long val = 0;
3362907fe36aSLars-Peter Clausen 	unsigned int regval;
33634183eed2SKristoffer KARLSSON 	unsigned int i;
3364907fe36aSLars-Peter Clausen 	int ret;
33654183eed2SKristoffer KARLSSON 
33664183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
3367907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, regbase+i, &regval);
3368907fe36aSLars-Peter Clausen 		if (ret)
3369907fe36aSLars-Peter Clausen 			return ret;
3370907fe36aSLars-Peter Clausen 		val |= (regval & regwmask) << (regwshift*(regcount-i-1));
33714183eed2SKristoffer KARLSSON 	}
33724183eed2SKristoffer KARLSSON 	val &= mask;
33734183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
33744183eed2SKristoffer KARLSSON 		val |= ~mask;
33754183eed2SKristoffer KARLSSON 	if (invert)
33764183eed2SKristoffer KARLSSON 		val = max - val;
33774183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
33784183eed2SKristoffer KARLSSON 
33794183eed2SKristoffer KARLSSON 	return 0;
33804183eed2SKristoffer KARLSSON }
33814183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
33824183eed2SKristoffer KARLSSON 
33834183eed2SKristoffer KARLSSON /**
33844183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
33854183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33864183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33874183eed2SKristoffer KARLSSON  *
33884183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
33894183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33904183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33914183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33924183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33934183eed2SKristoffer KARLSSON  *
33944183eed2SKristoffer KARLSSON  * Returns 0 for success.
33954183eed2SKristoffer KARLSSON  */
33964183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
33974183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33984183eed2SKristoffer KARLSSON {
3399907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
34004183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
34014183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
34024183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
34034183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
3404907fe36aSLars-Peter Clausen 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
34054183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
34064183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
34074183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
34084183eed2SKristoffer KARLSSON 	long max = mc->max;
34094183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
34104183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
34114183eed2SKristoffer KARLSSON 	int err;
34124183eed2SKristoffer KARLSSON 
34134183eed2SKristoffer KARLSSON 	if (invert)
34144183eed2SKristoffer KARLSSON 		val = max - val;
34154183eed2SKristoffer KARLSSON 	val &= mask;
34164183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
34174183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
34184183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3419907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, regbase+i,
34204183eed2SKristoffer KARLSSON 				regmask, regval);
34214183eed2SKristoffer KARLSSON 		if (err < 0)
34224183eed2SKristoffer KARLSSON 			return err;
34234183eed2SKristoffer KARLSSON 	}
34244183eed2SKristoffer KARLSSON 
34254183eed2SKristoffer KARLSSON 	return 0;
34264183eed2SKristoffer KARLSSON }
34274183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
34284183eed2SKristoffer KARLSSON 
34294183eed2SKristoffer KARLSSON /**
3430dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3431dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3432dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3433dd7b10b3SKristoffer KARLSSON  *
3434dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3435dd7b10b3SKristoffer KARLSSON  *
3436dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3437dd7b10b3SKristoffer KARLSSON  */
3438dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3439dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3440dd7b10b3SKristoffer KARLSSON {
3441907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3442dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3443dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3444dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3445dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3446dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3447dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3448907fe36aSLars-Peter Clausen 	unsigned int val;
3449907fe36aSLars-Peter Clausen 	int ret;
3450907fe36aSLars-Peter Clausen 
3451907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
3452907fe36aSLars-Peter Clausen 	if (ret)
3453907fe36aSLars-Peter Clausen 		return ret;
3454907fe36aSLars-Peter Clausen 
3455907fe36aSLars-Peter Clausen 	val &= mask;
3456dd7b10b3SKristoffer KARLSSON 
3457dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3458dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3459dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3460dd7b10b3SKristoffer KARLSSON 
3461dd7b10b3SKristoffer KARLSSON 	return 0;
3462dd7b10b3SKristoffer KARLSSON }
3463dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3464dd7b10b3SKristoffer KARLSSON 
3465dd7b10b3SKristoffer KARLSSON /**
3466dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3467dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3468dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3469dd7b10b3SKristoffer KARLSSON  *
3470dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3471dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3472dd7b10b3SKristoffer KARLSSON  *
3473dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3474dd7b10b3SKristoffer KARLSSON  */
3475dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3476dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3477dd7b10b3SKristoffer KARLSSON {
3478907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3479dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3480dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3481dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3482dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3483dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3484dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3485dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3486dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3487dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3488dd7b10b3SKristoffer KARLSSON 	int err;
3489dd7b10b3SKristoffer KARLSSON 
3490907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, mask, val1);
3491dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3492dd7b10b3SKristoffer KARLSSON 		return err;
3493dd7b10b3SKristoffer KARLSSON 
3494907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, reg, mask, val2);
3495dd7b10b3SKristoffer KARLSSON }
3496dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3497dd7b10b3SKristoffer KARLSSON 
3498dd7b10b3SKristoffer KARLSSON /**
34998c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
35008c6529dbSLiam Girdwood  * @dai: DAI
35018c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
35028c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
35038c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
35048c6529dbSLiam Girdwood  *
35058c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
35068c6529dbSLiam Girdwood  */
35078c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
35088c6529dbSLiam Girdwood 	unsigned int freq, int dir)
35098c6529dbSLiam Girdwood {
3510f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3511f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3512ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3513da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3514ec4ee52aSMark Brown 						      freq, dir);
35158c6529dbSLiam Girdwood 	else
35161104a9c8SMark Brown 		return -ENOTSUPP;
35178c6529dbSLiam Girdwood }
35188c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
35198c6529dbSLiam Girdwood 
35208c6529dbSLiam Girdwood /**
3521ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3522ec4ee52aSMark Brown  * @codec: CODEC
3523ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3524da1c6ea6SMark Brown  * @source: Source for the clock
3525ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3526ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3527ec4ee52aSMark Brown  *
3528ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3529ec4ee52aSMark Brown  */
3530ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3531da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3532ec4ee52aSMark Brown {
3533ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3534da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3535da1c6ea6SMark Brown 						 freq, dir);
3536ec4ee52aSMark Brown 	else
35371104a9c8SMark Brown 		return -ENOTSUPP;
3538ec4ee52aSMark Brown }
3539ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3540ec4ee52aSMark Brown 
3541ec4ee52aSMark Brown /**
35428c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
35438c6529dbSLiam Girdwood  * @dai: DAI
3544ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
35458c6529dbSLiam Girdwood  * @div: new clock divisor.
35468c6529dbSLiam Girdwood  *
35478c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
35488c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
35498c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
35508c6529dbSLiam Girdwood  */
35518c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
35528c6529dbSLiam Girdwood 	int div_id, int div)
35538c6529dbSLiam Girdwood {
3554f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3555f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
35568c6529dbSLiam Girdwood 	else
35578c6529dbSLiam Girdwood 		return -EINVAL;
35588c6529dbSLiam Girdwood }
35598c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
35608c6529dbSLiam Girdwood 
35618c6529dbSLiam Girdwood /**
35628c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
35638c6529dbSLiam Girdwood  * @dai: DAI
35648c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
356585488037SMark Brown  * @source: DAI specific source for the PLL
35668c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
35678c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
35688c6529dbSLiam Girdwood  *
35698c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
35708c6529dbSLiam Girdwood  */
357185488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
357285488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
35738c6529dbSLiam Girdwood {
3574f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3575f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
357685488037SMark Brown 					 freq_in, freq_out);
3577ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3578ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3579ec4ee52aSMark Brown 						   freq_in, freq_out);
35808c6529dbSLiam Girdwood 	else
35818c6529dbSLiam Girdwood 		return -EINVAL;
35828c6529dbSLiam Girdwood }
35838c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
35848c6529dbSLiam Girdwood 
3585ec4ee52aSMark Brown /*
3586ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3587ec4ee52aSMark Brown  * @codec: CODEC
3588ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3589ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3590ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3591ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3592ec4ee52aSMark Brown  *
3593ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3594ec4ee52aSMark Brown  */
3595ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3596ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3597ec4ee52aSMark Brown {
3598ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3599ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3600ec4ee52aSMark Brown 					      freq_in, freq_out);
3601ec4ee52aSMark Brown 	else
3602ec4ee52aSMark Brown 		return -EINVAL;
3603ec4ee52aSMark Brown }
3604ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3605ec4ee52aSMark Brown 
36068c6529dbSLiam Girdwood /**
3607e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3608e54cf76bSLiam Girdwood  * @dai: DAI
3609e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
3610e54cf76bSLiam Girdwood  *
3611e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
3612e54cf76bSLiam Girdwood  */
3613e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3614e54cf76bSLiam Girdwood {
3615e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
3616e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
3617e54cf76bSLiam Girdwood 	else
3618e54cf76bSLiam Girdwood 		return -EINVAL;
3619e54cf76bSLiam Girdwood }
3620e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3621e54cf76bSLiam Girdwood 
3622e54cf76bSLiam Girdwood /**
36238c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
36248c6529dbSLiam Girdwood  * @dai: DAI
36258c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
36268c6529dbSLiam Girdwood  *
36278c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
36288c6529dbSLiam Girdwood  */
36298c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
36308c6529dbSLiam Girdwood {
36315e4ba569SShawn Guo 	if (dai->driver == NULL)
36328c6529dbSLiam Girdwood 		return -EINVAL;
36335e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
36345e4ba569SShawn Guo 		return -ENOTSUPP;
36355e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
36368c6529dbSLiam Girdwood }
36378c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
36388c6529dbSLiam Girdwood 
36398c6529dbSLiam Girdwood /**
3640e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
364189c67857SXiubo Li  * @slots: Number of slots in use.
364289c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
364389c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
364489c67857SXiubo Li  *
364589c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
364689c67857SXiubo Li  */
3647e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
364889c67857SXiubo Li 					  unsigned int *tx_mask,
364989c67857SXiubo Li 					  unsigned int *rx_mask)
365089c67857SXiubo Li {
365189c67857SXiubo Li 	if (*tx_mask || *rx_mask)
365289c67857SXiubo Li 		return 0;
365389c67857SXiubo Li 
365489c67857SXiubo Li 	if (!slots)
365589c67857SXiubo Li 		return -EINVAL;
365689c67857SXiubo Li 
365789c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
365889c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
365989c67857SXiubo Li 
366089c67857SXiubo Li 	return 0;
366189c67857SXiubo Li }
366289c67857SXiubo Li 
366389c67857SXiubo Li /**
36648c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
36658c6529dbSLiam Girdwood  * @dai: DAI
3666a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3667a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
36688c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3669a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
36708c6529dbSLiam Girdwood  *
36718c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
36728c6529dbSLiam Girdwood  * specific.
36738c6529dbSLiam Girdwood  */
36748c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3675a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
36768c6529dbSLiam Girdwood {
3677e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3678e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
367989c67857SXiubo Li 						&tx_mask, &rx_mask);
368089c67857SXiubo Li 	else
3681e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
368289c67857SXiubo Li 
368388bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
368488bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
368588bd870fSBenoit Cousson 
3686f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3687f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3688a5479e38SDaniel Ribeiro 				slots, slot_width);
36898c6529dbSLiam Girdwood 	else
3690b2cbb6e1SXiubo Li 		return -ENOTSUPP;
36918c6529dbSLiam Girdwood }
36928c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
36938c6529dbSLiam Girdwood 
36948c6529dbSLiam Girdwood /**
3695472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3696472df3cbSBarry Song  * @dai: DAI
3697472df3cbSBarry Song  * @tx_num: how many TX channels
3698472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3699472df3cbSBarry Song  *           0~num-1 uses
3700472df3cbSBarry Song  * @rx_num: how many RX channels
3701472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3702472df3cbSBarry Song  *           0~num-1 uses
3703472df3cbSBarry Song  *
3704472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3705472df3cbSBarry Song  */
3706472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3707472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3708472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3709472df3cbSBarry Song {
3710f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3711f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3712472df3cbSBarry Song 			rx_num, rx_slot);
3713472df3cbSBarry Song 	else
3714472df3cbSBarry Song 		return -EINVAL;
3715472df3cbSBarry Song }
3716472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3717472df3cbSBarry Song 
3718472df3cbSBarry Song /**
37198c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
37208c6529dbSLiam Girdwood  * @dai: DAI
37218c6529dbSLiam Girdwood  * @tristate: tristate enable
37228c6529dbSLiam Girdwood  *
37238c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
37248c6529dbSLiam Girdwood  */
37258c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
37268c6529dbSLiam Girdwood {
3727f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3728f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
37298c6529dbSLiam Girdwood 	else
37308c6529dbSLiam Girdwood 		return -EINVAL;
37318c6529dbSLiam Girdwood }
37328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
37338c6529dbSLiam Girdwood 
37348c6529dbSLiam Girdwood /**
37358c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
37368c6529dbSLiam Girdwood  * @dai: DAI
37378c6529dbSLiam Girdwood  * @mute: mute enable
3738da18396fSMark Brown  * @direction: stream to mute
37398c6529dbSLiam Girdwood  *
37408c6529dbSLiam Girdwood  * Mutes the DAI DAC.
37418c6529dbSLiam Girdwood  */
3742da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3743da18396fSMark Brown 			     int direction)
37448c6529dbSLiam Girdwood {
3745da18396fSMark Brown 	if (!dai->driver)
3746da18396fSMark Brown 		return -ENOTSUPP;
3747da18396fSMark Brown 
3748da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
3749da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
3750da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3751da18396fSMark Brown 		 dai->driver->ops->digital_mute)
3752f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
37538c6529dbSLiam Girdwood 	else
375404570c62SMark Brown 		return -ENOTSUPP;
37558c6529dbSLiam Girdwood }
37568c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
37578c6529dbSLiam Girdwood 
375888bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card,
375988bd870fSBenoit Cousson 				   struct snd_soc_dai_link *dai_link)
376088bd870fSBenoit Cousson {
376188bd870fSBenoit Cousson 	/* Legacy codec/codec_dai link is a single entry in multicodec */
376288bd870fSBenoit Cousson 	if (dai_link->codec_name || dai_link->codec_of_node ||
376388bd870fSBenoit Cousson 	    dai_link->codec_dai_name) {
376488bd870fSBenoit Cousson 		dai_link->num_codecs = 1;
376588bd870fSBenoit Cousson 
376688bd870fSBenoit Cousson 		dai_link->codecs = devm_kzalloc(card->dev,
376788bd870fSBenoit Cousson 				sizeof(struct snd_soc_dai_link_component),
376888bd870fSBenoit Cousson 				GFP_KERNEL);
376988bd870fSBenoit Cousson 		if (!dai_link->codecs)
377088bd870fSBenoit Cousson 			return -ENOMEM;
377188bd870fSBenoit Cousson 
377288bd870fSBenoit Cousson 		dai_link->codecs[0].name = dai_link->codec_name;
377388bd870fSBenoit Cousson 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
377488bd870fSBenoit Cousson 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
377588bd870fSBenoit Cousson 	}
377688bd870fSBenoit Cousson 
377788bd870fSBenoit Cousson 	if (!dai_link->codecs) {
377888bd870fSBenoit Cousson 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
377988bd870fSBenoit Cousson 		return -EINVAL;
378088bd870fSBenoit Cousson 	}
378188bd870fSBenoit Cousson 
378288bd870fSBenoit Cousson 	return 0;
378388bd870fSBenoit Cousson }
378488bd870fSBenoit Cousson 
3785c5af3a2eSMark Brown /**
3786c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3787c5af3a2eSMark Brown  *
3788ac11a2b3SMark Brown  * @card: Card to register
3789c5af3a2eSMark Brown  *
3790c5af3a2eSMark Brown  */
379170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3792c5af3a2eSMark Brown {
379388bd870fSBenoit Cousson 	int i, j, ret;
3794f0fba2adSLiam Girdwood 
3795c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3796c5af3a2eSMark Brown 		return -EINVAL;
3797c5af3a2eSMark Brown 
37985a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
37995a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
38005a504963SStephen Warren 
380188bd870fSBenoit Cousson 		ret = snd_soc_init_multicodec(card, link);
380288bd870fSBenoit Cousson 		if (ret) {
380388bd870fSBenoit Cousson 			dev_err(card->dev, "ASoC: failed to init multicodec\n");
380488bd870fSBenoit Cousson 			return ret;
380588bd870fSBenoit Cousson 		}
380688bd870fSBenoit Cousson 
380788bd870fSBenoit Cousson 		for (j = 0; j < link->num_codecs; j++) {
38085a504963SStephen Warren 			/*
38095a504963SStephen Warren 			 * Codec must be specified by 1 of name or OF node,
38105a504963SStephen Warren 			 * not both or neither.
38115a504963SStephen Warren 			 */
381288bd870fSBenoit Cousson 			if (!!link->codecs[j].name ==
381388bd870fSBenoit Cousson 			    !!link->codecs[j].of_node) {
381488bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
381510e8aa9aSMichał Mirosław 					link->name);
38165a504963SStephen Warren 				return -EINVAL;
38175a504963SStephen Warren 			}
3818bc92657aSStephen Warren 			/* Codec DAI name must be specified */
381988bd870fSBenoit Cousson 			if (!link->codecs[j].dai_name) {
382088bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
382110e8aa9aSMichał Mirosław 					link->name);
3822bc92657aSStephen Warren 				return -EINVAL;
3823bc92657aSStephen Warren 			}
382488bd870fSBenoit Cousson 		}
38255a504963SStephen Warren 
38265a504963SStephen Warren 		/*
38275a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
38285a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
38295a504963SStephen Warren 		 */
38305a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
383110e8aa9aSMichał Mirosław 			dev_err(card->dev,
383210e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
383310e8aa9aSMichał Mirosław 				link->name);
38345a504963SStephen Warren 			return -EINVAL;
38355a504963SStephen Warren 		}
38365a504963SStephen Warren 
38375a504963SStephen Warren 		/*
3838bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3839bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3840bc92657aSStephen Warren 		 * name alone..
38415a504963SStephen Warren 		 */
3842bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
384310e8aa9aSMichał Mirosław 			dev_err(card->dev,
384410e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
384510e8aa9aSMichał Mirosław 				link->name);
3846bc92657aSStephen Warren 			return -EINVAL;
3847bc92657aSStephen Warren 		}
3848bc92657aSStephen Warren 		/*
3849bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3850bc92657aSStephen Warren 		 * specified
3851bc92657aSStephen Warren 		 */
3852bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3853bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
385410e8aa9aSMichał Mirosław 			dev_err(card->dev,
385510e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
385610e8aa9aSMichał Mirosław 				link->name);
38575a504963SStephen Warren 			return -EINVAL;
38585a504963SStephen Warren 		}
38595a504963SStephen Warren 	}
38605a504963SStephen Warren 
3861ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3862ed77cc12SMark Brown 
3863111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3864111c6419SStephen Warren 
3865150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3866150dd2f8SVinod Koul 
3867181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3868181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
38692eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3870f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3871f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3872f0fba2adSLiam Girdwood 		return -ENOMEM;
3873a7dbb603SLiam Girdwood 	card->num_rtd = 0;
38742eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3875f0fba2adSLiam Girdwood 
38765f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_links; i++) {
38775f3484acSLars-Peter Clausen 		card->rtd[i].card = card;
3878f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
387988bd870fSBenoit Cousson 		card->rtd[i].codec_dais = devm_kzalloc(card->dev,
388088bd870fSBenoit Cousson 					sizeof(struct snd_soc_dai *) *
388188bd870fSBenoit Cousson 					(card->rtd[i].dai_link->num_codecs),
388288bd870fSBenoit Cousson 					GFP_KERNEL);
388388bd870fSBenoit Cousson 		if (card->rtd[i].codec_dais == NULL)
388488bd870fSBenoit Cousson 			return -ENOMEM;
38855f3484acSLars-Peter Clausen 	}
38865f3484acSLars-Peter Clausen 
38875f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_aux_devs; i++)
38885f3484acSLars-Peter Clausen 		card->rtd_aux[i].card = card;
3889f0fba2adSLiam Girdwood 
3890db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3891c5af3a2eSMark Brown 	card->instantiated = 0;
3892f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3893a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3894c5af3a2eSMark Brown 
3895b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3896b19e6e7bSMark Brown 	if (ret != 0)
3897b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3898c5af3a2eSMark Brown 
3899988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
3900988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
390188bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
390288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
390388bd870fSBenoit Cousson 		int j;
390488bd870fSBenoit Cousson 
390588bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
390688bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3907988e8cc4SNicolin Chen 			if (!codec_dai->active)
3908988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
390988bd870fSBenoit Cousson 		}
391088bd870fSBenoit Cousson 
3911988e8cc4SNicolin Chen 		if (!cpu_dai->active)
3912988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
3913988e8cc4SNicolin Chen 	}
3914988e8cc4SNicolin Chen 
3915b19e6e7bSMark Brown 	return ret;
3916c5af3a2eSMark Brown }
391770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3918c5af3a2eSMark Brown 
3919c5af3a2eSMark Brown /**
3920c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3921c5af3a2eSMark Brown  *
3922ac11a2b3SMark Brown  * @card: Card to unregister
3923c5af3a2eSMark Brown  *
3924c5af3a2eSMark Brown  */
392570a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3926c5af3a2eSMark Brown {
3927b0e26485SVinod Koul 	if (card->instantiated)
3928b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3929f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3930c5af3a2eSMark Brown 
3931c5af3a2eSMark Brown 	return 0;
3932c5af3a2eSMark Brown }
393370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3934c5af3a2eSMark Brown 
3935f0fba2adSLiam Girdwood /*
3936f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3937f0fba2adSLiam Girdwood  * and sanitizing names.
3938f0fba2adSLiam Girdwood  */
39390b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3940f0fba2adSLiam Girdwood {
3941f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3942f0fba2adSLiam Girdwood 	int id1, id2;
3943f0fba2adSLiam Girdwood 
3944f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3945f0fba2adSLiam Girdwood 		return NULL;
3946f0fba2adSLiam Girdwood 
394758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3948f0fba2adSLiam Girdwood 
3949f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3950f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3951f0fba2adSLiam Girdwood 	if (found) {
3952f0fba2adSLiam Girdwood 		/* get ID */
3953f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3954f0fba2adSLiam Girdwood 
3955f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3956f0fba2adSLiam Girdwood 			if (*id == -1)
3957f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3958f0fba2adSLiam Girdwood 		}
3959f0fba2adSLiam Girdwood 
3960f0fba2adSLiam Girdwood 	} else {
3961f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3962f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3963f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3964f0fba2adSLiam Girdwood 
3965f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
396605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3967f0fba2adSLiam Girdwood 
3968f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3969f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
397058818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3971f0fba2adSLiam Girdwood 		} else
3972f0fba2adSLiam Girdwood 			*id = 0;
3973f0fba2adSLiam Girdwood 	}
3974f0fba2adSLiam Girdwood 
3975f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3976f0fba2adSLiam Girdwood }
3977f0fba2adSLiam Girdwood 
3978f0fba2adSLiam Girdwood /*
3979f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3980f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3981f0fba2adSLiam Girdwood  */
3982f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3983f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3984f0fba2adSLiam Girdwood {
3985f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
398610e8aa9aSMichał Mirosław 		dev_err(dev,
398710e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
398810e8aa9aSMichał Mirosław 			dev_name(dev));
3989f0fba2adSLiam Girdwood 		return NULL;
3990f0fba2adSLiam Girdwood 	}
3991f0fba2adSLiam Girdwood 
3992f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3993f0fba2adSLiam Girdwood }
3994f0fba2adSLiam Girdwood 
39959115171aSMark Brown /**
399632c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
39979115171aSMark Brown  *
399832c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
39999115171aSMark Brown  */
400032c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
40019115171aSMark Brown {
40025c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
40039115171aSMark Brown 
40045c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
400532c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
400632c9ba54SLars-Peter Clausen 			dai->name);
40079115171aSMark Brown 		list_del(&dai->list);
4008f0fba2adSLiam Girdwood 		kfree(dai->name);
4009f0fba2adSLiam Girdwood 		kfree(dai);
40109115171aSMark Brown 	}
401132c9ba54SLars-Peter Clausen }
40129115171aSMark Brown 
40139115171aSMark Brown /**
401432c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
40159115171aSMark Brown  *
40166106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
40176106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
4018ac11a2b3SMark Brown  * @count: Number of DAIs
401932c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
402032c9ba54SLars-Peter Clausen  *                     parent's name.
40219115171aSMark Brown  */
402232c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
4023bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
4024bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
40259115171aSMark Brown {
40266106d129SLars-Peter Clausen 	struct device *dev = component->dev;
4027f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
402832c9ba54SLars-Peter Clausen 	unsigned int i;
402932c9ba54SLars-Peter Clausen 	int ret;
4030f0fba2adSLiam Girdwood 
4031f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
40329115171aSMark Brown 
4033bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
4034bb13109dSLars-Peter Clausen 	component->num_dai = count;
4035bb13109dSLars-Peter Clausen 
40369115171aSMark Brown 	for (i = 0; i < count; i++) {
4037f0fba2adSLiam Girdwood 
4038f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
4039c46e0079SAxel Lin 		if (dai == NULL) {
4040c46e0079SAxel Lin 			ret = -ENOMEM;
4041c46e0079SAxel Lin 			goto err;
4042c46e0079SAxel Lin 		}
4043f0fba2adSLiam Girdwood 
404432c9ba54SLars-Peter Clausen 		/*
404532c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
404632c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
404732c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
404832c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
404932c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
405032c9ba54SLars-Peter Clausen 		 * component-less anymore.
405132c9ba54SLars-Peter Clausen 		 */
405232c9ba54SLars-Peter Clausen 		if (count == 1 && legacy_dai_naming) {
4053db2a4165SFrank Mandarino 			dai->name = fmt_single_name(dev, &dai->id);
405432c9ba54SLars-Peter Clausen 		} else {
4055f0fba2adSLiam Girdwood 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
405632c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
405732c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
405832c9ba54SLars-Peter Clausen 			else
405932c9ba54SLars-Peter Clausen 				dai->id = i;
406032c9ba54SLars-Peter Clausen 		}
4061f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
4062f0fba2adSLiam Girdwood 			kfree(dai);
406332c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
40649115171aSMark Brown 			goto err;
40659115171aSMark Brown 		}
40669115171aSMark Brown 
40676106d129SLars-Peter Clausen 		dai->component = component;
4068f0fba2adSLiam Girdwood 		dai->dev = dev;
4069f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
4070f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
4071f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
4072f0fba2adSLiam Girdwood 
40731438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
4074054880feSMark Brown 
40759115171aSMark Brown 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
4076f0fba2adSLiam Girdwood 	}
4077f0fba2adSLiam Girdwood 
40789115171aSMark Brown 	return 0;
40799115171aSMark Brown 
40809115171aSMark Brown err:
408132c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
40829115171aSMark Brown 
40839115171aSMark Brown 	return ret;
40849115171aSMark Brown }
40859115171aSMark Brown 
408614e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
408714e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
4088d191bd8dSKuninori Morimoto {
408914e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
4090d191bd8dSKuninori Morimoto 
409114e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
409214e8bdebSLars-Peter Clausen }
4093d191bd8dSKuninori Morimoto 
409414e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
409514e8bdebSLars-Peter Clausen 	int event)
409614e8bdebSLars-Peter Clausen {
409714e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
409814e8bdebSLars-Peter Clausen 
409914e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
410014e8bdebSLars-Peter Clausen }
410114e8bdebSLars-Peter Clausen 
4102bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
4103bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
4104d191bd8dSKuninori Morimoto {
4105ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
4106ce0fc93aSLars-Peter Clausen 
4107bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
4108bb13109dSLars-Peter Clausen 	if (!component->name) {
4109bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
4110d191bd8dSKuninori Morimoto 		return -ENOMEM;
4111d191bd8dSKuninori Morimoto 	}
4112d191bd8dSKuninori Morimoto 
4113bb13109dSLars-Peter Clausen 	component->dev = dev;
4114bb13109dSLars-Peter Clausen 	component->driver = driver;
4115e2c330b9SLars-Peter Clausen 
4116ce0fc93aSLars-Peter Clausen 	if (!component->dapm_ptr)
4117ce0fc93aSLars-Peter Clausen 		component->dapm_ptr = &component->dapm;
4118ce0fc93aSLars-Peter Clausen 
4119ce0fc93aSLars-Peter Clausen 	dapm = component->dapm_ptr;
4120ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
4121ce0fc93aSLars-Peter Clausen 	dapm->component = component;
4122ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
412314e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
412414e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
412514e8bdebSLars-Peter Clausen 	if (driver->stream_event)
412614e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
4127ce0fc93aSLars-Peter Clausen 
4128bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
4129bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
4130bb13109dSLars-Peter Clausen 
4131bb13109dSLars-Peter Clausen 	return 0;
4132d191bd8dSKuninori Morimoto }
4133d191bd8dSKuninori Morimoto 
4134bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4135bb13109dSLars-Peter Clausen {
4136bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
4137d191bd8dSKuninori Morimoto }
4138d191bd8dSKuninori Morimoto 
4139bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
4140bb13109dSLars-Peter Clausen {
4141d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
4142bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
4143d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
4144bb13109dSLars-Peter Clausen }
4145d191bd8dSKuninori Morimoto 
4146bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
4147bb13109dSLars-Peter Clausen {
4148bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
4149bb13109dSLars-Peter Clausen 	kfree(component->name);
4150bb13109dSLars-Peter Clausen }
4151d191bd8dSKuninori Morimoto 
4152bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4153bb13109dSLars-Peter Clausen {
4154bb13109dSLars-Peter Clausen 	list_del(&component->list);
4155bb13109dSLars-Peter Clausen }
4156d191bd8dSKuninori Morimoto 
4157bb13109dSLars-Peter Clausen static void snd_soc_component_del(struct snd_soc_component *component)
4158bb13109dSLars-Peter Clausen {
4159bb13109dSLars-Peter Clausen 	mutex_lock(&client_mutex);
4160bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(component);
4161bb13109dSLars-Peter Clausen 	mutex_unlock(&client_mutex);
4162d191bd8dSKuninori Morimoto }
4163d191bd8dSKuninori Morimoto 
4164d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
4165d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
4166d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
4167d191bd8dSKuninori Morimoto 			       int num_dai)
4168d191bd8dSKuninori Morimoto {
4169d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4170bb13109dSLars-Peter Clausen 	int ret;
4171d191bd8dSKuninori Morimoto 
4172bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4173d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4174d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
4175d191bd8dSKuninori Morimoto 		return -ENOMEM;
4176d191bd8dSKuninori Morimoto 	}
4177d191bd8dSKuninori Morimoto 
4178bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4179bb13109dSLars-Peter Clausen 	if (ret)
4180bb13109dSLars-Peter Clausen 		goto err_free;
4181bb13109dSLars-Peter Clausen 
41823d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
418398e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
41843d59400fSLars-Peter Clausen 
4185bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4186bb13109dSLars-Peter Clausen 	if (ret < 0) {
4187bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4188bb13109dSLars-Peter Clausen 		goto err_cleanup;
4189bb13109dSLars-Peter Clausen 	}
4190bb13109dSLars-Peter Clausen 
4191bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
4192bb13109dSLars-Peter Clausen 
4193bb13109dSLars-Peter Clausen 	return 0;
4194bb13109dSLars-Peter Clausen 
4195bb13109dSLars-Peter Clausen err_cleanup:
4196bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
4197bb13109dSLars-Peter Clausen err_free:
4198bb13109dSLars-Peter Clausen 	kfree(cmpnt);
4199bb13109dSLars-Peter Clausen 	return ret;
4200d191bd8dSKuninori Morimoto }
4201d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
4202d191bd8dSKuninori Morimoto 
4203d191bd8dSKuninori Morimoto /**
4204d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
4205d191bd8dSKuninori Morimoto  *
4206d191bd8dSKuninori Morimoto  */
4207d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
4208d191bd8dSKuninori Morimoto {
4209d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4210d191bd8dSKuninori Morimoto 
4211d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
421298e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
4213d191bd8dSKuninori Morimoto 			goto found;
4214d191bd8dSKuninori Morimoto 	}
4215d191bd8dSKuninori Morimoto 	return;
4216d191bd8dSKuninori Morimoto 
4217d191bd8dSKuninori Morimoto found:
4218bb13109dSLars-Peter Clausen 	snd_soc_component_del(cmpnt);
4219bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
4220bb13109dSLars-Peter Clausen 	kfree(cmpnt);
4221d191bd8dSKuninori Morimoto }
4222d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4223d191bd8dSKuninori Morimoto 
4224e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4225e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
4226e2c330b9SLars-Peter Clausen {
4227e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4228e2c330b9SLars-Peter Clausen 
4229e2c330b9SLars-Peter Clausen 	return platform->driver->write(platform, reg, val);
4230e2c330b9SLars-Peter Clausen }
4231e2c330b9SLars-Peter Clausen 
4232e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4233e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
4234e2c330b9SLars-Peter Clausen {
4235e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4236e2c330b9SLars-Peter Clausen 
4237e2c330b9SLars-Peter Clausen 	*val = platform->driver->read(platform, reg);
4238e2c330b9SLars-Peter Clausen 
4239e2c330b9SLars-Peter Clausen 	return 0;
4240e2c330b9SLars-Peter Clausen }
4241e2c330b9SLars-Peter Clausen 
4242d191bd8dSKuninori Morimoto /**
424371a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
424471a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
424571a45cdaSLars-Peter Clausen  * @platform: The platform to add
424671a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
424712a48a8cSMark Brown  */
424871a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4249d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
425012a48a8cSMark Brown {
4251b37f1d12SLars-Peter Clausen 	int ret;
4252b37f1d12SLars-Peter Clausen 
4253bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
4254bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
4255bb13109dSLars-Peter Clausen 	if (ret)
4256bb13109dSLars-Peter Clausen 		return ret;
4257f0fba2adSLiam Girdwood 
4258f0fba2adSLiam Girdwood 	platform->dev = dev;
4259f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
4260e2c330b9SLars-Peter Clausen 	if (platform_drv->write)
4261e2c330b9SLars-Peter Clausen 		platform->component.write = snd_soc_platform_drv_write;
4262e2c330b9SLars-Peter Clausen 	if (platform_drv->read)
4263e2c330b9SLars-Peter Clausen 		platform->component.read = snd_soc_platform_drv_read;
426412a48a8cSMark Brown 
426512a48a8cSMark Brown 	mutex_lock(&client_mutex);
4266bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
426712a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
426812a48a8cSMark Brown 	mutex_unlock(&client_mutex);
426912a48a8cSMark Brown 
4270f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4271f4333203SLars-Peter Clausen 		platform->component.name);
427212a48a8cSMark Brown 
427312a48a8cSMark Brown 	return 0;
427412a48a8cSMark Brown }
427571a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
427671a45cdaSLars-Peter Clausen 
427771a45cdaSLars-Peter Clausen /**
427871a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
427971a45cdaSLars-Peter Clausen  *
428071a45cdaSLars-Peter Clausen  * @platform: platform to register
428171a45cdaSLars-Peter Clausen  */
428271a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
428371a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
428471a45cdaSLars-Peter Clausen {
428571a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
428671a45cdaSLars-Peter Clausen 	int ret;
428771a45cdaSLars-Peter Clausen 
428871a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
428971a45cdaSLars-Peter Clausen 
429071a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
429171a45cdaSLars-Peter Clausen 	if (platform == NULL)
429271a45cdaSLars-Peter Clausen 		return -ENOMEM;
429371a45cdaSLars-Peter Clausen 
429471a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
429571a45cdaSLars-Peter Clausen 	if (ret)
429671a45cdaSLars-Peter Clausen 		kfree(platform);
429771a45cdaSLars-Peter Clausen 
429871a45cdaSLars-Peter Clausen 	return ret;
429971a45cdaSLars-Peter Clausen }
430012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
430112a48a8cSMark Brown 
430212a48a8cSMark Brown /**
430371a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
430471a45cdaSLars-Peter Clausen  * @platform: the platform to remove
430571a45cdaSLars-Peter Clausen  */
430671a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
430771a45cdaSLars-Peter Clausen {
4308b37f1d12SLars-Peter Clausen 
430971a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
431071a45cdaSLars-Peter Clausen 	list_del(&platform->list);
4311bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
431271a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
431371a45cdaSLars-Peter Clausen 
4314bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&platform->component);
4315bb13109dSLars-Peter Clausen 
431671a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4317f4333203SLars-Peter Clausen 		platform->component.name);
431871a45cdaSLars-Peter Clausen }
431971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
432071a45cdaSLars-Peter Clausen 
432171a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
432271a45cdaSLars-Peter Clausen {
432371a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
432471a45cdaSLars-Peter Clausen 
432571a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
432671a45cdaSLars-Peter Clausen 		if (dev == platform->dev)
432771a45cdaSLars-Peter Clausen 			return platform;
432871a45cdaSLars-Peter Clausen 	}
432971a45cdaSLars-Peter Clausen 
433071a45cdaSLars-Peter Clausen 	return NULL;
433171a45cdaSLars-Peter Clausen }
433271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
433371a45cdaSLars-Peter Clausen 
433471a45cdaSLars-Peter Clausen /**
433512a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
433612a48a8cSMark Brown  *
4337ac11a2b3SMark Brown  * @platform: platform to unregister
433812a48a8cSMark Brown  */
4339f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
434012a48a8cSMark Brown {
4341f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
4342f0fba2adSLiam Girdwood 
434371a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
434471a45cdaSLars-Peter Clausen 	if (!platform)
4345f0fba2adSLiam Girdwood 		return;
4346f0fba2adSLiam Girdwood 
434771a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
4348f0fba2adSLiam Girdwood 	kfree(platform);
434912a48a8cSMark Brown }
435012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
435112a48a8cSMark Brown 
4352151ab22cSMark Brown static u64 codec_format_map[] = {
4353151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4354151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4355151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4356151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4357151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4358151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4359151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4360151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4361151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4362151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4363151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4364151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4365151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4366151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4367151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4368151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4369151ab22cSMark Brown };
4370151ab22cSMark Brown 
4371151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
4372151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
4373151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
4374151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
4375151ab22cSMark Brown  */
4376151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4377151ab22cSMark Brown {
4378151ab22cSMark Brown 	int i;
4379151ab22cSMark Brown 
4380151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4381151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
4382151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
4383151ab22cSMark Brown }
4384151ab22cSMark Brown 
4385e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4386e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
4387e2c330b9SLars-Peter Clausen {
4388e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4389e2c330b9SLars-Peter Clausen 
4390e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
4391e2c330b9SLars-Peter Clausen }
4392e2c330b9SLars-Peter Clausen 
4393e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4394e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
4395e2c330b9SLars-Peter Clausen {
4396e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4397e2c330b9SLars-Peter Clausen 
4398e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
4399e2c330b9SLars-Peter Clausen 
4400e2c330b9SLars-Peter Clausen 	return 0;
4401e2c330b9SLars-Peter Clausen }
4402e2c330b9SLars-Peter Clausen 
440368f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
440468f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
440568f831c2SLars-Peter Clausen {
440668f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
440768f831c2SLars-Peter Clausen 
440868f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
440968f831c2SLars-Peter Clausen }
441068f831c2SLars-Peter Clausen 
44110d0cf00aSMark Brown /**
44120d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
44130d0cf00aSMark Brown  *
4414ac11a2b3SMark Brown  * @codec: codec to register
44150d0cf00aSMark Brown  */
4416f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
4417001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
4418001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
4419001ae4c0SMark Brown 			   int num_dai)
44200d0cf00aSMark Brown {
4421f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4422bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
4423a39f75f7SXiubo Li 	struct regmap *regmap;
4424f0fba2adSLiam Girdwood 	int ret, i;
4425151ab22cSMark Brown 
4426f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
44270d0cf00aSMark Brown 
4428f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4429f0fba2adSLiam Girdwood 	if (codec == NULL)
4430f0fba2adSLiam Girdwood 		return -ENOMEM;
44310d0cf00aSMark Brown 
4432ce0fc93aSLars-Peter Clausen 	codec->component.dapm_ptr = &codec->dapm;
4433ce0fc93aSLars-Peter Clausen 
4434bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
4435bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
4436bb13109dSLars-Peter Clausen 	if (ret)
4437bb13109dSLars-Peter Clausen 		goto err_free;
4438151ab22cSMark Brown 
4439e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
4440e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
4441e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
4442e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
44433d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4444ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
444514e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
4446474b62d6SMark Brown 		codec->dapm.seq_notifier = codec_drv->seq_notifier;
444768f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
444868f831c2SLars-Peter Clausen 		codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4449f0fba2adSLiam Girdwood 	codec->dev = dev;
4450f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
4451e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
4452f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
4453f0fba2adSLiam Girdwood 
4454e2c330b9SLars-Peter Clausen 	if (!codec->component.write) {
4455a39f75f7SXiubo Li 		if (codec_drv->get_regmap)
4456a39f75f7SXiubo Li 			regmap = codec_drv->get_regmap(dev);
4457a39f75f7SXiubo Li 		else
4458a39f75f7SXiubo Li 			regmap = dev_get_regmap(dev, NULL);
4459a39f75f7SXiubo Li 
4460a39f75f7SXiubo Li 		if (regmap) {
4461e2c330b9SLars-Peter Clausen 			ret = snd_soc_component_init_io(&codec->component,
4462e2c330b9SLars-Peter Clausen 				regmap);
4463e2c330b9SLars-Peter Clausen 			if (ret) {
4464a39f75f7SXiubo Li 				dev_err(codec->dev,
4465a39f75f7SXiubo Li 						"Failed to set cache I/O:%d\n",
4466a39f75f7SXiubo Li 						ret);
4467bb13109dSLars-Peter Clausen 				goto err_cleanup;
4468a39f75f7SXiubo Li 			}
4469a39f75f7SXiubo Li 		}
4470a39f75f7SXiubo Li 	}
4471a39f75f7SXiubo Li 
4472f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
4473f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
4474f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
4475f0fba2adSLiam Girdwood 	}
4476f0fba2adSLiam Girdwood 
4477bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4478bb13109dSLars-Peter Clausen 	if (ret < 0) {
4479bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4480bb13109dSLars-Peter Clausen 		goto err_cleanup;
4481bb13109dSLars-Peter Clausen 	}
4482bb13109dSLars-Peter Clausen 
4483bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
4484bb13109dSLars-Peter Clausen 		dai->codec = codec;
4485bb13109dSLars-Peter Clausen 
4486054880feSMark Brown 	mutex_lock(&client_mutex);
4487bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
4488054880feSMark Brown 	list_add(&codec->list, &codec_list);
4489054880feSMark Brown 	mutex_unlock(&client_mutex);
4490054880feSMark Brown 
4491f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4492f4333203SLars-Peter Clausen 		codec->component.name);
44930d0cf00aSMark Brown 	return 0;
4494f0fba2adSLiam Girdwood 
4495bb13109dSLars-Peter Clausen err_cleanup:
4496bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
4497bb13109dSLars-Peter Clausen err_free:
4498f0fba2adSLiam Girdwood 	kfree(codec);
4499f0fba2adSLiam Girdwood 	return ret;
45000d0cf00aSMark Brown }
45010d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
45020d0cf00aSMark Brown 
45030d0cf00aSMark Brown /**
45040d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
45050d0cf00aSMark Brown  *
4506ac11a2b3SMark Brown  * @codec: codec to unregister
45070d0cf00aSMark Brown  */
4508f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
45090d0cf00aSMark Brown {
4510f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4511f0fba2adSLiam Girdwood 
4512f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
4513f0fba2adSLiam Girdwood 		if (dev == codec->dev)
4514f0fba2adSLiam Girdwood 			goto found;
4515f0fba2adSLiam Girdwood 	}
4516f0fba2adSLiam Girdwood 	return;
4517f0fba2adSLiam Girdwood 
4518f0fba2adSLiam Girdwood found:
4519f0fba2adSLiam Girdwood 
45200d0cf00aSMark Brown 	mutex_lock(&client_mutex);
45210d0cf00aSMark Brown 	list_del(&codec->list);
4522bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
45230d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
45240d0cf00aSMark Brown 
4525f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4526f4333203SLars-Peter Clausen 			codec->component.name);
4527f0fba2adSLiam Girdwood 
4528bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
45297a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
4530f0fba2adSLiam Girdwood 	kfree(codec);
45310d0cf00aSMark Brown }
45320d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
45330d0cf00aSMark Brown 
4534bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4535bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4536bec4fa05SStephen Warren 			       const char *propname)
4537bec4fa05SStephen Warren {
45387e07e7c0STushar Behera 	struct device_node *np;
4539bec4fa05SStephen Warren 	int ret;
4540bec4fa05SStephen Warren 
45417e07e7c0STushar Behera 	if (!card->dev) {
45427e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
45437e07e7c0STushar Behera 		return -EINVAL;
45447e07e7c0STushar Behera 	}
45457e07e7c0STushar Behera 
45467e07e7c0STushar Behera 	np = card->dev->of_node;
45477e07e7c0STushar Behera 
4548bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4549bec4fa05SStephen Warren 	/*
4550bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4551bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4552bec4fa05SStephen Warren 	 * snd_soc_register_card.
4553bec4fa05SStephen Warren 	 */
4554bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4555bec4fa05SStephen Warren 		dev_err(card->dev,
4556f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
4557bec4fa05SStephen Warren 			propname, ret);
4558bec4fa05SStephen Warren 		return ret;
4559bec4fa05SStephen Warren 	}
4560bec4fa05SStephen Warren 
4561bec4fa05SStephen Warren 	return 0;
4562bec4fa05SStephen Warren }
4563bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4564bec4fa05SStephen Warren 
45659a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
45669a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
45679a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
45689a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
45699a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
45709a6d4860SXiubo Li };
45719a6d4860SXiubo Li 
45729a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
45739a6d4860SXiubo Li 					  const char *propname)
45749a6d4860SXiubo Li {
45759a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
45769a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
45779a6d4860SXiubo Li 	const char *template, *wname;
45789a6d4860SXiubo Li 	int i, j, num_widgets, ret;
45799a6d4860SXiubo Li 
45809a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
45819a6d4860SXiubo Li 	if (num_widgets < 0) {
45829a6d4860SXiubo Li 		dev_err(card->dev,
45839a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
45849a6d4860SXiubo Li 		return -EINVAL;
45859a6d4860SXiubo Li 	}
45869a6d4860SXiubo Li 	if (num_widgets & 1) {
45879a6d4860SXiubo Li 		dev_err(card->dev,
45889a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
45899a6d4860SXiubo Li 		return -EINVAL;
45909a6d4860SXiubo Li 	}
45919a6d4860SXiubo Li 
45929a6d4860SXiubo Li 	num_widgets /= 2;
45939a6d4860SXiubo Li 	if (!num_widgets) {
45949a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
45959a6d4860SXiubo Li 			propname);
45969a6d4860SXiubo Li 		return -EINVAL;
45979a6d4860SXiubo Li 	}
45989a6d4860SXiubo Li 
45999a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
46009a6d4860SXiubo Li 			       GFP_KERNEL);
46019a6d4860SXiubo Li 	if (!widgets) {
46029a6d4860SXiubo Li 		dev_err(card->dev,
46039a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
46049a6d4860SXiubo Li 		return -ENOMEM;
46059a6d4860SXiubo Li 	}
46069a6d4860SXiubo Li 
46079a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
46089a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
46099a6d4860SXiubo Li 			2 * i, &template);
46109a6d4860SXiubo Li 		if (ret) {
46119a6d4860SXiubo Li 			dev_err(card->dev,
46129a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
46139a6d4860SXiubo Li 				propname, 2 * i, ret);
46149a6d4860SXiubo Li 			return -EINVAL;
46159a6d4860SXiubo Li 		}
46169a6d4860SXiubo Li 
46179a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
46189a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
46199a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
46209a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
46219a6d4860SXiubo Li 				break;
46229a6d4860SXiubo Li 			}
46239a6d4860SXiubo Li 		}
46249a6d4860SXiubo Li 
46259a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
46269a6d4860SXiubo Li 			dev_err(card->dev,
46279a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
46289a6d4860SXiubo Li 				template);
46299a6d4860SXiubo Li 			return -EINVAL;
46309a6d4860SXiubo Li 		}
46319a6d4860SXiubo Li 
46329a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
46339a6d4860SXiubo Li 						    (2 * i) + 1,
46349a6d4860SXiubo Li 						    &wname);
46359a6d4860SXiubo Li 		if (ret) {
46369a6d4860SXiubo Li 			dev_err(card->dev,
46379a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
46389a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
46399a6d4860SXiubo Li 			return -EINVAL;
46409a6d4860SXiubo Li 		}
46419a6d4860SXiubo Li 
46429a6d4860SXiubo Li 		widgets[i].name = wname;
46439a6d4860SXiubo Li 	}
46449a6d4860SXiubo Li 
46459a6d4860SXiubo Li 	card->dapm_widgets = widgets;
46469a6d4860SXiubo Li 	card->num_dapm_widgets = num_widgets;
46479a6d4860SXiubo Li 
46489a6d4860SXiubo Li 	return 0;
46499a6d4860SXiubo Li }
46509a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
46519a6d4860SXiubo Li 
465289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
465389c67857SXiubo Li 			      unsigned int *slots,
465489c67857SXiubo Li 			      unsigned int *slot_width)
465589c67857SXiubo Li {
465689c67857SXiubo Li 	u32 val;
465789c67857SXiubo Li 	int ret;
465889c67857SXiubo Li 
465989c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
466089c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
466189c67857SXiubo Li 		if (ret)
466289c67857SXiubo Li 			return ret;
466389c67857SXiubo Li 
466489c67857SXiubo Li 		if (slots)
466589c67857SXiubo Li 			*slots = val;
466689c67857SXiubo Li 	}
466789c67857SXiubo Li 
466889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
466989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
467089c67857SXiubo Li 		if (ret)
467189c67857SXiubo Li 			return ret;
467289c67857SXiubo Li 
467389c67857SXiubo Li 		if (slot_width)
467489c67857SXiubo Li 			*slot_width = val;
467589c67857SXiubo Li 	}
467689c67857SXiubo Li 
467789c67857SXiubo Li 	return 0;
467889c67857SXiubo Li }
467989c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
468089c67857SXiubo Li 
4681a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4682a4a54dd5SStephen Warren 				   const char *propname)
4683a4a54dd5SStephen Warren {
4684a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4685a4a54dd5SStephen Warren 	int num_routes;
4686a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4687a4a54dd5SStephen Warren 	int i, ret;
4688a4a54dd5SStephen Warren 
4689a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4690c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
469110e8aa9aSMichał Mirosław 		dev_err(card->dev,
469210e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
469310e8aa9aSMichał Mirosław 			propname);
4694a4a54dd5SStephen Warren 		return -EINVAL;
4695a4a54dd5SStephen Warren 	}
4696a4a54dd5SStephen Warren 	num_routes /= 2;
4697a4a54dd5SStephen Warren 	if (!num_routes) {
4698f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4699a4a54dd5SStephen Warren 			propname);
4700a4a54dd5SStephen Warren 		return -EINVAL;
4701a4a54dd5SStephen Warren 	}
4702a4a54dd5SStephen Warren 
4703a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4704a4a54dd5SStephen Warren 			      GFP_KERNEL);
4705a4a54dd5SStephen Warren 	if (!routes) {
4706a4a54dd5SStephen Warren 		dev_err(card->dev,
4707f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4708a4a54dd5SStephen Warren 		return -EINVAL;
4709a4a54dd5SStephen Warren 	}
4710a4a54dd5SStephen Warren 
4711a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4712a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4713a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4714a4a54dd5SStephen Warren 		if (ret) {
4715c871bd0bSMark Brown 			dev_err(card->dev,
4716c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4717c871bd0bSMark Brown 				propname, 2 * i, ret);
4718a4a54dd5SStephen Warren 			return -EINVAL;
4719a4a54dd5SStephen Warren 		}
4720a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4721a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4722a4a54dd5SStephen Warren 		if (ret) {
4723a4a54dd5SStephen Warren 			dev_err(card->dev,
4724c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4725c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4726a4a54dd5SStephen Warren 			return -EINVAL;
4727a4a54dd5SStephen Warren 		}
4728a4a54dd5SStephen Warren 	}
4729a4a54dd5SStephen Warren 
4730a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4731a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4732a4a54dd5SStephen Warren 
4733a4a54dd5SStephen Warren 	return 0;
4734a4a54dd5SStephen Warren }
4735a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4736a4a54dd5SStephen Warren 
4737a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4738389cb834SJyri Sarha 				     const char *prefix,
4739389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
4740389cb834SJyri Sarha 				     struct device_node **framemaster)
4741a7930ed4SKuninori Morimoto {
4742a7930ed4SKuninori Morimoto 	int ret, i;
4743a7930ed4SKuninori Morimoto 	char prop[128];
4744a7930ed4SKuninori Morimoto 	unsigned int format = 0;
4745a7930ed4SKuninori Morimoto 	int bit, frame;
4746a7930ed4SKuninori Morimoto 	const char *str;
4747a7930ed4SKuninori Morimoto 	struct {
4748a7930ed4SKuninori Morimoto 		char *name;
4749a7930ed4SKuninori Morimoto 		unsigned int val;
4750a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
4751a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
4752a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
4753a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
4754a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
4755a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
4756a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
4757a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
4758a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
4759a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
4760a7930ed4SKuninori Morimoto 	};
4761a7930ed4SKuninori Morimoto 
4762a7930ed4SKuninori Morimoto 	if (!prefix)
4763a7930ed4SKuninori Morimoto 		prefix = "";
4764a7930ed4SKuninori Morimoto 
4765a7930ed4SKuninori Morimoto 	/*
4766a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
4767a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4768a7930ed4SKuninori Morimoto 	 */
4769a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
4770a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
4771a7930ed4SKuninori Morimoto 	if (ret == 0) {
4772a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4773a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4774a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4775a7930ed4SKuninori Morimoto 				break;
4776a7930ed4SKuninori Morimoto 			}
4777a7930ed4SKuninori Morimoto 		}
4778a7930ed4SKuninori Morimoto 	}
4779a7930ed4SKuninori Morimoto 
4780a7930ed4SKuninori Morimoto 	/*
47818c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4782a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4783a7930ed4SKuninori Morimoto 	 */
47848c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
47858c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
47868c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
47878c2d6a9fSKuninori Morimoto 	else
47888c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4789a7930ed4SKuninori Morimoto 
4790a7930ed4SKuninori Morimoto 	/*
4791a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4792a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4793a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4794a7930ed4SKuninori Morimoto 	 */
4795a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4796a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4797a7930ed4SKuninori Morimoto 
4798a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4799a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4800a7930ed4SKuninori Morimoto 
4801a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4802a7930ed4SKuninori Morimoto 	case 0x11:
4803a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4804a7930ed4SKuninori Morimoto 		break;
4805a7930ed4SKuninori Morimoto 	case 0x10:
4806a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4807a7930ed4SKuninori Morimoto 		break;
4808a7930ed4SKuninori Morimoto 	case 0x01:
4809a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4810a7930ed4SKuninori Morimoto 		break;
4811a7930ed4SKuninori Morimoto 	default:
4812a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4813a7930ed4SKuninori Morimoto 		break;
4814a7930ed4SKuninori Morimoto 	}
4815a7930ed4SKuninori Morimoto 
4816a7930ed4SKuninori Morimoto 	/*
4817a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4818a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4819a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4820a7930ed4SKuninori Morimoto 	 */
4821a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4822a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4823389cb834SJyri Sarha 	if (bit && bitclkmaster)
4824389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4825a7930ed4SKuninori Morimoto 
4826a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4827a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4828389cb834SJyri Sarha 	if (frame && framemaster)
4829389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4830a7930ed4SKuninori Morimoto 
4831a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4832a7930ed4SKuninori Morimoto 	case 0x11:
4833a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4834a7930ed4SKuninori Morimoto 		break;
4835a7930ed4SKuninori Morimoto 	case 0x10:
4836a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4837a7930ed4SKuninori Morimoto 		break;
4838a7930ed4SKuninori Morimoto 	case 0x01:
4839a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4840a7930ed4SKuninori Morimoto 		break;
4841a7930ed4SKuninori Morimoto 	default:
4842a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4843a7930ed4SKuninori Morimoto 		break;
4844a7930ed4SKuninori Morimoto 	}
4845a7930ed4SKuninori Morimoto 
4846a7930ed4SKuninori Morimoto 	return format;
4847a7930ed4SKuninori Morimoto }
4848a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4849a7930ed4SKuninori Morimoto 
4850cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node,
4851cb470087SKuninori Morimoto 			    const char **dai_name)
4852cb470087SKuninori Morimoto {
4853cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
4854cb470087SKuninori Morimoto 	struct of_phandle_args args;
4855cb470087SKuninori Morimoto 	int ret;
4856cb470087SKuninori Morimoto 
4857cb470087SKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
4858cb470087SKuninori Morimoto 					 "#sound-dai-cells", 0, &args);
4859cb470087SKuninori Morimoto 	if (ret)
4860cb470087SKuninori Morimoto 		return ret;
4861cb470087SKuninori Morimoto 
4862cb470087SKuninori Morimoto 	ret = -EPROBE_DEFER;
4863cb470087SKuninori Morimoto 
4864cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4865cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4866cb470087SKuninori Morimoto 		if (pos->dev->of_node != args.np)
4867cb470087SKuninori Morimoto 			continue;
4868cb470087SKuninori Morimoto 
48696833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
48706833c452SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
48716833c452SKuninori Morimoto 		} else {
48726833c452SKuninori Morimoto 			int id = -1;
48736833c452SKuninori Morimoto 
48746833c452SKuninori Morimoto 			switch (args.args_count) {
48756833c452SKuninori Morimoto 			case 0:
48766833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
48776833c452SKuninori Morimoto 				break;
48786833c452SKuninori Morimoto 			case 1:
48796833c452SKuninori Morimoto 				id = args.args[0];
48806833c452SKuninori Morimoto 				break;
48816833c452SKuninori Morimoto 			default:
48826833c452SKuninori Morimoto 				/* not supported */
4883cb470087SKuninori Morimoto 				break;
4884cb470087SKuninori Morimoto 			}
4885cb470087SKuninori Morimoto 
48866833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
48876833c452SKuninori Morimoto 				ret = -EINVAL;
48883dcba280SNicolin Chen 				continue;
48896833c452SKuninori Morimoto 			}
4890e41975edSXiubo Li 
4891e41975edSXiubo Li 			ret = 0;
4892e41975edSXiubo Li 
4893e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4894e41975edSXiubo Li 			if (!*dai_name)
4895e41975edSXiubo Li 				*dai_name = pos->name;
48966833c452SKuninori Morimoto 		}
48976833c452SKuninori Morimoto 
4898cb470087SKuninori Morimoto 		break;
4899cb470087SKuninori Morimoto 	}
4900cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
4901cb470087SKuninori Morimoto 
4902cb470087SKuninori Morimoto 	of_node_put(args.np);
4903cb470087SKuninori Morimoto 
4904cb470087SKuninori Morimoto 	return ret;
4905cb470087SKuninori Morimoto }
4906cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4907cb470087SKuninori Morimoto 
4908c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4909db2a4165SFrank Mandarino {
4910db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
49118a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
49128a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
49130837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
49148a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4915db2a4165SFrank Mandarino 	}
4916c3c5a19aSMark Brown 
49178a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4918c3c5a19aSMark Brown 				 &codec_list_fops))
4919c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4920c3c5a19aSMark Brown 
49218a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4922f3208780SMark Brown 				 &dai_list_fops))
4923f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
492419c7ac27SMark Brown 
49258a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
492619c7ac27SMark Brown 				 &platform_list_fops))
492719c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4928db2a4165SFrank Mandarino #endif
4929db2a4165SFrank Mandarino 
4930fb257897SMark Brown 	snd_soc_util_init();
4931fb257897SMark Brown 
4932db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4933db2a4165SFrank Mandarino }
49344abe8e16SMark Brown module_init(snd_soc_init);
4935db2a4165SFrank Mandarino 
4936db2a4165SFrank Mandarino static void __exit snd_soc_exit(void)
4937db2a4165SFrank Mandarino {
4938fb257897SMark Brown 	snd_soc_util_exit();
4939fb257897SMark Brown 
4940db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
49418a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4942db2a4165SFrank Mandarino #endif
4943db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4944db2a4165SFrank Mandarino }
4945db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4946db2a4165SFrank Mandarino 
4947db2a4165SFrank Mandarino /* Module information */
4948db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4949db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4950db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4951db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4952