xref: /openbmc/linux/sound/soc/soc-core.c (revision cb2cf612fbdf457291b6e47064fed24f0dfdd9a5)
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>
335a0e3ad6STejun Heo #include <linux/slab.h>
34474828a4SMarek Vasut #include <sound/ac97_codec.h>
35db2a4165SFrank Mandarino #include <sound/core.h>
363028eb8cSMark Brown #include <sound/jack.h>
37db2a4165SFrank Mandarino #include <sound/pcm.h>
38db2a4165SFrank Mandarino #include <sound/pcm_params.h>
39db2a4165SFrank Mandarino #include <sound/soc.h>
40db2a4165SFrank Mandarino #include <sound/initval.h>
41db2a4165SFrank Mandarino 
42a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
43a8b1d34fSMark Brown #include <trace/events/asoc.h>
44a8b1d34fSMark Brown 
45f0fba2adSLiam Girdwood #define NAME_SIZE	32
46f0fba2adSLiam Girdwood 
47db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
48db2a4165SFrank Mandarino 
49384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
508a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
518a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
52384c89e2SMark Brown #endif
53384c89e2SMark Brown 
54c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
55c5af3a2eSMark Brown static LIST_HEAD(card_list);
569115171aSMark Brown static LIST_HEAD(dai_list);
5712a48a8cSMark Brown static LIST_HEAD(platform_list);
580d0cf00aSMark Brown static LIST_HEAD(codec_list);
59c5af3a2eSMark Brown 
60ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
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) {
1461500b7b5SDimitris Papastamos 		if (codec->readable_register && !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 {
172f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
173f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
174f0fba2adSLiam Girdwood 
17513fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1762624d5faSMark Brown }
1772624d5faSMark Brown 
1782624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1792624d5faSMark Brown 
180dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
181dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
182dbe21408SMark Brown {
183f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
184f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
185dbe21408SMark Brown 
186f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
187dbe21408SMark Brown }
188dbe21408SMark Brown 
189dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
190dbe21408SMark Brown 			       struct device_attribute *attr,
191dbe21408SMark Brown 			       const char *buf, size_t count)
192dbe21408SMark Brown {
193f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
194f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
195c593b520SMark Brown 	int ret;
196dbe21408SMark Brown 
197c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
198c593b520SMark Brown 	if (ret)
199c593b520SMark Brown 		return ret;
200dbe21408SMark Brown 
201dbe21408SMark Brown 	return count;
202dbe21408SMark Brown }
203dbe21408SMark Brown 
204dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
205dbe21408SMark Brown 
2062624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2072624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
2082624d5faSMark Brown {
2092624d5faSMark Brown 	file->private_data = inode->i_private;
2102624d5faSMark Brown 	return 0;
2112624d5faSMark Brown }
2122624d5faSMark Brown 
2132624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2142624d5faSMark Brown 				   size_t count, loff_t *ppos)
2152624d5faSMark Brown {
2162624d5faSMark Brown 	ssize_t ret;
2172624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
21813fd179fSDimitris Papastamos 	char *buf;
21913fd179fSDimitris Papastamos 
22013fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22113fd179fSDimitris Papastamos 		return -EINVAL;
22213fd179fSDimitris Papastamos 
22313fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2242624d5faSMark Brown 	if (!buf)
2252624d5faSMark Brown 		return -ENOMEM;
22613fd179fSDimitris Papastamos 
22713fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
22813fd179fSDimitris Papastamos 	if (ret >= 0) {
22913fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23013fd179fSDimitris Papastamos 			kfree(buf);
23113fd179fSDimitris Papastamos 			return -EFAULT;
23213fd179fSDimitris Papastamos 		}
23313fd179fSDimitris Papastamos 		*ppos += ret;
23413fd179fSDimitris Papastamos 	}
23513fd179fSDimitris Papastamos 
2362624d5faSMark Brown 	kfree(buf);
2372624d5faSMark Brown 	return ret;
2382624d5faSMark Brown }
2392624d5faSMark Brown 
2402624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2412624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2422624d5faSMark Brown {
2432624d5faSMark Brown 	char buf[32];
24434e268d8SStephen Boyd 	size_t buf_size;
2452624d5faSMark Brown 	char *start = buf;
2462624d5faSMark Brown 	unsigned long reg, value;
2472624d5faSMark Brown 	int step = 1;
2482624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2492624d5faSMark Brown 
2502624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2512624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2522624d5faSMark Brown 		return -EFAULT;
2532624d5faSMark Brown 	buf[buf_size] = 0;
2542624d5faSMark Brown 
255f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
256f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
2572624d5faSMark Brown 
2582624d5faSMark Brown 	while (*start == ' ')
2592624d5faSMark Brown 		start++;
2602624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2612624d5faSMark Brown 	while (*start == ' ')
2622624d5faSMark Brown 		start++;
2632624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2642624d5faSMark Brown 		return -EINVAL;
2650d51a9cbSMark Brown 
2660d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2670d51a9cbSMark Brown 	add_taint(TAINT_USER);
2680d51a9cbSMark Brown 
269e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2702624d5faSMark Brown 	return buf_size;
2712624d5faSMark Brown }
2722624d5faSMark Brown 
2732624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2742624d5faSMark Brown 	.open = codec_reg_open_file,
2752624d5faSMark Brown 	.read = codec_reg_read_file,
2762624d5faSMark Brown 	.write = codec_reg_write_file,
2776038f373SArnd Bergmann 	.llseek = default_llseek,
2782624d5faSMark Brown };
2792624d5faSMark Brown 
2802624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2812624d5faSMark Brown {
282d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
283d6ce4cf3SJarkko Nikula 
2846ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
285d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2862624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2872624d5faSMark Brown 		printk(KERN_WARNING
2882624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2892624d5faSMark Brown 		return;
2902624d5faSMark Brown 	}
2912624d5faSMark Brown 
292aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
293aaee8ef1SMark Brown 			    &codec->cache_sync);
294aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
295aaee8ef1SMark Brown 			    &codec->cache_only);
296aaee8ef1SMark Brown 
2972624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2982624d5faSMark Brown 						 codec->debugfs_codec_root,
2992624d5faSMark Brown 						 codec, &codec_reg_fops);
3002624d5faSMark Brown 	if (!codec->debugfs_reg)
3012624d5faSMark Brown 		printk(KERN_WARNING
3022624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
3032624d5faSMark Brown 
3048eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
3052624d5faSMark Brown }
3062624d5faSMark Brown 
3072624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3082624d5faSMark Brown {
3092624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3102624d5faSMark Brown }
3112624d5faSMark Brown 
312c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
313c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
314c3c5a19aSMark Brown {
315c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3162b194f9dSMark Brown 	ssize_t len, ret = 0;
317c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
318c3c5a19aSMark Brown 
319c3c5a19aSMark Brown 	if (!buf)
320c3c5a19aSMark Brown 		return -ENOMEM;
321c3c5a19aSMark Brown 
3222b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3232b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
324c3c5a19aSMark Brown 			       codec->name);
3252b194f9dSMark Brown 		if (len >= 0)
3262b194f9dSMark Brown 			ret += len;
3272b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3282b194f9dSMark Brown 			ret = PAGE_SIZE;
3292b194f9dSMark Brown 			break;
3302b194f9dSMark Brown 		}
3312b194f9dSMark Brown 	}
332c3c5a19aSMark Brown 
333c3c5a19aSMark Brown 	if (ret >= 0)
334c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
335c3c5a19aSMark Brown 
336c3c5a19aSMark Brown 	kfree(buf);
337c3c5a19aSMark Brown 
338c3c5a19aSMark Brown 	return ret;
339c3c5a19aSMark Brown }
340c3c5a19aSMark Brown 
341c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
342c3c5a19aSMark Brown 	.read = codec_list_read_file,
343c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
344c3c5a19aSMark Brown };
345c3c5a19aSMark Brown 
346f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
347f3208780SMark Brown 				  size_t count, loff_t *ppos)
348f3208780SMark Brown {
349f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3502b194f9dSMark Brown 	ssize_t len, ret = 0;
351f3208780SMark Brown 	struct snd_soc_dai *dai;
352f3208780SMark Brown 
353f3208780SMark Brown 	if (!buf)
354f3208780SMark Brown 		return -ENOMEM;
355f3208780SMark Brown 
3562b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3572b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3582b194f9dSMark Brown 		if (len >= 0)
3592b194f9dSMark Brown 			ret += len;
3602b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3612b194f9dSMark Brown 			ret = PAGE_SIZE;
3622b194f9dSMark Brown 			break;
3632b194f9dSMark Brown 		}
3642b194f9dSMark Brown 	}
365f3208780SMark Brown 
366f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
367f3208780SMark Brown 
368f3208780SMark Brown 	kfree(buf);
369f3208780SMark Brown 
370f3208780SMark Brown 	return ret;
371f3208780SMark Brown }
372f3208780SMark Brown 
373f3208780SMark Brown static const struct file_operations dai_list_fops = {
374f3208780SMark Brown 	.read = dai_list_read_file,
375f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
376f3208780SMark Brown };
377f3208780SMark Brown 
37819c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
37919c7ac27SMark Brown 				       char __user *user_buf,
38019c7ac27SMark Brown 				       size_t count, loff_t *ppos)
38119c7ac27SMark Brown {
38219c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3832b194f9dSMark Brown 	ssize_t len, ret = 0;
38419c7ac27SMark Brown 	struct snd_soc_platform *platform;
38519c7ac27SMark Brown 
38619c7ac27SMark Brown 	if (!buf)
38719c7ac27SMark Brown 		return -ENOMEM;
38819c7ac27SMark Brown 
3892b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3902b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
39119c7ac27SMark Brown 			       platform->name);
3922b194f9dSMark Brown 		if (len >= 0)
3932b194f9dSMark Brown 			ret += len;
3942b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3952b194f9dSMark Brown 			ret = PAGE_SIZE;
3962b194f9dSMark Brown 			break;
3972b194f9dSMark Brown 		}
3982b194f9dSMark Brown 	}
39919c7ac27SMark Brown 
40019c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
40119c7ac27SMark Brown 
40219c7ac27SMark Brown 	kfree(buf);
40319c7ac27SMark Brown 
40419c7ac27SMark Brown 	return ret;
40519c7ac27SMark Brown }
40619c7ac27SMark Brown 
40719c7ac27SMark Brown static const struct file_operations platform_list_fops = {
40819c7ac27SMark Brown 	.read = platform_list_read_file,
40919c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
41019c7ac27SMark Brown };
41119c7ac27SMark Brown 
412a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
413a6052154SJarkko Nikula {
414a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4158a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4163a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
417a6052154SJarkko Nikula 		dev_warn(card->dev,
418a6052154SJarkko Nikula 			 "ASoC: Failed to create codec debugfs directory\n");
4193a45b867SJarkko Nikula 		return;
4203a45b867SJarkko Nikula 	}
4213a45b867SJarkko Nikula 
4223a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4233a45b867SJarkko Nikula 						    card->debugfs_card_root,
4243a45b867SJarkko Nikula 						    &card->pop_time);
4253a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4263a45b867SJarkko Nikula 		dev_warn(card->dev,
4273a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
428a6052154SJarkko Nikula }
429a6052154SJarkko Nikula 
430a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
431a6052154SJarkko Nikula {
432a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
433a6052154SJarkko Nikula }
434a6052154SJarkko Nikula 
4352624d5faSMark Brown #else
4362624d5faSMark Brown 
4372624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4382624d5faSMark Brown {
4392624d5faSMark Brown }
4402624d5faSMark Brown 
4412624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4422624d5faSMark Brown {
4432624d5faSMark Brown }
444b95fccbcSAxel Lin 
445b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
446b95fccbcSAxel Lin {
447b95fccbcSAxel Lin }
448b95fccbcSAxel Lin 
449b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
450b95fccbcSAxel Lin {
451b95fccbcSAxel Lin }
4522624d5faSMark Brown #endif
4532624d5faSMark Brown 
454db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
455db2a4165SFrank Mandarino /* unregister ac97 codec */
456db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
457db2a4165SFrank Mandarino {
458db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
459db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
460db2a4165SFrank Mandarino 	return 0;
461db2a4165SFrank Mandarino }
462db2a4165SFrank Mandarino 
463db2a4165SFrank Mandarino /* stop no dev release warning */
464db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
465db2a4165SFrank Mandarino 
466db2a4165SFrank Mandarino /* register ac97 codec to bus */
467db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
468db2a4165SFrank Mandarino {
469db2a4165SFrank Mandarino 	int err;
470db2a4165SFrank Mandarino 
471db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4724ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
473db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
474db2a4165SFrank Mandarino 
475bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
476f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
477db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
478db2a4165SFrank Mandarino 	if (err < 0) {
479db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
480db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
481db2a4165SFrank Mandarino 		return err;
482db2a4165SFrank Mandarino 	}
483db2a4165SFrank Mandarino 	return 0;
484db2a4165SFrank Mandarino }
485db2a4165SFrank Mandarino #endif
486db2a4165SFrank Mandarino 
4876f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
488db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4896f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
490db2a4165SFrank Mandarino {
4916f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
4922eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
493db2a4165SFrank Mandarino 	int i;
494db2a4165SFrank Mandarino 
495e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
496e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
497e3509ff0SDaniel Mack 	 */
498f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
499e3509ff0SDaniel Mack 		return 0;
500e3509ff0SDaniel Mack 
5016ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5026ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5036ed25978SAndy Green 	 */
504f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
505f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
506f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5076ed25978SAndy Green 
5086ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
509f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5106ed25978SAndy Green 
511a00f90f9SMark Brown 	/* mute any active DACs */
512f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
513f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
514f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5153efab7dcSMark Brown 
516f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5173efab7dcSMark Brown 			continue;
5183efab7dcSMark Brown 
519f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
520f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
521db2a4165SFrank Mandarino 	}
522db2a4165SFrank Mandarino 
5234ccab3e7SLiam Girdwood 	/* suspend all pcms */
524f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
525f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5263efab7dcSMark Brown 			continue;
5273efab7dcSMark Brown 
528f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5293efab7dcSMark Brown 	}
5304ccab3e7SLiam Girdwood 
53187506549SMark Brown 	if (card->suspend_pre)
53270b2ac12SMark Brown 		card->suspend_pre(card);
533db2a4165SFrank Mandarino 
534f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
535f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
536f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5373efab7dcSMark Brown 
538f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5393efab7dcSMark Brown 			continue;
5403efab7dcSMark Brown 
541f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
542f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
543f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
544f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
545f0fba2adSLiam Girdwood 			platform->suspended = 1;
546f0fba2adSLiam Girdwood 		}
547db2a4165SFrank Mandarino 	}
548db2a4165SFrank Mandarino 
549db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
550f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5515b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
552ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
553f0fba2adSLiam Girdwood 	}
554db2a4165SFrank Mandarino 
555f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
556f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
5573efab7dcSMark Brown 
558f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5593efab7dcSMark Brown 			continue;
5603efab7dcSMark Brown 
561f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
562f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
563db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
564f0fba2adSLiam Girdwood 
565f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
566f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
567db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
568db2a4165SFrank Mandarino 	}
569db2a4165SFrank Mandarino 
570f0fba2adSLiam Girdwood 	/* suspend all CODECs */
5712eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
5721547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
5731547aba9SMark Brown 		 * bias _ON and should not be suspended. */
574f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
575ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
5761547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
5771547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
578f0fba2adSLiam Girdwood 				codec->driver->suspend(codec, PMSG_SUSPEND);
579f0fba2adSLiam Girdwood 				codec->suspended = 1;
5801547aba9SMark Brown 				break;
5811547aba9SMark Brown 			default:
582f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
5831547aba9SMark Brown 				break;
5841547aba9SMark Brown 			}
5851547aba9SMark Brown 		}
586f0fba2adSLiam Girdwood 	}
587db2a4165SFrank Mandarino 
588f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
589f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
5903efab7dcSMark Brown 
591f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5923efab7dcSMark Brown 			continue;
5933efab7dcSMark Brown 
594f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
595f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
596db2a4165SFrank Mandarino 	}
597db2a4165SFrank Mandarino 
59887506549SMark Brown 	if (card->suspend_post)
59970b2ac12SMark Brown 		card->suspend_post(card);
600db2a4165SFrank Mandarino 
601db2a4165SFrank Mandarino 	return 0;
602db2a4165SFrank Mandarino }
6036f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
604db2a4165SFrank Mandarino 
6056ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6066ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6076ed25978SAndy Green  */
6086ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
609db2a4165SFrank Mandarino {
610f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
611f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6122eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
613db2a4165SFrank Mandarino 	int i;
614db2a4165SFrank Mandarino 
6156ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6166ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6176ed25978SAndy Green 	 */
6186ed25978SAndy Green 
619f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
6206ed25978SAndy Green 
6219949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
622f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6239949788bSMark Brown 
62487506549SMark Brown 	if (card->resume_pre)
62570b2ac12SMark Brown 		card->resume_pre(card);
626db2a4165SFrank Mandarino 
627f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
628f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
629f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6303efab7dcSMark Brown 
631f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6323efab7dcSMark Brown 			continue;
6333efab7dcSMark Brown 
634f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
635f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
636db2a4165SFrank Mandarino 	}
637db2a4165SFrank Mandarino 
6382eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6391547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6401547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
6411547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
6421547aba9SMark Brown 		 */
643f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
644ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6451547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
6461547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
647f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
648f0fba2adSLiam Girdwood 				codec->suspended = 0;
6491547aba9SMark Brown 				break;
6501547aba9SMark Brown 			default:
651f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
6521547aba9SMark Brown 				break;
6531547aba9SMark Brown 			}
6541547aba9SMark Brown 		}
655f0fba2adSLiam Girdwood 	}
656db2a4165SFrank Mandarino 
657f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
658f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
6593efab7dcSMark Brown 
660f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6613efab7dcSMark Brown 			continue;
6623efab7dcSMark Brown 
663f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
664f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
665db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
666f0fba2adSLiam Girdwood 
667f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
668f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
669db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
670db2a4165SFrank Mandarino 	}
671db2a4165SFrank Mandarino 
6723ff3f64bSMark Brown 	/* unmute any active DACs */
673f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
674f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
675f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
6763efab7dcSMark Brown 
677f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6783efab7dcSMark Brown 			continue;
6793efab7dcSMark Brown 
680f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
681f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
682db2a4165SFrank Mandarino 	}
683db2a4165SFrank Mandarino 
684f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
685f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
686f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
6873efab7dcSMark Brown 
688f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6893efab7dcSMark Brown 			continue;
6903efab7dcSMark Brown 
691f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
692f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
693f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
694f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
695f0fba2adSLiam Girdwood 			platform->suspended = 0;
696f0fba2adSLiam Girdwood 		}
697db2a4165SFrank Mandarino 	}
698db2a4165SFrank Mandarino 
69987506549SMark Brown 	if (card->resume_post)
70070b2ac12SMark Brown 		card->resume_post(card);
701db2a4165SFrank Mandarino 
702f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
7036ed25978SAndy Green 
7046ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
705f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7066ed25978SAndy Green }
7076ed25978SAndy Green 
7086ed25978SAndy Green /* powers up audio subsystem after a suspend */
7096f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7106ed25978SAndy Green {
7116f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
71282e14e8bSStephen Warren 	int i, ac97_control = 0;
713b9dd94a8SPeter Ujfalusi 
71464ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
71564ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
71664ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
71764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
71864ab9baaSMark Brown 	 */
719f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
720f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
72182e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
72282e14e8bSStephen Warren 	}
72382e14e8bSStephen Warren 	if (ac97_control) {
724f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
72564ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
72664ab9baaSMark Brown 	} else {
727f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
7286308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
729f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
730f0fba2adSLiam Girdwood 	}
7316ed25978SAndy Green 
732db2a4165SFrank Mandarino 	return 0;
733db2a4165SFrank Mandarino }
7346f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
735db2a4165SFrank Mandarino #else
7366f8ab4acSMark Brown #define snd_soc_suspend NULL
7376f8ab4acSMark Brown #define snd_soc_resume NULL
738db2a4165SFrank Mandarino #endif
739db2a4165SFrank Mandarino 
74002a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
74102a06d30SBarry Song };
74202a06d30SBarry Song 
743f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
744db2a4165SFrank Mandarino {
745f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
746f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
747fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
748435c5e25SMark Brown 	struct snd_soc_platform *platform;
749f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
750848dd8beSMark Brown 	const char *platform_name;
751db2a4165SFrank Mandarino 
752f0fba2adSLiam Girdwood 	if (rtd->complete)
753f0fba2adSLiam Girdwood 		return 1;
754f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
7556308419aSMark Brown 
756f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
757f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
758f0fba2adSLiam Girdwood 		goto find_codec;
759435c5e25SMark Brown 	}
760f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
761f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
762f0fba2adSLiam Girdwood 		if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
763f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
764f0fba2adSLiam Girdwood 			goto find_codec;
765f0fba2adSLiam Girdwood 		}
766f0fba2adSLiam Girdwood 	}
767f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
768f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
769f0fba2adSLiam Girdwood 
770f0fba2adSLiam Girdwood find_codec:
771f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
772f0fba2adSLiam Girdwood 	if (rtd->codec) {
773f0fba2adSLiam Girdwood 		goto find_platform;
774c5af3a2eSMark Brown 	}
775c5af3a2eSMark Brown 
776f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
777f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
778f0fba2adSLiam Girdwood 		if (!strcmp(codec->name, dai_link->codec_name)) {
779f0fba2adSLiam Girdwood 			rtd->codec = codec;
780f0fba2adSLiam Girdwood 
781f0fba2adSLiam Girdwood 			/* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
782f0fba2adSLiam Girdwood 			list_for_each_entry(codec_dai, &dai_list, list) {
783f0fba2adSLiam Girdwood 				if (codec->dev == codec_dai->dev &&
784f0fba2adSLiam Girdwood 						!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
785f0fba2adSLiam Girdwood 					rtd->codec_dai = codec_dai;
786f0fba2adSLiam Girdwood 					goto find_platform;
787435c5e25SMark Brown 				}
788f0fba2adSLiam Girdwood 			}
789f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
790f0fba2adSLiam Girdwood 					dai_link->codec_dai_name);
791f0fba2adSLiam Girdwood 
792f0fba2adSLiam Girdwood 			goto find_platform;
793f0fba2adSLiam Girdwood 		}
794f0fba2adSLiam Girdwood 	}
795f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
796f0fba2adSLiam Girdwood 			dai_link->codec_name);
797f0fba2adSLiam Girdwood 
798f0fba2adSLiam Girdwood find_platform:
799848dd8beSMark Brown 	/* do we need a platform? */
800848dd8beSMark Brown 	if (rtd->platform)
801f0fba2adSLiam Girdwood 		goto out;
802848dd8beSMark Brown 
803848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
804848dd8beSMark Brown 	platform_name = dai_link->platform_name;
805848dd8beSMark Brown 	if (!platform_name)
806848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
807848dd8beSMark Brown 
808848dd8beSMark Brown 	/* no, then find one from the set of registered platforms */
809f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
810848dd8beSMark Brown 		if (!strcmp(platform->name, platform_name)) {
811f0fba2adSLiam Girdwood 			rtd->platform = platform;
812f0fba2adSLiam Girdwood 			goto out;
813f0fba2adSLiam Girdwood 		}
814435c5e25SMark Brown 	}
8156b05eda6SMark Brown 
816f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
817f0fba2adSLiam Girdwood 			dai_link->platform_name);
818f0fba2adSLiam Girdwood 	return 0;
819f0fba2adSLiam Girdwood 
820f0fba2adSLiam Girdwood out:
821f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
822f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
823f0fba2adSLiam Girdwood 		rtd->complete = 1;
824f0fba2adSLiam Girdwood 		card->num_rtd++;
825f0fba2adSLiam Girdwood 	}
826f0fba2adSLiam Girdwood 	return 1;
8276b05eda6SMark Brown }
8286b05eda6SMark Brown 
829589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
830589c3563SJarkko Nikula {
831589c3563SJarkko Nikula 	int err;
832589c3563SJarkko Nikula 
833589c3563SJarkko Nikula 	if (codec->driver->remove) {
834589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
835589c3563SJarkko Nikula 		if (err < 0)
836589c3563SJarkko Nikula 			dev_err(codec->dev,
837589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
838589c3563SJarkko Nikula 				codec->name, err);
839589c3563SJarkko Nikula 	}
840589c3563SJarkko Nikula 
841589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
842589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
843589c3563SJarkko Nikula 
844589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
845589c3563SJarkko Nikula 	codec->probed = 0;
846589c3563SJarkko Nikula 	list_del(&codec->card_list);
847589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
848589c3563SJarkko Nikula }
849589c3563SJarkko Nikula 
8500168bf0dSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
851f0fba2adSLiam Girdwood {
852f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
853f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
854f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
855f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
856f0fba2adSLiam Girdwood 	int err;
857f0fba2adSLiam Girdwood 
858f0fba2adSLiam Girdwood 	/* unregister the rtd device */
859f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
860f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
861589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
862f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
863f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
86402a06d30SBarry Song 	}
86502a06d30SBarry Song 
866f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
8670168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
8680168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
869f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
870f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
871f0fba2adSLiam Girdwood 			if (err < 0)
872f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
873f0fba2adSLiam Girdwood 		}
874f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
875f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
876f0fba2adSLiam Girdwood 	}
877f0fba2adSLiam Girdwood 
878f0fba2adSLiam Girdwood 	/* remove the platform */
8790168bf0dSLiam Girdwood 	if (platform && platform->probed &&
8800168bf0dSLiam Girdwood 			platform->driver->remove_order == order) {
881f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
882f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
883f0fba2adSLiam Girdwood 			if (err < 0)
884f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
885f0fba2adSLiam Girdwood 		}
886f0fba2adSLiam Girdwood 		platform->probed = 0;
887f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
888f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
889f0fba2adSLiam Girdwood 	}
890f0fba2adSLiam Girdwood 
891f0fba2adSLiam Girdwood 	/* remove the CODEC */
8920168bf0dSLiam Girdwood 	if (codec && codec->probed &&
8930168bf0dSLiam Girdwood 			codec->driver->remove_order == order)
894589c3563SJarkko Nikula 		soc_remove_codec(codec);
895f0fba2adSLiam Girdwood 
896f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
8970168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
8980168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
899f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
900f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
901f0fba2adSLiam Girdwood 			if (err < 0)
902f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
903f0fba2adSLiam Girdwood 		}
904f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
905f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
906f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
907f0fba2adSLiam Girdwood 	}
908f0fba2adSLiam Girdwood }
909f0fba2adSLiam Girdwood 
9100671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9110671fd8eSKuninori Morimoto {
9120168bf0dSLiam Girdwood 	int dai, order;
9130671fd8eSKuninori Morimoto 
9140168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9150168bf0dSLiam Girdwood 			order++) {
9160168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
9170168bf0dSLiam Girdwood 			soc_remove_dai_link(card, dai, order);
9180168bf0dSLiam Girdwood 	}
9190671fd8eSKuninori Morimoto 	card->num_rtd = 0;
9200671fd8eSKuninori Morimoto }
9210671fd8eSKuninori Morimoto 
922ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
923ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
924ead9b919SJarkko Nikula {
925ead9b919SJarkko Nikula 	int i;
926ead9b919SJarkko Nikula 
927ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
928ead9b919SJarkko Nikula 		return;
929ead9b919SJarkko Nikula 
930ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
931ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
932ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
933ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
934ead9b919SJarkko Nikula 			break;
935ead9b919SJarkko Nikula 		}
936ead9b919SJarkko Nikula 	}
937ead9b919SJarkko Nikula }
938ead9b919SJarkko Nikula 
939589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
940589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
941589c3563SJarkko Nikula {
942589c3563SJarkko Nikula 	int ret = 0;
94389b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
944589c3563SJarkko Nikula 
945589c3563SJarkko Nikula 	codec->card = card;
946589c3563SJarkko Nikula 	codec->dapm.card = card;
947589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
948589c3563SJarkko Nikula 
94970d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
95070d29331SJarkko Nikula 		return -ENODEV;
95170d29331SJarkko Nikula 
952d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
953d5d1e0beSLars-Peter Clausen 
95477530150SLars-Peter Clausen 	if (driver->dapm_widgets)
95577530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
95677530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
95777530150SLars-Peter Clausen 
95889b95ac0SMark Brown 	if (driver->probe) {
95989b95ac0SMark Brown 		ret = driver->probe(codec);
960589c3563SJarkko Nikula 		if (ret < 0) {
961589c3563SJarkko Nikula 			dev_err(codec->dev,
962589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
963589c3563SJarkko Nikula 				codec->name, ret);
96470d29331SJarkko Nikula 			goto err_probe;
965589c3563SJarkko Nikula 		}
966589c3563SJarkko Nikula 	}
967589c3563SJarkko Nikula 
968b7af1dafSMark Brown 	if (driver->controls)
969b7af1dafSMark Brown 		snd_soc_add_controls(codec, driver->controls,
970b7af1dafSMark Brown 				     driver->num_controls);
97189b95ac0SMark Brown 	if (driver->dapm_routes)
97289b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
97389b95ac0SMark Brown 					driver->num_dapm_routes);
97489b95ac0SMark Brown 
975589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
976589c3563SJarkko Nikula 	codec->probed = 1;
977589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
9787be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
979589c3563SJarkko Nikula 
98070d29331SJarkko Nikula 	return 0;
98170d29331SJarkko Nikula 
98270d29331SJarkko Nikula err_probe:
983d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
98470d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
98570d29331SJarkko Nikula 
986589c3563SJarkko Nikula 	return ret;
987589c3563SJarkko Nikula }
988589c3563SJarkko Nikula 
989956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
990956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
991956245e9SLiam Girdwood {
992956245e9SLiam Girdwood 	int ret = 0;
993956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
994956245e9SLiam Girdwood 
995956245e9SLiam Girdwood 	platform->card = card;
996b7950641SLiam Girdwood 	platform->dapm.card = card;
997956245e9SLiam Girdwood 
998956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
999956245e9SLiam Girdwood 		return -ENODEV;
1000956245e9SLiam Girdwood 
1001*cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1002*cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1003*cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1004*cb2cf612SLiam Girdwood 
1005956245e9SLiam Girdwood 	if (driver->probe) {
1006956245e9SLiam Girdwood 		ret = driver->probe(platform);
1007956245e9SLiam Girdwood 		if (ret < 0) {
1008956245e9SLiam Girdwood 			dev_err(platform->dev,
1009956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1010956245e9SLiam Girdwood 				platform->name, ret);
1011956245e9SLiam Girdwood 			goto err_probe;
1012956245e9SLiam Girdwood 		}
1013956245e9SLiam Girdwood 	}
1014956245e9SLiam Girdwood 
1015*cb2cf612SLiam Girdwood 	if (driver->controls)
1016*cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1017*cb2cf612SLiam Girdwood 				     driver->num_controls);
1018*cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1019*cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1020*cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1021*cb2cf612SLiam Girdwood 
1022956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1023956245e9SLiam Girdwood 	platform->probed = 1;
1024956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1025b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1026956245e9SLiam Girdwood 
1027956245e9SLiam Girdwood 	return 0;
1028956245e9SLiam Girdwood 
1029956245e9SLiam Girdwood err_probe:
1030956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1031956245e9SLiam Girdwood 
1032956245e9SLiam Girdwood 	return ret;
1033956245e9SLiam Girdwood }
1034956245e9SLiam Girdwood 
1035f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1036f0fba2adSLiam Girdwood 
1037589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1038589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1039589c3563SJarkko Nikula 				   int num, int dailess)
1040589c3563SJarkko Nikula {
1041589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1042589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1043589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1044589c3563SJarkko Nikula 	const char *temp, *name;
1045589c3563SJarkko Nikula 	int ret = 0;
1046589c3563SJarkko Nikula 
1047589c3563SJarkko Nikula 	if (!dailess) {
1048589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1049589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1050589c3563SJarkko Nikula 		name = dai_link->name;
1051589c3563SJarkko Nikula 	} else {
1052589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1053589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1054589c3563SJarkko Nikula 		name = aux_dev->name;
1055589c3563SJarkko Nikula 	}
10560962bb21SJanusz Krzysztofik 	rtd->card = card;
1057589c3563SJarkko Nikula 
1058589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1059589c3563SJarkko Nikula 	temp = codec->name_prefix;
1060589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1061589c3563SJarkko Nikula 
1062589c3563SJarkko Nikula 	/* do machine specific initialization */
1063589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1064589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1065589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1066589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1067589c3563SJarkko Nikula 	if (ret < 0) {
1068589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1069589c3563SJarkko Nikula 		return ret;
1070589c3563SJarkko Nikula 	}
1071589c3563SJarkko Nikula 	codec->name_prefix = temp;
1072589c3563SJarkko Nikula 
1073589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are instantiated */
1074589c3563SJarkko Nikula 	snd_soc_dapm_new_widgets(&codec->dapm);
1075589c3563SJarkko Nikula 
1076589c3563SJarkko Nikula 	/* register the rtd device */
1077589c3563SJarkko Nikula 	rtd->codec = codec;
1078589c3563SJarkko Nikula 	rtd->dev.parent = card->dev;
1079589c3563SJarkko Nikula 	rtd->dev.release = rtd_release;
1080589c3563SJarkko Nikula 	rtd->dev.init_name = name;
1081b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
1082589c3563SJarkko Nikula 	ret = device_register(&rtd->dev);
1083589c3563SJarkko Nikula 	if (ret < 0) {
1084589c3563SJarkko Nikula 		dev_err(card->dev,
1085589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1086589c3563SJarkko Nikula 		return ret;
1087589c3563SJarkko Nikula 	}
1088589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1089589c3563SJarkko Nikula 
1090589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
1091589c3563SJarkko Nikula 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1092589c3563SJarkko Nikula 	if (ret < 0)
1093589c3563SJarkko Nikula 		dev_err(codec->dev,
1094589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1095589c3563SJarkko Nikula 			ret);
1096589c3563SJarkko Nikula 
1097589c3563SJarkko Nikula 	/* add codec sysfs entries */
1098589c3563SJarkko Nikula 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1099589c3563SJarkko Nikula 	if (ret < 0)
1100589c3563SJarkko Nikula 		dev_err(codec->dev,
1101589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1102589c3563SJarkko Nikula 
1103589c3563SJarkko Nikula 	return 0;
1104589c3563SJarkko Nikula }
1105589c3563SJarkko Nikula 
11060168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1107f0fba2adSLiam Girdwood {
1108f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1109f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1110f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1111f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1112f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1113f0fba2adSLiam Girdwood 	int ret;
1114f0fba2adSLiam Girdwood 
11150168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
11160168bf0dSLiam Girdwood 			card->name, num, order);
1117f0fba2adSLiam Girdwood 
1118f0fba2adSLiam Girdwood 	/* config components */
1119f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1120f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1121f0fba2adSLiam Girdwood 	codec_dai->card = card;
1122f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1123f0fba2adSLiam Girdwood 
1124f0fba2adSLiam Girdwood 	/* set default power off timeout */
1125f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1126f0fba2adSLiam Girdwood 
1127f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
11280168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
11290168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
113061b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
113161b61e3cSLiam Girdwood 			return -ENODEV;
113261b61e3cSLiam Girdwood 
1133f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1134f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1135f0fba2adSLiam Girdwood 			if (ret < 0) {
1136f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1137f0fba2adSLiam Girdwood 						cpu_dai->name);
113861b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1139f0fba2adSLiam Girdwood 				return ret;
1140f0fba2adSLiam Girdwood 			}
1141f0fba2adSLiam Girdwood 		}
1142f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1143f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1144f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1145f0fba2adSLiam Girdwood 	}
1146f0fba2adSLiam Girdwood 
1147f0fba2adSLiam Girdwood 	/* probe the CODEC */
11480168bf0dSLiam Girdwood 	if (!codec->probed &&
11490168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1150589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1151589c3563SJarkko Nikula 		if (ret < 0)
1152f0fba2adSLiam Girdwood 			return ret;
1153f0fba2adSLiam Girdwood 	}
1154f0fba2adSLiam Girdwood 
1155f0fba2adSLiam Girdwood 	/* probe the platform */
11560168bf0dSLiam Girdwood 	if (!platform->probed &&
11570168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1158956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1159956245e9SLiam Girdwood 		if (ret < 0)
1160f0fba2adSLiam Girdwood 			return ret;
1161f0fba2adSLiam Girdwood 	}
1162f0fba2adSLiam Girdwood 
1163f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
11640168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1165f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1166f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1167f0fba2adSLiam Girdwood 			if (ret < 0) {
1168f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1169f0fba2adSLiam Girdwood 						codec_dai->name);
1170f0fba2adSLiam Girdwood 				return ret;
1171f0fba2adSLiam Girdwood 			}
1172f0fba2adSLiam Girdwood 		}
1173f0fba2adSLiam Girdwood 
1174f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1175f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1176f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1177f0fba2adSLiam Girdwood 	}
1178f0fba2adSLiam Girdwood 
11790168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
11800168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
11810168bf0dSLiam Girdwood 		return 0;
11820168bf0dSLiam Girdwood 
1183589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1184589c3563SJarkko Nikula 	if (ret)
1185f0fba2adSLiam Girdwood 		return ret;
1186f0fba2adSLiam Girdwood 
1187f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1188f0fba2adSLiam Girdwood 	if (ret < 0)
1189f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1190f0fba2adSLiam Girdwood 
1191f0fba2adSLiam Girdwood 	/* create the pcm */
1192f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1193f0fba2adSLiam Girdwood 	if (ret < 0) {
1194f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1195f0fba2adSLiam Girdwood 		return ret;
1196f0fba2adSLiam Girdwood 	}
1197f0fba2adSLiam Girdwood 
1198f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1199f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1200f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1201f0fba2adSLiam Girdwood 
1202f0fba2adSLiam Girdwood 	return 0;
1203f0fba2adSLiam Girdwood }
1204f0fba2adSLiam Girdwood 
1205f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1206f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1207f0fba2adSLiam Girdwood {
1208f0fba2adSLiam Girdwood 	int ret;
1209f0fba2adSLiam Girdwood 
1210f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1211f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
12126b05eda6SMark Brown 	 */
1213f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
12140562f788SMika Westerberg 		/*
12150562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
12160562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
12170562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
12180562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
12190562f788SMika Westerberg 		 *
12200562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
12210562f788SMika Westerberg 		 */
12220562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
12230562f788SMika Westerberg 			return 0;
1224f0fba2adSLiam Girdwood 
1225f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1226f0fba2adSLiam Girdwood 		if (ret < 0) {
1227f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1228f0fba2adSLiam Girdwood 			return ret;
1229435c5e25SMark Brown 		}
1230435c5e25SMark Brown 
1231f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1232f0fba2adSLiam Girdwood 	}
1233f0fba2adSLiam Girdwood 	return 0;
1234f0fba2adSLiam Girdwood }
1235435c5e25SMark Brown 
1236f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1237f0fba2adSLiam Girdwood {
1238f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1239f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1240f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1241f0fba2adSLiam Girdwood 	}
1242f0fba2adSLiam Girdwood }
1243f0fba2adSLiam Girdwood #endif
1244435c5e25SMark Brown 
12452eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
12462eea392dSJarkko Nikula {
12472eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
12482eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1249676ad98aSJarkko Nikula 	int ret = -ENODEV;
12502eea392dSJarkko Nikula 
12512eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
12522eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
12532eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
12542eea392dSJarkko Nikula 			if (codec->probed) {
12552eea392dSJarkko Nikula 				dev_err(codec->dev,
12562eea392dSJarkko Nikula 					"asoc: codec already probed");
12572eea392dSJarkko Nikula 				ret = -EBUSY;
12582eea392dSJarkko Nikula 				goto out;
12592eea392dSJarkko Nikula 			}
1260676ad98aSJarkko Nikula 			goto found;
12612eea392dSJarkko Nikula 		}
12622eea392dSJarkko Nikula 	}
1263676ad98aSJarkko Nikula 	/* codec not found */
1264676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1265676ad98aSJarkko Nikula 	goto out;
12662eea392dSJarkko Nikula 
1267676ad98aSJarkko Nikula found:
1268589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
12692eea392dSJarkko Nikula 	if (ret < 0)
1270589c3563SJarkko Nikula 		return ret;
12712eea392dSJarkko Nikula 
1272589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
12732eea392dSJarkko Nikula 
12742eea392dSJarkko Nikula out:
12752eea392dSJarkko Nikula 	return ret;
12762eea392dSJarkko Nikula }
12772eea392dSJarkko Nikula 
12782eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
12792eea392dSJarkko Nikula {
12802eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
12812eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
12822eea392dSJarkko Nikula 
12832eea392dSJarkko Nikula 	/* unregister the rtd device */
12842eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1285589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
12862eea392dSJarkko Nikula 		device_unregister(&rtd->dev);
12872eea392dSJarkko Nikula 		rtd->dev_registered = 0;
12882eea392dSJarkko Nikula 	}
12892eea392dSJarkko Nikula 
1290589c3563SJarkko Nikula 	if (codec && codec->probed)
1291589c3563SJarkko Nikula 		soc_remove_codec(codec);
12922eea392dSJarkko Nikula }
12932eea392dSJarkko Nikula 
1294fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1295fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1296fdf0f54dSDimitris Papastamos {
1297fdf0f54dSDimitris Papastamos 	int ret;
1298fdf0f54dSDimitris Papastamos 
1299fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1300fdf0f54dSDimitris Papastamos 		return 0;
1301fdf0f54dSDimitris Papastamos 
1302fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1303fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1304fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1305fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1306fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1307fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1308fdf0f54dSDimitris Papastamos 			ret);
1309fdf0f54dSDimitris Papastamos 		return ret;
1310fdf0f54dSDimitris Papastamos 	}
1311fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1312fdf0f54dSDimitris Papastamos 	return 0;
1313fdf0f54dSDimitris Papastamos }
1314fdf0f54dSDimitris Papastamos 
1315f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1316f0fba2adSLiam Girdwood {
1317fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1318fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1319fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
13200168bf0dSLiam Girdwood 	int ret, i, order;
132196dd3622SMark Brown 
1322f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1323f0fba2adSLiam Girdwood 
1324f0fba2adSLiam Girdwood 	if (card->instantiated) {
1325f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1326435c5e25SMark Brown 		return;
1327db2a4165SFrank Mandarino 	}
1328db2a4165SFrank Mandarino 
1329f0fba2adSLiam Girdwood 	/* bind DAIs */
1330f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1331f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1332f0fba2adSLiam Girdwood 
1333f0fba2adSLiam Girdwood 	/* bind completed ? */
1334f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1335f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1336f0fba2adSLiam Girdwood 		return;
1337db2a4165SFrank Mandarino 	}
1338db2a4165SFrank Mandarino 
1339fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1340fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1341fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1342fdf0f54dSDimitris Papastamos 			continue;
1343861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1344861f2fafSDimitris Papastamos 		compress_type = 0;
1345fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1346fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1347fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1348fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1349fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1350fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1351fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1352fdf0f54dSDimitris Papastamos 					break;
1353fdf0f54dSDimitris Papastamos 			}
1354fdf0f54dSDimitris Papastamos 		}
1355fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1356fdf0f54dSDimitris Papastamos 		if (ret < 0) {
1357fdf0f54dSDimitris Papastamos 			mutex_unlock(&card->mutex);
1358fdf0f54dSDimitris Papastamos 			return;
1359fdf0f54dSDimitris Papastamos 		}
1360fdf0f54dSDimitris Papastamos 	}
1361fdf0f54dSDimitris Papastamos 
1362f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1363f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1364f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1365f0fba2adSLiam Girdwood 	if (ret < 0) {
1366f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1367f0fba2adSLiam Girdwood 			card->name);
1368f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1369f0fba2adSLiam Girdwood 		return;
1370db2a4165SFrank Mandarino 	}
1371f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1372db2a4165SFrank Mandarino 
1373e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1374e37a4970SMark Brown 	card->dapm.dev = card->dev;
1375e37a4970SMark Brown 	card->dapm.card = card;
1376e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1377e37a4970SMark Brown 
1378d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1379d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1380d5d1e0beSLars-Peter Clausen #endif
1381d5d1e0beSLars-Peter Clausen 
138288ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
13836ed25978SAndy Green 	/* deferred resume work */
13846308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
13851301a964SRandy Dunlap #endif
13866ed25978SAndy Green 
13879a841ebbSMark Brown 	if (card->dapm_widgets)
13889a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
13899a841ebbSMark Brown 					  card->num_dapm_widgets);
13909a841ebbSMark Brown 
1391f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1392f0fba2adSLiam Girdwood 	if (card->probe) {
1393e7361ec4SMark Brown 		ret = card->probe(card);
1394f0fba2adSLiam Girdwood 		if (ret < 0)
1395f0fba2adSLiam Girdwood 			goto card_probe_error;
1396f0fba2adSLiam Girdwood 	}
1397f0fba2adSLiam Girdwood 
13980168bf0dSLiam Girdwood 	/* early DAI link probe */
13990168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
14000168bf0dSLiam Girdwood 			order++) {
1401fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
14020168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1403fe3e78e0SMark Brown 			if (ret < 0) {
1404321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1405321de0d0SMark Brown 			       card->name, ret);
1406f0fba2adSLiam Girdwood 				goto probe_dai_err;
1407fe3e78e0SMark Brown 			}
1408fe3e78e0SMark Brown 		}
14090168bf0dSLiam Girdwood 	}
1410fe3e78e0SMark Brown 
14112eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
14122eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
14132eea392dSJarkko Nikula 		if (ret < 0) {
14142eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
14152eea392dSJarkko Nikula 			       card->name, ret);
14162eea392dSJarkko Nikula 			goto probe_aux_dev_err;
14172eea392dSJarkko Nikula 		}
14182eea392dSJarkko Nikula 	}
14192eea392dSJarkko Nikula 
1420b7af1dafSMark Brown 	/* We should have a non-codec control add function but we don't */
1421b7af1dafSMark Brown 	if (card->controls)
1422b7af1dafSMark Brown 		snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1423b7af1dafSMark Brown 						      struct snd_soc_codec,
1424b7af1dafSMark Brown 						      card_list),
1425b7af1dafSMark Brown 				     card->controls,
1426b7af1dafSMark Brown 				     card->num_controls);
1427b7af1dafSMark Brown 
1428b8ad29deSMark Brown 	if (card->dapm_routes)
1429b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1430b8ad29deSMark Brown 					card->num_dapm_routes);
1431b8ad29deSMark Brown 
1432f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1433fe3e78e0SMark Brown 		 "%s", card->name);
1434f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
143522de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
143622de71baSLiam Girdwood 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
14372b39535bSJarkko Nikula 		 "%s", card->driver_name ? card->driver_name : card->name);
1438fe3e78e0SMark Brown 
143928e9ad92SMark Brown 	if (card->late_probe) {
144028e9ad92SMark Brown 		ret = card->late_probe(card);
144128e9ad92SMark Brown 		if (ret < 0) {
144228e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
144328e9ad92SMark Brown 				card->name, ret);
144428e9ad92SMark Brown 			goto probe_aux_dev_err;
144528e9ad92SMark Brown 		}
144628e9ad92SMark Brown 	}
144728e9ad92SMark Brown 
1448f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1449fe3e78e0SMark Brown 	if (ret < 0) {
1450f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
14516b3ed785SAxel Lin 		goto probe_aux_dev_err;
1452fe3e78e0SMark Brown 	}
1453fe3e78e0SMark Brown 
1454fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1455f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1456f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1457f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1458fe3e78e0SMark Brown 		if (ret < 0) {
1459f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
14606b3ed785SAxel Lin 			while (--i >= 0)
14617d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
14626b3ed785SAxel Lin 			goto probe_aux_dev_err;
1463fe3e78e0SMark Brown 		}
1464fe3e78e0SMark Brown 	}
1465fe3e78e0SMark Brown #endif
1466fe3e78e0SMark Brown 
1467435c5e25SMark Brown 	card->instantiated = 1;
1468f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1469435c5e25SMark Brown 	return;
1470db2a4165SFrank Mandarino 
14712eea392dSJarkko Nikula probe_aux_dev_err:
14722eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
14732eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
14742eea392dSJarkko Nikula 
1475f0fba2adSLiam Girdwood probe_dai_err:
14760671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1477fe3e78e0SMark Brown 
1478f0fba2adSLiam Girdwood card_probe_error:
147987506549SMark Brown 	if (card->remove)
1480e7361ec4SMark Brown 		card->remove(card);
1481f0fba2adSLiam Girdwood 
1482f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1483f0fba2adSLiam Girdwood 
1484f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1485435c5e25SMark Brown }
1486db2a4165SFrank Mandarino 
1487435c5e25SMark Brown /*
1488421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1489435c5e25SMark Brown  * client_mutex.
1490435c5e25SMark Brown  */
1491435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1492435c5e25SMark Brown {
1493435c5e25SMark Brown 	struct snd_soc_card *card;
1494435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1495435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1496435c5e25SMark Brown }
1497435c5e25SMark Brown 
1498435c5e25SMark Brown /* probes a new socdev */
1499435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1500435c5e25SMark Brown {
1501f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1502435c5e25SMark Brown 	int ret = 0;
1503435c5e25SMark Brown 
150470a7ca34SVinod Koul 	/*
150570a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
150670a7ca34SVinod Koul 	 * we should not be here in that case so ret error
150770a7ca34SVinod Koul 	 */
150870a7ca34SVinod Koul 	if (!card)
150970a7ca34SVinod Koul 		return -EINVAL;
151070a7ca34SVinod Koul 
1511435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1512435c5e25SMark Brown 	card->dev = &pdev->dev;
1513f0fba2adSLiam Girdwood 
1514435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1515435c5e25SMark Brown 	if (ret != 0) {
1516435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1517db2a4165SFrank Mandarino 		return ret;
1518db2a4165SFrank Mandarino 	}
1519db2a4165SFrank Mandarino 
1520435c5e25SMark Brown 	return 0;
1521435c5e25SMark Brown }
1522435c5e25SMark Brown 
1523b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1524db2a4165SFrank Mandarino {
1525db2a4165SFrank Mandarino 	int i;
1526db2a4165SFrank Mandarino 
1527f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1528f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1529f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
15305b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1531db2a4165SFrank Mandarino 	}
1532db2a4165SFrank Mandarino 
15332eea392dSJarkko Nikula 	/* remove auxiliary devices */
15342eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
15352eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
15362eea392dSJarkko Nikula 
1537f0fba2adSLiam Girdwood 	/* remove and free each DAI */
15380671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1539f0fba2adSLiam Girdwood 
1540a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1541a6052154SJarkko Nikula 
1542f0fba2adSLiam Girdwood 	/* remove the card */
154387506549SMark Brown 	if (card->remove)
1544e7361ec4SMark Brown 		card->remove(card);
1545f0fba2adSLiam Girdwood 
15460aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
15470aaae527SLars-Peter Clausen 
1548f0fba2adSLiam Girdwood 	kfree(card->rtd);
1549f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1550b0e26485SVinod Koul 	return 0;
1551b0e26485SVinod Koul 
1552b2dfa62cSGuennadi Liakhovetski }
1553b0e26485SVinod Koul 
1554b0e26485SVinod Koul /* removes a socdev */
1555b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1556b0e26485SVinod Koul {
1557b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1558b0e26485SVinod Koul 
1559c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1560db2a4165SFrank Mandarino 	return 0;
1561db2a4165SFrank Mandarino }
1562db2a4165SFrank Mandarino 
15636f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
156451737470SMark Brown {
15656f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1566f0fba2adSLiam Girdwood 	int i;
156751737470SMark Brown 
156851737470SMark Brown 	if (!card->instantiated)
1569416356fcSMark Brown 		return 0;
157051737470SMark Brown 
157151737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
157251737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1573f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1574f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
15755b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1576f0fba2adSLiam Girdwood 	}
157751737470SMark Brown 
1578f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1579416356fcSMark Brown 
1580416356fcSMark Brown 	return 0;
158151737470SMark Brown }
15826f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
158351737470SMark Brown 
15846f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
15856f8ab4acSMark Brown 	.suspend = snd_soc_suspend,
15866f8ab4acSMark Brown 	.resume = snd_soc_resume,
15876f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1588416356fcSMark Brown };
1589deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1590416356fcSMark Brown 
1591db2a4165SFrank Mandarino /* ASoC platform driver */
1592db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1593db2a4165SFrank Mandarino 	.driver		= {
1594db2a4165SFrank Mandarino 		.name		= "soc-audio",
15958b45a209SKay Sievers 		.owner		= THIS_MODULE,
15966f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1597db2a4165SFrank Mandarino 	},
1598db2a4165SFrank Mandarino 	.probe		= soc_probe,
1599db2a4165SFrank Mandarino 	.remove		= soc_remove,
1600db2a4165SFrank Mandarino };
1601db2a4165SFrank Mandarino 
1602096e49d5SMark Brown /**
1603096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1604096e49d5SMark Brown  *
1605096e49d5SMark Brown  * @codec: CODEC to query.
1606096e49d5SMark Brown  * @reg: Register to query.
1607096e49d5SMark Brown  *
1608096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1609096e49d5SMark Brown  */
1610181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1611181e055eSMark Brown 				    unsigned int reg)
1612096e49d5SMark Brown {
16131500b7b5SDimitris Papastamos 	if (codec->volatile_register)
16141500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1615096e49d5SMark Brown 	else
1616096e49d5SMark Brown 		return 0;
1617096e49d5SMark Brown }
1618096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1619096e49d5SMark Brown 
1620db2a4165SFrank Mandarino /**
1621239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1622239c9706SDimitris Papastamos  *
1623239c9706SDimitris Papastamos  * @codec: CODEC to query.
1624239c9706SDimitris Papastamos  * @reg: Register to query.
1625239c9706SDimitris Papastamos  *
1626239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1627239c9706SDimitris Papastamos  */
1628239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1629239c9706SDimitris Papastamos 				    unsigned int reg)
1630239c9706SDimitris Papastamos {
1631239c9706SDimitris Papastamos 	if (codec->readable_register)
1632239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1633239c9706SDimitris Papastamos 	else
1634239c9706SDimitris Papastamos 		return 0;
1635239c9706SDimitris Papastamos }
1636239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1637239c9706SDimitris Papastamos 
1638239c9706SDimitris Papastamos /**
1639239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1640239c9706SDimitris Papastamos  *
1641239c9706SDimitris Papastamos  * @codec: CODEC to query.
1642239c9706SDimitris Papastamos  * @reg: Register to query.
1643239c9706SDimitris Papastamos  *
1644239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1645239c9706SDimitris Papastamos  */
1646239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1647239c9706SDimitris Papastamos 				    unsigned int reg)
1648239c9706SDimitris Papastamos {
1649239c9706SDimitris Papastamos 	if (codec->writable_register)
1650239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1651239c9706SDimitris Papastamos 	else
1652239c9706SDimitris Papastamos 		return 0;
1653239c9706SDimitris Papastamos }
1654239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1655239c9706SDimitris Papastamos 
1656f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1657f1442bc1SLiam Girdwood 					unsigned int reg)
1658f1442bc1SLiam Girdwood {
1659f1442bc1SLiam Girdwood 	unsigned int ret;
1660f1442bc1SLiam Girdwood 
1661f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1662f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1663f1442bc1SLiam Girdwood 		return -1;
1664f1442bc1SLiam Girdwood 	}
1665f1442bc1SLiam Girdwood 
1666f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1667f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1668f1442bc1SLiam Girdwood 
1669f1442bc1SLiam Girdwood 	return ret;
1670f1442bc1SLiam Girdwood }
1671f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1672f1442bc1SLiam Girdwood 
1673f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1674f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1675f1442bc1SLiam Girdwood {
1676f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1677f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1678f1442bc1SLiam Girdwood 		return -1;
1679f1442bc1SLiam Girdwood 	}
1680f1442bc1SLiam Girdwood 
1681f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1682f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1683f1442bc1SLiam Girdwood }
1684f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1685f1442bc1SLiam Girdwood 
1686239c9706SDimitris Papastamos /**
1687db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1688db2a4165SFrank Mandarino  * @codec: audio codec
1689db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1690db2a4165SFrank Mandarino  * @num: AC97 codec number
1691db2a4165SFrank Mandarino  *
1692db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1693db2a4165SFrank Mandarino  */
1694db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1695db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1696db2a4165SFrank Mandarino {
1697db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1698db2a4165SFrank Mandarino 
1699db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1700db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1701db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1702db2a4165SFrank Mandarino 		return -ENOMEM;
1703db2a4165SFrank Mandarino 	}
1704db2a4165SFrank Mandarino 
1705db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1706db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1707db2a4165SFrank Mandarino 		kfree(codec->ac97);
1708db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1709db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1710db2a4165SFrank Mandarino 		return -ENOMEM;
1711db2a4165SFrank Mandarino 	}
1712db2a4165SFrank Mandarino 
1713db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1714db2a4165SFrank Mandarino 	codec->ac97->num = num;
17150562f788SMika Westerberg 
17160562f788SMika Westerberg 	/*
17170562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
17180562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
17190562f788SMika Westerberg 	 */
17200562f788SMika Westerberg 	codec->ac97_created = 1;
17210562f788SMika Westerberg 
1722db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1723db2a4165SFrank Mandarino 	return 0;
1724db2a4165SFrank Mandarino }
1725db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1726db2a4165SFrank Mandarino 
1727db2a4165SFrank Mandarino /**
1728db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1729db2a4165SFrank Mandarino  * @codec: audio codec
1730db2a4165SFrank Mandarino  *
1731db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1732db2a4165SFrank Mandarino  */
1733db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1734db2a4165SFrank Mandarino {
1735db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1736f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1737f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1738f0fba2adSLiam Girdwood #endif
1739db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1740db2a4165SFrank Mandarino 	kfree(codec->ac97);
1741db2a4165SFrank Mandarino 	codec->ac97 = NULL;
17420562f788SMika Westerberg 	codec->ac97_created = 0;
1743db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1744db2a4165SFrank Mandarino }
1745db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1746db2a4165SFrank Mandarino 
1747c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1748c3753707SMark Brown {
1749c3753707SMark Brown 	unsigned int ret;
1750c3753707SMark Brown 
1751c3acec26SMark Brown 	ret = codec->read(codec, reg);
1752c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1753a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1754c3753707SMark Brown 
1755c3753707SMark Brown 	return ret;
1756c3753707SMark Brown }
1757c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1758c3753707SMark Brown 
1759c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1760c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1761c3753707SMark Brown {
1762c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1763a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1764c3acec26SMark Brown 	return codec->write(codec, reg, val);
1765c3753707SMark Brown }
1766c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1767c3753707SMark Brown 
17685fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
17695fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
17705fb609d4SDimitris Papastamos {
17715fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
17725fb609d4SDimitris Papastamos }
17735fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
17745fb609d4SDimitris Papastamos 
1775db2a4165SFrank Mandarino /**
1776db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1777db2a4165SFrank Mandarino  * @codec: audio codec
1778db2a4165SFrank Mandarino  * @reg: codec register
1779db2a4165SFrank Mandarino  * @mask: register mask
1780db2a4165SFrank Mandarino  * @value: new value
1781db2a4165SFrank Mandarino  *
1782db2a4165SFrank Mandarino  * Writes new register value.
1783db2a4165SFrank Mandarino  *
1784180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
1785db2a4165SFrank Mandarino  */
1786db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
178746f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1788db2a4165SFrank Mandarino {
1789db2a4165SFrank Mandarino 	int change;
179046f5822fSDaniel Ribeiro 	unsigned int old, new;
1791180c329dSTimur Tabi 	int ret;
1792db2a4165SFrank Mandarino 
1793180c329dSTimur Tabi 	ret = snd_soc_read(codec, reg);
1794180c329dSTimur Tabi 	if (ret < 0)
1795180c329dSTimur Tabi 		return ret;
1796180c329dSTimur Tabi 
1797180c329dSTimur Tabi 	old = ret;
179878bf3c9aSMark Brown 	new = (old & ~mask) | (value & mask);
1799db2a4165SFrank Mandarino 	change = old != new;
1800180c329dSTimur Tabi 	if (change) {
1801180c329dSTimur Tabi 		ret = snd_soc_write(codec, reg, new);
1802180c329dSTimur Tabi 		if (ret < 0)
1803180c329dSTimur Tabi 			return ret;
1804180c329dSTimur Tabi 	}
1805db2a4165SFrank Mandarino 
1806db2a4165SFrank Mandarino 	return change;
1807db2a4165SFrank Mandarino }
1808db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1809db2a4165SFrank Mandarino 
1810db2a4165SFrank Mandarino /**
18116c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
18126c508c62SEero Nurkkala  * @codec: audio codec
18136c508c62SEero Nurkkala  * @reg: codec register
18146c508c62SEero Nurkkala  * @mask: register mask
18156c508c62SEero Nurkkala  * @value: new value
18166c508c62SEero Nurkkala  *
18176c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
18186c508c62SEero Nurkkala  *
18196c508c62SEero Nurkkala  * Returns 1 for change else 0.
18206c508c62SEero Nurkkala  */
1821dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
18226c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
18236c508c62SEero Nurkkala 			       unsigned int value)
18246c508c62SEero Nurkkala {
18256c508c62SEero Nurkkala 	int change;
18266c508c62SEero Nurkkala 
18276c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
18286c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
18296c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
18306c508c62SEero Nurkkala 
18316c508c62SEero Nurkkala 	return change;
18326c508c62SEero Nurkkala }
1833dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
18346c508c62SEero Nurkkala 
18356c508c62SEero Nurkkala /**
1836db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1837db2a4165SFrank Mandarino  * @codec: audio codec
1838db2a4165SFrank Mandarino  * @reg: codec register
1839db2a4165SFrank Mandarino  * @mask: register mask
1840db2a4165SFrank Mandarino  * @value: new value
1841db2a4165SFrank Mandarino  *
1842db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1843db2a4165SFrank Mandarino  * different from the old value.
1844db2a4165SFrank Mandarino  *
1845db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1846db2a4165SFrank Mandarino  */
1847db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
184846f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1849db2a4165SFrank Mandarino {
1850db2a4165SFrank Mandarino 	int change;
185146f5822fSDaniel Ribeiro 	unsigned int old, new;
1852db2a4165SFrank Mandarino 
1853db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1854db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1855db2a4165SFrank Mandarino 	change = old != new;
1856db2a4165SFrank Mandarino 
1857db2a4165SFrank Mandarino 	return change;
1858db2a4165SFrank Mandarino }
1859db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1860db2a4165SFrank Mandarino 
1861db2a4165SFrank Mandarino /**
1862db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1863db2a4165SFrank Mandarino  * @substream: the pcm substream
1864db2a4165SFrank Mandarino  * @hw: the hardware parameters
1865db2a4165SFrank Mandarino  *
1866db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1867db2a4165SFrank Mandarino  */
1868db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1869db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1870db2a4165SFrank Mandarino {
1871db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1872db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1873db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1874db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1875db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1876db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1877db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1878db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1879db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1880db2a4165SFrank Mandarino 	return 0;
1881db2a4165SFrank Mandarino }
1882db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1883db2a4165SFrank Mandarino 
1884db2a4165SFrank Mandarino /**
1885db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1886db2a4165SFrank Mandarino  * @_template: control template
1887db2a4165SFrank Mandarino  * @data: control private data
1888ac11a2b3SMark Brown  * @long_name: control long name
1889efb7ac3fSMark Brown  * @prefix: control name prefix
1890db2a4165SFrank Mandarino  *
1891db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1892db2a4165SFrank Mandarino  *
1893db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1894db2a4165SFrank Mandarino  */
1895db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1896efb7ac3fSMark Brown 				  void *data, char *long_name,
1897efb7ac3fSMark Brown 				  const char *prefix)
1898db2a4165SFrank Mandarino {
1899db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1900efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
1901efb7ac3fSMark Brown 	char *name = NULL;
1902efb7ac3fSMark Brown 	int name_len;
1903db2a4165SFrank Mandarino 
1904db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1905db2a4165SFrank Mandarino 	template.index = 0;
1906db2a4165SFrank Mandarino 
1907efb7ac3fSMark Brown 	if (!long_name)
1908efb7ac3fSMark Brown 		long_name = template.name;
1909efb7ac3fSMark Brown 
1910efb7ac3fSMark Brown 	if (prefix) {
1911efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
1912efb7ac3fSMark Brown 		name = kmalloc(name_len, GFP_ATOMIC);
1913efb7ac3fSMark Brown 		if (!name)
1914efb7ac3fSMark Brown 			return NULL;
1915efb7ac3fSMark Brown 
1916efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
1917efb7ac3fSMark Brown 
1918efb7ac3fSMark Brown 		template.name = name;
1919efb7ac3fSMark Brown 	} else {
1920efb7ac3fSMark Brown 		template.name = long_name;
1921efb7ac3fSMark Brown 	}
1922efb7ac3fSMark Brown 
1923efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
1924efb7ac3fSMark Brown 
1925efb7ac3fSMark Brown 	kfree(name);
1926efb7ac3fSMark Brown 
1927efb7ac3fSMark Brown 	return kcontrol;
1928db2a4165SFrank Mandarino }
1929db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1930db2a4165SFrank Mandarino 
1931db2a4165SFrank Mandarino /**
19323e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
19333e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
19343e8e1952SIan Molton  * duplicating this code.
19353e8e1952SIan Molton  *
19363e8e1952SIan Molton  * @codec: codec to add controls to
19373e8e1952SIan Molton  * @controls: array of controls to add
19383e8e1952SIan Molton  * @num_controls: number of elements in the array
19393e8e1952SIan Molton  *
19403e8e1952SIan Molton  * Return 0 for success, else error.
19413e8e1952SIan Molton  */
19423e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
19433e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
19443e8e1952SIan Molton {
1945f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
19463e8e1952SIan Molton 	int err, i;
19473e8e1952SIan Molton 
19483e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
19493e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
1950efb7ac3fSMark Brown 		err = snd_ctl_add(card, snd_soc_cnew(control, codec,
1951efb7ac3fSMark Brown 						     control->name,
1952efb7ac3fSMark Brown 						     codec->name_prefix));
19533e8e1952SIan Molton 		if (err < 0) {
1954082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
1955efb7ac3fSMark Brown 				codec->name, control->name, err);
19563e8e1952SIan Molton 			return err;
19573e8e1952SIan Molton 		}
19583e8e1952SIan Molton 	}
19593e8e1952SIan Molton 
19603e8e1952SIan Molton 	return 0;
19613e8e1952SIan Molton }
19623e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
19633e8e1952SIan Molton 
19643e8e1952SIan Molton /**
1965a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
1966a491a5c8SLiam Girdwood  * Convienience function to add a list of controls.
1967a491a5c8SLiam Girdwood  *
1968a491a5c8SLiam Girdwood  * @platform: platform to add controls to
1969a491a5c8SLiam Girdwood  * @controls: array of controls to add
1970a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
1971a491a5c8SLiam Girdwood  *
1972a491a5c8SLiam Girdwood  * Return 0 for success, else error.
1973a491a5c8SLiam Girdwood  */
1974a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
1975a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
1976a491a5c8SLiam Girdwood {
1977a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
1978a491a5c8SLiam Girdwood 	int err, i;
1979a491a5c8SLiam Girdwood 
1980a491a5c8SLiam Girdwood 	for (i = 0; i < num_controls; i++) {
1981a491a5c8SLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
1982a491a5c8SLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, platform,
1983a491a5c8SLiam Girdwood 				control->name, NULL));
1984a491a5c8SLiam Girdwood 		if (err < 0) {
1985a491a5c8SLiam Girdwood 			dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
1986a491a5c8SLiam Girdwood 			return err;
1987a491a5c8SLiam Girdwood 		}
1988a491a5c8SLiam Girdwood 	}
1989a491a5c8SLiam Girdwood 
1990a491a5c8SLiam Girdwood 	return 0;
1991a491a5c8SLiam Girdwood }
1992a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1993a491a5c8SLiam Girdwood 
1994a491a5c8SLiam Girdwood /**
1995db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1996db2a4165SFrank Mandarino  * @kcontrol: mixer control
1997db2a4165SFrank Mandarino  * @uinfo: control element information
1998db2a4165SFrank Mandarino  *
1999db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2000db2a4165SFrank Mandarino  * mixer control.
2001db2a4165SFrank Mandarino  *
2002db2a4165SFrank Mandarino  * Returns 0 for success.
2003db2a4165SFrank Mandarino  */
2004db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2005db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2006db2a4165SFrank Mandarino {
2007db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2008db2a4165SFrank Mandarino 
2009db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2010db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2011f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2012db2a4165SFrank Mandarino 
2013f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2014f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2015db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2016db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2017db2a4165SFrank Mandarino 	return 0;
2018db2a4165SFrank Mandarino }
2019db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2020db2a4165SFrank Mandarino 
2021db2a4165SFrank Mandarino /**
2022db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2023db2a4165SFrank Mandarino  * @kcontrol: mixer control
2024ac11a2b3SMark Brown  * @ucontrol: control element information
2025db2a4165SFrank Mandarino  *
2026db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2027db2a4165SFrank Mandarino  *
2028db2a4165SFrank Mandarino  * Returns 0 for success.
2029db2a4165SFrank Mandarino  */
2030db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2031db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2032db2a4165SFrank Mandarino {
2033db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2034db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
203546f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2036db2a4165SFrank Mandarino 
2037f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2038db2a4165SFrank Mandarino 		;
2039db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
20403ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
20413ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2042db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2043db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2044db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2045db2a4165SFrank Mandarino 
2046db2a4165SFrank Mandarino 	return 0;
2047db2a4165SFrank Mandarino }
2048db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2049db2a4165SFrank Mandarino 
2050db2a4165SFrank Mandarino /**
2051db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2052db2a4165SFrank Mandarino  * @kcontrol: mixer control
2053ac11a2b3SMark Brown  * @ucontrol: control element information
2054db2a4165SFrank Mandarino  *
2055db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2056db2a4165SFrank Mandarino  *
2057db2a4165SFrank Mandarino  * Returns 0 for success.
2058db2a4165SFrank Mandarino  */
2059db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2060db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2061db2a4165SFrank Mandarino {
2062db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2063db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
206446f5822fSDaniel Ribeiro 	unsigned int val;
206546f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2066db2a4165SFrank Mandarino 
2067f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2068db2a4165SFrank Mandarino 		;
2069f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2070db2a4165SFrank Mandarino 		return -EINVAL;
2071db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2072db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2073db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2074f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2075db2a4165SFrank Mandarino 			return -EINVAL;
2076db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2077db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2078db2a4165SFrank Mandarino 	}
2079db2a4165SFrank Mandarino 
20806c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2081db2a4165SFrank Mandarino }
2082db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2083db2a4165SFrank Mandarino 
2084db2a4165SFrank Mandarino /**
20852e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
20862e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
20872e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
20882e72f8e3SPeter Ujfalusi  *
20892e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
20902e72f8e3SPeter Ujfalusi  *
20912e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
20922e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
20932e72f8e3SPeter Ujfalusi  *
20942e72f8e3SPeter Ujfalusi  * Returns 0 for success.
20952e72f8e3SPeter Ujfalusi  */
20962e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
20972e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
20982e72f8e3SPeter Ujfalusi {
20992e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
210074155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
210146f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
21022e72f8e3SPeter Ujfalusi 
21032e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
21042e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
21052e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
21062e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
21072e72f8e3SPeter Ujfalusi 			break;
21082e72f8e3SPeter Ujfalusi 	}
21092e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
21102e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
21112e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
21122e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
21132e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
21142e72f8e3SPeter Ujfalusi 				break;
21152e72f8e3SPeter Ujfalusi 		}
21162e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
21172e72f8e3SPeter Ujfalusi 	}
21182e72f8e3SPeter Ujfalusi 
21192e72f8e3SPeter Ujfalusi 	return 0;
21202e72f8e3SPeter Ujfalusi }
21212e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
21222e72f8e3SPeter Ujfalusi 
21232e72f8e3SPeter Ujfalusi /**
21242e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
21252e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
21262e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
21272e72f8e3SPeter Ujfalusi  *
21282e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
21292e72f8e3SPeter Ujfalusi  *
21302e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
21312e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
21322e72f8e3SPeter Ujfalusi  *
21332e72f8e3SPeter Ujfalusi  * Returns 0 for success.
21342e72f8e3SPeter Ujfalusi  */
21352e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
21362e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
21372e72f8e3SPeter Ujfalusi {
21382e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
213974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
214046f5822fSDaniel Ribeiro 	unsigned int val;
214146f5822fSDaniel Ribeiro 	unsigned int mask;
21422e72f8e3SPeter Ujfalusi 
21432e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
21442e72f8e3SPeter Ujfalusi 		return -EINVAL;
21452e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
21462e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
21472e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
21482e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
21492e72f8e3SPeter Ujfalusi 			return -EINVAL;
21502e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
21512e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
21522e72f8e3SPeter Ujfalusi 	}
21532e72f8e3SPeter Ujfalusi 
21546c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
21552e72f8e3SPeter Ujfalusi }
21562e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
21572e72f8e3SPeter Ujfalusi 
21582e72f8e3SPeter Ujfalusi /**
2159db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2160db2a4165SFrank Mandarino  * @kcontrol: mixer control
2161db2a4165SFrank Mandarino  * @uinfo: control element information
2162db2a4165SFrank Mandarino  *
2163db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2164db2a4165SFrank Mandarino  * single mixer.
2165db2a4165SFrank Mandarino  *
2166db2a4165SFrank Mandarino  * Returns 0 for success.
2167db2a4165SFrank Mandarino  */
2168db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2169db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2170db2a4165SFrank Mandarino {
2171db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2172db2a4165SFrank Mandarino 
2173db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2174db2a4165SFrank Mandarino 	uinfo->count = 1;
2175f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2176db2a4165SFrank Mandarino 
2177f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2178f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2179db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2180db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2181db2a4165SFrank Mandarino 	return 0;
2182db2a4165SFrank Mandarino }
2183db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2184db2a4165SFrank Mandarino 
2185db2a4165SFrank Mandarino /**
2186db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2187db2a4165SFrank Mandarino  * @kcontrol: mixer control
2188db2a4165SFrank Mandarino  * @uinfo: control element information
2189db2a4165SFrank Mandarino  *
2190db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2191db2a4165SFrank Mandarino  *
2192db2a4165SFrank Mandarino  * Returns 0 for success.
2193db2a4165SFrank Mandarino  */
2194db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2195db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2196db2a4165SFrank Mandarino {
2197a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2198db2a4165SFrank Mandarino 
2199fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2200a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2201a7a4ac86SPhilipp Zabel 	else
2202a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2203a7a4ac86SPhilipp Zabel 
2204db2a4165SFrank Mandarino 	uinfo->count = 1;
2205db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2206a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2207db2a4165SFrank Mandarino 	return 0;
2208db2a4165SFrank Mandarino }
2209db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2210db2a4165SFrank Mandarino 
2211db2a4165SFrank Mandarino /**
2212db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2213db2a4165SFrank Mandarino  * @kcontrol: mixer control
2214db2a4165SFrank Mandarino  * @uinfo: control element information
2215db2a4165SFrank Mandarino  *
2216db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2217db2a4165SFrank Mandarino  *
2218db2a4165SFrank Mandarino  * Returns 0 for success.
2219db2a4165SFrank Mandarino  */
2220db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2221db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2222db2a4165SFrank Mandarino {
22234eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22244eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2225d11bb4a9SPeter Ujfalusi 	int platform_max;
2226762b8df7SMark Brown 	unsigned int shift = mc->shift;
2227815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2228db2a4165SFrank Mandarino 
2229d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2230d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2231d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2232d11bb4a9SPeter Ujfalusi 
2233d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2234a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2235a7a4ac86SPhilipp Zabel 	else
2236a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2237a7a4ac86SPhilipp Zabel 
2238db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2239db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2240d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2241db2a4165SFrank Mandarino 	return 0;
2242db2a4165SFrank Mandarino }
2243db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2244db2a4165SFrank Mandarino 
2245db2a4165SFrank Mandarino /**
2246db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2247db2a4165SFrank Mandarino  * @kcontrol: mixer control
2248ac11a2b3SMark Brown  * @ucontrol: control element information
2249db2a4165SFrank Mandarino  *
2250db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2251db2a4165SFrank Mandarino  *
2252db2a4165SFrank Mandarino  * Returns 0 for success.
2253db2a4165SFrank Mandarino  */
2254db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2255db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2256db2a4165SFrank Mandarino {
22574eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22584eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2259db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2260815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2261815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2262815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
22634eaa9819SJon Smirl 	int max = mc->max;
2264815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2265815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2266db2a4165SFrank Mandarino 
2267db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2268db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2269db2a4165SFrank Mandarino 	if (shift != rshift)
2270db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2271db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2272db2a4165SFrank Mandarino 	if (invert) {
2273db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2274a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2275db2a4165SFrank Mandarino 		if (shift != rshift)
2276db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2277a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2278db2a4165SFrank Mandarino 	}
2279db2a4165SFrank Mandarino 
2280db2a4165SFrank Mandarino 	return 0;
2281db2a4165SFrank Mandarino }
2282db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2283db2a4165SFrank Mandarino 
2284db2a4165SFrank Mandarino /**
2285db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2286db2a4165SFrank Mandarino  * @kcontrol: mixer control
2287ac11a2b3SMark Brown  * @ucontrol: control element information
2288db2a4165SFrank Mandarino  *
2289db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2290db2a4165SFrank Mandarino  *
2291db2a4165SFrank Mandarino  * Returns 0 for success.
2292db2a4165SFrank Mandarino  */
2293db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2294db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2295db2a4165SFrank Mandarino {
22964eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22974eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2298db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2299815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2300815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2301815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
23024eaa9819SJon Smirl 	int max = mc->max;
2303815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2304815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
230546f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2306db2a4165SFrank Mandarino 
2307db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2308db2a4165SFrank Mandarino 	if (invert)
2309a7a4ac86SPhilipp Zabel 		val = max - val;
2310db2a4165SFrank Mandarino 	val_mask = mask << shift;
2311db2a4165SFrank Mandarino 	val = val << shift;
2312db2a4165SFrank Mandarino 	if (shift != rshift) {
2313db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2314db2a4165SFrank Mandarino 		if (invert)
2315a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2316db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2317db2a4165SFrank Mandarino 		val |= val2 << rshift;
2318db2a4165SFrank Mandarino 	}
23196c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2320db2a4165SFrank Mandarino }
2321db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2322db2a4165SFrank Mandarino 
2323db2a4165SFrank Mandarino /**
2324db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2325db2a4165SFrank Mandarino  * @kcontrol: mixer control
2326db2a4165SFrank Mandarino  * @uinfo: control element information
2327db2a4165SFrank Mandarino  *
2328db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2329db2a4165SFrank Mandarino  * spans 2 codec registers.
2330db2a4165SFrank Mandarino  *
2331db2a4165SFrank Mandarino  * Returns 0 for success.
2332db2a4165SFrank Mandarino  */
2333db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2334db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2335db2a4165SFrank Mandarino {
23364eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23374eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2338d11bb4a9SPeter Ujfalusi 	int platform_max;
2339db2a4165SFrank Mandarino 
2340d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2341d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2342d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2343d11bb4a9SPeter Ujfalusi 
2344d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2345a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2346a7a4ac86SPhilipp Zabel 	else
2347a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2348a7a4ac86SPhilipp Zabel 
2349db2a4165SFrank Mandarino 	uinfo->count = 2;
2350db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2351d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2352db2a4165SFrank Mandarino 	return 0;
2353db2a4165SFrank Mandarino }
2354db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2355db2a4165SFrank Mandarino 
2356db2a4165SFrank Mandarino /**
2357db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2358db2a4165SFrank Mandarino  * @kcontrol: mixer control
2359ac11a2b3SMark Brown  * @ucontrol: control element information
2360db2a4165SFrank Mandarino  *
2361db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2362db2a4165SFrank Mandarino  *
2363db2a4165SFrank Mandarino  * Returns 0 for success.
2364db2a4165SFrank Mandarino  */
2365db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2366db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2367db2a4165SFrank Mandarino {
23684eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23694eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2370db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2371815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2372815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2373815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
23744eaa9819SJon Smirl 	int max = mc->max;
2375815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2376815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2377db2a4165SFrank Mandarino 
2378db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2379db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2380db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2381db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2382db2a4165SFrank Mandarino 	if (invert) {
2383db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2384a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2385db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2386a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2387db2a4165SFrank Mandarino 	}
2388db2a4165SFrank Mandarino 
2389db2a4165SFrank Mandarino 	return 0;
2390db2a4165SFrank Mandarino }
2391db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2392db2a4165SFrank Mandarino 
2393db2a4165SFrank Mandarino /**
2394db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2395db2a4165SFrank Mandarino  * @kcontrol: mixer control
2396ac11a2b3SMark Brown  * @ucontrol: control element information
2397db2a4165SFrank Mandarino  *
2398db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2399db2a4165SFrank Mandarino  *
2400db2a4165SFrank Mandarino  * Returns 0 for success.
2401db2a4165SFrank Mandarino  */
2402db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2403db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2404db2a4165SFrank Mandarino {
24054eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24064eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2407db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2408815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2409815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2410815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
24114eaa9819SJon Smirl 	int max = mc->max;
2412815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2413815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2414db2a4165SFrank Mandarino 	int err;
241546f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2416db2a4165SFrank Mandarino 
2417db2a4165SFrank Mandarino 	val_mask = mask << shift;
2418db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2419db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2420db2a4165SFrank Mandarino 
2421db2a4165SFrank Mandarino 	if (invert) {
2422a7a4ac86SPhilipp Zabel 		val = max - val;
2423a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2424db2a4165SFrank Mandarino 	}
2425db2a4165SFrank Mandarino 
2426db2a4165SFrank Mandarino 	val = val << shift;
2427db2a4165SFrank Mandarino 	val2 = val2 << shift;
2428db2a4165SFrank Mandarino 
24296c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
24303ff3f64bSMark Brown 	if (err < 0)
2431db2a4165SFrank Mandarino 		return err;
2432db2a4165SFrank Mandarino 
24336c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2434db2a4165SFrank Mandarino 	return err;
2435db2a4165SFrank Mandarino }
2436db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2437db2a4165SFrank Mandarino 
2438e13ac2e9SMark Brown /**
2439e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2440e13ac2e9SMark Brown  * @kcontrol: mixer control
2441e13ac2e9SMark Brown  * @uinfo: control element information
2442e13ac2e9SMark Brown  *
2443e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2444e13ac2e9SMark Brown  *
2445e13ac2e9SMark Brown  * Returns 0 for success.
2446e13ac2e9SMark Brown  */
2447e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2448e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2449e13ac2e9SMark Brown {
24504eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24514eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2452d11bb4a9SPeter Ujfalusi 	int platform_max;
24534eaa9819SJon Smirl 	int min = mc->min;
2454e13ac2e9SMark Brown 
2455d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2456d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2457d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2458d11bb4a9SPeter Ujfalusi 
2459e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2460e13ac2e9SMark Brown 	uinfo->count = 2;
2461e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2462d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2463e13ac2e9SMark Brown 	return 0;
2464e13ac2e9SMark Brown }
2465e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2466e13ac2e9SMark Brown 
2467e13ac2e9SMark Brown /**
2468e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2469e13ac2e9SMark Brown  * @kcontrol: mixer control
2470ac11a2b3SMark Brown  * @ucontrol: control element information
2471e13ac2e9SMark Brown  *
2472e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2473e13ac2e9SMark Brown  *
2474e13ac2e9SMark Brown  * Returns 0 for success.
2475e13ac2e9SMark Brown  */
2476e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2477e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2478e13ac2e9SMark Brown {
24794eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24804eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2481e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2482815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
24834eaa9819SJon Smirl 	int min = mc->min;
2484e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2485e13ac2e9SMark Brown 
2486e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2487e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2488e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2489e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2490e13ac2e9SMark Brown 	return 0;
2491e13ac2e9SMark Brown }
2492e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2493e13ac2e9SMark Brown 
2494e13ac2e9SMark Brown /**
2495e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2496e13ac2e9SMark Brown  * @kcontrol: mixer control
2497ac11a2b3SMark Brown  * @ucontrol: control element information
2498e13ac2e9SMark Brown  *
2499e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2500e13ac2e9SMark Brown  *
2501e13ac2e9SMark Brown  * Returns 0 for success.
2502e13ac2e9SMark Brown  */
2503e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2504e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2505e13ac2e9SMark Brown {
25064eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25074eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2508e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2509815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
25104eaa9819SJon Smirl 	int min = mc->min;
251146f5822fSDaniel Ribeiro 	unsigned int val;
2512e13ac2e9SMark Brown 
2513e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2514e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2515e13ac2e9SMark Brown 
25166c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2517e13ac2e9SMark Brown }
2518e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2519e13ac2e9SMark Brown 
25208c6529dbSLiam Girdwood /**
2521637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2522637d3847SPeter Ujfalusi  *
2523637d3847SPeter Ujfalusi  * @codec: where to look for the control
2524637d3847SPeter Ujfalusi  * @name: Name of the control
2525637d3847SPeter Ujfalusi  * @max: new maximum limit
2526637d3847SPeter Ujfalusi  *
2527637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2528637d3847SPeter Ujfalusi  */
2529637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2530637d3847SPeter Ujfalusi 	const char *name, int max)
2531637d3847SPeter Ujfalusi {
2532f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2533637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2534637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2535637d3847SPeter Ujfalusi 	int found = 0;
2536637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2537637d3847SPeter Ujfalusi 
2538637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2539637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2540637d3847SPeter Ujfalusi 		return -EINVAL;
2541637d3847SPeter Ujfalusi 
2542637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2543637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2544637d3847SPeter Ujfalusi 			found = 1;
2545637d3847SPeter Ujfalusi 			break;
2546637d3847SPeter Ujfalusi 		}
2547637d3847SPeter Ujfalusi 	}
2548637d3847SPeter Ujfalusi 	if (found) {
2549637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2550637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2551d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2552637d3847SPeter Ujfalusi 			ret = 0;
2553637d3847SPeter Ujfalusi 		}
2554637d3847SPeter Ujfalusi 	}
2555637d3847SPeter Ujfalusi 	return ret;
2556637d3847SPeter Ujfalusi }
2557637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2558637d3847SPeter Ujfalusi 
2559637d3847SPeter Ujfalusi /**
2560b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2561b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2562b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2563b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2564b6f4bb38Sapatard@mandriva.com  *
2565b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2566b6f4bb38Sapatard@mandriva.com  */
2567b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2568b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2569b6f4bb38Sapatard@mandriva.com {
2570b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2571b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2572b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2573b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2574b6f4bb38Sapatard@mandriva.com 
2575b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2576b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2577b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2578b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2579b6f4bb38Sapatard@mandriva.com 
2580b6f4bb38Sapatard@mandriva.com 	return 0;
2581b6f4bb38Sapatard@mandriva.com }
2582b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2583b6f4bb38Sapatard@mandriva.com 
2584b6f4bb38Sapatard@mandriva.com /**
2585b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2586b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2587b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2588b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2589b6f4bb38Sapatard@mandriva.com  *
2590b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2591b6f4bb38Sapatard@mandriva.com  */
2592b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2593b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2594b6f4bb38Sapatard@mandriva.com {
2595b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2596b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2597b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2598b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2599b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2600b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2601b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2602b6f4bb38Sapatard@mandriva.com 
260320630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
260420630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2605b6f4bb38Sapatard@mandriva.com 	return 0;
2606b6f4bb38Sapatard@mandriva.com }
2607b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2608b6f4bb38Sapatard@mandriva.com 
2609b6f4bb38Sapatard@mandriva.com /**
2610b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2611b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2612b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2613b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2614b6f4bb38Sapatard@mandriva.com  *
2615b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2616b6f4bb38Sapatard@mandriva.com  */
2617b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2618b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2619b6f4bb38Sapatard@mandriva.com {
2620b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2621b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2622b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2623b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2624b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2625b6f4bb38Sapatard@mandriva.com 	int ret;
2626b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2627b6f4bb38Sapatard@mandriva.com 
2628b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2629b6f4bb38Sapatard@mandriva.com 	val &= mask;
2630b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2631b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2632b6f4bb38Sapatard@mandriva.com 
2633b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2634b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2635b6f4bb38Sapatard@mandriva.com 
2636b6f4bb38Sapatard@mandriva.com 	ret = 0;
2637b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2638b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2639b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2640f1df5aecSMark Brown 			return ret;
2641b6f4bb38Sapatard@mandriva.com 	}
2642b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2643b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2644b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2645f1df5aecSMark Brown 			return ret;
2646b6f4bb38Sapatard@mandriva.com 	}
2647b6f4bb38Sapatard@mandriva.com 
2648b6f4bb38Sapatard@mandriva.com 	return 0;
2649b6f4bb38Sapatard@mandriva.com }
2650b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2651b6f4bb38Sapatard@mandriva.com 
2652b6f4bb38Sapatard@mandriva.com /**
26538c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
26548c6529dbSLiam Girdwood  * @dai: DAI
26558c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
26568c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
26578c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
26588c6529dbSLiam Girdwood  *
26598c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
26608c6529dbSLiam Girdwood  */
26618c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
26628c6529dbSLiam Girdwood 	unsigned int freq, int dir)
26638c6529dbSLiam Girdwood {
2664f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2665f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2666ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2667ec4ee52aSMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id,
2668ec4ee52aSMark Brown 						      freq, dir);
26698c6529dbSLiam Girdwood 	else
26708c6529dbSLiam Girdwood 		return -EINVAL;
26718c6529dbSLiam Girdwood }
26728c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
26738c6529dbSLiam Girdwood 
26748c6529dbSLiam Girdwood /**
2675ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2676ec4ee52aSMark Brown  * @codec: CODEC
2677ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2678ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2679ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2680ec4ee52aSMark Brown  *
2681ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2682ec4ee52aSMark Brown  */
2683ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2684ec4ee52aSMark Brown 	unsigned int freq, int dir)
2685ec4ee52aSMark Brown {
2686ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2687ec4ee52aSMark Brown 		return codec->driver->set_sysclk(codec, clk_id, freq, dir);
2688ec4ee52aSMark Brown 	else
2689ec4ee52aSMark Brown 		return -EINVAL;
2690ec4ee52aSMark Brown }
2691ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2692ec4ee52aSMark Brown 
2693ec4ee52aSMark Brown /**
26948c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
26958c6529dbSLiam Girdwood  * @dai: DAI
2696ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
26978c6529dbSLiam Girdwood  * @div: new clock divisor.
26988c6529dbSLiam Girdwood  *
26998c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
27008c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
27018c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
27028c6529dbSLiam Girdwood  */
27038c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
27048c6529dbSLiam Girdwood 	int div_id, int div)
27058c6529dbSLiam Girdwood {
2706f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2707f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
27088c6529dbSLiam Girdwood 	else
27098c6529dbSLiam Girdwood 		return -EINVAL;
27108c6529dbSLiam Girdwood }
27118c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
27128c6529dbSLiam Girdwood 
27138c6529dbSLiam Girdwood /**
27148c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
27158c6529dbSLiam Girdwood  * @dai: DAI
27168c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
271785488037SMark Brown  * @source: DAI specific source for the PLL
27188c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
27198c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
27208c6529dbSLiam Girdwood  *
27218c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
27228c6529dbSLiam Girdwood  */
272385488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
272485488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
27258c6529dbSLiam Girdwood {
2726f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2727f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
272885488037SMark Brown 					 freq_in, freq_out);
2729ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2730ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2731ec4ee52aSMark Brown 						   freq_in, freq_out);
27328c6529dbSLiam Girdwood 	else
27338c6529dbSLiam Girdwood 		return -EINVAL;
27348c6529dbSLiam Girdwood }
27358c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
27368c6529dbSLiam Girdwood 
2737ec4ee52aSMark Brown /*
2738ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2739ec4ee52aSMark Brown  * @codec: CODEC
2740ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2741ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2742ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2743ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2744ec4ee52aSMark Brown  *
2745ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2746ec4ee52aSMark Brown  */
2747ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2748ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2749ec4ee52aSMark Brown {
2750ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2751ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2752ec4ee52aSMark Brown 					      freq_in, freq_out);
2753ec4ee52aSMark Brown 	else
2754ec4ee52aSMark Brown 		return -EINVAL;
2755ec4ee52aSMark Brown }
2756ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2757ec4ee52aSMark Brown 
27588c6529dbSLiam Girdwood /**
27598c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
27608c6529dbSLiam Girdwood  * @dai: DAI
27618c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
27628c6529dbSLiam Girdwood  *
27638c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
27648c6529dbSLiam Girdwood  */
27658c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
27668c6529dbSLiam Girdwood {
2767f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
2768f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
27698c6529dbSLiam Girdwood 	else
27708c6529dbSLiam Girdwood 		return -EINVAL;
27718c6529dbSLiam Girdwood }
27728c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
27738c6529dbSLiam Girdwood 
27748c6529dbSLiam Girdwood /**
27758c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
27768c6529dbSLiam Girdwood  * @dai: DAI
2777a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2778a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
27798c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2780a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
27818c6529dbSLiam Girdwood  *
27828c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
27838c6529dbSLiam Girdwood  * specific.
27848c6529dbSLiam Girdwood  */
27858c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2786a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
27878c6529dbSLiam Girdwood {
2788f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2789f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2790a5479e38SDaniel Ribeiro 				slots, slot_width);
27918c6529dbSLiam Girdwood 	else
27928c6529dbSLiam Girdwood 		return -EINVAL;
27938c6529dbSLiam Girdwood }
27948c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
27958c6529dbSLiam Girdwood 
27968c6529dbSLiam Girdwood /**
2797472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2798472df3cbSBarry Song  * @dai: DAI
2799472df3cbSBarry Song  * @tx_num: how many TX channels
2800472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2801472df3cbSBarry Song  *           0~num-1 uses
2802472df3cbSBarry Song  * @rx_num: how many RX channels
2803472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2804472df3cbSBarry Song  *           0~num-1 uses
2805472df3cbSBarry Song  *
2806472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2807472df3cbSBarry Song  */
2808472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2809472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2810472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2811472df3cbSBarry Song {
2812f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2813f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2814472df3cbSBarry Song 			rx_num, rx_slot);
2815472df3cbSBarry Song 	else
2816472df3cbSBarry Song 		return -EINVAL;
2817472df3cbSBarry Song }
2818472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2819472df3cbSBarry Song 
2820472df3cbSBarry Song /**
28218c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
28228c6529dbSLiam Girdwood  * @dai: DAI
28238c6529dbSLiam Girdwood  * @tristate: tristate enable
28248c6529dbSLiam Girdwood  *
28258c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
28268c6529dbSLiam Girdwood  */
28278c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
28288c6529dbSLiam Girdwood {
2829f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2830f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
28318c6529dbSLiam Girdwood 	else
28328c6529dbSLiam Girdwood 		return -EINVAL;
28338c6529dbSLiam Girdwood }
28348c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
28358c6529dbSLiam Girdwood 
28368c6529dbSLiam Girdwood /**
28378c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
28388c6529dbSLiam Girdwood  * @dai: DAI
28398c6529dbSLiam Girdwood  * @mute: mute enable
28408c6529dbSLiam Girdwood  *
28418c6529dbSLiam Girdwood  * Mutes the DAI DAC.
28428c6529dbSLiam Girdwood  */
28438c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
28448c6529dbSLiam Girdwood {
2845f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
2846f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
28478c6529dbSLiam Girdwood 	else
28488c6529dbSLiam Girdwood 		return -EINVAL;
28498c6529dbSLiam Girdwood }
28508c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
28518c6529dbSLiam Girdwood 
2852c5af3a2eSMark Brown /**
2853c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2854c5af3a2eSMark Brown  *
2855ac11a2b3SMark Brown  * @card: Card to register
2856c5af3a2eSMark Brown  *
2857c5af3a2eSMark Brown  */
285870a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2859c5af3a2eSMark Brown {
2860f0fba2adSLiam Girdwood 	int i;
2861f0fba2adSLiam Girdwood 
2862c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2863c5af3a2eSMark Brown 		return -EINVAL;
2864c5af3a2eSMark Brown 
2865ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2866ed77cc12SMark Brown 
2867111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2868111c6419SStephen Warren 
2869150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
2870150dd2f8SVinod Koul 
28712eea392dSJarkko Nikula 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
28722eea392dSJarkko Nikula 			    (card->num_links + card->num_aux_devs),
2873f0fba2adSLiam Girdwood 			    GFP_KERNEL);
2874f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2875f0fba2adSLiam Girdwood 		return -ENOMEM;
28762eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
2877f0fba2adSLiam Girdwood 
2878f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
2879f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
2880f0fba2adSLiam Girdwood 
2881c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2882c5af3a2eSMark Brown 	card->instantiated = 0;
2883f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2884c5af3a2eSMark Brown 
2885c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2886c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2887435c5e25SMark Brown 	snd_soc_instantiate_cards();
2888c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2889c5af3a2eSMark Brown 
2890c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2891c5af3a2eSMark Brown 
2892c5af3a2eSMark Brown 	return 0;
2893c5af3a2eSMark Brown }
289470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2895c5af3a2eSMark Brown 
2896c5af3a2eSMark Brown /**
2897c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2898c5af3a2eSMark Brown  *
2899ac11a2b3SMark Brown  * @card: Card to unregister
2900c5af3a2eSMark Brown  *
2901c5af3a2eSMark Brown  */
290270a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2903c5af3a2eSMark Brown {
2904b0e26485SVinod Koul 	if (card->instantiated)
2905b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2906c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2907c5af3a2eSMark Brown 	list_del(&card->list);
2908c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2909c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2910c5af3a2eSMark Brown 
2911c5af3a2eSMark Brown 	return 0;
2912c5af3a2eSMark Brown }
291370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2914c5af3a2eSMark Brown 
2915f0fba2adSLiam Girdwood /*
2916f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2917f0fba2adSLiam Girdwood  * and sanitizing names.
2918f0fba2adSLiam Girdwood  */
29190b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2920f0fba2adSLiam Girdwood {
2921f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2922f0fba2adSLiam Girdwood 	int id1, id2;
2923f0fba2adSLiam Girdwood 
2924f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2925f0fba2adSLiam Girdwood 		return NULL;
2926f0fba2adSLiam Girdwood 
292758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2928f0fba2adSLiam Girdwood 
2929f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2930f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2931f0fba2adSLiam Girdwood 	if (found) {
2932f0fba2adSLiam Girdwood 		/* get ID */
2933f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2934f0fba2adSLiam Girdwood 
2935f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2936f0fba2adSLiam Girdwood 			if (*id == -1)
2937f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2938f0fba2adSLiam Girdwood 		}
2939f0fba2adSLiam Girdwood 
2940f0fba2adSLiam Girdwood 	} else {
2941f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2942f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2943f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2944f0fba2adSLiam Girdwood 
2945f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
294605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2947f0fba2adSLiam Girdwood 
2948f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2949f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
295058818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2951f0fba2adSLiam Girdwood 		} else
2952f0fba2adSLiam Girdwood 			*id = 0;
2953f0fba2adSLiam Girdwood 	}
2954f0fba2adSLiam Girdwood 
2955f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2956f0fba2adSLiam Girdwood }
2957f0fba2adSLiam Girdwood 
2958f0fba2adSLiam Girdwood /*
2959f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2960f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2961f0fba2adSLiam Girdwood  */
2962f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2963f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2964f0fba2adSLiam Girdwood {
2965f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
2966f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2967f0fba2adSLiam Girdwood 				dev_name(dev));
2968f0fba2adSLiam Girdwood 		return NULL;
2969f0fba2adSLiam Girdwood 	}
2970f0fba2adSLiam Girdwood 
2971f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2972f0fba2adSLiam Girdwood }
2973f0fba2adSLiam Girdwood 
29749115171aSMark Brown /**
29759115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
29769115171aSMark Brown  *
2977ac11a2b3SMark Brown  * @dai: DAI to register
29789115171aSMark Brown  */
2979f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
2980f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
29819115171aSMark Brown {
2982f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
29839115171aSMark Brown 
2984f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
29859115171aSMark Brown 
2986f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2987f0fba2adSLiam Girdwood 	if (dai == NULL)
2988f0fba2adSLiam Girdwood 		return -ENOMEM;
29896335d055SEric Miao 
2990f0fba2adSLiam Girdwood 	/* create DAI component name */
2991f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
2992f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
2993f0fba2adSLiam Girdwood 		kfree(dai);
2994f0fba2adSLiam Girdwood 		return -ENOMEM;
2995f0fba2adSLiam Girdwood 	}
2996f0fba2adSLiam Girdwood 
2997f0fba2adSLiam Girdwood 	dai->dev = dev;
2998f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
2999f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3000f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
30019115171aSMark Brown 
30029115171aSMark Brown 	mutex_lock(&client_mutex);
30039115171aSMark Brown 	list_add(&dai->list, &dai_list);
3004435c5e25SMark Brown 	snd_soc_instantiate_cards();
30059115171aSMark Brown 	mutex_unlock(&client_mutex);
30069115171aSMark Brown 
30079115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
30089115171aSMark Brown 
30099115171aSMark Brown 	return 0;
30109115171aSMark Brown }
30119115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
30129115171aSMark Brown 
30139115171aSMark Brown /**
30149115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
30159115171aSMark Brown  *
3016ac11a2b3SMark Brown  * @dai: DAI to unregister
30179115171aSMark Brown  */
3018f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
30199115171aSMark Brown {
3020f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3021f0fba2adSLiam Girdwood 
3022f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3023f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3024f0fba2adSLiam Girdwood 			goto found;
3025f0fba2adSLiam Girdwood 	}
3026f0fba2adSLiam Girdwood 	return;
3027f0fba2adSLiam Girdwood 
3028f0fba2adSLiam Girdwood found:
30299115171aSMark Brown 	mutex_lock(&client_mutex);
30309115171aSMark Brown 	list_del(&dai->list);
30319115171aSMark Brown 	mutex_unlock(&client_mutex);
30329115171aSMark Brown 
30339115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3034f0fba2adSLiam Girdwood 	kfree(dai->name);
3035f0fba2adSLiam Girdwood 	kfree(dai);
30369115171aSMark Brown }
30379115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
30389115171aSMark Brown 
30399115171aSMark Brown /**
30409115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
30419115171aSMark Brown  *
3042ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3043ac11a2b3SMark Brown  * @count: Number of DAIs
30449115171aSMark Brown  */
3045f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3046f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
30479115171aSMark Brown {
3048f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3049f0fba2adSLiam Girdwood 	int i, ret = 0;
3050f0fba2adSLiam Girdwood 
3051720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
30529115171aSMark Brown 
30539115171aSMark Brown 	for (i = 0; i < count; i++) {
3054f0fba2adSLiam Girdwood 
3055f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3056c46e0079SAxel Lin 		if (dai == NULL) {
3057c46e0079SAxel Lin 			ret = -ENOMEM;
3058c46e0079SAxel Lin 			goto err;
3059c46e0079SAxel Lin 		}
3060f0fba2adSLiam Girdwood 
3061f0fba2adSLiam Girdwood 		/* create DAI component name */
3062f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3063f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3064f0fba2adSLiam Girdwood 			kfree(dai);
3065f0fba2adSLiam Girdwood 			ret = -EINVAL;
30669115171aSMark Brown 			goto err;
30679115171aSMark Brown 		}
30689115171aSMark Brown 
3069f0fba2adSLiam Girdwood 		dai->dev = dev;
3070f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
30710f9141c9SMark Brown 		if (dai->driver->id)
30720f9141c9SMark Brown 			dai->id = dai->driver->id;
30730f9141c9SMark Brown 		else
30740f9141c9SMark Brown 			dai->id = i;
3075f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3076f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3077f0fba2adSLiam Girdwood 
3078f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3079f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3080f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3081f0fba2adSLiam Girdwood 
3082f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3083f0fba2adSLiam Girdwood 	}
3084f0fba2adSLiam Girdwood 
30851dcb4f38SAxel Lin 	mutex_lock(&client_mutex);
3086f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
30871dcb4f38SAxel Lin 	mutex_unlock(&client_mutex);
30889115171aSMark Brown 	return 0;
30899115171aSMark Brown 
30909115171aSMark Brown err:
30919115171aSMark Brown 	for (i--; i >= 0; i--)
3092f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
30939115171aSMark Brown 
30949115171aSMark Brown 	return ret;
30959115171aSMark Brown }
30969115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
30979115171aSMark Brown 
30989115171aSMark Brown /**
30999115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
31009115171aSMark Brown  *
3101ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3102ac11a2b3SMark Brown  * @count: Number of DAIs
31039115171aSMark Brown  */
3104f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
31059115171aSMark Brown {
31069115171aSMark Brown 	int i;
31079115171aSMark Brown 
31089115171aSMark Brown 	for (i = 0; i < count; i++)
3109f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
31109115171aSMark Brown }
31119115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
31129115171aSMark Brown 
311312a48a8cSMark Brown /**
311412a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
311512a48a8cSMark Brown  *
3116ac11a2b3SMark Brown  * @platform: platform to register
311712a48a8cSMark Brown  */
3118f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3119f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
312012a48a8cSMark Brown {
3121f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
312212a48a8cSMark Brown 
3123f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3124f0fba2adSLiam Girdwood 
3125f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3126f0fba2adSLiam Girdwood 	if (platform == NULL)
3127f0fba2adSLiam Girdwood 		return -ENOMEM;
3128f0fba2adSLiam Girdwood 
3129f0fba2adSLiam Girdwood 	/* create platform component name */
3130f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3131f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3132f0fba2adSLiam Girdwood 		kfree(platform);
3133f0fba2adSLiam Girdwood 		return -ENOMEM;
3134f0fba2adSLiam Girdwood 	}
3135f0fba2adSLiam Girdwood 
3136f0fba2adSLiam Girdwood 	platform->dev = dev;
3137f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3138b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3139b7950641SLiam Girdwood 	platform->dapm.platform = platform;
314012a48a8cSMark Brown 
314112a48a8cSMark Brown 	mutex_lock(&client_mutex);
314212a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3143435c5e25SMark Brown 	snd_soc_instantiate_cards();
314412a48a8cSMark Brown 	mutex_unlock(&client_mutex);
314512a48a8cSMark Brown 
314612a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
314712a48a8cSMark Brown 
314812a48a8cSMark Brown 	return 0;
314912a48a8cSMark Brown }
315012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
315112a48a8cSMark Brown 
315212a48a8cSMark Brown /**
315312a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
315412a48a8cSMark Brown  *
3155ac11a2b3SMark Brown  * @platform: platform to unregister
315612a48a8cSMark Brown  */
3157f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
315812a48a8cSMark Brown {
3159f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3160f0fba2adSLiam Girdwood 
3161f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3162f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3163f0fba2adSLiam Girdwood 			goto found;
3164f0fba2adSLiam Girdwood 	}
3165f0fba2adSLiam Girdwood 	return;
3166f0fba2adSLiam Girdwood 
3167f0fba2adSLiam Girdwood found:
316812a48a8cSMark Brown 	mutex_lock(&client_mutex);
316912a48a8cSMark Brown 	list_del(&platform->list);
317012a48a8cSMark Brown 	mutex_unlock(&client_mutex);
317112a48a8cSMark Brown 
317212a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3173f0fba2adSLiam Girdwood 	kfree(platform->name);
3174f0fba2adSLiam Girdwood 	kfree(platform);
317512a48a8cSMark Brown }
317612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
317712a48a8cSMark Brown 
3178151ab22cSMark Brown static u64 codec_format_map[] = {
3179151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3180151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3181151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3182151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3183151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3184151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3185151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3186151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3187151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3188151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3189151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3190151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3191151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3192151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3193151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3194151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3195151ab22cSMark Brown };
3196151ab22cSMark Brown 
3197151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3198151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3199151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3200151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3201151ab22cSMark Brown  */
3202151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3203151ab22cSMark Brown {
3204151ab22cSMark Brown 	int i;
3205151ab22cSMark Brown 
3206151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3207151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3208151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3209151ab22cSMark Brown }
3210151ab22cSMark Brown 
32110d0cf00aSMark Brown /**
32120d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
32130d0cf00aSMark Brown  *
3214ac11a2b3SMark Brown  * @codec: codec to register
32150d0cf00aSMark Brown  */
3216f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3217001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3218001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3219001ae4c0SMark Brown 			   int num_dai)
32200d0cf00aSMark Brown {
32213335ddcaSDimitris Papastamos 	size_t reg_size;
3222f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3223f0fba2adSLiam Girdwood 	int ret, i;
3224151ab22cSMark Brown 
3225f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
32260d0cf00aSMark Brown 
3227f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3228f0fba2adSLiam Girdwood 	if (codec == NULL)
3229f0fba2adSLiam Girdwood 		return -ENOMEM;
32300d0cf00aSMark Brown 
3231f0fba2adSLiam Girdwood 	/* create CODEC component name */
3232f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3233f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3234f0fba2adSLiam Girdwood 		kfree(codec);
3235f0fba2adSLiam Girdwood 		return -ENOMEM;
3236151ab22cSMark Brown 	}
3237151ab22cSMark Brown 
323823bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
323923bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
324023bbce34SDimitris Papastamos 	else
324123bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
324223bbce34SDimitris Papastamos 
3243c3acec26SMark Brown 	codec->write = codec_drv->write;
3244c3acec26SMark Brown 	codec->read = codec_drv->read;
32451500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
32461500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
32478020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
3248ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3249ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3250ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3251474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
3252f0fba2adSLiam Girdwood 	codec->dev = dev;
3253f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3254f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3255f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3256f0fba2adSLiam Girdwood 
32577a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
32587a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
32593335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3260aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
32613335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
32623335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
32633335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
32643335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
32653335ddcaSDimitris Papastamos 		 * the cache.
32663335ddcaSDimitris Papastamos 		 */
32672aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
32683335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
32693335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
32703335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
32713335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3272fdf0f54dSDimitris Papastamos 				goto fail;
32737a30a3dbSDimitris Papastamos 			}
32747a30a3dbSDimitris Papastamos 		}
32752aa86323SDimitris Papastamos 	}
32767a30a3dbSDimitris Papastamos 
32771500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
32781500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
32791500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
32801500b7b5SDimitris Papastamos 		if (!codec->readable_register)
32811500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
32828020454cSDimitris Papastamos 		if (!codec->writable_register)
32838020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
32841500b7b5SDimitris Papastamos 	}
32851500b7b5SDimitris Papastamos 
3286f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3287f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3288f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3289f0fba2adSLiam Girdwood 	}
3290f0fba2adSLiam Girdwood 
329126b01ccdSMark Brown 	/* register any DAIs */
329226b01ccdSMark Brown 	if (num_dai) {
3293f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3294f0fba2adSLiam Girdwood 		if (ret < 0)
3295fdf0f54dSDimitris Papastamos 			goto fail;
329626b01ccdSMark Brown 	}
3297f0fba2adSLiam Girdwood 
32980d0cf00aSMark Brown 	mutex_lock(&client_mutex);
32990d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
33000d0cf00aSMark Brown 	snd_soc_instantiate_cards();
33010d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
33020d0cf00aSMark Brown 
33030d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
33040d0cf00aSMark Brown 	return 0;
3305f0fba2adSLiam Girdwood 
3306fdf0f54dSDimitris Papastamos fail:
33073335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
33083335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3309f0fba2adSLiam Girdwood 	kfree(codec->name);
3310f0fba2adSLiam Girdwood 	kfree(codec);
3311f0fba2adSLiam Girdwood 	return ret;
33120d0cf00aSMark Brown }
33130d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
33140d0cf00aSMark Brown 
33150d0cf00aSMark Brown /**
33160d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
33170d0cf00aSMark Brown  *
3318ac11a2b3SMark Brown  * @codec: codec to unregister
33190d0cf00aSMark Brown  */
3320f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
33210d0cf00aSMark Brown {
3322f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3323f0fba2adSLiam Girdwood 	int i;
3324f0fba2adSLiam Girdwood 
3325f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3326f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3327f0fba2adSLiam Girdwood 			goto found;
3328f0fba2adSLiam Girdwood 	}
3329f0fba2adSLiam Girdwood 	return;
3330f0fba2adSLiam Girdwood 
3331f0fba2adSLiam Girdwood found:
333226b01ccdSMark Brown 	if (codec->num_dai)
3333f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3334f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3335f0fba2adSLiam Girdwood 
33360d0cf00aSMark Brown 	mutex_lock(&client_mutex);
33370d0cf00aSMark Brown 	list_del(&codec->list);
33380d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
33390d0cf00aSMark Brown 
33400d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3341f0fba2adSLiam Girdwood 
33427a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
33433335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
33441aafcd4dSDimitris Papastamos 	kfree(codec->name);
3345f0fba2adSLiam Girdwood 	kfree(codec);
33460d0cf00aSMark Brown }
33470d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
33480d0cf00aSMark Brown 
3349c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3350db2a4165SFrank Mandarino {
3351384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
33528a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
33538a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3354384c89e2SMark Brown 		printk(KERN_WARNING
3355384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
33568a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3357384c89e2SMark Brown 	}
3358c3c5a19aSMark Brown 
33598a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3360c3c5a19aSMark Brown 				 &codec_list_fops))
3361c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3362c3c5a19aSMark Brown 
33638a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3364f3208780SMark Brown 				 &dai_list_fops))
3365f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
336619c7ac27SMark Brown 
33678a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
336819c7ac27SMark Brown 				 &platform_list_fops))
336919c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3370384c89e2SMark Brown #endif
3371384c89e2SMark Brown 
3372fb257897SMark Brown 	snd_soc_util_init();
3373fb257897SMark Brown 
3374db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3375db2a4165SFrank Mandarino }
33764abe8e16SMark Brown module_init(snd_soc_init);
3377db2a4165SFrank Mandarino 
33787d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3379db2a4165SFrank Mandarino {
3380fb257897SMark Brown 	snd_soc_util_exit();
3381fb257897SMark Brown 
3382384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
33838a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3384384c89e2SMark Brown #endif
3385db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3386db2a4165SFrank Mandarino }
3387db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3388db2a4165SFrank Mandarino 
3389db2a4165SFrank Mandarino /* Module information */
3390d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3391db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3392db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
33938b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3394