xref: /openbmc/linux/sound/soc/soc-core.c (revision 43829731dd372d04d6706c51052b9dabab9ca356)
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++) {
594*43829731STejun Heo 		flush_delayed_work(&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 
901d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform)
902d12cd198SStephen Warren {
903d12cd198SStephen Warren 	int ret;
904d12cd198SStephen Warren 
905d12cd198SStephen Warren 	if (platform->driver->remove) {
906d12cd198SStephen Warren 		ret = platform->driver->remove(platform);
907d12cd198SStephen Warren 		if (ret < 0)
908d12cd198SStephen Warren 			pr_err("asoc: failed to remove %s: %d\n",
909d12cd198SStephen Warren 				platform->name, ret);
910d12cd198SStephen Warren 	}
911d12cd198SStephen Warren 
912d12cd198SStephen Warren 	/* Make sure all DAPM widgets are freed */
913d12cd198SStephen Warren 	snd_soc_dapm_free(&platform->dapm);
914d12cd198SStephen Warren 
915d12cd198SStephen Warren 	soc_cleanup_platform_debugfs(platform);
916d12cd198SStephen Warren 	platform->probed = 0;
917d12cd198SStephen Warren 	list_del(&platform->card_list);
918d12cd198SStephen Warren 	module_put(platform->dev->driver->owner);
919d12cd198SStephen Warren 
920d12cd198SStephen Warren 	return 0;
921d12cd198SStephen Warren }
922d12cd198SStephen Warren 
923589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
924589c3563SJarkko Nikula {
925589c3563SJarkko Nikula 	int err;
926589c3563SJarkko Nikula 
927589c3563SJarkko Nikula 	if (codec->driver->remove) {
928589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
929589c3563SJarkko Nikula 		if (err < 0)
930589c3563SJarkko Nikula 			dev_err(codec->dev,
931589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
932589c3563SJarkko Nikula 				codec->name, err);
933589c3563SJarkko Nikula 	}
934589c3563SJarkko Nikula 
935589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
936589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
937589c3563SJarkko Nikula 
938589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
939589c3563SJarkko Nikula 	codec->probed = 0;
940589c3563SJarkko Nikula 	list_del(&codec->card_list);
941589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
942589c3563SJarkko Nikula }
943589c3563SJarkko Nikula 
94462ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
945f0fba2adSLiam Girdwood {
946f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
947f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
948f0fba2adSLiam Girdwood 	int err;
949f0fba2adSLiam Girdwood 
950f0fba2adSLiam Girdwood 	/* unregister the rtd device */
951f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
95236ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
95336ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
95436ae1a96SMark Brown 		device_unregister(rtd->dev);
955f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
95602a06d30SBarry Song 	}
95702a06d30SBarry Song 
958f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9590168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
9600168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
961f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
962f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
963f0fba2adSLiam Girdwood 			if (err < 0)
9640837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9650837fc62SFabio Estevam 							codec_dai->name, err);
966f0fba2adSLiam Girdwood 		}
967f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
968f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
969f0fba2adSLiam Girdwood 	}
970f0fba2adSLiam Girdwood 
971f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
9720168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
9730168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
974f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
975f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
976f0fba2adSLiam Girdwood 			if (err < 0)
9770837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9780837fc62SFabio Estevam 							cpu_dai->name, err);
979f0fba2adSLiam Girdwood 		}
980f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
981f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
982a9db7dbeSStephen Warren 
98318d75644SStephen Warren 		if (!cpu_dai->codec) {
98418d75644SStephen Warren 			snd_soc_dapm_free(&cpu_dai->dapm);
985f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
986f0fba2adSLiam Girdwood 		}
987f0fba2adSLiam Girdwood 	}
98818d75644SStephen Warren }
989f0fba2adSLiam Girdwood 
99062ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
99162ae68faSStephen Warren 				       int order)
99262ae68faSStephen Warren {
99362ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
99462ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
99562ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
99662ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
99762ae68faSStephen Warren 	struct snd_soc_codec *codec;
99862ae68faSStephen Warren 
99962ae68faSStephen Warren 	/* remove the platform */
100062ae68faSStephen Warren 	if (platform && platform->probed &&
100162ae68faSStephen Warren 	    platform->driver->remove_order == order) {
100262ae68faSStephen Warren 		soc_remove_platform(platform);
100362ae68faSStephen Warren 	}
100462ae68faSStephen Warren 
100562ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
100662ae68faSStephen Warren 	if (codec_dai) {
100762ae68faSStephen Warren 		codec = codec_dai->codec;
100862ae68faSStephen Warren 		if (codec && codec->probed &&
100962ae68faSStephen Warren 		    codec->driver->remove_order == order)
101062ae68faSStephen Warren 			soc_remove_codec(codec);
101162ae68faSStephen Warren 	}
101262ae68faSStephen Warren 
101362ae68faSStephen Warren 	/* remove any CPU-side CODEC */
101462ae68faSStephen Warren 	if (cpu_dai) {
101562ae68faSStephen Warren 		codec = cpu_dai->codec;
101662ae68faSStephen Warren 		if (codec && codec->probed &&
101762ae68faSStephen Warren 		    codec->driver->remove_order == order)
101862ae68faSStephen Warren 			soc_remove_codec(codec);
101962ae68faSStephen Warren 	}
102062ae68faSStephen Warren }
102162ae68faSStephen Warren 
10220671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10230671fd8eSKuninori Morimoto {
10240168bf0dSLiam Girdwood 	int dai, order;
10250671fd8eSKuninori Morimoto 
10260168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10270168bf0dSLiam Girdwood 			order++) {
10280168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
102962ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10300168bf0dSLiam Girdwood 	}
103162ae68faSStephen Warren 
103262ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
103362ae68faSStephen Warren 			order++) {
103462ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
103562ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
103662ae68faSStephen Warren 	}
103762ae68faSStephen Warren 
10380671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10390671fd8eSKuninori Morimoto }
10400671fd8eSKuninori Morimoto 
1041ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1042ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1043ead9b919SJarkko Nikula {
1044ead9b919SJarkko Nikula 	int i;
1045ead9b919SJarkko Nikula 
1046ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1047ead9b919SJarkko Nikula 		return;
1048ead9b919SJarkko Nikula 
1049ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1050ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1051ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1052ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1053ead9b919SJarkko Nikula 			break;
1054ead9b919SJarkko Nikula 		}
1055ead9b919SJarkko Nikula 	}
1056ead9b919SJarkko Nikula }
1057ead9b919SJarkko Nikula 
1058589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1059589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1060589c3563SJarkko Nikula {
1061589c3563SJarkko Nikula 	int ret = 0;
106289b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1063888df395SMark Brown 	struct snd_soc_dai *dai;
1064589c3563SJarkko Nikula 
1065589c3563SJarkko Nikula 	codec->card = card;
1066589c3563SJarkko Nikula 	codec->dapm.card = card;
1067589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1068589c3563SJarkko Nikula 
106970d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
107070d29331SJarkko Nikula 		return -ENODEV;
107170d29331SJarkko Nikula 
1072d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1073d5d1e0beSLars-Peter Clausen 
107477530150SLars-Peter Clausen 	if (driver->dapm_widgets)
107577530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
107677530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
107777530150SLars-Peter Clausen 
1078888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1079888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1080888df395SMark Brown 		if (dai->dev != codec->dev)
1081888df395SMark Brown 			continue;
1082888df395SMark Brown 
1083888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1084888df395SMark Brown 	}
1085888df395SMark Brown 
108633c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
108733c5f969SMark Brown 
108889b95ac0SMark Brown 	if (driver->probe) {
108989b95ac0SMark Brown 		ret = driver->probe(codec);
1090589c3563SJarkko Nikula 		if (ret < 0) {
1091589c3563SJarkko Nikula 			dev_err(codec->dev,
1092589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1093589c3563SJarkko Nikula 				codec->name, ret);
109470d29331SJarkko Nikula 			goto err_probe;
1095589c3563SJarkko Nikula 		}
1096589c3563SJarkko Nikula 	}
1097589c3563SJarkko Nikula 
109838cbf959SMark Brown 	/* If the driver didn't set I/O up try regmap */
109938cbf959SMark Brown 	if (!codec->control_data)
110038cbf959SMark Brown 		snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
110138cbf959SMark Brown 
1102b7af1dafSMark Brown 	if (driver->controls)
1103022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1104b7af1dafSMark Brown 				     driver->num_controls);
110589b95ac0SMark Brown 	if (driver->dapm_routes)
110689b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
110789b95ac0SMark Brown 					driver->num_dapm_routes);
110889b95ac0SMark Brown 
1109589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1110589c3563SJarkko Nikula 	codec->probed = 1;
1111589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
11127be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1113589c3563SJarkko Nikula 
111470d29331SJarkko Nikula 	return 0;
111570d29331SJarkko Nikula 
111670d29331SJarkko Nikula err_probe:
1117d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
111870d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
111970d29331SJarkko Nikula 
1120589c3563SJarkko Nikula 	return ret;
1121589c3563SJarkko Nikula }
1122589c3563SJarkko Nikula 
1123956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1124956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1125956245e9SLiam Girdwood {
1126956245e9SLiam Girdwood 	int ret = 0;
1127956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1128be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1129956245e9SLiam Girdwood 
1130956245e9SLiam Girdwood 	platform->card = card;
1131b7950641SLiam Girdwood 	platform->dapm.card = card;
1132956245e9SLiam Girdwood 
1133956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1134956245e9SLiam Girdwood 		return -ENODEV;
1135956245e9SLiam Girdwood 
1136731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1137731f1ab2SSebastien Guiriec 
1138cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1139cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1140cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1141cb2cf612SLiam Girdwood 
1142be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
1143be09ad90SLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
1144be09ad90SLiam Girdwood 		if (dai->dev != platform->dev)
1145be09ad90SLiam Girdwood 			continue;
1146be09ad90SLiam Girdwood 
1147be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1148be09ad90SLiam Girdwood 	}
1149be09ad90SLiam Girdwood 
11503fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
11513fec6b6dSStephen Warren 
1152956245e9SLiam Girdwood 	if (driver->probe) {
1153956245e9SLiam Girdwood 		ret = driver->probe(platform);
1154956245e9SLiam Girdwood 		if (ret < 0) {
1155956245e9SLiam Girdwood 			dev_err(platform->dev,
1156956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1157956245e9SLiam Girdwood 				platform->name, ret);
1158956245e9SLiam Girdwood 			goto err_probe;
1159956245e9SLiam Girdwood 		}
1160956245e9SLiam Girdwood 	}
1161956245e9SLiam Girdwood 
1162cb2cf612SLiam Girdwood 	if (driver->controls)
1163cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1164cb2cf612SLiam Girdwood 				     driver->num_controls);
1165cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1166cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1167cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1168cb2cf612SLiam Girdwood 
1169956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1170956245e9SLiam Girdwood 	platform->probed = 1;
1171956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1172b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1173956245e9SLiam Girdwood 
1174956245e9SLiam Girdwood 	return 0;
1175956245e9SLiam Girdwood 
1176956245e9SLiam Girdwood err_probe:
117702db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1178956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1179956245e9SLiam Girdwood 
1180956245e9SLiam Girdwood 	return ret;
1181956245e9SLiam Girdwood }
1182956245e9SLiam Girdwood 
118336ae1a96SMark Brown static void rtd_release(struct device *dev)
118436ae1a96SMark Brown {
118536ae1a96SMark Brown 	kfree(dev);
118636ae1a96SMark Brown }
1187f0fba2adSLiam Girdwood 
1188589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1189589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1190589c3563SJarkko Nikula 				   int num, int dailess)
1191589c3563SJarkko Nikula {
1192589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1193589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1194589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1195589c3563SJarkko Nikula 	const char *temp, *name;
1196589c3563SJarkko Nikula 	int ret = 0;
1197589c3563SJarkko Nikula 
1198589c3563SJarkko Nikula 	if (!dailess) {
1199589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1200589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1201589c3563SJarkko Nikula 		name = dai_link->name;
1202589c3563SJarkko Nikula 	} else {
1203589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1204589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1205589c3563SJarkko Nikula 		name = aux_dev->name;
1206589c3563SJarkko Nikula 	}
12070962bb21SJanusz Krzysztofik 	rtd->card = card;
1208589c3563SJarkko Nikula 
12094b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
12104b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
12114b1cfcb4SMark Brown 
1212589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1213589c3563SJarkko Nikula 	temp = codec->name_prefix;
1214589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1215589c3563SJarkko Nikula 
1216589c3563SJarkko Nikula 	/* do machine specific initialization */
1217589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1218589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1219589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1220589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1221589c3563SJarkko Nikula 	if (ret < 0) {
1222589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1223589c3563SJarkko Nikula 		return ret;
1224589c3563SJarkko Nikula 	}
1225589c3563SJarkko Nikula 	codec->name_prefix = temp;
1226589c3563SJarkko Nikula 
1227589c3563SJarkko Nikula 	/* register the rtd device */
1228589c3563SJarkko Nikula 	rtd->codec = codec;
122936ae1a96SMark Brown 
123036ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
123136ae1a96SMark Brown 	if (!rtd->dev)
123236ae1a96SMark Brown 		return -ENOMEM;
123336ae1a96SMark Brown 	device_initialize(rtd->dev);
123436ae1a96SMark Brown 	rtd->dev->parent = card->dev;
123536ae1a96SMark Brown 	rtd->dev->release = rtd_release;
123636ae1a96SMark Brown 	rtd->dev->init_name = name;
123736ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1238b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
123901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
124001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
124101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
124201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
124336ae1a96SMark Brown 	ret = device_add(rtd->dev);
1244589c3563SJarkko Nikula 	if (ret < 0) {
1245589c3563SJarkko Nikula 		dev_err(card->dev,
1246589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1247589c3563SJarkko Nikula 		return ret;
1248589c3563SJarkko Nikula 	}
1249589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1250589c3563SJarkko Nikula 
1251589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
125236ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1253589c3563SJarkko Nikula 	if (ret < 0)
1254589c3563SJarkko Nikula 		dev_err(codec->dev,
1255589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1256589c3563SJarkko Nikula 			ret);
1257589c3563SJarkko Nikula 
1258589c3563SJarkko Nikula 	/* add codec sysfs entries */
125936ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1260589c3563SJarkko Nikula 	if (ret < 0)
1261589c3563SJarkko Nikula 		dev_err(codec->dev,
1262589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1263589c3563SJarkko Nikula 
1264f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1265f86dcef8SLiam Girdwood 	/* add DPCM sysfs entries */
1266cd0f8911SLiam Girdwood 	if (!dailess && !dai_link->dynamic)
1267f86dcef8SLiam Girdwood 		goto out;
1268f86dcef8SLiam Girdwood 
1269f86dcef8SLiam Girdwood 	ret = soc_dpcm_debugfs_add(rtd);
1270f86dcef8SLiam Girdwood 	if (ret < 0)
1271f86dcef8SLiam Girdwood 		dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1272f86dcef8SLiam Girdwood 
1273f86dcef8SLiam Girdwood out:
1274f86dcef8SLiam Girdwood #endif
1275589c3563SJarkko Nikula 	return 0;
1276589c3563SJarkko Nikula }
1277589c3563SJarkko Nikula 
127862ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
127962ae68faSStephen Warren 				     int order)
128062ae68faSStephen Warren {
128162ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
128262ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
128362ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
128462ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
128562ae68faSStephen Warren 	int ret;
128662ae68faSStephen Warren 
128762ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
128862ae68faSStephen Warren 	if (cpu_dai->codec &&
128962ae68faSStephen Warren 	    !cpu_dai->codec->probed &&
129062ae68faSStephen Warren 	    cpu_dai->codec->driver->probe_order == order) {
129162ae68faSStephen Warren 		ret = soc_probe_codec(card, cpu_dai->codec);
129262ae68faSStephen Warren 		if (ret < 0)
129362ae68faSStephen Warren 			return ret;
129462ae68faSStephen Warren 	}
129562ae68faSStephen Warren 
129662ae68faSStephen Warren 	/* probe the CODEC-side component */
129762ae68faSStephen Warren 	if (!codec_dai->codec->probed &&
129862ae68faSStephen Warren 	    codec_dai->codec->driver->probe_order == order) {
129962ae68faSStephen Warren 		ret = soc_probe_codec(card, codec_dai->codec);
130062ae68faSStephen Warren 		if (ret < 0)
130162ae68faSStephen Warren 			return ret;
130262ae68faSStephen Warren 	}
130362ae68faSStephen Warren 
130462ae68faSStephen Warren 	/* probe the platform */
130562ae68faSStephen Warren 	if (!platform->probed &&
130662ae68faSStephen Warren 	    platform->driver->probe_order == order) {
130762ae68faSStephen Warren 		ret = soc_probe_platform(card, platform);
130862ae68faSStephen Warren 		if (ret < 0)
130962ae68faSStephen Warren 			return ret;
131062ae68faSStephen Warren 	}
131162ae68faSStephen Warren 
131262ae68faSStephen Warren 	return 0;
131362ae68faSStephen Warren }
131462ae68faSStephen Warren 
131562ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1316f0fba2adSLiam Girdwood {
1317f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1318f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1319f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1320f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1321c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1322c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1323c74184edSMark Brown 	struct snd_soc_dapm_widget *play_w, *capture_w;
1324f0fba2adSLiam Girdwood 	int ret;
1325f0fba2adSLiam Girdwood 
13260168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
13270168bf0dSLiam Girdwood 			card->name, num, order);
1328f0fba2adSLiam Girdwood 
1329f0fba2adSLiam Girdwood 	/* config components */
1330f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1331f0fba2adSLiam Girdwood 	codec_dai->card = card;
1332f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1333f0fba2adSLiam Girdwood 
1334f0fba2adSLiam Girdwood 	/* set default power off timeout */
1335f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1336f0fba2adSLiam Girdwood 
1337f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
13380168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
13390168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1340a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
1341be09ad90SLiam Girdwood 			cpu_dai->dapm.card = card;
134261b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
134361b61e3cSLiam Girdwood 				return -ENODEV;
134461b61e3cSLiam Girdwood 
134518d75644SStephen Warren 			list_add(&cpu_dai->dapm.list, &card->dapm_list);
1346be09ad90SLiam Girdwood 			snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1347a9db7dbeSStephen Warren 		}
1348be09ad90SLiam Girdwood 
1349f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1350f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1351f0fba2adSLiam Girdwood 			if (ret < 0) {
13520837fc62SFabio Estevam 				pr_err("asoc: failed to probe CPU DAI %s: %d\n",
13530837fc62SFabio Estevam 							cpu_dai->name, ret);
135461b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1355f0fba2adSLiam Girdwood 				return ret;
1356f0fba2adSLiam Girdwood 			}
1357f0fba2adSLiam Girdwood 		}
1358f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
13591c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1360f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1361f0fba2adSLiam Girdwood 	}
1362f0fba2adSLiam Girdwood 
1363f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
13640168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1365f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1366f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1367f0fba2adSLiam Girdwood 			if (ret < 0) {
13680837fc62SFabio Estevam 				pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
13690837fc62SFabio Estevam 							codec_dai->name, ret);
1370f0fba2adSLiam Girdwood 				return ret;
1371f0fba2adSLiam Girdwood 			}
1372f0fba2adSLiam Girdwood 		}
1373f0fba2adSLiam Girdwood 
13741c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1375f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1376f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1377f0fba2adSLiam Girdwood 	}
1378f0fba2adSLiam Girdwood 
13790168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
13800168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
13810168bf0dSLiam Girdwood 		return 0;
13820168bf0dSLiam Girdwood 
1383589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1384589c3563SJarkko Nikula 	if (ret)
1385f0fba2adSLiam Girdwood 		return ret;
1386f0fba2adSLiam Girdwood 
138736ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1388f0fba2adSLiam Girdwood 	if (ret < 0)
13890837fc62SFabio Estevam 		pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1390f0fba2adSLiam Girdwood 
1391c74184edSMark Brown 	if (!dai_link->params) {
1392f0fba2adSLiam Girdwood 		/* create the pcm */
1393f0fba2adSLiam Girdwood 		ret = soc_new_pcm(rtd, num);
1394f0fba2adSLiam Girdwood 		if (ret < 0) {
13950837fc62SFabio Estevam 			pr_err("asoc: can't create pcm %s :%d\n",
13960837fc62SFabio Estevam 			       dai_link->stream_name, ret);
1397f0fba2adSLiam Girdwood 			return ret;
1398f0fba2adSLiam Girdwood 		}
1399c74184edSMark Brown 	} else {
1400c74184edSMark Brown 		/* link the DAI widgets */
1401c74184edSMark Brown 		play_w = codec_dai->playback_widget;
1402c74184edSMark Brown 		capture_w = cpu_dai->capture_widget;
1403c74184edSMark Brown 		if (play_w && capture_w) {
1404c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1405c74184edSMark Brown 						   capture_w, play_w);
1406c74184edSMark Brown 			if (ret != 0) {
1407c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1408c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1409c74184edSMark Brown 				return ret;
1410c74184edSMark Brown 			}
1411c74184edSMark Brown 		}
1412c74184edSMark Brown 
1413c74184edSMark Brown 		play_w = cpu_dai->playback_widget;
1414c74184edSMark Brown 		capture_w = codec_dai->capture_widget;
1415c74184edSMark Brown 		if (play_w && capture_w) {
1416c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1417c74184edSMark Brown 						   capture_w, play_w);
1418c74184edSMark Brown 			if (ret != 0) {
1419c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1420c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1421c74184edSMark Brown 				return ret;
1422c74184edSMark Brown 			}
1423c74184edSMark Brown 		}
1424c74184edSMark Brown 	}
1425f0fba2adSLiam Girdwood 
1426f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1427f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1428f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1429f0fba2adSLiam Girdwood 
1430f0fba2adSLiam Girdwood 	return 0;
1431f0fba2adSLiam Girdwood }
1432f0fba2adSLiam Girdwood 
1433f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1434f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1435f0fba2adSLiam Girdwood {
1436f0fba2adSLiam Girdwood 	int ret;
1437f0fba2adSLiam Girdwood 
1438f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1439f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
14406b05eda6SMark Brown 	 */
1441f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
14420562f788SMika Westerberg 		/*
14430562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
14440562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
14450562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
14460562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
14470562f788SMika Westerberg 		 *
14480562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
14490562f788SMika Westerberg 		 */
14500562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
14510562f788SMika Westerberg 			return 0;
1452f0fba2adSLiam Girdwood 
1453f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1454f0fba2adSLiam Girdwood 		if (ret < 0) {
14550837fc62SFabio Estevam 			pr_err("asoc: AC97 device register failed:%d\n", ret);
1456f0fba2adSLiam Girdwood 			return ret;
1457435c5e25SMark Brown 		}
1458435c5e25SMark Brown 
1459f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1460f0fba2adSLiam Girdwood 	}
1461f0fba2adSLiam Girdwood 	return 0;
1462f0fba2adSLiam Girdwood }
1463435c5e25SMark Brown 
1464f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1465f0fba2adSLiam Girdwood {
1466f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1467f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1468f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1469f0fba2adSLiam Girdwood 	}
1470f0fba2adSLiam Girdwood }
1471f0fba2adSLiam Girdwood #endif
1472435c5e25SMark Brown 
1473b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1474b19e6e7bSMark Brown {
1475b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1476b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1477b19e6e7bSMark Brown 
1478b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1479b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1480b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1481b19e6e7bSMark Brown 			return 0;
1482b19e6e7bSMark Brown 	}
1483b19e6e7bSMark Brown 
1484b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1485b19e6e7bSMark Brown }
1486b19e6e7bSMark Brown 
14872eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
14882eea392dSJarkko Nikula {
14892eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
14902eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1491676ad98aSJarkko Nikula 	int ret = -ENODEV;
14922eea392dSJarkko Nikula 
14932eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
14942eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
14952eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
14962eea392dSJarkko Nikula 			if (codec->probed) {
14972eea392dSJarkko Nikula 				dev_err(codec->dev,
14982eea392dSJarkko Nikula 					"asoc: codec already probed");
14992eea392dSJarkko Nikula 				ret = -EBUSY;
15002eea392dSJarkko Nikula 				goto out;
15012eea392dSJarkko Nikula 			}
1502676ad98aSJarkko Nikula 			goto found;
15032eea392dSJarkko Nikula 		}
15042eea392dSJarkko Nikula 	}
1505676ad98aSJarkko Nikula 	/* codec not found */
1506676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1507b19e6e7bSMark Brown 	return -EPROBE_DEFER;
15082eea392dSJarkko Nikula 
1509676ad98aSJarkko Nikula found:
1510589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
15112eea392dSJarkko Nikula 	if (ret < 0)
1512589c3563SJarkko Nikula 		return ret;
15132eea392dSJarkko Nikula 
1514589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
15152eea392dSJarkko Nikula 
15162eea392dSJarkko Nikula out:
15172eea392dSJarkko Nikula 	return ret;
15182eea392dSJarkko Nikula }
15192eea392dSJarkko Nikula 
15202eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
15212eea392dSJarkko Nikula {
15222eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
15232eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
15242eea392dSJarkko Nikula 
15252eea392dSJarkko Nikula 	/* unregister the rtd device */
15262eea392dSJarkko Nikula 	if (rtd->dev_registered) {
152736ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
152836ae1a96SMark Brown 		device_del(rtd->dev);
15292eea392dSJarkko Nikula 		rtd->dev_registered = 0;
15302eea392dSJarkko Nikula 	}
15312eea392dSJarkko Nikula 
1532589c3563SJarkko Nikula 	if (codec && codec->probed)
1533589c3563SJarkko Nikula 		soc_remove_codec(codec);
15342eea392dSJarkko Nikula }
15352eea392dSJarkko Nikula 
1536fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1537fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1538fdf0f54dSDimitris Papastamos {
1539fdf0f54dSDimitris Papastamos 	int ret;
1540fdf0f54dSDimitris Papastamos 
1541fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1542fdf0f54dSDimitris Papastamos 		return 0;
1543fdf0f54dSDimitris Papastamos 
1544fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1545fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1546fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1547fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1548fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1549fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1550fdf0f54dSDimitris Papastamos 			ret);
1551fdf0f54dSDimitris Papastamos 		return ret;
1552fdf0f54dSDimitris Papastamos 	}
1553fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1554fdf0f54dSDimitris Papastamos 	return 0;
1555fdf0f54dSDimitris Papastamos }
1556fdf0f54dSDimitris Papastamos 
1557b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1558f0fba2adSLiam Girdwood {
1559fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1560fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1561fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
156275d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1563f04209a7SMark Brown 	int ret, i, order, dai_fmt;
156496dd3622SMark Brown 
156501b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1566f0fba2adSLiam Girdwood 
1567b19e6e7bSMark Brown 	/* bind DAIs */
1568b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1569b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1570b19e6e7bSMark Brown 		if (ret != 0)
1571b19e6e7bSMark Brown 			goto base_error;
1572db2a4165SFrank Mandarino 	}
1573db2a4165SFrank Mandarino 
1574b19e6e7bSMark Brown 	/* check aux_devs too */
1575b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1576b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1577b19e6e7bSMark Brown 		if (ret != 0)
1578b19e6e7bSMark Brown 			goto base_error;
1579db2a4165SFrank Mandarino 	}
1580db2a4165SFrank Mandarino 
1581fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1582fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1583fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1584fdf0f54dSDimitris Papastamos 			continue;
1585861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1586861f2fafSDimitris Papastamos 		compress_type = 0;
1587fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1588fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1589fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1590fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1591fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1592fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1593fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1594fdf0f54dSDimitris Papastamos 					break;
1595fdf0f54dSDimitris Papastamos 			}
1596fdf0f54dSDimitris Papastamos 		}
1597fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1598b19e6e7bSMark Brown 		if (ret < 0)
1599b19e6e7bSMark Brown 			goto base_error;
1600fdf0f54dSDimitris Papastamos 	}
1601fdf0f54dSDimitris Papastamos 
1602f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1603f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1604f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1605f0fba2adSLiam Girdwood 	if (ret < 0) {
16060837fc62SFabio Estevam 		pr_err("asoc: can't create sound card for card %s: %d\n",
16070837fc62SFabio Estevam 			card->name, ret);
1608b19e6e7bSMark Brown 		goto base_error;
1609db2a4165SFrank Mandarino 	}
1610f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1611db2a4165SFrank Mandarino 
1612e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1613e37a4970SMark Brown 	card->dapm.dev = card->dev;
1614e37a4970SMark Brown 	card->dapm.card = card;
1615e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1616e37a4970SMark Brown 
1617d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1618d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1619d5d1e0beSLars-Peter Clausen #endif
1620d5d1e0beSLars-Peter Clausen 
162188ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
16226ed25978SAndy Green 	/* deferred resume work */
16236308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16241301a964SRandy Dunlap #endif
16256ed25978SAndy Green 
16269a841ebbSMark Brown 	if (card->dapm_widgets)
16279a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
16289a841ebbSMark Brown 					  card->num_dapm_widgets);
16299a841ebbSMark Brown 
1630f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1631f0fba2adSLiam Girdwood 	if (card->probe) {
1632e7361ec4SMark Brown 		ret = card->probe(card);
1633f0fba2adSLiam Girdwood 		if (ret < 0)
1634f0fba2adSLiam Girdwood 			goto card_probe_error;
1635f0fba2adSLiam Girdwood 	}
1636f0fba2adSLiam Girdwood 
163762ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
16380168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
16390168bf0dSLiam Girdwood 			order++) {
1640fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
164162ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
164262ae68faSStephen Warren 			if (ret < 0) {
164362ae68faSStephen Warren 				pr_err("asoc: failed to instantiate card %s: %d\n",
164462ae68faSStephen Warren 				       card->name, ret);
164562ae68faSStephen Warren 				goto probe_dai_err;
164662ae68faSStephen Warren 			}
164762ae68faSStephen Warren 		}
164862ae68faSStephen Warren 	}
164962ae68faSStephen Warren 
165062ae68faSStephen Warren 	/* probe all DAI links on this card */
165162ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
165262ae68faSStephen Warren 			order++) {
165362ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
165462ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1655fe3e78e0SMark Brown 			if (ret < 0) {
1656321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1657321de0d0SMark Brown 				       card->name, ret);
1658f0fba2adSLiam Girdwood 				goto probe_dai_err;
1659fe3e78e0SMark Brown 			}
1660fe3e78e0SMark Brown 		}
16610168bf0dSLiam Girdwood 	}
1662fe3e78e0SMark Brown 
16632eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
16642eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
16652eea392dSJarkko Nikula 		if (ret < 0) {
16662eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
16672eea392dSJarkko Nikula 			       card->name, ret);
16682eea392dSJarkko Nikula 			goto probe_aux_dev_err;
16692eea392dSJarkko Nikula 		}
16702eea392dSJarkko Nikula 	}
16712eea392dSJarkko Nikula 
1672888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1673888df395SMark Brown 
1674b7af1dafSMark Brown 	if (card->controls)
1675022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1676b7af1dafSMark Brown 
1677b8ad29deSMark Brown 	if (card->dapm_routes)
1678b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1679b8ad29deSMark Brown 					card->num_dapm_routes);
1680b8ad29deSMark Brown 
1681b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1682b90d2f90SMark Brown 
168375d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
168475d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1685f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
168675d9ac46SMark Brown 
1687f04209a7SMark Brown 		if (dai_fmt) {
168875d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1689f04209a7SMark Brown 						  dai_fmt);
16905e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
169175d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
169275d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
169375d9ac46SMark Brown 					 ret);
1694f04209a7SMark Brown 		}
1695f04209a7SMark Brown 
1696f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1697fe33d4c5SStephen Warren 		if (dai_fmt &&
1698fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1699f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1700f04209a7SMark Brown 						  dai_fmt);
1701f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1702f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1703f04209a7SMark Brown 					 "Failed to set DAI format: %d\n",
1704f04209a7SMark Brown 					 ret);
1705f04209a7SMark Brown 		} else if (dai_fmt) {
1706f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1707f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1708f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1709f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1710f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1711f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1712f04209a7SMark Brown 				break;
1713f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1714f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1715f04209a7SMark Brown 				break;
1716f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1717f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1718f04209a7SMark Brown 				break;
1719f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1720f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1721f04209a7SMark Brown 				break;
1722f04209a7SMark Brown 			}
172375d9ac46SMark Brown 
172475d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1725f04209a7SMark Brown 						  dai_fmt);
17265e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
172775d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
172875d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
172975d9ac46SMark Brown 					 ret);
173075d9ac46SMark Brown 		}
173175d9ac46SMark Brown 	}
173275d9ac46SMark Brown 
1733f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1734fe3e78e0SMark Brown 		 "%s", card->name);
1735f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
173622de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1737f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1738f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1739f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1740f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1741f0e8ed85SMark Brown 		case '_':
1742f0e8ed85SMark Brown 		case '-':
1743f0e8ed85SMark Brown 		case '\0':
1744f0e8ed85SMark Brown 			break;
1745f0e8ed85SMark Brown 		default:
1746f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1747f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1748f0e8ed85SMark Brown 			break;
1749f0e8ed85SMark Brown 		}
1750f0e8ed85SMark Brown 	}
1751fe3e78e0SMark Brown 
175228e9ad92SMark Brown 	if (card->late_probe) {
175328e9ad92SMark Brown 		ret = card->late_probe(card);
175428e9ad92SMark Brown 		if (ret < 0) {
175528e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
175628e9ad92SMark Brown 				card->name, ret);
175728e9ad92SMark Brown 			goto probe_aux_dev_err;
175828e9ad92SMark Brown 		}
175928e9ad92SMark Brown 	}
176028e9ad92SMark Brown 
17612dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
17622dc00213SMark Brown 
17631633281bSStephen Warren 	if (card->fully_routed)
1764b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
17651633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
17661633281bSStephen Warren 
1767f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1768fe3e78e0SMark Brown 	if (ret < 0) {
17690837fc62SFabio Estevam 		pr_err("asoc: failed to register soundcard for %s: %d\n",
17700837fc62SFabio Estevam 							card->name, ret);
17716b3ed785SAxel Lin 		goto probe_aux_dev_err;
1772fe3e78e0SMark Brown 	}
1773fe3e78e0SMark Brown 
1774fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1775f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1776f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1777f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1778fe3e78e0SMark Brown 		if (ret < 0) {
17790837fc62SFabio Estevam 			pr_err("asoc: failed to register AC97 %s: %d\n",
17800837fc62SFabio Estevam 							card->name, ret);
17816b3ed785SAxel Lin 			while (--i >= 0)
17827d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
17836b3ed785SAxel Lin 			goto probe_aux_dev_err;
1784fe3e78e0SMark Brown 		}
1785fe3e78e0SMark Brown 	}
1786fe3e78e0SMark Brown #endif
1787fe3e78e0SMark Brown 
1788435c5e25SMark Brown 	card->instantiated = 1;
17894f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1790f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1791b19e6e7bSMark Brown 
1792b19e6e7bSMark Brown 	return 0;
1793db2a4165SFrank Mandarino 
17942eea392dSJarkko Nikula probe_aux_dev_err:
17952eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17962eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17972eea392dSJarkko Nikula 
1798f0fba2adSLiam Girdwood probe_dai_err:
17990671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1800fe3e78e0SMark Brown 
1801f0fba2adSLiam Girdwood card_probe_error:
180287506549SMark Brown 	if (card->remove)
1803e7361ec4SMark Brown 		card->remove(card);
1804f0fba2adSLiam Girdwood 
1805f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1806f0fba2adSLiam Girdwood 
1807b19e6e7bSMark Brown base_error:
1808f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1809db2a4165SFrank Mandarino 
1810b19e6e7bSMark Brown 	return ret;
1811435c5e25SMark Brown }
1812435c5e25SMark Brown 
1813435c5e25SMark Brown /* probes a new socdev */
1814435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1815435c5e25SMark Brown {
1816f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1817435c5e25SMark Brown 	int ret = 0;
1818435c5e25SMark Brown 
181970a7ca34SVinod Koul 	/*
182070a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
182170a7ca34SVinod Koul 	 * we should not be here in that case so ret error
182270a7ca34SVinod Koul 	 */
182370a7ca34SVinod Koul 	if (!card)
182470a7ca34SVinod Koul 		return -EINVAL;
182570a7ca34SVinod Koul 
1826fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1827fe4085e8SMark Brown 		 "ASoC machine %s should use snd_soc_register_card()\n",
1828fe4085e8SMark Brown 		 card->name);
1829fe4085e8SMark Brown 
1830435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1831435c5e25SMark Brown 	card->dev = &pdev->dev;
1832f0fba2adSLiam Girdwood 
1833435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1834435c5e25SMark Brown 	if (ret != 0) {
1835435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1836db2a4165SFrank Mandarino 		return ret;
1837db2a4165SFrank Mandarino 	}
1838db2a4165SFrank Mandarino 
1839435c5e25SMark Brown 	return 0;
1840435c5e25SMark Brown }
1841435c5e25SMark Brown 
1842b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1843db2a4165SFrank Mandarino {
1844db2a4165SFrank Mandarino 	int i;
1845db2a4165SFrank Mandarino 
1846f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1847f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1848f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1849*43829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1850db2a4165SFrank Mandarino 	}
1851db2a4165SFrank Mandarino 
18522eea392dSJarkko Nikula 	/* remove auxiliary devices */
18532eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18542eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18552eea392dSJarkko Nikula 
1856f0fba2adSLiam Girdwood 	/* remove and free each DAI */
18570671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1858f0fba2adSLiam Girdwood 
1859a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1860a6052154SJarkko Nikula 
1861f0fba2adSLiam Girdwood 	/* remove the card */
186287506549SMark Brown 	if (card->remove)
1863e7361ec4SMark Brown 		card->remove(card);
1864f0fba2adSLiam Girdwood 
18650aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
18660aaae527SLars-Peter Clausen 
1867f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1868b0e26485SVinod Koul 	return 0;
1869b0e26485SVinod Koul 
1870b2dfa62cSGuennadi Liakhovetski }
1871b0e26485SVinod Koul 
1872b0e26485SVinod Koul /* removes a socdev */
1873b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1874b0e26485SVinod Koul {
1875b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1876b0e26485SVinod Koul 
1877c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1878db2a4165SFrank Mandarino 	return 0;
1879db2a4165SFrank Mandarino }
1880db2a4165SFrank Mandarino 
18816f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
188251737470SMark Brown {
18836f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1884f0fba2adSLiam Girdwood 	int i;
188551737470SMark Brown 
188651737470SMark Brown 	if (!card->instantiated)
1887416356fcSMark Brown 		return 0;
188851737470SMark Brown 
188951737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
189051737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1891f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1892f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1893*43829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1894f0fba2adSLiam Girdwood 	}
189551737470SMark Brown 
1896f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1897416356fcSMark Brown 
1898416356fcSMark Brown 	return 0;
189951737470SMark Brown }
19006f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
190151737470SMark Brown 
19026f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1903b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1904b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1905b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1906b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
19076f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1908b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1909416356fcSMark Brown };
1910deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1911416356fcSMark Brown 
1912db2a4165SFrank Mandarino /* ASoC platform driver */
1913db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1914db2a4165SFrank Mandarino 	.driver		= {
1915db2a4165SFrank Mandarino 		.name		= "soc-audio",
19168b45a209SKay Sievers 		.owner		= THIS_MODULE,
19176f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1918db2a4165SFrank Mandarino 	},
1919db2a4165SFrank Mandarino 	.probe		= soc_probe,
1920db2a4165SFrank Mandarino 	.remove		= soc_remove,
1921db2a4165SFrank Mandarino };
1922db2a4165SFrank Mandarino 
1923096e49d5SMark Brown /**
1924096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1925096e49d5SMark Brown  *
1926096e49d5SMark Brown  * @codec: CODEC to query.
1927096e49d5SMark Brown  * @reg: Register to query.
1928096e49d5SMark Brown  *
1929096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1930096e49d5SMark Brown  */
1931181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1932181e055eSMark Brown 				    unsigned int reg)
1933096e49d5SMark Brown {
19341500b7b5SDimitris Papastamos 	if (codec->volatile_register)
19351500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1936096e49d5SMark Brown 	else
1937096e49d5SMark Brown 		return 0;
1938096e49d5SMark Brown }
1939096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1940096e49d5SMark Brown 
1941db2a4165SFrank Mandarino /**
1942239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1943239c9706SDimitris Papastamos  *
1944239c9706SDimitris Papastamos  * @codec: CODEC to query.
1945239c9706SDimitris Papastamos  * @reg: Register to query.
1946239c9706SDimitris Papastamos  *
1947239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1948239c9706SDimitris Papastamos  */
1949239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1950239c9706SDimitris Papastamos 				    unsigned int reg)
1951239c9706SDimitris Papastamos {
1952239c9706SDimitris Papastamos 	if (codec->readable_register)
1953239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1954239c9706SDimitris Papastamos 	else
195563fa0a28SLars-Peter Clausen 		return 1;
1956239c9706SDimitris Papastamos }
1957239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1958239c9706SDimitris Papastamos 
1959239c9706SDimitris Papastamos /**
1960239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1961239c9706SDimitris Papastamos  *
1962239c9706SDimitris Papastamos  * @codec: CODEC to query.
1963239c9706SDimitris Papastamos  * @reg: Register to query.
1964239c9706SDimitris Papastamos  *
1965239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1966239c9706SDimitris Papastamos  */
1967239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1968239c9706SDimitris Papastamos 				    unsigned int reg)
1969239c9706SDimitris Papastamos {
1970239c9706SDimitris Papastamos 	if (codec->writable_register)
1971239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1972239c9706SDimitris Papastamos 	else
197363fa0a28SLars-Peter Clausen 		return 1;
1974239c9706SDimitris Papastamos }
1975239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1976239c9706SDimitris Papastamos 
1977f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1978f1442bc1SLiam Girdwood 					unsigned int reg)
1979f1442bc1SLiam Girdwood {
1980f1442bc1SLiam Girdwood 	unsigned int ret;
1981f1442bc1SLiam Girdwood 
1982f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1983f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1984f1442bc1SLiam Girdwood 		return -1;
1985f1442bc1SLiam Girdwood 	}
1986f1442bc1SLiam Girdwood 
1987f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1988f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1989a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1990f1442bc1SLiam Girdwood 
1991f1442bc1SLiam Girdwood 	return ret;
1992f1442bc1SLiam Girdwood }
1993f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1994f1442bc1SLiam Girdwood 
1995f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1996f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1997f1442bc1SLiam Girdwood {
1998f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1999f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
2000f1442bc1SLiam Girdwood 		return -1;
2001f1442bc1SLiam Girdwood 	}
2002f1442bc1SLiam Girdwood 
2003f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2004a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
2005f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
2006f1442bc1SLiam Girdwood }
2007f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2008f1442bc1SLiam Girdwood 
2009239c9706SDimitris Papastamos /**
2010db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2011db2a4165SFrank Mandarino  * @codec: audio codec
2012db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2013db2a4165SFrank Mandarino  * @num: AC97 codec number
2014db2a4165SFrank Mandarino  *
2015db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2016db2a4165SFrank Mandarino  */
2017db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2018db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2019db2a4165SFrank Mandarino {
2020db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2021db2a4165SFrank Mandarino 
2022db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2023db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2024db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2025db2a4165SFrank Mandarino 		return -ENOMEM;
2026db2a4165SFrank Mandarino 	}
2027db2a4165SFrank Mandarino 
2028db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2029db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2030db2a4165SFrank Mandarino 		kfree(codec->ac97);
2031db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2032db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2033db2a4165SFrank Mandarino 		return -ENOMEM;
2034db2a4165SFrank Mandarino 	}
2035db2a4165SFrank Mandarino 
2036db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2037db2a4165SFrank Mandarino 	codec->ac97->num = num;
20380562f788SMika Westerberg 
20390562f788SMika Westerberg 	/*
20400562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
20410562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
20420562f788SMika Westerberg 	 */
20430562f788SMika Westerberg 	codec->ac97_created = 1;
20440562f788SMika Westerberg 
2045db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2046db2a4165SFrank Mandarino 	return 0;
2047db2a4165SFrank Mandarino }
2048db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2049db2a4165SFrank Mandarino 
2050db2a4165SFrank Mandarino /**
2051db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2052db2a4165SFrank Mandarino  * @codec: audio codec
2053db2a4165SFrank Mandarino  *
2054db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2055db2a4165SFrank Mandarino  */
2056db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2057db2a4165SFrank Mandarino {
2058db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2059f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2060f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2061f0fba2adSLiam Girdwood #endif
2062db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2063db2a4165SFrank Mandarino 	kfree(codec->ac97);
2064db2a4165SFrank Mandarino 	codec->ac97 = NULL;
20650562f788SMika Westerberg 	codec->ac97_created = 0;
2066db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2067db2a4165SFrank Mandarino }
2068db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2069db2a4165SFrank Mandarino 
2070c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2071c3753707SMark Brown {
2072c3753707SMark Brown 	unsigned int ret;
2073c3753707SMark Brown 
2074c3acec26SMark Brown 	ret = codec->read(codec, reg);
2075c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2076a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2077c3753707SMark Brown 
2078c3753707SMark Brown 	return ret;
2079c3753707SMark Brown }
2080c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2081c3753707SMark Brown 
2082c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2083c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2084c3753707SMark Brown {
2085c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2086a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2087c3acec26SMark Brown 	return codec->write(codec, reg, val);
2088c3753707SMark Brown }
2089c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2090c3753707SMark Brown 
20915fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
20925fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
20935fb609d4SDimitris Papastamos {
20945fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
20955fb609d4SDimitris Papastamos }
20965fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
20975fb609d4SDimitris Papastamos 
2098db2a4165SFrank Mandarino /**
2099db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2100db2a4165SFrank Mandarino  * @codec: audio codec
2101db2a4165SFrank Mandarino  * @reg: codec register
2102db2a4165SFrank Mandarino  * @mask: register mask
2103db2a4165SFrank Mandarino  * @value: new value
2104db2a4165SFrank Mandarino  *
2105db2a4165SFrank Mandarino  * Writes new register value.
2106db2a4165SFrank Mandarino  *
2107180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
2108db2a4165SFrank Mandarino  */
2109db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
211046f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2111db2a4165SFrank Mandarino {
21128a713da8SMark Brown 	bool change;
211346f5822fSDaniel Ribeiro 	unsigned int old, new;
2114180c329dSTimur Tabi 	int ret;
2115db2a4165SFrank Mandarino 
21168a713da8SMark Brown 	if (codec->using_regmap) {
21178a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
21188a713da8SMark Brown 					       mask, value, &change);
21198a713da8SMark Brown 	} else {
2120180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
2121180c329dSTimur Tabi 		if (ret < 0)
2122180c329dSTimur Tabi 			return ret;
2123180c329dSTimur Tabi 
2124180c329dSTimur Tabi 		old = ret;
212578bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
2126db2a4165SFrank Mandarino 		change = old != new;
21278a713da8SMark Brown 		if (change)
2128180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
21298a713da8SMark Brown 	}
21308a713da8SMark Brown 
2131180c329dSTimur Tabi 	if (ret < 0)
2132180c329dSTimur Tabi 		return ret;
2133db2a4165SFrank Mandarino 
2134db2a4165SFrank Mandarino 	return change;
2135db2a4165SFrank Mandarino }
2136db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2137db2a4165SFrank Mandarino 
2138db2a4165SFrank Mandarino /**
21396c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
21406c508c62SEero Nurkkala  * @codec: audio codec
21416c508c62SEero Nurkkala  * @reg: codec register
21426c508c62SEero Nurkkala  * @mask: register mask
21436c508c62SEero Nurkkala  * @value: new value
21446c508c62SEero Nurkkala  *
21456c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
21466c508c62SEero Nurkkala  *
21476c508c62SEero Nurkkala  * Returns 1 for change else 0.
21486c508c62SEero Nurkkala  */
2149dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
21506c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
21516c508c62SEero Nurkkala 			       unsigned int value)
21526c508c62SEero Nurkkala {
21536c508c62SEero Nurkkala 	int change;
21546c508c62SEero Nurkkala 
21556c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
21566c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
21576c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
21586c508c62SEero Nurkkala 
21596c508c62SEero Nurkkala 	return change;
21606c508c62SEero Nurkkala }
2161dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
21626c508c62SEero Nurkkala 
21636c508c62SEero Nurkkala /**
2164db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2165db2a4165SFrank Mandarino  * @codec: audio codec
2166db2a4165SFrank Mandarino  * @reg: codec register
2167db2a4165SFrank Mandarino  * @mask: register mask
2168db2a4165SFrank Mandarino  * @value: new value
2169db2a4165SFrank Mandarino  *
2170db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2171db2a4165SFrank Mandarino  * different from the old value.
2172db2a4165SFrank Mandarino  *
2173db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2174db2a4165SFrank Mandarino  */
2175db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
217646f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2177db2a4165SFrank Mandarino {
2178db2a4165SFrank Mandarino 	int change;
217946f5822fSDaniel Ribeiro 	unsigned int old, new;
2180db2a4165SFrank Mandarino 
2181db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2182db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2183db2a4165SFrank Mandarino 	change = old != new;
2184db2a4165SFrank Mandarino 
2185db2a4165SFrank Mandarino 	return change;
2186db2a4165SFrank Mandarino }
2187db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2188db2a4165SFrank Mandarino 
2189db2a4165SFrank Mandarino /**
2190db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2191db2a4165SFrank Mandarino  * @substream: the pcm substream
2192db2a4165SFrank Mandarino  * @hw: the hardware parameters
2193db2a4165SFrank Mandarino  *
2194db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2195db2a4165SFrank Mandarino  */
2196db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2197db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2198db2a4165SFrank Mandarino {
2199db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2200db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2201db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2202db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2203db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2204db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2205db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2206db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2207db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2208db2a4165SFrank Mandarino 	return 0;
2209db2a4165SFrank Mandarino }
2210db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2211db2a4165SFrank Mandarino 
2212db2a4165SFrank Mandarino /**
2213db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2214db2a4165SFrank Mandarino  * @_template: control template
2215db2a4165SFrank Mandarino  * @data: control private data
2216ac11a2b3SMark Brown  * @long_name: control long name
2217efb7ac3fSMark Brown  * @prefix: control name prefix
2218db2a4165SFrank Mandarino  *
2219db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2220db2a4165SFrank Mandarino  *
2221db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2222db2a4165SFrank Mandarino  */
2223db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22243056557fSMark Brown 				  void *data, const char *long_name,
2225efb7ac3fSMark Brown 				  const char *prefix)
2226db2a4165SFrank Mandarino {
2227db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2228efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2229efb7ac3fSMark Brown 	char *name = NULL;
2230efb7ac3fSMark Brown 	int name_len;
2231db2a4165SFrank Mandarino 
2232db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2233db2a4165SFrank Mandarino 	template.index = 0;
2234db2a4165SFrank Mandarino 
2235efb7ac3fSMark Brown 	if (!long_name)
2236efb7ac3fSMark Brown 		long_name = template.name;
2237efb7ac3fSMark Brown 
2238efb7ac3fSMark Brown 	if (prefix) {
2239efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
224057cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2241efb7ac3fSMark Brown 		if (!name)
2242efb7ac3fSMark Brown 			return NULL;
2243efb7ac3fSMark Brown 
2244efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2245efb7ac3fSMark Brown 
2246efb7ac3fSMark Brown 		template.name = name;
2247efb7ac3fSMark Brown 	} else {
2248efb7ac3fSMark Brown 		template.name = long_name;
2249efb7ac3fSMark Brown 	}
2250efb7ac3fSMark Brown 
2251efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2252efb7ac3fSMark Brown 
2253efb7ac3fSMark Brown 	kfree(name);
2254efb7ac3fSMark Brown 
2255efb7ac3fSMark Brown 	return kcontrol;
2256db2a4165SFrank Mandarino }
2257db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2258db2a4165SFrank Mandarino 
2259022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2260022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2261022658beSLiam Girdwood 	const char *prefix, void *data)
2262022658beSLiam Girdwood {
2263022658beSLiam Girdwood 	int err, i;
2264022658beSLiam Girdwood 
2265022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2266022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2267022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2268022658beSLiam Girdwood 						     control->name, prefix));
2269022658beSLiam Girdwood 		if (err < 0) {
2270022658beSLiam Girdwood 			dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2271022658beSLiam Girdwood 			return err;
2272022658beSLiam Girdwood 		}
2273022658beSLiam Girdwood 	}
2274022658beSLiam Girdwood 
2275022658beSLiam Girdwood 	return 0;
2276022658beSLiam Girdwood }
2277022658beSLiam Girdwood 
2278db2a4165SFrank Mandarino /**
2279022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2280022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
22813e8e1952SIan Molton  * duplicating this code.
22823e8e1952SIan Molton  *
22833e8e1952SIan Molton  * @codec: codec to add controls to
22843e8e1952SIan Molton  * @controls: array of controls to add
22853e8e1952SIan Molton  * @num_controls: number of elements in the array
22863e8e1952SIan Molton  *
22873e8e1952SIan Molton  * Return 0 for success, else error.
22883e8e1952SIan Molton  */
2289022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
22903e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
22913e8e1952SIan Molton {
2292f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
22933e8e1952SIan Molton 
2294022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2295022658beSLiam Girdwood 			codec->name_prefix, codec);
22963e8e1952SIan Molton }
2297022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
22983e8e1952SIan Molton 
22993e8e1952SIan Molton /**
2300a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2301022658beSLiam Girdwood  * Convenience function to add a list of controls.
2302a491a5c8SLiam Girdwood  *
2303a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2304a491a5c8SLiam Girdwood  * @controls: array of controls to add
2305a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2306a491a5c8SLiam Girdwood  *
2307a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2308a491a5c8SLiam Girdwood  */
2309a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2310a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2311a491a5c8SLiam Girdwood {
2312a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2313a491a5c8SLiam Girdwood 
2314022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2315022658beSLiam Girdwood 			NULL, platform);
2316a491a5c8SLiam Girdwood }
2317a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2318a491a5c8SLiam Girdwood 
2319a491a5c8SLiam Girdwood /**
2320022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2321022658beSLiam Girdwood  * Convenience function to add a list of controls.
2322022658beSLiam Girdwood  *
2323022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2324022658beSLiam Girdwood  * @controls: array of controls to add
2325022658beSLiam Girdwood  * @num_controls: number of elements in the array
2326022658beSLiam Girdwood  *
2327022658beSLiam Girdwood  * Return 0 for success, else error.
2328022658beSLiam Girdwood  */
2329022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2330022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2331022658beSLiam Girdwood {
2332022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2333022658beSLiam Girdwood 
2334022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2335022658beSLiam Girdwood 			NULL, soc_card);
2336022658beSLiam Girdwood }
2337022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2338022658beSLiam Girdwood 
2339022658beSLiam Girdwood /**
2340022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2341022658beSLiam Girdwood  * Convienience function to add a list of controls.
2342022658beSLiam Girdwood  *
2343022658beSLiam Girdwood  * @dai: DAI to add controls to
2344022658beSLiam Girdwood  * @controls: array of controls to add
2345022658beSLiam Girdwood  * @num_controls: number of elements in the array
2346022658beSLiam Girdwood  *
2347022658beSLiam Girdwood  * Return 0 for success, else error.
2348022658beSLiam Girdwood  */
2349022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2350022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2351022658beSLiam Girdwood {
2352022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2353022658beSLiam Girdwood 
2354022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2355022658beSLiam Girdwood 			NULL, dai);
2356022658beSLiam Girdwood }
2357022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2358022658beSLiam Girdwood 
2359022658beSLiam Girdwood /**
2360db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2361db2a4165SFrank Mandarino  * @kcontrol: mixer control
2362db2a4165SFrank Mandarino  * @uinfo: control element information
2363db2a4165SFrank Mandarino  *
2364db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2365db2a4165SFrank Mandarino  * mixer control.
2366db2a4165SFrank Mandarino  *
2367db2a4165SFrank Mandarino  * Returns 0 for success.
2368db2a4165SFrank Mandarino  */
2369db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2370db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2371db2a4165SFrank Mandarino {
2372db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2373db2a4165SFrank Mandarino 
2374db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2375db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2376f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2377db2a4165SFrank Mandarino 
2378f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2379f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2380db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2381db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2382db2a4165SFrank Mandarino 	return 0;
2383db2a4165SFrank Mandarino }
2384db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2385db2a4165SFrank Mandarino 
2386db2a4165SFrank Mandarino /**
2387db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2388db2a4165SFrank Mandarino  * @kcontrol: mixer control
2389ac11a2b3SMark Brown  * @ucontrol: control element information
2390db2a4165SFrank Mandarino  *
2391db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2392db2a4165SFrank Mandarino  *
2393db2a4165SFrank Mandarino  * Returns 0 for success.
2394db2a4165SFrank Mandarino  */
2395db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2396db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2397db2a4165SFrank Mandarino {
2398db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2399db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
240046f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2401db2a4165SFrank Mandarino 
2402f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2403db2a4165SFrank Mandarino 		;
2404db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
24053ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
24063ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2407db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2408db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2409db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2410db2a4165SFrank Mandarino 
2411db2a4165SFrank Mandarino 	return 0;
2412db2a4165SFrank Mandarino }
2413db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2414db2a4165SFrank Mandarino 
2415db2a4165SFrank Mandarino /**
2416db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2417db2a4165SFrank Mandarino  * @kcontrol: mixer control
2418ac11a2b3SMark Brown  * @ucontrol: control element information
2419db2a4165SFrank Mandarino  *
2420db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2421db2a4165SFrank Mandarino  *
2422db2a4165SFrank Mandarino  * Returns 0 for success.
2423db2a4165SFrank Mandarino  */
2424db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2425db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2426db2a4165SFrank Mandarino {
2427db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2428db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
242946f5822fSDaniel Ribeiro 	unsigned int val;
243046f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2431db2a4165SFrank Mandarino 
2432f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2433db2a4165SFrank Mandarino 		;
2434f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2435db2a4165SFrank Mandarino 		return -EINVAL;
2436db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2437db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2438db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2439f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2440db2a4165SFrank Mandarino 			return -EINVAL;
2441db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2442db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2443db2a4165SFrank Mandarino 	}
2444db2a4165SFrank Mandarino 
24456c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2446db2a4165SFrank Mandarino }
2447db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2448db2a4165SFrank Mandarino 
2449db2a4165SFrank Mandarino /**
24502e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
24512e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
24522e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
24532e72f8e3SPeter Ujfalusi  *
24542e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
24552e72f8e3SPeter Ujfalusi  *
24562e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24572e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24582e72f8e3SPeter Ujfalusi  *
24592e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24602e72f8e3SPeter Ujfalusi  */
24612e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
24622e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
24632e72f8e3SPeter Ujfalusi {
24642e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
246574155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
246646f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
24672e72f8e3SPeter Ujfalusi 
24682e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
24692e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
24702e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
24712e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
24722e72f8e3SPeter Ujfalusi 			break;
24732e72f8e3SPeter Ujfalusi 	}
24742e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
24752e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24762e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
24772e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
24782e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
24792e72f8e3SPeter Ujfalusi 				break;
24802e72f8e3SPeter Ujfalusi 		}
24812e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
24822e72f8e3SPeter Ujfalusi 	}
24832e72f8e3SPeter Ujfalusi 
24842e72f8e3SPeter Ujfalusi 	return 0;
24852e72f8e3SPeter Ujfalusi }
24862e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
24872e72f8e3SPeter Ujfalusi 
24882e72f8e3SPeter Ujfalusi /**
24892e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
24902e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
24912e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
24922e72f8e3SPeter Ujfalusi  *
24932e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
24942e72f8e3SPeter Ujfalusi  *
24952e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24962e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24972e72f8e3SPeter Ujfalusi  *
24982e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24992e72f8e3SPeter Ujfalusi  */
25002e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
25012e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
25022e72f8e3SPeter Ujfalusi {
25032e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
250474155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
250546f5822fSDaniel Ribeiro 	unsigned int val;
250646f5822fSDaniel Ribeiro 	unsigned int mask;
25072e72f8e3SPeter Ujfalusi 
25082e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
25092e72f8e3SPeter Ujfalusi 		return -EINVAL;
25102e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
25112e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
25122e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
25132e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
25142e72f8e3SPeter Ujfalusi 			return -EINVAL;
25152e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
25162e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
25172e72f8e3SPeter Ujfalusi 	}
25182e72f8e3SPeter Ujfalusi 
25196c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
25202e72f8e3SPeter Ujfalusi }
25212e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
25222e72f8e3SPeter Ujfalusi 
25232e72f8e3SPeter Ujfalusi /**
2524db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2525db2a4165SFrank Mandarino  * @kcontrol: mixer control
2526db2a4165SFrank Mandarino  * @uinfo: control element information
2527db2a4165SFrank Mandarino  *
2528db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2529db2a4165SFrank Mandarino  * single mixer.
2530db2a4165SFrank Mandarino  *
2531db2a4165SFrank Mandarino  * Returns 0 for success.
2532db2a4165SFrank Mandarino  */
2533db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2534db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2535db2a4165SFrank Mandarino {
2536db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2537db2a4165SFrank Mandarino 
2538db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2539db2a4165SFrank Mandarino 	uinfo->count = 1;
2540f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2541db2a4165SFrank Mandarino 
2542f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2543f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2544db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2545db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2546db2a4165SFrank Mandarino 	return 0;
2547db2a4165SFrank Mandarino }
2548db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2549db2a4165SFrank Mandarino 
2550db2a4165SFrank Mandarino /**
2551db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2552db2a4165SFrank Mandarino  * @kcontrol: mixer control
2553db2a4165SFrank Mandarino  * @uinfo: control element information
2554db2a4165SFrank Mandarino  *
2555db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2556db2a4165SFrank Mandarino  *
2557db2a4165SFrank Mandarino  * Returns 0 for success.
2558db2a4165SFrank Mandarino  */
2559db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2560db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2561db2a4165SFrank Mandarino {
2562a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2563db2a4165SFrank Mandarino 
2564fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2565a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2566a7a4ac86SPhilipp Zabel 	else
2567a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2568a7a4ac86SPhilipp Zabel 
2569db2a4165SFrank Mandarino 	uinfo->count = 1;
2570db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2571a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2572db2a4165SFrank Mandarino 	return 0;
2573db2a4165SFrank Mandarino }
2574db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2575db2a4165SFrank Mandarino 
2576db2a4165SFrank Mandarino /**
2577db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2578db2a4165SFrank Mandarino  * @kcontrol: mixer control
2579db2a4165SFrank Mandarino  * @uinfo: control element information
2580db2a4165SFrank Mandarino  *
2581e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2582e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2583db2a4165SFrank Mandarino  *
2584db2a4165SFrank Mandarino  * Returns 0 for success.
2585db2a4165SFrank Mandarino  */
2586db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2587db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2588db2a4165SFrank Mandarino {
25894eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25904eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2591d11bb4a9SPeter Ujfalusi 	int platform_max;
2592db2a4165SFrank Mandarino 
2593d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2594d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2595d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2596d11bb4a9SPeter Ujfalusi 
2597d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2598a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2599a7a4ac86SPhilipp Zabel 	else
2600a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2601a7a4ac86SPhilipp Zabel 
2602e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2603db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2604d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2605db2a4165SFrank Mandarino 	return 0;
2606db2a4165SFrank Mandarino }
2607db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2608db2a4165SFrank Mandarino 
2609db2a4165SFrank Mandarino /**
2610db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2611db2a4165SFrank Mandarino  * @kcontrol: mixer control
2612ac11a2b3SMark Brown  * @ucontrol: control element information
2613db2a4165SFrank Mandarino  *
2614f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2615f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2616db2a4165SFrank Mandarino  *
2617db2a4165SFrank Mandarino  * Returns 0 for success.
2618db2a4165SFrank Mandarino  */
2619db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2620db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2621db2a4165SFrank Mandarino {
26224eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26234eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2624db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2626f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2627815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2628815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26294eaa9819SJon Smirl 	int max = mc->max;
2630815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2631815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2632db2a4165SFrank Mandarino 
2633db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2634db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2635f7915d99SPeter Ujfalusi 	if (invert)
2636db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2637a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2638f7915d99SPeter Ujfalusi 
2639f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2640f7915d99SPeter Ujfalusi 		if (reg == reg2)
2641f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2642f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2643f7915d99SPeter Ujfalusi 		else
2644f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2645f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2646f7915d99SPeter Ujfalusi 		if (invert)
2647db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2648a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2649db2a4165SFrank Mandarino 	}
2650db2a4165SFrank Mandarino 
2651db2a4165SFrank Mandarino 	return 0;
2652db2a4165SFrank Mandarino }
2653db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2654db2a4165SFrank Mandarino 
2655db2a4165SFrank Mandarino /**
2656db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2657db2a4165SFrank Mandarino  * @kcontrol: mixer control
2658ac11a2b3SMark Brown  * @ucontrol: control element information
2659db2a4165SFrank Mandarino  *
2660974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2661974815baSPeter Ujfalusi  * control that spans 2 registers.
2662db2a4165SFrank Mandarino  *
2663db2a4165SFrank Mandarino  * Returns 0 for success.
2664db2a4165SFrank Mandarino  */
2665db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2666db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2667db2a4165SFrank Mandarino {
26684eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26694eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2670db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2671815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2672974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2673815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2674815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26754eaa9819SJon Smirl 	int max = mc->max;
2676815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2677815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2678974815baSPeter Ujfalusi 	int err;
2679974815baSPeter Ujfalusi 	bool type_2r = 0;
2680974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2681974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2682db2a4165SFrank Mandarino 
2683db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2684db2a4165SFrank Mandarino 	if (invert)
2685a7a4ac86SPhilipp Zabel 		val = max - val;
2686db2a4165SFrank Mandarino 	val_mask = mask << shift;
2687db2a4165SFrank Mandarino 	val = val << shift;
2688974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2689db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2690db2a4165SFrank Mandarino 		if (invert)
2691a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2692974815baSPeter Ujfalusi 		if (reg == reg2) {
2693db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2694db2a4165SFrank Mandarino 			val |= val2 << rshift;
2695974815baSPeter Ujfalusi 		} else {
2696db2a4165SFrank Mandarino 			val2 = val2 << shift;
2697974815baSPeter Ujfalusi 			type_2r = 1;
2698974815baSPeter Ujfalusi 		}
2699974815baSPeter Ujfalusi 	}
27006c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
27013ff3f64bSMark Brown 	if (err < 0)
2702db2a4165SFrank Mandarino 		return err;
2703db2a4165SFrank Mandarino 
2704974815baSPeter Ujfalusi 	if (type_2r)
27056c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2706974815baSPeter Ujfalusi 
2707db2a4165SFrank Mandarino 	return err;
2708db2a4165SFrank Mandarino }
2709974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2710db2a4165SFrank Mandarino 
2711e13ac2e9SMark Brown /**
27121d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
27131d99f243SBrian Austin  * @kcontrol: mixer control
27141d99f243SBrian Austin  * @ucontrol: control element information
27151d99f243SBrian Austin  *
27161d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
27171d99f243SBrian Austin  * control that spans 2 registers.
27181d99f243SBrian Austin  *
27191d99f243SBrian Austin  * Returns 0 for success.
27201d99f243SBrian Austin  */
27211d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
27221d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
27231d99f243SBrian Austin {
27241d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
27251d99f243SBrian Austin 	struct soc_mixer_control *mc =
27261d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
27271d99f243SBrian Austin 
27281d99f243SBrian Austin 	unsigned int reg = mc->reg;
27291d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
27301d99f243SBrian Austin 	unsigned int shift = mc->shift;
27311d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
27321d99f243SBrian Austin 	int max = mc->max;
27331d99f243SBrian Austin 	int min = mc->min;
27341d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
27351d99f243SBrian Austin 
27361d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
27371d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
27381d99f243SBrian Austin 
27391d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
27401d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
27411d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
27421d99f243SBrian Austin 
27431d99f243SBrian Austin 	return 0;
27441d99f243SBrian Austin }
27451d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
27461d99f243SBrian Austin 
27471d99f243SBrian Austin /**
27481d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
27491d99f243SBrian Austin  * @kcontrol: mixer control
27501d99f243SBrian Austin  * @uinfo: control element information
27511d99f243SBrian Austin  *
27521d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
27531d99f243SBrian Austin  *
27541d99f243SBrian Austin  * Returns 0 for success.
27551d99f243SBrian Austin  */
27561d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
27571d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
27581d99f243SBrian Austin {
27591d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
27601d99f243SBrian Austin 	struct soc_mixer_control *mc =
27611d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
27621d99f243SBrian Austin 
27631d99f243SBrian Austin 	unsigned int reg = mc->reg;
27641d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
27651d99f243SBrian Austin 	unsigned int shift = mc->shift;
27661d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
27671d99f243SBrian Austin 	int max = mc->max;
27681d99f243SBrian Austin 	int min = mc->min;
27691d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
277027f1d759SBrian Austin 	int err = 0;
27711d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
27721d99f243SBrian Austin 
27731d99f243SBrian Austin 	val_mask = mask << shift;
27741d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
27751d99f243SBrian Austin 	val = val << shift;
27761d99f243SBrian Austin 
27771d99f243SBrian Austin 	if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
27781d99f243SBrian Austin 			return err;
27791d99f243SBrian Austin 
27801d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
27811d99f243SBrian Austin 		val_mask = mask << rshift;
27821d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
27831d99f243SBrian Austin 		val2 = val2 << rshift;
27841d99f243SBrian Austin 
27851d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
27861d99f243SBrian Austin 			return err;
27871d99f243SBrian Austin 	}
27881d99f243SBrian Austin 	return 0;
27891d99f243SBrian Austin }
27901d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
27911d99f243SBrian Austin 
27921d99f243SBrian Austin /**
2793e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2794e13ac2e9SMark Brown  * @kcontrol: mixer control
2795e13ac2e9SMark Brown  * @uinfo: control element information
2796e13ac2e9SMark Brown  *
2797e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2798e13ac2e9SMark Brown  *
2799e13ac2e9SMark Brown  * Returns 0 for success.
2800e13ac2e9SMark Brown  */
2801e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2802e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2803e13ac2e9SMark Brown {
28044eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28054eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2806d11bb4a9SPeter Ujfalusi 	int platform_max;
28074eaa9819SJon Smirl 	int min = mc->min;
2808e13ac2e9SMark Brown 
2809d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2810d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2811d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2812d11bb4a9SPeter Ujfalusi 
2813e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2814e13ac2e9SMark Brown 	uinfo->count = 2;
2815e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2816d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2817e13ac2e9SMark Brown 	return 0;
2818e13ac2e9SMark Brown }
2819e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2820e13ac2e9SMark Brown 
2821e13ac2e9SMark Brown /**
2822e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2823e13ac2e9SMark Brown  * @kcontrol: mixer control
2824ac11a2b3SMark Brown  * @ucontrol: control element information
2825e13ac2e9SMark Brown  *
2826e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2827e13ac2e9SMark Brown  *
2828e13ac2e9SMark Brown  * Returns 0 for success.
2829e13ac2e9SMark Brown  */
2830e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2831e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2832e13ac2e9SMark Brown {
28334eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28344eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2835e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2836815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
28374eaa9819SJon Smirl 	int min = mc->min;
2838e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2839e13ac2e9SMark Brown 
2840e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2841e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2842e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2843e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2844e13ac2e9SMark Brown 	return 0;
2845e13ac2e9SMark Brown }
2846e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2847e13ac2e9SMark Brown 
2848e13ac2e9SMark Brown /**
2849e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2850e13ac2e9SMark Brown  * @kcontrol: mixer control
2851ac11a2b3SMark Brown  * @ucontrol: control element information
2852e13ac2e9SMark Brown  *
2853e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2854e13ac2e9SMark Brown  *
2855e13ac2e9SMark Brown  * Returns 0 for success.
2856e13ac2e9SMark Brown  */
2857e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2858e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2859e13ac2e9SMark Brown {
28604eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28614eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2862e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2863815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
28644eaa9819SJon Smirl 	int min = mc->min;
286546f5822fSDaniel Ribeiro 	unsigned int val;
2866e13ac2e9SMark Brown 
2867e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2868e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2869e13ac2e9SMark Brown 
28706c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2871e13ac2e9SMark Brown }
2872e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2873e13ac2e9SMark Brown 
28748c6529dbSLiam Girdwood /**
28756c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
28766c9d8cf6SAdam Thomson  * @kcontrol: mixer control
28776c9d8cf6SAdam Thomson  * @uinfo: control element information
28786c9d8cf6SAdam Thomson  *
28796c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
28806c9d8cf6SAdam Thomson  * mixer control.
28816c9d8cf6SAdam Thomson  *
28826c9d8cf6SAdam Thomson  * returns 0 for success.
28836c9d8cf6SAdam Thomson  */
28846c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
28856c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
28866c9d8cf6SAdam Thomson {
28876c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
28886c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
28896c9d8cf6SAdam Thomson 	int platform_max;
28906c9d8cf6SAdam Thomson 	int min = mc->min;
28916c9d8cf6SAdam Thomson 
28926c9d8cf6SAdam Thomson 	if (!mc->platform_max)
28936c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
28946c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
28956c9d8cf6SAdam Thomson 
28966c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
28976c9d8cf6SAdam Thomson 	uinfo->count = 1;
28986c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
28996c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
29006c9d8cf6SAdam Thomson 
29016c9d8cf6SAdam Thomson 	return 0;
29026c9d8cf6SAdam Thomson }
29036c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
29046c9d8cf6SAdam Thomson 
29056c9d8cf6SAdam Thomson /**
29066c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
29076c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29086c9d8cf6SAdam Thomson  * @ucontrol: control element information
29096c9d8cf6SAdam Thomson  *
29106c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
29116c9d8cf6SAdam Thomson  *
29126c9d8cf6SAdam Thomson  * Returns 0 for success.
29136c9d8cf6SAdam Thomson  */
29146c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
29156c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
29166c9d8cf6SAdam Thomson {
29176c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29186c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29196c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29206c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
29216c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
29226c9d8cf6SAdam Thomson 	int min = mc->min;
29236c9d8cf6SAdam Thomson 	int max = mc->max;
29246c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
29256c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
29266c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
29276c9d8cf6SAdam Thomson 
29286c9d8cf6SAdam Thomson 	val = ((ucontrol->value.integer.value[0] + min) & mask);
29296c9d8cf6SAdam Thomson 	if (invert)
29306c9d8cf6SAdam Thomson 		val = max - val;
29316c9d8cf6SAdam Thomson 	val_mask = mask << shift;
29326c9d8cf6SAdam Thomson 	val = val << shift;
29336c9d8cf6SAdam Thomson 
29346c9d8cf6SAdam Thomson 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
29356c9d8cf6SAdam Thomson }
29366c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
29376c9d8cf6SAdam Thomson 
29386c9d8cf6SAdam Thomson /**
29396c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
29406c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29416c9d8cf6SAdam Thomson  * @ucontrol: control element information
29426c9d8cf6SAdam Thomson  *
29436c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
29446c9d8cf6SAdam Thomson  *
29456c9d8cf6SAdam Thomson  * Returns 0 for success.
29466c9d8cf6SAdam Thomson  */
29476c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
29486c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
29496c9d8cf6SAdam Thomson {
29506c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29516c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29526c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29536c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
29546c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
29556c9d8cf6SAdam Thomson 	int min = mc->min;
29566c9d8cf6SAdam Thomson 	int max = mc->max;
29576c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
29586c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
29596c9d8cf6SAdam Thomson 
29606c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
29616c9d8cf6SAdam Thomson 		(snd_soc_read(codec, reg) >> shift) & mask;
29626c9d8cf6SAdam Thomson 	if (invert)
29636c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
29646c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
29656c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
29666c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] - min;
29676c9d8cf6SAdam Thomson 
29686c9d8cf6SAdam Thomson 	return 0;
29696c9d8cf6SAdam Thomson }
29706c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
29716c9d8cf6SAdam Thomson 
29726c9d8cf6SAdam Thomson /**
2973637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2974637d3847SPeter Ujfalusi  *
2975637d3847SPeter Ujfalusi  * @codec: where to look for the control
2976637d3847SPeter Ujfalusi  * @name: Name of the control
2977637d3847SPeter Ujfalusi  * @max: new maximum limit
2978637d3847SPeter Ujfalusi  *
2979637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2980637d3847SPeter Ujfalusi  */
2981637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2982637d3847SPeter Ujfalusi 	const char *name, int max)
2983637d3847SPeter Ujfalusi {
2984f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2985637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2986637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2987637d3847SPeter Ujfalusi 	int found = 0;
2988637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2989637d3847SPeter Ujfalusi 
2990637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2991637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2992637d3847SPeter Ujfalusi 		return -EINVAL;
2993637d3847SPeter Ujfalusi 
2994637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2995637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2996637d3847SPeter Ujfalusi 			found = 1;
2997637d3847SPeter Ujfalusi 			break;
2998637d3847SPeter Ujfalusi 		}
2999637d3847SPeter Ujfalusi 	}
3000637d3847SPeter Ujfalusi 	if (found) {
3001637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3002637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3003d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3004637d3847SPeter Ujfalusi 			ret = 0;
3005637d3847SPeter Ujfalusi 		}
3006637d3847SPeter Ujfalusi 	}
3007637d3847SPeter Ujfalusi 	return ret;
3008637d3847SPeter Ujfalusi }
3009637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3010637d3847SPeter Ujfalusi 
301171d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
301271d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
301371d08516SMark Brown {
301471d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
301571d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
301671d08516SMark Brown 
301771d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
301871d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
301971d08516SMark Brown 
302071d08516SMark Brown 	return 0;
302171d08516SMark Brown }
302271d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
302371d08516SMark Brown 
302471d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
302571d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
302671d08516SMark Brown {
302771d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
302871d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
302971d08516SMark Brown 	int ret;
303071d08516SMark Brown 
303171d08516SMark Brown 	if (codec->using_regmap)
303271d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
303371d08516SMark Brown 				      ucontrol->value.bytes.data,
303471d08516SMark Brown 				      params->num_regs * codec->val_bytes);
303571d08516SMark Brown 	else
303671d08516SMark Brown 		ret = -EINVAL;
303771d08516SMark Brown 
3038f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
3039f831b055SMark Brown 	if (ret == 0 && params->mask) {
3040f831b055SMark Brown 		switch (codec->val_bytes) {
3041f831b055SMark Brown 		case 1:
3042f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
3043f831b055SMark Brown 			break;
3044f831b055SMark Brown 		case 2:
3045f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
3046f831b055SMark Brown 				&= ~params->mask;
3047f831b055SMark Brown 			break;
3048f831b055SMark Brown 		case 4:
3049f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
3050f831b055SMark Brown 				&= ~params->mask;
3051f831b055SMark Brown 			break;
3052f831b055SMark Brown 		default:
3053f831b055SMark Brown 			return -EINVAL;
3054f831b055SMark Brown 		}
3055f831b055SMark Brown 	}
3056f831b055SMark Brown 
305771d08516SMark Brown 	return ret;
305871d08516SMark Brown }
305971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
306071d08516SMark Brown 
306171d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
306271d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
306371d08516SMark Brown {
306471d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
306571d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3066f831b055SMark Brown 	int ret, len;
3067f831b055SMark Brown 	unsigned int val;
3068f831b055SMark Brown 	void *data;
306971d08516SMark Brown 
3070f831b055SMark Brown 	if (!codec->using_regmap)
3071f831b055SMark Brown 		return -EINVAL;
3072f831b055SMark Brown 
3073f831b055SMark Brown 	data = ucontrol->value.bytes.data;
3074f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
3075f831b055SMark Brown 
3076f831b055SMark Brown 	/*
3077f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3078f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3079f831b055SMark Brown 	 * copy.
3080f831b055SMark Brown 	 */
3081f831b055SMark Brown 	if (params->mask) {
3082f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
3083f831b055SMark Brown 		if (ret != 0)
3084f831b055SMark Brown 			return ret;
3085f831b055SMark Brown 
3086f831b055SMark Brown 		val &= params->mask;
3087f831b055SMark Brown 
3088f831b055SMark Brown 		data = kmemdup(data, len, GFP_KERNEL);
3089f831b055SMark Brown 		if (!data)
3090f831b055SMark Brown 			return -ENOMEM;
3091f831b055SMark Brown 
3092f831b055SMark Brown 		switch (codec->val_bytes) {
3093f831b055SMark Brown 		case 1:
3094f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3095f831b055SMark Brown 			((u8 *)data)[0] |= val;
3096f831b055SMark Brown 			break;
3097f831b055SMark Brown 		case 2:
3098f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3099f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
3100f831b055SMark Brown 			break;
3101f831b055SMark Brown 		case 4:
3102f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3103f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
3104f831b055SMark Brown 			break;
3105f831b055SMark Brown 		default:
3106f831b055SMark Brown 			return -EINVAL;
3107f831b055SMark Brown 		}
3108f831b055SMark Brown 	}
3109f831b055SMark Brown 
311071d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
3111f831b055SMark Brown 			       data, len);
3112f831b055SMark Brown 
3113f831b055SMark Brown 	if (params->mask)
3114f831b055SMark Brown 		kfree(data);
311571d08516SMark Brown 
311671d08516SMark Brown 	return ret;
311771d08516SMark Brown }
311871d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
311971d08516SMark Brown 
3120b6f4bb38Sapatard@mandriva.com /**
31214183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
31224183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
31234183eed2SKristoffer KARLSSON  * @uinfo: control element information
31244183eed2SKristoffer KARLSSON  *
31254183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
31264183eed2SKristoffer KARLSSON  * span multiple codec registers which together
31274183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
31284183eed2SKristoffer KARLSSON  *
31294183eed2SKristoffer KARLSSON  * Returns 0 for success.
31304183eed2SKristoffer KARLSSON  */
31314183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
31324183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
31334183eed2SKristoffer KARLSSON {
31344183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
31354183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
31364183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
31374183eed2SKristoffer KARLSSON 	uinfo->count = 1;
31384183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
31394183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
31404183eed2SKristoffer KARLSSON 
31414183eed2SKristoffer KARLSSON 	return 0;
31424183eed2SKristoffer KARLSSON }
31434183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
31444183eed2SKristoffer KARLSSON 
31454183eed2SKristoffer KARLSSON /**
31464183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
31474183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
31484183eed2SKristoffer KARLSSON  * @ucontrol: control element information
31494183eed2SKristoffer KARLSSON  *
31504183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
31514183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
31524183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
31534183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
31544183eed2SKristoffer KARLSSON  * across the multiple codec registers.
31554183eed2SKristoffer KARLSSON  *
31564183eed2SKristoffer KARLSSON  * Returns 0 for success.
31574183eed2SKristoffer KARLSSON  */
31584183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
31594183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
31604183eed2SKristoffer KARLSSON {
31614183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
31624183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
31634183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
31644183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
31654183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
31664183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
31674183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
31684183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
31694183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
31704183eed2SKristoffer KARLSSON 	long min = mc->min;
31714183eed2SKristoffer KARLSSON 	long max = mc->max;
31724183eed2SKristoffer KARLSSON 	long val = 0;
31734183eed2SKristoffer KARLSSON 	unsigned long regval;
31744183eed2SKristoffer KARLSSON 	unsigned int i;
31754183eed2SKristoffer KARLSSON 
31764183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
31774183eed2SKristoffer KARLSSON 		regval = snd_soc_read(codec, regbase+i) & regwmask;
31784183eed2SKristoffer KARLSSON 		val |= regval << (regwshift*(regcount-i-1));
31794183eed2SKristoffer KARLSSON 	}
31804183eed2SKristoffer KARLSSON 	val &= mask;
31814183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
31824183eed2SKristoffer KARLSSON 		val |= ~mask;
31834183eed2SKristoffer KARLSSON 	if (invert)
31844183eed2SKristoffer KARLSSON 		val = max - val;
31854183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
31864183eed2SKristoffer KARLSSON 
31874183eed2SKristoffer KARLSSON 	return 0;
31884183eed2SKristoffer KARLSSON }
31894183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
31904183eed2SKristoffer KARLSSON 
31914183eed2SKristoffer KARLSSON /**
31924183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
31934183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
31944183eed2SKristoffer KARLSSON  * @ucontrol: control element information
31954183eed2SKristoffer KARLSSON  *
31964183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
31974183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
31984183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
31994183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
32004183eed2SKristoffer KARLSSON  * across the multiple codec registers.
32014183eed2SKristoffer KARLSSON  *
32024183eed2SKristoffer KARLSSON  * Returns 0 for success.
32034183eed2SKristoffer KARLSSON  */
32044183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
32054183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
32064183eed2SKristoffer KARLSSON {
32074183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
32084183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
32094183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
32104183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
32114183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
32124183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
32134183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
32144183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
32154183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
32164183eed2SKristoffer KARLSSON 	long max = mc->max;
32174183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
32184183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
32194183eed2SKristoffer KARLSSON 	int err;
32204183eed2SKristoffer KARLSSON 
32214183eed2SKristoffer KARLSSON 	if (invert)
32224183eed2SKristoffer KARLSSON 		val = max - val;
32234183eed2SKristoffer KARLSSON 	val &= mask;
32244183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
32254183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
32264183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
32274183eed2SKristoffer KARLSSON 		err = snd_soc_update_bits_locked(codec, regbase+i,
32284183eed2SKristoffer KARLSSON 				regmask, regval);
32294183eed2SKristoffer KARLSSON 		if (err < 0)
32304183eed2SKristoffer KARLSSON 			return err;
32314183eed2SKristoffer KARLSSON 	}
32324183eed2SKristoffer KARLSSON 
32334183eed2SKristoffer KARLSSON 	return 0;
32344183eed2SKristoffer KARLSSON }
32354183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
32364183eed2SKristoffer KARLSSON 
32374183eed2SKristoffer KARLSSON /**
3238dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3239dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3240dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3241dd7b10b3SKristoffer KARLSSON  *
3242dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3243dd7b10b3SKristoffer KARLSSON  *
3244dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3245dd7b10b3SKristoffer KARLSSON  */
3246dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3247dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3248dd7b10b3SKristoffer KARLSSON {
3249dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3250dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3251dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3252dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3253dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3254dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3255dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3256dd7b10b3SKristoffer KARLSSON 	unsigned int val = snd_soc_read(codec, reg) & mask;
3257dd7b10b3SKristoffer KARLSSON 
3258dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3259dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3260dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3261dd7b10b3SKristoffer KARLSSON 
3262dd7b10b3SKristoffer KARLSSON 	return 0;
3263dd7b10b3SKristoffer KARLSSON }
3264dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3265dd7b10b3SKristoffer KARLSSON 
3266dd7b10b3SKristoffer KARLSSON /**
3267dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3268dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3269dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3270dd7b10b3SKristoffer KARLSSON  *
3271dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3272dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3273dd7b10b3SKristoffer KARLSSON  *
3274dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3275dd7b10b3SKristoffer KARLSSON  */
3276dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3277dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3278dd7b10b3SKristoffer KARLSSON {
3279dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3280dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3281dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3282dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3283dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3284dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3285dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3286dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3287dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3288dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3289dd7b10b3SKristoffer KARLSSON 	int err;
3290dd7b10b3SKristoffer KARLSSON 
3291dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3292dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3293dd7b10b3SKristoffer KARLSSON 		return err;
3294dd7b10b3SKristoffer KARLSSON 
3295dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3296dd7b10b3SKristoffer KARLSSON 	return err;
3297dd7b10b3SKristoffer KARLSSON }
3298dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3299dd7b10b3SKristoffer KARLSSON 
3300dd7b10b3SKristoffer KARLSSON /**
33018c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
33028c6529dbSLiam Girdwood  * @dai: DAI
33038c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
33048c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
33058c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
33068c6529dbSLiam Girdwood  *
33078c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
33088c6529dbSLiam Girdwood  */
33098c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
33108c6529dbSLiam Girdwood 	unsigned int freq, int dir)
33118c6529dbSLiam Girdwood {
3312f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3313f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3314ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3315da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3316ec4ee52aSMark Brown 						      freq, dir);
33178c6529dbSLiam Girdwood 	else
33188c6529dbSLiam Girdwood 		return -EINVAL;
33198c6529dbSLiam Girdwood }
33208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
33218c6529dbSLiam Girdwood 
33228c6529dbSLiam Girdwood /**
3323ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3324ec4ee52aSMark Brown  * @codec: CODEC
3325ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3326da1c6ea6SMark Brown  * @source: Source for the clock
3327ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3328ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3329ec4ee52aSMark Brown  *
3330ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3331ec4ee52aSMark Brown  */
3332ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3333da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3334ec4ee52aSMark Brown {
3335ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3336da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3337da1c6ea6SMark Brown 						 freq, dir);
3338ec4ee52aSMark Brown 	else
3339ec4ee52aSMark Brown 		return -EINVAL;
3340ec4ee52aSMark Brown }
3341ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3342ec4ee52aSMark Brown 
3343ec4ee52aSMark Brown /**
33448c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
33458c6529dbSLiam Girdwood  * @dai: DAI
3346ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
33478c6529dbSLiam Girdwood  * @div: new clock divisor.
33488c6529dbSLiam Girdwood  *
33498c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
33508c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
33518c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
33528c6529dbSLiam Girdwood  */
33538c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
33548c6529dbSLiam Girdwood 	int div_id, int div)
33558c6529dbSLiam Girdwood {
3356f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3357f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
33588c6529dbSLiam Girdwood 	else
33598c6529dbSLiam Girdwood 		return -EINVAL;
33608c6529dbSLiam Girdwood }
33618c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
33628c6529dbSLiam Girdwood 
33638c6529dbSLiam Girdwood /**
33648c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
33658c6529dbSLiam Girdwood  * @dai: DAI
33668c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
336785488037SMark Brown  * @source: DAI specific source for the PLL
33688c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
33698c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
33708c6529dbSLiam Girdwood  *
33718c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
33728c6529dbSLiam Girdwood  */
337385488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
337485488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
33758c6529dbSLiam Girdwood {
3376f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3377f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
337885488037SMark Brown 					 freq_in, freq_out);
3379ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3380ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3381ec4ee52aSMark Brown 						   freq_in, freq_out);
33828c6529dbSLiam Girdwood 	else
33838c6529dbSLiam Girdwood 		return -EINVAL;
33848c6529dbSLiam Girdwood }
33858c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
33868c6529dbSLiam Girdwood 
3387ec4ee52aSMark Brown /*
3388ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3389ec4ee52aSMark Brown  * @codec: CODEC
3390ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3391ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3392ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3393ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3394ec4ee52aSMark Brown  *
3395ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3396ec4ee52aSMark Brown  */
3397ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3398ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3399ec4ee52aSMark Brown {
3400ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3401ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3402ec4ee52aSMark Brown 					      freq_in, freq_out);
3403ec4ee52aSMark Brown 	else
3404ec4ee52aSMark Brown 		return -EINVAL;
3405ec4ee52aSMark Brown }
3406ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3407ec4ee52aSMark Brown 
34088c6529dbSLiam Girdwood /**
34098c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
34108c6529dbSLiam Girdwood  * @dai: DAI
34118c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
34128c6529dbSLiam Girdwood  *
34138c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
34148c6529dbSLiam Girdwood  */
34158c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
34168c6529dbSLiam Girdwood {
34175e4ba569SShawn Guo 	if (dai->driver == NULL)
34188c6529dbSLiam Girdwood 		return -EINVAL;
34195e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
34205e4ba569SShawn Guo 		return -ENOTSUPP;
34215e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
34228c6529dbSLiam Girdwood }
34238c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
34248c6529dbSLiam Girdwood 
34258c6529dbSLiam Girdwood /**
34268c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
34278c6529dbSLiam Girdwood  * @dai: DAI
3428a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3429a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
34308c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3431a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
34328c6529dbSLiam Girdwood  *
34338c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
34348c6529dbSLiam Girdwood  * specific.
34358c6529dbSLiam Girdwood  */
34368c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3437a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
34388c6529dbSLiam Girdwood {
3439f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3440f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3441a5479e38SDaniel Ribeiro 				slots, slot_width);
34428c6529dbSLiam Girdwood 	else
34438c6529dbSLiam Girdwood 		return -EINVAL;
34448c6529dbSLiam Girdwood }
34458c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
34468c6529dbSLiam Girdwood 
34478c6529dbSLiam Girdwood /**
3448472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3449472df3cbSBarry Song  * @dai: DAI
3450472df3cbSBarry Song  * @tx_num: how many TX channels
3451472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3452472df3cbSBarry Song  *           0~num-1 uses
3453472df3cbSBarry Song  * @rx_num: how many RX channels
3454472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3455472df3cbSBarry Song  *           0~num-1 uses
3456472df3cbSBarry Song  *
3457472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3458472df3cbSBarry Song  */
3459472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3460472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3461472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3462472df3cbSBarry Song {
3463f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3464f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3465472df3cbSBarry Song 			rx_num, rx_slot);
3466472df3cbSBarry Song 	else
3467472df3cbSBarry Song 		return -EINVAL;
3468472df3cbSBarry Song }
3469472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3470472df3cbSBarry Song 
3471472df3cbSBarry Song /**
34728c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
34738c6529dbSLiam Girdwood  * @dai: DAI
34748c6529dbSLiam Girdwood  * @tristate: tristate enable
34758c6529dbSLiam Girdwood  *
34768c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
34778c6529dbSLiam Girdwood  */
34788c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
34798c6529dbSLiam Girdwood {
3480f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3481f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
34828c6529dbSLiam Girdwood 	else
34838c6529dbSLiam Girdwood 		return -EINVAL;
34848c6529dbSLiam Girdwood }
34858c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
34868c6529dbSLiam Girdwood 
34878c6529dbSLiam Girdwood /**
34888c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
34898c6529dbSLiam Girdwood  * @dai: DAI
34908c6529dbSLiam Girdwood  * @mute: mute enable
34918c6529dbSLiam Girdwood  *
34928c6529dbSLiam Girdwood  * Mutes the DAI DAC.
34938c6529dbSLiam Girdwood  */
34948c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
34958c6529dbSLiam Girdwood {
3496f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3497f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
34988c6529dbSLiam Girdwood 	else
349904570c62SMark Brown 		return -ENOTSUPP;
35008c6529dbSLiam Girdwood }
35018c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
35028c6529dbSLiam Girdwood 
3503c5af3a2eSMark Brown /**
3504c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3505c5af3a2eSMark Brown  *
3506ac11a2b3SMark Brown  * @card: Card to register
3507c5af3a2eSMark Brown  *
3508c5af3a2eSMark Brown  */
350970a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3510c5af3a2eSMark Brown {
3511b19e6e7bSMark Brown 	int i, ret;
3512f0fba2adSLiam Girdwood 
3513c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3514c5af3a2eSMark Brown 		return -EINVAL;
3515c5af3a2eSMark Brown 
35165a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
35175a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
35185a504963SStephen Warren 
35195a504963SStephen Warren 		/*
35205a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
35215a504963SStephen Warren 		 * not both or neither.
35225a504963SStephen Warren 		 */
35235a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
35245a504963SStephen Warren 			dev_err(card->dev,
35253b09bb82SLiam Girdwood 				"Neither/both codec name/of_node are set for %s\n",
35263b09bb82SLiam Girdwood 				link->name);
35275a504963SStephen Warren 			return -EINVAL;
35285a504963SStephen Warren 		}
3529bc92657aSStephen Warren 		/* Codec DAI name must be specified */
3530bc92657aSStephen Warren 		if (!link->codec_dai_name) {
3531bc92657aSStephen Warren 			dev_err(card->dev, "codec_dai_name not set for %s\n",
3532bc92657aSStephen Warren 				link->name);
3533bc92657aSStephen Warren 			return -EINVAL;
3534bc92657aSStephen Warren 		}
35355a504963SStephen Warren 
35365a504963SStephen Warren 		/*
35375a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
35385a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
35395a504963SStephen Warren 		 */
35405a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
35415a504963SStephen Warren 			dev_err(card->dev,
35423b09bb82SLiam Girdwood 				"Both platform name/of_node are set for %s\n", link->name);
35435a504963SStephen Warren 			return -EINVAL;
35445a504963SStephen Warren 		}
35455a504963SStephen Warren 
35465a504963SStephen Warren 		/*
3547bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3548bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3549bc92657aSStephen Warren 		 * name alone..
35505a504963SStephen Warren 		 */
3551bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
35525a504963SStephen Warren 			dev_err(card->dev,
3553bc92657aSStephen Warren 				"Neither/both cpu name/of_node are set for %s\n",
3554bc92657aSStephen Warren 				link->name);
3555bc92657aSStephen Warren 			return -EINVAL;
3556bc92657aSStephen Warren 		}
3557bc92657aSStephen Warren 		/*
3558bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3559bc92657aSStephen Warren 		 * specified
3560bc92657aSStephen Warren 		 */
3561bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3562bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
3563bc92657aSStephen Warren 			dev_err(card->dev,
3564bc92657aSStephen Warren 				"Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
35653b09bb82SLiam Girdwood 				link->name);
35665a504963SStephen Warren 			return -EINVAL;
35675a504963SStephen Warren 		}
35685a504963SStephen Warren 	}
35695a504963SStephen Warren 
3570ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3571ed77cc12SMark Brown 
3572111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3573111c6419SStephen Warren 
3574150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3575150dd2f8SVinod Koul 
3576181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3577181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
35782eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3579f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3580f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3581f0fba2adSLiam Girdwood 		return -ENOMEM;
3582a7dbb603SLiam Girdwood 	card->num_rtd = 0;
35832eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3584f0fba2adSLiam Girdwood 
3585f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3586f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3587f0fba2adSLiam Girdwood 
3588c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3589db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3590c5af3a2eSMark Brown 	card->instantiated = 0;
3591f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3592a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3593c5af3a2eSMark Brown 
3594b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3595b19e6e7bSMark Brown 	if (ret != 0)
3596b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3597c5af3a2eSMark Brown 
3598b19e6e7bSMark Brown 	return ret;
3599c5af3a2eSMark Brown }
360070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3601c5af3a2eSMark Brown 
3602c5af3a2eSMark Brown /**
3603c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3604c5af3a2eSMark Brown  *
3605ac11a2b3SMark Brown  * @card: Card to unregister
3606c5af3a2eSMark Brown  *
3607c5af3a2eSMark Brown  */
360870a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3609c5af3a2eSMark Brown {
3610b0e26485SVinod Koul 	if (card->instantiated)
3611b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3612c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3613c5af3a2eSMark Brown 
3614c5af3a2eSMark Brown 	return 0;
3615c5af3a2eSMark Brown }
361670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3617c5af3a2eSMark Brown 
3618f0fba2adSLiam Girdwood /*
3619f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3620f0fba2adSLiam Girdwood  * and sanitizing names.
3621f0fba2adSLiam Girdwood  */
36220b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3623f0fba2adSLiam Girdwood {
3624f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3625f0fba2adSLiam Girdwood 	int id1, id2;
3626f0fba2adSLiam Girdwood 
3627f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3628f0fba2adSLiam Girdwood 		return NULL;
3629f0fba2adSLiam Girdwood 
363058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3631f0fba2adSLiam Girdwood 
3632f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3633f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3634f0fba2adSLiam Girdwood 	if (found) {
3635f0fba2adSLiam Girdwood 		/* get ID */
3636f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3637f0fba2adSLiam Girdwood 
3638f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3639f0fba2adSLiam Girdwood 			if (*id == -1)
3640f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3641f0fba2adSLiam Girdwood 		}
3642f0fba2adSLiam Girdwood 
3643f0fba2adSLiam Girdwood 	} else {
3644f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3645f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3646f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3647f0fba2adSLiam Girdwood 
3648f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
364905899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3650f0fba2adSLiam Girdwood 
3651f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3652f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
365358818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3654f0fba2adSLiam Girdwood 		} else
3655f0fba2adSLiam Girdwood 			*id = 0;
3656f0fba2adSLiam Girdwood 	}
3657f0fba2adSLiam Girdwood 
3658f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3659f0fba2adSLiam Girdwood }
3660f0fba2adSLiam Girdwood 
3661f0fba2adSLiam Girdwood /*
3662f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3663f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3664f0fba2adSLiam Girdwood  */
3665f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3666f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3667f0fba2adSLiam Girdwood {
3668f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
36690837fc62SFabio Estevam 		pr_err("asoc: error - multiple DAI %s registered with no name\n",
3670f0fba2adSLiam Girdwood 				dev_name(dev));
3671f0fba2adSLiam Girdwood 		return NULL;
3672f0fba2adSLiam Girdwood 	}
3673f0fba2adSLiam Girdwood 
3674f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3675f0fba2adSLiam Girdwood }
3676f0fba2adSLiam Girdwood 
36779115171aSMark Brown /**
36789115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
36799115171aSMark Brown  *
3680ac11a2b3SMark Brown  * @dai: DAI to register
36819115171aSMark Brown  */
3682f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3683f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
36849115171aSMark Brown {
3685054880feSMark Brown 	struct snd_soc_codec *codec;
3686f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
36879115171aSMark Brown 
3688f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
36899115171aSMark Brown 
3690f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3691f0fba2adSLiam Girdwood 	if (dai == NULL)
3692f0fba2adSLiam Girdwood 		return -ENOMEM;
36936335d055SEric Miao 
3694f0fba2adSLiam Girdwood 	/* create DAI component name */
3695f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3696f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3697f0fba2adSLiam Girdwood 		kfree(dai);
3698f0fba2adSLiam Girdwood 		return -ENOMEM;
3699f0fba2adSLiam Girdwood 	}
3700f0fba2adSLiam Girdwood 
3701f0fba2adSLiam Girdwood 	dai->dev = dev;
3702f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3703be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3704f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3705f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
37069115171aSMark Brown 
37079115171aSMark Brown 	mutex_lock(&client_mutex);
3708054880feSMark Brown 
3709054880feSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3710054880feSMark Brown 		if (codec->dev == dev) {
3711054880feSMark Brown 			dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3712054880feSMark Brown 				dai->name, codec->name);
3713054880feSMark Brown 			dai->codec = codec;
3714054880feSMark Brown 			break;
3715054880feSMark Brown 		}
3716054880feSMark Brown 	}
3717054880feSMark Brown 
37189115171aSMark Brown 	list_add(&dai->list, &dai_list);
3719054880feSMark Brown 
37209115171aSMark Brown 	mutex_unlock(&client_mutex);
37219115171aSMark Brown 
37229115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
37239115171aSMark Brown 
37249115171aSMark Brown 	return 0;
37259115171aSMark Brown }
37269115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
37279115171aSMark Brown 
37289115171aSMark Brown /**
37299115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
37309115171aSMark Brown  *
3731ac11a2b3SMark Brown  * @dai: DAI to unregister
37329115171aSMark Brown  */
3733f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
37349115171aSMark Brown {
3735f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3736f0fba2adSLiam Girdwood 
3737f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3738f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3739f0fba2adSLiam Girdwood 			goto found;
3740f0fba2adSLiam Girdwood 	}
3741f0fba2adSLiam Girdwood 	return;
3742f0fba2adSLiam Girdwood 
3743f0fba2adSLiam Girdwood found:
37449115171aSMark Brown 	mutex_lock(&client_mutex);
37459115171aSMark Brown 	list_del(&dai->list);
37469115171aSMark Brown 	mutex_unlock(&client_mutex);
37479115171aSMark Brown 
37489115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3749f0fba2adSLiam Girdwood 	kfree(dai->name);
3750f0fba2adSLiam Girdwood 	kfree(dai);
37519115171aSMark Brown }
37529115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
37539115171aSMark Brown 
37549115171aSMark Brown /**
37559115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
37569115171aSMark Brown  *
3757ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3758ac11a2b3SMark Brown  * @count: Number of DAIs
37599115171aSMark Brown  */
3760f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3761f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
37629115171aSMark Brown {
3763054880feSMark Brown 	struct snd_soc_codec *codec;
3764f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3765f0fba2adSLiam Girdwood 	int i, ret = 0;
3766f0fba2adSLiam Girdwood 
3767720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
37689115171aSMark Brown 
37699115171aSMark Brown 	for (i = 0; i < count; i++) {
3770f0fba2adSLiam Girdwood 
3771f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3772c46e0079SAxel Lin 		if (dai == NULL) {
3773c46e0079SAxel Lin 			ret = -ENOMEM;
3774c46e0079SAxel Lin 			goto err;
3775c46e0079SAxel Lin 		}
3776f0fba2adSLiam Girdwood 
3777f0fba2adSLiam Girdwood 		/* create DAI component name */
3778f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3779f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3780f0fba2adSLiam Girdwood 			kfree(dai);
3781f0fba2adSLiam Girdwood 			ret = -EINVAL;
37829115171aSMark Brown 			goto err;
37839115171aSMark Brown 		}
37849115171aSMark Brown 
3785f0fba2adSLiam Girdwood 		dai->dev = dev;
3786f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
37870f9141c9SMark Brown 		if (dai->driver->id)
37880f9141c9SMark Brown 			dai->id = dai->driver->id;
37890f9141c9SMark Brown 		else
37900f9141c9SMark Brown 			dai->id = i;
3791be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3792f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3793f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3794f0fba2adSLiam Girdwood 
3795f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3796054880feSMark Brown 
3797054880feSMark Brown 		list_for_each_entry(codec, &codec_list, list) {
3798054880feSMark Brown 			if (codec->dev == dev) {
3799054880feSMark Brown 				dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3800054880feSMark Brown 					dai->name, codec->name);
3801054880feSMark Brown 				dai->codec = codec;
3802054880feSMark Brown 				break;
3803054880feSMark Brown 			}
3804054880feSMark Brown 		}
3805054880feSMark Brown 
3806f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3807054880feSMark Brown 
3808f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3809f0fba2adSLiam Girdwood 
3810f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3811f0fba2adSLiam Girdwood 	}
3812f0fba2adSLiam Girdwood 
38139115171aSMark Brown 	return 0;
38149115171aSMark Brown 
38159115171aSMark Brown err:
38169115171aSMark Brown 	for (i--; i >= 0; i--)
3817f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
38189115171aSMark Brown 
38199115171aSMark Brown 	return ret;
38209115171aSMark Brown }
38219115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
38229115171aSMark Brown 
38239115171aSMark Brown /**
38249115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
38259115171aSMark Brown  *
3826ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3827ac11a2b3SMark Brown  * @count: Number of DAIs
38289115171aSMark Brown  */
3829f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
38309115171aSMark Brown {
38319115171aSMark Brown 	int i;
38329115171aSMark Brown 
38339115171aSMark Brown 	for (i = 0; i < count; i++)
3834f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
38359115171aSMark Brown }
38369115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
38379115171aSMark Brown 
383812a48a8cSMark Brown /**
383912a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
384012a48a8cSMark Brown  *
3841ac11a2b3SMark Brown  * @platform: platform to register
384212a48a8cSMark Brown  */
3843f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3844f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
384512a48a8cSMark Brown {
3846f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
384712a48a8cSMark Brown 
3848f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3849f0fba2adSLiam Girdwood 
3850f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3851f0fba2adSLiam Girdwood 	if (platform == NULL)
3852f0fba2adSLiam Girdwood 		return -ENOMEM;
3853f0fba2adSLiam Girdwood 
3854f0fba2adSLiam Girdwood 	/* create platform component name */
3855f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3856f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3857f0fba2adSLiam Girdwood 		kfree(platform);
3858f0fba2adSLiam Girdwood 		return -ENOMEM;
3859f0fba2adSLiam Girdwood 	}
3860f0fba2adSLiam Girdwood 
3861f0fba2adSLiam Girdwood 	platform->dev = dev;
3862f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3863b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3864b7950641SLiam Girdwood 	platform->dapm.platform = platform;
386564a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3866cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
386712a48a8cSMark Brown 
386812a48a8cSMark Brown 	mutex_lock(&client_mutex);
386912a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
387012a48a8cSMark Brown 	mutex_unlock(&client_mutex);
387112a48a8cSMark Brown 
387212a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
387312a48a8cSMark Brown 
387412a48a8cSMark Brown 	return 0;
387512a48a8cSMark Brown }
387612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
387712a48a8cSMark Brown 
387812a48a8cSMark Brown /**
387912a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
388012a48a8cSMark Brown  *
3881ac11a2b3SMark Brown  * @platform: platform to unregister
388212a48a8cSMark Brown  */
3883f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
388412a48a8cSMark Brown {
3885f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3886f0fba2adSLiam Girdwood 
3887f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3888f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3889f0fba2adSLiam Girdwood 			goto found;
3890f0fba2adSLiam Girdwood 	}
3891f0fba2adSLiam Girdwood 	return;
3892f0fba2adSLiam Girdwood 
3893f0fba2adSLiam Girdwood found:
389412a48a8cSMark Brown 	mutex_lock(&client_mutex);
389512a48a8cSMark Brown 	list_del(&platform->list);
389612a48a8cSMark Brown 	mutex_unlock(&client_mutex);
389712a48a8cSMark Brown 
389812a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3899f0fba2adSLiam Girdwood 	kfree(platform->name);
3900f0fba2adSLiam Girdwood 	kfree(platform);
390112a48a8cSMark Brown }
390212a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
390312a48a8cSMark Brown 
3904151ab22cSMark Brown static u64 codec_format_map[] = {
3905151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3906151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3907151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3908151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3909151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3910151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3911151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3912151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3913151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3914151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3915151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3916151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3917151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3918151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3919151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3920151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3921151ab22cSMark Brown };
3922151ab22cSMark Brown 
3923151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3924151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3925151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3926151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3927151ab22cSMark Brown  */
3928151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3929151ab22cSMark Brown {
3930151ab22cSMark Brown 	int i;
3931151ab22cSMark Brown 
3932151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3933151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3934151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3935151ab22cSMark Brown }
3936151ab22cSMark Brown 
39370d0cf00aSMark Brown /**
39380d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
39390d0cf00aSMark Brown  *
3940ac11a2b3SMark Brown  * @codec: codec to register
39410d0cf00aSMark Brown  */
3942f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3943001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3944001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3945001ae4c0SMark Brown 			   int num_dai)
39460d0cf00aSMark Brown {
39473335ddcaSDimitris Papastamos 	size_t reg_size;
3948f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3949f0fba2adSLiam Girdwood 	int ret, i;
3950151ab22cSMark Brown 
3951f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
39520d0cf00aSMark Brown 
3953f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3954f0fba2adSLiam Girdwood 	if (codec == NULL)
3955f0fba2adSLiam Girdwood 		return -ENOMEM;
39560d0cf00aSMark Brown 
3957f0fba2adSLiam Girdwood 	/* create CODEC component name */
3958f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3959f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3960f0fba2adSLiam Girdwood 		kfree(codec);
3961f0fba2adSLiam Girdwood 		return -ENOMEM;
3962151ab22cSMark Brown 	}
3963151ab22cSMark Brown 
396423bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
396523bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
396623bbce34SDimitris Papastamos 	else
396723bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
396823bbce34SDimitris Papastamos 
3969c3acec26SMark Brown 	codec->write = codec_drv->write;
3970c3acec26SMark Brown 	codec->read = codec_drv->read;
39711500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
39721500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
39738020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
39745124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3975ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3976ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3977ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3978474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
397964a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3980f0fba2adSLiam Girdwood 	codec->dev = dev;
3981f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3982f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3983f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3984f0fba2adSLiam Girdwood 
39857a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
39867a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
39873335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3988aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
39893335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
39903335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
39913335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
39923335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
39933335ddcaSDimitris Papastamos 		 * the cache.
39943335ddcaSDimitris Papastamos 		 */
39952aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
39963335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
39973335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
39983335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
39993335ddcaSDimitris Papastamos 				ret = -ENOMEM;
4000fdf0f54dSDimitris Papastamos 				goto fail;
40017a30a3dbSDimitris Papastamos 			}
40027a30a3dbSDimitris Papastamos 		}
40032aa86323SDimitris Papastamos 	}
40047a30a3dbSDimitris Papastamos 
40051500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
40061500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
40071500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
40081500b7b5SDimitris Papastamos 		if (!codec->readable_register)
40091500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
40108020454cSDimitris Papastamos 		if (!codec->writable_register)
40118020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
40121500b7b5SDimitris Papastamos 	}
40131500b7b5SDimitris Papastamos 
4014f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
4015f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
4016f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
4017f0fba2adSLiam Girdwood 	}
4018f0fba2adSLiam Girdwood 
4019054880feSMark Brown 	mutex_lock(&client_mutex);
4020054880feSMark Brown 	list_add(&codec->list, &codec_list);
4021054880feSMark Brown 	mutex_unlock(&client_mutex);
4022054880feSMark Brown 
402326b01ccdSMark Brown 	/* register any DAIs */
402426b01ccdSMark Brown 	if (num_dai) {
4025f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4026f0fba2adSLiam Girdwood 		if (ret < 0)
4027054880feSMark Brown 			dev_err(codec->dev, "Failed to regster DAIs: %d\n",
4028054880feSMark Brown 				ret);
402926b01ccdSMark Brown 	}
4030f0fba2adSLiam Girdwood 
40310d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
40320d0cf00aSMark Brown 	return 0;
4033f0fba2adSLiam Girdwood 
4034fdf0f54dSDimitris Papastamos fail:
40353335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
40363335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
4037f0fba2adSLiam Girdwood 	kfree(codec->name);
4038f0fba2adSLiam Girdwood 	kfree(codec);
4039f0fba2adSLiam Girdwood 	return ret;
40400d0cf00aSMark Brown }
40410d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
40420d0cf00aSMark Brown 
40430d0cf00aSMark Brown /**
40440d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
40450d0cf00aSMark Brown  *
4046ac11a2b3SMark Brown  * @codec: codec to unregister
40470d0cf00aSMark Brown  */
4048f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
40490d0cf00aSMark Brown {
4050f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4051f0fba2adSLiam Girdwood 	int i;
4052f0fba2adSLiam Girdwood 
4053f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
4054f0fba2adSLiam Girdwood 		if (dev == codec->dev)
4055f0fba2adSLiam Girdwood 			goto found;
4056f0fba2adSLiam Girdwood 	}
4057f0fba2adSLiam Girdwood 	return;
4058f0fba2adSLiam Girdwood 
4059f0fba2adSLiam Girdwood found:
406026b01ccdSMark Brown 	if (codec->num_dai)
4061f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
4062f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
4063f0fba2adSLiam Girdwood 
40640d0cf00aSMark Brown 	mutex_lock(&client_mutex);
40650d0cf00aSMark Brown 	list_del(&codec->list);
40660d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
40670d0cf00aSMark Brown 
40680d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
4069f0fba2adSLiam Girdwood 
40707a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
40713335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
40721aafcd4dSDimitris Papastamos 	kfree(codec->name);
4073f0fba2adSLiam Girdwood 	kfree(codec);
40740d0cf00aSMark Brown }
40750d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
40760d0cf00aSMark Brown 
4077bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4078bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4079bec4fa05SStephen Warren 			       const char *propname)
4080bec4fa05SStephen Warren {
4081bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
4082bec4fa05SStephen Warren 	int ret;
4083bec4fa05SStephen Warren 
4084bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4085bec4fa05SStephen Warren 	/*
4086bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4087bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4088bec4fa05SStephen Warren 	 * snd_soc_register_card.
4089bec4fa05SStephen Warren 	 */
4090bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4091bec4fa05SStephen Warren 		dev_err(card->dev,
4092bec4fa05SStephen Warren 			"Property '%s' could not be read: %d\n",
4093bec4fa05SStephen Warren 			propname, ret);
4094bec4fa05SStephen Warren 		return ret;
4095bec4fa05SStephen Warren 	}
4096bec4fa05SStephen Warren 
4097bec4fa05SStephen Warren 	return 0;
4098bec4fa05SStephen Warren }
4099bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4100bec4fa05SStephen Warren 
4101a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4102a4a54dd5SStephen Warren 				   const char *propname)
4103a4a54dd5SStephen Warren {
4104a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4105a4a54dd5SStephen Warren 	int num_routes;
4106a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4107a4a54dd5SStephen Warren 	int i, ret;
4108a4a54dd5SStephen Warren 
4109a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4110c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
4111a4a54dd5SStephen Warren 		dev_err(card->dev,
4112c34ce320SRichard Zhao 		     "Property '%s' does not exist or its length is not even\n",
4113a4a54dd5SStephen Warren 		     propname);
4114a4a54dd5SStephen Warren 		return -EINVAL;
4115a4a54dd5SStephen Warren 	}
4116a4a54dd5SStephen Warren 	num_routes /= 2;
4117a4a54dd5SStephen Warren 	if (!num_routes) {
4118a4a54dd5SStephen Warren 		dev_err(card->dev,
4119a4a54dd5SStephen Warren 			"Property '%s's length is zero\n",
4120a4a54dd5SStephen Warren 			propname);
4121a4a54dd5SStephen Warren 		return -EINVAL;
4122a4a54dd5SStephen Warren 	}
4123a4a54dd5SStephen Warren 
4124a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4125a4a54dd5SStephen Warren 			      GFP_KERNEL);
4126a4a54dd5SStephen Warren 	if (!routes) {
4127a4a54dd5SStephen Warren 		dev_err(card->dev,
4128a4a54dd5SStephen Warren 			"Could not allocate DAPM route table\n");
4129a4a54dd5SStephen Warren 		return -EINVAL;
4130a4a54dd5SStephen Warren 	}
4131a4a54dd5SStephen Warren 
4132a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4133a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4134a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4135a4a54dd5SStephen Warren 		if (ret) {
4136a4a54dd5SStephen Warren 			dev_err(card->dev,
4137a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
4138a4a54dd5SStephen Warren 				propname, 2 * i, ret);
4139b761c0caSMatthias Kaehlcke 			kfree(routes);
4140a4a54dd5SStephen Warren 			return -EINVAL;
4141a4a54dd5SStephen Warren 		}
4142a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4143a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4144a4a54dd5SStephen Warren 		if (ret) {
4145a4a54dd5SStephen Warren 			dev_err(card->dev,
4146a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
4147a4a54dd5SStephen Warren 				propname, (2 * i) + 1, ret);
4148b761c0caSMatthias Kaehlcke 			kfree(routes);
4149a4a54dd5SStephen Warren 			return -EINVAL;
4150a4a54dd5SStephen Warren 		}
4151a4a54dd5SStephen Warren 	}
4152a4a54dd5SStephen Warren 
4153a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4154a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4155a4a54dd5SStephen Warren 
4156a4a54dd5SStephen Warren 	return 0;
4157a4a54dd5SStephen Warren }
4158a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4159a4a54dd5SStephen Warren 
4160c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4161db2a4165SFrank Mandarino {
4162384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
41638a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
41648a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
41650837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
41668a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4167384c89e2SMark Brown 	}
4168c3c5a19aSMark Brown 
41698a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4170c3c5a19aSMark Brown 				 &codec_list_fops))
4171c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4172c3c5a19aSMark Brown 
41738a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4174f3208780SMark Brown 				 &dai_list_fops))
4175f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
417619c7ac27SMark Brown 
41778a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
417819c7ac27SMark Brown 				 &platform_list_fops))
417919c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4180384c89e2SMark Brown #endif
4181384c89e2SMark Brown 
4182fb257897SMark Brown 	snd_soc_util_init();
4183fb257897SMark Brown 
4184db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4185db2a4165SFrank Mandarino }
41864abe8e16SMark Brown module_init(snd_soc_init);
4187db2a4165SFrank Mandarino 
41887d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4189db2a4165SFrank Mandarino {
4190fb257897SMark Brown 	snd_soc_util_exit();
4191fb257897SMark Brown 
4192384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
41938a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4194384c89e2SMark Brown #endif
4195db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4196db2a4165SFrank Mandarino }
4197db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4198db2a4165SFrank Mandarino 
4199db2a4165SFrank Mandarino /* Module information */
4200d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4201db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4202db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
42038b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
4204