xref: /openbmc/linux/sound/soc/soc-core.c (revision 861f2faf4fbd4fb46a436d41dcb5ff75b243ab3a)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6f0fba2adSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7f0fba2adSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
80664d888SLiam Girdwood  *
9d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
100664d888SLiam Girdwood  *         with code, comments and ideas from :-
110664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
14db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
15db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
16db2a4165SFrank Mandarino  *  option) any later version.
17db2a4165SFrank Mandarino  *
18db2a4165SFrank Mandarino  *  TODO:
19db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
20db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
21db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
22db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
23db2a4165SFrank Mandarino  */
24db2a4165SFrank Mandarino 
25db2a4165SFrank Mandarino #include <linux/module.h>
26db2a4165SFrank Mandarino #include <linux/moduleparam.h>
27db2a4165SFrank Mandarino #include <linux/init.h>
28db2a4165SFrank Mandarino #include <linux/delay.h>
29db2a4165SFrank Mandarino #include <linux/pm.h>
30db2a4165SFrank Mandarino #include <linux/bitops.h>
3112ef193dSTroy Kisky #include <linux/debugfs.h>
32db2a4165SFrank Mandarino #include <linux/platform_device.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
34474828a4SMarek Vasut #include <sound/ac97_codec.h>
35db2a4165SFrank Mandarino #include <sound/core.h>
363028eb8cSMark Brown #include <sound/jack.h>
37db2a4165SFrank Mandarino #include <sound/pcm.h>
38db2a4165SFrank Mandarino #include <sound/pcm_params.h>
39db2a4165SFrank Mandarino #include <sound/soc.h>
40db2a4165SFrank Mandarino #include <sound/initval.h>
41db2a4165SFrank Mandarino 
42a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
43a8b1d34fSMark Brown #include <trace/events/asoc.h>
44a8b1d34fSMark Brown 
45f0fba2adSLiam Girdwood #define NAME_SIZE	32
46f0fba2adSLiam Girdwood 
47db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex);
48db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49db2a4165SFrank Mandarino 
50384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
518a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
528a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53384c89e2SMark Brown #endif
54384c89e2SMark Brown 
55c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
56c5af3a2eSMark Brown static LIST_HEAD(card_list);
579115171aSMark Brown static LIST_HEAD(dai_list);
5812a48a8cSMark Brown static LIST_HEAD(platform_list);
590d0cf00aSMark Brown static LIST_HEAD(codec_list);
60c5af3a2eSMark Brown 
61c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card);
62c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card);
63f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
64c5af3a2eSMark Brown 
65db2a4165SFrank Mandarino /*
66db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
67db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
68db2a4165SFrank Mandarino  * between two audio tracks.
69db2a4165SFrank Mandarino  */
70db2a4165SFrank Mandarino static int pmdown_time = 5000;
71db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
72db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
73db2a4165SFrank Mandarino 
742624d5faSMark Brown /* codec register dump */
752624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
762624d5faSMark Brown {
775164d74dSMark Brown 	int ret, i, step = 1, count = 0;
782624d5faSMark Brown 
79f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
802624d5faSMark Brown 		return 0;
812624d5faSMark Brown 
82f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
83f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
842624d5faSMark Brown 
852624d5faSMark Brown 	count += sprintf(buf, "%s registers\n", codec->name);
86f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
87f0fba2adSLiam Girdwood 		if (codec->driver->readable_register && !codec->driver->readable_register(i))
882624d5faSMark Brown 			continue;
892624d5faSMark Brown 
902624d5faSMark Brown 		count += sprintf(buf + count, "%2x: ", i);
912624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
922624d5faSMark Brown 			break;
932624d5faSMark Brown 
94f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
95f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
962624d5faSMark Brown 							 PAGE_SIZE - count, i);
975164d74dSMark Brown 		} else {
985164d74dSMark Brown 			/* If the read fails it's almost certainly due to
995164d74dSMark Brown 			 * the register being volatile and the device being
1005164d74dSMark Brown 			 * powered off.
1015164d74dSMark Brown 			 */
102e4f078d8SDimitris Papastamos 			ret = snd_soc_read(codec, i);
1035164d74dSMark Brown 			if (ret >= 0)
1045164d74dSMark Brown 				count += snprintf(buf + count,
1055164d74dSMark Brown 						  PAGE_SIZE - count,
1065164d74dSMark Brown 						  "%4x", ret);
1072624d5faSMark Brown 			else
1085164d74dSMark Brown 				count += snprintf(buf + count,
1095164d74dSMark Brown 						  PAGE_SIZE - count,
1105164d74dSMark Brown 						  "<no data: %d>", ret);
1115164d74dSMark Brown 		}
1122624d5faSMark Brown 
1132624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1142624d5faSMark Brown 			break;
1152624d5faSMark Brown 
1162624d5faSMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1172624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1182624d5faSMark Brown 			break;
1192624d5faSMark Brown 	}
1202624d5faSMark Brown 
1212624d5faSMark Brown 	/* Truncate count; min() would cause a warning */
1222624d5faSMark Brown 	if (count >= PAGE_SIZE)
1232624d5faSMark Brown 		count = PAGE_SIZE - 1;
1242624d5faSMark Brown 
1252624d5faSMark Brown 	return count;
1262624d5faSMark Brown }
1272624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1282624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1292624d5faSMark Brown {
130f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
131f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
132f0fba2adSLiam Girdwood 
133f0fba2adSLiam Girdwood 	return soc_codec_reg_show(rtd->codec, buf);
1342624d5faSMark Brown }
1352624d5faSMark Brown 
1362624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1372624d5faSMark Brown 
138dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
139dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
140dbe21408SMark Brown {
141f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
142f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
143dbe21408SMark Brown 
144f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
145dbe21408SMark Brown }
146dbe21408SMark Brown 
147dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
148dbe21408SMark Brown 			       struct device_attribute *attr,
149dbe21408SMark Brown 			       const char *buf, size_t count)
150dbe21408SMark Brown {
151f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
152f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
153c593b520SMark Brown 	int ret;
154dbe21408SMark Brown 
155c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
156c593b520SMark Brown 	if (ret)
157c593b520SMark Brown 		return ret;
158dbe21408SMark Brown 
159dbe21408SMark Brown 	return count;
160dbe21408SMark Brown }
161dbe21408SMark Brown 
162dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
163dbe21408SMark Brown 
1642624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
1652624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
1662624d5faSMark Brown {
1672624d5faSMark Brown 	file->private_data = inode->i_private;
1682624d5faSMark Brown 	return 0;
1692624d5faSMark Brown }
1702624d5faSMark Brown 
1712624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1722624d5faSMark Brown 			       size_t count, loff_t *ppos)
1732624d5faSMark Brown {
1742624d5faSMark Brown 	ssize_t ret;
1752624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1762624d5faSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1772624d5faSMark Brown 	if (!buf)
1782624d5faSMark Brown 		return -ENOMEM;
1792624d5faSMark Brown 	ret = soc_codec_reg_show(codec, buf);
1802624d5faSMark Brown 	if (ret >= 0)
1812624d5faSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1822624d5faSMark Brown 	kfree(buf);
1832624d5faSMark Brown 	return ret;
1842624d5faSMark Brown }
1852624d5faSMark Brown 
1862624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
1872624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
1882624d5faSMark Brown {
1892624d5faSMark Brown 	char buf[32];
1902624d5faSMark Brown 	int buf_size;
1912624d5faSMark Brown 	char *start = buf;
1922624d5faSMark Brown 	unsigned long reg, value;
1932624d5faSMark Brown 	int step = 1;
1942624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1952624d5faSMark Brown 
1962624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
1972624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
1982624d5faSMark Brown 		return -EFAULT;
1992624d5faSMark Brown 	buf[buf_size] = 0;
2002624d5faSMark Brown 
201f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
202f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
2032624d5faSMark Brown 
2042624d5faSMark Brown 	while (*start == ' ')
2052624d5faSMark Brown 		start++;
2062624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
207f0fba2adSLiam Girdwood 	if ((reg >= codec->driver->reg_cache_size) || (reg % step))
2082624d5faSMark Brown 		return -EINVAL;
2092624d5faSMark Brown 	while (*start == ' ')
2102624d5faSMark Brown 		start++;
2112624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2122624d5faSMark Brown 		return -EINVAL;
2130d51a9cbSMark Brown 
2140d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2150d51a9cbSMark Brown 	add_taint(TAINT_USER);
2160d51a9cbSMark Brown 
217e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2182624d5faSMark Brown 	return buf_size;
2192624d5faSMark Brown }
2202624d5faSMark Brown 
2212624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2222624d5faSMark Brown 	.open = codec_reg_open_file,
2232624d5faSMark Brown 	.read = codec_reg_read_file,
2242624d5faSMark Brown 	.write = codec_reg_write_file,
2256038f373SArnd Bergmann 	.llseek = default_llseek,
2262624d5faSMark Brown };
2272624d5faSMark Brown 
2282624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2292624d5faSMark Brown {
230d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
231d6ce4cf3SJarkko Nikula 
2326ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
233d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2342624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2352624d5faSMark Brown 		printk(KERN_WARNING
2362624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2372624d5faSMark Brown 		return;
2382624d5faSMark Brown 	}
2392624d5faSMark Brown 
2402624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2412624d5faSMark Brown 						 codec->debugfs_codec_root,
2422624d5faSMark Brown 						 codec, &codec_reg_fops);
2432624d5faSMark Brown 	if (!codec->debugfs_reg)
2442624d5faSMark Brown 		printk(KERN_WARNING
2452624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
2462624d5faSMark Brown 
247ce6120ccSLiam Girdwood 	codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
2482624d5faSMark Brown 						 codec->debugfs_codec_root);
249ce6120ccSLiam Girdwood 	if (!codec->dapm.debugfs_dapm)
2502624d5faSMark Brown 		printk(KERN_WARNING
2512624d5faSMark Brown 		       "Failed to create DAPM debugfs directory\n");
2522624d5faSMark Brown 
253ce6120ccSLiam Girdwood 	snd_soc_dapm_debugfs_init(&codec->dapm);
2542624d5faSMark Brown }
2552624d5faSMark Brown 
2562624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2572624d5faSMark Brown {
2582624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2592624d5faSMark Brown }
2602624d5faSMark Brown 
261c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
262c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
263c3c5a19aSMark Brown {
264c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2652b194f9dSMark Brown 	ssize_t len, ret = 0;
266c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
267c3c5a19aSMark Brown 
268c3c5a19aSMark Brown 	if (!buf)
269c3c5a19aSMark Brown 		return -ENOMEM;
270c3c5a19aSMark Brown 
2712b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
2722b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
273c3c5a19aSMark Brown 			       codec->name);
2742b194f9dSMark Brown 		if (len >= 0)
2752b194f9dSMark Brown 			ret += len;
2762b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
2772b194f9dSMark Brown 			ret = PAGE_SIZE;
2782b194f9dSMark Brown 			break;
2792b194f9dSMark Brown 		}
2802b194f9dSMark Brown 	}
281c3c5a19aSMark Brown 
282c3c5a19aSMark Brown 	if (ret >= 0)
283c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
284c3c5a19aSMark Brown 
285c3c5a19aSMark Brown 	kfree(buf);
286c3c5a19aSMark Brown 
287c3c5a19aSMark Brown 	return ret;
288c3c5a19aSMark Brown }
289c3c5a19aSMark Brown 
290c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
291c3c5a19aSMark Brown 	.read = codec_list_read_file,
292c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
293c3c5a19aSMark Brown };
294c3c5a19aSMark Brown 
295f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
296f3208780SMark Brown 				  size_t count, loff_t *ppos)
297f3208780SMark Brown {
298f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2992b194f9dSMark Brown 	ssize_t len, ret = 0;
300f3208780SMark Brown 	struct snd_soc_dai *dai;
301f3208780SMark Brown 
302f3208780SMark Brown 	if (!buf)
303f3208780SMark Brown 		return -ENOMEM;
304f3208780SMark Brown 
3052b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3062b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3072b194f9dSMark Brown 		if (len >= 0)
3082b194f9dSMark Brown 			ret += len;
3092b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3102b194f9dSMark Brown 			ret = PAGE_SIZE;
3112b194f9dSMark Brown 			break;
3122b194f9dSMark Brown 		}
3132b194f9dSMark Brown 	}
314f3208780SMark Brown 
315f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
316f3208780SMark Brown 
317f3208780SMark Brown 	kfree(buf);
318f3208780SMark Brown 
319f3208780SMark Brown 	return ret;
320f3208780SMark Brown }
321f3208780SMark Brown 
322f3208780SMark Brown static const struct file_operations dai_list_fops = {
323f3208780SMark Brown 	.read = dai_list_read_file,
324f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
325f3208780SMark Brown };
326f3208780SMark Brown 
32719c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
32819c7ac27SMark Brown 				       char __user *user_buf,
32919c7ac27SMark Brown 				       size_t count, loff_t *ppos)
33019c7ac27SMark Brown {
33119c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3322b194f9dSMark Brown 	ssize_t len, ret = 0;
33319c7ac27SMark Brown 	struct snd_soc_platform *platform;
33419c7ac27SMark Brown 
33519c7ac27SMark Brown 	if (!buf)
33619c7ac27SMark Brown 		return -ENOMEM;
33719c7ac27SMark Brown 
3382b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3392b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
34019c7ac27SMark Brown 			       platform->name);
3412b194f9dSMark Brown 		if (len >= 0)
3422b194f9dSMark Brown 			ret += len;
3432b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3442b194f9dSMark Brown 			ret = PAGE_SIZE;
3452b194f9dSMark Brown 			break;
3462b194f9dSMark Brown 		}
3472b194f9dSMark Brown 	}
34819c7ac27SMark Brown 
34919c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
35019c7ac27SMark Brown 
35119c7ac27SMark Brown 	kfree(buf);
35219c7ac27SMark Brown 
35319c7ac27SMark Brown 	return ret;
35419c7ac27SMark Brown }
35519c7ac27SMark Brown 
35619c7ac27SMark Brown static const struct file_operations platform_list_fops = {
35719c7ac27SMark Brown 	.read = platform_list_read_file,
35819c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
35919c7ac27SMark Brown };
36019c7ac27SMark Brown 
361a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
362a6052154SJarkko Nikula {
363a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
3648a9dab1aSMark Brown 						     snd_soc_debugfs_root);
3653a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
366a6052154SJarkko Nikula 		dev_warn(card->dev,
367a6052154SJarkko Nikula 			 "ASoC: Failed to create codec debugfs directory\n");
3683a45b867SJarkko Nikula 		return;
3693a45b867SJarkko Nikula 	}
3703a45b867SJarkko Nikula 
3713a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
3723a45b867SJarkko Nikula 						    card->debugfs_card_root,
3733a45b867SJarkko Nikula 						    &card->pop_time);
3743a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
3753a45b867SJarkko Nikula 		dev_warn(card->dev,
3763a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
377a6052154SJarkko Nikula }
378a6052154SJarkko Nikula 
379a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
380a6052154SJarkko Nikula {
381a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
382a6052154SJarkko Nikula }
383a6052154SJarkko Nikula 
3842624d5faSMark Brown #else
3852624d5faSMark Brown 
3862624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
3872624d5faSMark Brown {
3882624d5faSMark Brown }
3892624d5faSMark Brown 
3902624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3912624d5faSMark Brown {
3922624d5faSMark Brown }
393b95fccbcSAxel Lin 
394b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
395b95fccbcSAxel Lin {
396b95fccbcSAxel Lin }
397b95fccbcSAxel Lin 
398b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
399b95fccbcSAxel Lin {
400b95fccbcSAxel Lin }
4012624d5faSMark Brown #endif
4022624d5faSMark Brown 
403db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
404db2a4165SFrank Mandarino /* unregister ac97 codec */
405db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
406db2a4165SFrank Mandarino {
407db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
408db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
409db2a4165SFrank Mandarino 	return 0;
410db2a4165SFrank Mandarino }
411db2a4165SFrank Mandarino 
412db2a4165SFrank Mandarino /* stop no dev release warning */
413db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
414db2a4165SFrank Mandarino 
415db2a4165SFrank Mandarino /* register ac97 codec to bus */
416db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
417db2a4165SFrank Mandarino {
418db2a4165SFrank Mandarino 	int err;
419db2a4165SFrank Mandarino 
420db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4214ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
422db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
423db2a4165SFrank Mandarino 
424bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
425f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
426db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
427db2a4165SFrank Mandarino 	if (err < 0) {
428db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
429db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
430db2a4165SFrank Mandarino 		return err;
431db2a4165SFrank Mandarino 	}
432db2a4165SFrank Mandarino 	return 0;
433db2a4165SFrank Mandarino }
434db2a4165SFrank Mandarino #endif
435db2a4165SFrank Mandarino 
43606f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
43706f409d7SMark Brown {
43806f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
439f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
440f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
44106f409d7SMark Brown 	int ret;
44206f409d7SMark Brown 
443f0fba2adSLiam Girdwood 	if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
444f0fba2adSLiam Girdwood 			rtd->dai_link->symmetric_rates) {
445f0fba2adSLiam Girdwood 		dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
446f0fba2adSLiam Girdwood 				rtd->rate);
44706f409d7SMark Brown 
44806f409d7SMark Brown 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
44906f409d7SMark Brown 						   SNDRV_PCM_HW_PARAM_RATE,
450f0fba2adSLiam Girdwood 						   rtd->rate,
451f0fba2adSLiam Girdwood 						   rtd->rate);
45206f409d7SMark Brown 		if (ret < 0) {
453f0fba2adSLiam Girdwood 			dev_err(&rtd->dev,
45406f409d7SMark Brown 				"Unable to apply rate symmetry constraint: %d\n", ret);
45506f409d7SMark Brown 			return ret;
45606f409d7SMark Brown 		}
45706f409d7SMark Brown 	}
45806f409d7SMark Brown 
45906f409d7SMark Brown 	return 0;
46006f409d7SMark Brown }
46106f409d7SMark Brown 
462db2a4165SFrank Mandarino /*
463db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
464db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
465db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
466db2a4165SFrank Mandarino  */
467db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
468db2a4165SFrank Mandarino {
469db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
470db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
471f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
472f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
473f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
474f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
475f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
476db2a4165SFrank Mandarino 	int ret = 0;
477db2a4165SFrank Mandarino 
478db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
479db2a4165SFrank Mandarino 
480db2a4165SFrank Mandarino 	/* startup the audio subsystem */
481f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->startup) {
482f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
483db2a4165SFrank Mandarino 		if (ret < 0) {
484db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
485cb666e5bSLiam Girdwood 				cpu_dai->name);
486db2a4165SFrank Mandarino 			goto out;
487db2a4165SFrank Mandarino 		}
488db2a4165SFrank Mandarino 	}
489db2a4165SFrank Mandarino 
490f0fba2adSLiam Girdwood 	if (platform->driver->ops->open) {
491f0fba2adSLiam Girdwood 		ret = platform->driver->ops->open(substream);
492db2a4165SFrank Mandarino 		if (ret < 0) {
493db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
494db2a4165SFrank Mandarino 			goto platform_err;
495db2a4165SFrank Mandarino 		}
496db2a4165SFrank Mandarino 	}
497db2a4165SFrank Mandarino 
498f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->startup) {
499f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->startup(substream, codec_dai);
500cb666e5bSLiam Girdwood 		if (ret < 0) {
501cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
502cb666e5bSLiam Girdwood 				codec_dai->name);
503cb666e5bSLiam Girdwood 			goto codec_dai_err;
504cb666e5bSLiam Girdwood 		}
505cb666e5bSLiam Girdwood 	}
506cb666e5bSLiam Girdwood 
507f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
508f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
509db2a4165SFrank Mandarino 		if (ret < 0) {
510f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
511db2a4165SFrank Mandarino 			goto machine_err;
512db2a4165SFrank Mandarino 		}
513db2a4165SFrank Mandarino 	}
514db2a4165SFrank Mandarino 
515a00f90f9SMark Brown 	/* Check that the codec and cpu DAIs are compatible */
516db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
517db2a4165SFrank Mandarino 		runtime->hw.rate_min =
518f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.rate_min,
519f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_min);
520db2a4165SFrank Mandarino 		runtime->hw.rate_max =
521f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.rate_max,
522f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_max);
523db2a4165SFrank Mandarino 		runtime->hw.channels_min =
524f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.channels_min,
525f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_min);
526db2a4165SFrank Mandarino 		runtime->hw.channels_max =
527f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.channels_max,
528f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_max);
529cb666e5bSLiam Girdwood 		runtime->hw.formats =
530f0fba2adSLiam Girdwood 			codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
531cb666e5bSLiam Girdwood 		runtime->hw.rates =
532f0fba2adSLiam Girdwood 			codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
533f0fba2adSLiam Girdwood 		if (codec_dai_drv->playback.rates
534d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
535f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->playback.rates;
536f0fba2adSLiam Girdwood 		if (cpu_dai_drv->playback.rates
537d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
538f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->playback.rates;
539db2a4165SFrank Mandarino 	} else {
540db2a4165SFrank Mandarino 		runtime->hw.rate_min =
541f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.rate_min,
542f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_min);
543db2a4165SFrank Mandarino 		runtime->hw.rate_max =
544f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.rate_max,
545f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_max);
546db2a4165SFrank Mandarino 		runtime->hw.channels_min =
547f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.channels_min,
548f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_min);
549db2a4165SFrank Mandarino 		runtime->hw.channels_max =
550f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.channels_max,
551f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_max);
552cb666e5bSLiam Girdwood 		runtime->hw.formats =
553f0fba2adSLiam Girdwood 			codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
554cb666e5bSLiam Girdwood 		runtime->hw.rates =
555f0fba2adSLiam Girdwood 			codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
556f0fba2adSLiam Girdwood 		if (codec_dai_drv->capture.rates
557d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
558f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->capture.rates;
559f0fba2adSLiam Girdwood 		if (cpu_dai_drv->capture.rates
560d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
561f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->capture.rates;
562db2a4165SFrank Mandarino 	}
563db2a4165SFrank Mandarino 
564db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
565db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
566db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
567cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
568bb1c0478SJassi Brar 		goto config_err;
569db2a4165SFrank Mandarino 	}
570db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
571db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
572cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
573bb1c0478SJassi Brar 		goto config_err;
574db2a4165SFrank Mandarino 	}
575db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
576db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
577cb666e5bSLiam Girdwood 				codec_dai->name, cpu_dai->name);
578bb1c0478SJassi Brar 		goto config_err;
579db2a4165SFrank Mandarino 	}
580db2a4165SFrank Mandarino 
58106f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
58206f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
58306f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
58406f409d7SMark Brown 		if (ret != 0)
585bb1c0478SJassi Brar 			goto config_err;
58606f409d7SMark Brown 	}
58706f409d7SMark Brown 
588f0fba2adSLiam Girdwood 	pr_debug("asoc: %s <-> %s info:\n",
589f0fba2adSLiam Girdwood 			codec_dai->name, cpu_dai->name);
590f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
591f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
592b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
593f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
594b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
595db2a4165SFrank Mandarino 
59614dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597f0fba2adSLiam Girdwood 		cpu_dai->playback_active++;
598f0fba2adSLiam Girdwood 		codec_dai->playback_active++;
59914dc5734SJassi Brar 	} else {
600f0fba2adSLiam Girdwood 		cpu_dai->capture_active++;
601f0fba2adSLiam Girdwood 		codec_dai->capture_active++;
60214dc5734SJassi Brar 	}
60314dc5734SJassi Brar 	cpu_dai->active++;
60414dc5734SJassi Brar 	codec_dai->active++;
605f0fba2adSLiam Girdwood 	rtd->codec->active++;
606db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
607db2a4165SFrank Mandarino 	return 0;
608db2a4165SFrank Mandarino 
609bb1c0478SJassi Brar config_err:
610f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
611f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
612db2a4165SFrank Mandarino 
613bb1c0478SJassi Brar machine_err:
614f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
615f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
616bb1c0478SJassi Brar 
617cb666e5bSLiam Girdwood codec_dai_err:
618f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
619f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
620db2a4165SFrank Mandarino 
621db2a4165SFrank Mandarino platform_err:
622f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
623f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
624db2a4165SFrank Mandarino out:
625db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
626db2a4165SFrank Mandarino 	return ret;
627db2a4165SFrank Mandarino }
628db2a4165SFrank Mandarino 
629db2a4165SFrank Mandarino /*
6303a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
631db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
632db2a4165SFrank Mandarino  * due to DAPM power cycling.
633db2a4165SFrank Mandarino  */
6344484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
635db2a4165SFrank Mandarino {
636f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
637f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
638f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
639db2a4165SFrank Mandarino 
640db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
641db2a4165SFrank Mandarino 
642f24368c2SMark Brown 	pr_debug("pop wq checking: %s status: %s waiting: %s\n",
643f0fba2adSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
644f0fba2adSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
645db2a4165SFrank Mandarino 		 codec_dai->pop_wait ? "yes" : "no");
646db2a4165SFrank Mandarino 
647db2a4165SFrank Mandarino 	/* are we waiting on this codec DAI stream */
648db2a4165SFrank Mandarino 	if (codec_dai->pop_wait == 1) {
649db2a4165SFrank Mandarino 		codec_dai->pop_wait = 0;
650f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
651f0fba2adSLiam Girdwood 			codec_dai->driver->playback.stream_name,
652db2a4165SFrank Mandarino 			SND_SOC_DAPM_STREAM_STOP);
653db2a4165SFrank Mandarino 	}
654f0fba2adSLiam Girdwood 
655db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
656db2a4165SFrank Mandarino }
657db2a4165SFrank Mandarino 
658db2a4165SFrank Mandarino /*
659db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
660db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
661db2a4165SFrank Mandarino  * shutdown.
662db2a4165SFrank Mandarino  */
663db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
664db2a4165SFrank Mandarino {
665db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
666f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
667f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
668f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
669f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
670db2a4165SFrank Mandarino 
671db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
672db2a4165SFrank Mandarino 
67314dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
674f0fba2adSLiam Girdwood 		cpu_dai->playback_active--;
675f0fba2adSLiam Girdwood 		codec_dai->playback_active--;
67614dc5734SJassi Brar 	} else {
677f0fba2adSLiam Girdwood 		cpu_dai->capture_active--;
678f0fba2adSLiam Girdwood 		codec_dai->capture_active--;
679db2a4165SFrank Mandarino 	}
68014dc5734SJassi Brar 
68114dc5734SJassi Brar 	cpu_dai->active--;
68214dc5734SJassi Brar 	codec_dai->active--;
683db2a4165SFrank Mandarino 	codec->active--;
684db2a4165SFrank Mandarino 
6856010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
6866010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
6876010b2daSMark Brown 	 */
6886010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6896010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
6906010b2daSMark Brown 
691f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
692f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
693db2a4165SFrank Mandarino 
694f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
695f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
696db2a4165SFrank Mandarino 
697f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
698f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
699db2a4165SFrank Mandarino 
700f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
701f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
702f0fba2adSLiam Girdwood 	cpu_dai->runtime = NULL;
703db2a4165SFrank Mandarino 
704db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
705db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
706cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
707f0fba2adSLiam Girdwood 		schedule_delayed_work(&rtd->delayed_work,
708f0fba2adSLiam Girdwood 			msecs_to_jiffies(rtd->pmdown_time));
709db2a4165SFrank Mandarino 	} else {
710db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
711f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
712f0fba2adSLiam Girdwood 			codec_dai->driver->capture.stream_name,
7130b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
714db2a4165SFrank Mandarino 	}
715db2a4165SFrank Mandarino 
716db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
717db2a4165SFrank Mandarino 	return 0;
718db2a4165SFrank Mandarino }
719db2a4165SFrank Mandarino 
720db2a4165SFrank Mandarino /*
721db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
722db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
723db2a4165SFrank Mandarino  * it can refer to the runtime info.
724db2a4165SFrank Mandarino  */
725db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
726db2a4165SFrank Mandarino {
727db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
728f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
729f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
730f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
731db2a4165SFrank Mandarino 	int ret = 0;
732db2a4165SFrank Mandarino 
733db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
734cb666e5bSLiam Girdwood 
735f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
736f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
737cb666e5bSLiam Girdwood 		if (ret < 0) {
738cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
739cb666e5bSLiam Girdwood 			goto out;
740cb666e5bSLiam Girdwood 		}
741cb666e5bSLiam Girdwood 	}
742cb666e5bSLiam Girdwood 
743f0fba2adSLiam Girdwood 	if (platform->driver->ops->prepare) {
744f0fba2adSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
745a71a468aSLiam Girdwood 		if (ret < 0) {
746a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
747db2a4165SFrank Mandarino 			goto out;
748db2a4165SFrank Mandarino 		}
749a71a468aSLiam Girdwood 	}
750db2a4165SFrank Mandarino 
751f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->prepare) {
752f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->prepare(substream, codec_dai);
753a71a468aSLiam Girdwood 		if (ret < 0) {
754a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
755db2a4165SFrank Mandarino 			goto out;
756db2a4165SFrank Mandarino 		}
757a71a468aSLiam Girdwood 	}
758db2a4165SFrank Mandarino 
759f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->prepare) {
760f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
761cb666e5bSLiam Girdwood 		if (ret < 0) {
762cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
763cb666e5bSLiam Girdwood 			goto out;
764cb666e5bSLiam Girdwood 		}
765cb666e5bSLiam Girdwood 	}
766db2a4165SFrank Mandarino 
767d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
768d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
769d45f6219SMark Brown 	    codec_dai->pop_wait) {
770cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
771f0fba2adSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
772db2a4165SFrank Mandarino 	}
773db2a4165SFrank Mandarino 
774db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
775f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
776f0fba2adSLiam Girdwood 					  codec_dai->driver->playback.stream_name,
777db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
778db2a4165SFrank Mandarino 	else
779f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
780f0fba2adSLiam Girdwood 					  codec_dai->driver->capture.stream_name,
781db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
7828c6529dbSLiam Girdwood 
7838c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
784db2a4165SFrank Mandarino 
785db2a4165SFrank Mandarino out:
786db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
787db2a4165SFrank Mandarino 	return ret;
788db2a4165SFrank Mandarino }
789db2a4165SFrank Mandarino 
790db2a4165SFrank Mandarino /*
791db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
792db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
793db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
794db2a4165SFrank Mandarino  */
795db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
796db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
797db2a4165SFrank Mandarino {
798db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
799f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
800f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
801f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
802db2a4165SFrank Mandarino 	int ret = 0;
803db2a4165SFrank Mandarino 
804db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
805db2a4165SFrank Mandarino 
806f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
807f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
808cb666e5bSLiam Girdwood 		if (ret < 0) {
809cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
810db2a4165SFrank Mandarino 			goto out;
811db2a4165SFrank Mandarino 		}
812cb666e5bSLiam Girdwood 	}
813db2a4165SFrank Mandarino 
814f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_params) {
815f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
816db2a4165SFrank Mandarino 		if (ret < 0) {
817db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
818cb666e5bSLiam Girdwood 				codec_dai->name);
819cb666e5bSLiam Girdwood 			goto codec_err;
820db2a4165SFrank Mandarino 		}
821db2a4165SFrank Mandarino 	}
822db2a4165SFrank Mandarino 
823f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_params) {
824f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
825db2a4165SFrank Mandarino 		if (ret < 0) {
8263ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
827cb666e5bSLiam Girdwood 				cpu_dai->name);
828db2a4165SFrank Mandarino 			goto interface_err;
829db2a4165SFrank Mandarino 		}
830db2a4165SFrank Mandarino 	}
831db2a4165SFrank Mandarino 
832f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_params) {
833f0fba2adSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
834db2a4165SFrank Mandarino 		if (ret < 0) {
8353ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
836db2a4165SFrank Mandarino 				platform->name);
837db2a4165SFrank Mandarino 			goto platform_err;
838db2a4165SFrank Mandarino 		}
839db2a4165SFrank Mandarino 	}
840db2a4165SFrank Mandarino 
841f0fba2adSLiam Girdwood 	rtd->rate = params_rate(params);
84206f409d7SMark Brown 
843db2a4165SFrank Mandarino out:
844db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
845db2a4165SFrank Mandarino 	return ret;
846db2a4165SFrank Mandarino 
847db2a4165SFrank Mandarino platform_err:
848f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
849f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
850db2a4165SFrank Mandarino 
851db2a4165SFrank Mandarino interface_err:
852f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
853f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
854cb666e5bSLiam Girdwood 
855cb666e5bSLiam Girdwood codec_err:
856f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
857f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
858db2a4165SFrank Mandarino 
859db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
860db2a4165SFrank Mandarino 	return ret;
861db2a4165SFrank Mandarino }
862db2a4165SFrank Mandarino 
863db2a4165SFrank Mandarino /*
864a00f90f9SMark Brown  * Frees resources allocated by hw_params, can be called multiple times
865db2a4165SFrank Mandarino  */
866db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
867db2a4165SFrank Mandarino {
868db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
869f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
870f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
871f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
872f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
873db2a4165SFrank Mandarino 
874db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
875db2a4165SFrank Mandarino 
876db2a4165SFrank Mandarino 	/* apply codec digital mute */
8778c6529dbSLiam Girdwood 	if (!codec->active)
8788c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
879db2a4165SFrank Mandarino 
880db2a4165SFrank Mandarino 	/* free any machine hw params */
881f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
882f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
883db2a4165SFrank Mandarino 
884db2a4165SFrank Mandarino 	/* free any DMA resources */
885f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_free)
886f0fba2adSLiam Girdwood 		platform->driver->ops->hw_free(substream);
887db2a4165SFrank Mandarino 
888a00f90f9SMark Brown 	/* now free hw params for the DAIs  */
889f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
890f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
891db2a4165SFrank Mandarino 
892f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
893f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
894db2a4165SFrank Mandarino 
895db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
896db2a4165SFrank Mandarino 	return 0;
897db2a4165SFrank Mandarino }
898db2a4165SFrank Mandarino 
899db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
900db2a4165SFrank Mandarino {
901db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
902f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
903f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
904f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
905db2a4165SFrank Mandarino 	int ret;
906db2a4165SFrank Mandarino 
907f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->trigger) {
908f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
909db2a4165SFrank Mandarino 		if (ret < 0)
910db2a4165SFrank Mandarino 			return ret;
911db2a4165SFrank Mandarino 	}
912db2a4165SFrank Mandarino 
913f0fba2adSLiam Girdwood 	if (platform->driver->ops->trigger) {
914f0fba2adSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
915db2a4165SFrank Mandarino 		if (ret < 0)
916db2a4165SFrank Mandarino 			return ret;
917db2a4165SFrank Mandarino 	}
918db2a4165SFrank Mandarino 
919f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->trigger) {
920f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
921db2a4165SFrank Mandarino 		if (ret < 0)
922db2a4165SFrank Mandarino 			return ret;
923db2a4165SFrank Mandarino 	}
924db2a4165SFrank Mandarino 	return 0;
925db2a4165SFrank Mandarino }
926db2a4165SFrank Mandarino 
927377b6f62SPeter Ujfalusi /*
928377b6f62SPeter Ujfalusi  * soc level wrapper for pointer callback
929258020d0SPeter Ujfalusi  * If cpu_dai, codec_dai, platform driver has the delay callback, than
930258020d0SPeter Ujfalusi  * the runtime->delay will be updated accordingly.
931377b6f62SPeter Ujfalusi  */
932377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
933377b6f62SPeter Ujfalusi {
934377b6f62SPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
935f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
936f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
937f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
938258020d0SPeter Ujfalusi 	struct snd_pcm_runtime *runtime = substream->runtime;
939377b6f62SPeter Ujfalusi 	snd_pcm_uframes_t offset = 0;
940258020d0SPeter Ujfalusi 	snd_pcm_sframes_t delay = 0;
941377b6f62SPeter Ujfalusi 
942f0fba2adSLiam Girdwood 	if (platform->driver->ops->pointer)
943f0fba2adSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
944377b6f62SPeter Ujfalusi 
945f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->delay)
946f0fba2adSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
947258020d0SPeter Ujfalusi 
948f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->delay)
949f0fba2adSLiam Girdwood 		delay += codec_dai->driver->ops->delay(substream, codec_dai);
950258020d0SPeter Ujfalusi 
951f0fba2adSLiam Girdwood 	if (platform->driver->delay)
952f0fba2adSLiam Girdwood 		delay += platform->driver->delay(substream, codec_dai);
953258020d0SPeter Ujfalusi 
954258020d0SPeter Ujfalusi 	runtime->delay = delay;
955258020d0SPeter Ujfalusi 
956377b6f62SPeter Ujfalusi 	return offset;
957377b6f62SPeter Ujfalusi }
958377b6f62SPeter Ujfalusi 
959db2a4165SFrank Mandarino /* ASoC PCM operations */
960db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
961db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
962db2a4165SFrank Mandarino 	.close		= soc_codec_close,
963db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
964db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
965db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
966db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
967377b6f62SPeter Ujfalusi 	.pointer	= soc_pcm_pointer,
968db2a4165SFrank Mandarino };
969db2a4165SFrank Mandarino 
970db2a4165SFrank Mandarino #ifdef CONFIG_PM
971db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
972416356fcSMark Brown static int soc_suspend(struct device *dev)
973db2a4165SFrank Mandarino {
974416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
975f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
9762eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
977db2a4165SFrank Mandarino 	int i;
978db2a4165SFrank Mandarino 
979e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
980e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
981e3509ff0SDaniel Mack 	 */
982f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
983e3509ff0SDaniel Mack 		return 0;
984e3509ff0SDaniel Mack 
9856ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
9866ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
9876ed25978SAndy Green 	 */
988f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
989f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
990f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
9916ed25978SAndy Green 
9926ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
993f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
9946ed25978SAndy Green 
995a00f90f9SMark Brown 	/* mute any active DACs */
996f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
997f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
998f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
9993efab7dcSMark Brown 
1000f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10013efab7dcSMark Brown 			continue;
10023efab7dcSMark Brown 
1003f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1004f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
1005db2a4165SFrank Mandarino 	}
1006db2a4165SFrank Mandarino 
10074ccab3e7SLiam Girdwood 	/* suspend all pcms */
1008f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1009f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10103efab7dcSMark Brown 			continue;
10113efab7dcSMark Brown 
1012f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
10133efab7dcSMark Brown 	}
10144ccab3e7SLiam Girdwood 
101587506549SMark Brown 	if (card->suspend_pre)
1016416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
1017db2a4165SFrank Mandarino 
1018f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1019f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1020f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
10213efab7dcSMark Brown 
1022f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10233efab7dcSMark Brown 			continue;
10243efab7dcSMark Brown 
1025f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1026f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1027f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
1028f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
1029f0fba2adSLiam Girdwood 			platform->suspended = 1;
1030f0fba2adSLiam Girdwood 		}
1031db2a4165SFrank Mandarino 	}
1032db2a4165SFrank Mandarino 
1033db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
1034f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
10355b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
1036ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1037f0fba2adSLiam Girdwood 	}
1038db2a4165SFrank Mandarino 
1039f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1040f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
10413efab7dcSMark Brown 
1042f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10433efab7dcSMark Brown 			continue;
10443efab7dcSMark Brown 
1045f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1046f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1047db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1048f0fba2adSLiam Girdwood 
1049f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1050f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1051db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1052db2a4165SFrank Mandarino 	}
1053db2a4165SFrank Mandarino 
1054f0fba2adSLiam Girdwood 	/* suspend all CODECs */
10552eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
10561547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
10571547aba9SMark Brown 		 * bias _ON and should not be suspended. */
1058f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
1059ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
10601547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
10611547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1062f0fba2adSLiam Girdwood 				codec->driver->suspend(codec, PMSG_SUSPEND);
1063f0fba2adSLiam Girdwood 				codec->suspended = 1;
10641547aba9SMark Brown 				break;
10651547aba9SMark Brown 			default:
1066f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
10671547aba9SMark Brown 				break;
10681547aba9SMark Brown 			}
10691547aba9SMark Brown 		}
1070f0fba2adSLiam Girdwood 	}
1071db2a4165SFrank Mandarino 
1072f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1073f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
10743efab7dcSMark Brown 
1075f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10763efab7dcSMark Brown 			continue;
10773efab7dcSMark Brown 
1078f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1079f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1080db2a4165SFrank Mandarino 	}
1081db2a4165SFrank Mandarino 
108287506549SMark Brown 	if (card->suspend_post)
1083416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
1084db2a4165SFrank Mandarino 
1085db2a4165SFrank Mandarino 	return 0;
1086db2a4165SFrank Mandarino }
1087db2a4165SFrank Mandarino 
10886ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
10896ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
10906ed25978SAndy Green  */
10916ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
1092db2a4165SFrank Mandarino {
1093f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
1094f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
1095f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
10962eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1097db2a4165SFrank Mandarino 	int i;
1098db2a4165SFrank Mandarino 
10996ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
11006ed25978SAndy Green 	 * so userspace apps are blocked from touching us
11016ed25978SAndy Green 	 */
11026ed25978SAndy Green 
1103f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
11046ed25978SAndy Green 
11059949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
1106f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
11079949788bSMark Brown 
110887506549SMark Brown 	if (card->resume_pre)
110987506549SMark Brown 		card->resume_pre(pdev);
1110db2a4165SFrank Mandarino 
1111f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
1112f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1113f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
11143efab7dcSMark Brown 
1115f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11163efab7dcSMark Brown 			continue;
11173efab7dcSMark Brown 
1118f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1119f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1120db2a4165SFrank Mandarino 	}
1121db2a4165SFrank Mandarino 
11222eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
11231547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
11241547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
11251547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
11261547aba9SMark Brown 		 */
1127f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
1128ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
11291547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
11301547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1131f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
1132f0fba2adSLiam Girdwood 				codec->suspended = 0;
11331547aba9SMark Brown 				break;
11341547aba9SMark Brown 			default:
1135f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
11361547aba9SMark Brown 				break;
11371547aba9SMark Brown 			}
11381547aba9SMark Brown 		}
1139f0fba2adSLiam Girdwood 	}
1140db2a4165SFrank Mandarino 
1141f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1142f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
11433efab7dcSMark Brown 
1144f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11453efab7dcSMark Brown 			continue;
11463efab7dcSMark Brown 
1147f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1148f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1149db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1150f0fba2adSLiam Girdwood 
1151f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1152f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1153db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1154db2a4165SFrank Mandarino 	}
1155db2a4165SFrank Mandarino 
11563ff3f64bSMark Brown 	/* unmute any active DACs */
1157f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1158f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1159f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
11603efab7dcSMark Brown 
1161f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11623efab7dcSMark Brown 			continue;
11633efab7dcSMark Brown 
1164f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1165f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
1166db2a4165SFrank Mandarino 	}
1167db2a4165SFrank Mandarino 
1168f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1169f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1170f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
11713efab7dcSMark Brown 
1172f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11733efab7dcSMark Brown 			continue;
11743efab7dcSMark Brown 
1175f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1176f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1177f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
1178f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
1179f0fba2adSLiam Girdwood 			platform->suspended = 0;
1180f0fba2adSLiam Girdwood 		}
1181db2a4165SFrank Mandarino 	}
1182db2a4165SFrank Mandarino 
118387506549SMark Brown 	if (card->resume_post)
118487506549SMark Brown 		card->resume_post(pdev);
1185db2a4165SFrank Mandarino 
1186f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
11876ed25978SAndy Green 
11886ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
1189f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
11906ed25978SAndy Green }
11916ed25978SAndy Green 
11926ed25978SAndy Green /* powers up audio subsystem after a suspend */
1193416356fcSMark Brown static int soc_resume(struct device *dev)
11946ed25978SAndy Green {
1195416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1196f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1197f0fba2adSLiam Girdwood 	int i;
1198b9dd94a8SPeter Ujfalusi 
119964ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
120064ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
120164ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
120264ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
120364ab9baaSMark Brown 	 */
1204f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1205f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1206f0fba2adSLiam Girdwood 		if (cpu_dai->driver->ac97_control) {
1207f0fba2adSLiam Girdwood 			dev_dbg(dev, "Resuming AC97 immediately\n");
120864ab9baaSMark Brown 			soc_resume_deferred(&card->deferred_resume_work);
120964ab9baaSMark Brown 		} else {
1210f0fba2adSLiam Girdwood 			dev_dbg(dev, "Scheduling resume work\n");
12116308419aSMark Brown 			if (!schedule_work(&card->deferred_resume_work))
1212f0fba2adSLiam Girdwood 				dev_err(dev, "resume work item may be lost\n");
1213f0fba2adSLiam Girdwood 		}
121464ab9baaSMark Brown 	}
12156ed25978SAndy Green 
1216db2a4165SFrank Mandarino 	return 0;
1217db2a4165SFrank Mandarino }
1218db2a4165SFrank Mandarino #else
1219db2a4165SFrank Mandarino #define soc_suspend	NULL
1220db2a4165SFrank Mandarino #define soc_resume	NULL
1221db2a4165SFrank Mandarino #endif
1222db2a4165SFrank Mandarino 
122302a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
122402a06d30SBarry Song };
122502a06d30SBarry Song 
1226f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1227db2a4165SFrank Mandarino {
1228f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1229f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1230fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1231435c5e25SMark Brown 	struct snd_soc_platform *platform;
1232f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
1233db2a4165SFrank Mandarino 
1234f0fba2adSLiam Girdwood 	if (rtd->complete)
1235f0fba2adSLiam Girdwood 		return 1;
1236f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
12376308419aSMark Brown 
1238f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
1239f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
1240f0fba2adSLiam Girdwood 		goto find_codec;
1241435c5e25SMark Brown 	}
1242f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1243f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
1244f0fba2adSLiam Girdwood 		if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1245f0fba2adSLiam Girdwood 
1246f0fba2adSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
1247f0fba2adSLiam Girdwood 				return -ENODEV;
1248f0fba2adSLiam Girdwood 
1249f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
1250f0fba2adSLiam Girdwood 			goto find_codec;
1251f0fba2adSLiam Girdwood 		}
1252f0fba2adSLiam Girdwood 	}
1253f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
1254f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
1255f0fba2adSLiam Girdwood 
1256f0fba2adSLiam Girdwood find_codec:
1257f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
1258f0fba2adSLiam Girdwood 	if (rtd->codec) {
1259f0fba2adSLiam Girdwood 		goto find_platform;
1260c5af3a2eSMark Brown 	}
1261c5af3a2eSMark Brown 
1262f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
1263f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
1264f0fba2adSLiam Girdwood 		if (!strcmp(codec->name, dai_link->codec_name)) {
1265f0fba2adSLiam Girdwood 			rtd->codec = codec;
1266f0fba2adSLiam Girdwood 
1267f0fba2adSLiam Girdwood 			/* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1268f0fba2adSLiam Girdwood 			list_for_each_entry(codec_dai, &dai_list, list) {
1269f0fba2adSLiam Girdwood 				if (codec->dev == codec_dai->dev &&
1270f0fba2adSLiam Girdwood 						!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1271f0fba2adSLiam Girdwood 					rtd->codec_dai = codec_dai;
1272f0fba2adSLiam Girdwood 					goto find_platform;
1273435c5e25SMark Brown 				}
1274f0fba2adSLiam Girdwood 			}
1275f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1276f0fba2adSLiam Girdwood 					dai_link->codec_dai_name);
1277f0fba2adSLiam Girdwood 
1278f0fba2adSLiam Girdwood 			goto find_platform;
1279f0fba2adSLiam Girdwood 		}
1280f0fba2adSLiam Girdwood 	}
1281f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
1282f0fba2adSLiam Girdwood 			dai_link->codec_name);
1283f0fba2adSLiam Girdwood 
1284f0fba2adSLiam Girdwood find_platform:
1285f0fba2adSLiam Girdwood 	/* do we already have the CODEC DAI for this link ? */
1286f0fba2adSLiam Girdwood 	if (rtd->platform) {
1287f0fba2adSLiam Girdwood 		goto out;
1288f0fba2adSLiam Girdwood 	}
1289f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1290f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
1291f0fba2adSLiam Girdwood 		if (!strcmp(platform->name, dai_link->platform_name)) {
1292f0fba2adSLiam Girdwood 			rtd->platform = platform;
1293f0fba2adSLiam Girdwood 			goto out;
1294f0fba2adSLiam Girdwood 		}
1295435c5e25SMark Brown 	}
12966b05eda6SMark Brown 
1297f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
1298f0fba2adSLiam Girdwood 			dai_link->platform_name);
1299f0fba2adSLiam Girdwood 	return 0;
1300f0fba2adSLiam Girdwood 
1301f0fba2adSLiam Girdwood out:
1302f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
1303f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1304f0fba2adSLiam Girdwood 		rtd->complete = 1;
1305f0fba2adSLiam Girdwood 		card->num_rtd++;
1306f0fba2adSLiam Girdwood 	}
1307f0fba2adSLiam Girdwood 	return 1;
13086b05eda6SMark Brown }
13096b05eda6SMark Brown 
1310589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
1311589c3563SJarkko Nikula {
1312589c3563SJarkko Nikula 	int err;
1313589c3563SJarkko Nikula 
1314589c3563SJarkko Nikula 	if (codec->driver->remove) {
1315589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
1316589c3563SJarkko Nikula 		if (err < 0)
1317589c3563SJarkko Nikula 			dev_err(codec->dev,
1318589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
1319589c3563SJarkko Nikula 				codec->name, err);
1320589c3563SJarkko Nikula 	}
1321589c3563SJarkko Nikula 
1322589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
1323589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
1324589c3563SJarkko Nikula 
1325589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
1326589c3563SJarkko Nikula 	codec->probed = 0;
1327589c3563SJarkko Nikula 	list_del(&codec->card_list);
1328589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
1329589c3563SJarkko Nikula }
1330589c3563SJarkko Nikula 
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);
1342589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1343f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
1344f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
134502a06d30SBarry Song 	}
134602a06d30SBarry Song 
1347f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
1348f0fba2adSLiam Girdwood 	if (codec_dai && codec_dai->probed) {
1349f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1350f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1351f0fba2adSLiam Girdwood 			if (err < 0)
1352f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1353f0fba2adSLiam Girdwood 		}
1354f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1355f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1356f0fba2adSLiam Girdwood 	}
1357f0fba2adSLiam Girdwood 
1358f0fba2adSLiam Girdwood 	/* remove the platform */
1359f0fba2adSLiam Girdwood 	if (platform && platform->probed) {
1360f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
1361f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
1362f0fba2adSLiam Girdwood 			if (err < 0)
1363f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1364f0fba2adSLiam Girdwood 		}
1365f0fba2adSLiam Girdwood 		platform->probed = 0;
1366f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
1367f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
1368f0fba2adSLiam Girdwood 	}
1369f0fba2adSLiam Girdwood 
1370f0fba2adSLiam Girdwood 	/* remove the CODEC */
1371589c3563SJarkko Nikula 	if (codec && codec->probed)
1372589c3563SJarkko Nikula 		soc_remove_codec(codec);
1373f0fba2adSLiam Girdwood 
1374f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
1375f0fba2adSLiam Girdwood 	if (cpu_dai && cpu_dai->probed) {
1376f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1377f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1378f0fba2adSLiam Girdwood 			if (err < 0)
1379f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1380f0fba2adSLiam Girdwood 		}
1381f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1382f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1383f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
1384f0fba2adSLiam Girdwood 	}
1385f0fba2adSLiam Girdwood }
1386f0fba2adSLiam Girdwood 
1387ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1388ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1389ead9b919SJarkko Nikula {
1390ead9b919SJarkko Nikula 	int i;
1391ead9b919SJarkko Nikula 
1392ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1393ead9b919SJarkko Nikula 		return;
1394ead9b919SJarkko Nikula 
1395ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1396ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1397ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1398ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1399ead9b919SJarkko Nikula 			break;
1400ead9b919SJarkko Nikula 		}
1401ead9b919SJarkko Nikula 	}
1402ead9b919SJarkko Nikula }
1403ead9b919SJarkko Nikula 
1404589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1405589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1406589c3563SJarkko Nikula {
1407589c3563SJarkko Nikula 	int ret = 0;
1408589c3563SJarkko Nikula 
1409589c3563SJarkko Nikula 	codec->card = card;
1410589c3563SJarkko Nikula 	codec->dapm.card = card;
1411589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1412589c3563SJarkko Nikula 
1413589c3563SJarkko Nikula 	if (codec->driver->probe) {
1414589c3563SJarkko Nikula 		ret = codec->driver->probe(codec);
1415589c3563SJarkko Nikula 		if (ret < 0) {
1416589c3563SJarkko Nikula 			dev_err(codec->dev,
1417589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1418589c3563SJarkko Nikula 				codec->name, ret);
1419589c3563SJarkko Nikula 			return ret;
1420589c3563SJarkko Nikula 		}
1421589c3563SJarkko Nikula 	}
1422589c3563SJarkko Nikula 
1423589c3563SJarkko Nikula 	soc_init_codec_debugfs(codec);
1424589c3563SJarkko Nikula 
1425589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1426f6c2ed5dSHarsha Priya 	if (!try_module_get(codec->dev->driver->owner))
1427f6c2ed5dSHarsha Priya 		return -ENODEV;
1428f6c2ed5dSHarsha Priya 
1429589c3563SJarkko Nikula 	codec->probed = 1;
1430589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
14317be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1432589c3563SJarkko Nikula 
1433589c3563SJarkko Nikula 	return ret;
1434589c3563SJarkko Nikula }
1435589c3563SJarkko Nikula 
1436f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1437f0fba2adSLiam Girdwood 
1438589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1439589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1440589c3563SJarkko Nikula 				   int num, int dailess)
1441589c3563SJarkko Nikula {
1442589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1443589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1444589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1445589c3563SJarkko Nikula 	const char *temp, *name;
1446589c3563SJarkko Nikula 	int ret = 0;
1447589c3563SJarkko Nikula 
1448589c3563SJarkko Nikula 	if (!dailess) {
1449589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1450589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1451589c3563SJarkko Nikula 		name = dai_link->name;
1452589c3563SJarkko Nikula 	} else {
1453589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1454589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1455589c3563SJarkko Nikula 		name = aux_dev->name;
1456589c3563SJarkko Nikula 	}
1457589c3563SJarkko Nikula 
1458589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1459589c3563SJarkko Nikula 	temp = codec->name_prefix;
1460589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1461589c3563SJarkko Nikula 
1462589c3563SJarkko Nikula 	/* do machine specific initialization */
1463589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1464589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1465589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1466589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1467589c3563SJarkko Nikula 	if (ret < 0) {
1468589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1469589c3563SJarkko Nikula 		return ret;
1470589c3563SJarkko Nikula 	}
1471589c3563SJarkko Nikula 	codec->name_prefix = temp;
1472589c3563SJarkko Nikula 
1473589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are instantiated */
1474589c3563SJarkko Nikula 	snd_soc_dapm_new_widgets(&codec->dapm);
1475589c3563SJarkko Nikula 	snd_soc_dapm_sync(&codec->dapm);
1476589c3563SJarkko Nikula 
1477589c3563SJarkko Nikula 	/* register the rtd device */
1478589c3563SJarkko Nikula 	rtd->codec = codec;
1479589c3563SJarkko Nikula 	rtd->card = card;
1480589c3563SJarkko Nikula 	rtd->dev.parent = card->dev;
1481589c3563SJarkko Nikula 	rtd->dev.release = rtd_release;
1482589c3563SJarkko Nikula 	rtd->dev.init_name = name;
1483589c3563SJarkko Nikula 	ret = device_register(&rtd->dev);
1484589c3563SJarkko Nikula 	if (ret < 0) {
1485589c3563SJarkko Nikula 		dev_err(card->dev,
1486589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1487589c3563SJarkko Nikula 		return ret;
1488589c3563SJarkko Nikula 	}
1489589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1490589c3563SJarkko Nikula 
1491589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
1492589c3563SJarkko Nikula 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1493589c3563SJarkko Nikula 	if (ret < 0)
1494589c3563SJarkko Nikula 		dev_err(codec->dev,
1495589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1496589c3563SJarkko Nikula 			ret);
1497589c3563SJarkko Nikula 
1498589c3563SJarkko Nikula 	/* add codec sysfs entries */
1499589c3563SJarkko Nikula 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1500589c3563SJarkko Nikula 	if (ret < 0)
1501589c3563SJarkko Nikula 		dev_err(codec->dev,
1502589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1503589c3563SJarkko Nikula 
1504589c3563SJarkko Nikula 	return 0;
1505589c3563SJarkko Nikula }
1506589c3563SJarkko Nikula 
1507f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1508f0fba2adSLiam Girdwood {
1509f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1510f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1511f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1512f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1513f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1514f0fba2adSLiam Girdwood 	int ret;
1515f0fba2adSLiam Girdwood 
1516f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1517f0fba2adSLiam Girdwood 
1518f0fba2adSLiam Girdwood 	/* config components */
1519f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1520f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1521f0fba2adSLiam Girdwood 	codec_dai->card = card;
1522f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1523f0fba2adSLiam Girdwood 
1524f0fba2adSLiam Girdwood 	/* set default power off timeout */
1525f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1526f0fba2adSLiam Girdwood 
1527f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
1528f0fba2adSLiam Girdwood 	if (!cpu_dai->probed) {
1529f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1530f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1531f0fba2adSLiam Girdwood 			if (ret < 0) {
1532f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1533f0fba2adSLiam Girdwood 						cpu_dai->name);
1534f0fba2adSLiam Girdwood 				return ret;
1535f0fba2adSLiam Girdwood 			}
1536f0fba2adSLiam Girdwood 		}
1537f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1538f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1539f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1540f0fba2adSLiam Girdwood 	}
1541f0fba2adSLiam Girdwood 
1542f0fba2adSLiam Girdwood 	/* probe the CODEC */
1543f0fba2adSLiam Girdwood 	if (!codec->probed) {
1544589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1545589c3563SJarkko Nikula 		if (ret < 0)
1546f0fba2adSLiam Girdwood 			return ret;
1547f0fba2adSLiam Girdwood 	}
1548f0fba2adSLiam Girdwood 
1549f0fba2adSLiam Girdwood 	/* probe the platform */
1550f0fba2adSLiam Girdwood 	if (!platform->probed) {
1551f0fba2adSLiam Girdwood 		if (platform->driver->probe) {
1552f0fba2adSLiam Girdwood 			ret = platform->driver->probe(platform);
1553f0fba2adSLiam Girdwood 			if (ret < 0) {
1554f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe platform %s\n",
1555f0fba2adSLiam Girdwood 						platform->name);
1556f0fba2adSLiam Girdwood 				return ret;
1557f0fba2adSLiam Girdwood 			}
1558f0fba2adSLiam Girdwood 		}
1559f0fba2adSLiam Girdwood 		/* mark platform as probed and add to card platform list */
1560f6c2ed5dSHarsha Priya 
1561f6c2ed5dSHarsha Priya 		if (!try_module_get(platform->dev->driver->owner))
1562f6c2ed5dSHarsha Priya 			return -ENODEV;
1563f6c2ed5dSHarsha Priya 
1564f0fba2adSLiam Girdwood 		platform->probed = 1;
1565f0fba2adSLiam Girdwood 		list_add(&platform->card_list, &card->platform_dev_list);
1566f0fba2adSLiam Girdwood 	}
1567f0fba2adSLiam Girdwood 
1568f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
1569f0fba2adSLiam Girdwood 	if (!codec_dai->probed) {
1570f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1571f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1572f0fba2adSLiam Girdwood 			if (ret < 0) {
1573f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1574f0fba2adSLiam Girdwood 						codec_dai->name);
1575f0fba2adSLiam Girdwood 				return ret;
1576f0fba2adSLiam Girdwood 			}
1577f0fba2adSLiam Girdwood 		}
1578f0fba2adSLiam Girdwood 
1579f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1580f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1581f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1582f0fba2adSLiam Girdwood 	}
1583f0fba2adSLiam Girdwood 
1584f0fba2adSLiam Girdwood 	/* DAPM dai link stream work */
1585f0fba2adSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1586f0fba2adSLiam Girdwood 
1587589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1588589c3563SJarkko Nikula 	if (ret)
1589f0fba2adSLiam Girdwood 		return ret;
1590f0fba2adSLiam Girdwood 
1591f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1592f0fba2adSLiam Girdwood 	if (ret < 0)
1593f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1594f0fba2adSLiam Girdwood 
1595f0fba2adSLiam Girdwood 	/* create the pcm */
1596f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1597f0fba2adSLiam Girdwood 	if (ret < 0) {
1598f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1599f0fba2adSLiam Girdwood 		return ret;
1600f0fba2adSLiam Girdwood 	}
1601f0fba2adSLiam Girdwood 
1602f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1603f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1604f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1605f0fba2adSLiam Girdwood 
1606f0fba2adSLiam Girdwood 	return 0;
1607f0fba2adSLiam Girdwood }
1608f0fba2adSLiam Girdwood 
1609f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1610f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1611f0fba2adSLiam Girdwood {
1612f0fba2adSLiam Girdwood 	int ret;
1613f0fba2adSLiam Girdwood 
1614f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1615f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
16166b05eda6SMark Brown 	 */
1617f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
16180562f788SMika Westerberg 		/*
16190562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
16200562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
16210562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
16220562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
16230562f788SMika Westerberg 		 *
16240562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
16250562f788SMika Westerberg 		 */
16260562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
16270562f788SMika Westerberg 			return 0;
1628f0fba2adSLiam Girdwood 
1629f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1630f0fba2adSLiam Girdwood 		if (ret < 0) {
1631f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1632f0fba2adSLiam Girdwood 			return ret;
1633435c5e25SMark Brown 		}
1634435c5e25SMark Brown 
1635f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1636f0fba2adSLiam Girdwood 	}
1637f0fba2adSLiam Girdwood 	return 0;
1638f0fba2adSLiam Girdwood }
1639435c5e25SMark Brown 
1640f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1641f0fba2adSLiam Girdwood {
1642f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1643f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1644f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1645f0fba2adSLiam Girdwood 	}
1646f0fba2adSLiam Girdwood }
1647f0fba2adSLiam Girdwood #endif
1648435c5e25SMark Brown 
16492eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
16502eea392dSJarkko Nikula {
16512eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
16522eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1653676ad98aSJarkko Nikula 	int ret = -ENODEV;
16542eea392dSJarkko Nikula 
16552eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
16562eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
16572eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
16582eea392dSJarkko Nikula 			if (codec->probed) {
16592eea392dSJarkko Nikula 				dev_err(codec->dev,
16602eea392dSJarkko Nikula 					"asoc: codec already probed");
16612eea392dSJarkko Nikula 				ret = -EBUSY;
16622eea392dSJarkko Nikula 				goto out;
16632eea392dSJarkko Nikula 			}
1664676ad98aSJarkko Nikula 			goto found;
16652eea392dSJarkko Nikula 		}
16662eea392dSJarkko Nikula 	}
1667676ad98aSJarkko Nikula 	/* codec not found */
1668676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1669676ad98aSJarkko Nikula 	goto out;
16702eea392dSJarkko Nikula 
1671676ad98aSJarkko Nikula found:
16722eea392dSJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
16732eea392dSJarkko Nikula 		return -ENODEV;
16742eea392dSJarkko Nikula 
1675589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
16762eea392dSJarkko Nikula 	if (ret < 0)
1677589c3563SJarkko Nikula 		return ret;
16782eea392dSJarkko Nikula 
1679589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
16802eea392dSJarkko Nikula 
16812eea392dSJarkko Nikula out:
16822eea392dSJarkko Nikula 	return ret;
16832eea392dSJarkko Nikula }
16842eea392dSJarkko Nikula 
16852eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
16862eea392dSJarkko Nikula {
16872eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
16882eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
16892eea392dSJarkko Nikula 
16902eea392dSJarkko Nikula 	/* unregister the rtd device */
16912eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1692589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
16932eea392dSJarkko Nikula 		device_unregister(&rtd->dev);
16942eea392dSJarkko Nikula 		rtd->dev_registered = 0;
16952eea392dSJarkko Nikula 	}
16962eea392dSJarkko Nikula 
1697589c3563SJarkko Nikula 	if (codec && codec->probed)
1698589c3563SJarkko Nikula 		soc_remove_codec(codec);
16992eea392dSJarkko Nikula }
17002eea392dSJarkko Nikula 
1701fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1702fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1703fdf0f54dSDimitris Papastamos {
1704fdf0f54dSDimitris Papastamos 	int ret;
1705fdf0f54dSDimitris Papastamos 
1706fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1707fdf0f54dSDimitris Papastamos 		return 0;
1708fdf0f54dSDimitris Papastamos 
1709fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1710fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1711fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1712fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1713fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1714fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1715fdf0f54dSDimitris Papastamos 			ret);
1716fdf0f54dSDimitris Papastamos 		return ret;
1717fdf0f54dSDimitris Papastamos 	}
1718fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1719fdf0f54dSDimitris Papastamos 	return 0;
1720fdf0f54dSDimitris Papastamos }
1721fdf0f54dSDimitris Papastamos 
1722f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1723f0fba2adSLiam Girdwood {
1724f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
1725fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1726fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1727fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
1728f0fba2adSLiam Girdwood 	int ret, i;
172996dd3622SMark Brown 
1730f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1731f0fba2adSLiam Girdwood 
1732f0fba2adSLiam Girdwood 	if (card->instantiated) {
1733f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1734435c5e25SMark Brown 		return;
1735db2a4165SFrank Mandarino 	}
1736db2a4165SFrank Mandarino 
1737f0fba2adSLiam Girdwood 	/* bind DAIs */
1738f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1739f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1740f0fba2adSLiam Girdwood 
1741f0fba2adSLiam Girdwood 	/* bind completed ? */
1742f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1743f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1744f0fba2adSLiam Girdwood 		return;
1745db2a4165SFrank Mandarino 	}
1746db2a4165SFrank Mandarino 
1747fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1748fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1749fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1750fdf0f54dSDimitris Papastamos 			continue;
1751*861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1752*861f2fafSDimitris Papastamos 		compress_type = 0;
1753fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1754fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1755fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1756fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1757fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1758fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1759fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1760fdf0f54dSDimitris Papastamos 					break;
1761fdf0f54dSDimitris Papastamos 			}
1762fdf0f54dSDimitris Papastamos 		}
1763fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1764fdf0f54dSDimitris Papastamos 		if (ret < 0) {
1765fdf0f54dSDimitris Papastamos 			mutex_unlock(&card->mutex);
1766fdf0f54dSDimitris Papastamos 			return;
1767fdf0f54dSDimitris Papastamos 		}
1768fdf0f54dSDimitris Papastamos 	}
1769fdf0f54dSDimitris Papastamos 
1770f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1771f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1772f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1773f0fba2adSLiam Girdwood 	if (ret < 0) {
1774f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1775f0fba2adSLiam Girdwood 			card->name);
1776f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1777f0fba2adSLiam Girdwood 		return;
1778db2a4165SFrank Mandarino 	}
1779f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1780db2a4165SFrank Mandarino 
17811301a964SRandy Dunlap #ifdef CONFIG_PM
17826ed25978SAndy Green 	/* deferred resume work */
17836308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
17841301a964SRandy Dunlap #endif
17856ed25978SAndy Green 
1786f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1787f0fba2adSLiam Girdwood 	if (card->probe) {
1788f0fba2adSLiam Girdwood 		ret = card->probe(pdev);
1789f0fba2adSLiam Girdwood 		if (ret < 0)
1790f0fba2adSLiam Girdwood 			goto card_probe_error;
1791f0fba2adSLiam Girdwood 	}
1792f0fba2adSLiam Girdwood 
1793fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1794f0fba2adSLiam Girdwood 		ret = soc_probe_dai_link(card, i);
1795fe3e78e0SMark Brown 		if (ret < 0) {
1796321de0d0SMark Brown 			pr_err("asoc: failed to instantiate card %s: %d\n",
1797321de0d0SMark Brown 			       card->name, ret);
1798f0fba2adSLiam Girdwood 			goto probe_dai_err;
1799fe3e78e0SMark Brown 		}
1800fe3e78e0SMark Brown 	}
1801fe3e78e0SMark Brown 
18022eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
18032eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
18042eea392dSJarkko Nikula 		if (ret < 0) {
18052eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
18062eea392dSJarkko Nikula 			       card->name, ret);
18072eea392dSJarkko Nikula 			goto probe_aux_dev_err;
18082eea392dSJarkko Nikula 		}
18092eea392dSJarkko Nikula 	}
18102eea392dSJarkko Nikula 
1811f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1812fe3e78e0SMark Brown 		 "%s",  card->name);
1813f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1814f0fba2adSLiam Girdwood 		 "%s", card->name);
1815fe3e78e0SMark Brown 
1816f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1817fe3e78e0SMark Brown 	if (ret < 0) {
1818f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
18196b3ed785SAxel Lin 		goto probe_aux_dev_err;
1820fe3e78e0SMark Brown 	}
1821fe3e78e0SMark Brown 
1822fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1823f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1824f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1825f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1826fe3e78e0SMark Brown 		if (ret < 0) {
1827f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
18286b3ed785SAxel Lin 			while (--i >= 0)
18297d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
18306b3ed785SAxel Lin 			goto probe_aux_dev_err;
1831fe3e78e0SMark Brown 		}
1832fe3e78e0SMark Brown 	}
1833fe3e78e0SMark Brown #endif
1834fe3e78e0SMark Brown 
1835435c5e25SMark Brown 	card->instantiated = 1;
1836f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1837435c5e25SMark Brown 	return;
1838db2a4165SFrank Mandarino 
18392eea392dSJarkko Nikula probe_aux_dev_err:
18402eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18412eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18422eea392dSJarkko Nikula 
1843f0fba2adSLiam Girdwood probe_dai_err:
1844f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1845f0fba2adSLiam Girdwood 		soc_remove_dai_link(card, i);
1846fe3e78e0SMark Brown 
1847f0fba2adSLiam Girdwood card_probe_error:
184887506549SMark Brown 	if (card->remove)
184987506549SMark Brown 		card->remove(pdev);
1850f0fba2adSLiam Girdwood 
1851f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1852f0fba2adSLiam Girdwood 
1853f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1854435c5e25SMark Brown }
1855db2a4165SFrank Mandarino 
1856435c5e25SMark Brown /*
1857421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1858435c5e25SMark Brown  * client_mutex.
1859435c5e25SMark Brown  */
1860435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1861435c5e25SMark Brown {
1862435c5e25SMark Brown 	struct snd_soc_card *card;
1863435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1864435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1865435c5e25SMark Brown }
1866435c5e25SMark Brown 
1867435c5e25SMark Brown /* probes a new socdev */
1868435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1869435c5e25SMark Brown {
1870f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1871435c5e25SMark Brown 	int ret = 0;
1872435c5e25SMark Brown 
1873435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1874435c5e25SMark Brown 	card->dev = &pdev->dev;
1875f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->dai_dev_list);
1876f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->codec_dev_list);
1877f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->platform_dev_list);
187897c866deSJarkko Nikula 	INIT_LIST_HEAD(&card->widgets);
18798ddab3f5SJarkko Nikula 	INIT_LIST_HEAD(&card->paths);
18807be31be8SJarkko Nikula 	INIT_LIST_HEAD(&card->dapm_list);
1881f0fba2adSLiam Girdwood 
1882a6052154SJarkko Nikula 	soc_init_card_debugfs(card);
1883a6052154SJarkko Nikula 
1884435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1885435c5e25SMark Brown 	if (ret != 0) {
1886435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1887db2a4165SFrank Mandarino 		return ret;
1888db2a4165SFrank Mandarino 	}
1889db2a4165SFrank Mandarino 
1890435c5e25SMark Brown 	return 0;
1891435c5e25SMark Brown }
1892435c5e25SMark Brown 
1893db2a4165SFrank Mandarino /* removes a socdev */
1894db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1895db2a4165SFrank Mandarino {
1896f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1897db2a4165SFrank Mandarino 	int i;
1898db2a4165SFrank Mandarino 
1899b2dfa62cSGuennadi Liakhovetski 		if (card->instantiated) {
1900965ac42cSLiam Girdwood 
1901f0fba2adSLiam Girdwood 		/* make sure any delayed work runs */
1902f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++) {
1903f0fba2adSLiam Girdwood 			struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
19045b84ba26STejun Heo 			flush_delayed_work_sync(&rtd->delayed_work);
1905db2a4165SFrank Mandarino 		}
1906db2a4165SFrank Mandarino 
19072eea392dSJarkko Nikula 		/* remove auxiliary devices */
19082eea392dSJarkko Nikula 		for (i = 0; i < card->num_aux_devs; i++)
19092eea392dSJarkko Nikula 			soc_remove_aux_dev(card, i);
19102eea392dSJarkko Nikula 
1911f0fba2adSLiam Girdwood 		/* remove and free each DAI */
1912f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++)
1913f0fba2adSLiam Girdwood 			soc_remove_dai_link(card, i);
1914f0fba2adSLiam Girdwood 
1915a6052154SJarkko Nikula 		soc_cleanup_card_debugfs(card);
1916a6052154SJarkko Nikula 
1917f0fba2adSLiam Girdwood 		/* remove the card */
191887506549SMark Brown 		if (card->remove)
191987506549SMark Brown 			card->remove(pdev);
1920f0fba2adSLiam Girdwood 
1921f0fba2adSLiam Girdwood 		kfree(card->rtd);
1922f0fba2adSLiam Girdwood 		snd_card_free(card->snd_card);
1923b2dfa62cSGuennadi Liakhovetski 	}
1924c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1925db2a4165SFrank Mandarino 	return 0;
1926db2a4165SFrank Mandarino }
1927db2a4165SFrank Mandarino 
1928416356fcSMark Brown static int soc_poweroff(struct device *dev)
192951737470SMark Brown {
1930416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1931f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1932f0fba2adSLiam Girdwood 	int i;
193351737470SMark Brown 
193451737470SMark Brown 	if (!card->instantiated)
1935416356fcSMark Brown 		return 0;
193651737470SMark Brown 
193751737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
193851737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1939f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1940f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
19415b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1942f0fba2adSLiam Girdwood 	}
194351737470SMark Brown 
1944f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1945416356fcSMark Brown 
1946416356fcSMark Brown 	return 0;
194751737470SMark Brown }
194851737470SMark Brown 
194947145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1950416356fcSMark Brown 	.suspend = soc_suspend,
1951416356fcSMark Brown 	.resume = soc_resume,
1952416356fcSMark Brown 	.poweroff = soc_poweroff,
1953416356fcSMark Brown };
1954416356fcSMark Brown 
1955db2a4165SFrank Mandarino /* ASoC platform driver */
1956db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1957db2a4165SFrank Mandarino 	.driver		= {
1958db2a4165SFrank Mandarino 		.name		= "soc-audio",
19598b45a209SKay Sievers 		.owner		= THIS_MODULE,
1960416356fcSMark Brown 		.pm		= &soc_pm_ops,
1961db2a4165SFrank Mandarino 	},
1962db2a4165SFrank Mandarino 	.probe		= soc_probe,
1963db2a4165SFrank Mandarino 	.remove		= soc_remove,
1964db2a4165SFrank Mandarino };
1965db2a4165SFrank Mandarino 
1966db2a4165SFrank Mandarino /* create a new pcm */
1967f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1968db2a4165SFrank Mandarino {
1969f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1970f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1971f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1972f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1973db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1974db2a4165SFrank Mandarino 	char new_name[64];
1975db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1976db2a4165SFrank Mandarino 
1977db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
197840ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
1979f0fba2adSLiam Girdwood 			rtd->dai_link->stream_name, codec_dai->name, num);
1980db2a4165SFrank Mandarino 
1981f0fba2adSLiam Girdwood 	if (codec_dai->driver->playback.channels_min)
1982db2a4165SFrank Mandarino 		playback = 1;
1983f0fba2adSLiam Girdwood 	if (codec_dai->driver->capture.channels_min)
1984db2a4165SFrank Mandarino 		capture = 1;
1985db2a4165SFrank Mandarino 
1986f0fba2adSLiam Girdwood 	dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1987f0fba2adSLiam Girdwood 	ret = snd_pcm_new(rtd->card->snd_card, new_name,
1988f0fba2adSLiam Girdwood 			num, playback, capture, &pcm);
1989db2a4165SFrank Mandarino 	if (ret < 0) {
1990f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1991db2a4165SFrank Mandarino 		return ret;
1992db2a4165SFrank Mandarino 	}
1993db2a4165SFrank Mandarino 
1994f0fba2adSLiam Girdwood 	rtd->pcm = pcm;
1995db2a4165SFrank Mandarino 	pcm->private_data = rtd;
1996f0fba2adSLiam Girdwood 	soc_pcm_ops.mmap = platform->driver->ops->mmap;
1997f0fba2adSLiam Girdwood 	soc_pcm_ops.pointer = platform->driver->ops->pointer;
1998f0fba2adSLiam Girdwood 	soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1999f0fba2adSLiam Girdwood 	soc_pcm_ops.copy = platform->driver->ops->copy;
2000f0fba2adSLiam Girdwood 	soc_pcm_ops.silence = platform->driver->ops->silence;
2001f0fba2adSLiam Girdwood 	soc_pcm_ops.ack = platform->driver->ops->ack;
2002f0fba2adSLiam Girdwood 	soc_pcm_ops.page = platform->driver->ops->page;
2003db2a4165SFrank Mandarino 
2004db2a4165SFrank Mandarino 	if (playback)
2005db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2006db2a4165SFrank Mandarino 
2007db2a4165SFrank Mandarino 	if (capture)
2008db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2009db2a4165SFrank Mandarino 
2010f0fba2adSLiam Girdwood 	ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
2011db2a4165SFrank Mandarino 	if (ret < 0) {
2012db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
2013db2a4165SFrank Mandarino 		return ret;
2014db2a4165SFrank Mandarino 	}
2015db2a4165SFrank Mandarino 
2016f0fba2adSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
2017db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2018db2a4165SFrank Mandarino 		cpu_dai->name);
2019db2a4165SFrank Mandarino 	return ret;
2020db2a4165SFrank Mandarino }
2021db2a4165SFrank Mandarino 
2022096e49d5SMark Brown /**
2023096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
2024096e49d5SMark Brown  *
2025096e49d5SMark Brown  * @codec: CODEC to query.
2026096e49d5SMark Brown  * @reg: Register to query.
2027096e49d5SMark Brown  *
2028096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
2029096e49d5SMark Brown  */
2030096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
2031096e49d5SMark Brown {
2032f0fba2adSLiam Girdwood 	if (codec->driver->volatile_register)
2033f0fba2adSLiam Girdwood 		return codec->driver->volatile_register(reg);
2034096e49d5SMark Brown 	else
2035096e49d5SMark Brown 		return 0;
2036096e49d5SMark Brown }
2037096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2038096e49d5SMark Brown 
2039db2a4165SFrank Mandarino /**
2040db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2041db2a4165SFrank Mandarino  * @codec: audio codec
2042db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2043db2a4165SFrank Mandarino  * @num: AC97 codec number
2044db2a4165SFrank Mandarino  *
2045db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2046db2a4165SFrank Mandarino  */
2047db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2048db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2049db2a4165SFrank Mandarino {
2050db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2051db2a4165SFrank Mandarino 
2052db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2053db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2054db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2055db2a4165SFrank Mandarino 		return -ENOMEM;
2056db2a4165SFrank Mandarino 	}
2057db2a4165SFrank Mandarino 
2058db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2059db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2060db2a4165SFrank Mandarino 		kfree(codec->ac97);
2061db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2062db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2063db2a4165SFrank Mandarino 		return -ENOMEM;
2064db2a4165SFrank Mandarino 	}
2065db2a4165SFrank Mandarino 
2066db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2067db2a4165SFrank Mandarino 	codec->ac97->num = num;
20680562f788SMika Westerberg 
20690562f788SMika Westerberg 	/*
20700562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
20710562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
20720562f788SMika Westerberg 	 */
20730562f788SMika Westerberg 	codec->ac97_created = 1;
20740562f788SMika Westerberg 
2075db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2076db2a4165SFrank Mandarino 	return 0;
2077db2a4165SFrank Mandarino }
2078db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2079db2a4165SFrank Mandarino 
2080db2a4165SFrank Mandarino /**
2081db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2082db2a4165SFrank Mandarino  * @codec: audio codec
2083db2a4165SFrank Mandarino  *
2084db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2085db2a4165SFrank Mandarino  */
2086db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2087db2a4165SFrank Mandarino {
2088db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2089f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2090f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2091f0fba2adSLiam Girdwood #endif
2092db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2093db2a4165SFrank Mandarino 	kfree(codec->ac97);
2094db2a4165SFrank Mandarino 	codec->ac97 = NULL;
20950562f788SMika Westerberg 	codec->ac97_created = 0;
2096db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2097db2a4165SFrank Mandarino }
2098db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2099db2a4165SFrank Mandarino 
2100c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2101c3753707SMark Brown {
2102c3753707SMark Brown 	unsigned int ret;
2103c3753707SMark Brown 
2104c3acec26SMark Brown 	ret = codec->read(codec, reg);
2105c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2106a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2107c3753707SMark Brown 
2108c3753707SMark Brown 	return ret;
2109c3753707SMark Brown }
2110c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2111c3753707SMark Brown 
2112c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2113c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2114c3753707SMark Brown {
2115c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2116a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2117c3acec26SMark Brown 	return codec->write(codec, reg, val);
2118c3753707SMark Brown }
2119c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2120c3753707SMark Brown 
2121db2a4165SFrank Mandarino /**
2122db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2123db2a4165SFrank Mandarino  * @codec: audio codec
2124db2a4165SFrank Mandarino  * @reg: codec register
2125db2a4165SFrank Mandarino  * @mask: register mask
2126db2a4165SFrank Mandarino  * @value: new value
2127db2a4165SFrank Mandarino  *
2128db2a4165SFrank Mandarino  * Writes new register value.
2129db2a4165SFrank Mandarino  *
2130180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
2131db2a4165SFrank Mandarino  */
2132db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
213346f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2134db2a4165SFrank Mandarino {
2135db2a4165SFrank Mandarino 	int change;
213646f5822fSDaniel Ribeiro 	unsigned int old, new;
2137180c329dSTimur Tabi 	int ret;
2138db2a4165SFrank Mandarino 
2139180c329dSTimur Tabi 	ret = snd_soc_read(codec, reg);
2140180c329dSTimur Tabi 	if (ret < 0)
2141180c329dSTimur Tabi 		return ret;
2142180c329dSTimur Tabi 
2143180c329dSTimur Tabi 	old = ret;
2144db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2145db2a4165SFrank Mandarino 	change = old != new;
2146180c329dSTimur Tabi 	if (change) {
2147180c329dSTimur Tabi 		ret = snd_soc_write(codec, reg, new);
2148180c329dSTimur Tabi 		if (ret < 0)
2149180c329dSTimur Tabi 			return ret;
2150180c329dSTimur Tabi 	}
2151db2a4165SFrank Mandarino 
2152db2a4165SFrank Mandarino 	return change;
2153db2a4165SFrank Mandarino }
2154db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2155db2a4165SFrank Mandarino 
2156db2a4165SFrank Mandarino /**
21576c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
21586c508c62SEero Nurkkala  * @codec: audio codec
21596c508c62SEero Nurkkala  * @reg: codec register
21606c508c62SEero Nurkkala  * @mask: register mask
21616c508c62SEero Nurkkala  * @value: new value
21626c508c62SEero Nurkkala  *
21636c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
21646c508c62SEero Nurkkala  *
21656c508c62SEero Nurkkala  * Returns 1 for change else 0.
21666c508c62SEero Nurkkala  */
2167dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
21686c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
21696c508c62SEero Nurkkala 			       unsigned int value)
21706c508c62SEero Nurkkala {
21716c508c62SEero Nurkkala 	int change;
21726c508c62SEero Nurkkala 
21736c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
21746c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
21756c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
21766c508c62SEero Nurkkala 
21776c508c62SEero Nurkkala 	return change;
21786c508c62SEero Nurkkala }
2179dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
21806c508c62SEero Nurkkala 
21816c508c62SEero Nurkkala /**
2182db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2183db2a4165SFrank Mandarino  * @codec: audio codec
2184db2a4165SFrank Mandarino  * @reg: codec register
2185db2a4165SFrank Mandarino  * @mask: register mask
2186db2a4165SFrank Mandarino  * @value: new value
2187db2a4165SFrank Mandarino  *
2188db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2189db2a4165SFrank Mandarino  * different from the old value.
2190db2a4165SFrank Mandarino  *
2191db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2192db2a4165SFrank Mandarino  */
2193db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
219446f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2195db2a4165SFrank Mandarino {
2196db2a4165SFrank Mandarino 	int change;
219746f5822fSDaniel Ribeiro 	unsigned int old, new;
2198db2a4165SFrank Mandarino 
2199db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2200db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2201db2a4165SFrank Mandarino 	change = old != new;
2202db2a4165SFrank Mandarino 
2203db2a4165SFrank Mandarino 	return change;
2204db2a4165SFrank Mandarino }
2205db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2206db2a4165SFrank Mandarino 
2207db2a4165SFrank Mandarino /**
2208db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2209db2a4165SFrank Mandarino  * @substream: the pcm substream
2210db2a4165SFrank Mandarino  * @hw: the hardware parameters
2211db2a4165SFrank Mandarino  *
2212db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2213db2a4165SFrank Mandarino  */
2214db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2215db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2216db2a4165SFrank Mandarino {
2217db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2218db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2219db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2220db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2221db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2222db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2223db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2224db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2225db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2226db2a4165SFrank Mandarino 	return 0;
2227db2a4165SFrank Mandarino }
2228db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2229db2a4165SFrank Mandarino 
2230db2a4165SFrank Mandarino /**
2231db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2232db2a4165SFrank Mandarino  * @_template: control template
2233db2a4165SFrank Mandarino  * @data: control private data
2234ac11a2b3SMark Brown  * @long_name: control long name
2235db2a4165SFrank Mandarino  *
2236db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2237db2a4165SFrank Mandarino  *
2238db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2239db2a4165SFrank Mandarino  */
2240db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2241db2a4165SFrank Mandarino 	void *data, char *long_name)
2242db2a4165SFrank Mandarino {
2243db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2244db2a4165SFrank Mandarino 
2245db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2246db2a4165SFrank Mandarino 	if (long_name)
2247db2a4165SFrank Mandarino 		template.name = long_name;
2248db2a4165SFrank Mandarino 	template.index = 0;
2249db2a4165SFrank Mandarino 
2250db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
2251db2a4165SFrank Mandarino }
2252db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2253db2a4165SFrank Mandarino 
2254db2a4165SFrank Mandarino /**
22553e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
22563e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
22573e8e1952SIan Molton  * duplicating this code.
22583e8e1952SIan Molton  *
22593e8e1952SIan Molton  * @codec: codec to add controls to
22603e8e1952SIan Molton  * @controls: array of controls to add
22613e8e1952SIan Molton  * @num_controls: number of elements in the array
22623e8e1952SIan Molton  *
22633e8e1952SIan Molton  * Return 0 for success, else error.
22643e8e1952SIan Molton  */
22653e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
22663e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
22673e8e1952SIan Molton {
2268f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2269ead9b919SJarkko Nikula 	char prefixed_name[44], *name;
22703e8e1952SIan Molton 	int err, i;
22713e8e1952SIan Molton 
22723e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
22733e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
2274ead9b919SJarkko Nikula 		if (codec->name_prefix) {
2275ead9b919SJarkko Nikula 			snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2276ead9b919SJarkko Nikula 				 codec->name_prefix, control->name);
2277ead9b919SJarkko Nikula 			name = prefixed_name;
2278ead9b919SJarkko Nikula 		} else {
2279ead9b919SJarkko Nikula 			name = control->name;
2280ead9b919SJarkko Nikula 		}
2281ead9b919SJarkko Nikula 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
22823e8e1952SIan Molton 		if (err < 0) {
2283082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2284ead9b919SJarkko Nikula 				codec->name, name, err);
22853e8e1952SIan Molton 			return err;
22863e8e1952SIan Molton 		}
22873e8e1952SIan Molton 	}
22883e8e1952SIan Molton 
22893e8e1952SIan Molton 	return 0;
22903e8e1952SIan Molton }
22913e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
22923e8e1952SIan Molton 
22933e8e1952SIan Molton /**
2294db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2295db2a4165SFrank Mandarino  * @kcontrol: mixer control
2296db2a4165SFrank Mandarino  * @uinfo: control element information
2297db2a4165SFrank Mandarino  *
2298db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2299db2a4165SFrank Mandarino  * mixer control.
2300db2a4165SFrank Mandarino  *
2301db2a4165SFrank Mandarino  * Returns 0 for success.
2302db2a4165SFrank Mandarino  */
2303db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2304db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2305db2a4165SFrank Mandarino {
2306db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2307db2a4165SFrank Mandarino 
2308db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2309db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2310f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2311db2a4165SFrank Mandarino 
2312f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2313f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2314db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2315db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2316db2a4165SFrank Mandarino 	return 0;
2317db2a4165SFrank Mandarino }
2318db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2319db2a4165SFrank Mandarino 
2320db2a4165SFrank Mandarino /**
2321db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2322db2a4165SFrank Mandarino  * @kcontrol: mixer control
2323ac11a2b3SMark Brown  * @ucontrol: control element information
2324db2a4165SFrank Mandarino  *
2325db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2326db2a4165SFrank Mandarino  *
2327db2a4165SFrank Mandarino  * Returns 0 for success.
2328db2a4165SFrank Mandarino  */
2329db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2330db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2331db2a4165SFrank Mandarino {
2332db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2333db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
233446f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2335db2a4165SFrank Mandarino 
2336f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2337db2a4165SFrank Mandarino 		;
2338db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
23393ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
23403ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2341db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2342db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2343db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2344db2a4165SFrank Mandarino 
2345db2a4165SFrank Mandarino 	return 0;
2346db2a4165SFrank Mandarino }
2347db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2348db2a4165SFrank Mandarino 
2349db2a4165SFrank Mandarino /**
2350db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2351db2a4165SFrank Mandarino  * @kcontrol: mixer control
2352ac11a2b3SMark Brown  * @ucontrol: control element information
2353db2a4165SFrank Mandarino  *
2354db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2355db2a4165SFrank Mandarino  *
2356db2a4165SFrank Mandarino  * Returns 0 for success.
2357db2a4165SFrank Mandarino  */
2358db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2359db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2360db2a4165SFrank Mandarino {
2361db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2362db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
236346f5822fSDaniel Ribeiro 	unsigned int val;
236446f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2365db2a4165SFrank Mandarino 
2366f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2367db2a4165SFrank Mandarino 		;
2368f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2369db2a4165SFrank Mandarino 		return -EINVAL;
2370db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2371db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2372db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2373f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2374db2a4165SFrank Mandarino 			return -EINVAL;
2375db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2376db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2377db2a4165SFrank Mandarino 	}
2378db2a4165SFrank Mandarino 
23796c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2380db2a4165SFrank Mandarino }
2381db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2382db2a4165SFrank Mandarino 
2383db2a4165SFrank Mandarino /**
23842e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
23852e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23862e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23872e72f8e3SPeter Ujfalusi  *
23882e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
23892e72f8e3SPeter Ujfalusi  *
23902e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23912e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23922e72f8e3SPeter Ujfalusi  *
23932e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23942e72f8e3SPeter Ujfalusi  */
23952e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
23962e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23972e72f8e3SPeter Ujfalusi {
23982e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
239974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
240046f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
24012e72f8e3SPeter Ujfalusi 
24022e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
24032e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
24042e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
24052e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
24062e72f8e3SPeter Ujfalusi 			break;
24072e72f8e3SPeter Ujfalusi 	}
24082e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
24092e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24102e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
24112e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
24122e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
24132e72f8e3SPeter Ujfalusi 				break;
24142e72f8e3SPeter Ujfalusi 		}
24152e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
24162e72f8e3SPeter Ujfalusi 	}
24172e72f8e3SPeter Ujfalusi 
24182e72f8e3SPeter Ujfalusi 	return 0;
24192e72f8e3SPeter Ujfalusi }
24202e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
24212e72f8e3SPeter Ujfalusi 
24222e72f8e3SPeter Ujfalusi /**
24232e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
24242e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
24252e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
24262e72f8e3SPeter Ujfalusi  *
24272e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
24282e72f8e3SPeter Ujfalusi  *
24292e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
24302e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
24312e72f8e3SPeter Ujfalusi  *
24322e72f8e3SPeter Ujfalusi  * Returns 0 for success.
24332e72f8e3SPeter Ujfalusi  */
24342e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
24352e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
24362e72f8e3SPeter Ujfalusi {
24372e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
243874155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
243946f5822fSDaniel Ribeiro 	unsigned int val;
244046f5822fSDaniel Ribeiro 	unsigned int mask;
24412e72f8e3SPeter Ujfalusi 
24422e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
24432e72f8e3SPeter Ujfalusi 		return -EINVAL;
24442e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
24452e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
24462e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24472e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
24482e72f8e3SPeter Ujfalusi 			return -EINVAL;
24492e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
24502e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
24512e72f8e3SPeter Ujfalusi 	}
24522e72f8e3SPeter Ujfalusi 
24536c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
24542e72f8e3SPeter Ujfalusi }
24552e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
24562e72f8e3SPeter Ujfalusi 
24572e72f8e3SPeter Ujfalusi /**
2458db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2459db2a4165SFrank Mandarino  * @kcontrol: mixer control
2460db2a4165SFrank Mandarino  * @uinfo: control element information
2461db2a4165SFrank Mandarino  *
2462db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2463db2a4165SFrank Mandarino  * single mixer.
2464db2a4165SFrank Mandarino  *
2465db2a4165SFrank Mandarino  * Returns 0 for success.
2466db2a4165SFrank Mandarino  */
2467db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2468db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2469db2a4165SFrank Mandarino {
2470db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2471db2a4165SFrank Mandarino 
2472db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2473db2a4165SFrank Mandarino 	uinfo->count = 1;
2474f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2475db2a4165SFrank Mandarino 
2476f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2477f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2478db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2479db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2480db2a4165SFrank Mandarino 	return 0;
2481db2a4165SFrank Mandarino }
2482db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2483db2a4165SFrank Mandarino 
2484db2a4165SFrank Mandarino /**
2485db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2486db2a4165SFrank Mandarino  * @kcontrol: mixer control
2487db2a4165SFrank Mandarino  * @uinfo: control element information
2488db2a4165SFrank Mandarino  *
2489db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2490db2a4165SFrank Mandarino  *
2491db2a4165SFrank Mandarino  * Returns 0 for success.
2492db2a4165SFrank Mandarino  */
2493db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2494db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2495db2a4165SFrank Mandarino {
2496a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2497db2a4165SFrank Mandarino 
2498fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2499a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2500a7a4ac86SPhilipp Zabel 	else
2501a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2502a7a4ac86SPhilipp Zabel 
2503db2a4165SFrank Mandarino 	uinfo->count = 1;
2504db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2505a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2506db2a4165SFrank Mandarino 	return 0;
2507db2a4165SFrank Mandarino }
2508db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2509db2a4165SFrank Mandarino 
2510db2a4165SFrank Mandarino /**
2511db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2512db2a4165SFrank Mandarino  * @kcontrol: mixer control
2513db2a4165SFrank Mandarino  * @uinfo: control element information
2514db2a4165SFrank Mandarino  *
2515db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2516db2a4165SFrank Mandarino  *
2517db2a4165SFrank Mandarino  * Returns 0 for success.
2518db2a4165SFrank Mandarino  */
2519db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2520db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2521db2a4165SFrank Mandarino {
25224eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25234eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2524d11bb4a9SPeter Ujfalusi 	int platform_max;
2525762b8df7SMark Brown 	unsigned int shift = mc->shift;
2526815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2527db2a4165SFrank Mandarino 
2528d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2529d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2530d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2531d11bb4a9SPeter Ujfalusi 
2532d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2533a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2534a7a4ac86SPhilipp Zabel 	else
2535a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2536a7a4ac86SPhilipp Zabel 
2537db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2538db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2539d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2540db2a4165SFrank Mandarino 	return 0;
2541db2a4165SFrank Mandarino }
2542db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2543db2a4165SFrank Mandarino 
2544db2a4165SFrank Mandarino /**
2545db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2546db2a4165SFrank Mandarino  * @kcontrol: mixer control
2547ac11a2b3SMark Brown  * @ucontrol: control element information
2548db2a4165SFrank Mandarino  *
2549db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2550db2a4165SFrank Mandarino  *
2551db2a4165SFrank Mandarino  * Returns 0 for success.
2552db2a4165SFrank Mandarino  */
2553db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2554db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2555db2a4165SFrank Mandarino {
25564eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25574eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2558db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2559815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2560815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2561815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25624eaa9819SJon Smirl 	int max = mc->max;
2563815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2564815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2565db2a4165SFrank Mandarino 
2566db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2567db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2568db2a4165SFrank Mandarino 	if (shift != rshift)
2569db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2570db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2571db2a4165SFrank Mandarino 	if (invert) {
2572db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2573a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2574db2a4165SFrank Mandarino 		if (shift != rshift)
2575db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2576a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2577db2a4165SFrank Mandarino 	}
2578db2a4165SFrank Mandarino 
2579db2a4165SFrank Mandarino 	return 0;
2580db2a4165SFrank Mandarino }
2581db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2582db2a4165SFrank Mandarino 
2583db2a4165SFrank Mandarino /**
2584db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2585db2a4165SFrank Mandarino  * @kcontrol: mixer control
2586ac11a2b3SMark Brown  * @ucontrol: control element information
2587db2a4165SFrank Mandarino  *
2588db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2589db2a4165SFrank Mandarino  *
2590db2a4165SFrank Mandarino  * Returns 0 for success.
2591db2a4165SFrank Mandarino  */
2592db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2593db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2594db2a4165SFrank Mandarino {
25954eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25964eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2597db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2598815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2599815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2600815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
26014eaa9819SJon Smirl 	int max = mc->max;
2602815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2603815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
260446f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2605db2a4165SFrank Mandarino 
2606db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2607db2a4165SFrank Mandarino 	if (invert)
2608a7a4ac86SPhilipp Zabel 		val = max - val;
2609db2a4165SFrank Mandarino 	val_mask = mask << shift;
2610db2a4165SFrank Mandarino 	val = val << shift;
2611db2a4165SFrank Mandarino 	if (shift != rshift) {
2612db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2613db2a4165SFrank Mandarino 		if (invert)
2614a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2615db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2616db2a4165SFrank Mandarino 		val |= val2 << rshift;
2617db2a4165SFrank Mandarino 	}
26186c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2619db2a4165SFrank Mandarino }
2620db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2621db2a4165SFrank Mandarino 
2622db2a4165SFrank Mandarino /**
2623db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2624db2a4165SFrank Mandarino  * @kcontrol: mixer control
2625db2a4165SFrank Mandarino  * @uinfo: control element information
2626db2a4165SFrank Mandarino  *
2627db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2628db2a4165SFrank Mandarino  * spans 2 codec registers.
2629db2a4165SFrank Mandarino  *
2630db2a4165SFrank Mandarino  * Returns 0 for success.
2631db2a4165SFrank Mandarino  */
2632db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2633db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2634db2a4165SFrank Mandarino {
26354eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26364eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2637d11bb4a9SPeter Ujfalusi 	int platform_max;
2638db2a4165SFrank Mandarino 
2639d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2640d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2641d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2642d11bb4a9SPeter Ujfalusi 
2643d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2644a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2645a7a4ac86SPhilipp Zabel 	else
2646a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2647a7a4ac86SPhilipp Zabel 
2648db2a4165SFrank Mandarino 	uinfo->count = 2;
2649db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2650d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2651db2a4165SFrank Mandarino 	return 0;
2652db2a4165SFrank Mandarino }
2653db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2654db2a4165SFrank Mandarino 
2655db2a4165SFrank Mandarino /**
2656db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2657db2a4165SFrank Mandarino  * @kcontrol: mixer control
2658ac11a2b3SMark Brown  * @ucontrol: control element information
2659db2a4165SFrank Mandarino  *
2660db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2661db2a4165SFrank Mandarino  *
2662db2a4165SFrank Mandarino  * Returns 0 for success.
2663db2a4165SFrank Mandarino  */
2664db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2665db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2666db2a4165SFrank Mandarino {
26674eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26684eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2669db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2670815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2671815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2672815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
26734eaa9819SJon Smirl 	int max = mc->max;
2674815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2675815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2676db2a4165SFrank Mandarino 
2677db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2678db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2679db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2680db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2681db2a4165SFrank Mandarino 	if (invert) {
2682db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2683a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2684db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2685a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2686db2a4165SFrank Mandarino 	}
2687db2a4165SFrank Mandarino 
2688db2a4165SFrank Mandarino 	return 0;
2689db2a4165SFrank Mandarino }
2690db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2691db2a4165SFrank Mandarino 
2692db2a4165SFrank Mandarino /**
2693db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2694db2a4165SFrank Mandarino  * @kcontrol: mixer control
2695ac11a2b3SMark Brown  * @ucontrol: control element information
2696db2a4165SFrank Mandarino  *
2697db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2698db2a4165SFrank Mandarino  *
2699db2a4165SFrank Mandarino  * Returns 0 for success.
2700db2a4165SFrank Mandarino  */
2701db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2702db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2703db2a4165SFrank Mandarino {
27044eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27054eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2706db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2707815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2708815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2709815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
27104eaa9819SJon Smirl 	int max = mc->max;
2711815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2712815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2713db2a4165SFrank Mandarino 	int err;
271446f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2715db2a4165SFrank Mandarino 
2716db2a4165SFrank Mandarino 	val_mask = mask << shift;
2717db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2718db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2719db2a4165SFrank Mandarino 
2720db2a4165SFrank Mandarino 	if (invert) {
2721a7a4ac86SPhilipp Zabel 		val = max - val;
2722a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2723db2a4165SFrank Mandarino 	}
2724db2a4165SFrank Mandarino 
2725db2a4165SFrank Mandarino 	val = val << shift;
2726db2a4165SFrank Mandarino 	val2 = val2 << shift;
2727db2a4165SFrank Mandarino 
27286c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
27293ff3f64bSMark Brown 	if (err < 0)
2730db2a4165SFrank Mandarino 		return err;
2731db2a4165SFrank Mandarino 
27326c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2733db2a4165SFrank Mandarino 	return err;
2734db2a4165SFrank Mandarino }
2735db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2736db2a4165SFrank Mandarino 
2737e13ac2e9SMark Brown /**
2738e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2739e13ac2e9SMark Brown  * @kcontrol: mixer control
2740e13ac2e9SMark Brown  * @uinfo: control element information
2741e13ac2e9SMark Brown  *
2742e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2743e13ac2e9SMark Brown  *
2744e13ac2e9SMark Brown  * Returns 0 for success.
2745e13ac2e9SMark Brown  */
2746e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2747e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2748e13ac2e9SMark Brown {
27494eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27504eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2751d11bb4a9SPeter Ujfalusi 	int platform_max;
27524eaa9819SJon Smirl 	int min = mc->min;
2753e13ac2e9SMark Brown 
2754d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2755d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2756d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2757d11bb4a9SPeter Ujfalusi 
2758e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2759e13ac2e9SMark Brown 	uinfo->count = 2;
2760e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2761d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2762e13ac2e9SMark Brown 	return 0;
2763e13ac2e9SMark Brown }
2764e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2765e13ac2e9SMark Brown 
2766e13ac2e9SMark Brown /**
2767e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2768e13ac2e9SMark Brown  * @kcontrol: mixer control
2769ac11a2b3SMark Brown  * @ucontrol: control element information
2770e13ac2e9SMark Brown  *
2771e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2772e13ac2e9SMark Brown  *
2773e13ac2e9SMark Brown  * Returns 0 for success.
2774e13ac2e9SMark Brown  */
2775e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2776e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2777e13ac2e9SMark Brown {
27784eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27794eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2780e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2781815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27824eaa9819SJon Smirl 	int min = mc->min;
2783e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2784e13ac2e9SMark Brown 
2785e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2786e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2787e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2788e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2789e13ac2e9SMark Brown 	return 0;
2790e13ac2e9SMark Brown }
2791e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2792e13ac2e9SMark Brown 
2793e13ac2e9SMark Brown /**
2794e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2795e13ac2e9SMark Brown  * @kcontrol: mixer control
2796ac11a2b3SMark Brown  * @ucontrol: control element information
2797e13ac2e9SMark Brown  *
2798e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2799e13ac2e9SMark Brown  *
2800e13ac2e9SMark Brown  * Returns 0 for success.
2801e13ac2e9SMark Brown  */
2802e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2803e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2804e13ac2e9SMark Brown {
28054eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28064eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2807e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2808815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
28094eaa9819SJon Smirl 	int min = mc->min;
281046f5822fSDaniel Ribeiro 	unsigned int val;
2811e13ac2e9SMark Brown 
2812e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2813e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2814e13ac2e9SMark Brown 
28156c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2816e13ac2e9SMark Brown }
2817e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2818e13ac2e9SMark Brown 
28198c6529dbSLiam Girdwood /**
2820637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2821637d3847SPeter Ujfalusi  *
2822637d3847SPeter Ujfalusi  * @codec: where to look for the control
2823637d3847SPeter Ujfalusi  * @name: Name of the control
2824637d3847SPeter Ujfalusi  * @max: new maximum limit
2825637d3847SPeter Ujfalusi  *
2826637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2827637d3847SPeter Ujfalusi  */
2828637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2829637d3847SPeter Ujfalusi 	const char *name, int max)
2830637d3847SPeter Ujfalusi {
2831f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2832637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2833637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2834637d3847SPeter Ujfalusi 	int found = 0;
2835637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2836637d3847SPeter Ujfalusi 
2837637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2838637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2839637d3847SPeter Ujfalusi 		return -EINVAL;
2840637d3847SPeter Ujfalusi 
2841637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2842637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2843637d3847SPeter Ujfalusi 			found = 1;
2844637d3847SPeter Ujfalusi 			break;
2845637d3847SPeter Ujfalusi 		}
2846637d3847SPeter Ujfalusi 	}
2847637d3847SPeter Ujfalusi 	if (found) {
2848637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2849637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2850d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2851637d3847SPeter Ujfalusi 			ret = 0;
2852637d3847SPeter Ujfalusi 		}
2853637d3847SPeter Ujfalusi 	}
2854637d3847SPeter Ujfalusi 	return ret;
2855637d3847SPeter Ujfalusi }
2856637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2857637d3847SPeter Ujfalusi 
2858637d3847SPeter Ujfalusi /**
2859b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2860b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2861b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2862b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2863b6f4bb38Sapatard@mandriva.com  *
2864b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2865b6f4bb38Sapatard@mandriva.com  */
2866b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2867b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2868b6f4bb38Sapatard@mandriva.com {
2869b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2870b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2871b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2872b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2873b6f4bb38Sapatard@mandriva.com 
2874b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2875b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2876b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2877b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2878b6f4bb38Sapatard@mandriva.com 
2879b6f4bb38Sapatard@mandriva.com 	return 0;
2880b6f4bb38Sapatard@mandriva.com }
2881b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2882b6f4bb38Sapatard@mandriva.com 
2883b6f4bb38Sapatard@mandriva.com /**
2884b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2885b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2886b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2887b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2888b6f4bb38Sapatard@mandriva.com  *
2889b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2890b6f4bb38Sapatard@mandriva.com  */
2891b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2892b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2893b6f4bb38Sapatard@mandriva.com {
2894b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2895b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2896b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2897b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2898b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2899b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2900b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2901b6f4bb38Sapatard@mandriva.com 
290220630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
290320630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2904b6f4bb38Sapatard@mandriva.com 	return 0;
2905b6f4bb38Sapatard@mandriva.com }
2906b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2907b6f4bb38Sapatard@mandriva.com 
2908b6f4bb38Sapatard@mandriva.com /**
2909b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2910b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2911b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2912b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2913b6f4bb38Sapatard@mandriva.com  *
2914b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2915b6f4bb38Sapatard@mandriva.com  */
2916b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2917b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2918b6f4bb38Sapatard@mandriva.com {
2919b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2920b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2921b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2922b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2923b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2924b6f4bb38Sapatard@mandriva.com 	int ret;
2925b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2926b6f4bb38Sapatard@mandriva.com 
2927b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2928b6f4bb38Sapatard@mandriva.com 	val &= mask;
2929b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2930b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2931b6f4bb38Sapatard@mandriva.com 
2932b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2933b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2934b6f4bb38Sapatard@mandriva.com 
2935b6f4bb38Sapatard@mandriva.com 	ret = 0;
2936b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2937b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2938b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2939f1df5aecSMark Brown 			return ret;
2940b6f4bb38Sapatard@mandriva.com 	}
2941b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2942b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2943b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2944f1df5aecSMark Brown 			return ret;
2945b6f4bb38Sapatard@mandriva.com 	}
2946b6f4bb38Sapatard@mandriva.com 
2947b6f4bb38Sapatard@mandriva.com 	return 0;
2948b6f4bb38Sapatard@mandriva.com }
2949b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2950b6f4bb38Sapatard@mandriva.com 
2951b6f4bb38Sapatard@mandriva.com /**
29528c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
29538c6529dbSLiam Girdwood  * @dai: DAI
29548c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
29558c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
29568c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
29578c6529dbSLiam Girdwood  *
29588c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
29598c6529dbSLiam Girdwood  */
29608c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
29618c6529dbSLiam Girdwood 	unsigned int freq, int dir)
29628c6529dbSLiam Girdwood {
2963f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2964f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
29658c6529dbSLiam Girdwood 	else
29668c6529dbSLiam Girdwood 		return -EINVAL;
29678c6529dbSLiam Girdwood }
29688c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
29698c6529dbSLiam Girdwood 
29708c6529dbSLiam Girdwood /**
29718c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
29728c6529dbSLiam Girdwood  * @dai: DAI
2973ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
29748c6529dbSLiam Girdwood  * @div: new clock divisor.
29758c6529dbSLiam Girdwood  *
29768c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
29778c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
29788c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
29798c6529dbSLiam Girdwood  */
29808c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
29818c6529dbSLiam Girdwood 	int div_id, int div)
29828c6529dbSLiam Girdwood {
2983f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2984f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
29858c6529dbSLiam Girdwood 	else
29868c6529dbSLiam Girdwood 		return -EINVAL;
29878c6529dbSLiam Girdwood }
29888c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
29898c6529dbSLiam Girdwood 
29908c6529dbSLiam Girdwood /**
29918c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
29928c6529dbSLiam Girdwood  * @dai: DAI
29938c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
299485488037SMark Brown  * @source: DAI specific source for the PLL
29958c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
29968c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
29978c6529dbSLiam Girdwood  *
29988c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
29998c6529dbSLiam Girdwood  */
300085488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
300185488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
30028c6529dbSLiam Girdwood {
3003f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3004f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
300585488037SMark Brown 					 freq_in, freq_out);
30068c6529dbSLiam Girdwood 	else
30078c6529dbSLiam Girdwood 		return -EINVAL;
30088c6529dbSLiam Girdwood }
30098c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
30108c6529dbSLiam Girdwood 
30118c6529dbSLiam Girdwood /**
30128c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
30138c6529dbSLiam Girdwood  * @dai: DAI
30148c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
30158c6529dbSLiam Girdwood  *
30168c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
30178c6529dbSLiam Girdwood  */
30188c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
30198c6529dbSLiam Girdwood {
3020f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
3021f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
30228c6529dbSLiam Girdwood 	else
30238c6529dbSLiam Girdwood 		return -EINVAL;
30248c6529dbSLiam Girdwood }
30258c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
30268c6529dbSLiam Girdwood 
30278c6529dbSLiam Girdwood /**
30288c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
30298c6529dbSLiam Girdwood  * @dai: DAI
3030a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3031a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
30328c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3033a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
30348c6529dbSLiam Girdwood  *
30358c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
30368c6529dbSLiam Girdwood  * specific.
30378c6529dbSLiam Girdwood  */
30388c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3039a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
30408c6529dbSLiam Girdwood {
3041f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3042f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3043a5479e38SDaniel Ribeiro 				slots, slot_width);
30448c6529dbSLiam Girdwood 	else
30458c6529dbSLiam Girdwood 		return -EINVAL;
30468c6529dbSLiam Girdwood }
30478c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
30488c6529dbSLiam Girdwood 
30498c6529dbSLiam Girdwood /**
3050472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3051472df3cbSBarry Song  * @dai: DAI
3052472df3cbSBarry Song  * @tx_num: how many TX channels
3053472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3054472df3cbSBarry Song  *           0~num-1 uses
3055472df3cbSBarry Song  * @rx_num: how many RX channels
3056472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3057472df3cbSBarry Song  *           0~num-1 uses
3058472df3cbSBarry Song  *
3059472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3060472df3cbSBarry Song  */
3061472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3062472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3063472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3064472df3cbSBarry Song {
3065f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3066f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3067472df3cbSBarry Song 			rx_num, rx_slot);
3068472df3cbSBarry Song 	else
3069472df3cbSBarry Song 		return -EINVAL;
3070472df3cbSBarry Song }
3071472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3072472df3cbSBarry Song 
3073472df3cbSBarry Song /**
30748c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
30758c6529dbSLiam Girdwood  * @dai: DAI
30768c6529dbSLiam Girdwood  * @tristate: tristate enable
30778c6529dbSLiam Girdwood  *
30788c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
30798c6529dbSLiam Girdwood  */
30808c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
30818c6529dbSLiam Girdwood {
3082f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3083f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
30848c6529dbSLiam Girdwood 	else
30858c6529dbSLiam Girdwood 		return -EINVAL;
30868c6529dbSLiam Girdwood }
30878c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
30888c6529dbSLiam Girdwood 
30898c6529dbSLiam Girdwood /**
30908c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
30918c6529dbSLiam Girdwood  * @dai: DAI
30928c6529dbSLiam Girdwood  * @mute: mute enable
30938c6529dbSLiam Girdwood  *
30948c6529dbSLiam Girdwood  * Mutes the DAI DAC.
30958c6529dbSLiam Girdwood  */
30968c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
30978c6529dbSLiam Girdwood {
3098f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3099f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
31008c6529dbSLiam Girdwood 	else
31018c6529dbSLiam Girdwood 		return -EINVAL;
31028c6529dbSLiam Girdwood }
31038c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
31048c6529dbSLiam Girdwood 
3105c5af3a2eSMark Brown /**
3106c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3107c5af3a2eSMark Brown  *
3108ac11a2b3SMark Brown  * @card: Card to register
3109c5af3a2eSMark Brown  *
3110c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
3111c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
3112c5af3a2eSMark Brown  * registration APIs.
3113c5af3a2eSMark Brown  */
3114c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
3115c5af3a2eSMark Brown {
3116f0fba2adSLiam Girdwood 	int i;
3117f0fba2adSLiam Girdwood 
3118c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3119c5af3a2eSMark Brown 		return -EINVAL;
3120c5af3a2eSMark Brown 
31212eea392dSJarkko Nikula 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
31222eea392dSJarkko Nikula 			    (card->num_links + card->num_aux_devs),
3123f0fba2adSLiam Girdwood 			    GFP_KERNEL);
3124f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3125f0fba2adSLiam Girdwood 		return -ENOMEM;
31262eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3127f0fba2adSLiam Girdwood 
3128f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3129f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3130f0fba2adSLiam Girdwood 
3131c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3132c5af3a2eSMark Brown 	card->instantiated = 0;
3133f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3134c5af3a2eSMark Brown 
3135c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3136c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
3137435c5e25SMark Brown 	snd_soc_instantiate_cards();
3138c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3139c5af3a2eSMark Brown 
3140c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3141c5af3a2eSMark Brown 
3142c5af3a2eSMark Brown 	return 0;
3143c5af3a2eSMark Brown }
3144c5af3a2eSMark Brown 
3145c5af3a2eSMark Brown /**
3146c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3147c5af3a2eSMark Brown  *
3148ac11a2b3SMark Brown  * @card: Card to unregister
3149c5af3a2eSMark Brown  *
3150c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
3151c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
3152c5af3a2eSMark Brown  * registration APIs.
3153c5af3a2eSMark Brown  */
3154c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
3155c5af3a2eSMark Brown {
3156c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3157c5af3a2eSMark Brown 	list_del(&card->list);
3158c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3159c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3160c5af3a2eSMark Brown 
3161c5af3a2eSMark Brown 	return 0;
3162c5af3a2eSMark Brown }
3163c5af3a2eSMark Brown 
3164f0fba2adSLiam Girdwood /*
3165f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3166f0fba2adSLiam Girdwood  * and sanitizing names.
3167f0fba2adSLiam Girdwood  */
31680b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3169f0fba2adSLiam Girdwood {
3170f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3171f0fba2adSLiam Girdwood 	int id1, id2;
3172f0fba2adSLiam Girdwood 
3173f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3174f0fba2adSLiam Girdwood 		return NULL;
3175f0fba2adSLiam Girdwood 
317658818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3177f0fba2adSLiam Girdwood 
3178f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3179f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3180f0fba2adSLiam Girdwood 	if (found) {
3181f0fba2adSLiam Girdwood 		/* get ID */
3182f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3183f0fba2adSLiam Girdwood 
3184f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3185f0fba2adSLiam Girdwood 			if (*id == -1)
3186f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3187f0fba2adSLiam Girdwood 		}
3188f0fba2adSLiam Girdwood 
3189f0fba2adSLiam Girdwood 	} else {
3190f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3191f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3192f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3193f0fba2adSLiam Girdwood 
3194f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
319505899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3196f0fba2adSLiam Girdwood 
3197f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3198f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
319958818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3200f0fba2adSLiam Girdwood 		} else
3201f0fba2adSLiam Girdwood 			*id = 0;
3202f0fba2adSLiam Girdwood 	}
3203f0fba2adSLiam Girdwood 
3204f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3205f0fba2adSLiam Girdwood }
3206f0fba2adSLiam Girdwood 
3207f0fba2adSLiam Girdwood /*
3208f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3209f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3210f0fba2adSLiam Girdwood  */
3211f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3212f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3213f0fba2adSLiam Girdwood {
3214f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
3215f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3216f0fba2adSLiam Girdwood 				dev_name(dev));
3217f0fba2adSLiam Girdwood 		return NULL;
3218f0fba2adSLiam Girdwood 	}
3219f0fba2adSLiam Girdwood 
3220f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3221f0fba2adSLiam Girdwood }
3222f0fba2adSLiam Girdwood 
32239115171aSMark Brown /**
32249115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
32259115171aSMark Brown  *
3226ac11a2b3SMark Brown  * @dai: DAI to register
32279115171aSMark Brown  */
3228f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3229f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
32309115171aSMark Brown {
3231f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
32329115171aSMark Brown 
3233f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
32349115171aSMark Brown 
3235f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3236f0fba2adSLiam Girdwood 	if (dai == NULL)
3237f0fba2adSLiam Girdwood 			return -ENOMEM;
32386335d055SEric Miao 
3239f0fba2adSLiam Girdwood 	/* create DAI component name */
3240f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3241f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3242f0fba2adSLiam Girdwood 		kfree(dai);
3243f0fba2adSLiam Girdwood 		return -ENOMEM;
3244f0fba2adSLiam Girdwood 	}
3245f0fba2adSLiam Girdwood 
3246f0fba2adSLiam Girdwood 	dai->dev = dev;
3247f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3248f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3249f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
32509115171aSMark Brown 
32519115171aSMark Brown 	mutex_lock(&client_mutex);
32529115171aSMark Brown 	list_add(&dai->list, &dai_list);
3253435c5e25SMark Brown 	snd_soc_instantiate_cards();
32549115171aSMark Brown 	mutex_unlock(&client_mutex);
32559115171aSMark Brown 
32569115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
32579115171aSMark Brown 
32589115171aSMark Brown 	return 0;
32599115171aSMark Brown }
32609115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
32619115171aSMark Brown 
32629115171aSMark Brown /**
32639115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
32649115171aSMark Brown  *
3265ac11a2b3SMark Brown  * @dai: DAI to unregister
32669115171aSMark Brown  */
3267f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
32689115171aSMark Brown {
3269f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3270f0fba2adSLiam Girdwood 
3271f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3272f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3273f0fba2adSLiam Girdwood 			goto found;
3274f0fba2adSLiam Girdwood 	}
3275f0fba2adSLiam Girdwood 	return;
3276f0fba2adSLiam Girdwood 
3277f0fba2adSLiam Girdwood found:
32789115171aSMark Brown 	mutex_lock(&client_mutex);
32799115171aSMark Brown 	list_del(&dai->list);
32809115171aSMark Brown 	mutex_unlock(&client_mutex);
32819115171aSMark Brown 
32829115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3283f0fba2adSLiam Girdwood 	kfree(dai->name);
3284f0fba2adSLiam Girdwood 	kfree(dai);
32859115171aSMark Brown }
32869115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
32879115171aSMark Brown 
32889115171aSMark Brown /**
32899115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
32909115171aSMark Brown  *
3291ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3292ac11a2b3SMark Brown  * @count: Number of DAIs
32939115171aSMark Brown  */
3294f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3295f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
32969115171aSMark Brown {
3297f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3298f0fba2adSLiam Girdwood 	int i, ret = 0;
3299f0fba2adSLiam Girdwood 
3300720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
33019115171aSMark Brown 
33029115171aSMark Brown 	for (i = 0; i < count; i++) {
3303f0fba2adSLiam Girdwood 
3304f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3305c46e0079SAxel Lin 		if (dai == NULL) {
3306c46e0079SAxel Lin 			ret = -ENOMEM;
3307c46e0079SAxel Lin 			goto err;
3308c46e0079SAxel Lin 		}
3309f0fba2adSLiam Girdwood 
3310f0fba2adSLiam Girdwood 		/* create DAI component name */
3311f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3312f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3313f0fba2adSLiam Girdwood 			kfree(dai);
3314f0fba2adSLiam Girdwood 			ret = -EINVAL;
33159115171aSMark Brown 			goto err;
33169115171aSMark Brown 		}
33179115171aSMark Brown 
3318f0fba2adSLiam Girdwood 		dai->dev = dev;
3319f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
33200f9141c9SMark Brown 		if (dai->driver->id)
33210f9141c9SMark Brown 			dai->id = dai->driver->id;
33220f9141c9SMark Brown 		else
33230f9141c9SMark Brown 			dai->id = i;
3324f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3325f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3326f0fba2adSLiam Girdwood 
3327f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3328f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3329f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3330f0fba2adSLiam Girdwood 
3331f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3332f0fba2adSLiam Girdwood 	}
3333f0fba2adSLiam Girdwood 
33341dcb4f38SAxel Lin 	mutex_lock(&client_mutex);
3335f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
33361dcb4f38SAxel Lin 	mutex_unlock(&client_mutex);
33379115171aSMark Brown 	return 0;
33389115171aSMark Brown 
33399115171aSMark Brown err:
33409115171aSMark Brown 	for (i--; i >= 0; i--)
3341f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33429115171aSMark Brown 
33439115171aSMark Brown 	return ret;
33449115171aSMark Brown }
33459115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
33469115171aSMark Brown 
33479115171aSMark Brown /**
33489115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
33499115171aSMark Brown  *
3350ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3351ac11a2b3SMark Brown  * @count: Number of DAIs
33529115171aSMark Brown  */
3353f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
33549115171aSMark Brown {
33559115171aSMark Brown 	int i;
33569115171aSMark Brown 
33579115171aSMark Brown 	for (i = 0; i < count; i++)
3358f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33599115171aSMark Brown }
33609115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
33619115171aSMark Brown 
336212a48a8cSMark Brown /**
336312a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
336412a48a8cSMark Brown  *
3365ac11a2b3SMark Brown  * @platform: platform to register
336612a48a8cSMark Brown  */
3367f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3368f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
336912a48a8cSMark Brown {
3370f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
337112a48a8cSMark Brown 
3372f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3373f0fba2adSLiam Girdwood 
3374f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3375f0fba2adSLiam Girdwood 	if (platform == NULL)
3376f0fba2adSLiam Girdwood 			return -ENOMEM;
3377f0fba2adSLiam Girdwood 
3378f0fba2adSLiam Girdwood 	/* create platform component name */
3379f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3380f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3381f0fba2adSLiam Girdwood 		kfree(platform);
3382f0fba2adSLiam Girdwood 		return -ENOMEM;
3383f0fba2adSLiam Girdwood 	}
3384f0fba2adSLiam Girdwood 
3385f0fba2adSLiam Girdwood 	platform->dev = dev;
3386f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
338712a48a8cSMark Brown 
338812a48a8cSMark Brown 	mutex_lock(&client_mutex);
338912a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3390435c5e25SMark Brown 	snd_soc_instantiate_cards();
339112a48a8cSMark Brown 	mutex_unlock(&client_mutex);
339212a48a8cSMark Brown 
339312a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
339412a48a8cSMark Brown 
339512a48a8cSMark Brown 	return 0;
339612a48a8cSMark Brown }
339712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
339812a48a8cSMark Brown 
339912a48a8cSMark Brown /**
340012a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
340112a48a8cSMark Brown  *
3402ac11a2b3SMark Brown  * @platform: platform to unregister
340312a48a8cSMark Brown  */
3404f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
340512a48a8cSMark Brown {
3406f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3407f0fba2adSLiam Girdwood 
3408f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3409f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3410f0fba2adSLiam Girdwood 			goto found;
3411f0fba2adSLiam Girdwood 	}
3412f0fba2adSLiam Girdwood 	return;
3413f0fba2adSLiam Girdwood 
3414f0fba2adSLiam Girdwood found:
341512a48a8cSMark Brown 	mutex_lock(&client_mutex);
341612a48a8cSMark Brown 	list_del(&platform->list);
341712a48a8cSMark Brown 	mutex_unlock(&client_mutex);
341812a48a8cSMark Brown 
341912a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3420f0fba2adSLiam Girdwood 	kfree(platform->name);
3421f0fba2adSLiam Girdwood 	kfree(platform);
342212a48a8cSMark Brown }
342312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
342412a48a8cSMark Brown 
3425151ab22cSMark Brown static u64 codec_format_map[] = {
3426151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3427151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3428151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3429151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3430151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3431151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3432151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3433151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3434151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3435151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3436151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3437151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3438151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3439151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3440151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3441151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3442151ab22cSMark Brown };
3443151ab22cSMark Brown 
3444151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3445151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3446151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3447151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3448151ab22cSMark Brown  */
3449151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3450151ab22cSMark Brown {
3451151ab22cSMark Brown 	int i;
3452151ab22cSMark Brown 
3453151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3454151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3455151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3456151ab22cSMark Brown }
3457151ab22cSMark Brown 
34580d0cf00aSMark Brown /**
34590d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
34600d0cf00aSMark Brown  *
3461ac11a2b3SMark Brown  * @codec: codec to register
34620d0cf00aSMark Brown  */
3463f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3464001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3465001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3466001ae4c0SMark Brown 			   int num_dai)
34670d0cf00aSMark Brown {
34683335ddcaSDimitris Papastamos 	size_t reg_size;
3469f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3470f0fba2adSLiam Girdwood 	int ret, i;
3471151ab22cSMark Brown 
3472f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
34730d0cf00aSMark Brown 
3474f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3475f0fba2adSLiam Girdwood 	if (codec == NULL)
3476f0fba2adSLiam Girdwood 		return -ENOMEM;
34770d0cf00aSMark Brown 
3478f0fba2adSLiam Girdwood 	/* create CODEC component name */
3479f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3480f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3481f0fba2adSLiam Girdwood 		kfree(codec);
3482f0fba2adSLiam Girdwood 		return -ENOMEM;
3483151ab22cSMark Brown 	}
3484151ab22cSMark Brown 
348523bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
348623bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
348723bbce34SDimitris Papastamos 	else
348823bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
348923bbce34SDimitris Papastamos 
3490c3acec26SMark Brown 	codec->write = codec_drv->write;
3491c3acec26SMark Brown 	codec->read = codec_drv->read;
3492ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3493ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3494ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3495f0fba2adSLiam Girdwood 	codec->dev = dev;
3496f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3497f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3498f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3499f0fba2adSLiam Girdwood 
35007a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
35017a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
35023335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
35033335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
35043335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
35053335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
35063335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
35073335ddcaSDimitris Papastamos 		 * the cache.
35083335ddcaSDimitris Papastamos 		 */
35092aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
35103335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
35113335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
35123335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
35133335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3514fdf0f54dSDimitris Papastamos 				goto fail;
35157a30a3dbSDimitris Papastamos 			}
35167a30a3dbSDimitris Papastamos 		}
35172aa86323SDimitris Papastamos 	}
35187a30a3dbSDimitris Papastamos 
3519f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3520f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3521f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3522f0fba2adSLiam Girdwood 	}
3523f0fba2adSLiam Girdwood 
352426b01ccdSMark Brown 	/* register any DAIs */
352526b01ccdSMark Brown 	if (num_dai) {
3526f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3527f0fba2adSLiam Girdwood 		if (ret < 0)
3528fdf0f54dSDimitris Papastamos 			goto fail;
352926b01ccdSMark Brown 	}
3530f0fba2adSLiam Girdwood 
35310d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35320d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
35330d0cf00aSMark Brown 	snd_soc_instantiate_cards();
35340d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35350d0cf00aSMark Brown 
35360d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
35370d0cf00aSMark Brown 	return 0;
3538f0fba2adSLiam Girdwood 
3539fdf0f54dSDimitris Papastamos fail:
35403335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35413335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3542f0fba2adSLiam Girdwood 	kfree(codec->name);
3543f0fba2adSLiam Girdwood 	kfree(codec);
3544f0fba2adSLiam Girdwood 	return ret;
35450d0cf00aSMark Brown }
35460d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
35470d0cf00aSMark Brown 
35480d0cf00aSMark Brown /**
35490d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
35500d0cf00aSMark Brown  *
3551ac11a2b3SMark Brown  * @codec: codec to unregister
35520d0cf00aSMark Brown  */
3553f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
35540d0cf00aSMark Brown {
3555f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3556f0fba2adSLiam Girdwood 	int i;
3557f0fba2adSLiam Girdwood 
3558f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3559f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3560f0fba2adSLiam Girdwood 			goto found;
3561f0fba2adSLiam Girdwood 	}
3562f0fba2adSLiam Girdwood 	return;
3563f0fba2adSLiam Girdwood 
3564f0fba2adSLiam Girdwood found:
356526b01ccdSMark Brown 	if (codec->num_dai)
3566f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3567f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3568f0fba2adSLiam Girdwood 
35690d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35700d0cf00aSMark Brown 	list_del(&codec->list);
35710d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35720d0cf00aSMark Brown 
35730d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3574f0fba2adSLiam Girdwood 
35757a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
35763335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35771aafcd4dSDimitris Papastamos 	kfree(codec->name);
3578f0fba2adSLiam Girdwood 	kfree(codec);
35790d0cf00aSMark Brown }
35800d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
35810d0cf00aSMark Brown 
3582c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3583db2a4165SFrank Mandarino {
3584384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
35858a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
35868a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3587384c89e2SMark Brown 		printk(KERN_WARNING
3588384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
35898a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3590384c89e2SMark Brown 	}
3591c3c5a19aSMark Brown 
35928a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3593c3c5a19aSMark Brown 				 &codec_list_fops))
3594c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3595c3c5a19aSMark Brown 
35968a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3597f3208780SMark Brown 				 &dai_list_fops))
3598f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
359919c7ac27SMark Brown 
36008a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
360119c7ac27SMark Brown 				 &platform_list_fops))
360219c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3603384c89e2SMark Brown #endif
3604384c89e2SMark Brown 
3605db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3606db2a4165SFrank Mandarino }
36074abe8e16SMark Brown module_init(snd_soc_init);
3608db2a4165SFrank Mandarino 
36097d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3610db2a4165SFrank Mandarino {
3611384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
36128a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3613384c89e2SMark Brown #endif
3614db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3615db2a4165SFrank Mandarino }
3616db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3617db2a4165SFrank Mandarino 
3618db2a4165SFrank Mandarino /* Module information */
3619d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3620db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3621db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
36228b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3623