xref: /openbmc/linux/sound/soc/soc-core.c (revision 70090bbb8b7d7da7a6f64969b43a61c493c560ff)
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 
27381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
274e73f3de5SRussell King {
27581c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
27681c7cfd1SLars-Peter Clausen 		char *name;
277e73f3de5SRussell King 
27881c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
27981c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
28081c7cfd1SLars-Peter Clausen 		if (name) {
28181c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
28281c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
28381c7cfd1SLars-Peter Clausen 			kfree(name);
28481c7cfd1SLars-Peter Clausen 		}
28581c7cfd1SLars-Peter Clausen 	} else {
28681c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
28781c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
288e73f3de5SRussell King 	}
289e73f3de5SRussell King 
29081c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
29181c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
29281c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
2932624d5faSMark Brown 		return;
2942624d5faSMark Brown 	}
2952624d5faSMark Brown 
29681c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
29781c7cfd1SLars-Peter Clausen 		component->debugfs_root);
29881c7cfd1SLars-Peter Clausen 
29981c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
30081c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
30181c7cfd1SLars-Peter Clausen }
30281c7cfd1SLars-Peter Clausen 
30381c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
30481c7cfd1SLars-Peter Clausen {
30581c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
30681c7cfd1SLars-Peter Clausen }
30781c7cfd1SLars-Peter Clausen 
30881c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
30981c7cfd1SLars-Peter Clausen {
31081c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
31181c7cfd1SLars-Peter Clausen 
31281c7cfd1SLars-Peter Clausen 	debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
313aaee8ef1SMark Brown 			    &codec->cache_sync);
31481c7cfd1SLars-Peter Clausen 	debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
315aaee8ef1SMark Brown 			    &codec->cache_only);
316aaee8ef1SMark Brown 
3172624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
31881c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3192624d5faSMark Brown 						 codec, &codec_reg_fops);
3202624d5faSMark Brown 	if (!codec->debugfs_reg)
32110e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
32210e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
323731f1ab2SSebastien Guiriec }
324731f1ab2SSebastien Guiriec 
325c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
327c3c5a19aSMark Brown {
328c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3292b194f9dSMark Brown 	ssize_t len, ret = 0;
330c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
331c3c5a19aSMark Brown 
332c3c5a19aSMark Brown 	if (!buf)
333c3c5a19aSMark Brown 		return -ENOMEM;
334c3c5a19aSMark Brown 
3352b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3362b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
337f4333203SLars-Peter Clausen 			       codec->component.name);
3382b194f9dSMark Brown 		if (len >= 0)
3392b194f9dSMark Brown 			ret += len;
3402b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3412b194f9dSMark Brown 			ret = PAGE_SIZE;
3422b194f9dSMark Brown 			break;
3432b194f9dSMark Brown 		}
3442b194f9dSMark Brown 	}
345c3c5a19aSMark Brown 
346c3c5a19aSMark Brown 	if (ret >= 0)
347c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348c3c5a19aSMark Brown 
349c3c5a19aSMark Brown 	kfree(buf);
350c3c5a19aSMark Brown 
351c3c5a19aSMark Brown 	return ret;
352c3c5a19aSMark Brown }
353c3c5a19aSMark Brown 
354c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
355c3c5a19aSMark Brown 	.read = codec_list_read_file,
356c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
357c3c5a19aSMark Brown };
358c3c5a19aSMark Brown 
359f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360f3208780SMark Brown 				  size_t count, loff_t *ppos)
361f3208780SMark Brown {
362f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3632b194f9dSMark Brown 	ssize_t len, ret = 0;
3641438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
365f3208780SMark Brown 	struct snd_soc_dai *dai;
366f3208780SMark Brown 
367f3208780SMark Brown 	if (!buf)
368f3208780SMark Brown 		return -ENOMEM;
369f3208780SMark Brown 
3701438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3711438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3721438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3731438c2f6SLars-Peter Clausen 				dai->name);
3742b194f9dSMark Brown 			if (len >= 0)
3752b194f9dSMark Brown 				ret += len;
3762b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3772b194f9dSMark Brown 				ret = PAGE_SIZE;
3782b194f9dSMark Brown 				break;
3792b194f9dSMark Brown 			}
3802b194f9dSMark Brown 		}
3811438c2f6SLars-Peter Clausen 	}
382f3208780SMark Brown 
383f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
384f3208780SMark Brown 
385f3208780SMark Brown 	kfree(buf);
386f3208780SMark Brown 
387f3208780SMark Brown 	return ret;
388f3208780SMark Brown }
389f3208780SMark Brown 
390f3208780SMark Brown static const struct file_operations dai_list_fops = {
391f3208780SMark Brown 	.read = dai_list_read_file,
392f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
393f3208780SMark Brown };
394f3208780SMark Brown 
39519c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
39619c7ac27SMark Brown 				       char __user *user_buf,
39719c7ac27SMark Brown 				       size_t count, loff_t *ppos)
39819c7ac27SMark Brown {
39919c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4002b194f9dSMark Brown 	ssize_t len, ret = 0;
40119c7ac27SMark Brown 	struct snd_soc_platform *platform;
40219c7ac27SMark Brown 
40319c7ac27SMark Brown 	if (!buf)
40419c7ac27SMark Brown 		return -ENOMEM;
40519c7ac27SMark Brown 
4062b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4072b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
408f4333203SLars-Peter Clausen 			       platform->component.name);
4092b194f9dSMark Brown 		if (len >= 0)
4102b194f9dSMark Brown 			ret += len;
4112b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4122b194f9dSMark Brown 			ret = PAGE_SIZE;
4132b194f9dSMark Brown 			break;
4142b194f9dSMark Brown 		}
4152b194f9dSMark Brown 	}
41619c7ac27SMark Brown 
41719c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
41819c7ac27SMark Brown 
41919c7ac27SMark Brown 	kfree(buf);
42019c7ac27SMark Brown 
42119c7ac27SMark Brown 	return ret;
42219c7ac27SMark Brown }
42319c7ac27SMark Brown 
42419c7ac27SMark Brown static const struct file_operations platform_list_fops = {
42519c7ac27SMark Brown 	.read = platform_list_read_file,
42619c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
42719c7ac27SMark Brown };
42819c7ac27SMark Brown 
429a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
430a6052154SJarkko Nikula {
431a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4328a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4333a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
434a6052154SJarkko Nikula 		dev_warn(card->dev,
4357c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4363a45b867SJarkko Nikula 		return;
4373a45b867SJarkko Nikula 	}
4383a45b867SJarkko Nikula 
4393a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4403a45b867SJarkko Nikula 						    card->debugfs_card_root,
4413a45b867SJarkko Nikula 						    &card->pop_time);
4423a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4433a45b867SJarkko Nikula 		dev_warn(card->dev,
444f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
445a6052154SJarkko Nikula }
446a6052154SJarkko Nikula 
447a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448a6052154SJarkko Nikula {
449a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
450a6052154SJarkko Nikula }
451a6052154SJarkko Nikula 
4522624d5faSMark Brown #else
4532624d5faSMark Brown 
45481c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
45581c7cfd1SLars-Peter Clausen 
45681c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
45781c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
4582624d5faSMark Brown {
4592624d5faSMark Brown }
4602624d5faSMark Brown 
46181c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
46281c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
463731f1ab2SSebastien Guiriec {
464731f1ab2SSebastien Guiriec }
465731f1ab2SSebastien Guiriec 
466b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467b95fccbcSAxel Lin {
468b95fccbcSAxel Lin }
469b95fccbcSAxel Lin 
470b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471b95fccbcSAxel Lin {
472b95fccbcSAxel Lin }
4732624d5faSMark Brown #endif
4742624d5faSMark Brown 
47547c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
47647c88fffSLiam Girdwood 		const char *dai_link, int stream)
47747c88fffSLiam Girdwood {
47847c88fffSLiam Girdwood 	int i;
47947c88fffSLiam Girdwood 
48047c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
48147c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
48247c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
48347c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
48447c88fffSLiam Girdwood 	}
485f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
48647c88fffSLiam Girdwood 	return NULL;
48747c88fffSLiam Girdwood }
48847c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
48947c88fffSLiam Girdwood 
49047c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
49147c88fffSLiam Girdwood 		const char *dai_link)
49247c88fffSLiam Girdwood {
49347c88fffSLiam Girdwood 	int i;
49447c88fffSLiam Girdwood 
49547c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
49647c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
49747c88fffSLiam Girdwood 			return &card->rtd[i];
49847c88fffSLiam Girdwood 	}
499f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
50047c88fffSLiam Girdwood 	return NULL;
50147c88fffSLiam Girdwood }
50247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
50347c88fffSLiam Girdwood 
504db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
505db2a4165SFrank Mandarino /* unregister ac97 codec */
506db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
507db2a4165SFrank Mandarino {
508db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
509db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
510db2a4165SFrank Mandarino 	return 0;
511db2a4165SFrank Mandarino }
512db2a4165SFrank Mandarino 
513db2a4165SFrank Mandarino /* stop no dev release warning */
514db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
515db2a4165SFrank Mandarino 
516db2a4165SFrank Mandarino /* register ac97 codec to bus */
517db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
518db2a4165SFrank Mandarino {
519db2a4165SFrank Mandarino 	int err;
520db2a4165SFrank Mandarino 
521db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
52200200107SLars-Peter Clausen 	codec->ac97->dev.parent = codec->component.card->dev;
523db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
524db2a4165SFrank Mandarino 
525bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
52600200107SLars-Peter Clausen 		     codec->component.card->snd_card->number, 0,
52700200107SLars-Peter Clausen 		     codec->component.name);
528db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
529db2a4165SFrank Mandarino 	if (err < 0) {
530f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
531db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
532db2a4165SFrank Mandarino 		return err;
533db2a4165SFrank Mandarino 	}
534db2a4165SFrank Mandarino 	return 0;
535db2a4165SFrank Mandarino }
536db2a4165SFrank Mandarino #endif
537db2a4165SFrank Mandarino 
5389d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5399d58a077SRichard Fitzgerald {
5409d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5419d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5429d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5439d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5449d58a077SRichard Fitzgerald 	 */
5459d58a077SRichard Fitzgerald }
5469d58a077SRichard Fitzgerald 
5476f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
548db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5496f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
550db2a4165SFrank Mandarino {
5516f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5522eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
55388bd870fSBenoit Cousson 	int i, j;
554db2a4165SFrank Mandarino 
555e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
556e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
557e3509ff0SDaniel Mack 	 */
558f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
559e3509ff0SDaniel Mack 		return 0;
560e3509ff0SDaniel Mack 
5616ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5626ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5636ed25978SAndy Green 	 */
564f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
565f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5676ed25978SAndy Green 
5686ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
569f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5706ed25978SAndy Green 
571a00f90f9SMark Brown 	/* mute any active DACs */
572f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5733efab7dcSMark Brown 
574f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5753efab7dcSMark Brown 			continue;
5763efab7dcSMark Brown 
57788bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
57888bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
57988bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
58088bd870fSBenoit Cousson 
581f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
582f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
583db2a4165SFrank Mandarino 		}
58488bd870fSBenoit Cousson 	}
585db2a4165SFrank Mandarino 
5864ccab3e7SLiam Girdwood 	/* suspend all pcms */
587f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
588f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5893efab7dcSMark Brown 			continue;
5903efab7dcSMark Brown 
591f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5923efab7dcSMark Brown 	}
5934ccab3e7SLiam Girdwood 
59487506549SMark Brown 	if (card->suspend_pre)
59570b2ac12SMark Brown 		card->suspend_pre(card);
596db2a4165SFrank Mandarino 
597f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
598f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
599f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
6003efab7dcSMark Brown 
601f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6023efab7dcSMark Brown 			continue;
6033efab7dcSMark Brown 
604f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
605f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
606f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
607f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
608f0fba2adSLiam Girdwood 			platform->suspended = 1;
609f0fba2adSLiam Girdwood 		}
610db2a4165SFrank Mandarino 	}
611db2a4165SFrank Mandarino 
612db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
613f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
61488bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
61543829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
61688bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
61788bd870fSBenoit Cousson 			codec_dais[j]->codec->dapm.suspend_bias_level =
61888bd870fSBenoit Cousson 					codec_dais[j]->codec->dapm.bias_level;
61988bd870fSBenoit Cousson 		}
620f0fba2adSLiam Girdwood 	}
621db2a4165SFrank Mandarino 
622f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6233efab7dcSMark Brown 
624f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6253efab7dcSMark Brown 			continue;
6263efab7dcSMark Brown 
6277bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6287bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
629db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
630f0fba2adSLiam Girdwood 
6317bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6327bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
633db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
634db2a4165SFrank Mandarino 	}
635db2a4165SFrank Mandarino 
636e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
637e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
638e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
639e2d32ff6SMark Brown 
640f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6412eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6421547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6431547aba9SMark Brown 		 * bias _ON and should not be suspended. */
644f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
645ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6461547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
647125a25daSMark Brown 				/*
648125a25daSMark Brown 				 * If the CODEC is capable of idle
649125a25daSMark Brown 				 * bias off then being in STANDBY
650125a25daSMark Brown 				 * means it's doing something,
651125a25daSMark Brown 				 * otherwise fall through.
652125a25daSMark Brown 				 */
653125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
654125a25daSMark Brown 					dev_dbg(codec->dev,
65510e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
656125a25daSMark Brown 					break;
657125a25daSMark Brown 				}
6581547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
65984b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
660f0fba2adSLiam Girdwood 				codec->suspended = 1;
6617be4ba24SMark Brown 				codec->cache_sync = 1;
662e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
663e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
664988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
665988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6661547aba9SMark Brown 				break;
6671547aba9SMark Brown 			default:
66810e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
66910e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6701547aba9SMark Brown 				break;
6711547aba9SMark Brown 			}
6721547aba9SMark Brown 		}
673f0fba2adSLiam Girdwood 	}
674db2a4165SFrank Mandarino 
675f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
676f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6773efab7dcSMark Brown 
678f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6793efab7dcSMark Brown 			continue;
6803efab7dcSMark Brown 
681f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
682f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
683988e8cc4SNicolin Chen 
684988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
685988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
686db2a4165SFrank Mandarino 	}
687db2a4165SFrank Mandarino 
68887506549SMark Brown 	if (card->suspend_post)
68970b2ac12SMark Brown 		card->suspend_post(card);
690db2a4165SFrank Mandarino 
691db2a4165SFrank Mandarino 	return 0;
692db2a4165SFrank Mandarino }
6936f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
694db2a4165SFrank Mandarino 
6956ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6966ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6976ed25978SAndy Green  */
6986ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
699db2a4165SFrank Mandarino {
700f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
701f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7022eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
70388bd870fSBenoit Cousson 	int i, j;
704db2a4165SFrank Mandarino 
7056ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7066ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7076ed25978SAndy Green 	 */
7086ed25978SAndy Green 
709f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7106ed25978SAndy Green 
7119949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
712f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7139949788bSMark Brown 
71487506549SMark Brown 	if (card->resume_pre)
71570b2ac12SMark Brown 		card->resume_pre(card);
716db2a4165SFrank Mandarino 
717f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
718f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
719f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7203efab7dcSMark Brown 
721f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7223efab7dcSMark Brown 			continue;
7233efab7dcSMark Brown 
724f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
725f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
726db2a4165SFrank Mandarino 	}
727db2a4165SFrank Mandarino 
7282eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7291547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7301547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7311547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7321547aba9SMark Brown 		 */
733f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
734ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7351547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7361547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
737f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
738f0fba2adSLiam Girdwood 				codec->suspended = 0;
7391547aba9SMark Brown 				break;
7401547aba9SMark Brown 			default:
74110e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
74210e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7431547aba9SMark Brown 				break;
7441547aba9SMark Brown 			}
7451547aba9SMark Brown 		}
746f0fba2adSLiam Girdwood 	}
747db2a4165SFrank Mandarino 
748f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7493efab7dcSMark Brown 
750f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7513efab7dcSMark Brown 			continue;
7523efab7dcSMark Brown 
7537bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
754d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
755db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
756f0fba2adSLiam Girdwood 
7577bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
758d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
759db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
760db2a4165SFrank Mandarino 	}
761db2a4165SFrank Mandarino 
7623ff3f64bSMark Brown 	/* unmute any active DACs */
763f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7643efab7dcSMark Brown 
765f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7663efab7dcSMark Brown 			continue;
7673efab7dcSMark Brown 
76888bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
76988bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
77088bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
77188bd870fSBenoit Cousson 
772f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
773f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
774db2a4165SFrank Mandarino 		}
77588bd870fSBenoit Cousson 	}
776db2a4165SFrank Mandarino 
777f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
778f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7803efab7dcSMark Brown 
781f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7823efab7dcSMark Brown 			continue;
7833efab7dcSMark Brown 
784f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
786f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
787f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
788f0fba2adSLiam Girdwood 			platform->suspended = 0;
789f0fba2adSLiam Girdwood 		}
790db2a4165SFrank Mandarino 	}
791db2a4165SFrank Mandarino 
79287506549SMark Brown 	if (card->resume_post)
79370b2ac12SMark Brown 		card->resume_post(card);
794db2a4165SFrank Mandarino 
795f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7966ed25978SAndy Green 
7976ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
798f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
799e2d32ff6SMark Brown 
800e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
801e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
802e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8036ed25978SAndy Green }
8046ed25978SAndy Green 
8056ed25978SAndy Green /* powers up audio subsystem after a suspend */
8066f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8076ed25978SAndy Green {
8086f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
80982e14e8bSStephen Warren 	int i, ac97_control = 0;
810b9dd94a8SPeter Ujfalusi 
8115ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
8125ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
8135ff1ddf2SEric Miao 	 */
8145ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
8155ff1ddf2SEric Miao 		return 0;
8165ff1ddf2SEric Miao 
817988e8cc4SNicolin Chen 	/* activate pins from sleep state */
818988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
81988bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
82088bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
82188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
82288bd870fSBenoit Cousson 		int j;
82388bd870fSBenoit Cousson 
824988e8cc4SNicolin Chen 		if (cpu_dai->active)
825988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
82688bd870fSBenoit Cousson 
82788bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
82888bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
829988e8cc4SNicolin Chen 			if (codec_dai->active)
830988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
831988e8cc4SNicolin Chen 		}
83288bd870fSBenoit Cousson 	}
833988e8cc4SNicolin Chen 
83464ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
83564ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
83664ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
83764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
83864ab9baaSMark Brown 	 */
839f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
840f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
84182e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
84282e14e8bSStephen Warren 	}
84382e14e8bSStephen Warren 	if (ac97_control) {
844f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
84564ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
84664ab9baaSMark Brown 	} else {
847f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8486308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
849f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
850f0fba2adSLiam Girdwood 	}
8516ed25978SAndy Green 
852db2a4165SFrank Mandarino 	return 0;
853db2a4165SFrank Mandarino }
8546f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
855db2a4165SFrank Mandarino #else
8566f8ab4acSMark Brown #define snd_soc_suspend NULL
8576f8ab4acSMark Brown #define snd_soc_resume NULL
858db2a4165SFrank Mandarino #endif
859db2a4165SFrank Mandarino 
86085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
86102a06d30SBarry Song };
86202a06d30SBarry Song 
86365d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
86465d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
86565d9361fSLars-Peter Clausen {
86665d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
86765d9361fSLars-Peter Clausen 
86865d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
86965d9361fSLars-Peter Clausen 		if (of_node) {
87065d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
87165d9361fSLars-Peter Clausen 				return component;
87265d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
87365d9361fSLars-Peter Clausen 			return component;
87465d9361fSLars-Peter Clausen 		}
87565d9361fSLars-Peter Clausen 	}
87665d9361fSLars-Peter Clausen 
87765d9361fSLars-Peter Clausen 	return NULL;
87865d9361fSLars-Peter Clausen }
87965d9361fSLars-Peter Clausen 
88088bd870fSBenoit Cousson static struct snd_soc_codec *soc_find_codec(
88188bd870fSBenoit Cousson 					const struct device_node *codec_of_node,
88212023a9aSMisael Lopez Cruz 					const char *codec_name)
88312023a9aSMisael Lopez Cruz {
88412023a9aSMisael Lopez Cruz 	struct snd_soc_codec *codec;
88512023a9aSMisael Lopez Cruz 
88612023a9aSMisael Lopez Cruz 	list_for_each_entry(codec, &codec_list, list) {
88712023a9aSMisael Lopez Cruz 		if (codec_of_node) {
88812023a9aSMisael Lopez Cruz 			if (codec->dev->of_node != codec_of_node)
88912023a9aSMisael Lopez Cruz 				continue;
89012023a9aSMisael Lopez Cruz 		} else {
891f4333203SLars-Peter Clausen 			if (strcmp(codec->component.name, codec_name))
89212023a9aSMisael Lopez Cruz 				continue;
89312023a9aSMisael Lopez Cruz 		}
89412023a9aSMisael Lopez Cruz 
89512023a9aSMisael Lopez Cruz 		return codec;
89612023a9aSMisael Lopez Cruz 	}
89712023a9aSMisael Lopez Cruz 
89812023a9aSMisael Lopez Cruz 	return NULL;
89912023a9aSMisael Lopez Cruz }
90012023a9aSMisael Lopez Cruz 
90112023a9aSMisael Lopez Cruz static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
90212023a9aSMisael Lopez Cruz 					      const char *codec_dai_name)
90312023a9aSMisael Lopez Cruz {
90412023a9aSMisael Lopez Cruz 	struct snd_soc_dai *codec_dai;
90512023a9aSMisael Lopez Cruz 
90612023a9aSMisael Lopez Cruz 	list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
90712023a9aSMisael Lopez Cruz 		if (!strcmp(codec_dai->name, codec_dai_name)) {
90812023a9aSMisael Lopez Cruz 			return codec_dai;
90912023a9aSMisael Lopez Cruz 		}
91012023a9aSMisael Lopez Cruz 	}
91112023a9aSMisael Lopez Cruz 
91212023a9aSMisael Lopez Cruz 	return NULL;
91312023a9aSMisael Lopez Cruz }
91412023a9aSMisael Lopez Cruz 
915f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
916db2a4165SFrank Mandarino {
917f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
918f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
9191438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
92088bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
92188bd870fSBenoit Cousson 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
922435c5e25SMark Brown 	struct snd_soc_platform *platform;
92312023a9aSMisael Lopez Cruz 	struct snd_soc_dai *cpu_dai;
924848dd8beSMark Brown 	const char *platform_name;
92588bd870fSBenoit Cousson 	int i;
926db2a4165SFrank Mandarino 
927f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
9286308419aSMark Brown 
929b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
9301438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
931bc92657aSStephen Warren 		if (dai_link->cpu_of_node &&
9321438c2f6SLars-Peter Clausen 			component->dev->of_node != dai_link->cpu_of_node)
9335a504963SStephen Warren 			continue;
934bc92657aSStephen Warren 		if (dai_link->cpu_name &&
9351438c2f6SLars-Peter Clausen 			strcmp(dev_name(component->dev), dai_link->cpu_name))
9362610ab77SStephen Warren 			continue;
9371438c2f6SLars-Peter Clausen 		list_for_each_entry(cpu_dai, &component->dai_list, list) {
938bc92657aSStephen Warren 			if (dai_link->cpu_dai_name &&
939bc92657aSStephen Warren 				strcmp(cpu_dai->name, dai_link->cpu_dai_name))
940bc92657aSStephen Warren 				continue;
9412610ab77SStephen Warren 
942f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
943f0fba2adSLiam Girdwood 		}
9441438c2f6SLars-Peter Clausen 	}
945b19e6e7bSMark Brown 
946b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
947f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
948f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
949b19e6e7bSMark Brown 		return -EPROBE_DEFER;
950c5af3a2eSMark Brown 	}
951c5af3a2eSMark Brown 
95288bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
95388bd870fSBenoit Cousson 
95488bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
95588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
95688bd870fSBenoit Cousson 		struct snd_soc_codec *codec;
95788bd870fSBenoit Cousson 		codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
95888bd870fSBenoit Cousson 		if (!codec) {
959f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: CODEC %s not registered\n",
96088bd870fSBenoit Cousson 				codecs[i].name);
961b19e6e7bSMark Brown 			return -EPROBE_DEFER;
962b19e6e7bSMark Brown 		}
963848dd8beSMark Brown 
96488bd870fSBenoit Cousson 		codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
96588bd870fSBenoit Cousson 		if (!codec_dais[i]) {
96612023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
96788bd870fSBenoit Cousson 				codecs[i].dai_name);
96812023a9aSMisael Lopez Cruz 			return -EPROBE_DEFER;
96912023a9aSMisael Lopez Cruz 		}
97088bd870fSBenoit Cousson 	}
97188bd870fSBenoit Cousson 
97288bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
97388bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
97488bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
97512023a9aSMisael Lopez Cruz 
976848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
977848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9785a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
979848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
980848dd8beSMark Brown 
981b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
982f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9835a504963SStephen Warren 		if (dai_link->platform_of_node) {
9845a504963SStephen Warren 			if (platform->dev->of_node !=
9855a504963SStephen Warren 			    dai_link->platform_of_node)
9865a504963SStephen Warren 				continue;
9875a504963SStephen Warren 		} else {
988f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
9892610ab77SStephen Warren 				continue;
9905a504963SStephen Warren 		}
9912610ab77SStephen Warren 
992f0fba2adSLiam Girdwood 		rtd->platform = platform;
993f0fba2adSLiam Girdwood 	}
994b19e6e7bSMark Brown 	if (!rtd->platform) {
995f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
996f0fba2adSLiam Girdwood 			dai_link->platform_name);
997b19e6e7bSMark Brown 		return -EPROBE_DEFER;
998f0fba2adSLiam Girdwood 	}
999b19e6e7bSMark Brown 
1000b19e6e7bSMark Brown 	card->num_rtd++;
1001b19e6e7bSMark Brown 
1002b19e6e7bSMark Brown 	return 0;
10036b05eda6SMark Brown }
10046b05eda6SMark Brown 
1005f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1006d12cd198SStephen Warren {
1007*70090bbbSLars-Peter Clausen 	if (!component->probed)
1008*70090bbbSLars-Peter Clausen 		return;
1009*70090bbbSLars-Peter Clausen 
1010f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1011f1d45cc3SLars-Peter Clausen 	if (component->codec)
1012f1d45cc3SLars-Peter Clausen 		list_del(&component->codec->card_list);
1013d12cd198SStephen Warren 
1014f1d45cc3SLars-Peter Clausen 	if (component->remove)
1015f1d45cc3SLars-Peter Clausen 		component->remove(component);
1016d12cd198SStephen Warren 
1017f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1018d12cd198SStephen Warren 
1019f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1020f1d45cc3SLars-Peter Clausen 	component->probed = 0;
1021f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1022589c3563SJarkko Nikula }
1023589c3563SJarkko Nikula 
1024b0aa88afSMisael Lopez Cruz static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1025b0aa88afSMisael Lopez Cruz {
1026b0aa88afSMisael Lopez Cruz 	int err;
1027b0aa88afSMisael Lopez Cruz 
1028b0aa88afSMisael Lopez Cruz 	if (codec_dai && codec_dai->probed &&
1029b0aa88afSMisael Lopez Cruz 			codec_dai->driver->remove_order == order) {
1030b0aa88afSMisael Lopez Cruz 		if (codec_dai->driver->remove) {
1031b0aa88afSMisael Lopez Cruz 			err = codec_dai->driver->remove(codec_dai);
1032b0aa88afSMisael Lopez Cruz 			if (err < 0)
1033b0aa88afSMisael Lopez Cruz 				dev_err(codec_dai->dev,
1034b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
1035b0aa88afSMisael Lopez Cruz 					codec_dai->name, err);
1036b0aa88afSMisael Lopez Cruz 		}
1037b0aa88afSMisael Lopez Cruz 		codec_dai->probed = 0;
1038b0aa88afSMisael Lopez Cruz 	}
1039b0aa88afSMisael Lopez Cruz }
1040b0aa88afSMisael Lopez Cruz 
104162ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1042f0fba2adSLiam Girdwood {
1043f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
104488bd870fSBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
104588bd870fSBenoit Cousson 	int i, err;
1046f0fba2adSLiam Girdwood 
1047f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1048f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
104936ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
105036ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
105136ae1a96SMark Brown 		device_unregister(rtd->dev);
1052f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
105302a06d30SBarry Song 	}
105402a06d30SBarry Song 
1055f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
105688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
105788bd870fSBenoit Cousson 		soc_remove_codec_dai(rtd->codec_dais[i], order);
1058f0fba2adSLiam Girdwood 
1059f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
10600168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
10610168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
1062f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1063f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1064f0fba2adSLiam Girdwood 			if (err < 0)
1065f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1066f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
10670837fc62SFabio Estevam 					cpu_dai->name, err);
1068f0fba2adSLiam Girdwood 		}
1069f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
10709420d97bSLars-Peter Clausen 		if (!cpu_dai->codec)
1071f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
1072f0fba2adSLiam Girdwood 	}
1073f0fba2adSLiam Girdwood }
1074f0fba2adSLiam Girdwood 
107562ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
107662ae68faSStephen Warren 				       int order)
107762ae68faSStephen Warren {
107862ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
107962ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
108062ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
108161aca564SLars-Peter Clausen 	struct snd_soc_component *component;
108288bd870fSBenoit Cousson 	int i;
108362ae68faSStephen Warren 
108462ae68faSStephen Warren 	/* remove the platform */
1085*70090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1086f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
108762ae68faSStephen Warren 
108862ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
108988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
109061aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
1091*70090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
109261aca564SLars-Peter Clausen 			soc_remove_component(component);
109362ae68faSStephen Warren 	}
109462ae68faSStephen Warren 
109562ae68faSStephen Warren 	/* remove any CPU-side CODEC */
109662ae68faSStephen Warren 	if (cpu_dai) {
1097*70090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
109861aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
109962ae68faSStephen Warren 	}
110062ae68faSStephen Warren }
110162ae68faSStephen Warren 
11020671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11030671fd8eSKuninori Morimoto {
11040168bf0dSLiam Girdwood 	int dai, order;
11050671fd8eSKuninori Morimoto 
11060168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
11070168bf0dSLiam Girdwood 			order++) {
11080168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
110962ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
11100168bf0dSLiam Girdwood 	}
111162ae68faSStephen Warren 
111262ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
111362ae68faSStephen Warren 			order++) {
111462ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
111562ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
111662ae68faSStephen Warren 	}
111762ae68faSStephen Warren 
11180671fd8eSKuninori Morimoto 	card->num_rtd = 0;
11190671fd8eSKuninori Morimoto }
11200671fd8eSKuninori Morimoto 
1121ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
112294f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1123ead9b919SJarkko Nikula {
1124ead9b919SJarkko Nikula 	int i;
1125ead9b919SJarkko Nikula 
1126ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1127ead9b919SJarkko Nikula 		return;
1128ead9b919SJarkko Nikula 
1129ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1130ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
113194f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
11323ca041edSSebastian Reichel 			continue;
113394f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
11343ca041edSSebastian Reichel 			continue;
113594f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1136ead9b919SJarkko Nikula 		break;
1137ead9b919SJarkko Nikula 	}
1138ead9b919SJarkko Nikula }
1139ead9b919SJarkko Nikula 
1140f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1141f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1142589c3563SJarkko Nikula {
1143f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1144f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *dai_component, *component2;
1145888df395SMark Brown 	struct snd_soc_dai *dai;
1146f1d45cc3SLars-Peter Clausen 	int ret;
1147589c3563SJarkko Nikula 
1148*70090bbbSLars-Peter Clausen 	if (component->probed)
1149*70090bbbSLars-Peter Clausen 		return 0;
1150*70090bbbSLars-Peter Clausen 
1151f1d45cc3SLars-Peter Clausen 	component->card = card;
1152f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1153f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1154589c3563SJarkko Nikula 
1155f1d45cc3SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
115670d29331SJarkko Nikula 		return -ENODEV;
115770d29331SJarkko Nikula 
1158f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1159d5d1e0beSLars-Peter Clausen 
1160f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1161f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1162f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
116377530150SLars-Peter Clausen 
1164b318ad50SNariman Poushin 		if (ret != 0) {
1165f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1166b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1167b318ad50SNariman Poushin 			goto err_probe;
1168b318ad50SNariman Poushin 		}
1169b318ad50SNariman Poushin 	}
1170b318ad50SNariman Poushin 
1171f1d45cc3SLars-Peter Clausen 	/*
1172f1d45cc3SLars-Peter Clausen 	 * This is rather ugly, but certain platforms expect that the DAPM
1173f1d45cc3SLars-Peter Clausen 	 * widgets for the DAIs for components with the same parent device are
1174f1d45cc3SLars-Peter Clausen 	 * created in the platforms DAPM context. Until that is fixed we need to
1175f1d45cc3SLars-Peter Clausen 	 * keep this.
1176f1d45cc3SLars-Peter Clausen 	 */
1177f1d45cc3SLars-Peter Clausen 	if (component->steal_sibling_dai_widgets) {
1178f1d45cc3SLars-Peter Clausen 		dai_component = NULL;
1179f1d45cc3SLars-Peter Clausen 		list_for_each_entry(component2, &component_list, list) {
1180f1d45cc3SLars-Peter Clausen 			if (component == component2)
1181be09ad90SLiam Girdwood 				continue;
1182f1d45cc3SLars-Peter Clausen 
1183f1d45cc3SLars-Peter Clausen 			if (component2->dev == component->dev &&
1184f1d45cc3SLars-Peter Clausen 			    !list_empty(&component2->dai_list)) {
1185f1d45cc3SLars-Peter Clausen 				dai_component = component2;
1186f1d45cc3SLars-Peter Clausen 				break;
1187f1d45cc3SLars-Peter Clausen 			}
1188f1d45cc3SLars-Peter Clausen 		}
1189f1d45cc3SLars-Peter Clausen 	} else {
1190f1d45cc3SLars-Peter Clausen 		dai_component = component;
1191f1d45cc3SLars-Peter Clausen 		list_for_each_entry(component2, &component_list, list) {
1192f1d45cc3SLars-Peter Clausen 			if (component2->dev == component->dev &&
1193f1d45cc3SLars-Peter Clausen 			    component2->steal_sibling_dai_widgets) {
1194f1d45cc3SLars-Peter Clausen 				dai_component = NULL;
1195f1d45cc3SLars-Peter Clausen 				break;
1196f1d45cc3SLars-Peter Clausen 			}
1197f1d45cc3SLars-Peter Clausen 		}
1198be09ad90SLiam Girdwood 	}
1199be09ad90SLiam Girdwood 
1200f1d45cc3SLars-Peter Clausen 	if (dai_component) {
1201f1d45cc3SLars-Peter Clausen 		list_for_each_entry(dai, &dai_component->dai_list, list) {
1202f1d45cc3SLars-Peter Clausen 			snd_soc_dapm_new_dai_widgets(dapm, dai);
1203f1d45cc3SLars-Peter Clausen 			if (ret != 0) {
1204f1d45cc3SLars-Peter Clausen 				dev_err(component->dev,
1205f1d45cc3SLars-Peter Clausen 					"Failed to create DAI widgets %d\n",
1206f1d45cc3SLars-Peter Clausen 					ret);
1207956245e9SLiam Girdwood 				goto err_probe;
1208956245e9SLiam Girdwood 			}
1209956245e9SLiam Girdwood 		}
1210f1d45cc3SLars-Peter Clausen 	}
1211956245e9SLiam Girdwood 
1212f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1213f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1214f1d45cc3SLars-Peter Clausen 		if (ret < 0) {
1215f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1216f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
1217f1d45cc3SLars-Peter Clausen 			goto err_probe;
1218f1d45cc3SLars-Peter Clausen 		}
1219cb2cf612SLiam Girdwood 
1220f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1221f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1222f1d45cc3SLars-Peter Clausen 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1223f1d45cc3SLars-Peter Clausen 			component->name);
1224f1d45cc3SLars-Peter Clausen 	}
1225f1d45cc3SLars-Peter Clausen 
1226f1d45cc3SLars-Peter Clausen 	if (component->controls)
1227f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1228f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1229f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1230f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1231f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1232f1d45cc3SLars-Peter Clausen 
1233f1d45cc3SLars-Peter Clausen 	component->probed = 1;
1234f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1235f1d45cc3SLars-Peter Clausen 
1236f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1237f1d45cc3SLars-Peter Clausen 	if (component->codec)
1238f1d45cc3SLars-Peter Clausen 		list_add(&component->codec->card_list, &card->codec_dev_list);
1239956245e9SLiam Girdwood 
1240956245e9SLiam Girdwood 	return 0;
1241956245e9SLiam Girdwood 
1242956245e9SLiam Girdwood err_probe:
1243f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1244f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1245956245e9SLiam Girdwood 
1246956245e9SLiam Girdwood 	return ret;
1247956245e9SLiam Girdwood }
1248956245e9SLiam Girdwood 
124936ae1a96SMark Brown static void rtd_release(struct device *dev)
125036ae1a96SMark Brown {
125136ae1a96SMark Brown 	kfree(dev);
125236ae1a96SMark Brown }
1253f0fba2adSLiam Girdwood 
12545f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
12555f3484acSLars-Peter Clausen 	const char *name)
1256503ae5e0SMisael Lopez Cruz {
1257589c3563SJarkko Nikula 	int ret = 0;
1258589c3563SJarkko Nikula 
1259589c3563SJarkko Nikula 	/* register the rtd device */
126036ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
126136ae1a96SMark Brown 	if (!rtd->dev)
126236ae1a96SMark Brown 		return -ENOMEM;
126336ae1a96SMark Brown 	device_initialize(rtd->dev);
12645f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
126536ae1a96SMark Brown 	rtd->dev->release = rtd_release;
126636ae1a96SMark Brown 	rtd->dev->init_name = name;
126736ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1268b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
126901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
127001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
127101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
127201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
127336ae1a96SMark Brown 	ret = device_add(rtd->dev);
1274589c3563SJarkko Nikula 	if (ret < 0) {
1275865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1276865df9cbSChuansheng Liu 		put_device(rtd->dev);
12775f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1278f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1279589c3563SJarkko Nikula 		return ret;
1280589c3563SJarkko Nikula 	}
1281589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1282589c3563SJarkko Nikula 
128393c3ce76SLars-Peter Clausen 	if (rtd->codec) {
1284589c3563SJarkko Nikula 		/* add DAPM sysfs entries for this codec */
128536ae1a96SMark Brown 		ret = snd_soc_dapm_sys_add(rtd->dev);
1286589c3563SJarkko Nikula 		if (ret < 0)
128744c69bb1SLars-Peter Clausen 			dev_err(rtd->dev,
128893c3ce76SLars-Peter Clausen 				"ASoC: failed to add codec dapm sysfs entries: %d\n",
128993c3ce76SLars-Peter Clausen 				ret);
1290589c3563SJarkko Nikula 
1291589c3563SJarkko Nikula 		/* add codec sysfs entries */
129236ae1a96SMark Brown 		ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1293589c3563SJarkko Nikula 		if (ret < 0)
129444c69bb1SLars-Peter Clausen 			dev_err(rtd->dev,
129593c3ce76SLars-Peter Clausen 				"ASoC: failed to add codec sysfs files: %d\n",
129693c3ce76SLars-Peter Clausen 				ret);
129793c3ce76SLars-Peter Clausen 	}
1298589c3563SJarkko Nikula 
1299589c3563SJarkko Nikula 	return 0;
1300589c3563SJarkko Nikula }
1301589c3563SJarkko Nikula 
130262ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
130362ae68faSStephen Warren 				     int order)
130462ae68faSStephen Warren {
130562ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
130662ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1307f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
130888bd870fSBenoit Cousson 	int i, ret;
130962ae68faSStephen Warren 
131062ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
131161aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
1312*70090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1313f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
131462ae68faSStephen Warren 		if (ret < 0)
131562ae68faSStephen Warren 			return ret;
131662ae68faSStephen Warren 	}
131762ae68faSStephen Warren 
131888bd870fSBenoit Cousson 	/* probe the CODEC-side components */
131988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
132061aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
1321*70090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1322f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
132362ae68faSStephen Warren 			if (ret < 0)
132462ae68faSStephen Warren 				return ret;
132562ae68faSStephen Warren 		}
132688bd870fSBenoit Cousson 	}
132762ae68faSStephen Warren 
132862ae68faSStephen Warren 	/* probe the platform */
1329*70090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1330f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
133162ae68faSStephen Warren 		if (ret < 0)
133262ae68faSStephen Warren 			return ret;
133362ae68faSStephen Warren 	}
133462ae68faSStephen Warren 
133562ae68faSStephen Warren 	return 0;
133662ae68faSStephen Warren }
133762ae68faSStephen Warren 
1338b0aa88afSMisael Lopez Cruz static int soc_probe_codec_dai(struct snd_soc_card *card,
1339b0aa88afSMisael Lopez Cruz 			       struct snd_soc_dai *codec_dai,
1340b0aa88afSMisael Lopez Cruz 			       int order)
1341b0aa88afSMisael Lopez Cruz {
1342b0aa88afSMisael Lopez Cruz 	int ret;
1343b0aa88afSMisael Lopez Cruz 
1344b0aa88afSMisael Lopez Cruz 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1345b0aa88afSMisael Lopez Cruz 		if (codec_dai->driver->probe) {
1346b0aa88afSMisael Lopez Cruz 			ret = codec_dai->driver->probe(codec_dai);
1347b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
1348b0aa88afSMisael Lopez Cruz 				dev_err(codec_dai->dev,
1349b0aa88afSMisael Lopez Cruz 					"ASoC: failed to probe CODEC DAI %s: %d\n",
1350b0aa88afSMisael Lopez Cruz 					codec_dai->name, ret);
1351b0aa88afSMisael Lopez Cruz 				return ret;
1352b0aa88afSMisael Lopez Cruz 			}
1353b0aa88afSMisael Lopez Cruz 		}
1354b0aa88afSMisael Lopez Cruz 
1355b0aa88afSMisael Lopez Cruz 		/* mark codec_dai as probed and add to card dai list */
1356b0aa88afSMisael Lopez Cruz 		codec_dai->probed = 1;
1357b0aa88afSMisael Lopez Cruz 	}
1358b0aa88afSMisael Lopez Cruz 
1359b0aa88afSMisael Lopez Cruz 	return 0;
1360b0aa88afSMisael Lopez Cruz }
1361b0aa88afSMisael Lopez Cruz 
13622436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
13632436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
13643f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
13652436a723SMisael Lopez Cruz {
13663f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
13673f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
13682436a723SMisael Lopez Cruz 	struct snd_soc_dapm_widget *play_w, *capture_w;
13692436a723SMisael Lopez Cruz 	int ret;
13702436a723SMisael Lopez Cruz 
137188bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
137288bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
137388bd870fSBenoit Cousson 
13742436a723SMisael Lopez Cruz 	/* link the DAI widgets */
13752436a723SMisael Lopez Cruz 	play_w = codec_dai->playback_widget;
13762436a723SMisael Lopez Cruz 	capture_w = cpu_dai->capture_widget;
13772436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
13782436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
13792436a723SMisael Lopez Cruz 					   capture_w, play_w);
13802436a723SMisael Lopez Cruz 		if (ret != 0) {
13812436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
13822436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
13832436a723SMisael Lopez Cruz 			return ret;
13842436a723SMisael Lopez Cruz 		}
13852436a723SMisael Lopez Cruz 	}
13862436a723SMisael Lopez Cruz 
13872436a723SMisael Lopez Cruz 	play_w = cpu_dai->playback_widget;
13882436a723SMisael Lopez Cruz 	capture_w = codec_dai->capture_widget;
13892436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
13902436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
13912436a723SMisael Lopez Cruz 					   capture_w, play_w);
13922436a723SMisael Lopez Cruz 		if (ret != 0) {
13932436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
13942436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
13952436a723SMisael Lopez Cruz 			return ret;
13962436a723SMisael Lopez Cruz 		}
13972436a723SMisael Lopez Cruz 	}
13982436a723SMisael Lopez Cruz 
13992436a723SMisael Lopez Cruz 	return 0;
14002436a723SMisael Lopez Cruz }
14012436a723SMisael Lopez Cruz 
140262ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1403f0fba2adSLiam Girdwood {
1404f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1405f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1406f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1407c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
140888bd870fSBenoit Cousson 	int i, ret;
1409f0fba2adSLiam Girdwood 
1410f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14110168bf0dSLiam Girdwood 			card->name, num, order);
1412f0fba2adSLiam Girdwood 
1413f0fba2adSLiam Girdwood 	/* config components */
1414f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1415f0fba2adSLiam Girdwood 	cpu_dai->card = card;
141688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
141788bd870fSBenoit Cousson 		rtd->codec_dais[i]->card = card;
1418f0fba2adSLiam Girdwood 
1419f0fba2adSLiam Girdwood 	/* set default power off timeout */
1420f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1421f0fba2adSLiam Girdwood 
1422f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
14230168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
14240168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1425a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
142661b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
142761b61e3cSLiam Girdwood 				return -ENODEV;
1428a9db7dbeSStephen Warren 		}
1429be09ad90SLiam Girdwood 
1430f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1431f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1432f0fba2adSLiam Girdwood 			if (ret < 0) {
1433f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1434f110bfc7SLiam Girdwood 					"ASoC: failed to probe CPU DAI %s: %d\n",
14350837fc62SFabio Estevam 					cpu_dai->name, ret);
143661b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1437f0fba2adSLiam Girdwood 				return ret;
1438f0fba2adSLiam Girdwood 			}
1439f0fba2adSLiam Girdwood 		}
1440f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1441f0fba2adSLiam Girdwood 	}
1442f0fba2adSLiam Girdwood 
1443f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
144488bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
144588bd870fSBenoit Cousson 		ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1446b0aa88afSMisael Lopez Cruz 		if (ret)
1447f0fba2adSLiam Girdwood 			return ret;
144888bd870fSBenoit Cousson 	}
1449f0fba2adSLiam Girdwood 
14500168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14510168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14520168bf0dSLiam Girdwood 		return 0;
14530168bf0dSLiam Girdwood 
14545f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14555f3484acSLars-Peter Clausen 	if (dai_link->init) {
14565f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14575f3484acSLars-Peter Clausen 		if (ret < 0) {
14585f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14595f3484acSLars-Peter Clausen 				dai_link->name, ret);
14605f3484acSLars-Peter Clausen 			return ret;
14615f3484acSLars-Peter Clausen 		}
14625f3484acSLars-Peter Clausen 	}
14635f3484acSLars-Peter Clausen 
14645f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1465589c3563SJarkko Nikula 	if (ret)
1466f0fba2adSLiam Girdwood 		return ret;
1467f0fba2adSLiam Girdwood 
14685f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
14695f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
14705f3484acSLars-Peter Clausen 	if (dai_link->dynamic) {
14715f3484acSLars-Peter Clausen 		ret = soc_dpcm_debugfs_add(rtd);
14725f3484acSLars-Peter Clausen 		if (ret < 0) {
14735f3484acSLars-Peter Clausen 			dev_err(rtd->dev,
14745f3484acSLars-Peter Clausen 				"ASoC: failed to add dpcm sysfs entries: %d\n",
14755f3484acSLars-Peter Clausen 				ret);
14765f3484acSLars-Peter Clausen 			return ret;
14775f3484acSLars-Peter Clausen 		}
14785f3484acSLars-Peter Clausen 	}
14795f3484acSLars-Peter Clausen #endif
14805f3484acSLars-Peter Clausen 
148136ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1482f0fba2adSLiam Girdwood 	if (ret < 0)
1483f110bfc7SLiam Girdwood 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1484f110bfc7SLiam Girdwood 			ret);
1485f0fba2adSLiam Girdwood 
14861245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
14871245b700SNamarta Kohli 		/*create compress_device"*/
14881245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
14891245b700SNamarta Kohli 		if (ret < 0) {
1490f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
14911245b700SNamarta Kohli 					 dai_link->stream_name);
14921245b700SNamarta Kohli 			return ret;
14931245b700SNamarta Kohli 		}
14941245b700SNamarta Kohli 	} else {
14951245b700SNamarta Kohli 
1496c74184edSMark Brown 		if (!dai_link->params) {
1497f0fba2adSLiam Girdwood 			/* create the pcm */
1498f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1499f0fba2adSLiam Girdwood 			if (ret < 0) {
1500f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15010837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1502f0fba2adSLiam Girdwood 				return ret;
1503f0fba2adSLiam Girdwood 			}
1504c74184edSMark Brown 		} else {
15059d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
15069d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
15079d58a077SRichard Fitzgerald 
1508c74184edSMark Brown 			/* link the DAI widgets */
15093f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
15102436a723SMisael Lopez Cruz 			if (ret)
1511c74184edSMark Brown 				return ret;
1512c74184edSMark Brown 		}
1513c74184edSMark Brown 	}
1514c74184edSMark Brown 
1515f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
151688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
151788bd870fSBenoit Cousson 		if (rtd->codec_dais[i]->driver->ac97_control)
151888bd870fSBenoit Cousson 			snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
151988bd870fSBenoit Cousson 					       rtd->cpu_dai->ac97_pdata);
152088bd870fSBenoit Cousson 	}
1521f0fba2adSLiam Girdwood 
1522f0fba2adSLiam Girdwood 	return 0;
1523f0fba2adSLiam Girdwood }
1524f0fba2adSLiam Girdwood 
1525f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
152602c9c7b9SMisael Lopez Cruz static int soc_register_ac97_codec(struct snd_soc_codec *codec,
152702c9c7b9SMisael Lopez Cruz 				   struct snd_soc_dai *codec_dai)
1528f0fba2adSLiam Girdwood {
1529f0fba2adSLiam Girdwood 	int ret;
1530f0fba2adSLiam Girdwood 
1531f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1532f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
15336b05eda6SMark Brown 	 */
153402c9c7b9SMisael Lopez Cruz 	if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
15350562f788SMika Westerberg 		/*
15360562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
15370562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
15380562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
15390562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
15400562f788SMika Westerberg 		 *
15410562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
15420562f788SMika Westerberg 		 */
154302c9c7b9SMisael Lopez Cruz 		if (!codec->ac97_created)
15440562f788SMika Westerberg 			return 0;
1545f0fba2adSLiam Girdwood 
154602c9c7b9SMisael Lopez Cruz 		ret = soc_ac97_dev_register(codec);
1547f0fba2adSLiam Girdwood 		if (ret < 0) {
154802c9c7b9SMisael Lopez Cruz 			dev_err(codec->dev,
1549f110bfc7SLiam Girdwood 				"ASoC: AC97 device register failed: %d\n", ret);
1550f0fba2adSLiam Girdwood 			return ret;
1551435c5e25SMark Brown 		}
1552435c5e25SMark Brown 
155302c9c7b9SMisael Lopez Cruz 		codec->ac97_registered = 1;
1554f0fba2adSLiam Girdwood 	}
1555f0fba2adSLiam Girdwood 	return 0;
1556f0fba2adSLiam Girdwood }
1557435c5e25SMark Brown 
155802c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1559f0fba2adSLiam Girdwood {
1560f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1561f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1562f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1563f0fba2adSLiam Girdwood 	}
1564f0fba2adSLiam Girdwood }
156502c9c7b9SMisael Lopez Cruz 
156688bd870fSBenoit Cousson static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
156788bd870fSBenoit Cousson {
156888bd870fSBenoit Cousson 	int i, ret;
156988bd870fSBenoit Cousson 
157088bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
157188bd870fSBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
157288bd870fSBenoit Cousson 
157388bd870fSBenoit Cousson 		ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
157488bd870fSBenoit Cousson 		if (ret) {
157588bd870fSBenoit Cousson 			while (--i >= 0)
157688bd870fSBenoit Cousson 				soc_unregister_ac97_codec(codec_dai->codec);
157788bd870fSBenoit Cousson 			return ret;
157888bd870fSBenoit Cousson 		}
157988bd870fSBenoit Cousson 	}
158088bd870fSBenoit Cousson 
158188bd870fSBenoit Cousson 	return 0;
158288bd870fSBenoit Cousson }
158388bd870fSBenoit Cousson 
158402c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
158502c9c7b9SMisael Lopez Cruz {
158688bd870fSBenoit Cousson 	int i;
158788bd870fSBenoit Cousson 
158888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
158988bd870fSBenoit Cousson 		soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
159002c9c7b9SMisael Lopez Cruz }
1591f0fba2adSLiam Girdwood #endif
1592435c5e25SMark Brown 
159344c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1594b19e6e7bSMark Brown {
159544c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
15963ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
159765d9361fSLars-Peter Clausen 	const char *name = aux_dev->codec_name;
15983ca041edSSebastian Reichel 
159965d9361fSLars-Peter Clausen 	rtd->component = soc_find_component(aux_dev->codec_of_node, name);
160065d9361fSLars-Peter Clausen 	if (!rtd->component) {
16013ca041edSSebastian Reichel 		if (aux_dev->codec_of_node)
160265d9361fSLars-Peter Clausen 			name = of_node_full_name(aux_dev->codec_of_node);
16033ca041edSSebastian Reichel 
160465d9361fSLars-Peter Clausen 		dev_err(card->dev, "ASoC: %s not registered\n", name);
1605b19e6e7bSMark Brown 		return -EPROBE_DEFER;
1606b19e6e7bSMark Brown 	}
1607b19e6e7bSMark Brown 
160865d9361fSLars-Peter Clausen 	/*
160965d9361fSLars-Peter Clausen 	 * Some places still reference rtd->codec, so we have to keep that
161065d9361fSLars-Peter Clausen 	 * initialized if the component is a CODEC. Once all those references
161165d9361fSLars-Peter Clausen 	 * have been removed, this code can be removed as well.
161265d9361fSLars-Peter Clausen 	 */
161365d9361fSLars-Peter Clausen 	 rtd->codec = rtd->component->codec;
161465d9361fSLars-Peter Clausen 
161544c69bb1SLars-Peter Clausen 	return 0;
16163ca041edSSebastian Reichel }
16172eea392dSJarkko Nikula 
161844c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
161944c69bb1SLars-Peter Clausen {
162044c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
16215f3484acSLars-Peter Clausen 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
162244c69bb1SLars-Peter Clausen 	int ret;
162344c69bb1SLars-Peter Clausen 
162465d9361fSLars-Peter Clausen 	ret = soc_probe_component(card, rtd->component);
16252eea392dSJarkko Nikula 	if (ret < 0)
1626589c3563SJarkko Nikula 		return ret;
16272eea392dSJarkko Nikula 
16285f3484acSLars-Peter Clausen 	/* do machine specific initialization */
16295f3484acSLars-Peter Clausen 	if (aux_dev->init) {
163057bf7726SLars-Peter Clausen 		ret = aux_dev->init(rtd->component);
16315f3484acSLars-Peter Clausen 		if (ret < 0) {
16325f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16335f3484acSLars-Peter Clausen 				aux_dev->name, ret);
16342eea392dSJarkko Nikula 			return ret;
16352eea392dSJarkko Nikula 		}
16365f3484acSLars-Peter Clausen 	}
16375f3484acSLars-Peter Clausen 
16385f3484acSLars-Peter Clausen 	return soc_post_component_init(rtd, aux_dev->name);
16392eea392dSJarkko Nikula }
16402eea392dSJarkko Nikula 
16412eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
16422eea392dSJarkko Nikula {
16432eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
164465d9361fSLars-Peter Clausen 	struct snd_soc_component *component = rtd->component;
16452eea392dSJarkko Nikula 
16462eea392dSJarkko Nikula 	/* unregister the rtd device */
16472eea392dSJarkko Nikula 	if (rtd->dev_registered) {
164836ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1649d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
16502eea392dSJarkko Nikula 		rtd->dev_registered = 0;
16512eea392dSJarkko Nikula 	}
16522eea392dSJarkko Nikula 
165365d9361fSLars-Peter Clausen 	if (component && component->probed)
165465d9361fSLars-Peter Clausen 		soc_remove_component(component);
16552eea392dSJarkko Nikula }
16562eea392dSJarkko Nikula 
1657f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1658fdf0f54dSDimitris Papastamos {
1659fdf0f54dSDimitris Papastamos 	int ret;
1660fdf0f54dSDimitris Papastamos 
1661fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1662fdf0f54dSDimitris Papastamos 		return 0;
1663fdf0f54dSDimitris Papastamos 
1664fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1665fdf0f54dSDimitris Papastamos 	if (ret < 0) {
166610e8aa9aSMichał Mirosław 		dev_err(codec->dev,
166710e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
166810e8aa9aSMichał Mirosław 			ret);
1669fdf0f54dSDimitris Papastamos 		return ret;
1670fdf0f54dSDimitris Papastamos 	}
1671fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1672fdf0f54dSDimitris Papastamos 	return 0;
1673fdf0f54dSDimitris Papastamos }
1674fdf0f54dSDimitris Papastamos 
1675b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1676f0fba2adSLiam Girdwood {
1677fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
167875d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1679f04209a7SMark Brown 	int ret, i, order, dai_fmt;
168096dd3622SMark Brown 
168101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1682f0fba2adSLiam Girdwood 
1683b19e6e7bSMark Brown 	/* bind DAIs */
1684b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1685b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1686b19e6e7bSMark Brown 		if (ret != 0)
1687b19e6e7bSMark Brown 			goto base_error;
1688db2a4165SFrank Mandarino 	}
1689db2a4165SFrank Mandarino 
169044c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1691b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
169244c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1693b19e6e7bSMark Brown 		if (ret != 0)
1694b19e6e7bSMark Brown 			goto base_error;
1695db2a4165SFrank Mandarino 	}
1696db2a4165SFrank Mandarino 
1697fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1698fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1699fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1700fdf0f54dSDimitris Papastamos 			continue;
1701f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1702b19e6e7bSMark Brown 		if (ret < 0)
1703b19e6e7bSMark Brown 			goto base_error;
1704fdf0f54dSDimitris Papastamos 	}
1705fdf0f54dSDimitris Papastamos 
1706f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1707102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1708f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1709f0fba2adSLiam Girdwood 	if (ret < 0) {
171010e8aa9aSMichał Mirosław 		dev_err(card->dev,
171110e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
171210e8aa9aSMichał Mirosław 			card->name, ret);
1713b19e6e7bSMark Brown 		goto base_error;
1714db2a4165SFrank Mandarino 	}
1715db2a4165SFrank Mandarino 
1716e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1717e37a4970SMark Brown 	card->dapm.dev = card->dev;
1718e37a4970SMark Brown 	card->dapm.card = card;
1719e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1720e37a4970SMark Brown 
1721d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1722d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1723d5d1e0beSLars-Peter Clausen #endif
1724d5d1e0beSLars-Peter Clausen 
172588ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
17266ed25978SAndy Green 	/* deferred resume work */
17276308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
17281301a964SRandy Dunlap #endif
17296ed25978SAndy Green 
17309a841ebbSMark Brown 	if (card->dapm_widgets)
17319a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
17329a841ebbSMark Brown 					  card->num_dapm_widgets);
17339a841ebbSMark Brown 
1734f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1735f0fba2adSLiam Girdwood 	if (card->probe) {
1736e7361ec4SMark Brown 		ret = card->probe(card);
1737f0fba2adSLiam Girdwood 		if (ret < 0)
1738f0fba2adSLiam Girdwood 			goto card_probe_error;
1739f0fba2adSLiam Girdwood 	}
1740f0fba2adSLiam Girdwood 
174162ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
17420168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
17430168bf0dSLiam Girdwood 			order++) {
1744fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
174562ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
174662ae68faSStephen Warren 			if (ret < 0) {
1747f110bfc7SLiam Girdwood 				dev_err(card->dev,
1748f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1749f110bfc7SLiam Girdwood 					ret);
175062ae68faSStephen Warren 				goto probe_dai_err;
175162ae68faSStephen Warren 			}
175262ae68faSStephen Warren 		}
175362ae68faSStephen Warren 	}
175462ae68faSStephen Warren 
175562ae68faSStephen Warren 	/* probe all DAI links on this card */
175662ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
175762ae68faSStephen Warren 			order++) {
175862ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
175962ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1760fe3e78e0SMark Brown 			if (ret < 0) {
1761f110bfc7SLiam Girdwood 				dev_err(card->dev,
1762f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1763f110bfc7SLiam Girdwood 					ret);
1764f0fba2adSLiam Girdwood 				goto probe_dai_err;
1765fe3e78e0SMark Brown 			}
1766fe3e78e0SMark Brown 		}
17670168bf0dSLiam Girdwood 	}
1768fe3e78e0SMark Brown 
17692eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
17702eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
17712eea392dSJarkko Nikula 		if (ret < 0) {
1772f110bfc7SLiam Girdwood 			dev_err(card->dev,
1773f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1774f110bfc7SLiam Girdwood 				ret);
17752eea392dSJarkko Nikula 			goto probe_aux_dev_err;
17762eea392dSJarkko Nikula 		}
17772eea392dSJarkko Nikula 	}
17782eea392dSJarkko Nikula 
1779888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1780b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1781888df395SMark Brown 
1782b7af1dafSMark Brown 	if (card->controls)
1783022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1784b7af1dafSMark Brown 
1785b8ad29deSMark Brown 	if (card->dapm_routes)
1786b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1787b8ad29deSMark Brown 					card->num_dapm_routes);
1788b8ad29deSMark Brown 
178975d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
179088bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
179175d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1792f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
179375d9ac46SMark Brown 
1794f04209a7SMark Brown 		if (dai_fmt) {
179588bd870fSBenoit Cousson 			struct snd_soc_dai **codec_dais = rtd->codec_dais;
179688bd870fSBenoit Cousson 			int j;
179788bd870fSBenoit Cousson 
179888bd870fSBenoit Cousson 			for (j = 0; j < rtd->num_codecs; j++) {
179988bd870fSBenoit Cousson 				struct snd_soc_dai *codec_dai = codec_dais[j];
180088bd870fSBenoit Cousson 
180188bd870fSBenoit Cousson 				ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
18025e4ba569SShawn Guo 				if (ret != 0 && ret != -ENOTSUPP)
180388bd870fSBenoit Cousson 					dev_warn(codec_dai->dev,
1804f110bfc7SLiam Girdwood 						 "ASoC: Failed to set DAI format: %d\n",
180575d9ac46SMark Brown 						 ret);
1806f04209a7SMark Brown 			}
180788bd870fSBenoit Cousson 		}
1808f04209a7SMark Brown 
1809f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1810fe33d4c5SStephen Warren 		if (dai_fmt &&
1811fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1812f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1813f04209a7SMark Brown 						  dai_fmt);
1814f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1815f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1816f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
1817f04209a7SMark Brown 					 ret);
1818f04209a7SMark Brown 		} else if (dai_fmt) {
1819f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1820f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1821f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1822f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1823f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1824f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1825f04209a7SMark Brown 				break;
1826f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1827f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1828f04209a7SMark Brown 				break;
1829f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1830f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1831f04209a7SMark Brown 				break;
1832f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1833f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1834f04209a7SMark Brown 				break;
1835f04209a7SMark Brown 			}
183675d9ac46SMark Brown 
183775d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1838f04209a7SMark Brown 						  dai_fmt);
18395e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
184075d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1841f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
184275d9ac46SMark Brown 					 ret);
184375d9ac46SMark Brown 		}
184475d9ac46SMark Brown 	}
184575d9ac46SMark Brown 
1846f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1847fe3e78e0SMark Brown 		 "%s", card->name);
1848f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
184922de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1850f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1851f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1852f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1853f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1854f0e8ed85SMark Brown 		case '_':
1855f0e8ed85SMark Brown 		case '-':
1856f0e8ed85SMark Brown 		case '\0':
1857f0e8ed85SMark Brown 			break;
1858f0e8ed85SMark Brown 		default:
1859f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1860f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1861f0e8ed85SMark Brown 			break;
1862f0e8ed85SMark Brown 		}
1863f0e8ed85SMark Brown 	}
1864fe3e78e0SMark Brown 
186528e9ad92SMark Brown 	if (card->late_probe) {
186628e9ad92SMark Brown 		ret = card->late_probe(card);
186728e9ad92SMark Brown 		if (ret < 0) {
1868f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
186928e9ad92SMark Brown 				card->name, ret);
187028e9ad92SMark Brown 			goto probe_aux_dev_err;
187128e9ad92SMark Brown 		}
187228e9ad92SMark Brown 	}
187328e9ad92SMark Brown 
18741633281bSStephen Warren 	if (card->fully_routed)
18757df37884SLars-Peter Clausen 		snd_soc_dapm_auto_nc_pins(card);
18761633281bSStephen Warren 
1877824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
18788c193b8dSLars-Peter Clausen 
1879f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1880fe3e78e0SMark Brown 	if (ret < 0) {
1881f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1882f110bfc7SLiam Girdwood 				ret);
18836b3ed785SAxel Lin 		goto probe_aux_dev_err;
1884fe3e78e0SMark Brown 	}
1885fe3e78e0SMark Brown 
1886fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1887f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1888f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1889f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1890fe3e78e0SMark Brown 		if (ret < 0) {
189110e8aa9aSMichał Mirosław 			dev_err(card->dev,
189210e8aa9aSMichał Mirosław 				"ASoC: failed to register AC97: %d\n", ret);
18936b3ed785SAxel Lin 			while (--i >= 0)
189402c9c7b9SMisael Lopez Cruz 				soc_unregister_ac97_dai_link(&card->rtd[i]);
18956b3ed785SAxel Lin 			goto probe_aux_dev_err;
1896fe3e78e0SMark Brown 		}
1897fe3e78e0SMark Brown 	}
1898fe3e78e0SMark Brown #endif
1899fe3e78e0SMark Brown 
1900435c5e25SMark Brown 	card->instantiated = 1;
19014f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1902f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1903b19e6e7bSMark Brown 
1904b19e6e7bSMark Brown 	return 0;
1905db2a4165SFrank Mandarino 
19062eea392dSJarkko Nikula probe_aux_dev_err:
19072eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
19082eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
19092eea392dSJarkko Nikula 
1910f0fba2adSLiam Girdwood probe_dai_err:
19110671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1912fe3e78e0SMark Brown 
1913f0fba2adSLiam Girdwood card_probe_error:
191487506549SMark Brown 	if (card->remove)
1915e7361ec4SMark Brown 		card->remove(card);
1916f0fba2adSLiam Girdwood 
1917f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1918f0fba2adSLiam Girdwood 
1919b19e6e7bSMark Brown base_error:
1920f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1921db2a4165SFrank Mandarino 
1922b19e6e7bSMark Brown 	return ret;
1923435c5e25SMark Brown }
1924435c5e25SMark Brown 
1925435c5e25SMark Brown /* probes a new socdev */
1926435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1927435c5e25SMark Brown {
1928f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1929435c5e25SMark Brown 
193070a7ca34SVinod Koul 	/*
193170a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
193270a7ca34SVinod Koul 	 * we should not be here in that case so ret error
193370a7ca34SVinod Koul 	 */
193470a7ca34SVinod Koul 	if (!card)
193570a7ca34SVinod Koul 		return -EINVAL;
193670a7ca34SVinod Koul 
1937fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1938f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1939fe4085e8SMark Brown 		 card->name);
1940fe4085e8SMark Brown 
1941435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1942435c5e25SMark Brown 	card->dev = &pdev->dev;
1943f0fba2adSLiam Girdwood 
194428d528c8SMark Brown 	return snd_soc_register_card(card);
1945435c5e25SMark Brown }
1946435c5e25SMark Brown 
1947b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1948db2a4165SFrank Mandarino {
1949db2a4165SFrank Mandarino 	int i;
1950db2a4165SFrank Mandarino 
1951f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1952f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1953f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
195443829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1955db2a4165SFrank Mandarino 	}
1956db2a4165SFrank Mandarino 
19572eea392dSJarkko Nikula 	/* remove auxiliary devices */
19582eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
19592eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
19602eea392dSJarkko Nikula 
1961f0fba2adSLiam Girdwood 	/* remove and free each DAI */
19620671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1963f0fba2adSLiam Girdwood 
1964a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1965a6052154SJarkko Nikula 
1966f0fba2adSLiam Girdwood 	/* remove the card */
196787506549SMark Brown 	if (card->remove)
1968e7361ec4SMark Brown 		card->remove(card);
1969f0fba2adSLiam Girdwood 
19700aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
19710aaae527SLars-Peter Clausen 
1972f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1973b0e26485SVinod Koul 	return 0;
1974b0e26485SVinod Koul 
1975b2dfa62cSGuennadi Liakhovetski }
1976b0e26485SVinod Koul 
1977b0e26485SVinod Koul /* removes a socdev */
1978b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1979b0e26485SVinod Koul {
1980b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1981b0e26485SVinod Koul 
1982c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1983db2a4165SFrank Mandarino 	return 0;
1984db2a4165SFrank Mandarino }
1985db2a4165SFrank Mandarino 
19866f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
198751737470SMark Brown {
19886f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1989f0fba2adSLiam Girdwood 	int i;
199051737470SMark Brown 
199151737470SMark Brown 	if (!card->instantiated)
1992416356fcSMark Brown 		return 0;
199351737470SMark Brown 
199451737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
199551737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1996f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1997f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
199843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1999f0fba2adSLiam Girdwood 	}
200051737470SMark Brown 
2001f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2002416356fcSMark Brown 
2003988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2004988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
200588bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
200688bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
200788bd870fSBenoit Cousson 		int j;
200888bd870fSBenoit Cousson 
2009988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
201088bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
201188bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
201288bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
201388bd870fSBenoit Cousson 		}
2014988e8cc4SNicolin Chen 	}
2015988e8cc4SNicolin Chen 
2016416356fcSMark Brown 	return 0;
201751737470SMark Brown }
20186f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
201951737470SMark Brown 
20206f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2021b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2022b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2023b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2024b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
20256f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2026b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2027416356fcSMark Brown };
2028deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2029416356fcSMark Brown 
2030db2a4165SFrank Mandarino /* ASoC platform driver */
2031db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2032db2a4165SFrank Mandarino 	.driver		= {
2033db2a4165SFrank Mandarino 		.name		= "soc-audio",
20348b45a209SKay Sievers 		.owner		= THIS_MODULE,
20356f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2036db2a4165SFrank Mandarino 	},
2037db2a4165SFrank Mandarino 	.probe		= soc_probe,
2038db2a4165SFrank Mandarino 	.remove		= soc_remove,
2039db2a4165SFrank Mandarino };
2040db2a4165SFrank Mandarino 
2041096e49d5SMark Brown /**
2042db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2043db2a4165SFrank Mandarino  * @codec: audio codec
2044db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2045db2a4165SFrank Mandarino  * @num: AC97 codec number
2046db2a4165SFrank Mandarino  *
2047db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2048db2a4165SFrank Mandarino  */
2049db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2050db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2051db2a4165SFrank Mandarino {
2052db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2053db2a4165SFrank Mandarino 
2054db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2055db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2056db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2057db2a4165SFrank Mandarino 		return -ENOMEM;
2058db2a4165SFrank Mandarino 	}
2059db2a4165SFrank Mandarino 
2060db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2061db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2062db2a4165SFrank Mandarino 		kfree(codec->ac97);
2063db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2064db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2065db2a4165SFrank Mandarino 		return -ENOMEM;
2066db2a4165SFrank Mandarino 	}
2067db2a4165SFrank Mandarino 
2068db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2069db2a4165SFrank Mandarino 	codec->ac97->num = num;
20700562f788SMika Westerberg 
20710562f788SMika Westerberg 	/*
20720562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
20730562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
20740562f788SMika Westerberg 	 */
20750562f788SMika Westerberg 	codec->ac97_created = 1;
20760562f788SMika Westerberg 
2077db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2078db2a4165SFrank Mandarino 	return 0;
2079db2a4165SFrank Mandarino }
2080db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2081db2a4165SFrank Mandarino 
2082741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2083741a509fSMarkus Pargmann 
2084741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2085741a509fSMarkus Pargmann {
2086741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2087741a509fSMarkus Pargmann 
2088741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2089741a509fSMarkus Pargmann 
2090741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2091741a509fSMarkus Pargmann 
2092741a509fSMarkus Pargmann 	udelay(10);
2093741a509fSMarkus Pargmann 
2094741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2095741a509fSMarkus Pargmann 
2096741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2097741a509fSMarkus Pargmann 	msleep(2);
2098741a509fSMarkus Pargmann }
2099741a509fSMarkus Pargmann 
2100741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2101741a509fSMarkus Pargmann {
2102741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2103741a509fSMarkus Pargmann 
2104741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2105741a509fSMarkus Pargmann 
2106741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2107741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2108741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2109741a509fSMarkus Pargmann 
2110741a509fSMarkus Pargmann 	udelay(10);
2111741a509fSMarkus Pargmann 
2112741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2113741a509fSMarkus Pargmann 
2114741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2115741a509fSMarkus Pargmann 	msleep(2);
2116741a509fSMarkus Pargmann }
2117741a509fSMarkus Pargmann 
2118741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev,
2119741a509fSMarkus Pargmann 		struct snd_ac97_reset_cfg *cfg)
2120741a509fSMarkus Pargmann {
2121741a509fSMarkus Pargmann 	struct pinctrl *p;
2122741a509fSMarkus Pargmann 	struct pinctrl_state *state;
2123741a509fSMarkus Pargmann 	int gpio;
2124741a509fSMarkus Pargmann 	int ret;
2125741a509fSMarkus Pargmann 
2126741a509fSMarkus Pargmann 	p = devm_pinctrl_get(dev);
2127741a509fSMarkus Pargmann 	if (IS_ERR(p)) {
2128741a509fSMarkus Pargmann 		dev_err(dev, "Failed to get pinctrl\n");
2129b7580cdeSChristoph Jaeger 		return PTR_ERR(p);
2130741a509fSMarkus Pargmann 	}
2131741a509fSMarkus Pargmann 	cfg->pctl = p;
2132741a509fSMarkus Pargmann 
2133741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-reset");
2134741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2135741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2136b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2137741a509fSMarkus Pargmann 	}
2138741a509fSMarkus Pargmann 	cfg->pstate_reset = state;
2139741a509fSMarkus Pargmann 
2140741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
2141741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2142741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2143b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2144741a509fSMarkus Pargmann 	}
2145741a509fSMarkus Pargmann 	cfg->pstate_warm_reset = state;
2146741a509fSMarkus Pargmann 
2147741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-running");
2148741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2149741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
2150b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2151741a509fSMarkus Pargmann 	}
2152741a509fSMarkus Pargmann 	cfg->pstate_run = state;
2153741a509fSMarkus Pargmann 
2154741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2155741a509fSMarkus Pargmann 	if (gpio < 0) {
2156741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sync gpio\n");
2157741a509fSMarkus Pargmann 		return gpio;
2158741a509fSMarkus Pargmann 	}
2159741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2160741a509fSMarkus Pargmann 	if (ret) {
2161741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
2162741a509fSMarkus Pargmann 		return ret;
2163741a509fSMarkus Pargmann 	}
2164741a509fSMarkus Pargmann 	cfg->gpio_sync = gpio;
2165741a509fSMarkus Pargmann 
2166741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2167741a509fSMarkus Pargmann 	if (gpio < 0) {
2168741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2169741a509fSMarkus Pargmann 		return gpio;
2170741a509fSMarkus Pargmann 	}
2171741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2172741a509fSMarkus Pargmann 	if (ret) {
2173741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2174741a509fSMarkus Pargmann 		return ret;
2175741a509fSMarkus Pargmann 	}
2176741a509fSMarkus Pargmann 	cfg->gpio_sdata = gpio;
2177741a509fSMarkus Pargmann 
2178741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2179741a509fSMarkus Pargmann 	if (gpio < 0) {
2180741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-reset gpio\n");
2181741a509fSMarkus Pargmann 		return gpio;
2182741a509fSMarkus Pargmann 	}
2183741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2184741a509fSMarkus Pargmann 	if (ret) {
2185741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
2186741a509fSMarkus Pargmann 		return ret;
2187741a509fSMarkus Pargmann 	}
2188741a509fSMarkus Pargmann 	cfg->gpio_reset = gpio;
2189741a509fSMarkus Pargmann 
2190741a509fSMarkus Pargmann 	return 0;
2191741a509fSMarkus Pargmann }
2192741a509fSMarkus Pargmann 
2193b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops;
2194f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops);
2195b047e1ccSMark Brown 
2196b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2197b047e1ccSMark Brown {
2198b047e1ccSMark Brown 	if (ops == soc_ac97_ops)
2199b047e1ccSMark Brown 		return 0;
2200b047e1ccSMark Brown 
2201b047e1ccSMark Brown 	if (soc_ac97_ops && ops)
2202b047e1ccSMark Brown 		return -EBUSY;
2203b047e1ccSMark Brown 
2204b047e1ccSMark Brown 	soc_ac97_ops = ops;
2205b047e1ccSMark Brown 
2206b047e1ccSMark Brown 	return 0;
2207b047e1ccSMark Brown }
2208b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2209b047e1ccSMark Brown 
2210db2a4165SFrank Mandarino /**
2211741a509fSMarkus Pargmann  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2212741a509fSMarkus Pargmann  *
2213741a509fSMarkus Pargmann  * This function sets the reset and warm_reset properties of ops and parses
2214741a509fSMarkus Pargmann  * the device node of pdev to get pinctrl states and gpio numbers to use.
2215741a509fSMarkus Pargmann  */
2216741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2217741a509fSMarkus Pargmann 		struct platform_device *pdev)
2218741a509fSMarkus Pargmann {
2219741a509fSMarkus Pargmann 	struct device *dev = &pdev->dev;
2220741a509fSMarkus Pargmann 	struct snd_ac97_reset_cfg cfg;
2221741a509fSMarkus Pargmann 	int ret;
2222741a509fSMarkus Pargmann 
2223741a509fSMarkus Pargmann 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2224741a509fSMarkus Pargmann 	if (ret)
2225741a509fSMarkus Pargmann 		return ret;
2226741a509fSMarkus Pargmann 
2227741a509fSMarkus Pargmann 	ret = snd_soc_set_ac97_ops(ops);
2228741a509fSMarkus Pargmann 	if (ret)
2229741a509fSMarkus Pargmann 		return ret;
2230741a509fSMarkus Pargmann 
2231741a509fSMarkus Pargmann 	ops->warm_reset = snd_soc_ac97_warm_reset;
2232741a509fSMarkus Pargmann 	ops->reset = snd_soc_ac97_reset;
2233741a509fSMarkus Pargmann 
2234741a509fSMarkus Pargmann 	snd_ac97_rst_cfg = cfg;
2235741a509fSMarkus Pargmann 	return 0;
2236741a509fSMarkus Pargmann }
2237741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2238741a509fSMarkus Pargmann 
2239741a509fSMarkus Pargmann /**
2240db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2241db2a4165SFrank Mandarino  * @codec: audio codec
2242db2a4165SFrank Mandarino  *
2243db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2244db2a4165SFrank Mandarino  */
2245db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2246db2a4165SFrank Mandarino {
2247db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2248f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
224902c9c7b9SMisael Lopez Cruz 	soc_unregister_ac97_codec(codec);
2250f0fba2adSLiam Girdwood #endif
2251db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2252db2a4165SFrank Mandarino 	kfree(codec->ac97);
2253db2a4165SFrank Mandarino 	codec->ac97 = NULL;
22540562f788SMika Westerberg 	codec->ac97_created = 0;
2255db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2256db2a4165SFrank Mandarino }
2257db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2258db2a4165SFrank Mandarino 
2259db2a4165SFrank Mandarino /**
2260db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2261db2a4165SFrank Mandarino  * @_template: control template
2262db2a4165SFrank Mandarino  * @data: control private data
2263ac11a2b3SMark Brown  * @long_name: control long name
2264efb7ac3fSMark Brown  * @prefix: control name prefix
2265db2a4165SFrank Mandarino  *
2266db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2267db2a4165SFrank Mandarino  *
2268db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2269db2a4165SFrank Mandarino  */
2270db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22713056557fSMark Brown 				  void *data, const char *long_name,
2272efb7ac3fSMark Brown 				  const char *prefix)
2273db2a4165SFrank Mandarino {
2274db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2275efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2276efb7ac3fSMark Brown 	char *name = NULL;
2277db2a4165SFrank Mandarino 
2278db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2279db2a4165SFrank Mandarino 	template.index = 0;
2280db2a4165SFrank Mandarino 
2281efb7ac3fSMark Brown 	if (!long_name)
2282efb7ac3fSMark Brown 		long_name = template.name;
2283efb7ac3fSMark Brown 
2284efb7ac3fSMark Brown 	if (prefix) {
22852b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2286efb7ac3fSMark Brown 		if (!name)
2287efb7ac3fSMark Brown 			return NULL;
2288efb7ac3fSMark Brown 
2289efb7ac3fSMark Brown 		template.name = name;
2290efb7ac3fSMark Brown 	} else {
2291efb7ac3fSMark Brown 		template.name = long_name;
2292efb7ac3fSMark Brown 	}
2293efb7ac3fSMark Brown 
2294efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2295efb7ac3fSMark Brown 
2296efb7ac3fSMark Brown 	kfree(name);
2297efb7ac3fSMark Brown 
2298efb7ac3fSMark Brown 	return kcontrol;
2299db2a4165SFrank Mandarino }
2300db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2301db2a4165SFrank Mandarino 
2302022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2303022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2304022658beSLiam Girdwood 	const char *prefix, void *data)
2305022658beSLiam Girdwood {
2306022658beSLiam Girdwood 	int err, i;
2307022658beSLiam Girdwood 
2308022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2309022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2310022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2311022658beSLiam Girdwood 						     control->name, prefix));
2312022658beSLiam Girdwood 		if (err < 0) {
2313f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2314f110bfc7SLiam Girdwood 				control->name, err);
2315022658beSLiam Girdwood 			return err;
2316022658beSLiam Girdwood 		}
2317022658beSLiam Girdwood 	}
2318022658beSLiam Girdwood 
2319022658beSLiam Girdwood 	return 0;
2320022658beSLiam Girdwood }
2321022658beSLiam Girdwood 
23224fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23234fefd698SDimitris Papastamos 					       const char *name)
23244fefd698SDimitris Papastamos {
23254fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23264fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23274fefd698SDimitris Papastamos 
23284fefd698SDimitris Papastamos 	if (unlikely(!name))
23294fefd698SDimitris Papastamos 		return NULL;
23304fefd698SDimitris Papastamos 
23314fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23324fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23334fefd698SDimitris Papastamos 			return kctl;
23344fefd698SDimitris Papastamos 	return NULL;
23354fefd698SDimitris Papastamos }
23364fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23374fefd698SDimitris Papastamos 
2338db2a4165SFrank Mandarino /**
23390f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23400f2780adSLars-Peter Clausen  *
23410f2780adSLars-Peter Clausen  * @component: Component to add controls to
23420f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23430f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23440f2780adSLars-Peter Clausen  *
23450f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23460f2780adSLars-Peter Clausen  */
23470f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23480f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23490f2780adSLars-Peter Clausen {
23500f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23510f2780adSLars-Peter Clausen 
23520f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23530f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23540f2780adSLars-Peter Clausen }
23550f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23560f2780adSLars-Peter Clausen 
23570f2780adSLars-Peter Clausen /**
2358022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2359022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
23603e8e1952SIan Molton  * duplicating this code.
23613e8e1952SIan Molton  *
23623e8e1952SIan Molton  * @codec: codec to add controls to
23633e8e1952SIan Molton  * @controls: array of controls to add
23643e8e1952SIan Molton  * @num_controls: number of elements in the array
23653e8e1952SIan Molton  *
23663e8e1952SIan Molton  * Return 0 for success, else error.
23673e8e1952SIan Molton  */
2368022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
23690f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23703e8e1952SIan Molton {
23710f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
23720f2780adSLars-Peter Clausen 		num_controls);
23733e8e1952SIan Molton }
2374022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
23753e8e1952SIan Molton 
23763e8e1952SIan Molton /**
2377a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2378022658beSLiam Girdwood  * Convenience function to add a list of controls.
2379a491a5c8SLiam Girdwood  *
2380a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2381a491a5c8SLiam Girdwood  * @controls: array of controls to add
2382a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2383a491a5c8SLiam Girdwood  *
2384a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2385a491a5c8SLiam Girdwood  */
2386a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
23870f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2388a491a5c8SLiam Girdwood {
23890f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
23900f2780adSLars-Peter Clausen 		num_controls);
2391a491a5c8SLiam Girdwood }
2392a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2393a491a5c8SLiam Girdwood 
2394a491a5c8SLiam Girdwood /**
2395022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2396022658beSLiam Girdwood  * Convenience function to add a list of controls.
2397022658beSLiam Girdwood  *
2398022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2399022658beSLiam Girdwood  * @controls: array of controls to add
2400022658beSLiam Girdwood  * @num_controls: number of elements in the array
2401022658beSLiam Girdwood  *
2402022658beSLiam Girdwood  * Return 0 for success, else error.
2403022658beSLiam Girdwood  */
2404022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2405022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2406022658beSLiam Girdwood {
2407022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2408022658beSLiam Girdwood 
2409022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2410022658beSLiam Girdwood 			NULL, soc_card);
2411022658beSLiam Girdwood }
2412022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2413022658beSLiam Girdwood 
2414022658beSLiam Girdwood /**
2415022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2416022658beSLiam Girdwood  * Convienience function to add a list of controls.
2417022658beSLiam Girdwood  *
2418022658beSLiam Girdwood  * @dai: DAI to add controls to
2419022658beSLiam Girdwood  * @controls: array of controls to add
2420022658beSLiam Girdwood  * @num_controls: number of elements in the array
2421022658beSLiam Girdwood  *
2422022658beSLiam Girdwood  * Return 0 for success, else error.
2423022658beSLiam Girdwood  */
2424022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2425022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2426022658beSLiam Girdwood {
2427022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2428022658beSLiam Girdwood 
2429022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2430022658beSLiam Girdwood 			NULL, dai);
2431022658beSLiam Girdwood }
2432022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2433022658beSLiam Girdwood 
2434022658beSLiam Girdwood /**
2435db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2436db2a4165SFrank Mandarino  * @kcontrol: mixer control
2437db2a4165SFrank Mandarino  * @uinfo: control element information
2438db2a4165SFrank Mandarino  *
2439db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2440db2a4165SFrank Mandarino  * mixer control.
2441db2a4165SFrank Mandarino  *
2442db2a4165SFrank Mandarino  * Returns 0 for success.
2443db2a4165SFrank Mandarino  */
2444db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2445db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2446db2a4165SFrank Mandarino {
2447db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2448db2a4165SFrank Mandarino 
2449db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2450db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
24519a8d38dbSTakashi Iwai 	uinfo->value.enumerated.items = e->items;
2452db2a4165SFrank Mandarino 
24539a8d38dbSTakashi Iwai 	if (uinfo->value.enumerated.item >= e->items)
24549a8d38dbSTakashi Iwai 		uinfo->value.enumerated.item = e->items - 1;
2455a19685cbSTakashi Iwai 	strlcpy(uinfo->value.enumerated.name,
2456a19685cbSTakashi Iwai 		e->texts[uinfo->value.enumerated.item],
2457a19685cbSTakashi Iwai 		sizeof(uinfo->value.enumerated.name));
2458db2a4165SFrank Mandarino 	return 0;
2459db2a4165SFrank Mandarino }
2460db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2461db2a4165SFrank Mandarino 
2462db2a4165SFrank Mandarino /**
2463db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2464db2a4165SFrank Mandarino  * @kcontrol: mixer control
2465ac11a2b3SMark Brown  * @ucontrol: control element information
2466db2a4165SFrank Mandarino  *
2467db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2468db2a4165SFrank Mandarino  *
2469db2a4165SFrank Mandarino  * Returns 0 for success.
2470db2a4165SFrank Mandarino  */
2471db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2472db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2473db2a4165SFrank Mandarino {
2474907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2475db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
247629ae2fa5SLars-Peter Clausen 	unsigned int val, item;
247729ae2fa5SLars-Peter Clausen 	unsigned int reg_val;
2478907fe36aSLars-Peter Clausen 	int ret;
2479db2a4165SFrank Mandarino 
2480907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, e->reg, &reg_val);
2481907fe36aSLars-Peter Clausen 	if (ret)
2482907fe36aSLars-Peter Clausen 		return ret;
248329ae2fa5SLars-Peter Clausen 	val = (reg_val >> e->shift_l) & e->mask;
248429ae2fa5SLars-Peter Clausen 	item = snd_soc_enum_val_to_item(e, val);
248529ae2fa5SLars-Peter Clausen 	ucontrol->value.enumerated.item[0] = item;
248629ae2fa5SLars-Peter Clausen 	if (e->shift_l != e->shift_r) {
248729ae2fa5SLars-Peter Clausen 		val = (reg_val >> e->shift_l) & e->mask;
248829ae2fa5SLars-Peter Clausen 		item = snd_soc_enum_val_to_item(e, val);
248929ae2fa5SLars-Peter Clausen 		ucontrol->value.enumerated.item[1] = item;
249029ae2fa5SLars-Peter Clausen 	}
2491db2a4165SFrank Mandarino 
2492db2a4165SFrank Mandarino 	return 0;
2493db2a4165SFrank Mandarino }
2494db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2495db2a4165SFrank Mandarino 
2496db2a4165SFrank Mandarino /**
2497db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2498db2a4165SFrank Mandarino  * @kcontrol: mixer control
2499ac11a2b3SMark Brown  * @ucontrol: control element information
2500db2a4165SFrank Mandarino  *
2501db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2502db2a4165SFrank Mandarino  *
2503db2a4165SFrank Mandarino  * Returns 0 for success.
2504db2a4165SFrank Mandarino  */
2505db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2506db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2507db2a4165SFrank Mandarino {
2508907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2509db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
251029ae2fa5SLars-Peter Clausen 	unsigned int *item = ucontrol->value.enumerated.item;
251146f5822fSDaniel Ribeiro 	unsigned int val;
251286767b7dSLars-Peter Clausen 	unsigned int mask;
2513db2a4165SFrank Mandarino 
251429ae2fa5SLars-Peter Clausen 	if (item[0] >= e->items)
2515db2a4165SFrank Mandarino 		return -EINVAL;
251629ae2fa5SLars-Peter Clausen 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
251786767b7dSLars-Peter Clausen 	mask = e->mask << e->shift_l;
2518db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
251929ae2fa5SLars-Peter Clausen 		if (item[1] >= e->items)
2520db2a4165SFrank Mandarino 			return -EINVAL;
252129ae2fa5SLars-Peter Clausen 		val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
252286767b7dSLars-Peter Clausen 		mask |= e->mask << e->shift_r;
2523db2a4165SFrank Mandarino 	}
2524db2a4165SFrank Mandarino 
2525907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, e->reg, mask, val);
2526db2a4165SFrank Mandarino }
2527db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2528db2a4165SFrank Mandarino 
2529db2a4165SFrank Mandarino /**
2530f227b88fSMarkus Pargmann  * snd_soc_read_signed - Read a codec register and interprete as signed value
2531907fe36aSLars-Peter Clausen  * @component: component
2532f227b88fSMarkus Pargmann  * @reg: Register to read
2533f227b88fSMarkus Pargmann  * @mask: Mask to use after shifting the register value
2534f227b88fSMarkus Pargmann  * @shift: Right shift of register value
2535f227b88fSMarkus Pargmann  * @sign_bit: Bit that describes if a number is negative or not.
2536907fe36aSLars-Peter Clausen  * @signed_val: Pointer to where the read value should be stored
25372e72f8e3SPeter Ujfalusi  *
2538f227b88fSMarkus Pargmann  * This functions reads a codec register. The register value is shifted right
2539f227b88fSMarkus Pargmann  * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2540f227b88fSMarkus Pargmann  * the given registervalue into a signed integer if sign_bit is non-zero.
25412e72f8e3SPeter Ujfalusi  *
2542907fe36aSLars-Peter Clausen  * Returns 0 on sucess, otherwise an error value
25432e72f8e3SPeter Ujfalusi  */
2544907fe36aSLars-Peter Clausen static int snd_soc_read_signed(struct snd_soc_component *component,
2545907fe36aSLars-Peter Clausen 	unsigned int reg, unsigned int mask, unsigned int shift,
2546907fe36aSLars-Peter Clausen 	unsigned int sign_bit, int *signed_val)
25472e72f8e3SPeter Ujfalusi {
2548f227b88fSMarkus Pargmann 	int ret;
254946f5822fSDaniel Ribeiro 	unsigned int val;
25502e72f8e3SPeter Ujfalusi 
2551907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2552907fe36aSLars-Peter Clausen 	if (ret < 0)
2553907fe36aSLars-Peter Clausen 		return ret;
25542e72f8e3SPeter Ujfalusi 
2555907fe36aSLars-Peter Clausen 	val = (val >> shift) & mask;
2556907fe36aSLars-Peter Clausen 
2557907fe36aSLars-Peter Clausen 	if (!sign_bit) {
2558907fe36aSLars-Peter Clausen 		*signed_val = val;
2559907fe36aSLars-Peter Clausen 		return 0;
2560907fe36aSLars-Peter Clausen 	}
2561f227b88fSMarkus Pargmann 
2562f227b88fSMarkus Pargmann 	/* non-negative number */
2563907fe36aSLars-Peter Clausen 	if (!(val & BIT(sign_bit))) {
2564907fe36aSLars-Peter Clausen 		*signed_val = val;
2565907fe36aSLars-Peter Clausen 		return 0;
2566907fe36aSLars-Peter Clausen 	}
2567f227b88fSMarkus Pargmann 
2568f227b88fSMarkus Pargmann 	ret = val;
2569f227b88fSMarkus Pargmann 
2570f227b88fSMarkus Pargmann 	/*
2571f227b88fSMarkus Pargmann 	 * The register most probably does not contain a full-sized int.
2572f227b88fSMarkus Pargmann 	 * Instead we have an arbitrary number of bits in a signed
2573f227b88fSMarkus Pargmann 	 * representation which has to be translated into a full-sized int.
2574f227b88fSMarkus Pargmann 	 * This is done by filling up all bits above the sign-bit.
2575f227b88fSMarkus Pargmann 	 */
2576f227b88fSMarkus Pargmann 	ret |= ~((int)(BIT(sign_bit) - 1));
2577f227b88fSMarkus Pargmann 
2578907fe36aSLars-Peter Clausen 	*signed_val = ret;
2579907fe36aSLars-Peter Clausen 
2580907fe36aSLars-Peter Clausen 	return 0;
25812e72f8e3SPeter Ujfalusi }
25822e72f8e3SPeter Ujfalusi 
25832e72f8e3SPeter Ujfalusi /**
2584db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2585db2a4165SFrank Mandarino  * @kcontrol: mixer control
2586db2a4165SFrank Mandarino  * @uinfo: control element information
2587db2a4165SFrank Mandarino  *
2588e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2589e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2590db2a4165SFrank Mandarino  *
2591db2a4165SFrank Mandarino  * Returns 0 for success.
2592db2a4165SFrank Mandarino  */
2593db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2594db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2595db2a4165SFrank Mandarino {
25964eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25974eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2598d11bb4a9SPeter Ujfalusi 	int platform_max;
2599db2a4165SFrank Mandarino 
2600d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2601d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2602d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2603d11bb4a9SPeter Ujfalusi 
2604d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2605a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2606a7a4ac86SPhilipp Zabel 	else
2607a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2608a7a4ac86SPhilipp Zabel 
2609e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2610db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2611f227b88fSMarkus Pargmann 	uinfo->value.integer.max = platform_max - mc->min;
2612db2a4165SFrank Mandarino 	return 0;
2613db2a4165SFrank Mandarino }
2614db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2615db2a4165SFrank Mandarino 
2616db2a4165SFrank Mandarino /**
2617db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2618db2a4165SFrank Mandarino  * @kcontrol: mixer control
2619ac11a2b3SMark Brown  * @ucontrol: control element information
2620db2a4165SFrank Mandarino  *
2621f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2622f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2623db2a4165SFrank Mandarino  *
2624db2a4165SFrank Mandarino  * Returns 0 for success.
2625db2a4165SFrank Mandarino  */
2626db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2627db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2628db2a4165SFrank Mandarino {
2629907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
26304eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26314eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2632815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2633f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2634815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2635815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26364eaa9819SJon Smirl 	int max = mc->max;
2637f227b88fSMarkus Pargmann 	int min = mc->min;
2638f227b88fSMarkus Pargmann 	int sign_bit = mc->sign_bit;
2639815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2640815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2641907fe36aSLars-Peter Clausen 	int val;
2642907fe36aSLars-Peter Clausen 	int ret;
2643db2a4165SFrank Mandarino 
2644f227b88fSMarkus Pargmann 	if (sign_bit)
2645f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2646f227b88fSMarkus Pargmann 
2647907fe36aSLars-Peter Clausen 	ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2648907fe36aSLars-Peter Clausen 	if (ret)
2649907fe36aSLars-Peter Clausen 		return ret;
2650907fe36aSLars-Peter Clausen 
2651907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = val - min;
2652f7915d99SPeter Ujfalusi 	if (invert)
2653db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2654a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2655f7915d99SPeter Ujfalusi 
2656f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2657f7915d99SPeter Ujfalusi 		if (reg == reg2)
2658907fe36aSLars-Peter Clausen 			ret = snd_soc_read_signed(component, reg, mask, rshift,
2659907fe36aSLars-Peter Clausen 				sign_bit, &val);
2660f7915d99SPeter Ujfalusi 		else
2661907fe36aSLars-Peter Clausen 			ret = snd_soc_read_signed(component, reg2, mask, shift,
2662907fe36aSLars-Peter Clausen 				sign_bit, &val);
2663907fe36aSLars-Peter Clausen 		if (ret)
2664907fe36aSLars-Peter Clausen 			return ret;
2665907fe36aSLars-Peter Clausen 
2666907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = val - min;
2667f7915d99SPeter Ujfalusi 		if (invert)
2668db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2669a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2670db2a4165SFrank Mandarino 	}
2671db2a4165SFrank Mandarino 
2672db2a4165SFrank Mandarino 	return 0;
2673db2a4165SFrank Mandarino }
2674db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2675db2a4165SFrank Mandarino 
2676db2a4165SFrank Mandarino /**
2677db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2678db2a4165SFrank Mandarino  * @kcontrol: mixer control
2679ac11a2b3SMark Brown  * @ucontrol: control element information
2680db2a4165SFrank Mandarino  *
2681974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2682974815baSPeter Ujfalusi  * control that spans 2 registers.
2683db2a4165SFrank Mandarino  *
2684db2a4165SFrank Mandarino  * Returns 0 for success.
2685db2a4165SFrank Mandarino  */
2686db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2687db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2688db2a4165SFrank Mandarino {
2689907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
26904eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26914eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2692815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2693974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2694815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2695815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26964eaa9819SJon Smirl 	int max = mc->max;
2697f227b88fSMarkus Pargmann 	int min = mc->min;
2698f227b88fSMarkus Pargmann 	unsigned int sign_bit = mc->sign_bit;
2699815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2700815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2701974815baSPeter Ujfalusi 	int err;
2702939d9f16SNenghua Cao 	bool type_2r = false;
2703974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2704974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2705db2a4165SFrank Mandarino 
2706f227b88fSMarkus Pargmann 	if (sign_bit)
2707f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2708f227b88fSMarkus Pargmann 
2709f227b88fSMarkus Pargmann 	val = ((ucontrol->value.integer.value[0] + min) & mask);
2710db2a4165SFrank Mandarino 	if (invert)
2711a7a4ac86SPhilipp Zabel 		val = max - val;
2712db2a4165SFrank Mandarino 	val_mask = mask << shift;
2713db2a4165SFrank Mandarino 	val = val << shift;
2714974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2715f227b88fSMarkus Pargmann 		val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2716db2a4165SFrank Mandarino 		if (invert)
2717a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2718974815baSPeter Ujfalusi 		if (reg == reg2) {
2719db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2720db2a4165SFrank Mandarino 			val |= val2 << rshift;
2721974815baSPeter Ujfalusi 		} else {
2722db2a4165SFrank Mandarino 			val2 = val2 << shift;
2723939d9f16SNenghua Cao 			type_2r = true;
2724974815baSPeter Ujfalusi 		}
2725974815baSPeter Ujfalusi 	}
2726907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
27273ff3f64bSMark Brown 	if (err < 0)
2728db2a4165SFrank Mandarino 		return err;
2729db2a4165SFrank Mandarino 
2730974815baSPeter Ujfalusi 	if (type_2r)
2731907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, reg2, val_mask,
2732907fe36aSLars-Peter Clausen 			val2);
2733974815baSPeter Ujfalusi 
2734db2a4165SFrank Mandarino 	return err;
2735db2a4165SFrank Mandarino }
2736974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2737db2a4165SFrank Mandarino 
2738e13ac2e9SMark Brown /**
27391d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
27401d99f243SBrian Austin  * @kcontrol: mixer control
27411d99f243SBrian Austin  * @ucontrol: control element information
27421d99f243SBrian Austin  *
27431d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
27441d99f243SBrian Austin  * control that spans 2 registers.
27451d99f243SBrian Austin  *
27461d99f243SBrian Austin  * Returns 0 for success.
27471d99f243SBrian Austin  */
27481d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
27491d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
27501d99f243SBrian Austin {
2751907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
27521d99f243SBrian Austin 	struct soc_mixer_control *mc =
27531d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
27541d99f243SBrian Austin 	unsigned int reg = mc->reg;
27551d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
27561d99f243SBrian Austin 	unsigned int shift = mc->shift;
27571d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
27581d99f243SBrian Austin 	int max = mc->max;
27591d99f243SBrian Austin 	int min = mc->min;
27601d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
2761907fe36aSLars-Peter Clausen 	unsigned int val;
2762907fe36aSLars-Peter Clausen 	int ret;
27631d99f243SBrian Austin 
2764907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2765907fe36aSLars-Peter Clausen 	if (ret < 0)
2766907fe36aSLars-Peter Clausen 		return ret;
27671d99f243SBrian Austin 
2768907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2769907fe36aSLars-Peter Clausen 
2770907fe36aSLars-Peter Clausen 	if (snd_soc_volsw_is_stereo(mc)) {
2771907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, reg2, &val);
2772907fe36aSLars-Peter Clausen 		if (ret < 0)
2773907fe36aSLars-Peter Clausen 			return ret;
2774907fe36aSLars-Peter Clausen 
2775907fe36aSLars-Peter Clausen 		val = ((val >> rshift) - min) & mask;
2776907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = val;
2777907fe36aSLars-Peter Clausen 	}
27781d99f243SBrian Austin 
27791d99f243SBrian Austin 	return 0;
27801d99f243SBrian Austin }
27811d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
27821d99f243SBrian Austin 
27831d99f243SBrian Austin /**
27841d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
27851d99f243SBrian Austin  * @kcontrol: mixer control
27861d99f243SBrian Austin  * @uinfo: control element information
27871d99f243SBrian Austin  *
27881d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
27891d99f243SBrian Austin  *
27901d99f243SBrian Austin  * Returns 0 for success.
27911d99f243SBrian Austin  */
27921d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
27931d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
27941d99f243SBrian Austin {
2795907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
27961d99f243SBrian Austin 	struct soc_mixer_control *mc =
27971d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
27981d99f243SBrian Austin 
27991d99f243SBrian Austin 	unsigned int reg = mc->reg;
28001d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28011d99f243SBrian Austin 	unsigned int shift = mc->shift;
28021d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28031d99f243SBrian Austin 	int max = mc->max;
28041d99f243SBrian Austin 	int min = mc->min;
28051d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
280627f1d759SBrian Austin 	int err = 0;
28071a39019eSBard Liao 	unsigned int val, val_mask, val2 = 0;
28081d99f243SBrian Austin 
28091d99f243SBrian Austin 	val_mask = mask << shift;
28101d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
28111d99f243SBrian Austin 	val = val << shift;
28121d99f243SBrian Austin 
2813907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, val_mask, val);
2814d055852eSMukund Navada 	if (err < 0)
28151d99f243SBrian Austin 		return err;
28161d99f243SBrian Austin 
28171d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
28181d99f243SBrian Austin 		val_mask = mask << rshift;
28191d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
28201d99f243SBrian Austin 		val2 = val2 << rshift;
28211d99f243SBrian Austin 
2822907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, reg2, val_mask,
2823907fe36aSLars-Peter Clausen 			val2);
28241d99f243SBrian Austin 	}
2825907fe36aSLars-Peter Clausen 	return err;
28261d99f243SBrian Austin }
28271d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
28281d99f243SBrian Austin 
28291d99f243SBrian Austin /**
2830e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2831e13ac2e9SMark Brown  * @kcontrol: mixer control
2832e13ac2e9SMark Brown  * @uinfo: control element information
2833e13ac2e9SMark Brown  *
2834e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2835e13ac2e9SMark Brown  *
2836e13ac2e9SMark Brown  * Returns 0 for success.
2837e13ac2e9SMark Brown  */
2838e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2839e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2840e13ac2e9SMark Brown {
28414eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28424eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2843d11bb4a9SPeter Ujfalusi 	int platform_max;
28444eaa9819SJon Smirl 	int min = mc->min;
2845e13ac2e9SMark Brown 
2846d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2847d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2848d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2849d11bb4a9SPeter Ujfalusi 
2850e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2851e13ac2e9SMark Brown 	uinfo->count = 2;
2852e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2853d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2854e13ac2e9SMark Brown 	return 0;
2855e13ac2e9SMark Brown }
2856e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2857e13ac2e9SMark Brown 
2858e13ac2e9SMark Brown /**
2859e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2860e13ac2e9SMark Brown  * @kcontrol: mixer control
2861ac11a2b3SMark Brown  * @ucontrol: control element information
2862e13ac2e9SMark Brown  *
2863e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2864e13ac2e9SMark Brown  *
2865e13ac2e9SMark Brown  * Returns 0 for success.
2866e13ac2e9SMark Brown  */
2867e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2868e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2869e13ac2e9SMark Brown {
28704eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28714eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2872907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2873815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2874907fe36aSLars-Peter Clausen 	unsigned int val;
28754eaa9819SJon Smirl 	int min = mc->min;
2876907fe36aSLars-Peter Clausen 	int ret;
2877907fe36aSLars-Peter Clausen 
2878907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
2879907fe36aSLars-Peter Clausen 	if (ret)
2880907fe36aSLars-Peter Clausen 		return ret;
2881e13ac2e9SMark Brown 
2882e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2883e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2884e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2885e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2886e13ac2e9SMark Brown 	return 0;
2887e13ac2e9SMark Brown }
2888e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2889e13ac2e9SMark Brown 
2890e13ac2e9SMark Brown /**
2891e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2892e13ac2e9SMark Brown  * @kcontrol: mixer control
2893ac11a2b3SMark Brown  * @ucontrol: control element information
2894e13ac2e9SMark Brown  *
2895e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2896e13ac2e9SMark Brown  *
2897e13ac2e9SMark Brown  * Returns 0 for success.
2898e13ac2e9SMark Brown  */
2899e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2900e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2901e13ac2e9SMark Brown {
29024eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29034eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2904907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2905815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
29064eaa9819SJon Smirl 	int min = mc->min;
290746f5822fSDaniel Ribeiro 	unsigned int val;
2908e13ac2e9SMark Brown 
2909e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2910e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2911e13ac2e9SMark Brown 
2912907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, reg, 0xffff, val);
2913e13ac2e9SMark Brown }
2914e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2915e13ac2e9SMark Brown 
29168c6529dbSLiam Girdwood /**
29176c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
29186c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29196c9d8cf6SAdam Thomson  * @uinfo: control element information
29206c9d8cf6SAdam Thomson  *
29216c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
29226c9d8cf6SAdam Thomson  * mixer control.
29236c9d8cf6SAdam Thomson  *
29246c9d8cf6SAdam Thomson  * returns 0 for success.
29256c9d8cf6SAdam Thomson  */
29266c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
29276c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
29286c9d8cf6SAdam Thomson {
29296c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29306c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29316c9d8cf6SAdam Thomson 	int platform_max;
29326c9d8cf6SAdam Thomson 	int min = mc->min;
29336c9d8cf6SAdam Thomson 
29346c9d8cf6SAdam Thomson 	if (!mc->platform_max)
29356c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
29366c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
29376c9d8cf6SAdam Thomson 
29386c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29399bde4f0bSMark Brown 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
29406c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
29416c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
29426c9d8cf6SAdam Thomson 
29436c9d8cf6SAdam Thomson 	return 0;
29446c9d8cf6SAdam Thomson }
29456c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
29466c9d8cf6SAdam Thomson 
29476c9d8cf6SAdam Thomson /**
29486c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
29496c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29506c9d8cf6SAdam Thomson  * @ucontrol: control element information
29516c9d8cf6SAdam Thomson  *
29526c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
29536c9d8cf6SAdam Thomson  *
29546c9d8cf6SAdam Thomson  * Returns 0 for success.
29556c9d8cf6SAdam Thomson  */
29566c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
29576c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
29586c9d8cf6SAdam Thomson {
29596c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29606c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
2961907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
29626c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
29639bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
29646c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
29656c9d8cf6SAdam Thomson 	int min = mc->min;
29666c9d8cf6SAdam Thomson 	int max = mc->max;
29676c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
29686c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
29696c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
29709bde4f0bSMark Brown 	int ret;
29716c9d8cf6SAdam Thomson 
29726c9d8cf6SAdam Thomson 	val = ((ucontrol->value.integer.value[0] + min) & mask);
29736c9d8cf6SAdam Thomson 	if (invert)
29746c9d8cf6SAdam Thomson 		val = max - val;
29756c9d8cf6SAdam Thomson 	val_mask = mask << shift;
29766c9d8cf6SAdam Thomson 	val = val << shift;
29776c9d8cf6SAdam Thomson 
2978907fe36aSLars-Peter Clausen 	ret = snd_soc_component_update_bits(component, reg, val_mask, val);
29790eaa6ccaSJoonyoung Shim 	if (ret < 0)
29809bde4f0bSMark Brown 		return ret;
29819bde4f0bSMark Brown 
29829bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
29839bde4f0bSMark Brown 		val = ((ucontrol->value.integer.value[1] + min) & mask);
29849bde4f0bSMark Brown 		if (invert)
29859bde4f0bSMark Brown 			val = max - val;
29869bde4f0bSMark Brown 		val_mask = mask << shift;
29879bde4f0bSMark Brown 		val = val << shift;
29889bde4f0bSMark Brown 
2989907fe36aSLars-Peter Clausen 		ret = snd_soc_component_update_bits(component, rreg, val_mask,
2990907fe36aSLars-Peter Clausen 			val);
29919bde4f0bSMark Brown 	}
29929bde4f0bSMark Brown 
29939bde4f0bSMark Brown 	return ret;
29946c9d8cf6SAdam Thomson }
29956c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
29966c9d8cf6SAdam Thomson 
29976c9d8cf6SAdam Thomson /**
29986c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
29996c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30006c9d8cf6SAdam Thomson  * @ucontrol: control element information
30016c9d8cf6SAdam Thomson  *
30026c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
30036c9d8cf6SAdam Thomson  *
30046c9d8cf6SAdam Thomson  * Returns 0 for success.
30056c9d8cf6SAdam Thomson  */
30066c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
30076c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30086c9d8cf6SAdam Thomson {
3009907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
30106c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30116c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
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;
3019907fe36aSLars-Peter Clausen 	unsigned int val;
3020907fe36aSLars-Peter Clausen 	int ret;
30216c9d8cf6SAdam Thomson 
3022907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
3023907fe36aSLars-Peter Clausen 	if (ret)
3024907fe36aSLars-Peter Clausen 		return ret;
3025907fe36aSLars-Peter Clausen 
3026907fe36aSLars-Peter Clausen 	ucontrol->value.integer.value[0] = (val >> shift) & mask;
30276c9d8cf6SAdam Thomson 	if (invert)
30286c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
30296c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
30306c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
30316c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] - min;
30326c9d8cf6SAdam Thomson 
30339bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
3034907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, rreg, &val);
3035907fe36aSLars-Peter Clausen 		if (ret)
3036907fe36aSLars-Peter Clausen 			return ret;
3037907fe36aSLars-Peter Clausen 
3038907fe36aSLars-Peter Clausen 		ucontrol->value.integer.value[1] = (val >> shift) & mask;
30399bde4f0bSMark Brown 		if (invert)
30409bde4f0bSMark Brown 			ucontrol->value.integer.value[1] =
30419bde4f0bSMark Brown 				max - ucontrol->value.integer.value[1];
30429bde4f0bSMark Brown 		ucontrol->value.integer.value[1] =
30439bde4f0bSMark Brown 			ucontrol->value.integer.value[1] - min;
30449bde4f0bSMark Brown 	}
30459bde4f0bSMark Brown 
30466c9d8cf6SAdam Thomson 	return 0;
30476c9d8cf6SAdam Thomson }
30486c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
30496c9d8cf6SAdam Thomson 
30506c9d8cf6SAdam Thomson /**
3051637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
3052637d3847SPeter Ujfalusi  *
3053637d3847SPeter Ujfalusi  * @codec: where to look for the control
3054637d3847SPeter Ujfalusi  * @name: Name of the control
3055637d3847SPeter Ujfalusi  * @max: new maximum limit
3056637d3847SPeter Ujfalusi  *
3057637d3847SPeter Ujfalusi  * Return 0 for success, else error.
3058637d3847SPeter Ujfalusi  */
3059637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
3060637d3847SPeter Ujfalusi 	const char *name, int max)
3061637d3847SPeter Ujfalusi {
306200200107SLars-Peter Clausen 	struct snd_card *card = codec->component.card->snd_card;
3063637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
3064637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
3065637d3847SPeter Ujfalusi 	int found = 0;
3066637d3847SPeter Ujfalusi 	int ret = -EINVAL;
3067637d3847SPeter Ujfalusi 
3068637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
3069637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
3070637d3847SPeter Ujfalusi 		return -EINVAL;
3071637d3847SPeter Ujfalusi 
3072637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
3073637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3074637d3847SPeter Ujfalusi 			found = 1;
3075637d3847SPeter Ujfalusi 			break;
3076637d3847SPeter Ujfalusi 		}
3077637d3847SPeter Ujfalusi 	}
3078637d3847SPeter Ujfalusi 	if (found) {
3079637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3080637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3081d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3082637d3847SPeter Ujfalusi 			ret = 0;
3083637d3847SPeter Ujfalusi 		}
3084637d3847SPeter Ujfalusi 	}
3085637d3847SPeter Ujfalusi 	return ret;
3086637d3847SPeter Ujfalusi }
3087637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3088637d3847SPeter Ujfalusi 
308971d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
309071d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
309171d08516SMark Brown {
3092907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
309371d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
309471d08516SMark Brown 
309571d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3096907fe36aSLars-Peter Clausen 	uinfo->count = params->num_regs * component->val_bytes;
309771d08516SMark Brown 
309871d08516SMark Brown 	return 0;
309971d08516SMark Brown }
310071d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
310171d08516SMark Brown 
310271d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
310371d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
310471d08516SMark Brown {
3105907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
310671d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
310771d08516SMark Brown 	int ret;
310871d08516SMark Brown 
3109907fe36aSLars-Peter Clausen 	if (component->regmap)
3110907fe36aSLars-Peter Clausen 		ret = regmap_raw_read(component->regmap, params->base,
311171d08516SMark Brown 				      ucontrol->value.bytes.data,
3112907fe36aSLars-Peter Clausen 				      params->num_regs * component->val_bytes);
311371d08516SMark Brown 	else
311471d08516SMark Brown 		ret = -EINVAL;
311571d08516SMark Brown 
3116f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
3117f831b055SMark Brown 	if (ret == 0 && params->mask) {
3118907fe36aSLars-Peter Clausen 		switch (component->val_bytes) {
3119f831b055SMark Brown 		case 1:
3120f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
3121f831b055SMark Brown 			break;
3122f831b055SMark Brown 		case 2:
3123f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
31248f1ec93aSCharles Keepax 				&= cpu_to_be16(~params->mask);
3125f831b055SMark Brown 			break;
3126f831b055SMark Brown 		case 4:
3127f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
31288f1ec93aSCharles Keepax 				&= cpu_to_be32(~params->mask);
3129f831b055SMark Brown 			break;
3130f831b055SMark Brown 		default:
3131f831b055SMark Brown 			return -EINVAL;
3132f831b055SMark Brown 		}
3133f831b055SMark Brown 	}
3134f831b055SMark Brown 
313571d08516SMark Brown 	return ret;
313671d08516SMark Brown }
313771d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
313871d08516SMark Brown 
313971d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
314071d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
314171d08516SMark Brown {
3142907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
314371d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
3144f831b055SMark Brown 	int ret, len;
3145a1a564edSNenghua Cao 	unsigned int val, mask;
3146f831b055SMark Brown 	void *data;
314771d08516SMark Brown 
3148907fe36aSLars-Peter Clausen 	if (!component->regmap)
3149f831b055SMark Brown 		return -EINVAL;
3150f831b055SMark Brown 
3151907fe36aSLars-Peter Clausen 	len = params->num_regs * component->val_bytes;
3152f831b055SMark Brown 
3153b5a8fe43SMark Brown 	data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3154b5a8fe43SMark Brown 	if (!data)
3155b5a8fe43SMark Brown 		return -ENOMEM;
3156b5a8fe43SMark Brown 
3157f831b055SMark Brown 	/*
3158f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3159f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3160f831b055SMark Brown 	 * copy.
3161f831b055SMark Brown 	 */
3162f831b055SMark Brown 	if (params->mask) {
3163907fe36aSLars-Peter Clausen 		ret = regmap_read(component->regmap, params->base, &val);
3164f831b055SMark Brown 		if (ret != 0)
3165e8b18addSWei Yongjun 			goto out;
3166f831b055SMark Brown 
3167f831b055SMark Brown 		val &= params->mask;
3168f831b055SMark Brown 
3169907fe36aSLars-Peter Clausen 		switch (component->val_bytes) {
3170f831b055SMark Brown 		case 1:
3171f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3172f831b055SMark Brown 			((u8 *)data)[0] |= val;
3173f831b055SMark Brown 			break;
3174f831b055SMark Brown 		case 2:
3175a1a564edSNenghua Cao 			mask = ~params->mask;
3176907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3177a1a564edSNenghua Cao 							&mask, &mask);
3178a1a564edSNenghua Cao 			if (ret != 0)
3179a1a564edSNenghua Cao 				goto out;
3180a1a564edSNenghua Cao 
3181a1a564edSNenghua Cao 			((u16 *)data)[0] &= mask;
3182a1a564edSNenghua Cao 
3183907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3184a1a564edSNenghua Cao 							&val, &val);
3185a1a564edSNenghua Cao 			if (ret != 0)
3186a1a564edSNenghua Cao 				goto out;
3187a1a564edSNenghua Cao 
3188a1a564edSNenghua Cao 			((u16 *)data)[0] |= val;
3189f831b055SMark Brown 			break;
3190f831b055SMark Brown 		case 4:
3191a1a564edSNenghua Cao 			mask = ~params->mask;
3192907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3193a1a564edSNenghua Cao 							&mask, &mask);
3194a1a564edSNenghua Cao 			if (ret != 0)
3195a1a564edSNenghua Cao 				goto out;
3196a1a564edSNenghua Cao 
3197a1a564edSNenghua Cao 			((u32 *)data)[0] &= mask;
3198a1a564edSNenghua Cao 
3199907fe36aSLars-Peter Clausen 			ret = regmap_parse_val(component->regmap,
3200a1a564edSNenghua Cao 							&val, &val);
3201a1a564edSNenghua Cao 			if (ret != 0)
3202a1a564edSNenghua Cao 				goto out;
3203a1a564edSNenghua Cao 
3204a1a564edSNenghua Cao 			((u32 *)data)[0] |= val;
3205f831b055SMark Brown 			break;
3206f831b055SMark Brown 		default:
3207e8b18addSWei Yongjun 			ret = -EINVAL;
3208e8b18addSWei Yongjun 			goto out;
3209f831b055SMark Brown 		}
3210f831b055SMark Brown 	}
3211f831b055SMark Brown 
3212907fe36aSLars-Peter Clausen 	ret = regmap_raw_write(component->regmap, params->base,
3213f831b055SMark Brown 			       data, len);
3214f831b055SMark Brown 
3215e8b18addSWei Yongjun out:
3216f831b055SMark Brown 	kfree(data);
321771d08516SMark Brown 
321871d08516SMark Brown 	return ret;
321971d08516SMark Brown }
322071d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
322171d08516SMark Brown 
3222d9881208SVinod Koul int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3223d9881208SVinod Koul 			struct snd_ctl_elem_info *ucontrol)
3224d9881208SVinod Koul {
3225d9881208SVinod Koul 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3226d9881208SVinod Koul 
3227d9881208SVinod Koul 	ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3228d9881208SVinod Koul 	ucontrol->count = params->max;
3229d9881208SVinod Koul 
3230d9881208SVinod Koul 	return 0;
3231d9881208SVinod Koul }
3232d9881208SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3233d9881208SVinod Koul 
32347523a271SOmair Mohammed Abdullah int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
32357523a271SOmair Mohammed Abdullah 				unsigned int size, unsigned int __user *tlv)
32367523a271SOmair Mohammed Abdullah {
32377523a271SOmair Mohammed Abdullah 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
32387523a271SOmair Mohammed Abdullah 	unsigned int count = size < params->max ? size : params->max;
32397523a271SOmair Mohammed Abdullah 	int ret = -ENXIO;
32407523a271SOmair Mohammed Abdullah 
32417523a271SOmair Mohammed Abdullah 	switch (op_flag) {
32427523a271SOmair Mohammed Abdullah 	case SNDRV_CTL_TLV_OP_READ:
32437523a271SOmair Mohammed Abdullah 		if (params->get)
32447523a271SOmair Mohammed Abdullah 			ret = params->get(tlv, count);
32457523a271SOmair Mohammed Abdullah 		break;
32467523a271SOmair Mohammed Abdullah 	case SNDRV_CTL_TLV_OP_WRITE:
32477523a271SOmair Mohammed Abdullah 		if (params->put)
32487523a271SOmair Mohammed Abdullah 			ret = params->put(tlv, count);
32497523a271SOmair Mohammed Abdullah 		break;
32507523a271SOmair Mohammed Abdullah 	}
32517523a271SOmair Mohammed Abdullah 	return ret;
32527523a271SOmair Mohammed Abdullah }
32537523a271SOmair Mohammed Abdullah EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
32547523a271SOmair Mohammed Abdullah 
3255b6f4bb38Sapatard@mandriva.com /**
32564183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
32574183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
32584183eed2SKristoffer KARLSSON  * @uinfo: control element information
32594183eed2SKristoffer KARLSSON  *
32604183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
32614183eed2SKristoffer KARLSSON  * span multiple codec registers which together
32624183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
32634183eed2SKristoffer KARLSSON  *
32644183eed2SKristoffer KARLSSON  * Returns 0 for success.
32654183eed2SKristoffer KARLSSON  */
32664183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
32674183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
32684183eed2SKristoffer KARLSSON {
32694183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
32704183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
32714183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
32724183eed2SKristoffer KARLSSON 	uinfo->count = 1;
32734183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
32744183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
32754183eed2SKristoffer KARLSSON 
32764183eed2SKristoffer KARLSSON 	return 0;
32774183eed2SKristoffer KARLSSON }
32784183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
32794183eed2SKristoffer KARLSSON 
32804183eed2SKristoffer KARLSSON /**
32814183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
32824183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
32834183eed2SKristoffer KARLSSON  * @ucontrol: control element information
32844183eed2SKristoffer KARLSSON  *
32854183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
32864183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
32874183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
32884183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
32894183eed2SKristoffer KARLSSON  * across the multiple codec registers.
32904183eed2SKristoffer KARLSSON  *
32914183eed2SKristoffer KARLSSON  * Returns 0 for success.
32924183eed2SKristoffer KARLSSON  */
32934183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
32944183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
32954183eed2SKristoffer KARLSSON {
3296907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
32974183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
32984183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
32994183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33004183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
3301907fe36aSLars-Peter Clausen 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
33024183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33034183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33044183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33054183eed2SKristoffer KARLSSON 	long min = mc->min;
33064183eed2SKristoffer KARLSSON 	long max = mc->max;
33074183eed2SKristoffer KARLSSON 	long val = 0;
3308907fe36aSLars-Peter Clausen 	unsigned int regval;
33094183eed2SKristoffer KARLSSON 	unsigned int i;
3310907fe36aSLars-Peter Clausen 	int ret;
33114183eed2SKristoffer KARLSSON 
33124183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
3313907fe36aSLars-Peter Clausen 		ret = snd_soc_component_read(component, regbase+i, &regval);
3314907fe36aSLars-Peter Clausen 		if (ret)
3315907fe36aSLars-Peter Clausen 			return ret;
3316907fe36aSLars-Peter Clausen 		val |= (regval & regwmask) << (regwshift*(regcount-i-1));
33174183eed2SKristoffer KARLSSON 	}
33184183eed2SKristoffer KARLSSON 	val &= mask;
33194183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
33204183eed2SKristoffer KARLSSON 		val |= ~mask;
33214183eed2SKristoffer KARLSSON 	if (invert)
33224183eed2SKristoffer KARLSSON 		val = max - val;
33234183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
33244183eed2SKristoffer KARLSSON 
33254183eed2SKristoffer KARLSSON 	return 0;
33264183eed2SKristoffer KARLSSON }
33274183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
33284183eed2SKristoffer KARLSSON 
33294183eed2SKristoffer KARLSSON /**
33304183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
33314183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33324183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33334183eed2SKristoffer KARLSSON  *
33344183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
33354183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33364183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33374183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33384183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33394183eed2SKristoffer KARLSSON  *
33404183eed2SKristoffer KARLSSON  * Returns 0 for success.
33414183eed2SKristoffer KARLSSON  */
33424183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
33434183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33444183eed2SKristoffer KARLSSON {
3345907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
33464183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33474183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33484183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33494183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
3350907fe36aSLars-Peter Clausen 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
33514183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33524183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33534183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33544183eed2SKristoffer KARLSSON 	long max = mc->max;
33554183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
33564183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
33574183eed2SKristoffer KARLSSON 	int err;
33584183eed2SKristoffer KARLSSON 
33594183eed2SKristoffer KARLSSON 	if (invert)
33604183eed2SKristoffer KARLSSON 		val = max - val;
33614183eed2SKristoffer KARLSSON 	val &= mask;
33624183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
33634183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
33644183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3365907fe36aSLars-Peter Clausen 		err = snd_soc_component_update_bits(component, regbase+i,
33664183eed2SKristoffer KARLSSON 				regmask, regval);
33674183eed2SKristoffer KARLSSON 		if (err < 0)
33684183eed2SKristoffer KARLSSON 			return err;
33694183eed2SKristoffer KARLSSON 	}
33704183eed2SKristoffer KARLSSON 
33714183eed2SKristoffer KARLSSON 	return 0;
33724183eed2SKristoffer KARLSSON }
33734183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
33744183eed2SKristoffer KARLSSON 
33754183eed2SKristoffer KARLSSON /**
3376dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3377dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3378dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3379dd7b10b3SKristoffer KARLSSON  *
3380dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3381dd7b10b3SKristoffer KARLSSON  *
3382dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3383dd7b10b3SKristoffer KARLSSON  */
3384dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3385dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3386dd7b10b3SKristoffer KARLSSON {
3387907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3388dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3389dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3390dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3391dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3392dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3393dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3394907fe36aSLars-Peter Clausen 	unsigned int val;
3395907fe36aSLars-Peter Clausen 	int ret;
3396907fe36aSLars-Peter Clausen 
3397907fe36aSLars-Peter Clausen 	ret = snd_soc_component_read(component, reg, &val);
3398907fe36aSLars-Peter Clausen 	if (ret)
3399907fe36aSLars-Peter Clausen 		return ret;
3400907fe36aSLars-Peter Clausen 
3401907fe36aSLars-Peter Clausen 	val &= mask;
3402dd7b10b3SKristoffer KARLSSON 
3403dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3404dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3405dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3406dd7b10b3SKristoffer KARLSSON 
3407dd7b10b3SKristoffer KARLSSON 	return 0;
3408dd7b10b3SKristoffer KARLSSON }
3409dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3410dd7b10b3SKristoffer KARLSSON 
3411dd7b10b3SKristoffer KARLSSON /**
3412dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3413dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3414dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3415dd7b10b3SKristoffer KARLSSON  *
3416dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3417dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3418dd7b10b3SKristoffer KARLSSON  *
3419dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3420dd7b10b3SKristoffer KARLSSON  */
3421dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3422dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3423dd7b10b3SKristoffer KARLSSON {
3424907fe36aSLars-Peter Clausen 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3425dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3426dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3427dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3428dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3429dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3430dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3431dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3432dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3433dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3434dd7b10b3SKristoffer KARLSSON 	int err;
3435dd7b10b3SKristoffer KARLSSON 
3436907fe36aSLars-Peter Clausen 	err = snd_soc_component_update_bits(component, reg, mask, val1);
3437dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3438dd7b10b3SKristoffer KARLSSON 		return err;
3439dd7b10b3SKristoffer KARLSSON 
3440907fe36aSLars-Peter Clausen 	return snd_soc_component_update_bits(component, reg, mask, val2);
3441dd7b10b3SKristoffer KARLSSON }
3442dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3443dd7b10b3SKristoffer KARLSSON 
3444dd7b10b3SKristoffer KARLSSON /**
34458c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
34468c6529dbSLiam Girdwood  * @dai: DAI
34478c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
34488c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
34498c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
34508c6529dbSLiam Girdwood  *
34518c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
34528c6529dbSLiam Girdwood  */
34538c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
34548c6529dbSLiam Girdwood 	unsigned int freq, int dir)
34558c6529dbSLiam Girdwood {
3456f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3457f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3458ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3459da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3460ec4ee52aSMark Brown 						      freq, dir);
34618c6529dbSLiam Girdwood 	else
34621104a9c8SMark Brown 		return -ENOTSUPP;
34638c6529dbSLiam Girdwood }
34648c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
34658c6529dbSLiam Girdwood 
34668c6529dbSLiam Girdwood /**
3467ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3468ec4ee52aSMark Brown  * @codec: CODEC
3469ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3470da1c6ea6SMark Brown  * @source: Source for the clock
3471ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3472ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3473ec4ee52aSMark Brown  *
3474ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3475ec4ee52aSMark Brown  */
3476ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3477da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3478ec4ee52aSMark Brown {
3479ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3480da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3481da1c6ea6SMark Brown 						 freq, dir);
3482ec4ee52aSMark Brown 	else
34831104a9c8SMark Brown 		return -ENOTSUPP;
3484ec4ee52aSMark Brown }
3485ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3486ec4ee52aSMark Brown 
3487ec4ee52aSMark Brown /**
34888c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
34898c6529dbSLiam Girdwood  * @dai: DAI
3490ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
34918c6529dbSLiam Girdwood  * @div: new clock divisor.
34928c6529dbSLiam Girdwood  *
34938c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
34948c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
34958c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
34968c6529dbSLiam Girdwood  */
34978c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
34988c6529dbSLiam Girdwood 	int div_id, int div)
34998c6529dbSLiam Girdwood {
3500f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3501f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
35028c6529dbSLiam Girdwood 	else
35038c6529dbSLiam Girdwood 		return -EINVAL;
35048c6529dbSLiam Girdwood }
35058c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
35068c6529dbSLiam Girdwood 
35078c6529dbSLiam Girdwood /**
35088c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
35098c6529dbSLiam Girdwood  * @dai: DAI
35108c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
351185488037SMark Brown  * @source: DAI specific source for the PLL
35128c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
35138c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
35148c6529dbSLiam Girdwood  *
35158c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
35168c6529dbSLiam Girdwood  */
351785488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
351885488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
35198c6529dbSLiam Girdwood {
3520f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3521f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
352285488037SMark Brown 					 freq_in, freq_out);
3523ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3524ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3525ec4ee52aSMark Brown 						   freq_in, freq_out);
35268c6529dbSLiam Girdwood 	else
35278c6529dbSLiam Girdwood 		return -EINVAL;
35288c6529dbSLiam Girdwood }
35298c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
35308c6529dbSLiam Girdwood 
3531ec4ee52aSMark Brown /*
3532ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3533ec4ee52aSMark Brown  * @codec: CODEC
3534ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3535ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3536ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3537ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3538ec4ee52aSMark Brown  *
3539ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3540ec4ee52aSMark Brown  */
3541ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3542ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3543ec4ee52aSMark Brown {
3544ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3545ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3546ec4ee52aSMark Brown 					      freq_in, freq_out);
3547ec4ee52aSMark Brown 	else
3548ec4ee52aSMark Brown 		return -EINVAL;
3549ec4ee52aSMark Brown }
3550ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3551ec4ee52aSMark Brown 
35528c6529dbSLiam Girdwood /**
3553e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3554e54cf76bSLiam Girdwood  * @dai: DAI
3555e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
3556e54cf76bSLiam Girdwood  *
3557e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
3558e54cf76bSLiam Girdwood  */
3559e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3560e54cf76bSLiam Girdwood {
3561e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
3562e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
3563e54cf76bSLiam Girdwood 	else
3564e54cf76bSLiam Girdwood 		return -EINVAL;
3565e54cf76bSLiam Girdwood }
3566e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3567e54cf76bSLiam Girdwood 
3568e54cf76bSLiam Girdwood /**
35698c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
35708c6529dbSLiam Girdwood  * @dai: DAI
35718c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
35728c6529dbSLiam Girdwood  *
35738c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
35748c6529dbSLiam Girdwood  */
35758c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
35768c6529dbSLiam Girdwood {
35775e4ba569SShawn Guo 	if (dai->driver == NULL)
35788c6529dbSLiam Girdwood 		return -EINVAL;
35795e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
35805e4ba569SShawn Guo 		return -ENOTSUPP;
35815e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
35828c6529dbSLiam Girdwood }
35838c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
35848c6529dbSLiam Girdwood 
35858c6529dbSLiam Girdwood /**
3586e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
358789c67857SXiubo Li  * @slots: Number of slots in use.
358889c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
358989c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
359089c67857SXiubo Li  *
359189c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
359289c67857SXiubo Li  */
3593e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
359489c67857SXiubo Li 					  unsigned int *tx_mask,
359589c67857SXiubo Li 					  unsigned int *rx_mask)
359689c67857SXiubo Li {
359789c67857SXiubo Li 	if (*tx_mask || *rx_mask)
359889c67857SXiubo Li 		return 0;
359989c67857SXiubo Li 
360089c67857SXiubo Li 	if (!slots)
360189c67857SXiubo Li 		return -EINVAL;
360289c67857SXiubo Li 
360389c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
360489c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
360589c67857SXiubo Li 
360689c67857SXiubo Li 	return 0;
360789c67857SXiubo Li }
360889c67857SXiubo Li 
360989c67857SXiubo Li /**
36108c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
36118c6529dbSLiam Girdwood  * @dai: DAI
3612a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3613a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
36148c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3615a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
36168c6529dbSLiam Girdwood  *
36178c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
36188c6529dbSLiam Girdwood  * specific.
36198c6529dbSLiam Girdwood  */
36208c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3621a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
36228c6529dbSLiam Girdwood {
3623e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3624e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
362589c67857SXiubo Li 						&tx_mask, &rx_mask);
362689c67857SXiubo Li 	else
3627e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
362889c67857SXiubo Li 
362988bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
363088bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
363188bd870fSBenoit Cousson 
3632f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3633f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3634a5479e38SDaniel Ribeiro 				slots, slot_width);
36358c6529dbSLiam Girdwood 	else
3636b2cbb6e1SXiubo Li 		return -ENOTSUPP;
36378c6529dbSLiam Girdwood }
36388c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
36398c6529dbSLiam Girdwood 
36408c6529dbSLiam Girdwood /**
3641472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3642472df3cbSBarry Song  * @dai: DAI
3643472df3cbSBarry Song  * @tx_num: how many TX channels
3644472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3645472df3cbSBarry Song  *           0~num-1 uses
3646472df3cbSBarry Song  * @rx_num: how many RX channels
3647472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3648472df3cbSBarry Song  *           0~num-1 uses
3649472df3cbSBarry Song  *
3650472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3651472df3cbSBarry Song  */
3652472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3653472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3654472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3655472df3cbSBarry Song {
3656f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3657f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3658472df3cbSBarry Song 			rx_num, rx_slot);
3659472df3cbSBarry Song 	else
3660472df3cbSBarry Song 		return -EINVAL;
3661472df3cbSBarry Song }
3662472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3663472df3cbSBarry Song 
3664472df3cbSBarry Song /**
36658c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
36668c6529dbSLiam Girdwood  * @dai: DAI
36678c6529dbSLiam Girdwood  * @tristate: tristate enable
36688c6529dbSLiam Girdwood  *
36698c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
36708c6529dbSLiam Girdwood  */
36718c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
36728c6529dbSLiam Girdwood {
3673f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3674f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
36758c6529dbSLiam Girdwood 	else
36768c6529dbSLiam Girdwood 		return -EINVAL;
36778c6529dbSLiam Girdwood }
36788c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
36798c6529dbSLiam Girdwood 
36808c6529dbSLiam Girdwood /**
36818c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
36828c6529dbSLiam Girdwood  * @dai: DAI
36838c6529dbSLiam Girdwood  * @mute: mute enable
3684da18396fSMark Brown  * @direction: stream to mute
36858c6529dbSLiam Girdwood  *
36868c6529dbSLiam Girdwood  * Mutes the DAI DAC.
36878c6529dbSLiam Girdwood  */
3688da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3689da18396fSMark Brown 			     int direction)
36908c6529dbSLiam Girdwood {
3691da18396fSMark Brown 	if (!dai->driver)
3692da18396fSMark Brown 		return -ENOTSUPP;
3693da18396fSMark Brown 
3694da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
3695da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
3696da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3697da18396fSMark Brown 		 dai->driver->ops->digital_mute)
3698f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
36998c6529dbSLiam Girdwood 	else
370004570c62SMark Brown 		return -ENOTSUPP;
37018c6529dbSLiam Girdwood }
37028c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
37038c6529dbSLiam Girdwood 
370488bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card,
370588bd870fSBenoit Cousson 				   struct snd_soc_dai_link *dai_link)
370688bd870fSBenoit Cousson {
370788bd870fSBenoit Cousson 	/* Legacy codec/codec_dai link is a single entry in multicodec */
370888bd870fSBenoit Cousson 	if (dai_link->codec_name || dai_link->codec_of_node ||
370988bd870fSBenoit Cousson 	    dai_link->codec_dai_name) {
371088bd870fSBenoit Cousson 		dai_link->num_codecs = 1;
371188bd870fSBenoit Cousson 
371288bd870fSBenoit Cousson 		dai_link->codecs = devm_kzalloc(card->dev,
371388bd870fSBenoit Cousson 				sizeof(struct snd_soc_dai_link_component),
371488bd870fSBenoit Cousson 				GFP_KERNEL);
371588bd870fSBenoit Cousson 		if (!dai_link->codecs)
371688bd870fSBenoit Cousson 			return -ENOMEM;
371788bd870fSBenoit Cousson 
371888bd870fSBenoit Cousson 		dai_link->codecs[0].name = dai_link->codec_name;
371988bd870fSBenoit Cousson 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
372088bd870fSBenoit Cousson 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
372188bd870fSBenoit Cousson 	}
372288bd870fSBenoit Cousson 
372388bd870fSBenoit Cousson 	if (!dai_link->codecs) {
372488bd870fSBenoit Cousson 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
372588bd870fSBenoit Cousson 		return -EINVAL;
372688bd870fSBenoit Cousson 	}
372788bd870fSBenoit Cousson 
372888bd870fSBenoit Cousson 	return 0;
372988bd870fSBenoit Cousson }
373088bd870fSBenoit Cousson 
3731c5af3a2eSMark Brown /**
3732c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3733c5af3a2eSMark Brown  *
3734ac11a2b3SMark Brown  * @card: Card to register
3735c5af3a2eSMark Brown  *
3736c5af3a2eSMark Brown  */
373770a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3738c5af3a2eSMark Brown {
373988bd870fSBenoit Cousson 	int i, j, ret;
3740f0fba2adSLiam Girdwood 
3741c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3742c5af3a2eSMark Brown 		return -EINVAL;
3743c5af3a2eSMark Brown 
37445a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
37455a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
37465a504963SStephen Warren 
374788bd870fSBenoit Cousson 		ret = snd_soc_init_multicodec(card, link);
374888bd870fSBenoit Cousson 		if (ret) {
374988bd870fSBenoit Cousson 			dev_err(card->dev, "ASoC: failed to init multicodec\n");
375088bd870fSBenoit Cousson 			return ret;
375188bd870fSBenoit Cousson 		}
375288bd870fSBenoit Cousson 
375388bd870fSBenoit Cousson 		for (j = 0; j < link->num_codecs; j++) {
37545a504963SStephen Warren 			/*
37555a504963SStephen Warren 			 * Codec must be specified by 1 of name or OF node,
37565a504963SStephen Warren 			 * not both or neither.
37575a504963SStephen Warren 			 */
375888bd870fSBenoit Cousson 			if (!!link->codecs[j].name ==
375988bd870fSBenoit Cousson 			    !!link->codecs[j].of_node) {
376088bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
376110e8aa9aSMichał Mirosław 					link->name);
37625a504963SStephen Warren 				return -EINVAL;
37635a504963SStephen Warren 			}
3764bc92657aSStephen Warren 			/* Codec DAI name must be specified */
376588bd870fSBenoit Cousson 			if (!link->codecs[j].dai_name) {
376688bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
376710e8aa9aSMichał Mirosław 					link->name);
3768bc92657aSStephen Warren 				return -EINVAL;
3769bc92657aSStephen Warren 			}
377088bd870fSBenoit Cousson 		}
37715a504963SStephen Warren 
37725a504963SStephen Warren 		/*
37735a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
37745a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
37755a504963SStephen Warren 		 */
37765a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
377710e8aa9aSMichał Mirosław 			dev_err(card->dev,
377810e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
377910e8aa9aSMichał Mirosław 				link->name);
37805a504963SStephen Warren 			return -EINVAL;
37815a504963SStephen Warren 		}
37825a504963SStephen Warren 
37835a504963SStephen Warren 		/*
3784bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3785bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3786bc92657aSStephen Warren 		 * name alone..
37875a504963SStephen Warren 		 */
3788bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
378910e8aa9aSMichał Mirosław 			dev_err(card->dev,
379010e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
379110e8aa9aSMichał Mirosław 				link->name);
3792bc92657aSStephen Warren 			return -EINVAL;
3793bc92657aSStephen Warren 		}
3794bc92657aSStephen Warren 		/*
3795bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3796bc92657aSStephen Warren 		 * specified
3797bc92657aSStephen Warren 		 */
3798bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3799bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
380010e8aa9aSMichał Mirosław 			dev_err(card->dev,
380110e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
380210e8aa9aSMichał Mirosław 				link->name);
38035a504963SStephen Warren 			return -EINVAL;
38045a504963SStephen Warren 		}
38055a504963SStephen Warren 	}
38065a504963SStephen Warren 
3807ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3808ed77cc12SMark Brown 
3809111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3810111c6419SStephen Warren 
3811150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3812150dd2f8SVinod Koul 
3813181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3814181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
38152eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3816f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3817f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3818f0fba2adSLiam Girdwood 		return -ENOMEM;
3819a7dbb603SLiam Girdwood 	card->num_rtd = 0;
38202eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3821f0fba2adSLiam Girdwood 
38225f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_links; i++) {
38235f3484acSLars-Peter Clausen 		card->rtd[i].card = card;
3824f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
382588bd870fSBenoit Cousson 		card->rtd[i].codec_dais = devm_kzalloc(card->dev,
382688bd870fSBenoit Cousson 					sizeof(struct snd_soc_dai *) *
382788bd870fSBenoit Cousson 					(card->rtd[i].dai_link->num_codecs),
382888bd870fSBenoit Cousson 					GFP_KERNEL);
382988bd870fSBenoit Cousson 		if (card->rtd[i].codec_dais == NULL)
383088bd870fSBenoit Cousson 			return -ENOMEM;
38315f3484acSLars-Peter Clausen 	}
38325f3484acSLars-Peter Clausen 
38335f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_aux_devs; i++)
38345f3484acSLars-Peter Clausen 		card->rtd_aux[i].card = card;
3835f0fba2adSLiam Girdwood 
3836db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3837c5af3a2eSMark Brown 	card->instantiated = 0;
3838f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3839a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3840c5af3a2eSMark Brown 
3841b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3842b19e6e7bSMark Brown 	if (ret != 0)
3843b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3844c5af3a2eSMark Brown 
3845988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
3846988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
384788bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
384888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
384988bd870fSBenoit Cousson 		int j;
385088bd870fSBenoit Cousson 
385188bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
385288bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3853988e8cc4SNicolin Chen 			if (!codec_dai->active)
3854988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
385588bd870fSBenoit Cousson 		}
385688bd870fSBenoit Cousson 
3857988e8cc4SNicolin Chen 		if (!cpu_dai->active)
3858988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
3859988e8cc4SNicolin Chen 	}
3860988e8cc4SNicolin Chen 
3861b19e6e7bSMark Brown 	return ret;
3862c5af3a2eSMark Brown }
386370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3864c5af3a2eSMark Brown 
3865c5af3a2eSMark Brown /**
3866c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3867c5af3a2eSMark Brown  *
3868ac11a2b3SMark Brown  * @card: Card to unregister
3869c5af3a2eSMark Brown  *
3870c5af3a2eSMark Brown  */
387170a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3872c5af3a2eSMark Brown {
3873b0e26485SVinod Koul 	if (card->instantiated)
3874b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3875f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3876c5af3a2eSMark Brown 
3877c5af3a2eSMark Brown 	return 0;
3878c5af3a2eSMark Brown }
387970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3880c5af3a2eSMark Brown 
3881f0fba2adSLiam Girdwood /*
3882f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3883f0fba2adSLiam Girdwood  * and sanitizing names.
3884f0fba2adSLiam Girdwood  */
38850b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3886f0fba2adSLiam Girdwood {
3887f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3888f0fba2adSLiam Girdwood 	int id1, id2;
3889f0fba2adSLiam Girdwood 
3890f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3891f0fba2adSLiam Girdwood 		return NULL;
3892f0fba2adSLiam Girdwood 
389358818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3894f0fba2adSLiam Girdwood 
3895f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3896f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3897f0fba2adSLiam Girdwood 	if (found) {
3898f0fba2adSLiam Girdwood 		/* get ID */
3899f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3900f0fba2adSLiam Girdwood 
3901f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3902f0fba2adSLiam Girdwood 			if (*id == -1)
3903f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3904f0fba2adSLiam Girdwood 		}
3905f0fba2adSLiam Girdwood 
3906f0fba2adSLiam Girdwood 	} else {
3907f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3908f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3909f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3910f0fba2adSLiam Girdwood 
3911f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
391205899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3913f0fba2adSLiam Girdwood 
3914f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3915f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
391658818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3917f0fba2adSLiam Girdwood 		} else
3918f0fba2adSLiam Girdwood 			*id = 0;
3919f0fba2adSLiam Girdwood 	}
3920f0fba2adSLiam Girdwood 
3921f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3922f0fba2adSLiam Girdwood }
3923f0fba2adSLiam Girdwood 
3924f0fba2adSLiam Girdwood /*
3925f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3926f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3927f0fba2adSLiam Girdwood  */
3928f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3929f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3930f0fba2adSLiam Girdwood {
3931f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
393210e8aa9aSMichał Mirosław 		dev_err(dev,
393310e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
393410e8aa9aSMichał Mirosław 			dev_name(dev));
3935f0fba2adSLiam Girdwood 		return NULL;
3936f0fba2adSLiam Girdwood 	}
3937f0fba2adSLiam Girdwood 
3938f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3939f0fba2adSLiam Girdwood }
3940f0fba2adSLiam Girdwood 
39419115171aSMark Brown /**
394232c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
39439115171aSMark Brown  *
394432c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
39459115171aSMark Brown  */
394632c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
39479115171aSMark Brown {
39485c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
39499115171aSMark Brown 
39505c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
395132c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
395232c9ba54SLars-Peter Clausen 			dai->name);
39539115171aSMark Brown 		list_del(&dai->list);
3954f0fba2adSLiam Girdwood 		kfree(dai->name);
3955f0fba2adSLiam Girdwood 		kfree(dai);
39569115171aSMark Brown 	}
395732c9ba54SLars-Peter Clausen }
39589115171aSMark Brown 
39599115171aSMark Brown /**
396032c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
39619115171aSMark Brown  *
39626106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
39636106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3964ac11a2b3SMark Brown  * @count: Number of DAIs
396532c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
396632c9ba54SLars-Peter Clausen  *                     parent's name.
39679115171aSMark Brown  */
396832c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
3969bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
3970bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
39719115171aSMark Brown {
39726106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3973f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
397432c9ba54SLars-Peter Clausen 	unsigned int i;
397532c9ba54SLars-Peter Clausen 	int ret;
3976f0fba2adSLiam Girdwood 
3977f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
39789115171aSMark Brown 
3979bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
3980bb13109dSLars-Peter Clausen 	component->num_dai = count;
3981bb13109dSLars-Peter Clausen 
39829115171aSMark Brown 	for (i = 0; i < count; i++) {
3983f0fba2adSLiam Girdwood 
3984f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3985c46e0079SAxel Lin 		if (dai == NULL) {
3986c46e0079SAxel Lin 			ret = -ENOMEM;
3987c46e0079SAxel Lin 			goto err;
3988c46e0079SAxel Lin 		}
3989f0fba2adSLiam Girdwood 
399032c9ba54SLars-Peter Clausen 		/*
399132c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
399232c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
399332c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
399432c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
399532c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
399632c9ba54SLars-Peter Clausen 		 * component-less anymore.
399732c9ba54SLars-Peter Clausen 		 */
399832c9ba54SLars-Peter Clausen 		if (count == 1 && legacy_dai_naming) {
3999db2a4165SFrank Mandarino 			dai->name = fmt_single_name(dev, &dai->id);
400032c9ba54SLars-Peter Clausen 		} else {
4001f0fba2adSLiam Girdwood 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
400232c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
400332c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
400432c9ba54SLars-Peter Clausen 			else
400532c9ba54SLars-Peter Clausen 				dai->id = i;
400632c9ba54SLars-Peter Clausen 		}
4007f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
4008f0fba2adSLiam Girdwood 			kfree(dai);
400932c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
40109115171aSMark Brown 			goto err;
40119115171aSMark Brown 		}
40129115171aSMark Brown 
40136106d129SLars-Peter Clausen 		dai->component = component;
4014f0fba2adSLiam Girdwood 		dai->dev = dev;
4015f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
4016f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
4017f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
4018f0fba2adSLiam Girdwood 
40191438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
4020054880feSMark Brown 
40219115171aSMark Brown 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
4022f0fba2adSLiam Girdwood 	}
4023f0fba2adSLiam Girdwood 
40249115171aSMark Brown 	return 0;
40259115171aSMark Brown 
40269115171aSMark Brown err:
402732c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
40289115171aSMark Brown 
40299115171aSMark Brown 	return ret;
40309115171aSMark Brown }
40319115171aSMark Brown 
403214e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
403314e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
4034d191bd8dSKuninori Morimoto {
403514e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
4036d191bd8dSKuninori Morimoto 
403714e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
403814e8bdebSLars-Peter Clausen }
4039d191bd8dSKuninori Morimoto 
404014e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
404114e8bdebSLars-Peter Clausen 	int event)
404214e8bdebSLars-Peter Clausen {
404314e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
404414e8bdebSLars-Peter Clausen 
404514e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
404614e8bdebSLars-Peter Clausen }
404714e8bdebSLars-Peter Clausen 
4048bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
4049bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
4050d191bd8dSKuninori Morimoto {
4051ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
4052ce0fc93aSLars-Peter Clausen 
4053bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
4054bb13109dSLars-Peter Clausen 	if (!component->name) {
4055bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
4056d191bd8dSKuninori Morimoto 		return -ENOMEM;
4057d191bd8dSKuninori Morimoto 	}
4058d191bd8dSKuninori Morimoto 
4059bb13109dSLars-Peter Clausen 	component->dev = dev;
4060bb13109dSLars-Peter Clausen 	component->driver = driver;
406161aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
406261aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
4063e2c330b9SLars-Peter Clausen 
4064ce0fc93aSLars-Peter Clausen 	if (!component->dapm_ptr)
4065ce0fc93aSLars-Peter Clausen 		component->dapm_ptr = &component->dapm;
4066ce0fc93aSLars-Peter Clausen 
4067ce0fc93aSLars-Peter Clausen 	dapm = component->dapm_ptr;
4068ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
4069ce0fc93aSLars-Peter Clausen 	dapm->component = component;
4070ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
407114e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
407214e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
407314e8bdebSLars-Peter Clausen 	if (driver->stream_event)
407414e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
4075ce0fc93aSLars-Peter Clausen 
407661aca564SLars-Peter Clausen 	component->controls = driver->controls;
407761aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
407861aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
407961aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
408061aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
408161aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
408261aca564SLars-Peter Clausen 
4083bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
4084bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
4085bb13109dSLars-Peter Clausen 
4086bb13109dSLars-Peter Clausen 	return 0;
4087d191bd8dSKuninori Morimoto }
4088d191bd8dSKuninori Morimoto 
4089bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4090bb13109dSLars-Peter Clausen {
4091bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
4092d191bd8dSKuninori Morimoto }
4093d191bd8dSKuninori Morimoto 
4094bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
4095bb13109dSLars-Peter Clausen {
4096d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
4097bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
4098d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
4099bb13109dSLars-Peter Clausen }
4100d191bd8dSKuninori Morimoto 
4101bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
4102bb13109dSLars-Peter Clausen {
4103bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
4104bb13109dSLars-Peter Clausen 	kfree(component->name);
4105bb13109dSLars-Peter Clausen }
4106d191bd8dSKuninori Morimoto 
4107bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4108bb13109dSLars-Peter Clausen {
4109bb13109dSLars-Peter Clausen 	list_del(&component->list);
4110bb13109dSLars-Peter Clausen }
4111d191bd8dSKuninori Morimoto 
4112bb13109dSLars-Peter Clausen static void snd_soc_component_del(struct snd_soc_component *component)
4113bb13109dSLars-Peter Clausen {
4114bb13109dSLars-Peter Clausen 	mutex_lock(&client_mutex);
4115bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(component);
4116bb13109dSLars-Peter Clausen 	mutex_unlock(&client_mutex);
4117d191bd8dSKuninori Morimoto }
4118d191bd8dSKuninori Morimoto 
4119d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
4120d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
4121d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
4122d191bd8dSKuninori Morimoto 			       int num_dai)
4123d191bd8dSKuninori Morimoto {
4124d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4125bb13109dSLars-Peter Clausen 	int ret;
4126d191bd8dSKuninori Morimoto 
4127bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4128d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4129d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
4130d191bd8dSKuninori Morimoto 		return -ENOMEM;
4131d191bd8dSKuninori Morimoto 	}
4132d191bd8dSKuninori Morimoto 
4133bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4134bb13109dSLars-Peter Clausen 	if (ret)
4135bb13109dSLars-Peter Clausen 		goto err_free;
4136bb13109dSLars-Peter Clausen 
41373d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
413898e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
41393d59400fSLars-Peter Clausen 
4140bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4141bb13109dSLars-Peter Clausen 	if (ret < 0) {
4142bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4143bb13109dSLars-Peter Clausen 		goto err_cleanup;
4144bb13109dSLars-Peter Clausen 	}
4145bb13109dSLars-Peter Clausen 
4146bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
4147bb13109dSLars-Peter Clausen 
4148bb13109dSLars-Peter Clausen 	return 0;
4149bb13109dSLars-Peter Clausen 
4150bb13109dSLars-Peter Clausen err_cleanup:
4151bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
4152bb13109dSLars-Peter Clausen err_free:
4153bb13109dSLars-Peter Clausen 	kfree(cmpnt);
4154bb13109dSLars-Peter Clausen 	return ret;
4155d191bd8dSKuninori Morimoto }
4156d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
4157d191bd8dSKuninori Morimoto 
4158d191bd8dSKuninori Morimoto /**
4159d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
4160d191bd8dSKuninori Morimoto  *
4161d191bd8dSKuninori Morimoto  */
4162d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
4163d191bd8dSKuninori Morimoto {
4164d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4165d191bd8dSKuninori Morimoto 
4166d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
416798e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
4168d191bd8dSKuninori Morimoto 			goto found;
4169d191bd8dSKuninori Morimoto 	}
4170d191bd8dSKuninori Morimoto 	return;
4171d191bd8dSKuninori Morimoto 
4172d191bd8dSKuninori Morimoto found:
4173bb13109dSLars-Peter Clausen 	snd_soc_component_del(cmpnt);
4174bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
4175bb13109dSLars-Peter Clausen 	kfree(cmpnt);
4176d191bd8dSKuninori Morimoto }
4177d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4178d191bd8dSKuninori Morimoto 
4179f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4180f1d45cc3SLars-Peter Clausen {
4181f1d45cc3SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4182f1d45cc3SLars-Peter Clausen 
4183f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
4184f1d45cc3SLars-Peter Clausen }
4185f1d45cc3SLars-Peter Clausen 
4186f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4187f1d45cc3SLars-Peter Clausen {
4188f1d45cc3SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4189f1d45cc3SLars-Peter Clausen 
4190f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
4191f1d45cc3SLars-Peter Clausen }
4192f1d45cc3SLars-Peter Clausen 
4193e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4194e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
4195e2c330b9SLars-Peter Clausen {
4196e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4197e2c330b9SLars-Peter Clausen 
4198e2c330b9SLars-Peter Clausen 	return platform->driver->write(platform, reg, val);
4199e2c330b9SLars-Peter Clausen }
4200e2c330b9SLars-Peter Clausen 
4201e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4202e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
4203e2c330b9SLars-Peter Clausen {
4204e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4205e2c330b9SLars-Peter Clausen 
4206e2c330b9SLars-Peter Clausen 	*val = platform->driver->read(platform, reg);
4207e2c330b9SLars-Peter Clausen 
4208e2c330b9SLars-Peter Clausen 	return 0;
4209e2c330b9SLars-Peter Clausen }
4210e2c330b9SLars-Peter Clausen 
4211d191bd8dSKuninori Morimoto /**
421271a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
421371a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
421471a45cdaSLars-Peter Clausen  * @platform: The platform to add
421571a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
421612a48a8cSMark Brown  */
421771a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4218d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
421912a48a8cSMark Brown {
4220b37f1d12SLars-Peter Clausen 	int ret;
4221b37f1d12SLars-Peter Clausen 
4222bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
4223bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
4224bb13109dSLars-Peter Clausen 	if (ret)
4225bb13109dSLars-Peter Clausen 		return ret;
4226f0fba2adSLiam Girdwood 
4227f0fba2adSLiam Girdwood 	platform->dev = dev;
4228f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
4229f1d45cc3SLars-Peter Clausen 	if (platform_drv->controls) {
4230f1d45cc3SLars-Peter Clausen 		platform->component.controls = platform_drv->controls;
4231f1d45cc3SLars-Peter Clausen 		platform->component.num_controls = platform_drv->num_controls;
4232f1d45cc3SLars-Peter Clausen 	}
4233f1d45cc3SLars-Peter Clausen 	if (platform_drv->dapm_widgets) {
4234f1d45cc3SLars-Peter Clausen 		platform->component.dapm_widgets = platform_drv->dapm_widgets;
4235f1d45cc3SLars-Peter Clausen 		platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4236f1d45cc3SLars-Peter Clausen 		platform->component.steal_sibling_dai_widgets = true;
4237f1d45cc3SLars-Peter Clausen 	}
4238f1d45cc3SLars-Peter Clausen 	if (platform_drv->dapm_routes) {
4239f1d45cc3SLars-Peter Clausen 		platform->component.dapm_routes = platform_drv->dapm_routes;
4240f1d45cc3SLars-Peter Clausen 		platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4241f1d45cc3SLars-Peter Clausen 	}
4242f1d45cc3SLars-Peter Clausen 
4243f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
4244f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
4245f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
4246f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
4247e2c330b9SLars-Peter Clausen 	if (platform_drv->write)
4248e2c330b9SLars-Peter Clausen 		platform->component.write = snd_soc_platform_drv_write;
4249e2c330b9SLars-Peter Clausen 	if (platform_drv->read)
4250e2c330b9SLars-Peter Clausen 		platform->component.read = snd_soc_platform_drv_read;
425112a48a8cSMark Brown 
425281c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
425381c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
425481c7cfd1SLars-Peter Clausen #endif
425581c7cfd1SLars-Peter Clausen 
425612a48a8cSMark Brown 	mutex_lock(&client_mutex);
4257bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
425812a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
425912a48a8cSMark Brown 	mutex_unlock(&client_mutex);
426012a48a8cSMark Brown 
4261f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4262f4333203SLars-Peter Clausen 		platform->component.name);
426312a48a8cSMark Brown 
426412a48a8cSMark Brown 	return 0;
426512a48a8cSMark Brown }
426671a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
426771a45cdaSLars-Peter Clausen 
426871a45cdaSLars-Peter Clausen /**
426971a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
427071a45cdaSLars-Peter Clausen  *
427171a45cdaSLars-Peter Clausen  * @platform: platform to register
427271a45cdaSLars-Peter Clausen  */
427371a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
427471a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
427571a45cdaSLars-Peter Clausen {
427671a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
427771a45cdaSLars-Peter Clausen 	int ret;
427871a45cdaSLars-Peter Clausen 
427971a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
428071a45cdaSLars-Peter Clausen 
428171a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
428271a45cdaSLars-Peter Clausen 	if (platform == NULL)
428371a45cdaSLars-Peter Clausen 		return -ENOMEM;
428471a45cdaSLars-Peter Clausen 
428571a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
428671a45cdaSLars-Peter Clausen 	if (ret)
428771a45cdaSLars-Peter Clausen 		kfree(platform);
428871a45cdaSLars-Peter Clausen 
428971a45cdaSLars-Peter Clausen 	return ret;
429071a45cdaSLars-Peter Clausen }
429112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
429212a48a8cSMark Brown 
429312a48a8cSMark Brown /**
429471a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
429571a45cdaSLars-Peter Clausen  * @platform: the platform to remove
429671a45cdaSLars-Peter Clausen  */
429771a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
429871a45cdaSLars-Peter Clausen {
4299b37f1d12SLars-Peter Clausen 
430071a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
430171a45cdaSLars-Peter Clausen 	list_del(&platform->list);
4302bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
430371a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
430471a45cdaSLars-Peter Clausen 
4305bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&platform->component);
4306bb13109dSLars-Peter Clausen 
430771a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4308f4333203SLars-Peter Clausen 		platform->component.name);
430971a45cdaSLars-Peter Clausen }
431071a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
431171a45cdaSLars-Peter Clausen 
431271a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
431371a45cdaSLars-Peter Clausen {
431471a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
431571a45cdaSLars-Peter Clausen 
431671a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
431771a45cdaSLars-Peter Clausen 		if (dev == platform->dev)
431871a45cdaSLars-Peter Clausen 			return platform;
431971a45cdaSLars-Peter Clausen 	}
432071a45cdaSLars-Peter Clausen 
432171a45cdaSLars-Peter Clausen 	return NULL;
432271a45cdaSLars-Peter Clausen }
432371a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
432471a45cdaSLars-Peter Clausen 
432571a45cdaSLars-Peter Clausen /**
432612a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
432712a48a8cSMark Brown  *
4328ac11a2b3SMark Brown  * @platform: platform to unregister
432912a48a8cSMark Brown  */
4330f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
433112a48a8cSMark Brown {
4332f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
4333f0fba2adSLiam Girdwood 
433471a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
433571a45cdaSLars-Peter Clausen 	if (!platform)
4336f0fba2adSLiam Girdwood 		return;
4337f0fba2adSLiam Girdwood 
433871a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
4339f0fba2adSLiam Girdwood 	kfree(platform);
434012a48a8cSMark Brown }
434112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
434212a48a8cSMark Brown 
4343151ab22cSMark Brown static u64 codec_format_map[] = {
4344151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4345151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4346151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4347151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4348151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4349151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4350151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4351151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4352151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4353151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4354151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4355151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4356151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4357151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4358151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4359151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4360151ab22cSMark Brown };
4361151ab22cSMark Brown 
4362151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
4363151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
4364151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
4365151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
4366151ab22cSMark Brown  */
4367151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4368151ab22cSMark Brown {
4369151ab22cSMark Brown 	int i;
4370151ab22cSMark Brown 
4371151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4372151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
4373151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
4374151ab22cSMark Brown }
4375151ab22cSMark Brown 
4376f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4377f1d45cc3SLars-Peter Clausen {
4378f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4379f1d45cc3SLars-Peter Clausen 
4380f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
4381f1d45cc3SLars-Peter Clausen }
4382f1d45cc3SLars-Peter Clausen 
4383f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4384f1d45cc3SLars-Peter Clausen {
4385f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4386f1d45cc3SLars-Peter Clausen 
4387f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
4388f1d45cc3SLars-Peter Clausen }
4389f1d45cc3SLars-Peter Clausen 
4390e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4391e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
4392e2c330b9SLars-Peter Clausen {
4393e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4394e2c330b9SLars-Peter Clausen 
4395e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
4396e2c330b9SLars-Peter Clausen }
4397e2c330b9SLars-Peter Clausen 
4398e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4399e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
4400e2c330b9SLars-Peter Clausen {
4401e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4402e2c330b9SLars-Peter Clausen 
4403e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
4404e2c330b9SLars-Peter Clausen 
4405e2c330b9SLars-Peter Clausen 	return 0;
4406e2c330b9SLars-Peter Clausen }
4407e2c330b9SLars-Peter Clausen 
440868f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
440968f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
441068f831c2SLars-Peter Clausen {
441168f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
441268f831c2SLars-Peter Clausen 
441368f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
441468f831c2SLars-Peter Clausen }
441568f831c2SLars-Peter Clausen 
44160d0cf00aSMark Brown /**
44170d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
44180d0cf00aSMark Brown  *
4419ac11a2b3SMark Brown  * @codec: codec to register
44200d0cf00aSMark Brown  */
4421f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
4422001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
4423001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
4424001ae4c0SMark Brown 			   int num_dai)
44250d0cf00aSMark Brown {
4426f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4427bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
4428a39f75f7SXiubo Li 	struct regmap *regmap;
4429f0fba2adSLiam Girdwood 	int ret, i;
4430151ab22cSMark Brown 
4431f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
44320d0cf00aSMark Brown 
4433f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4434f0fba2adSLiam Girdwood 	if (codec == NULL)
4435f0fba2adSLiam Girdwood 		return -ENOMEM;
44360d0cf00aSMark Brown 
4437ce0fc93aSLars-Peter Clausen 	codec->component.dapm_ptr = &codec->dapm;
4438f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
4439ce0fc93aSLars-Peter Clausen 
4440bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
4441bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
4442bb13109dSLars-Peter Clausen 	if (ret)
4443bb13109dSLars-Peter Clausen 		goto err_free;
4444151ab22cSMark Brown 
4445f1d45cc3SLars-Peter Clausen 	if (codec_drv->controls) {
4446f1d45cc3SLars-Peter Clausen 		codec->component.controls = codec_drv->controls;
4447f1d45cc3SLars-Peter Clausen 		codec->component.num_controls = codec_drv->num_controls;
4448f1d45cc3SLars-Peter Clausen 	}
4449f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_widgets) {
4450f1d45cc3SLars-Peter Clausen 		codec->component.dapm_widgets = codec_drv->dapm_widgets;
4451f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4452f1d45cc3SLars-Peter Clausen 	}
4453f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_routes) {
4454f1d45cc3SLars-Peter Clausen 		codec->component.dapm_routes = codec_drv->dapm_routes;
4455f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4456f1d45cc3SLars-Peter Clausen 	}
4457f1d45cc3SLars-Peter Clausen 
4458f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
4459f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
4460f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
4461f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
4462e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
4463e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
4464e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
4465e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
44663d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4467ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
446814e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
4469474b62d6SMark Brown 		codec->dapm.seq_notifier = codec_drv->seq_notifier;
447068f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
447168f831c2SLars-Peter Clausen 		codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4472f0fba2adSLiam Girdwood 	codec->dev = dev;
4473f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
4474e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
4475f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
4476f0fba2adSLiam Girdwood 
447781c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
447881c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
447981c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
448081c7cfd1SLars-Peter Clausen #endif
448181c7cfd1SLars-Peter Clausen 
4482e2c330b9SLars-Peter Clausen 	if (!codec->component.write) {
4483a39f75f7SXiubo Li 		if (codec_drv->get_regmap)
4484a39f75f7SXiubo Li 			regmap = codec_drv->get_regmap(dev);
4485a39f75f7SXiubo Li 		else
4486a39f75f7SXiubo Li 			regmap = dev_get_regmap(dev, NULL);
4487a39f75f7SXiubo Li 
4488a39f75f7SXiubo Li 		if (regmap) {
4489e2c330b9SLars-Peter Clausen 			ret = snd_soc_component_init_io(&codec->component,
4490e2c330b9SLars-Peter Clausen 				regmap);
4491e2c330b9SLars-Peter Clausen 			if (ret) {
4492a39f75f7SXiubo Li 				dev_err(codec->dev,
4493a39f75f7SXiubo Li 						"Failed to set cache I/O:%d\n",
4494a39f75f7SXiubo Li 						ret);
4495bb13109dSLars-Peter Clausen 				goto err_cleanup;
4496a39f75f7SXiubo Li 			}
4497a39f75f7SXiubo Li 		}
4498a39f75f7SXiubo Li 	}
4499a39f75f7SXiubo Li 
4500f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
4501f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
4502f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
4503f0fba2adSLiam Girdwood 	}
4504f0fba2adSLiam Girdwood 
4505bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4506bb13109dSLars-Peter Clausen 	if (ret < 0) {
4507bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4508bb13109dSLars-Peter Clausen 		goto err_cleanup;
4509bb13109dSLars-Peter Clausen 	}
4510bb13109dSLars-Peter Clausen 
4511bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
4512bb13109dSLars-Peter Clausen 		dai->codec = codec;
4513bb13109dSLars-Peter Clausen 
4514054880feSMark Brown 	mutex_lock(&client_mutex);
4515bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
4516054880feSMark Brown 	list_add(&codec->list, &codec_list);
4517054880feSMark Brown 	mutex_unlock(&client_mutex);
4518054880feSMark Brown 
4519f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4520f4333203SLars-Peter Clausen 		codec->component.name);
45210d0cf00aSMark Brown 	return 0;
4522f0fba2adSLiam Girdwood 
4523bb13109dSLars-Peter Clausen err_cleanup:
4524bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
4525bb13109dSLars-Peter Clausen err_free:
4526f0fba2adSLiam Girdwood 	kfree(codec);
4527f0fba2adSLiam Girdwood 	return ret;
45280d0cf00aSMark Brown }
45290d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
45300d0cf00aSMark Brown 
45310d0cf00aSMark Brown /**
45320d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
45330d0cf00aSMark Brown  *
4534ac11a2b3SMark Brown  * @codec: codec to unregister
45350d0cf00aSMark Brown  */
4536f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
45370d0cf00aSMark Brown {
4538f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4539f0fba2adSLiam Girdwood 
4540f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
4541f0fba2adSLiam Girdwood 		if (dev == codec->dev)
4542f0fba2adSLiam Girdwood 			goto found;
4543f0fba2adSLiam Girdwood 	}
4544f0fba2adSLiam Girdwood 	return;
4545f0fba2adSLiam Girdwood 
4546f0fba2adSLiam Girdwood found:
4547f0fba2adSLiam Girdwood 
45480d0cf00aSMark Brown 	mutex_lock(&client_mutex);
45490d0cf00aSMark Brown 	list_del(&codec->list);
4550bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
45510d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
45520d0cf00aSMark Brown 
4553f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4554f4333203SLars-Peter Clausen 			codec->component.name);
4555f0fba2adSLiam Girdwood 
4556bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
45577a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
4558f0fba2adSLiam Girdwood 	kfree(codec);
45590d0cf00aSMark Brown }
45600d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
45610d0cf00aSMark Brown 
4562bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4563bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4564bec4fa05SStephen Warren 			       const char *propname)
4565bec4fa05SStephen Warren {
45667e07e7c0STushar Behera 	struct device_node *np;
4567bec4fa05SStephen Warren 	int ret;
4568bec4fa05SStephen Warren 
45697e07e7c0STushar Behera 	if (!card->dev) {
45707e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
45717e07e7c0STushar Behera 		return -EINVAL;
45727e07e7c0STushar Behera 	}
45737e07e7c0STushar Behera 
45747e07e7c0STushar Behera 	np = card->dev->of_node;
45757e07e7c0STushar Behera 
4576bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4577bec4fa05SStephen Warren 	/*
4578bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4579bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4580bec4fa05SStephen Warren 	 * snd_soc_register_card.
4581bec4fa05SStephen Warren 	 */
4582bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4583bec4fa05SStephen Warren 		dev_err(card->dev,
4584f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
4585bec4fa05SStephen Warren 			propname, ret);
4586bec4fa05SStephen Warren 		return ret;
4587bec4fa05SStephen Warren 	}
4588bec4fa05SStephen Warren 
4589bec4fa05SStephen Warren 	return 0;
4590bec4fa05SStephen Warren }
4591bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4592bec4fa05SStephen Warren 
45939a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
45949a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
45959a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
45969a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
45979a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
45989a6d4860SXiubo Li };
45999a6d4860SXiubo Li 
46009a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
46019a6d4860SXiubo Li 					  const char *propname)
46029a6d4860SXiubo Li {
46039a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
46049a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
46059a6d4860SXiubo Li 	const char *template, *wname;
46069a6d4860SXiubo Li 	int i, j, num_widgets, ret;
46079a6d4860SXiubo Li 
46089a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
46099a6d4860SXiubo Li 	if (num_widgets < 0) {
46109a6d4860SXiubo Li 		dev_err(card->dev,
46119a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
46129a6d4860SXiubo Li 		return -EINVAL;
46139a6d4860SXiubo Li 	}
46149a6d4860SXiubo Li 	if (num_widgets & 1) {
46159a6d4860SXiubo Li 		dev_err(card->dev,
46169a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
46179a6d4860SXiubo Li 		return -EINVAL;
46189a6d4860SXiubo Li 	}
46199a6d4860SXiubo Li 
46209a6d4860SXiubo Li 	num_widgets /= 2;
46219a6d4860SXiubo Li 	if (!num_widgets) {
46229a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
46239a6d4860SXiubo Li 			propname);
46249a6d4860SXiubo Li 		return -EINVAL;
46259a6d4860SXiubo Li 	}
46269a6d4860SXiubo Li 
46279a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
46289a6d4860SXiubo Li 			       GFP_KERNEL);
46299a6d4860SXiubo Li 	if (!widgets) {
46309a6d4860SXiubo Li 		dev_err(card->dev,
46319a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
46329a6d4860SXiubo Li 		return -ENOMEM;
46339a6d4860SXiubo Li 	}
46349a6d4860SXiubo Li 
46359a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
46369a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
46379a6d4860SXiubo Li 			2 * i, &template);
46389a6d4860SXiubo Li 		if (ret) {
46399a6d4860SXiubo Li 			dev_err(card->dev,
46409a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
46419a6d4860SXiubo Li 				propname, 2 * i, ret);
46429a6d4860SXiubo Li 			return -EINVAL;
46439a6d4860SXiubo Li 		}
46449a6d4860SXiubo Li 
46459a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
46469a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
46479a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
46489a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
46499a6d4860SXiubo Li 				break;
46509a6d4860SXiubo Li 			}
46519a6d4860SXiubo Li 		}
46529a6d4860SXiubo Li 
46539a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
46549a6d4860SXiubo Li 			dev_err(card->dev,
46559a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
46569a6d4860SXiubo Li 				template);
46579a6d4860SXiubo Li 			return -EINVAL;
46589a6d4860SXiubo Li 		}
46599a6d4860SXiubo Li 
46609a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
46619a6d4860SXiubo Li 						    (2 * i) + 1,
46629a6d4860SXiubo Li 						    &wname);
46639a6d4860SXiubo Li 		if (ret) {
46649a6d4860SXiubo Li 			dev_err(card->dev,
46659a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
46669a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
46679a6d4860SXiubo Li 			return -EINVAL;
46689a6d4860SXiubo Li 		}
46699a6d4860SXiubo Li 
46709a6d4860SXiubo Li 		widgets[i].name = wname;
46719a6d4860SXiubo Li 	}
46729a6d4860SXiubo Li 
46739a6d4860SXiubo Li 	card->dapm_widgets = widgets;
46749a6d4860SXiubo Li 	card->num_dapm_widgets = num_widgets;
46759a6d4860SXiubo Li 
46769a6d4860SXiubo Li 	return 0;
46779a6d4860SXiubo Li }
46789a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
46799a6d4860SXiubo Li 
468089c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
468189c67857SXiubo Li 			      unsigned int *slots,
468289c67857SXiubo Li 			      unsigned int *slot_width)
468389c67857SXiubo Li {
468489c67857SXiubo Li 	u32 val;
468589c67857SXiubo Li 	int ret;
468689c67857SXiubo Li 
468789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
468889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
468989c67857SXiubo Li 		if (ret)
469089c67857SXiubo Li 			return ret;
469189c67857SXiubo Li 
469289c67857SXiubo Li 		if (slots)
469389c67857SXiubo Li 			*slots = val;
469489c67857SXiubo Li 	}
469589c67857SXiubo Li 
469689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
469789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
469889c67857SXiubo Li 		if (ret)
469989c67857SXiubo Li 			return ret;
470089c67857SXiubo Li 
470189c67857SXiubo Li 		if (slot_width)
470289c67857SXiubo Li 			*slot_width = val;
470389c67857SXiubo Li 	}
470489c67857SXiubo Li 
470589c67857SXiubo Li 	return 0;
470689c67857SXiubo Li }
470789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
470889c67857SXiubo Li 
4709a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4710a4a54dd5SStephen Warren 				   const char *propname)
4711a4a54dd5SStephen Warren {
4712a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4713a4a54dd5SStephen Warren 	int num_routes;
4714a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4715a4a54dd5SStephen Warren 	int i, ret;
4716a4a54dd5SStephen Warren 
4717a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4718c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
471910e8aa9aSMichał Mirosław 		dev_err(card->dev,
472010e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
472110e8aa9aSMichał Mirosław 			propname);
4722a4a54dd5SStephen Warren 		return -EINVAL;
4723a4a54dd5SStephen Warren 	}
4724a4a54dd5SStephen Warren 	num_routes /= 2;
4725a4a54dd5SStephen Warren 	if (!num_routes) {
4726f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4727a4a54dd5SStephen Warren 			propname);
4728a4a54dd5SStephen Warren 		return -EINVAL;
4729a4a54dd5SStephen Warren 	}
4730a4a54dd5SStephen Warren 
4731a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4732a4a54dd5SStephen Warren 			      GFP_KERNEL);
4733a4a54dd5SStephen Warren 	if (!routes) {
4734a4a54dd5SStephen Warren 		dev_err(card->dev,
4735f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4736a4a54dd5SStephen Warren 		return -EINVAL;
4737a4a54dd5SStephen Warren 	}
4738a4a54dd5SStephen Warren 
4739a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4740a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4741a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4742a4a54dd5SStephen Warren 		if (ret) {
4743c871bd0bSMark Brown 			dev_err(card->dev,
4744c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4745c871bd0bSMark Brown 				propname, 2 * i, ret);
4746a4a54dd5SStephen Warren 			return -EINVAL;
4747a4a54dd5SStephen Warren 		}
4748a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4749a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4750a4a54dd5SStephen Warren 		if (ret) {
4751a4a54dd5SStephen Warren 			dev_err(card->dev,
4752c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4753c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4754a4a54dd5SStephen Warren 			return -EINVAL;
4755a4a54dd5SStephen Warren 		}
4756a4a54dd5SStephen Warren 	}
4757a4a54dd5SStephen Warren 
4758a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4759a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4760a4a54dd5SStephen Warren 
4761a4a54dd5SStephen Warren 	return 0;
4762a4a54dd5SStephen Warren }
4763a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4764a4a54dd5SStephen Warren 
4765a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4766389cb834SJyri Sarha 				     const char *prefix,
4767389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
4768389cb834SJyri Sarha 				     struct device_node **framemaster)
4769a7930ed4SKuninori Morimoto {
4770a7930ed4SKuninori Morimoto 	int ret, i;
4771a7930ed4SKuninori Morimoto 	char prop[128];
4772a7930ed4SKuninori Morimoto 	unsigned int format = 0;
4773a7930ed4SKuninori Morimoto 	int bit, frame;
4774a7930ed4SKuninori Morimoto 	const char *str;
4775a7930ed4SKuninori Morimoto 	struct {
4776a7930ed4SKuninori Morimoto 		char *name;
4777a7930ed4SKuninori Morimoto 		unsigned int val;
4778a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
4779a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
4780a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
4781a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
4782a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
4783a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
4784a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
4785a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
4786a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
4787a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
4788a7930ed4SKuninori Morimoto 	};
4789a7930ed4SKuninori Morimoto 
4790a7930ed4SKuninori Morimoto 	if (!prefix)
4791a7930ed4SKuninori Morimoto 		prefix = "";
4792a7930ed4SKuninori Morimoto 
4793a7930ed4SKuninori Morimoto 	/*
4794a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
4795a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4796a7930ed4SKuninori Morimoto 	 */
4797a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
4798a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
4799a7930ed4SKuninori Morimoto 	if (ret == 0) {
4800a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4801a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4802a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4803a7930ed4SKuninori Morimoto 				break;
4804a7930ed4SKuninori Morimoto 			}
4805a7930ed4SKuninori Morimoto 		}
4806a7930ed4SKuninori Morimoto 	}
4807a7930ed4SKuninori Morimoto 
4808a7930ed4SKuninori Morimoto 	/*
48098c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4810a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4811a7930ed4SKuninori Morimoto 	 */
48128c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
48138c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
48148c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
48158c2d6a9fSKuninori Morimoto 	else
48168c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4817a7930ed4SKuninori Morimoto 
4818a7930ed4SKuninori Morimoto 	/*
4819a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4820a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4821a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4822a7930ed4SKuninori Morimoto 	 */
4823a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4824a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4825a7930ed4SKuninori Morimoto 
4826a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4827a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4828a7930ed4SKuninori Morimoto 
4829a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4830a7930ed4SKuninori Morimoto 	case 0x11:
4831a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4832a7930ed4SKuninori Morimoto 		break;
4833a7930ed4SKuninori Morimoto 	case 0x10:
4834a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4835a7930ed4SKuninori Morimoto 		break;
4836a7930ed4SKuninori Morimoto 	case 0x01:
4837a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4838a7930ed4SKuninori Morimoto 		break;
4839a7930ed4SKuninori Morimoto 	default:
4840a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4841a7930ed4SKuninori Morimoto 		break;
4842a7930ed4SKuninori Morimoto 	}
4843a7930ed4SKuninori Morimoto 
4844a7930ed4SKuninori Morimoto 	/*
4845a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4846a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4847a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4848a7930ed4SKuninori Morimoto 	 */
4849a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4850a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4851389cb834SJyri Sarha 	if (bit && bitclkmaster)
4852389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
4853a7930ed4SKuninori Morimoto 
4854a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4855a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4856389cb834SJyri Sarha 	if (frame && framemaster)
4857389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
4858a7930ed4SKuninori Morimoto 
4859a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4860a7930ed4SKuninori Morimoto 	case 0x11:
4861a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4862a7930ed4SKuninori Morimoto 		break;
4863a7930ed4SKuninori Morimoto 	case 0x10:
4864a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4865a7930ed4SKuninori Morimoto 		break;
4866a7930ed4SKuninori Morimoto 	case 0x01:
4867a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4868a7930ed4SKuninori Morimoto 		break;
4869a7930ed4SKuninori Morimoto 	default:
4870a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4871a7930ed4SKuninori Morimoto 		break;
4872a7930ed4SKuninori Morimoto 	}
4873a7930ed4SKuninori Morimoto 
4874a7930ed4SKuninori Morimoto 	return format;
4875a7930ed4SKuninori Morimoto }
4876a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4877a7930ed4SKuninori Morimoto 
4878cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node,
4879cb470087SKuninori Morimoto 			    const char **dai_name)
4880cb470087SKuninori Morimoto {
4881cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
4882cb470087SKuninori Morimoto 	struct of_phandle_args args;
4883cb470087SKuninori Morimoto 	int ret;
4884cb470087SKuninori Morimoto 
4885cb470087SKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
4886cb470087SKuninori Morimoto 					 "#sound-dai-cells", 0, &args);
4887cb470087SKuninori Morimoto 	if (ret)
4888cb470087SKuninori Morimoto 		return ret;
4889cb470087SKuninori Morimoto 
4890cb470087SKuninori Morimoto 	ret = -EPROBE_DEFER;
4891cb470087SKuninori Morimoto 
4892cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4893cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4894cb470087SKuninori Morimoto 		if (pos->dev->of_node != args.np)
4895cb470087SKuninori Morimoto 			continue;
4896cb470087SKuninori Morimoto 
48976833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
48986833c452SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
48996833c452SKuninori Morimoto 		} else {
49006833c452SKuninori Morimoto 			int id = -1;
49016833c452SKuninori Morimoto 
49026833c452SKuninori Morimoto 			switch (args.args_count) {
49036833c452SKuninori Morimoto 			case 0:
49046833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
49056833c452SKuninori Morimoto 				break;
49066833c452SKuninori Morimoto 			case 1:
49076833c452SKuninori Morimoto 				id = args.args[0];
49086833c452SKuninori Morimoto 				break;
49096833c452SKuninori Morimoto 			default:
49106833c452SKuninori Morimoto 				/* not supported */
4911cb470087SKuninori Morimoto 				break;
4912cb470087SKuninori Morimoto 			}
4913cb470087SKuninori Morimoto 
49146833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
49156833c452SKuninori Morimoto 				ret = -EINVAL;
49163dcba280SNicolin Chen 				continue;
49176833c452SKuninori Morimoto 			}
4918e41975edSXiubo Li 
4919e41975edSXiubo Li 			ret = 0;
4920e41975edSXiubo Li 
4921e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4922e41975edSXiubo Li 			if (!*dai_name)
4923e41975edSXiubo Li 				*dai_name = pos->name;
49246833c452SKuninori Morimoto 		}
49256833c452SKuninori Morimoto 
4926cb470087SKuninori Morimoto 		break;
4927cb470087SKuninori Morimoto 	}
4928cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
4929cb470087SKuninori Morimoto 
4930cb470087SKuninori Morimoto 	of_node_put(args.np);
4931cb470087SKuninori Morimoto 
4932cb470087SKuninori Morimoto 	return ret;
4933cb470087SKuninori Morimoto }
4934cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4935cb470087SKuninori Morimoto 
4936c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4937db2a4165SFrank Mandarino {
4938db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
49398a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
49408a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
49410837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
49428a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4943db2a4165SFrank Mandarino 	}
4944c3c5a19aSMark Brown 
49458a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4946c3c5a19aSMark Brown 				 &codec_list_fops))
4947c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4948c3c5a19aSMark Brown 
49498a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4950f3208780SMark Brown 				 &dai_list_fops))
4951f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
495219c7ac27SMark Brown 
49538a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
495419c7ac27SMark Brown 				 &platform_list_fops))
495519c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4956db2a4165SFrank Mandarino #endif
4957db2a4165SFrank Mandarino 
4958fb257897SMark Brown 	snd_soc_util_init();
4959fb257897SMark Brown 
4960db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4961db2a4165SFrank Mandarino }
49624abe8e16SMark Brown module_init(snd_soc_init);
4963db2a4165SFrank Mandarino 
4964db2a4165SFrank Mandarino static void __exit snd_soc_exit(void)
4965db2a4165SFrank Mandarino {
4966fb257897SMark Brown 	snd_soc_util_exit();
4967fb257897SMark Brown 
4968db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
49698a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4970db2a4165SFrank Mandarino #endif
4971db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4972db2a4165SFrank Mandarino }
4973db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4974db2a4165SFrank Mandarino 
4975db2a4165SFrank Mandarino /* Module information */
4976db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4977db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4978db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
4979db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
4980