xref: /openbmc/linux/sound/soc/soc-core.c (revision 78bf3c9ab69a957dfa58e521ce7fca056c86b980)
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 DEFINE_MUTEX(pcm_mutex);
48db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49db2a4165SFrank Mandarino 
50384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
518a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
528a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53384c89e2SMark Brown #endif
54384c89e2SMark Brown 
55c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
56c5af3a2eSMark Brown static LIST_HEAD(card_list);
579115171aSMark Brown static LIST_HEAD(dai_list);
5812a48a8cSMark Brown static LIST_HEAD(platform_list);
590d0cf00aSMark Brown static LIST_HEAD(codec_list);
60c5af3a2eSMark Brown 
61f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
62c5af3a2eSMark Brown 
63db2a4165SFrank Mandarino /*
64db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
65db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
66db2a4165SFrank Mandarino  * between two audio tracks.
67db2a4165SFrank Mandarino  */
68db2a4165SFrank Mandarino static int pmdown_time = 5000;
69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71db2a4165SFrank Mandarino 
722bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
732bc9a81eSDimitris Papastamos  * a particular given value */
742bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
752bc9a81eSDimitris Papastamos {
762bc9a81eSDimitris Papastamos 	int c = 0;
772bc9a81eSDimitris Papastamos 	int i;
782bc9a81eSDimitris Papastamos 
792bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
802bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
812bc9a81eSDimitris Papastamos 			break;
822bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
832bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
842bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
852bc9a81eSDimitris Papastamos 	else
862bc9a81eSDimitris Papastamos 		c /= 8;
872bc9a81eSDimitris Papastamos 	return c;
882bc9a81eSDimitris Papastamos }
892bc9a81eSDimitris Papastamos 
9013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
9113fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9313fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
942624d5faSMark Brown {
9500b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9600b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9713fd179fSDimitris Papastamos 	int ret;
9813fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
9913fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
10013fd179fSDimitris Papastamos 
10113fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
10213fd179fSDimitris Papastamos 	 * try to abuse this function */
10313fd179fSDimitris Papastamos 	WARN_ON(len > 63);
10413fd179fSDimitris Papastamos 
10513fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10613fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10713fd179fSDimitris Papastamos 		return -EINVAL;
10813fd179fSDimitris Papastamos 
10913fd179fSDimitris Papastamos 	ret = snd_soc_read(codec , reg);
11013fd179fSDimitris Papastamos 	if (ret < 0) {
11113fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
11213fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
11313fd179fSDimitris Papastamos 	} else {
11413fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
11513fd179fSDimitris Papastamos 	}
11613fd179fSDimitris Papastamos 
11713fd179fSDimitris Papastamos 	/* prepare the buffer */
11813fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
11913fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
12013fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
12113fd179fSDimitris Papastamos 
12213fd179fSDimitris Papastamos 	return 0;
12313fd179fSDimitris Papastamos }
12413fd179fSDimitris Papastamos 
12513fd179fSDimitris Papastamos /* codec register dump */
12613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
12713fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
12813fd179fSDimitris Papastamos {
12913fd179fSDimitris Papastamos 	int i, step = 1;
1302bc9a81eSDimitris Papastamos 	int wordsize, regsize;
13113fd179fSDimitris Papastamos 	int len;
13213fd179fSDimitris Papastamos 	size_t total = 0;
13313fd179fSDimitris Papastamos 	loff_t p = 0;
1342bc9a81eSDimitris Papastamos 
13500b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
13600b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1372624d5faSMark Brown 
13813fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13913fd179fSDimitris Papastamos 
140f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1412624d5faSMark Brown 		return 0;
1422624d5faSMark Brown 
143f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
144f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1452624d5faSMark Brown 
146f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
1471500b7b5SDimitris Papastamos 		if (codec->readable_register && !codec->readable_register(codec, i))
1482624d5faSMark Brown 			continue;
149f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
150f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1512624d5faSMark Brown 							 PAGE_SIZE - count, i);
1525164d74dSMark Brown 		} else {
15313fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
15413fd179fSDimitris Papastamos 			 * entries for the default case */
15513fd179fSDimitris Papastamos 			if (p >= pos) {
15613fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1572624d5faSMark Brown 					break;
15813fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
15913fd179fSDimitris Papastamos 				total += len;
16013fd179fSDimitris Papastamos 			}
16113fd179fSDimitris Papastamos 			p += len;
16213fd179fSDimitris Papastamos 		}
1632624d5faSMark Brown 	}
1642624d5faSMark Brown 
16513fd179fSDimitris Papastamos 	total = min(total, count - 1);
1662624d5faSMark Brown 
16713fd179fSDimitris Papastamos 	return total;
1682624d5faSMark Brown }
16913fd179fSDimitris Papastamos 
1702624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1712624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1722624d5faSMark Brown {
173f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
174f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
175f0fba2adSLiam Girdwood 
17613fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1772624d5faSMark Brown }
1782624d5faSMark Brown 
1792624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1802624d5faSMark Brown 
181dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
182dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
183dbe21408SMark Brown {
184f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
185f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
186dbe21408SMark Brown 
187f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
188dbe21408SMark Brown }
189dbe21408SMark Brown 
190dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
191dbe21408SMark Brown 			       struct device_attribute *attr,
192dbe21408SMark Brown 			       const char *buf, size_t count)
193dbe21408SMark Brown {
194f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
195f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
196c593b520SMark Brown 	int ret;
197dbe21408SMark Brown 
198c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
199c593b520SMark Brown 	if (ret)
200c593b520SMark Brown 		return ret;
201dbe21408SMark Brown 
202dbe21408SMark Brown 	return count;
203dbe21408SMark Brown }
204dbe21408SMark Brown 
205dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206dbe21408SMark Brown 
2072624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2082624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
2092624d5faSMark Brown {
2102624d5faSMark Brown 	file->private_data = inode->i_private;
2112624d5faSMark Brown 	return 0;
2122624d5faSMark Brown }
2132624d5faSMark Brown 
2142624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2152624d5faSMark Brown 				   size_t count, loff_t *ppos)
2162624d5faSMark Brown {
2172624d5faSMark Brown 	ssize_t ret;
2182624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
21913fd179fSDimitris Papastamos 	char *buf;
22013fd179fSDimitris Papastamos 
22113fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22213fd179fSDimitris Papastamos 		return -EINVAL;
22313fd179fSDimitris Papastamos 
22413fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2252624d5faSMark Brown 	if (!buf)
2262624d5faSMark Brown 		return -ENOMEM;
22713fd179fSDimitris Papastamos 
22813fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
22913fd179fSDimitris Papastamos 	if (ret >= 0) {
23013fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23113fd179fSDimitris Papastamos 			kfree(buf);
23213fd179fSDimitris Papastamos 			return -EFAULT;
23313fd179fSDimitris Papastamos 		}
23413fd179fSDimitris Papastamos 		*ppos += ret;
23513fd179fSDimitris Papastamos 	}
23613fd179fSDimitris Papastamos 
2372624d5faSMark Brown 	kfree(buf);
2382624d5faSMark Brown 	return ret;
2392624d5faSMark Brown }
2402624d5faSMark Brown 
2412624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2422624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2432624d5faSMark Brown {
2442624d5faSMark Brown 	char buf[32];
24534e268d8SStephen Boyd 	size_t buf_size;
2462624d5faSMark Brown 	char *start = buf;
2472624d5faSMark Brown 	unsigned long reg, value;
2482624d5faSMark Brown 	int step = 1;
2492624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2502624d5faSMark Brown 
2512624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2522624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2532624d5faSMark Brown 		return -EFAULT;
2542624d5faSMark Brown 	buf[buf_size] = 0;
2552624d5faSMark Brown 
256f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
257f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
2582624d5faSMark Brown 
2592624d5faSMark Brown 	while (*start == ' ')
2602624d5faSMark Brown 		start++;
2612624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2622624d5faSMark Brown 	while (*start == ' ')
2632624d5faSMark Brown 		start++;
2642624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2652624d5faSMark Brown 		return -EINVAL;
2660d51a9cbSMark Brown 
2670d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2680d51a9cbSMark Brown 	add_taint(TAINT_USER);
2690d51a9cbSMark Brown 
270e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2712624d5faSMark Brown 	return buf_size;
2722624d5faSMark Brown }
2732624d5faSMark Brown 
2742624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2752624d5faSMark Brown 	.open = codec_reg_open_file,
2762624d5faSMark Brown 	.read = codec_reg_read_file,
2772624d5faSMark Brown 	.write = codec_reg_write_file,
2786038f373SArnd Bergmann 	.llseek = default_llseek,
2792624d5faSMark Brown };
2802624d5faSMark Brown 
2812624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2822624d5faSMark Brown {
283d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
284d6ce4cf3SJarkko Nikula 
2856ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
286d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2872624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2882624d5faSMark Brown 		printk(KERN_WARNING
2892624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2902624d5faSMark Brown 		return;
2912624d5faSMark Brown 	}
2922624d5faSMark Brown 
293aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
294aaee8ef1SMark Brown 			    &codec->cache_sync);
295aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
296aaee8ef1SMark Brown 			    &codec->cache_only);
297aaee8ef1SMark Brown 
2982624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2992624d5faSMark Brown 						 codec->debugfs_codec_root,
3002624d5faSMark Brown 						 codec, &codec_reg_fops);
3012624d5faSMark Brown 	if (!codec->debugfs_reg)
3022624d5faSMark Brown 		printk(KERN_WARNING
3032624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
3042624d5faSMark Brown 
3058eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
3062624d5faSMark Brown }
3072624d5faSMark Brown 
3082624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3092624d5faSMark Brown {
3102624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3112624d5faSMark Brown }
3122624d5faSMark Brown 
313c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
314c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
315c3c5a19aSMark Brown {
316c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3172b194f9dSMark Brown 	ssize_t len, ret = 0;
318c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
319c3c5a19aSMark Brown 
320c3c5a19aSMark Brown 	if (!buf)
321c3c5a19aSMark Brown 		return -ENOMEM;
322c3c5a19aSMark Brown 
3232b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3242b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
325c3c5a19aSMark Brown 			       codec->name);
3262b194f9dSMark Brown 		if (len >= 0)
3272b194f9dSMark Brown 			ret += len;
3282b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3292b194f9dSMark Brown 			ret = PAGE_SIZE;
3302b194f9dSMark Brown 			break;
3312b194f9dSMark Brown 		}
3322b194f9dSMark Brown 	}
333c3c5a19aSMark Brown 
334c3c5a19aSMark Brown 	if (ret >= 0)
335c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
336c3c5a19aSMark Brown 
337c3c5a19aSMark Brown 	kfree(buf);
338c3c5a19aSMark Brown 
339c3c5a19aSMark Brown 	return ret;
340c3c5a19aSMark Brown }
341c3c5a19aSMark Brown 
342c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
343c3c5a19aSMark Brown 	.read = codec_list_read_file,
344c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
345c3c5a19aSMark Brown };
346c3c5a19aSMark Brown 
347f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
348f3208780SMark Brown 				  size_t count, loff_t *ppos)
349f3208780SMark Brown {
350f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3512b194f9dSMark Brown 	ssize_t len, ret = 0;
352f3208780SMark Brown 	struct snd_soc_dai *dai;
353f3208780SMark Brown 
354f3208780SMark Brown 	if (!buf)
355f3208780SMark Brown 		return -ENOMEM;
356f3208780SMark Brown 
3572b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3582b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3592b194f9dSMark Brown 		if (len >= 0)
3602b194f9dSMark Brown 			ret += len;
3612b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3622b194f9dSMark Brown 			ret = PAGE_SIZE;
3632b194f9dSMark Brown 			break;
3642b194f9dSMark Brown 		}
3652b194f9dSMark Brown 	}
366f3208780SMark Brown 
367f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
368f3208780SMark Brown 
369f3208780SMark Brown 	kfree(buf);
370f3208780SMark Brown 
371f3208780SMark Brown 	return ret;
372f3208780SMark Brown }
373f3208780SMark Brown 
374f3208780SMark Brown static const struct file_operations dai_list_fops = {
375f3208780SMark Brown 	.read = dai_list_read_file,
376f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
377f3208780SMark Brown };
378f3208780SMark Brown 
37919c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
38019c7ac27SMark Brown 				       char __user *user_buf,
38119c7ac27SMark Brown 				       size_t count, loff_t *ppos)
38219c7ac27SMark Brown {
38319c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3842b194f9dSMark Brown 	ssize_t len, ret = 0;
38519c7ac27SMark Brown 	struct snd_soc_platform *platform;
38619c7ac27SMark Brown 
38719c7ac27SMark Brown 	if (!buf)
38819c7ac27SMark Brown 		return -ENOMEM;
38919c7ac27SMark Brown 
3902b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3912b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
39219c7ac27SMark Brown 			       platform->name);
3932b194f9dSMark Brown 		if (len >= 0)
3942b194f9dSMark Brown 			ret += len;
3952b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3962b194f9dSMark Brown 			ret = PAGE_SIZE;
3972b194f9dSMark Brown 			break;
3982b194f9dSMark Brown 		}
3992b194f9dSMark Brown 	}
40019c7ac27SMark Brown 
40119c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
40219c7ac27SMark Brown 
40319c7ac27SMark Brown 	kfree(buf);
40419c7ac27SMark Brown 
40519c7ac27SMark Brown 	return ret;
40619c7ac27SMark Brown }
40719c7ac27SMark Brown 
40819c7ac27SMark Brown static const struct file_operations platform_list_fops = {
40919c7ac27SMark Brown 	.read = platform_list_read_file,
41019c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
41119c7ac27SMark Brown };
41219c7ac27SMark Brown 
413a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
414a6052154SJarkko Nikula {
415a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4168a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4173a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
418a6052154SJarkko Nikula 		dev_warn(card->dev,
419a6052154SJarkko Nikula 			 "ASoC: Failed to create codec debugfs directory\n");
4203a45b867SJarkko Nikula 		return;
4213a45b867SJarkko Nikula 	}
4223a45b867SJarkko Nikula 
4233a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4243a45b867SJarkko Nikula 						    card->debugfs_card_root,
4253a45b867SJarkko Nikula 						    &card->pop_time);
4263a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4273a45b867SJarkko Nikula 		dev_warn(card->dev,
4283a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
429a6052154SJarkko Nikula }
430a6052154SJarkko Nikula 
431a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
432a6052154SJarkko Nikula {
433a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
434a6052154SJarkko Nikula }
435a6052154SJarkko Nikula 
4362624d5faSMark Brown #else
4372624d5faSMark Brown 
4382624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4392624d5faSMark Brown {
4402624d5faSMark Brown }
4412624d5faSMark Brown 
4422624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4432624d5faSMark Brown {
4442624d5faSMark Brown }
445b95fccbcSAxel Lin 
446b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
447b95fccbcSAxel Lin {
448b95fccbcSAxel Lin }
449b95fccbcSAxel Lin 
450b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
451b95fccbcSAxel Lin {
452b95fccbcSAxel Lin }
4532624d5faSMark Brown #endif
4542624d5faSMark Brown 
455db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
456db2a4165SFrank Mandarino /* unregister ac97 codec */
457db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
458db2a4165SFrank Mandarino {
459db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
460db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
461db2a4165SFrank Mandarino 	return 0;
462db2a4165SFrank Mandarino }
463db2a4165SFrank Mandarino 
464db2a4165SFrank Mandarino /* stop no dev release warning */
465db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
466db2a4165SFrank Mandarino 
467db2a4165SFrank Mandarino /* register ac97 codec to bus */
468db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
469db2a4165SFrank Mandarino {
470db2a4165SFrank Mandarino 	int err;
471db2a4165SFrank Mandarino 
472db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4734ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
474db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
475db2a4165SFrank Mandarino 
476bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
477f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
478db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
479db2a4165SFrank Mandarino 	if (err < 0) {
480db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
481db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
482db2a4165SFrank Mandarino 		return err;
483db2a4165SFrank Mandarino 	}
484db2a4165SFrank Mandarino 	return 0;
485db2a4165SFrank Mandarino }
486db2a4165SFrank Mandarino #endif
487db2a4165SFrank Mandarino 
48806f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
48906f409d7SMark Brown {
49006f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
491f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
492f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
49306f409d7SMark Brown 	int ret;
49406f409d7SMark Brown 
4954f333b20SMark Brown 	if (!codec_dai->driver->symmetric_rates &&
4964f333b20SMark Brown 	    !cpu_dai->driver->symmetric_rates &&
4974f333b20SMark Brown 	    !rtd->dai_link->symmetric_rates)
4984f333b20SMark Brown 		return 0;
4994f333b20SMark Brown 
500c4ef8786SMark Brown 	/* This can happen if multiple streams are starting simultaneously -
501c4ef8786SMark Brown 	 * the second can need to get its constraints before the first has
502c4ef8786SMark Brown 	 * picked a rate.  Complain and allow the application to carry on.
503c4ef8786SMark Brown 	 */
504c4ef8786SMark Brown 	if (!rtd->rate) {
505c4ef8786SMark Brown 		dev_warn(&rtd->dev,
506c4ef8786SMark Brown 			 "Not enforcing symmetric_rates due to race\n");
507c4ef8786SMark Brown 		return 0;
508c4ef8786SMark Brown 	}
509c4ef8786SMark Brown 
5104f333b20SMark Brown 	dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n", rtd->rate);
51106f409d7SMark Brown 
51206f409d7SMark Brown 	ret = snd_pcm_hw_constraint_minmax(substream->runtime,
51306f409d7SMark Brown 					   SNDRV_PCM_HW_PARAM_RATE,
5144f333b20SMark Brown 					   rtd->rate, rtd->rate);
51506f409d7SMark Brown 	if (ret < 0) {
516f0fba2adSLiam Girdwood 		dev_err(&rtd->dev,
51706f409d7SMark Brown 			"Unable to apply rate symmetry constraint: %d\n", ret);
51806f409d7SMark Brown 		return ret;
51906f409d7SMark Brown 	}
52006f409d7SMark Brown 
52106f409d7SMark Brown 	return 0;
52206f409d7SMark Brown }
52306f409d7SMark Brown 
524db2a4165SFrank Mandarino /*
525db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
526db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
527db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
528db2a4165SFrank Mandarino  */
529db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
530db2a4165SFrank Mandarino {
531db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
532db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
533f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
534f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
535f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
536f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
537f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
538db2a4165SFrank Mandarino 	int ret = 0;
539db2a4165SFrank Mandarino 
540db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
541db2a4165SFrank Mandarino 
542db2a4165SFrank Mandarino 	/* startup the audio subsystem */
543f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->startup) {
544f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
545db2a4165SFrank Mandarino 		if (ret < 0) {
546db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
547cb666e5bSLiam Girdwood 				cpu_dai->name);
548db2a4165SFrank Mandarino 			goto out;
549db2a4165SFrank Mandarino 		}
550db2a4165SFrank Mandarino 	}
551db2a4165SFrank Mandarino 
5528842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->open) {
553f0fba2adSLiam Girdwood 		ret = platform->driver->ops->open(substream);
554db2a4165SFrank Mandarino 		if (ret < 0) {
555db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
556db2a4165SFrank Mandarino 			goto platform_err;
557db2a4165SFrank Mandarino 		}
558db2a4165SFrank Mandarino 	}
559db2a4165SFrank Mandarino 
560f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->startup) {
561f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->startup(substream, codec_dai);
562cb666e5bSLiam Girdwood 		if (ret < 0) {
563cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
564cb666e5bSLiam Girdwood 				codec_dai->name);
565cb666e5bSLiam Girdwood 			goto codec_dai_err;
566cb666e5bSLiam Girdwood 		}
567cb666e5bSLiam Girdwood 	}
568cb666e5bSLiam Girdwood 
569f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
570f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
571db2a4165SFrank Mandarino 		if (ret < 0) {
572f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
573db2a4165SFrank Mandarino 			goto machine_err;
574db2a4165SFrank Mandarino 		}
575db2a4165SFrank Mandarino 	}
576db2a4165SFrank Mandarino 
577a00f90f9SMark Brown 	/* Check that the codec and cpu DAIs are compatible */
578db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
579db2a4165SFrank Mandarino 		runtime->hw.rate_min =
580f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.rate_min,
581f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_min);
582db2a4165SFrank Mandarino 		runtime->hw.rate_max =
583f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.rate_max,
584f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_max);
585db2a4165SFrank Mandarino 		runtime->hw.channels_min =
586f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.channels_min,
587f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_min);
588db2a4165SFrank Mandarino 		runtime->hw.channels_max =
589f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.channels_max,
590f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_max);
591cb666e5bSLiam Girdwood 		runtime->hw.formats =
592f0fba2adSLiam Girdwood 			codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
593cb666e5bSLiam Girdwood 		runtime->hw.rates =
594f0fba2adSLiam Girdwood 			codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
595f0fba2adSLiam Girdwood 		if (codec_dai_drv->playback.rates
596d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
597f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->playback.rates;
598f0fba2adSLiam Girdwood 		if (cpu_dai_drv->playback.rates
599d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
600f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->playback.rates;
601db2a4165SFrank Mandarino 	} else {
602db2a4165SFrank Mandarino 		runtime->hw.rate_min =
603f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.rate_min,
604f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_min);
605db2a4165SFrank Mandarino 		runtime->hw.rate_max =
606f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.rate_max,
607f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_max);
608db2a4165SFrank Mandarino 		runtime->hw.channels_min =
609f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.channels_min,
610f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_min);
611db2a4165SFrank Mandarino 		runtime->hw.channels_max =
612f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.channels_max,
613f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_max);
614cb666e5bSLiam Girdwood 		runtime->hw.formats =
615f0fba2adSLiam Girdwood 			codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
616cb666e5bSLiam Girdwood 		runtime->hw.rates =
617f0fba2adSLiam Girdwood 			codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
618f0fba2adSLiam Girdwood 		if (codec_dai_drv->capture.rates
619d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
620f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->capture.rates;
621f0fba2adSLiam Girdwood 		if (cpu_dai_drv->capture.rates
622d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
623f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->capture.rates;
624db2a4165SFrank Mandarino 	}
625db2a4165SFrank Mandarino 
626c51def65SLu Guanqun 	ret = -EINVAL;
627db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
628db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
629db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
630cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
631bb1c0478SJassi Brar 		goto config_err;
632db2a4165SFrank Mandarino 	}
633db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
634db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
635cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
636bb1c0478SJassi Brar 		goto config_err;
637db2a4165SFrank Mandarino 	}
638b04cfcf7SLu Guanqun 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
639b04cfcf7SLu Guanqun 	    runtime->hw.channels_min > runtime->hw.channels_max) {
640db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
641cb666e5bSLiam Girdwood 				codec_dai->name, cpu_dai->name);
642bb1c0478SJassi Brar 		goto config_err;
643db2a4165SFrank Mandarino 	}
644db2a4165SFrank Mandarino 
64506f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
64606f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
64706f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
64806f409d7SMark Brown 		if (ret != 0)
649bb1c0478SJassi Brar 			goto config_err;
65006f409d7SMark Brown 	}
65106f409d7SMark Brown 
652f0fba2adSLiam Girdwood 	pr_debug("asoc: %s <-> %s info:\n",
653f0fba2adSLiam Girdwood 			codec_dai->name, cpu_dai->name);
654f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
655f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
656b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
657f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
658b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
659db2a4165SFrank Mandarino 
66014dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
661f0fba2adSLiam Girdwood 		cpu_dai->playback_active++;
662f0fba2adSLiam Girdwood 		codec_dai->playback_active++;
66314dc5734SJassi Brar 	} else {
664f0fba2adSLiam Girdwood 		cpu_dai->capture_active++;
665f0fba2adSLiam Girdwood 		codec_dai->capture_active++;
66614dc5734SJassi Brar 	}
66714dc5734SJassi Brar 	cpu_dai->active++;
66814dc5734SJassi Brar 	codec_dai->active++;
669f0fba2adSLiam Girdwood 	rtd->codec->active++;
670db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
671db2a4165SFrank Mandarino 	return 0;
672db2a4165SFrank Mandarino 
673bb1c0478SJassi Brar config_err:
674f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
675f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
676db2a4165SFrank Mandarino 
677bb1c0478SJassi Brar machine_err:
678f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
679f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
680bb1c0478SJassi Brar 
681cb666e5bSLiam Girdwood codec_dai_err:
6828842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->close)
683f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
684db2a4165SFrank Mandarino 
685db2a4165SFrank Mandarino platform_err:
686f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
687f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
688db2a4165SFrank Mandarino out:
689db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
690db2a4165SFrank Mandarino 	return ret;
691db2a4165SFrank Mandarino }
692db2a4165SFrank Mandarino 
693db2a4165SFrank Mandarino /*
6943a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
695db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
696db2a4165SFrank Mandarino  * due to DAPM power cycling.
697db2a4165SFrank Mandarino  */
6984484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
699db2a4165SFrank Mandarino {
700f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
701f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
702f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
703db2a4165SFrank Mandarino 
704db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
705db2a4165SFrank Mandarino 
706f24368c2SMark Brown 	pr_debug("pop wq checking: %s status: %s waiting: %s\n",
707f0fba2adSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
708f0fba2adSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
709db2a4165SFrank Mandarino 		 codec_dai->pop_wait ? "yes" : "no");
710db2a4165SFrank Mandarino 
711db2a4165SFrank Mandarino 	/* are we waiting on this codec DAI stream */
712db2a4165SFrank Mandarino 	if (codec_dai->pop_wait == 1) {
713db2a4165SFrank Mandarino 		codec_dai->pop_wait = 0;
714f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
715f0fba2adSLiam Girdwood 			codec_dai->driver->playback.stream_name,
716db2a4165SFrank Mandarino 			SND_SOC_DAPM_STREAM_STOP);
717db2a4165SFrank Mandarino 	}
718f0fba2adSLiam Girdwood 
719db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
720db2a4165SFrank Mandarino }
721db2a4165SFrank Mandarino 
722db2a4165SFrank Mandarino /*
723db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
724db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
725db2a4165SFrank Mandarino  * shutdown.
726db2a4165SFrank Mandarino  */
727db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
728db2a4165SFrank Mandarino {
729db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
730f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
731f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
732f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
733f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
734db2a4165SFrank Mandarino 
735db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
736db2a4165SFrank Mandarino 
73714dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
738f0fba2adSLiam Girdwood 		cpu_dai->playback_active--;
739f0fba2adSLiam Girdwood 		codec_dai->playback_active--;
74014dc5734SJassi Brar 	} else {
741f0fba2adSLiam Girdwood 		cpu_dai->capture_active--;
742f0fba2adSLiam Girdwood 		codec_dai->capture_active--;
743db2a4165SFrank Mandarino 	}
74414dc5734SJassi Brar 
74514dc5734SJassi Brar 	cpu_dai->active--;
74614dc5734SJassi Brar 	codec_dai->active--;
747db2a4165SFrank Mandarino 	codec->active--;
748db2a4165SFrank Mandarino 
7496010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
7506010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
7516010b2daSMark Brown 	 */
7526010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
7536010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
7546010b2daSMark Brown 
755f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
756f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
757db2a4165SFrank Mandarino 
758f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
759f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
760db2a4165SFrank Mandarino 
761f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
762f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
763db2a4165SFrank Mandarino 
7648842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->close)
765f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
766f0fba2adSLiam Girdwood 	cpu_dai->runtime = NULL;
767db2a4165SFrank Mandarino 
768db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
769db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
770cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
771f0fba2adSLiam Girdwood 		schedule_delayed_work(&rtd->delayed_work,
772f0fba2adSLiam Girdwood 			msecs_to_jiffies(rtd->pmdown_time));
773db2a4165SFrank Mandarino 	} else {
774db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
775f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
776f0fba2adSLiam Girdwood 			codec_dai->driver->capture.stream_name,
7770b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
778db2a4165SFrank Mandarino 	}
779db2a4165SFrank Mandarino 
780db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
781db2a4165SFrank Mandarino 	return 0;
782db2a4165SFrank Mandarino }
783db2a4165SFrank Mandarino 
784db2a4165SFrank Mandarino /*
785db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
786db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
787db2a4165SFrank Mandarino  * it can refer to the runtime info.
788db2a4165SFrank Mandarino  */
789db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
790db2a4165SFrank Mandarino {
791db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
792f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
793f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
794f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
795db2a4165SFrank Mandarino 	int ret = 0;
796db2a4165SFrank Mandarino 
797db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
798cb666e5bSLiam Girdwood 
799f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
800f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
801cb666e5bSLiam Girdwood 		if (ret < 0) {
802cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
803cb666e5bSLiam Girdwood 			goto out;
804cb666e5bSLiam Girdwood 		}
805cb666e5bSLiam Girdwood 	}
806cb666e5bSLiam Girdwood 
8078842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->prepare) {
808f0fba2adSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
809a71a468aSLiam Girdwood 		if (ret < 0) {
810a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
811db2a4165SFrank Mandarino 			goto out;
812db2a4165SFrank Mandarino 		}
813a71a468aSLiam Girdwood 	}
814db2a4165SFrank Mandarino 
815f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->prepare) {
816f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->prepare(substream, codec_dai);
817a71a468aSLiam Girdwood 		if (ret < 0) {
818a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
819db2a4165SFrank Mandarino 			goto out;
820db2a4165SFrank Mandarino 		}
821a71a468aSLiam Girdwood 	}
822db2a4165SFrank Mandarino 
823f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->prepare) {
824f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
825cb666e5bSLiam Girdwood 		if (ret < 0) {
826cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
827cb666e5bSLiam Girdwood 			goto out;
828cb666e5bSLiam Girdwood 		}
829cb666e5bSLiam Girdwood 	}
830db2a4165SFrank Mandarino 
831d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
832d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
833d45f6219SMark Brown 	    codec_dai->pop_wait) {
834cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
835f0fba2adSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
836db2a4165SFrank Mandarino 	}
837db2a4165SFrank Mandarino 
838db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
839f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
840f0fba2adSLiam Girdwood 					  codec_dai->driver->playback.stream_name,
841db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
842db2a4165SFrank Mandarino 	else
843f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
844f0fba2adSLiam Girdwood 					  codec_dai->driver->capture.stream_name,
845db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
8468c6529dbSLiam Girdwood 
8478c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
848db2a4165SFrank Mandarino 
849db2a4165SFrank Mandarino out:
850db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
851db2a4165SFrank Mandarino 	return ret;
852db2a4165SFrank Mandarino }
853db2a4165SFrank Mandarino 
854db2a4165SFrank Mandarino /*
855db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
856db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
857db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
858db2a4165SFrank Mandarino  */
859db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
860db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
861db2a4165SFrank Mandarino {
862db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
863f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
864f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
865f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
866db2a4165SFrank Mandarino 	int ret = 0;
867db2a4165SFrank Mandarino 
868db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
869db2a4165SFrank Mandarino 
870f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
871f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
872cb666e5bSLiam Girdwood 		if (ret < 0) {
873cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
874db2a4165SFrank Mandarino 			goto out;
875db2a4165SFrank Mandarino 		}
876cb666e5bSLiam Girdwood 	}
877db2a4165SFrank Mandarino 
878f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_params) {
879f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
880db2a4165SFrank Mandarino 		if (ret < 0) {
881db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
882cb666e5bSLiam Girdwood 				codec_dai->name);
883cb666e5bSLiam Girdwood 			goto codec_err;
884db2a4165SFrank Mandarino 		}
885db2a4165SFrank Mandarino 	}
886db2a4165SFrank Mandarino 
887f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_params) {
888f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
889db2a4165SFrank Mandarino 		if (ret < 0) {
8903ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
891cb666e5bSLiam Girdwood 				cpu_dai->name);
892db2a4165SFrank Mandarino 			goto interface_err;
893db2a4165SFrank Mandarino 		}
894db2a4165SFrank Mandarino 	}
895db2a4165SFrank Mandarino 
8968842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->hw_params) {
897f0fba2adSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
898db2a4165SFrank Mandarino 		if (ret < 0) {
8993ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
900db2a4165SFrank Mandarino 				platform->name);
901db2a4165SFrank Mandarino 			goto platform_err;
902db2a4165SFrank Mandarino 		}
903db2a4165SFrank Mandarino 	}
904db2a4165SFrank Mandarino 
905f0fba2adSLiam Girdwood 	rtd->rate = params_rate(params);
90606f409d7SMark Brown 
907db2a4165SFrank Mandarino out:
908db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
909db2a4165SFrank Mandarino 	return ret;
910db2a4165SFrank Mandarino 
911db2a4165SFrank Mandarino platform_err:
912f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
913f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
914db2a4165SFrank Mandarino 
915db2a4165SFrank Mandarino interface_err:
916f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
917f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
918cb666e5bSLiam Girdwood 
919cb666e5bSLiam Girdwood codec_err:
920f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
921f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
922db2a4165SFrank Mandarino 
923db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
924db2a4165SFrank Mandarino 	return ret;
925db2a4165SFrank Mandarino }
926db2a4165SFrank Mandarino 
927db2a4165SFrank Mandarino /*
928a00f90f9SMark Brown  * Frees resources allocated by hw_params, can be called multiple times
929db2a4165SFrank Mandarino  */
930db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
931db2a4165SFrank Mandarino {
932db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
933f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
934f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
935f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
936f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
937db2a4165SFrank Mandarino 
938db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
939db2a4165SFrank Mandarino 
940db2a4165SFrank Mandarino 	/* apply codec digital mute */
9418c6529dbSLiam Girdwood 	if (!codec->active)
9428c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
943db2a4165SFrank Mandarino 
944db2a4165SFrank Mandarino 	/* free any machine hw params */
945f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
946f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
947db2a4165SFrank Mandarino 
948db2a4165SFrank Mandarino 	/* free any DMA resources */
9498842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->hw_free)
950f0fba2adSLiam Girdwood 		platform->driver->ops->hw_free(substream);
951db2a4165SFrank Mandarino 
952a00f90f9SMark Brown 	/* now free hw params for the DAIs  */
953f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
954f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
955db2a4165SFrank Mandarino 
956f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
957f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
958db2a4165SFrank Mandarino 
959db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
960db2a4165SFrank Mandarino 	return 0;
961db2a4165SFrank Mandarino }
962db2a4165SFrank Mandarino 
963db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
964db2a4165SFrank Mandarino {
965db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
966f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
967f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
968f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
969db2a4165SFrank Mandarino 	int ret;
970db2a4165SFrank Mandarino 
971f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->trigger) {
972f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
973db2a4165SFrank Mandarino 		if (ret < 0)
974db2a4165SFrank Mandarino 			return ret;
975db2a4165SFrank Mandarino 	}
976db2a4165SFrank Mandarino 
9778842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->trigger) {
978f0fba2adSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
979db2a4165SFrank Mandarino 		if (ret < 0)
980db2a4165SFrank Mandarino 			return ret;
981db2a4165SFrank Mandarino 	}
982db2a4165SFrank Mandarino 
983f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->trigger) {
984f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
985db2a4165SFrank Mandarino 		if (ret < 0)
986db2a4165SFrank Mandarino 			return ret;
987db2a4165SFrank Mandarino 	}
988db2a4165SFrank Mandarino 	return 0;
989db2a4165SFrank Mandarino }
990db2a4165SFrank Mandarino 
991377b6f62SPeter Ujfalusi /*
992377b6f62SPeter Ujfalusi  * soc level wrapper for pointer callback
993258020d0SPeter Ujfalusi  * If cpu_dai, codec_dai, platform driver has the delay callback, than
994258020d0SPeter Ujfalusi  * the runtime->delay will be updated accordingly.
995377b6f62SPeter Ujfalusi  */
996377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
997377b6f62SPeter Ujfalusi {
998377b6f62SPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
999f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1000f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1001f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1002258020d0SPeter Ujfalusi 	struct snd_pcm_runtime *runtime = substream->runtime;
1003377b6f62SPeter Ujfalusi 	snd_pcm_uframes_t offset = 0;
1004258020d0SPeter Ujfalusi 	snd_pcm_sframes_t delay = 0;
1005377b6f62SPeter Ujfalusi 
10068842c72aSMark Brown 	if (platform->driver->ops && platform->driver->ops->pointer)
1007f0fba2adSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
1008377b6f62SPeter Ujfalusi 
1009f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->delay)
1010f0fba2adSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1011258020d0SPeter Ujfalusi 
1012f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->delay)
1013f0fba2adSLiam Girdwood 		delay += codec_dai->driver->ops->delay(substream, codec_dai);
1014258020d0SPeter Ujfalusi 
1015f0fba2adSLiam Girdwood 	if (platform->driver->delay)
1016f0fba2adSLiam Girdwood 		delay += platform->driver->delay(substream, codec_dai);
1017258020d0SPeter Ujfalusi 
1018258020d0SPeter Ujfalusi 	runtime->delay = delay;
1019258020d0SPeter Ujfalusi 
1020377b6f62SPeter Ujfalusi 	return offset;
1021377b6f62SPeter Ujfalusi }
1022377b6f62SPeter Ujfalusi 
1023db2a4165SFrank Mandarino /* ASoC PCM operations */
1024db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
1025db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
1026db2a4165SFrank Mandarino 	.close		= soc_codec_close,
1027db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
1028db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
1029db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
1030db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
1031377b6f62SPeter Ujfalusi 	.pointer	= soc_pcm_pointer,
1032db2a4165SFrank Mandarino };
1033db2a4165SFrank Mandarino 
10346f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
1035db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
10366f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
1037db2a4165SFrank Mandarino {
10386f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
10392eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1040db2a4165SFrank Mandarino 	int i;
1041db2a4165SFrank Mandarino 
1042e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
1043e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
1044e3509ff0SDaniel Mack 	 */
1045f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
1046e3509ff0SDaniel Mack 		return 0;
1047e3509ff0SDaniel Mack 
10486ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
10496ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
10506ed25978SAndy Green 	 */
1051f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
1052f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1053f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
10546ed25978SAndy Green 
10556ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
1056f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
10576ed25978SAndy Green 
1058a00f90f9SMark Brown 	/* mute any active DACs */
1059f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1060f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1061f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
10623efab7dcSMark Brown 
1063f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10643efab7dcSMark Brown 			continue;
10653efab7dcSMark Brown 
1066f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1067f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
1068db2a4165SFrank Mandarino 	}
1069db2a4165SFrank Mandarino 
10704ccab3e7SLiam Girdwood 	/* suspend all pcms */
1071f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1072f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10733efab7dcSMark Brown 			continue;
10743efab7dcSMark Brown 
1075f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
10763efab7dcSMark Brown 	}
10774ccab3e7SLiam Girdwood 
107887506549SMark Brown 	if (card->suspend_pre)
107970b2ac12SMark Brown 		card->suspend_pre(card);
1080db2a4165SFrank Mandarino 
1081f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1082f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1083f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
10843efab7dcSMark Brown 
1085f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10863efab7dcSMark Brown 			continue;
10873efab7dcSMark Brown 
1088f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1089f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1090f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
1091f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
1092f0fba2adSLiam Girdwood 			platform->suspended = 1;
1093f0fba2adSLiam Girdwood 		}
1094db2a4165SFrank Mandarino 	}
1095db2a4165SFrank Mandarino 
1096db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
1097f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
10985b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
1099ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1100f0fba2adSLiam Girdwood 	}
1101db2a4165SFrank Mandarino 
1102f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1103f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
11043efab7dcSMark Brown 
1105f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11063efab7dcSMark Brown 			continue;
11073efab7dcSMark Brown 
1108f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1109f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1110db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1111f0fba2adSLiam Girdwood 
1112f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1113f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1114db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1115db2a4165SFrank Mandarino 	}
1116db2a4165SFrank Mandarino 
1117f0fba2adSLiam Girdwood 	/* suspend all CODECs */
11182eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
11191547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
11201547aba9SMark Brown 		 * bias _ON and should not be suspended. */
1121f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
1122ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
11231547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
11241547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1125f0fba2adSLiam Girdwood 				codec->driver->suspend(codec, PMSG_SUSPEND);
1126f0fba2adSLiam Girdwood 				codec->suspended = 1;
11271547aba9SMark Brown 				break;
11281547aba9SMark Brown 			default:
1129f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
11301547aba9SMark Brown 				break;
11311547aba9SMark Brown 			}
11321547aba9SMark Brown 		}
1133f0fba2adSLiam Girdwood 	}
1134db2a4165SFrank Mandarino 
1135f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1136f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
11373efab7dcSMark Brown 
1138f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11393efab7dcSMark Brown 			continue;
11403efab7dcSMark Brown 
1141f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1142f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1143db2a4165SFrank Mandarino 	}
1144db2a4165SFrank Mandarino 
114587506549SMark Brown 	if (card->suspend_post)
114670b2ac12SMark Brown 		card->suspend_post(card);
1147db2a4165SFrank Mandarino 
1148db2a4165SFrank Mandarino 	return 0;
1149db2a4165SFrank Mandarino }
11506f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
1151db2a4165SFrank Mandarino 
11526ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
11536ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
11546ed25978SAndy Green  */
11556ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
1156db2a4165SFrank Mandarino {
1157f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
1158f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
11592eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1160db2a4165SFrank Mandarino 	int i;
1161db2a4165SFrank Mandarino 
11626ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
11636ed25978SAndy Green 	 * so userspace apps are blocked from touching us
11646ed25978SAndy Green 	 */
11656ed25978SAndy Green 
1166f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
11676ed25978SAndy Green 
11689949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
1169f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
11709949788bSMark Brown 
117187506549SMark Brown 	if (card->resume_pre)
117270b2ac12SMark Brown 		card->resume_pre(card);
1173db2a4165SFrank Mandarino 
1174f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
1175f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1176f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
11773efab7dcSMark Brown 
1178f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11793efab7dcSMark Brown 			continue;
11803efab7dcSMark Brown 
1181f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1182f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1183db2a4165SFrank Mandarino 	}
1184db2a4165SFrank Mandarino 
11852eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
11861547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
11871547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
11881547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
11891547aba9SMark Brown 		 */
1190f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
1191ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
11921547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
11931547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1194f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
1195f0fba2adSLiam Girdwood 				codec->suspended = 0;
11961547aba9SMark Brown 				break;
11971547aba9SMark Brown 			default:
1198f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
11991547aba9SMark Brown 				break;
12001547aba9SMark Brown 			}
12011547aba9SMark Brown 		}
1202f0fba2adSLiam Girdwood 	}
1203db2a4165SFrank Mandarino 
1204f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1205f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
12063efab7dcSMark Brown 
1207f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
12083efab7dcSMark Brown 			continue;
12093efab7dcSMark Brown 
1210f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1211f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1212db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1213f0fba2adSLiam Girdwood 
1214f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1215f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1216db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1217db2a4165SFrank Mandarino 	}
1218db2a4165SFrank Mandarino 
12193ff3f64bSMark Brown 	/* unmute any active DACs */
1220f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1221f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1222f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
12233efab7dcSMark Brown 
1224f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
12253efab7dcSMark Brown 			continue;
12263efab7dcSMark Brown 
1227f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1228f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
1229db2a4165SFrank Mandarino 	}
1230db2a4165SFrank Mandarino 
1231f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1232f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1233f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
12343efab7dcSMark Brown 
1235f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
12363efab7dcSMark Brown 			continue;
12373efab7dcSMark Brown 
1238f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1239f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1240f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
1241f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
1242f0fba2adSLiam Girdwood 			platform->suspended = 0;
1243f0fba2adSLiam Girdwood 		}
1244db2a4165SFrank Mandarino 	}
1245db2a4165SFrank Mandarino 
124687506549SMark Brown 	if (card->resume_post)
124770b2ac12SMark Brown 		card->resume_post(card);
1248db2a4165SFrank Mandarino 
1249f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
12506ed25978SAndy Green 
12516ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
1252f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
12536ed25978SAndy Green }
12546ed25978SAndy Green 
12556ed25978SAndy Green /* powers up audio subsystem after a suspend */
12566f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
12576ed25978SAndy Green {
12586f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
125982e14e8bSStephen Warren 	int i, ac97_control = 0;
1260b9dd94a8SPeter Ujfalusi 
126164ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
126264ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
126364ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
126464ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
126564ab9baaSMark Brown 	 */
1266f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1267f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
126882e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
126982e14e8bSStephen Warren 	}
127082e14e8bSStephen Warren 	if (ac97_control) {
1271f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
127264ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
127364ab9baaSMark Brown 	} else {
1274f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
12756308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
1276f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
1277f0fba2adSLiam Girdwood 	}
12786ed25978SAndy Green 
1279db2a4165SFrank Mandarino 	return 0;
1280db2a4165SFrank Mandarino }
12816f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
1282db2a4165SFrank Mandarino #else
12836f8ab4acSMark Brown #define snd_soc_suspend NULL
12846f8ab4acSMark Brown #define snd_soc_resume NULL
1285db2a4165SFrank Mandarino #endif
1286db2a4165SFrank Mandarino 
128702a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
128802a06d30SBarry Song };
128902a06d30SBarry Song 
1290f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1291db2a4165SFrank Mandarino {
1292f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1293f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1294fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1295435c5e25SMark Brown 	struct snd_soc_platform *platform;
1296f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
1297848dd8beSMark Brown 	const char *platform_name;
1298db2a4165SFrank Mandarino 
1299f0fba2adSLiam Girdwood 	if (rtd->complete)
1300f0fba2adSLiam Girdwood 		return 1;
1301f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
13026308419aSMark Brown 
1303f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
1304f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
1305f0fba2adSLiam Girdwood 		goto find_codec;
1306435c5e25SMark Brown 	}
1307f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1308f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
1309f0fba2adSLiam Girdwood 		if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1310f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
1311f0fba2adSLiam Girdwood 			goto find_codec;
1312f0fba2adSLiam Girdwood 		}
1313f0fba2adSLiam Girdwood 	}
1314f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
1315f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
1316f0fba2adSLiam Girdwood 
1317f0fba2adSLiam Girdwood find_codec:
1318f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
1319f0fba2adSLiam Girdwood 	if (rtd->codec) {
1320f0fba2adSLiam Girdwood 		goto find_platform;
1321c5af3a2eSMark Brown 	}
1322c5af3a2eSMark Brown 
1323f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
1324f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
1325f0fba2adSLiam Girdwood 		if (!strcmp(codec->name, dai_link->codec_name)) {
1326f0fba2adSLiam Girdwood 			rtd->codec = codec;
1327f0fba2adSLiam Girdwood 
1328f0fba2adSLiam Girdwood 			/* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1329f0fba2adSLiam Girdwood 			list_for_each_entry(codec_dai, &dai_list, list) {
1330f0fba2adSLiam Girdwood 				if (codec->dev == codec_dai->dev &&
1331f0fba2adSLiam Girdwood 						!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1332f0fba2adSLiam Girdwood 					rtd->codec_dai = codec_dai;
1333f0fba2adSLiam Girdwood 					goto find_platform;
1334435c5e25SMark Brown 				}
1335f0fba2adSLiam Girdwood 			}
1336f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1337f0fba2adSLiam Girdwood 					dai_link->codec_dai_name);
1338f0fba2adSLiam Girdwood 
1339f0fba2adSLiam Girdwood 			goto find_platform;
1340f0fba2adSLiam Girdwood 		}
1341f0fba2adSLiam Girdwood 	}
1342f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
1343f0fba2adSLiam Girdwood 			dai_link->codec_name);
1344f0fba2adSLiam Girdwood 
1345f0fba2adSLiam Girdwood find_platform:
1346848dd8beSMark Brown 	/* do we need a platform? */
1347848dd8beSMark Brown 	if (rtd->platform)
1348f0fba2adSLiam Girdwood 		goto out;
1349848dd8beSMark Brown 
1350848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
1351848dd8beSMark Brown 	platform_name = dai_link->platform_name;
1352848dd8beSMark Brown 	if (!platform_name)
1353848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
1354848dd8beSMark Brown 
1355848dd8beSMark Brown 	/* no, then find one from the set of registered platforms */
1356f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
1357848dd8beSMark Brown 		if (!strcmp(platform->name, platform_name)) {
1358f0fba2adSLiam Girdwood 			rtd->platform = platform;
1359f0fba2adSLiam Girdwood 			goto out;
1360f0fba2adSLiam Girdwood 		}
1361435c5e25SMark Brown 	}
13626b05eda6SMark Brown 
1363f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
1364f0fba2adSLiam Girdwood 			dai_link->platform_name);
1365f0fba2adSLiam Girdwood 	return 0;
1366f0fba2adSLiam Girdwood 
1367f0fba2adSLiam Girdwood out:
1368f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
1369f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1370f0fba2adSLiam Girdwood 		rtd->complete = 1;
1371f0fba2adSLiam Girdwood 		card->num_rtd++;
1372f0fba2adSLiam Girdwood 	}
1373f0fba2adSLiam Girdwood 	return 1;
13746b05eda6SMark Brown }
13756b05eda6SMark Brown 
1376589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
1377589c3563SJarkko Nikula {
1378589c3563SJarkko Nikula 	int err;
1379589c3563SJarkko Nikula 
1380589c3563SJarkko Nikula 	if (codec->driver->remove) {
1381589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
1382589c3563SJarkko Nikula 		if (err < 0)
1383589c3563SJarkko Nikula 			dev_err(codec->dev,
1384589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
1385589c3563SJarkko Nikula 				codec->name, err);
1386589c3563SJarkko Nikula 	}
1387589c3563SJarkko Nikula 
1388589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
1389589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
1390589c3563SJarkko Nikula 
1391589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
1392589c3563SJarkko Nikula 	codec->probed = 0;
1393589c3563SJarkko Nikula 	list_del(&codec->card_list);
1394589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
1395589c3563SJarkko Nikula }
1396589c3563SJarkko Nikula 
1397f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1398f0fba2adSLiam Girdwood {
1399f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1400f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1401f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1402f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1403f0fba2adSLiam Girdwood 	int err;
1404f0fba2adSLiam Girdwood 
1405f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1406f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
1407f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1408589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1409f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
1410f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
141102a06d30SBarry Song 	}
141202a06d30SBarry Song 
1413f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
1414f0fba2adSLiam Girdwood 	if (codec_dai && codec_dai->probed) {
1415f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1416f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1417f0fba2adSLiam Girdwood 			if (err < 0)
1418f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1419f0fba2adSLiam Girdwood 		}
1420f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1421f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1422f0fba2adSLiam Girdwood 	}
1423f0fba2adSLiam Girdwood 
1424f0fba2adSLiam Girdwood 	/* remove the platform */
1425f0fba2adSLiam Girdwood 	if (platform && platform->probed) {
1426f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
1427f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
1428f0fba2adSLiam Girdwood 			if (err < 0)
1429f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1430f0fba2adSLiam Girdwood 		}
1431f0fba2adSLiam Girdwood 		platform->probed = 0;
1432f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
1433f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
1434f0fba2adSLiam Girdwood 	}
1435f0fba2adSLiam Girdwood 
1436f0fba2adSLiam Girdwood 	/* remove the CODEC */
1437589c3563SJarkko Nikula 	if (codec && codec->probed)
1438589c3563SJarkko Nikula 		soc_remove_codec(codec);
1439f0fba2adSLiam Girdwood 
1440f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
1441f0fba2adSLiam Girdwood 	if (cpu_dai && cpu_dai->probed) {
1442f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1443f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1444f0fba2adSLiam Girdwood 			if (err < 0)
1445f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1446f0fba2adSLiam Girdwood 		}
1447f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1448f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1449f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
1450f0fba2adSLiam Girdwood 	}
1451f0fba2adSLiam Girdwood }
1452f0fba2adSLiam Girdwood 
14530671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
14540671fd8eSKuninori Morimoto {
14550671fd8eSKuninori Morimoto 	int i;
14560671fd8eSKuninori Morimoto 
14570671fd8eSKuninori Morimoto 	for (i = 0; i < card->num_rtd; i++)
14580671fd8eSKuninori Morimoto 		soc_remove_dai_link(card, i);
14590671fd8eSKuninori Morimoto 
14600671fd8eSKuninori Morimoto 	card->num_rtd = 0;
14610671fd8eSKuninori Morimoto }
14620671fd8eSKuninori Morimoto 
1463ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1464ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1465ead9b919SJarkko Nikula {
1466ead9b919SJarkko Nikula 	int i;
1467ead9b919SJarkko Nikula 
1468ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1469ead9b919SJarkko Nikula 		return;
1470ead9b919SJarkko Nikula 
1471ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1472ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1473ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1474ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1475ead9b919SJarkko Nikula 			break;
1476ead9b919SJarkko Nikula 		}
1477ead9b919SJarkko Nikula 	}
1478ead9b919SJarkko Nikula }
1479ead9b919SJarkko Nikula 
1480589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1481589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1482589c3563SJarkko Nikula {
1483589c3563SJarkko Nikula 	int ret = 0;
148489b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1485589c3563SJarkko Nikula 
1486589c3563SJarkko Nikula 	codec->card = card;
1487589c3563SJarkko Nikula 	codec->dapm.card = card;
1488589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1489589c3563SJarkko Nikula 
149070d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
149170d29331SJarkko Nikula 		return -ENODEV;
149270d29331SJarkko Nikula 
1493d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1494d5d1e0beSLars-Peter Clausen 
149577530150SLars-Peter Clausen 	if (driver->dapm_widgets)
149677530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
149777530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
149877530150SLars-Peter Clausen 
149989b95ac0SMark Brown 	if (driver->probe) {
150089b95ac0SMark Brown 		ret = driver->probe(codec);
1501589c3563SJarkko Nikula 		if (ret < 0) {
1502589c3563SJarkko Nikula 			dev_err(codec->dev,
1503589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1504589c3563SJarkko Nikula 				codec->name, ret);
150570d29331SJarkko Nikula 			goto err_probe;
1506589c3563SJarkko Nikula 		}
1507589c3563SJarkko Nikula 	}
1508589c3563SJarkko Nikula 
1509b7af1dafSMark Brown 	if (driver->controls)
1510b7af1dafSMark Brown 		snd_soc_add_controls(codec, driver->controls,
1511b7af1dafSMark Brown 				     driver->num_controls);
151289b95ac0SMark Brown 	if (driver->dapm_routes)
151389b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
151489b95ac0SMark Brown 					driver->num_dapm_routes);
151589b95ac0SMark Brown 
1516589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1517589c3563SJarkko Nikula 	codec->probed = 1;
1518589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
15197be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1520589c3563SJarkko Nikula 
152170d29331SJarkko Nikula 	return 0;
152270d29331SJarkko Nikula 
152370d29331SJarkko Nikula err_probe:
1524d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
152570d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
152670d29331SJarkko Nikula 
1527589c3563SJarkko Nikula 	return ret;
1528589c3563SJarkko Nikula }
1529589c3563SJarkko Nikula 
1530f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1531f0fba2adSLiam Girdwood 
1532589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1533589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1534589c3563SJarkko Nikula 				   int num, int dailess)
1535589c3563SJarkko Nikula {
1536589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1537589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1538589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1539589c3563SJarkko Nikula 	const char *temp, *name;
1540589c3563SJarkko Nikula 	int ret = 0;
1541589c3563SJarkko Nikula 
1542589c3563SJarkko Nikula 	if (!dailess) {
1543589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1544589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1545589c3563SJarkko Nikula 		name = dai_link->name;
1546589c3563SJarkko Nikula 	} else {
1547589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1548589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1549589c3563SJarkko Nikula 		name = aux_dev->name;
1550589c3563SJarkko Nikula 	}
15510962bb21SJanusz Krzysztofik 	rtd->card = card;
1552589c3563SJarkko Nikula 
1553589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1554589c3563SJarkko Nikula 	temp = codec->name_prefix;
1555589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1556589c3563SJarkko Nikula 
1557589c3563SJarkko Nikula 	/* do machine specific initialization */
1558589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1559589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1560589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1561589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1562589c3563SJarkko Nikula 	if (ret < 0) {
1563589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1564589c3563SJarkko Nikula 		return ret;
1565589c3563SJarkko Nikula 	}
1566589c3563SJarkko Nikula 	codec->name_prefix = temp;
1567589c3563SJarkko Nikula 
1568589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are instantiated */
1569589c3563SJarkko Nikula 	snd_soc_dapm_new_widgets(&codec->dapm);
1570589c3563SJarkko Nikula 
1571589c3563SJarkko Nikula 	/* register the rtd device */
1572589c3563SJarkko Nikula 	rtd->codec = codec;
1573589c3563SJarkko Nikula 	rtd->dev.parent = card->dev;
1574589c3563SJarkko Nikula 	rtd->dev.release = rtd_release;
1575589c3563SJarkko Nikula 	rtd->dev.init_name = name;
1576589c3563SJarkko Nikula 	ret = device_register(&rtd->dev);
1577589c3563SJarkko Nikula 	if (ret < 0) {
1578589c3563SJarkko Nikula 		dev_err(card->dev,
1579589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1580589c3563SJarkko Nikula 		return ret;
1581589c3563SJarkko Nikula 	}
1582589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1583589c3563SJarkko Nikula 
1584589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
1585589c3563SJarkko Nikula 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1586589c3563SJarkko Nikula 	if (ret < 0)
1587589c3563SJarkko Nikula 		dev_err(codec->dev,
1588589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1589589c3563SJarkko Nikula 			ret);
1590589c3563SJarkko Nikula 
1591589c3563SJarkko Nikula 	/* add codec sysfs entries */
1592589c3563SJarkko Nikula 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1593589c3563SJarkko Nikula 	if (ret < 0)
1594589c3563SJarkko Nikula 		dev_err(codec->dev,
1595589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1596589c3563SJarkko Nikula 
1597589c3563SJarkko Nikula 	return 0;
1598589c3563SJarkko Nikula }
1599589c3563SJarkko Nikula 
1600f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1601f0fba2adSLiam Girdwood {
1602f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1603f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1604f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1605f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1606f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1607f0fba2adSLiam Girdwood 	int ret;
1608f0fba2adSLiam Girdwood 
1609f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1610f0fba2adSLiam Girdwood 
1611f0fba2adSLiam Girdwood 	/* config components */
1612f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1613f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1614f0fba2adSLiam Girdwood 	codec_dai->card = card;
1615f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1616f0fba2adSLiam Girdwood 
1617f0fba2adSLiam Girdwood 	/* set default power off timeout */
1618f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1619f0fba2adSLiam Girdwood 
1620f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
1621f0fba2adSLiam Girdwood 	if (!cpu_dai->probed) {
162261b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
162361b61e3cSLiam Girdwood 			return -ENODEV;
162461b61e3cSLiam Girdwood 
1625f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1626f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1627f0fba2adSLiam Girdwood 			if (ret < 0) {
1628f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1629f0fba2adSLiam Girdwood 						cpu_dai->name);
163061b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1631f0fba2adSLiam Girdwood 				return ret;
1632f0fba2adSLiam Girdwood 			}
1633f0fba2adSLiam Girdwood 		}
1634f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1635f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1636f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1637f0fba2adSLiam Girdwood 	}
1638f0fba2adSLiam Girdwood 
1639f0fba2adSLiam Girdwood 	/* probe the CODEC */
1640f0fba2adSLiam Girdwood 	if (!codec->probed) {
1641589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1642589c3563SJarkko Nikula 		if (ret < 0)
1643f0fba2adSLiam Girdwood 			return ret;
1644f0fba2adSLiam Girdwood 	}
1645f0fba2adSLiam Girdwood 
1646f0fba2adSLiam Girdwood 	/* probe the platform */
1647f0fba2adSLiam Girdwood 	if (!platform->probed) {
164870d29331SJarkko Nikula 		if (!try_module_get(platform->dev->driver->owner))
164970d29331SJarkko Nikula 			return -ENODEV;
165070d29331SJarkko Nikula 
1651f0fba2adSLiam Girdwood 		if (platform->driver->probe) {
1652f0fba2adSLiam Girdwood 			ret = platform->driver->probe(platform);
1653f0fba2adSLiam Girdwood 			if (ret < 0) {
1654f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe platform %s\n",
1655f0fba2adSLiam Girdwood 						platform->name);
165670d29331SJarkko Nikula 				module_put(platform->dev->driver->owner);
1657f0fba2adSLiam Girdwood 				return ret;
1658f0fba2adSLiam Girdwood 			}
1659f0fba2adSLiam Girdwood 		}
1660f0fba2adSLiam Girdwood 		/* mark platform as probed and add to card platform list */
1661f0fba2adSLiam Girdwood 		platform->probed = 1;
1662f0fba2adSLiam Girdwood 		list_add(&platform->card_list, &card->platform_dev_list);
1663f0fba2adSLiam Girdwood 	}
1664f0fba2adSLiam Girdwood 
1665f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
1666f0fba2adSLiam Girdwood 	if (!codec_dai->probed) {
1667f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1668f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1669f0fba2adSLiam Girdwood 			if (ret < 0) {
1670f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1671f0fba2adSLiam Girdwood 						codec_dai->name);
1672f0fba2adSLiam Girdwood 				return ret;
1673f0fba2adSLiam Girdwood 			}
1674f0fba2adSLiam Girdwood 		}
1675f0fba2adSLiam Girdwood 
1676f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1677f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1678f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1679f0fba2adSLiam Girdwood 	}
1680f0fba2adSLiam Girdwood 
1681f0fba2adSLiam Girdwood 	/* DAPM dai link stream work */
1682f0fba2adSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1683f0fba2adSLiam Girdwood 
1684589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1685589c3563SJarkko Nikula 	if (ret)
1686f0fba2adSLiam Girdwood 		return ret;
1687f0fba2adSLiam Girdwood 
1688f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1689f0fba2adSLiam Girdwood 	if (ret < 0)
1690f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1691f0fba2adSLiam Girdwood 
1692f0fba2adSLiam Girdwood 	/* create the pcm */
1693f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1694f0fba2adSLiam Girdwood 	if (ret < 0) {
1695f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1696f0fba2adSLiam Girdwood 		return ret;
1697f0fba2adSLiam Girdwood 	}
1698f0fba2adSLiam Girdwood 
1699f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1700f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1701f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1702f0fba2adSLiam Girdwood 
1703f0fba2adSLiam Girdwood 	return 0;
1704f0fba2adSLiam Girdwood }
1705f0fba2adSLiam Girdwood 
1706f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1707f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1708f0fba2adSLiam Girdwood {
1709f0fba2adSLiam Girdwood 	int ret;
1710f0fba2adSLiam Girdwood 
1711f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1712f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
17136b05eda6SMark Brown 	 */
1714f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
17150562f788SMika Westerberg 		/*
17160562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
17170562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
17180562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
17190562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
17200562f788SMika Westerberg 		 *
17210562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
17220562f788SMika Westerberg 		 */
17230562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
17240562f788SMika Westerberg 			return 0;
1725f0fba2adSLiam Girdwood 
1726f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1727f0fba2adSLiam Girdwood 		if (ret < 0) {
1728f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1729f0fba2adSLiam Girdwood 			return ret;
1730435c5e25SMark Brown 		}
1731435c5e25SMark Brown 
1732f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1733f0fba2adSLiam Girdwood 	}
1734f0fba2adSLiam Girdwood 	return 0;
1735f0fba2adSLiam Girdwood }
1736435c5e25SMark Brown 
1737f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1738f0fba2adSLiam Girdwood {
1739f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1740f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1741f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1742f0fba2adSLiam Girdwood 	}
1743f0fba2adSLiam Girdwood }
1744f0fba2adSLiam Girdwood #endif
1745435c5e25SMark Brown 
17462eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
17472eea392dSJarkko Nikula {
17482eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
17492eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1750676ad98aSJarkko Nikula 	int ret = -ENODEV;
17512eea392dSJarkko Nikula 
17522eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
17532eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
17542eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
17552eea392dSJarkko Nikula 			if (codec->probed) {
17562eea392dSJarkko Nikula 				dev_err(codec->dev,
17572eea392dSJarkko Nikula 					"asoc: codec already probed");
17582eea392dSJarkko Nikula 				ret = -EBUSY;
17592eea392dSJarkko Nikula 				goto out;
17602eea392dSJarkko Nikula 			}
1761676ad98aSJarkko Nikula 			goto found;
17622eea392dSJarkko Nikula 		}
17632eea392dSJarkko Nikula 	}
1764676ad98aSJarkko Nikula 	/* codec not found */
1765676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1766676ad98aSJarkko Nikula 	goto out;
17672eea392dSJarkko Nikula 
1768676ad98aSJarkko Nikula found:
1769589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
17702eea392dSJarkko Nikula 	if (ret < 0)
1771589c3563SJarkko Nikula 		return ret;
17722eea392dSJarkko Nikula 
1773589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
17742eea392dSJarkko Nikula 
17752eea392dSJarkko Nikula out:
17762eea392dSJarkko Nikula 	return ret;
17772eea392dSJarkko Nikula }
17782eea392dSJarkko Nikula 
17792eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
17802eea392dSJarkko Nikula {
17812eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
17822eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
17832eea392dSJarkko Nikula 
17842eea392dSJarkko Nikula 	/* unregister the rtd device */
17852eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1786589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
17872eea392dSJarkko Nikula 		device_unregister(&rtd->dev);
17882eea392dSJarkko Nikula 		rtd->dev_registered = 0;
17892eea392dSJarkko Nikula 	}
17902eea392dSJarkko Nikula 
1791589c3563SJarkko Nikula 	if (codec && codec->probed)
1792589c3563SJarkko Nikula 		soc_remove_codec(codec);
17932eea392dSJarkko Nikula }
17942eea392dSJarkko Nikula 
1795fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1796fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1797fdf0f54dSDimitris Papastamos {
1798fdf0f54dSDimitris Papastamos 	int ret;
1799fdf0f54dSDimitris Papastamos 
1800fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1801fdf0f54dSDimitris Papastamos 		return 0;
1802fdf0f54dSDimitris Papastamos 
1803fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1804fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1805fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1806fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1807fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1808fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1809fdf0f54dSDimitris Papastamos 			ret);
1810fdf0f54dSDimitris Papastamos 		return ret;
1811fdf0f54dSDimitris Papastamos 	}
1812fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1813fdf0f54dSDimitris Papastamos 	return 0;
1814fdf0f54dSDimitris Papastamos }
1815fdf0f54dSDimitris Papastamos 
1816f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1817f0fba2adSLiam Girdwood {
1818fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1819fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1820fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
1821f0fba2adSLiam Girdwood 	int ret, i;
182296dd3622SMark Brown 
1823f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1824f0fba2adSLiam Girdwood 
1825f0fba2adSLiam Girdwood 	if (card->instantiated) {
1826f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1827435c5e25SMark Brown 		return;
1828db2a4165SFrank Mandarino 	}
1829db2a4165SFrank Mandarino 
1830f0fba2adSLiam Girdwood 	/* bind DAIs */
1831f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1832f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1833f0fba2adSLiam Girdwood 
1834f0fba2adSLiam Girdwood 	/* bind completed ? */
1835f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1836f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1837f0fba2adSLiam Girdwood 		return;
1838db2a4165SFrank Mandarino 	}
1839db2a4165SFrank Mandarino 
1840fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1841fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1842fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1843fdf0f54dSDimitris Papastamos 			continue;
1844861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1845861f2fafSDimitris Papastamos 		compress_type = 0;
1846fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1847fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1848fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1849fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1850fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1851fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1852fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1853fdf0f54dSDimitris Papastamos 					break;
1854fdf0f54dSDimitris Papastamos 			}
1855fdf0f54dSDimitris Papastamos 		}
1856fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1857fdf0f54dSDimitris Papastamos 		if (ret < 0) {
1858fdf0f54dSDimitris Papastamos 			mutex_unlock(&card->mutex);
1859fdf0f54dSDimitris Papastamos 			return;
1860fdf0f54dSDimitris Papastamos 		}
1861fdf0f54dSDimitris Papastamos 	}
1862fdf0f54dSDimitris Papastamos 
1863f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1864f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1865f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1866f0fba2adSLiam Girdwood 	if (ret < 0) {
1867f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1868f0fba2adSLiam Girdwood 			card->name);
1869f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1870f0fba2adSLiam Girdwood 		return;
1871db2a4165SFrank Mandarino 	}
1872f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1873db2a4165SFrank Mandarino 
1874e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1875e37a4970SMark Brown 	card->dapm.dev = card->dev;
1876e37a4970SMark Brown 	card->dapm.card = card;
1877e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1878e37a4970SMark Brown 
1879d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1880d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1881d5d1e0beSLars-Peter Clausen #endif
1882d5d1e0beSLars-Peter Clausen 
188388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
18846ed25978SAndy Green 	/* deferred resume work */
18856308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
18861301a964SRandy Dunlap #endif
18876ed25978SAndy Green 
18889a841ebbSMark Brown 	if (card->dapm_widgets)
18899a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
18909a841ebbSMark Brown 					  card->num_dapm_widgets);
18919a841ebbSMark Brown 
1892f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1893f0fba2adSLiam Girdwood 	if (card->probe) {
1894e7361ec4SMark Brown 		ret = card->probe(card);
1895f0fba2adSLiam Girdwood 		if (ret < 0)
1896f0fba2adSLiam Girdwood 			goto card_probe_error;
1897f0fba2adSLiam Girdwood 	}
1898f0fba2adSLiam Girdwood 
1899fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1900f0fba2adSLiam Girdwood 		ret = soc_probe_dai_link(card, i);
1901fe3e78e0SMark Brown 		if (ret < 0) {
1902321de0d0SMark Brown 			pr_err("asoc: failed to instantiate card %s: %d\n",
1903321de0d0SMark Brown 			       card->name, ret);
1904f0fba2adSLiam Girdwood 			goto probe_dai_err;
1905fe3e78e0SMark Brown 		}
1906fe3e78e0SMark Brown 	}
1907fe3e78e0SMark Brown 
19082eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
19092eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
19102eea392dSJarkko Nikula 		if (ret < 0) {
19112eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
19122eea392dSJarkko Nikula 			       card->name, ret);
19132eea392dSJarkko Nikula 			goto probe_aux_dev_err;
19142eea392dSJarkko Nikula 		}
19152eea392dSJarkko Nikula 	}
19162eea392dSJarkko Nikula 
1917b7af1dafSMark Brown 	/* We should have a non-codec control add function but we don't */
1918b7af1dafSMark Brown 	if (card->controls)
1919b7af1dafSMark Brown 		snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1920b7af1dafSMark Brown 						      struct snd_soc_codec,
1921b7af1dafSMark Brown 						      card_list),
1922b7af1dafSMark Brown 				     card->controls,
1923b7af1dafSMark Brown 				     card->num_controls);
1924b7af1dafSMark Brown 
1925b8ad29deSMark Brown 	if (card->dapm_routes)
1926b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1927b8ad29deSMark Brown 					card->num_dapm_routes);
1928b8ad29deSMark Brown 
1929f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1930fe3e78e0SMark Brown 		 "%s", card->name);
1931f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
193222de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
193322de71baSLiam Girdwood 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
19342b39535bSJarkko Nikula 		 "%s", card->driver_name ? card->driver_name : card->name);
1935fe3e78e0SMark Brown 
193628e9ad92SMark Brown 	if (card->late_probe) {
193728e9ad92SMark Brown 		ret = card->late_probe(card);
193828e9ad92SMark Brown 		if (ret < 0) {
193928e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
194028e9ad92SMark Brown 				card->name, ret);
194128e9ad92SMark Brown 			goto probe_aux_dev_err;
194228e9ad92SMark Brown 		}
194328e9ad92SMark Brown 	}
194428e9ad92SMark Brown 
1945f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1946fe3e78e0SMark Brown 	if (ret < 0) {
1947f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
19486b3ed785SAxel Lin 		goto probe_aux_dev_err;
1949fe3e78e0SMark Brown 	}
1950fe3e78e0SMark Brown 
1951fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1952f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1953f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1954f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1955fe3e78e0SMark Brown 		if (ret < 0) {
1956f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
19576b3ed785SAxel Lin 			while (--i >= 0)
19587d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
19596b3ed785SAxel Lin 			goto probe_aux_dev_err;
1960fe3e78e0SMark Brown 		}
1961fe3e78e0SMark Brown 	}
1962fe3e78e0SMark Brown #endif
1963fe3e78e0SMark Brown 
1964435c5e25SMark Brown 	card->instantiated = 1;
1965f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1966435c5e25SMark Brown 	return;
1967db2a4165SFrank Mandarino 
19682eea392dSJarkko Nikula probe_aux_dev_err:
19692eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
19702eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
19712eea392dSJarkko Nikula 
1972f0fba2adSLiam Girdwood probe_dai_err:
19730671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1974fe3e78e0SMark Brown 
1975f0fba2adSLiam Girdwood card_probe_error:
197687506549SMark Brown 	if (card->remove)
1977e7361ec4SMark Brown 		card->remove(card);
1978f0fba2adSLiam Girdwood 
1979f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1980f0fba2adSLiam Girdwood 
1981f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1982435c5e25SMark Brown }
1983db2a4165SFrank Mandarino 
1984435c5e25SMark Brown /*
1985421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1986435c5e25SMark Brown  * client_mutex.
1987435c5e25SMark Brown  */
1988435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1989435c5e25SMark Brown {
1990435c5e25SMark Brown 	struct snd_soc_card *card;
1991435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1992435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1993435c5e25SMark Brown }
1994435c5e25SMark Brown 
1995435c5e25SMark Brown /* probes a new socdev */
1996435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1997435c5e25SMark Brown {
1998f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1999435c5e25SMark Brown 	int ret = 0;
2000435c5e25SMark Brown 
200170a7ca34SVinod Koul 	/*
200270a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
200370a7ca34SVinod Koul 	 * we should not be here in that case so ret error
200470a7ca34SVinod Koul 	 */
200570a7ca34SVinod Koul 	if (!card)
200670a7ca34SVinod Koul 		return -EINVAL;
200770a7ca34SVinod Koul 
2008435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2009435c5e25SMark Brown 	card->dev = &pdev->dev;
2010f0fba2adSLiam Girdwood 
2011435c5e25SMark Brown 	ret = snd_soc_register_card(card);
2012435c5e25SMark Brown 	if (ret != 0) {
2013435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
2014db2a4165SFrank Mandarino 		return ret;
2015db2a4165SFrank Mandarino 	}
2016db2a4165SFrank Mandarino 
2017435c5e25SMark Brown 	return 0;
2018435c5e25SMark Brown }
2019435c5e25SMark Brown 
2020b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2021db2a4165SFrank Mandarino {
2022db2a4165SFrank Mandarino 	int i;
2023db2a4165SFrank Mandarino 
2024f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
2025f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2026f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
20275b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
2028db2a4165SFrank Mandarino 	}
2029db2a4165SFrank Mandarino 
20302eea392dSJarkko Nikula 	/* remove auxiliary devices */
20312eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
20322eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
20332eea392dSJarkko Nikula 
2034f0fba2adSLiam Girdwood 	/* remove and free each DAI */
20350671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2036f0fba2adSLiam Girdwood 
2037a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2038a6052154SJarkko Nikula 
2039f0fba2adSLiam Girdwood 	/* remove the card */
204087506549SMark Brown 	if (card->remove)
2041e7361ec4SMark Brown 		card->remove(card);
2042f0fba2adSLiam Girdwood 
20430aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
20440aaae527SLars-Peter Clausen 
2045f0fba2adSLiam Girdwood 	kfree(card->rtd);
2046f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2047b0e26485SVinod Koul 	return 0;
2048b0e26485SVinod Koul 
2049b2dfa62cSGuennadi Liakhovetski }
2050b0e26485SVinod Koul 
2051b0e26485SVinod Koul /* removes a socdev */
2052b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2053b0e26485SVinod Koul {
2054b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2055b0e26485SVinod Koul 
2056c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2057db2a4165SFrank Mandarino 	return 0;
2058db2a4165SFrank Mandarino }
2059db2a4165SFrank Mandarino 
20606f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
206151737470SMark Brown {
20626f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
2063f0fba2adSLiam Girdwood 	int i;
206451737470SMark Brown 
206551737470SMark Brown 	if (!card->instantiated)
2066416356fcSMark Brown 		return 0;
206751737470SMark Brown 
206851737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
206951737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
2070f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2071f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
20725b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
2073f0fba2adSLiam Girdwood 	}
207451737470SMark Brown 
2075f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2076416356fcSMark Brown 
2077416356fcSMark Brown 	return 0;
207851737470SMark Brown }
20796f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
208051737470SMark Brown 
20816f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
20826f8ab4acSMark Brown 	.suspend = snd_soc_suspend,
20836f8ab4acSMark Brown 	.resume = snd_soc_resume,
20846f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2085416356fcSMark Brown };
2086deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2087416356fcSMark Brown 
2088db2a4165SFrank Mandarino /* ASoC platform driver */
2089db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2090db2a4165SFrank Mandarino 	.driver		= {
2091db2a4165SFrank Mandarino 		.name		= "soc-audio",
20928b45a209SKay Sievers 		.owner		= THIS_MODULE,
20936f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2094db2a4165SFrank Mandarino 	},
2095db2a4165SFrank Mandarino 	.probe		= soc_probe,
2096db2a4165SFrank Mandarino 	.remove		= soc_remove,
2097db2a4165SFrank Mandarino };
2098db2a4165SFrank Mandarino 
2099db2a4165SFrank Mandarino /* create a new pcm */
2100f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2101db2a4165SFrank Mandarino {
2102f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
2103f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
2104f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
2105f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2106db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
2107db2a4165SFrank Mandarino 	char new_name[64];
2108db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
2109db2a4165SFrank Mandarino 
2110db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
211140ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
2112f0fba2adSLiam Girdwood 			rtd->dai_link->stream_name, codec_dai->name, num);
2113db2a4165SFrank Mandarino 
2114f0fba2adSLiam Girdwood 	if (codec_dai->driver->playback.channels_min)
2115db2a4165SFrank Mandarino 		playback = 1;
2116f0fba2adSLiam Girdwood 	if (codec_dai->driver->capture.channels_min)
2117db2a4165SFrank Mandarino 		capture = 1;
2118db2a4165SFrank Mandarino 
2119f0fba2adSLiam Girdwood 	dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2120f0fba2adSLiam Girdwood 	ret = snd_pcm_new(rtd->card->snd_card, new_name,
2121f0fba2adSLiam Girdwood 			num, playback, capture, &pcm);
2122db2a4165SFrank Mandarino 	if (ret < 0) {
2123f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2124db2a4165SFrank Mandarino 		return ret;
2125db2a4165SFrank Mandarino 	}
2126db2a4165SFrank Mandarino 
2127f0fba2adSLiam Girdwood 	rtd->pcm = pcm;
2128db2a4165SFrank Mandarino 	pcm->private_data = rtd;
21298842c72aSMark Brown 	if (platform->driver->ops) {
2130f0fba2adSLiam Girdwood 		soc_pcm_ops.mmap = platform->driver->ops->mmap;
2131f0fba2adSLiam Girdwood 		soc_pcm_ops.pointer = platform->driver->ops->pointer;
2132f0fba2adSLiam Girdwood 		soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2133f0fba2adSLiam Girdwood 		soc_pcm_ops.copy = platform->driver->ops->copy;
2134f0fba2adSLiam Girdwood 		soc_pcm_ops.silence = platform->driver->ops->silence;
2135f0fba2adSLiam Girdwood 		soc_pcm_ops.ack = platform->driver->ops->ack;
2136f0fba2adSLiam Girdwood 		soc_pcm_ops.page = platform->driver->ops->page;
21378842c72aSMark Brown 	}
2138db2a4165SFrank Mandarino 
2139db2a4165SFrank Mandarino 	if (playback)
2140db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2141db2a4165SFrank Mandarino 
2142db2a4165SFrank Mandarino 	if (capture)
2143db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2144db2a4165SFrank Mandarino 
21458842c72aSMark Brown 	if (platform->driver->pcm_new) {
21468842c72aSMark Brown 		ret = platform->driver->pcm_new(rtd->card->snd_card,
21478842c72aSMark Brown 						codec_dai, pcm);
2148db2a4165SFrank Mandarino 		if (ret < 0) {
21498842c72aSMark Brown 			pr_err("asoc: platform pcm constructor failed\n");
2150db2a4165SFrank Mandarino 			return ret;
2151db2a4165SFrank Mandarino 		}
21528842c72aSMark Brown 	}
2153db2a4165SFrank Mandarino 
2154f0fba2adSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
2155db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2156db2a4165SFrank Mandarino 		cpu_dai->name);
2157db2a4165SFrank Mandarino 	return ret;
2158db2a4165SFrank Mandarino }
2159db2a4165SFrank Mandarino 
2160096e49d5SMark Brown /**
2161096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
2162096e49d5SMark Brown  *
2163096e49d5SMark Brown  * @codec: CODEC to query.
2164096e49d5SMark Brown  * @reg: Register to query.
2165096e49d5SMark Brown  *
2166096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
2167096e49d5SMark Brown  */
2168181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2169181e055eSMark Brown 				    unsigned int reg)
2170096e49d5SMark Brown {
21711500b7b5SDimitris Papastamos 	if (codec->volatile_register)
21721500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
2173096e49d5SMark Brown 	else
2174096e49d5SMark Brown 		return 0;
2175096e49d5SMark Brown }
2176096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2177096e49d5SMark Brown 
2178db2a4165SFrank Mandarino /**
2179239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
2180239c9706SDimitris Papastamos  *
2181239c9706SDimitris Papastamos  * @codec: CODEC to query.
2182239c9706SDimitris Papastamos  * @reg: Register to query.
2183239c9706SDimitris Papastamos  *
2184239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
2185239c9706SDimitris Papastamos  */
2186239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2187239c9706SDimitris Papastamos 				    unsigned int reg)
2188239c9706SDimitris Papastamos {
2189239c9706SDimitris Papastamos 	if (codec->readable_register)
2190239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
2191239c9706SDimitris Papastamos 	else
2192239c9706SDimitris Papastamos 		return 0;
2193239c9706SDimitris Papastamos }
2194239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2195239c9706SDimitris Papastamos 
2196239c9706SDimitris Papastamos /**
2197239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
2198239c9706SDimitris Papastamos  *
2199239c9706SDimitris Papastamos  * @codec: CODEC to query.
2200239c9706SDimitris Papastamos  * @reg: Register to query.
2201239c9706SDimitris Papastamos  *
2202239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
2203239c9706SDimitris Papastamos  */
2204239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2205239c9706SDimitris Papastamos 				    unsigned int reg)
2206239c9706SDimitris Papastamos {
2207239c9706SDimitris Papastamos 	if (codec->writable_register)
2208239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
2209239c9706SDimitris Papastamos 	else
2210239c9706SDimitris Papastamos 		return 0;
2211239c9706SDimitris Papastamos }
2212239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2213239c9706SDimitris Papastamos 
2214239c9706SDimitris Papastamos /**
2215db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2216db2a4165SFrank Mandarino  * @codec: audio codec
2217db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2218db2a4165SFrank Mandarino  * @num: AC97 codec number
2219db2a4165SFrank Mandarino  *
2220db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2221db2a4165SFrank Mandarino  */
2222db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2223db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2224db2a4165SFrank Mandarino {
2225db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2226db2a4165SFrank Mandarino 
2227db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2228db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2229db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2230db2a4165SFrank Mandarino 		return -ENOMEM;
2231db2a4165SFrank Mandarino 	}
2232db2a4165SFrank Mandarino 
2233db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2234db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2235db2a4165SFrank Mandarino 		kfree(codec->ac97);
2236db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2237db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2238db2a4165SFrank Mandarino 		return -ENOMEM;
2239db2a4165SFrank Mandarino 	}
2240db2a4165SFrank Mandarino 
2241db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2242db2a4165SFrank Mandarino 	codec->ac97->num = num;
22430562f788SMika Westerberg 
22440562f788SMika Westerberg 	/*
22450562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
22460562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
22470562f788SMika Westerberg 	 */
22480562f788SMika Westerberg 	codec->ac97_created = 1;
22490562f788SMika Westerberg 
2250db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2251db2a4165SFrank Mandarino 	return 0;
2252db2a4165SFrank Mandarino }
2253db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2254db2a4165SFrank Mandarino 
2255db2a4165SFrank Mandarino /**
2256db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2257db2a4165SFrank Mandarino  * @codec: audio codec
2258db2a4165SFrank Mandarino  *
2259db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2260db2a4165SFrank Mandarino  */
2261db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2262db2a4165SFrank Mandarino {
2263db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2264f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2265f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2266f0fba2adSLiam Girdwood #endif
2267db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2268db2a4165SFrank Mandarino 	kfree(codec->ac97);
2269db2a4165SFrank Mandarino 	codec->ac97 = NULL;
22700562f788SMika Westerberg 	codec->ac97_created = 0;
2271db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2272db2a4165SFrank Mandarino }
2273db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2274db2a4165SFrank Mandarino 
2275c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2276c3753707SMark Brown {
2277c3753707SMark Brown 	unsigned int ret;
2278c3753707SMark Brown 
2279c3acec26SMark Brown 	ret = codec->read(codec, reg);
2280c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2281a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2282c3753707SMark Brown 
2283c3753707SMark Brown 	return ret;
2284c3753707SMark Brown }
2285c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2286c3753707SMark Brown 
2287c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2288c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2289c3753707SMark Brown {
2290c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2291a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2292c3acec26SMark Brown 	return codec->write(codec, reg, val);
2293c3753707SMark Brown }
2294c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2295c3753707SMark Brown 
22965fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
22975fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
22985fb609d4SDimitris Papastamos {
22995fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
23005fb609d4SDimitris Papastamos }
23015fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
23025fb609d4SDimitris Papastamos 
2303db2a4165SFrank Mandarino /**
2304db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2305db2a4165SFrank Mandarino  * @codec: audio codec
2306db2a4165SFrank Mandarino  * @reg: codec register
2307db2a4165SFrank Mandarino  * @mask: register mask
2308db2a4165SFrank Mandarino  * @value: new value
2309db2a4165SFrank Mandarino  *
2310db2a4165SFrank Mandarino  * Writes new register value.
2311db2a4165SFrank Mandarino  *
2312180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
2313db2a4165SFrank Mandarino  */
2314db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
231546f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2316db2a4165SFrank Mandarino {
2317db2a4165SFrank Mandarino 	int change;
231846f5822fSDaniel Ribeiro 	unsigned int old, new;
2319180c329dSTimur Tabi 	int ret;
2320db2a4165SFrank Mandarino 
2321180c329dSTimur Tabi 	ret = snd_soc_read(codec, reg);
2322180c329dSTimur Tabi 	if (ret < 0)
2323180c329dSTimur Tabi 		return ret;
2324180c329dSTimur Tabi 
2325180c329dSTimur Tabi 	old = ret;
2326*78bf3c9aSMark Brown 	new = (old & ~mask) | (value & mask);
2327db2a4165SFrank Mandarino 	change = old != new;
2328180c329dSTimur Tabi 	if (change) {
2329180c329dSTimur Tabi 		ret = snd_soc_write(codec, reg, new);
2330180c329dSTimur Tabi 		if (ret < 0)
2331180c329dSTimur Tabi 			return ret;
2332180c329dSTimur Tabi 	}
2333db2a4165SFrank Mandarino 
2334db2a4165SFrank Mandarino 	return change;
2335db2a4165SFrank Mandarino }
2336db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2337db2a4165SFrank Mandarino 
2338db2a4165SFrank Mandarino /**
23396c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
23406c508c62SEero Nurkkala  * @codec: audio codec
23416c508c62SEero Nurkkala  * @reg: codec register
23426c508c62SEero Nurkkala  * @mask: register mask
23436c508c62SEero Nurkkala  * @value: new value
23446c508c62SEero Nurkkala  *
23456c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
23466c508c62SEero Nurkkala  *
23476c508c62SEero Nurkkala  * Returns 1 for change else 0.
23486c508c62SEero Nurkkala  */
2349dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
23506c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
23516c508c62SEero Nurkkala 			       unsigned int value)
23526c508c62SEero Nurkkala {
23536c508c62SEero Nurkkala 	int change;
23546c508c62SEero Nurkkala 
23556c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
23566c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
23576c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
23586c508c62SEero Nurkkala 
23596c508c62SEero Nurkkala 	return change;
23606c508c62SEero Nurkkala }
2361dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
23626c508c62SEero Nurkkala 
23636c508c62SEero Nurkkala /**
2364db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2365db2a4165SFrank Mandarino  * @codec: audio codec
2366db2a4165SFrank Mandarino  * @reg: codec register
2367db2a4165SFrank Mandarino  * @mask: register mask
2368db2a4165SFrank Mandarino  * @value: new value
2369db2a4165SFrank Mandarino  *
2370db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2371db2a4165SFrank Mandarino  * different from the old value.
2372db2a4165SFrank Mandarino  *
2373db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2374db2a4165SFrank Mandarino  */
2375db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
237646f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2377db2a4165SFrank Mandarino {
2378db2a4165SFrank Mandarino 	int change;
237946f5822fSDaniel Ribeiro 	unsigned int old, new;
2380db2a4165SFrank Mandarino 
2381db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2382db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2383db2a4165SFrank Mandarino 	change = old != new;
2384db2a4165SFrank Mandarino 
2385db2a4165SFrank Mandarino 	return change;
2386db2a4165SFrank Mandarino }
2387db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2388db2a4165SFrank Mandarino 
2389db2a4165SFrank Mandarino /**
2390db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2391db2a4165SFrank Mandarino  * @substream: the pcm substream
2392db2a4165SFrank Mandarino  * @hw: the hardware parameters
2393db2a4165SFrank Mandarino  *
2394db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2395db2a4165SFrank Mandarino  */
2396db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2397db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2398db2a4165SFrank Mandarino {
2399db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2400db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2401db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2402db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2403db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2404db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2405db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2406db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2407db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2408db2a4165SFrank Mandarino 	return 0;
2409db2a4165SFrank Mandarino }
2410db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2411db2a4165SFrank Mandarino 
2412db2a4165SFrank Mandarino /**
2413db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2414db2a4165SFrank Mandarino  * @_template: control template
2415db2a4165SFrank Mandarino  * @data: control private data
2416ac11a2b3SMark Brown  * @long_name: control long name
2417efb7ac3fSMark Brown  * @prefix: control name prefix
2418db2a4165SFrank Mandarino  *
2419db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2420db2a4165SFrank Mandarino  *
2421db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2422db2a4165SFrank Mandarino  */
2423db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2424efb7ac3fSMark Brown 				  void *data, char *long_name,
2425efb7ac3fSMark Brown 				  const char *prefix)
2426db2a4165SFrank Mandarino {
2427db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2428efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2429efb7ac3fSMark Brown 	char *name = NULL;
2430efb7ac3fSMark Brown 	int name_len;
2431db2a4165SFrank Mandarino 
2432db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2433db2a4165SFrank Mandarino 	template.index = 0;
2434db2a4165SFrank Mandarino 
2435efb7ac3fSMark Brown 	if (!long_name)
2436efb7ac3fSMark Brown 		long_name = template.name;
2437efb7ac3fSMark Brown 
2438efb7ac3fSMark Brown 	if (prefix) {
2439efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
2440efb7ac3fSMark Brown 		name = kmalloc(name_len, GFP_ATOMIC);
2441efb7ac3fSMark Brown 		if (!name)
2442efb7ac3fSMark Brown 			return NULL;
2443efb7ac3fSMark Brown 
2444efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2445efb7ac3fSMark Brown 
2446efb7ac3fSMark Brown 		template.name = name;
2447efb7ac3fSMark Brown 	} else {
2448efb7ac3fSMark Brown 		template.name = long_name;
2449efb7ac3fSMark Brown 	}
2450efb7ac3fSMark Brown 
2451efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2452efb7ac3fSMark Brown 
2453efb7ac3fSMark Brown 	kfree(name);
2454efb7ac3fSMark Brown 
2455efb7ac3fSMark Brown 	return kcontrol;
2456db2a4165SFrank Mandarino }
2457db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2458db2a4165SFrank Mandarino 
2459db2a4165SFrank Mandarino /**
24603e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
24613e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
24623e8e1952SIan Molton  * duplicating this code.
24633e8e1952SIan Molton  *
24643e8e1952SIan Molton  * @codec: codec to add controls to
24653e8e1952SIan Molton  * @controls: array of controls to add
24663e8e1952SIan Molton  * @num_controls: number of elements in the array
24673e8e1952SIan Molton  *
24683e8e1952SIan Molton  * Return 0 for success, else error.
24693e8e1952SIan Molton  */
24703e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
24713e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
24723e8e1952SIan Molton {
2473f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
24743e8e1952SIan Molton 	int err, i;
24753e8e1952SIan Molton 
24763e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
24773e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
2478efb7ac3fSMark Brown 		err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2479efb7ac3fSMark Brown 						     control->name,
2480efb7ac3fSMark Brown 						     codec->name_prefix));
24813e8e1952SIan Molton 		if (err < 0) {
2482082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2483efb7ac3fSMark Brown 				codec->name, control->name, err);
24843e8e1952SIan Molton 			return err;
24853e8e1952SIan Molton 		}
24863e8e1952SIan Molton 	}
24873e8e1952SIan Molton 
24883e8e1952SIan Molton 	return 0;
24893e8e1952SIan Molton }
24903e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
24913e8e1952SIan Molton 
24923e8e1952SIan Molton /**
2493db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2494db2a4165SFrank Mandarino  * @kcontrol: mixer control
2495db2a4165SFrank Mandarino  * @uinfo: control element information
2496db2a4165SFrank Mandarino  *
2497db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2498db2a4165SFrank Mandarino  * mixer control.
2499db2a4165SFrank Mandarino  *
2500db2a4165SFrank Mandarino  * Returns 0 for success.
2501db2a4165SFrank Mandarino  */
2502db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2503db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2504db2a4165SFrank Mandarino {
2505db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2506db2a4165SFrank Mandarino 
2507db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2508db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2509f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2510db2a4165SFrank Mandarino 
2511f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2512f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2513db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2514db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2515db2a4165SFrank Mandarino 	return 0;
2516db2a4165SFrank Mandarino }
2517db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2518db2a4165SFrank Mandarino 
2519db2a4165SFrank Mandarino /**
2520db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2521db2a4165SFrank Mandarino  * @kcontrol: mixer control
2522ac11a2b3SMark Brown  * @ucontrol: control element information
2523db2a4165SFrank Mandarino  *
2524db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2525db2a4165SFrank Mandarino  *
2526db2a4165SFrank Mandarino  * Returns 0 for success.
2527db2a4165SFrank Mandarino  */
2528db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2529db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2530db2a4165SFrank Mandarino {
2531db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2532db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
253346f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2534db2a4165SFrank Mandarino 
2535f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2536db2a4165SFrank Mandarino 		;
2537db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
25383ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
25393ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2540db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2541db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2542db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2543db2a4165SFrank Mandarino 
2544db2a4165SFrank Mandarino 	return 0;
2545db2a4165SFrank Mandarino }
2546db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2547db2a4165SFrank Mandarino 
2548db2a4165SFrank Mandarino /**
2549db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2550db2a4165SFrank Mandarino  * @kcontrol: mixer control
2551ac11a2b3SMark Brown  * @ucontrol: control element information
2552db2a4165SFrank Mandarino  *
2553db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2554db2a4165SFrank Mandarino  *
2555db2a4165SFrank Mandarino  * Returns 0 for success.
2556db2a4165SFrank Mandarino  */
2557db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2558db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2559db2a4165SFrank Mandarino {
2560db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2561db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
256246f5822fSDaniel Ribeiro 	unsigned int val;
256346f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2564db2a4165SFrank Mandarino 
2565f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2566db2a4165SFrank Mandarino 		;
2567f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2568db2a4165SFrank Mandarino 		return -EINVAL;
2569db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2570db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2571db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2572f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2573db2a4165SFrank Mandarino 			return -EINVAL;
2574db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2575db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2576db2a4165SFrank Mandarino 	}
2577db2a4165SFrank Mandarino 
25786c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2579db2a4165SFrank Mandarino }
2580db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2581db2a4165SFrank Mandarino 
2582db2a4165SFrank Mandarino /**
25832e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
25842e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
25852e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
25862e72f8e3SPeter Ujfalusi  *
25872e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
25882e72f8e3SPeter Ujfalusi  *
25892e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
25902e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
25912e72f8e3SPeter Ujfalusi  *
25922e72f8e3SPeter Ujfalusi  * Returns 0 for success.
25932e72f8e3SPeter Ujfalusi  */
25942e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
25952e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
25962e72f8e3SPeter Ujfalusi {
25972e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
259874155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
259946f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
26002e72f8e3SPeter Ujfalusi 
26012e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
26022e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
26032e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
26042e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
26052e72f8e3SPeter Ujfalusi 			break;
26062e72f8e3SPeter Ujfalusi 	}
26072e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
26082e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
26092e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
26102e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
26112e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
26122e72f8e3SPeter Ujfalusi 				break;
26132e72f8e3SPeter Ujfalusi 		}
26142e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
26152e72f8e3SPeter Ujfalusi 	}
26162e72f8e3SPeter Ujfalusi 
26172e72f8e3SPeter Ujfalusi 	return 0;
26182e72f8e3SPeter Ujfalusi }
26192e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
26202e72f8e3SPeter Ujfalusi 
26212e72f8e3SPeter Ujfalusi /**
26222e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
26232e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
26242e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
26252e72f8e3SPeter Ujfalusi  *
26262e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
26272e72f8e3SPeter Ujfalusi  *
26282e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
26292e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
26302e72f8e3SPeter Ujfalusi  *
26312e72f8e3SPeter Ujfalusi  * Returns 0 for success.
26322e72f8e3SPeter Ujfalusi  */
26332e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
26342e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
26352e72f8e3SPeter Ujfalusi {
26362e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
263774155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
263846f5822fSDaniel Ribeiro 	unsigned int val;
263946f5822fSDaniel Ribeiro 	unsigned int mask;
26402e72f8e3SPeter Ujfalusi 
26412e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
26422e72f8e3SPeter Ujfalusi 		return -EINVAL;
26432e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
26442e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
26452e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
26462e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
26472e72f8e3SPeter Ujfalusi 			return -EINVAL;
26482e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
26492e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
26502e72f8e3SPeter Ujfalusi 	}
26512e72f8e3SPeter Ujfalusi 
26526c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
26532e72f8e3SPeter Ujfalusi }
26542e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
26552e72f8e3SPeter Ujfalusi 
26562e72f8e3SPeter Ujfalusi /**
2657db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2658db2a4165SFrank Mandarino  * @kcontrol: mixer control
2659db2a4165SFrank Mandarino  * @uinfo: control element information
2660db2a4165SFrank Mandarino  *
2661db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2662db2a4165SFrank Mandarino  * single mixer.
2663db2a4165SFrank Mandarino  *
2664db2a4165SFrank Mandarino  * Returns 0 for success.
2665db2a4165SFrank Mandarino  */
2666db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2667db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2668db2a4165SFrank Mandarino {
2669db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2670db2a4165SFrank Mandarino 
2671db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2672db2a4165SFrank Mandarino 	uinfo->count = 1;
2673f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2674db2a4165SFrank Mandarino 
2675f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2676f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2677db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2678db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2679db2a4165SFrank Mandarino 	return 0;
2680db2a4165SFrank Mandarino }
2681db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2682db2a4165SFrank Mandarino 
2683db2a4165SFrank Mandarino /**
2684db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2685db2a4165SFrank Mandarino  * @kcontrol: mixer control
2686db2a4165SFrank Mandarino  * @uinfo: control element information
2687db2a4165SFrank Mandarino  *
2688db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2689db2a4165SFrank Mandarino  *
2690db2a4165SFrank Mandarino  * Returns 0 for success.
2691db2a4165SFrank Mandarino  */
2692db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2693db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2694db2a4165SFrank Mandarino {
2695a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2696db2a4165SFrank Mandarino 
2697fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2698a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2699a7a4ac86SPhilipp Zabel 	else
2700a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2701a7a4ac86SPhilipp Zabel 
2702db2a4165SFrank Mandarino 	uinfo->count = 1;
2703db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2704a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2705db2a4165SFrank Mandarino 	return 0;
2706db2a4165SFrank Mandarino }
2707db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2708db2a4165SFrank Mandarino 
2709db2a4165SFrank Mandarino /**
2710db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2711db2a4165SFrank Mandarino  * @kcontrol: mixer control
2712db2a4165SFrank Mandarino  * @uinfo: control element information
2713db2a4165SFrank Mandarino  *
2714db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2715db2a4165SFrank Mandarino  *
2716db2a4165SFrank Mandarino  * Returns 0 for success.
2717db2a4165SFrank Mandarino  */
2718db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2719db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2720db2a4165SFrank Mandarino {
27214eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27224eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2723d11bb4a9SPeter Ujfalusi 	int platform_max;
2724762b8df7SMark Brown 	unsigned int shift = mc->shift;
2725815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2726db2a4165SFrank Mandarino 
2727d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2728d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2729d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2730d11bb4a9SPeter Ujfalusi 
2731d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2732a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2733a7a4ac86SPhilipp Zabel 	else
2734a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2735a7a4ac86SPhilipp Zabel 
2736db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2737db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2738d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2739db2a4165SFrank Mandarino 	return 0;
2740db2a4165SFrank Mandarino }
2741db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2742db2a4165SFrank Mandarino 
2743db2a4165SFrank Mandarino /**
2744db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2745db2a4165SFrank Mandarino  * @kcontrol: mixer control
2746ac11a2b3SMark Brown  * @ucontrol: control element information
2747db2a4165SFrank Mandarino  *
2748db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2749db2a4165SFrank Mandarino  *
2750db2a4165SFrank Mandarino  * Returns 0 for success.
2751db2a4165SFrank Mandarino  */
2752db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2753db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2754db2a4165SFrank Mandarino {
27554eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27564eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2757db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2758815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2759815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2760815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
27614eaa9819SJon Smirl 	int max = mc->max;
2762815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2763815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2764db2a4165SFrank Mandarino 
2765db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2766db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2767db2a4165SFrank Mandarino 	if (shift != rshift)
2768db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2769db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2770db2a4165SFrank Mandarino 	if (invert) {
2771db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2772a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2773db2a4165SFrank Mandarino 		if (shift != rshift)
2774db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2775a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2776db2a4165SFrank Mandarino 	}
2777db2a4165SFrank Mandarino 
2778db2a4165SFrank Mandarino 	return 0;
2779db2a4165SFrank Mandarino }
2780db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2781db2a4165SFrank Mandarino 
2782db2a4165SFrank Mandarino /**
2783db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2784db2a4165SFrank Mandarino  * @kcontrol: mixer control
2785ac11a2b3SMark Brown  * @ucontrol: control element information
2786db2a4165SFrank Mandarino  *
2787db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2788db2a4165SFrank Mandarino  *
2789db2a4165SFrank Mandarino  * Returns 0 for success.
2790db2a4165SFrank Mandarino  */
2791db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2792db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2793db2a4165SFrank Mandarino {
27944eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27954eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2796db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2797815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2798815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2799815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
28004eaa9819SJon Smirl 	int max = mc->max;
2801815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2802815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
280346f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2804db2a4165SFrank Mandarino 
2805db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2806db2a4165SFrank Mandarino 	if (invert)
2807a7a4ac86SPhilipp Zabel 		val = max - val;
2808db2a4165SFrank Mandarino 	val_mask = mask << shift;
2809db2a4165SFrank Mandarino 	val = val << shift;
2810db2a4165SFrank Mandarino 	if (shift != rshift) {
2811db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2812db2a4165SFrank Mandarino 		if (invert)
2813a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2814db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2815db2a4165SFrank Mandarino 		val |= val2 << rshift;
2816db2a4165SFrank Mandarino 	}
28176c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2818db2a4165SFrank Mandarino }
2819db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2820db2a4165SFrank Mandarino 
2821db2a4165SFrank Mandarino /**
2822db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2823db2a4165SFrank Mandarino  * @kcontrol: mixer control
2824db2a4165SFrank Mandarino  * @uinfo: control element information
2825db2a4165SFrank Mandarino  *
2826db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2827db2a4165SFrank Mandarino  * spans 2 codec registers.
2828db2a4165SFrank Mandarino  *
2829db2a4165SFrank Mandarino  * Returns 0 for success.
2830db2a4165SFrank Mandarino  */
2831db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2832db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2833db2a4165SFrank Mandarino {
28344eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28354eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2836d11bb4a9SPeter Ujfalusi 	int platform_max;
2837db2a4165SFrank Mandarino 
2838d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2839d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2840d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2841d11bb4a9SPeter Ujfalusi 
2842d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2843a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2844a7a4ac86SPhilipp Zabel 	else
2845a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2846a7a4ac86SPhilipp Zabel 
2847db2a4165SFrank Mandarino 	uinfo->count = 2;
2848db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2849d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2850db2a4165SFrank Mandarino 	return 0;
2851db2a4165SFrank Mandarino }
2852db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2853db2a4165SFrank Mandarino 
2854db2a4165SFrank Mandarino /**
2855db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2856db2a4165SFrank Mandarino  * @kcontrol: mixer control
2857ac11a2b3SMark Brown  * @ucontrol: control element information
2858db2a4165SFrank Mandarino  *
2859db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2860db2a4165SFrank Mandarino  *
2861db2a4165SFrank Mandarino  * Returns 0 for success.
2862db2a4165SFrank Mandarino  */
2863db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2864db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2865db2a4165SFrank Mandarino {
28664eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28674eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2868db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2869815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2870815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2871815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
28724eaa9819SJon Smirl 	int max = mc->max;
2873815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2874815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2875db2a4165SFrank Mandarino 
2876db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2877db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2878db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2879db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2880db2a4165SFrank Mandarino 	if (invert) {
2881db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2882a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2883db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2884a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2885db2a4165SFrank Mandarino 	}
2886db2a4165SFrank Mandarino 
2887db2a4165SFrank Mandarino 	return 0;
2888db2a4165SFrank Mandarino }
2889db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2890db2a4165SFrank Mandarino 
2891db2a4165SFrank Mandarino /**
2892db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2893db2a4165SFrank Mandarino  * @kcontrol: mixer control
2894ac11a2b3SMark Brown  * @ucontrol: control element information
2895db2a4165SFrank Mandarino  *
2896db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2897db2a4165SFrank Mandarino  *
2898db2a4165SFrank Mandarino  * Returns 0 for success.
2899db2a4165SFrank Mandarino  */
2900db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2901db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2902db2a4165SFrank Mandarino {
29034eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29044eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2905db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2906815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2907815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2908815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
29094eaa9819SJon Smirl 	int max = mc->max;
2910815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2911815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2912db2a4165SFrank Mandarino 	int err;
291346f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2914db2a4165SFrank Mandarino 
2915db2a4165SFrank Mandarino 	val_mask = mask << shift;
2916db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2917db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2918db2a4165SFrank Mandarino 
2919db2a4165SFrank Mandarino 	if (invert) {
2920a7a4ac86SPhilipp Zabel 		val = max - val;
2921a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2922db2a4165SFrank Mandarino 	}
2923db2a4165SFrank Mandarino 
2924db2a4165SFrank Mandarino 	val = val << shift;
2925db2a4165SFrank Mandarino 	val2 = val2 << shift;
2926db2a4165SFrank Mandarino 
29276c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
29283ff3f64bSMark Brown 	if (err < 0)
2929db2a4165SFrank Mandarino 		return err;
2930db2a4165SFrank Mandarino 
29316c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2932db2a4165SFrank Mandarino 	return err;
2933db2a4165SFrank Mandarino }
2934db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2935db2a4165SFrank Mandarino 
2936e13ac2e9SMark Brown /**
2937e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2938e13ac2e9SMark Brown  * @kcontrol: mixer control
2939e13ac2e9SMark Brown  * @uinfo: control element information
2940e13ac2e9SMark Brown  *
2941e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2942e13ac2e9SMark Brown  *
2943e13ac2e9SMark Brown  * Returns 0 for success.
2944e13ac2e9SMark Brown  */
2945e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2946e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2947e13ac2e9SMark Brown {
29484eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29494eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2950d11bb4a9SPeter Ujfalusi 	int platform_max;
29514eaa9819SJon Smirl 	int min = mc->min;
2952e13ac2e9SMark Brown 
2953d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2954d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2955d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2956d11bb4a9SPeter Ujfalusi 
2957e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2958e13ac2e9SMark Brown 	uinfo->count = 2;
2959e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2960d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2961e13ac2e9SMark Brown 	return 0;
2962e13ac2e9SMark Brown }
2963e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2964e13ac2e9SMark Brown 
2965e13ac2e9SMark Brown /**
2966e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2967e13ac2e9SMark Brown  * @kcontrol: mixer control
2968ac11a2b3SMark Brown  * @ucontrol: control element information
2969e13ac2e9SMark Brown  *
2970e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2971e13ac2e9SMark Brown  *
2972e13ac2e9SMark Brown  * Returns 0 for success.
2973e13ac2e9SMark Brown  */
2974e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2975e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2976e13ac2e9SMark Brown {
29774eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29784eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2979e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2980815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
29814eaa9819SJon Smirl 	int min = mc->min;
2982e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2983e13ac2e9SMark Brown 
2984e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2985e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2986e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2987e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2988e13ac2e9SMark Brown 	return 0;
2989e13ac2e9SMark Brown }
2990e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2991e13ac2e9SMark Brown 
2992e13ac2e9SMark Brown /**
2993e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2994e13ac2e9SMark Brown  * @kcontrol: mixer control
2995ac11a2b3SMark Brown  * @ucontrol: control element information
2996e13ac2e9SMark Brown  *
2997e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2998e13ac2e9SMark Brown  *
2999e13ac2e9SMark Brown  * Returns 0 for success.
3000e13ac2e9SMark Brown  */
3001e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
3002e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
3003e13ac2e9SMark Brown {
30044eaa9819SJon Smirl 	struct soc_mixer_control *mc =
30054eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
3006e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3007815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
30084eaa9819SJon Smirl 	int min = mc->min;
300946f5822fSDaniel Ribeiro 	unsigned int val;
3010e13ac2e9SMark Brown 
3011e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
3012e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
3013e13ac2e9SMark Brown 
30146c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
3015e13ac2e9SMark Brown }
3016e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3017e13ac2e9SMark Brown 
30188c6529dbSLiam Girdwood /**
3019637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
3020637d3847SPeter Ujfalusi  *
3021637d3847SPeter Ujfalusi  * @codec: where to look for the control
3022637d3847SPeter Ujfalusi  * @name: Name of the control
3023637d3847SPeter Ujfalusi  * @max: new maximum limit
3024637d3847SPeter Ujfalusi  *
3025637d3847SPeter Ujfalusi  * Return 0 for success, else error.
3026637d3847SPeter Ujfalusi  */
3027637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
3028637d3847SPeter Ujfalusi 	const char *name, int max)
3029637d3847SPeter Ujfalusi {
3030f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
3031637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
3032637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
3033637d3847SPeter Ujfalusi 	int found = 0;
3034637d3847SPeter Ujfalusi 	int ret = -EINVAL;
3035637d3847SPeter Ujfalusi 
3036637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
3037637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
3038637d3847SPeter Ujfalusi 		return -EINVAL;
3039637d3847SPeter Ujfalusi 
3040637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
3041637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3042637d3847SPeter Ujfalusi 			found = 1;
3043637d3847SPeter Ujfalusi 			break;
3044637d3847SPeter Ujfalusi 		}
3045637d3847SPeter Ujfalusi 	}
3046637d3847SPeter Ujfalusi 	if (found) {
3047637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3048637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3049d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3050637d3847SPeter Ujfalusi 			ret = 0;
3051637d3847SPeter Ujfalusi 		}
3052637d3847SPeter Ujfalusi 	}
3053637d3847SPeter Ujfalusi 	return ret;
3054637d3847SPeter Ujfalusi }
3055637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3056637d3847SPeter Ujfalusi 
3057637d3847SPeter Ujfalusi /**
3058b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
3059b6f4bb38Sapatard@mandriva.com  *  mixer info callback
3060b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
3061b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
3062b6f4bb38Sapatard@mandriva.com  *
3063b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
3064b6f4bb38Sapatard@mandriva.com  */
3065b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3066b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
3067b6f4bb38Sapatard@mandriva.com {
3068b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
3069b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
3070b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
3071b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
3072b6f4bb38Sapatard@mandriva.com 
3073b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3074b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
3075b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
3076b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
3077b6f4bb38Sapatard@mandriva.com 
3078b6f4bb38Sapatard@mandriva.com 	return 0;
3079b6f4bb38Sapatard@mandriva.com }
3080b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
3081b6f4bb38Sapatard@mandriva.com 
3082b6f4bb38Sapatard@mandriva.com /**
3083b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
3084b6f4bb38Sapatard@mandriva.com  *  mixer get callback
3085b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
3086b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
3087b6f4bb38Sapatard@mandriva.com  *
3088b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
3089b6f4bb38Sapatard@mandriva.com  */
3090b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3091b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
3092b6f4bb38Sapatard@mandriva.com {
3093b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
3094b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
3095b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3096b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
3097b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
3098b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
3099b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
3100b6f4bb38Sapatard@mandriva.com 
310120630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
310220630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
3103b6f4bb38Sapatard@mandriva.com 	return 0;
3104b6f4bb38Sapatard@mandriva.com }
3105b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
3106b6f4bb38Sapatard@mandriva.com 
3107b6f4bb38Sapatard@mandriva.com /**
3108b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
3109b6f4bb38Sapatard@mandriva.com  *  mixer put callback
3110b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
3111b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
3112b6f4bb38Sapatard@mandriva.com  *
3113b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
3114b6f4bb38Sapatard@mandriva.com  */
3115b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3116b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
3117b6f4bb38Sapatard@mandriva.com {
3118b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
3119b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
3120b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3121b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
3122b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
3123b6f4bb38Sapatard@mandriva.com 	int ret;
3124b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
3125b6f4bb38Sapatard@mandriva.com 
3126b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
3127b6f4bb38Sapatard@mandriva.com 	val &= mask;
3128b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
3129b6f4bb38Sapatard@mandriva.com 	valr &= mask;
3130b6f4bb38Sapatard@mandriva.com 
3131b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
3132b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
3133b6f4bb38Sapatard@mandriva.com 
3134b6f4bb38Sapatard@mandriva.com 	ret = 0;
3135b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
3136b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
3137b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
3138f1df5aecSMark Brown 			return ret;
3139b6f4bb38Sapatard@mandriva.com 	}
3140b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
3141b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
3142b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
3143f1df5aecSMark Brown 			return ret;
3144b6f4bb38Sapatard@mandriva.com 	}
3145b6f4bb38Sapatard@mandriva.com 
3146b6f4bb38Sapatard@mandriva.com 	return 0;
3147b6f4bb38Sapatard@mandriva.com }
3148b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
3149b6f4bb38Sapatard@mandriva.com 
3150b6f4bb38Sapatard@mandriva.com /**
31518c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
31528c6529dbSLiam Girdwood  * @dai: DAI
31538c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
31548c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
31558c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
31568c6529dbSLiam Girdwood  *
31578c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
31588c6529dbSLiam Girdwood  */
31598c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
31608c6529dbSLiam Girdwood 	unsigned int freq, int dir)
31618c6529dbSLiam Girdwood {
3162f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3163f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3164ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3165ec4ee52aSMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id,
3166ec4ee52aSMark Brown 						      freq, dir);
31678c6529dbSLiam Girdwood 	else
31688c6529dbSLiam Girdwood 		return -EINVAL;
31698c6529dbSLiam Girdwood }
31708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
31718c6529dbSLiam Girdwood 
31728c6529dbSLiam Girdwood /**
3173ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3174ec4ee52aSMark Brown  * @codec: CODEC
3175ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3176ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3177ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3178ec4ee52aSMark Brown  *
3179ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3180ec4ee52aSMark Brown  */
3181ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3182ec4ee52aSMark Brown 	unsigned int freq, int dir)
3183ec4ee52aSMark Brown {
3184ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3185ec4ee52aSMark Brown 		return codec->driver->set_sysclk(codec, clk_id, freq, dir);
3186ec4ee52aSMark Brown 	else
3187ec4ee52aSMark Brown 		return -EINVAL;
3188ec4ee52aSMark Brown }
3189ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3190ec4ee52aSMark Brown 
3191ec4ee52aSMark Brown /**
31928c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
31938c6529dbSLiam Girdwood  * @dai: DAI
3194ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
31958c6529dbSLiam Girdwood  * @div: new clock divisor.
31968c6529dbSLiam Girdwood  *
31978c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
31988c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
31998c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
32008c6529dbSLiam Girdwood  */
32018c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
32028c6529dbSLiam Girdwood 	int div_id, int div)
32038c6529dbSLiam Girdwood {
3204f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3205f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
32068c6529dbSLiam Girdwood 	else
32078c6529dbSLiam Girdwood 		return -EINVAL;
32088c6529dbSLiam Girdwood }
32098c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
32108c6529dbSLiam Girdwood 
32118c6529dbSLiam Girdwood /**
32128c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
32138c6529dbSLiam Girdwood  * @dai: DAI
32148c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
321585488037SMark Brown  * @source: DAI specific source for the PLL
32168c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
32178c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
32188c6529dbSLiam Girdwood  *
32198c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
32208c6529dbSLiam Girdwood  */
322185488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
322285488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
32238c6529dbSLiam Girdwood {
3224f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3225f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
322685488037SMark Brown 					 freq_in, freq_out);
3227ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3228ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3229ec4ee52aSMark Brown 						   freq_in, freq_out);
32308c6529dbSLiam Girdwood 	else
32318c6529dbSLiam Girdwood 		return -EINVAL;
32328c6529dbSLiam Girdwood }
32338c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
32348c6529dbSLiam Girdwood 
3235ec4ee52aSMark Brown /*
3236ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3237ec4ee52aSMark Brown  * @codec: CODEC
3238ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3239ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3240ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3241ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3242ec4ee52aSMark Brown  *
3243ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3244ec4ee52aSMark Brown  */
3245ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3246ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3247ec4ee52aSMark Brown {
3248ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3249ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3250ec4ee52aSMark Brown 					      freq_in, freq_out);
3251ec4ee52aSMark Brown 	else
3252ec4ee52aSMark Brown 		return -EINVAL;
3253ec4ee52aSMark Brown }
3254ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3255ec4ee52aSMark Brown 
32568c6529dbSLiam Girdwood /**
32578c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
32588c6529dbSLiam Girdwood  * @dai: DAI
32598c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
32608c6529dbSLiam Girdwood  *
32618c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
32628c6529dbSLiam Girdwood  */
32638c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
32648c6529dbSLiam Girdwood {
3265f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
3266f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
32678c6529dbSLiam Girdwood 	else
32688c6529dbSLiam Girdwood 		return -EINVAL;
32698c6529dbSLiam Girdwood }
32708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
32718c6529dbSLiam Girdwood 
32728c6529dbSLiam Girdwood /**
32738c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
32748c6529dbSLiam Girdwood  * @dai: DAI
3275a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3276a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
32778c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3278a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
32798c6529dbSLiam Girdwood  *
32808c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
32818c6529dbSLiam Girdwood  * specific.
32828c6529dbSLiam Girdwood  */
32838c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3284a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
32858c6529dbSLiam Girdwood {
3286f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3287f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3288a5479e38SDaniel Ribeiro 				slots, slot_width);
32898c6529dbSLiam Girdwood 	else
32908c6529dbSLiam Girdwood 		return -EINVAL;
32918c6529dbSLiam Girdwood }
32928c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
32938c6529dbSLiam Girdwood 
32948c6529dbSLiam Girdwood /**
3295472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3296472df3cbSBarry Song  * @dai: DAI
3297472df3cbSBarry Song  * @tx_num: how many TX channels
3298472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3299472df3cbSBarry Song  *           0~num-1 uses
3300472df3cbSBarry Song  * @rx_num: how many RX channels
3301472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3302472df3cbSBarry Song  *           0~num-1 uses
3303472df3cbSBarry Song  *
3304472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3305472df3cbSBarry Song  */
3306472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3307472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3308472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3309472df3cbSBarry Song {
3310f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3311f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3312472df3cbSBarry Song 			rx_num, rx_slot);
3313472df3cbSBarry Song 	else
3314472df3cbSBarry Song 		return -EINVAL;
3315472df3cbSBarry Song }
3316472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3317472df3cbSBarry Song 
3318472df3cbSBarry Song /**
33198c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
33208c6529dbSLiam Girdwood  * @dai: DAI
33218c6529dbSLiam Girdwood  * @tristate: tristate enable
33228c6529dbSLiam Girdwood  *
33238c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
33248c6529dbSLiam Girdwood  */
33258c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
33268c6529dbSLiam Girdwood {
3327f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3328f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
33298c6529dbSLiam Girdwood 	else
33308c6529dbSLiam Girdwood 		return -EINVAL;
33318c6529dbSLiam Girdwood }
33328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
33338c6529dbSLiam Girdwood 
33348c6529dbSLiam Girdwood /**
33358c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
33368c6529dbSLiam Girdwood  * @dai: DAI
33378c6529dbSLiam Girdwood  * @mute: mute enable
33388c6529dbSLiam Girdwood  *
33398c6529dbSLiam Girdwood  * Mutes the DAI DAC.
33408c6529dbSLiam Girdwood  */
33418c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
33428c6529dbSLiam Girdwood {
3343f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3344f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
33458c6529dbSLiam Girdwood 	else
33468c6529dbSLiam Girdwood 		return -EINVAL;
33478c6529dbSLiam Girdwood }
33488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
33498c6529dbSLiam Girdwood 
3350c5af3a2eSMark Brown /**
3351c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3352c5af3a2eSMark Brown  *
3353ac11a2b3SMark Brown  * @card: Card to register
3354c5af3a2eSMark Brown  *
3355c5af3a2eSMark Brown  */
335670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3357c5af3a2eSMark Brown {
3358f0fba2adSLiam Girdwood 	int i;
3359f0fba2adSLiam Girdwood 
3360c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3361c5af3a2eSMark Brown 		return -EINVAL;
3362c5af3a2eSMark Brown 
3363ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3364ed77cc12SMark Brown 
3365111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3366111c6419SStephen Warren 
3367150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3368150dd2f8SVinod Koul 
33692eea392dSJarkko Nikula 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
33702eea392dSJarkko Nikula 			    (card->num_links + card->num_aux_devs),
3371f0fba2adSLiam Girdwood 			    GFP_KERNEL);
3372f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3373f0fba2adSLiam Girdwood 		return -ENOMEM;
33742eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3375f0fba2adSLiam Girdwood 
3376f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3377f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3378f0fba2adSLiam Girdwood 
3379c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3380c5af3a2eSMark Brown 	card->instantiated = 0;
3381f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3382c5af3a2eSMark Brown 
3383c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3384c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
3385435c5e25SMark Brown 	snd_soc_instantiate_cards();
3386c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3387c5af3a2eSMark Brown 
3388c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3389c5af3a2eSMark Brown 
3390c5af3a2eSMark Brown 	return 0;
3391c5af3a2eSMark Brown }
339270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3393c5af3a2eSMark Brown 
3394c5af3a2eSMark Brown /**
3395c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3396c5af3a2eSMark Brown  *
3397ac11a2b3SMark Brown  * @card: Card to unregister
3398c5af3a2eSMark Brown  *
3399c5af3a2eSMark Brown  */
340070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3401c5af3a2eSMark Brown {
3402b0e26485SVinod Koul 	if (card->instantiated)
3403b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3404c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3405c5af3a2eSMark Brown 	list_del(&card->list);
3406c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3407c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3408c5af3a2eSMark Brown 
3409c5af3a2eSMark Brown 	return 0;
3410c5af3a2eSMark Brown }
341170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3412c5af3a2eSMark Brown 
3413f0fba2adSLiam Girdwood /*
3414f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3415f0fba2adSLiam Girdwood  * and sanitizing names.
3416f0fba2adSLiam Girdwood  */
34170b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3418f0fba2adSLiam Girdwood {
3419f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3420f0fba2adSLiam Girdwood 	int id1, id2;
3421f0fba2adSLiam Girdwood 
3422f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3423f0fba2adSLiam Girdwood 		return NULL;
3424f0fba2adSLiam Girdwood 
342558818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3426f0fba2adSLiam Girdwood 
3427f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3428f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3429f0fba2adSLiam Girdwood 	if (found) {
3430f0fba2adSLiam Girdwood 		/* get ID */
3431f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3432f0fba2adSLiam Girdwood 
3433f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3434f0fba2adSLiam Girdwood 			if (*id == -1)
3435f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3436f0fba2adSLiam Girdwood 		}
3437f0fba2adSLiam Girdwood 
3438f0fba2adSLiam Girdwood 	} else {
3439f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3440f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3441f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3442f0fba2adSLiam Girdwood 
3443f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
344405899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3445f0fba2adSLiam Girdwood 
3446f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3447f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
344858818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3449f0fba2adSLiam Girdwood 		} else
3450f0fba2adSLiam Girdwood 			*id = 0;
3451f0fba2adSLiam Girdwood 	}
3452f0fba2adSLiam Girdwood 
3453f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3454f0fba2adSLiam Girdwood }
3455f0fba2adSLiam Girdwood 
3456f0fba2adSLiam Girdwood /*
3457f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3458f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3459f0fba2adSLiam Girdwood  */
3460f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3461f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3462f0fba2adSLiam Girdwood {
3463f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
3464f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3465f0fba2adSLiam Girdwood 				dev_name(dev));
3466f0fba2adSLiam Girdwood 		return NULL;
3467f0fba2adSLiam Girdwood 	}
3468f0fba2adSLiam Girdwood 
3469f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3470f0fba2adSLiam Girdwood }
3471f0fba2adSLiam Girdwood 
34729115171aSMark Brown /**
34739115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
34749115171aSMark Brown  *
3475ac11a2b3SMark Brown  * @dai: DAI to register
34769115171aSMark Brown  */
3477f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3478f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
34799115171aSMark Brown {
3480f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
34819115171aSMark Brown 
3482f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
34839115171aSMark Brown 
3484f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3485f0fba2adSLiam Girdwood 	if (dai == NULL)
3486f0fba2adSLiam Girdwood 		return -ENOMEM;
34876335d055SEric Miao 
3488f0fba2adSLiam Girdwood 	/* create DAI component name */
3489f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3490f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3491f0fba2adSLiam Girdwood 		kfree(dai);
3492f0fba2adSLiam Girdwood 		return -ENOMEM;
3493f0fba2adSLiam Girdwood 	}
3494f0fba2adSLiam Girdwood 
3495f0fba2adSLiam Girdwood 	dai->dev = dev;
3496f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3497f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3498f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
34999115171aSMark Brown 
35009115171aSMark Brown 	mutex_lock(&client_mutex);
35019115171aSMark Brown 	list_add(&dai->list, &dai_list);
3502435c5e25SMark Brown 	snd_soc_instantiate_cards();
35039115171aSMark Brown 	mutex_unlock(&client_mutex);
35049115171aSMark Brown 
35059115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
35069115171aSMark Brown 
35079115171aSMark Brown 	return 0;
35089115171aSMark Brown }
35099115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
35109115171aSMark Brown 
35119115171aSMark Brown /**
35129115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
35139115171aSMark Brown  *
3514ac11a2b3SMark Brown  * @dai: DAI to unregister
35159115171aSMark Brown  */
3516f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
35179115171aSMark Brown {
3518f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3519f0fba2adSLiam Girdwood 
3520f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3521f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3522f0fba2adSLiam Girdwood 			goto found;
3523f0fba2adSLiam Girdwood 	}
3524f0fba2adSLiam Girdwood 	return;
3525f0fba2adSLiam Girdwood 
3526f0fba2adSLiam Girdwood found:
35279115171aSMark Brown 	mutex_lock(&client_mutex);
35289115171aSMark Brown 	list_del(&dai->list);
35299115171aSMark Brown 	mutex_unlock(&client_mutex);
35309115171aSMark Brown 
35319115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3532f0fba2adSLiam Girdwood 	kfree(dai->name);
3533f0fba2adSLiam Girdwood 	kfree(dai);
35349115171aSMark Brown }
35359115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
35369115171aSMark Brown 
35379115171aSMark Brown /**
35389115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
35399115171aSMark Brown  *
3540ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3541ac11a2b3SMark Brown  * @count: Number of DAIs
35429115171aSMark Brown  */
3543f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3544f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
35459115171aSMark Brown {
3546f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3547f0fba2adSLiam Girdwood 	int i, ret = 0;
3548f0fba2adSLiam Girdwood 
3549720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
35509115171aSMark Brown 
35519115171aSMark Brown 	for (i = 0; i < count; i++) {
3552f0fba2adSLiam Girdwood 
3553f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3554c46e0079SAxel Lin 		if (dai == NULL) {
3555c46e0079SAxel Lin 			ret = -ENOMEM;
3556c46e0079SAxel Lin 			goto err;
3557c46e0079SAxel Lin 		}
3558f0fba2adSLiam Girdwood 
3559f0fba2adSLiam Girdwood 		/* create DAI component name */
3560f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3561f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3562f0fba2adSLiam Girdwood 			kfree(dai);
3563f0fba2adSLiam Girdwood 			ret = -EINVAL;
35649115171aSMark Brown 			goto err;
35659115171aSMark Brown 		}
35669115171aSMark Brown 
3567f0fba2adSLiam Girdwood 		dai->dev = dev;
3568f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
35690f9141c9SMark Brown 		if (dai->driver->id)
35700f9141c9SMark Brown 			dai->id = dai->driver->id;
35710f9141c9SMark Brown 		else
35720f9141c9SMark Brown 			dai->id = i;
3573f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3574f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3575f0fba2adSLiam Girdwood 
3576f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3577f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3578f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3579f0fba2adSLiam Girdwood 
3580f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3581f0fba2adSLiam Girdwood 	}
3582f0fba2adSLiam Girdwood 
35831dcb4f38SAxel Lin 	mutex_lock(&client_mutex);
3584f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
35851dcb4f38SAxel Lin 	mutex_unlock(&client_mutex);
35869115171aSMark Brown 	return 0;
35879115171aSMark Brown 
35889115171aSMark Brown err:
35899115171aSMark Brown 	for (i--; i >= 0; i--)
3590f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
35919115171aSMark Brown 
35929115171aSMark Brown 	return ret;
35939115171aSMark Brown }
35949115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
35959115171aSMark Brown 
35969115171aSMark Brown /**
35979115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
35989115171aSMark Brown  *
3599ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3600ac11a2b3SMark Brown  * @count: Number of DAIs
36019115171aSMark Brown  */
3602f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
36039115171aSMark Brown {
36049115171aSMark Brown 	int i;
36059115171aSMark Brown 
36069115171aSMark Brown 	for (i = 0; i < count; i++)
3607f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
36089115171aSMark Brown }
36099115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
36109115171aSMark Brown 
361112a48a8cSMark Brown /**
361212a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
361312a48a8cSMark Brown  *
3614ac11a2b3SMark Brown  * @platform: platform to register
361512a48a8cSMark Brown  */
3616f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3617f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
361812a48a8cSMark Brown {
3619f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
362012a48a8cSMark Brown 
3621f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3622f0fba2adSLiam Girdwood 
3623f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3624f0fba2adSLiam Girdwood 	if (platform == NULL)
3625f0fba2adSLiam Girdwood 		return -ENOMEM;
3626f0fba2adSLiam Girdwood 
3627f0fba2adSLiam Girdwood 	/* create platform component name */
3628f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3629f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3630f0fba2adSLiam Girdwood 		kfree(platform);
3631f0fba2adSLiam Girdwood 		return -ENOMEM;
3632f0fba2adSLiam Girdwood 	}
3633f0fba2adSLiam Girdwood 
3634f0fba2adSLiam Girdwood 	platform->dev = dev;
3635f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
363612a48a8cSMark Brown 
363712a48a8cSMark Brown 	mutex_lock(&client_mutex);
363812a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3639435c5e25SMark Brown 	snd_soc_instantiate_cards();
364012a48a8cSMark Brown 	mutex_unlock(&client_mutex);
364112a48a8cSMark Brown 
364212a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
364312a48a8cSMark Brown 
364412a48a8cSMark Brown 	return 0;
364512a48a8cSMark Brown }
364612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
364712a48a8cSMark Brown 
364812a48a8cSMark Brown /**
364912a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
365012a48a8cSMark Brown  *
3651ac11a2b3SMark Brown  * @platform: platform to unregister
365212a48a8cSMark Brown  */
3653f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
365412a48a8cSMark Brown {
3655f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3656f0fba2adSLiam Girdwood 
3657f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3658f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3659f0fba2adSLiam Girdwood 			goto found;
3660f0fba2adSLiam Girdwood 	}
3661f0fba2adSLiam Girdwood 	return;
3662f0fba2adSLiam Girdwood 
3663f0fba2adSLiam Girdwood found:
366412a48a8cSMark Brown 	mutex_lock(&client_mutex);
366512a48a8cSMark Brown 	list_del(&platform->list);
366612a48a8cSMark Brown 	mutex_unlock(&client_mutex);
366712a48a8cSMark Brown 
366812a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3669f0fba2adSLiam Girdwood 	kfree(platform->name);
3670f0fba2adSLiam Girdwood 	kfree(platform);
367112a48a8cSMark Brown }
367212a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
367312a48a8cSMark Brown 
3674151ab22cSMark Brown static u64 codec_format_map[] = {
3675151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3676151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3677151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3678151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3679151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3680151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3681151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3682151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3683151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3684151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3685151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3686151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3687151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3688151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3689151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3690151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3691151ab22cSMark Brown };
3692151ab22cSMark Brown 
3693151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3694151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3695151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3696151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3697151ab22cSMark Brown  */
3698151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3699151ab22cSMark Brown {
3700151ab22cSMark Brown 	int i;
3701151ab22cSMark Brown 
3702151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3703151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3704151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3705151ab22cSMark Brown }
3706151ab22cSMark Brown 
37070d0cf00aSMark Brown /**
37080d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
37090d0cf00aSMark Brown  *
3710ac11a2b3SMark Brown  * @codec: codec to register
37110d0cf00aSMark Brown  */
3712f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3713001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3714001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3715001ae4c0SMark Brown 			   int num_dai)
37160d0cf00aSMark Brown {
37173335ddcaSDimitris Papastamos 	size_t reg_size;
3718f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3719f0fba2adSLiam Girdwood 	int ret, i;
3720151ab22cSMark Brown 
3721f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
37220d0cf00aSMark Brown 
3723f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3724f0fba2adSLiam Girdwood 	if (codec == NULL)
3725f0fba2adSLiam Girdwood 		return -ENOMEM;
37260d0cf00aSMark Brown 
3727f0fba2adSLiam Girdwood 	/* create CODEC component name */
3728f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3729f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3730f0fba2adSLiam Girdwood 		kfree(codec);
3731f0fba2adSLiam Girdwood 		return -ENOMEM;
3732151ab22cSMark Brown 	}
3733151ab22cSMark Brown 
373423bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
373523bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
373623bbce34SDimitris Papastamos 	else
373723bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
373823bbce34SDimitris Papastamos 
3739c3acec26SMark Brown 	codec->write = codec_drv->write;
3740c3acec26SMark Brown 	codec->read = codec_drv->read;
37411500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
37421500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
37438020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
3744ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3745ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3746ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3747474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
3748f0fba2adSLiam Girdwood 	codec->dev = dev;
3749f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3750f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3751f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3752f0fba2adSLiam Girdwood 
37537a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
37547a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
37553335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3756aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
37573335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
37583335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
37593335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
37603335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
37613335ddcaSDimitris Papastamos 		 * the cache.
37623335ddcaSDimitris Papastamos 		 */
37632aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
37643335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
37653335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
37663335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
37673335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3768fdf0f54dSDimitris Papastamos 				goto fail;
37697a30a3dbSDimitris Papastamos 			}
37707a30a3dbSDimitris Papastamos 		}
37712aa86323SDimitris Papastamos 	}
37727a30a3dbSDimitris Papastamos 
37731500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
37741500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
37751500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
37761500b7b5SDimitris Papastamos 		if (!codec->readable_register)
37771500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
37788020454cSDimitris Papastamos 		if (!codec->writable_register)
37798020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
37801500b7b5SDimitris Papastamos 	}
37811500b7b5SDimitris Papastamos 
3782f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3783f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3784f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3785f0fba2adSLiam Girdwood 	}
3786f0fba2adSLiam Girdwood 
378726b01ccdSMark Brown 	/* register any DAIs */
378826b01ccdSMark Brown 	if (num_dai) {
3789f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3790f0fba2adSLiam Girdwood 		if (ret < 0)
3791fdf0f54dSDimitris Papastamos 			goto fail;
379226b01ccdSMark Brown 	}
3793f0fba2adSLiam Girdwood 
37940d0cf00aSMark Brown 	mutex_lock(&client_mutex);
37950d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
37960d0cf00aSMark Brown 	snd_soc_instantiate_cards();
37970d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
37980d0cf00aSMark Brown 
37990d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
38000d0cf00aSMark Brown 	return 0;
3801f0fba2adSLiam Girdwood 
3802fdf0f54dSDimitris Papastamos fail:
38033335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
38043335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3805f0fba2adSLiam Girdwood 	kfree(codec->name);
3806f0fba2adSLiam Girdwood 	kfree(codec);
3807f0fba2adSLiam Girdwood 	return ret;
38080d0cf00aSMark Brown }
38090d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
38100d0cf00aSMark Brown 
38110d0cf00aSMark Brown /**
38120d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
38130d0cf00aSMark Brown  *
3814ac11a2b3SMark Brown  * @codec: codec to unregister
38150d0cf00aSMark Brown  */
3816f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
38170d0cf00aSMark Brown {
3818f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3819f0fba2adSLiam Girdwood 	int i;
3820f0fba2adSLiam Girdwood 
3821f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3822f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3823f0fba2adSLiam Girdwood 			goto found;
3824f0fba2adSLiam Girdwood 	}
3825f0fba2adSLiam Girdwood 	return;
3826f0fba2adSLiam Girdwood 
3827f0fba2adSLiam Girdwood found:
382826b01ccdSMark Brown 	if (codec->num_dai)
3829f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3830f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3831f0fba2adSLiam Girdwood 
38320d0cf00aSMark Brown 	mutex_lock(&client_mutex);
38330d0cf00aSMark Brown 	list_del(&codec->list);
38340d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
38350d0cf00aSMark Brown 
38360d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3837f0fba2adSLiam Girdwood 
38387a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
38393335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
38401aafcd4dSDimitris Papastamos 	kfree(codec->name);
3841f0fba2adSLiam Girdwood 	kfree(codec);
38420d0cf00aSMark Brown }
38430d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
38440d0cf00aSMark Brown 
3845c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3846db2a4165SFrank Mandarino {
3847384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
38488a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
38498a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3850384c89e2SMark Brown 		printk(KERN_WARNING
3851384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
38528a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3853384c89e2SMark Brown 	}
3854c3c5a19aSMark Brown 
38558a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3856c3c5a19aSMark Brown 				 &codec_list_fops))
3857c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3858c3c5a19aSMark Brown 
38598a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3860f3208780SMark Brown 				 &dai_list_fops))
3861f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
386219c7ac27SMark Brown 
38638a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
386419c7ac27SMark Brown 				 &platform_list_fops))
386519c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3866384c89e2SMark Brown #endif
3867384c89e2SMark Brown 
3868fb257897SMark Brown 	snd_soc_util_init();
3869fb257897SMark Brown 
3870db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3871db2a4165SFrank Mandarino }
38724abe8e16SMark Brown module_init(snd_soc_init);
3873db2a4165SFrank Mandarino 
38747d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3875db2a4165SFrank Mandarino {
3876fb257897SMark Brown 	snd_soc_util_exit();
3877fb257897SMark Brown 
3878384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
38798a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3880384c89e2SMark Brown #endif
3881db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3882db2a4165SFrank Mandarino }
3883db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3884db2a4165SFrank Mandarino 
3885db2a4165SFrank Mandarino /* Module information */
3886d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3887db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3888db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
38898b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3890