xref: /openbmc/linux/sound/soc/soc-core.c (revision a8b1d34f3ee1bc139ac5fbe3f84f6d16c90136bb)
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>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
39db2a4165SFrank Mandarino #include <sound/soc-dapm.h>
40db2a4165SFrank Mandarino #include <sound/initval.h>
41db2a4165SFrank Mandarino 
42*a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
43*a8b1d34fSMark Brown #include <trace/events/asoc.h>
44*a8b1d34fSMark 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 
529db2a4165SFrank Mandarino 	/* Check that the codec and cpu DAI's 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 /*
878db2a4165SFrank Mandarino  * Free's 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 
902db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
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);
990db2a4165SFrank Mandarino 	int i;
991db2a4165SFrank Mandarino 
992e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
993e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
994e3509ff0SDaniel Mack 	 */
995f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
996e3509ff0SDaniel Mack 		return 0;
997e3509ff0SDaniel Mack 
9986ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
9996ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
10006ed25978SAndy Green 	 */
1001f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
1002f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1003f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
10046ed25978SAndy Green 
10056ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
1006f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
10076ed25978SAndy Green 
1008db2a4165SFrank Mandarino 	/* mute any active DAC's */
1009f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1010f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1011f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
10123efab7dcSMark Brown 
1013f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10143efab7dcSMark Brown 			continue;
10153efab7dcSMark Brown 
1016f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1017f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
1018db2a4165SFrank Mandarino 	}
1019db2a4165SFrank Mandarino 
10204ccab3e7SLiam Girdwood 	/* suspend all pcms */
1021f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1022f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10233efab7dcSMark Brown 			continue;
10243efab7dcSMark Brown 
1025f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
10263efab7dcSMark Brown 	}
10274ccab3e7SLiam Girdwood 
102887506549SMark Brown 	if (card->suspend_pre)
1029416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
1030db2a4165SFrank Mandarino 
1031f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1032f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1033f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
10343efab7dcSMark Brown 
1035f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10363efab7dcSMark Brown 			continue;
10373efab7dcSMark Brown 
1038f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1039f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1040f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
1041f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
1042f0fba2adSLiam Girdwood 			platform->suspended = 1;
1043f0fba2adSLiam Girdwood 		}
1044db2a4165SFrank Mandarino 	}
1045db2a4165SFrank Mandarino 
1046db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
1047f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1048f0fba2adSLiam Girdwood 		run_delayed_work(&card->rtd[i].delayed_work);
1049ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1050f0fba2adSLiam Girdwood 	}
1051db2a4165SFrank Mandarino 
1052f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1053f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
10543efab7dcSMark Brown 
1055f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10563efab7dcSMark Brown 			continue;
10573efab7dcSMark Brown 
1058f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1059f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1060db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1061f0fba2adSLiam Girdwood 
1062f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1063f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1064db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1065db2a4165SFrank Mandarino 	}
1066db2a4165SFrank Mandarino 
1067f0fba2adSLiam Girdwood 	/* suspend all CODECs */
1068f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1069f0fba2adSLiam Girdwood 		struct snd_soc_codec *codec = card->rtd[i].codec;
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);
1110db2a4165SFrank Mandarino 	int i;
1111db2a4165SFrank Mandarino 
11126ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
11136ed25978SAndy Green 	 * so userspace apps are blocked from touching us
11146ed25978SAndy Green 	 */
11156ed25978SAndy Green 
1116f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
11176ed25978SAndy Green 
11189949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
1119f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
11209949788bSMark Brown 
112187506549SMark Brown 	if (card->resume_pre)
112287506549SMark Brown 		card->resume_pre(pdev);
1123db2a4165SFrank Mandarino 
1124f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
1125f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1126f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
11273efab7dcSMark Brown 
1128f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11293efab7dcSMark Brown 			continue;
11303efab7dcSMark Brown 
1131f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1132f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1133db2a4165SFrank Mandarino 	}
1134db2a4165SFrank Mandarino 
1135f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1136f0fba2adSLiam Girdwood 		struct snd_soc_codec *codec = card->rtd[i].codec;
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 
1331f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1332f0fba2adSLiam Girdwood {
1333f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1334f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1335f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1336f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1337f0fba2adSLiam Girdwood 	int err;
1338f0fba2adSLiam Girdwood 
1339f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1340f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
1341f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1342f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
1343f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
134402a06d30SBarry Song 	}
134502a06d30SBarry Song 
1346f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
1347f0fba2adSLiam Girdwood 	if (codec_dai && codec_dai->probed) {
1348f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1349f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1350f0fba2adSLiam Girdwood 			if (err < 0)
1351f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1352f0fba2adSLiam Girdwood 		}
1353f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1354f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1355f0fba2adSLiam Girdwood 	}
1356f0fba2adSLiam Girdwood 
1357f0fba2adSLiam Girdwood 	/* remove the platform */
1358f0fba2adSLiam Girdwood 	if (platform && platform->probed) {
1359f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
1360f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
1361f0fba2adSLiam Girdwood 			if (err < 0)
1362f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1363f0fba2adSLiam Girdwood 		}
1364f0fba2adSLiam Girdwood 		platform->probed = 0;
1365f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
1366f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
1367f0fba2adSLiam Girdwood 	}
1368f0fba2adSLiam Girdwood 
1369f0fba2adSLiam Girdwood 	/* remove the CODEC */
1370f0fba2adSLiam Girdwood 	if (codec && codec->probed) {
1371f0fba2adSLiam Girdwood 		if (codec->driver->remove) {
1372f0fba2adSLiam Girdwood 			err = codec->driver->remove(codec);
1373f0fba2adSLiam Girdwood 			if (err < 0)
1374f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1375f0fba2adSLiam Girdwood 		}
1376f0fba2adSLiam Girdwood 
1377f0fba2adSLiam Girdwood 		/* Make sure all DAPM widgets are freed */
1378ce6120ccSLiam Girdwood 		snd_soc_dapm_free(&codec->dapm);
1379f0fba2adSLiam Girdwood 
1380f0fba2adSLiam Girdwood 		soc_cleanup_codec_debugfs(codec);
1381f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1382f0fba2adSLiam Girdwood 		codec->probed = 0;
1383f0fba2adSLiam Girdwood 		list_del(&codec->card_list);
1384f0fba2adSLiam Girdwood 		module_put(codec->dev->driver->owner);
1385f0fba2adSLiam Girdwood 	}
1386f0fba2adSLiam Girdwood 
1387f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
1388f0fba2adSLiam Girdwood 	if (cpu_dai && cpu_dai->probed) {
1389f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1390f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1391f0fba2adSLiam Girdwood 			if (err < 0)
1392f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1393f0fba2adSLiam Girdwood 		}
1394f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1395f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1396f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
1397f0fba2adSLiam Girdwood 	}
1398f0fba2adSLiam Girdwood }
1399f0fba2adSLiam Girdwood 
1400f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1401f0fba2adSLiam Girdwood 
1402f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1403f0fba2adSLiam Girdwood {
1404f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1405f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1406f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1407f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1408f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1409f0fba2adSLiam Girdwood 	int ret;
1410f0fba2adSLiam Girdwood 
1411f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1412f0fba2adSLiam Girdwood 
1413f0fba2adSLiam Girdwood 	/* config components */
1414f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1415f0fba2adSLiam Girdwood 	codec->card = card;
1416f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1417f0fba2adSLiam Girdwood 	rtd->card = card;
1418f0fba2adSLiam Girdwood 	rtd->dev.parent = card->dev;
1419f0fba2adSLiam Girdwood 	codec_dai->card = card;
1420f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1421f0fba2adSLiam Girdwood 
1422f0fba2adSLiam Girdwood 	/* set default power off timeout */
1423f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1424f0fba2adSLiam Girdwood 
1425f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
1426f0fba2adSLiam Girdwood 	if (!cpu_dai->probed) {
1427f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1428f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1429f0fba2adSLiam Girdwood 			if (ret < 0) {
1430f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1431f0fba2adSLiam Girdwood 						cpu_dai->name);
1432f0fba2adSLiam Girdwood 				return ret;
1433f0fba2adSLiam Girdwood 			}
1434f0fba2adSLiam Girdwood 		}
1435f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1436f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1437f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1438f0fba2adSLiam Girdwood 	}
1439f0fba2adSLiam Girdwood 
1440f0fba2adSLiam Girdwood 	/* probe the CODEC */
1441f0fba2adSLiam Girdwood 	if (!codec->probed) {
14423a45b867SJarkko Nikula 		codec->dapm.card = card;
1443f0fba2adSLiam Girdwood 		if (codec->driver->probe) {
1444f0fba2adSLiam Girdwood 			ret = codec->driver->probe(codec);
1445f0fba2adSLiam Girdwood 			if (ret < 0) {
1446f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1447f0fba2adSLiam Girdwood 						codec->name);
1448f0fba2adSLiam Girdwood 				return ret;
1449f0fba2adSLiam Girdwood 			}
1450f0fba2adSLiam Girdwood 		}
145113cb61f8SMark Brown 
145213cb61f8SMark Brown 		soc_init_codec_debugfs(codec);
145313cb61f8SMark Brown 
1454f0fba2adSLiam Girdwood 		/* mark codec as probed and add to card codec list */
1455f0fba2adSLiam Girdwood 		codec->probed = 1;
1456f0fba2adSLiam Girdwood 		list_add(&codec->card_list, &card->codec_dev_list);
1457f0fba2adSLiam Girdwood 	}
1458f0fba2adSLiam Girdwood 
1459f0fba2adSLiam Girdwood 	/* probe the platform */
1460f0fba2adSLiam Girdwood 	if (!platform->probed) {
1461f0fba2adSLiam Girdwood 		if (platform->driver->probe) {
1462f0fba2adSLiam Girdwood 			ret = platform->driver->probe(platform);
1463f0fba2adSLiam Girdwood 			if (ret < 0) {
1464f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe platform %s\n",
1465f0fba2adSLiam Girdwood 						platform->name);
1466f0fba2adSLiam Girdwood 				return ret;
1467f0fba2adSLiam Girdwood 			}
1468f0fba2adSLiam Girdwood 		}
1469f0fba2adSLiam Girdwood 		/* mark platform as probed and add to card platform list */
1470f0fba2adSLiam Girdwood 		platform->probed = 1;
1471f0fba2adSLiam Girdwood 		list_add(&platform->card_list, &card->platform_dev_list);
1472f0fba2adSLiam Girdwood 	}
1473f0fba2adSLiam Girdwood 
1474f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
1475f0fba2adSLiam Girdwood 	if (!codec_dai->probed) {
1476f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1477f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1478f0fba2adSLiam Girdwood 			if (ret < 0) {
1479f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1480f0fba2adSLiam Girdwood 						codec_dai->name);
1481f0fba2adSLiam Girdwood 				return ret;
1482f0fba2adSLiam Girdwood 			}
1483f0fba2adSLiam Girdwood 		}
1484f0fba2adSLiam Girdwood 
1485f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1486f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1487f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1488f0fba2adSLiam Girdwood 	}
1489f0fba2adSLiam Girdwood 
1490f0fba2adSLiam Girdwood 	/* DAPM dai link stream work */
1491f0fba2adSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1492f0fba2adSLiam Girdwood 
1493f0fba2adSLiam Girdwood 	/* now that all clients have probed, initialise the DAI link */
1494f0fba2adSLiam Girdwood 	if (dai_link->init) {
1495f0fba2adSLiam Girdwood 		ret = dai_link->init(rtd);
1496f0fba2adSLiam Girdwood 		if (ret < 0) {
1497f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1498f0fba2adSLiam Girdwood 			return ret;
1499f0fba2adSLiam Girdwood 		}
1500f0fba2adSLiam Girdwood 	}
1501f0fba2adSLiam Girdwood 
1502f0fba2adSLiam Girdwood 	/* Make sure all DAPM widgets are instantiated */
1503ce6120ccSLiam Girdwood 	snd_soc_dapm_new_widgets(&codec->dapm);
1504ce6120ccSLiam Girdwood 	snd_soc_dapm_sync(&codec->dapm);
1505f0fba2adSLiam Girdwood 
1506f0fba2adSLiam Girdwood 	/* register the rtd device */
1507f0fba2adSLiam Girdwood 	rtd->dev.release = rtd_release;
1508f0fba2adSLiam Girdwood 	rtd->dev.init_name = dai_link->name;
1509f0fba2adSLiam Girdwood 	ret = device_register(&rtd->dev);
1510f0fba2adSLiam Girdwood 	if (ret < 0) {
1511f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1512f0fba2adSLiam Girdwood 		return ret;
1513f0fba2adSLiam Girdwood 	}
1514f0fba2adSLiam Girdwood 
1515f0fba2adSLiam Girdwood 	rtd->dev_registered = 1;
1516f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1517f0fba2adSLiam Girdwood 	if (ret < 0)
1518f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1519f0fba2adSLiam Girdwood 
1520f0fba2adSLiam Girdwood 	/* add DAPM sysfs entries for this codec */
1521f0fba2adSLiam Girdwood 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1522f0fba2adSLiam Girdwood 	if (ret < 0)
1523f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1524f0fba2adSLiam Girdwood 
1525f0fba2adSLiam Girdwood 	/* add codec sysfs entries */
1526f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1527f0fba2adSLiam Girdwood 	if (ret < 0)
1528f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1529f0fba2adSLiam Girdwood 
1530f0fba2adSLiam Girdwood 	/* create the pcm */
1531f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1532f0fba2adSLiam Girdwood 	if (ret < 0) {
1533f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1534f0fba2adSLiam Girdwood 		return ret;
1535f0fba2adSLiam Girdwood 	}
1536f0fba2adSLiam Girdwood 
1537f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1538f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1539f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1540f0fba2adSLiam Girdwood 
1541f0fba2adSLiam Girdwood 	return 0;
1542f0fba2adSLiam Girdwood }
1543f0fba2adSLiam Girdwood 
1544f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1545f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1546f0fba2adSLiam Girdwood {
1547f0fba2adSLiam Girdwood 	int ret;
1548f0fba2adSLiam Girdwood 
1549f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1550f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
15516b05eda6SMark Brown 	 */
1552f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
15530562f788SMika Westerberg 		/*
15540562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
15550562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
15560562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
15570562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
15580562f788SMika Westerberg 		 *
15590562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
15600562f788SMika Westerberg 		 */
15610562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
15620562f788SMika Westerberg 			return 0;
1563f0fba2adSLiam Girdwood 
1564f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1565f0fba2adSLiam Girdwood 		if (ret < 0) {
1566f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1567f0fba2adSLiam Girdwood 			return ret;
1568435c5e25SMark Brown 		}
1569435c5e25SMark Brown 
1570f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1571f0fba2adSLiam Girdwood 	}
1572f0fba2adSLiam Girdwood 	return 0;
1573f0fba2adSLiam Girdwood }
1574435c5e25SMark Brown 
1575f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1576f0fba2adSLiam Girdwood {
1577f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1578f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1579f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1580f0fba2adSLiam Girdwood 	}
1581f0fba2adSLiam Girdwood }
1582f0fba2adSLiam Girdwood #endif
1583435c5e25SMark Brown 
1584f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1585f0fba2adSLiam Girdwood {
1586f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
1587f0fba2adSLiam Girdwood 	int ret, i;
158896dd3622SMark Brown 
1589f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1590f0fba2adSLiam Girdwood 
1591f0fba2adSLiam Girdwood 	if (card->instantiated) {
1592f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1593435c5e25SMark Brown 		return;
1594db2a4165SFrank Mandarino 	}
1595db2a4165SFrank Mandarino 
1596f0fba2adSLiam Girdwood 	/* bind DAIs */
1597f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1598f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1599f0fba2adSLiam Girdwood 
1600f0fba2adSLiam Girdwood 	/* bind completed ? */
1601f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1602f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1603f0fba2adSLiam Girdwood 		return;
1604db2a4165SFrank Mandarino 	}
1605db2a4165SFrank Mandarino 
1606f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1607f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1608f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1609f0fba2adSLiam Girdwood 	if (ret < 0) {
1610f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1611f0fba2adSLiam Girdwood 			card->name);
1612f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1613f0fba2adSLiam Girdwood 		return;
1614db2a4165SFrank Mandarino 	}
1615f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1616db2a4165SFrank Mandarino 
16171301a964SRandy Dunlap #ifdef CONFIG_PM
16186ed25978SAndy Green 	/* deferred resume work */
16196308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16201301a964SRandy Dunlap #endif
16216ed25978SAndy Green 
1622f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1623f0fba2adSLiam Girdwood 	if (card->probe) {
1624f0fba2adSLiam Girdwood 		ret = card->probe(pdev);
1625f0fba2adSLiam Girdwood 		if (ret < 0)
1626f0fba2adSLiam Girdwood 			goto card_probe_error;
1627f0fba2adSLiam Girdwood 	}
1628f0fba2adSLiam Girdwood 
1629fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1630f0fba2adSLiam Girdwood 		ret = soc_probe_dai_link(card, i);
1631fe3e78e0SMark Brown 		if (ret < 0) {
1632321de0d0SMark Brown 			pr_err("asoc: failed to instantiate card %s: %d\n",
1633321de0d0SMark Brown 			       card->name, ret);
1634f0fba2adSLiam Girdwood 			goto probe_dai_err;
1635fe3e78e0SMark Brown 		}
1636fe3e78e0SMark Brown 	}
1637fe3e78e0SMark Brown 
1638f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1639fe3e78e0SMark Brown 		 "%s",  card->name);
1640f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1641f0fba2adSLiam Girdwood 		 "%s", card->name);
1642fe3e78e0SMark Brown 
1643f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1644fe3e78e0SMark Brown 	if (ret < 0) {
1645f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1646f0fba2adSLiam Girdwood 		goto probe_dai_err;
1647fe3e78e0SMark Brown 	}
1648fe3e78e0SMark Brown 
1649fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1650f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1651f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1652f0fba2adSLiam Girdwood 			ret = soc_register_ac97_dai_link(&card->rtd[i]);
1653fe3e78e0SMark Brown 			if (ret < 0) {
1654f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1655f0fba2adSLiam Girdwood 				goto probe_dai_err;
1656fe3e78e0SMark Brown 			}
1657fe3e78e0SMark Brown 		}
1658fe3e78e0SMark Brown #endif
1659fe3e78e0SMark Brown 
1660435c5e25SMark Brown 	card->instantiated = 1;
1661f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1662435c5e25SMark Brown 	return;
1663db2a4165SFrank Mandarino 
1664f0fba2adSLiam Girdwood probe_dai_err:
1665f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1666f0fba2adSLiam Girdwood 		soc_remove_dai_link(card, i);
1667fe3e78e0SMark Brown 
1668f0fba2adSLiam Girdwood card_probe_error:
166987506549SMark Brown 	if (card->remove)
167087506549SMark Brown 		card->remove(pdev);
1671f0fba2adSLiam Girdwood 
1672f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1673f0fba2adSLiam Girdwood 
1674f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1675435c5e25SMark Brown }
1676db2a4165SFrank Mandarino 
1677435c5e25SMark Brown /*
1678421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1679435c5e25SMark Brown  * client_mutex.
1680435c5e25SMark Brown  */
1681435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1682435c5e25SMark Brown {
1683435c5e25SMark Brown 	struct snd_soc_card *card;
1684435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1685435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1686435c5e25SMark Brown }
1687435c5e25SMark Brown 
1688435c5e25SMark Brown /* probes a new socdev */
1689435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1690435c5e25SMark Brown {
1691f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1692435c5e25SMark Brown 	int ret = 0;
1693435c5e25SMark Brown 
1694435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1695435c5e25SMark Brown 	card->dev = &pdev->dev;
1696f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->dai_dev_list);
1697f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->codec_dev_list);
1698f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->platform_dev_list);
1699f0fba2adSLiam Girdwood 
1700a6052154SJarkko Nikula 	soc_init_card_debugfs(card);
1701a6052154SJarkko Nikula 
1702435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1703435c5e25SMark Brown 	if (ret != 0) {
1704435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1705db2a4165SFrank Mandarino 		return ret;
1706db2a4165SFrank Mandarino 	}
1707db2a4165SFrank Mandarino 
1708435c5e25SMark Brown 	return 0;
1709435c5e25SMark Brown }
1710435c5e25SMark Brown 
1711db2a4165SFrank Mandarino /* removes a socdev */
1712db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1713db2a4165SFrank Mandarino {
1714f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1715db2a4165SFrank Mandarino 	int i;
1716db2a4165SFrank Mandarino 
1717b2dfa62cSGuennadi Liakhovetski 		if (card->instantiated) {
1718965ac42cSLiam Girdwood 
1719f0fba2adSLiam Girdwood 		/* make sure any delayed work runs */
1720f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++) {
1721f0fba2adSLiam Girdwood 			struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1722f0fba2adSLiam Girdwood 			run_delayed_work(&rtd->delayed_work);
1723db2a4165SFrank Mandarino 		}
1724db2a4165SFrank Mandarino 
1725f0fba2adSLiam Girdwood 		/* remove and free each DAI */
1726f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++)
1727f0fba2adSLiam Girdwood 			soc_remove_dai_link(card, i);
1728f0fba2adSLiam Girdwood 
1729a6052154SJarkko Nikula 		soc_cleanup_card_debugfs(card);
1730a6052154SJarkko Nikula 
1731f0fba2adSLiam Girdwood 		/* remove the card */
173287506549SMark Brown 		if (card->remove)
173387506549SMark Brown 			card->remove(pdev);
1734f0fba2adSLiam Girdwood 
1735f0fba2adSLiam Girdwood 		kfree(card->rtd);
1736f0fba2adSLiam Girdwood 		snd_card_free(card->snd_card);
1737b2dfa62cSGuennadi Liakhovetski 	}
1738c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1739db2a4165SFrank Mandarino 	return 0;
1740db2a4165SFrank Mandarino }
1741db2a4165SFrank Mandarino 
1742416356fcSMark Brown static int soc_poweroff(struct device *dev)
174351737470SMark Brown {
1744416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1745f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1746f0fba2adSLiam Girdwood 	int i;
174751737470SMark Brown 
174851737470SMark Brown 	if (!card->instantiated)
1749416356fcSMark Brown 		return 0;
175051737470SMark Brown 
175151737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
175251737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1753f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1754f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1755f0fba2adSLiam Girdwood 		run_delayed_work(&rtd->delayed_work);
1756f0fba2adSLiam Girdwood 	}
175751737470SMark Brown 
1758f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1759416356fcSMark Brown 
1760416356fcSMark Brown 	return 0;
176151737470SMark Brown }
176251737470SMark Brown 
176347145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1764416356fcSMark Brown 	.suspend = soc_suspend,
1765416356fcSMark Brown 	.resume = soc_resume,
1766416356fcSMark Brown 	.poweroff = soc_poweroff,
1767416356fcSMark Brown };
1768416356fcSMark Brown 
1769db2a4165SFrank Mandarino /* ASoC platform driver */
1770db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1771db2a4165SFrank Mandarino 	.driver		= {
1772db2a4165SFrank Mandarino 		.name		= "soc-audio",
17738b45a209SKay Sievers 		.owner		= THIS_MODULE,
1774416356fcSMark Brown 		.pm		= &soc_pm_ops,
1775db2a4165SFrank Mandarino 	},
1776db2a4165SFrank Mandarino 	.probe		= soc_probe,
1777db2a4165SFrank Mandarino 	.remove		= soc_remove,
1778db2a4165SFrank Mandarino };
1779db2a4165SFrank Mandarino 
1780db2a4165SFrank Mandarino /* create a new pcm */
1781f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1782db2a4165SFrank Mandarino {
1783f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1784f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1785f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1786f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1787db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1788db2a4165SFrank Mandarino 	char new_name[64];
1789db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1790db2a4165SFrank Mandarino 
1791db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
179240ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
1793f0fba2adSLiam Girdwood 			rtd->dai_link->stream_name, codec_dai->name, num);
1794db2a4165SFrank Mandarino 
1795f0fba2adSLiam Girdwood 	if (codec_dai->driver->playback.channels_min)
1796db2a4165SFrank Mandarino 		playback = 1;
1797f0fba2adSLiam Girdwood 	if (codec_dai->driver->capture.channels_min)
1798db2a4165SFrank Mandarino 		capture = 1;
1799db2a4165SFrank Mandarino 
1800f0fba2adSLiam Girdwood 	dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1801f0fba2adSLiam Girdwood 	ret = snd_pcm_new(rtd->card->snd_card, new_name,
1802f0fba2adSLiam Girdwood 			num, playback, capture, &pcm);
1803db2a4165SFrank Mandarino 	if (ret < 0) {
1804f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1805db2a4165SFrank Mandarino 		return ret;
1806db2a4165SFrank Mandarino 	}
1807db2a4165SFrank Mandarino 
1808f0fba2adSLiam Girdwood 	rtd->pcm = pcm;
1809db2a4165SFrank Mandarino 	pcm->private_data = rtd;
1810f0fba2adSLiam Girdwood 	soc_pcm_ops.mmap = platform->driver->ops->mmap;
1811f0fba2adSLiam Girdwood 	soc_pcm_ops.pointer = platform->driver->ops->pointer;
1812f0fba2adSLiam Girdwood 	soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1813f0fba2adSLiam Girdwood 	soc_pcm_ops.copy = platform->driver->ops->copy;
1814f0fba2adSLiam Girdwood 	soc_pcm_ops.silence = platform->driver->ops->silence;
1815f0fba2adSLiam Girdwood 	soc_pcm_ops.ack = platform->driver->ops->ack;
1816f0fba2adSLiam Girdwood 	soc_pcm_ops.page = platform->driver->ops->page;
1817db2a4165SFrank Mandarino 
1818db2a4165SFrank Mandarino 	if (playback)
1819db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1820db2a4165SFrank Mandarino 
1821db2a4165SFrank Mandarino 	if (capture)
1822db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1823db2a4165SFrank Mandarino 
1824f0fba2adSLiam Girdwood 	ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
1825db2a4165SFrank Mandarino 	if (ret < 0) {
1826db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1827db2a4165SFrank Mandarino 		return ret;
1828db2a4165SFrank Mandarino 	}
1829db2a4165SFrank Mandarino 
1830f0fba2adSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
1831db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1832db2a4165SFrank Mandarino 		cpu_dai->name);
1833db2a4165SFrank Mandarino 	return ret;
1834db2a4165SFrank Mandarino }
1835db2a4165SFrank Mandarino 
1836096e49d5SMark Brown /**
1837096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1838096e49d5SMark Brown  *
1839096e49d5SMark Brown  * @codec: CODEC to query.
1840096e49d5SMark Brown  * @reg: Register to query.
1841096e49d5SMark Brown  *
1842096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1843096e49d5SMark Brown  */
1844096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1845096e49d5SMark Brown {
1846f0fba2adSLiam Girdwood 	if (codec->driver->volatile_register)
1847f0fba2adSLiam Girdwood 		return codec->driver->volatile_register(reg);
1848096e49d5SMark Brown 	else
1849096e49d5SMark Brown 		return 0;
1850096e49d5SMark Brown }
1851096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1852096e49d5SMark Brown 
1853db2a4165SFrank Mandarino /**
1854db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1855db2a4165SFrank Mandarino  * @codec: audio codec
1856db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1857db2a4165SFrank Mandarino  * @num: AC97 codec number
1858db2a4165SFrank Mandarino  *
1859db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1860db2a4165SFrank Mandarino  */
1861db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1862db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1863db2a4165SFrank Mandarino {
1864db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1865db2a4165SFrank Mandarino 
1866db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1867db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1868db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1869db2a4165SFrank Mandarino 		return -ENOMEM;
1870db2a4165SFrank Mandarino 	}
1871db2a4165SFrank Mandarino 
1872db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1873db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1874db2a4165SFrank Mandarino 		kfree(codec->ac97);
1875db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1876db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1877db2a4165SFrank Mandarino 		return -ENOMEM;
1878db2a4165SFrank Mandarino 	}
1879db2a4165SFrank Mandarino 
1880db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1881db2a4165SFrank Mandarino 	codec->ac97->num = num;
18820562f788SMika Westerberg 
18830562f788SMika Westerberg 	/*
18840562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
18850562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
18860562f788SMika Westerberg 	 */
18870562f788SMika Westerberg 	codec->ac97_created = 1;
18880562f788SMika Westerberg 
1889db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1890db2a4165SFrank Mandarino 	return 0;
1891db2a4165SFrank Mandarino }
1892db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1893db2a4165SFrank Mandarino 
1894db2a4165SFrank Mandarino /**
1895db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1896db2a4165SFrank Mandarino  * @codec: audio codec
1897db2a4165SFrank Mandarino  *
1898db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1899db2a4165SFrank Mandarino  */
1900db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1901db2a4165SFrank Mandarino {
1902db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1903f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1904f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1905f0fba2adSLiam Girdwood #endif
1906db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1907db2a4165SFrank Mandarino 	kfree(codec->ac97);
1908db2a4165SFrank Mandarino 	codec->ac97 = NULL;
19090562f788SMika Westerberg 	codec->ac97_created = 0;
1910db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1911db2a4165SFrank Mandarino }
1912db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1913db2a4165SFrank Mandarino 
1914c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1915c3753707SMark Brown {
1916c3753707SMark Brown 	unsigned int ret;
1917c3753707SMark Brown 
1918c3753707SMark Brown 	ret = codec->driver->read(codec, reg);
1919c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1920*a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1921c3753707SMark Brown 
1922c3753707SMark Brown 	return ret;
1923c3753707SMark Brown }
1924c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1925c3753707SMark Brown 
1926c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1927c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1928c3753707SMark Brown {
1929c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1930*a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1931c3753707SMark Brown 	return codec->driver->write(codec, reg, val);
1932c3753707SMark Brown }
1933c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1934c3753707SMark Brown 
1935db2a4165SFrank Mandarino /**
1936db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1937db2a4165SFrank Mandarino  * @codec: audio codec
1938db2a4165SFrank Mandarino  * @reg: codec register
1939db2a4165SFrank Mandarino  * @mask: register mask
1940db2a4165SFrank Mandarino  * @value: new value
1941db2a4165SFrank Mandarino  *
1942db2a4165SFrank Mandarino  * Writes new register value.
1943db2a4165SFrank Mandarino  *
1944db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1945db2a4165SFrank Mandarino  */
1946db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
194746f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1948db2a4165SFrank Mandarino {
1949db2a4165SFrank Mandarino 	int change;
195046f5822fSDaniel Ribeiro 	unsigned int old, new;
1951db2a4165SFrank Mandarino 
1952db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1953db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1954db2a4165SFrank Mandarino 	change = old != new;
1955db2a4165SFrank Mandarino 	if (change)
1956db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1957db2a4165SFrank Mandarino 
1958db2a4165SFrank Mandarino 	return change;
1959db2a4165SFrank Mandarino }
1960db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1961db2a4165SFrank Mandarino 
1962db2a4165SFrank Mandarino /**
19636c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
19646c508c62SEero Nurkkala  * @codec: audio codec
19656c508c62SEero Nurkkala  * @reg: codec register
19666c508c62SEero Nurkkala  * @mask: register mask
19676c508c62SEero Nurkkala  * @value: new value
19686c508c62SEero Nurkkala  *
19696c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
19706c508c62SEero Nurkkala  *
19716c508c62SEero Nurkkala  * Returns 1 for change else 0.
19726c508c62SEero Nurkkala  */
1973dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
19746c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
19756c508c62SEero Nurkkala 			       unsigned int value)
19766c508c62SEero Nurkkala {
19776c508c62SEero Nurkkala 	int change;
19786c508c62SEero Nurkkala 
19796c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
19806c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
19816c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
19826c508c62SEero Nurkkala 
19836c508c62SEero Nurkkala 	return change;
19846c508c62SEero Nurkkala }
1985dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
19866c508c62SEero Nurkkala 
19876c508c62SEero Nurkkala /**
1988db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1989db2a4165SFrank Mandarino  * @codec: audio codec
1990db2a4165SFrank Mandarino  * @reg: codec register
1991db2a4165SFrank Mandarino  * @mask: register mask
1992db2a4165SFrank Mandarino  * @value: new value
1993db2a4165SFrank Mandarino  *
1994db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1995db2a4165SFrank Mandarino  * different from the old value.
1996db2a4165SFrank Mandarino  *
1997db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1998db2a4165SFrank Mandarino  */
1999db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
200046f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2001db2a4165SFrank Mandarino {
2002db2a4165SFrank Mandarino 	int change;
200346f5822fSDaniel Ribeiro 	unsigned int old, new;
2004db2a4165SFrank Mandarino 
2005db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2006db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2007db2a4165SFrank Mandarino 	change = old != new;
2008db2a4165SFrank Mandarino 
2009db2a4165SFrank Mandarino 	return change;
2010db2a4165SFrank Mandarino }
2011db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2012db2a4165SFrank Mandarino 
2013db2a4165SFrank Mandarino /**
2014db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2015db2a4165SFrank Mandarino  * @substream: the pcm substream
2016db2a4165SFrank Mandarino  * @hw: the hardware parameters
2017db2a4165SFrank Mandarino  *
2018db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2019db2a4165SFrank Mandarino  */
2020db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2021db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2022db2a4165SFrank Mandarino {
2023db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2024db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2025db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2026db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2027db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2028db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2029db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2030db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2031db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2032db2a4165SFrank Mandarino 	return 0;
2033db2a4165SFrank Mandarino }
2034db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2035db2a4165SFrank Mandarino 
2036db2a4165SFrank Mandarino /**
2037db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2038db2a4165SFrank Mandarino  * @_template: control template
2039db2a4165SFrank Mandarino  * @data: control private data
2040ac11a2b3SMark Brown  * @long_name: control long name
2041db2a4165SFrank Mandarino  *
2042db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2043db2a4165SFrank Mandarino  *
2044db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2045db2a4165SFrank Mandarino  */
2046db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2047db2a4165SFrank Mandarino 	void *data, char *long_name)
2048db2a4165SFrank Mandarino {
2049db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2050db2a4165SFrank Mandarino 
2051db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2052db2a4165SFrank Mandarino 	if (long_name)
2053db2a4165SFrank Mandarino 		template.name = long_name;
2054db2a4165SFrank Mandarino 	template.index = 0;
2055db2a4165SFrank Mandarino 
2056db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
2057db2a4165SFrank Mandarino }
2058db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2059db2a4165SFrank Mandarino 
2060db2a4165SFrank Mandarino /**
20613e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
20623e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
20633e8e1952SIan Molton  * duplicating this code.
20643e8e1952SIan Molton  *
20653e8e1952SIan Molton  * @codec: codec to add controls to
20663e8e1952SIan Molton  * @controls: array of controls to add
20673e8e1952SIan Molton  * @num_controls: number of elements in the array
20683e8e1952SIan Molton  *
20693e8e1952SIan Molton  * Return 0 for success, else error.
20703e8e1952SIan Molton  */
20713e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
20723e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
20733e8e1952SIan Molton {
2074f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
20753e8e1952SIan Molton 	int err, i;
20763e8e1952SIan Molton 
20773e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
20783e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
20793e8e1952SIan Molton 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
20803e8e1952SIan Molton 		if (err < 0) {
2081082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2082082100dcSMark Brown 				codec->name, control->name, err);
20833e8e1952SIan Molton 			return err;
20843e8e1952SIan Molton 		}
20853e8e1952SIan Molton 	}
20863e8e1952SIan Molton 
20873e8e1952SIan Molton 	return 0;
20883e8e1952SIan Molton }
20893e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
20903e8e1952SIan Molton 
20913e8e1952SIan Molton /**
2092db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2093db2a4165SFrank Mandarino  * @kcontrol: mixer control
2094db2a4165SFrank Mandarino  * @uinfo: control element information
2095db2a4165SFrank Mandarino  *
2096db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2097db2a4165SFrank Mandarino  * mixer control.
2098db2a4165SFrank Mandarino  *
2099db2a4165SFrank Mandarino  * Returns 0 for success.
2100db2a4165SFrank Mandarino  */
2101db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2102db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2103db2a4165SFrank Mandarino {
2104db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2105db2a4165SFrank Mandarino 
2106db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2107db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2108f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2109db2a4165SFrank Mandarino 
2110f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2111f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2112db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2113db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2114db2a4165SFrank Mandarino 	return 0;
2115db2a4165SFrank Mandarino }
2116db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2117db2a4165SFrank Mandarino 
2118db2a4165SFrank Mandarino /**
2119db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2120db2a4165SFrank Mandarino  * @kcontrol: mixer control
2121ac11a2b3SMark Brown  * @ucontrol: control element information
2122db2a4165SFrank Mandarino  *
2123db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2124db2a4165SFrank Mandarino  *
2125db2a4165SFrank Mandarino  * Returns 0 for success.
2126db2a4165SFrank Mandarino  */
2127db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2128db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2129db2a4165SFrank Mandarino {
2130db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2131db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
213246f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2133db2a4165SFrank Mandarino 
2134f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2135db2a4165SFrank Mandarino 		;
2136db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
21373ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
21383ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2139db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2140db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2141db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2142db2a4165SFrank Mandarino 
2143db2a4165SFrank Mandarino 	return 0;
2144db2a4165SFrank Mandarino }
2145db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2146db2a4165SFrank Mandarino 
2147db2a4165SFrank Mandarino /**
2148db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2149db2a4165SFrank Mandarino  * @kcontrol: mixer control
2150ac11a2b3SMark Brown  * @ucontrol: control element information
2151db2a4165SFrank Mandarino  *
2152db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2153db2a4165SFrank Mandarino  *
2154db2a4165SFrank Mandarino  * Returns 0 for success.
2155db2a4165SFrank Mandarino  */
2156db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2157db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2158db2a4165SFrank Mandarino {
2159db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2160db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
216146f5822fSDaniel Ribeiro 	unsigned int val;
216246f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2163db2a4165SFrank Mandarino 
2164f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2165db2a4165SFrank Mandarino 		;
2166f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2167db2a4165SFrank Mandarino 		return -EINVAL;
2168db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2169db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2170db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2171f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2172db2a4165SFrank Mandarino 			return -EINVAL;
2173db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2174db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2175db2a4165SFrank Mandarino 	}
2176db2a4165SFrank Mandarino 
21776c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2178db2a4165SFrank Mandarino }
2179db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2180db2a4165SFrank Mandarino 
2181db2a4165SFrank Mandarino /**
21822e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
21832e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
21842e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
21852e72f8e3SPeter Ujfalusi  *
21862e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
21872e72f8e3SPeter Ujfalusi  *
21882e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
21892e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
21902e72f8e3SPeter Ujfalusi  *
21912e72f8e3SPeter Ujfalusi  * Returns 0 for success.
21922e72f8e3SPeter Ujfalusi  */
21932e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
21942e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
21952e72f8e3SPeter Ujfalusi {
21962e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
219774155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
219846f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
21992e72f8e3SPeter Ujfalusi 
22002e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
22012e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
22022e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
22032e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
22042e72f8e3SPeter Ujfalusi 			break;
22052e72f8e3SPeter Ujfalusi 	}
22062e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
22072e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
22082e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
22092e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
22102e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
22112e72f8e3SPeter Ujfalusi 				break;
22122e72f8e3SPeter Ujfalusi 		}
22132e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
22142e72f8e3SPeter Ujfalusi 	}
22152e72f8e3SPeter Ujfalusi 
22162e72f8e3SPeter Ujfalusi 	return 0;
22172e72f8e3SPeter Ujfalusi }
22182e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
22192e72f8e3SPeter Ujfalusi 
22202e72f8e3SPeter Ujfalusi /**
22212e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
22222e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
22232e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
22242e72f8e3SPeter Ujfalusi  *
22252e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
22262e72f8e3SPeter Ujfalusi  *
22272e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
22282e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
22292e72f8e3SPeter Ujfalusi  *
22302e72f8e3SPeter Ujfalusi  * Returns 0 for success.
22312e72f8e3SPeter Ujfalusi  */
22322e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
22332e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
22342e72f8e3SPeter Ujfalusi {
22352e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
223674155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
223746f5822fSDaniel Ribeiro 	unsigned int val;
223846f5822fSDaniel Ribeiro 	unsigned int mask;
22392e72f8e3SPeter Ujfalusi 
22402e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
22412e72f8e3SPeter Ujfalusi 		return -EINVAL;
22422e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
22432e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
22442e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
22452e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
22462e72f8e3SPeter Ujfalusi 			return -EINVAL;
22472e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
22482e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
22492e72f8e3SPeter Ujfalusi 	}
22502e72f8e3SPeter Ujfalusi 
22516c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
22522e72f8e3SPeter Ujfalusi }
22532e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
22542e72f8e3SPeter Ujfalusi 
22552e72f8e3SPeter Ujfalusi /**
2256db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2257db2a4165SFrank Mandarino  * @kcontrol: mixer control
2258db2a4165SFrank Mandarino  * @uinfo: control element information
2259db2a4165SFrank Mandarino  *
2260db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2261db2a4165SFrank Mandarino  * single mixer.
2262db2a4165SFrank Mandarino  *
2263db2a4165SFrank Mandarino  * Returns 0 for success.
2264db2a4165SFrank Mandarino  */
2265db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2266db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2267db2a4165SFrank Mandarino {
2268db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2269db2a4165SFrank Mandarino 
2270db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2271db2a4165SFrank Mandarino 	uinfo->count = 1;
2272f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2273db2a4165SFrank Mandarino 
2274f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2275f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2276db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2277db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2278db2a4165SFrank Mandarino 	return 0;
2279db2a4165SFrank Mandarino }
2280db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2281db2a4165SFrank Mandarino 
2282db2a4165SFrank Mandarino /**
2283db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2284db2a4165SFrank Mandarino  * @kcontrol: mixer control
2285db2a4165SFrank Mandarino  * @uinfo: control element information
2286db2a4165SFrank Mandarino  *
2287db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2288db2a4165SFrank Mandarino  *
2289db2a4165SFrank Mandarino  * Returns 0 for success.
2290db2a4165SFrank Mandarino  */
2291db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2292db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2293db2a4165SFrank Mandarino {
2294a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2295db2a4165SFrank Mandarino 
2296fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2297a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2298a7a4ac86SPhilipp Zabel 	else
2299a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2300a7a4ac86SPhilipp Zabel 
2301db2a4165SFrank Mandarino 	uinfo->count = 1;
2302db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2303a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2304db2a4165SFrank Mandarino 	return 0;
2305db2a4165SFrank Mandarino }
2306db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2307db2a4165SFrank Mandarino 
2308db2a4165SFrank Mandarino /**
2309db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2310db2a4165SFrank Mandarino  * @kcontrol: mixer control
2311db2a4165SFrank Mandarino  * @uinfo: control element information
2312db2a4165SFrank Mandarino  *
2313db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2314db2a4165SFrank Mandarino  *
2315db2a4165SFrank Mandarino  * Returns 0 for success.
2316db2a4165SFrank Mandarino  */
2317db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2318db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2319db2a4165SFrank Mandarino {
23204eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23214eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2322d11bb4a9SPeter Ujfalusi 	int platform_max;
2323762b8df7SMark Brown 	unsigned int shift = mc->shift;
2324815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2325db2a4165SFrank Mandarino 
2326d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2327d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2328d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2329d11bb4a9SPeter Ujfalusi 
2330d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2331a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2332a7a4ac86SPhilipp Zabel 	else
2333a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2334a7a4ac86SPhilipp Zabel 
2335db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2336db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2337d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2338db2a4165SFrank Mandarino 	return 0;
2339db2a4165SFrank Mandarino }
2340db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2341db2a4165SFrank Mandarino 
2342db2a4165SFrank Mandarino /**
2343db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2344db2a4165SFrank Mandarino  * @kcontrol: mixer control
2345ac11a2b3SMark Brown  * @ucontrol: control element information
2346db2a4165SFrank Mandarino  *
2347db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2348db2a4165SFrank Mandarino  *
2349db2a4165SFrank Mandarino  * Returns 0 for success.
2350db2a4165SFrank Mandarino  */
2351db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2352db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2353db2a4165SFrank Mandarino {
23544eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23554eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2356db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2357815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2358815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2359815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
23604eaa9819SJon Smirl 	int max = mc->max;
2361815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2362815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2363db2a4165SFrank Mandarino 
2364db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2365db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2366db2a4165SFrank Mandarino 	if (shift != rshift)
2367db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2368db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2369db2a4165SFrank Mandarino 	if (invert) {
2370db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2371a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2372db2a4165SFrank Mandarino 		if (shift != rshift)
2373db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2374a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2375db2a4165SFrank Mandarino 	}
2376db2a4165SFrank Mandarino 
2377db2a4165SFrank Mandarino 	return 0;
2378db2a4165SFrank Mandarino }
2379db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2380db2a4165SFrank Mandarino 
2381db2a4165SFrank Mandarino /**
2382db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2383db2a4165SFrank Mandarino  * @kcontrol: mixer control
2384ac11a2b3SMark Brown  * @ucontrol: control element information
2385db2a4165SFrank Mandarino  *
2386db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2387db2a4165SFrank Mandarino  *
2388db2a4165SFrank Mandarino  * Returns 0 for success.
2389db2a4165SFrank Mandarino  */
2390db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2391db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2392db2a4165SFrank Mandarino {
23934eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23944eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2395db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2396815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2397815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2398815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
23994eaa9819SJon Smirl 	int max = mc->max;
2400815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2401815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
240246f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2403db2a4165SFrank Mandarino 
2404db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2405db2a4165SFrank Mandarino 	if (invert)
2406a7a4ac86SPhilipp Zabel 		val = max - val;
2407db2a4165SFrank Mandarino 	val_mask = mask << shift;
2408db2a4165SFrank Mandarino 	val = val << shift;
2409db2a4165SFrank Mandarino 	if (shift != rshift) {
2410db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2411db2a4165SFrank Mandarino 		if (invert)
2412a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2413db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2414db2a4165SFrank Mandarino 		val |= val2 << rshift;
2415db2a4165SFrank Mandarino 	}
24166c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2417db2a4165SFrank Mandarino }
2418db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2419db2a4165SFrank Mandarino 
2420db2a4165SFrank Mandarino /**
2421db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2422db2a4165SFrank Mandarino  * @kcontrol: mixer control
2423db2a4165SFrank Mandarino  * @uinfo: control element information
2424db2a4165SFrank Mandarino  *
2425db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2426db2a4165SFrank Mandarino  * spans 2 codec registers.
2427db2a4165SFrank Mandarino  *
2428db2a4165SFrank Mandarino  * Returns 0 for success.
2429db2a4165SFrank Mandarino  */
2430db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2431db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2432db2a4165SFrank Mandarino {
24334eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24344eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2435d11bb4a9SPeter Ujfalusi 	int platform_max;
2436db2a4165SFrank Mandarino 
2437d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2438d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2439d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2440d11bb4a9SPeter Ujfalusi 
2441d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2442a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2443a7a4ac86SPhilipp Zabel 	else
2444a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2445a7a4ac86SPhilipp Zabel 
2446db2a4165SFrank Mandarino 	uinfo->count = 2;
2447db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2448d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2449db2a4165SFrank Mandarino 	return 0;
2450db2a4165SFrank Mandarino }
2451db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2452db2a4165SFrank Mandarino 
2453db2a4165SFrank Mandarino /**
2454db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2455db2a4165SFrank Mandarino  * @kcontrol: mixer control
2456ac11a2b3SMark Brown  * @ucontrol: control element information
2457db2a4165SFrank Mandarino  *
2458db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2459db2a4165SFrank Mandarino  *
2460db2a4165SFrank Mandarino  * Returns 0 for success.
2461db2a4165SFrank Mandarino  */
2462db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2463db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2464db2a4165SFrank Mandarino {
24654eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24664eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2467db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2468815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2469815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2470815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
24714eaa9819SJon Smirl 	int max = mc->max;
2472815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2473815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2474db2a4165SFrank Mandarino 
2475db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2476db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2477db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2478db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2479db2a4165SFrank Mandarino 	if (invert) {
2480db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2481a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2482db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2483a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2484db2a4165SFrank Mandarino 	}
2485db2a4165SFrank Mandarino 
2486db2a4165SFrank Mandarino 	return 0;
2487db2a4165SFrank Mandarino }
2488db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2489db2a4165SFrank Mandarino 
2490db2a4165SFrank Mandarino /**
2491db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2492db2a4165SFrank Mandarino  * @kcontrol: mixer control
2493ac11a2b3SMark Brown  * @ucontrol: control element information
2494db2a4165SFrank Mandarino  *
2495db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2496db2a4165SFrank Mandarino  *
2497db2a4165SFrank Mandarino  * Returns 0 for success.
2498db2a4165SFrank Mandarino  */
2499db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2500db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2501db2a4165SFrank Mandarino {
25024eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25034eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2504db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2505815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2506815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2507815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
25084eaa9819SJon Smirl 	int max = mc->max;
2509815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2510815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2511db2a4165SFrank Mandarino 	int err;
251246f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2513db2a4165SFrank Mandarino 
2514db2a4165SFrank Mandarino 	val_mask = mask << shift;
2515db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2516db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2517db2a4165SFrank Mandarino 
2518db2a4165SFrank Mandarino 	if (invert) {
2519a7a4ac86SPhilipp Zabel 		val = max - val;
2520a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2521db2a4165SFrank Mandarino 	}
2522db2a4165SFrank Mandarino 
2523db2a4165SFrank Mandarino 	val = val << shift;
2524db2a4165SFrank Mandarino 	val2 = val2 << shift;
2525db2a4165SFrank Mandarino 
25266c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
25273ff3f64bSMark Brown 	if (err < 0)
2528db2a4165SFrank Mandarino 		return err;
2529db2a4165SFrank Mandarino 
25306c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2531db2a4165SFrank Mandarino 	return err;
2532db2a4165SFrank Mandarino }
2533db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2534db2a4165SFrank Mandarino 
2535e13ac2e9SMark Brown /**
2536e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2537e13ac2e9SMark Brown  * @kcontrol: mixer control
2538e13ac2e9SMark Brown  * @uinfo: control element information
2539e13ac2e9SMark Brown  *
2540e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2541e13ac2e9SMark Brown  *
2542e13ac2e9SMark Brown  * Returns 0 for success.
2543e13ac2e9SMark Brown  */
2544e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2545e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2546e13ac2e9SMark Brown {
25474eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25484eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2549d11bb4a9SPeter Ujfalusi 	int platform_max;
25504eaa9819SJon Smirl 	int min = mc->min;
2551e13ac2e9SMark Brown 
2552d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2553d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2554d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2555d11bb4a9SPeter Ujfalusi 
2556e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2557e13ac2e9SMark Brown 	uinfo->count = 2;
2558e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2559d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2560e13ac2e9SMark Brown 	return 0;
2561e13ac2e9SMark Brown }
2562e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2563e13ac2e9SMark Brown 
2564e13ac2e9SMark Brown /**
2565e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2566e13ac2e9SMark Brown  * @kcontrol: mixer control
2567ac11a2b3SMark Brown  * @ucontrol: control element information
2568e13ac2e9SMark Brown  *
2569e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2570e13ac2e9SMark Brown  *
2571e13ac2e9SMark Brown  * Returns 0 for success.
2572e13ac2e9SMark Brown  */
2573e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2574e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2575e13ac2e9SMark Brown {
25764eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25774eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2578e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2579815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
25804eaa9819SJon Smirl 	int min = mc->min;
2581e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2582e13ac2e9SMark Brown 
2583e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2584e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2585e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2586e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2587e13ac2e9SMark Brown 	return 0;
2588e13ac2e9SMark Brown }
2589e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2590e13ac2e9SMark Brown 
2591e13ac2e9SMark Brown /**
2592e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2593e13ac2e9SMark Brown  * @kcontrol: mixer control
2594ac11a2b3SMark Brown  * @ucontrol: control element information
2595e13ac2e9SMark Brown  *
2596e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2597e13ac2e9SMark Brown  *
2598e13ac2e9SMark Brown  * Returns 0 for success.
2599e13ac2e9SMark Brown  */
2600e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2601e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2602e13ac2e9SMark Brown {
26034eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26044eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2605e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2606815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
26074eaa9819SJon Smirl 	int min = mc->min;
260846f5822fSDaniel Ribeiro 	unsigned int val;
2609e13ac2e9SMark Brown 
2610e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2611e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2612e13ac2e9SMark Brown 
26136c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2614e13ac2e9SMark Brown }
2615e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2616e13ac2e9SMark Brown 
26178c6529dbSLiam Girdwood /**
2618637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2619637d3847SPeter Ujfalusi  *
2620637d3847SPeter Ujfalusi  * @codec: where to look for the control
2621637d3847SPeter Ujfalusi  * @name: Name of the control
2622637d3847SPeter Ujfalusi  * @max: new maximum limit
2623637d3847SPeter Ujfalusi  *
2624637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2625637d3847SPeter Ujfalusi  */
2626637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2627637d3847SPeter Ujfalusi 	const char *name, int max)
2628637d3847SPeter Ujfalusi {
2629f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2630637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2631637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2632637d3847SPeter Ujfalusi 	int found = 0;
2633637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2634637d3847SPeter Ujfalusi 
2635637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2636637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2637637d3847SPeter Ujfalusi 		return -EINVAL;
2638637d3847SPeter Ujfalusi 
2639637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2640637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2641637d3847SPeter Ujfalusi 			found = 1;
2642637d3847SPeter Ujfalusi 			break;
2643637d3847SPeter Ujfalusi 		}
2644637d3847SPeter Ujfalusi 	}
2645637d3847SPeter Ujfalusi 	if (found) {
2646637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2647637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2648d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2649637d3847SPeter Ujfalusi 			ret = 0;
2650637d3847SPeter Ujfalusi 		}
2651637d3847SPeter Ujfalusi 	}
2652637d3847SPeter Ujfalusi 	return ret;
2653637d3847SPeter Ujfalusi }
2654637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2655637d3847SPeter Ujfalusi 
2656637d3847SPeter Ujfalusi /**
2657b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2658b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2659b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2660b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2661b6f4bb38Sapatard@mandriva.com  *
2662b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2663b6f4bb38Sapatard@mandriva.com  */
2664b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2665b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2666b6f4bb38Sapatard@mandriva.com {
2667b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2668b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2669b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2670b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2671b6f4bb38Sapatard@mandriva.com 
2672b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2673b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2674b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2675b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2676b6f4bb38Sapatard@mandriva.com 
2677b6f4bb38Sapatard@mandriva.com 	return 0;
2678b6f4bb38Sapatard@mandriva.com }
2679b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2680b6f4bb38Sapatard@mandriva.com 
2681b6f4bb38Sapatard@mandriva.com /**
2682b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2683b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2684b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2685b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2686b6f4bb38Sapatard@mandriva.com  *
2687b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2688b6f4bb38Sapatard@mandriva.com  */
2689b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2690b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2691b6f4bb38Sapatard@mandriva.com {
2692b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2693b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2694b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2695b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2696b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2697b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2698b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2699b6f4bb38Sapatard@mandriva.com 
270020630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
270120630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2702b6f4bb38Sapatard@mandriva.com 	return 0;
2703b6f4bb38Sapatard@mandriva.com }
2704b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2705b6f4bb38Sapatard@mandriva.com 
2706b6f4bb38Sapatard@mandriva.com /**
2707b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2708b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2709b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2710b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2711b6f4bb38Sapatard@mandriva.com  *
2712b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2713b6f4bb38Sapatard@mandriva.com  */
2714b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2715b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2716b6f4bb38Sapatard@mandriva.com {
2717b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2718b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2719b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2720b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2721b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2722b6f4bb38Sapatard@mandriva.com 	int ret;
2723b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2724b6f4bb38Sapatard@mandriva.com 
2725b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2726b6f4bb38Sapatard@mandriva.com 	val &= mask;
2727b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2728b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2729b6f4bb38Sapatard@mandriva.com 
2730b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2731b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2732b6f4bb38Sapatard@mandriva.com 
2733b6f4bb38Sapatard@mandriva.com 	ret = 0;
2734b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2735b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2736b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2737f1df5aecSMark Brown 			return ret;
2738b6f4bb38Sapatard@mandriva.com 	}
2739b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2740b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2741b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2742f1df5aecSMark Brown 			return ret;
2743b6f4bb38Sapatard@mandriva.com 	}
2744b6f4bb38Sapatard@mandriva.com 
2745b6f4bb38Sapatard@mandriva.com 	return 0;
2746b6f4bb38Sapatard@mandriva.com }
2747b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2748b6f4bb38Sapatard@mandriva.com 
2749b6f4bb38Sapatard@mandriva.com /**
27508c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
27518c6529dbSLiam Girdwood  * @dai: DAI
27528c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
27538c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
27548c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
27558c6529dbSLiam Girdwood  *
27568c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
27578c6529dbSLiam Girdwood  */
27588c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
27598c6529dbSLiam Girdwood 	unsigned int freq, int dir)
27608c6529dbSLiam Girdwood {
2761f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2762f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
27638c6529dbSLiam Girdwood 	else
27648c6529dbSLiam Girdwood 		return -EINVAL;
27658c6529dbSLiam Girdwood }
27668c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
27678c6529dbSLiam Girdwood 
27688c6529dbSLiam Girdwood /**
27698c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
27708c6529dbSLiam Girdwood  * @dai: DAI
2771ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
27728c6529dbSLiam Girdwood  * @div: new clock divisor.
27738c6529dbSLiam Girdwood  *
27748c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
27758c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
27768c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
27778c6529dbSLiam Girdwood  */
27788c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
27798c6529dbSLiam Girdwood 	int div_id, int div)
27808c6529dbSLiam Girdwood {
2781f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2782f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
27838c6529dbSLiam Girdwood 	else
27848c6529dbSLiam Girdwood 		return -EINVAL;
27858c6529dbSLiam Girdwood }
27868c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
27878c6529dbSLiam Girdwood 
27888c6529dbSLiam Girdwood /**
27898c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
27908c6529dbSLiam Girdwood  * @dai: DAI
27918c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
279285488037SMark Brown  * @source: DAI specific source for the PLL
27938c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
27948c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
27958c6529dbSLiam Girdwood  *
27968c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
27978c6529dbSLiam Girdwood  */
279885488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
279985488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
28008c6529dbSLiam Girdwood {
2801f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2802f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
280385488037SMark Brown 					 freq_in, freq_out);
28048c6529dbSLiam Girdwood 	else
28058c6529dbSLiam Girdwood 		return -EINVAL;
28068c6529dbSLiam Girdwood }
28078c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
28088c6529dbSLiam Girdwood 
28098c6529dbSLiam Girdwood /**
28108c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
28118c6529dbSLiam Girdwood  * @dai: DAI
28128c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
28138c6529dbSLiam Girdwood  *
28148c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
28158c6529dbSLiam Girdwood  */
28168c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
28178c6529dbSLiam Girdwood {
2818f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
2819f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
28208c6529dbSLiam Girdwood 	else
28218c6529dbSLiam Girdwood 		return -EINVAL;
28228c6529dbSLiam Girdwood }
28238c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
28248c6529dbSLiam Girdwood 
28258c6529dbSLiam Girdwood /**
28268c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
28278c6529dbSLiam Girdwood  * @dai: DAI
2828a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2829a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
28308c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2831a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
28328c6529dbSLiam Girdwood  *
28338c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
28348c6529dbSLiam Girdwood  * specific.
28358c6529dbSLiam Girdwood  */
28368c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2837a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
28388c6529dbSLiam Girdwood {
2839f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2840f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2841a5479e38SDaniel Ribeiro 				slots, slot_width);
28428c6529dbSLiam Girdwood 	else
28438c6529dbSLiam Girdwood 		return -EINVAL;
28448c6529dbSLiam Girdwood }
28458c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
28468c6529dbSLiam Girdwood 
28478c6529dbSLiam Girdwood /**
2848472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2849472df3cbSBarry Song  * @dai: DAI
2850472df3cbSBarry Song  * @tx_num: how many TX channels
2851472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2852472df3cbSBarry Song  *           0~num-1 uses
2853472df3cbSBarry Song  * @rx_num: how many RX channels
2854472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2855472df3cbSBarry Song  *           0~num-1 uses
2856472df3cbSBarry Song  *
2857472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2858472df3cbSBarry Song  */
2859472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2860472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2861472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2862472df3cbSBarry Song {
2863f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2864f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2865472df3cbSBarry Song 			rx_num, rx_slot);
2866472df3cbSBarry Song 	else
2867472df3cbSBarry Song 		return -EINVAL;
2868472df3cbSBarry Song }
2869472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2870472df3cbSBarry Song 
2871472df3cbSBarry Song /**
28728c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
28738c6529dbSLiam Girdwood  * @dai: DAI
28748c6529dbSLiam Girdwood  * @tristate: tristate enable
28758c6529dbSLiam Girdwood  *
28768c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
28778c6529dbSLiam Girdwood  */
28788c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
28798c6529dbSLiam Girdwood {
2880f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2881f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
28828c6529dbSLiam Girdwood 	else
28838c6529dbSLiam Girdwood 		return -EINVAL;
28848c6529dbSLiam Girdwood }
28858c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
28868c6529dbSLiam Girdwood 
28878c6529dbSLiam Girdwood /**
28888c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
28898c6529dbSLiam Girdwood  * @dai: DAI
28908c6529dbSLiam Girdwood  * @mute: mute enable
28918c6529dbSLiam Girdwood  *
28928c6529dbSLiam Girdwood  * Mutes the DAI DAC.
28938c6529dbSLiam Girdwood  */
28948c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
28958c6529dbSLiam Girdwood {
2896f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
2897f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
28988c6529dbSLiam Girdwood 	else
28998c6529dbSLiam Girdwood 		return -EINVAL;
29008c6529dbSLiam Girdwood }
29018c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
29028c6529dbSLiam Girdwood 
2903c5af3a2eSMark Brown /**
2904c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2905c5af3a2eSMark Brown  *
2906ac11a2b3SMark Brown  * @card: Card to register
2907c5af3a2eSMark Brown  *
2908c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2909c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2910c5af3a2eSMark Brown  * registration APIs.
2911c5af3a2eSMark Brown  */
2912c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
2913c5af3a2eSMark Brown {
2914f0fba2adSLiam Girdwood 	int i;
2915f0fba2adSLiam Girdwood 
2916c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2917c5af3a2eSMark Brown 		return -EINVAL;
2918c5af3a2eSMark Brown 
2919f0fba2adSLiam Girdwood 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2920f0fba2adSLiam Girdwood 			GFP_KERNEL);
2921f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2922f0fba2adSLiam Girdwood 		return -ENOMEM;
2923f0fba2adSLiam Girdwood 
2924f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
2925f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
2926f0fba2adSLiam Girdwood 
2927c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2928c5af3a2eSMark Brown 	card->instantiated = 0;
2929f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2930c5af3a2eSMark Brown 
2931c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2932c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2933435c5e25SMark Brown 	snd_soc_instantiate_cards();
2934c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2935c5af3a2eSMark Brown 
2936c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2937c5af3a2eSMark Brown 
2938c5af3a2eSMark Brown 	return 0;
2939c5af3a2eSMark Brown }
2940c5af3a2eSMark Brown 
2941c5af3a2eSMark Brown /**
2942c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2943c5af3a2eSMark Brown  *
2944ac11a2b3SMark Brown  * @card: Card to unregister
2945c5af3a2eSMark Brown  *
2946c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2947c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2948c5af3a2eSMark Brown  * registration APIs.
2949c5af3a2eSMark Brown  */
2950c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
2951c5af3a2eSMark Brown {
2952c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2953c5af3a2eSMark Brown 	list_del(&card->list);
2954c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2955c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2956c5af3a2eSMark Brown 
2957c5af3a2eSMark Brown 	return 0;
2958c5af3a2eSMark Brown }
2959c5af3a2eSMark Brown 
2960f0fba2adSLiam Girdwood /*
2961f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2962f0fba2adSLiam Girdwood  * and sanitizing names.
2963f0fba2adSLiam Girdwood  */
2964f0fba2adSLiam Girdwood static inline char *fmt_single_name(struct device *dev, int *id)
2965f0fba2adSLiam Girdwood {
2966f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2967f0fba2adSLiam Girdwood 	int id1, id2;
2968f0fba2adSLiam Girdwood 
2969f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2970f0fba2adSLiam Girdwood 		return NULL;
2971f0fba2adSLiam Girdwood 
2972f0fba2adSLiam Girdwood 	strncpy(name, dev_name(dev), NAME_SIZE);
2973f0fba2adSLiam Girdwood 
2974f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2975f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2976f0fba2adSLiam Girdwood 	if (found) {
2977f0fba2adSLiam Girdwood 		/* get ID */
2978f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2979f0fba2adSLiam Girdwood 
2980f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2981f0fba2adSLiam Girdwood 			if (*id == -1)
2982f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2983f0fba2adSLiam Girdwood 		}
2984f0fba2adSLiam Girdwood 
2985f0fba2adSLiam Girdwood 	} else {
2986f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2987f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2988f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2989f0fba2adSLiam Girdwood 
2990f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
299105899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2992f0fba2adSLiam Girdwood 
2993f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2994f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2995f0fba2adSLiam Girdwood 			strncpy(name, tmp, NAME_SIZE);
2996f0fba2adSLiam Girdwood 		} else
2997f0fba2adSLiam Girdwood 			*id = 0;
2998f0fba2adSLiam Girdwood 	}
2999f0fba2adSLiam Girdwood 
3000f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3001f0fba2adSLiam Girdwood }
3002f0fba2adSLiam Girdwood 
3003f0fba2adSLiam Girdwood /*
3004f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3005f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3006f0fba2adSLiam Girdwood  */
3007f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3008f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3009f0fba2adSLiam Girdwood {
3010f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
3011f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3012f0fba2adSLiam Girdwood 				dev_name(dev));
3013f0fba2adSLiam Girdwood 		return NULL;
3014f0fba2adSLiam Girdwood 	}
3015f0fba2adSLiam Girdwood 
3016f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3017f0fba2adSLiam Girdwood }
3018f0fba2adSLiam Girdwood 
30199115171aSMark Brown /**
30209115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
30219115171aSMark Brown  *
3022ac11a2b3SMark Brown  * @dai: DAI to register
30239115171aSMark Brown  */
3024f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3025f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
30269115171aSMark Brown {
3027f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
30289115171aSMark Brown 
3029f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
30309115171aSMark Brown 
3031f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3032f0fba2adSLiam Girdwood 	if (dai == NULL)
3033f0fba2adSLiam Girdwood 			return -ENOMEM;
30346335d055SEric Miao 
3035f0fba2adSLiam Girdwood 	/* create DAI component name */
3036f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3037f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3038f0fba2adSLiam Girdwood 		kfree(dai);
3039f0fba2adSLiam Girdwood 		return -ENOMEM;
3040f0fba2adSLiam Girdwood 	}
3041f0fba2adSLiam Girdwood 
3042f0fba2adSLiam Girdwood 	dai->dev = dev;
3043f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3044f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3045f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
30469115171aSMark Brown 
30479115171aSMark Brown 	mutex_lock(&client_mutex);
30489115171aSMark Brown 	list_add(&dai->list, &dai_list);
3049435c5e25SMark Brown 	snd_soc_instantiate_cards();
30509115171aSMark Brown 	mutex_unlock(&client_mutex);
30519115171aSMark Brown 
30529115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
30539115171aSMark Brown 
30549115171aSMark Brown 	return 0;
30559115171aSMark Brown }
30569115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
30579115171aSMark Brown 
30589115171aSMark Brown /**
30599115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
30609115171aSMark Brown  *
3061ac11a2b3SMark Brown  * @dai: DAI to unregister
30629115171aSMark Brown  */
3063f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
30649115171aSMark Brown {
3065f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3066f0fba2adSLiam Girdwood 
3067f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3068f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3069f0fba2adSLiam Girdwood 			goto found;
3070f0fba2adSLiam Girdwood 	}
3071f0fba2adSLiam Girdwood 	return;
3072f0fba2adSLiam Girdwood 
3073f0fba2adSLiam Girdwood found:
30749115171aSMark Brown 	mutex_lock(&client_mutex);
30759115171aSMark Brown 	list_del(&dai->list);
30769115171aSMark Brown 	mutex_unlock(&client_mutex);
30779115171aSMark Brown 
30789115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3079f0fba2adSLiam Girdwood 	kfree(dai->name);
3080f0fba2adSLiam Girdwood 	kfree(dai);
30819115171aSMark Brown }
30829115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
30839115171aSMark Brown 
30849115171aSMark Brown /**
30859115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
30869115171aSMark Brown  *
3087ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3088ac11a2b3SMark Brown  * @count: Number of DAIs
30899115171aSMark Brown  */
3090f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3091f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
30929115171aSMark Brown {
3093f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3094f0fba2adSLiam Girdwood 	int i, ret = 0;
3095f0fba2adSLiam Girdwood 
3096720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
30979115171aSMark Brown 
30989115171aSMark Brown 	for (i = 0; i < count; i++) {
3099f0fba2adSLiam Girdwood 
3100f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3101c46e0079SAxel Lin 		if (dai == NULL) {
3102c46e0079SAxel Lin 			ret = -ENOMEM;
3103c46e0079SAxel Lin 			goto err;
3104c46e0079SAxel Lin 		}
3105f0fba2adSLiam Girdwood 
3106f0fba2adSLiam Girdwood 		/* create DAI component name */
3107f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3108f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3109f0fba2adSLiam Girdwood 			kfree(dai);
3110f0fba2adSLiam Girdwood 			ret = -EINVAL;
31119115171aSMark Brown 			goto err;
31129115171aSMark Brown 		}
31139115171aSMark Brown 
3114f0fba2adSLiam Girdwood 		dai->dev = dev;
3115f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
31160f9141c9SMark Brown 		if (dai->driver->id)
31170f9141c9SMark Brown 			dai->id = dai->driver->id;
31180f9141c9SMark Brown 		else
31190f9141c9SMark Brown 			dai->id = i;
3120f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3121f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3122f0fba2adSLiam Girdwood 
3123f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3124f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3125f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3126f0fba2adSLiam Girdwood 
3127f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3128f0fba2adSLiam Girdwood 	}
3129f0fba2adSLiam Girdwood 
3130f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
31319115171aSMark Brown 	return 0;
31329115171aSMark Brown 
31339115171aSMark Brown err:
31349115171aSMark Brown 	for (i--; i >= 0; i--)
3135f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
31369115171aSMark Brown 
31379115171aSMark Brown 	return ret;
31389115171aSMark Brown }
31399115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
31409115171aSMark Brown 
31419115171aSMark Brown /**
31429115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
31439115171aSMark Brown  *
3144ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3145ac11a2b3SMark Brown  * @count: Number of DAIs
31469115171aSMark Brown  */
3147f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
31489115171aSMark Brown {
31499115171aSMark Brown 	int i;
31509115171aSMark Brown 
31519115171aSMark Brown 	for (i = 0; i < count; i++)
3152f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
31539115171aSMark Brown }
31549115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
31559115171aSMark Brown 
315612a48a8cSMark Brown /**
315712a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
315812a48a8cSMark Brown  *
3159ac11a2b3SMark Brown  * @platform: platform to register
316012a48a8cSMark Brown  */
3161f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3162f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
316312a48a8cSMark Brown {
3164f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
316512a48a8cSMark Brown 
3166f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3167f0fba2adSLiam Girdwood 
3168f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3169f0fba2adSLiam Girdwood 	if (platform == NULL)
3170f0fba2adSLiam Girdwood 			return -ENOMEM;
3171f0fba2adSLiam Girdwood 
3172f0fba2adSLiam Girdwood 	/* create platform component name */
3173f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3174f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3175f0fba2adSLiam Girdwood 		kfree(platform);
3176f0fba2adSLiam Girdwood 		return -ENOMEM;
3177f0fba2adSLiam Girdwood 	}
3178f0fba2adSLiam Girdwood 
3179f0fba2adSLiam Girdwood 	platform->dev = dev;
3180f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
318112a48a8cSMark Brown 
318212a48a8cSMark Brown 	mutex_lock(&client_mutex);
318312a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3184435c5e25SMark Brown 	snd_soc_instantiate_cards();
318512a48a8cSMark Brown 	mutex_unlock(&client_mutex);
318612a48a8cSMark Brown 
318712a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
318812a48a8cSMark Brown 
318912a48a8cSMark Brown 	return 0;
319012a48a8cSMark Brown }
319112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
319212a48a8cSMark Brown 
319312a48a8cSMark Brown /**
319412a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
319512a48a8cSMark Brown  *
3196ac11a2b3SMark Brown  * @platform: platform to unregister
319712a48a8cSMark Brown  */
3198f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
319912a48a8cSMark Brown {
3200f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3201f0fba2adSLiam Girdwood 
3202f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3203f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3204f0fba2adSLiam Girdwood 			goto found;
3205f0fba2adSLiam Girdwood 	}
3206f0fba2adSLiam Girdwood 	return;
3207f0fba2adSLiam Girdwood 
3208f0fba2adSLiam Girdwood found:
320912a48a8cSMark Brown 	mutex_lock(&client_mutex);
321012a48a8cSMark Brown 	list_del(&platform->list);
321112a48a8cSMark Brown 	mutex_unlock(&client_mutex);
321212a48a8cSMark Brown 
321312a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3214f0fba2adSLiam Girdwood 	kfree(platform->name);
3215f0fba2adSLiam Girdwood 	kfree(platform);
321612a48a8cSMark Brown }
321712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
321812a48a8cSMark Brown 
3219151ab22cSMark Brown static u64 codec_format_map[] = {
3220151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3221151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3222151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3223151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3224151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3225151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3226151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3227151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3228151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3229151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3230151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3231151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3232151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3233151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3234151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3235151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3236151ab22cSMark Brown };
3237151ab22cSMark Brown 
3238151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3239151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3240151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3241151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3242151ab22cSMark Brown  */
3243151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3244151ab22cSMark Brown {
3245151ab22cSMark Brown 	int i;
3246151ab22cSMark Brown 
3247151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3248151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3249151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3250151ab22cSMark Brown }
3251151ab22cSMark Brown 
32520d0cf00aSMark Brown /**
32530d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
32540d0cf00aSMark Brown  *
3255ac11a2b3SMark Brown  * @codec: codec to register
32560d0cf00aSMark Brown  */
3257f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3258f0fba2adSLiam Girdwood 		struct snd_soc_codec_driver *codec_drv,
3259f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, int num_dai)
32600d0cf00aSMark Brown {
3261f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3262f0fba2adSLiam Girdwood 	int ret, i;
3263151ab22cSMark Brown 
3264f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
32650d0cf00aSMark Brown 
3266f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3267f0fba2adSLiam Girdwood 	if (codec == NULL)
3268f0fba2adSLiam Girdwood 		return -ENOMEM;
32690d0cf00aSMark Brown 
3270f0fba2adSLiam Girdwood 	/* create CODEC component name */
3271f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3272f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3273f0fba2adSLiam Girdwood 		kfree(codec);
3274f0fba2adSLiam Girdwood 		return -ENOMEM;
3275151ab22cSMark Brown 	}
3276151ab22cSMark Brown 
3277ce6120ccSLiam Girdwood 	INIT_LIST_HEAD(&codec->dapm.widgets);
3278ce6120ccSLiam Girdwood 	INIT_LIST_HEAD(&codec->dapm.paths);
3279ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3280ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3281ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3282ce6120ccSLiam Girdwood 
3283f0fba2adSLiam Girdwood 	/* allocate CODEC register cache */
3284f0fba2adSLiam Girdwood 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3285f0fba2adSLiam Girdwood 
3286f0fba2adSLiam Girdwood 		if (codec_drv->reg_cache_default)
3287f0fba2adSLiam Girdwood 			codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3288f0fba2adSLiam Girdwood 				codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3289f0fba2adSLiam Girdwood 		else
3290f0fba2adSLiam Girdwood 			codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3291f0fba2adSLiam Girdwood 				codec_drv->reg_word_size, GFP_KERNEL);
3292f0fba2adSLiam Girdwood 
3293f0fba2adSLiam Girdwood 		if (codec->reg_cache == NULL) {
3294f0fba2adSLiam Girdwood 			kfree(codec->name);
3295f0fba2adSLiam Girdwood 			kfree(codec);
3296f0fba2adSLiam Girdwood 			return -ENOMEM;
3297f0fba2adSLiam Girdwood 		}
3298f0fba2adSLiam Girdwood 	}
3299f0fba2adSLiam Girdwood 
3300f0fba2adSLiam Girdwood 	codec->dev = dev;
3301f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3302f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3303f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3304f0fba2adSLiam Girdwood 
3305f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3306f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3307f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3308f0fba2adSLiam Girdwood 	}
3309f0fba2adSLiam Girdwood 
331026b01ccdSMark Brown 	/* register any DAIs */
331126b01ccdSMark Brown 	if (num_dai) {
3312f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3313f0fba2adSLiam Girdwood 		if (ret < 0)
3314f0fba2adSLiam Girdwood 			goto error;
331526b01ccdSMark Brown 	}
3316f0fba2adSLiam Girdwood 
33170d0cf00aSMark Brown 	mutex_lock(&client_mutex);
33180d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
33190d0cf00aSMark Brown 	snd_soc_instantiate_cards();
33200d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
33210d0cf00aSMark Brown 
33220d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
33230d0cf00aSMark Brown 	return 0;
3324f0fba2adSLiam Girdwood 
3325f0fba2adSLiam Girdwood error:
3326f0fba2adSLiam Girdwood 	if (codec->reg_cache)
3327f0fba2adSLiam Girdwood 		kfree(codec->reg_cache);
3328f0fba2adSLiam Girdwood 	kfree(codec->name);
3329f0fba2adSLiam Girdwood 	kfree(codec);
3330f0fba2adSLiam Girdwood 	return ret;
33310d0cf00aSMark Brown }
33320d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
33330d0cf00aSMark Brown 
33340d0cf00aSMark Brown /**
33350d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
33360d0cf00aSMark Brown  *
3337ac11a2b3SMark Brown  * @codec: codec to unregister
33380d0cf00aSMark Brown  */
3339f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
33400d0cf00aSMark Brown {
3341f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3342f0fba2adSLiam Girdwood 	int i;
3343f0fba2adSLiam Girdwood 
3344f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3345f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3346f0fba2adSLiam Girdwood 			goto found;
3347f0fba2adSLiam Girdwood 	}
3348f0fba2adSLiam Girdwood 	return;
3349f0fba2adSLiam Girdwood 
3350f0fba2adSLiam Girdwood found:
335126b01ccdSMark Brown 	if (codec->num_dai)
3352f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3353f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3354f0fba2adSLiam Girdwood 
33550d0cf00aSMark Brown 	mutex_lock(&client_mutex);
33560d0cf00aSMark Brown 	list_del(&codec->list);
33570d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
33580d0cf00aSMark Brown 
33590d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3360f0fba2adSLiam Girdwood 
3361f0fba2adSLiam Girdwood 	if (codec->reg_cache)
3362f0fba2adSLiam Girdwood 		kfree(codec->reg_cache);
33631aafcd4dSDimitris Papastamos 	kfree(codec->name);
3364f0fba2adSLiam Girdwood 	kfree(codec);
33650d0cf00aSMark Brown }
33660d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
33670d0cf00aSMark Brown 
3368c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3369db2a4165SFrank Mandarino {
3370384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3371384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
3372384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
3373384c89e2SMark Brown 		printk(KERN_WARNING
3374384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
3375384c89e2SMark Brown 		debugfs_root = NULL;
3376384c89e2SMark Brown 	}
3377c3c5a19aSMark Brown 
3378c3c5a19aSMark Brown 	if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3379c3c5a19aSMark Brown 				 &codec_list_fops))
3380c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3381c3c5a19aSMark Brown 
3382f3208780SMark Brown 	if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3383f3208780SMark Brown 				 &dai_list_fops))
3384f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
338519c7ac27SMark Brown 
338619c7ac27SMark Brown 	if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
338719c7ac27SMark Brown 				 &platform_list_fops))
338819c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3389384c89e2SMark Brown #endif
3390384c89e2SMark Brown 
3391db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3392db2a4165SFrank Mandarino }
33934abe8e16SMark Brown module_init(snd_soc_init);
3394db2a4165SFrank Mandarino 
33957d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3396db2a4165SFrank Mandarino {
3397384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3398384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
3399384c89e2SMark Brown #endif
3400db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3401db2a4165SFrank Mandarino }
3402db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3403db2a4165SFrank Mandarino 
3404db2a4165SFrank Mandarino /* Module information */
3405d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3406db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3407db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34088b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3409