xref: /openbmc/linux/sound/soc/soc-core.c (revision 18d756440e8ed292b772682c4440a9d33643225b)
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>
33f0e8ed85SMark Brown #include <linux/ctype.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
35bec4fa05SStephen Warren #include <linux/of.h>
36474828a4SMarek Vasut #include <sound/ac97_codec.h>
37db2a4165SFrank Mandarino #include <sound/core.h>
383028eb8cSMark Brown #include <sound/jack.h>
39db2a4165SFrank Mandarino #include <sound/pcm.h>
40db2a4165SFrank Mandarino #include <sound/pcm_params.h>
41db2a4165SFrank Mandarino #include <sound/soc.h>
4201d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
43db2a4165SFrank Mandarino #include <sound/initval.h>
44db2a4165SFrank Mandarino 
45a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
46a8b1d34fSMark Brown #include <trace/events/asoc.h>
47a8b1d34fSMark Brown 
48f0fba2adSLiam Girdwood #define NAME_SIZE	32
49f0fba2adSLiam Girdwood 
50db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51db2a4165SFrank Mandarino 
52384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
538a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
548a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
55384c89e2SMark Brown #endif
56384c89e2SMark Brown 
57c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
589115171aSMark Brown static LIST_HEAD(dai_list);
5912a48a8cSMark Brown static LIST_HEAD(platform_list);
600d0cf00aSMark Brown static LIST_HEAD(codec_list);
61c5af3a2eSMark Brown 
62db2a4165SFrank Mandarino /*
63db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
64db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
65db2a4165SFrank Mandarino  * between two audio tracks.
66db2a4165SFrank Mandarino  */
67db2a4165SFrank Mandarino static int pmdown_time = 5000;
68db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
69db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70db2a4165SFrank Mandarino 
712bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
722bc9a81eSDimitris Papastamos  * a particular given value */
732bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
742bc9a81eSDimitris Papastamos {
752bc9a81eSDimitris Papastamos 	int c = 0;
762bc9a81eSDimitris Papastamos 	int i;
772bc9a81eSDimitris Papastamos 
782bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
792bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
802bc9a81eSDimitris Papastamos 			break;
812bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
822bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
832bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
842bc9a81eSDimitris Papastamos 	else
852bc9a81eSDimitris Papastamos 		c /= 8;
862bc9a81eSDimitris Papastamos 	return c;
872bc9a81eSDimitris Papastamos }
882bc9a81eSDimitris Papastamos 
8913fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
9013fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9113fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9213fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
932624d5faSMark Brown {
9400b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9500b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9613fd179fSDimitris Papastamos 	int ret;
9713fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
9813fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
9913fd179fSDimitris Papastamos 
10013fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
10113fd179fSDimitris Papastamos 	 * try to abuse this function */
10213fd179fSDimitris Papastamos 	WARN_ON(len > 63);
10313fd179fSDimitris Papastamos 
10413fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10513fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10613fd179fSDimitris Papastamos 		return -EINVAL;
10713fd179fSDimitris Papastamos 
10813fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
10913fd179fSDimitris Papastamos 	if (ret < 0) {
11013fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
11113fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
11213fd179fSDimitris Papastamos 	} else {
11313fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
11413fd179fSDimitris Papastamos 	}
11513fd179fSDimitris Papastamos 
11613fd179fSDimitris Papastamos 	/* prepare the buffer */
11713fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
11813fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
11913fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
12013fd179fSDimitris Papastamos 
12113fd179fSDimitris Papastamos 	return 0;
12213fd179fSDimitris Papastamos }
12313fd179fSDimitris Papastamos 
12413fd179fSDimitris Papastamos /* codec register dump */
12513fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
12613fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
12713fd179fSDimitris Papastamos {
12813fd179fSDimitris Papastamos 	int i, step = 1;
1292bc9a81eSDimitris Papastamos 	int wordsize, regsize;
13013fd179fSDimitris Papastamos 	int len;
13113fd179fSDimitris Papastamos 	size_t total = 0;
13213fd179fSDimitris Papastamos 	loff_t p = 0;
1332bc9a81eSDimitris Papastamos 
13400b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
13500b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1362624d5faSMark Brown 
13713fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13813fd179fSDimitris Papastamos 
139f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1402624d5faSMark Brown 		return 0;
1412624d5faSMark Brown 
142f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
143f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1442624d5faSMark Brown 
145f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
146b92d150bSLars-Peter Clausen 		if (!snd_soc_codec_readable_register(codec, i))
1472624d5faSMark Brown 			continue;
148f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
149f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1502624d5faSMark Brown 							 PAGE_SIZE - count, i);
1515164d74dSMark Brown 		} else {
15213fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
15313fd179fSDimitris Papastamos 			 * entries for the default case */
15413fd179fSDimitris Papastamos 			if (p >= pos) {
15513fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1562624d5faSMark Brown 					break;
15713fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
15813fd179fSDimitris Papastamos 				total += len;
15913fd179fSDimitris Papastamos 			}
16013fd179fSDimitris Papastamos 			p += len;
16113fd179fSDimitris Papastamos 		}
1622624d5faSMark Brown 	}
1632624d5faSMark Brown 
16413fd179fSDimitris Papastamos 	total = min(total, count - 1);
1652624d5faSMark Brown 
16613fd179fSDimitris Papastamos 	return total;
1672624d5faSMark Brown }
16813fd179fSDimitris Papastamos 
1692624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1702624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1712624d5faSMark Brown {
17236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
173f0fba2adSLiam Girdwood 
17413fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1752624d5faSMark Brown }
1762624d5faSMark Brown 
1772624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1782624d5faSMark Brown 
179dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
180dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
181dbe21408SMark Brown {
18236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183dbe21408SMark Brown 
184f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
185dbe21408SMark Brown }
186dbe21408SMark Brown 
187dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
188dbe21408SMark Brown 			       struct device_attribute *attr,
189dbe21408SMark Brown 			       const char *buf, size_t count)
190dbe21408SMark Brown {
19136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
192c593b520SMark Brown 	int ret;
193dbe21408SMark Brown 
194c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195c593b520SMark Brown 	if (ret)
196c593b520SMark Brown 		return ret;
197dbe21408SMark Brown 
198dbe21408SMark Brown 	return count;
199dbe21408SMark Brown }
200dbe21408SMark Brown 
201dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202dbe21408SMark Brown 
2032624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2042624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2052624d5faSMark Brown 				   size_t count, loff_t *ppos)
2062624d5faSMark Brown {
2072624d5faSMark Brown 	ssize_t ret;
2082624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
20913fd179fSDimitris Papastamos 	char *buf;
21013fd179fSDimitris Papastamos 
21113fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
21213fd179fSDimitris Papastamos 		return -EINVAL;
21313fd179fSDimitris Papastamos 
21413fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2152624d5faSMark Brown 	if (!buf)
2162624d5faSMark Brown 		return -ENOMEM;
21713fd179fSDimitris Papastamos 
21813fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
21913fd179fSDimitris Papastamos 	if (ret >= 0) {
22013fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
22113fd179fSDimitris Papastamos 			kfree(buf);
22213fd179fSDimitris Papastamos 			return -EFAULT;
22313fd179fSDimitris Papastamos 		}
22413fd179fSDimitris Papastamos 		*ppos += ret;
22513fd179fSDimitris Papastamos 	}
22613fd179fSDimitris Papastamos 
2272624d5faSMark Brown 	kfree(buf);
2282624d5faSMark Brown 	return ret;
2292624d5faSMark Brown }
2302624d5faSMark Brown 
2312624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2322624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2332624d5faSMark Brown {
2342624d5faSMark Brown 	char buf[32];
23534e268d8SStephen Boyd 	size_t buf_size;
2362624d5faSMark Brown 	char *start = buf;
2372624d5faSMark Brown 	unsigned long reg, value;
2382624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2392624d5faSMark Brown 
2402624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2412624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2422624d5faSMark Brown 		return -EFAULT;
2432624d5faSMark Brown 	buf[buf_size] = 0;
2442624d5faSMark Brown 
2452624d5faSMark Brown 	while (*start == ' ')
2462624d5faSMark Brown 		start++;
2472624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2482624d5faSMark Brown 	while (*start == ' ')
2492624d5faSMark Brown 		start++;
2502624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2512624d5faSMark Brown 		return -EINVAL;
2520d51a9cbSMark Brown 
2530d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2540d51a9cbSMark Brown 	add_taint(TAINT_USER);
2550d51a9cbSMark Brown 
256e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2572624d5faSMark Brown 	return buf_size;
2582624d5faSMark Brown }
2592624d5faSMark Brown 
2602624d5faSMark Brown static const struct file_operations codec_reg_fops = {
261234e3405SStephen Boyd 	.open = simple_open,
2622624d5faSMark Brown 	.read = codec_reg_read_file,
2632624d5faSMark Brown 	.write = codec_reg_write_file,
2646038f373SArnd Bergmann 	.llseek = default_llseek,
2652624d5faSMark Brown };
2662624d5faSMark Brown 
2672624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2682624d5faSMark Brown {
269d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270d6ce4cf3SJarkko Nikula 
2716ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2732624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
27464e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
2752624d5faSMark Brown 		return;
2762624d5faSMark Brown 	}
2772624d5faSMark Brown 
278aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
279aaee8ef1SMark Brown 			    &codec->cache_sync);
280aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
281aaee8ef1SMark Brown 			    &codec->cache_only);
282aaee8ef1SMark Brown 
2832624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2842624d5faSMark Brown 						 codec->debugfs_codec_root,
2852624d5faSMark Brown 						 codec, &codec_reg_fops);
2862624d5faSMark Brown 	if (!codec->debugfs_reg)
28764e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
2882624d5faSMark Brown 
2898eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
2902624d5faSMark Brown }
2912624d5faSMark Brown 
2922624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2932624d5faSMark Brown {
2942624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2952624d5faSMark Brown }
2962624d5faSMark Brown 
297731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
298731f1ab2SSebastien Guiriec {
299731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
300731f1ab2SSebastien Guiriec 
301731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
302731f1ab2SSebastien Guiriec 						       debugfs_card_root);
303731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
304731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
305731f1ab2SSebastien Guiriec 			"Failed to create platform debugfs directory\n");
306731f1ab2SSebastien Guiriec 		return;
307731f1ab2SSebastien Guiriec 	}
308731f1ab2SSebastien Guiriec 
309731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
310731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
311731f1ab2SSebastien Guiriec }
312731f1ab2SSebastien Guiriec 
313731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
314731f1ab2SSebastien Guiriec {
315731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
316731f1ab2SSebastien Guiriec }
317731f1ab2SSebastien Guiriec 
318c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
319c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
320c3c5a19aSMark Brown {
321c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3222b194f9dSMark Brown 	ssize_t len, ret = 0;
323c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
324c3c5a19aSMark Brown 
325c3c5a19aSMark Brown 	if (!buf)
326c3c5a19aSMark Brown 		return -ENOMEM;
327c3c5a19aSMark Brown 
3282b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3292b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
330c3c5a19aSMark Brown 			       codec->name);
3312b194f9dSMark Brown 		if (len >= 0)
3322b194f9dSMark Brown 			ret += len;
3332b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3342b194f9dSMark Brown 			ret = PAGE_SIZE;
3352b194f9dSMark Brown 			break;
3362b194f9dSMark Brown 		}
3372b194f9dSMark Brown 	}
338c3c5a19aSMark Brown 
339c3c5a19aSMark Brown 	if (ret >= 0)
340c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
341c3c5a19aSMark Brown 
342c3c5a19aSMark Brown 	kfree(buf);
343c3c5a19aSMark Brown 
344c3c5a19aSMark Brown 	return ret;
345c3c5a19aSMark Brown }
346c3c5a19aSMark Brown 
347c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
348c3c5a19aSMark Brown 	.read = codec_list_read_file,
349c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
350c3c5a19aSMark Brown };
351c3c5a19aSMark Brown 
352f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
353f3208780SMark Brown 				  size_t count, loff_t *ppos)
354f3208780SMark Brown {
355f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3562b194f9dSMark Brown 	ssize_t len, ret = 0;
357f3208780SMark Brown 	struct snd_soc_dai *dai;
358f3208780SMark Brown 
359f3208780SMark Brown 	if (!buf)
360f3208780SMark Brown 		return -ENOMEM;
361f3208780SMark Brown 
3622b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3632b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3642b194f9dSMark Brown 		if (len >= 0)
3652b194f9dSMark Brown 			ret += len;
3662b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3672b194f9dSMark Brown 			ret = PAGE_SIZE;
3682b194f9dSMark Brown 			break;
3692b194f9dSMark Brown 		}
3702b194f9dSMark Brown 	}
371f3208780SMark Brown 
372f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
373f3208780SMark Brown 
374f3208780SMark Brown 	kfree(buf);
375f3208780SMark Brown 
376f3208780SMark Brown 	return ret;
377f3208780SMark Brown }
378f3208780SMark Brown 
379f3208780SMark Brown static const struct file_operations dai_list_fops = {
380f3208780SMark Brown 	.read = dai_list_read_file,
381f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
382f3208780SMark Brown };
383f3208780SMark Brown 
38419c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
38519c7ac27SMark Brown 				       char __user *user_buf,
38619c7ac27SMark Brown 				       size_t count, loff_t *ppos)
38719c7ac27SMark Brown {
38819c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3892b194f9dSMark Brown 	ssize_t len, ret = 0;
39019c7ac27SMark Brown 	struct snd_soc_platform *platform;
39119c7ac27SMark Brown 
39219c7ac27SMark Brown 	if (!buf)
39319c7ac27SMark Brown 		return -ENOMEM;
39419c7ac27SMark Brown 
3952b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3962b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
39719c7ac27SMark Brown 			       platform->name);
3982b194f9dSMark Brown 		if (len >= 0)
3992b194f9dSMark Brown 			ret += len;
4002b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4012b194f9dSMark Brown 			ret = PAGE_SIZE;
4022b194f9dSMark Brown 			break;
4032b194f9dSMark Brown 		}
4042b194f9dSMark Brown 	}
40519c7ac27SMark Brown 
40619c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
40719c7ac27SMark Brown 
40819c7ac27SMark Brown 	kfree(buf);
40919c7ac27SMark Brown 
41019c7ac27SMark Brown 	return ret;
41119c7ac27SMark Brown }
41219c7ac27SMark Brown 
41319c7ac27SMark Brown static const struct file_operations platform_list_fops = {
41419c7ac27SMark Brown 	.read = platform_list_read_file,
41519c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
41619c7ac27SMark Brown };
41719c7ac27SMark Brown 
418a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
419a6052154SJarkko Nikula {
420a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4218a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4223a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
423a6052154SJarkko Nikula 		dev_warn(card->dev,
4247c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4253a45b867SJarkko Nikula 		return;
4263a45b867SJarkko Nikula 	}
4273a45b867SJarkko Nikula 
4283a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4293a45b867SJarkko Nikula 						    card->debugfs_card_root,
4303a45b867SJarkko Nikula 						    &card->pop_time);
4313a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4323a45b867SJarkko Nikula 		dev_warn(card->dev,
4333a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
434a6052154SJarkko Nikula }
435a6052154SJarkko Nikula 
436a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
437a6052154SJarkko Nikula {
438a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
439a6052154SJarkko Nikula }
440a6052154SJarkko Nikula 
4412624d5faSMark Brown #else
4422624d5faSMark Brown 
4432624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4442624d5faSMark Brown {
4452624d5faSMark Brown }
4462624d5faSMark Brown 
4472624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4482624d5faSMark Brown {
4492624d5faSMark Brown }
450b95fccbcSAxel Lin 
451731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
452731f1ab2SSebastien Guiriec {
453731f1ab2SSebastien Guiriec }
454731f1ab2SSebastien Guiriec 
455731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
456731f1ab2SSebastien Guiriec {
457731f1ab2SSebastien Guiriec }
458731f1ab2SSebastien Guiriec 
459b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
460b95fccbcSAxel Lin {
461b95fccbcSAxel Lin }
462b95fccbcSAxel Lin 
463b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
464b95fccbcSAxel Lin {
465b95fccbcSAxel Lin }
4662624d5faSMark Brown #endif
4672624d5faSMark Brown 
46847c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
46947c88fffSLiam Girdwood 		const char *dai_link, int stream)
47047c88fffSLiam Girdwood {
47147c88fffSLiam Girdwood 	int i;
47247c88fffSLiam Girdwood 
47347c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
47447c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
47547c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
47647c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
47747c88fffSLiam Girdwood 	}
47847c88fffSLiam Girdwood 	dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
47947c88fffSLiam Girdwood 	return NULL;
48047c88fffSLiam Girdwood }
48147c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
48247c88fffSLiam Girdwood 
48347c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
48447c88fffSLiam Girdwood 		const char *dai_link)
48547c88fffSLiam Girdwood {
48647c88fffSLiam Girdwood 	int i;
48747c88fffSLiam Girdwood 
48847c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
48947c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
49047c88fffSLiam Girdwood 			return &card->rtd[i];
49147c88fffSLiam Girdwood 	}
49247c88fffSLiam Girdwood 	dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
49347c88fffSLiam Girdwood 	return NULL;
49447c88fffSLiam Girdwood }
49547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
49647c88fffSLiam Girdwood 
497db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
498db2a4165SFrank Mandarino /* unregister ac97 codec */
499db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
500db2a4165SFrank Mandarino {
501db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
502db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
503db2a4165SFrank Mandarino 	return 0;
504db2a4165SFrank Mandarino }
505db2a4165SFrank Mandarino 
506db2a4165SFrank Mandarino /* stop no dev release warning */
507db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
508db2a4165SFrank Mandarino 
509db2a4165SFrank Mandarino /* register ac97 codec to bus */
510db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
511db2a4165SFrank Mandarino {
512db2a4165SFrank Mandarino 	int err;
513db2a4165SFrank Mandarino 
514db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
5154ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
516db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
517db2a4165SFrank Mandarino 
518bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
519f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
520db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
521db2a4165SFrank Mandarino 	if (err < 0) {
522db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
523db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
524db2a4165SFrank Mandarino 		return err;
525db2a4165SFrank Mandarino 	}
526db2a4165SFrank Mandarino 	return 0;
527db2a4165SFrank Mandarino }
528db2a4165SFrank Mandarino #endif
529db2a4165SFrank Mandarino 
5306f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
531db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5326f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
533db2a4165SFrank Mandarino {
5346f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5352eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
536db2a4165SFrank Mandarino 	int i;
537db2a4165SFrank Mandarino 
538e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
539e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
540e3509ff0SDaniel Mack 	 */
541f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
542e3509ff0SDaniel Mack 		return 0;
543e3509ff0SDaniel Mack 
5446ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5456ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5466ed25978SAndy Green 	 */
547f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
548f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
549f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5506ed25978SAndy Green 
5516ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
552f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5536ed25978SAndy Green 
554a00f90f9SMark Brown 	/* mute any active DACs */
555f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
556f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
557f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5583efab7dcSMark Brown 
559f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5603efab7dcSMark Brown 			continue;
5613efab7dcSMark Brown 
562f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
563f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
564db2a4165SFrank Mandarino 	}
565db2a4165SFrank Mandarino 
5664ccab3e7SLiam Girdwood 	/* suspend all pcms */
567f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
568f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5693efab7dcSMark Brown 			continue;
5703efab7dcSMark Brown 
571f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5723efab7dcSMark Brown 	}
5734ccab3e7SLiam Girdwood 
57487506549SMark Brown 	if (card->suspend_pre)
57570b2ac12SMark Brown 		card->suspend_pre(card);
576db2a4165SFrank Mandarino 
577f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
578f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
579f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5803efab7dcSMark Brown 
581f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5823efab7dcSMark Brown 			continue;
5833efab7dcSMark Brown 
584f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
585f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
586f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
587f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
588f0fba2adSLiam Girdwood 			platform->suspended = 1;
589f0fba2adSLiam Girdwood 		}
590db2a4165SFrank Mandarino 	}
591db2a4165SFrank Mandarino 
592db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
593f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5945b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
595ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
596f0fba2adSLiam Girdwood 	}
597db2a4165SFrank Mandarino 
598f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5993efab7dcSMark Brown 
600f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6013efab7dcSMark Brown 			continue;
6023efab7dcSMark Brown 
6037bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6047bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
605db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
606f0fba2adSLiam Girdwood 
6077bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6087bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
609db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
610db2a4165SFrank Mandarino 	}
611db2a4165SFrank Mandarino 
612f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6132eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6141547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6151547aba9SMark Brown 		 * bias _ON and should not be suspended. */
616f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
617ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6181547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
619125a25daSMark Brown 				/*
620125a25daSMark Brown 				 * If the CODEC is capable of idle
621125a25daSMark Brown 				 * bias off then being in STANDBY
622125a25daSMark Brown 				 * means it's doing something,
623125a25daSMark Brown 				 * otherwise fall through.
624125a25daSMark Brown 				 */
625125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
626125a25daSMark Brown 					dev_dbg(codec->dev,
627125a25daSMark Brown 						"idle_bias_off CODEC on over suspend\n");
628125a25daSMark Brown 					break;
629125a25daSMark Brown 				}
6301547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
63184b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
632f0fba2adSLiam Girdwood 				codec->suspended = 1;
6337be4ba24SMark Brown 				codec->cache_sync = 1;
6341547aba9SMark Brown 				break;
6351547aba9SMark Brown 			default:
636f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
6371547aba9SMark Brown 				break;
6381547aba9SMark Brown 			}
6391547aba9SMark Brown 		}
640f0fba2adSLiam Girdwood 	}
641db2a4165SFrank Mandarino 
642f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
643f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6443efab7dcSMark Brown 
645f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6463efab7dcSMark Brown 			continue;
6473efab7dcSMark Brown 
648f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
649f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
650db2a4165SFrank Mandarino 	}
651db2a4165SFrank Mandarino 
65287506549SMark Brown 	if (card->suspend_post)
65370b2ac12SMark Brown 		card->suspend_post(card);
654db2a4165SFrank Mandarino 
655db2a4165SFrank Mandarino 	return 0;
656db2a4165SFrank Mandarino }
6576f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
658db2a4165SFrank Mandarino 
6596ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6606ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6616ed25978SAndy Green  */
6626ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
663db2a4165SFrank Mandarino {
664f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
665f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6662eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
667db2a4165SFrank Mandarino 	int i;
668db2a4165SFrank Mandarino 
6696ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6706ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6716ed25978SAndy Green 	 */
6726ed25978SAndy Green 
673f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
6746ed25978SAndy Green 
6759949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
676f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6779949788bSMark Brown 
67887506549SMark Brown 	if (card->resume_pre)
67970b2ac12SMark Brown 		card->resume_pre(card);
680db2a4165SFrank Mandarino 
681f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
682f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
683f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6843efab7dcSMark Brown 
685f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6863efab7dcSMark Brown 			continue;
6873efab7dcSMark Brown 
688f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
689f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
690db2a4165SFrank Mandarino 	}
691db2a4165SFrank Mandarino 
6922eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6931547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6941547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
6951547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
6961547aba9SMark Brown 		 */
697f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
698ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6991547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7001547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
701f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
702f0fba2adSLiam Girdwood 				codec->suspended = 0;
7031547aba9SMark Brown 				break;
7041547aba9SMark Brown 			default:
705f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
7061547aba9SMark Brown 				break;
7071547aba9SMark Brown 			}
7081547aba9SMark Brown 		}
709f0fba2adSLiam Girdwood 	}
710db2a4165SFrank Mandarino 
711f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7123efab7dcSMark Brown 
713f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7143efab7dcSMark Brown 			continue;
7153efab7dcSMark Brown 
7167bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
717d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
718db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
719f0fba2adSLiam Girdwood 
7207bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
721d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
722db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
723db2a4165SFrank Mandarino 	}
724db2a4165SFrank Mandarino 
7253ff3f64bSMark Brown 	/* unmute any active DACs */
726f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
727f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
728f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7293efab7dcSMark Brown 
730f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7313efab7dcSMark Brown 			continue;
7323efab7dcSMark Brown 
733f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
734f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
735db2a4165SFrank Mandarino 	}
736db2a4165SFrank Mandarino 
737f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
738f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
739f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7403efab7dcSMark Brown 
741f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7423efab7dcSMark Brown 			continue;
7433efab7dcSMark Brown 
744f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
745f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
746f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
747f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
748f0fba2adSLiam Girdwood 			platform->suspended = 0;
749f0fba2adSLiam Girdwood 		}
750db2a4165SFrank Mandarino 	}
751db2a4165SFrank Mandarino 
75287506549SMark Brown 	if (card->resume_post)
75370b2ac12SMark Brown 		card->resume_post(card);
754db2a4165SFrank Mandarino 
755f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
7566ed25978SAndy Green 
7576ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
758f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7596ed25978SAndy Green }
7606ed25978SAndy Green 
7616ed25978SAndy Green /* powers up audio subsystem after a suspend */
7626f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7636ed25978SAndy Green {
7646f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
76582e14e8bSStephen Warren 	int i, ac97_control = 0;
766b9dd94a8SPeter Ujfalusi 
7675ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
7685ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
7695ff1ddf2SEric Miao 	 */
7705ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
7715ff1ddf2SEric Miao 		return 0;
7725ff1ddf2SEric Miao 
77364ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
77464ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
77564ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
77664ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
77764ab9baaSMark Brown 	 */
778f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
779f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
78082e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
78182e14e8bSStephen Warren 	}
78282e14e8bSStephen Warren 	if (ac97_control) {
783f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
78464ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
78564ab9baaSMark Brown 	} else {
786f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
7876308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
788f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
789f0fba2adSLiam Girdwood 	}
7906ed25978SAndy Green 
791db2a4165SFrank Mandarino 	return 0;
792db2a4165SFrank Mandarino }
7936f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
794db2a4165SFrank Mandarino #else
7956f8ab4acSMark Brown #define snd_soc_suspend NULL
7966f8ab4acSMark Brown #define snd_soc_resume NULL
797db2a4165SFrank Mandarino #endif
798db2a4165SFrank Mandarino 
79985e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
80002a06d30SBarry Song };
80102a06d30SBarry Song 
802f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
803db2a4165SFrank Mandarino {
804f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
805f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
806fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
807435c5e25SMark Brown 	struct snd_soc_platform *platform;
808f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
809848dd8beSMark Brown 	const char *platform_name;
810db2a4165SFrank Mandarino 
811f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
8126308419aSMark Brown 
813b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
814f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
815bc92657aSStephen Warren 		if (dai_link->cpu_of_node &&
816bc92657aSStephen Warren 		    (cpu_dai->dev->of_node != dai_link->cpu_of_node))
8175a504963SStephen Warren 			continue;
818bc92657aSStephen Warren 		if (dai_link->cpu_name &&
819bc92657aSStephen Warren 		    strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
8202610ab77SStephen Warren 			continue;
821bc92657aSStephen Warren 		if (dai_link->cpu_dai_name &&
822bc92657aSStephen Warren 		    strcmp(cpu_dai->name, dai_link->cpu_dai_name))
823bc92657aSStephen Warren 			continue;
8242610ab77SStephen Warren 
825f0fba2adSLiam Girdwood 		rtd->cpu_dai = cpu_dai;
826f0fba2adSLiam Girdwood 	}
827b19e6e7bSMark Brown 
828b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
829f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CPU DAI %s not registered\n",
830f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
831b19e6e7bSMark Brown 		return -EPROBE_DEFER;
832c5af3a2eSMark Brown 	}
833c5af3a2eSMark Brown 
834b19e6e7bSMark Brown 	/* Find CODEC from registered CODECs */
835f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8365a504963SStephen Warren 		if (dai_link->codec_of_node) {
8375a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8385a504963SStephen Warren 				continue;
8395a504963SStephen Warren 		} else {
8402610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8412610ab77SStephen Warren 				continue;
8425a504963SStephen Warren 		}
8432610ab77SStephen Warren 
844f0fba2adSLiam Girdwood 		rtd->codec = codec;
845f0fba2adSLiam Girdwood 
8462610ab77SStephen Warren 		/*
8472610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
8482610ab77SStephen Warren 		 * this CODEC
8492610ab77SStephen Warren 		 */
850f0fba2adSLiam Girdwood 		list_for_each_entry(codec_dai, &dai_list, list) {
851f0fba2adSLiam Girdwood 			if (codec->dev == codec_dai->dev &&
8522610ab77SStephen Warren 				!strcmp(codec_dai->name,
8532610ab77SStephen Warren 					dai_link->codec_dai_name)) {
8542610ab77SStephen Warren 
855f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
856435c5e25SMark Brown 			}
857f0fba2adSLiam Girdwood 		}
858b19e6e7bSMark Brown 
859b19e6e7bSMark Brown 		if (!rtd->codec_dai) {
860f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
861f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
862b19e6e7bSMark Brown 			return -EPROBE_DEFER;
863f0fba2adSLiam Girdwood 		}
864b19e6e7bSMark Brown 	}
865b19e6e7bSMark Brown 
866b19e6e7bSMark Brown 	if (!rtd->codec) {
867f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CODEC %s not registered\n",
868f0fba2adSLiam Girdwood 			dai_link->codec_name);
869b19e6e7bSMark Brown 		return -EPROBE_DEFER;
870b19e6e7bSMark Brown 	}
871848dd8beSMark Brown 
872848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
873848dd8beSMark Brown 	platform_name = dai_link->platform_name;
8745a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
875848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
876848dd8beSMark Brown 
877b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
878f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
8795a504963SStephen Warren 		if (dai_link->platform_of_node) {
8805a504963SStephen Warren 			if (platform->dev->of_node !=
8815a504963SStephen Warren 			    dai_link->platform_of_node)
8825a504963SStephen Warren 				continue;
8835a504963SStephen Warren 		} else {
8842610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
8852610ab77SStephen Warren 				continue;
8865a504963SStephen Warren 		}
8872610ab77SStephen Warren 
888f0fba2adSLiam Girdwood 		rtd->platform = platform;
889f0fba2adSLiam Girdwood 	}
890b19e6e7bSMark Brown 	if (!rtd->platform) {
891f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "platform %s not registered\n",
892f0fba2adSLiam Girdwood 			dai_link->platform_name);
893b19e6e7bSMark Brown 		return -EPROBE_DEFER;
894f0fba2adSLiam Girdwood 	}
895b19e6e7bSMark Brown 
896b19e6e7bSMark Brown 	card->num_rtd++;
897b19e6e7bSMark Brown 
898b19e6e7bSMark Brown 	return 0;
8996b05eda6SMark Brown }
9006b05eda6SMark Brown 
901589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
902589c3563SJarkko Nikula {
903589c3563SJarkko Nikula 	int err;
904589c3563SJarkko Nikula 
905589c3563SJarkko Nikula 	if (codec->driver->remove) {
906589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
907589c3563SJarkko Nikula 		if (err < 0)
908589c3563SJarkko Nikula 			dev_err(codec->dev,
909589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
910589c3563SJarkko Nikula 				codec->name, err);
911589c3563SJarkko Nikula 	}
912589c3563SJarkko Nikula 
913589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
914589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
915589c3563SJarkko Nikula 
916589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
917589c3563SJarkko Nikula 	codec->probed = 0;
918589c3563SJarkko Nikula 	list_del(&codec->card_list);
919589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
920589c3563SJarkko Nikula }
921589c3563SJarkko Nikula 
9220168bf0dSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
923f0fba2adSLiam Girdwood {
924f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
925f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
926f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
927f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
928f0fba2adSLiam Girdwood 	int err;
929f0fba2adSLiam Girdwood 
930f0fba2adSLiam Girdwood 	/* unregister the rtd device */
931f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
93236ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
93336ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
93436ae1a96SMark Brown 		device_unregister(rtd->dev);
935f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
93602a06d30SBarry Song 	}
93702a06d30SBarry Song 
938f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9390168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
9400168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
941f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
942f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
943f0fba2adSLiam Girdwood 			if (err < 0)
9440837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9450837fc62SFabio Estevam 							codec_dai->name, err);
946f0fba2adSLiam Girdwood 		}
947f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
948f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
949f0fba2adSLiam Girdwood 	}
950f0fba2adSLiam Girdwood 
951f0fba2adSLiam Girdwood 	/* remove the platform */
9520168bf0dSLiam Girdwood 	if (platform && platform->probed &&
9530168bf0dSLiam Girdwood 			platform->driver->remove_order == order) {
954f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
955f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
956f0fba2adSLiam Girdwood 			if (err < 0)
9570837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9580837fc62SFabio Estevam 							platform->name, err);
959f0fba2adSLiam Girdwood 		}
960675c496bSLiam Girdwood 
961675c496bSLiam Girdwood 		/* Make sure all DAPM widgets are freed */
962675c496bSLiam Girdwood 		snd_soc_dapm_free(&platform->dapm);
963675c496bSLiam Girdwood 
964731f1ab2SSebastien Guiriec 		soc_cleanup_platform_debugfs(platform);
965f0fba2adSLiam Girdwood 		platform->probed = 0;
966f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
967f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
968f0fba2adSLiam Girdwood 	}
969f0fba2adSLiam Girdwood 
970f0fba2adSLiam Girdwood 	/* remove the CODEC */
9710168bf0dSLiam Girdwood 	if (codec && codec->probed &&
9720168bf0dSLiam Girdwood 			codec->driver->remove_order == order)
973589c3563SJarkko Nikula 		soc_remove_codec(codec);
974f0fba2adSLiam Girdwood 
975f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
9760168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
9770168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
978f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
979f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
980f0fba2adSLiam Girdwood 			if (err < 0)
9810837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9820837fc62SFabio Estevam 							cpu_dai->name, err);
983f0fba2adSLiam Girdwood 		}
984f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
985f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
986a9db7dbeSStephen Warren 
987*18d75644SStephen Warren 		if (!cpu_dai->codec) {
988*18d75644SStephen Warren 			snd_soc_dapm_free(&cpu_dai->dapm);
989f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
990f0fba2adSLiam Girdwood 		}
991f0fba2adSLiam Girdwood 	}
992*18d75644SStephen Warren }
993f0fba2adSLiam Girdwood 
9940671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9950671fd8eSKuninori Morimoto {
9960168bf0dSLiam Girdwood 	int dai, order;
9970671fd8eSKuninori Morimoto 
9980168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9990168bf0dSLiam Girdwood 			order++) {
10000168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
10010168bf0dSLiam Girdwood 			soc_remove_dai_link(card, dai, order);
10020168bf0dSLiam Girdwood 	}
10030671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10040671fd8eSKuninori Morimoto }
10050671fd8eSKuninori Morimoto 
1006ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1007ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1008ead9b919SJarkko Nikula {
1009ead9b919SJarkko Nikula 	int i;
1010ead9b919SJarkko Nikula 
1011ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1012ead9b919SJarkko Nikula 		return;
1013ead9b919SJarkko Nikula 
1014ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1015ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1016ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1017ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1018ead9b919SJarkko Nikula 			break;
1019ead9b919SJarkko Nikula 		}
1020ead9b919SJarkko Nikula 	}
1021ead9b919SJarkko Nikula }
1022ead9b919SJarkko Nikula 
1023589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1024589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1025589c3563SJarkko Nikula {
1026589c3563SJarkko Nikula 	int ret = 0;
102789b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1028888df395SMark Brown 	struct snd_soc_dai *dai;
1029589c3563SJarkko Nikula 
1030589c3563SJarkko Nikula 	codec->card = card;
1031589c3563SJarkko Nikula 	codec->dapm.card = card;
1032589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1033589c3563SJarkko Nikula 
103470d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
103570d29331SJarkko Nikula 		return -ENODEV;
103670d29331SJarkko Nikula 
1037d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1038d5d1e0beSLars-Peter Clausen 
103977530150SLars-Peter Clausen 	if (driver->dapm_widgets)
104077530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
104177530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
104277530150SLars-Peter Clausen 
1043888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1044888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1045888df395SMark Brown 		if (dai->dev != codec->dev)
1046888df395SMark Brown 			continue;
1047888df395SMark Brown 
1048888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1049888df395SMark Brown 	}
1050888df395SMark Brown 
105133c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
105233c5f969SMark Brown 
105389b95ac0SMark Brown 	if (driver->probe) {
105489b95ac0SMark Brown 		ret = driver->probe(codec);
1055589c3563SJarkko Nikula 		if (ret < 0) {
1056589c3563SJarkko Nikula 			dev_err(codec->dev,
1057589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1058589c3563SJarkko Nikula 				codec->name, ret);
105970d29331SJarkko Nikula 			goto err_probe;
1060589c3563SJarkko Nikula 		}
1061589c3563SJarkko Nikula 	}
1062589c3563SJarkko Nikula 
1063b7af1dafSMark Brown 	if (driver->controls)
1064022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1065b7af1dafSMark Brown 				     driver->num_controls);
106689b95ac0SMark Brown 	if (driver->dapm_routes)
106789b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
106889b95ac0SMark Brown 					driver->num_dapm_routes);
106989b95ac0SMark Brown 
1070589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1071589c3563SJarkko Nikula 	codec->probed = 1;
1072589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
10737be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1074589c3563SJarkko Nikula 
107570d29331SJarkko Nikula 	return 0;
107670d29331SJarkko Nikula 
107770d29331SJarkko Nikula err_probe:
1078d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
107970d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
108070d29331SJarkko Nikula 
1081589c3563SJarkko Nikula 	return ret;
1082589c3563SJarkko Nikula }
1083589c3563SJarkko Nikula 
1084956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1085956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1086956245e9SLiam Girdwood {
1087956245e9SLiam Girdwood 	int ret = 0;
1088956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1089be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1090956245e9SLiam Girdwood 
1091956245e9SLiam Girdwood 	platform->card = card;
1092b7950641SLiam Girdwood 	platform->dapm.card = card;
1093956245e9SLiam Girdwood 
1094956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1095956245e9SLiam Girdwood 		return -ENODEV;
1096956245e9SLiam Girdwood 
1097731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1098731f1ab2SSebastien Guiriec 
1099cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1100cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1101cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1102cb2cf612SLiam Girdwood 
1103be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
1104be09ad90SLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
1105be09ad90SLiam Girdwood 		if (dai->dev != platform->dev)
1106be09ad90SLiam Girdwood 			continue;
1107be09ad90SLiam Girdwood 
1108be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1109be09ad90SLiam Girdwood 	}
1110be09ad90SLiam Girdwood 
11113fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
11123fec6b6dSStephen Warren 
1113956245e9SLiam Girdwood 	if (driver->probe) {
1114956245e9SLiam Girdwood 		ret = driver->probe(platform);
1115956245e9SLiam Girdwood 		if (ret < 0) {
1116956245e9SLiam Girdwood 			dev_err(platform->dev,
1117956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1118956245e9SLiam Girdwood 				platform->name, ret);
1119956245e9SLiam Girdwood 			goto err_probe;
1120956245e9SLiam Girdwood 		}
1121956245e9SLiam Girdwood 	}
1122956245e9SLiam Girdwood 
1123cb2cf612SLiam Girdwood 	if (driver->controls)
1124cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1125cb2cf612SLiam Girdwood 				     driver->num_controls);
1126cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1127cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1128cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1129cb2cf612SLiam Girdwood 
1130956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1131956245e9SLiam Girdwood 	platform->probed = 1;
1132956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1133b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1134956245e9SLiam Girdwood 
1135956245e9SLiam Girdwood 	return 0;
1136956245e9SLiam Girdwood 
1137956245e9SLiam Girdwood err_probe:
113802db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1139956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1140956245e9SLiam Girdwood 
1141956245e9SLiam Girdwood 	return ret;
1142956245e9SLiam Girdwood }
1143956245e9SLiam Girdwood 
114436ae1a96SMark Brown static void rtd_release(struct device *dev)
114536ae1a96SMark Brown {
114636ae1a96SMark Brown 	kfree(dev);
114736ae1a96SMark Brown }
1148f0fba2adSLiam Girdwood 
1149589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1150589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1151589c3563SJarkko Nikula 				   int num, int dailess)
1152589c3563SJarkko Nikula {
1153589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1154589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1155589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1156589c3563SJarkko Nikula 	const char *temp, *name;
1157589c3563SJarkko Nikula 	int ret = 0;
1158589c3563SJarkko Nikula 
1159589c3563SJarkko Nikula 	if (!dailess) {
1160589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1161589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1162589c3563SJarkko Nikula 		name = dai_link->name;
1163589c3563SJarkko Nikula 	} else {
1164589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1165589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1166589c3563SJarkko Nikula 		name = aux_dev->name;
1167589c3563SJarkko Nikula 	}
11680962bb21SJanusz Krzysztofik 	rtd->card = card;
1169589c3563SJarkko Nikula 
11704b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
11714b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
11724b1cfcb4SMark Brown 
1173589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1174589c3563SJarkko Nikula 	temp = codec->name_prefix;
1175589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1176589c3563SJarkko Nikula 
1177589c3563SJarkko Nikula 	/* do machine specific initialization */
1178589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1179589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1180589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1181589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1182589c3563SJarkko Nikula 	if (ret < 0) {
1183589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1184589c3563SJarkko Nikula 		return ret;
1185589c3563SJarkko Nikula 	}
1186589c3563SJarkko Nikula 	codec->name_prefix = temp;
1187589c3563SJarkko Nikula 
1188589c3563SJarkko Nikula 	/* register the rtd device */
1189589c3563SJarkko Nikula 	rtd->codec = codec;
119036ae1a96SMark Brown 
119136ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
119236ae1a96SMark Brown 	if (!rtd->dev)
119336ae1a96SMark Brown 		return -ENOMEM;
119436ae1a96SMark Brown 	device_initialize(rtd->dev);
119536ae1a96SMark Brown 	rtd->dev->parent = card->dev;
119636ae1a96SMark Brown 	rtd->dev->release = rtd_release;
119736ae1a96SMark Brown 	rtd->dev->init_name = name;
119836ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1199b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
120001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
120101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
120201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
120301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
120436ae1a96SMark Brown 	ret = device_add(rtd->dev);
1205589c3563SJarkko Nikula 	if (ret < 0) {
1206589c3563SJarkko Nikula 		dev_err(card->dev,
1207589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1208589c3563SJarkko Nikula 		return ret;
1209589c3563SJarkko Nikula 	}
1210589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1211589c3563SJarkko Nikula 
1212589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
121336ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1214589c3563SJarkko Nikula 	if (ret < 0)
1215589c3563SJarkko Nikula 		dev_err(codec->dev,
1216589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1217589c3563SJarkko Nikula 			ret);
1218589c3563SJarkko Nikula 
1219589c3563SJarkko Nikula 	/* add codec sysfs entries */
122036ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1221589c3563SJarkko Nikula 	if (ret < 0)
1222589c3563SJarkko Nikula 		dev_err(codec->dev,
1223589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1224589c3563SJarkko Nikula 
1225f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1226f86dcef8SLiam Girdwood 	/* add DPCM sysfs entries */
1227cd0f8911SLiam Girdwood 	if (!dailess && !dai_link->dynamic)
1228f86dcef8SLiam Girdwood 		goto out;
1229f86dcef8SLiam Girdwood 
1230f86dcef8SLiam Girdwood 	ret = soc_dpcm_debugfs_add(rtd);
1231f86dcef8SLiam Girdwood 	if (ret < 0)
1232f86dcef8SLiam Girdwood 		dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1233f86dcef8SLiam Girdwood 
1234f86dcef8SLiam Girdwood out:
1235f86dcef8SLiam Girdwood #endif
1236589c3563SJarkko Nikula 	return 0;
1237589c3563SJarkko Nikula }
1238589c3563SJarkko Nikula 
12390168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1240f0fba2adSLiam Girdwood {
1241f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1242f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1243f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1244f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1245c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1246c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1247c74184edSMark Brown 	struct snd_soc_dapm_widget *play_w, *capture_w;
1248f0fba2adSLiam Girdwood 	int ret;
1249f0fba2adSLiam Girdwood 
12500168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
12510168bf0dSLiam Girdwood 			card->name, num, order);
1252f0fba2adSLiam Girdwood 
1253f0fba2adSLiam Girdwood 	/* config components */
1254f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1255f0fba2adSLiam Girdwood 	codec_dai->card = card;
1256f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1257f0fba2adSLiam Girdwood 
1258f0fba2adSLiam Girdwood 	/* set default power off timeout */
1259f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1260f0fba2adSLiam Girdwood 
1261f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
12620168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
12630168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1264a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
1265be09ad90SLiam Girdwood 			cpu_dai->dapm.card = card;
126661b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
126761b61e3cSLiam Girdwood 				return -ENODEV;
126861b61e3cSLiam Girdwood 
1269*18d75644SStephen Warren 			list_add(&cpu_dai->dapm.list, &card->dapm_list);
1270be09ad90SLiam Girdwood 			snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1271a9db7dbeSStephen Warren 		}
1272be09ad90SLiam Girdwood 
1273f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1274f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1275f0fba2adSLiam Girdwood 			if (ret < 0) {
12760837fc62SFabio Estevam 				pr_err("asoc: failed to probe CPU DAI %s: %d\n",
12770837fc62SFabio Estevam 							cpu_dai->name, ret);
127861b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1279f0fba2adSLiam Girdwood 				return ret;
1280f0fba2adSLiam Girdwood 			}
1281f0fba2adSLiam Girdwood 		}
1282f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
12831c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1284f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1285f0fba2adSLiam Girdwood 	}
1286f0fba2adSLiam Girdwood 
1287f0fba2adSLiam Girdwood 	/* probe the CODEC */
12880168bf0dSLiam Girdwood 	if (!codec->probed &&
12890168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1290589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1291589c3563SJarkko Nikula 		if (ret < 0)
1292f0fba2adSLiam Girdwood 			return ret;
1293f0fba2adSLiam Girdwood 	}
1294f0fba2adSLiam Girdwood 
1295f0fba2adSLiam Girdwood 	/* probe the platform */
12960168bf0dSLiam Girdwood 	if (!platform->probed &&
12970168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1298956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1299956245e9SLiam Girdwood 		if (ret < 0)
1300f0fba2adSLiam Girdwood 			return ret;
1301f0fba2adSLiam Girdwood 	}
1302f0fba2adSLiam Girdwood 
1303f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
13040168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1305f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1306f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1307f0fba2adSLiam Girdwood 			if (ret < 0) {
13080837fc62SFabio Estevam 				pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
13090837fc62SFabio Estevam 							codec_dai->name, ret);
1310f0fba2adSLiam Girdwood 				return ret;
1311f0fba2adSLiam Girdwood 			}
1312f0fba2adSLiam Girdwood 		}
1313f0fba2adSLiam Girdwood 
13141c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1315f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1316f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1317f0fba2adSLiam Girdwood 	}
1318f0fba2adSLiam Girdwood 
13190168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
13200168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
13210168bf0dSLiam Girdwood 		return 0;
13220168bf0dSLiam Girdwood 
1323589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1324589c3563SJarkko Nikula 	if (ret)
1325f0fba2adSLiam Girdwood 		return ret;
1326f0fba2adSLiam Girdwood 
132736ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1328f0fba2adSLiam Girdwood 	if (ret < 0)
13290837fc62SFabio Estevam 		pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1330f0fba2adSLiam Girdwood 
1331c74184edSMark Brown 	if (!dai_link->params) {
1332f0fba2adSLiam Girdwood 		/* create the pcm */
1333f0fba2adSLiam Girdwood 		ret = soc_new_pcm(rtd, num);
1334f0fba2adSLiam Girdwood 		if (ret < 0) {
13350837fc62SFabio Estevam 			pr_err("asoc: can't create pcm %s :%d\n",
13360837fc62SFabio Estevam 			       dai_link->stream_name, ret);
1337f0fba2adSLiam Girdwood 			return ret;
1338f0fba2adSLiam Girdwood 		}
1339c74184edSMark Brown 	} else {
1340c74184edSMark Brown 		/* link the DAI widgets */
1341c74184edSMark Brown 		play_w = codec_dai->playback_widget;
1342c74184edSMark Brown 		capture_w = cpu_dai->capture_widget;
1343c74184edSMark Brown 		if (play_w && capture_w) {
1344c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1345c74184edSMark Brown 						   capture_w, play_w);
1346c74184edSMark Brown 			if (ret != 0) {
1347c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1348c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1349c74184edSMark Brown 				return ret;
1350c74184edSMark Brown 			}
1351c74184edSMark Brown 		}
1352c74184edSMark Brown 
1353c74184edSMark Brown 		play_w = cpu_dai->playback_widget;
1354c74184edSMark Brown 		capture_w = codec_dai->capture_widget;
1355c74184edSMark Brown 		if (play_w && capture_w) {
1356c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1357c74184edSMark Brown 						   capture_w, play_w);
1358c74184edSMark Brown 			if (ret != 0) {
1359c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1360c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1361c74184edSMark Brown 				return ret;
1362c74184edSMark Brown 			}
1363c74184edSMark Brown 		}
1364c74184edSMark Brown 	}
1365f0fba2adSLiam Girdwood 
1366f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1367f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1368f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1369f0fba2adSLiam Girdwood 
1370f0fba2adSLiam Girdwood 	return 0;
1371f0fba2adSLiam Girdwood }
1372f0fba2adSLiam Girdwood 
1373f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1374f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1375f0fba2adSLiam Girdwood {
1376f0fba2adSLiam Girdwood 	int ret;
1377f0fba2adSLiam Girdwood 
1378f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1379f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
13806b05eda6SMark Brown 	 */
1381f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
13820562f788SMika Westerberg 		/*
13830562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
13840562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
13850562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
13860562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
13870562f788SMika Westerberg 		 *
13880562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
13890562f788SMika Westerberg 		 */
13900562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
13910562f788SMika Westerberg 			return 0;
1392f0fba2adSLiam Girdwood 
1393f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1394f0fba2adSLiam Girdwood 		if (ret < 0) {
13950837fc62SFabio Estevam 			pr_err("asoc: AC97 device register failed:%d\n", ret);
1396f0fba2adSLiam Girdwood 			return ret;
1397435c5e25SMark Brown 		}
1398435c5e25SMark Brown 
1399f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1400f0fba2adSLiam Girdwood 	}
1401f0fba2adSLiam Girdwood 	return 0;
1402f0fba2adSLiam Girdwood }
1403435c5e25SMark Brown 
1404f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1405f0fba2adSLiam Girdwood {
1406f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1407f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1408f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1409f0fba2adSLiam Girdwood 	}
1410f0fba2adSLiam Girdwood }
1411f0fba2adSLiam Girdwood #endif
1412435c5e25SMark Brown 
1413b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1414b19e6e7bSMark Brown {
1415b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1416b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1417b19e6e7bSMark Brown 
1418b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1419b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1420b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1421b19e6e7bSMark Brown 			return 0;
1422b19e6e7bSMark Brown 	}
1423b19e6e7bSMark Brown 
1424b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1425b19e6e7bSMark Brown }
1426b19e6e7bSMark Brown 
14272eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
14282eea392dSJarkko Nikula {
14292eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
14302eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1431676ad98aSJarkko Nikula 	int ret = -ENODEV;
14322eea392dSJarkko Nikula 
14332eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
14342eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
14352eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
14362eea392dSJarkko Nikula 			if (codec->probed) {
14372eea392dSJarkko Nikula 				dev_err(codec->dev,
14382eea392dSJarkko Nikula 					"asoc: codec already probed");
14392eea392dSJarkko Nikula 				ret = -EBUSY;
14402eea392dSJarkko Nikula 				goto out;
14412eea392dSJarkko Nikula 			}
1442676ad98aSJarkko Nikula 			goto found;
14432eea392dSJarkko Nikula 		}
14442eea392dSJarkko Nikula 	}
1445676ad98aSJarkko Nikula 	/* codec not found */
1446676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1447b19e6e7bSMark Brown 	return -EPROBE_DEFER;
14482eea392dSJarkko Nikula 
1449676ad98aSJarkko Nikula found:
1450589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
14512eea392dSJarkko Nikula 	if (ret < 0)
1452589c3563SJarkko Nikula 		return ret;
14532eea392dSJarkko Nikula 
1454589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
14552eea392dSJarkko Nikula 
14562eea392dSJarkko Nikula out:
14572eea392dSJarkko Nikula 	return ret;
14582eea392dSJarkko Nikula }
14592eea392dSJarkko Nikula 
14602eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
14612eea392dSJarkko Nikula {
14622eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14632eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
14642eea392dSJarkko Nikula 
14652eea392dSJarkko Nikula 	/* unregister the rtd device */
14662eea392dSJarkko Nikula 	if (rtd->dev_registered) {
146736ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
146836ae1a96SMark Brown 		device_del(rtd->dev);
14692eea392dSJarkko Nikula 		rtd->dev_registered = 0;
14702eea392dSJarkko Nikula 	}
14712eea392dSJarkko Nikula 
1472589c3563SJarkko Nikula 	if (codec && codec->probed)
1473589c3563SJarkko Nikula 		soc_remove_codec(codec);
14742eea392dSJarkko Nikula }
14752eea392dSJarkko Nikula 
1476fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1477fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1478fdf0f54dSDimitris Papastamos {
1479fdf0f54dSDimitris Papastamos 	int ret;
1480fdf0f54dSDimitris Papastamos 
1481fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1482fdf0f54dSDimitris Papastamos 		return 0;
1483fdf0f54dSDimitris Papastamos 
1484fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1485fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1486fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1487fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1488fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1489fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1490fdf0f54dSDimitris Papastamos 			ret);
1491fdf0f54dSDimitris Papastamos 		return ret;
1492fdf0f54dSDimitris Papastamos 	}
1493fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1494fdf0f54dSDimitris Papastamos 	return 0;
1495fdf0f54dSDimitris Papastamos }
1496fdf0f54dSDimitris Papastamos 
1497b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1498f0fba2adSLiam Girdwood {
1499fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1500fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1501fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
150275d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1503f04209a7SMark Brown 	int ret, i, order, dai_fmt;
150496dd3622SMark Brown 
150501b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1506f0fba2adSLiam Girdwood 
1507b19e6e7bSMark Brown 	/* bind DAIs */
1508b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1509b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1510b19e6e7bSMark Brown 		if (ret != 0)
1511b19e6e7bSMark Brown 			goto base_error;
1512db2a4165SFrank Mandarino 	}
1513db2a4165SFrank Mandarino 
1514b19e6e7bSMark Brown 	/* check aux_devs too */
1515b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1516b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1517b19e6e7bSMark Brown 		if (ret != 0)
1518b19e6e7bSMark Brown 			goto base_error;
1519db2a4165SFrank Mandarino 	}
1520db2a4165SFrank Mandarino 
1521fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1522fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1523fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1524fdf0f54dSDimitris Papastamos 			continue;
1525861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1526861f2fafSDimitris Papastamos 		compress_type = 0;
1527fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1528fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1529fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1530fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1531fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1532fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1533fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1534fdf0f54dSDimitris Papastamos 					break;
1535fdf0f54dSDimitris Papastamos 			}
1536fdf0f54dSDimitris Papastamos 		}
1537fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1538b19e6e7bSMark Brown 		if (ret < 0)
1539b19e6e7bSMark Brown 			goto base_error;
1540fdf0f54dSDimitris Papastamos 	}
1541fdf0f54dSDimitris Papastamos 
1542f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1543f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1544f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1545f0fba2adSLiam Girdwood 	if (ret < 0) {
15460837fc62SFabio Estevam 		pr_err("asoc: can't create sound card for card %s: %d\n",
15470837fc62SFabio Estevam 			card->name, ret);
1548b19e6e7bSMark Brown 		goto base_error;
1549db2a4165SFrank Mandarino 	}
1550f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1551db2a4165SFrank Mandarino 
1552e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1553e37a4970SMark Brown 	card->dapm.dev = card->dev;
1554e37a4970SMark Brown 	card->dapm.card = card;
1555e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1556e37a4970SMark Brown 
1557d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1558d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1559d5d1e0beSLars-Peter Clausen #endif
1560d5d1e0beSLars-Peter Clausen 
156188ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
15626ed25978SAndy Green 	/* deferred resume work */
15636308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
15641301a964SRandy Dunlap #endif
15656ed25978SAndy Green 
15669a841ebbSMark Brown 	if (card->dapm_widgets)
15679a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
15689a841ebbSMark Brown 					  card->num_dapm_widgets);
15699a841ebbSMark Brown 
1570f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1571f0fba2adSLiam Girdwood 	if (card->probe) {
1572e7361ec4SMark Brown 		ret = card->probe(card);
1573f0fba2adSLiam Girdwood 		if (ret < 0)
1574f0fba2adSLiam Girdwood 			goto card_probe_error;
1575f0fba2adSLiam Girdwood 	}
1576f0fba2adSLiam Girdwood 
15770168bf0dSLiam Girdwood 	/* early DAI link probe */
15780168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
15790168bf0dSLiam Girdwood 			order++) {
1580fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
15810168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1582fe3e78e0SMark Brown 			if (ret < 0) {
1583321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1584321de0d0SMark Brown 			       card->name, ret);
1585f0fba2adSLiam Girdwood 				goto probe_dai_err;
1586fe3e78e0SMark Brown 			}
1587fe3e78e0SMark Brown 		}
15880168bf0dSLiam Girdwood 	}
1589fe3e78e0SMark Brown 
15902eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
15912eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
15922eea392dSJarkko Nikula 		if (ret < 0) {
15932eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
15942eea392dSJarkko Nikula 			       card->name, ret);
15952eea392dSJarkko Nikula 			goto probe_aux_dev_err;
15962eea392dSJarkko Nikula 		}
15972eea392dSJarkko Nikula 	}
15982eea392dSJarkko Nikula 
1599888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1600888df395SMark Brown 
1601b7af1dafSMark Brown 	if (card->controls)
1602022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1603b7af1dafSMark Brown 
1604b8ad29deSMark Brown 	if (card->dapm_routes)
1605b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1606b8ad29deSMark Brown 					card->num_dapm_routes);
1607b8ad29deSMark Brown 
1608b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1609b90d2f90SMark Brown 
161075d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
161175d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1612f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
161375d9ac46SMark Brown 
1614f04209a7SMark Brown 		if (dai_fmt) {
161575d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1616f04209a7SMark Brown 						  dai_fmt);
16175e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
161875d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
161975d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
162075d9ac46SMark Brown 					 ret);
1621f04209a7SMark Brown 		}
1622f04209a7SMark Brown 
1623f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1624fe33d4c5SStephen Warren 		if (dai_fmt &&
1625fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1626f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1627f04209a7SMark Brown 						  dai_fmt);
1628f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1629f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1630f04209a7SMark Brown 					 "Failed to set DAI format: %d\n",
1631f04209a7SMark Brown 					 ret);
1632f04209a7SMark Brown 		} else if (dai_fmt) {
1633f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1634f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1635f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1636f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1637f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1638f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1639f04209a7SMark Brown 				break;
1640f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1641f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1642f04209a7SMark Brown 				break;
1643f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1644f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1645f04209a7SMark Brown 				break;
1646f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1647f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1648f04209a7SMark Brown 				break;
1649f04209a7SMark Brown 			}
165075d9ac46SMark Brown 
165175d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1652f04209a7SMark Brown 						  dai_fmt);
16535e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
165475d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
165575d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
165675d9ac46SMark Brown 					 ret);
165775d9ac46SMark Brown 		}
165875d9ac46SMark Brown 	}
165975d9ac46SMark Brown 
1660f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1661fe3e78e0SMark Brown 		 "%s", card->name);
1662f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
166322de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1664f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1665f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1666f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1667f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1668f0e8ed85SMark Brown 		case '_':
1669f0e8ed85SMark Brown 		case '-':
1670f0e8ed85SMark Brown 		case '\0':
1671f0e8ed85SMark Brown 			break;
1672f0e8ed85SMark Brown 		default:
1673f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1674f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1675f0e8ed85SMark Brown 			break;
1676f0e8ed85SMark Brown 		}
1677f0e8ed85SMark Brown 	}
1678fe3e78e0SMark Brown 
167928e9ad92SMark Brown 	if (card->late_probe) {
168028e9ad92SMark Brown 		ret = card->late_probe(card);
168128e9ad92SMark Brown 		if (ret < 0) {
168228e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
168328e9ad92SMark Brown 				card->name, ret);
168428e9ad92SMark Brown 			goto probe_aux_dev_err;
168528e9ad92SMark Brown 		}
168628e9ad92SMark Brown 	}
168728e9ad92SMark Brown 
16882dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
16892dc00213SMark Brown 
16901633281bSStephen Warren 	if (card->fully_routed)
1691b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
16921633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
16931633281bSStephen Warren 
1694f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1695fe3e78e0SMark Brown 	if (ret < 0) {
16960837fc62SFabio Estevam 		pr_err("asoc: failed to register soundcard for %s: %d\n",
16970837fc62SFabio Estevam 							card->name, ret);
16986b3ed785SAxel Lin 		goto probe_aux_dev_err;
1699fe3e78e0SMark Brown 	}
1700fe3e78e0SMark Brown 
1701fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1702f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1703f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1704f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1705fe3e78e0SMark Brown 		if (ret < 0) {
17060837fc62SFabio Estevam 			pr_err("asoc: failed to register AC97 %s: %d\n",
17070837fc62SFabio Estevam 							card->name, ret);
17086b3ed785SAxel Lin 			while (--i >= 0)
17097d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
17106b3ed785SAxel Lin 			goto probe_aux_dev_err;
1711fe3e78e0SMark Brown 		}
1712fe3e78e0SMark Brown 	}
1713fe3e78e0SMark Brown #endif
1714fe3e78e0SMark Brown 
1715435c5e25SMark Brown 	card->instantiated = 1;
17164f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1717f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1718b19e6e7bSMark Brown 
1719b19e6e7bSMark Brown 	return 0;
1720db2a4165SFrank Mandarino 
17212eea392dSJarkko Nikula probe_aux_dev_err:
17222eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17232eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17242eea392dSJarkko Nikula 
1725f0fba2adSLiam Girdwood probe_dai_err:
17260671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1727fe3e78e0SMark Brown 
1728f0fba2adSLiam Girdwood card_probe_error:
172987506549SMark Brown 	if (card->remove)
1730e7361ec4SMark Brown 		card->remove(card);
1731f0fba2adSLiam Girdwood 
1732f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1733f0fba2adSLiam Girdwood 
1734b19e6e7bSMark Brown base_error:
1735f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1736db2a4165SFrank Mandarino 
1737b19e6e7bSMark Brown 	return ret;
1738435c5e25SMark Brown }
1739435c5e25SMark Brown 
1740435c5e25SMark Brown /* probes a new socdev */
1741435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1742435c5e25SMark Brown {
1743f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1744435c5e25SMark Brown 	int ret = 0;
1745435c5e25SMark Brown 
174670a7ca34SVinod Koul 	/*
174770a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
174870a7ca34SVinod Koul 	 * we should not be here in that case so ret error
174970a7ca34SVinod Koul 	 */
175070a7ca34SVinod Koul 	if (!card)
175170a7ca34SVinod Koul 		return -EINVAL;
175270a7ca34SVinod Koul 
1753fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1754fe4085e8SMark Brown 		 "ASoC machine %s should use snd_soc_register_card()\n",
1755fe4085e8SMark Brown 		 card->name);
1756fe4085e8SMark Brown 
1757435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1758435c5e25SMark Brown 	card->dev = &pdev->dev;
1759f0fba2adSLiam Girdwood 
1760435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1761435c5e25SMark Brown 	if (ret != 0) {
1762435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1763db2a4165SFrank Mandarino 		return ret;
1764db2a4165SFrank Mandarino 	}
1765db2a4165SFrank Mandarino 
1766435c5e25SMark Brown 	return 0;
1767435c5e25SMark Brown }
1768435c5e25SMark Brown 
1769b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1770db2a4165SFrank Mandarino {
1771db2a4165SFrank Mandarino 	int i;
1772db2a4165SFrank Mandarino 
1773f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1774f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1775f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17765b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1777db2a4165SFrank Mandarino 	}
1778db2a4165SFrank Mandarino 
17792eea392dSJarkko Nikula 	/* remove auxiliary devices */
17802eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17812eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17822eea392dSJarkko Nikula 
1783f0fba2adSLiam Girdwood 	/* remove and free each DAI */
17840671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1785f0fba2adSLiam Girdwood 
1786a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1787a6052154SJarkko Nikula 
1788f0fba2adSLiam Girdwood 	/* remove the card */
178987506549SMark Brown 	if (card->remove)
1790e7361ec4SMark Brown 		card->remove(card);
1791f0fba2adSLiam Girdwood 
17920aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
17930aaae527SLars-Peter Clausen 
1794f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1795b0e26485SVinod Koul 	return 0;
1796b0e26485SVinod Koul 
1797b2dfa62cSGuennadi Liakhovetski }
1798b0e26485SVinod Koul 
1799b0e26485SVinod Koul /* removes a socdev */
1800b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1801b0e26485SVinod Koul {
1802b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1803b0e26485SVinod Koul 
1804c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1805db2a4165SFrank Mandarino 	return 0;
1806db2a4165SFrank Mandarino }
1807db2a4165SFrank Mandarino 
18086f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
180951737470SMark Brown {
18106f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1811f0fba2adSLiam Girdwood 	int i;
181251737470SMark Brown 
181351737470SMark Brown 	if (!card->instantiated)
1814416356fcSMark Brown 		return 0;
181551737470SMark Brown 
181651737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
181751737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1818f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1819f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
18205b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1821f0fba2adSLiam Girdwood 	}
182251737470SMark Brown 
1823f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1824416356fcSMark Brown 
1825416356fcSMark Brown 	return 0;
182651737470SMark Brown }
18276f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
182851737470SMark Brown 
18296f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1830b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1831b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1832b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1833b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
18346f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1835b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1836416356fcSMark Brown };
1837deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1838416356fcSMark Brown 
1839db2a4165SFrank Mandarino /* ASoC platform driver */
1840db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1841db2a4165SFrank Mandarino 	.driver		= {
1842db2a4165SFrank Mandarino 		.name		= "soc-audio",
18438b45a209SKay Sievers 		.owner		= THIS_MODULE,
18446f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1845db2a4165SFrank Mandarino 	},
1846db2a4165SFrank Mandarino 	.probe		= soc_probe,
1847db2a4165SFrank Mandarino 	.remove		= soc_remove,
1848db2a4165SFrank Mandarino };
1849db2a4165SFrank Mandarino 
1850096e49d5SMark Brown /**
1851096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1852096e49d5SMark Brown  *
1853096e49d5SMark Brown  * @codec: CODEC to query.
1854096e49d5SMark Brown  * @reg: Register to query.
1855096e49d5SMark Brown  *
1856096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1857096e49d5SMark Brown  */
1858181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1859181e055eSMark Brown 				    unsigned int reg)
1860096e49d5SMark Brown {
18611500b7b5SDimitris Papastamos 	if (codec->volatile_register)
18621500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1863096e49d5SMark Brown 	else
1864096e49d5SMark Brown 		return 0;
1865096e49d5SMark Brown }
1866096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1867096e49d5SMark Brown 
1868db2a4165SFrank Mandarino /**
1869239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1870239c9706SDimitris Papastamos  *
1871239c9706SDimitris Papastamos  * @codec: CODEC to query.
1872239c9706SDimitris Papastamos  * @reg: Register to query.
1873239c9706SDimitris Papastamos  *
1874239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1875239c9706SDimitris Papastamos  */
1876239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1877239c9706SDimitris Papastamos 				    unsigned int reg)
1878239c9706SDimitris Papastamos {
1879239c9706SDimitris Papastamos 	if (codec->readable_register)
1880239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1881239c9706SDimitris Papastamos 	else
188263fa0a28SLars-Peter Clausen 		return 1;
1883239c9706SDimitris Papastamos }
1884239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1885239c9706SDimitris Papastamos 
1886239c9706SDimitris Papastamos /**
1887239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1888239c9706SDimitris Papastamos  *
1889239c9706SDimitris Papastamos  * @codec: CODEC to query.
1890239c9706SDimitris Papastamos  * @reg: Register to query.
1891239c9706SDimitris Papastamos  *
1892239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1893239c9706SDimitris Papastamos  */
1894239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1895239c9706SDimitris Papastamos 				    unsigned int reg)
1896239c9706SDimitris Papastamos {
1897239c9706SDimitris Papastamos 	if (codec->writable_register)
1898239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1899239c9706SDimitris Papastamos 	else
190063fa0a28SLars-Peter Clausen 		return 1;
1901239c9706SDimitris Papastamos }
1902239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1903239c9706SDimitris Papastamos 
1904f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1905f1442bc1SLiam Girdwood 					unsigned int reg)
1906f1442bc1SLiam Girdwood {
1907f1442bc1SLiam Girdwood 	unsigned int ret;
1908f1442bc1SLiam Girdwood 
1909f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1910f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1911f1442bc1SLiam Girdwood 		return -1;
1912f1442bc1SLiam Girdwood 	}
1913f1442bc1SLiam Girdwood 
1914f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1915f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1916a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1917f1442bc1SLiam Girdwood 
1918f1442bc1SLiam Girdwood 	return ret;
1919f1442bc1SLiam Girdwood }
1920f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1921f1442bc1SLiam Girdwood 
1922f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1923f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1924f1442bc1SLiam Girdwood {
1925f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1926f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1927f1442bc1SLiam Girdwood 		return -1;
1928f1442bc1SLiam Girdwood 	}
1929f1442bc1SLiam Girdwood 
1930f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1931a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
1932f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1933f1442bc1SLiam Girdwood }
1934f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1935f1442bc1SLiam Girdwood 
1936239c9706SDimitris Papastamos /**
1937db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1938db2a4165SFrank Mandarino  * @codec: audio codec
1939db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1940db2a4165SFrank Mandarino  * @num: AC97 codec number
1941db2a4165SFrank Mandarino  *
1942db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1943db2a4165SFrank Mandarino  */
1944db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1945db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1946db2a4165SFrank Mandarino {
1947db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1948db2a4165SFrank Mandarino 
1949db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1950db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1951db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1952db2a4165SFrank Mandarino 		return -ENOMEM;
1953db2a4165SFrank Mandarino 	}
1954db2a4165SFrank Mandarino 
1955db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1956db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1957db2a4165SFrank Mandarino 		kfree(codec->ac97);
1958db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1959db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1960db2a4165SFrank Mandarino 		return -ENOMEM;
1961db2a4165SFrank Mandarino 	}
1962db2a4165SFrank Mandarino 
1963db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1964db2a4165SFrank Mandarino 	codec->ac97->num = num;
19650562f788SMika Westerberg 
19660562f788SMika Westerberg 	/*
19670562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
19680562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
19690562f788SMika Westerberg 	 */
19700562f788SMika Westerberg 	codec->ac97_created = 1;
19710562f788SMika Westerberg 
1972db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1973db2a4165SFrank Mandarino 	return 0;
1974db2a4165SFrank Mandarino }
1975db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1976db2a4165SFrank Mandarino 
1977db2a4165SFrank Mandarino /**
1978db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1979db2a4165SFrank Mandarino  * @codec: audio codec
1980db2a4165SFrank Mandarino  *
1981db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1982db2a4165SFrank Mandarino  */
1983db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1984db2a4165SFrank Mandarino {
1985db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1986f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1987f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1988f0fba2adSLiam Girdwood #endif
1989db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1990db2a4165SFrank Mandarino 	kfree(codec->ac97);
1991db2a4165SFrank Mandarino 	codec->ac97 = NULL;
19920562f788SMika Westerberg 	codec->ac97_created = 0;
1993db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1994db2a4165SFrank Mandarino }
1995db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1996db2a4165SFrank Mandarino 
1997c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1998c3753707SMark Brown {
1999c3753707SMark Brown 	unsigned int ret;
2000c3753707SMark Brown 
2001c3acec26SMark Brown 	ret = codec->read(codec, reg);
2002c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2003a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2004c3753707SMark Brown 
2005c3753707SMark Brown 	return ret;
2006c3753707SMark Brown }
2007c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2008c3753707SMark Brown 
2009c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2010c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2011c3753707SMark Brown {
2012c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2013a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2014c3acec26SMark Brown 	return codec->write(codec, reg, val);
2015c3753707SMark Brown }
2016c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2017c3753707SMark Brown 
20185fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
20195fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
20205fb609d4SDimitris Papastamos {
20215fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
20225fb609d4SDimitris Papastamos }
20235fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
20245fb609d4SDimitris Papastamos 
2025db2a4165SFrank Mandarino /**
2026db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2027db2a4165SFrank Mandarino  * @codec: audio codec
2028db2a4165SFrank Mandarino  * @reg: codec register
2029db2a4165SFrank Mandarino  * @mask: register mask
2030db2a4165SFrank Mandarino  * @value: new value
2031db2a4165SFrank Mandarino  *
2032db2a4165SFrank Mandarino  * Writes new register value.
2033db2a4165SFrank Mandarino  *
2034180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
2035db2a4165SFrank Mandarino  */
2036db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
203746f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2038db2a4165SFrank Mandarino {
20398a713da8SMark Brown 	bool change;
204046f5822fSDaniel Ribeiro 	unsigned int old, new;
2041180c329dSTimur Tabi 	int ret;
2042db2a4165SFrank Mandarino 
20438a713da8SMark Brown 	if (codec->using_regmap) {
20448a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
20458a713da8SMark Brown 					       mask, value, &change);
20468a713da8SMark Brown 	} else {
2047180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
2048180c329dSTimur Tabi 		if (ret < 0)
2049180c329dSTimur Tabi 			return ret;
2050180c329dSTimur Tabi 
2051180c329dSTimur Tabi 		old = ret;
205278bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
2053db2a4165SFrank Mandarino 		change = old != new;
20548a713da8SMark Brown 		if (change)
2055180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
20568a713da8SMark Brown 	}
20578a713da8SMark Brown 
2058180c329dSTimur Tabi 	if (ret < 0)
2059180c329dSTimur Tabi 		return ret;
2060db2a4165SFrank Mandarino 
2061db2a4165SFrank Mandarino 	return change;
2062db2a4165SFrank Mandarino }
2063db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2064db2a4165SFrank Mandarino 
2065db2a4165SFrank Mandarino /**
20666c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
20676c508c62SEero Nurkkala  * @codec: audio codec
20686c508c62SEero Nurkkala  * @reg: codec register
20696c508c62SEero Nurkkala  * @mask: register mask
20706c508c62SEero Nurkkala  * @value: new value
20716c508c62SEero Nurkkala  *
20726c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
20736c508c62SEero Nurkkala  *
20746c508c62SEero Nurkkala  * Returns 1 for change else 0.
20756c508c62SEero Nurkkala  */
2076dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
20776c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
20786c508c62SEero Nurkkala 			       unsigned int value)
20796c508c62SEero Nurkkala {
20806c508c62SEero Nurkkala 	int change;
20816c508c62SEero Nurkkala 
20826c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
20836c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
20846c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
20856c508c62SEero Nurkkala 
20866c508c62SEero Nurkkala 	return change;
20876c508c62SEero Nurkkala }
2088dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
20896c508c62SEero Nurkkala 
20906c508c62SEero Nurkkala /**
2091db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2092db2a4165SFrank Mandarino  * @codec: audio codec
2093db2a4165SFrank Mandarino  * @reg: codec register
2094db2a4165SFrank Mandarino  * @mask: register mask
2095db2a4165SFrank Mandarino  * @value: new value
2096db2a4165SFrank Mandarino  *
2097db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2098db2a4165SFrank Mandarino  * different from the old value.
2099db2a4165SFrank Mandarino  *
2100db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2101db2a4165SFrank Mandarino  */
2102db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
210346f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2104db2a4165SFrank Mandarino {
2105db2a4165SFrank Mandarino 	int change;
210646f5822fSDaniel Ribeiro 	unsigned int old, new;
2107db2a4165SFrank Mandarino 
2108db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2109db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2110db2a4165SFrank Mandarino 	change = old != new;
2111db2a4165SFrank Mandarino 
2112db2a4165SFrank Mandarino 	return change;
2113db2a4165SFrank Mandarino }
2114db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2115db2a4165SFrank Mandarino 
2116db2a4165SFrank Mandarino /**
2117db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2118db2a4165SFrank Mandarino  * @substream: the pcm substream
2119db2a4165SFrank Mandarino  * @hw: the hardware parameters
2120db2a4165SFrank Mandarino  *
2121db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2122db2a4165SFrank Mandarino  */
2123db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2124db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2125db2a4165SFrank Mandarino {
2126db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2127db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2128db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2129db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2130db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2131db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2132db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2133db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2134db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2135db2a4165SFrank Mandarino 	return 0;
2136db2a4165SFrank Mandarino }
2137db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2138db2a4165SFrank Mandarino 
2139db2a4165SFrank Mandarino /**
2140db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2141db2a4165SFrank Mandarino  * @_template: control template
2142db2a4165SFrank Mandarino  * @data: control private data
2143ac11a2b3SMark Brown  * @long_name: control long name
2144efb7ac3fSMark Brown  * @prefix: control name prefix
2145db2a4165SFrank Mandarino  *
2146db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2147db2a4165SFrank Mandarino  *
2148db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2149db2a4165SFrank Mandarino  */
2150db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21513056557fSMark Brown 				  void *data, const char *long_name,
2152efb7ac3fSMark Brown 				  const char *prefix)
2153db2a4165SFrank Mandarino {
2154db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2155efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2156efb7ac3fSMark Brown 	char *name = NULL;
2157efb7ac3fSMark Brown 	int name_len;
2158db2a4165SFrank Mandarino 
2159db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2160db2a4165SFrank Mandarino 	template.index = 0;
2161db2a4165SFrank Mandarino 
2162efb7ac3fSMark Brown 	if (!long_name)
2163efb7ac3fSMark Brown 		long_name = template.name;
2164efb7ac3fSMark Brown 
2165efb7ac3fSMark Brown 	if (prefix) {
2166efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
216757cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2168efb7ac3fSMark Brown 		if (!name)
2169efb7ac3fSMark Brown 			return NULL;
2170efb7ac3fSMark Brown 
2171efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2172efb7ac3fSMark Brown 
2173efb7ac3fSMark Brown 		template.name = name;
2174efb7ac3fSMark Brown 	} else {
2175efb7ac3fSMark Brown 		template.name = long_name;
2176efb7ac3fSMark Brown 	}
2177efb7ac3fSMark Brown 
2178efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2179efb7ac3fSMark Brown 
2180efb7ac3fSMark Brown 	kfree(name);
2181efb7ac3fSMark Brown 
2182efb7ac3fSMark Brown 	return kcontrol;
2183db2a4165SFrank Mandarino }
2184db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2185db2a4165SFrank Mandarino 
2186022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2187022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2188022658beSLiam Girdwood 	const char *prefix, void *data)
2189022658beSLiam Girdwood {
2190022658beSLiam Girdwood 	int err, i;
2191022658beSLiam Girdwood 
2192022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2193022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2194022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2195022658beSLiam Girdwood 						     control->name, prefix));
2196022658beSLiam Girdwood 		if (err < 0) {
2197022658beSLiam Girdwood 			dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2198022658beSLiam Girdwood 			return err;
2199022658beSLiam Girdwood 		}
2200022658beSLiam Girdwood 	}
2201022658beSLiam Girdwood 
2202022658beSLiam Girdwood 	return 0;
2203022658beSLiam Girdwood }
2204022658beSLiam Girdwood 
2205db2a4165SFrank Mandarino /**
2206022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2207022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
22083e8e1952SIan Molton  * duplicating this code.
22093e8e1952SIan Molton  *
22103e8e1952SIan Molton  * @codec: codec to add controls to
22113e8e1952SIan Molton  * @controls: array of controls to add
22123e8e1952SIan Molton  * @num_controls: number of elements in the array
22133e8e1952SIan Molton  *
22143e8e1952SIan Molton  * Return 0 for success, else error.
22153e8e1952SIan Molton  */
2216022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
22173e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
22183e8e1952SIan Molton {
2219f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
22203e8e1952SIan Molton 
2221022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2222022658beSLiam Girdwood 			codec->name_prefix, codec);
22233e8e1952SIan Molton }
2224022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
22253e8e1952SIan Molton 
22263e8e1952SIan Molton /**
2227a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2228022658beSLiam Girdwood  * Convenience function to add a list of controls.
2229a491a5c8SLiam Girdwood  *
2230a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2231a491a5c8SLiam Girdwood  * @controls: array of controls to add
2232a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2233a491a5c8SLiam Girdwood  *
2234a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2235a491a5c8SLiam Girdwood  */
2236a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2237a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2238a491a5c8SLiam Girdwood {
2239a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2240a491a5c8SLiam Girdwood 
2241022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2242022658beSLiam Girdwood 			NULL, platform);
2243a491a5c8SLiam Girdwood }
2244a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2245a491a5c8SLiam Girdwood 
2246a491a5c8SLiam Girdwood /**
2247022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2248022658beSLiam Girdwood  * Convenience function to add a list of controls.
2249022658beSLiam Girdwood  *
2250022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2251022658beSLiam Girdwood  * @controls: array of controls to add
2252022658beSLiam Girdwood  * @num_controls: number of elements in the array
2253022658beSLiam Girdwood  *
2254022658beSLiam Girdwood  * Return 0 for success, else error.
2255022658beSLiam Girdwood  */
2256022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2257022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2258022658beSLiam Girdwood {
2259022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2260022658beSLiam Girdwood 
2261022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2262022658beSLiam Girdwood 			NULL, soc_card);
2263022658beSLiam Girdwood }
2264022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2265022658beSLiam Girdwood 
2266022658beSLiam Girdwood /**
2267022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2268022658beSLiam Girdwood  * Convienience function to add a list of controls.
2269022658beSLiam Girdwood  *
2270022658beSLiam Girdwood  * @dai: DAI to add controls to
2271022658beSLiam Girdwood  * @controls: array of controls to add
2272022658beSLiam Girdwood  * @num_controls: number of elements in the array
2273022658beSLiam Girdwood  *
2274022658beSLiam Girdwood  * Return 0 for success, else error.
2275022658beSLiam Girdwood  */
2276022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2277022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2278022658beSLiam Girdwood {
2279022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2280022658beSLiam Girdwood 
2281022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2282022658beSLiam Girdwood 			NULL, dai);
2283022658beSLiam Girdwood }
2284022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2285022658beSLiam Girdwood 
2286022658beSLiam Girdwood /**
2287db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2288db2a4165SFrank Mandarino  * @kcontrol: mixer control
2289db2a4165SFrank Mandarino  * @uinfo: control element information
2290db2a4165SFrank Mandarino  *
2291db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2292db2a4165SFrank Mandarino  * mixer control.
2293db2a4165SFrank Mandarino  *
2294db2a4165SFrank Mandarino  * Returns 0 for success.
2295db2a4165SFrank Mandarino  */
2296db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2297db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2298db2a4165SFrank Mandarino {
2299db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2300db2a4165SFrank Mandarino 
2301db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2302db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2303f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2304db2a4165SFrank Mandarino 
2305f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2306f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2307db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2308db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2309db2a4165SFrank Mandarino 	return 0;
2310db2a4165SFrank Mandarino }
2311db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2312db2a4165SFrank Mandarino 
2313db2a4165SFrank Mandarino /**
2314db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2315db2a4165SFrank Mandarino  * @kcontrol: mixer control
2316ac11a2b3SMark Brown  * @ucontrol: control element information
2317db2a4165SFrank Mandarino  *
2318db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2319db2a4165SFrank Mandarino  *
2320db2a4165SFrank Mandarino  * Returns 0 for success.
2321db2a4165SFrank Mandarino  */
2322db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2323db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2324db2a4165SFrank Mandarino {
2325db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2326db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
232746f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2328db2a4165SFrank Mandarino 
2329f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2330db2a4165SFrank Mandarino 		;
2331db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
23323ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
23333ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2334db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2335db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2336db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2337db2a4165SFrank Mandarino 
2338db2a4165SFrank Mandarino 	return 0;
2339db2a4165SFrank Mandarino }
2340db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2341db2a4165SFrank Mandarino 
2342db2a4165SFrank Mandarino /**
2343db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2344db2a4165SFrank Mandarino  * @kcontrol: mixer control
2345ac11a2b3SMark Brown  * @ucontrol: control element information
2346db2a4165SFrank Mandarino  *
2347db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2348db2a4165SFrank Mandarino  *
2349db2a4165SFrank Mandarino  * Returns 0 for success.
2350db2a4165SFrank Mandarino  */
2351db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2352db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2353db2a4165SFrank Mandarino {
2354db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2355db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
235646f5822fSDaniel Ribeiro 	unsigned int val;
235746f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2358db2a4165SFrank Mandarino 
2359f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2360db2a4165SFrank Mandarino 		;
2361f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2362db2a4165SFrank Mandarino 		return -EINVAL;
2363db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2364db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2365db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2366f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2367db2a4165SFrank Mandarino 			return -EINVAL;
2368db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2369db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2370db2a4165SFrank Mandarino 	}
2371db2a4165SFrank Mandarino 
23726c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2373db2a4165SFrank Mandarino }
2374db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2375db2a4165SFrank Mandarino 
2376db2a4165SFrank Mandarino /**
23772e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
23782e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23792e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23802e72f8e3SPeter Ujfalusi  *
23812e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
23822e72f8e3SPeter Ujfalusi  *
23832e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23842e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23852e72f8e3SPeter Ujfalusi  *
23862e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23872e72f8e3SPeter Ujfalusi  */
23882e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
23892e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23902e72f8e3SPeter Ujfalusi {
23912e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
239274155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
239346f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
23942e72f8e3SPeter Ujfalusi 
23952e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
23962e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
23972e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
23982e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
23992e72f8e3SPeter Ujfalusi 			break;
24002e72f8e3SPeter Ujfalusi 	}
24012e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
24022e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24032e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
24042e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
24052e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
24062e72f8e3SPeter Ujfalusi 				break;
24072e72f8e3SPeter Ujfalusi 		}
24082e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
24092e72f8e3SPeter Ujfalusi 	}
24102e72f8e3SPeter Ujfalusi 
24112e72f8e3SPeter Ujfalusi 	return 0;
24122e72f8e3SPeter Ujfalusi }
24132e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
24142e72f8e3SPeter Ujfalusi 
24152e72f8e3SPeter Ujfalusi /**
24162e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
24172e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
24182e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
24192e72f8e3SPeter Ujfalusi  *
24202e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
24212e72f8e3SPeter Ujfalusi  *
24222e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24232e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24242e72f8e3SPeter Ujfalusi  *
24252e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24262e72f8e3SPeter Ujfalusi  */
24272e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
24282e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
24292e72f8e3SPeter Ujfalusi {
24302e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
243174155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
243246f5822fSDaniel Ribeiro 	unsigned int val;
243346f5822fSDaniel Ribeiro 	unsigned int mask;
24342e72f8e3SPeter Ujfalusi 
24352e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
24362e72f8e3SPeter Ujfalusi 		return -EINVAL;
24372e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
24382e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
24392e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24402e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
24412e72f8e3SPeter Ujfalusi 			return -EINVAL;
24422e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
24432e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
24442e72f8e3SPeter Ujfalusi 	}
24452e72f8e3SPeter Ujfalusi 
24466c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
24472e72f8e3SPeter Ujfalusi }
24482e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
24492e72f8e3SPeter Ujfalusi 
24502e72f8e3SPeter Ujfalusi /**
2451db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2452db2a4165SFrank Mandarino  * @kcontrol: mixer control
2453db2a4165SFrank Mandarino  * @uinfo: control element information
2454db2a4165SFrank Mandarino  *
2455db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2456db2a4165SFrank Mandarino  * single mixer.
2457db2a4165SFrank Mandarino  *
2458db2a4165SFrank Mandarino  * Returns 0 for success.
2459db2a4165SFrank Mandarino  */
2460db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2461db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2462db2a4165SFrank Mandarino {
2463db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2464db2a4165SFrank Mandarino 
2465db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2466db2a4165SFrank Mandarino 	uinfo->count = 1;
2467f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2468db2a4165SFrank Mandarino 
2469f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2470f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2471db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2472db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2473db2a4165SFrank Mandarino 	return 0;
2474db2a4165SFrank Mandarino }
2475db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2476db2a4165SFrank Mandarino 
2477db2a4165SFrank Mandarino /**
2478db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2479db2a4165SFrank Mandarino  * @kcontrol: mixer control
2480db2a4165SFrank Mandarino  * @uinfo: control element information
2481db2a4165SFrank Mandarino  *
2482db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2483db2a4165SFrank Mandarino  *
2484db2a4165SFrank Mandarino  * Returns 0 for success.
2485db2a4165SFrank Mandarino  */
2486db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2487db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2488db2a4165SFrank Mandarino {
2489a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2490db2a4165SFrank Mandarino 
2491fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2492a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2493a7a4ac86SPhilipp Zabel 	else
2494a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2495a7a4ac86SPhilipp Zabel 
2496db2a4165SFrank Mandarino 	uinfo->count = 1;
2497db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2498a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2499db2a4165SFrank Mandarino 	return 0;
2500db2a4165SFrank Mandarino }
2501db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2502db2a4165SFrank Mandarino 
2503db2a4165SFrank Mandarino /**
2504db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2505db2a4165SFrank Mandarino  * @kcontrol: mixer control
2506db2a4165SFrank Mandarino  * @uinfo: control element information
2507db2a4165SFrank Mandarino  *
2508e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2509e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2510db2a4165SFrank Mandarino  *
2511db2a4165SFrank Mandarino  * Returns 0 for success.
2512db2a4165SFrank Mandarino  */
2513db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2514db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2515db2a4165SFrank Mandarino {
25164eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25174eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2518d11bb4a9SPeter Ujfalusi 	int platform_max;
2519db2a4165SFrank Mandarino 
2520d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2521d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2522d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2523d11bb4a9SPeter Ujfalusi 
2524d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2525a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2526a7a4ac86SPhilipp Zabel 	else
2527a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2528a7a4ac86SPhilipp Zabel 
2529e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2530db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2531d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2532db2a4165SFrank Mandarino 	return 0;
2533db2a4165SFrank Mandarino }
2534db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2535db2a4165SFrank Mandarino 
2536db2a4165SFrank Mandarino /**
2537db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2538db2a4165SFrank Mandarino  * @kcontrol: mixer control
2539ac11a2b3SMark Brown  * @ucontrol: control element information
2540db2a4165SFrank Mandarino  *
2541f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2542f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2543db2a4165SFrank Mandarino  *
2544db2a4165SFrank Mandarino  * Returns 0 for success.
2545db2a4165SFrank Mandarino  */
2546db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2547db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2548db2a4165SFrank Mandarino {
25494eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25504eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2551db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2552815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2553f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2554815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2555815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25564eaa9819SJon Smirl 	int max = mc->max;
2557815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2558815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2559db2a4165SFrank Mandarino 
2560db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2561db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2562f7915d99SPeter Ujfalusi 	if (invert)
2563db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2564a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2565f7915d99SPeter Ujfalusi 
2566f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2567f7915d99SPeter Ujfalusi 		if (reg == reg2)
2568f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2569f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2570f7915d99SPeter Ujfalusi 		else
2571f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2572f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2573f7915d99SPeter Ujfalusi 		if (invert)
2574db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2575a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2576db2a4165SFrank Mandarino 	}
2577db2a4165SFrank Mandarino 
2578db2a4165SFrank Mandarino 	return 0;
2579db2a4165SFrank Mandarino }
2580db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2581db2a4165SFrank Mandarino 
2582db2a4165SFrank Mandarino /**
2583db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2584db2a4165SFrank Mandarino  * @kcontrol: mixer control
2585ac11a2b3SMark Brown  * @ucontrol: control element information
2586db2a4165SFrank Mandarino  *
2587974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2588974815baSPeter Ujfalusi  * control that spans 2 registers.
2589db2a4165SFrank Mandarino  *
2590db2a4165SFrank Mandarino  * Returns 0 for success.
2591db2a4165SFrank Mandarino  */
2592db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2593db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2594db2a4165SFrank Mandarino {
25954eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25964eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2597db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2598815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2599974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2600815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2601815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26024eaa9819SJon Smirl 	int max = mc->max;
2603815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2604815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2605974815baSPeter Ujfalusi 	int err;
2606974815baSPeter Ujfalusi 	bool type_2r = 0;
2607974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2608974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2609db2a4165SFrank Mandarino 
2610db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2611db2a4165SFrank Mandarino 	if (invert)
2612a7a4ac86SPhilipp Zabel 		val = max - val;
2613db2a4165SFrank Mandarino 	val_mask = mask << shift;
2614db2a4165SFrank Mandarino 	val = val << shift;
2615974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2616db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2617db2a4165SFrank Mandarino 		if (invert)
2618a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2619974815baSPeter Ujfalusi 		if (reg == reg2) {
2620db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2621db2a4165SFrank Mandarino 			val |= val2 << rshift;
2622974815baSPeter Ujfalusi 		} else {
2623db2a4165SFrank Mandarino 			val2 = val2 << shift;
2624974815baSPeter Ujfalusi 			type_2r = 1;
2625974815baSPeter Ujfalusi 		}
2626974815baSPeter Ujfalusi 	}
26276c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
26283ff3f64bSMark Brown 	if (err < 0)
2629db2a4165SFrank Mandarino 		return err;
2630db2a4165SFrank Mandarino 
2631974815baSPeter Ujfalusi 	if (type_2r)
26326c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2633974815baSPeter Ujfalusi 
2634db2a4165SFrank Mandarino 	return err;
2635db2a4165SFrank Mandarino }
2636974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2637db2a4165SFrank Mandarino 
2638e13ac2e9SMark Brown /**
26391d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
26401d99f243SBrian Austin  * @kcontrol: mixer control
26411d99f243SBrian Austin  * @ucontrol: control element information
26421d99f243SBrian Austin  *
26431d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
26441d99f243SBrian Austin  * control that spans 2 registers.
26451d99f243SBrian Austin  *
26461d99f243SBrian Austin  * Returns 0 for success.
26471d99f243SBrian Austin  */
26481d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
26491d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
26501d99f243SBrian Austin {
26511d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
26521d99f243SBrian Austin 	struct soc_mixer_control *mc =
26531d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
26541d99f243SBrian Austin 
26551d99f243SBrian Austin 	unsigned int reg = mc->reg;
26561d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
26571d99f243SBrian Austin 	unsigned int shift = mc->shift;
26581d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
26591d99f243SBrian Austin 	int max = mc->max;
26601d99f243SBrian Austin 	int min = mc->min;
26611d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
26621d99f243SBrian Austin 
26631d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
26641d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
26651d99f243SBrian Austin 
26661d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
26671d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
26681d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
26691d99f243SBrian Austin 
26701d99f243SBrian Austin 	return 0;
26711d99f243SBrian Austin }
26721d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
26731d99f243SBrian Austin 
26741d99f243SBrian Austin /**
26751d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
26761d99f243SBrian Austin  * @kcontrol: mixer control
26771d99f243SBrian Austin  * @uinfo: control element information
26781d99f243SBrian Austin  *
26791d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
26801d99f243SBrian Austin  *
26811d99f243SBrian Austin  * Returns 0 for success.
26821d99f243SBrian Austin  */
26831d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
26841d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
26851d99f243SBrian Austin {
26861d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
26871d99f243SBrian Austin 	struct soc_mixer_control *mc =
26881d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
26891d99f243SBrian Austin 
26901d99f243SBrian Austin 	unsigned int reg = mc->reg;
26911d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
26921d99f243SBrian Austin 	unsigned int shift = mc->shift;
26931d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
26941d99f243SBrian Austin 	int max = mc->max;
26951d99f243SBrian Austin 	int min = mc->min;
26961d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
269727f1d759SBrian Austin 	int err = 0;
26981d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
26991d99f243SBrian Austin 
27001d99f243SBrian Austin 	val_mask = mask << shift;
27011d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
27021d99f243SBrian Austin 	val = val << shift;
27031d99f243SBrian Austin 
27041d99f243SBrian Austin 	if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
27051d99f243SBrian Austin 			return err;
27061d99f243SBrian Austin 
27071d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
27081d99f243SBrian Austin 		val_mask = mask << rshift;
27091d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
27101d99f243SBrian Austin 		val2 = val2 << rshift;
27111d99f243SBrian Austin 
27121d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
27131d99f243SBrian Austin 			return err;
27141d99f243SBrian Austin 	}
27151d99f243SBrian Austin 	return 0;
27161d99f243SBrian Austin }
27171d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
27181d99f243SBrian Austin 
27191d99f243SBrian Austin /**
2720e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2721e13ac2e9SMark Brown  * @kcontrol: mixer control
2722e13ac2e9SMark Brown  * @uinfo: control element information
2723e13ac2e9SMark Brown  *
2724e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2725e13ac2e9SMark Brown  *
2726e13ac2e9SMark Brown  * Returns 0 for success.
2727e13ac2e9SMark Brown  */
2728e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2729e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2730e13ac2e9SMark Brown {
27314eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27324eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2733d11bb4a9SPeter Ujfalusi 	int platform_max;
27344eaa9819SJon Smirl 	int min = mc->min;
2735e13ac2e9SMark Brown 
2736d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2737d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2738d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2739d11bb4a9SPeter Ujfalusi 
2740e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2741e13ac2e9SMark Brown 	uinfo->count = 2;
2742e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2743d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2744e13ac2e9SMark Brown 	return 0;
2745e13ac2e9SMark Brown }
2746e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2747e13ac2e9SMark Brown 
2748e13ac2e9SMark Brown /**
2749e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2750e13ac2e9SMark Brown  * @kcontrol: mixer control
2751ac11a2b3SMark Brown  * @ucontrol: control element information
2752e13ac2e9SMark Brown  *
2753e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2754e13ac2e9SMark Brown  *
2755e13ac2e9SMark Brown  * Returns 0 for success.
2756e13ac2e9SMark Brown  */
2757e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2758e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2759e13ac2e9SMark Brown {
27604eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27614eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2762e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2763815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27644eaa9819SJon Smirl 	int min = mc->min;
2765e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2766e13ac2e9SMark Brown 
2767e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2768e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2769e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2770e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2771e13ac2e9SMark Brown 	return 0;
2772e13ac2e9SMark Brown }
2773e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2774e13ac2e9SMark Brown 
2775e13ac2e9SMark Brown /**
2776e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2777e13ac2e9SMark Brown  * @kcontrol: mixer control
2778ac11a2b3SMark Brown  * @ucontrol: control element information
2779e13ac2e9SMark Brown  *
2780e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2781e13ac2e9SMark Brown  *
2782e13ac2e9SMark Brown  * Returns 0 for success.
2783e13ac2e9SMark Brown  */
2784e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2785e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2786e13ac2e9SMark Brown {
27874eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27884eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2789e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2790815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27914eaa9819SJon Smirl 	int min = mc->min;
279246f5822fSDaniel Ribeiro 	unsigned int val;
2793e13ac2e9SMark Brown 
2794e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2795e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2796e13ac2e9SMark Brown 
27976c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2798e13ac2e9SMark Brown }
2799e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2800e13ac2e9SMark Brown 
28018c6529dbSLiam Girdwood /**
28026c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
28036c9d8cf6SAdam Thomson  * @kcontrol: mixer control
28046c9d8cf6SAdam Thomson  * @uinfo: control element information
28056c9d8cf6SAdam Thomson  *
28066c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
28076c9d8cf6SAdam Thomson  * mixer control.
28086c9d8cf6SAdam Thomson  *
28096c9d8cf6SAdam Thomson  * returns 0 for success.
28106c9d8cf6SAdam Thomson  */
28116c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
28126c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
28136c9d8cf6SAdam Thomson {
28146c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
28156c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
28166c9d8cf6SAdam Thomson 	int platform_max;
28176c9d8cf6SAdam Thomson 	int min = mc->min;
28186c9d8cf6SAdam Thomson 
28196c9d8cf6SAdam Thomson 	if (!mc->platform_max)
28206c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
28216c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
28226c9d8cf6SAdam Thomson 
28236c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
28246c9d8cf6SAdam Thomson 	uinfo->count = 1;
28256c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
28266c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
28276c9d8cf6SAdam Thomson 
28286c9d8cf6SAdam Thomson 	return 0;
28296c9d8cf6SAdam Thomson }
28306c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
28316c9d8cf6SAdam Thomson 
28326c9d8cf6SAdam Thomson /**
28336c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
28346c9d8cf6SAdam Thomson  * @kcontrol: mixer control
28356c9d8cf6SAdam Thomson  * @ucontrol: control element information
28366c9d8cf6SAdam Thomson  *
28376c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
28386c9d8cf6SAdam Thomson  *
28396c9d8cf6SAdam Thomson  * Returns 0 for success.
28406c9d8cf6SAdam Thomson  */
28416c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
28426c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
28436c9d8cf6SAdam Thomson {
28446c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
28456c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
28466c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
28476c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
28486c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
28496c9d8cf6SAdam Thomson 	int min = mc->min;
28506c9d8cf6SAdam Thomson 	int max = mc->max;
28516c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
28526c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
28536c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
28546c9d8cf6SAdam Thomson 
28556c9d8cf6SAdam Thomson 	val = ((ucontrol->value.integer.value[0] + min) & mask);
28566c9d8cf6SAdam Thomson 	if (invert)
28576c9d8cf6SAdam Thomson 		val = max - val;
28586c9d8cf6SAdam Thomson 	val_mask = mask << shift;
28596c9d8cf6SAdam Thomson 	val = val << shift;
28606c9d8cf6SAdam Thomson 
28616c9d8cf6SAdam Thomson 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
28626c9d8cf6SAdam Thomson }
28636c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
28646c9d8cf6SAdam Thomson 
28656c9d8cf6SAdam Thomson /**
28666c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
28676c9d8cf6SAdam Thomson  * @kcontrol: mixer control
28686c9d8cf6SAdam Thomson  * @ucontrol: control element information
28696c9d8cf6SAdam Thomson  *
28706c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
28716c9d8cf6SAdam Thomson  *
28726c9d8cf6SAdam Thomson  * Returns 0 for success.
28736c9d8cf6SAdam Thomson  */
28746c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
28756c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
28766c9d8cf6SAdam Thomson {
28776c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
28786c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
28796c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
28806c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
28816c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
28826c9d8cf6SAdam Thomson 	int min = mc->min;
28836c9d8cf6SAdam Thomson 	int max = mc->max;
28846c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
28856c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
28866c9d8cf6SAdam Thomson 
28876c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
28886c9d8cf6SAdam Thomson 		(snd_soc_read(codec, reg) >> shift) & mask;
28896c9d8cf6SAdam Thomson 	if (invert)
28906c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
28916c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
28926c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
28936c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] - min;
28946c9d8cf6SAdam Thomson 
28956c9d8cf6SAdam Thomson 	return 0;
28966c9d8cf6SAdam Thomson }
28976c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
28986c9d8cf6SAdam Thomson 
28996c9d8cf6SAdam Thomson /**
2900637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2901637d3847SPeter Ujfalusi  *
2902637d3847SPeter Ujfalusi  * @codec: where to look for the control
2903637d3847SPeter Ujfalusi  * @name: Name of the control
2904637d3847SPeter Ujfalusi  * @max: new maximum limit
2905637d3847SPeter Ujfalusi  *
2906637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2907637d3847SPeter Ujfalusi  */
2908637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2909637d3847SPeter Ujfalusi 	const char *name, int max)
2910637d3847SPeter Ujfalusi {
2911f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2912637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2913637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2914637d3847SPeter Ujfalusi 	int found = 0;
2915637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2916637d3847SPeter Ujfalusi 
2917637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2918637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2919637d3847SPeter Ujfalusi 		return -EINVAL;
2920637d3847SPeter Ujfalusi 
2921637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2922637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2923637d3847SPeter Ujfalusi 			found = 1;
2924637d3847SPeter Ujfalusi 			break;
2925637d3847SPeter Ujfalusi 		}
2926637d3847SPeter Ujfalusi 	}
2927637d3847SPeter Ujfalusi 	if (found) {
2928637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2929637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2930d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2931637d3847SPeter Ujfalusi 			ret = 0;
2932637d3847SPeter Ujfalusi 		}
2933637d3847SPeter Ujfalusi 	}
2934637d3847SPeter Ujfalusi 	return ret;
2935637d3847SPeter Ujfalusi }
2936637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2937637d3847SPeter Ujfalusi 
293871d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
293971d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
294071d08516SMark Brown {
294171d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
294271d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
294371d08516SMark Brown 
294471d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
294571d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
294671d08516SMark Brown 
294771d08516SMark Brown 	return 0;
294871d08516SMark Brown }
294971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
295071d08516SMark Brown 
295171d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
295271d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
295371d08516SMark Brown {
295471d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
295571d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
295671d08516SMark Brown 	int ret;
295771d08516SMark Brown 
295871d08516SMark Brown 	if (codec->using_regmap)
295971d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
296071d08516SMark Brown 				      ucontrol->value.bytes.data,
296171d08516SMark Brown 				      params->num_regs * codec->val_bytes);
296271d08516SMark Brown 	else
296371d08516SMark Brown 		ret = -EINVAL;
296471d08516SMark Brown 
2965f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
2966f831b055SMark Brown 	if (ret == 0 && params->mask) {
2967f831b055SMark Brown 		switch (codec->val_bytes) {
2968f831b055SMark Brown 		case 1:
2969f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
2970f831b055SMark Brown 			break;
2971f831b055SMark Brown 		case 2:
2972f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
2973f831b055SMark Brown 				&= ~params->mask;
2974f831b055SMark Brown 			break;
2975f831b055SMark Brown 		case 4:
2976f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
2977f831b055SMark Brown 				&= ~params->mask;
2978f831b055SMark Brown 			break;
2979f831b055SMark Brown 		default:
2980f831b055SMark Brown 			return -EINVAL;
2981f831b055SMark Brown 		}
2982f831b055SMark Brown 	}
2983f831b055SMark Brown 
298471d08516SMark Brown 	return ret;
298571d08516SMark Brown }
298671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
298771d08516SMark Brown 
298871d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
298971d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
299071d08516SMark Brown {
299171d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
299271d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2993f831b055SMark Brown 	int ret, len;
2994f831b055SMark Brown 	unsigned int val;
2995f831b055SMark Brown 	void *data;
299671d08516SMark Brown 
2997f831b055SMark Brown 	if (!codec->using_regmap)
2998f831b055SMark Brown 		return -EINVAL;
2999f831b055SMark Brown 
3000f831b055SMark Brown 	data = ucontrol->value.bytes.data;
3001f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
3002f831b055SMark Brown 
3003f831b055SMark Brown 	/*
3004f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3005f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3006f831b055SMark Brown 	 * copy.
3007f831b055SMark Brown 	 */
3008f831b055SMark Brown 	if (params->mask) {
3009f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
3010f831b055SMark Brown 		if (ret != 0)
3011f831b055SMark Brown 			return ret;
3012f831b055SMark Brown 
3013f831b055SMark Brown 		val &= params->mask;
3014f831b055SMark Brown 
3015f831b055SMark Brown 		data = kmemdup(data, len, GFP_KERNEL);
3016f831b055SMark Brown 		if (!data)
3017f831b055SMark Brown 			return -ENOMEM;
3018f831b055SMark Brown 
3019f831b055SMark Brown 		switch (codec->val_bytes) {
3020f831b055SMark Brown 		case 1:
3021f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3022f831b055SMark Brown 			((u8 *)data)[0] |= val;
3023f831b055SMark Brown 			break;
3024f831b055SMark Brown 		case 2:
3025f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3026f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
3027f831b055SMark Brown 			break;
3028f831b055SMark Brown 		case 4:
3029f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3030f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
3031f831b055SMark Brown 			break;
3032f831b055SMark Brown 		default:
3033f831b055SMark Brown 			return -EINVAL;
3034f831b055SMark Brown 		}
3035f831b055SMark Brown 	}
3036f831b055SMark Brown 
303771d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
3038f831b055SMark Brown 			       data, len);
3039f831b055SMark Brown 
3040f831b055SMark Brown 	if (params->mask)
3041f831b055SMark Brown 		kfree(data);
304271d08516SMark Brown 
304371d08516SMark Brown 	return ret;
304471d08516SMark Brown }
304571d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
304671d08516SMark Brown 
3047b6f4bb38Sapatard@mandriva.com /**
30484183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
30494183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
30504183eed2SKristoffer KARLSSON  * @uinfo: control element information
30514183eed2SKristoffer KARLSSON  *
30524183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
30534183eed2SKristoffer KARLSSON  * span multiple codec registers which together
30544183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
30554183eed2SKristoffer KARLSSON  *
30564183eed2SKristoffer KARLSSON  * Returns 0 for success.
30574183eed2SKristoffer KARLSSON  */
30584183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
30594183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
30604183eed2SKristoffer KARLSSON {
30614183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
30624183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
30634183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
30644183eed2SKristoffer KARLSSON 	uinfo->count = 1;
30654183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
30664183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
30674183eed2SKristoffer KARLSSON 
30684183eed2SKristoffer KARLSSON 	return 0;
30694183eed2SKristoffer KARLSSON }
30704183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
30714183eed2SKristoffer KARLSSON 
30724183eed2SKristoffer KARLSSON /**
30734183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
30744183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
30754183eed2SKristoffer KARLSSON  * @ucontrol: control element information
30764183eed2SKristoffer KARLSSON  *
30774183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
30784183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
30794183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
30804183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
30814183eed2SKristoffer KARLSSON  * across the multiple codec registers.
30824183eed2SKristoffer KARLSSON  *
30834183eed2SKristoffer KARLSSON  * Returns 0 for success.
30844183eed2SKristoffer KARLSSON  */
30854183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
30864183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
30874183eed2SKristoffer KARLSSON {
30884183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
30894183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
30904183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
30914183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
30924183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
30934183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
30944183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
30954183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
30964183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
30974183eed2SKristoffer KARLSSON 	long min = mc->min;
30984183eed2SKristoffer KARLSSON 	long max = mc->max;
30994183eed2SKristoffer KARLSSON 	long val = 0;
31004183eed2SKristoffer KARLSSON 	unsigned long regval;
31014183eed2SKristoffer KARLSSON 	unsigned int i;
31024183eed2SKristoffer KARLSSON 
31034183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
31044183eed2SKristoffer KARLSSON 		regval = snd_soc_read(codec, regbase+i) & regwmask;
31054183eed2SKristoffer KARLSSON 		val |= regval << (regwshift*(regcount-i-1));
31064183eed2SKristoffer KARLSSON 	}
31074183eed2SKristoffer KARLSSON 	val &= mask;
31084183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
31094183eed2SKristoffer KARLSSON 		val |= ~mask;
31104183eed2SKristoffer KARLSSON 	if (invert)
31114183eed2SKristoffer KARLSSON 		val = max - val;
31124183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
31134183eed2SKristoffer KARLSSON 
31144183eed2SKristoffer KARLSSON 	return 0;
31154183eed2SKristoffer KARLSSON }
31164183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
31174183eed2SKristoffer KARLSSON 
31184183eed2SKristoffer KARLSSON /**
31194183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
31204183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
31214183eed2SKristoffer KARLSSON  * @ucontrol: control element information
31224183eed2SKristoffer KARLSSON  *
31234183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
31244183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
31254183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
31264183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
31274183eed2SKristoffer KARLSSON  * across the multiple codec registers.
31284183eed2SKristoffer KARLSSON  *
31294183eed2SKristoffer KARLSSON  * Returns 0 for success.
31304183eed2SKristoffer KARLSSON  */
31314183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
31324183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
31334183eed2SKristoffer KARLSSON {
31344183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
31354183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
31364183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
31374183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
31384183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
31394183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
31404183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
31414183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
31424183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
31434183eed2SKristoffer KARLSSON 	long max = mc->max;
31444183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
31454183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
31464183eed2SKristoffer KARLSSON 	int err;
31474183eed2SKristoffer KARLSSON 
31484183eed2SKristoffer KARLSSON 	if (invert)
31494183eed2SKristoffer KARLSSON 		val = max - val;
31504183eed2SKristoffer KARLSSON 	val &= mask;
31514183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
31524183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
31534183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
31544183eed2SKristoffer KARLSSON 		err = snd_soc_update_bits_locked(codec, regbase+i,
31554183eed2SKristoffer KARLSSON 				regmask, regval);
31564183eed2SKristoffer KARLSSON 		if (err < 0)
31574183eed2SKristoffer KARLSSON 			return err;
31584183eed2SKristoffer KARLSSON 	}
31594183eed2SKristoffer KARLSSON 
31604183eed2SKristoffer KARLSSON 	return 0;
31614183eed2SKristoffer KARLSSON }
31624183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
31634183eed2SKristoffer KARLSSON 
31644183eed2SKristoffer KARLSSON /**
3165dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3166dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3167dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3168dd7b10b3SKristoffer KARLSSON  *
3169dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3170dd7b10b3SKristoffer KARLSSON  *
3171dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3172dd7b10b3SKristoffer KARLSSON  */
3173dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3174dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3175dd7b10b3SKristoffer KARLSSON {
3176dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3177dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3178dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3179dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3180dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3181dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3182dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3183dd7b10b3SKristoffer KARLSSON 	unsigned int val = snd_soc_read(codec, reg) & mask;
3184dd7b10b3SKristoffer KARLSSON 
3185dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3186dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3187dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3188dd7b10b3SKristoffer KARLSSON 
3189dd7b10b3SKristoffer KARLSSON 	return 0;
3190dd7b10b3SKristoffer KARLSSON }
3191dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3192dd7b10b3SKristoffer KARLSSON 
3193dd7b10b3SKristoffer KARLSSON /**
3194dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3195dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3196dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3197dd7b10b3SKristoffer KARLSSON  *
3198dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3199dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3200dd7b10b3SKristoffer KARLSSON  *
3201dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3202dd7b10b3SKristoffer KARLSSON  */
3203dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3204dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3205dd7b10b3SKristoffer KARLSSON {
3206dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3207dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3208dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3209dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3210dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3211dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3212dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3213dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3214dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3215dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3216dd7b10b3SKristoffer KARLSSON 	int err;
3217dd7b10b3SKristoffer KARLSSON 
3218dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3219dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3220dd7b10b3SKristoffer KARLSSON 		return err;
3221dd7b10b3SKristoffer KARLSSON 
3222dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3223dd7b10b3SKristoffer KARLSSON 	return err;
3224dd7b10b3SKristoffer KARLSSON }
3225dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3226dd7b10b3SKristoffer KARLSSON 
3227dd7b10b3SKristoffer KARLSSON /**
32288c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
32298c6529dbSLiam Girdwood  * @dai: DAI
32308c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
32318c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
32328c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
32338c6529dbSLiam Girdwood  *
32348c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
32358c6529dbSLiam Girdwood  */
32368c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
32378c6529dbSLiam Girdwood 	unsigned int freq, int dir)
32388c6529dbSLiam Girdwood {
3239f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3240f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3241ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3242da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3243ec4ee52aSMark Brown 						      freq, dir);
32448c6529dbSLiam Girdwood 	else
32458c6529dbSLiam Girdwood 		return -EINVAL;
32468c6529dbSLiam Girdwood }
32478c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
32488c6529dbSLiam Girdwood 
32498c6529dbSLiam Girdwood /**
3250ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3251ec4ee52aSMark Brown  * @codec: CODEC
3252ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3253da1c6ea6SMark Brown  * @source: Source for the clock
3254ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3255ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3256ec4ee52aSMark Brown  *
3257ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3258ec4ee52aSMark Brown  */
3259ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3260da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3261ec4ee52aSMark Brown {
3262ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3263da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3264da1c6ea6SMark Brown 						 freq, dir);
3265ec4ee52aSMark Brown 	else
3266ec4ee52aSMark Brown 		return -EINVAL;
3267ec4ee52aSMark Brown }
3268ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3269ec4ee52aSMark Brown 
3270ec4ee52aSMark Brown /**
32718c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
32728c6529dbSLiam Girdwood  * @dai: DAI
3273ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
32748c6529dbSLiam Girdwood  * @div: new clock divisor.
32758c6529dbSLiam Girdwood  *
32768c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
32778c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
32788c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
32798c6529dbSLiam Girdwood  */
32808c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
32818c6529dbSLiam Girdwood 	int div_id, int div)
32828c6529dbSLiam Girdwood {
3283f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3284f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
32858c6529dbSLiam Girdwood 	else
32868c6529dbSLiam Girdwood 		return -EINVAL;
32878c6529dbSLiam Girdwood }
32888c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
32898c6529dbSLiam Girdwood 
32908c6529dbSLiam Girdwood /**
32918c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
32928c6529dbSLiam Girdwood  * @dai: DAI
32938c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
329485488037SMark Brown  * @source: DAI specific source for the PLL
32958c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
32968c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
32978c6529dbSLiam Girdwood  *
32988c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
32998c6529dbSLiam Girdwood  */
330085488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
330185488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
33028c6529dbSLiam Girdwood {
3303f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3304f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
330585488037SMark Brown 					 freq_in, freq_out);
3306ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3307ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3308ec4ee52aSMark Brown 						   freq_in, freq_out);
33098c6529dbSLiam Girdwood 	else
33108c6529dbSLiam Girdwood 		return -EINVAL;
33118c6529dbSLiam Girdwood }
33128c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
33138c6529dbSLiam Girdwood 
3314ec4ee52aSMark Brown /*
3315ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3316ec4ee52aSMark Brown  * @codec: CODEC
3317ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3318ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3319ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3320ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3321ec4ee52aSMark Brown  *
3322ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3323ec4ee52aSMark Brown  */
3324ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3325ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3326ec4ee52aSMark Brown {
3327ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3328ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3329ec4ee52aSMark Brown 					      freq_in, freq_out);
3330ec4ee52aSMark Brown 	else
3331ec4ee52aSMark Brown 		return -EINVAL;
3332ec4ee52aSMark Brown }
3333ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3334ec4ee52aSMark Brown 
33358c6529dbSLiam Girdwood /**
33368c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
33378c6529dbSLiam Girdwood  * @dai: DAI
33388c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
33398c6529dbSLiam Girdwood  *
33408c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
33418c6529dbSLiam Girdwood  */
33428c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
33438c6529dbSLiam Girdwood {
33445e4ba569SShawn Guo 	if (dai->driver == NULL)
33458c6529dbSLiam Girdwood 		return -EINVAL;
33465e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
33475e4ba569SShawn Guo 		return -ENOTSUPP;
33485e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
33498c6529dbSLiam Girdwood }
33508c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
33518c6529dbSLiam Girdwood 
33528c6529dbSLiam Girdwood /**
33538c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
33548c6529dbSLiam Girdwood  * @dai: DAI
3355a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3356a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
33578c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3358a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
33598c6529dbSLiam Girdwood  *
33608c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
33618c6529dbSLiam Girdwood  * specific.
33628c6529dbSLiam Girdwood  */
33638c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3364a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
33658c6529dbSLiam Girdwood {
3366f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3367f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3368a5479e38SDaniel Ribeiro 				slots, slot_width);
33698c6529dbSLiam Girdwood 	else
33708c6529dbSLiam Girdwood 		return -EINVAL;
33718c6529dbSLiam Girdwood }
33728c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
33738c6529dbSLiam Girdwood 
33748c6529dbSLiam Girdwood /**
3375472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3376472df3cbSBarry Song  * @dai: DAI
3377472df3cbSBarry Song  * @tx_num: how many TX channels
3378472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3379472df3cbSBarry Song  *           0~num-1 uses
3380472df3cbSBarry Song  * @rx_num: how many RX channels
3381472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3382472df3cbSBarry Song  *           0~num-1 uses
3383472df3cbSBarry Song  *
3384472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3385472df3cbSBarry Song  */
3386472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3387472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3388472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3389472df3cbSBarry Song {
3390f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3391f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3392472df3cbSBarry Song 			rx_num, rx_slot);
3393472df3cbSBarry Song 	else
3394472df3cbSBarry Song 		return -EINVAL;
3395472df3cbSBarry Song }
3396472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3397472df3cbSBarry Song 
3398472df3cbSBarry Song /**
33998c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
34008c6529dbSLiam Girdwood  * @dai: DAI
34018c6529dbSLiam Girdwood  * @tristate: tristate enable
34028c6529dbSLiam Girdwood  *
34038c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
34048c6529dbSLiam Girdwood  */
34058c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
34068c6529dbSLiam Girdwood {
3407f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3408f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
34098c6529dbSLiam Girdwood 	else
34108c6529dbSLiam Girdwood 		return -EINVAL;
34118c6529dbSLiam Girdwood }
34128c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
34138c6529dbSLiam Girdwood 
34148c6529dbSLiam Girdwood /**
34158c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
34168c6529dbSLiam Girdwood  * @dai: DAI
34178c6529dbSLiam Girdwood  * @mute: mute enable
34188c6529dbSLiam Girdwood  *
34198c6529dbSLiam Girdwood  * Mutes the DAI DAC.
34208c6529dbSLiam Girdwood  */
34218c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
34228c6529dbSLiam Girdwood {
3423f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3424f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
34258c6529dbSLiam Girdwood 	else
342604570c62SMark Brown 		return -ENOTSUPP;
34278c6529dbSLiam Girdwood }
34288c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
34298c6529dbSLiam Girdwood 
3430c5af3a2eSMark Brown /**
3431c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3432c5af3a2eSMark Brown  *
3433ac11a2b3SMark Brown  * @card: Card to register
3434c5af3a2eSMark Brown  *
3435c5af3a2eSMark Brown  */
343670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3437c5af3a2eSMark Brown {
3438b19e6e7bSMark Brown 	int i, ret;
3439f0fba2adSLiam Girdwood 
3440c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3441c5af3a2eSMark Brown 		return -EINVAL;
3442c5af3a2eSMark Brown 
34435a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
34445a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
34455a504963SStephen Warren 
34465a504963SStephen Warren 		/*
34475a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
34485a504963SStephen Warren 		 * not both or neither.
34495a504963SStephen Warren 		 */
34505a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
34515a504963SStephen Warren 			dev_err(card->dev,
34523b09bb82SLiam Girdwood 				"Neither/both codec name/of_node are set for %s\n",
34533b09bb82SLiam Girdwood 				link->name);
34545a504963SStephen Warren 			return -EINVAL;
34555a504963SStephen Warren 		}
3456bc92657aSStephen Warren 		/* Codec DAI name must be specified */
3457bc92657aSStephen Warren 		if (!link->codec_dai_name) {
3458bc92657aSStephen Warren 			dev_err(card->dev, "codec_dai_name not set for %s\n",
3459bc92657aSStephen Warren 				link->name);
3460bc92657aSStephen Warren 			return -EINVAL;
3461bc92657aSStephen Warren 		}
34625a504963SStephen Warren 
34635a504963SStephen Warren 		/*
34645a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
34655a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
34665a504963SStephen Warren 		 */
34675a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
34685a504963SStephen Warren 			dev_err(card->dev,
34693b09bb82SLiam Girdwood 				"Both platform name/of_node are set for %s\n", link->name);
34705a504963SStephen Warren 			return -EINVAL;
34715a504963SStephen Warren 		}
34725a504963SStephen Warren 
34735a504963SStephen Warren 		/*
3474bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3475bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3476bc92657aSStephen Warren 		 * name alone..
34775a504963SStephen Warren 		 */
3478bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
34795a504963SStephen Warren 			dev_err(card->dev,
3480bc92657aSStephen Warren 				"Neither/both cpu name/of_node are set for %s\n",
3481bc92657aSStephen Warren 				link->name);
3482bc92657aSStephen Warren 			return -EINVAL;
3483bc92657aSStephen Warren 		}
3484bc92657aSStephen Warren 		/*
3485bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3486bc92657aSStephen Warren 		 * specified
3487bc92657aSStephen Warren 		 */
3488bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3489bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
3490bc92657aSStephen Warren 			dev_err(card->dev,
3491bc92657aSStephen Warren 				"Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
34923b09bb82SLiam Girdwood 				link->name);
34935a504963SStephen Warren 			return -EINVAL;
34945a504963SStephen Warren 		}
34955a504963SStephen Warren 	}
34965a504963SStephen Warren 
3497ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3498ed77cc12SMark Brown 
3499111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3500111c6419SStephen Warren 
3501150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3502150dd2f8SVinod Koul 
3503181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3504181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
35052eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3506f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3507f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3508f0fba2adSLiam Girdwood 		return -ENOMEM;
3509a7dbb603SLiam Girdwood 	card->num_rtd = 0;
35102eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3511f0fba2adSLiam Girdwood 
3512f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3513f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3514f0fba2adSLiam Girdwood 
3515c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3516db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3517c5af3a2eSMark Brown 	card->instantiated = 0;
3518f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3519a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3520c5af3a2eSMark Brown 
3521b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3522b19e6e7bSMark Brown 	if (ret != 0)
3523b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3524c5af3a2eSMark Brown 
3525b19e6e7bSMark Brown 	return ret;
3526c5af3a2eSMark Brown }
352770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3528c5af3a2eSMark Brown 
3529c5af3a2eSMark Brown /**
3530c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3531c5af3a2eSMark Brown  *
3532ac11a2b3SMark Brown  * @card: Card to unregister
3533c5af3a2eSMark Brown  *
3534c5af3a2eSMark Brown  */
353570a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3536c5af3a2eSMark Brown {
3537b0e26485SVinod Koul 	if (card->instantiated)
3538b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3539c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3540c5af3a2eSMark Brown 
3541c5af3a2eSMark Brown 	return 0;
3542c5af3a2eSMark Brown }
354370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3544c5af3a2eSMark Brown 
3545f0fba2adSLiam Girdwood /*
3546f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3547f0fba2adSLiam Girdwood  * and sanitizing names.
3548f0fba2adSLiam Girdwood  */
35490b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3550f0fba2adSLiam Girdwood {
3551f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3552f0fba2adSLiam Girdwood 	int id1, id2;
3553f0fba2adSLiam Girdwood 
3554f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3555f0fba2adSLiam Girdwood 		return NULL;
3556f0fba2adSLiam Girdwood 
355758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3558f0fba2adSLiam Girdwood 
3559f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3560f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3561f0fba2adSLiam Girdwood 	if (found) {
3562f0fba2adSLiam Girdwood 		/* get ID */
3563f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3564f0fba2adSLiam Girdwood 
3565f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3566f0fba2adSLiam Girdwood 			if (*id == -1)
3567f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3568f0fba2adSLiam Girdwood 		}
3569f0fba2adSLiam Girdwood 
3570f0fba2adSLiam Girdwood 	} else {
3571f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3572f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3573f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3574f0fba2adSLiam Girdwood 
3575f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
357605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3577f0fba2adSLiam Girdwood 
3578f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3579f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
358058818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3581f0fba2adSLiam Girdwood 		} else
3582f0fba2adSLiam Girdwood 			*id = 0;
3583f0fba2adSLiam Girdwood 	}
3584f0fba2adSLiam Girdwood 
3585f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3586f0fba2adSLiam Girdwood }
3587f0fba2adSLiam Girdwood 
3588f0fba2adSLiam Girdwood /*
3589f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3590f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3591f0fba2adSLiam Girdwood  */
3592f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3593f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3594f0fba2adSLiam Girdwood {
3595f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
35960837fc62SFabio Estevam 		pr_err("asoc: error - multiple DAI %s registered with no name\n",
3597f0fba2adSLiam Girdwood 				dev_name(dev));
3598f0fba2adSLiam Girdwood 		return NULL;
3599f0fba2adSLiam Girdwood 	}
3600f0fba2adSLiam Girdwood 
3601f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3602f0fba2adSLiam Girdwood }
3603f0fba2adSLiam Girdwood 
36049115171aSMark Brown /**
36059115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
36069115171aSMark Brown  *
3607ac11a2b3SMark Brown  * @dai: DAI to register
36089115171aSMark Brown  */
3609f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3610f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
36119115171aSMark Brown {
3612054880feSMark Brown 	struct snd_soc_codec *codec;
3613f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
36149115171aSMark Brown 
3615f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
36169115171aSMark Brown 
3617f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3618f0fba2adSLiam Girdwood 	if (dai == NULL)
3619f0fba2adSLiam Girdwood 		return -ENOMEM;
36206335d055SEric Miao 
3621f0fba2adSLiam Girdwood 	/* create DAI component name */
3622f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3623f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3624f0fba2adSLiam Girdwood 		kfree(dai);
3625f0fba2adSLiam Girdwood 		return -ENOMEM;
3626f0fba2adSLiam Girdwood 	}
3627f0fba2adSLiam Girdwood 
3628f0fba2adSLiam Girdwood 	dai->dev = dev;
3629f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3630be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3631f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3632f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
36339115171aSMark Brown 
36349115171aSMark Brown 	mutex_lock(&client_mutex);
3635054880feSMark Brown 
3636054880feSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3637054880feSMark Brown 		if (codec->dev == dev) {
3638054880feSMark Brown 			dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3639054880feSMark Brown 				dai->name, codec->name);
3640054880feSMark Brown 			dai->codec = codec;
3641054880feSMark Brown 			break;
3642054880feSMark Brown 		}
3643054880feSMark Brown 	}
3644054880feSMark Brown 
36459115171aSMark Brown 	list_add(&dai->list, &dai_list);
3646054880feSMark Brown 
36479115171aSMark Brown 	mutex_unlock(&client_mutex);
36489115171aSMark Brown 
36499115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
36509115171aSMark Brown 
36519115171aSMark Brown 	return 0;
36529115171aSMark Brown }
36539115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
36549115171aSMark Brown 
36559115171aSMark Brown /**
36569115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
36579115171aSMark Brown  *
3658ac11a2b3SMark Brown  * @dai: DAI to unregister
36599115171aSMark Brown  */
3660f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
36619115171aSMark Brown {
3662f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3663f0fba2adSLiam Girdwood 
3664f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3665f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3666f0fba2adSLiam Girdwood 			goto found;
3667f0fba2adSLiam Girdwood 	}
3668f0fba2adSLiam Girdwood 	return;
3669f0fba2adSLiam Girdwood 
3670f0fba2adSLiam Girdwood found:
36719115171aSMark Brown 	mutex_lock(&client_mutex);
36729115171aSMark Brown 	list_del(&dai->list);
36739115171aSMark Brown 	mutex_unlock(&client_mutex);
36749115171aSMark Brown 
36759115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3676f0fba2adSLiam Girdwood 	kfree(dai->name);
3677f0fba2adSLiam Girdwood 	kfree(dai);
36789115171aSMark Brown }
36799115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
36809115171aSMark Brown 
36819115171aSMark Brown /**
36829115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
36839115171aSMark Brown  *
3684ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3685ac11a2b3SMark Brown  * @count: Number of DAIs
36869115171aSMark Brown  */
3687f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3688f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
36899115171aSMark Brown {
3690054880feSMark Brown 	struct snd_soc_codec *codec;
3691f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3692f0fba2adSLiam Girdwood 	int i, ret = 0;
3693f0fba2adSLiam Girdwood 
3694720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
36959115171aSMark Brown 
36969115171aSMark Brown 	for (i = 0; i < count; i++) {
3697f0fba2adSLiam Girdwood 
3698f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3699c46e0079SAxel Lin 		if (dai == NULL) {
3700c46e0079SAxel Lin 			ret = -ENOMEM;
3701c46e0079SAxel Lin 			goto err;
3702c46e0079SAxel Lin 		}
3703f0fba2adSLiam Girdwood 
3704f0fba2adSLiam Girdwood 		/* create DAI component name */
3705f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3706f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3707f0fba2adSLiam Girdwood 			kfree(dai);
3708f0fba2adSLiam Girdwood 			ret = -EINVAL;
37099115171aSMark Brown 			goto err;
37109115171aSMark Brown 		}
37119115171aSMark Brown 
3712f0fba2adSLiam Girdwood 		dai->dev = dev;
3713f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
37140f9141c9SMark Brown 		if (dai->driver->id)
37150f9141c9SMark Brown 			dai->id = dai->driver->id;
37160f9141c9SMark Brown 		else
37170f9141c9SMark Brown 			dai->id = i;
3718be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3719f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3720f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3721f0fba2adSLiam Girdwood 
3722f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3723054880feSMark Brown 
3724054880feSMark Brown 		list_for_each_entry(codec, &codec_list, list) {
3725054880feSMark Brown 			if (codec->dev == dev) {
3726054880feSMark Brown 				dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3727054880feSMark Brown 					dai->name, codec->name);
3728054880feSMark Brown 				dai->codec = codec;
3729054880feSMark Brown 				break;
3730054880feSMark Brown 			}
3731054880feSMark Brown 		}
3732054880feSMark Brown 
3733f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3734054880feSMark Brown 
3735f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3736f0fba2adSLiam Girdwood 
3737f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3738f0fba2adSLiam Girdwood 	}
3739f0fba2adSLiam Girdwood 
37409115171aSMark Brown 	return 0;
37419115171aSMark Brown 
37429115171aSMark Brown err:
37439115171aSMark Brown 	for (i--; i >= 0; i--)
3744f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
37459115171aSMark Brown 
37469115171aSMark Brown 	return ret;
37479115171aSMark Brown }
37489115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
37499115171aSMark Brown 
37509115171aSMark Brown /**
37519115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
37529115171aSMark Brown  *
3753ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3754ac11a2b3SMark Brown  * @count: Number of DAIs
37559115171aSMark Brown  */
3756f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
37579115171aSMark Brown {
37589115171aSMark Brown 	int i;
37599115171aSMark Brown 
37609115171aSMark Brown 	for (i = 0; i < count; i++)
3761f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
37629115171aSMark Brown }
37639115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
37649115171aSMark Brown 
376512a48a8cSMark Brown /**
376612a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
376712a48a8cSMark Brown  *
3768ac11a2b3SMark Brown  * @platform: platform to register
376912a48a8cSMark Brown  */
3770f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3771f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
377212a48a8cSMark Brown {
3773f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
377412a48a8cSMark Brown 
3775f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3776f0fba2adSLiam Girdwood 
3777f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3778f0fba2adSLiam Girdwood 	if (platform == NULL)
3779f0fba2adSLiam Girdwood 		return -ENOMEM;
3780f0fba2adSLiam Girdwood 
3781f0fba2adSLiam Girdwood 	/* create platform component name */
3782f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3783f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3784f0fba2adSLiam Girdwood 		kfree(platform);
3785f0fba2adSLiam Girdwood 		return -ENOMEM;
3786f0fba2adSLiam Girdwood 	}
3787f0fba2adSLiam Girdwood 
3788f0fba2adSLiam Girdwood 	platform->dev = dev;
3789f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3790b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3791b7950641SLiam Girdwood 	platform->dapm.platform = platform;
379264a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3793cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
379412a48a8cSMark Brown 
379512a48a8cSMark Brown 	mutex_lock(&client_mutex);
379612a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
379712a48a8cSMark Brown 	mutex_unlock(&client_mutex);
379812a48a8cSMark Brown 
379912a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
380012a48a8cSMark Brown 
380112a48a8cSMark Brown 	return 0;
380212a48a8cSMark Brown }
380312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
380412a48a8cSMark Brown 
380512a48a8cSMark Brown /**
380612a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
380712a48a8cSMark Brown  *
3808ac11a2b3SMark Brown  * @platform: platform to unregister
380912a48a8cSMark Brown  */
3810f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
381112a48a8cSMark Brown {
3812f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3813f0fba2adSLiam Girdwood 
3814f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3815f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3816f0fba2adSLiam Girdwood 			goto found;
3817f0fba2adSLiam Girdwood 	}
3818f0fba2adSLiam Girdwood 	return;
3819f0fba2adSLiam Girdwood 
3820f0fba2adSLiam Girdwood found:
382112a48a8cSMark Brown 	mutex_lock(&client_mutex);
382212a48a8cSMark Brown 	list_del(&platform->list);
382312a48a8cSMark Brown 	mutex_unlock(&client_mutex);
382412a48a8cSMark Brown 
382512a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3826f0fba2adSLiam Girdwood 	kfree(platform->name);
3827f0fba2adSLiam Girdwood 	kfree(platform);
382812a48a8cSMark Brown }
382912a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
383012a48a8cSMark Brown 
3831151ab22cSMark Brown static u64 codec_format_map[] = {
3832151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3833151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3834151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3835151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3836151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3837151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3838151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3839151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3840151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3841151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3842151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3843151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3844151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3845151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3846151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3847151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3848151ab22cSMark Brown };
3849151ab22cSMark Brown 
3850151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3851151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3852151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3853151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3854151ab22cSMark Brown  */
3855151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3856151ab22cSMark Brown {
3857151ab22cSMark Brown 	int i;
3858151ab22cSMark Brown 
3859151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3860151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3861151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3862151ab22cSMark Brown }
3863151ab22cSMark Brown 
38640d0cf00aSMark Brown /**
38650d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
38660d0cf00aSMark Brown  *
3867ac11a2b3SMark Brown  * @codec: codec to register
38680d0cf00aSMark Brown  */
3869f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3870001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3871001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3872001ae4c0SMark Brown 			   int num_dai)
38730d0cf00aSMark Brown {
38743335ddcaSDimitris Papastamos 	size_t reg_size;
3875f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3876f0fba2adSLiam Girdwood 	int ret, i;
3877151ab22cSMark Brown 
3878f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
38790d0cf00aSMark Brown 
3880f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3881f0fba2adSLiam Girdwood 	if (codec == NULL)
3882f0fba2adSLiam Girdwood 		return -ENOMEM;
38830d0cf00aSMark Brown 
3884f0fba2adSLiam Girdwood 	/* create CODEC component name */
3885f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3886f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3887f0fba2adSLiam Girdwood 		kfree(codec);
3888f0fba2adSLiam Girdwood 		return -ENOMEM;
3889151ab22cSMark Brown 	}
3890151ab22cSMark Brown 
389123bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
389223bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
389323bbce34SDimitris Papastamos 	else
389423bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
389523bbce34SDimitris Papastamos 
3896c3acec26SMark Brown 	codec->write = codec_drv->write;
3897c3acec26SMark Brown 	codec->read = codec_drv->read;
38981500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
38991500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
39008020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
39015124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3902ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3903ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3904ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3905474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
390664a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3907f0fba2adSLiam Girdwood 	codec->dev = dev;
3908f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3909f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3910f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3911f0fba2adSLiam Girdwood 
39127a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
39137a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
39143335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3915aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
39163335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
39173335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
39183335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
39193335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
39203335ddcaSDimitris Papastamos 		 * the cache.
39213335ddcaSDimitris Papastamos 		 */
39222aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
39233335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
39243335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
39253335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
39263335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3927fdf0f54dSDimitris Papastamos 				goto fail;
39287a30a3dbSDimitris Papastamos 			}
39297a30a3dbSDimitris Papastamos 		}
39302aa86323SDimitris Papastamos 	}
39317a30a3dbSDimitris Papastamos 
39321500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
39331500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
39341500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
39351500b7b5SDimitris Papastamos 		if (!codec->readable_register)
39361500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
39378020454cSDimitris Papastamos 		if (!codec->writable_register)
39388020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
39391500b7b5SDimitris Papastamos 	}
39401500b7b5SDimitris Papastamos 
3941f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3942f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3943f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3944f0fba2adSLiam Girdwood 	}
3945f0fba2adSLiam Girdwood 
3946054880feSMark Brown 	mutex_lock(&client_mutex);
3947054880feSMark Brown 	list_add(&codec->list, &codec_list);
3948054880feSMark Brown 	mutex_unlock(&client_mutex);
3949054880feSMark Brown 
395026b01ccdSMark Brown 	/* register any DAIs */
395126b01ccdSMark Brown 	if (num_dai) {
3952f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3953f0fba2adSLiam Girdwood 		if (ret < 0)
3954054880feSMark Brown 			dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3955054880feSMark Brown 				ret);
395626b01ccdSMark Brown 	}
3957f0fba2adSLiam Girdwood 
39580d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
39590d0cf00aSMark Brown 	return 0;
3960f0fba2adSLiam Girdwood 
3961fdf0f54dSDimitris Papastamos fail:
39623335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
39633335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3964f0fba2adSLiam Girdwood 	kfree(codec->name);
3965f0fba2adSLiam Girdwood 	kfree(codec);
3966f0fba2adSLiam Girdwood 	return ret;
39670d0cf00aSMark Brown }
39680d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
39690d0cf00aSMark Brown 
39700d0cf00aSMark Brown /**
39710d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
39720d0cf00aSMark Brown  *
3973ac11a2b3SMark Brown  * @codec: codec to unregister
39740d0cf00aSMark Brown  */
3975f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
39760d0cf00aSMark Brown {
3977f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3978f0fba2adSLiam Girdwood 	int i;
3979f0fba2adSLiam Girdwood 
3980f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3981f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3982f0fba2adSLiam Girdwood 			goto found;
3983f0fba2adSLiam Girdwood 	}
3984f0fba2adSLiam Girdwood 	return;
3985f0fba2adSLiam Girdwood 
3986f0fba2adSLiam Girdwood found:
398726b01ccdSMark Brown 	if (codec->num_dai)
3988f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3989f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3990f0fba2adSLiam Girdwood 
39910d0cf00aSMark Brown 	mutex_lock(&client_mutex);
39920d0cf00aSMark Brown 	list_del(&codec->list);
39930d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
39940d0cf00aSMark Brown 
39950d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3996f0fba2adSLiam Girdwood 
39977a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
39983335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
39991aafcd4dSDimitris Papastamos 	kfree(codec->name);
4000f0fba2adSLiam Girdwood 	kfree(codec);
40010d0cf00aSMark Brown }
40020d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
40030d0cf00aSMark Brown 
4004bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4005bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4006bec4fa05SStephen Warren 			       const char *propname)
4007bec4fa05SStephen Warren {
4008bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
4009bec4fa05SStephen Warren 	int ret;
4010bec4fa05SStephen Warren 
4011bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4012bec4fa05SStephen Warren 	/*
4013bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4014bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4015bec4fa05SStephen Warren 	 * snd_soc_register_card.
4016bec4fa05SStephen Warren 	 */
4017bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4018bec4fa05SStephen Warren 		dev_err(card->dev,
4019bec4fa05SStephen Warren 			"Property '%s' could not be read: %d\n",
4020bec4fa05SStephen Warren 			propname, ret);
4021bec4fa05SStephen Warren 		return ret;
4022bec4fa05SStephen Warren 	}
4023bec4fa05SStephen Warren 
4024bec4fa05SStephen Warren 	return 0;
4025bec4fa05SStephen Warren }
4026bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4027bec4fa05SStephen Warren 
4028a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4029a4a54dd5SStephen Warren 				   const char *propname)
4030a4a54dd5SStephen Warren {
4031a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4032a4a54dd5SStephen Warren 	int num_routes;
4033a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4034a4a54dd5SStephen Warren 	int i, ret;
4035a4a54dd5SStephen Warren 
4036a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4037c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
4038a4a54dd5SStephen Warren 		dev_err(card->dev,
4039c34ce320SRichard Zhao 		     "Property '%s' does not exist or its length is not even\n",
4040a4a54dd5SStephen Warren 		     propname);
4041a4a54dd5SStephen Warren 		return -EINVAL;
4042a4a54dd5SStephen Warren 	}
4043a4a54dd5SStephen Warren 	num_routes /= 2;
4044a4a54dd5SStephen Warren 	if (!num_routes) {
4045a4a54dd5SStephen Warren 		dev_err(card->dev,
4046a4a54dd5SStephen Warren 			"Property '%s's length is zero\n",
4047a4a54dd5SStephen Warren 			propname);
4048a4a54dd5SStephen Warren 		return -EINVAL;
4049a4a54dd5SStephen Warren 	}
4050a4a54dd5SStephen Warren 
4051a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4052a4a54dd5SStephen Warren 			      GFP_KERNEL);
4053a4a54dd5SStephen Warren 	if (!routes) {
4054a4a54dd5SStephen Warren 		dev_err(card->dev,
4055a4a54dd5SStephen Warren 			"Could not allocate DAPM route table\n");
4056a4a54dd5SStephen Warren 		return -EINVAL;
4057a4a54dd5SStephen Warren 	}
4058a4a54dd5SStephen Warren 
4059a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4060a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4061a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4062a4a54dd5SStephen Warren 		if (ret) {
4063a4a54dd5SStephen Warren 			dev_err(card->dev,
4064a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
4065a4a54dd5SStephen Warren 				propname, 2 * i, ret);
4066a4a54dd5SStephen Warren 			return -EINVAL;
4067a4a54dd5SStephen Warren 		}
4068a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4069a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4070a4a54dd5SStephen Warren 		if (ret) {
4071a4a54dd5SStephen Warren 			dev_err(card->dev,
4072a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
4073a4a54dd5SStephen Warren 				propname, (2 * i) + 1, ret);
4074a4a54dd5SStephen Warren 			return -EINVAL;
4075a4a54dd5SStephen Warren 		}
4076a4a54dd5SStephen Warren 	}
4077a4a54dd5SStephen Warren 
4078a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4079a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4080a4a54dd5SStephen Warren 
4081a4a54dd5SStephen Warren 	return 0;
4082a4a54dd5SStephen Warren }
4083a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4084a4a54dd5SStephen Warren 
4085c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4086db2a4165SFrank Mandarino {
4087384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
40888a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
40898a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
40900837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
40918a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4092384c89e2SMark Brown 	}
4093c3c5a19aSMark Brown 
40948a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4095c3c5a19aSMark Brown 				 &codec_list_fops))
4096c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4097c3c5a19aSMark Brown 
40988a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4099f3208780SMark Brown 				 &dai_list_fops))
4100f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
410119c7ac27SMark Brown 
41028a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
410319c7ac27SMark Brown 				 &platform_list_fops))
410419c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4105384c89e2SMark Brown #endif
4106384c89e2SMark Brown 
4107fb257897SMark Brown 	snd_soc_util_init();
4108fb257897SMark Brown 
4109db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4110db2a4165SFrank Mandarino }
41114abe8e16SMark Brown module_init(snd_soc_init);
4112db2a4165SFrank Mandarino 
41137d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4114db2a4165SFrank Mandarino {
4115fb257897SMark Brown 	snd_soc_util_exit();
4116fb257897SMark Brown 
4117384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
41188a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4119384c89e2SMark Brown #endif
4120db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4121db2a4165SFrank Mandarino }
4122db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4123db2a4165SFrank Mandarino 
4124db2a4165SFrank Mandarino /* Module information */
4125d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4126db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4127db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
41288b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
4129