xref: /openbmc/linux/sound/soc/soc-core.c (revision 58818a77cd415e2f76607749de5a1ff24e58cefe)
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 
73965ac42cSLiam Girdwood /*
74965ac42cSLiam Girdwood  * This function forces any delayed work to be queued and run.
75965ac42cSLiam Girdwood  */
76965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork)
77965ac42cSLiam Girdwood {
78965ac42cSLiam Girdwood 	int ret;
79965ac42cSLiam Girdwood 
80965ac42cSLiam Girdwood 	/* cancel any work waiting to be queued. */
81965ac42cSLiam Girdwood 	ret = cancel_delayed_work(dwork);
82965ac42cSLiam Girdwood 
83965ac42cSLiam Girdwood 	/* if there was any work waiting then we run it now and
84965ac42cSLiam Girdwood 	 * wait for it's completion */
85965ac42cSLiam Girdwood 	if (ret) {
86965ac42cSLiam Girdwood 		schedule_delayed_work(dwork, 0);
87965ac42cSLiam Girdwood 		flush_scheduled_work();
88965ac42cSLiam Girdwood 	}
89965ac42cSLiam Girdwood 	return ret;
90965ac42cSLiam Girdwood }
91965ac42cSLiam Girdwood 
922624d5faSMark Brown /* codec register dump */
932624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
942624d5faSMark Brown {
955164d74dSMark Brown 	int ret, i, step = 1, count = 0;
962624d5faSMark Brown 
97f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
982624d5faSMark Brown 		return 0;
992624d5faSMark Brown 
100f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
101f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1022624d5faSMark Brown 
1032624d5faSMark Brown 	count += sprintf(buf, "%s registers\n", codec->name);
104f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
105f0fba2adSLiam Girdwood 		if (codec->driver->readable_register && !codec->driver->readable_register(i))
1062624d5faSMark Brown 			continue;
1072624d5faSMark Brown 
1082624d5faSMark Brown 		count += sprintf(buf + count, "%2x: ", i);
1092624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1102624d5faSMark Brown 			break;
1112624d5faSMark Brown 
112f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
113f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1142624d5faSMark Brown 							 PAGE_SIZE - count, i);
1155164d74dSMark Brown 		} else {
1165164d74dSMark Brown 			/* If the read fails it's almost certainly due to
1175164d74dSMark Brown 			 * the register being volatile and the device being
1185164d74dSMark Brown 			 * powered off.
1195164d74dSMark Brown 			 */
120f0fba2adSLiam Girdwood 			ret = codec->driver->read(codec, i);
1215164d74dSMark Brown 			if (ret >= 0)
1225164d74dSMark Brown 				count += snprintf(buf + count,
1235164d74dSMark Brown 						  PAGE_SIZE - count,
1245164d74dSMark Brown 						  "%4x", ret);
1252624d5faSMark Brown 			else
1265164d74dSMark Brown 				count += snprintf(buf + count,
1275164d74dSMark Brown 						  PAGE_SIZE - count,
1285164d74dSMark Brown 						  "<no data: %d>", ret);
1295164d74dSMark Brown 		}
1302624d5faSMark Brown 
1312624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1322624d5faSMark Brown 			break;
1332624d5faSMark Brown 
1342624d5faSMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1352624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1362624d5faSMark Brown 			break;
1372624d5faSMark Brown 	}
1382624d5faSMark Brown 
1392624d5faSMark Brown 	/* Truncate count; min() would cause a warning */
1402624d5faSMark Brown 	if (count >= PAGE_SIZE)
1412624d5faSMark Brown 		count = PAGE_SIZE - 1;
1422624d5faSMark Brown 
1432624d5faSMark Brown 	return count;
1442624d5faSMark Brown }
1452624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1462624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1472624d5faSMark Brown {
148f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
149f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
150f0fba2adSLiam Girdwood 
151f0fba2adSLiam Girdwood 	return soc_codec_reg_show(rtd->codec, buf);
1522624d5faSMark Brown }
1532624d5faSMark Brown 
1542624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1552624d5faSMark Brown 
156dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
157dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
158dbe21408SMark Brown {
159f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
160f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
161dbe21408SMark Brown 
162f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
163dbe21408SMark Brown }
164dbe21408SMark Brown 
165dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
166dbe21408SMark Brown 			       struct device_attribute *attr,
167dbe21408SMark Brown 			       const char *buf, size_t count)
168dbe21408SMark Brown {
169f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
170f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
171c593b520SMark Brown 	int ret;
172dbe21408SMark Brown 
173c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
174c593b520SMark Brown 	if (ret)
175c593b520SMark Brown 		return ret;
176dbe21408SMark Brown 
177dbe21408SMark Brown 	return count;
178dbe21408SMark Brown }
179dbe21408SMark Brown 
180dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
181dbe21408SMark Brown 
1822624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
1832624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
1842624d5faSMark Brown {
1852624d5faSMark Brown 	file->private_data = inode->i_private;
1862624d5faSMark Brown 	return 0;
1872624d5faSMark Brown }
1882624d5faSMark Brown 
1892624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1902624d5faSMark Brown 			       size_t count, loff_t *ppos)
1912624d5faSMark Brown {
1922624d5faSMark Brown 	ssize_t ret;
1932624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1942624d5faSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1952624d5faSMark Brown 	if (!buf)
1962624d5faSMark Brown 		return -ENOMEM;
1972624d5faSMark Brown 	ret = soc_codec_reg_show(codec, buf);
1982624d5faSMark Brown 	if (ret >= 0)
1992624d5faSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2002624d5faSMark Brown 	kfree(buf);
2012624d5faSMark Brown 	return ret;
2022624d5faSMark Brown }
2032624d5faSMark Brown 
2042624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2052624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2062624d5faSMark Brown {
2072624d5faSMark Brown 	char buf[32];
2082624d5faSMark Brown 	int buf_size;
2092624d5faSMark Brown 	char *start = buf;
2102624d5faSMark Brown 	unsigned long reg, value;
2112624d5faSMark Brown 	int step = 1;
2122624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2132624d5faSMark Brown 
2142624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2152624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2162624d5faSMark Brown 		return -EFAULT;
2172624d5faSMark Brown 	buf[buf_size] = 0;
2182624d5faSMark Brown 
219f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
220f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
2212624d5faSMark Brown 
2222624d5faSMark Brown 	while (*start == ' ')
2232624d5faSMark Brown 		start++;
2242624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
225f0fba2adSLiam Girdwood 	if ((reg >= codec->driver->reg_cache_size) || (reg % step))
2262624d5faSMark Brown 		return -EINVAL;
2272624d5faSMark Brown 	while (*start == ' ')
2282624d5faSMark Brown 		start++;
2292624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2302624d5faSMark Brown 		return -EINVAL;
231f0fba2adSLiam Girdwood 	codec->driver->write(codec, reg, value);
2322624d5faSMark Brown 	return buf_size;
2332624d5faSMark Brown }
2342624d5faSMark Brown 
2352624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2362624d5faSMark Brown 	.open = codec_reg_open_file,
2372624d5faSMark Brown 	.read = codec_reg_read_file,
2382624d5faSMark Brown 	.write = codec_reg_write_file,
2396038f373SArnd Bergmann 	.llseek = default_llseek,
2402624d5faSMark Brown };
2412624d5faSMark Brown 
2422624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2432624d5faSMark Brown {
244d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
245d6ce4cf3SJarkko Nikula 
2466ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
247d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2482624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2492624d5faSMark Brown 		printk(KERN_WARNING
2502624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2512624d5faSMark Brown 		return;
2522624d5faSMark Brown 	}
2532624d5faSMark Brown 
2542624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2552624d5faSMark Brown 						 codec->debugfs_codec_root,
2562624d5faSMark Brown 						 codec, &codec_reg_fops);
2572624d5faSMark Brown 	if (!codec->debugfs_reg)
2582624d5faSMark Brown 		printk(KERN_WARNING
2592624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
2602624d5faSMark Brown 
261ce6120ccSLiam Girdwood 	codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
2622624d5faSMark Brown 						 codec->debugfs_codec_root);
263ce6120ccSLiam Girdwood 	if (!codec->dapm.debugfs_dapm)
2642624d5faSMark Brown 		printk(KERN_WARNING
2652624d5faSMark Brown 		       "Failed to create DAPM debugfs directory\n");
2662624d5faSMark Brown 
267ce6120ccSLiam Girdwood 	snd_soc_dapm_debugfs_init(&codec->dapm);
2682624d5faSMark Brown }
2692624d5faSMark Brown 
2702624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2712624d5faSMark Brown {
2722624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2732624d5faSMark Brown }
2742624d5faSMark Brown 
275c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
276c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
277c3c5a19aSMark Brown {
278c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2792b194f9dSMark Brown 	ssize_t len, ret = 0;
280c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
281c3c5a19aSMark Brown 
282c3c5a19aSMark Brown 	if (!buf)
283c3c5a19aSMark Brown 		return -ENOMEM;
284c3c5a19aSMark Brown 
2852b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
2862b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
287c3c5a19aSMark Brown 			       codec->name);
2882b194f9dSMark Brown 		if (len >= 0)
2892b194f9dSMark Brown 			ret += len;
2902b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
2912b194f9dSMark Brown 			ret = PAGE_SIZE;
2922b194f9dSMark Brown 			break;
2932b194f9dSMark Brown 		}
2942b194f9dSMark Brown 	}
295c3c5a19aSMark Brown 
296c3c5a19aSMark Brown 	if (ret >= 0)
297c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
298c3c5a19aSMark Brown 
299c3c5a19aSMark Brown 	kfree(buf);
300c3c5a19aSMark Brown 
301c3c5a19aSMark Brown 	return ret;
302c3c5a19aSMark Brown }
303c3c5a19aSMark Brown 
304c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
305c3c5a19aSMark Brown 	.read = codec_list_read_file,
306c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
307c3c5a19aSMark Brown };
308c3c5a19aSMark Brown 
309f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
310f3208780SMark Brown 				  size_t count, loff_t *ppos)
311f3208780SMark Brown {
312f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3132b194f9dSMark Brown 	ssize_t len, ret = 0;
314f3208780SMark Brown 	struct snd_soc_dai *dai;
315f3208780SMark Brown 
316f3208780SMark Brown 	if (!buf)
317f3208780SMark Brown 		return -ENOMEM;
318f3208780SMark Brown 
3192b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3202b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3212b194f9dSMark Brown 		if (len >= 0)
3222b194f9dSMark Brown 			ret += len;
3232b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3242b194f9dSMark Brown 			ret = PAGE_SIZE;
3252b194f9dSMark Brown 			break;
3262b194f9dSMark Brown 		}
3272b194f9dSMark Brown 	}
328f3208780SMark Brown 
329f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
330f3208780SMark Brown 
331f3208780SMark Brown 	kfree(buf);
332f3208780SMark Brown 
333f3208780SMark Brown 	return ret;
334f3208780SMark Brown }
335f3208780SMark Brown 
336f3208780SMark Brown static const struct file_operations dai_list_fops = {
337f3208780SMark Brown 	.read = dai_list_read_file,
338f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
339f3208780SMark Brown };
340f3208780SMark Brown 
34119c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
34219c7ac27SMark Brown 				       char __user *user_buf,
34319c7ac27SMark Brown 				       size_t count, loff_t *ppos)
34419c7ac27SMark Brown {
34519c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3462b194f9dSMark Brown 	ssize_t len, ret = 0;
34719c7ac27SMark Brown 	struct snd_soc_platform *platform;
34819c7ac27SMark Brown 
34919c7ac27SMark Brown 	if (!buf)
35019c7ac27SMark Brown 		return -ENOMEM;
35119c7ac27SMark Brown 
3522b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3532b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
35419c7ac27SMark Brown 			       platform->name);
3552b194f9dSMark Brown 		if (len >= 0)
3562b194f9dSMark Brown 			ret += len;
3572b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3582b194f9dSMark Brown 			ret = PAGE_SIZE;
3592b194f9dSMark Brown 			break;
3602b194f9dSMark Brown 		}
3612b194f9dSMark Brown 	}
36219c7ac27SMark Brown 
36319c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
36419c7ac27SMark Brown 
36519c7ac27SMark Brown 	kfree(buf);
36619c7ac27SMark Brown 
36719c7ac27SMark Brown 	return ret;
36819c7ac27SMark Brown }
36919c7ac27SMark Brown 
37019c7ac27SMark Brown static const struct file_operations platform_list_fops = {
37119c7ac27SMark Brown 	.read = platform_list_read_file,
37219c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
37319c7ac27SMark Brown };
37419c7ac27SMark Brown 
375a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
376a6052154SJarkko Nikula {
377a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
378a6052154SJarkko Nikula 						     debugfs_root);
3793a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
380a6052154SJarkko Nikula 		dev_warn(card->dev,
381a6052154SJarkko Nikula 			 "ASoC: Failed to create codec debugfs directory\n");
3823a45b867SJarkko Nikula 		return;
3833a45b867SJarkko Nikula 	}
3843a45b867SJarkko Nikula 
3853a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
3863a45b867SJarkko Nikula 						    card->debugfs_card_root,
3873a45b867SJarkko Nikula 						    &card->pop_time);
3883a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
3893a45b867SJarkko Nikula 		dev_warn(card->dev,
3903a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
391a6052154SJarkko Nikula }
392a6052154SJarkko Nikula 
393a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
394a6052154SJarkko Nikula {
395a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
396a6052154SJarkko Nikula }
397a6052154SJarkko Nikula 
3982624d5faSMark Brown #else
3992624d5faSMark Brown 
4002624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4012624d5faSMark Brown {
4022624d5faSMark Brown }
4032624d5faSMark Brown 
4042624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4052624d5faSMark Brown {
4062624d5faSMark Brown }
407b95fccbcSAxel Lin 
408b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
409b95fccbcSAxel Lin {
410b95fccbcSAxel Lin }
411b95fccbcSAxel Lin 
412b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
413b95fccbcSAxel Lin {
414b95fccbcSAxel Lin }
4152624d5faSMark Brown #endif
4162624d5faSMark Brown 
417db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
418db2a4165SFrank Mandarino /* unregister ac97 codec */
419db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
420db2a4165SFrank Mandarino {
421db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
422db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
423db2a4165SFrank Mandarino 	return 0;
424db2a4165SFrank Mandarino }
425db2a4165SFrank Mandarino 
426db2a4165SFrank Mandarino /* stop no dev release warning */
427db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
428db2a4165SFrank Mandarino 
429db2a4165SFrank Mandarino /* register ac97 codec to bus */
430db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
431db2a4165SFrank Mandarino {
432db2a4165SFrank Mandarino 	int err;
433db2a4165SFrank Mandarino 
434db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4354ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
436db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
437db2a4165SFrank Mandarino 
438bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
439f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
440db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
441db2a4165SFrank Mandarino 	if (err < 0) {
442db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
443db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
444db2a4165SFrank Mandarino 		return err;
445db2a4165SFrank Mandarino 	}
446db2a4165SFrank Mandarino 	return 0;
447db2a4165SFrank Mandarino }
448db2a4165SFrank Mandarino #endif
449db2a4165SFrank Mandarino 
45006f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
45106f409d7SMark Brown {
45206f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
453f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
45506f409d7SMark Brown 	int ret;
45606f409d7SMark Brown 
457f0fba2adSLiam Girdwood 	if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
458f0fba2adSLiam Girdwood 			rtd->dai_link->symmetric_rates) {
459f0fba2adSLiam Girdwood 		dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
460f0fba2adSLiam Girdwood 				rtd->rate);
46106f409d7SMark Brown 
46206f409d7SMark Brown 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
46306f409d7SMark Brown 						   SNDRV_PCM_HW_PARAM_RATE,
464f0fba2adSLiam Girdwood 						   rtd->rate,
465f0fba2adSLiam Girdwood 						   rtd->rate);
46606f409d7SMark Brown 		if (ret < 0) {
467f0fba2adSLiam Girdwood 			dev_err(&rtd->dev,
46806f409d7SMark Brown 				"Unable to apply rate symmetry constraint: %d\n", ret);
46906f409d7SMark Brown 			return ret;
47006f409d7SMark Brown 		}
47106f409d7SMark Brown 	}
47206f409d7SMark Brown 
47306f409d7SMark Brown 	return 0;
47406f409d7SMark Brown }
47506f409d7SMark Brown 
476db2a4165SFrank Mandarino /*
477db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
478db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
479db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
480db2a4165SFrank Mandarino  */
481db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
482db2a4165SFrank Mandarino {
483db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
484db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
485f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
486f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
487f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
488f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
489f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
490db2a4165SFrank Mandarino 	int ret = 0;
491db2a4165SFrank Mandarino 
492db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
493db2a4165SFrank Mandarino 
494db2a4165SFrank Mandarino 	/* startup the audio subsystem */
495f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->startup) {
496f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
497db2a4165SFrank Mandarino 		if (ret < 0) {
498db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
499cb666e5bSLiam Girdwood 				cpu_dai->name);
500db2a4165SFrank Mandarino 			goto out;
501db2a4165SFrank Mandarino 		}
502db2a4165SFrank Mandarino 	}
503db2a4165SFrank Mandarino 
504f0fba2adSLiam Girdwood 	if (platform->driver->ops->open) {
505f0fba2adSLiam Girdwood 		ret = platform->driver->ops->open(substream);
506db2a4165SFrank Mandarino 		if (ret < 0) {
507db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
508db2a4165SFrank Mandarino 			goto platform_err;
509db2a4165SFrank Mandarino 		}
510db2a4165SFrank Mandarino 	}
511db2a4165SFrank Mandarino 
512f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->startup) {
513f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->startup(substream, codec_dai);
514cb666e5bSLiam Girdwood 		if (ret < 0) {
515cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
516cb666e5bSLiam Girdwood 				codec_dai->name);
517cb666e5bSLiam Girdwood 			goto codec_dai_err;
518cb666e5bSLiam Girdwood 		}
519cb666e5bSLiam Girdwood 	}
520cb666e5bSLiam Girdwood 
521f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
522f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
523db2a4165SFrank Mandarino 		if (ret < 0) {
524f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
525db2a4165SFrank Mandarino 			goto machine_err;
526db2a4165SFrank Mandarino 		}
527db2a4165SFrank Mandarino 	}
528db2a4165SFrank Mandarino 
529a00f90f9SMark Brown 	/* Check that the codec and cpu DAIs are compatible */
530db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
531db2a4165SFrank Mandarino 		runtime->hw.rate_min =
532f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.rate_min,
533f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_min);
534db2a4165SFrank Mandarino 		runtime->hw.rate_max =
535f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.rate_max,
536f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_max);
537db2a4165SFrank Mandarino 		runtime->hw.channels_min =
538f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.channels_min,
539f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_min);
540db2a4165SFrank Mandarino 		runtime->hw.channels_max =
541f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.channels_max,
542f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_max);
543cb666e5bSLiam Girdwood 		runtime->hw.formats =
544f0fba2adSLiam Girdwood 			codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
545cb666e5bSLiam Girdwood 		runtime->hw.rates =
546f0fba2adSLiam Girdwood 			codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
547f0fba2adSLiam Girdwood 		if (codec_dai_drv->playback.rates
548d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
549f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->playback.rates;
550f0fba2adSLiam Girdwood 		if (cpu_dai_drv->playback.rates
551d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
552f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->playback.rates;
553db2a4165SFrank Mandarino 	} else {
554db2a4165SFrank Mandarino 		runtime->hw.rate_min =
555f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.rate_min,
556f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_min);
557db2a4165SFrank Mandarino 		runtime->hw.rate_max =
558f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.rate_max,
559f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_max);
560db2a4165SFrank Mandarino 		runtime->hw.channels_min =
561f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.channels_min,
562f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_min);
563db2a4165SFrank Mandarino 		runtime->hw.channels_max =
564f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.channels_max,
565f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_max);
566cb666e5bSLiam Girdwood 		runtime->hw.formats =
567f0fba2adSLiam Girdwood 			codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
568cb666e5bSLiam Girdwood 		runtime->hw.rates =
569f0fba2adSLiam Girdwood 			codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
570f0fba2adSLiam Girdwood 		if (codec_dai_drv->capture.rates
571d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
572f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->capture.rates;
573f0fba2adSLiam Girdwood 		if (cpu_dai_drv->capture.rates
574d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
575f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->capture.rates;
576db2a4165SFrank Mandarino 	}
577db2a4165SFrank Mandarino 
578db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
579db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
580db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
581cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
582bb1c0478SJassi Brar 		goto config_err;
583db2a4165SFrank Mandarino 	}
584db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
585db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
586cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
587bb1c0478SJassi Brar 		goto config_err;
588db2a4165SFrank Mandarino 	}
589db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
590db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
591cb666e5bSLiam Girdwood 				codec_dai->name, cpu_dai->name);
592bb1c0478SJassi Brar 		goto config_err;
593db2a4165SFrank Mandarino 	}
594db2a4165SFrank Mandarino 
59506f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
59606f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
59706f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
59806f409d7SMark Brown 		if (ret != 0)
599bb1c0478SJassi Brar 			goto config_err;
60006f409d7SMark Brown 	}
60106f409d7SMark Brown 
602f0fba2adSLiam Girdwood 	pr_debug("asoc: %s <-> %s info:\n",
603f0fba2adSLiam Girdwood 			codec_dai->name, cpu_dai->name);
604f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
605f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
606b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
607f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
608b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
609db2a4165SFrank Mandarino 
61014dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
611f0fba2adSLiam Girdwood 		cpu_dai->playback_active++;
612f0fba2adSLiam Girdwood 		codec_dai->playback_active++;
61314dc5734SJassi Brar 	} else {
614f0fba2adSLiam Girdwood 		cpu_dai->capture_active++;
615f0fba2adSLiam Girdwood 		codec_dai->capture_active++;
61614dc5734SJassi Brar 	}
61714dc5734SJassi Brar 	cpu_dai->active++;
61814dc5734SJassi Brar 	codec_dai->active++;
619f0fba2adSLiam Girdwood 	rtd->codec->active++;
620db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
621db2a4165SFrank Mandarino 	return 0;
622db2a4165SFrank Mandarino 
623bb1c0478SJassi Brar config_err:
624f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
625f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
626db2a4165SFrank Mandarino 
627bb1c0478SJassi Brar machine_err:
628f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
629f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
630bb1c0478SJassi Brar 
631cb666e5bSLiam Girdwood codec_dai_err:
632f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
633f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
634db2a4165SFrank Mandarino 
635db2a4165SFrank Mandarino platform_err:
636f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
637f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
638db2a4165SFrank Mandarino out:
639db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
640db2a4165SFrank Mandarino 	return ret;
641db2a4165SFrank Mandarino }
642db2a4165SFrank Mandarino 
643db2a4165SFrank Mandarino /*
6443a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
645db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
646db2a4165SFrank Mandarino  * due to DAPM power cycling.
647db2a4165SFrank Mandarino  */
6484484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
649db2a4165SFrank Mandarino {
650f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
651f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
652f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
653db2a4165SFrank Mandarino 
654db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
655db2a4165SFrank Mandarino 
656f24368c2SMark Brown 	pr_debug("pop wq checking: %s status: %s waiting: %s\n",
657f0fba2adSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
658f0fba2adSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
659db2a4165SFrank Mandarino 		 codec_dai->pop_wait ? "yes" : "no");
660db2a4165SFrank Mandarino 
661db2a4165SFrank Mandarino 	/* are we waiting on this codec DAI stream */
662db2a4165SFrank Mandarino 	if (codec_dai->pop_wait == 1) {
663db2a4165SFrank Mandarino 		codec_dai->pop_wait = 0;
664f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
665f0fba2adSLiam Girdwood 			codec_dai->driver->playback.stream_name,
666db2a4165SFrank Mandarino 			SND_SOC_DAPM_STREAM_STOP);
667db2a4165SFrank Mandarino 	}
668f0fba2adSLiam Girdwood 
669db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
670db2a4165SFrank Mandarino }
671db2a4165SFrank Mandarino 
672db2a4165SFrank Mandarino /*
673db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
674db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
675db2a4165SFrank Mandarino  * shutdown.
676db2a4165SFrank Mandarino  */
677db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
678db2a4165SFrank Mandarino {
679db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
680f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
681f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
682f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
683f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
684db2a4165SFrank Mandarino 
685db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
686db2a4165SFrank Mandarino 
68714dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
688f0fba2adSLiam Girdwood 		cpu_dai->playback_active--;
689f0fba2adSLiam Girdwood 		codec_dai->playback_active--;
69014dc5734SJassi Brar 	} else {
691f0fba2adSLiam Girdwood 		cpu_dai->capture_active--;
692f0fba2adSLiam Girdwood 		codec_dai->capture_active--;
693db2a4165SFrank Mandarino 	}
69414dc5734SJassi Brar 
69514dc5734SJassi Brar 	cpu_dai->active--;
69614dc5734SJassi Brar 	codec_dai->active--;
697db2a4165SFrank Mandarino 	codec->active--;
698db2a4165SFrank Mandarino 
6996010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
7006010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
7016010b2daSMark Brown 	 */
7026010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
7036010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
7046010b2daSMark Brown 
705f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
706f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
707db2a4165SFrank Mandarino 
708f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
709f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
710db2a4165SFrank Mandarino 
711f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
712f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
713db2a4165SFrank Mandarino 
714f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
715f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
716f0fba2adSLiam Girdwood 	cpu_dai->runtime = NULL;
717db2a4165SFrank Mandarino 
718db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
719db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
720cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
721f0fba2adSLiam Girdwood 		schedule_delayed_work(&rtd->delayed_work,
722f0fba2adSLiam Girdwood 			msecs_to_jiffies(rtd->pmdown_time));
723db2a4165SFrank Mandarino 	} else {
724db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
725f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
726f0fba2adSLiam Girdwood 			codec_dai->driver->capture.stream_name,
7270b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
728db2a4165SFrank Mandarino 	}
729db2a4165SFrank Mandarino 
730db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
731db2a4165SFrank Mandarino 	return 0;
732db2a4165SFrank Mandarino }
733db2a4165SFrank Mandarino 
734db2a4165SFrank Mandarino /*
735db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
736db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
737db2a4165SFrank Mandarino  * it can refer to the runtime info.
738db2a4165SFrank Mandarino  */
739db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
740db2a4165SFrank Mandarino {
741db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
742f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
743f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
744f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
745db2a4165SFrank Mandarino 	int ret = 0;
746db2a4165SFrank Mandarino 
747db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
748cb666e5bSLiam Girdwood 
749f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
750f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
751cb666e5bSLiam Girdwood 		if (ret < 0) {
752cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
753cb666e5bSLiam Girdwood 			goto out;
754cb666e5bSLiam Girdwood 		}
755cb666e5bSLiam Girdwood 	}
756cb666e5bSLiam Girdwood 
757f0fba2adSLiam Girdwood 	if (platform->driver->ops->prepare) {
758f0fba2adSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
759a71a468aSLiam Girdwood 		if (ret < 0) {
760a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
761db2a4165SFrank Mandarino 			goto out;
762db2a4165SFrank Mandarino 		}
763a71a468aSLiam Girdwood 	}
764db2a4165SFrank Mandarino 
765f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->prepare) {
766f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->prepare(substream, codec_dai);
767a71a468aSLiam Girdwood 		if (ret < 0) {
768a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
769db2a4165SFrank Mandarino 			goto out;
770db2a4165SFrank Mandarino 		}
771a71a468aSLiam Girdwood 	}
772db2a4165SFrank Mandarino 
773f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->prepare) {
774f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
775cb666e5bSLiam Girdwood 		if (ret < 0) {
776cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
777cb666e5bSLiam Girdwood 			goto out;
778cb666e5bSLiam Girdwood 		}
779cb666e5bSLiam Girdwood 	}
780db2a4165SFrank Mandarino 
781d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
782d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
783d45f6219SMark Brown 	    codec_dai->pop_wait) {
784cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
785f0fba2adSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
786db2a4165SFrank Mandarino 	}
787db2a4165SFrank Mandarino 
788db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
789f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
790f0fba2adSLiam Girdwood 					  codec_dai->driver->playback.stream_name,
791db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
792db2a4165SFrank Mandarino 	else
793f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
794f0fba2adSLiam Girdwood 					  codec_dai->driver->capture.stream_name,
795db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
7968c6529dbSLiam Girdwood 
7978c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
798db2a4165SFrank Mandarino 
799db2a4165SFrank Mandarino out:
800db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
801db2a4165SFrank Mandarino 	return ret;
802db2a4165SFrank Mandarino }
803db2a4165SFrank Mandarino 
804db2a4165SFrank Mandarino /*
805db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
806db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
807db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
808db2a4165SFrank Mandarino  */
809db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
810db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
811db2a4165SFrank Mandarino {
812db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
813f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
814f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
815f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
816db2a4165SFrank Mandarino 	int ret = 0;
817db2a4165SFrank Mandarino 
818db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
819db2a4165SFrank Mandarino 
820f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
821f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
822cb666e5bSLiam Girdwood 		if (ret < 0) {
823cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
824db2a4165SFrank Mandarino 			goto out;
825db2a4165SFrank Mandarino 		}
826cb666e5bSLiam Girdwood 	}
827db2a4165SFrank Mandarino 
828f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_params) {
829f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
830db2a4165SFrank Mandarino 		if (ret < 0) {
831db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
832cb666e5bSLiam Girdwood 				codec_dai->name);
833cb666e5bSLiam Girdwood 			goto codec_err;
834db2a4165SFrank Mandarino 		}
835db2a4165SFrank Mandarino 	}
836db2a4165SFrank Mandarino 
837f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_params) {
838f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
839db2a4165SFrank Mandarino 		if (ret < 0) {
8403ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
841cb666e5bSLiam Girdwood 				cpu_dai->name);
842db2a4165SFrank Mandarino 			goto interface_err;
843db2a4165SFrank Mandarino 		}
844db2a4165SFrank Mandarino 	}
845db2a4165SFrank Mandarino 
846f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_params) {
847f0fba2adSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
848db2a4165SFrank Mandarino 		if (ret < 0) {
8493ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
850db2a4165SFrank Mandarino 				platform->name);
851db2a4165SFrank Mandarino 			goto platform_err;
852db2a4165SFrank Mandarino 		}
853db2a4165SFrank Mandarino 	}
854db2a4165SFrank Mandarino 
855f0fba2adSLiam Girdwood 	rtd->rate = params_rate(params);
85606f409d7SMark Brown 
857db2a4165SFrank Mandarino out:
858db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
859db2a4165SFrank Mandarino 	return ret;
860db2a4165SFrank Mandarino 
861db2a4165SFrank Mandarino platform_err:
862f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
863f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
864db2a4165SFrank Mandarino 
865db2a4165SFrank Mandarino interface_err:
866f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
867f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
868cb666e5bSLiam Girdwood 
869cb666e5bSLiam Girdwood codec_err:
870f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
871f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
872db2a4165SFrank Mandarino 
873db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
874db2a4165SFrank Mandarino 	return ret;
875db2a4165SFrank Mandarino }
876db2a4165SFrank Mandarino 
877db2a4165SFrank Mandarino /*
878a00f90f9SMark Brown  * Frees resources allocated by hw_params, can be called multiple times
879db2a4165SFrank Mandarino  */
880db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
881db2a4165SFrank Mandarino {
882db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
883f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
884f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
885f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
886f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
887db2a4165SFrank Mandarino 
888db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
889db2a4165SFrank Mandarino 
890db2a4165SFrank Mandarino 	/* apply codec digital mute */
8918c6529dbSLiam Girdwood 	if (!codec->active)
8928c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
893db2a4165SFrank Mandarino 
894db2a4165SFrank Mandarino 	/* free any machine hw params */
895f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
896f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
897db2a4165SFrank Mandarino 
898db2a4165SFrank Mandarino 	/* free any DMA resources */
899f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_free)
900f0fba2adSLiam Girdwood 		platform->driver->ops->hw_free(substream);
901db2a4165SFrank Mandarino 
902a00f90f9SMark Brown 	/* now free hw params for the DAIs  */
903f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
904f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
905db2a4165SFrank Mandarino 
906f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
907f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
908db2a4165SFrank Mandarino 
909db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
910db2a4165SFrank Mandarino 	return 0;
911db2a4165SFrank Mandarino }
912db2a4165SFrank Mandarino 
913db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
914db2a4165SFrank Mandarino {
915db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
916f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
917f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
918f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
919db2a4165SFrank Mandarino 	int ret;
920db2a4165SFrank Mandarino 
921f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->trigger) {
922f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
923db2a4165SFrank Mandarino 		if (ret < 0)
924db2a4165SFrank Mandarino 			return ret;
925db2a4165SFrank Mandarino 	}
926db2a4165SFrank Mandarino 
927f0fba2adSLiam Girdwood 	if (platform->driver->ops->trigger) {
928f0fba2adSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
929db2a4165SFrank Mandarino 		if (ret < 0)
930db2a4165SFrank Mandarino 			return ret;
931db2a4165SFrank Mandarino 	}
932db2a4165SFrank Mandarino 
933f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->trigger) {
934f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
935db2a4165SFrank Mandarino 		if (ret < 0)
936db2a4165SFrank Mandarino 			return ret;
937db2a4165SFrank Mandarino 	}
938db2a4165SFrank Mandarino 	return 0;
939db2a4165SFrank Mandarino }
940db2a4165SFrank Mandarino 
941377b6f62SPeter Ujfalusi /*
942377b6f62SPeter Ujfalusi  * soc level wrapper for pointer callback
943258020d0SPeter Ujfalusi  * If cpu_dai, codec_dai, platform driver has the delay callback, than
944258020d0SPeter Ujfalusi  * the runtime->delay will be updated accordingly.
945377b6f62SPeter Ujfalusi  */
946377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
947377b6f62SPeter Ujfalusi {
948377b6f62SPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
949f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
950f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
951f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
952258020d0SPeter Ujfalusi 	struct snd_pcm_runtime *runtime = substream->runtime;
953377b6f62SPeter Ujfalusi 	snd_pcm_uframes_t offset = 0;
954258020d0SPeter Ujfalusi 	snd_pcm_sframes_t delay = 0;
955377b6f62SPeter Ujfalusi 
956f0fba2adSLiam Girdwood 	if (platform->driver->ops->pointer)
957f0fba2adSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
958377b6f62SPeter Ujfalusi 
959f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->delay)
960f0fba2adSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
961258020d0SPeter Ujfalusi 
962f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->delay)
963f0fba2adSLiam Girdwood 		delay += codec_dai->driver->ops->delay(substream, codec_dai);
964258020d0SPeter Ujfalusi 
965f0fba2adSLiam Girdwood 	if (platform->driver->delay)
966f0fba2adSLiam Girdwood 		delay += platform->driver->delay(substream, codec_dai);
967258020d0SPeter Ujfalusi 
968258020d0SPeter Ujfalusi 	runtime->delay = delay;
969258020d0SPeter Ujfalusi 
970377b6f62SPeter Ujfalusi 	return offset;
971377b6f62SPeter Ujfalusi }
972377b6f62SPeter Ujfalusi 
973db2a4165SFrank Mandarino /* ASoC PCM operations */
974db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
975db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
976db2a4165SFrank Mandarino 	.close		= soc_codec_close,
977db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
978db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
979db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
980db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
981377b6f62SPeter Ujfalusi 	.pointer	= soc_pcm_pointer,
982db2a4165SFrank Mandarino };
983db2a4165SFrank Mandarino 
984db2a4165SFrank Mandarino #ifdef CONFIG_PM
985db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
986416356fcSMark Brown static int soc_suspend(struct device *dev)
987db2a4165SFrank Mandarino {
988416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
989f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
9902eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
991db2a4165SFrank Mandarino 	int i;
992db2a4165SFrank Mandarino 
993e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
994e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
995e3509ff0SDaniel Mack 	 */
996f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
997e3509ff0SDaniel Mack 		return 0;
998e3509ff0SDaniel Mack 
9996ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
10006ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
10016ed25978SAndy Green 	 */
1002f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
1003f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1004f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
10056ed25978SAndy Green 
10066ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
1007f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
10086ed25978SAndy Green 
1009a00f90f9SMark Brown 	/* mute any active DACs */
1010f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1011f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1012f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
10133efab7dcSMark Brown 
1014f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10153efab7dcSMark Brown 			continue;
10163efab7dcSMark Brown 
1017f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1018f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
1019db2a4165SFrank Mandarino 	}
1020db2a4165SFrank Mandarino 
10214ccab3e7SLiam Girdwood 	/* suspend all pcms */
1022f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1023f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10243efab7dcSMark Brown 			continue;
10253efab7dcSMark Brown 
1026f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
10273efab7dcSMark Brown 	}
10284ccab3e7SLiam Girdwood 
102987506549SMark Brown 	if (card->suspend_pre)
1030416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
1031db2a4165SFrank Mandarino 
1032f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1033f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1034f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
10353efab7dcSMark Brown 
1036f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10373efab7dcSMark Brown 			continue;
10383efab7dcSMark Brown 
1039f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1040f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1041f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
1042f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
1043f0fba2adSLiam Girdwood 			platform->suspended = 1;
1044f0fba2adSLiam Girdwood 		}
1045db2a4165SFrank Mandarino 	}
1046db2a4165SFrank Mandarino 
1047db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
1048f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1049f0fba2adSLiam Girdwood 		run_delayed_work(&card->rtd[i].delayed_work);
1050ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1051f0fba2adSLiam Girdwood 	}
1052db2a4165SFrank Mandarino 
1053f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1054f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
10553efab7dcSMark Brown 
1056f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10573efab7dcSMark Brown 			continue;
10583efab7dcSMark Brown 
1059f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1060f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1061db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1062f0fba2adSLiam Girdwood 
1063f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1064f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1065db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1066db2a4165SFrank Mandarino 	}
1067db2a4165SFrank Mandarino 
1068f0fba2adSLiam Girdwood 	/* suspend all CODECs */
10692eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
10701547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
10711547aba9SMark Brown 		 * bias _ON and should not be suspended. */
1072f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
1073ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
10741547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
10751547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1076f0fba2adSLiam Girdwood 				codec->driver->suspend(codec, PMSG_SUSPEND);
1077f0fba2adSLiam Girdwood 				codec->suspended = 1;
10781547aba9SMark Brown 				break;
10791547aba9SMark Brown 			default:
1080f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
10811547aba9SMark Brown 				break;
10821547aba9SMark Brown 			}
10831547aba9SMark Brown 		}
1084f0fba2adSLiam Girdwood 	}
1085db2a4165SFrank Mandarino 
1086f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1087f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
10883efab7dcSMark Brown 
1089f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10903efab7dcSMark Brown 			continue;
10913efab7dcSMark Brown 
1092f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1093f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1094db2a4165SFrank Mandarino 	}
1095db2a4165SFrank Mandarino 
109687506549SMark Brown 	if (card->suspend_post)
1097416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
1098db2a4165SFrank Mandarino 
1099db2a4165SFrank Mandarino 	return 0;
1100db2a4165SFrank Mandarino }
1101db2a4165SFrank Mandarino 
11026ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
11036ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
11046ed25978SAndy Green  */
11056ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
1106db2a4165SFrank Mandarino {
1107f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
1108f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
1109f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
11102eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1111db2a4165SFrank Mandarino 	int i;
1112db2a4165SFrank Mandarino 
11136ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
11146ed25978SAndy Green 	 * so userspace apps are blocked from touching us
11156ed25978SAndy Green 	 */
11166ed25978SAndy Green 
1117f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
11186ed25978SAndy Green 
11199949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
1120f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
11219949788bSMark Brown 
112287506549SMark Brown 	if (card->resume_pre)
112387506549SMark Brown 		card->resume_pre(pdev);
1124db2a4165SFrank Mandarino 
1125f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
1126f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1127f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
11283efab7dcSMark Brown 
1129f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11303efab7dcSMark Brown 			continue;
11313efab7dcSMark Brown 
1132f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1133f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1134db2a4165SFrank Mandarino 	}
1135db2a4165SFrank Mandarino 
11362eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
11371547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
11381547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
11391547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
11401547aba9SMark Brown 		 */
1141f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
1142ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
11431547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
11441547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1145f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
1146f0fba2adSLiam Girdwood 				codec->suspended = 0;
11471547aba9SMark Brown 				break;
11481547aba9SMark Brown 			default:
1149f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
11501547aba9SMark Brown 				break;
11511547aba9SMark Brown 			}
11521547aba9SMark Brown 		}
1153f0fba2adSLiam Girdwood 	}
1154db2a4165SFrank Mandarino 
1155f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1156f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
11573efab7dcSMark Brown 
1158f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11593efab7dcSMark Brown 			continue;
11603efab7dcSMark Brown 
1161f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1162f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1163db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1164f0fba2adSLiam Girdwood 
1165f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1166f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1167db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1168db2a4165SFrank Mandarino 	}
1169db2a4165SFrank Mandarino 
11703ff3f64bSMark Brown 	/* unmute any active DACs */
1171f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1172f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1173f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
11743efab7dcSMark Brown 
1175f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11763efab7dcSMark Brown 			continue;
11773efab7dcSMark Brown 
1178f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1179f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
1180db2a4165SFrank Mandarino 	}
1181db2a4165SFrank Mandarino 
1182f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1183f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1184f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
11853efab7dcSMark Brown 
1186f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11873efab7dcSMark Brown 			continue;
11883efab7dcSMark Brown 
1189f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1190f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1191f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
1192f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
1193f0fba2adSLiam Girdwood 			platform->suspended = 0;
1194f0fba2adSLiam Girdwood 		}
1195db2a4165SFrank Mandarino 	}
1196db2a4165SFrank Mandarino 
119787506549SMark Brown 	if (card->resume_post)
119887506549SMark Brown 		card->resume_post(pdev);
1199db2a4165SFrank Mandarino 
1200f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
12016ed25978SAndy Green 
12026ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
1203f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
12046ed25978SAndy Green }
12056ed25978SAndy Green 
12066ed25978SAndy Green /* powers up audio subsystem after a suspend */
1207416356fcSMark Brown static int soc_resume(struct device *dev)
12086ed25978SAndy Green {
1209416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1210f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1211f0fba2adSLiam Girdwood 	int i;
1212b9dd94a8SPeter Ujfalusi 
121364ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
121464ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
121564ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
121664ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
121764ab9baaSMark Brown 	 */
1218f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1219f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1220f0fba2adSLiam Girdwood 		if (cpu_dai->driver->ac97_control) {
1221f0fba2adSLiam Girdwood 			dev_dbg(dev, "Resuming AC97 immediately\n");
122264ab9baaSMark Brown 			soc_resume_deferred(&card->deferred_resume_work);
122364ab9baaSMark Brown 		} else {
1224f0fba2adSLiam Girdwood 			dev_dbg(dev, "Scheduling resume work\n");
12256308419aSMark Brown 			if (!schedule_work(&card->deferred_resume_work))
1226f0fba2adSLiam Girdwood 				dev_err(dev, "resume work item may be lost\n");
1227f0fba2adSLiam Girdwood 		}
122864ab9baaSMark Brown 	}
12296ed25978SAndy Green 
1230db2a4165SFrank Mandarino 	return 0;
1231db2a4165SFrank Mandarino }
1232db2a4165SFrank Mandarino #else
1233db2a4165SFrank Mandarino #define soc_suspend	NULL
1234db2a4165SFrank Mandarino #define soc_resume	NULL
1235db2a4165SFrank Mandarino #endif
1236db2a4165SFrank Mandarino 
123702a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
123802a06d30SBarry Song };
123902a06d30SBarry Song 
1240f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1241db2a4165SFrank Mandarino {
1242f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1243f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1244fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1245435c5e25SMark Brown 	struct snd_soc_platform *platform;
1246f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
1247db2a4165SFrank Mandarino 
1248f0fba2adSLiam Girdwood 	if (rtd->complete)
1249f0fba2adSLiam Girdwood 		return 1;
1250f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
12516308419aSMark Brown 
1252f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
1253f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
1254f0fba2adSLiam Girdwood 		goto find_codec;
1255435c5e25SMark Brown 	}
1256f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1257f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
1258f0fba2adSLiam Girdwood 		if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1259f0fba2adSLiam Girdwood 
1260f0fba2adSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
1261f0fba2adSLiam Girdwood 				return -ENODEV;
1262f0fba2adSLiam Girdwood 
1263f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
1264f0fba2adSLiam Girdwood 			goto find_codec;
1265f0fba2adSLiam Girdwood 		}
1266f0fba2adSLiam Girdwood 	}
1267f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
1268f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
1269f0fba2adSLiam Girdwood 
1270f0fba2adSLiam Girdwood find_codec:
1271f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
1272f0fba2adSLiam Girdwood 	if (rtd->codec) {
1273f0fba2adSLiam Girdwood 		goto find_platform;
1274c5af3a2eSMark Brown 	}
1275c5af3a2eSMark Brown 
1276f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
1277f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
1278f0fba2adSLiam Girdwood 		if (!strcmp(codec->name, dai_link->codec_name)) {
1279f0fba2adSLiam Girdwood 			rtd->codec = codec;
1280f0fba2adSLiam Girdwood 
1281f0fba2adSLiam Girdwood 			if (!try_module_get(codec->dev->driver->owner))
1282f0fba2adSLiam Girdwood 				return -ENODEV;
1283f0fba2adSLiam Girdwood 
1284f0fba2adSLiam Girdwood 			/* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1285f0fba2adSLiam Girdwood 			list_for_each_entry(codec_dai, &dai_list, list) {
1286f0fba2adSLiam Girdwood 				if (codec->dev == codec_dai->dev &&
1287f0fba2adSLiam Girdwood 						!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1288f0fba2adSLiam Girdwood 					rtd->codec_dai = codec_dai;
1289f0fba2adSLiam Girdwood 					goto find_platform;
1290435c5e25SMark Brown 				}
1291f0fba2adSLiam Girdwood 			}
1292f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1293f0fba2adSLiam Girdwood 					dai_link->codec_dai_name);
1294f0fba2adSLiam Girdwood 
1295f0fba2adSLiam Girdwood 			goto find_platform;
1296f0fba2adSLiam Girdwood 		}
1297f0fba2adSLiam Girdwood 	}
1298f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
1299f0fba2adSLiam Girdwood 			dai_link->codec_name);
1300f0fba2adSLiam Girdwood 
1301f0fba2adSLiam Girdwood find_platform:
1302f0fba2adSLiam Girdwood 	/* do we already have the CODEC DAI for this link ? */
1303f0fba2adSLiam Girdwood 	if (rtd->platform) {
1304f0fba2adSLiam Girdwood 		goto out;
1305f0fba2adSLiam Girdwood 	}
1306f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1307f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
1308f0fba2adSLiam Girdwood 		if (!strcmp(platform->name, dai_link->platform_name)) {
1309f0fba2adSLiam Girdwood 
1310f0fba2adSLiam Girdwood 			if (!try_module_get(platform->dev->driver->owner))
1311f0fba2adSLiam Girdwood 				return -ENODEV;
1312f0fba2adSLiam Girdwood 
1313f0fba2adSLiam Girdwood 			rtd->platform = platform;
1314f0fba2adSLiam Girdwood 			goto out;
1315f0fba2adSLiam Girdwood 		}
1316435c5e25SMark Brown 	}
13176b05eda6SMark Brown 
1318f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
1319f0fba2adSLiam Girdwood 			dai_link->platform_name);
1320f0fba2adSLiam Girdwood 	return 0;
1321f0fba2adSLiam Girdwood 
1322f0fba2adSLiam Girdwood out:
1323f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
1324f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1325f0fba2adSLiam Girdwood 		rtd->complete = 1;
1326f0fba2adSLiam Girdwood 		card->num_rtd++;
1327f0fba2adSLiam Girdwood 	}
1328f0fba2adSLiam Girdwood 	return 1;
13296b05eda6SMark Brown }
13306b05eda6SMark Brown 
1331589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
1332589c3563SJarkko Nikula {
1333589c3563SJarkko Nikula 	int err;
1334589c3563SJarkko Nikula 
1335589c3563SJarkko Nikula 	if (codec->driver->remove) {
1336589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
1337589c3563SJarkko Nikula 		if (err < 0)
1338589c3563SJarkko Nikula 			dev_err(codec->dev,
1339589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
1340589c3563SJarkko Nikula 				codec->name, err);
1341589c3563SJarkko Nikula 	}
1342589c3563SJarkko Nikula 
1343589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
1344589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
1345589c3563SJarkko Nikula 
1346589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
1347589c3563SJarkko Nikula 	codec->probed = 0;
1348589c3563SJarkko Nikula 	list_del(&codec->card_list);
1349589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
1350589c3563SJarkko Nikula }
1351589c3563SJarkko Nikula 
1352f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1353f0fba2adSLiam Girdwood {
1354f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1355f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1356f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1357f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1358f0fba2adSLiam Girdwood 	int err;
1359f0fba2adSLiam Girdwood 
1360f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1361f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
1362f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1363589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1364f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
1365f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
136602a06d30SBarry Song 	}
136702a06d30SBarry Song 
1368f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
1369f0fba2adSLiam Girdwood 	if (codec_dai && codec_dai->probed) {
1370f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1371f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1372f0fba2adSLiam Girdwood 			if (err < 0)
1373f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1374f0fba2adSLiam Girdwood 		}
1375f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1376f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1377f0fba2adSLiam Girdwood 	}
1378f0fba2adSLiam Girdwood 
1379f0fba2adSLiam Girdwood 	/* remove the platform */
1380f0fba2adSLiam Girdwood 	if (platform && platform->probed) {
1381f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
1382f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
1383f0fba2adSLiam Girdwood 			if (err < 0)
1384f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1385f0fba2adSLiam Girdwood 		}
1386f0fba2adSLiam Girdwood 		platform->probed = 0;
1387f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
1388f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
1389f0fba2adSLiam Girdwood 	}
1390f0fba2adSLiam Girdwood 
1391f0fba2adSLiam Girdwood 	/* remove the CODEC */
1392589c3563SJarkko Nikula 	if (codec && codec->probed)
1393589c3563SJarkko Nikula 		soc_remove_codec(codec);
1394f0fba2adSLiam Girdwood 
1395f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
1396f0fba2adSLiam Girdwood 	if (cpu_dai && cpu_dai->probed) {
1397f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1398f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1399f0fba2adSLiam Girdwood 			if (err < 0)
1400f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1401f0fba2adSLiam Girdwood 		}
1402f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1403f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1404f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
1405f0fba2adSLiam Girdwood 	}
1406f0fba2adSLiam Girdwood }
1407f0fba2adSLiam Girdwood 
1408ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1409ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1410ead9b919SJarkko Nikula {
1411ead9b919SJarkko Nikula 	int i;
1412ead9b919SJarkko Nikula 
1413ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1414ead9b919SJarkko Nikula 		return;
1415ead9b919SJarkko Nikula 
1416ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1417ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1418ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1419ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1420ead9b919SJarkko Nikula 			break;
1421ead9b919SJarkko Nikula 		}
1422ead9b919SJarkko Nikula 	}
1423ead9b919SJarkko Nikula }
1424ead9b919SJarkko Nikula 
1425589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1426589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1427589c3563SJarkko Nikula {
1428589c3563SJarkko Nikula 	int ret = 0;
1429589c3563SJarkko Nikula 
1430589c3563SJarkko Nikula 	codec->card = card;
1431589c3563SJarkko Nikula 	codec->dapm.card = card;
1432589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1433589c3563SJarkko Nikula 
1434589c3563SJarkko Nikula 	if (codec->driver->probe) {
1435589c3563SJarkko Nikula 		ret = codec->driver->probe(codec);
1436589c3563SJarkko Nikula 		if (ret < 0) {
1437589c3563SJarkko Nikula 			dev_err(codec->dev,
1438589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1439589c3563SJarkko Nikula 				codec->name, ret);
1440589c3563SJarkko Nikula 			return ret;
1441589c3563SJarkko Nikula 		}
1442589c3563SJarkko Nikula 	}
1443589c3563SJarkko Nikula 
1444589c3563SJarkko Nikula 	soc_init_codec_debugfs(codec);
1445589c3563SJarkko Nikula 
1446589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1447589c3563SJarkko Nikula 	codec->probed = 1;
1448589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
1449589c3563SJarkko Nikula 
1450589c3563SJarkko Nikula 	return ret;
1451589c3563SJarkko Nikula }
1452589c3563SJarkko Nikula 
1453f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1454f0fba2adSLiam Girdwood 
1455589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1456589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1457589c3563SJarkko Nikula 				   int num, int dailess)
1458589c3563SJarkko Nikula {
1459589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1460589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1461589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1462589c3563SJarkko Nikula 	const char *temp, *name;
1463589c3563SJarkko Nikula 	int ret = 0;
1464589c3563SJarkko Nikula 
1465589c3563SJarkko Nikula 	if (!dailess) {
1466589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1467589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1468589c3563SJarkko Nikula 		name = dai_link->name;
1469589c3563SJarkko Nikula 	} else {
1470589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1471589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1472589c3563SJarkko Nikula 		name = aux_dev->name;
1473589c3563SJarkko Nikula 	}
1474589c3563SJarkko Nikula 
1475589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1476589c3563SJarkko Nikula 	temp = codec->name_prefix;
1477589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1478589c3563SJarkko Nikula 
1479589c3563SJarkko Nikula 	/* do machine specific initialization */
1480589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1481589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1482589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1483589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1484589c3563SJarkko Nikula 	if (ret < 0) {
1485589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1486589c3563SJarkko Nikula 		return ret;
1487589c3563SJarkko Nikula 	}
1488589c3563SJarkko Nikula 	codec->name_prefix = temp;
1489589c3563SJarkko Nikula 
1490589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are instantiated */
1491589c3563SJarkko Nikula 	snd_soc_dapm_new_widgets(&codec->dapm);
1492589c3563SJarkko Nikula 	snd_soc_dapm_sync(&codec->dapm);
1493589c3563SJarkko Nikula 
1494589c3563SJarkko Nikula 	/* register the rtd device */
1495589c3563SJarkko Nikula 	rtd->codec = codec;
1496589c3563SJarkko Nikula 	rtd->card = card;
1497589c3563SJarkko Nikula 	rtd->dev.parent = card->dev;
1498589c3563SJarkko Nikula 	rtd->dev.release = rtd_release;
1499589c3563SJarkko Nikula 	rtd->dev.init_name = name;
1500589c3563SJarkko Nikula 	ret = device_register(&rtd->dev);
1501589c3563SJarkko Nikula 	if (ret < 0) {
1502589c3563SJarkko Nikula 		dev_err(card->dev,
1503589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1504589c3563SJarkko Nikula 		return ret;
1505589c3563SJarkko Nikula 	}
1506589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1507589c3563SJarkko Nikula 
1508589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
1509589c3563SJarkko Nikula 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1510589c3563SJarkko Nikula 	if (ret < 0)
1511589c3563SJarkko Nikula 		dev_err(codec->dev,
1512589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1513589c3563SJarkko Nikula 			ret);
1514589c3563SJarkko Nikula 
1515589c3563SJarkko Nikula 	/* add codec sysfs entries */
1516589c3563SJarkko Nikula 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1517589c3563SJarkko Nikula 	if (ret < 0)
1518589c3563SJarkko Nikula 		dev_err(codec->dev,
1519589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1520589c3563SJarkko Nikula 
1521589c3563SJarkko Nikula 	return 0;
1522589c3563SJarkko Nikula }
1523589c3563SJarkko Nikula 
1524f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1525f0fba2adSLiam Girdwood {
1526f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1527f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1528f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1529f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1530f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1531f0fba2adSLiam Girdwood 	int ret;
1532f0fba2adSLiam Girdwood 
1533f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1534f0fba2adSLiam Girdwood 
1535f0fba2adSLiam Girdwood 	/* config components */
1536f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1537f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1538f0fba2adSLiam Girdwood 	codec_dai->card = card;
1539f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1540f0fba2adSLiam Girdwood 
1541f0fba2adSLiam Girdwood 	/* set default power off timeout */
1542f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1543f0fba2adSLiam Girdwood 
1544f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
1545f0fba2adSLiam Girdwood 	if (!cpu_dai->probed) {
1546f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1547f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1548f0fba2adSLiam Girdwood 			if (ret < 0) {
1549f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1550f0fba2adSLiam Girdwood 						cpu_dai->name);
1551f0fba2adSLiam Girdwood 				return ret;
1552f0fba2adSLiam Girdwood 			}
1553f0fba2adSLiam Girdwood 		}
1554f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1555f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1556f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1557f0fba2adSLiam Girdwood 	}
1558f0fba2adSLiam Girdwood 
1559f0fba2adSLiam Girdwood 	/* probe the CODEC */
1560f0fba2adSLiam Girdwood 	if (!codec->probed) {
1561589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1562589c3563SJarkko Nikula 		if (ret < 0)
1563f0fba2adSLiam Girdwood 			return ret;
1564f0fba2adSLiam Girdwood 	}
1565f0fba2adSLiam Girdwood 
1566f0fba2adSLiam Girdwood 	/* probe the platform */
1567f0fba2adSLiam Girdwood 	if (!platform->probed) {
1568f0fba2adSLiam Girdwood 		if (platform->driver->probe) {
1569f0fba2adSLiam Girdwood 			ret = platform->driver->probe(platform);
1570f0fba2adSLiam Girdwood 			if (ret < 0) {
1571f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe platform %s\n",
1572f0fba2adSLiam Girdwood 						platform->name);
1573f0fba2adSLiam Girdwood 				return ret;
1574f0fba2adSLiam Girdwood 			}
1575f0fba2adSLiam Girdwood 		}
1576f0fba2adSLiam Girdwood 		/* mark platform as probed and add to card platform list */
1577f0fba2adSLiam Girdwood 		platform->probed = 1;
1578f0fba2adSLiam Girdwood 		list_add(&platform->card_list, &card->platform_dev_list);
1579f0fba2adSLiam Girdwood 	}
1580f0fba2adSLiam Girdwood 
1581f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
1582f0fba2adSLiam Girdwood 	if (!codec_dai->probed) {
1583f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1584f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1585f0fba2adSLiam Girdwood 			if (ret < 0) {
1586f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1587f0fba2adSLiam Girdwood 						codec_dai->name);
1588f0fba2adSLiam Girdwood 				return ret;
1589f0fba2adSLiam Girdwood 			}
1590f0fba2adSLiam Girdwood 		}
1591f0fba2adSLiam Girdwood 
1592f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1593f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1594f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1595f0fba2adSLiam Girdwood 	}
1596f0fba2adSLiam Girdwood 
1597f0fba2adSLiam Girdwood 	/* DAPM dai link stream work */
1598f0fba2adSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1599f0fba2adSLiam Girdwood 
1600589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1601589c3563SJarkko Nikula 	if (ret)
1602f0fba2adSLiam Girdwood 		return ret;
1603f0fba2adSLiam Girdwood 
1604f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1605f0fba2adSLiam Girdwood 	if (ret < 0)
1606f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1607f0fba2adSLiam Girdwood 
1608f0fba2adSLiam Girdwood 	/* create the pcm */
1609f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1610f0fba2adSLiam Girdwood 	if (ret < 0) {
1611f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1612f0fba2adSLiam Girdwood 		return ret;
1613f0fba2adSLiam Girdwood 	}
1614f0fba2adSLiam Girdwood 
1615f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1616f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1617f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1618f0fba2adSLiam Girdwood 
1619f0fba2adSLiam Girdwood 	return 0;
1620f0fba2adSLiam Girdwood }
1621f0fba2adSLiam Girdwood 
1622f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1623f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1624f0fba2adSLiam Girdwood {
1625f0fba2adSLiam Girdwood 	int ret;
1626f0fba2adSLiam Girdwood 
1627f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1628f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
16296b05eda6SMark Brown 	 */
1630f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
16310562f788SMika Westerberg 		/*
16320562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
16330562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
16340562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
16350562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
16360562f788SMika Westerberg 		 *
16370562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
16380562f788SMika Westerberg 		 */
16390562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
16400562f788SMika Westerberg 			return 0;
1641f0fba2adSLiam Girdwood 
1642f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1643f0fba2adSLiam Girdwood 		if (ret < 0) {
1644f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1645f0fba2adSLiam Girdwood 			return ret;
1646435c5e25SMark Brown 		}
1647435c5e25SMark Brown 
1648f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1649f0fba2adSLiam Girdwood 	}
1650f0fba2adSLiam Girdwood 	return 0;
1651f0fba2adSLiam Girdwood }
1652435c5e25SMark Brown 
1653f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1654f0fba2adSLiam Girdwood {
1655f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1656f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1657f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1658f0fba2adSLiam Girdwood 	}
1659f0fba2adSLiam Girdwood }
1660f0fba2adSLiam Girdwood #endif
1661435c5e25SMark Brown 
16622eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
16632eea392dSJarkko Nikula {
16642eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
16652eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1666676ad98aSJarkko Nikula 	int ret = -ENODEV;
16672eea392dSJarkko Nikula 
16682eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
16692eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
16702eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
16712eea392dSJarkko Nikula 			if (codec->probed) {
16722eea392dSJarkko Nikula 				dev_err(codec->dev,
16732eea392dSJarkko Nikula 					"asoc: codec already probed");
16742eea392dSJarkko Nikula 				ret = -EBUSY;
16752eea392dSJarkko Nikula 				goto out;
16762eea392dSJarkko Nikula 			}
1677676ad98aSJarkko Nikula 			goto found;
16782eea392dSJarkko Nikula 		}
16792eea392dSJarkko Nikula 	}
1680676ad98aSJarkko Nikula 	/* codec not found */
1681676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1682676ad98aSJarkko Nikula 	goto out;
16832eea392dSJarkko Nikula 
1684676ad98aSJarkko Nikula found:
16852eea392dSJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
16862eea392dSJarkko Nikula 		return -ENODEV;
16872eea392dSJarkko Nikula 
1688589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
16892eea392dSJarkko Nikula 	if (ret < 0)
1690589c3563SJarkko Nikula 		return ret;
16912eea392dSJarkko Nikula 
1692589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
16932eea392dSJarkko Nikula 
16942eea392dSJarkko Nikula out:
16952eea392dSJarkko Nikula 	return ret;
16962eea392dSJarkko Nikula }
16972eea392dSJarkko Nikula 
16982eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
16992eea392dSJarkko Nikula {
17002eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
17012eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
17022eea392dSJarkko Nikula 
17032eea392dSJarkko Nikula 	/* unregister the rtd device */
17042eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1705589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
17062eea392dSJarkko Nikula 		device_unregister(&rtd->dev);
17072eea392dSJarkko Nikula 		rtd->dev_registered = 0;
17082eea392dSJarkko Nikula 	}
17092eea392dSJarkko Nikula 
1710589c3563SJarkko Nikula 	if (codec && codec->probed)
1711589c3563SJarkko Nikula 		soc_remove_codec(codec);
17122eea392dSJarkko Nikula }
17132eea392dSJarkko Nikula 
1714fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1715fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1716fdf0f54dSDimitris Papastamos {
1717fdf0f54dSDimitris Papastamos 	int ret;
1718fdf0f54dSDimitris Papastamos 
1719fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1720fdf0f54dSDimitris Papastamos 		return 0;
1721fdf0f54dSDimitris Papastamos 
1722fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1723fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1724fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1725fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1726fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1727fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1728fdf0f54dSDimitris Papastamos 			ret);
1729fdf0f54dSDimitris Papastamos 		return ret;
1730fdf0f54dSDimitris Papastamos 	}
1731fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1732fdf0f54dSDimitris Papastamos 	return 0;
1733fdf0f54dSDimitris Papastamos }
1734fdf0f54dSDimitris Papastamos 
1735f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1736f0fba2adSLiam Girdwood {
1737f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
1738fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1739fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1740fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
1741f0fba2adSLiam Girdwood 	int ret, i;
174296dd3622SMark Brown 
1743f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1744f0fba2adSLiam Girdwood 
1745f0fba2adSLiam Girdwood 	if (card->instantiated) {
1746f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1747435c5e25SMark Brown 		return;
1748db2a4165SFrank Mandarino 	}
1749db2a4165SFrank Mandarino 
1750f0fba2adSLiam Girdwood 	/* bind DAIs */
1751f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1752f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1753f0fba2adSLiam Girdwood 
1754f0fba2adSLiam Girdwood 	/* bind completed ? */
1755f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1756f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1757f0fba2adSLiam Girdwood 		return;
1758db2a4165SFrank Mandarino 	}
1759db2a4165SFrank Mandarino 
1760fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1761fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1762fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1763fdf0f54dSDimitris Papastamos 			continue;
1764fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1765fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1766fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1767fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1768fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1769fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1770fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1771fdf0f54dSDimitris Papastamos 					break;
1772fdf0f54dSDimitris Papastamos 			}
1773fdf0f54dSDimitris Papastamos 		}
1774fdf0f54dSDimitris Papastamos 		if (i == card->num_configs) {
1775fdf0f54dSDimitris Papastamos 			/* no need to override the compress_type so
1776fdf0f54dSDimitris Papastamos 			 * go ahead and do the standard thing */
1777fdf0f54dSDimitris Papastamos 			ret = snd_soc_init_codec_cache(codec, 0);
1778fdf0f54dSDimitris Papastamos 			if (ret < 0) {
1779fdf0f54dSDimitris Papastamos 				mutex_unlock(&card->mutex);
1780fdf0f54dSDimitris Papastamos 				return;
1781fdf0f54dSDimitris Papastamos 			}
1782fdf0f54dSDimitris Papastamos 			continue;
1783fdf0f54dSDimitris Papastamos 		}
1784fdf0f54dSDimitris Papastamos 		/* override the compress_type with the one supplied in
1785fdf0f54dSDimitris Papastamos 		 * the machine driver */
1786fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1787fdf0f54dSDimitris Papastamos 		if (ret < 0) {
1788fdf0f54dSDimitris Papastamos 			mutex_unlock(&card->mutex);
1789fdf0f54dSDimitris Papastamos 			return;
1790fdf0f54dSDimitris Papastamos 		}
1791fdf0f54dSDimitris Papastamos 	}
1792fdf0f54dSDimitris Papastamos 
1793f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1794f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1795f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1796f0fba2adSLiam Girdwood 	if (ret < 0) {
1797f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1798f0fba2adSLiam Girdwood 			card->name);
1799f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1800f0fba2adSLiam Girdwood 		return;
1801db2a4165SFrank Mandarino 	}
1802f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1803db2a4165SFrank Mandarino 
18041301a964SRandy Dunlap #ifdef CONFIG_PM
18056ed25978SAndy Green 	/* deferred resume work */
18066308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
18071301a964SRandy Dunlap #endif
18086ed25978SAndy Green 
1809f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1810f0fba2adSLiam Girdwood 	if (card->probe) {
1811f0fba2adSLiam Girdwood 		ret = card->probe(pdev);
1812f0fba2adSLiam Girdwood 		if (ret < 0)
1813f0fba2adSLiam Girdwood 			goto card_probe_error;
1814f0fba2adSLiam Girdwood 	}
1815f0fba2adSLiam Girdwood 
1816fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1817f0fba2adSLiam Girdwood 		ret = soc_probe_dai_link(card, i);
1818fe3e78e0SMark Brown 		if (ret < 0) {
1819321de0d0SMark Brown 			pr_err("asoc: failed to instantiate card %s: %d\n",
1820321de0d0SMark Brown 			       card->name, ret);
1821f0fba2adSLiam Girdwood 			goto probe_dai_err;
1822fe3e78e0SMark Brown 		}
1823fe3e78e0SMark Brown 	}
1824fe3e78e0SMark Brown 
18252eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
18262eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
18272eea392dSJarkko Nikula 		if (ret < 0) {
18282eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
18292eea392dSJarkko Nikula 			       card->name, ret);
18302eea392dSJarkko Nikula 			goto probe_aux_dev_err;
18312eea392dSJarkko Nikula 		}
18322eea392dSJarkko Nikula 	}
18332eea392dSJarkko Nikula 
1834f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1835fe3e78e0SMark Brown 		 "%s",  card->name);
1836f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1837f0fba2adSLiam Girdwood 		 "%s", card->name);
1838fe3e78e0SMark Brown 
1839f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1840fe3e78e0SMark Brown 	if (ret < 0) {
1841f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1842f0fba2adSLiam Girdwood 		goto probe_dai_err;
1843fe3e78e0SMark Brown 	}
1844fe3e78e0SMark Brown 
1845fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1846f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1847f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1848f0fba2adSLiam Girdwood 			ret = soc_register_ac97_dai_link(&card->rtd[i]);
1849fe3e78e0SMark Brown 			if (ret < 0) {
1850f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1851f0fba2adSLiam Girdwood 				goto probe_dai_err;
1852fe3e78e0SMark Brown 			}
1853fe3e78e0SMark Brown 		}
1854fe3e78e0SMark Brown #endif
1855fe3e78e0SMark Brown 
1856435c5e25SMark Brown 	card->instantiated = 1;
1857f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1858435c5e25SMark Brown 	return;
1859db2a4165SFrank Mandarino 
18602eea392dSJarkko Nikula probe_aux_dev_err:
18612eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18622eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18632eea392dSJarkko Nikula 
1864f0fba2adSLiam Girdwood probe_dai_err:
1865f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1866f0fba2adSLiam Girdwood 		soc_remove_dai_link(card, i);
1867fe3e78e0SMark Brown 
1868f0fba2adSLiam Girdwood card_probe_error:
186987506549SMark Brown 	if (card->remove)
187087506549SMark Brown 		card->remove(pdev);
1871f0fba2adSLiam Girdwood 
1872f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1873f0fba2adSLiam Girdwood 
1874f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1875435c5e25SMark Brown }
1876db2a4165SFrank Mandarino 
1877435c5e25SMark Brown /*
1878421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1879435c5e25SMark Brown  * client_mutex.
1880435c5e25SMark Brown  */
1881435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1882435c5e25SMark Brown {
1883435c5e25SMark Brown 	struct snd_soc_card *card;
1884435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1885435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1886435c5e25SMark Brown }
1887435c5e25SMark Brown 
1888435c5e25SMark Brown /* probes a new socdev */
1889435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1890435c5e25SMark Brown {
1891f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1892435c5e25SMark Brown 	int ret = 0;
1893435c5e25SMark Brown 
1894435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1895435c5e25SMark Brown 	card->dev = &pdev->dev;
1896f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->dai_dev_list);
1897f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->codec_dev_list);
1898f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->platform_dev_list);
1899f0fba2adSLiam Girdwood 
1900a6052154SJarkko Nikula 	soc_init_card_debugfs(card);
1901a6052154SJarkko Nikula 
1902435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1903435c5e25SMark Brown 	if (ret != 0) {
1904435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1905db2a4165SFrank Mandarino 		return ret;
1906db2a4165SFrank Mandarino 	}
1907db2a4165SFrank Mandarino 
1908435c5e25SMark Brown 	return 0;
1909435c5e25SMark Brown }
1910435c5e25SMark Brown 
1911db2a4165SFrank Mandarino /* removes a socdev */
1912db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1913db2a4165SFrank Mandarino {
1914f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1915db2a4165SFrank Mandarino 	int i;
1916db2a4165SFrank Mandarino 
1917b2dfa62cSGuennadi Liakhovetski 		if (card->instantiated) {
1918965ac42cSLiam Girdwood 
1919f0fba2adSLiam Girdwood 		/* make sure any delayed work runs */
1920f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++) {
1921f0fba2adSLiam Girdwood 			struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1922f0fba2adSLiam Girdwood 			run_delayed_work(&rtd->delayed_work);
1923db2a4165SFrank Mandarino 		}
1924db2a4165SFrank Mandarino 
19252eea392dSJarkko Nikula 		/* remove auxiliary devices */
19262eea392dSJarkko Nikula 		for (i = 0; i < card->num_aux_devs; i++)
19272eea392dSJarkko Nikula 			soc_remove_aux_dev(card, i);
19282eea392dSJarkko Nikula 
1929f0fba2adSLiam Girdwood 		/* remove and free each DAI */
1930f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++)
1931f0fba2adSLiam Girdwood 			soc_remove_dai_link(card, i);
1932f0fba2adSLiam Girdwood 
1933a6052154SJarkko Nikula 		soc_cleanup_card_debugfs(card);
1934a6052154SJarkko Nikula 
1935f0fba2adSLiam Girdwood 		/* remove the card */
193687506549SMark Brown 		if (card->remove)
193787506549SMark Brown 			card->remove(pdev);
1938f0fba2adSLiam Girdwood 
1939f0fba2adSLiam Girdwood 		kfree(card->rtd);
1940f0fba2adSLiam Girdwood 		snd_card_free(card->snd_card);
1941b2dfa62cSGuennadi Liakhovetski 	}
1942c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1943db2a4165SFrank Mandarino 	return 0;
1944db2a4165SFrank Mandarino }
1945db2a4165SFrank Mandarino 
1946416356fcSMark Brown static int soc_poweroff(struct device *dev)
194751737470SMark Brown {
1948416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1949f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1950f0fba2adSLiam Girdwood 	int i;
195151737470SMark Brown 
195251737470SMark Brown 	if (!card->instantiated)
1953416356fcSMark Brown 		return 0;
195451737470SMark Brown 
195551737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
195651737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1957f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1958f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1959f0fba2adSLiam Girdwood 		run_delayed_work(&rtd->delayed_work);
1960f0fba2adSLiam Girdwood 	}
196151737470SMark Brown 
1962f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1963416356fcSMark Brown 
1964416356fcSMark Brown 	return 0;
196551737470SMark Brown }
196651737470SMark Brown 
196747145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1968416356fcSMark Brown 	.suspend = soc_suspend,
1969416356fcSMark Brown 	.resume = soc_resume,
1970416356fcSMark Brown 	.poweroff = soc_poweroff,
1971416356fcSMark Brown };
1972416356fcSMark Brown 
1973db2a4165SFrank Mandarino /* ASoC platform driver */
1974db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1975db2a4165SFrank Mandarino 	.driver		= {
1976db2a4165SFrank Mandarino 		.name		= "soc-audio",
19778b45a209SKay Sievers 		.owner		= THIS_MODULE,
1978416356fcSMark Brown 		.pm		= &soc_pm_ops,
1979db2a4165SFrank Mandarino 	},
1980db2a4165SFrank Mandarino 	.probe		= soc_probe,
1981db2a4165SFrank Mandarino 	.remove		= soc_remove,
1982db2a4165SFrank Mandarino };
1983db2a4165SFrank Mandarino 
1984db2a4165SFrank Mandarino /* create a new pcm */
1985f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1986db2a4165SFrank Mandarino {
1987f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1988f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1989f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1990f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1991db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1992db2a4165SFrank Mandarino 	char new_name[64];
1993db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1994db2a4165SFrank Mandarino 
1995db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
199640ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
1997f0fba2adSLiam Girdwood 			rtd->dai_link->stream_name, codec_dai->name, num);
1998db2a4165SFrank Mandarino 
1999f0fba2adSLiam Girdwood 	if (codec_dai->driver->playback.channels_min)
2000db2a4165SFrank Mandarino 		playback = 1;
2001f0fba2adSLiam Girdwood 	if (codec_dai->driver->capture.channels_min)
2002db2a4165SFrank Mandarino 		capture = 1;
2003db2a4165SFrank Mandarino 
2004f0fba2adSLiam Girdwood 	dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2005f0fba2adSLiam Girdwood 	ret = snd_pcm_new(rtd->card->snd_card, new_name,
2006f0fba2adSLiam Girdwood 			num, playback, capture, &pcm);
2007db2a4165SFrank Mandarino 	if (ret < 0) {
2008f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2009db2a4165SFrank Mandarino 		return ret;
2010db2a4165SFrank Mandarino 	}
2011db2a4165SFrank Mandarino 
2012f0fba2adSLiam Girdwood 	rtd->pcm = pcm;
2013db2a4165SFrank Mandarino 	pcm->private_data = rtd;
2014f0fba2adSLiam Girdwood 	soc_pcm_ops.mmap = platform->driver->ops->mmap;
2015f0fba2adSLiam Girdwood 	soc_pcm_ops.pointer = platform->driver->ops->pointer;
2016f0fba2adSLiam Girdwood 	soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2017f0fba2adSLiam Girdwood 	soc_pcm_ops.copy = platform->driver->ops->copy;
2018f0fba2adSLiam Girdwood 	soc_pcm_ops.silence = platform->driver->ops->silence;
2019f0fba2adSLiam Girdwood 	soc_pcm_ops.ack = platform->driver->ops->ack;
2020f0fba2adSLiam Girdwood 	soc_pcm_ops.page = platform->driver->ops->page;
2021db2a4165SFrank Mandarino 
2022db2a4165SFrank Mandarino 	if (playback)
2023db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2024db2a4165SFrank Mandarino 
2025db2a4165SFrank Mandarino 	if (capture)
2026db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2027db2a4165SFrank Mandarino 
2028f0fba2adSLiam Girdwood 	ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
2029db2a4165SFrank Mandarino 	if (ret < 0) {
2030db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
2031db2a4165SFrank Mandarino 		return ret;
2032db2a4165SFrank Mandarino 	}
2033db2a4165SFrank Mandarino 
2034f0fba2adSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
2035db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2036db2a4165SFrank Mandarino 		cpu_dai->name);
2037db2a4165SFrank Mandarino 	return ret;
2038db2a4165SFrank Mandarino }
2039db2a4165SFrank Mandarino 
2040096e49d5SMark Brown /**
2041096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
2042096e49d5SMark Brown  *
2043096e49d5SMark Brown  * @codec: CODEC to query.
2044096e49d5SMark Brown  * @reg: Register to query.
2045096e49d5SMark Brown  *
2046096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
2047096e49d5SMark Brown  */
2048096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
2049096e49d5SMark Brown {
2050f0fba2adSLiam Girdwood 	if (codec->driver->volatile_register)
2051f0fba2adSLiam Girdwood 		return codec->driver->volatile_register(reg);
2052096e49d5SMark Brown 	else
2053096e49d5SMark Brown 		return 0;
2054096e49d5SMark Brown }
2055096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2056096e49d5SMark Brown 
2057db2a4165SFrank Mandarino /**
2058db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2059db2a4165SFrank Mandarino  * @codec: audio codec
2060db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2061db2a4165SFrank Mandarino  * @num: AC97 codec number
2062db2a4165SFrank Mandarino  *
2063db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2064db2a4165SFrank Mandarino  */
2065db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2066db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2067db2a4165SFrank Mandarino {
2068db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2069db2a4165SFrank Mandarino 
2070db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2071db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2072db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2073db2a4165SFrank Mandarino 		return -ENOMEM;
2074db2a4165SFrank Mandarino 	}
2075db2a4165SFrank Mandarino 
2076db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2077db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2078db2a4165SFrank Mandarino 		kfree(codec->ac97);
2079db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2080db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2081db2a4165SFrank Mandarino 		return -ENOMEM;
2082db2a4165SFrank Mandarino 	}
2083db2a4165SFrank Mandarino 
2084db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2085db2a4165SFrank Mandarino 	codec->ac97->num = num;
20860562f788SMika Westerberg 
20870562f788SMika Westerberg 	/*
20880562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
20890562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
20900562f788SMika Westerberg 	 */
20910562f788SMika Westerberg 	codec->ac97_created = 1;
20920562f788SMika Westerberg 
2093db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2094db2a4165SFrank Mandarino 	return 0;
2095db2a4165SFrank Mandarino }
2096db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2097db2a4165SFrank Mandarino 
2098db2a4165SFrank Mandarino /**
2099db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2100db2a4165SFrank Mandarino  * @codec: audio codec
2101db2a4165SFrank Mandarino  *
2102db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2103db2a4165SFrank Mandarino  */
2104db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2105db2a4165SFrank Mandarino {
2106db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2107f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2108f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2109f0fba2adSLiam Girdwood #endif
2110db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2111db2a4165SFrank Mandarino 	kfree(codec->ac97);
2112db2a4165SFrank Mandarino 	codec->ac97 = NULL;
21130562f788SMika Westerberg 	codec->ac97_created = 0;
2114db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2115db2a4165SFrank Mandarino }
2116db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2117db2a4165SFrank Mandarino 
2118c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2119c3753707SMark Brown {
2120c3753707SMark Brown 	unsigned int ret;
2121c3753707SMark Brown 
2122c3acec26SMark Brown 	ret = codec->read(codec, reg);
2123c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2124a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2125c3753707SMark Brown 
2126c3753707SMark Brown 	return ret;
2127c3753707SMark Brown }
2128c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2129c3753707SMark Brown 
2130c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2131c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2132c3753707SMark Brown {
2133c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2134a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2135c3acec26SMark Brown 	return codec->write(codec, reg, val);
2136c3753707SMark Brown }
2137c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2138c3753707SMark Brown 
2139db2a4165SFrank Mandarino /**
2140db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2141db2a4165SFrank Mandarino  * @codec: audio codec
2142db2a4165SFrank Mandarino  * @reg: codec register
2143db2a4165SFrank Mandarino  * @mask: register mask
2144db2a4165SFrank Mandarino  * @value: new value
2145db2a4165SFrank Mandarino  *
2146db2a4165SFrank Mandarino  * Writes new register value.
2147db2a4165SFrank Mandarino  *
2148db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2149db2a4165SFrank Mandarino  */
2150db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
215146f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2152db2a4165SFrank Mandarino {
2153db2a4165SFrank Mandarino 	int change;
215446f5822fSDaniel Ribeiro 	unsigned int old, new;
2155db2a4165SFrank Mandarino 
2156db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2157db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2158db2a4165SFrank Mandarino 	change = old != new;
2159db2a4165SFrank Mandarino 	if (change)
2160db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
2161db2a4165SFrank Mandarino 
2162db2a4165SFrank Mandarino 	return change;
2163db2a4165SFrank Mandarino }
2164db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2165db2a4165SFrank Mandarino 
2166db2a4165SFrank Mandarino /**
21676c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
21686c508c62SEero Nurkkala  * @codec: audio codec
21696c508c62SEero Nurkkala  * @reg: codec register
21706c508c62SEero Nurkkala  * @mask: register mask
21716c508c62SEero Nurkkala  * @value: new value
21726c508c62SEero Nurkkala  *
21736c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
21746c508c62SEero Nurkkala  *
21756c508c62SEero Nurkkala  * Returns 1 for change else 0.
21766c508c62SEero Nurkkala  */
2177dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
21786c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
21796c508c62SEero Nurkkala 			       unsigned int value)
21806c508c62SEero Nurkkala {
21816c508c62SEero Nurkkala 	int change;
21826c508c62SEero Nurkkala 
21836c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
21846c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
21856c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
21866c508c62SEero Nurkkala 
21876c508c62SEero Nurkkala 	return change;
21886c508c62SEero Nurkkala }
2189dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
21906c508c62SEero Nurkkala 
21916c508c62SEero Nurkkala /**
2192db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2193db2a4165SFrank Mandarino  * @codec: audio codec
2194db2a4165SFrank Mandarino  * @reg: codec register
2195db2a4165SFrank Mandarino  * @mask: register mask
2196db2a4165SFrank Mandarino  * @value: new value
2197db2a4165SFrank Mandarino  *
2198db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2199db2a4165SFrank Mandarino  * different from the old value.
2200db2a4165SFrank Mandarino  *
2201db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2202db2a4165SFrank Mandarino  */
2203db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
220446f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2205db2a4165SFrank Mandarino {
2206db2a4165SFrank Mandarino 	int change;
220746f5822fSDaniel Ribeiro 	unsigned int old, new;
2208db2a4165SFrank Mandarino 
2209db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2210db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2211db2a4165SFrank Mandarino 	change = old != new;
2212db2a4165SFrank Mandarino 
2213db2a4165SFrank Mandarino 	return change;
2214db2a4165SFrank Mandarino }
2215db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2216db2a4165SFrank Mandarino 
2217db2a4165SFrank Mandarino /**
2218db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2219db2a4165SFrank Mandarino  * @substream: the pcm substream
2220db2a4165SFrank Mandarino  * @hw: the hardware parameters
2221db2a4165SFrank Mandarino  *
2222db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2223db2a4165SFrank Mandarino  */
2224db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2225db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2226db2a4165SFrank Mandarino {
2227db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2228db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2229db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2230db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2231db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2232db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2233db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2234db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2235db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2236db2a4165SFrank Mandarino 	return 0;
2237db2a4165SFrank Mandarino }
2238db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2239db2a4165SFrank Mandarino 
2240db2a4165SFrank Mandarino /**
2241db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2242db2a4165SFrank Mandarino  * @_template: control template
2243db2a4165SFrank Mandarino  * @data: control private data
2244ac11a2b3SMark Brown  * @long_name: control long name
2245db2a4165SFrank Mandarino  *
2246db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2247db2a4165SFrank Mandarino  *
2248db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2249db2a4165SFrank Mandarino  */
2250db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2251db2a4165SFrank Mandarino 	void *data, char *long_name)
2252db2a4165SFrank Mandarino {
2253db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2254db2a4165SFrank Mandarino 
2255db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2256db2a4165SFrank Mandarino 	if (long_name)
2257db2a4165SFrank Mandarino 		template.name = long_name;
2258db2a4165SFrank Mandarino 	template.index = 0;
2259db2a4165SFrank Mandarino 
2260db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
2261db2a4165SFrank Mandarino }
2262db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2263db2a4165SFrank Mandarino 
2264db2a4165SFrank Mandarino /**
22653e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
22663e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
22673e8e1952SIan Molton  * duplicating this code.
22683e8e1952SIan Molton  *
22693e8e1952SIan Molton  * @codec: codec to add controls to
22703e8e1952SIan Molton  * @controls: array of controls to add
22713e8e1952SIan Molton  * @num_controls: number of elements in the array
22723e8e1952SIan Molton  *
22733e8e1952SIan Molton  * Return 0 for success, else error.
22743e8e1952SIan Molton  */
22753e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
22763e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
22773e8e1952SIan Molton {
2278f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2279ead9b919SJarkko Nikula 	char prefixed_name[44], *name;
22803e8e1952SIan Molton 	int err, i;
22813e8e1952SIan Molton 
22823e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
22833e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
2284ead9b919SJarkko Nikula 		if (codec->name_prefix) {
2285ead9b919SJarkko Nikula 			snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2286ead9b919SJarkko Nikula 				 codec->name_prefix, control->name);
2287ead9b919SJarkko Nikula 			name = prefixed_name;
2288ead9b919SJarkko Nikula 		} else {
2289ead9b919SJarkko Nikula 			name = control->name;
2290ead9b919SJarkko Nikula 		}
2291ead9b919SJarkko Nikula 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
22923e8e1952SIan Molton 		if (err < 0) {
2293082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2294ead9b919SJarkko Nikula 				codec->name, name, err);
22953e8e1952SIan Molton 			return err;
22963e8e1952SIan Molton 		}
22973e8e1952SIan Molton 	}
22983e8e1952SIan Molton 
22993e8e1952SIan Molton 	return 0;
23003e8e1952SIan Molton }
23013e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
23023e8e1952SIan Molton 
23033e8e1952SIan Molton /**
2304db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2305db2a4165SFrank Mandarino  * @kcontrol: mixer control
2306db2a4165SFrank Mandarino  * @uinfo: control element information
2307db2a4165SFrank Mandarino  *
2308db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2309db2a4165SFrank Mandarino  * mixer control.
2310db2a4165SFrank Mandarino  *
2311db2a4165SFrank Mandarino  * Returns 0 for success.
2312db2a4165SFrank Mandarino  */
2313db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2314db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2315db2a4165SFrank Mandarino {
2316db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2317db2a4165SFrank Mandarino 
2318db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2319db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2320f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2321db2a4165SFrank Mandarino 
2322f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2323f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2324db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2325db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2326db2a4165SFrank Mandarino 	return 0;
2327db2a4165SFrank Mandarino }
2328db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2329db2a4165SFrank Mandarino 
2330db2a4165SFrank Mandarino /**
2331db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2332db2a4165SFrank Mandarino  * @kcontrol: mixer control
2333ac11a2b3SMark Brown  * @ucontrol: control element information
2334db2a4165SFrank Mandarino  *
2335db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2336db2a4165SFrank Mandarino  *
2337db2a4165SFrank Mandarino  * Returns 0 for success.
2338db2a4165SFrank Mandarino  */
2339db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2340db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2341db2a4165SFrank Mandarino {
2342db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2343db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
234446f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2345db2a4165SFrank Mandarino 
2346f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2347db2a4165SFrank Mandarino 		;
2348db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
23493ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
23503ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2351db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2352db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2353db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2354db2a4165SFrank Mandarino 
2355db2a4165SFrank Mandarino 	return 0;
2356db2a4165SFrank Mandarino }
2357db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2358db2a4165SFrank Mandarino 
2359db2a4165SFrank Mandarino /**
2360db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2361db2a4165SFrank Mandarino  * @kcontrol: mixer control
2362ac11a2b3SMark Brown  * @ucontrol: control element information
2363db2a4165SFrank Mandarino  *
2364db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2365db2a4165SFrank Mandarino  *
2366db2a4165SFrank Mandarino  * Returns 0 for success.
2367db2a4165SFrank Mandarino  */
2368db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2369db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2370db2a4165SFrank Mandarino {
2371db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2372db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
237346f5822fSDaniel Ribeiro 	unsigned int val;
237446f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2375db2a4165SFrank Mandarino 
2376f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2377db2a4165SFrank Mandarino 		;
2378f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2379db2a4165SFrank Mandarino 		return -EINVAL;
2380db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2381db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2382db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2383f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2384db2a4165SFrank Mandarino 			return -EINVAL;
2385db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2386db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2387db2a4165SFrank Mandarino 	}
2388db2a4165SFrank Mandarino 
23896c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2390db2a4165SFrank Mandarino }
2391db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2392db2a4165SFrank Mandarino 
2393db2a4165SFrank Mandarino /**
23942e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
23952e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23962e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23972e72f8e3SPeter Ujfalusi  *
23982e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
23992e72f8e3SPeter Ujfalusi  *
24002e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24012e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24022e72f8e3SPeter Ujfalusi  *
24032e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24042e72f8e3SPeter Ujfalusi  */
24052e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
24062e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
24072e72f8e3SPeter Ujfalusi {
24082e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
240974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
241046f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
24112e72f8e3SPeter Ujfalusi 
24122e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
24132e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
24142e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
24152e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
24162e72f8e3SPeter Ujfalusi 			break;
24172e72f8e3SPeter Ujfalusi 	}
24182e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
24192e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24202e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
24212e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
24222e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
24232e72f8e3SPeter Ujfalusi 				break;
24242e72f8e3SPeter Ujfalusi 		}
24252e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
24262e72f8e3SPeter Ujfalusi 	}
24272e72f8e3SPeter Ujfalusi 
24282e72f8e3SPeter Ujfalusi 	return 0;
24292e72f8e3SPeter Ujfalusi }
24302e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
24312e72f8e3SPeter Ujfalusi 
24322e72f8e3SPeter Ujfalusi /**
24332e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
24342e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
24352e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
24362e72f8e3SPeter Ujfalusi  *
24372e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
24382e72f8e3SPeter Ujfalusi  *
24392e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24402e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24412e72f8e3SPeter Ujfalusi  *
24422e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24432e72f8e3SPeter Ujfalusi  */
24442e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
24452e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
24462e72f8e3SPeter Ujfalusi {
24472e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
244874155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
244946f5822fSDaniel Ribeiro 	unsigned int val;
245046f5822fSDaniel Ribeiro 	unsigned int mask;
24512e72f8e3SPeter Ujfalusi 
24522e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
24532e72f8e3SPeter Ujfalusi 		return -EINVAL;
24542e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
24552e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
24562e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24572e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
24582e72f8e3SPeter Ujfalusi 			return -EINVAL;
24592e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
24602e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
24612e72f8e3SPeter Ujfalusi 	}
24622e72f8e3SPeter Ujfalusi 
24636c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
24642e72f8e3SPeter Ujfalusi }
24652e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
24662e72f8e3SPeter Ujfalusi 
24672e72f8e3SPeter Ujfalusi /**
2468db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2469db2a4165SFrank Mandarino  * @kcontrol: mixer control
2470db2a4165SFrank Mandarino  * @uinfo: control element information
2471db2a4165SFrank Mandarino  *
2472db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2473db2a4165SFrank Mandarino  * single mixer.
2474db2a4165SFrank Mandarino  *
2475db2a4165SFrank Mandarino  * Returns 0 for success.
2476db2a4165SFrank Mandarino  */
2477db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2478db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2479db2a4165SFrank Mandarino {
2480db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2481db2a4165SFrank Mandarino 
2482db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2483db2a4165SFrank Mandarino 	uinfo->count = 1;
2484f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2485db2a4165SFrank Mandarino 
2486f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2487f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2488db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2489db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2490db2a4165SFrank Mandarino 	return 0;
2491db2a4165SFrank Mandarino }
2492db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2493db2a4165SFrank Mandarino 
2494db2a4165SFrank Mandarino /**
2495db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2496db2a4165SFrank Mandarino  * @kcontrol: mixer control
2497db2a4165SFrank Mandarino  * @uinfo: control element information
2498db2a4165SFrank Mandarino  *
2499db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2500db2a4165SFrank Mandarino  *
2501db2a4165SFrank Mandarino  * Returns 0 for success.
2502db2a4165SFrank Mandarino  */
2503db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2504db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2505db2a4165SFrank Mandarino {
2506a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2507db2a4165SFrank Mandarino 
2508fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2509a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2510a7a4ac86SPhilipp Zabel 	else
2511a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2512a7a4ac86SPhilipp Zabel 
2513db2a4165SFrank Mandarino 	uinfo->count = 1;
2514db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2515a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2516db2a4165SFrank Mandarino 	return 0;
2517db2a4165SFrank Mandarino }
2518db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2519db2a4165SFrank Mandarino 
2520db2a4165SFrank Mandarino /**
2521db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2522db2a4165SFrank Mandarino  * @kcontrol: mixer control
2523db2a4165SFrank Mandarino  * @uinfo: control element information
2524db2a4165SFrank Mandarino  *
2525db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2526db2a4165SFrank Mandarino  *
2527db2a4165SFrank Mandarino  * Returns 0 for success.
2528db2a4165SFrank Mandarino  */
2529db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2530db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2531db2a4165SFrank Mandarino {
25324eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25334eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2534d11bb4a9SPeter Ujfalusi 	int platform_max;
2535762b8df7SMark Brown 	unsigned int shift = mc->shift;
2536815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2537db2a4165SFrank Mandarino 
2538d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2539d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2540d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2541d11bb4a9SPeter Ujfalusi 
2542d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2543a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2544a7a4ac86SPhilipp Zabel 	else
2545a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2546a7a4ac86SPhilipp Zabel 
2547db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2548db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2549d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2550db2a4165SFrank Mandarino 	return 0;
2551db2a4165SFrank Mandarino }
2552db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2553db2a4165SFrank Mandarino 
2554db2a4165SFrank Mandarino /**
2555db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2556db2a4165SFrank Mandarino  * @kcontrol: mixer control
2557ac11a2b3SMark Brown  * @ucontrol: control element information
2558db2a4165SFrank Mandarino  *
2559db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2560db2a4165SFrank Mandarino  *
2561db2a4165SFrank Mandarino  * Returns 0 for success.
2562db2a4165SFrank Mandarino  */
2563db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2564db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2565db2a4165SFrank Mandarino {
25664eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25674eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2568db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2569815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2570815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2571815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25724eaa9819SJon Smirl 	int max = mc->max;
2573815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2574815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2575db2a4165SFrank Mandarino 
2576db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2577db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2578db2a4165SFrank Mandarino 	if (shift != rshift)
2579db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2580db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2581db2a4165SFrank Mandarino 	if (invert) {
2582db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2583a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2584db2a4165SFrank Mandarino 		if (shift != rshift)
2585db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2586a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2587db2a4165SFrank Mandarino 	}
2588db2a4165SFrank Mandarino 
2589db2a4165SFrank Mandarino 	return 0;
2590db2a4165SFrank Mandarino }
2591db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2592db2a4165SFrank Mandarino 
2593db2a4165SFrank Mandarino /**
2594db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2595db2a4165SFrank Mandarino  * @kcontrol: mixer control
2596ac11a2b3SMark Brown  * @ucontrol: control element information
2597db2a4165SFrank Mandarino  *
2598db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2599db2a4165SFrank Mandarino  *
2600db2a4165SFrank Mandarino  * Returns 0 for success.
2601db2a4165SFrank Mandarino  */
2602db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2603db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2604db2a4165SFrank Mandarino {
26054eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26064eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2607db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2608815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2609815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2610815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26114eaa9819SJon Smirl 	int max = mc->max;
2612815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2613815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
261446f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2615db2a4165SFrank Mandarino 
2616db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2617db2a4165SFrank Mandarino 	if (invert)
2618a7a4ac86SPhilipp Zabel 		val = max - val;
2619db2a4165SFrank Mandarino 	val_mask = mask << shift;
2620db2a4165SFrank Mandarino 	val = val << shift;
2621db2a4165SFrank Mandarino 	if (shift != rshift) {
2622db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2623db2a4165SFrank Mandarino 		if (invert)
2624a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2625db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2626db2a4165SFrank Mandarino 		val |= val2 << rshift;
2627db2a4165SFrank Mandarino 	}
26286c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2629db2a4165SFrank Mandarino }
2630db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2631db2a4165SFrank Mandarino 
2632db2a4165SFrank Mandarino /**
2633db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2634db2a4165SFrank Mandarino  * @kcontrol: mixer control
2635db2a4165SFrank Mandarino  * @uinfo: control element information
2636db2a4165SFrank Mandarino  *
2637db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2638db2a4165SFrank Mandarino  * spans 2 codec registers.
2639db2a4165SFrank Mandarino  *
2640db2a4165SFrank Mandarino  * Returns 0 for success.
2641db2a4165SFrank Mandarino  */
2642db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2643db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2644db2a4165SFrank Mandarino {
26454eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26464eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2647d11bb4a9SPeter Ujfalusi 	int platform_max;
2648db2a4165SFrank Mandarino 
2649d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2650d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2651d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2652d11bb4a9SPeter Ujfalusi 
2653d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2654a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2655a7a4ac86SPhilipp Zabel 	else
2656a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2657a7a4ac86SPhilipp Zabel 
2658db2a4165SFrank Mandarino 	uinfo->count = 2;
2659db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2660d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2661db2a4165SFrank Mandarino 	return 0;
2662db2a4165SFrank Mandarino }
2663db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2664db2a4165SFrank Mandarino 
2665db2a4165SFrank Mandarino /**
2666db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2667db2a4165SFrank Mandarino  * @kcontrol: mixer control
2668ac11a2b3SMark Brown  * @ucontrol: control element information
2669db2a4165SFrank Mandarino  *
2670db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2671db2a4165SFrank Mandarino  *
2672db2a4165SFrank Mandarino  * Returns 0 for success.
2673db2a4165SFrank Mandarino  */
2674db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2675db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2676db2a4165SFrank Mandarino {
26774eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26784eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2679db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2680815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2681815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2682815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
26834eaa9819SJon Smirl 	int max = mc->max;
2684815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2685815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2686db2a4165SFrank Mandarino 
2687db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2688db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2689db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2690db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2691db2a4165SFrank Mandarino 	if (invert) {
2692db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2693a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2694db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2695a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2696db2a4165SFrank Mandarino 	}
2697db2a4165SFrank Mandarino 
2698db2a4165SFrank Mandarino 	return 0;
2699db2a4165SFrank Mandarino }
2700db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2701db2a4165SFrank Mandarino 
2702db2a4165SFrank Mandarino /**
2703db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2704db2a4165SFrank Mandarino  * @kcontrol: mixer control
2705ac11a2b3SMark Brown  * @ucontrol: control element information
2706db2a4165SFrank Mandarino  *
2707db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2708db2a4165SFrank Mandarino  *
2709db2a4165SFrank Mandarino  * Returns 0 for success.
2710db2a4165SFrank Mandarino  */
2711db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2712db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2713db2a4165SFrank Mandarino {
27144eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27154eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2716db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2717815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2718815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2719815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
27204eaa9819SJon Smirl 	int max = mc->max;
2721815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2722815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2723db2a4165SFrank Mandarino 	int err;
272446f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2725db2a4165SFrank Mandarino 
2726db2a4165SFrank Mandarino 	val_mask = mask << shift;
2727db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2728db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2729db2a4165SFrank Mandarino 
2730db2a4165SFrank Mandarino 	if (invert) {
2731a7a4ac86SPhilipp Zabel 		val = max - val;
2732a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2733db2a4165SFrank Mandarino 	}
2734db2a4165SFrank Mandarino 
2735db2a4165SFrank Mandarino 	val = val << shift;
2736db2a4165SFrank Mandarino 	val2 = val2 << shift;
2737db2a4165SFrank Mandarino 
27386c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
27393ff3f64bSMark Brown 	if (err < 0)
2740db2a4165SFrank Mandarino 		return err;
2741db2a4165SFrank Mandarino 
27426c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2743db2a4165SFrank Mandarino 	return err;
2744db2a4165SFrank Mandarino }
2745db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2746db2a4165SFrank Mandarino 
2747e13ac2e9SMark Brown /**
2748e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2749e13ac2e9SMark Brown  * @kcontrol: mixer control
2750e13ac2e9SMark Brown  * @uinfo: control element information
2751e13ac2e9SMark Brown  *
2752e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2753e13ac2e9SMark Brown  *
2754e13ac2e9SMark Brown  * Returns 0 for success.
2755e13ac2e9SMark Brown  */
2756e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2757e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2758e13ac2e9SMark Brown {
27594eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27604eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2761d11bb4a9SPeter Ujfalusi 	int platform_max;
27624eaa9819SJon Smirl 	int min = mc->min;
2763e13ac2e9SMark Brown 
2764d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2765d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2766d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2767d11bb4a9SPeter Ujfalusi 
2768e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2769e13ac2e9SMark Brown 	uinfo->count = 2;
2770e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2771d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2772e13ac2e9SMark Brown 	return 0;
2773e13ac2e9SMark Brown }
2774e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2775e13ac2e9SMark Brown 
2776e13ac2e9SMark Brown /**
2777e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2778e13ac2e9SMark Brown  * @kcontrol: mixer control
2779ac11a2b3SMark Brown  * @ucontrol: control element information
2780e13ac2e9SMark Brown  *
2781e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2782e13ac2e9SMark Brown  *
2783e13ac2e9SMark Brown  * Returns 0 for success.
2784e13ac2e9SMark Brown  */
2785e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2786e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2787e13ac2e9SMark Brown {
27884eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27894eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2790e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2791815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27924eaa9819SJon Smirl 	int min = mc->min;
2793e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2794e13ac2e9SMark Brown 
2795e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2796e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2797e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2798e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2799e13ac2e9SMark Brown 	return 0;
2800e13ac2e9SMark Brown }
2801e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2802e13ac2e9SMark Brown 
2803e13ac2e9SMark Brown /**
2804e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2805e13ac2e9SMark Brown  * @kcontrol: mixer control
2806ac11a2b3SMark Brown  * @ucontrol: control element information
2807e13ac2e9SMark Brown  *
2808e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2809e13ac2e9SMark Brown  *
2810e13ac2e9SMark Brown  * Returns 0 for success.
2811e13ac2e9SMark Brown  */
2812e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2813e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2814e13ac2e9SMark Brown {
28154eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28164eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2817e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2818815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
28194eaa9819SJon Smirl 	int min = mc->min;
282046f5822fSDaniel Ribeiro 	unsigned int val;
2821e13ac2e9SMark Brown 
2822e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2823e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2824e13ac2e9SMark Brown 
28256c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2826e13ac2e9SMark Brown }
2827e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2828e13ac2e9SMark Brown 
28298c6529dbSLiam Girdwood /**
2830637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2831637d3847SPeter Ujfalusi  *
2832637d3847SPeter Ujfalusi  * @codec: where to look for the control
2833637d3847SPeter Ujfalusi  * @name: Name of the control
2834637d3847SPeter Ujfalusi  * @max: new maximum limit
2835637d3847SPeter Ujfalusi  *
2836637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2837637d3847SPeter Ujfalusi  */
2838637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2839637d3847SPeter Ujfalusi 	const char *name, int max)
2840637d3847SPeter Ujfalusi {
2841f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2842637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2843637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2844637d3847SPeter Ujfalusi 	int found = 0;
2845637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2846637d3847SPeter Ujfalusi 
2847637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2848637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2849637d3847SPeter Ujfalusi 		return -EINVAL;
2850637d3847SPeter Ujfalusi 
2851637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2852637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2853637d3847SPeter Ujfalusi 			found = 1;
2854637d3847SPeter Ujfalusi 			break;
2855637d3847SPeter Ujfalusi 		}
2856637d3847SPeter Ujfalusi 	}
2857637d3847SPeter Ujfalusi 	if (found) {
2858637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2859637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2860d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2861637d3847SPeter Ujfalusi 			ret = 0;
2862637d3847SPeter Ujfalusi 		}
2863637d3847SPeter Ujfalusi 	}
2864637d3847SPeter Ujfalusi 	return ret;
2865637d3847SPeter Ujfalusi }
2866637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2867637d3847SPeter Ujfalusi 
2868637d3847SPeter Ujfalusi /**
2869b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2870b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2871b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2872b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2873b6f4bb38Sapatard@mandriva.com  *
2874b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2875b6f4bb38Sapatard@mandriva.com  */
2876b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2877b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2878b6f4bb38Sapatard@mandriva.com {
2879b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2880b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2881b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2882b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2883b6f4bb38Sapatard@mandriva.com 
2884b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2885b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2886b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2887b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2888b6f4bb38Sapatard@mandriva.com 
2889b6f4bb38Sapatard@mandriva.com 	return 0;
2890b6f4bb38Sapatard@mandriva.com }
2891b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2892b6f4bb38Sapatard@mandriva.com 
2893b6f4bb38Sapatard@mandriva.com /**
2894b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2895b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2896b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2897b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2898b6f4bb38Sapatard@mandriva.com  *
2899b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2900b6f4bb38Sapatard@mandriva.com  */
2901b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2902b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2903b6f4bb38Sapatard@mandriva.com {
2904b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2905b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2906b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2907b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2908b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2909b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2910b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2911b6f4bb38Sapatard@mandriva.com 
291220630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
291320630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2914b6f4bb38Sapatard@mandriva.com 	return 0;
2915b6f4bb38Sapatard@mandriva.com }
2916b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2917b6f4bb38Sapatard@mandriva.com 
2918b6f4bb38Sapatard@mandriva.com /**
2919b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2920b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2921b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2922b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2923b6f4bb38Sapatard@mandriva.com  *
2924b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2925b6f4bb38Sapatard@mandriva.com  */
2926b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2927b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2928b6f4bb38Sapatard@mandriva.com {
2929b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2930b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2931b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2932b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2933b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2934b6f4bb38Sapatard@mandriva.com 	int ret;
2935b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2936b6f4bb38Sapatard@mandriva.com 
2937b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2938b6f4bb38Sapatard@mandriva.com 	val &= mask;
2939b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2940b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2941b6f4bb38Sapatard@mandriva.com 
2942b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2943b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2944b6f4bb38Sapatard@mandriva.com 
2945b6f4bb38Sapatard@mandriva.com 	ret = 0;
2946b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2947b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2948b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2949f1df5aecSMark Brown 			return ret;
2950b6f4bb38Sapatard@mandriva.com 	}
2951b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2952b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2953b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2954f1df5aecSMark Brown 			return ret;
2955b6f4bb38Sapatard@mandriva.com 	}
2956b6f4bb38Sapatard@mandriva.com 
2957b6f4bb38Sapatard@mandriva.com 	return 0;
2958b6f4bb38Sapatard@mandriva.com }
2959b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2960b6f4bb38Sapatard@mandriva.com 
2961b6f4bb38Sapatard@mandriva.com /**
29628c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
29638c6529dbSLiam Girdwood  * @dai: DAI
29648c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
29658c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
29668c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
29678c6529dbSLiam Girdwood  *
29688c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
29698c6529dbSLiam Girdwood  */
29708c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
29718c6529dbSLiam Girdwood 	unsigned int freq, int dir)
29728c6529dbSLiam Girdwood {
2973f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2974f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
29758c6529dbSLiam Girdwood 	else
29768c6529dbSLiam Girdwood 		return -EINVAL;
29778c6529dbSLiam Girdwood }
29788c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
29798c6529dbSLiam Girdwood 
29808c6529dbSLiam Girdwood /**
29818c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
29828c6529dbSLiam Girdwood  * @dai: DAI
2983ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
29848c6529dbSLiam Girdwood  * @div: new clock divisor.
29858c6529dbSLiam Girdwood  *
29868c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
29878c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
29888c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
29898c6529dbSLiam Girdwood  */
29908c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
29918c6529dbSLiam Girdwood 	int div_id, int div)
29928c6529dbSLiam Girdwood {
2993f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2994f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
29958c6529dbSLiam Girdwood 	else
29968c6529dbSLiam Girdwood 		return -EINVAL;
29978c6529dbSLiam Girdwood }
29988c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
29998c6529dbSLiam Girdwood 
30008c6529dbSLiam Girdwood /**
30018c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
30028c6529dbSLiam Girdwood  * @dai: DAI
30038c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
300485488037SMark Brown  * @source: DAI specific source for the PLL
30058c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
30068c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
30078c6529dbSLiam Girdwood  *
30088c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
30098c6529dbSLiam Girdwood  */
301085488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
301185488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
30128c6529dbSLiam Girdwood {
3013f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3014f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
301585488037SMark Brown 					 freq_in, freq_out);
30168c6529dbSLiam Girdwood 	else
30178c6529dbSLiam Girdwood 		return -EINVAL;
30188c6529dbSLiam Girdwood }
30198c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
30208c6529dbSLiam Girdwood 
30218c6529dbSLiam Girdwood /**
30228c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
30238c6529dbSLiam Girdwood  * @dai: DAI
30248c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
30258c6529dbSLiam Girdwood  *
30268c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
30278c6529dbSLiam Girdwood  */
30288c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
30298c6529dbSLiam Girdwood {
3030f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
3031f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
30328c6529dbSLiam Girdwood 	else
30338c6529dbSLiam Girdwood 		return -EINVAL;
30348c6529dbSLiam Girdwood }
30358c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
30368c6529dbSLiam Girdwood 
30378c6529dbSLiam Girdwood /**
30388c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
30398c6529dbSLiam Girdwood  * @dai: DAI
3040a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3041a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
30428c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3043a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
30448c6529dbSLiam Girdwood  *
30458c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
30468c6529dbSLiam Girdwood  * specific.
30478c6529dbSLiam Girdwood  */
30488c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3049a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
30508c6529dbSLiam Girdwood {
3051f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3052f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3053a5479e38SDaniel Ribeiro 				slots, slot_width);
30548c6529dbSLiam Girdwood 	else
30558c6529dbSLiam Girdwood 		return -EINVAL;
30568c6529dbSLiam Girdwood }
30578c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
30588c6529dbSLiam Girdwood 
30598c6529dbSLiam Girdwood /**
3060472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3061472df3cbSBarry Song  * @dai: DAI
3062472df3cbSBarry Song  * @tx_num: how many TX channels
3063472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3064472df3cbSBarry Song  *           0~num-1 uses
3065472df3cbSBarry Song  * @rx_num: how many RX channels
3066472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3067472df3cbSBarry Song  *           0~num-1 uses
3068472df3cbSBarry Song  *
3069472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3070472df3cbSBarry Song  */
3071472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3072472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3073472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3074472df3cbSBarry Song {
3075f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3076f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3077472df3cbSBarry Song 			rx_num, rx_slot);
3078472df3cbSBarry Song 	else
3079472df3cbSBarry Song 		return -EINVAL;
3080472df3cbSBarry Song }
3081472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3082472df3cbSBarry Song 
3083472df3cbSBarry Song /**
30848c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
30858c6529dbSLiam Girdwood  * @dai: DAI
30868c6529dbSLiam Girdwood  * @tristate: tristate enable
30878c6529dbSLiam Girdwood  *
30888c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
30898c6529dbSLiam Girdwood  */
30908c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
30918c6529dbSLiam Girdwood {
3092f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3093f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
30948c6529dbSLiam Girdwood 	else
30958c6529dbSLiam Girdwood 		return -EINVAL;
30968c6529dbSLiam Girdwood }
30978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
30988c6529dbSLiam Girdwood 
30998c6529dbSLiam Girdwood /**
31008c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
31018c6529dbSLiam Girdwood  * @dai: DAI
31028c6529dbSLiam Girdwood  * @mute: mute enable
31038c6529dbSLiam Girdwood  *
31048c6529dbSLiam Girdwood  * Mutes the DAI DAC.
31058c6529dbSLiam Girdwood  */
31068c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
31078c6529dbSLiam Girdwood {
3108f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3109f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
31108c6529dbSLiam Girdwood 	else
31118c6529dbSLiam Girdwood 		return -EINVAL;
31128c6529dbSLiam Girdwood }
31138c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
31148c6529dbSLiam Girdwood 
3115c5af3a2eSMark Brown /**
3116c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3117c5af3a2eSMark Brown  *
3118ac11a2b3SMark Brown  * @card: Card to register
3119c5af3a2eSMark Brown  *
3120c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
3121c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
3122c5af3a2eSMark Brown  * registration APIs.
3123c5af3a2eSMark Brown  */
3124c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
3125c5af3a2eSMark Brown {
3126f0fba2adSLiam Girdwood 	int i;
3127f0fba2adSLiam Girdwood 
3128c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3129c5af3a2eSMark Brown 		return -EINVAL;
3130c5af3a2eSMark Brown 
31312eea392dSJarkko Nikula 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
31322eea392dSJarkko Nikula 			    (card->num_links + card->num_aux_devs),
3133f0fba2adSLiam Girdwood 			    GFP_KERNEL);
3134f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3135f0fba2adSLiam Girdwood 		return -ENOMEM;
31362eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3137f0fba2adSLiam Girdwood 
3138f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3139f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3140f0fba2adSLiam Girdwood 
3141c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3142c5af3a2eSMark Brown 	card->instantiated = 0;
3143f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3144c5af3a2eSMark Brown 
3145c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3146c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
3147435c5e25SMark Brown 	snd_soc_instantiate_cards();
3148c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3149c5af3a2eSMark Brown 
3150c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3151c5af3a2eSMark Brown 
3152c5af3a2eSMark Brown 	return 0;
3153c5af3a2eSMark Brown }
3154c5af3a2eSMark Brown 
3155c5af3a2eSMark Brown /**
3156c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3157c5af3a2eSMark Brown  *
3158ac11a2b3SMark Brown  * @card: Card to unregister
3159c5af3a2eSMark Brown  *
3160c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
3161c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
3162c5af3a2eSMark Brown  * registration APIs.
3163c5af3a2eSMark Brown  */
3164c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
3165c5af3a2eSMark Brown {
3166c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3167c5af3a2eSMark Brown 	list_del(&card->list);
3168c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3169c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3170c5af3a2eSMark Brown 
3171c5af3a2eSMark Brown 	return 0;
3172c5af3a2eSMark Brown }
3173c5af3a2eSMark Brown 
3174f0fba2adSLiam Girdwood /*
3175f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3176f0fba2adSLiam Girdwood  * and sanitizing names.
3177f0fba2adSLiam Girdwood  */
3178f0fba2adSLiam Girdwood static inline char *fmt_single_name(struct device *dev, int *id)
3179f0fba2adSLiam Girdwood {
3180f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3181f0fba2adSLiam Girdwood 	int id1, id2;
3182f0fba2adSLiam Girdwood 
3183f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3184f0fba2adSLiam Girdwood 		return NULL;
3185f0fba2adSLiam Girdwood 
3186*58818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3187f0fba2adSLiam Girdwood 
3188f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3189f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3190f0fba2adSLiam Girdwood 	if (found) {
3191f0fba2adSLiam Girdwood 		/* get ID */
3192f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3193f0fba2adSLiam Girdwood 
3194f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3195f0fba2adSLiam Girdwood 			if (*id == -1)
3196f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3197f0fba2adSLiam Girdwood 		}
3198f0fba2adSLiam Girdwood 
3199f0fba2adSLiam Girdwood 	} else {
3200f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3201f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3202f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3203f0fba2adSLiam Girdwood 
3204f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
320505899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3206f0fba2adSLiam Girdwood 
3207f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3208f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3209*58818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3210f0fba2adSLiam Girdwood 		} else
3211f0fba2adSLiam Girdwood 			*id = 0;
3212f0fba2adSLiam Girdwood 	}
3213f0fba2adSLiam Girdwood 
3214f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3215f0fba2adSLiam Girdwood }
3216f0fba2adSLiam Girdwood 
3217f0fba2adSLiam Girdwood /*
3218f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3219f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3220f0fba2adSLiam Girdwood  */
3221f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3222f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3223f0fba2adSLiam Girdwood {
3224f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
3225f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3226f0fba2adSLiam Girdwood 				dev_name(dev));
3227f0fba2adSLiam Girdwood 		return NULL;
3228f0fba2adSLiam Girdwood 	}
3229f0fba2adSLiam Girdwood 
3230f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3231f0fba2adSLiam Girdwood }
3232f0fba2adSLiam Girdwood 
32339115171aSMark Brown /**
32349115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
32359115171aSMark Brown  *
3236ac11a2b3SMark Brown  * @dai: DAI to register
32379115171aSMark Brown  */
3238f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3239f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
32409115171aSMark Brown {
3241f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
32429115171aSMark Brown 
3243f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
32449115171aSMark Brown 
3245f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3246f0fba2adSLiam Girdwood 	if (dai == NULL)
3247f0fba2adSLiam Girdwood 			return -ENOMEM;
32486335d055SEric Miao 
3249f0fba2adSLiam Girdwood 	/* create DAI component name */
3250f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3251f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3252f0fba2adSLiam Girdwood 		kfree(dai);
3253f0fba2adSLiam Girdwood 		return -ENOMEM;
3254f0fba2adSLiam Girdwood 	}
3255f0fba2adSLiam Girdwood 
3256f0fba2adSLiam Girdwood 	dai->dev = dev;
3257f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3258f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3259f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
32609115171aSMark Brown 
32619115171aSMark Brown 	mutex_lock(&client_mutex);
32629115171aSMark Brown 	list_add(&dai->list, &dai_list);
3263435c5e25SMark Brown 	snd_soc_instantiate_cards();
32649115171aSMark Brown 	mutex_unlock(&client_mutex);
32659115171aSMark Brown 
32669115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
32679115171aSMark Brown 
32689115171aSMark Brown 	return 0;
32699115171aSMark Brown }
32709115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
32719115171aSMark Brown 
32729115171aSMark Brown /**
32739115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
32749115171aSMark Brown  *
3275ac11a2b3SMark Brown  * @dai: DAI to unregister
32769115171aSMark Brown  */
3277f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
32789115171aSMark Brown {
3279f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3280f0fba2adSLiam Girdwood 
3281f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3282f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3283f0fba2adSLiam Girdwood 			goto found;
3284f0fba2adSLiam Girdwood 	}
3285f0fba2adSLiam Girdwood 	return;
3286f0fba2adSLiam Girdwood 
3287f0fba2adSLiam Girdwood found:
32889115171aSMark Brown 	mutex_lock(&client_mutex);
32899115171aSMark Brown 	list_del(&dai->list);
32909115171aSMark Brown 	mutex_unlock(&client_mutex);
32919115171aSMark Brown 
32929115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3293f0fba2adSLiam Girdwood 	kfree(dai->name);
3294f0fba2adSLiam Girdwood 	kfree(dai);
32959115171aSMark Brown }
32969115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
32979115171aSMark Brown 
32989115171aSMark Brown /**
32999115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
33009115171aSMark Brown  *
3301ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3302ac11a2b3SMark Brown  * @count: Number of DAIs
33039115171aSMark Brown  */
3304f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3305f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
33069115171aSMark Brown {
3307f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3308f0fba2adSLiam Girdwood 	int i, ret = 0;
3309f0fba2adSLiam Girdwood 
3310720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
33119115171aSMark Brown 
33129115171aSMark Brown 	for (i = 0; i < count; i++) {
3313f0fba2adSLiam Girdwood 
3314f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3315c46e0079SAxel Lin 		if (dai == NULL) {
3316c46e0079SAxel Lin 			ret = -ENOMEM;
3317c46e0079SAxel Lin 			goto err;
3318c46e0079SAxel Lin 		}
3319f0fba2adSLiam Girdwood 
3320f0fba2adSLiam Girdwood 		/* create DAI component name */
3321f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3322f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3323f0fba2adSLiam Girdwood 			kfree(dai);
3324f0fba2adSLiam Girdwood 			ret = -EINVAL;
33259115171aSMark Brown 			goto err;
33269115171aSMark Brown 		}
33279115171aSMark Brown 
3328f0fba2adSLiam Girdwood 		dai->dev = dev;
3329f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
33300f9141c9SMark Brown 		if (dai->driver->id)
33310f9141c9SMark Brown 			dai->id = dai->driver->id;
33320f9141c9SMark Brown 		else
33330f9141c9SMark Brown 			dai->id = i;
3334f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3335f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3336f0fba2adSLiam Girdwood 
3337f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3338f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3339f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3340f0fba2adSLiam Girdwood 
3341f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3342f0fba2adSLiam Girdwood 	}
3343f0fba2adSLiam Girdwood 
33441dcb4f38SAxel Lin 	mutex_lock(&client_mutex);
3345f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
33461dcb4f38SAxel Lin 	mutex_unlock(&client_mutex);
33479115171aSMark Brown 	return 0;
33489115171aSMark Brown 
33499115171aSMark Brown err:
33509115171aSMark Brown 	for (i--; i >= 0; i--)
3351f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33529115171aSMark Brown 
33539115171aSMark Brown 	return ret;
33549115171aSMark Brown }
33559115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
33569115171aSMark Brown 
33579115171aSMark Brown /**
33589115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
33599115171aSMark Brown  *
3360ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3361ac11a2b3SMark Brown  * @count: Number of DAIs
33629115171aSMark Brown  */
3363f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
33649115171aSMark Brown {
33659115171aSMark Brown 	int i;
33669115171aSMark Brown 
33679115171aSMark Brown 	for (i = 0; i < count; i++)
3368f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33699115171aSMark Brown }
33709115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
33719115171aSMark Brown 
337212a48a8cSMark Brown /**
337312a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
337412a48a8cSMark Brown  *
3375ac11a2b3SMark Brown  * @platform: platform to register
337612a48a8cSMark Brown  */
3377f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3378f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
337912a48a8cSMark Brown {
3380f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
338112a48a8cSMark Brown 
3382f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3383f0fba2adSLiam Girdwood 
3384f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3385f0fba2adSLiam Girdwood 	if (platform == NULL)
3386f0fba2adSLiam Girdwood 			return -ENOMEM;
3387f0fba2adSLiam Girdwood 
3388f0fba2adSLiam Girdwood 	/* create platform component name */
3389f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3390f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3391f0fba2adSLiam Girdwood 		kfree(platform);
3392f0fba2adSLiam Girdwood 		return -ENOMEM;
3393f0fba2adSLiam Girdwood 	}
3394f0fba2adSLiam Girdwood 
3395f0fba2adSLiam Girdwood 	platform->dev = dev;
3396f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
339712a48a8cSMark Brown 
339812a48a8cSMark Brown 	mutex_lock(&client_mutex);
339912a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3400435c5e25SMark Brown 	snd_soc_instantiate_cards();
340112a48a8cSMark Brown 	mutex_unlock(&client_mutex);
340212a48a8cSMark Brown 
340312a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
340412a48a8cSMark Brown 
340512a48a8cSMark Brown 	return 0;
340612a48a8cSMark Brown }
340712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
340812a48a8cSMark Brown 
340912a48a8cSMark Brown /**
341012a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
341112a48a8cSMark Brown  *
3412ac11a2b3SMark Brown  * @platform: platform to unregister
341312a48a8cSMark Brown  */
3414f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
341512a48a8cSMark Brown {
3416f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3417f0fba2adSLiam Girdwood 
3418f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3419f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3420f0fba2adSLiam Girdwood 			goto found;
3421f0fba2adSLiam Girdwood 	}
3422f0fba2adSLiam Girdwood 	return;
3423f0fba2adSLiam Girdwood 
3424f0fba2adSLiam Girdwood found:
342512a48a8cSMark Brown 	mutex_lock(&client_mutex);
342612a48a8cSMark Brown 	list_del(&platform->list);
342712a48a8cSMark Brown 	mutex_unlock(&client_mutex);
342812a48a8cSMark Brown 
342912a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3430f0fba2adSLiam Girdwood 	kfree(platform->name);
3431f0fba2adSLiam Girdwood 	kfree(platform);
343212a48a8cSMark Brown }
343312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
343412a48a8cSMark Brown 
3435151ab22cSMark Brown static u64 codec_format_map[] = {
3436151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3437151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3438151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3439151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3440151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3441151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3442151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3443151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3444151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3445151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3446151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3447151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3448151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3449151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3450151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3451151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3452151ab22cSMark Brown };
3453151ab22cSMark Brown 
3454151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3455151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3456151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3457151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3458151ab22cSMark Brown  */
3459151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3460151ab22cSMark Brown {
3461151ab22cSMark Brown 	int i;
3462151ab22cSMark Brown 
3463151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3464151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3465151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3466151ab22cSMark Brown }
3467151ab22cSMark Brown 
34680d0cf00aSMark Brown /**
34690d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
34700d0cf00aSMark Brown  *
3471ac11a2b3SMark Brown  * @codec: codec to register
34720d0cf00aSMark Brown  */
3473f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3474001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3475001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3476001ae4c0SMark Brown 			   int num_dai)
34770d0cf00aSMark Brown {
34783335ddcaSDimitris Papastamos 	size_t reg_size;
3479f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3480f0fba2adSLiam Girdwood 	int ret, i;
3481151ab22cSMark Brown 
3482f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
34830d0cf00aSMark Brown 
3484f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3485f0fba2adSLiam Girdwood 	if (codec == NULL)
3486f0fba2adSLiam Girdwood 		return -ENOMEM;
34870d0cf00aSMark Brown 
3488f0fba2adSLiam Girdwood 	/* create CODEC component name */
3489f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3490f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3491f0fba2adSLiam Girdwood 		kfree(codec);
3492f0fba2adSLiam Girdwood 		return -ENOMEM;
3493151ab22cSMark Brown 	}
3494151ab22cSMark Brown 
349523bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
349623bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
349723bbce34SDimitris Papastamos 	else
349823bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
349923bbce34SDimitris Papastamos 
3500ce6120ccSLiam Girdwood 	INIT_LIST_HEAD(&codec->dapm.widgets);
3501ce6120ccSLiam Girdwood 	INIT_LIST_HEAD(&codec->dapm.paths);
3502c3acec26SMark Brown 	codec->write = codec_drv->write;
3503c3acec26SMark Brown 	codec->read = codec_drv->read;
3504ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3505ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3506ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3507f0fba2adSLiam Girdwood 	codec->dev = dev;
3508f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3509f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3510f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3511f0fba2adSLiam Girdwood 
35127a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
35137a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
35143335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
35153335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
35163335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
35173335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
35183335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
35193335ddcaSDimitris Papastamos 		 * the cache.
35203335ddcaSDimitris Papastamos 		 */
35213335ddcaSDimitris Papastamos 		codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
35223335ddcaSDimitris Papastamos 					      reg_size, GFP_KERNEL);
35233335ddcaSDimitris Papastamos 		if (!codec->reg_def_copy) {
35243335ddcaSDimitris Papastamos 			ret = -ENOMEM;
3525fdf0f54dSDimitris Papastamos 			goto fail;
35267a30a3dbSDimitris Papastamos 		}
35277a30a3dbSDimitris Papastamos 	}
35287a30a3dbSDimitris Papastamos 
3529f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3530f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3531f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3532f0fba2adSLiam Girdwood 	}
3533f0fba2adSLiam Girdwood 
353426b01ccdSMark Brown 	/* register any DAIs */
353526b01ccdSMark Brown 	if (num_dai) {
3536f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3537f0fba2adSLiam Girdwood 		if (ret < 0)
3538fdf0f54dSDimitris Papastamos 			goto fail;
353926b01ccdSMark Brown 	}
3540f0fba2adSLiam Girdwood 
35410d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35420d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
35430d0cf00aSMark Brown 	snd_soc_instantiate_cards();
35440d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35450d0cf00aSMark Brown 
35460d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
35470d0cf00aSMark Brown 	return 0;
3548f0fba2adSLiam Girdwood 
3549fdf0f54dSDimitris Papastamos fail:
35503335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35513335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3552f0fba2adSLiam Girdwood 	kfree(codec->name);
3553f0fba2adSLiam Girdwood 	kfree(codec);
3554f0fba2adSLiam Girdwood 	return ret;
35550d0cf00aSMark Brown }
35560d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
35570d0cf00aSMark Brown 
35580d0cf00aSMark Brown /**
35590d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
35600d0cf00aSMark Brown  *
3561ac11a2b3SMark Brown  * @codec: codec to unregister
35620d0cf00aSMark Brown  */
3563f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
35640d0cf00aSMark Brown {
3565f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3566f0fba2adSLiam Girdwood 	int i;
3567f0fba2adSLiam Girdwood 
3568f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3569f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3570f0fba2adSLiam Girdwood 			goto found;
3571f0fba2adSLiam Girdwood 	}
3572f0fba2adSLiam Girdwood 	return;
3573f0fba2adSLiam Girdwood 
3574f0fba2adSLiam Girdwood found:
357526b01ccdSMark Brown 	if (codec->num_dai)
3576f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3577f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3578f0fba2adSLiam Girdwood 
35790d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35800d0cf00aSMark Brown 	list_del(&codec->list);
35810d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35820d0cf00aSMark Brown 
35830d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3584f0fba2adSLiam Girdwood 
35857a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
35863335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35871aafcd4dSDimitris Papastamos 	kfree(codec->name);
3588f0fba2adSLiam Girdwood 	kfree(codec);
35890d0cf00aSMark Brown }
35900d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
35910d0cf00aSMark Brown 
3592c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3593db2a4165SFrank Mandarino {
3594384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3595384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
3596384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
3597384c89e2SMark Brown 		printk(KERN_WARNING
3598384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
3599384c89e2SMark Brown 		debugfs_root = NULL;
3600384c89e2SMark Brown 	}
3601c3c5a19aSMark Brown 
3602c3c5a19aSMark Brown 	if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3603c3c5a19aSMark Brown 				 &codec_list_fops))
3604c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3605c3c5a19aSMark Brown 
3606f3208780SMark Brown 	if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3607f3208780SMark Brown 				 &dai_list_fops))
3608f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
360919c7ac27SMark Brown 
361019c7ac27SMark Brown 	if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
361119c7ac27SMark Brown 				 &platform_list_fops))
361219c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3613384c89e2SMark Brown #endif
3614384c89e2SMark Brown 
3615db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3616db2a4165SFrank Mandarino }
36174abe8e16SMark Brown module_init(snd_soc_init);
3618db2a4165SFrank Mandarino 
36197d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3620db2a4165SFrank Mandarino {
3621384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3622384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
3623384c89e2SMark Brown #endif
3624db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3625db2a4165SFrank Mandarino }
3626db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3627db2a4165SFrank Mandarino 
3628db2a4165SFrank Mandarino /* Module information */
3629d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3630db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3631db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
36328b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3633