xref: /openbmc/linux/sound/soc/soc-core.c (revision 8ddab3f5107c3955e70e87a632d4d179ddba1189)
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
51384c89e2SMark Brown static struct dentry *debugfs_root;
52384c89e2SMark Brown #endif
53384c89e2SMark Brown 
54c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
55c5af3a2eSMark Brown static LIST_HEAD(card_list);
569115171aSMark Brown static LIST_HEAD(dai_list);
5712a48a8cSMark Brown static LIST_HEAD(platform_list);
580d0cf00aSMark Brown static LIST_HEAD(codec_list);
59c5af3a2eSMark Brown 
60c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card);
61c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card);
62f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
63c5af3a2eSMark Brown 
64db2a4165SFrank Mandarino /*
65db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
66db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
67db2a4165SFrank Mandarino  * between two audio tracks.
68db2a4165SFrank Mandarino  */
69db2a4165SFrank Mandarino static int pmdown_time = 5000;
70db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
71db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72db2a4165SFrank Mandarino 
732624d5faSMark Brown /* codec register dump */
742624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
752624d5faSMark Brown {
765164d74dSMark Brown 	int ret, i, step = 1, count = 0;
772624d5faSMark Brown 
78f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
792624d5faSMark Brown 		return 0;
802624d5faSMark Brown 
81f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
82f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
832624d5faSMark Brown 
842624d5faSMark Brown 	count += sprintf(buf, "%s registers\n", codec->name);
85f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
86f0fba2adSLiam Girdwood 		if (codec->driver->readable_register && !codec->driver->readable_register(i))
872624d5faSMark Brown 			continue;
882624d5faSMark Brown 
892624d5faSMark Brown 		count += sprintf(buf + count, "%2x: ", i);
902624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
912624d5faSMark Brown 			break;
922624d5faSMark Brown 
93f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
94f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
952624d5faSMark Brown 							 PAGE_SIZE - count, i);
965164d74dSMark Brown 		} else {
975164d74dSMark Brown 			/* If the read fails it's almost certainly due to
985164d74dSMark Brown 			 * the register being volatile and the device being
995164d74dSMark Brown 			 * powered off.
1005164d74dSMark Brown 			 */
101e4f078d8SDimitris Papastamos 			ret = snd_soc_read(codec, i);
1025164d74dSMark Brown 			if (ret >= 0)
1035164d74dSMark Brown 				count += snprintf(buf + count,
1045164d74dSMark Brown 						  PAGE_SIZE - count,
1055164d74dSMark Brown 						  "%4x", ret);
1062624d5faSMark Brown 			else
1075164d74dSMark Brown 				count += snprintf(buf + count,
1085164d74dSMark Brown 						  PAGE_SIZE - count,
1095164d74dSMark Brown 						  "<no data: %d>", ret);
1105164d74dSMark Brown 		}
1112624d5faSMark Brown 
1122624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1132624d5faSMark Brown 			break;
1142624d5faSMark Brown 
1152624d5faSMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1162624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1172624d5faSMark Brown 			break;
1182624d5faSMark Brown 	}
1192624d5faSMark Brown 
1202624d5faSMark Brown 	/* Truncate count; min() would cause a warning */
1212624d5faSMark Brown 	if (count >= PAGE_SIZE)
1222624d5faSMark Brown 		count = PAGE_SIZE - 1;
1232624d5faSMark Brown 
1242624d5faSMark Brown 	return count;
1252624d5faSMark Brown }
1262624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1272624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1282624d5faSMark Brown {
129f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
130f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
131f0fba2adSLiam Girdwood 
132f0fba2adSLiam Girdwood 	return soc_codec_reg_show(rtd->codec, buf);
1332624d5faSMark Brown }
1342624d5faSMark Brown 
1352624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1362624d5faSMark Brown 
137dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
138dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
139dbe21408SMark Brown {
140f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
141f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
142dbe21408SMark Brown 
143f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
144dbe21408SMark Brown }
145dbe21408SMark Brown 
146dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
147dbe21408SMark Brown 			       struct device_attribute *attr,
148dbe21408SMark Brown 			       const char *buf, size_t count)
149dbe21408SMark Brown {
150f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
151f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
152c593b520SMark Brown 	int ret;
153dbe21408SMark Brown 
154c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
155c593b520SMark Brown 	if (ret)
156c593b520SMark Brown 		return ret;
157dbe21408SMark Brown 
158dbe21408SMark Brown 	return count;
159dbe21408SMark Brown }
160dbe21408SMark Brown 
161dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
162dbe21408SMark Brown 
1632624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
1642624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
1652624d5faSMark Brown {
1662624d5faSMark Brown 	file->private_data = inode->i_private;
1672624d5faSMark Brown 	return 0;
1682624d5faSMark Brown }
1692624d5faSMark Brown 
1702624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1712624d5faSMark Brown 			       size_t count, loff_t *ppos)
1722624d5faSMark Brown {
1732624d5faSMark Brown 	ssize_t ret;
1742624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1752624d5faSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1762624d5faSMark Brown 	if (!buf)
1772624d5faSMark Brown 		return -ENOMEM;
1782624d5faSMark Brown 	ret = soc_codec_reg_show(codec, buf);
1792624d5faSMark Brown 	if (ret >= 0)
1802624d5faSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1812624d5faSMark Brown 	kfree(buf);
1822624d5faSMark Brown 	return ret;
1832624d5faSMark Brown }
1842624d5faSMark Brown 
1852624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
1862624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
1872624d5faSMark Brown {
1882624d5faSMark Brown 	char buf[32];
1892624d5faSMark Brown 	int buf_size;
1902624d5faSMark Brown 	char *start = buf;
1912624d5faSMark Brown 	unsigned long reg, value;
1922624d5faSMark Brown 	int step = 1;
1932624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1942624d5faSMark Brown 
1952624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
1962624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
1972624d5faSMark Brown 		return -EFAULT;
1982624d5faSMark Brown 	buf[buf_size] = 0;
1992624d5faSMark Brown 
200f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
201f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
2022624d5faSMark Brown 
2032624d5faSMark Brown 	while (*start == ' ')
2042624d5faSMark Brown 		start++;
2052624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
206f0fba2adSLiam Girdwood 	if ((reg >= codec->driver->reg_cache_size) || (reg % step))
2072624d5faSMark Brown 		return -EINVAL;
2082624d5faSMark Brown 	while (*start == ' ')
2092624d5faSMark Brown 		start++;
2102624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2112624d5faSMark Brown 		return -EINVAL;
212e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2132624d5faSMark Brown 	return buf_size;
2142624d5faSMark Brown }
2152624d5faSMark Brown 
2162624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2172624d5faSMark Brown 	.open = codec_reg_open_file,
2182624d5faSMark Brown 	.read = codec_reg_read_file,
2192624d5faSMark Brown 	.write = codec_reg_write_file,
2206038f373SArnd Bergmann 	.llseek = default_llseek,
2212624d5faSMark Brown };
2222624d5faSMark Brown 
2232624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2242624d5faSMark Brown {
225d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
226d6ce4cf3SJarkko Nikula 
2276ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
228d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2292624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2302624d5faSMark Brown 		printk(KERN_WARNING
2312624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2322624d5faSMark Brown 		return;
2332624d5faSMark Brown 	}
2342624d5faSMark Brown 
2352624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2362624d5faSMark Brown 						 codec->debugfs_codec_root,
2372624d5faSMark Brown 						 codec, &codec_reg_fops);
2382624d5faSMark Brown 	if (!codec->debugfs_reg)
2392624d5faSMark Brown 		printk(KERN_WARNING
2402624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
2412624d5faSMark Brown 
242ce6120ccSLiam Girdwood 	codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
2432624d5faSMark Brown 						 codec->debugfs_codec_root);
244ce6120ccSLiam Girdwood 	if (!codec->dapm.debugfs_dapm)
2452624d5faSMark Brown 		printk(KERN_WARNING
2462624d5faSMark Brown 		       "Failed to create DAPM debugfs directory\n");
2472624d5faSMark Brown 
248ce6120ccSLiam Girdwood 	snd_soc_dapm_debugfs_init(&codec->dapm);
2492624d5faSMark Brown }
2502624d5faSMark Brown 
2512624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2522624d5faSMark Brown {
2532624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2542624d5faSMark Brown }
2552624d5faSMark Brown 
256c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
257c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
258c3c5a19aSMark Brown {
259c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2602b194f9dSMark Brown 	ssize_t len, ret = 0;
261c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
262c3c5a19aSMark Brown 
263c3c5a19aSMark Brown 	if (!buf)
264c3c5a19aSMark Brown 		return -ENOMEM;
265c3c5a19aSMark Brown 
2662b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
2672b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
268c3c5a19aSMark Brown 			       codec->name);
2692b194f9dSMark Brown 		if (len >= 0)
2702b194f9dSMark Brown 			ret += len;
2712b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
2722b194f9dSMark Brown 			ret = PAGE_SIZE;
2732b194f9dSMark Brown 			break;
2742b194f9dSMark Brown 		}
2752b194f9dSMark Brown 	}
276c3c5a19aSMark Brown 
277c3c5a19aSMark Brown 	if (ret >= 0)
278c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
279c3c5a19aSMark Brown 
280c3c5a19aSMark Brown 	kfree(buf);
281c3c5a19aSMark Brown 
282c3c5a19aSMark Brown 	return ret;
283c3c5a19aSMark Brown }
284c3c5a19aSMark Brown 
285c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
286c3c5a19aSMark Brown 	.read = codec_list_read_file,
287c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
288c3c5a19aSMark Brown };
289c3c5a19aSMark Brown 
290f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
291f3208780SMark Brown 				  size_t count, loff_t *ppos)
292f3208780SMark Brown {
293f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2942b194f9dSMark Brown 	ssize_t len, ret = 0;
295f3208780SMark Brown 	struct snd_soc_dai *dai;
296f3208780SMark Brown 
297f3208780SMark Brown 	if (!buf)
298f3208780SMark Brown 		return -ENOMEM;
299f3208780SMark Brown 
3002b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3012b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3022b194f9dSMark Brown 		if (len >= 0)
3032b194f9dSMark Brown 			ret += len;
3042b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3052b194f9dSMark Brown 			ret = PAGE_SIZE;
3062b194f9dSMark Brown 			break;
3072b194f9dSMark Brown 		}
3082b194f9dSMark Brown 	}
309f3208780SMark Brown 
310f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
311f3208780SMark Brown 
312f3208780SMark Brown 	kfree(buf);
313f3208780SMark Brown 
314f3208780SMark Brown 	return ret;
315f3208780SMark Brown }
316f3208780SMark Brown 
317f3208780SMark Brown static const struct file_operations dai_list_fops = {
318f3208780SMark Brown 	.read = dai_list_read_file,
319f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
320f3208780SMark Brown };
321f3208780SMark Brown 
32219c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
32319c7ac27SMark Brown 				       char __user *user_buf,
32419c7ac27SMark Brown 				       size_t count, loff_t *ppos)
32519c7ac27SMark Brown {
32619c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3272b194f9dSMark Brown 	ssize_t len, ret = 0;
32819c7ac27SMark Brown 	struct snd_soc_platform *platform;
32919c7ac27SMark Brown 
33019c7ac27SMark Brown 	if (!buf)
33119c7ac27SMark Brown 		return -ENOMEM;
33219c7ac27SMark Brown 
3332b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3342b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
33519c7ac27SMark Brown 			       platform->name);
3362b194f9dSMark Brown 		if (len >= 0)
3372b194f9dSMark Brown 			ret += len;
3382b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3392b194f9dSMark Brown 			ret = PAGE_SIZE;
3402b194f9dSMark Brown 			break;
3412b194f9dSMark Brown 		}
3422b194f9dSMark Brown 	}
34319c7ac27SMark Brown 
34419c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
34519c7ac27SMark Brown 
34619c7ac27SMark Brown 	kfree(buf);
34719c7ac27SMark Brown 
34819c7ac27SMark Brown 	return ret;
34919c7ac27SMark Brown }
35019c7ac27SMark Brown 
35119c7ac27SMark Brown static const struct file_operations platform_list_fops = {
35219c7ac27SMark Brown 	.read = platform_list_read_file,
35319c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
35419c7ac27SMark Brown };
35519c7ac27SMark Brown 
356a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
357a6052154SJarkko Nikula {
358a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
359a6052154SJarkko Nikula 						     debugfs_root);
3603a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
361a6052154SJarkko Nikula 		dev_warn(card->dev,
362a6052154SJarkko Nikula 			 "ASoC: Failed to create codec debugfs directory\n");
3633a45b867SJarkko Nikula 		return;
3643a45b867SJarkko Nikula 	}
3653a45b867SJarkko Nikula 
3663a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
3673a45b867SJarkko Nikula 						    card->debugfs_card_root,
3683a45b867SJarkko Nikula 						    &card->pop_time);
3693a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
3703a45b867SJarkko Nikula 		dev_warn(card->dev,
3713a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
372a6052154SJarkko Nikula }
373a6052154SJarkko Nikula 
374a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
375a6052154SJarkko Nikula {
376a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
377a6052154SJarkko Nikula }
378a6052154SJarkko Nikula 
3792624d5faSMark Brown #else
3802624d5faSMark Brown 
3812624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
3822624d5faSMark Brown {
3832624d5faSMark Brown }
3842624d5faSMark Brown 
3852624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3862624d5faSMark Brown {
3872624d5faSMark Brown }
388b95fccbcSAxel Lin 
389b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
390b95fccbcSAxel Lin {
391b95fccbcSAxel Lin }
392b95fccbcSAxel Lin 
393b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
394b95fccbcSAxel Lin {
395b95fccbcSAxel Lin }
3962624d5faSMark Brown #endif
3972624d5faSMark Brown 
398db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
399db2a4165SFrank Mandarino /* unregister ac97 codec */
400db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
401db2a4165SFrank Mandarino {
402db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
403db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
404db2a4165SFrank Mandarino 	return 0;
405db2a4165SFrank Mandarino }
406db2a4165SFrank Mandarino 
407db2a4165SFrank Mandarino /* stop no dev release warning */
408db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
409db2a4165SFrank Mandarino 
410db2a4165SFrank Mandarino /* register ac97 codec to bus */
411db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
412db2a4165SFrank Mandarino {
413db2a4165SFrank Mandarino 	int err;
414db2a4165SFrank Mandarino 
415db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4164ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
417db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
418db2a4165SFrank Mandarino 
419bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
420f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
421db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
422db2a4165SFrank Mandarino 	if (err < 0) {
423db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
424db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
425db2a4165SFrank Mandarino 		return err;
426db2a4165SFrank Mandarino 	}
427db2a4165SFrank Mandarino 	return 0;
428db2a4165SFrank Mandarino }
429db2a4165SFrank Mandarino #endif
430db2a4165SFrank Mandarino 
43106f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
43206f409d7SMark Brown {
43306f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
434f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
435f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
43606f409d7SMark Brown 	int ret;
43706f409d7SMark Brown 
438f0fba2adSLiam Girdwood 	if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
439f0fba2adSLiam Girdwood 			rtd->dai_link->symmetric_rates) {
440f0fba2adSLiam Girdwood 		dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
441f0fba2adSLiam Girdwood 				rtd->rate);
44206f409d7SMark Brown 
44306f409d7SMark Brown 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
44406f409d7SMark Brown 						   SNDRV_PCM_HW_PARAM_RATE,
445f0fba2adSLiam Girdwood 						   rtd->rate,
446f0fba2adSLiam Girdwood 						   rtd->rate);
44706f409d7SMark Brown 		if (ret < 0) {
448f0fba2adSLiam Girdwood 			dev_err(&rtd->dev,
44906f409d7SMark Brown 				"Unable to apply rate symmetry constraint: %d\n", ret);
45006f409d7SMark Brown 			return ret;
45106f409d7SMark Brown 		}
45206f409d7SMark Brown 	}
45306f409d7SMark Brown 
45406f409d7SMark Brown 	return 0;
45506f409d7SMark Brown }
45606f409d7SMark Brown 
457db2a4165SFrank Mandarino /*
458db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
459db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
460db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
461db2a4165SFrank Mandarino  */
462db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
463db2a4165SFrank Mandarino {
464db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
465db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
466f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
467f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
468f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
469f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
470f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
471db2a4165SFrank Mandarino 	int ret = 0;
472db2a4165SFrank Mandarino 
473db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
474db2a4165SFrank Mandarino 
475db2a4165SFrank Mandarino 	/* startup the audio subsystem */
476f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->startup) {
477f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
478db2a4165SFrank Mandarino 		if (ret < 0) {
479db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
480cb666e5bSLiam Girdwood 				cpu_dai->name);
481db2a4165SFrank Mandarino 			goto out;
482db2a4165SFrank Mandarino 		}
483db2a4165SFrank Mandarino 	}
484db2a4165SFrank Mandarino 
485f0fba2adSLiam Girdwood 	if (platform->driver->ops->open) {
486f0fba2adSLiam Girdwood 		ret = platform->driver->ops->open(substream);
487db2a4165SFrank Mandarino 		if (ret < 0) {
488db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
489db2a4165SFrank Mandarino 			goto platform_err;
490db2a4165SFrank Mandarino 		}
491db2a4165SFrank Mandarino 	}
492db2a4165SFrank Mandarino 
493f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->startup) {
494f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->startup(substream, codec_dai);
495cb666e5bSLiam Girdwood 		if (ret < 0) {
496cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
497cb666e5bSLiam Girdwood 				codec_dai->name);
498cb666e5bSLiam Girdwood 			goto codec_dai_err;
499cb666e5bSLiam Girdwood 		}
500cb666e5bSLiam Girdwood 	}
501cb666e5bSLiam Girdwood 
502f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
503f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
504db2a4165SFrank Mandarino 		if (ret < 0) {
505f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
506db2a4165SFrank Mandarino 			goto machine_err;
507db2a4165SFrank Mandarino 		}
508db2a4165SFrank Mandarino 	}
509db2a4165SFrank Mandarino 
510a00f90f9SMark Brown 	/* Check that the codec and cpu DAIs are compatible */
511db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
512db2a4165SFrank Mandarino 		runtime->hw.rate_min =
513f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.rate_min,
514f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_min);
515db2a4165SFrank Mandarino 		runtime->hw.rate_max =
516f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.rate_max,
517f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_max);
518db2a4165SFrank Mandarino 		runtime->hw.channels_min =
519f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.channels_min,
520f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_min);
521db2a4165SFrank Mandarino 		runtime->hw.channels_max =
522f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.channels_max,
523f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_max);
524cb666e5bSLiam Girdwood 		runtime->hw.formats =
525f0fba2adSLiam Girdwood 			codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
526cb666e5bSLiam Girdwood 		runtime->hw.rates =
527f0fba2adSLiam Girdwood 			codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
528f0fba2adSLiam Girdwood 		if (codec_dai_drv->playback.rates
529d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
530f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->playback.rates;
531f0fba2adSLiam Girdwood 		if (cpu_dai_drv->playback.rates
532d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
533f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->playback.rates;
534db2a4165SFrank Mandarino 	} else {
535db2a4165SFrank Mandarino 		runtime->hw.rate_min =
536f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.rate_min,
537f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_min);
538db2a4165SFrank Mandarino 		runtime->hw.rate_max =
539f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.rate_max,
540f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_max);
541db2a4165SFrank Mandarino 		runtime->hw.channels_min =
542f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.channels_min,
543f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_min);
544db2a4165SFrank Mandarino 		runtime->hw.channels_max =
545f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.channels_max,
546f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_max);
547cb666e5bSLiam Girdwood 		runtime->hw.formats =
548f0fba2adSLiam Girdwood 			codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
549cb666e5bSLiam Girdwood 		runtime->hw.rates =
550f0fba2adSLiam Girdwood 			codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
551f0fba2adSLiam Girdwood 		if (codec_dai_drv->capture.rates
552d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
553f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->capture.rates;
554f0fba2adSLiam Girdwood 		if (cpu_dai_drv->capture.rates
555d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
556f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->capture.rates;
557db2a4165SFrank Mandarino 	}
558db2a4165SFrank Mandarino 
559db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
560db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
561db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
562cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
563bb1c0478SJassi Brar 		goto config_err;
564db2a4165SFrank Mandarino 	}
565db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
566db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
567cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
568bb1c0478SJassi Brar 		goto config_err;
569db2a4165SFrank Mandarino 	}
570db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
571db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
572cb666e5bSLiam Girdwood 				codec_dai->name, cpu_dai->name);
573bb1c0478SJassi Brar 		goto config_err;
574db2a4165SFrank Mandarino 	}
575db2a4165SFrank Mandarino 
57606f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
57706f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
57806f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
57906f409d7SMark Brown 		if (ret != 0)
580bb1c0478SJassi Brar 			goto config_err;
58106f409d7SMark Brown 	}
58206f409d7SMark Brown 
583f0fba2adSLiam Girdwood 	pr_debug("asoc: %s <-> %s info:\n",
584f0fba2adSLiam Girdwood 			codec_dai->name, cpu_dai->name);
585f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
586f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
587b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
588f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
589b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
590db2a4165SFrank Mandarino 
59114dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
592f0fba2adSLiam Girdwood 		cpu_dai->playback_active++;
593f0fba2adSLiam Girdwood 		codec_dai->playback_active++;
59414dc5734SJassi Brar 	} else {
595f0fba2adSLiam Girdwood 		cpu_dai->capture_active++;
596f0fba2adSLiam Girdwood 		codec_dai->capture_active++;
59714dc5734SJassi Brar 	}
59814dc5734SJassi Brar 	cpu_dai->active++;
59914dc5734SJassi Brar 	codec_dai->active++;
600f0fba2adSLiam Girdwood 	rtd->codec->active++;
601db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
602db2a4165SFrank Mandarino 	return 0;
603db2a4165SFrank Mandarino 
604bb1c0478SJassi Brar config_err:
605f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
606f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
607db2a4165SFrank Mandarino 
608bb1c0478SJassi Brar machine_err:
609f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
610f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
611bb1c0478SJassi Brar 
612cb666e5bSLiam Girdwood codec_dai_err:
613f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
614f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
615db2a4165SFrank Mandarino 
616db2a4165SFrank Mandarino platform_err:
617f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
618f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
619db2a4165SFrank Mandarino out:
620db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
621db2a4165SFrank Mandarino 	return ret;
622db2a4165SFrank Mandarino }
623db2a4165SFrank Mandarino 
624db2a4165SFrank Mandarino /*
6253a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
626db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
627db2a4165SFrank Mandarino  * due to DAPM power cycling.
628db2a4165SFrank Mandarino  */
6294484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
630db2a4165SFrank Mandarino {
631f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
632f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
633f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
634db2a4165SFrank Mandarino 
635db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
636db2a4165SFrank Mandarino 
637f24368c2SMark Brown 	pr_debug("pop wq checking: %s status: %s waiting: %s\n",
638f0fba2adSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
639f0fba2adSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
640db2a4165SFrank Mandarino 		 codec_dai->pop_wait ? "yes" : "no");
641db2a4165SFrank Mandarino 
642db2a4165SFrank Mandarino 	/* are we waiting on this codec DAI stream */
643db2a4165SFrank Mandarino 	if (codec_dai->pop_wait == 1) {
644db2a4165SFrank Mandarino 		codec_dai->pop_wait = 0;
645f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
646f0fba2adSLiam Girdwood 			codec_dai->driver->playback.stream_name,
647db2a4165SFrank Mandarino 			SND_SOC_DAPM_STREAM_STOP);
648db2a4165SFrank Mandarino 	}
649f0fba2adSLiam Girdwood 
650db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
651db2a4165SFrank Mandarino }
652db2a4165SFrank Mandarino 
653db2a4165SFrank Mandarino /*
654db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
655db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
656db2a4165SFrank Mandarino  * shutdown.
657db2a4165SFrank Mandarino  */
658db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
659db2a4165SFrank Mandarino {
660db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
661f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
662f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
663f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
664f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
665db2a4165SFrank Mandarino 
666db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
667db2a4165SFrank Mandarino 
66814dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
669f0fba2adSLiam Girdwood 		cpu_dai->playback_active--;
670f0fba2adSLiam Girdwood 		codec_dai->playback_active--;
67114dc5734SJassi Brar 	} else {
672f0fba2adSLiam Girdwood 		cpu_dai->capture_active--;
673f0fba2adSLiam Girdwood 		codec_dai->capture_active--;
674db2a4165SFrank Mandarino 	}
67514dc5734SJassi Brar 
67614dc5734SJassi Brar 	cpu_dai->active--;
67714dc5734SJassi Brar 	codec_dai->active--;
678db2a4165SFrank Mandarino 	codec->active--;
679db2a4165SFrank Mandarino 
6806010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
6816010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
6826010b2daSMark Brown 	 */
6836010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6846010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
6856010b2daSMark Brown 
686f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
687f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
688db2a4165SFrank Mandarino 
689f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
690f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
691db2a4165SFrank Mandarino 
692f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
693f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
694db2a4165SFrank Mandarino 
695f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
696f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
697f0fba2adSLiam Girdwood 	cpu_dai->runtime = NULL;
698db2a4165SFrank Mandarino 
699db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
700db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
701cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
702f0fba2adSLiam Girdwood 		schedule_delayed_work(&rtd->delayed_work,
703f0fba2adSLiam Girdwood 			msecs_to_jiffies(rtd->pmdown_time));
704db2a4165SFrank Mandarino 	} else {
705db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
706f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
707f0fba2adSLiam Girdwood 			codec_dai->driver->capture.stream_name,
7080b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
709db2a4165SFrank Mandarino 	}
710db2a4165SFrank Mandarino 
711db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
712db2a4165SFrank Mandarino 	return 0;
713db2a4165SFrank Mandarino }
714db2a4165SFrank Mandarino 
715db2a4165SFrank Mandarino /*
716db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
717db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
718db2a4165SFrank Mandarino  * it can refer to the runtime info.
719db2a4165SFrank Mandarino  */
720db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
721db2a4165SFrank Mandarino {
722db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
723f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
724f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
725f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
726db2a4165SFrank Mandarino 	int ret = 0;
727db2a4165SFrank Mandarino 
728db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
729cb666e5bSLiam Girdwood 
730f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
731f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
732cb666e5bSLiam Girdwood 		if (ret < 0) {
733cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
734cb666e5bSLiam Girdwood 			goto out;
735cb666e5bSLiam Girdwood 		}
736cb666e5bSLiam Girdwood 	}
737cb666e5bSLiam Girdwood 
738f0fba2adSLiam Girdwood 	if (platform->driver->ops->prepare) {
739f0fba2adSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
740a71a468aSLiam Girdwood 		if (ret < 0) {
741a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
742db2a4165SFrank Mandarino 			goto out;
743db2a4165SFrank Mandarino 		}
744a71a468aSLiam Girdwood 	}
745db2a4165SFrank Mandarino 
746f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->prepare) {
747f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->prepare(substream, codec_dai);
748a71a468aSLiam Girdwood 		if (ret < 0) {
749a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
750db2a4165SFrank Mandarino 			goto out;
751db2a4165SFrank Mandarino 		}
752a71a468aSLiam Girdwood 	}
753db2a4165SFrank Mandarino 
754f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->prepare) {
755f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
756cb666e5bSLiam Girdwood 		if (ret < 0) {
757cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
758cb666e5bSLiam Girdwood 			goto out;
759cb666e5bSLiam Girdwood 		}
760cb666e5bSLiam Girdwood 	}
761db2a4165SFrank Mandarino 
762d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
763d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
764d45f6219SMark Brown 	    codec_dai->pop_wait) {
765cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
766f0fba2adSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
767db2a4165SFrank Mandarino 	}
768db2a4165SFrank Mandarino 
769db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
770f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
771f0fba2adSLiam Girdwood 					  codec_dai->driver->playback.stream_name,
772db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
773db2a4165SFrank Mandarino 	else
774f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
775f0fba2adSLiam Girdwood 					  codec_dai->driver->capture.stream_name,
776db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
7778c6529dbSLiam Girdwood 
7788c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
779db2a4165SFrank Mandarino 
780db2a4165SFrank Mandarino out:
781db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
782db2a4165SFrank Mandarino 	return ret;
783db2a4165SFrank Mandarino }
784db2a4165SFrank Mandarino 
785db2a4165SFrank Mandarino /*
786db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
787db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
788db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
789db2a4165SFrank Mandarino  */
790db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
791db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
792db2a4165SFrank Mandarino {
793db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
794f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
795f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
796f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
797db2a4165SFrank Mandarino 	int ret = 0;
798db2a4165SFrank Mandarino 
799db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
800db2a4165SFrank Mandarino 
801f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
802f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
803cb666e5bSLiam Girdwood 		if (ret < 0) {
804cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
805db2a4165SFrank Mandarino 			goto out;
806db2a4165SFrank Mandarino 		}
807cb666e5bSLiam Girdwood 	}
808db2a4165SFrank Mandarino 
809f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_params) {
810f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
811db2a4165SFrank Mandarino 		if (ret < 0) {
812db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
813cb666e5bSLiam Girdwood 				codec_dai->name);
814cb666e5bSLiam Girdwood 			goto codec_err;
815db2a4165SFrank Mandarino 		}
816db2a4165SFrank Mandarino 	}
817db2a4165SFrank Mandarino 
818f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_params) {
819f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
820db2a4165SFrank Mandarino 		if (ret < 0) {
8213ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
822cb666e5bSLiam Girdwood 				cpu_dai->name);
823db2a4165SFrank Mandarino 			goto interface_err;
824db2a4165SFrank Mandarino 		}
825db2a4165SFrank Mandarino 	}
826db2a4165SFrank Mandarino 
827f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_params) {
828f0fba2adSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
829db2a4165SFrank Mandarino 		if (ret < 0) {
8303ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
831db2a4165SFrank Mandarino 				platform->name);
832db2a4165SFrank Mandarino 			goto platform_err;
833db2a4165SFrank Mandarino 		}
834db2a4165SFrank Mandarino 	}
835db2a4165SFrank Mandarino 
836f0fba2adSLiam Girdwood 	rtd->rate = params_rate(params);
83706f409d7SMark Brown 
838db2a4165SFrank Mandarino out:
839db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
840db2a4165SFrank Mandarino 	return ret;
841db2a4165SFrank Mandarino 
842db2a4165SFrank Mandarino platform_err:
843f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
844f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
845db2a4165SFrank Mandarino 
846db2a4165SFrank Mandarino interface_err:
847f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
848f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
849cb666e5bSLiam Girdwood 
850cb666e5bSLiam Girdwood codec_err:
851f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
852f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
853db2a4165SFrank Mandarino 
854db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
855db2a4165SFrank Mandarino 	return ret;
856db2a4165SFrank Mandarino }
857db2a4165SFrank Mandarino 
858db2a4165SFrank Mandarino /*
859a00f90f9SMark Brown  * Frees resources allocated by hw_params, can be called multiple times
860db2a4165SFrank Mandarino  */
861db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
862db2a4165SFrank Mandarino {
863db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
864f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
865f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
866f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
867f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
868db2a4165SFrank Mandarino 
869db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
870db2a4165SFrank Mandarino 
871db2a4165SFrank Mandarino 	/* apply codec digital mute */
8728c6529dbSLiam Girdwood 	if (!codec->active)
8738c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
874db2a4165SFrank Mandarino 
875db2a4165SFrank Mandarino 	/* free any machine hw params */
876f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
877f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
878db2a4165SFrank Mandarino 
879db2a4165SFrank Mandarino 	/* free any DMA resources */
880f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_free)
881f0fba2adSLiam Girdwood 		platform->driver->ops->hw_free(substream);
882db2a4165SFrank Mandarino 
883a00f90f9SMark Brown 	/* now free hw params for the DAIs  */
884f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
885f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
886db2a4165SFrank Mandarino 
887f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
888f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
889db2a4165SFrank Mandarino 
890db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
891db2a4165SFrank Mandarino 	return 0;
892db2a4165SFrank Mandarino }
893db2a4165SFrank Mandarino 
894db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
895db2a4165SFrank Mandarino {
896db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
897f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
898f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
899f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
900db2a4165SFrank Mandarino 	int ret;
901db2a4165SFrank Mandarino 
902f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->trigger) {
903f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
904db2a4165SFrank Mandarino 		if (ret < 0)
905db2a4165SFrank Mandarino 			return ret;
906db2a4165SFrank Mandarino 	}
907db2a4165SFrank Mandarino 
908f0fba2adSLiam Girdwood 	if (platform->driver->ops->trigger) {
909f0fba2adSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
910db2a4165SFrank Mandarino 		if (ret < 0)
911db2a4165SFrank Mandarino 			return ret;
912db2a4165SFrank Mandarino 	}
913db2a4165SFrank Mandarino 
914f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->trigger) {
915f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
916db2a4165SFrank Mandarino 		if (ret < 0)
917db2a4165SFrank Mandarino 			return ret;
918db2a4165SFrank Mandarino 	}
919db2a4165SFrank Mandarino 	return 0;
920db2a4165SFrank Mandarino }
921db2a4165SFrank Mandarino 
922377b6f62SPeter Ujfalusi /*
923377b6f62SPeter Ujfalusi  * soc level wrapper for pointer callback
924258020d0SPeter Ujfalusi  * If cpu_dai, codec_dai, platform driver has the delay callback, than
925258020d0SPeter Ujfalusi  * the runtime->delay will be updated accordingly.
926377b6f62SPeter Ujfalusi  */
927377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
928377b6f62SPeter Ujfalusi {
929377b6f62SPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
930f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
931f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
932f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
933258020d0SPeter Ujfalusi 	struct snd_pcm_runtime *runtime = substream->runtime;
934377b6f62SPeter Ujfalusi 	snd_pcm_uframes_t offset = 0;
935258020d0SPeter Ujfalusi 	snd_pcm_sframes_t delay = 0;
936377b6f62SPeter Ujfalusi 
937f0fba2adSLiam Girdwood 	if (platform->driver->ops->pointer)
938f0fba2adSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
939377b6f62SPeter Ujfalusi 
940f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->delay)
941f0fba2adSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
942258020d0SPeter Ujfalusi 
943f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->delay)
944f0fba2adSLiam Girdwood 		delay += codec_dai->driver->ops->delay(substream, codec_dai);
945258020d0SPeter Ujfalusi 
946f0fba2adSLiam Girdwood 	if (platform->driver->delay)
947f0fba2adSLiam Girdwood 		delay += platform->driver->delay(substream, codec_dai);
948258020d0SPeter Ujfalusi 
949258020d0SPeter Ujfalusi 	runtime->delay = delay;
950258020d0SPeter Ujfalusi 
951377b6f62SPeter Ujfalusi 	return offset;
952377b6f62SPeter Ujfalusi }
953377b6f62SPeter Ujfalusi 
954db2a4165SFrank Mandarino /* ASoC PCM operations */
955db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
956db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
957db2a4165SFrank Mandarino 	.close		= soc_codec_close,
958db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
959db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
960db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
961db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
962377b6f62SPeter Ujfalusi 	.pointer	= soc_pcm_pointer,
963db2a4165SFrank Mandarino };
964db2a4165SFrank Mandarino 
965db2a4165SFrank Mandarino #ifdef CONFIG_PM
966db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
967416356fcSMark Brown static int soc_suspend(struct device *dev)
968db2a4165SFrank Mandarino {
969416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
970f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
9712eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
972db2a4165SFrank Mandarino 	int i;
973db2a4165SFrank Mandarino 
974e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
975e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
976e3509ff0SDaniel Mack 	 */
977f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
978e3509ff0SDaniel Mack 		return 0;
979e3509ff0SDaniel Mack 
9806ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
9816ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
9826ed25978SAndy Green 	 */
983f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
984f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
985f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
9866ed25978SAndy Green 
9876ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
988f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
9896ed25978SAndy Green 
990a00f90f9SMark Brown 	/* mute any active DACs */
991f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
992f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
993f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
9943efab7dcSMark Brown 
995f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
9963efab7dcSMark Brown 			continue;
9973efab7dcSMark Brown 
998f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
999f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
1000db2a4165SFrank Mandarino 	}
1001db2a4165SFrank Mandarino 
10024ccab3e7SLiam Girdwood 	/* suspend all pcms */
1003f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1004f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10053efab7dcSMark Brown 			continue;
10063efab7dcSMark Brown 
1007f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
10083efab7dcSMark Brown 	}
10094ccab3e7SLiam Girdwood 
101087506549SMark Brown 	if (card->suspend_pre)
1011416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
1012db2a4165SFrank Mandarino 
1013f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1014f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1015f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
10163efab7dcSMark Brown 
1017f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10183efab7dcSMark Brown 			continue;
10193efab7dcSMark Brown 
1020f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1021f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1022f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
1023f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
1024f0fba2adSLiam Girdwood 			platform->suspended = 1;
1025f0fba2adSLiam Girdwood 		}
1026db2a4165SFrank Mandarino 	}
1027db2a4165SFrank Mandarino 
1028db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
1029f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
10305b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
1031ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1032f0fba2adSLiam Girdwood 	}
1033db2a4165SFrank Mandarino 
1034f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1035f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
10363efab7dcSMark Brown 
1037f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10383efab7dcSMark Brown 			continue;
10393efab7dcSMark Brown 
1040f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1041f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1042db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1043f0fba2adSLiam Girdwood 
1044f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1045f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1046db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1047db2a4165SFrank Mandarino 	}
1048db2a4165SFrank Mandarino 
1049f0fba2adSLiam Girdwood 	/* suspend all CODECs */
10502eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
10511547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
10521547aba9SMark Brown 		 * bias _ON and should not be suspended. */
1053f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
1054ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
10551547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
10561547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1057f0fba2adSLiam Girdwood 				codec->driver->suspend(codec, PMSG_SUSPEND);
1058f0fba2adSLiam Girdwood 				codec->suspended = 1;
10591547aba9SMark Brown 				break;
10601547aba9SMark Brown 			default:
1061f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
10621547aba9SMark Brown 				break;
10631547aba9SMark Brown 			}
10641547aba9SMark Brown 		}
1065f0fba2adSLiam Girdwood 	}
1066db2a4165SFrank Mandarino 
1067f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1068f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
10693efab7dcSMark Brown 
1070f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10713efab7dcSMark Brown 			continue;
10723efab7dcSMark Brown 
1073f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1074f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1075db2a4165SFrank Mandarino 	}
1076db2a4165SFrank Mandarino 
107787506549SMark Brown 	if (card->suspend_post)
1078416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
1079db2a4165SFrank Mandarino 
1080db2a4165SFrank Mandarino 	return 0;
1081db2a4165SFrank Mandarino }
1082db2a4165SFrank Mandarino 
10836ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
10846ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
10856ed25978SAndy Green  */
10866ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
1087db2a4165SFrank Mandarino {
1088f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
1089f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
1090f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
10912eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1092db2a4165SFrank Mandarino 	int i;
1093db2a4165SFrank Mandarino 
10946ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
10956ed25978SAndy Green 	 * so userspace apps are blocked from touching us
10966ed25978SAndy Green 	 */
10976ed25978SAndy Green 
1098f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
10996ed25978SAndy Green 
11009949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
1101f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
11029949788bSMark Brown 
110387506549SMark Brown 	if (card->resume_pre)
110487506549SMark Brown 		card->resume_pre(pdev);
1105db2a4165SFrank Mandarino 
1106f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
1107f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1108f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
11093efab7dcSMark Brown 
1110f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11113efab7dcSMark Brown 			continue;
11123efab7dcSMark Brown 
1113f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1114f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1115db2a4165SFrank Mandarino 	}
1116db2a4165SFrank Mandarino 
11172eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
11181547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
11191547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
11201547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
11211547aba9SMark Brown 		 */
1122f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
1123ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
11241547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
11251547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1126f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
1127f0fba2adSLiam Girdwood 				codec->suspended = 0;
11281547aba9SMark Brown 				break;
11291547aba9SMark Brown 			default:
1130f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
11311547aba9SMark Brown 				break;
11321547aba9SMark Brown 			}
11331547aba9SMark Brown 		}
1134f0fba2adSLiam Girdwood 	}
1135db2a4165SFrank Mandarino 
1136f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1137f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
11383efab7dcSMark Brown 
1139f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11403efab7dcSMark Brown 			continue;
11413efab7dcSMark Brown 
1142f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1143f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1144db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1145f0fba2adSLiam Girdwood 
1146f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1147f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1148db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1149db2a4165SFrank Mandarino 	}
1150db2a4165SFrank Mandarino 
11513ff3f64bSMark Brown 	/* unmute any active DACs */
1152f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1153f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1154f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
11553efab7dcSMark Brown 
1156f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11573efab7dcSMark Brown 			continue;
11583efab7dcSMark Brown 
1159f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1160f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
1161db2a4165SFrank Mandarino 	}
1162db2a4165SFrank Mandarino 
1163f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1164f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1165f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
11663efab7dcSMark Brown 
1167f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11683efab7dcSMark Brown 			continue;
11693efab7dcSMark Brown 
1170f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1171f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1172f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
1173f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
1174f0fba2adSLiam Girdwood 			platform->suspended = 0;
1175f0fba2adSLiam Girdwood 		}
1176db2a4165SFrank Mandarino 	}
1177db2a4165SFrank Mandarino 
117887506549SMark Brown 	if (card->resume_post)
117987506549SMark Brown 		card->resume_post(pdev);
1180db2a4165SFrank Mandarino 
1181f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
11826ed25978SAndy Green 
11836ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
1184f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
11856ed25978SAndy Green }
11866ed25978SAndy Green 
11876ed25978SAndy Green /* powers up audio subsystem after a suspend */
1188416356fcSMark Brown static int soc_resume(struct device *dev)
11896ed25978SAndy Green {
1190416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1191f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1192f0fba2adSLiam Girdwood 	int i;
1193b9dd94a8SPeter Ujfalusi 
119464ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
119564ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
119664ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
119764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
119864ab9baaSMark Brown 	 */
1199f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1200f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1201f0fba2adSLiam Girdwood 		if (cpu_dai->driver->ac97_control) {
1202f0fba2adSLiam Girdwood 			dev_dbg(dev, "Resuming AC97 immediately\n");
120364ab9baaSMark Brown 			soc_resume_deferred(&card->deferred_resume_work);
120464ab9baaSMark Brown 		} else {
1205f0fba2adSLiam Girdwood 			dev_dbg(dev, "Scheduling resume work\n");
12066308419aSMark Brown 			if (!schedule_work(&card->deferred_resume_work))
1207f0fba2adSLiam Girdwood 				dev_err(dev, "resume work item may be lost\n");
1208f0fba2adSLiam Girdwood 		}
120964ab9baaSMark Brown 	}
12106ed25978SAndy Green 
1211db2a4165SFrank Mandarino 	return 0;
1212db2a4165SFrank Mandarino }
1213db2a4165SFrank Mandarino #else
1214db2a4165SFrank Mandarino #define soc_suspend	NULL
1215db2a4165SFrank Mandarino #define soc_resume	NULL
1216db2a4165SFrank Mandarino #endif
1217db2a4165SFrank Mandarino 
121802a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
121902a06d30SBarry Song };
122002a06d30SBarry Song 
1221f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1222db2a4165SFrank Mandarino {
1223f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1224f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1225fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1226435c5e25SMark Brown 	struct snd_soc_platform *platform;
1227f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
1228db2a4165SFrank Mandarino 
1229f0fba2adSLiam Girdwood 	if (rtd->complete)
1230f0fba2adSLiam Girdwood 		return 1;
1231f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
12326308419aSMark Brown 
1233f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
1234f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
1235f0fba2adSLiam Girdwood 		goto find_codec;
1236435c5e25SMark Brown 	}
1237f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1238f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
1239f0fba2adSLiam Girdwood 		if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1240f0fba2adSLiam Girdwood 
1241f0fba2adSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
1242f0fba2adSLiam Girdwood 				return -ENODEV;
1243f0fba2adSLiam Girdwood 
1244f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
1245f0fba2adSLiam Girdwood 			goto find_codec;
1246f0fba2adSLiam Girdwood 		}
1247f0fba2adSLiam Girdwood 	}
1248f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
1249f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
1250f0fba2adSLiam Girdwood 
1251f0fba2adSLiam Girdwood find_codec:
1252f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
1253f0fba2adSLiam Girdwood 	if (rtd->codec) {
1254f0fba2adSLiam Girdwood 		goto find_platform;
1255c5af3a2eSMark Brown 	}
1256c5af3a2eSMark Brown 
1257f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
1258f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
1259f0fba2adSLiam Girdwood 		if (!strcmp(codec->name, dai_link->codec_name)) {
1260f0fba2adSLiam Girdwood 			rtd->codec = codec;
1261f0fba2adSLiam Girdwood 
1262f0fba2adSLiam Girdwood 			if (!try_module_get(codec->dev->driver->owner))
1263f0fba2adSLiam Girdwood 				return -ENODEV;
1264f0fba2adSLiam Girdwood 
1265f0fba2adSLiam Girdwood 			/* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1266f0fba2adSLiam Girdwood 			list_for_each_entry(codec_dai, &dai_list, list) {
1267f0fba2adSLiam Girdwood 				if (codec->dev == codec_dai->dev &&
1268f0fba2adSLiam Girdwood 						!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1269f0fba2adSLiam Girdwood 					rtd->codec_dai = codec_dai;
1270f0fba2adSLiam Girdwood 					goto find_platform;
1271435c5e25SMark Brown 				}
1272f0fba2adSLiam Girdwood 			}
1273f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1274f0fba2adSLiam Girdwood 					dai_link->codec_dai_name);
1275f0fba2adSLiam Girdwood 
1276f0fba2adSLiam Girdwood 			goto find_platform;
1277f0fba2adSLiam Girdwood 		}
1278f0fba2adSLiam Girdwood 	}
1279f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
1280f0fba2adSLiam Girdwood 			dai_link->codec_name);
1281f0fba2adSLiam Girdwood 
1282f0fba2adSLiam Girdwood find_platform:
1283f0fba2adSLiam Girdwood 	/* do we already have the CODEC DAI for this link ? */
1284f0fba2adSLiam Girdwood 	if (rtd->platform) {
1285f0fba2adSLiam Girdwood 		goto out;
1286f0fba2adSLiam Girdwood 	}
1287f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1288f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
1289f0fba2adSLiam Girdwood 		if (!strcmp(platform->name, dai_link->platform_name)) {
1290f0fba2adSLiam Girdwood 
1291f0fba2adSLiam Girdwood 			if (!try_module_get(platform->dev->driver->owner))
1292f0fba2adSLiam Girdwood 				return -ENODEV;
1293f0fba2adSLiam Girdwood 
1294f0fba2adSLiam Girdwood 			rtd->platform = platform;
1295f0fba2adSLiam Girdwood 			goto out;
1296f0fba2adSLiam Girdwood 		}
1297435c5e25SMark Brown 	}
12986b05eda6SMark Brown 
1299f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
1300f0fba2adSLiam Girdwood 			dai_link->platform_name);
1301f0fba2adSLiam Girdwood 	return 0;
1302f0fba2adSLiam Girdwood 
1303f0fba2adSLiam Girdwood out:
1304f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
1305f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1306f0fba2adSLiam Girdwood 		rtd->complete = 1;
1307f0fba2adSLiam Girdwood 		card->num_rtd++;
1308f0fba2adSLiam Girdwood 	}
1309f0fba2adSLiam Girdwood 	return 1;
13106b05eda6SMark Brown }
13116b05eda6SMark Brown 
1312589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
1313589c3563SJarkko Nikula {
1314589c3563SJarkko Nikula 	int err;
1315589c3563SJarkko Nikula 
1316589c3563SJarkko Nikula 	if (codec->driver->remove) {
1317589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
1318589c3563SJarkko Nikula 		if (err < 0)
1319589c3563SJarkko Nikula 			dev_err(codec->dev,
1320589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
1321589c3563SJarkko Nikula 				codec->name, err);
1322589c3563SJarkko Nikula 	}
1323589c3563SJarkko Nikula 
1324589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
1325589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
1326589c3563SJarkko Nikula 
1327589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
1328589c3563SJarkko Nikula 	codec->probed = 0;
1329589c3563SJarkko Nikula 	list_del(&codec->card_list);
1330589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
1331589c3563SJarkko Nikula }
1332589c3563SJarkko Nikula 
1333f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1334f0fba2adSLiam Girdwood {
1335f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1336f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1337f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1338f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1339f0fba2adSLiam Girdwood 	int err;
1340f0fba2adSLiam Girdwood 
1341f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1342f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
1343f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1344589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1345f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
1346f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
134702a06d30SBarry Song 	}
134802a06d30SBarry Song 
1349f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
1350f0fba2adSLiam Girdwood 	if (codec_dai && codec_dai->probed) {
1351f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1352f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1353f0fba2adSLiam Girdwood 			if (err < 0)
1354f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1355f0fba2adSLiam Girdwood 		}
1356f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1357f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1358f0fba2adSLiam Girdwood 	}
1359f0fba2adSLiam Girdwood 
1360f0fba2adSLiam Girdwood 	/* remove the platform */
1361f0fba2adSLiam Girdwood 	if (platform && platform->probed) {
1362f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
1363f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
1364f0fba2adSLiam Girdwood 			if (err < 0)
1365f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1366f0fba2adSLiam Girdwood 		}
1367f0fba2adSLiam Girdwood 		platform->probed = 0;
1368f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
1369f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
1370f0fba2adSLiam Girdwood 	}
1371f0fba2adSLiam Girdwood 
1372f0fba2adSLiam Girdwood 	/* remove the CODEC */
1373589c3563SJarkko Nikula 	if (codec && codec->probed)
1374589c3563SJarkko Nikula 		soc_remove_codec(codec);
1375f0fba2adSLiam Girdwood 
1376f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
1377f0fba2adSLiam Girdwood 	if (cpu_dai && cpu_dai->probed) {
1378f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1379f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1380f0fba2adSLiam Girdwood 			if (err < 0)
1381f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1382f0fba2adSLiam Girdwood 		}
1383f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1384f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1385f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
1386f0fba2adSLiam Girdwood 	}
1387f0fba2adSLiam Girdwood }
1388f0fba2adSLiam Girdwood 
1389ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1390ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1391ead9b919SJarkko Nikula {
1392ead9b919SJarkko Nikula 	int i;
1393ead9b919SJarkko Nikula 
1394ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1395ead9b919SJarkko Nikula 		return;
1396ead9b919SJarkko Nikula 
1397ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1398ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1399ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1400ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1401ead9b919SJarkko Nikula 			break;
1402ead9b919SJarkko Nikula 		}
1403ead9b919SJarkko Nikula 	}
1404ead9b919SJarkko Nikula }
1405ead9b919SJarkko Nikula 
1406589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1407589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1408589c3563SJarkko Nikula {
1409589c3563SJarkko Nikula 	int ret = 0;
1410589c3563SJarkko Nikula 
1411589c3563SJarkko Nikula 	codec->card = card;
1412589c3563SJarkko Nikula 	codec->dapm.card = card;
1413589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1414589c3563SJarkko Nikula 
1415589c3563SJarkko Nikula 	if (codec->driver->probe) {
1416589c3563SJarkko Nikula 		ret = codec->driver->probe(codec);
1417589c3563SJarkko Nikula 		if (ret < 0) {
1418589c3563SJarkko Nikula 			dev_err(codec->dev,
1419589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1420589c3563SJarkko Nikula 				codec->name, ret);
1421589c3563SJarkko Nikula 			return ret;
1422589c3563SJarkko Nikula 		}
1423589c3563SJarkko Nikula 	}
1424589c3563SJarkko Nikula 
1425589c3563SJarkko Nikula 	soc_init_codec_debugfs(codec);
1426589c3563SJarkko Nikula 
1427589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1428589c3563SJarkko Nikula 	codec->probed = 1;
1429589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
1430589c3563SJarkko Nikula 
1431589c3563SJarkko Nikula 	return ret;
1432589c3563SJarkko Nikula }
1433589c3563SJarkko Nikula 
1434f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1435f0fba2adSLiam Girdwood 
1436589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1437589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1438589c3563SJarkko Nikula 				   int num, int dailess)
1439589c3563SJarkko Nikula {
1440589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1441589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1442589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1443589c3563SJarkko Nikula 	const char *temp, *name;
1444589c3563SJarkko Nikula 	int ret = 0;
1445589c3563SJarkko Nikula 
1446589c3563SJarkko Nikula 	if (!dailess) {
1447589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1448589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1449589c3563SJarkko Nikula 		name = dai_link->name;
1450589c3563SJarkko Nikula 	} else {
1451589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1452589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1453589c3563SJarkko Nikula 		name = aux_dev->name;
1454589c3563SJarkko Nikula 	}
1455589c3563SJarkko Nikula 
1456589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1457589c3563SJarkko Nikula 	temp = codec->name_prefix;
1458589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1459589c3563SJarkko Nikula 
1460589c3563SJarkko Nikula 	/* do machine specific initialization */
1461589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1462589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1463589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1464589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1465589c3563SJarkko Nikula 	if (ret < 0) {
1466589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1467589c3563SJarkko Nikula 		return ret;
1468589c3563SJarkko Nikula 	}
1469589c3563SJarkko Nikula 	codec->name_prefix = temp;
1470589c3563SJarkko Nikula 
1471589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are instantiated */
1472589c3563SJarkko Nikula 	snd_soc_dapm_new_widgets(&codec->dapm);
1473589c3563SJarkko Nikula 	snd_soc_dapm_sync(&codec->dapm);
1474589c3563SJarkko Nikula 
1475589c3563SJarkko Nikula 	/* register the rtd device */
1476589c3563SJarkko Nikula 	rtd->codec = codec;
1477589c3563SJarkko Nikula 	rtd->card = card;
1478589c3563SJarkko Nikula 	rtd->dev.parent = card->dev;
1479589c3563SJarkko Nikula 	rtd->dev.release = rtd_release;
1480589c3563SJarkko Nikula 	rtd->dev.init_name = name;
1481589c3563SJarkko Nikula 	ret = device_register(&rtd->dev);
1482589c3563SJarkko Nikula 	if (ret < 0) {
1483589c3563SJarkko Nikula 		dev_err(card->dev,
1484589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1485589c3563SJarkko Nikula 		return ret;
1486589c3563SJarkko Nikula 	}
1487589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1488589c3563SJarkko Nikula 
1489589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
1490589c3563SJarkko Nikula 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1491589c3563SJarkko Nikula 	if (ret < 0)
1492589c3563SJarkko Nikula 		dev_err(codec->dev,
1493589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1494589c3563SJarkko Nikula 			ret);
1495589c3563SJarkko Nikula 
1496589c3563SJarkko Nikula 	/* add codec sysfs entries */
1497589c3563SJarkko Nikula 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1498589c3563SJarkko Nikula 	if (ret < 0)
1499589c3563SJarkko Nikula 		dev_err(codec->dev,
1500589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1501589c3563SJarkko Nikula 
1502589c3563SJarkko Nikula 	return 0;
1503589c3563SJarkko Nikula }
1504589c3563SJarkko Nikula 
1505f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1506f0fba2adSLiam Girdwood {
1507f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1508f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1509f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1510f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1511f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1512f0fba2adSLiam Girdwood 	int ret;
1513f0fba2adSLiam Girdwood 
1514f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1515f0fba2adSLiam Girdwood 
1516f0fba2adSLiam Girdwood 	/* config components */
1517f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1518f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1519f0fba2adSLiam Girdwood 	codec_dai->card = card;
1520f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1521f0fba2adSLiam Girdwood 
1522f0fba2adSLiam Girdwood 	/* set default power off timeout */
1523f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1524f0fba2adSLiam Girdwood 
1525f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
1526f0fba2adSLiam Girdwood 	if (!cpu_dai->probed) {
1527f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1528f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1529f0fba2adSLiam Girdwood 			if (ret < 0) {
1530f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1531f0fba2adSLiam Girdwood 						cpu_dai->name);
1532f0fba2adSLiam Girdwood 				return ret;
1533f0fba2adSLiam Girdwood 			}
1534f0fba2adSLiam Girdwood 		}
1535f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1536f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1537f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1538f0fba2adSLiam Girdwood 	}
1539f0fba2adSLiam Girdwood 
1540f0fba2adSLiam Girdwood 	/* probe the CODEC */
1541f0fba2adSLiam Girdwood 	if (!codec->probed) {
1542589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1543589c3563SJarkko Nikula 		if (ret < 0)
1544f0fba2adSLiam Girdwood 			return ret;
1545f0fba2adSLiam Girdwood 	}
1546f0fba2adSLiam Girdwood 
1547f0fba2adSLiam Girdwood 	/* probe the platform */
1548f0fba2adSLiam Girdwood 	if (!platform->probed) {
1549f0fba2adSLiam Girdwood 		if (platform->driver->probe) {
1550f0fba2adSLiam Girdwood 			ret = platform->driver->probe(platform);
1551f0fba2adSLiam Girdwood 			if (ret < 0) {
1552f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe platform %s\n",
1553f0fba2adSLiam Girdwood 						platform->name);
1554f0fba2adSLiam Girdwood 				return ret;
1555f0fba2adSLiam Girdwood 			}
1556f0fba2adSLiam Girdwood 		}
1557f0fba2adSLiam Girdwood 		/* mark platform as probed and add to card platform list */
1558f0fba2adSLiam Girdwood 		platform->probed = 1;
1559f0fba2adSLiam Girdwood 		list_add(&platform->card_list, &card->platform_dev_list);
1560f0fba2adSLiam Girdwood 	}
1561f0fba2adSLiam Girdwood 
1562f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
1563f0fba2adSLiam Girdwood 	if (!codec_dai->probed) {
1564f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1565f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1566f0fba2adSLiam Girdwood 			if (ret < 0) {
1567f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1568f0fba2adSLiam Girdwood 						codec_dai->name);
1569f0fba2adSLiam Girdwood 				return ret;
1570f0fba2adSLiam Girdwood 			}
1571f0fba2adSLiam Girdwood 		}
1572f0fba2adSLiam Girdwood 
1573f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1574f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1575f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1576f0fba2adSLiam Girdwood 	}
1577f0fba2adSLiam Girdwood 
1578f0fba2adSLiam Girdwood 	/* DAPM dai link stream work */
1579f0fba2adSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1580f0fba2adSLiam Girdwood 
1581589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1582589c3563SJarkko Nikula 	if (ret)
1583f0fba2adSLiam Girdwood 		return ret;
1584f0fba2adSLiam Girdwood 
1585f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1586f0fba2adSLiam Girdwood 	if (ret < 0)
1587f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1588f0fba2adSLiam Girdwood 
1589f0fba2adSLiam Girdwood 	/* create the pcm */
1590f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1591f0fba2adSLiam Girdwood 	if (ret < 0) {
1592f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1593f0fba2adSLiam Girdwood 		return ret;
1594f0fba2adSLiam Girdwood 	}
1595f0fba2adSLiam Girdwood 
1596f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1597f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1598f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1599f0fba2adSLiam Girdwood 
1600f0fba2adSLiam Girdwood 	return 0;
1601f0fba2adSLiam Girdwood }
1602f0fba2adSLiam Girdwood 
1603f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1604f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1605f0fba2adSLiam Girdwood {
1606f0fba2adSLiam Girdwood 	int ret;
1607f0fba2adSLiam Girdwood 
1608f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1609f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
16106b05eda6SMark Brown 	 */
1611f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
16120562f788SMika Westerberg 		/*
16130562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
16140562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
16150562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
16160562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
16170562f788SMika Westerberg 		 *
16180562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
16190562f788SMika Westerberg 		 */
16200562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
16210562f788SMika Westerberg 			return 0;
1622f0fba2adSLiam Girdwood 
1623f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1624f0fba2adSLiam Girdwood 		if (ret < 0) {
1625f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1626f0fba2adSLiam Girdwood 			return ret;
1627435c5e25SMark Brown 		}
1628435c5e25SMark Brown 
1629f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1630f0fba2adSLiam Girdwood 	}
1631f0fba2adSLiam Girdwood 	return 0;
1632f0fba2adSLiam Girdwood }
1633435c5e25SMark Brown 
1634f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1635f0fba2adSLiam Girdwood {
1636f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1637f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1638f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1639f0fba2adSLiam Girdwood 	}
1640f0fba2adSLiam Girdwood }
1641f0fba2adSLiam Girdwood #endif
1642435c5e25SMark Brown 
16432eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
16442eea392dSJarkko Nikula {
16452eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
16462eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1647676ad98aSJarkko Nikula 	int ret = -ENODEV;
16482eea392dSJarkko Nikula 
16492eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
16502eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
16512eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
16522eea392dSJarkko Nikula 			if (codec->probed) {
16532eea392dSJarkko Nikula 				dev_err(codec->dev,
16542eea392dSJarkko Nikula 					"asoc: codec already probed");
16552eea392dSJarkko Nikula 				ret = -EBUSY;
16562eea392dSJarkko Nikula 				goto out;
16572eea392dSJarkko Nikula 			}
1658676ad98aSJarkko Nikula 			goto found;
16592eea392dSJarkko Nikula 		}
16602eea392dSJarkko Nikula 	}
1661676ad98aSJarkko Nikula 	/* codec not found */
1662676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1663676ad98aSJarkko Nikula 	goto out;
16642eea392dSJarkko Nikula 
1665676ad98aSJarkko Nikula found:
16662eea392dSJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
16672eea392dSJarkko Nikula 		return -ENODEV;
16682eea392dSJarkko Nikula 
1669589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
16702eea392dSJarkko Nikula 	if (ret < 0)
1671589c3563SJarkko Nikula 		return ret;
16722eea392dSJarkko Nikula 
1673589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
16742eea392dSJarkko Nikula 
16752eea392dSJarkko Nikula out:
16762eea392dSJarkko Nikula 	return ret;
16772eea392dSJarkko Nikula }
16782eea392dSJarkko Nikula 
16792eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
16802eea392dSJarkko Nikula {
16812eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
16822eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
16832eea392dSJarkko Nikula 
16842eea392dSJarkko Nikula 	/* unregister the rtd device */
16852eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1686589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
16872eea392dSJarkko Nikula 		device_unregister(&rtd->dev);
16882eea392dSJarkko Nikula 		rtd->dev_registered = 0;
16892eea392dSJarkko Nikula 	}
16902eea392dSJarkko Nikula 
1691589c3563SJarkko Nikula 	if (codec && codec->probed)
1692589c3563SJarkko Nikula 		soc_remove_codec(codec);
16932eea392dSJarkko Nikula }
16942eea392dSJarkko Nikula 
1695fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1696fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1697fdf0f54dSDimitris Papastamos {
1698fdf0f54dSDimitris Papastamos 	int ret;
1699fdf0f54dSDimitris Papastamos 
1700fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1701fdf0f54dSDimitris Papastamos 		return 0;
1702fdf0f54dSDimitris Papastamos 
1703fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1704fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1705fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1706fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1707fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1708fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1709fdf0f54dSDimitris Papastamos 			ret);
1710fdf0f54dSDimitris Papastamos 		return ret;
1711fdf0f54dSDimitris Papastamos 	}
1712fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1713fdf0f54dSDimitris Papastamos 	return 0;
1714fdf0f54dSDimitris Papastamos }
1715fdf0f54dSDimitris Papastamos 
1716f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1717f0fba2adSLiam Girdwood {
1718f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
1719fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1720fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1721fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
1722f0fba2adSLiam Girdwood 	int ret, i;
172396dd3622SMark Brown 
1724f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1725f0fba2adSLiam Girdwood 
1726f0fba2adSLiam Girdwood 	if (card->instantiated) {
1727f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1728435c5e25SMark Brown 		return;
1729db2a4165SFrank Mandarino 	}
1730db2a4165SFrank Mandarino 
1731f0fba2adSLiam Girdwood 	/* bind DAIs */
1732f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1733f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1734f0fba2adSLiam Girdwood 
1735f0fba2adSLiam Girdwood 	/* bind completed ? */
1736f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1737f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1738f0fba2adSLiam Girdwood 		return;
1739db2a4165SFrank Mandarino 	}
1740db2a4165SFrank Mandarino 
1741fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1742fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1743fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1744fdf0f54dSDimitris Papastamos 			continue;
1745fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1746fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1747fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1748fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1749fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1750fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1751fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1752fdf0f54dSDimitris Papastamos 					break;
1753fdf0f54dSDimitris Papastamos 			}
1754fdf0f54dSDimitris Papastamos 		}
1755fdf0f54dSDimitris Papastamos 		if (i == card->num_configs) {
1756fdf0f54dSDimitris Papastamos 			/* no need to override the compress_type so
1757fdf0f54dSDimitris Papastamos 			 * go ahead and do the standard thing */
1758fdf0f54dSDimitris Papastamos 			ret = snd_soc_init_codec_cache(codec, 0);
1759fdf0f54dSDimitris Papastamos 			if (ret < 0) {
1760fdf0f54dSDimitris Papastamos 				mutex_unlock(&card->mutex);
1761fdf0f54dSDimitris Papastamos 				return;
1762fdf0f54dSDimitris Papastamos 			}
1763fdf0f54dSDimitris Papastamos 			continue;
1764fdf0f54dSDimitris Papastamos 		}
1765fdf0f54dSDimitris Papastamos 		/* override the compress_type with the one supplied in
1766fdf0f54dSDimitris Papastamos 		 * the machine driver */
1767fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1768fdf0f54dSDimitris Papastamos 		if (ret < 0) {
1769fdf0f54dSDimitris Papastamos 			mutex_unlock(&card->mutex);
1770fdf0f54dSDimitris Papastamos 			return;
1771fdf0f54dSDimitris Papastamos 		}
1772fdf0f54dSDimitris Papastamos 	}
1773fdf0f54dSDimitris Papastamos 
1774f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1775f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1776f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1777f0fba2adSLiam Girdwood 	if (ret < 0) {
1778f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1779f0fba2adSLiam Girdwood 			card->name);
1780f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1781f0fba2adSLiam Girdwood 		return;
1782db2a4165SFrank Mandarino 	}
1783f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1784db2a4165SFrank Mandarino 
17851301a964SRandy Dunlap #ifdef CONFIG_PM
17866ed25978SAndy Green 	/* deferred resume work */
17876308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
17881301a964SRandy Dunlap #endif
17896ed25978SAndy Green 
1790f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1791f0fba2adSLiam Girdwood 	if (card->probe) {
1792f0fba2adSLiam Girdwood 		ret = card->probe(pdev);
1793f0fba2adSLiam Girdwood 		if (ret < 0)
1794f0fba2adSLiam Girdwood 			goto card_probe_error;
1795f0fba2adSLiam Girdwood 	}
1796f0fba2adSLiam Girdwood 
1797fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1798f0fba2adSLiam Girdwood 		ret = soc_probe_dai_link(card, i);
1799fe3e78e0SMark Brown 		if (ret < 0) {
1800321de0d0SMark Brown 			pr_err("asoc: failed to instantiate card %s: %d\n",
1801321de0d0SMark Brown 			       card->name, ret);
1802f0fba2adSLiam Girdwood 			goto probe_dai_err;
1803fe3e78e0SMark Brown 		}
1804fe3e78e0SMark Brown 	}
1805fe3e78e0SMark Brown 
18062eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
18072eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
18082eea392dSJarkko Nikula 		if (ret < 0) {
18092eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
18102eea392dSJarkko Nikula 			       card->name, ret);
18112eea392dSJarkko Nikula 			goto probe_aux_dev_err;
18122eea392dSJarkko Nikula 		}
18132eea392dSJarkko Nikula 	}
18142eea392dSJarkko Nikula 
1815f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1816fe3e78e0SMark Brown 		 "%s",  card->name);
1817f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1818f0fba2adSLiam Girdwood 		 "%s", card->name);
1819fe3e78e0SMark Brown 
1820f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1821fe3e78e0SMark Brown 	if (ret < 0) {
1822f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
18236b3ed785SAxel Lin 		goto probe_aux_dev_err;
1824fe3e78e0SMark Brown 	}
1825fe3e78e0SMark Brown 
1826fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1827f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1828f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1829f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1830fe3e78e0SMark Brown 		if (ret < 0) {
1831f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
18326b3ed785SAxel Lin 			while (--i >= 0)
18337d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
18346b3ed785SAxel Lin 			goto probe_aux_dev_err;
1835fe3e78e0SMark Brown 		}
1836fe3e78e0SMark Brown 	}
1837fe3e78e0SMark Brown #endif
1838fe3e78e0SMark Brown 
1839435c5e25SMark Brown 	card->instantiated = 1;
1840f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1841435c5e25SMark Brown 	return;
1842db2a4165SFrank Mandarino 
18432eea392dSJarkko Nikula probe_aux_dev_err:
18442eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18452eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18462eea392dSJarkko Nikula 
1847f0fba2adSLiam Girdwood probe_dai_err:
1848f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1849f0fba2adSLiam Girdwood 		soc_remove_dai_link(card, i);
1850fe3e78e0SMark Brown 
1851f0fba2adSLiam Girdwood card_probe_error:
185287506549SMark Brown 	if (card->remove)
185387506549SMark Brown 		card->remove(pdev);
1854f0fba2adSLiam Girdwood 
1855f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1856f0fba2adSLiam Girdwood 
1857f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1858435c5e25SMark Brown }
1859db2a4165SFrank Mandarino 
1860435c5e25SMark Brown /*
1861421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1862435c5e25SMark Brown  * client_mutex.
1863435c5e25SMark Brown  */
1864435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1865435c5e25SMark Brown {
1866435c5e25SMark Brown 	struct snd_soc_card *card;
1867435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1868435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1869435c5e25SMark Brown }
1870435c5e25SMark Brown 
1871435c5e25SMark Brown /* probes a new socdev */
1872435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1873435c5e25SMark Brown {
1874f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1875435c5e25SMark Brown 	int ret = 0;
1876435c5e25SMark Brown 
1877435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1878435c5e25SMark Brown 	card->dev = &pdev->dev;
1879f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->dai_dev_list);
1880f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->codec_dev_list);
1881f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->platform_dev_list);
1882*8ddab3f5SJarkko Nikula 	INIT_LIST_HEAD(&card->paths);
1883f0fba2adSLiam Girdwood 
1884a6052154SJarkko Nikula 	soc_init_card_debugfs(card);
1885a6052154SJarkko Nikula 
1886435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1887435c5e25SMark Brown 	if (ret != 0) {
1888435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1889db2a4165SFrank Mandarino 		return ret;
1890db2a4165SFrank Mandarino 	}
1891db2a4165SFrank Mandarino 
1892435c5e25SMark Brown 	return 0;
1893435c5e25SMark Brown }
1894435c5e25SMark Brown 
1895db2a4165SFrank Mandarino /* removes a socdev */
1896db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1897db2a4165SFrank Mandarino {
1898f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1899db2a4165SFrank Mandarino 	int i;
1900db2a4165SFrank Mandarino 
1901b2dfa62cSGuennadi Liakhovetski 		if (card->instantiated) {
1902965ac42cSLiam Girdwood 
1903f0fba2adSLiam Girdwood 		/* make sure any delayed work runs */
1904f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++) {
1905f0fba2adSLiam Girdwood 			struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
19065b84ba26STejun Heo 			flush_delayed_work_sync(&rtd->delayed_work);
1907db2a4165SFrank Mandarino 		}
1908db2a4165SFrank Mandarino 
19092eea392dSJarkko Nikula 		/* remove auxiliary devices */
19102eea392dSJarkko Nikula 		for (i = 0; i < card->num_aux_devs; i++)
19112eea392dSJarkko Nikula 			soc_remove_aux_dev(card, i);
19122eea392dSJarkko Nikula 
1913f0fba2adSLiam Girdwood 		/* remove and free each DAI */
1914f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++)
1915f0fba2adSLiam Girdwood 			soc_remove_dai_link(card, i);
1916f0fba2adSLiam Girdwood 
1917a6052154SJarkko Nikula 		soc_cleanup_card_debugfs(card);
1918a6052154SJarkko Nikula 
1919f0fba2adSLiam Girdwood 		/* remove the card */
192087506549SMark Brown 		if (card->remove)
192187506549SMark Brown 			card->remove(pdev);
1922f0fba2adSLiam Girdwood 
1923f0fba2adSLiam Girdwood 		kfree(card->rtd);
1924f0fba2adSLiam Girdwood 		snd_card_free(card->snd_card);
1925b2dfa62cSGuennadi Liakhovetski 	}
1926c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1927db2a4165SFrank Mandarino 	return 0;
1928db2a4165SFrank Mandarino }
1929db2a4165SFrank Mandarino 
1930416356fcSMark Brown static int soc_poweroff(struct device *dev)
193151737470SMark Brown {
1932416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1933f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1934f0fba2adSLiam Girdwood 	int i;
193551737470SMark Brown 
193651737470SMark Brown 	if (!card->instantiated)
1937416356fcSMark Brown 		return 0;
193851737470SMark Brown 
193951737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
194051737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1941f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1942f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
19435b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1944f0fba2adSLiam Girdwood 	}
194551737470SMark Brown 
1946f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1947416356fcSMark Brown 
1948416356fcSMark Brown 	return 0;
194951737470SMark Brown }
195051737470SMark Brown 
195147145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1952416356fcSMark Brown 	.suspend = soc_suspend,
1953416356fcSMark Brown 	.resume = soc_resume,
1954416356fcSMark Brown 	.poweroff = soc_poweroff,
1955416356fcSMark Brown };
1956416356fcSMark Brown 
1957db2a4165SFrank Mandarino /* ASoC platform driver */
1958db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1959db2a4165SFrank Mandarino 	.driver		= {
1960db2a4165SFrank Mandarino 		.name		= "soc-audio",
19618b45a209SKay Sievers 		.owner		= THIS_MODULE,
1962416356fcSMark Brown 		.pm		= &soc_pm_ops,
1963db2a4165SFrank Mandarino 	},
1964db2a4165SFrank Mandarino 	.probe		= soc_probe,
1965db2a4165SFrank Mandarino 	.remove		= soc_remove,
1966db2a4165SFrank Mandarino };
1967db2a4165SFrank Mandarino 
1968db2a4165SFrank Mandarino /* create a new pcm */
1969f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1970db2a4165SFrank Mandarino {
1971f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1972f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1973f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1974f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1975db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1976db2a4165SFrank Mandarino 	char new_name[64];
1977db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1978db2a4165SFrank Mandarino 
1979db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
198040ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
1981f0fba2adSLiam Girdwood 			rtd->dai_link->stream_name, codec_dai->name, num);
1982db2a4165SFrank Mandarino 
1983f0fba2adSLiam Girdwood 	if (codec_dai->driver->playback.channels_min)
1984db2a4165SFrank Mandarino 		playback = 1;
1985f0fba2adSLiam Girdwood 	if (codec_dai->driver->capture.channels_min)
1986db2a4165SFrank Mandarino 		capture = 1;
1987db2a4165SFrank Mandarino 
1988f0fba2adSLiam Girdwood 	dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1989f0fba2adSLiam Girdwood 	ret = snd_pcm_new(rtd->card->snd_card, new_name,
1990f0fba2adSLiam Girdwood 			num, playback, capture, &pcm);
1991db2a4165SFrank Mandarino 	if (ret < 0) {
1992f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1993db2a4165SFrank Mandarino 		return ret;
1994db2a4165SFrank Mandarino 	}
1995db2a4165SFrank Mandarino 
1996f0fba2adSLiam Girdwood 	rtd->pcm = pcm;
1997db2a4165SFrank Mandarino 	pcm->private_data = rtd;
1998f0fba2adSLiam Girdwood 	soc_pcm_ops.mmap = platform->driver->ops->mmap;
1999f0fba2adSLiam Girdwood 	soc_pcm_ops.pointer = platform->driver->ops->pointer;
2000f0fba2adSLiam Girdwood 	soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2001f0fba2adSLiam Girdwood 	soc_pcm_ops.copy = platform->driver->ops->copy;
2002f0fba2adSLiam Girdwood 	soc_pcm_ops.silence = platform->driver->ops->silence;
2003f0fba2adSLiam Girdwood 	soc_pcm_ops.ack = platform->driver->ops->ack;
2004f0fba2adSLiam Girdwood 	soc_pcm_ops.page = platform->driver->ops->page;
2005db2a4165SFrank Mandarino 
2006db2a4165SFrank Mandarino 	if (playback)
2007db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2008db2a4165SFrank Mandarino 
2009db2a4165SFrank Mandarino 	if (capture)
2010db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2011db2a4165SFrank Mandarino 
2012f0fba2adSLiam Girdwood 	ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
2013db2a4165SFrank Mandarino 	if (ret < 0) {
2014db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
2015db2a4165SFrank Mandarino 		return ret;
2016db2a4165SFrank Mandarino 	}
2017db2a4165SFrank Mandarino 
2018f0fba2adSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
2019db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2020db2a4165SFrank Mandarino 		cpu_dai->name);
2021db2a4165SFrank Mandarino 	return ret;
2022db2a4165SFrank Mandarino }
2023db2a4165SFrank Mandarino 
2024096e49d5SMark Brown /**
2025096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
2026096e49d5SMark Brown  *
2027096e49d5SMark Brown  * @codec: CODEC to query.
2028096e49d5SMark Brown  * @reg: Register to query.
2029096e49d5SMark Brown  *
2030096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
2031096e49d5SMark Brown  */
2032096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
2033096e49d5SMark Brown {
2034f0fba2adSLiam Girdwood 	if (codec->driver->volatile_register)
2035f0fba2adSLiam Girdwood 		return codec->driver->volatile_register(reg);
2036096e49d5SMark Brown 	else
2037096e49d5SMark Brown 		return 0;
2038096e49d5SMark Brown }
2039096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2040096e49d5SMark Brown 
2041db2a4165SFrank Mandarino /**
2042db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2043db2a4165SFrank Mandarino  * @codec: audio codec
2044db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2045db2a4165SFrank Mandarino  * @num: AC97 codec number
2046db2a4165SFrank Mandarino  *
2047db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2048db2a4165SFrank Mandarino  */
2049db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2050db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2051db2a4165SFrank Mandarino {
2052db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2053db2a4165SFrank Mandarino 
2054db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2055db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2056db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2057db2a4165SFrank Mandarino 		return -ENOMEM;
2058db2a4165SFrank Mandarino 	}
2059db2a4165SFrank Mandarino 
2060db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2061db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2062db2a4165SFrank Mandarino 		kfree(codec->ac97);
2063db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2064db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2065db2a4165SFrank Mandarino 		return -ENOMEM;
2066db2a4165SFrank Mandarino 	}
2067db2a4165SFrank Mandarino 
2068db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2069db2a4165SFrank Mandarino 	codec->ac97->num = num;
20700562f788SMika Westerberg 
20710562f788SMika Westerberg 	/*
20720562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
20730562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
20740562f788SMika Westerberg 	 */
20750562f788SMika Westerberg 	codec->ac97_created = 1;
20760562f788SMika Westerberg 
2077db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2078db2a4165SFrank Mandarino 	return 0;
2079db2a4165SFrank Mandarino }
2080db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2081db2a4165SFrank Mandarino 
2082db2a4165SFrank Mandarino /**
2083db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2084db2a4165SFrank Mandarino  * @codec: audio codec
2085db2a4165SFrank Mandarino  *
2086db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2087db2a4165SFrank Mandarino  */
2088db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2089db2a4165SFrank Mandarino {
2090db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2091f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2092f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2093f0fba2adSLiam Girdwood #endif
2094db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2095db2a4165SFrank Mandarino 	kfree(codec->ac97);
2096db2a4165SFrank Mandarino 	codec->ac97 = NULL;
20970562f788SMika Westerberg 	codec->ac97_created = 0;
2098db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2099db2a4165SFrank Mandarino }
2100db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2101db2a4165SFrank Mandarino 
2102c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2103c3753707SMark Brown {
2104c3753707SMark Brown 	unsigned int ret;
2105c3753707SMark Brown 
2106c3acec26SMark Brown 	ret = codec->read(codec, reg);
2107c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2108a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2109c3753707SMark Brown 
2110c3753707SMark Brown 	return ret;
2111c3753707SMark Brown }
2112c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2113c3753707SMark Brown 
2114c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2115c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2116c3753707SMark Brown {
2117c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2118a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2119c3acec26SMark Brown 	return codec->write(codec, reg, val);
2120c3753707SMark Brown }
2121c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2122c3753707SMark Brown 
2123db2a4165SFrank Mandarino /**
2124db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2125db2a4165SFrank Mandarino  * @codec: audio codec
2126db2a4165SFrank Mandarino  * @reg: codec register
2127db2a4165SFrank Mandarino  * @mask: register mask
2128db2a4165SFrank Mandarino  * @value: new value
2129db2a4165SFrank Mandarino  *
2130db2a4165SFrank Mandarino  * Writes new register value.
2131db2a4165SFrank Mandarino  *
2132db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2133db2a4165SFrank Mandarino  */
2134db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
213546f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2136db2a4165SFrank Mandarino {
2137db2a4165SFrank Mandarino 	int change;
213846f5822fSDaniel Ribeiro 	unsigned int old, new;
2139db2a4165SFrank Mandarino 
2140db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2141db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2142db2a4165SFrank Mandarino 	change = old != new;
2143db2a4165SFrank Mandarino 	if (change)
2144db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
2145db2a4165SFrank Mandarino 
2146db2a4165SFrank Mandarino 	return change;
2147db2a4165SFrank Mandarino }
2148db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2149db2a4165SFrank Mandarino 
2150db2a4165SFrank Mandarino /**
21516c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
21526c508c62SEero Nurkkala  * @codec: audio codec
21536c508c62SEero Nurkkala  * @reg: codec register
21546c508c62SEero Nurkkala  * @mask: register mask
21556c508c62SEero Nurkkala  * @value: new value
21566c508c62SEero Nurkkala  *
21576c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
21586c508c62SEero Nurkkala  *
21596c508c62SEero Nurkkala  * Returns 1 for change else 0.
21606c508c62SEero Nurkkala  */
2161dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
21626c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
21636c508c62SEero Nurkkala 			       unsigned int value)
21646c508c62SEero Nurkkala {
21656c508c62SEero Nurkkala 	int change;
21666c508c62SEero Nurkkala 
21676c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
21686c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
21696c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
21706c508c62SEero Nurkkala 
21716c508c62SEero Nurkkala 	return change;
21726c508c62SEero Nurkkala }
2173dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
21746c508c62SEero Nurkkala 
21756c508c62SEero Nurkkala /**
2176db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2177db2a4165SFrank Mandarino  * @codec: audio codec
2178db2a4165SFrank Mandarino  * @reg: codec register
2179db2a4165SFrank Mandarino  * @mask: register mask
2180db2a4165SFrank Mandarino  * @value: new value
2181db2a4165SFrank Mandarino  *
2182db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2183db2a4165SFrank Mandarino  * different from the old value.
2184db2a4165SFrank Mandarino  *
2185db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2186db2a4165SFrank Mandarino  */
2187db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
218846f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2189db2a4165SFrank Mandarino {
2190db2a4165SFrank Mandarino 	int change;
219146f5822fSDaniel Ribeiro 	unsigned int old, new;
2192db2a4165SFrank Mandarino 
2193db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2194db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2195db2a4165SFrank Mandarino 	change = old != new;
2196db2a4165SFrank Mandarino 
2197db2a4165SFrank Mandarino 	return change;
2198db2a4165SFrank Mandarino }
2199db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2200db2a4165SFrank Mandarino 
2201db2a4165SFrank Mandarino /**
2202db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2203db2a4165SFrank Mandarino  * @substream: the pcm substream
2204db2a4165SFrank Mandarino  * @hw: the hardware parameters
2205db2a4165SFrank Mandarino  *
2206db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2207db2a4165SFrank Mandarino  */
2208db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2209db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2210db2a4165SFrank Mandarino {
2211db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2212db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2213db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2214db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2215db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2216db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2217db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2218db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2219db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2220db2a4165SFrank Mandarino 	return 0;
2221db2a4165SFrank Mandarino }
2222db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2223db2a4165SFrank Mandarino 
2224db2a4165SFrank Mandarino /**
2225db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2226db2a4165SFrank Mandarino  * @_template: control template
2227db2a4165SFrank Mandarino  * @data: control private data
2228ac11a2b3SMark Brown  * @long_name: control long name
2229db2a4165SFrank Mandarino  *
2230db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2231db2a4165SFrank Mandarino  *
2232db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2233db2a4165SFrank Mandarino  */
2234db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2235db2a4165SFrank Mandarino 	void *data, char *long_name)
2236db2a4165SFrank Mandarino {
2237db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2238db2a4165SFrank Mandarino 
2239db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2240db2a4165SFrank Mandarino 	if (long_name)
2241db2a4165SFrank Mandarino 		template.name = long_name;
2242db2a4165SFrank Mandarino 	template.index = 0;
2243db2a4165SFrank Mandarino 
2244db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
2245db2a4165SFrank Mandarino }
2246db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2247db2a4165SFrank Mandarino 
2248db2a4165SFrank Mandarino /**
22493e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
22503e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
22513e8e1952SIan Molton  * duplicating this code.
22523e8e1952SIan Molton  *
22533e8e1952SIan Molton  * @codec: codec to add controls to
22543e8e1952SIan Molton  * @controls: array of controls to add
22553e8e1952SIan Molton  * @num_controls: number of elements in the array
22563e8e1952SIan Molton  *
22573e8e1952SIan Molton  * Return 0 for success, else error.
22583e8e1952SIan Molton  */
22593e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
22603e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
22613e8e1952SIan Molton {
2262f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2263ead9b919SJarkko Nikula 	char prefixed_name[44], *name;
22643e8e1952SIan Molton 	int err, i;
22653e8e1952SIan Molton 
22663e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
22673e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
2268ead9b919SJarkko Nikula 		if (codec->name_prefix) {
2269ead9b919SJarkko Nikula 			snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2270ead9b919SJarkko Nikula 				 codec->name_prefix, control->name);
2271ead9b919SJarkko Nikula 			name = prefixed_name;
2272ead9b919SJarkko Nikula 		} else {
2273ead9b919SJarkko Nikula 			name = control->name;
2274ead9b919SJarkko Nikula 		}
2275ead9b919SJarkko Nikula 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
22763e8e1952SIan Molton 		if (err < 0) {
2277082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2278ead9b919SJarkko Nikula 				codec->name, name, err);
22793e8e1952SIan Molton 			return err;
22803e8e1952SIan Molton 		}
22813e8e1952SIan Molton 	}
22823e8e1952SIan Molton 
22833e8e1952SIan Molton 	return 0;
22843e8e1952SIan Molton }
22853e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
22863e8e1952SIan Molton 
22873e8e1952SIan Molton /**
2288db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2289db2a4165SFrank Mandarino  * @kcontrol: mixer control
2290db2a4165SFrank Mandarino  * @uinfo: control element information
2291db2a4165SFrank Mandarino  *
2292db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2293db2a4165SFrank Mandarino  * mixer control.
2294db2a4165SFrank Mandarino  *
2295db2a4165SFrank Mandarino  * Returns 0 for success.
2296db2a4165SFrank Mandarino  */
2297db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2298db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2299db2a4165SFrank Mandarino {
2300db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2301db2a4165SFrank Mandarino 
2302db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2303db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2304f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2305db2a4165SFrank Mandarino 
2306f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2307f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2308db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2309db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2310db2a4165SFrank Mandarino 	return 0;
2311db2a4165SFrank Mandarino }
2312db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2313db2a4165SFrank Mandarino 
2314db2a4165SFrank Mandarino /**
2315db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2316db2a4165SFrank Mandarino  * @kcontrol: mixer control
2317ac11a2b3SMark Brown  * @ucontrol: control element information
2318db2a4165SFrank Mandarino  *
2319db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2320db2a4165SFrank Mandarino  *
2321db2a4165SFrank Mandarino  * Returns 0 for success.
2322db2a4165SFrank Mandarino  */
2323db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2324db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2325db2a4165SFrank Mandarino {
2326db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2327db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
232846f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2329db2a4165SFrank Mandarino 
2330f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2331db2a4165SFrank Mandarino 		;
2332db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
23333ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
23343ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2335db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2336db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2337db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2338db2a4165SFrank Mandarino 
2339db2a4165SFrank Mandarino 	return 0;
2340db2a4165SFrank Mandarino }
2341db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2342db2a4165SFrank Mandarino 
2343db2a4165SFrank Mandarino /**
2344db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2345db2a4165SFrank Mandarino  * @kcontrol: mixer control
2346ac11a2b3SMark Brown  * @ucontrol: control element information
2347db2a4165SFrank Mandarino  *
2348db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2349db2a4165SFrank Mandarino  *
2350db2a4165SFrank Mandarino  * Returns 0 for success.
2351db2a4165SFrank Mandarino  */
2352db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2353db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2354db2a4165SFrank Mandarino {
2355db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2356db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
235746f5822fSDaniel Ribeiro 	unsigned int val;
235846f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2359db2a4165SFrank Mandarino 
2360f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2361db2a4165SFrank Mandarino 		;
2362f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2363db2a4165SFrank Mandarino 		return -EINVAL;
2364db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2365db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2366db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2367f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2368db2a4165SFrank Mandarino 			return -EINVAL;
2369db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2370db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2371db2a4165SFrank Mandarino 	}
2372db2a4165SFrank Mandarino 
23736c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2374db2a4165SFrank Mandarino }
2375db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2376db2a4165SFrank Mandarino 
2377db2a4165SFrank Mandarino /**
23782e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
23792e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23802e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23812e72f8e3SPeter Ujfalusi  *
23822e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
23832e72f8e3SPeter Ujfalusi  *
23842e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23852e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23862e72f8e3SPeter Ujfalusi  *
23872e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23882e72f8e3SPeter Ujfalusi  */
23892e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
23902e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23912e72f8e3SPeter Ujfalusi {
23922e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
239374155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
239446f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
23952e72f8e3SPeter Ujfalusi 
23962e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
23972e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
23982e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
23992e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
24002e72f8e3SPeter Ujfalusi 			break;
24012e72f8e3SPeter Ujfalusi 	}
24022e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
24032e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24042e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
24052e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
24062e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
24072e72f8e3SPeter Ujfalusi 				break;
24082e72f8e3SPeter Ujfalusi 		}
24092e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
24102e72f8e3SPeter Ujfalusi 	}
24112e72f8e3SPeter Ujfalusi 
24122e72f8e3SPeter Ujfalusi 	return 0;
24132e72f8e3SPeter Ujfalusi }
24142e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
24152e72f8e3SPeter Ujfalusi 
24162e72f8e3SPeter Ujfalusi /**
24172e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
24182e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
24192e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
24202e72f8e3SPeter Ujfalusi  *
24212e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
24222e72f8e3SPeter Ujfalusi  *
24232e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24242e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24252e72f8e3SPeter Ujfalusi  *
24262e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24272e72f8e3SPeter Ujfalusi  */
24282e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
24292e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
24302e72f8e3SPeter Ujfalusi {
24312e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
243274155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
243346f5822fSDaniel Ribeiro 	unsigned int val;
243446f5822fSDaniel Ribeiro 	unsigned int mask;
24352e72f8e3SPeter Ujfalusi 
24362e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
24372e72f8e3SPeter Ujfalusi 		return -EINVAL;
24382e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
24392e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
24402e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24412e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
24422e72f8e3SPeter Ujfalusi 			return -EINVAL;
24432e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
24442e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
24452e72f8e3SPeter Ujfalusi 	}
24462e72f8e3SPeter Ujfalusi 
24476c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
24482e72f8e3SPeter Ujfalusi }
24492e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
24502e72f8e3SPeter Ujfalusi 
24512e72f8e3SPeter Ujfalusi /**
2452db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2453db2a4165SFrank Mandarino  * @kcontrol: mixer control
2454db2a4165SFrank Mandarino  * @uinfo: control element information
2455db2a4165SFrank Mandarino  *
2456db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2457db2a4165SFrank Mandarino  * single mixer.
2458db2a4165SFrank Mandarino  *
2459db2a4165SFrank Mandarino  * Returns 0 for success.
2460db2a4165SFrank Mandarino  */
2461db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2462db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2463db2a4165SFrank Mandarino {
2464db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2465db2a4165SFrank Mandarino 
2466db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2467db2a4165SFrank Mandarino 	uinfo->count = 1;
2468f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2469db2a4165SFrank Mandarino 
2470f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2471f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2472db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2473db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2474db2a4165SFrank Mandarino 	return 0;
2475db2a4165SFrank Mandarino }
2476db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2477db2a4165SFrank Mandarino 
2478db2a4165SFrank Mandarino /**
2479db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2480db2a4165SFrank Mandarino  * @kcontrol: mixer control
2481db2a4165SFrank Mandarino  * @uinfo: control element information
2482db2a4165SFrank Mandarino  *
2483db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2484db2a4165SFrank Mandarino  *
2485db2a4165SFrank Mandarino  * Returns 0 for success.
2486db2a4165SFrank Mandarino  */
2487db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2488db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2489db2a4165SFrank Mandarino {
2490a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2491db2a4165SFrank Mandarino 
2492fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2493a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2494a7a4ac86SPhilipp Zabel 	else
2495a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2496a7a4ac86SPhilipp Zabel 
2497db2a4165SFrank Mandarino 	uinfo->count = 1;
2498db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2499a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2500db2a4165SFrank Mandarino 	return 0;
2501db2a4165SFrank Mandarino }
2502db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2503db2a4165SFrank Mandarino 
2504db2a4165SFrank Mandarino /**
2505db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2506db2a4165SFrank Mandarino  * @kcontrol: mixer control
2507db2a4165SFrank Mandarino  * @uinfo: control element information
2508db2a4165SFrank Mandarino  *
2509db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2510db2a4165SFrank Mandarino  *
2511db2a4165SFrank Mandarino  * Returns 0 for success.
2512db2a4165SFrank Mandarino  */
2513db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2514db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2515db2a4165SFrank Mandarino {
25164eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25174eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2518d11bb4a9SPeter Ujfalusi 	int platform_max;
2519762b8df7SMark Brown 	unsigned int shift = mc->shift;
2520815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2521db2a4165SFrank Mandarino 
2522d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2523d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2524d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2525d11bb4a9SPeter Ujfalusi 
2526d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2527a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2528a7a4ac86SPhilipp Zabel 	else
2529a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2530a7a4ac86SPhilipp Zabel 
2531db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2532db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2533d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2534db2a4165SFrank Mandarino 	return 0;
2535db2a4165SFrank Mandarino }
2536db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2537db2a4165SFrank Mandarino 
2538db2a4165SFrank Mandarino /**
2539db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2540db2a4165SFrank Mandarino  * @kcontrol: mixer control
2541ac11a2b3SMark Brown  * @ucontrol: control element information
2542db2a4165SFrank Mandarino  *
2543db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2544db2a4165SFrank Mandarino  *
2545db2a4165SFrank Mandarino  * Returns 0 for success.
2546db2a4165SFrank Mandarino  */
2547db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2548db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2549db2a4165SFrank Mandarino {
25504eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25514eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2552db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2553815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2554815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2555815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25564eaa9819SJon Smirl 	int max = mc->max;
2557815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2558815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2559db2a4165SFrank Mandarino 
2560db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2561db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2562db2a4165SFrank Mandarino 	if (shift != rshift)
2563db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2564db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2565db2a4165SFrank Mandarino 	if (invert) {
2566db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2567a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2568db2a4165SFrank Mandarino 		if (shift != rshift)
2569db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2570a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2571db2a4165SFrank Mandarino 	}
2572db2a4165SFrank Mandarino 
2573db2a4165SFrank Mandarino 	return 0;
2574db2a4165SFrank Mandarino }
2575db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2576db2a4165SFrank Mandarino 
2577db2a4165SFrank Mandarino /**
2578db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2579db2a4165SFrank Mandarino  * @kcontrol: mixer control
2580ac11a2b3SMark Brown  * @ucontrol: control element information
2581db2a4165SFrank Mandarino  *
2582db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2583db2a4165SFrank Mandarino  *
2584db2a4165SFrank Mandarino  * Returns 0 for success.
2585db2a4165SFrank Mandarino  */
2586db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2587db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2588db2a4165SFrank Mandarino {
25894eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25904eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2591db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2592815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2593815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2594815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25954eaa9819SJon Smirl 	int max = mc->max;
2596815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2597815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
259846f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2599db2a4165SFrank Mandarino 
2600db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2601db2a4165SFrank Mandarino 	if (invert)
2602a7a4ac86SPhilipp Zabel 		val = max - val;
2603db2a4165SFrank Mandarino 	val_mask = mask << shift;
2604db2a4165SFrank Mandarino 	val = val << shift;
2605db2a4165SFrank Mandarino 	if (shift != rshift) {
2606db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2607db2a4165SFrank Mandarino 		if (invert)
2608a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2609db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2610db2a4165SFrank Mandarino 		val |= val2 << rshift;
2611db2a4165SFrank Mandarino 	}
26126c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2613db2a4165SFrank Mandarino }
2614db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2615db2a4165SFrank Mandarino 
2616db2a4165SFrank Mandarino /**
2617db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2618db2a4165SFrank Mandarino  * @kcontrol: mixer control
2619db2a4165SFrank Mandarino  * @uinfo: control element information
2620db2a4165SFrank Mandarino  *
2621db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2622db2a4165SFrank Mandarino  * spans 2 codec registers.
2623db2a4165SFrank Mandarino  *
2624db2a4165SFrank Mandarino  * Returns 0 for success.
2625db2a4165SFrank Mandarino  */
2626db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2627db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2628db2a4165SFrank Mandarino {
26294eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26304eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2631d11bb4a9SPeter Ujfalusi 	int platform_max;
2632db2a4165SFrank Mandarino 
2633d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2634d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2635d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2636d11bb4a9SPeter Ujfalusi 
2637d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2638a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2639a7a4ac86SPhilipp Zabel 	else
2640a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2641a7a4ac86SPhilipp Zabel 
2642db2a4165SFrank Mandarino 	uinfo->count = 2;
2643db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2644d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2645db2a4165SFrank Mandarino 	return 0;
2646db2a4165SFrank Mandarino }
2647db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2648db2a4165SFrank Mandarino 
2649db2a4165SFrank Mandarino /**
2650db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2651db2a4165SFrank Mandarino  * @kcontrol: mixer control
2652ac11a2b3SMark Brown  * @ucontrol: control element information
2653db2a4165SFrank Mandarino  *
2654db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2655db2a4165SFrank Mandarino  *
2656db2a4165SFrank Mandarino  * Returns 0 for success.
2657db2a4165SFrank Mandarino  */
2658db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2659db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2660db2a4165SFrank Mandarino {
26614eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26624eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2663db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2664815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2665815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2666815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
26674eaa9819SJon Smirl 	int max = mc->max;
2668815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2669815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2670db2a4165SFrank Mandarino 
2671db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2672db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2673db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2674db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2675db2a4165SFrank Mandarino 	if (invert) {
2676db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2677a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2678db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2679a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2680db2a4165SFrank Mandarino 	}
2681db2a4165SFrank Mandarino 
2682db2a4165SFrank Mandarino 	return 0;
2683db2a4165SFrank Mandarino }
2684db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2685db2a4165SFrank Mandarino 
2686db2a4165SFrank Mandarino /**
2687db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2688db2a4165SFrank Mandarino  * @kcontrol: mixer control
2689ac11a2b3SMark Brown  * @ucontrol: control element information
2690db2a4165SFrank Mandarino  *
2691db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2692db2a4165SFrank Mandarino  *
2693db2a4165SFrank Mandarino  * Returns 0 for success.
2694db2a4165SFrank Mandarino  */
2695db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2696db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2697db2a4165SFrank Mandarino {
26984eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26994eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2700db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2701815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2702815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2703815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
27044eaa9819SJon Smirl 	int max = mc->max;
2705815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2706815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2707db2a4165SFrank Mandarino 	int err;
270846f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2709db2a4165SFrank Mandarino 
2710db2a4165SFrank Mandarino 	val_mask = mask << shift;
2711db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2712db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2713db2a4165SFrank Mandarino 
2714db2a4165SFrank Mandarino 	if (invert) {
2715a7a4ac86SPhilipp Zabel 		val = max - val;
2716a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2717db2a4165SFrank Mandarino 	}
2718db2a4165SFrank Mandarino 
2719db2a4165SFrank Mandarino 	val = val << shift;
2720db2a4165SFrank Mandarino 	val2 = val2 << shift;
2721db2a4165SFrank Mandarino 
27226c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
27233ff3f64bSMark Brown 	if (err < 0)
2724db2a4165SFrank Mandarino 		return err;
2725db2a4165SFrank Mandarino 
27266c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2727db2a4165SFrank Mandarino 	return err;
2728db2a4165SFrank Mandarino }
2729db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2730db2a4165SFrank Mandarino 
2731e13ac2e9SMark Brown /**
2732e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2733e13ac2e9SMark Brown  * @kcontrol: mixer control
2734e13ac2e9SMark Brown  * @uinfo: control element information
2735e13ac2e9SMark Brown  *
2736e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2737e13ac2e9SMark Brown  *
2738e13ac2e9SMark Brown  * Returns 0 for success.
2739e13ac2e9SMark Brown  */
2740e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2741e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2742e13ac2e9SMark Brown {
27434eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27444eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2745d11bb4a9SPeter Ujfalusi 	int platform_max;
27464eaa9819SJon Smirl 	int min = mc->min;
2747e13ac2e9SMark Brown 
2748d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2749d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2750d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2751d11bb4a9SPeter Ujfalusi 
2752e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2753e13ac2e9SMark Brown 	uinfo->count = 2;
2754e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2755d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2756e13ac2e9SMark Brown 	return 0;
2757e13ac2e9SMark Brown }
2758e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2759e13ac2e9SMark Brown 
2760e13ac2e9SMark Brown /**
2761e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2762e13ac2e9SMark Brown  * @kcontrol: mixer control
2763ac11a2b3SMark Brown  * @ucontrol: control element information
2764e13ac2e9SMark Brown  *
2765e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2766e13ac2e9SMark Brown  *
2767e13ac2e9SMark Brown  * Returns 0 for success.
2768e13ac2e9SMark Brown  */
2769e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2770e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2771e13ac2e9SMark Brown {
27724eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27734eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2774e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2775815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27764eaa9819SJon Smirl 	int min = mc->min;
2777e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2778e13ac2e9SMark Brown 
2779e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2780e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2781e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2782e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2783e13ac2e9SMark Brown 	return 0;
2784e13ac2e9SMark Brown }
2785e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2786e13ac2e9SMark Brown 
2787e13ac2e9SMark Brown /**
2788e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2789e13ac2e9SMark Brown  * @kcontrol: mixer control
2790ac11a2b3SMark Brown  * @ucontrol: control element information
2791e13ac2e9SMark Brown  *
2792e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2793e13ac2e9SMark Brown  *
2794e13ac2e9SMark Brown  * Returns 0 for success.
2795e13ac2e9SMark Brown  */
2796e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2797e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2798e13ac2e9SMark Brown {
27994eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28004eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2801e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2802815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
28034eaa9819SJon Smirl 	int min = mc->min;
280446f5822fSDaniel Ribeiro 	unsigned int val;
2805e13ac2e9SMark Brown 
2806e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2807e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2808e13ac2e9SMark Brown 
28096c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2810e13ac2e9SMark Brown }
2811e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2812e13ac2e9SMark Brown 
28138c6529dbSLiam Girdwood /**
2814637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2815637d3847SPeter Ujfalusi  *
2816637d3847SPeter Ujfalusi  * @codec: where to look for the control
2817637d3847SPeter Ujfalusi  * @name: Name of the control
2818637d3847SPeter Ujfalusi  * @max: new maximum limit
2819637d3847SPeter Ujfalusi  *
2820637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2821637d3847SPeter Ujfalusi  */
2822637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2823637d3847SPeter Ujfalusi 	const char *name, int max)
2824637d3847SPeter Ujfalusi {
2825f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2826637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2827637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2828637d3847SPeter Ujfalusi 	int found = 0;
2829637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2830637d3847SPeter Ujfalusi 
2831637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2832637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2833637d3847SPeter Ujfalusi 		return -EINVAL;
2834637d3847SPeter Ujfalusi 
2835637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2836637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2837637d3847SPeter Ujfalusi 			found = 1;
2838637d3847SPeter Ujfalusi 			break;
2839637d3847SPeter Ujfalusi 		}
2840637d3847SPeter Ujfalusi 	}
2841637d3847SPeter Ujfalusi 	if (found) {
2842637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2843637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2844d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2845637d3847SPeter Ujfalusi 			ret = 0;
2846637d3847SPeter Ujfalusi 		}
2847637d3847SPeter Ujfalusi 	}
2848637d3847SPeter Ujfalusi 	return ret;
2849637d3847SPeter Ujfalusi }
2850637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2851637d3847SPeter Ujfalusi 
2852637d3847SPeter Ujfalusi /**
2853b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2854b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2855b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2856b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2857b6f4bb38Sapatard@mandriva.com  *
2858b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2859b6f4bb38Sapatard@mandriva.com  */
2860b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2861b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2862b6f4bb38Sapatard@mandriva.com {
2863b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2864b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2865b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2866b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2867b6f4bb38Sapatard@mandriva.com 
2868b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2869b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2870b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2871b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2872b6f4bb38Sapatard@mandriva.com 
2873b6f4bb38Sapatard@mandriva.com 	return 0;
2874b6f4bb38Sapatard@mandriva.com }
2875b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2876b6f4bb38Sapatard@mandriva.com 
2877b6f4bb38Sapatard@mandriva.com /**
2878b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2879b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2880b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2881b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2882b6f4bb38Sapatard@mandriva.com  *
2883b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2884b6f4bb38Sapatard@mandriva.com  */
2885b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2886b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2887b6f4bb38Sapatard@mandriva.com {
2888b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2889b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2890b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2891b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2892b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2893b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2894b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2895b6f4bb38Sapatard@mandriva.com 
289620630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
289720630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2898b6f4bb38Sapatard@mandriva.com 	return 0;
2899b6f4bb38Sapatard@mandriva.com }
2900b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2901b6f4bb38Sapatard@mandriva.com 
2902b6f4bb38Sapatard@mandriva.com /**
2903b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2904b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2905b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2906b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2907b6f4bb38Sapatard@mandriva.com  *
2908b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2909b6f4bb38Sapatard@mandriva.com  */
2910b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2911b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2912b6f4bb38Sapatard@mandriva.com {
2913b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2914b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2915b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2916b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2917b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2918b6f4bb38Sapatard@mandriva.com 	int ret;
2919b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2920b6f4bb38Sapatard@mandriva.com 
2921b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2922b6f4bb38Sapatard@mandriva.com 	val &= mask;
2923b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2924b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2925b6f4bb38Sapatard@mandriva.com 
2926b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2927b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2928b6f4bb38Sapatard@mandriva.com 
2929b6f4bb38Sapatard@mandriva.com 	ret = 0;
2930b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2931b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2932b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2933f1df5aecSMark Brown 			return ret;
2934b6f4bb38Sapatard@mandriva.com 	}
2935b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2936b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2937b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2938f1df5aecSMark Brown 			return ret;
2939b6f4bb38Sapatard@mandriva.com 	}
2940b6f4bb38Sapatard@mandriva.com 
2941b6f4bb38Sapatard@mandriva.com 	return 0;
2942b6f4bb38Sapatard@mandriva.com }
2943b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2944b6f4bb38Sapatard@mandriva.com 
2945b6f4bb38Sapatard@mandriva.com /**
29468c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
29478c6529dbSLiam Girdwood  * @dai: DAI
29488c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
29498c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
29508c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
29518c6529dbSLiam Girdwood  *
29528c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
29538c6529dbSLiam Girdwood  */
29548c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
29558c6529dbSLiam Girdwood 	unsigned int freq, int dir)
29568c6529dbSLiam Girdwood {
2957f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2958f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
29598c6529dbSLiam Girdwood 	else
29608c6529dbSLiam Girdwood 		return -EINVAL;
29618c6529dbSLiam Girdwood }
29628c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
29638c6529dbSLiam Girdwood 
29648c6529dbSLiam Girdwood /**
29658c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
29668c6529dbSLiam Girdwood  * @dai: DAI
2967ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
29688c6529dbSLiam Girdwood  * @div: new clock divisor.
29698c6529dbSLiam Girdwood  *
29708c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
29718c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
29728c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
29738c6529dbSLiam Girdwood  */
29748c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
29758c6529dbSLiam Girdwood 	int div_id, int div)
29768c6529dbSLiam Girdwood {
2977f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2978f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
29798c6529dbSLiam Girdwood 	else
29808c6529dbSLiam Girdwood 		return -EINVAL;
29818c6529dbSLiam Girdwood }
29828c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
29838c6529dbSLiam Girdwood 
29848c6529dbSLiam Girdwood /**
29858c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
29868c6529dbSLiam Girdwood  * @dai: DAI
29878c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
298885488037SMark Brown  * @source: DAI specific source for the PLL
29898c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
29908c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
29918c6529dbSLiam Girdwood  *
29928c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
29938c6529dbSLiam Girdwood  */
299485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
299585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
29968c6529dbSLiam Girdwood {
2997f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2998f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
299985488037SMark Brown 					 freq_in, freq_out);
30008c6529dbSLiam Girdwood 	else
30018c6529dbSLiam Girdwood 		return -EINVAL;
30028c6529dbSLiam Girdwood }
30038c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
30048c6529dbSLiam Girdwood 
30058c6529dbSLiam Girdwood /**
30068c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
30078c6529dbSLiam Girdwood  * @dai: DAI
30088c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
30098c6529dbSLiam Girdwood  *
30108c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
30118c6529dbSLiam Girdwood  */
30128c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
30138c6529dbSLiam Girdwood {
3014f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
3015f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
30168c6529dbSLiam Girdwood 	else
30178c6529dbSLiam Girdwood 		return -EINVAL;
30188c6529dbSLiam Girdwood }
30198c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
30208c6529dbSLiam Girdwood 
30218c6529dbSLiam Girdwood /**
30228c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
30238c6529dbSLiam Girdwood  * @dai: DAI
3024a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3025a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
30268c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3027a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
30288c6529dbSLiam Girdwood  *
30298c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
30308c6529dbSLiam Girdwood  * specific.
30318c6529dbSLiam Girdwood  */
30328c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3033a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
30348c6529dbSLiam Girdwood {
3035f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3036f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3037a5479e38SDaniel Ribeiro 				slots, slot_width);
30388c6529dbSLiam Girdwood 	else
30398c6529dbSLiam Girdwood 		return -EINVAL;
30408c6529dbSLiam Girdwood }
30418c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
30428c6529dbSLiam Girdwood 
30438c6529dbSLiam Girdwood /**
3044472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3045472df3cbSBarry Song  * @dai: DAI
3046472df3cbSBarry Song  * @tx_num: how many TX channels
3047472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3048472df3cbSBarry Song  *           0~num-1 uses
3049472df3cbSBarry Song  * @rx_num: how many RX channels
3050472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3051472df3cbSBarry Song  *           0~num-1 uses
3052472df3cbSBarry Song  *
3053472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3054472df3cbSBarry Song  */
3055472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3056472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3057472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3058472df3cbSBarry Song {
3059f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3060f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3061472df3cbSBarry Song 			rx_num, rx_slot);
3062472df3cbSBarry Song 	else
3063472df3cbSBarry Song 		return -EINVAL;
3064472df3cbSBarry Song }
3065472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3066472df3cbSBarry Song 
3067472df3cbSBarry Song /**
30688c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
30698c6529dbSLiam Girdwood  * @dai: DAI
30708c6529dbSLiam Girdwood  * @tristate: tristate enable
30718c6529dbSLiam Girdwood  *
30728c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
30738c6529dbSLiam Girdwood  */
30748c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
30758c6529dbSLiam Girdwood {
3076f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3077f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
30788c6529dbSLiam Girdwood 	else
30798c6529dbSLiam Girdwood 		return -EINVAL;
30808c6529dbSLiam Girdwood }
30818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
30828c6529dbSLiam Girdwood 
30838c6529dbSLiam Girdwood /**
30848c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
30858c6529dbSLiam Girdwood  * @dai: DAI
30868c6529dbSLiam Girdwood  * @mute: mute enable
30878c6529dbSLiam Girdwood  *
30888c6529dbSLiam Girdwood  * Mutes the DAI DAC.
30898c6529dbSLiam Girdwood  */
30908c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
30918c6529dbSLiam Girdwood {
3092f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3093f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
30948c6529dbSLiam Girdwood 	else
30958c6529dbSLiam Girdwood 		return -EINVAL;
30968c6529dbSLiam Girdwood }
30978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
30988c6529dbSLiam Girdwood 
3099c5af3a2eSMark Brown /**
3100c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3101c5af3a2eSMark Brown  *
3102ac11a2b3SMark Brown  * @card: Card to register
3103c5af3a2eSMark Brown  *
3104c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
3105c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
3106c5af3a2eSMark Brown  * registration APIs.
3107c5af3a2eSMark Brown  */
3108c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
3109c5af3a2eSMark Brown {
3110f0fba2adSLiam Girdwood 	int i;
3111f0fba2adSLiam Girdwood 
3112c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3113c5af3a2eSMark Brown 		return -EINVAL;
3114c5af3a2eSMark Brown 
31152eea392dSJarkko Nikula 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
31162eea392dSJarkko Nikula 			    (card->num_links + card->num_aux_devs),
3117f0fba2adSLiam Girdwood 			    GFP_KERNEL);
3118f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3119f0fba2adSLiam Girdwood 		return -ENOMEM;
31202eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3121f0fba2adSLiam Girdwood 
3122f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3123f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3124f0fba2adSLiam Girdwood 
3125c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3126c5af3a2eSMark Brown 	card->instantiated = 0;
3127f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3128c5af3a2eSMark Brown 
3129c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3130c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
3131435c5e25SMark Brown 	snd_soc_instantiate_cards();
3132c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3133c5af3a2eSMark Brown 
3134c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3135c5af3a2eSMark Brown 
3136c5af3a2eSMark Brown 	return 0;
3137c5af3a2eSMark Brown }
3138c5af3a2eSMark Brown 
3139c5af3a2eSMark Brown /**
3140c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3141c5af3a2eSMark Brown  *
3142ac11a2b3SMark Brown  * @card: Card to unregister
3143c5af3a2eSMark Brown  *
3144c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
3145c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
3146c5af3a2eSMark Brown  * registration APIs.
3147c5af3a2eSMark Brown  */
3148c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
3149c5af3a2eSMark Brown {
3150c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3151c5af3a2eSMark Brown 	list_del(&card->list);
3152c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3153c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3154c5af3a2eSMark Brown 
3155c5af3a2eSMark Brown 	return 0;
3156c5af3a2eSMark Brown }
3157c5af3a2eSMark Brown 
3158f0fba2adSLiam Girdwood /*
3159f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3160f0fba2adSLiam Girdwood  * and sanitizing names.
3161f0fba2adSLiam Girdwood  */
31620b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3163f0fba2adSLiam Girdwood {
3164f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3165f0fba2adSLiam Girdwood 	int id1, id2;
3166f0fba2adSLiam Girdwood 
3167f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3168f0fba2adSLiam Girdwood 		return NULL;
3169f0fba2adSLiam Girdwood 
317058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3171f0fba2adSLiam Girdwood 
3172f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3173f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3174f0fba2adSLiam Girdwood 	if (found) {
3175f0fba2adSLiam Girdwood 		/* get ID */
3176f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3177f0fba2adSLiam Girdwood 
3178f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3179f0fba2adSLiam Girdwood 			if (*id == -1)
3180f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3181f0fba2adSLiam Girdwood 		}
3182f0fba2adSLiam Girdwood 
3183f0fba2adSLiam Girdwood 	} else {
3184f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3185f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3186f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3187f0fba2adSLiam Girdwood 
3188f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
318905899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3190f0fba2adSLiam Girdwood 
3191f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3192f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
319358818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3194f0fba2adSLiam Girdwood 		} else
3195f0fba2adSLiam Girdwood 			*id = 0;
3196f0fba2adSLiam Girdwood 	}
3197f0fba2adSLiam Girdwood 
3198f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3199f0fba2adSLiam Girdwood }
3200f0fba2adSLiam Girdwood 
3201f0fba2adSLiam Girdwood /*
3202f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3203f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3204f0fba2adSLiam Girdwood  */
3205f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3206f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3207f0fba2adSLiam Girdwood {
3208f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
3209f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3210f0fba2adSLiam Girdwood 				dev_name(dev));
3211f0fba2adSLiam Girdwood 		return NULL;
3212f0fba2adSLiam Girdwood 	}
3213f0fba2adSLiam Girdwood 
3214f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3215f0fba2adSLiam Girdwood }
3216f0fba2adSLiam Girdwood 
32179115171aSMark Brown /**
32189115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
32199115171aSMark Brown  *
3220ac11a2b3SMark Brown  * @dai: DAI to register
32219115171aSMark Brown  */
3222f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3223f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
32249115171aSMark Brown {
3225f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
32269115171aSMark Brown 
3227f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
32289115171aSMark Brown 
3229f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3230f0fba2adSLiam Girdwood 	if (dai == NULL)
3231f0fba2adSLiam Girdwood 			return -ENOMEM;
32326335d055SEric Miao 
3233f0fba2adSLiam Girdwood 	/* create DAI component name */
3234f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3235f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3236f0fba2adSLiam Girdwood 		kfree(dai);
3237f0fba2adSLiam Girdwood 		return -ENOMEM;
3238f0fba2adSLiam Girdwood 	}
3239f0fba2adSLiam Girdwood 
3240f0fba2adSLiam Girdwood 	dai->dev = dev;
3241f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3242f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3243f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
32449115171aSMark Brown 
32459115171aSMark Brown 	mutex_lock(&client_mutex);
32469115171aSMark Brown 	list_add(&dai->list, &dai_list);
3247435c5e25SMark Brown 	snd_soc_instantiate_cards();
32489115171aSMark Brown 	mutex_unlock(&client_mutex);
32499115171aSMark Brown 
32509115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
32519115171aSMark Brown 
32529115171aSMark Brown 	return 0;
32539115171aSMark Brown }
32549115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
32559115171aSMark Brown 
32569115171aSMark Brown /**
32579115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
32589115171aSMark Brown  *
3259ac11a2b3SMark Brown  * @dai: DAI to unregister
32609115171aSMark Brown  */
3261f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
32629115171aSMark Brown {
3263f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3264f0fba2adSLiam Girdwood 
3265f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3266f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3267f0fba2adSLiam Girdwood 			goto found;
3268f0fba2adSLiam Girdwood 	}
3269f0fba2adSLiam Girdwood 	return;
3270f0fba2adSLiam Girdwood 
3271f0fba2adSLiam Girdwood found:
32729115171aSMark Brown 	mutex_lock(&client_mutex);
32739115171aSMark Brown 	list_del(&dai->list);
32749115171aSMark Brown 	mutex_unlock(&client_mutex);
32759115171aSMark Brown 
32769115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3277f0fba2adSLiam Girdwood 	kfree(dai->name);
3278f0fba2adSLiam Girdwood 	kfree(dai);
32799115171aSMark Brown }
32809115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
32819115171aSMark Brown 
32829115171aSMark Brown /**
32839115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
32849115171aSMark Brown  *
3285ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3286ac11a2b3SMark Brown  * @count: Number of DAIs
32879115171aSMark Brown  */
3288f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3289f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
32909115171aSMark Brown {
3291f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3292f0fba2adSLiam Girdwood 	int i, ret = 0;
3293f0fba2adSLiam Girdwood 
3294720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
32959115171aSMark Brown 
32969115171aSMark Brown 	for (i = 0; i < count; i++) {
3297f0fba2adSLiam Girdwood 
3298f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3299c46e0079SAxel Lin 		if (dai == NULL) {
3300c46e0079SAxel Lin 			ret = -ENOMEM;
3301c46e0079SAxel Lin 			goto err;
3302c46e0079SAxel Lin 		}
3303f0fba2adSLiam Girdwood 
3304f0fba2adSLiam Girdwood 		/* create DAI component name */
3305f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3306f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3307f0fba2adSLiam Girdwood 			kfree(dai);
3308f0fba2adSLiam Girdwood 			ret = -EINVAL;
33099115171aSMark Brown 			goto err;
33109115171aSMark Brown 		}
33119115171aSMark Brown 
3312f0fba2adSLiam Girdwood 		dai->dev = dev;
3313f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
33140f9141c9SMark Brown 		if (dai->driver->id)
33150f9141c9SMark Brown 			dai->id = dai->driver->id;
33160f9141c9SMark Brown 		else
33170f9141c9SMark Brown 			dai->id = i;
3318f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3319f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3320f0fba2adSLiam Girdwood 
3321f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3322f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3323f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3324f0fba2adSLiam Girdwood 
3325f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3326f0fba2adSLiam Girdwood 	}
3327f0fba2adSLiam Girdwood 
33281dcb4f38SAxel Lin 	mutex_lock(&client_mutex);
3329f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
33301dcb4f38SAxel Lin 	mutex_unlock(&client_mutex);
33319115171aSMark Brown 	return 0;
33329115171aSMark Brown 
33339115171aSMark Brown err:
33349115171aSMark Brown 	for (i--; i >= 0; i--)
3335f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33369115171aSMark Brown 
33379115171aSMark Brown 	return ret;
33389115171aSMark Brown }
33399115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
33409115171aSMark Brown 
33419115171aSMark Brown /**
33429115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
33439115171aSMark Brown  *
3344ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3345ac11a2b3SMark Brown  * @count: Number of DAIs
33469115171aSMark Brown  */
3347f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
33489115171aSMark Brown {
33499115171aSMark Brown 	int i;
33509115171aSMark Brown 
33519115171aSMark Brown 	for (i = 0; i < count; i++)
3352f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33539115171aSMark Brown }
33549115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
33559115171aSMark Brown 
335612a48a8cSMark Brown /**
335712a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
335812a48a8cSMark Brown  *
3359ac11a2b3SMark Brown  * @platform: platform to register
336012a48a8cSMark Brown  */
3361f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3362f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
336312a48a8cSMark Brown {
3364f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
336512a48a8cSMark Brown 
3366f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3367f0fba2adSLiam Girdwood 
3368f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3369f0fba2adSLiam Girdwood 	if (platform == NULL)
3370f0fba2adSLiam Girdwood 			return -ENOMEM;
3371f0fba2adSLiam Girdwood 
3372f0fba2adSLiam Girdwood 	/* create platform component name */
3373f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3374f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3375f0fba2adSLiam Girdwood 		kfree(platform);
3376f0fba2adSLiam Girdwood 		return -ENOMEM;
3377f0fba2adSLiam Girdwood 	}
3378f0fba2adSLiam Girdwood 
3379f0fba2adSLiam Girdwood 	platform->dev = dev;
3380f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
338112a48a8cSMark Brown 
338212a48a8cSMark Brown 	mutex_lock(&client_mutex);
338312a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3384435c5e25SMark Brown 	snd_soc_instantiate_cards();
338512a48a8cSMark Brown 	mutex_unlock(&client_mutex);
338612a48a8cSMark Brown 
338712a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
338812a48a8cSMark Brown 
338912a48a8cSMark Brown 	return 0;
339012a48a8cSMark Brown }
339112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
339212a48a8cSMark Brown 
339312a48a8cSMark Brown /**
339412a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
339512a48a8cSMark Brown  *
3396ac11a2b3SMark Brown  * @platform: platform to unregister
339712a48a8cSMark Brown  */
3398f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
339912a48a8cSMark Brown {
3400f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3401f0fba2adSLiam Girdwood 
3402f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3403f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3404f0fba2adSLiam Girdwood 			goto found;
3405f0fba2adSLiam Girdwood 	}
3406f0fba2adSLiam Girdwood 	return;
3407f0fba2adSLiam Girdwood 
3408f0fba2adSLiam Girdwood found:
340912a48a8cSMark Brown 	mutex_lock(&client_mutex);
341012a48a8cSMark Brown 	list_del(&platform->list);
341112a48a8cSMark Brown 	mutex_unlock(&client_mutex);
341212a48a8cSMark Brown 
341312a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3414f0fba2adSLiam Girdwood 	kfree(platform->name);
3415f0fba2adSLiam Girdwood 	kfree(platform);
341612a48a8cSMark Brown }
341712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
341812a48a8cSMark Brown 
3419151ab22cSMark Brown static u64 codec_format_map[] = {
3420151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3421151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3422151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3423151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3424151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3425151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3426151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3427151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3428151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3429151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3430151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3431151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3432151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3433151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3434151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3435151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3436151ab22cSMark Brown };
3437151ab22cSMark Brown 
3438151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3439151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3440151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3441151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3442151ab22cSMark Brown  */
3443151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3444151ab22cSMark Brown {
3445151ab22cSMark Brown 	int i;
3446151ab22cSMark Brown 
3447151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3448151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3449151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3450151ab22cSMark Brown }
3451151ab22cSMark Brown 
34520d0cf00aSMark Brown /**
34530d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
34540d0cf00aSMark Brown  *
3455ac11a2b3SMark Brown  * @codec: codec to register
34560d0cf00aSMark Brown  */
3457f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3458001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3459001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3460001ae4c0SMark Brown 			   int num_dai)
34610d0cf00aSMark Brown {
34623335ddcaSDimitris Papastamos 	size_t reg_size;
3463f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3464f0fba2adSLiam Girdwood 	int ret, i;
3465151ab22cSMark Brown 
3466f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
34670d0cf00aSMark Brown 
3468f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3469f0fba2adSLiam Girdwood 	if (codec == NULL)
3470f0fba2adSLiam Girdwood 		return -ENOMEM;
34710d0cf00aSMark Brown 
3472f0fba2adSLiam Girdwood 	/* create CODEC component name */
3473f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3474f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3475f0fba2adSLiam Girdwood 		kfree(codec);
3476f0fba2adSLiam Girdwood 		return -ENOMEM;
3477151ab22cSMark Brown 	}
3478151ab22cSMark Brown 
347923bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
348023bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
348123bbce34SDimitris Papastamos 	else
348223bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
348323bbce34SDimitris Papastamos 
3484ce6120ccSLiam Girdwood 	INIT_LIST_HEAD(&codec->dapm.widgets);
3485c3acec26SMark Brown 	codec->write = codec_drv->write;
3486c3acec26SMark Brown 	codec->read = codec_drv->read;
3487ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3488ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3489ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3490f0fba2adSLiam Girdwood 	codec->dev = dev;
3491f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3492f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3493f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3494f0fba2adSLiam Girdwood 
34957a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
34967a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
34973335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
34983335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
34993335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
35003335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
35013335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
35023335ddcaSDimitris Papastamos 		 * the cache.
35033335ddcaSDimitris Papastamos 		 */
35043335ddcaSDimitris Papastamos 		codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
35053335ddcaSDimitris Papastamos 					      reg_size, GFP_KERNEL);
35063335ddcaSDimitris Papastamos 		if (!codec->reg_def_copy) {
35073335ddcaSDimitris Papastamos 			ret = -ENOMEM;
3508fdf0f54dSDimitris Papastamos 			goto fail;
35097a30a3dbSDimitris Papastamos 		}
35107a30a3dbSDimitris Papastamos 	}
35117a30a3dbSDimitris Papastamos 
3512f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3513f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3514f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3515f0fba2adSLiam Girdwood 	}
3516f0fba2adSLiam Girdwood 
351726b01ccdSMark Brown 	/* register any DAIs */
351826b01ccdSMark Brown 	if (num_dai) {
3519f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3520f0fba2adSLiam Girdwood 		if (ret < 0)
3521fdf0f54dSDimitris Papastamos 			goto fail;
352226b01ccdSMark Brown 	}
3523f0fba2adSLiam Girdwood 
35240d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35250d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
35260d0cf00aSMark Brown 	snd_soc_instantiate_cards();
35270d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35280d0cf00aSMark Brown 
35290d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
35300d0cf00aSMark Brown 	return 0;
3531f0fba2adSLiam Girdwood 
3532fdf0f54dSDimitris Papastamos fail:
35333335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35343335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3535f0fba2adSLiam Girdwood 	kfree(codec->name);
3536f0fba2adSLiam Girdwood 	kfree(codec);
3537f0fba2adSLiam Girdwood 	return ret;
35380d0cf00aSMark Brown }
35390d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
35400d0cf00aSMark Brown 
35410d0cf00aSMark Brown /**
35420d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
35430d0cf00aSMark Brown  *
3544ac11a2b3SMark Brown  * @codec: codec to unregister
35450d0cf00aSMark Brown  */
3546f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
35470d0cf00aSMark Brown {
3548f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3549f0fba2adSLiam Girdwood 	int i;
3550f0fba2adSLiam Girdwood 
3551f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3552f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3553f0fba2adSLiam Girdwood 			goto found;
3554f0fba2adSLiam Girdwood 	}
3555f0fba2adSLiam Girdwood 	return;
3556f0fba2adSLiam Girdwood 
3557f0fba2adSLiam Girdwood found:
355826b01ccdSMark Brown 	if (codec->num_dai)
3559f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3560f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3561f0fba2adSLiam Girdwood 
35620d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35630d0cf00aSMark Brown 	list_del(&codec->list);
35640d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35650d0cf00aSMark Brown 
35660d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3567f0fba2adSLiam Girdwood 
35687a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
35693335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35701aafcd4dSDimitris Papastamos 	kfree(codec->name);
3571f0fba2adSLiam Girdwood 	kfree(codec);
35720d0cf00aSMark Brown }
35730d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
35740d0cf00aSMark Brown 
3575c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3576db2a4165SFrank Mandarino {
3577384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3578384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
3579384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
3580384c89e2SMark Brown 		printk(KERN_WARNING
3581384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
3582384c89e2SMark Brown 		debugfs_root = NULL;
3583384c89e2SMark Brown 	}
3584c3c5a19aSMark Brown 
3585c3c5a19aSMark Brown 	if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3586c3c5a19aSMark Brown 				 &codec_list_fops))
3587c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3588c3c5a19aSMark Brown 
3589f3208780SMark Brown 	if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3590f3208780SMark Brown 				 &dai_list_fops))
3591f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
359219c7ac27SMark Brown 
359319c7ac27SMark Brown 	if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
359419c7ac27SMark Brown 				 &platform_list_fops))
359519c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3596384c89e2SMark Brown #endif
3597384c89e2SMark Brown 
3598db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3599db2a4165SFrank Mandarino }
36004abe8e16SMark Brown module_init(snd_soc_init);
3601db2a4165SFrank Mandarino 
36027d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3603db2a4165SFrank Mandarino {
3604384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3605384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
3606384c89e2SMark Brown #endif
3607db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3608db2a4165SFrank Mandarino }
3609db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3610db2a4165SFrank Mandarino 
3611db2a4165SFrank Mandarino /* Module information */
3612d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3613db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3614db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
36158b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3616