xref: /openbmc/linux/sound/soc/soc-core.c (revision b5a8fe439ab343631d905a51438db3de54ba94df)
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) {
274f110bfc7SLiam Girdwood 		dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
275f110bfc7SLiam Girdwood 			" directory\n");
2762624d5faSMark Brown 		return;
2772624d5faSMark Brown 	}
2782624d5faSMark Brown 
279aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
280aaee8ef1SMark Brown 			    &codec->cache_sync);
281aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
282aaee8ef1SMark Brown 			    &codec->cache_only);
283aaee8ef1SMark Brown 
2842624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2852624d5faSMark Brown 						 codec->debugfs_codec_root,
2862624d5faSMark Brown 						 codec, &codec_reg_fops);
2872624d5faSMark Brown 	if (!codec->debugfs_reg)
288f110bfc7SLiam Girdwood 		dev_warn(codec->dev, "ASoC: Failed to create codec register"
289f110bfc7SLiam Girdwood 			" debugfs file\n");
2902624d5faSMark Brown 
2918eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
2922624d5faSMark Brown }
2932624d5faSMark Brown 
2942624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2952624d5faSMark Brown {
2962624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2972624d5faSMark Brown }
2982624d5faSMark Brown 
299731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
300731f1ab2SSebastien Guiriec {
301731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
302731f1ab2SSebastien Guiriec 
303731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
304731f1ab2SSebastien Guiriec 						       debugfs_card_root);
305731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
306731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
307f110bfc7SLiam Girdwood 			"ASoC: Failed to create platform debugfs directory\n");
308731f1ab2SSebastien Guiriec 		return;
309731f1ab2SSebastien Guiriec 	}
310731f1ab2SSebastien Guiriec 
311731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
312731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
313731f1ab2SSebastien Guiriec }
314731f1ab2SSebastien Guiriec 
315731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
316731f1ab2SSebastien Guiriec {
317731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
318731f1ab2SSebastien Guiriec }
319731f1ab2SSebastien Guiriec 
320c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
321c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
322c3c5a19aSMark Brown {
323c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3242b194f9dSMark Brown 	ssize_t len, ret = 0;
325c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
326c3c5a19aSMark Brown 
327c3c5a19aSMark Brown 	if (!buf)
328c3c5a19aSMark Brown 		return -ENOMEM;
329c3c5a19aSMark Brown 
3302b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3312b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
332c3c5a19aSMark Brown 			       codec->name);
3332b194f9dSMark Brown 		if (len >= 0)
3342b194f9dSMark Brown 			ret += len;
3352b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3362b194f9dSMark Brown 			ret = PAGE_SIZE;
3372b194f9dSMark Brown 			break;
3382b194f9dSMark Brown 		}
3392b194f9dSMark Brown 	}
340c3c5a19aSMark Brown 
341c3c5a19aSMark Brown 	if (ret >= 0)
342c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
343c3c5a19aSMark Brown 
344c3c5a19aSMark Brown 	kfree(buf);
345c3c5a19aSMark Brown 
346c3c5a19aSMark Brown 	return ret;
347c3c5a19aSMark Brown }
348c3c5a19aSMark Brown 
349c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
350c3c5a19aSMark Brown 	.read = codec_list_read_file,
351c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
352c3c5a19aSMark Brown };
353c3c5a19aSMark Brown 
354f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
355f3208780SMark Brown 				  size_t count, loff_t *ppos)
356f3208780SMark Brown {
357f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3582b194f9dSMark Brown 	ssize_t len, ret = 0;
359f3208780SMark Brown 	struct snd_soc_dai *dai;
360f3208780SMark Brown 
361f3208780SMark Brown 	if (!buf)
362f3208780SMark Brown 		return -ENOMEM;
363f3208780SMark Brown 
3642b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3652b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3662b194f9dSMark Brown 		if (len >= 0)
3672b194f9dSMark Brown 			ret += len;
3682b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3692b194f9dSMark Brown 			ret = PAGE_SIZE;
3702b194f9dSMark Brown 			break;
3712b194f9dSMark Brown 		}
3722b194f9dSMark Brown 	}
373f3208780SMark Brown 
374f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
375f3208780SMark Brown 
376f3208780SMark Brown 	kfree(buf);
377f3208780SMark Brown 
378f3208780SMark Brown 	return ret;
379f3208780SMark Brown }
380f3208780SMark Brown 
381f3208780SMark Brown static const struct file_operations dai_list_fops = {
382f3208780SMark Brown 	.read = dai_list_read_file,
383f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
384f3208780SMark Brown };
385f3208780SMark Brown 
38619c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
38719c7ac27SMark Brown 				       char __user *user_buf,
38819c7ac27SMark Brown 				       size_t count, loff_t *ppos)
38919c7ac27SMark Brown {
39019c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3912b194f9dSMark Brown 	ssize_t len, ret = 0;
39219c7ac27SMark Brown 	struct snd_soc_platform *platform;
39319c7ac27SMark Brown 
39419c7ac27SMark Brown 	if (!buf)
39519c7ac27SMark Brown 		return -ENOMEM;
39619c7ac27SMark Brown 
3972b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3982b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
39919c7ac27SMark Brown 			       platform->name);
4002b194f9dSMark Brown 		if (len >= 0)
4012b194f9dSMark Brown 			ret += len;
4022b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4032b194f9dSMark Brown 			ret = PAGE_SIZE;
4042b194f9dSMark Brown 			break;
4052b194f9dSMark Brown 		}
4062b194f9dSMark Brown 	}
40719c7ac27SMark Brown 
40819c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
40919c7ac27SMark Brown 
41019c7ac27SMark Brown 	kfree(buf);
41119c7ac27SMark Brown 
41219c7ac27SMark Brown 	return ret;
41319c7ac27SMark Brown }
41419c7ac27SMark Brown 
41519c7ac27SMark Brown static const struct file_operations platform_list_fops = {
41619c7ac27SMark Brown 	.read = platform_list_read_file,
41719c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
41819c7ac27SMark Brown };
41919c7ac27SMark Brown 
420a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
421a6052154SJarkko Nikula {
422a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4238a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4243a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
425a6052154SJarkko Nikula 		dev_warn(card->dev,
4267c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4273a45b867SJarkko Nikula 		return;
4283a45b867SJarkko Nikula 	}
4293a45b867SJarkko Nikula 
4303a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4313a45b867SJarkko Nikula 						    card->debugfs_card_root,
4323a45b867SJarkko Nikula 						    &card->pop_time);
4333a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4343a45b867SJarkko Nikula 		dev_warn(card->dev,
435f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
436a6052154SJarkko Nikula }
437a6052154SJarkko Nikula 
438a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
439a6052154SJarkko Nikula {
440a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
441a6052154SJarkko Nikula }
442a6052154SJarkko Nikula 
4432624d5faSMark Brown #else
4442624d5faSMark Brown 
4452624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4462624d5faSMark Brown {
4472624d5faSMark Brown }
4482624d5faSMark Brown 
4492624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4502624d5faSMark Brown {
4512624d5faSMark Brown }
452b95fccbcSAxel Lin 
453731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
454731f1ab2SSebastien Guiriec {
455731f1ab2SSebastien Guiriec }
456731f1ab2SSebastien Guiriec 
457731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
458731f1ab2SSebastien Guiriec {
459731f1ab2SSebastien Guiriec }
460731f1ab2SSebastien Guiriec 
461b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
462b95fccbcSAxel Lin {
463b95fccbcSAxel Lin }
464b95fccbcSAxel Lin 
465b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
466b95fccbcSAxel Lin {
467b95fccbcSAxel Lin }
4682624d5faSMark Brown #endif
4692624d5faSMark Brown 
47047c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
47147c88fffSLiam Girdwood 		const char *dai_link, int stream)
47247c88fffSLiam Girdwood {
47347c88fffSLiam Girdwood 	int i;
47447c88fffSLiam Girdwood 
47547c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
47647c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
47747c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
47847c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
47947c88fffSLiam Girdwood 	}
480f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
48147c88fffSLiam Girdwood 	return NULL;
48247c88fffSLiam Girdwood }
48347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
48447c88fffSLiam Girdwood 
48547c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
48647c88fffSLiam Girdwood 		const char *dai_link)
48747c88fffSLiam Girdwood {
48847c88fffSLiam Girdwood 	int i;
48947c88fffSLiam Girdwood 
49047c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
49147c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
49247c88fffSLiam Girdwood 			return &card->rtd[i];
49347c88fffSLiam Girdwood 	}
494f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
49547c88fffSLiam Girdwood 	return NULL;
49647c88fffSLiam Girdwood }
49747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
49847c88fffSLiam Girdwood 
499db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
500db2a4165SFrank Mandarino /* unregister ac97 codec */
501db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
502db2a4165SFrank Mandarino {
503db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
504db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
505db2a4165SFrank Mandarino 	return 0;
506db2a4165SFrank Mandarino }
507db2a4165SFrank Mandarino 
508db2a4165SFrank Mandarino /* stop no dev release warning */
509db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
510db2a4165SFrank Mandarino 
511db2a4165SFrank Mandarino /* register ac97 codec to bus */
512db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
513db2a4165SFrank Mandarino {
514db2a4165SFrank Mandarino 	int err;
515db2a4165SFrank Mandarino 
516db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
5174ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
518db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
519db2a4165SFrank Mandarino 
520bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
521f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
522db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
523db2a4165SFrank Mandarino 	if (err < 0) {
524f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
525db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
526db2a4165SFrank Mandarino 		return err;
527db2a4165SFrank Mandarino 	}
528db2a4165SFrank Mandarino 	return 0;
529db2a4165SFrank Mandarino }
530db2a4165SFrank Mandarino #endif
531db2a4165SFrank Mandarino 
5326f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
533db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5346f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
535db2a4165SFrank Mandarino {
5366f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5372eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
538db2a4165SFrank Mandarino 	int i;
539db2a4165SFrank Mandarino 
540e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
541e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
542e3509ff0SDaniel Mack 	 */
543f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
544e3509ff0SDaniel Mack 		return 0;
545e3509ff0SDaniel Mack 
5466ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5476ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5486ed25978SAndy Green 	 */
549f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
550f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
551f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5526ed25978SAndy Green 
5536ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
554f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5556ed25978SAndy Green 
556a00f90f9SMark Brown 	/* mute any active DACs */
557f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
558f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
559f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5603efab7dcSMark Brown 
561f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5623efab7dcSMark Brown 			continue;
5633efab7dcSMark Brown 
564f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
565f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
566db2a4165SFrank Mandarino 	}
567db2a4165SFrank Mandarino 
5684ccab3e7SLiam Girdwood 	/* suspend all pcms */
569f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
570f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5713efab7dcSMark Brown 			continue;
5723efab7dcSMark Brown 
573f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5743efab7dcSMark Brown 	}
5754ccab3e7SLiam Girdwood 
57687506549SMark Brown 	if (card->suspend_pre)
57770b2ac12SMark Brown 		card->suspend_pre(card);
578db2a4165SFrank Mandarino 
579f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
580f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
581f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5823efab7dcSMark Brown 
583f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5843efab7dcSMark Brown 			continue;
5853efab7dcSMark Brown 
586f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
587f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
588f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
589f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
590f0fba2adSLiam Girdwood 			platform->suspended = 1;
591f0fba2adSLiam Girdwood 		}
592db2a4165SFrank Mandarino 	}
593db2a4165SFrank Mandarino 
594db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
595f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
59643829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
597ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
598f0fba2adSLiam Girdwood 	}
599db2a4165SFrank Mandarino 
600f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6013efab7dcSMark Brown 
602f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6033efab7dcSMark Brown 			continue;
6043efab7dcSMark Brown 
6057bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6067bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
607db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
608f0fba2adSLiam Girdwood 
6097bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6107bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
611db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
612db2a4165SFrank Mandarino 	}
613db2a4165SFrank Mandarino 
614e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
615e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
616e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
617e2d32ff6SMark Brown 
618f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6192eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6201547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6211547aba9SMark Brown 		 * bias _ON and should not be suspended. */
622f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
623ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6241547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
625125a25daSMark Brown 				/*
626125a25daSMark Brown 				 * If the CODEC is capable of idle
627125a25daSMark Brown 				 * bias off then being in STANDBY
628125a25daSMark Brown 				 * means it's doing something,
629125a25daSMark Brown 				 * otherwise fall through.
630125a25daSMark Brown 				 */
631125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
632125a25daSMark Brown 					dev_dbg(codec->dev,
633f110bfc7SLiam Girdwood 						"ASoC: idle_bias_off CODEC on"
634f110bfc7SLiam Girdwood 						" over suspend\n");
635125a25daSMark Brown 					break;
636125a25daSMark Brown 				}
6371547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
63884b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
639f0fba2adSLiam Girdwood 				codec->suspended = 1;
6407be4ba24SMark Brown 				codec->cache_sync = 1;
641da8b8e0fSMark Brown 				if (codec->using_regmap)
642da8b8e0fSMark Brown 					regcache_mark_dirty(codec->control_data);
6431547aba9SMark Brown 				break;
6441547aba9SMark Brown 			default:
645f110bfc7SLiam Girdwood 				dev_dbg(codec->dev, "ASoC: CODEC is on"
646f110bfc7SLiam Girdwood 					" over suspend\n");
6471547aba9SMark Brown 				break;
6481547aba9SMark Brown 			}
6491547aba9SMark Brown 		}
650f0fba2adSLiam Girdwood 	}
651db2a4165SFrank Mandarino 
652f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
653f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6543efab7dcSMark Brown 
655f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6563efab7dcSMark Brown 			continue;
6573efab7dcSMark Brown 
658f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
659f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
660db2a4165SFrank Mandarino 	}
661db2a4165SFrank Mandarino 
66287506549SMark Brown 	if (card->suspend_post)
66370b2ac12SMark Brown 		card->suspend_post(card);
664db2a4165SFrank Mandarino 
665db2a4165SFrank Mandarino 	return 0;
666db2a4165SFrank Mandarino }
6676f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
668db2a4165SFrank Mandarino 
6696ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6706ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6716ed25978SAndy Green  */
6726ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
673db2a4165SFrank Mandarino {
674f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
675f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6762eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
677db2a4165SFrank Mandarino 	int i;
678db2a4165SFrank Mandarino 
6796ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6806ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6816ed25978SAndy Green 	 */
6826ed25978SAndy Green 
683f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6846ed25978SAndy Green 
6859949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
686f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6879949788bSMark Brown 
68887506549SMark Brown 	if (card->resume_pre)
68970b2ac12SMark Brown 		card->resume_pre(card);
690db2a4165SFrank Mandarino 
691f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
692f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
693f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6943efab7dcSMark Brown 
695f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6963efab7dcSMark Brown 			continue;
6973efab7dcSMark Brown 
698f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
699f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
700db2a4165SFrank Mandarino 	}
701db2a4165SFrank Mandarino 
7022eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7031547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7041547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7051547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7061547aba9SMark Brown 		 */
707f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
708ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7091547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7101547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
711f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
712f0fba2adSLiam Girdwood 				codec->suspended = 0;
7131547aba9SMark Brown 				break;
7141547aba9SMark Brown 			default:
715f110bfc7SLiam Girdwood 				dev_dbg(codec->dev, "ASoC: CODEC was on over"
716f110bfc7SLiam Girdwood 					" suspend\n");
7171547aba9SMark Brown 				break;
7181547aba9SMark Brown 			}
7191547aba9SMark Brown 		}
720f0fba2adSLiam Girdwood 	}
721db2a4165SFrank Mandarino 
722f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7233efab7dcSMark Brown 
724f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7253efab7dcSMark Brown 			continue;
7263efab7dcSMark Brown 
7277bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
728d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
729db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
730f0fba2adSLiam Girdwood 
7317bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
732d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
733db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
734db2a4165SFrank Mandarino 	}
735db2a4165SFrank Mandarino 
7363ff3f64bSMark Brown 	/* unmute any active DACs */
737f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
738f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
739f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7403efab7dcSMark Brown 
741f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7423efab7dcSMark Brown 			continue;
7433efab7dcSMark Brown 
744f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
745f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
746db2a4165SFrank Mandarino 	}
747db2a4165SFrank Mandarino 
748f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
749f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
750f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7513efab7dcSMark Brown 
752f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7533efab7dcSMark Brown 			continue;
7543efab7dcSMark Brown 
755f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
756f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
757f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
758f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
759f0fba2adSLiam Girdwood 			platform->suspended = 0;
760f0fba2adSLiam Girdwood 		}
761db2a4165SFrank Mandarino 	}
762db2a4165SFrank Mandarino 
76387506549SMark Brown 	if (card->resume_post)
76470b2ac12SMark Brown 		card->resume_post(card);
765db2a4165SFrank Mandarino 
766f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7676ed25978SAndy Green 
7686ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
769f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
770e2d32ff6SMark Brown 
771e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
772e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
773e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7746ed25978SAndy Green }
7756ed25978SAndy Green 
7766ed25978SAndy Green /* powers up audio subsystem after a suspend */
7776f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7786ed25978SAndy Green {
7796f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
78082e14e8bSStephen Warren 	int i, ac97_control = 0;
781b9dd94a8SPeter Ujfalusi 
7825ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
7835ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
7845ff1ddf2SEric Miao 	 */
7855ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
7865ff1ddf2SEric Miao 		return 0;
7875ff1ddf2SEric Miao 
78864ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
78964ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
79064ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
79164ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
79264ab9baaSMark Brown 	 */
793f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
794f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
79582e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
79682e14e8bSStephen Warren 	}
79782e14e8bSStephen Warren 	if (ac97_control) {
798f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
79964ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
80064ab9baaSMark Brown 	} else {
801f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8026308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
803f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
804f0fba2adSLiam Girdwood 	}
8056ed25978SAndy Green 
806db2a4165SFrank Mandarino 	return 0;
807db2a4165SFrank Mandarino }
8086f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
809db2a4165SFrank Mandarino #else
8106f8ab4acSMark Brown #define snd_soc_suspend NULL
8116f8ab4acSMark Brown #define snd_soc_resume NULL
812db2a4165SFrank Mandarino #endif
813db2a4165SFrank Mandarino 
81485e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
81502a06d30SBarry Song };
81602a06d30SBarry Song 
817f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
818db2a4165SFrank Mandarino {
819f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
820f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
821fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
822435c5e25SMark Brown 	struct snd_soc_platform *platform;
823f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
824848dd8beSMark Brown 	const char *platform_name;
825db2a4165SFrank Mandarino 
826f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
8276308419aSMark Brown 
828b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
829f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
830bc92657aSStephen Warren 		if (dai_link->cpu_of_node &&
831bc92657aSStephen Warren 		    (cpu_dai->dev->of_node != dai_link->cpu_of_node))
8325a504963SStephen Warren 			continue;
833bc92657aSStephen Warren 		if (dai_link->cpu_name &&
834bc92657aSStephen Warren 		    strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
8352610ab77SStephen Warren 			continue;
836bc92657aSStephen Warren 		if (dai_link->cpu_dai_name &&
837bc92657aSStephen Warren 		    strcmp(cpu_dai->name, dai_link->cpu_dai_name))
838bc92657aSStephen Warren 			continue;
8392610ab77SStephen Warren 
840f0fba2adSLiam Girdwood 		rtd->cpu_dai = cpu_dai;
841f0fba2adSLiam Girdwood 	}
842b19e6e7bSMark Brown 
843b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
844f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
845f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
846b19e6e7bSMark Brown 		return -EPROBE_DEFER;
847c5af3a2eSMark Brown 	}
848c5af3a2eSMark Brown 
849b19e6e7bSMark Brown 	/* Find CODEC from registered CODECs */
850f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8515a504963SStephen Warren 		if (dai_link->codec_of_node) {
8525a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8535a504963SStephen Warren 				continue;
8545a504963SStephen Warren 		} else {
8552610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8562610ab77SStephen Warren 				continue;
8575a504963SStephen Warren 		}
8582610ab77SStephen Warren 
859f0fba2adSLiam Girdwood 		rtd->codec = codec;
860f0fba2adSLiam Girdwood 
8612610ab77SStephen Warren 		/*
8622610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
8632610ab77SStephen Warren 		 * this CODEC
8642610ab77SStephen Warren 		 */
865f0fba2adSLiam Girdwood 		list_for_each_entry(codec_dai, &dai_list, list) {
866f0fba2adSLiam Girdwood 			if (codec->dev == codec_dai->dev &&
8672610ab77SStephen Warren 				!strcmp(codec_dai->name,
8682610ab77SStephen Warren 					dai_link->codec_dai_name)) {
8692610ab77SStephen Warren 
870f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
871435c5e25SMark Brown 			}
872f0fba2adSLiam Girdwood 		}
873b19e6e7bSMark Brown 
874b19e6e7bSMark Brown 		if (!rtd->codec_dai) {
875f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
876f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
877b19e6e7bSMark Brown 			return -EPROBE_DEFER;
878f0fba2adSLiam Girdwood 		}
879b19e6e7bSMark Brown 	}
880b19e6e7bSMark Brown 
881b19e6e7bSMark Brown 	if (!rtd->codec) {
882f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CODEC %s not registered\n",
883f0fba2adSLiam Girdwood 			dai_link->codec_name);
884b19e6e7bSMark Brown 		return -EPROBE_DEFER;
885b19e6e7bSMark Brown 	}
886848dd8beSMark Brown 
887848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
888848dd8beSMark Brown 	platform_name = dai_link->platform_name;
8895a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
890848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
891848dd8beSMark Brown 
892b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
893f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
8945a504963SStephen Warren 		if (dai_link->platform_of_node) {
8955a504963SStephen Warren 			if (platform->dev->of_node !=
8965a504963SStephen Warren 			    dai_link->platform_of_node)
8975a504963SStephen Warren 				continue;
8985a504963SStephen Warren 		} else {
8992610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
9002610ab77SStephen Warren 				continue;
9015a504963SStephen Warren 		}
9022610ab77SStephen Warren 
903f0fba2adSLiam Girdwood 		rtd->platform = platform;
904f0fba2adSLiam Girdwood 	}
905b19e6e7bSMark Brown 	if (!rtd->platform) {
906f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
907f0fba2adSLiam Girdwood 			dai_link->platform_name);
908b19e6e7bSMark Brown 		return -EPROBE_DEFER;
909f0fba2adSLiam Girdwood 	}
910b19e6e7bSMark Brown 
911b19e6e7bSMark Brown 	card->num_rtd++;
912b19e6e7bSMark Brown 
913b19e6e7bSMark Brown 	return 0;
9146b05eda6SMark Brown }
9156b05eda6SMark Brown 
916d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform)
917d12cd198SStephen Warren {
918d12cd198SStephen Warren 	int ret;
919d12cd198SStephen Warren 
920d12cd198SStephen Warren 	if (platform->driver->remove) {
921d12cd198SStephen Warren 		ret = platform->driver->remove(platform);
922d12cd198SStephen Warren 		if (ret < 0)
923f110bfc7SLiam Girdwood 			dev_err(platform->dev, "ASoC: failed to remove %d\n",
924f110bfc7SLiam Girdwood 				ret);
925d12cd198SStephen Warren 	}
926d12cd198SStephen Warren 
927d12cd198SStephen Warren 	/* Make sure all DAPM widgets are freed */
928d12cd198SStephen Warren 	snd_soc_dapm_free(&platform->dapm);
929d12cd198SStephen Warren 
930d12cd198SStephen Warren 	soc_cleanup_platform_debugfs(platform);
931d12cd198SStephen Warren 	platform->probed = 0;
932d12cd198SStephen Warren 	list_del(&platform->card_list);
933d12cd198SStephen Warren 	module_put(platform->dev->driver->owner);
934d12cd198SStephen Warren 
935d12cd198SStephen Warren 	return 0;
936d12cd198SStephen Warren }
937d12cd198SStephen Warren 
938589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
939589c3563SJarkko Nikula {
940589c3563SJarkko Nikula 	int err;
941589c3563SJarkko Nikula 
942589c3563SJarkko Nikula 	if (codec->driver->remove) {
943589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
944589c3563SJarkko Nikula 		if (err < 0)
945f110bfc7SLiam Girdwood 			dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
946589c3563SJarkko Nikula 	}
947589c3563SJarkko Nikula 
948589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
949589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
950589c3563SJarkko Nikula 
951589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
952589c3563SJarkko Nikula 	codec->probed = 0;
953589c3563SJarkko Nikula 	list_del(&codec->card_list);
954589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
955589c3563SJarkko Nikula }
956589c3563SJarkko Nikula 
95762ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
958f0fba2adSLiam Girdwood {
959f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
960f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
961f0fba2adSLiam Girdwood 	int err;
962f0fba2adSLiam Girdwood 
963f0fba2adSLiam Girdwood 	/* unregister the rtd device */
964f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
96536ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
96636ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
96736ae1a96SMark Brown 		device_unregister(rtd->dev);
968f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
96902a06d30SBarry Song 	}
97002a06d30SBarry Song 
971f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9720168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
9730168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
974f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
975f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
976f0fba2adSLiam Girdwood 			if (err < 0)
977f110bfc7SLiam Girdwood 				dev_err(codec_dai->dev,
978f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
9790837fc62SFabio Estevam 					codec_dai->name, err);
980f0fba2adSLiam Girdwood 		}
981f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
982f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
983f0fba2adSLiam Girdwood 	}
984f0fba2adSLiam Girdwood 
985f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
9860168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
9870168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
988f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
989f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
990f0fba2adSLiam Girdwood 			if (err < 0)
991f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
992f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
9930837fc62SFabio Estevam 					cpu_dai->name, err);
994f0fba2adSLiam Girdwood 		}
995f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
996f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
997a9db7dbeSStephen Warren 
99818d75644SStephen Warren 		if (!cpu_dai->codec) {
99918d75644SStephen Warren 			snd_soc_dapm_free(&cpu_dai->dapm);
1000f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
1001f0fba2adSLiam Girdwood 		}
1002f0fba2adSLiam Girdwood 	}
100318d75644SStephen Warren }
1004f0fba2adSLiam Girdwood 
100562ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
100662ae68faSStephen Warren 				       int order)
100762ae68faSStephen Warren {
100862ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
100962ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
101062ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
101162ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
101262ae68faSStephen Warren 	struct snd_soc_codec *codec;
101362ae68faSStephen Warren 
101462ae68faSStephen Warren 	/* remove the platform */
101562ae68faSStephen Warren 	if (platform && platform->probed &&
101662ae68faSStephen Warren 	    platform->driver->remove_order == order) {
101762ae68faSStephen Warren 		soc_remove_platform(platform);
101862ae68faSStephen Warren 	}
101962ae68faSStephen Warren 
102062ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
102162ae68faSStephen Warren 	if (codec_dai) {
102262ae68faSStephen Warren 		codec = codec_dai->codec;
102362ae68faSStephen Warren 		if (codec && codec->probed &&
102462ae68faSStephen Warren 		    codec->driver->remove_order == order)
102562ae68faSStephen Warren 			soc_remove_codec(codec);
102662ae68faSStephen Warren 	}
102762ae68faSStephen Warren 
102862ae68faSStephen Warren 	/* remove any CPU-side CODEC */
102962ae68faSStephen Warren 	if (cpu_dai) {
103062ae68faSStephen Warren 		codec = cpu_dai->codec;
103162ae68faSStephen Warren 		if (codec && codec->probed &&
103262ae68faSStephen Warren 		    codec->driver->remove_order == order)
103362ae68faSStephen Warren 			soc_remove_codec(codec);
103462ae68faSStephen Warren 	}
103562ae68faSStephen Warren }
103662ae68faSStephen Warren 
10370671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10380671fd8eSKuninori Morimoto {
10390168bf0dSLiam Girdwood 	int dai, order;
10400671fd8eSKuninori Morimoto 
10410168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10420168bf0dSLiam Girdwood 			order++) {
10430168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
104462ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10450168bf0dSLiam Girdwood 	}
104662ae68faSStephen Warren 
104762ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
104862ae68faSStephen Warren 			order++) {
104962ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
105062ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
105162ae68faSStephen Warren 	}
105262ae68faSStephen Warren 
10530671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10540671fd8eSKuninori Morimoto }
10550671fd8eSKuninori Morimoto 
1056ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1057ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1058ead9b919SJarkko Nikula {
1059ead9b919SJarkko Nikula 	int i;
1060ead9b919SJarkko Nikula 
1061ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1062ead9b919SJarkko Nikula 		return;
1063ead9b919SJarkko Nikula 
1064ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1065ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1066ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1067ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1068ead9b919SJarkko Nikula 			break;
1069ead9b919SJarkko Nikula 		}
1070ead9b919SJarkko Nikula 	}
1071ead9b919SJarkko Nikula }
1072ead9b919SJarkko Nikula 
1073589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1074589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1075589c3563SJarkko Nikula {
1076589c3563SJarkko Nikula 	int ret = 0;
107789b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1078888df395SMark Brown 	struct snd_soc_dai *dai;
1079589c3563SJarkko Nikula 
1080589c3563SJarkko Nikula 	codec->card = card;
1081589c3563SJarkko Nikula 	codec->dapm.card = card;
1082589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1083589c3563SJarkko Nikula 
108470d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
108570d29331SJarkko Nikula 		return -ENODEV;
108670d29331SJarkko Nikula 
1087d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1088d5d1e0beSLars-Peter Clausen 
108977530150SLars-Peter Clausen 	if (driver->dapm_widgets)
109077530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
109177530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
109277530150SLars-Peter Clausen 
1093888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1094888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1095888df395SMark Brown 		if (dai->dev != codec->dev)
1096888df395SMark Brown 			continue;
1097888df395SMark Brown 
1098888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1099888df395SMark Brown 	}
1100888df395SMark Brown 
110133c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
110233c5f969SMark Brown 
110389b95ac0SMark Brown 	if (driver->probe) {
110489b95ac0SMark Brown 		ret = driver->probe(codec);
1105589c3563SJarkko Nikula 		if (ret < 0) {
1106589c3563SJarkko Nikula 			dev_err(codec->dev,
1107f110bfc7SLiam Girdwood 				"ASoC: failed to probe CODEC %d\n", ret);
110870d29331SJarkko Nikula 			goto err_probe;
1109589c3563SJarkko Nikula 		}
1110ff541f4bSChuansheng Liu 		WARN(codec->dapm.idle_bias_off &&
1111ff541f4bSChuansheng Liu 			codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1112ff541f4bSChuansheng Liu 			"codec %s can not start from non-off bias"
1113ff541f4bSChuansheng Liu 			" with idle_bias_off==1\n", codec->name);
1114589c3563SJarkko Nikula 	}
1115589c3563SJarkko Nikula 
111638cbf959SMark Brown 	/* If the driver didn't set I/O up try regmap */
111798d3088eSMark Brown 	if (!codec->write && dev_get_regmap(codec->dev, NULL))
111838cbf959SMark Brown 		snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
111938cbf959SMark Brown 
1120b7af1dafSMark Brown 	if (driver->controls)
1121022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1122b7af1dafSMark Brown 				     driver->num_controls);
112389b95ac0SMark Brown 	if (driver->dapm_routes)
112489b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
112589b95ac0SMark Brown 					driver->num_dapm_routes);
112689b95ac0SMark Brown 
1127589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1128589c3563SJarkko Nikula 	codec->probed = 1;
1129589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
11307be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1131589c3563SJarkko Nikula 
113270d29331SJarkko Nikula 	return 0;
113370d29331SJarkko Nikula 
113470d29331SJarkko Nikula err_probe:
1135d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
113670d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
113770d29331SJarkko Nikula 
1138589c3563SJarkko Nikula 	return ret;
1139589c3563SJarkko Nikula }
1140589c3563SJarkko Nikula 
1141956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1142956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1143956245e9SLiam Girdwood {
1144956245e9SLiam Girdwood 	int ret = 0;
1145956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1146be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1147956245e9SLiam Girdwood 
1148956245e9SLiam Girdwood 	platform->card = card;
1149b7950641SLiam Girdwood 	platform->dapm.card = card;
1150956245e9SLiam Girdwood 
1151956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1152956245e9SLiam Girdwood 		return -ENODEV;
1153956245e9SLiam Girdwood 
1154731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1155731f1ab2SSebastien Guiriec 
1156cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1157cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1158cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1159cb2cf612SLiam Girdwood 
1160be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
1161be09ad90SLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
1162be09ad90SLiam Girdwood 		if (dai->dev != platform->dev)
1163be09ad90SLiam Girdwood 			continue;
1164be09ad90SLiam Girdwood 
1165be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1166be09ad90SLiam Girdwood 	}
1167be09ad90SLiam Girdwood 
11683fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
11693fec6b6dSStephen Warren 
1170956245e9SLiam Girdwood 	if (driver->probe) {
1171956245e9SLiam Girdwood 		ret = driver->probe(platform);
1172956245e9SLiam Girdwood 		if (ret < 0) {
1173956245e9SLiam Girdwood 			dev_err(platform->dev,
1174f110bfc7SLiam Girdwood 				"ASoC: failed to probe platform %d\n", ret);
1175956245e9SLiam Girdwood 			goto err_probe;
1176956245e9SLiam Girdwood 		}
1177956245e9SLiam Girdwood 	}
1178956245e9SLiam Girdwood 
1179cb2cf612SLiam Girdwood 	if (driver->controls)
1180cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1181cb2cf612SLiam Girdwood 				     driver->num_controls);
1182cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1183cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1184cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1185cb2cf612SLiam Girdwood 
1186956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1187956245e9SLiam Girdwood 	platform->probed = 1;
1188956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1189b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1190956245e9SLiam Girdwood 
1191956245e9SLiam Girdwood 	return 0;
1192956245e9SLiam Girdwood 
1193956245e9SLiam Girdwood err_probe:
119402db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1195956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1196956245e9SLiam Girdwood 
1197956245e9SLiam Girdwood 	return ret;
1198956245e9SLiam Girdwood }
1199956245e9SLiam Girdwood 
120036ae1a96SMark Brown static void rtd_release(struct device *dev)
120136ae1a96SMark Brown {
120236ae1a96SMark Brown 	kfree(dev);
120336ae1a96SMark Brown }
1204f0fba2adSLiam Girdwood 
1205589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1206589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1207589c3563SJarkko Nikula 				   int num, int dailess)
1208589c3563SJarkko Nikula {
1209589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1210589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1211589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1212589c3563SJarkko Nikula 	const char *temp, *name;
1213589c3563SJarkko Nikula 	int ret = 0;
1214589c3563SJarkko Nikula 
1215589c3563SJarkko Nikula 	if (!dailess) {
1216589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1217589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1218589c3563SJarkko Nikula 		name = dai_link->name;
1219589c3563SJarkko Nikula 	} else {
1220589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1221589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1222589c3563SJarkko Nikula 		name = aux_dev->name;
1223589c3563SJarkko Nikula 	}
12240962bb21SJanusz Krzysztofik 	rtd->card = card;
1225589c3563SJarkko Nikula 
12264b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
12274b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
12284b1cfcb4SMark Brown 
1229589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1230589c3563SJarkko Nikula 	temp = codec->name_prefix;
1231589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1232589c3563SJarkko Nikula 
1233589c3563SJarkko Nikula 	/* do machine specific initialization */
1234589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1235589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1236589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1237589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1238589c3563SJarkko Nikula 	if (ret < 0) {
1239f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1240589c3563SJarkko Nikula 		return ret;
1241589c3563SJarkko Nikula 	}
1242589c3563SJarkko Nikula 	codec->name_prefix = temp;
1243589c3563SJarkko Nikula 
1244589c3563SJarkko Nikula 	/* register the rtd device */
1245589c3563SJarkko Nikula 	rtd->codec = codec;
124636ae1a96SMark Brown 
124736ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
124836ae1a96SMark Brown 	if (!rtd->dev)
124936ae1a96SMark Brown 		return -ENOMEM;
125036ae1a96SMark Brown 	device_initialize(rtd->dev);
125136ae1a96SMark Brown 	rtd->dev->parent = card->dev;
125236ae1a96SMark Brown 	rtd->dev->release = rtd_release;
125336ae1a96SMark Brown 	rtd->dev->init_name = name;
125436ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1255b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
125601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
125701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
125801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
125901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
126036ae1a96SMark Brown 	ret = device_add(rtd->dev);
1261589c3563SJarkko Nikula 	if (ret < 0) {
1262589c3563SJarkko Nikula 		dev_err(card->dev,
1263f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1264589c3563SJarkko Nikula 		return ret;
1265589c3563SJarkko Nikula 	}
1266589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1267589c3563SJarkko Nikula 
1268589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
126936ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1270589c3563SJarkko Nikula 	if (ret < 0)
1271589c3563SJarkko Nikula 		dev_err(codec->dev,
1272f110bfc7SLiam Girdwood 			"ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1273589c3563SJarkko Nikula 
1274589c3563SJarkko Nikula 	/* add codec sysfs entries */
127536ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1276589c3563SJarkko Nikula 	if (ret < 0)
1277589c3563SJarkko Nikula 		dev_err(codec->dev,
1278f110bfc7SLiam Girdwood 			"ASoC: failed to add codec sysfs files: %d\n", ret);
1279589c3563SJarkko Nikula 
1280f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1281f86dcef8SLiam Girdwood 	/* add DPCM sysfs entries */
1282cd0f8911SLiam Girdwood 	if (!dailess && !dai_link->dynamic)
1283f86dcef8SLiam Girdwood 		goto out;
1284f86dcef8SLiam Girdwood 
1285f86dcef8SLiam Girdwood 	ret = soc_dpcm_debugfs_add(rtd);
1286f86dcef8SLiam Girdwood 	if (ret < 0)
1287f110bfc7SLiam Girdwood 		dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1288f86dcef8SLiam Girdwood 
1289f86dcef8SLiam Girdwood out:
1290f86dcef8SLiam Girdwood #endif
1291589c3563SJarkko Nikula 	return 0;
1292589c3563SJarkko Nikula }
1293589c3563SJarkko Nikula 
129462ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
129562ae68faSStephen Warren 				     int order)
129662ae68faSStephen Warren {
129762ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
129862ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
129962ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
130062ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
130162ae68faSStephen Warren 	int ret;
130262ae68faSStephen Warren 
130362ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
130462ae68faSStephen Warren 	if (cpu_dai->codec &&
130562ae68faSStephen Warren 	    !cpu_dai->codec->probed &&
130662ae68faSStephen Warren 	    cpu_dai->codec->driver->probe_order == order) {
130762ae68faSStephen Warren 		ret = soc_probe_codec(card, cpu_dai->codec);
130862ae68faSStephen Warren 		if (ret < 0)
130962ae68faSStephen Warren 			return ret;
131062ae68faSStephen Warren 	}
131162ae68faSStephen Warren 
131262ae68faSStephen Warren 	/* probe the CODEC-side component */
131362ae68faSStephen Warren 	if (!codec_dai->codec->probed &&
131462ae68faSStephen Warren 	    codec_dai->codec->driver->probe_order == order) {
131562ae68faSStephen Warren 		ret = soc_probe_codec(card, codec_dai->codec);
131662ae68faSStephen Warren 		if (ret < 0)
131762ae68faSStephen Warren 			return ret;
131862ae68faSStephen Warren 	}
131962ae68faSStephen Warren 
132062ae68faSStephen Warren 	/* probe the platform */
132162ae68faSStephen Warren 	if (!platform->probed &&
132262ae68faSStephen Warren 	    platform->driver->probe_order == order) {
132362ae68faSStephen Warren 		ret = soc_probe_platform(card, platform);
132462ae68faSStephen Warren 		if (ret < 0)
132562ae68faSStephen Warren 			return ret;
132662ae68faSStephen Warren 	}
132762ae68faSStephen Warren 
132862ae68faSStephen Warren 	return 0;
132962ae68faSStephen Warren }
133062ae68faSStephen Warren 
133162ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1332f0fba2adSLiam Girdwood {
1333f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1334f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1335f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1336f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1337c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1338c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1339c74184edSMark Brown 	struct snd_soc_dapm_widget *play_w, *capture_w;
1340f0fba2adSLiam Girdwood 	int ret;
1341f0fba2adSLiam Girdwood 
1342f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
13430168bf0dSLiam Girdwood 			card->name, num, order);
1344f0fba2adSLiam Girdwood 
1345f0fba2adSLiam Girdwood 	/* config components */
1346f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1347f0fba2adSLiam Girdwood 	codec_dai->card = card;
1348f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1349f0fba2adSLiam Girdwood 
1350f0fba2adSLiam Girdwood 	/* set default power off timeout */
1351f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1352f0fba2adSLiam Girdwood 
1353f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
13540168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
13550168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1356a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
1357be09ad90SLiam Girdwood 			cpu_dai->dapm.card = card;
135861b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
135961b61e3cSLiam Girdwood 				return -ENODEV;
136061b61e3cSLiam Girdwood 
136118d75644SStephen Warren 			list_add(&cpu_dai->dapm.list, &card->dapm_list);
1362be09ad90SLiam Girdwood 			snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1363a9db7dbeSStephen Warren 		}
1364be09ad90SLiam Girdwood 
1365f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1366f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1367f0fba2adSLiam Girdwood 			if (ret < 0) {
1368f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1369f110bfc7SLiam Girdwood 					"ASoC: failed to probe CPU DAI %s: %d\n",
13700837fc62SFabio Estevam 					cpu_dai->name, ret);
137161b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1372f0fba2adSLiam Girdwood 				return ret;
1373f0fba2adSLiam Girdwood 			}
1374f0fba2adSLiam Girdwood 		}
1375f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
13761c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1377f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1378f0fba2adSLiam Girdwood 	}
1379f0fba2adSLiam Girdwood 
1380f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
13810168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1382f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1383f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1384f0fba2adSLiam Girdwood 			if (ret < 0) {
1385f110bfc7SLiam Girdwood 				dev_err(codec_dai->dev,
1386f110bfc7SLiam Girdwood 					"ASoC: failed to probe CODEC DAI %s: %d\n",
13870837fc62SFabio Estevam 					codec_dai->name, ret);
1388f0fba2adSLiam Girdwood 				return ret;
1389f0fba2adSLiam Girdwood 			}
1390f0fba2adSLiam Girdwood 		}
1391f0fba2adSLiam Girdwood 
13921c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1393f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1394f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1395f0fba2adSLiam Girdwood 	}
1396f0fba2adSLiam Girdwood 
13970168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
13980168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
13990168bf0dSLiam Girdwood 		return 0;
14000168bf0dSLiam Girdwood 
1401589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1402589c3563SJarkko Nikula 	if (ret)
1403f0fba2adSLiam Girdwood 		return ret;
1404f0fba2adSLiam Girdwood 
140536ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1406f0fba2adSLiam Girdwood 	if (ret < 0)
1407f110bfc7SLiam Girdwood 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1408f110bfc7SLiam Girdwood 			ret);
1409f0fba2adSLiam Girdwood 
14101245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
14111245b700SNamarta Kohli 		/*create compress_device"*/
14121245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
14131245b700SNamarta Kohli 		if (ret < 0) {
1414f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
14151245b700SNamarta Kohli 					 dai_link->stream_name);
14161245b700SNamarta Kohli 			return ret;
14171245b700SNamarta Kohli 		}
14181245b700SNamarta Kohli 	} else {
14191245b700SNamarta Kohli 
1420c74184edSMark Brown 		if (!dai_link->params) {
1421f0fba2adSLiam Girdwood 			/* create the pcm */
1422f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1423f0fba2adSLiam Girdwood 			if (ret < 0) {
1424f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
14250837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1426f0fba2adSLiam Girdwood 				return ret;
1427f0fba2adSLiam Girdwood 			}
1428c74184edSMark Brown 		} else {
1429c74184edSMark Brown 			/* link the DAI widgets */
1430c74184edSMark Brown 			play_w = codec_dai->playback_widget;
1431c74184edSMark Brown 			capture_w = cpu_dai->capture_widget;
1432c74184edSMark Brown 			if (play_w && capture_w) {
1433c74184edSMark Brown 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1434c74184edSMark Brown 						   capture_w, play_w);
1435c74184edSMark Brown 				if (ret != 0) {
1436f110bfc7SLiam Girdwood 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1437c74184edSMark Brown 						play_w->name, capture_w->name, ret);
1438c74184edSMark Brown 					return ret;
1439c74184edSMark Brown 				}
1440c74184edSMark Brown 			}
1441c74184edSMark Brown 
1442c74184edSMark Brown 			play_w = cpu_dai->playback_widget;
1443c74184edSMark Brown 			capture_w = codec_dai->capture_widget;
1444c74184edSMark Brown 			if (play_w && capture_w) {
1445c74184edSMark Brown 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1446c74184edSMark Brown 						   capture_w, play_w);
1447c74184edSMark Brown 				if (ret != 0) {
1448f110bfc7SLiam Girdwood 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1449c74184edSMark Brown 						play_w->name, capture_w->name, ret);
1450c74184edSMark Brown 					return ret;
1451c74184edSMark Brown 				}
1452c74184edSMark Brown 			}
1453c74184edSMark Brown 		}
14541245b700SNamarta Kohli 	}
1455f0fba2adSLiam Girdwood 
1456f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1457f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1458f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1459f0fba2adSLiam Girdwood 
1460f0fba2adSLiam Girdwood 	return 0;
1461f0fba2adSLiam Girdwood }
1462f0fba2adSLiam Girdwood 
1463f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1464f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1465f0fba2adSLiam Girdwood {
1466f0fba2adSLiam Girdwood 	int ret;
1467f0fba2adSLiam Girdwood 
1468f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1469f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
14706b05eda6SMark Brown 	 */
1471f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
14720562f788SMika Westerberg 		/*
14730562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
14740562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
14750562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
14760562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
14770562f788SMika Westerberg 		 *
14780562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
14790562f788SMika Westerberg 		 */
14800562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
14810562f788SMika Westerberg 			return 0;
1482f0fba2adSLiam Girdwood 
1483f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1484f0fba2adSLiam Girdwood 		if (ret < 0) {
1485f110bfc7SLiam Girdwood 			dev_err(rtd->codec->dev,
1486f110bfc7SLiam Girdwood 				"ASoC: AC97 device register failed: %d\n", ret);
1487f0fba2adSLiam Girdwood 			return ret;
1488435c5e25SMark Brown 		}
1489435c5e25SMark Brown 
1490f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1491f0fba2adSLiam Girdwood 	}
1492f0fba2adSLiam Girdwood 	return 0;
1493f0fba2adSLiam Girdwood }
1494435c5e25SMark Brown 
1495f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1496f0fba2adSLiam Girdwood {
1497f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1498f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1499f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1500f0fba2adSLiam Girdwood 	}
1501f0fba2adSLiam Girdwood }
1502f0fba2adSLiam Girdwood #endif
1503435c5e25SMark Brown 
1504b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1505b19e6e7bSMark Brown {
1506b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1507b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1508b19e6e7bSMark Brown 
1509b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1510b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1511b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1512b19e6e7bSMark Brown 			return 0;
1513b19e6e7bSMark Brown 	}
1514b19e6e7bSMark Brown 
1515f110bfc7SLiam Girdwood 	dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1516fb099cb7SMark Brown 
1517b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1518b19e6e7bSMark Brown }
1519b19e6e7bSMark Brown 
15202eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
15212eea392dSJarkko Nikula {
15222eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
15232eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1524676ad98aSJarkko Nikula 	int ret = -ENODEV;
15252eea392dSJarkko Nikula 
15262eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
15272eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
15282eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
15292eea392dSJarkko Nikula 			if (codec->probed) {
15302eea392dSJarkko Nikula 				dev_err(codec->dev,
1531f110bfc7SLiam Girdwood 					"ASoC: codec already probed");
15322eea392dSJarkko Nikula 				ret = -EBUSY;
15332eea392dSJarkko Nikula 				goto out;
15342eea392dSJarkko Nikula 			}
1535676ad98aSJarkko Nikula 			goto found;
15362eea392dSJarkko Nikula 		}
15372eea392dSJarkko Nikula 	}
1538676ad98aSJarkko Nikula 	/* codec not found */
1539f110bfc7SLiam Girdwood 	dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1540b19e6e7bSMark Brown 	return -EPROBE_DEFER;
15412eea392dSJarkko Nikula 
1542676ad98aSJarkko Nikula found:
1543589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
15442eea392dSJarkko Nikula 	if (ret < 0)
1545589c3563SJarkko Nikula 		return ret;
15462eea392dSJarkko Nikula 
1547589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
15482eea392dSJarkko Nikula 
15492eea392dSJarkko Nikula out:
15502eea392dSJarkko Nikula 	return ret;
15512eea392dSJarkko Nikula }
15522eea392dSJarkko Nikula 
15532eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
15542eea392dSJarkko Nikula {
15552eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
15562eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
15572eea392dSJarkko Nikula 
15582eea392dSJarkko Nikula 	/* unregister the rtd device */
15592eea392dSJarkko Nikula 	if (rtd->dev_registered) {
156036ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
156136ae1a96SMark Brown 		device_del(rtd->dev);
15622eea392dSJarkko Nikula 		rtd->dev_registered = 0;
15632eea392dSJarkko Nikula 	}
15642eea392dSJarkko Nikula 
1565589c3563SJarkko Nikula 	if (codec && codec->probed)
1566589c3563SJarkko Nikula 		soc_remove_codec(codec);
15672eea392dSJarkko Nikula }
15682eea392dSJarkko Nikula 
1569fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1570fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1571fdf0f54dSDimitris Papastamos {
1572fdf0f54dSDimitris Papastamos 	int ret;
1573fdf0f54dSDimitris Papastamos 
1574fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1575fdf0f54dSDimitris Papastamos 		return 0;
1576fdf0f54dSDimitris Papastamos 
1577fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1578fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1579fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1580fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1581fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1582f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Failed to set cache compression"
1583f110bfc7SLiam Girdwood 			" type: %d\n", ret);
1584fdf0f54dSDimitris Papastamos 		return ret;
1585fdf0f54dSDimitris Papastamos 	}
1586fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1587fdf0f54dSDimitris Papastamos 	return 0;
1588fdf0f54dSDimitris Papastamos }
1589fdf0f54dSDimitris Papastamos 
1590b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1591f0fba2adSLiam Girdwood {
1592fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1593fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1594fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
159575d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1596f04209a7SMark Brown 	int ret, i, order, dai_fmt;
159796dd3622SMark Brown 
159801b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1599f0fba2adSLiam Girdwood 
1600b19e6e7bSMark Brown 	/* bind DAIs */
1601b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1602b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1603b19e6e7bSMark Brown 		if (ret != 0)
1604b19e6e7bSMark Brown 			goto base_error;
1605db2a4165SFrank Mandarino 	}
1606db2a4165SFrank Mandarino 
1607b19e6e7bSMark Brown 	/* check aux_devs too */
1608b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1609b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1610b19e6e7bSMark Brown 		if (ret != 0)
1611b19e6e7bSMark Brown 			goto base_error;
1612db2a4165SFrank Mandarino 	}
1613db2a4165SFrank Mandarino 
1614fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1615fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1616fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1617fdf0f54dSDimitris Papastamos 			continue;
1618861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1619861f2fafSDimitris Papastamos 		compress_type = 0;
1620fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1621fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1622fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1623fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1624fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1625fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1626fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1627fdf0f54dSDimitris Papastamos 					break;
1628fdf0f54dSDimitris Papastamos 			}
1629fdf0f54dSDimitris Papastamos 		}
1630fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1631b19e6e7bSMark Brown 		if (ret < 0)
1632b19e6e7bSMark Brown 			goto base_error;
1633fdf0f54dSDimitris Papastamos 	}
1634fdf0f54dSDimitris Papastamos 
1635f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1636f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1637f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1638f0fba2adSLiam Girdwood 	if (ret < 0) {
1639f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create sound card for"
1640f110bfc7SLiam Girdwood 			" card %s: %d\n", card->name, ret);
1641b19e6e7bSMark Brown 		goto base_error;
1642db2a4165SFrank Mandarino 	}
1643f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1644db2a4165SFrank Mandarino 
1645e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1646e37a4970SMark Brown 	card->dapm.dev = card->dev;
1647e37a4970SMark Brown 	card->dapm.card = card;
1648e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1649e37a4970SMark Brown 
1650d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1651d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1652d5d1e0beSLars-Peter Clausen #endif
1653d5d1e0beSLars-Peter Clausen 
165488ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
16556ed25978SAndy Green 	/* deferred resume work */
16566308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16571301a964SRandy Dunlap #endif
16586ed25978SAndy Green 
16599a841ebbSMark Brown 	if (card->dapm_widgets)
16609a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
16619a841ebbSMark Brown 					  card->num_dapm_widgets);
16629a841ebbSMark Brown 
1663f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1664f0fba2adSLiam Girdwood 	if (card->probe) {
1665e7361ec4SMark Brown 		ret = card->probe(card);
1666f0fba2adSLiam Girdwood 		if (ret < 0)
1667f0fba2adSLiam Girdwood 			goto card_probe_error;
1668f0fba2adSLiam Girdwood 	}
1669f0fba2adSLiam Girdwood 
167062ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
16710168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
16720168bf0dSLiam Girdwood 			order++) {
1673fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
167462ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
167562ae68faSStephen Warren 			if (ret < 0) {
1676f110bfc7SLiam Girdwood 				dev_err(card->dev,
1677f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1678f110bfc7SLiam Girdwood 					ret);
167962ae68faSStephen Warren 				goto probe_dai_err;
168062ae68faSStephen Warren 			}
168162ae68faSStephen Warren 		}
168262ae68faSStephen Warren 	}
168362ae68faSStephen Warren 
168462ae68faSStephen Warren 	/* probe all DAI links on this card */
168562ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
168662ae68faSStephen Warren 			order++) {
168762ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
168862ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1689fe3e78e0SMark Brown 			if (ret < 0) {
1690f110bfc7SLiam Girdwood 				dev_err(card->dev,
1691f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1692f110bfc7SLiam Girdwood 					ret);
1693f0fba2adSLiam Girdwood 				goto probe_dai_err;
1694fe3e78e0SMark Brown 			}
1695fe3e78e0SMark Brown 		}
16960168bf0dSLiam Girdwood 	}
1697fe3e78e0SMark Brown 
16982eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
16992eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
17002eea392dSJarkko Nikula 		if (ret < 0) {
1701f110bfc7SLiam Girdwood 			dev_err(card->dev,
1702f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1703f110bfc7SLiam Girdwood 				ret);
17042eea392dSJarkko Nikula 			goto probe_aux_dev_err;
17052eea392dSJarkko Nikula 		}
17062eea392dSJarkko Nikula 	}
17072eea392dSJarkko Nikula 
1708888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1709888df395SMark Brown 
1710b7af1dafSMark Brown 	if (card->controls)
1711022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1712b7af1dafSMark Brown 
1713b8ad29deSMark Brown 	if (card->dapm_routes)
1714b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1715b8ad29deSMark Brown 					card->num_dapm_routes);
1716b8ad29deSMark Brown 
1717b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1718b90d2f90SMark Brown 
171975d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
172075d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1721f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
172275d9ac46SMark Brown 
1723f04209a7SMark Brown 		if (dai_fmt) {
172475d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1725f04209a7SMark Brown 						  dai_fmt);
17265e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
172775d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
1728f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
172975d9ac46SMark Brown 					 ret);
1730f04209a7SMark Brown 		}
1731f04209a7SMark Brown 
1732f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1733fe33d4c5SStephen Warren 		if (dai_fmt &&
1734fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1735f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1736f04209a7SMark Brown 						  dai_fmt);
1737f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1738f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1739f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
1740f04209a7SMark Brown 					 ret);
1741f04209a7SMark Brown 		} else if (dai_fmt) {
1742f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1743f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1744f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1745f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1746f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1747f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1748f04209a7SMark Brown 				break;
1749f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1750f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1751f04209a7SMark Brown 				break;
1752f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1753f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1754f04209a7SMark Brown 				break;
1755f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1756f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1757f04209a7SMark Brown 				break;
1758f04209a7SMark Brown 			}
175975d9ac46SMark Brown 
176075d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1761f04209a7SMark Brown 						  dai_fmt);
17625e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
176375d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1764f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
176575d9ac46SMark Brown 					 ret);
176675d9ac46SMark Brown 		}
176775d9ac46SMark Brown 	}
176875d9ac46SMark Brown 
1769f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1770fe3e78e0SMark Brown 		 "%s", card->name);
1771f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
177222de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1773f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1774f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1775f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1776f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1777f0e8ed85SMark Brown 		case '_':
1778f0e8ed85SMark Brown 		case '-':
1779f0e8ed85SMark Brown 		case '\0':
1780f0e8ed85SMark Brown 			break;
1781f0e8ed85SMark Brown 		default:
1782f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1783f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1784f0e8ed85SMark Brown 			break;
1785f0e8ed85SMark Brown 		}
1786f0e8ed85SMark Brown 	}
1787fe3e78e0SMark Brown 
178828e9ad92SMark Brown 	if (card->late_probe) {
178928e9ad92SMark Brown 		ret = card->late_probe(card);
179028e9ad92SMark Brown 		if (ret < 0) {
1791f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
179228e9ad92SMark Brown 				card->name, ret);
179328e9ad92SMark Brown 			goto probe_aux_dev_err;
179428e9ad92SMark Brown 		}
179528e9ad92SMark Brown 	}
179628e9ad92SMark Brown 
17972dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
17982dc00213SMark Brown 
17991633281bSStephen Warren 	if (card->fully_routed)
1800b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
18011633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
18021633281bSStephen Warren 
1803f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1804fe3e78e0SMark Brown 	if (ret < 0) {
1805f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1806f110bfc7SLiam Girdwood 				ret);
18076b3ed785SAxel Lin 		goto probe_aux_dev_err;
1808fe3e78e0SMark Brown 	}
1809fe3e78e0SMark Brown 
1810fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1811f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1812f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1813f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1814fe3e78e0SMark Brown 		if (ret < 0) {
1815f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: failed to register AC97:"
1816f110bfc7SLiam Girdwood 				" %d\n", ret);
18176b3ed785SAxel Lin 			while (--i >= 0)
18187d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
18196b3ed785SAxel Lin 			goto probe_aux_dev_err;
1820fe3e78e0SMark Brown 		}
1821fe3e78e0SMark Brown 	}
1822fe3e78e0SMark Brown #endif
1823fe3e78e0SMark Brown 
1824435c5e25SMark Brown 	card->instantiated = 1;
18254f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1826f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1827b19e6e7bSMark Brown 
1828b19e6e7bSMark Brown 	return 0;
1829db2a4165SFrank Mandarino 
18302eea392dSJarkko Nikula probe_aux_dev_err:
18312eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18322eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18332eea392dSJarkko Nikula 
1834f0fba2adSLiam Girdwood probe_dai_err:
18350671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1836fe3e78e0SMark Brown 
1837f0fba2adSLiam Girdwood card_probe_error:
183887506549SMark Brown 	if (card->remove)
1839e7361ec4SMark Brown 		card->remove(card);
1840f0fba2adSLiam Girdwood 
1841f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1842f0fba2adSLiam Girdwood 
1843b19e6e7bSMark Brown base_error:
1844f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1845db2a4165SFrank Mandarino 
1846b19e6e7bSMark Brown 	return ret;
1847435c5e25SMark Brown }
1848435c5e25SMark Brown 
1849435c5e25SMark Brown /* probes a new socdev */
1850435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1851435c5e25SMark Brown {
1852f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1853435c5e25SMark Brown 
185470a7ca34SVinod Koul 	/*
185570a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
185670a7ca34SVinod Koul 	 * we should not be here in that case so ret error
185770a7ca34SVinod Koul 	 */
185870a7ca34SVinod Koul 	if (!card)
185970a7ca34SVinod Koul 		return -EINVAL;
186070a7ca34SVinod Koul 
1861fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1862f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1863fe4085e8SMark Brown 		 card->name);
1864fe4085e8SMark Brown 
1865435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1866435c5e25SMark Brown 	card->dev = &pdev->dev;
1867f0fba2adSLiam Girdwood 
186828d528c8SMark Brown 	return snd_soc_register_card(card);
1869435c5e25SMark Brown }
1870435c5e25SMark Brown 
1871b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1872db2a4165SFrank Mandarino {
1873db2a4165SFrank Mandarino 	int i;
1874db2a4165SFrank Mandarino 
1875f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1876f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1877f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
187843829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1879db2a4165SFrank Mandarino 	}
1880db2a4165SFrank Mandarino 
18812eea392dSJarkko Nikula 	/* remove auxiliary devices */
18822eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18832eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18842eea392dSJarkko Nikula 
1885f0fba2adSLiam Girdwood 	/* remove and free each DAI */
18860671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1887f0fba2adSLiam Girdwood 
1888a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1889a6052154SJarkko Nikula 
1890f0fba2adSLiam Girdwood 	/* remove the card */
189187506549SMark Brown 	if (card->remove)
1892e7361ec4SMark Brown 		card->remove(card);
1893f0fba2adSLiam Girdwood 
18940aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
18950aaae527SLars-Peter Clausen 
1896f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1897b0e26485SVinod Koul 	return 0;
1898b0e26485SVinod Koul 
1899b2dfa62cSGuennadi Liakhovetski }
1900b0e26485SVinod Koul 
1901b0e26485SVinod Koul /* removes a socdev */
1902b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1903b0e26485SVinod Koul {
1904b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1905b0e26485SVinod Koul 
1906c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1907db2a4165SFrank Mandarino 	return 0;
1908db2a4165SFrank Mandarino }
1909db2a4165SFrank Mandarino 
19106f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
191151737470SMark Brown {
19126f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1913f0fba2adSLiam Girdwood 	int i;
191451737470SMark Brown 
191551737470SMark Brown 	if (!card->instantiated)
1916416356fcSMark Brown 		return 0;
191751737470SMark Brown 
191851737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
191951737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1920f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1921f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
192243829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1923f0fba2adSLiam Girdwood 	}
192451737470SMark Brown 
1925f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1926416356fcSMark Brown 
1927416356fcSMark Brown 	return 0;
192851737470SMark Brown }
19296f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
193051737470SMark Brown 
19316f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1932b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1933b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1934b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1935b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
19366f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1937b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1938416356fcSMark Brown };
1939deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1940416356fcSMark Brown 
1941db2a4165SFrank Mandarino /* ASoC platform driver */
1942db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1943db2a4165SFrank Mandarino 	.driver		= {
1944db2a4165SFrank Mandarino 		.name		= "soc-audio",
19458b45a209SKay Sievers 		.owner		= THIS_MODULE,
19466f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1947db2a4165SFrank Mandarino 	},
1948db2a4165SFrank Mandarino 	.probe		= soc_probe,
1949db2a4165SFrank Mandarino 	.remove		= soc_remove,
1950db2a4165SFrank Mandarino };
1951db2a4165SFrank Mandarino 
1952096e49d5SMark Brown /**
1953096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1954096e49d5SMark Brown  *
1955096e49d5SMark Brown  * @codec: CODEC to query.
1956096e49d5SMark Brown  * @reg: Register to query.
1957096e49d5SMark Brown  *
1958096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1959096e49d5SMark Brown  */
1960181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1961181e055eSMark Brown 				    unsigned int reg)
1962096e49d5SMark Brown {
19631500b7b5SDimitris Papastamos 	if (codec->volatile_register)
19641500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1965096e49d5SMark Brown 	else
1966096e49d5SMark Brown 		return 0;
1967096e49d5SMark Brown }
1968096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1969096e49d5SMark Brown 
1970db2a4165SFrank Mandarino /**
1971239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1972239c9706SDimitris Papastamos  *
1973239c9706SDimitris Papastamos  * @codec: CODEC to query.
1974239c9706SDimitris Papastamos  * @reg: Register to query.
1975239c9706SDimitris Papastamos  *
1976239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1977239c9706SDimitris Papastamos  */
1978239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1979239c9706SDimitris Papastamos 				    unsigned int reg)
1980239c9706SDimitris Papastamos {
1981239c9706SDimitris Papastamos 	if (codec->readable_register)
1982239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1983239c9706SDimitris Papastamos 	else
198463fa0a28SLars-Peter Clausen 		return 1;
1985239c9706SDimitris Papastamos }
1986239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1987239c9706SDimitris Papastamos 
1988239c9706SDimitris Papastamos /**
1989239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1990239c9706SDimitris Papastamos  *
1991239c9706SDimitris Papastamos  * @codec: CODEC to query.
1992239c9706SDimitris Papastamos  * @reg: Register to query.
1993239c9706SDimitris Papastamos  *
1994239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1995239c9706SDimitris Papastamos  */
1996239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1997239c9706SDimitris Papastamos 				    unsigned int reg)
1998239c9706SDimitris Papastamos {
1999239c9706SDimitris Papastamos 	if (codec->writable_register)
2000239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
2001239c9706SDimitris Papastamos 	else
200263fa0a28SLars-Peter Clausen 		return 1;
2003239c9706SDimitris Papastamos }
2004239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2005239c9706SDimitris Papastamos 
2006f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
2007f1442bc1SLiam Girdwood 					unsigned int reg)
2008f1442bc1SLiam Girdwood {
2009f1442bc1SLiam Girdwood 	unsigned int ret;
2010f1442bc1SLiam Girdwood 
2011f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
2012f110bfc7SLiam Girdwood 		dev_err(platform->dev, "ASoC: platform has no read back\n");
2013f1442bc1SLiam Girdwood 		return -1;
2014f1442bc1SLiam Girdwood 	}
2015f1442bc1SLiam Girdwood 
2016f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
2017f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2018a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
2019f1442bc1SLiam Girdwood 
2020f1442bc1SLiam Girdwood 	return ret;
2021f1442bc1SLiam Girdwood }
2022f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2023f1442bc1SLiam Girdwood 
2024f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
2025f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
2026f1442bc1SLiam Girdwood {
2027f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
2028f110bfc7SLiam Girdwood 		dev_err(platform->dev, "ASoC: platform has no write back\n");
2029f1442bc1SLiam Girdwood 		return -1;
2030f1442bc1SLiam Girdwood 	}
2031f1442bc1SLiam Girdwood 
2032f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2033a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
2034f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
2035f1442bc1SLiam Girdwood }
2036f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2037f1442bc1SLiam Girdwood 
2038239c9706SDimitris Papastamos /**
2039db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2040db2a4165SFrank Mandarino  * @codec: audio codec
2041db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2042db2a4165SFrank Mandarino  * @num: AC97 codec number
2043db2a4165SFrank Mandarino  *
2044db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2045db2a4165SFrank Mandarino  */
2046db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2047db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2048db2a4165SFrank Mandarino {
2049db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2050db2a4165SFrank Mandarino 
2051db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2052db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2053db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2054db2a4165SFrank Mandarino 		return -ENOMEM;
2055db2a4165SFrank Mandarino 	}
2056db2a4165SFrank Mandarino 
2057db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2058db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2059db2a4165SFrank Mandarino 		kfree(codec->ac97);
2060db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2061db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2062db2a4165SFrank Mandarino 		return -ENOMEM;
2063db2a4165SFrank Mandarino 	}
2064db2a4165SFrank Mandarino 
2065db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2066db2a4165SFrank Mandarino 	codec->ac97->num = num;
20670562f788SMika Westerberg 
20680562f788SMika Westerberg 	/*
20690562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
20700562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
20710562f788SMika Westerberg 	 */
20720562f788SMika Westerberg 	codec->ac97_created = 1;
20730562f788SMika Westerberg 
2074db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2075db2a4165SFrank Mandarino 	return 0;
2076db2a4165SFrank Mandarino }
2077db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2078db2a4165SFrank Mandarino 
2079db2a4165SFrank Mandarino /**
2080db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2081db2a4165SFrank Mandarino  * @codec: audio codec
2082db2a4165SFrank Mandarino  *
2083db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2084db2a4165SFrank Mandarino  */
2085db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2086db2a4165SFrank Mandarino {
2087db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2088f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2089f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2090f0fba2adSLiam Girdwood #endif
2091db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2092db2a4165SFrank Mandarino 	kfree(codec->ac97);
2093db2a4165SFrank Mandarino 	codec->ac97 = NULL;
20940562f788SMika Westerberg 	codec->ac97_created = 0;
2095db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2096db2a4165SFrank Mandarino }
2097db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2098db2a4165SFrank Mandarino 
2099c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2100c3753707SMark Brown {
2101c3753707SMark Brown 	unsigned int ret;
2102c3753707SMark Brown 
2103c3acec26SMark Brown 	ret = codec->read(codec, reg);
2104c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2105a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2106c3753707SMark Brown 
2107c3753707SMark Brown 	return ret;
2108c3753707SMark Brown }
2109c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2110c3753707SMark Brown 
2111c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2112c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2113c3753707SMark Brown {
2114c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2115a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2116c3acec26SMark Brown 	return codec->write(codec, reg, val);
2117c3753707SMark Brown }
2118c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2119c3753707SMark Brown 
21205fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
21215fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
21225fb609d4SDimitris Papastamos {
21235fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
21245fb609d4SDimitris Papastamos }
21255fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
21265fb609d4SDimitris Papastamos 
2127db2a4165SFrank Mandarino /**
2128db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2129db2a4165SFrank Mandarino  * @codec: audio codec
2130db2a4165SFrank Mandarino  * @reg: codec register
2131db2a4165SFrank Mandarino  * @mask: register mask
2132db2a4165SFrank Mandarino  * @value: new value
2133db2a4165SFrank Mandarino  *
2134db2a4165SFrank Mandarino  * Writes new register value.
2135db2a4165SFrank Mandarino  *
2136180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
2137db2a4165SFrank Mandarino  */
2138db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
213946f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2140db2a4165SFrank Mandarino {
21418a713da8SMark Brown 	bool change;
214246f5822fSDaniel Ribeiro 	unsigned int old, new;
2143180c329dSTimur Tabi 	int ret;
2144db2a4165SFrank Mandarino 
21458a713da8SMark Brown 	if (codec->using_regmap) {
21468a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
21478a713da8SMark Brown 					       mask, value, &change);
21488a713da8SMark Brown 	} else {
2149180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
2150180c329dSTimur Tabi 		if (ret < 0)
2151180c329dSTimur Tabi 			return ret;
2152180c329dSTimur Tabi 
2153180c329dSTimur Tabi 		old = ret;
215478bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
2155db2a4165SFrank Mandarino 		change = old != new;
21568a713da8SMark Brown 		if (change)
2157180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
21588a713da8SMark Brown 	}
21598a713da8SMark Brown 
2160180c329dSTimur Tabi 	if (ret < 0)
2161180c329dSTimur Tabi 		return ret;
2162db2a4165SFrank Mandarino 
2163db2a4165SFrank Mandarino 	return change;
2164db2a4165SFrank Mandarino }
2165db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2166db2a4165SFrank Mandarino 
2167db2a4165SFrank Mandarino /**
21686c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
21696c508c62SEero Nurkkala  * @codec: audio codec
21706c508c62SEero Nurkkala  * @reg: codec register
21716c508c62SEero Nurkkala  * @mask: register mask
21726c508c62SEero Nurkkala  * @value: new value
21736c508c62SEero Nurkkala  *
21746c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
21756c508c62SEero Nurkkala  *
21766c508c62SEero Nurkkala  * Returns 1 for change else 0.
21776c508c62SEero Nurkkala  */
2178dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
21796c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
21806c508c62SEero Nurkkala 			       unsigned int value)
21816c508c62SEero Nurkkala {
21826c508c62SEero Nurkkala 	int change;
21836c508c62SEero Nurkkala 
21846c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
21856c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
21866c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
21876c508c62SEero Nurkkala 
21886c508c62SEero Nurkkala 	return change;
21896c508c62SEero Nurkkala }
2190dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
21916c508c62SEero Nurkkala 
21926c508c62SEero Nurkkala /**
2193db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2194db2a4165SFrank Mandarino  * @codec: audio codec
2195db2a4165SFrank Mandarino  * @reg: codec register
2196db2a4165SFrank Mandarino  * @mask: register mask
2197db2a4165SFrank Mandarino  * @value: new value
2198db2a4165SFrank Mandarino  *
2199db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2200db2a4165SFrank Mandarino  * different from the old value.
2201db2a4165SFrank Mandarino  *
2202db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2203db2a4165SFrank Mandarino  */
2204db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
220546f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2206db2a4165SFrank Mandarino {
2207db2a4165SFrank Mandarino 	int change;
220846f5822fSDaniel Ribeiro 	unsigned int old, new;
2209db2a4165SFrank Mandarino 
2210db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2211db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2212db2a4165SFrank Mandarino 	change = old != new;
2213db2a4165SFrank Mandarino 
2214db2a4165SFrank Mandarino 	return change;
2215db2a4165SFrank Mandarino }
2216db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2217db2a4165SFrank Mandarino 
2218db2a4165SFrank Mandarino /**
2219db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2220db2a4165SFrank Mandarino  * @substream: the pcm substream
2221db2a4165SFrank Mandarino  * @hw: the hardware parameters
2222db2a4165SFrank Mandarino  *
2223db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2224db2a4165SFrank Mandarino  */
2225db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2226db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2227db2a4165SFrank Mandarino {
2228db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2229db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2230db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2231db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2232db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2233db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2234db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2235db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2236db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2237db2a4165SFrank Mandarino 	return 0;
2238db2a4165SFrank Mandarino }
2239db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2240db2a4165SFrank Mandarino 
2241db2a4165SFrank Mandarino /**
2242db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2243db2a4165SFrank Mandarino  * @_template: control template
2244db2a4165SFrank Mandarino  * @data: control private data
2245ac11a2b3SMark Brown  * @long_name: control long name
2246efb7ac3fSMark Brown  * @prefix: control name prefix
2247db2a4165SFrank Mandarino  *
2248db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2249db2a4165SFrank Mandarino  *
2250db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2251db2a4165SFrank Mandarino  */
2252db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22533056557fSMark Brown 				  void *data, const char *long_name,
2254efb7ac3fSMark Brown 				  const char *prefix)
2255db2a4165SFrank Mandarino {
2256db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2257efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2258efb7ac3fSMark Brown 	char *name = NULL;
2259efb7ac3fSMark Brown 	int name_len;
2260db2a4165SFrank Mandarino 
2261db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2262db2a4165SFrank Mandarino 	template.index = 0;
2263db2a4165SFrank Mandarino 
2264efb7ac3fSMark Brown 	if (!long_name)
2265efb7ac3fSMark Brown 		long_name = template.name;
2266efb7ac3fSMark Brown 
2267efb7ac3fSMark Brown 	if (prefix) {
2268efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
226957cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2270efb7ac3fSMark Brown 		if (!name)
2271efb7ac3fSMark Brown 			return NULL;
2272efb7ac3fSMark Brown 
2273efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2274efb7ac3fSMark Brown 
2275efb7ac3fSMark Brown 		template.name = name;
2276efb7ac3fSMark Brown 	} else {
2277efb7ac3fSMark Brown 		template.name = long_name;
2278efb7ac3fSMark Brown 	}
2279efb7ac3fSMark Brown 
2280efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2281efb7ac3fSMark Brown 
2282efb7ac3fSMark Brown 	kfree(name);
2283efb7ac3fSMark Brown 
2284efb7ac3fSMark Brown 	return kcontrol;
2285db2a4165SFrank Mandarino }
2286db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2287db2a4165SFrank Mandarino 
2288022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2289022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2290022658beSLiam Girdwood 	const char *prefix, void *data)
2291022658beSLiam Girdwood {
2292022658beSLiam Girdwood 	int err, i;
2293022658beSLiam Girdwood 
2294022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2295022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2296022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2297022658beSLiam Girdwood 						     control->name, prefix));
2298022658beSLiam Girdwood 		if (err < 0) {
2299f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2300f110bfc7SLiam Girdwood 				control->name, err);
2301022658beSLiam Girdwood 			return err;
2302022658beSLiam Girdwood 		}
2303022658beSLiam Girdwood 	}
2304022658beSLiam Girdwood 
2305022658beSLiam Girdwood 	return 0;
2306022658beSLiam Girdwood }
2307022658beSLiam Girdwood 
2308db2a4165SFrank Mandarino /**
2309022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2310022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
23113e8e1952SIan Molton  * duplicating this code.
23123e8e1952SIan Molton  *
23133e8e1952SIan Molton  * @codec: codec to add controls to
23143e8e1952SIan Molton  * @controls: array of controls to add
23153e8e1952SIan Molton  * @num_controls: number of elements in the array
23163e8e1952SIan Molton  *
23173e8e1952SIan Molton  * Return 0 for success, else error.
23183e8e1952SIan Molton  */
2319022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
23203e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
23213e8e1952SIan Molton {
2322f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
23233e8e1952SIan Molton 
2324022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2325022658beSLiam Girdwood 			codec->name_prefix, codec);
23263e8e1952SIan Molton }
2327022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
23283e8e1952SIan Molton 
23293e8e1952SIan Molton /**
2330a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2331022658beSLiam Girdwood  * Convenience function to add a list of controls.
2332a491a5c8SLiam Girdwood  *
2333a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2334a491a5c8SLiam Girdwood  * @controls: array of controls to add
2335a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2336a491a5c8SLiam Girdwood  *
2337a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2338a491a5c8SLiam Girdwood  */
2339a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2340a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2341a491a5c8SLiam Girdwood {
2342a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2343a491a5c8SLiam Girdwood 
2344022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2345022658beSLiam Girdwood 			NULL, platform);
2346a491a5c8SLiam Girdwood }
2347a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2348a491a5c8SLiam Girdwood 
2349a491a5c8SLiam Girdwood /**
2350022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2351022658beSLiam Girdwood  * Convenience function to add a list of controls.
2352022658beSLiam Girdwood  *
2353022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2354022658beSLiam Girdwood  * @controls: array of controls to add
2355022658beSLiam Girdwood  * @num_controls: number of elements in the array
2356022658beSLiam Girdwood  *
2357022658beSLiam Girdwood  * Return 0 for success, else error.
2358022658beSLiam Girdwood  */
2359022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2360022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2361022658beSLiam Girdwood {
2362022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2363022658beSLiam Girdwood 
2364022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2365022658beSLiam Girdwood 			NULL, soc_card);
2366022658beSLiam Girdwood }
2367022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2368022658beSLiam Girdwood 
2369022658beSLiam Girdwood /**
2370022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2371022658beSLiam Girdwood  * Convienience function to add a list of controls.
2372022658beSLiam Girdwood  *
2373022658beSLiam Girdwood  * @dai: DAI to add controls to
2374022658beSLiam Girdwood  * @controls: array of controls to add
2375022658beSLiam Girdwood  * @num_controls: number of elements in the array
2376022658beSLiam Girdwood  *
2377022658beSLiam Girdwood  * Return 0 for success, else error.
2378022658beSLiam Girdwood  */
2379022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2380022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2381022658beSLiam Girdwood {
2382022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2383022658beSLiam Girdwood 
2384022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2385022658beSLiam Girdwood 			NULL, dai);
2386022658beSLiam Girdwood }
2387022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2388022658beSLiam Girdwood 
2389022658beSLiam Girdwood /**
2390db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2391db2a4165SFrank Mandarino  * @kcontrol: mixer control
2392db2a4165SFrank Mandarino  * @uinfo: control element information
2393db2a4165SFrank Mandarino  *
2394db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2395db2a4165SFrank Mandarino  * mixer control.
2396db2a4165SFrank Mandarino  *
2397db2a4165SFrank Mandarino  * Returns 0 for success.
2398db2a4165SFrank Mandarino  */
2399db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2400db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2401db2a4165SFrank Mandarino {
2402db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2403db2a4165SFrank Mandarino 
2404db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2405db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2406f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2407db2a4165SFrank Mandarino 
2408f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2409f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2410db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2411db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2412db2a4165SFrank Mandarino 	return 0;
2413db2a4165SFrank Mandarino }
2414db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2415db2a4165SFrank Mandarino 
2416db2a4165SFrank Mandarino /**
2417db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2418db2a4165SFrank Mandarino  * @kcontrol: mixer control
2419ac11a2b3SMark Brown  * @ucontrol: control element information
2420db2a4165SFrank Mandarino  *
2421db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2422db2a4165SFrank Mandarino  *
2423db2a4165SFrank Mandarino  * Returns 0 for success.
2424db2a4165SFrank Mandarino  */
2425db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2426db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2427db2a4165SFrank Mandarino {
2428db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2429db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
243086767b7dSLars-Peter Clausen 	unsigned int val;
2431db2a4165SFrank Mandarino 
2432db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
24333ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
243486767b7dSLars-Peter Clausen 		= (val >> e->shift_l) & e->mask;
2435db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2436db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
243786767b7dSLars-Peter Clausen 			(val >> e->shift_r) & e->mask;
2438db2a4165SFrank Mandarino 
2439db2a4165SFrank Mandarino 	return 0;
2440db2a4165SFrank Mandarino }
2441db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2442db2a4165SFrank Mandarino 
2443db2a4165SFrank Mandarino /**
2444db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2445db2a4165SFrank Mandarino  * @kcontrol: mixer control
2446ac11a2b3SMark Brown  * @ucontrol: control element information
2447db2a4165SFrank Mandarino  *
2448db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2449db2a4165SFrank Mandarino  *
2450db2a4165SFrank Mandarino  * Returns 0 for success.
2451db2a4165SFrank Mandarino  */
2452db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2453db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2454db2a4165SFrank Mandarino {
2455db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2456db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
245746f5822fSDaniel Ribeiro 	unsigned int val;
245886767b7dSLars-Peter Clausen 	unsigned int mask;
2459db2a4165SFrank Mandarino 
2460f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2461db2a4165SFrank Mandarino 		return -EINVAL;
2462db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
246386767b7dSLars-Peter Clausen 	mask = e->mask << e->shift_l;
2464db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2465f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2466db2a4165SFrank Mandarino 			return -EINVAL;
2467db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
246886767b7dSLars-Peter Clausen 		mask |= e->mask << e->shift_r;
2469db2a4165SFrank Mandarino 	}
2470db2a4165SFrank Mandarino 
24716c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2472db2a4165SFrank Mandarino }
2473db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2474db2a4165SFrank Mandarino 
2475db2a4165SFrank Mandarino /**
24762e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
24772e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
24782e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
24792e72f8e3SPeter Ujfalusi  *
24802e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
24812e72f8e3SPeter Ujfalusi  *
24822e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24832e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24842e72f8e3SPeter Ujfalusi  *
24852e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24862e72f8e3SPeter Ujfalusi  */
24872e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
24882e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
24892e72f8e3SPeter Ujfalusi {
24902e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
249174155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
249246f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
24932e72f8e3SPeter Ujfalusi 
24942e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
24952e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
24962e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
24972e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
24982e72f8e3SPeter Ujfalusi 			break;
24992e72f8e3SPeter Ujfalusi 	}
25002e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
25012e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
25022e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
25032e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
25042e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
25052e72f8e3SPeter Ujfalusi 				break;
25062e72f8e3SPeter Ujfalusi 		}
25072e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
25082e72f8e3SPeter Ujfalusi 	}
25092e72f8e3SPeter Ujfalusi 
25102e72f8e3SPeter Ujfalusi 	return 0;
25112e72f8e3SPeter Ujfalusi }
25122e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
25132e72f8e3SPeter Ujfalusi 
25142e72f8e3SPeter Ujfalusi /**
25152e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
25162e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
25172e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
25182e72f8e3SPeter Ujfalusi  *
25192e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
25202e72f8e3SPeter Ujfalusi  *
25212e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
25222e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
25232e72f8e3SPeter Ujfalusi  *
25242e72f8e3SPeter Ujfalusi  * Returns 0 for success.
25252e72f8e3SPeter Ujfalusi  */
25262e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
25272e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
25282e72f8e3SPeter Ujfalusi {
25292e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
253074155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
253146f5822fSDaniel Ribeiro 	unsigned int val;
253246f5822fSDaniel Ribeiro 	unsigned int mask;
25332e72f8e3SPeter Ujfalusi 
25342e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
25352e72f8e3SPeter Ujfalusi 		return -EINVAL;
25362e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
25372e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
25382e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
25392e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
25402e72f8e3SPeter Ujfalusi 			return -EINVAL;
25412e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
25422e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
25432e72f8e3SPeter Ujfalusi 	}
25442e72f8e3SPeter Ujfalusi 
25456c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
25462e72f8e3SPeter Ujfalusi }
25472e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
25482e72f8e3SPeter Ujfalusi 
25492e72f8e3SPeter Ujfalusi /**
2550db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2551db2a4165SFrank Mandarino  * @kcontrol: mixer control
2552db2a4165SFrank Mandarino  * @uinfo: control element information
2553db2a4165SFrank Mandarino  *
2554db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2555db2a4165SFrank Mandarino  * single mixer.
2556db2a4165SFrank Mandarino  *
2557db2a4165SFrank Mandarino  * Returns 0 for success.
2558db2a4165SFrank Mandarino  */
2559db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2560db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2561db2a4165SFrank Mandarino {
2562db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2563db2a4165SFrank Mandarino 
2564db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2565db2a4165SFrank Mandarino 	uinfo->count = 1;
2566f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2567db2a4165SFrank Mandarino 
2568f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2569f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2570db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2571db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2572db2a4165SFrank Mandarino 	return 0;
2573db2a4165SFrank Mandarino }
2574db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2575db2a4165SFrank Mandarino 
2576db2a4165SFrank Mandarino /**
2577db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2578db2a4165SFrank Mandarino  * @kcontrol: mixer control
2579db2a4165SFrank Mandarino  * @uinfo: control element information
2580db2a4165SFrank Mandarino  *
2581db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2582db2a4165SFrank Mandarino  *
2583db2a4165SFrank Mandarino  * Returns 0 for success.
2584db2a4165SFrank Mandarino  */
2585db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2586db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2587db2a4165SFrank Mandarino {
2588a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2589db2a4165SFrank Mandarino 
2590fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2591a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2592a7a4ac86SPhilipp Zabel 	else
2593a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2594a7a4ac86SPhilipp Zabel 
2595db2a4165SFrank Mandarino 	uinfo->count = 1;
2596db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2597a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2598db2a4165SFrank Mandarino 	return 0;
2599db2a4165SFrank Mandarino }
2600db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2601db2a4165SFrank Mandarino 
2602db2a4165SFrank Mandarino /**
2603db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2604db2a4165SFrank Mandarino  * @kcontrol: mixer control
2605db2a4165SFrank Mandarino  * @uinfo: control element information
2606db2a4165SFrank Mandarino  *
2607e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2608e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2609db2a4165SFrank Mandarino  *
2610db2a4165SFrank Mandarino  * Returns 0 for success.
2611db2a4165SFrank Mandarino  */
2612db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2613db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2614db2a4165SFrank Mandarino {
26154eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26164eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2617d11bb4a9SPeter Ujfalusi 	int platform_max;
2618db2a4165SFrank Mandarino 
2619d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2620d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2621d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2622d11bb4a9SPeter Ujfalusi 
2623d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2624a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2625a7a4ac86SPhilipp Zabel 	else
2626a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2627a7a4ac86SPhilipp Zabel 
2628e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2629db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2630d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2631db2a4165SFrank Mandarino 	return 0;
2632db2a4165SFrank Mandarino }
2633db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2634db2a4165SFrank Mandarino 
2635db2a4165SFrank Mandarino /**
2636db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2637db2a4165SFrank Mandarino  * @kcontrol: mixer control
2638ac11a2b3SMark Brown  * @ucontrol: control element information
2639db2a4165SFrank Mandarino  *
2640f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2641f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2642db2a4165SFrank Mandarino  *
2643db2a4165SFrank Mandarino  * Returns 0 for success.
2644db2a4165SFrank Mandarino  */
2645db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2646db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2647db2a4165SFrank Mandarino {
26484eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26494eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2650db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2651815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2652f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2653815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2654815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26554eaa9819SJon Smirl 	int max = mc->max;
2656815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2657815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2658db2a4165SFrank Mandarino 
2659db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2660db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2661f7915d99SPeter Ujfalusi 	if (invert)
2662db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2663a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2664f7915d99SPeter Ujfalusi 
2665f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2666f7915d99SPeter Ujfalusi 		if (reg == reg2)
2667f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2668f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2669f7915d99SPeter Ujfalusi 		else
2670f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2671f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2672f7915d99SPeter Ujfalusi 		if (invert)
2673db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2674a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2675db2a4165SFrank Mandarino 	}
2676db2a4165SFrank Mandarino 
2677db2a4165SFrank Mandarino 	return 0;
2678db2a4165SFrank Mandarino }
2679db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2680db2a4165SFrank Mandarino 
2681db2a4165SFrank Mandarino /**
2682db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2683db2a4165SFrank Mandarino  * @kcontrol: mixer control
2684ac11a2b3SMark Brown  * @ucontrol: control element information
2685db2a4165SFrank Mandarino  *
2686974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2687974815baSPeter Ujfalusi  * control that spans 2 registers.
2688db2a4165SFrank Mandarino  *
2689db2a4165SFrank Mandarino  * Returns 0 for success.
2690db2a4165SFrank Mandarino  */
2691db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2692db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2693db2a4165SFrank Mandarino {
26944eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26954eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2696db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2697815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2698974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2699815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2700815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
27014eaa9819SJon Smirl 	int max = mc->max;
2702815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2703815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2704974815baSPeter Ujfalusi 	int err;
2705974815baSPeter Ujfalusi 	bool type_2r = 0;
2706974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2707974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2708db2a4165SFrank Mandarino 
2709db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2710db2a4165SFrank Mandarino 	if (invert)
2711a7a4ac86SPhilipp Zabel 		val = max - val;
2712db2a4165SFrank Mandarino 	val_mask = mask << shift;
2713db2a4165SFrank Mandarino 	val = val << shift;
2714974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2715db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2716db2a4165SFrank Mandarino 		if (invert)
2717a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2718974815baSPeter Ujfalusi 		if (reg == reg2) {
2719db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2720db2a4165SFrank Mandarino 			val |= val2 << rshift;
2721974815baSPeter Ujfalusi 		} else {
2722db2a4165SFrank Mandarino 			val2 = val2 << shift;
2723974815baSPeter Ujfalusi 			type_2r = 1;
2724974815baSPeter Ujfalusi 		}
2725974815baSPeter Ujfalusi 	}
27266c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
27273ff3f64bSMark Brown 	if (err < 0)
2728db2a4165SFrank Mandarino 		return err;
2729db2a4165SFrank Mandarino 
2730974815baSPeter Ujfalusi 	if (type_2r)
27316c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2732974815baSPeter Ujfalusi 
2733db2a4165SFrank Mandarino 	return err;
2734db2a4165SFrank Mandarino }
2735974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2736db2a4165SFrank Mandarino 
2737e13ac2e9SMark Brown /**
27381d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
27391d99f243SBrian Austin  * @kcontrol: mixer control
27401d99f243SBrian Austin  * @ucontrol: control element information
27411d99f243SBrian Austin  *
27421d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
27431d99f243SBrian Austin  * control that spans 2 registers.
27441d99f243SBrian Austin  *
27451d99f243SBrian Austin  * Returns 0 for success.
27461d99f243SBrian Austin  */
27471d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
27481d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
27491d99f243SBrian Austin {
27501d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
27511d99f243SBrian Austin 	struct soc_mixer_control *mc =
27521d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
27531d99f243SBrian Austin 
27541d99f243SBrian Austin 	unsigned int reg = mc->reg;
27551d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
27561d99f243SBrian Austin 	unsigned int shift = mc->shift;
27571d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
27581d99f243SBrian Austin 	int max = mc->max;
27591d99f243SBrian Austin 	int min = mc->min;
27601d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
27611d99f243SBrian Austin 
27621d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
27631d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
27641d99f243SBrian Austin 
27651d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
27661d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
27671d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
27681d99f243SBrian Austin 
27691d99f243SBrian Austin 	return 0;
27701d99f243SBrian Austin }
27711d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
27721d99f243SBrian Austin 
27731d99f243SBrian Austin /**
27741d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
27751d99f243SBrian Austin  * @kcontrol: mixer control
27761d99f243SBrian Austin  * @uinfo: control element information
27771d99f243SBrian Austin  *
27781d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
27791d99f243SBrian Austin  *
27801d99f243SBrian Austin  * Returns 0 for success.
27811d99f243SBrian Austin  */
27821d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
27831d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
27841d99f243SBrian Austin {
27851d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
27861d99f243SBrian Austin 	struct soc_mixer_control *mc =
27871d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
27881d99f243SBrian Austin 
27891d99f243SBrian Austin 	unsigned int reg = mc->reg;
27901d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
27911d99f243SBrian Austin 	unsigned int shift = mc->shift;
27921d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
27931d99f243SBrian Austin 	int max = mc->max;
27941d99f243SBrian Austin 	int min = mc->min;
27951d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
279627f1d759SBrian Austin 	int err = 0;
27971d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
27981d99f243SBrian Austin 
27991d99f243SBrian Austin 	val_mask = mask << shift;
28001d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
28011d99f243SBrian Austin 	val = val << shift;
28021d99f243SBrian Austin 
2803d055852eSMukund Navada 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2804d055852eSMukund Navada 	if (err < 0)
28051d99f243SBrian Austin 		return err;
28061d99f243SBrian Austin 
28071d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
28081d99f243SBrian Austin 		val_mask = mask << rshift;
28091d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
28101d99f243SBrian Austin 		val2 = val2 << rshift;
28111d99f243SBrian Austin 
28121d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
28131d99f243SBrian Austin 			return err;
28141d99f243SBrian Austin 	}
28151d99f243SBrian Austin 	return 0;
28161d99f243SBrian Austin }
28171d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
28181d99f243SBrian Austin 
28191d99f243SBrian Austin /**
2820e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2821e13ac2e9SMark Brown  * @kcontrol: mixer control
2822e13ac2e9SMark Brown  * @uinfo: control element information
2823e13ac2e9SMark Brown  *
2824e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2825e13ac2e9SMark Brown  *
2826e13ac2e9SMark Brown  * Returns 0 for success.
2827e13ac2e9SMark Brown  */
2828e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2829e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2830e13ac2e9SMark Brown {
28314eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28324eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2833d11bb4a9SPeter Ujfalusi 	int platform_max;
28344eaa9819SJon Smirl 	int min = mc->min;
2835e13ac2e9SMark Brown 
2836d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2837d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2838d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2839d11bb4a9SPeter Ujfalusi 
2840e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2841e13ac2e9SMark Brown 	uinfo->count = 2;
2842e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2843d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2844e13ac2e9SMark Brown 	return 0;
2845e13ac2e9SMark Brown }
2846e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2847e13ac2e9SMark Brown 
2848e13ac2e9SMark Brown /**
2849e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2850e13ac2e9SMark Brown  * @kcontrol: mixer control
2851ac11a2b3SMark Brown  * @ucontrol: control element information
2852e13ac2e9SMark Brown  *
2853e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2854e13ac2e9SMark Brown  *
2855e13ac2e9SMark Brown  * Returns 0 for success.
2856e13ac2e9SMark Brown  */
2857e13ac2e9SMark Brown int snd_soc_get_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;
2865e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2866e13ac2e9SMark Brown 
2867e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2868e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2869e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2870e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2871e13ac2e9SMark Brown 	return 0;
2872e13ac2e9SMark Brown }
2873e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2874e13ac2e9SMark Brown 
2875e13ac2e9SMark Brown /**
2876e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2877e13ac2e9SMark Brown  * @kcontrol: mixer control
2878ac11a2b3SMark Brown  * @ucontrol: control element information
2879e13ac2e9SMark Brown  *
2880e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2881e13ac2e9SMark Brown  *
2882e13ac2e9SMark Brown  * Returns 0 for success.
2883e13ac2e9SMark Brown  */
2884e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2885e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2886e13ac2e9SMark Brown {
28874eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28884eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2889e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2890815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
28914eaa9819SJon Smirl 	int min = mc->min;
289246f5822fSDaniel Ribeiro 	unsigned int val;
2893e13ac2e9SMark Brown 
2894e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2895e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2896e13ac2e9SMark Brown 
28976c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2898e13ac2e9SMark Brown }
2899e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2900e13ac2e9SMark Brown 
29018c6529dbSLiam Girdwood /**
29026c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
29036c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29046c9d8cf6SAdam Thomson  * @uinfo: control element information
29056c9d8cf6SAdam Thomson  *
29066c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
29076c9d8cf6SAdam Thomson  * mixer control.
29086c9d8cf6SAdam Thomson  *
29096c9d8cf6SAdam Thomson  * returns 0 for success.
29106c9d8cf6SAdam Thomson  */
29116c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
29126c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
29136c9d8cf6SAdam Thomson {
29146c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29156c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29166c9d8cf6SAdam Thomson 	int platform_max;
29176c9d8cf6SAdam Thomson 	int min = mc->min;
29186c9d8cf6SAdam Thomson 
29196c9d8cf6SAdam Thomson 	if (!mc->platform_max)
29206c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
29216c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
29226c9d8cf6SAdam Thomson 
29236c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29246c9d8cf6SAdam Thomson 	uinfo->count = 1;
29256c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
29266c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
29276c9d8cf6SAdam Thomson 
29286c9d8cf6SAdam Thomson 	return 0;
29296c9d8cf6SAdam Thomson }
29306c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
29316c9d8cf6SAdam Thomson 
29326c9d8cf6SAdam Thomson /**
29336c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
29346c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29356c9d8cf6SAdam Thomson  * @ucontrol: control element information
29366c9d8cf6SAdam Thomson  *
29376c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
29386c9d8cf6SAdam Thomson  *
29396c9d8cf6SAdam Thomson  * Returns 0 for success.
29406c9d8cf6SAdam Thomson  */
29416c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
29426c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
29436c9d8cf6SAdam Thomson {
29446c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29456c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29466c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29476c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
29486c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
29496c9d8cf6SAdam Thomson 	int min = mc->min;
29506c9d8cf6SAdam Thomson 	int max = mc->max;
29516c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
29526c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
29536c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
29546c9d8cf6SAdam Thomson 
29556c9d8cf6SAdam Thomson 	val = ((ucontrol->value.integer.value[0] + min) & mask);
29566c9d8cf6SAdam Thomson 	if (invert)
29576c9d8cf6SAdam Thomson 		val = max - val;
29586c9d8cf6SAdam Thomson 	val_mask = mask << shift;
29596c9d8cf6SAdam Thomson 	val = val << shift;
29606c9d8cf6SAdam Thomson 
29616c9d8cf6SAdam Thomson 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
29626c9d8cf6SAdam Thomson }
29636c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
29646c9d8cf6SAdam Thomson 
29656c9d8cf6SAdam Thomson /**
29666c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
29676c9d8cf6SAdam Thomson  * @kcontrol: mixer control
29686c9d8cf6SAdam Thomson  * @ucontrol: control element information
29696c9d8cf6SAdam Thomson  *
29706c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
29716c9d8cf6SAdam Thomson  *
29726c9d8cf6SAdam Thomson  * Returns 0 for success.
29736c9d8cf6SAdam Thomson  */
29746c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
29756c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
29766c9d8cf6SAdam Thomson {
29776c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
29786c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
29796c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29806c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
29816c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
29826c9d8cf6SAdam Thomson 	int min = mc->min;
29836c9d8cf6SAdam Thomson 	int max = mc->max;
29846c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
29856c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
29866c9d8cf6SAdam Thomson 
29876c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
29886c9d8cf6SAdam Thomson 		(snd_soc_read(codec, reg) >> shift) & mask;
29896c9d8cf6SAdam Thomson 	if (invert)
29906c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
29916c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
29926c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
29936c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] - min;
29946c9d8cf6SAdam Thomson 
29956c9d8cf6SAdam Thomson 	return 0;
29966c9d8cf6SAdam Thomson }
29976c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
29986c9d8cf6SAdam Thomson 
29996c9d8cf6SAdam Thomson /**
3000637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
3001637d3847SPeter Ujfalusi  *
3002637d3847SPeter Ujfalusi  * @codec: where to look for the control
3003637d3847SPeter Ujfalusi  * @name: Name of the control
3004637d3847SPeter Ujfalusi  * @max: new maximum limit
3005637d3847SPeter Ujfalusi  *
3006637d3847SPeter Ujfalusi  * Return 0 for success, else error.
3007637d3847SPeter Ujfalusi  */
3008637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
3009637d3847SPeter Ujfalusi 	const char *name, int max)
3010637d3847SPeter Ujfalusi {
3011f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
3012637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
3013637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
3014637d3847SPeter Ujfalusi 	int found = 0;
3015637d3847SPeter Ujfalusi 	int ret = -EINVAL;
3016637d3847SPeter Ujfalusi 
3017637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
3018637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
3019637d3847SPeter Ujfalusi 		return -EINVAL;
3020637d3847SPeter Ujfalusi 
3021637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
3022637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3023637d3847SPeter Ujfalusi 			found = 1;
3024637d3847SPeter Ujfalusi 			break;
3025637d3847SPeter Ujfalusi 		}
3026637d3847SPeter Ujfalusi 	}
3027637d3847SPeter Ujfalusi 	if (found) {
3028637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3029637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3030d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3031637d3847SPeter Ujfalusi 			ret = 0;
3032637d3847SPeter Ujfalusi 		}
3033637d3847SPeter Ujfalusi 	}
3034637d3847SPeter Ujfalusi 	return ret;
3035637d3847SPeter Ujfalusi }
3036637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3037637d3847SPeter Ujfalusi 
303871d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
303971d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
304071d08516SMark Brown {
304171d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
304271d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
304371d08516SMark Brown 
304471d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
304571d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
304671d08516SMark Brown 
304771d08516SMark Brown 	return 0;
304871d08516SMark Brown }
304971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
305071d08516SMark Brown 
305171d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
305271d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
305371d08516SMark Brown {
305471d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
305571d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
305671d08516SMark Brown 	int ret;
305771d08516SMark Brown 
305871d08516SMark Brown 	if (codec->using_regmap)
305971d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
306071d08516SMark Brown 				      ucontrol->value.bytes.data,
306171d08516SMark Brown 				      params->num_regs * codec->val_bytes);
306271d08516SMark Brown 	else
306371d08516SMark Brown 		ret = -EINVAL;
306471d08516SMark Brown 
3065f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
3066f831b055SMark Brown 	if (ret == 0 && params->mask) {
3067f831b055SMark Brown 		switch (codec->val_bytes) {
3068f831b055SMark Brown 		case 1:
3069f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
3070f831b055SMark Brown 			break;
3071f831b055SMark Brown 		case 2:
3072f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
3073f831b055SMark Brown 				&= ~params->mask;
3074f831b055SMark Brown 			break;
3075f831b055SMark Brown 		case 4:
3076f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
3077f831b055SMark Brown 				&= ~params->mask;
3078f831b055SMark Brown 			break;
3079f831b055SMark Brown 		default:
3080f831b055SMark Brown 			return -EINVAL;
3081f831b055SMark Brown 		}
3082f831b055SMark Brown 	}
3083f831b055SMark Brown 
308471d08516SMark Brown 	return ret;
308571d08516SMark Brown }
308671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
308771d08516SMark Brown 
308871d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
308971d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
309071d08516SMark Brown {
309171d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
309271d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3093f831b055SMark Brown 	int ret, len;
3094f831b055SMark Brown 	unsigned int val;
3095f831b055SMark Brown 	void *data;
309671d08516SMark Brown 
3097f831b055SMark Brown 	if (!codec->using_regmap)
3098f831b055SMark Brown 		return -EINVAL;
3099f831b055SMark Brown 
3100f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
3101f831b055SMark Brown 
3102*b5a8fe43SMark Brown 	data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3103*b5a8fe43SMark Brown 	if (!data)
3104*b5a8fe43SMark Brown 		return -ENOMEM;
3105*b5a8fe43SMark Brown 
3106f831b055SMark Brown 	/*
3107f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3108f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3109f831b055SMark Brown 	 * copy.
3110f831b055SMark Brown 	 */
3111f831b055SMark Brown 	if (params->mask) {
3112f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
3113f831b055SMark Brown 		if (ret != 0)
3114f831b055SMark Brown 			return ret;
3115f831b055SMark Brown 
3116f831b055SMark Brown 		val &= params->mask;
3117f831b055SMark Brown 
3118f831b055SMark Brown 		switch (codec->val_bytes) {
3119f831b055SMark Brown 		case 1:
3120f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3121f831b055SMark Brown 			((u8 *)data)[0] |= val;
3122f831b055SMark Brown 			break;
3123f831b055SMark Brown 		case 2:
3124f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3125f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
3126f831b055SMark Brown 			break;
3127f831b055SMark Brown 		case 4:
3128f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3129f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
3130f831b055SMark Brown 			break;
3131f831b055SMark Brown 		default:
3132f831b055SMark Brown 			return -EINVAL;
3133f831b055SMark Brown 		}
3134f831b055SMark Brown 	}
3135f831b055SMark Brown 
313671d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
3137f831b055SMark Brown 			       data, len);
3138f831b055SMark Brown 
3139f831b055SMark Brown 	kfree(data);
314071d08516SMark Brown 
314171d08516SMark Brown 	return ret;
314271d08516SMark Brown }
314371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
314471d08516SMark Brown 
3145b6f4bb38Sapatard@mandriva.com /**
31464183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
31474183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
31484183eed2SKristoffer KARLSSON  * @uinfo: control element information
31494183eed2SKristoffer KARLSSON  *
31504183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
31514183eed2SKristoffer KARLSSON  * span multiple codec registers which together
31524183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
31534183eed2SKristoffer KARLSSON  *
31544183eed2SKristoffer KARLSSON  * Returns 0 for success.
31554183eed2SKristoffer KARLSSON  */
31564183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
31574183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
31584183eed2SKristoffer KARLSSON {
31594183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
31604183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
31614183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
31624183eed2SKristoffer KARLSSON 	uinfo->count = 1;
31634183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
31644183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
31654183eed2SKristoffer KARLSSON 
31664183eed2SKristoffer KARLSSON 	return 0;
31674183eed2SKristoffer KARLSSON }
31684183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
31694183eed2SKristoffer KARLSSON 
31704183eed2SKristoffer KARLSSON /**
31714183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
31724183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
31734183eed2SKristoffer KARLSSON  * @ucontrol: control element information
31744183eed2SKristoffer KARLSSON  *
31754183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
31764183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
31774183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
31784183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
31794183eed2SKristoffer KARLSSON  * across the multiple codec registers.
31804183eed2SKristoffer KARLSSON  *
31814183eed2SKristoffer KARLSSON  * Returns 0 for success.
31824183eed2SKristoffer KARLSSON  */
31834183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
31844183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
31854183eed2SKristoffer KARLSSON {
31864183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
31874183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
31884183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
31894183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
31904183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
31914183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
31924183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
31934183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
31944183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
31954183eed2SKristoffer KARLSSON 	long min = mc->min;
31964183eed2SKristoffer KARLSSON 	long max = mc->max;
31974183eed2SKristoffer KARLSSON 	long val = 0;
31984183eed2SKristoffer KARLSSON 	unsigned long regval;
31994183eed2SKristoffer KARLSSON 	unsigned int i;
32004183eed2SKristoffer KARLSSON 
32014183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
32024183eed2SKristoffer KARLSSON 		regval = snd_soc_read(codec, regbase+i) & regwmask;
32034183eed2SKristoffer KARLSSON 		val |= regval << (regwshift*(regcount-i-1));
32044183eed2SKristoffer KARLSSON 	}
32054183eed2SKristoffer KARLSSON 	val &= mask;
32064183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
32074183eed2SKristoffer KARLSSON 		val |= ~mask;
32084183eed2SKristoffer KARLSSON 	if (invert)
32094183eed2SKristoffer KARLSSON 		val = max - val;
32104183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
32114183eed2SKristoffer KARLSSON 
32124183eed2SKristoffer KARLSSON 	return 0;
32134183eed2SKristoffer KARLSSON }
32144183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
32154183eed2SKristoffer KARLSSON 
32164183eed2SKristoffer KARLSSON /**
32174183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
32184183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
32194183eed2SKristoffer KARLSSON  * @ucontrol: control element information
32204183eed2SKristoffer KARLSSON  *
32214183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
32224183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
32234183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
32244183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
32254183eed2SKristoffer KARLSSON  * across the multiple codec registers.
32264183eed2SKristoffer KARLSSON  *
32274183eed2SKristoffer KARLSSON  * Returns 0 for success.
32284183eed2SKristoffer KARLSSON  */
32294183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
32304183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
32314183eed2SKristoffer KARLSSON {
32324183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
32334183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
32344183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
32354183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
32364183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
32374183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
32384183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
32394183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
32404183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
32414183eed2SKristoffer KARLSSON 	long max = mc->max;
32424183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
32434183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
32444183eed2SKristoffer KARLSSON 	int err;
32454183eed2SKristoffer KARLSSON 
32464183eed2SKristoffer KARLSSON 	if (invert)
32474183eed2SKristoffer KARLSSON 		val = max - val;
32484183eed2SKristoffer KARLSSON 	val &= mask;
32494183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
32504183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
32514183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
32524183eed2SKristoffer KARLSSON 		err = snd_soc_update_bits_locked(codec, regbase+i,
32534183eed2SKristoffer KARLSSON 				regmask, regval);
32544183eed2SKristoffer KARLSSON 		if (err < 0)
32554183eed2SKristoffer KARLSSON 			return err;
32564183eed2SKristoffer KARLSSON 	}
32574183eed2SKristoffer KARLSSON 
32584183eed2SKristoffer KARLSSON 	return 0;
32594183eed2SKristoffer KARLSSON }
32604183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
32614183eed2SKristoffer KARLSSON 
32624183eed2SKristoffer KARLSSON /**
3263dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3264dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3265dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3266dd7b10b3SKristoffer KARLSSON  *
3267dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3268dd7b10b3SKristoffer KARLSSON  *
3269dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3270dd7b10b3SKristoffer KARLSSON  */
3271dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3272dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3273dd7b10b3SKristoffer KARLSSON {
3274dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3275dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3276dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3277dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3278dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3279dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3280dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3281dd7b10b3SKristoffer KARLSSON 	unsigned int val = snd_soc_read(codec, reg) & mask;
3282dd7b10b3SKristoffer KARLSSON 
3283dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3284dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3285dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3286dd7b10b3SKristoffer KARLSSON 
3287dd7b10b3SKristoffer KARLSSON 	return 0;
3288dd7b10b3SKristoffer KARLSSON }
3289dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3290dd7b10b3SKristoffer KARLSSON 
3291dd7b10b3SKristoffer KARLSSON /**
3292dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3293dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3294dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3295dd7b10b3SKristoffer KARLSSON  *
3296dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3297dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3298dd7b10b3SKristoffer KARLSSON  *
3299dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3300dd7b10b3SKristoffer KARLSSON  */
3301dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3302dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3303dd7b10b3SKristoffer KARLSSON {
3304dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3305dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3306dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3307dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3308dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3309dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3310dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3311dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3312dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3313dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3314dd7b10b3SKristoffer KARLSSON 	int err;
3315dd7b10b3SKristoffer KARLSSON 
3316dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3317dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3318dd7b10b3SKristoffer KARLSSON 		return err;
3319dd7b10b3SKristoffer KARLSSON 
3320dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3321dd7b10b3SKristoffer KARLSSON 	return err;
3322dd7b10b3SKristoffer KARLSSON }
3323dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3324dd7b10b3SKristoffer KARLSSON 
3325dd7b10b3SKristoffer KARLSSON /**
33268c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
33278c6529dbSLiam Girdwood  * @dai: DAI
33288c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
33298c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
33308c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
33318c6529dbSLiam Girdwood  *
33328c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
33338c6529dbSLiam Girdwood  */
33348c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
33358c6529dbSLiam Girdwood 	unsigned int freq, int dir)
33368c6529dbSLiam Girdwood {
3337f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3338f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3339ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3340da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3341ec4ee52aSMark Brown 						      freq, dir);
33428c6529dbSLiam Girdwood 	else
33438c6529dbSLiam Girdwood 		return -EINVAL;
33448c6529dbSLiam Girdwood }
33458c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
33468c6529dbSLiam Girdwood 
33478c6529dbSLiam Girdwood /**
3348ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3349ec4ee52aSMark Brown  * @codec: CODEC
3350ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3351da1c6ea6SMark Brown  * @source: Source for the clock
3352ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3353ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3354ec4ee52aSMark Brown  *
3355ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3356ec4ee52aSMark Brown  */
3357ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3358da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3359ec4ee52aSMark Brown {
3360ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3361da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3362da1c6ea6SMark Brown 						 freq, dir);
3363ec4ee52aSMark Brown 	else
3364ec4ee52aSMark Brown 		return -EINVAL;
3365ec4ee52aSMark Brown }
3366ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3367ec4ee52aSMark Brown 
3368ec4ee52aSMark Brown /**
33698c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
33708c6529dbSLiam Girdwood  * @dai: DAI
3371ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
33728c6529dbSLiam Girdwood  * @div: new clock divisor.
33738c6529dbSLiam Girdwood  *
33748c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
33758c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
33768c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
33778c6529dbSLiam Girdwood  */
33788c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
33798c6529dbSLiam Girdwood 	int div_id, int div)
33808c6529dbSLiam Girdwood {
3381f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3382f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
33838c6529dbSLiam Girdwood 	else
33848c6529dbSLiam Girdwood 		return -EINVAL;
33858c6529dbSLiam Girdwood }
33868c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
33878c6529dbSLiam Girdwood 
33888c6529dbSLiam Girdwood /**
33898c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
33908c6529dbSLiam Girdwood  * @dai: DAI
33918c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
339285488037SMark Brown  * @source: DAI specific source for the PLL
33938c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
33948c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
33958c6529dbSLiam Girdwood  *
33968c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
33978c6529dbSLiam Girdwood  */
339885488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
339985488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
34008c6529dbSLiam Girdwood {
3401f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3402f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
340385488037SMark Brown 					 freq_in, freq_out);
3404ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3405ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3406ec4ee52aSMark Brown 						   freq_in, freq_out);
34078c6529dbSLiam Girdwood 	else
34088c6529dbSLiam Girdwood 		return -EINVAL;
34098c6529dbSLiam Girdwood }
34108c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
34118c6529dbSLiam Girdwood 
3412ec4ee52aSMark Brown /*
3413ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3414ec4ee52aSMark Brown  * @codec: CODEC
3415ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3416ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3417ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3418ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3419ec4ee52aSMark Brown  *
3420ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3421ec4ee52aSMark Brown  */
3422ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3423ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3424ec4ee52aSMark Brown {
3425ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3426ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3427ec4ee52aSMark Brown 					      freq_in, freq_out);
3428ec4ee52aSMark Brown 	else
3429ec4ee52aSMark Brown 		return -EINVAL;
3430ec4ee52aSMark Brown }
3431ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3432ec4ee52aSMark Brown 
34338c6529dbSLiam Girdwood /**
34348c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
34358c6529dbSLiam Girdwood  * @dai: DAI
34368c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
34378c6529dbSLiam Girdwood  *
34388c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
34398c6529dbSLiam Girdwood  */
34408c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
34418c6529dbSLiam Girdwood {
34425e4ba569SShawn Guo 	if (dai->driver == NULL)
34438c6529dbSLiam Girdwood 		return -EINVAL;
34445e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
34455e4ba569SShawn Guo 		return -ENOTSUPP;
34465e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
34478c6529dbSLiam Girdwood }
34488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
34498c6529dbSLiam Girdwood 
34508c6529dbSLiam Girdwood /**
34518c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
34528c6529dbSLiam Girdwood  * @dai: DAI
3453a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3454a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
34558c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3456a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
34578c6529dbSLiam Girdwood  *
34588c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
34598c6529dbSLiam Girdwood  * specific.
34608c6529dbSLiam Girdwood  */
34618c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3462a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
34638c6529dbSLiam Girdwood {
3464f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3465f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3466a5479e38SDaniel Ribeiro 				slots, slot_width);
34678c6529dbSLiam Girdwood 	else
34688c6529dbSLiam Girdwood 		return -EINVAL;
34698c6529dbSLiam Girdwood }
34708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
34718c6529dbSLiam Girdwood 
34728c6529dbSLiam Girdwood /**
3473472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3474472df3cbSBarry Song  * @dai: DAI
3475472df3cbSBarry Song  * @tx_num: how many TX channels
3476472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3477472df3cbSBarry Song  *           0~num-1 uses
3478472df3cbSBarry Song  * @rx_num: how many RX channels
3479472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3480472df3cbSBarry Song  *           0~num-1 uses
3481472df3cbSBarry Song  *
3482472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3483472df3cbSBarry Song  */
3484472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3485472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3486472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3487472df3cbSBarry Song {
3488f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3489f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3490472df3cbSBarry Song 			rx_num, rx_slot);
3491472df3cbSBarry Song 	else
3492472df3cbSBarry Song 		return -EINVAL;
3493472df3cbSBarry Song }
3494472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3495472df3cbSBarry Song 
3496472df3cbSBarry Song /**
34978c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
34988c6529dbSLiam Girdwood  * @dai: DAI
34998c6529dbSLiam Girdwood  * @tristate: tristate enable
35008c6529dbSLiam Girdwood  *
35018c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
35028c6529dbSLiam Girdwood  */
35038c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
35048c6529dbSLiam Girdwood {
3505f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3506f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
35078c6529dbSLiam Girdwood 	else
35088c6529dbSLiam Girdwood 		return -EINVAL;
35098c6529dbSLiam Girdwood }
35108c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
35118c6529dbSLiam Girdwood 
35128c6529dbSLiam Girdwood /**
35138c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
35148c6529dbSLiam Girdwood  * @dai: DAI
35158c6529dbSLiam Girdwood  * @mute: mute enable
35168c6529dbSLiam Girdwood  *
35178c6529dbSLiam Girdwood  * Mutes the DAI DAC.
35188c6529dbSLiam Girdwood  */
35198c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
35208c6529dbSLiam Girdwood {
3521f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3522f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
35238c6529dbSLiam Girdwood 	else
352404570c62SMark Brown 		return -ENOTSUPP;
35258c6529dbSLiam Girdwood }
35268c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
35278c6529dbSLiam Girdwood 
3528c5af3a2eSMark Brown /**
3529c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3530c5af3a2eSMark Brown  *
3531ac11a2b3SMark Brown  * @card: Card to register
3532c5af3a2eSMark Brown  *
3533c5af3a2eSMark Brown  */
353470a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3535c5af3a2eSMark Brown {
3536b19e6e7bSMark Brown 	int i, ret;
3537f0fba2adSLiam Girdwood 
3538c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3539c5af3a2eSMark Brown 		return -EINVAL;
3540c5af3a2eSMark Brown 
35415a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
35425a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
35435a504963SStephen Warren 
35445a504963SStephen Warren 		/*
35455a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
35465a504963SStephen Warren 		 * not both or neither.
35475a504963SStephen Warren 		 */
35485a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
3549f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: Neither/both codec"
3550f110bfc7SLiam Girdwood 				" name/of_node are set for %s\n", link->name);
35515a504963SStephen Warren 			return -EINVAL;
35525a504963SStephen Warren 		}
3553bc92657aSStephen Warren 		/* Codec DAI name must be specified */
3554bc92657aSStephen Warren 		if (!link->codec_dai_name) {
3555f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: codec_dai_name not"
3556f110bfc7SLiam Girdwood 				" set for %s\n", link->name);
3557bc92657aSStephen Warren 			return -EINVAL;
3558bc92657aSStephen Warren 		}
35595a504963SStephen Warren 
35605a504963SStephen Warren 		/*
35615a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
35625a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
35635a504963SStephen Warren 		 */
35645a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
3565f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: Both platform name/of_node"
3566f110bfc7SLiam Girdwood 				" are set for %s\n", link->name);
35675a504963SStephen Warren 			return -EINVAL;
35685a504963SStephen Warren 		}
35695a504963SStephen Warren 
35705a504963SStephen Warren 		/*
3571bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3572bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3573bc92657aSStephen Warren 		 * name alone..
35745a504963SStephen Warren 		 */
3575bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
3576f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: Neither/both "
3577f110bfc7SLiam Girdwood 				"cpu name/of_node are set for %s\n",link->name);
3578bc92657aSStephen Warren 			return -EINVAL;
3579bc92657aSStephen Warren 		}
3580bc92657aSStephen Warren 		/*
3581bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3582bc92657aSStephen Warren 		 * specified
3583bc92657aSStephen Warren 		 */
3584bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3585bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
3586f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3587f110bfc7SLiam Girdwood 				"cpu_name/of_node are set for %s\n", link->name);
35885a504963SStephen Warren 			return -EINVAL;
35895a504963SStephen Warren 		}
35905a504963SStephen Warren 	}
35915a504963SStephen Warren 
3592ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3593ed77cc12SMark Brown 
3594111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3595111c6419SStephen Warren 
3596150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3597150dd2f8SVinod Koul 
3598181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3599181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
36002eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3601f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3602f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3603f0fba2adSLiam Girdwood 		return -ENOMEM;
3604a7dbb603SLiam Girdwood 	card->num_rtd = 0;
36052eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3606f0fba2adSLiam Girdwood 
3607f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3608f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3609f0fba2adSLiam Girdwood 
3610c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3611db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3612c5af3a2eSMark Brown 	card->instantiated = 0;
3613f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3614a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3615c5af3a2eSMark Brown 
3616b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3617b19e6e7bSMark Brown 	if (ret != 0)
3618b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3619c5af3a2eSMark Brown 
3620b19e6e7bSMark Brown 	return ret;
3621c5af3a2eSMark Brown }
362270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3623c5af3a2eSMark Brown 
3624c5af3a2eSMark Brown /**
3625c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3626c5af3a2eSMark Brown  *
3627ac11a2b3SMark Brown  * @card: Card to unregister
3628c5af3a2eSMark Brown  *
3629c5af3a2eSMark Brown  */
363070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3631c5af3a2eSMark Brown {
3632b0e26485SVinod Koul 	if (card->instantiated)
3633b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3634f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3635c5af3a2eSMark Brown 
3636c5af3a2eSMark Brown 	return 0;
3637c5af3a2eSMark Brown }
363870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3639c5af3a2eSMark Brown 
3640f0fba2adSLiam Girdwood /*
3641f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3642f0fba2adSLiam Girdwood  * and sanitizing names.
3643f0fba2adSLiam Girdwood  */
36440b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3645f0fba2adSLiam Girdwood {
3646f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3647f0fba2adSLiam Girdwood 	int id1, id2;
3648f0fba2adSLiam Girdwood 
3649f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3650f0fba2adSLiam Girdwood 		return NULL;
3651f0fba2adSLiam Girdwood 
365258818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3653f0fba2adSLiam Girdwood 
3654f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3655f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3656f0fba2adSLiam Girdwood 	if (found) {
3657f0fba2adSLiam Girdwood 		/* get ID */
3658f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3659f0fba2adSLiam Girdwood 
3660f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3661f0fba2adSLiam Girdwood 			if (*id == -1)
3662f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3663f0fba2adSLiam Girdwood 		}
3664f0fba2adSLiam Girdwood 
3665f0fba2adSLiam Girdwood 	} else {
3666f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3667f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3668f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3669f0fba2adSLiam Girdwood 
3670f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
367105899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3672f0fba2adSLiam Girdwood 
3673f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3674f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
367558818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3676f0fba2adSLiam Girdwood 		} else
3677f0fba2adSLiam Girdwood 			*id = 0;
3678f0fba2adSLiam Girdwood 	}
3679f0fba2adSLiam Girdwood 
3680f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3681f0fba2adSLiam Girdwood }
3682f0fba2adSLiam Girdwood 
3683f0fba2adSLiam Girdwood /*
3684f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3685f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3686f0fba2adSLiam Girdwood  */
3687f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3688f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3689f0fba2adSLiam Girdwood {
3690f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
3691f110bfc7SLiam Girdwood 		dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3692f110bfc7SLiam Girdwood 				" no name\n", dev_name(dev));
3693f0fba2adSLiam Girdwood 		return NULL;
3694f0fba2adSLiam Girdwood 	}
3695f0fba2adSLiam Girdwood 
3696f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3697f0fba2adSLiam Girdwood }
3698f0fba2adSLiam Girdwood 
36999115171aSMark Brown /**
37009115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
37019115171aSMark Brown  *
3702ac11a2b3SMark Brown  * @dai: DAI to register
37039115171aSMark Brown  */
3704f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3705f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
37069115171aSMark Brown {
3707054880feSMark Brown 	struct snd_soc_codec *codec;
3708f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
37099115171aSMark Brown 
3710f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
37119115171aSMark Brown 
3712f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3713f0fba2adSLiam Girdwood 	if (dai == NULL)
3714f0fba2adSLiam Girdwood 		return -ENOMEM;
37156335d055SEric Miao 
3716f0fba2adSLiam Girdwood 	/* create DAI component name */
3717f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3718f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3719f0fba2adSLiam Girdwood 		kfree(dai);
3720f0fba2adSLiam Girdwood 		return -ENOMEM;
3721f0fba2adSLiam Girdwood 	}
3722f0fba2adSLiam Girdwood 
3723f0fba2adSLiam Girdwood 	dai->dev = dev;
3724f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3725be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3726f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3727f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
37289115171aSMark Brown 
37299115171aSMark Brown 	mutex_lock(&client_mutex);
3730054880feSMark Brown 
3731054880feSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3732054880feSMark Brown 		if (codec->dev == dev) {
3733f110bfc7SLiam Girdwood 			dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3734054880feSMark Brown 				dai->name, codec->name);
3735054880feSMark Brown 			dai->codec = codec;
3736054880feSMark Brown 			break;
3737054880feSMark Brown 		}
3738054880feSMark Brown 	}
3739054880feSMark Brown 
37405f800080SPeter Ujfalusi 	if (!dai->codec)
37415f800080SPeter Ujfalusi 		dai->dapm.idle_bias_off = 1;
37425f800080SPeter Ujfalusi 
37439115171aSMark Brown 	list_add(&dai->list, &dai_list);
3744054880feSMark Brown 
37459115171aSMark Brown 	mutex_unlock(&client_mutex);
37469115171aSMark Brown 
3747f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
37489115171aSMark Brown 
37499115171aSMark Brown 	return 0;
37509115171aSMark Brown }
37519115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
37529115171aSMark Brown 
37539115171aSMark Brown /**
37549115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
37559115171aSMark Brown  *
3756ac11a2b3SMark Brown  * @dai: DAI to unregister
37579115171aSMark Brown  */
3758f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
37599115171aSMark Brown {
3760f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3761f0fba2adSLiam Girdwood 
3762f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3763f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3764f0fba2adSLiam Girdwood 			goto found;
3765f0fba2adSLiam Girdwood 	}
3766f0fba2adSLiam Girdwood 	return;
3767f0fba2adSLiam Girdwood 
3768f0fba2adSLiam Girdwood found:
37699115171aSMark Brown 	mutex_lock(&client_mutex);
37709115171aSMark Brown 	list_del(&dai->list);
37719115171aSMark Brown 	mutex_unlock(&client_mutex);
37729115171aSMark Brown 
3773f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3774f0fba2adSLiam Girdwood 	kfree(dai->name);
3775f0fba2adSLiam Girdwood 	kfree(dai);
37769115171aSMark Brown }
37779115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
37789115171aSMark Brown 
37799115171aSMark Brown /**
37809115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
37819115171aSMark Brown  *
3782ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3783ac11a2b3SMark Brown  * @count: Number of DAIs
37849115171aSMark Brown  */
3785f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3786f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
37879115171aSMark Brown {
3788054880feSMark Brown 	struct snd_soc_codec *codec;
3789f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3790f0fba2adSLiam Girdwood 	int i, ret = 0;
3791f0fba2adSLiam Girdwood 
3792f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
37939115171aSMark Brown 
37949115171aSMark Brown 	for (i = 0; i < count; i++) {
3795f0fba2adSLiam Girdwood 
3796f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3797c46e0079SAxel Lin 		if (dai == NULL) {
3798c46e0079SAxel Lin 			ret = -ENOMEM;
3799c46e0079SAxel Lin 			goto err;
3800c46e0079SAxel Lin 		}
3801f0fba2adSLiam Girdwood 
3802f0fba2adSLiam Girdwood 		/* create DAI component name */
3803f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3804f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3805f0fba2adSLiam Girdwood 			kfree(dai);
3806f0fba2adSLiam Girdwood 			ret = -EINVAL;
38079115171aSMark Brown 			goto err;
38089115171aSMark Brown 		}
38099115171aSMark Brown 
3810f0fba2adSLiam Girdwood 		dai->dev = dev;
3811f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
38120f9141c9SMark Brown 		if (dai->driver->id)
38130f9141c9SMark Brown 			dai->id = dai->driver->id;
38140f9141c9SMark Brown 		else
38150f9141c9SMark Brown 			dai->id = i;
3816be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3817f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3818f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3819f0fba2adSLiam Girdwood 
3820f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3821054880feSMark Brown 
3822054880feSMark Brown 		list_for_each_entry(codec, &codec_list, list) {
3823054880feSMark Brown 			if (codec->dev == dev) {
3824f110bfc7SLiam Girdwood 				dev_dbg(dev, "ASoC: Mapped DAI %s to "
3825f110bfc7SLiam Girdwood 					"CODEC %s\n", dai->name, codec->name);
3826054880feSMark Brown 				dai->codec = codec;
3827054880feSMark Brown 				break;
3828054880feSMark Brown 			}
3829054880feSMark Brown 		}
3830054880feSMark Brown 
38315f800080SPeter Ujfalusi 		if (!dai->codec)
38325f800080SPeter Ujfalusi 			dai->dapm.idle_bias_off = 1;
38335f800080SPeter Ujfalusi 
3834f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3835054880feSMark Brown 
3836f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3837f0fba2adSLiam Girdwood 
3838f110bfc7SLiam Girdwood 		dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
3839f0fba2adSLiam Girdwood 	}
3840f0fba2adSLiam Girdwood 
38419115171aSMark Brown 	return 0;
38429115171aSMark Brown 
38439115171aSMark Brown err:
38449115171aSMark Brown 	for (i--; i >= 0; i--)
3845f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
38469115171aSMark Brown 
38479115171aSMark Brown 	return ret;
38489115171aSMark Brown }
38499115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
38509115171aSMark Brown 
38519115171aSMark Brown /**
38529115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
38539115171aSMark Brown  *
3854ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3855ac11a2b3SMark Brown  * @count: Number of DAIs
38569115171aSMark Brown  */
3857f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
38589115171aSMark Brown {
38599115171aSMark Brown 	int i;
38609115171aSMark Brown 
38619115171aSMark Brown 	for (i = 0; i < count; i++)
3862f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
38639115171aSMark Brown }
38649115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
38659115171aSMark Brown 
386612a48a8cSMark Brown /**
386712a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
386812a48a8cSMark Brown  *
3869ac11a2b3SMark Brown  * @platform: platform to register
387012a48a8cSMark Brown  */
3871f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3872f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
387312a48a8cSMark Brown {
3874f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
387512a48a8cSMark Brown 
3876f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3877f0fba2adSLiam Girdwood 
3878f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3879f0fba2adSLiam Girdwood 	if (platform == NULL)
3880f0fba2adSLiam Girdwood 		return -ENOMEM;
3881f0fba2adSLiam Girdwood 
3882f0fba2adSLiam Girdwood 	/* create platform component name */
3883f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3884f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3885f0fba2adSLiam Girdwood 		kfree(platform);
3886f0fba2adSLiam Girdwood 		return -ENOMEM;
3887f0fba2adSLiam Girdwood 	}
3888f0fba2adSLiam Girdwood 
3889f0fba2adSLiam Girdwood 	platform->dev = dev;
3890f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3891b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3892b7950641SLiam Girdwood 	platform->dapm.platform = platform;
389364a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3894cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
389512a48a8cSMark Brown 
389612a48a8cSMark Brown 	mutex_lock(&client_mutex);
389712a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
389812a48a8cSMark Brown 	mutex_unlock(&client_mutex);
389912a48a8cSMark Brown 
3900f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
390112a48a8cSMark Brown 
390212a48a8cSMark Brown 	return 0;
390312a48a8cSMark Brown }
390412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
390512a48a8cSMark Brown 
390612a48a8cSMark Brown /**
390712a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
390812a48a8cSMark Brown  *
3909ac11a2b3SMark Brown  * @platform: platform to unregister
391012a48a8cSMark Brown  */
3911f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
391212a48a8cSMark Brown {
3913f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3914f0fba2adSLiam Girdwood 
3915f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3916f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3917f0fba2adSLiam Girdwood 			goto found;
3918f0fba2adSLiam Girdwood 	}
3919f0fba2adSLiam Girdwood 	return;
3920f0fba2adSLiam Girdwood 
3921f0fba2adSLiam Girdwood found:
392212a48a8cSMark Brown 	mutex_lock(&client_mutex);
392312a48a8cSMark Brown 	list_del(&platform->list);
392412a48a8cSMark Brown 	mutex_unlock(&client_mutex);
392512a48a8cSMark Brown 
3926f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
3927f0fba2adSLiam Girdwood 	kfree(platform->name);
3928f0fba2adSLiam Girdwood 	kfree(platform);
392912a48a8cSMark Brown }
393012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
393112a48a8cSMark Brown 
3932151ab22cSMark Brown static u64 codec_format_map[] = {
3933151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3934151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3935151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3936151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3937151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3938151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3939151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3940151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3941151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3942151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3943151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3944151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3945151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3946151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3947151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3948151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3949151ab22cSMark Brown };
3950151ab22cSMark Brown 
3951151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3952151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3953151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3954151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3955151ab22cSMark Brown  */
3956151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3957151ab22cSMark Brown {
3958151ab22cSMark Brown 	int i;
3959151ab22cSMark Brown 
3960151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3961151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3962151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3963151ab22cSMark Brown }
3964151ab22cSMark Brown 
39650d0cf00aSMark Brown /**
39660d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
39670d0cf00aSMark Brown  *
3968ac11a2b3SMark Brown  * @codec: codec to register
39690d0cf00aSMark Brown  */
3970f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3971001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3972001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3973001ae4c0SMark Brown 			   int num_dai)
39740d0cf00aSMark Brown {
39753335ddcaSDimitris Papastamos 	size_t reg_size;
3976f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3977f0fba2adSLiam Girdwood 	int ret, i;
3978151ab22cSMark Brown 
3979f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
39800d0cf00aSMark Brown 
3981f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3982f0fba2adSLiam Girdwood 	if (codec == NULL)
3983f0fba2adSLiam Girdwood 		return -ENOMEM;
39840d0cf00aSMark Brown 
3985f0fba2adSLiam Girdwood 	/* create CODEC component name */
3986f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3987f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3988f0fba2adSLiam Girdwood 		kfree(codec);
3989f0fba2adSLiam Girdwood 		return -ENOMEM;
3990151ab22cSMark Brown 	}
3991151ab22cSMark Brown 
399223bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
399323bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
399423bbce34SDimitris Papastamos 	else
399523bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
399623bbce34SDimitris Papastamos 
3997c3acec26SMark Brown 	codec->write = codec_drv->write;
3998c3acec26SMark Brown 	codec->read = codec_drv->read;
39991500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
40001500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
40018020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
40025124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4003ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4004ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
4005ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
4006474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
400764a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
4008f0fba2adSLiam Girdwood 	codec->dev = dev;
4009f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
4010f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
4011f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
4012f0fba2adSLiam Girdwood 
40137a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
40147a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
40153335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
4016aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
40173335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
40183335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
4019f6e65744SBill Pemberton 		 * the default register cache to be marked as the
40203335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
40213335ddcaSDimitris Papastamos 		 * the cache.
40223335ddcaSDimitris Papastamos 		 */
40232aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
40243335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
40253335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
40263335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
40273335ddcaSDimitris Papastamos 				ret = -ENOMEM;
4028fdf0f54dSDimitris Papastamos 				goto fail;
40297a30a3dbSDimitris Papastamos 			}
40307a30a3dbSDimitris Papastamos 		}
40312aa86323SDimitris Papastamos 	}
40327a30a3dbSDimitris Papastamos 
40331500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
40341500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
40351500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
40361500b7b5SDimitris Papastamos 		if (!codec->readable_register)
40371500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
40388020454cSDimitris Papastamos 		if (!codec->writable_register)
40398020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
40401500b7b5SDimitris Papastamos 	}
40411500b7b5SDimitris Papastamos 
4042f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
4043f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
4044f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
4045f0fba2adSLiam Girdwood 	}
4046f0fba2adSLiam Girdwood 
4047054880feSMark Brown 	mutex_lock(&client_mutex);
4048054880feSMark Brown 	list_add(&codec->list, &codec_list);
4049054880feSMark Brown 	mutex_unlock(&client_mutex);
4050054880feSMark Brown 
405126b01ccdSMark Brown 	/* register any DAIs */
405226b01ccdSMark Brown 	if (num_dai) {
4053f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4054f0fba2adSLiam Girdwood 		if (ret < 0)
4055f110bfc7SLiam Girdwood 			dev_err(codec->dev, "ASoC: Failed to regster"
4056f110bfc7SLiam Girdwood 				" DAIs: %d\n", ret);
405726b01ccdSMark Brown 	}
4058f0fba2adSLiam Girdwood 
4059f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
40600d0cf00aSMark Brown 	return 0;
4061f0fba2adSLiam Girdwood 
4062fdf0f54dSDimitris Papastamos fail:
4063f0fba2adSLiam Girdwood 	kfree(codec->name);
4064f0fba2adSLiam Girdwood 	kfree(codec);
4065f0fba2adSLiam Girdwood 	return ret;
40660d0cf00aSMark Brown }
40670d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
40680d0cf00aSMark Brown 
40690d0cf00aSMark Brown /**
40700d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
40710d0cf00aSMark Brown  *
4072ac11a2b3SMark Brown  * @codec: codec to unregister
40730d0cf00aSMark Brown  */
4074f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
40750d0cf00aSMark Brown {
4076f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4077f0fba2adSLiam Girdwood 	int i;
4078f0fba2adSLiam Girdwood 
4079f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
4080f0fba2adSLiam Girdwood 		if (dev == codec->dev)
4081f0fba2adSLiam Girdwood 			goto found;
4082f0fba2adSLiam Girdwood 	}
4083f0fba2adSLiam Girdwood 	return;
4084f0fba2adSLiam Girdwood 
4085f0fba2adSLiam Girdwood found:
408626b01ccdSMark Brown 	if (codec->num_dai)
4087f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
4088f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
4089f0fba2adSLiam Girdwood 
40900d0cf00aSMark Brown 	mutex_lock(&client_mutex);
40910d0cf00aSMark Brown 	list_del(&codec->list);
40920d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
40930d0cf00aSMark Brown 
4094f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4095f0fba2adSLiam Girdwood 
40967a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
40973335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
40981aafcd4dSDimitris Papastamos 	kfree(codec->name);
4099f0fba2adSLiam Girdwood 	kfree(codec);
41000d0cf00aSMark Brown }
41010d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
41020d0cf00aSMark Brown 
4103bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4104bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4105bec4fa05SStephen Warren 			       const char *propname)
4106bec4fa05SStephen Warren {
4107bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
4108bec4fa05SStephen Warren 	int ret;
4109bec4fa05SStephen Warren 
4110bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4111bec4fa05SStephen Warren 	/*
4112bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4113bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4114bec4fa05SStephen Warren 	 * snd_soc_register_card.
4115bec4fa05SStephen Warren 	 */
4116bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4117bec4fa05SStephen Warren 		dev_err(card->dev,
4118f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
4119bec4fa05SStephen Warren 			propname, ret);
4120bec4fa05SStephen Warren 		return ret;
4121bec4fa05SStephen Warren 	}
4122bec4fa05SStephen Warren 
4123bec4fa05SStephen Warren 	return 0;
4124bec4fa05SStephen Warren }
4125bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4126bec4fa05SStephen Warren 
4127a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4128a4a54dd5SStephen Warren 				   const char *propname)
4129a4a54dd5SStephen Warren {
4130a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4131a4a54dd5SStephen Warren 	int num_routes;
4132a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4133a4a54dd5SStephen Warren 	int i, ret;
4134a4a54dd5SStephen Warren 
4135a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4136c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
4137f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4138f110bfc7SLiam Girdwood 			" length is not even\n", propname);
4139a4a54dd5SStephen Warren 		return -EINVAL;
4140a4a54dd5SStephen Warren 	}
4141a4a54dd5SStephen Warren 	num_routes /= 2;
4142a4a54dd5SStephen Warren 	if (!num_routes) {
4143f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4144a4a54dd5SStephen Warren 			propname);
4145a4a54dd5SStephen Warren 		return -EINVAL;
4146a4a54dd5SStephen Warren 	}
4147a4a54dd5SStephen Warren 
4148a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4149a4a54dd5SStephen Warren 			      GFP_KERNEL);
4150a4a54dd5SStephen Warren 	if (!routes) {
4151a4a54dd5SStephen Warren 		dev_err(card->dev,
4152f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4153a4a54dd5SStephen Warren 		return -EINVAL;
4154a4a54dd5SStephen Warren 	}
4155a4a54dd5SStephen Warren 
4156a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4157a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4158a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4159a4a54dd5SStephen Warren 		if (ret) {
4160c871bd0bSMark Brown 			dev_err(card->dev,
4161c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4162c871bd0bSMark Brown 				propname, 2 * i, ret);
4163b761c0caSMatthias Kaehlcke 			kfree(routes);
4164a4a54dd5SStephen Warren 			return -EINVAL;
4165a4a54dd5SStephen Warren 		}
4166a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4167a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4168a4a54dd5SStephen Warren 		if (ret) {
4169a4a54dd5SStephen Warren 			dev_err(card->dev,
4170c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4171c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4172b761c0caSMatthias Kaehlcke 			kfree(routes);
4173a4a54dd5SStephen Warren 			return -EINVAL;
4174a4a54dd5SStephen Warren 		}
4175a4a54dd5SStephen Warren 	}
4176a4a54dd5SStephen Warren 
4177a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4178a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4179a4a54dd5SStephen Warren 
4180a4a54dd5SStephen Warren 	return 0;
4181a4a54dd5SStephen Warren }
4182a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4183a4a54dd5SStephen Warren 
4184c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4185db2a4165SFrank Mandarino {
4186384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
41878a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
41888a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
41890837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
41908a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4191384c89e2SMark Brown 	}
4192c3c5a19aSMark Brown 
41938a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4194c3c5a19aSMark Brown 				 &codec_list_fops))
4195c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4196c3c5a19aSMark Brown 
41978a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4198f3208780SMark Brown 				 &dai_list_fops))
4199f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
420019c7ac27SMark Brown 
42018a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
420219c7ac27SMark Brown 				 &platform_list_fops))
420319c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4204384c89e2SMark Brown #endif
4205384c89e2SMark Brown 
4206fb257897SMark Brown 	snd_soc_util_init();
4207fb257897SMark Brown 
4208db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4209db2a4165SFrank Mandarino }
42104abe8e16SMark Brown module_init(snd_soc_init);
4211db2a4165SFrank Mandarino 
42127d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4213db2a4165SFrank Mandarino {
4214fb257897SMark Brown 	snd_soc_util_exit();
4215fb257897SMark Brown 
4216384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
42178a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4218384c89e2SMark Brown #endif
4219db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4220db2a4165SFrank Mandarino }
4221db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4222db2a4165SFrank Mandarino 
4223db2a4165SFrank Mandarino /* Module information */
4224d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4225db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4226db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
42278b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
4228