xref: /openbmc/linux/sound/soc/soc-core.c (revision 0f9141c974b2dc29b7d1cf245bbf4685350521be)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6f0fba2adSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7f0fba2adSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
80664d888SLiam Girdwood  *
9d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
100664d888SLiam Girdwood  *         with code, comments and ideas from :-
110664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
14db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
15db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
16db2a4165SFrank Mandarino  *  option) any later version.
17db2a4165SFrank Mandarino  *
18db2a4165SFrank Mandarino  *  TODO:
19db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
20db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
21db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
22db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
23db2a4165SFrank Mandarino  */
24db2a4165SFrank Mandarino 
25db2a4165SFrank Mandarino #include <linux/module.h>
26db2a4165SFrank Mandarino #include <linux/moduleparam.h>
27db2a4165SFrank Mandarino #include <linux/init.h>
28db2a4165SFrank Mandarino #include <linux/delay.h>
29db2a4165SFrank Mandarino #include <linux/pm.h>
30db2a4165SFrank Mandarino #include <linux/bitops.h>
3112ef193dSTroy Kisky #include <linux/debugfs.h>
32db2a4165SFrank Mandarino #include <linux/platform_device.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
34474828a4SMarek Vasut #include <sound/ac97_codec.h>
35db2a4165SFrank Mandarino #include <sound/core.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
39db2a4165SFrank Mandarino #include <sound/soc-dapm.h>
40db2a4165SFrank Mandarino #include <sound/initval.h>
41db2a4165SFrank Mandarino 
42f0fba2adSLiam Girdwood #define NAME_SIZE	32
43f0fba2adSLiam Girdwood 
44db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex);
45db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
46db2a4165SFrank Mandarino 
47384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
48384c89e2SMark Brown static struct dentry *debugfs_root;
49384c89e2SMark Brown #endif
50384c89e2SMark Brown 
51c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
52c5af3a2eSMark Brown static LIST_HEAD(card_list);
539115171aSMark Brown static LIST_HEAD(dai_list);
5412a48a8cSMark Brown static LIST_HEAD(platform_list);
550d0cf00aSMark Brown static LIST_HEAD(codec_list);
56c5af3a2eSMark Brown 
57c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card);
58c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card);
59f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
60c5af3a2eSMark Brown 
61db2a4165SFrank Mandarino /*
62db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
63db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
64db2a4165SFrank Mandarino  * between two audio tracks.
65db2a4165SFrank Mandarino  */
66db2a4165SFrank Mandarino static int pmdown_time = 5000;
67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69db2a4165SFrank Mandarino 
70965ac42cSLiam Girdwood /*
71965ac42cSLiam Girdwood  * This function forces any delayed work to be queued and run.
72965ac42cSLiam Girdwood  */
73965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork)
74965ac42cSLiam Girdwood {
75965ac42cSLiam Girdwood 	int ret;
76965ac42cSLiam Girdwood 
77965ac42cSLiam Girdwood 	/* cancel any work waiting to be queued. */
78965ac42cSLiam Girdwood 	ret = cancel_delayed_work(dwork);
79965ac42cSLiam Girdwood 
80965ac42cSLiam Girdwood 	/* if there was any work waiting then we run it now and
81965ac42cSLiam Girdwood 	 * wait for it's completion */
82965ac42cSLiam Girdwood 	if (ret) {
83965ac42cSLiam Girdwood 		schedule_delayed_work(dwork, 0);
84965ac42cSLiam Girdwood 		flush_scheduled_work();
85965ac42cSLiam Girdwood 	}
86965ac42cSLiam Girdwood 	return ret;
87965ac42cSLiam Girdwood }
88965ac42cSLiam Girdwood 
892624d5faSMark Brown /* codec register dump */
902624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
912624d5faSMark Brown {
925164d74dSMark Brown 	int ret, i, step = 1, count = 0;
932624d5faSMark Brown 
94f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
952624d5faSMark Brown 		return 0;
962624d5faSMark Brown 
97f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
98f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
992624d5faSMark Brown 
1002624d5faSMark Brown 	count += sprintf(buf, "%s registers\n", codec->name);
101f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
102f0fba2adSLiam Girdwood 		if (codec->driver->readable_register && !codec->driver->readable_register(i))
1032624d5faSMark Brown 			continue;
1042624d5faSMark Brown 
1052624d5faSMark Brown 		count += sprintf(buf + count, "%2x: ", i);
1062624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1072624d5faSMark Brown 			break;
1082624d5faSMark Brown 
109f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
110f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1112624d5faSMark Brown 							 PAGE_SIZE - count, i);
1125164d74dSMark Brown 		} else {
1135164d74dSMark Brown 			/* If the read fails it's almost certainly due to
1145164d74dSMark Brown 			 * the register being volatile and the device being
1155164d74dSMark Brown 			 * powered off.
1165164d74dSMark Brown 			 */
117f0fba2adSLiam Girdwood 			ret = codec->driver->read(codec, i);
1185164d74dSMark Brown 			if (ret >= 0)
1195164d74dSMark Brown 				count += snprintf(buf + count,
1205164d74dSMark Brown 						  PAGE_SIZE - count,
1215164d74dSMark Brown 						  "%4x", ret);
1222624d5faSMark Brown 			else
1235164d74dSMark Brown 				count += snprintf(buf + count,
1245164d74dSMark Brown 						  PAGE_SIZE - count,
1255164d74dSMark Brown 						  "<no data: %d>", ret);
1265164d74dSMark Brown 		}
1272624d5faSMark Brown 
1282624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1292624d5faSMark Brown 			break;
1302624d5faSMark Brown 
1312624d5faSMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1322624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1332624d5faSMark Brown 			break;
1342624d5faSMark Brown 	}
1352624d5faSMark Brown 
1362624d5faSMark Brown 	/* Truncate count; min() would cause a warning */
1372624d5faSMark Brown 	if (count >= PAGE_SIZE)
1382624d5faSMark Brown 		count = PAGE_SIZE - 1;
1392624d5faSMark Brown 
1402624d5faSMark Brown 	return count;
1412624d5faSMark Brown }
1422624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1432624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1442624d5faSMark Brown {
145f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
146f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
147f0fba2adSLiam Girdwood 
148f0fba2adSLiam Girdwood 	return soc_codec_reg_show(rtd->codec, buf);
1492624d5faSMark Brown }
1502624d5faSMark Brown 
1512624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1522624d5faSMark Brown 
153dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
154dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
155dbe21408SMark Brown {
156f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
157f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
158dbe21408SMark Brown 
159f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
160dbe21408SMark Brown }
161dbe21408SMark Brown 
162dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
163dbe21408SMark Brown 			       struct device_attribute *attr,
164dbe21408SMark Brown 			       const char *buf, size_t count)
165dbe21408SMark Brown {
166f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
167f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
168dbe21408SMark Brown 
169f0fba2adSLiam Girdwood 	strict_strtol(buf, 10, &rtd->pmdown_time);
170dbe21408SMark Brown 
171dbe21408SMark Brown 	return count;
172dbe21408SMark Brown }
173dbe21408SMark Brown 
174dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
175dbe21408SMark Brown 
1762624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
1772624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
1782624d5faSMark Brown {
1792624d5faSMark Brown 	file->private_data = inode->i_private;
1802624d5faSMark Brown 	return 0;
1812624d5faSMark Brown }
1822624d5faSMark Brown 
1832624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1842624d5faSMark Brown 			       size_t count, loff_t *ppos)
1852624d5faSMark Brown {
1862624d5faSMark Brown 	ssize_t ret;
1872624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1882624d5faSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1892624d5faSMark Brown 	if (!buf)
1902624d5faSMark Brown 		return -ENOMEM;
1912624d5faSMark Brown 	ret = soc_codec_reg_show(codec, buf);
1922624d5faSMark Brown 	if (ret >= 0)
1932624d5faSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1942624d5faSMark Brown 	kfree(buf);
1952624d5faSMark Brown 	return ret;
1962624d5faSMark Brown }
1972624d5faSMark Brown 
1982624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
1992624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2002624d5faSMark Brown {
2012624d5faSMark Brown 	char buf[32];
2022624d5faSMark Brown 	int buf_size;
2032624d5faSMark Brown 	char *start = buf;
2042624d5faSMark Brown 	unsigned long reg, value;
2052624d5faSMark Brown 	int step = 1;
2062624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2072624d5faSMark Brown 
2082624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2092624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2102624d5faSMark Brown 		return -EFAULT;
2112624d5faSMark Brown 	buf[buf_size] = 0;
2122624d5faSMark Brown 
213f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
214f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
2152624d5faSMark Brown 
2162624d5faSMark Brown 	while (*start == ' ')
2172624d5faSMark Brown 		start++;
2182624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
219f0fba2adSLiam Girdwood 	if ((reg >= codec->driver->reg_cache_size) || (reg % step))
2202624d5faSMark Brown 		return -EINVAL;
2212624d5faSMark Brown 	while (*start == ' ')
2222624d5faSMark Brown 		start++;
2232624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2242624d5faSMark Brown 		return -EINVAL;
225f0fba2adSLiam Girdwood 	codec->driver->write(codec, reg, value);
2262624d5faSMark Brown 	return buf_size;
2272624d5faSMark Brown }
2282624d5faSMark Brown 
2292624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2302624d5faSMark Brown 	.open = codec_reg_open_file,
2312624d5faSMark Brown 	.read = codec_reg_read_file,
2322624d5faSMark Brown 	.write = codec_reg_write_file,
2332624d5faSMark Brown };
2342624d5faSMark Brown 
2352624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2362624d5faSMark Brown {
2376ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
2382624d5faSMark Brown 						       debugfs_root);
2392624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2402624d5faSMark Brown 		printk(KERN_WARNING
2412624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2422624d5faSMark Brown 		return;
2432624d5faSMark Brown 	}
2442624d5faSMark Brown 
2452624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2462624d5faSMark Brown 						 codec->debugfs_codec_root,
2472624d5faSMark Brown 						 codec, &codec_reg_fops);
2482624d5faSMark Brown 	if (!codec->debugfs_reg)
2492624d5faSMark Brown 		printk(KERN_WARNING
2502624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
2512624d5faSMark Brown 
252708fafb3SAxel Lin 	codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
2532624d5faSMark Brown 						     codec->debugfs_codec_root,
2542624d5faSMark Brown 						     &codec->pop_time);
2552624d5faSMark Brown 	if (!codec->debugfs_pop_time)
2562624d5faSMark Brown 		printk(KERN_WARNING
2572624d5faSMark Brown 		       "Failed to create pop time debugfs file\n");
2582624d5faSMark Brown 
2592624d5faSMark Brown 	codec->debugfs_dapm = debugfs_create_dir("dapm",
2602624d5faSMark Brown 						 codec->debugfs_codec_root);
2612624d5faSMark Brown 	if (!codec->debugfs_dapm)
2622624d5faSMark Brown 		printk(KERN_WARNING
2632624d5faSMark Brown 		       "Failed to create DAPM debugfs directory\n");
2642624d5faSMark Brown 
2652624d5faSMark Brown 	snd_soc_dapm_debugfs_init(codec);
2662624d5faSMark Brown }
2672624d5faSMark Brown 
2682624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2692624d5faSMark Brown {
2702624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2712624d5faSMark Brown }
2722624d5faSMark Brown 
273c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
274c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
275c3c5a19aSMark Brown {
276c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2772b194f9dSMark Brown 	ssize_t len, ret = 0;
278c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
279c3c5a19aSMark Brown 
280c3c5a19aSMark Brown 	if (!buf)
281c3c5a19aSMark Brown 		return -ENOMEM;
282c3c5a19aSMark Brown 
2832b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
2842b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
285c3c5a19aSMark Brown 			       codec->name);
2862b194f9dSMark Brown 		if (len >= 0)
2872b194f9dSMark Brown 			ret += len;
2882b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
2892b194f9dSMark Brown 			ret = PAGE_SIZE;
2902b194f9dSMark Brown 			break;
2912b194f9dSMark Brown 		}
2922b194f9dSMark Brown 	}
293c3c5a19aSMark Brown 
294c3c5a19aSMark Brown 	if (ret >= 0)
295c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
296c3c5a19aSMark Brown 
297c3c5a19aSMark Brown 	kfree(buf);
298c3c5a19aSMark Brown 
299c3c5a19aSMark Brown 	return ret;
300c3c5a19aSMark Brown }
301c3c5a19aSMark Brown 
302c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
303c3c5a19aSMark Brown 	.read = codec_list_read_file,
304c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
305c3c5a19aSMark Brown };
306c3c5a19aSMark Brown 
307f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
308f3208780SMark Brown 				  size_t count, loff_t *ppos)
309f3208780SMark Brown {
310f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3112b194f9dSMark Brown 	ssize_t len, ret = 0;
312f3208780SMark Brown 	struct snd_soc_dai *dai;
313f3208780SMark Brown 
314f3208780SMark Brown 	if (!buf)
315f3208780SMark Brown 		return -ENOMEM;
316f3208780SMark Brown 
3172b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3182b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3192b194f9dSMark Brown 		if (len >= 0)
3202b194f9dSMark Brown 			ret += len;
3212b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3222b194f9dSMark Brown 			ret = PAGE_SIZE;
3232b194f9dSMark Brown 			break;
3242b194f9dSMark Brown 		}
3252b194f9dSMark Brown 	}
326f3208780SMark Brown 
327f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
328f3208780SMark Brown 
329f3208780SMark Brown 	kfree(buf);
330f3208780SMark Brown 
331f3208780SMark Brown 	return ret;
332f3208780SMark Brown }
333f3208780SMark Brown 
334f3208780SMark Brown static const struct file_operations dai_list_fops = {
335f3208780SMark Brown 	.read = dai_list_read_file,
336f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
337f3208780SMark Brown };
338f3208780SMark Brown 
33919c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
34019c7ac27SMark Brown 				       char __user *user_buf,
34119c7ac27SMark Brown 				       size_t count, loff_t *ppos)
34219c7ac27SMark Brown {
34319c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3442b194f9dSMark Brown 	ssize_t len, ret = 0;
34519c7ac27SMark Brown 	struct snd_soc_platform *platform;
34619c7ac27SMark Brown 
34719c7ac27SMark Brown 	if (!buf)
34819c7ac27SMark Brown 		return -ENOMEM;
34919c7ac27SMark Brown 
3502b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3512b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
35219c7ac27SMark Brown 			       platform->name);
3532b194f9dSMark Brown 		if (len >= 0)
3542b194f9dSMark Brown 			ret += len;
3552b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3562b194f9dSMark Brown 			ret = PAGE_SIZE;
3572b194f9dSMark Brown 			break;
3582b194f9dSMark Brown 		}
3592b194f9dSMark Brown 	}
36019c7ac27SMark Brown 
36119c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
36219c7ac27SMark Brown 
36319c7ac27SMark Brown 	kfree(buf);
36419c7ac27SMark Brown 
36519c7ac27SMark Brown 	return ret;
36619c7ac27SMark Brown }
36719c7ac27SMark Brown 
36819c7ac27SMark Brown static const struct file_operations platform_list_fops = {
36919c7ac27SMark Brown 	.read = platform_list_read_file,
37019c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
37119c7ac27SMark Brown };
37219c7ac27SMark Brown 
3732624d5faSMark Brown #else
3742624d5faSMark Brown 
3752624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
3762624d5faSMark Brown {
3772624d5faSMark Brown }
3782624d5faSMark Brown 
3792624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3802624d5faSMark Brown {
3812624d5faSMark Brown }
3822624d5faSMark Brown #endif
3832624d5faSMark Brown 
384db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
385db2a4165SFrank Mandarino /* unregister ac97 codec */
386db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
387db2a4165SFrank Mandarino {
388db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
389db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
390db2a4165SFrank Mandarino 	return 0;
391db2a4165SFrank Mandarino }
392db2a4165SFrank Mandarino 
393db2a4165SFrank Mandarino /* stop no dev release warning */
394db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
395db2a4165SFrank Mandarino 
396db2a4165SFrank Mandarino /* register ac97 codec to bus */
397db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
398db2a4165SFrank Mandarino {
399db2a4165SFrank Mandarino 	int err;
400db2a4165SFrank Mandarino 
401db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4024ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
403db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
404db2a4165SFrank Mandarino 
405bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
406f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
407db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
408db2a4165SFrank Mandarino 	if (err < 0) {
409db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
410db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
411db2a4165SFrank Mandarino 		return err;
412db2a4165SFrank Mandarino 	}
413db2a4165SFrank Mandarino 	return 0;
414db2a4165SFrank Mandarino }
415db2a4165SFrank Mandarino #endif
416db2a4165SFrank Mandarino 
41706f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
41806f409d7SMark Brown {
41906f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
420f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
421f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
42206f409d7SMark Brown 	int ret;
42306f409d7SMark Brown 
424f0fba2adSLiam Girdwood 	if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
425f0fba2adSLiam Girdwood 			rtd->dai_link->symmetric_rates) {
426f0fba2adSLiam Girdwood 		dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
427f0fba2adSLiam Girdwood 				rtd->rate);
42806f409d7SMark Brown 
42906f409d7SMark Brown 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
43006f409d7SMark Brown 						   SNDRV_PCM_HW_PARAM_RATE,
431f0fba2adSLiam Girdwood 						   rtd->rate,
432f0fba2adSLiam Girdwood 						   rtd->rate);
43306f409d7SMark Brown 		if (ret < 0) {
434f0fba2adSLiam Girdwood 			dev_err(&rtd->dev,
43506f409d7SMark Brown 				"Unable to apply rate symmetry constraint: %d\n", ret);
43606f409d7SMark Brown 			return ret;
43706f409d7SMark Brown 		}
43806f409d7SMark Brown 	}
43906f409d7SMark Brown 
44006f409d7SMark Brown 	return 0;
44106f409d7SMark Brown }
44206f409d7SMark Brown 
443db2a4165SFrank Mandarino /*
444db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
445db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
446db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
447db2a4165SFrank Mandarino  */
448db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
449db2a4165SFrank Mandarino {
450db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
451db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
452f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
453f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
455f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
456f0fba2adSLiam Girdwood 	struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
457db2a4165SFrank Mandarino 	int ret = 0;
458db2a4165SFrank Mandarino 
459db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
460db2a4165SFrank Mandarino 
461db2a4165SFrank Mandarino 	/* startup the audio subsystem */
462f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->startup) {
463f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
464db2a4165SFrank Mandarino 		if (ret < 0) {
465db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
466cb666e5bSLiam Girdwood 				cpu_dai->name);
467db2a4165SFrank Mandarino 			goto out;
468db2a4165SFrank Mandarino 		}
469db2a4165SFrank Mandarino 	}
470db2a4165SFrank Mandarino 
471f0fba2adSLiam Girdwood 	if (platform->driver->ops->open) {
472f0fba2adSLiam Girdwood 		ret = platform->driver->ops->open(substream);
473db2a4165SFrank Mandarino 		if (ret < 0) {
474db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
475db2a4165SFrank Mandarino 			goto platform_err;
476db2a4165SFrank Mandarino 		}
477db2a4165SFrank Mandarino 	}
478db2a4165SFrank Mandarino 
479f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->startup) {
480f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->startup(substream, codec_dai);
481cb666e5bSLiam Girdwood 		if (ret < 0) {
482cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
483cb666e5bSLiam Girdwood 				codec_dai->name);
484cb666e5bSLiam Girdwood 			goto codec_dai_err;
485cb666e5bSLiam Girdwood 		}
486cb666e5bSLiam Girdwood 	}
487cb666e5bSLiam Girdwood 
488f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
489f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
490db2a4165SFrank Mandarino 		if (ret < 0) {
491f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
492db2a4165SFrank Mandarino 			goto machine_err;
493db2a4165SFrank Mandarino 		}
494db2a4165SFrank Mandarino 	}
495db2a4165SFrank Mandarino 
496db2a4165SFrank Mandarino 	/* Check that the codec and cpu DAI's are compatible */
497db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
498db2a4165SFrank Mandarino 		runtime->hw.rate_min =
499f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.rate_min,
500f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_min);
501db2a4165SFrank Mandarino 		runtime->hw.rate_max =
502f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.rate_max,
503f0fba2adSLiam Girdwood 			    cpu_dai_drv->playback.rate_max);
504db2a4165SFrank Mandarino 		runtime->hw.channels_min =
505f0fba2adSLiam Girdwood 			max(codec_dai_drv->playback.channels_min,
506f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_min);
507db2a4165SFrank Mandarino 		runtime->hw.channels_max =
508f0fba2adSLiam Girdwood 			min(codec_dai_drv->playback.channels_max,
509f0fba2adSLiam Girdwood 				cpu_dai_drv->playback.channels_max);
510cb666e5bSLiam Girdwood 		runtime->hw.formats =
511f0fba2adSLiam Girdwood 			codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
512cb666e5bSLiam Girdwood 		runtime->hw.rates =
513f0fba2adSLiam Girdwood 			codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
514f0fba2adSLiam Girdwood 		if (codec_dai_drv->playback.rates
515d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
516f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->playback.rates;
517f0fba2adSLiam Girdwood 		if (cpu_dai_drv->playback.rates
518d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
519f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->playback.rates;
520db2a4165SFrank Mandarino 	} else {
521db2a4165SFrank Mandarino 		runtime->hw.rate_min =
522f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.rate_min,
523f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_min);
524db2a4165SFrank Mandarino 		runtime->hw.rate_max =
525f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.rate_max,
526f0fba2adSLiam Girdwood 			    cpu_dai_drv->capture.rate_max);
527db2a4165SFrank Mandarino 		runtime->hw.channels_min =
528f0fba2adSLiam Girdwood 			max(codec_dai_drv->capture.channels_min,
529f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_min);
530db2a4165SFrank Mandarino 		runtime->hw.channels_max =
531f0fba2adSLiam Girdwood 			min(codec_dai_drv->capture.channels_max,
532f0fba2adSLiam Girdwood 				cpu_dai_drv->capture.channels_max);
533cb666e5bSLiam Girdwood 		runtime->hw.formats =
534f0fba2adSLiam Girdwood 			codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
535cb666e5bSLiam Girdwood 		runtime->hw.rates =
536f0fba2adSLiam Girdwood 			codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
537f0fba2adSLiam Girdwood 		if (codec_dai_drv->capture.rates
538d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
539f0fba2adSLiam Girdwood 			runtime->hw.rates |= cpu_dai_drv->capture.rates;
540f0fba2adSLiam Girdwood 		if (cpu_dai_drv->capture.rates
541d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
542f0fba2adSLiam Girdwood 			runtime->hw.rates |= codec_dai_drv->capture.rates;
543db2a4165SFrank Mandarino 	}
544db2a4165SFrank Mandarino 
545db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
546db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
547db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
548cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
549bb1c0478SJassi Brar 		goto config_err;
550db2a4165SFrank Mandarino 	}
551db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
552db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
553cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
554bb1c0478SJassi Brar 		goto config_err;
555db2a4165SFrank Mandarino 	}
556db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
557db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
558cb666e5bSLiam Girdwood 				codec_dai->name, cpu_dai->name);
559bb1c0478SJassi Brar 		goto config_err;
560db2a4165SFrank Mandarino 	}
561db2a4165SFrank Mandarino 
56206f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
56306f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
56406f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
56506f409d7SMark Brown 		if (ret != 0)
566bb1c0478SJassi Brar 			goto config_err;
56706f409d7SMark Brown 	}
56806f409d7SMark Brown 
569f0fba2adSLiam Girdwood 	pr_debug("asoc: %s <-> %s info:\n",
570f0fba2adSLiam Girdwood 			codec_dai->name, cpu_dai->name);
571f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
572f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
573b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
574f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
575b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
576db2a4165SFrank Mandarino 
57714dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
578f0fba2adSLiam Girdwood 		cpu_dai->playback_active++;
579f0fba2adSLiam Girdwood 		codec_dai->playback_active++;
58014dc5734SJassi Brar 	} else {
581f0fba2adSLiam Girdwood 		cpu_dai->capture_active++;
582f0fba2adSLiam Girdwood 		codec_dai->capture_active++;
58314dc5734SJassi Brar 	}
58414dc5734SJassi Brar 	cpu_dai->active++;
58514dc5734SJassi Brar 	codec_dai->active++;
586f0fba2adSLiam Girdwood 	rtd->codec->active++;
587db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
588db2a4165SFrank Mandarino 	return 0;
589db2a4165SFrank Mandarino 
590bb1c0478SJassi Brar config_err:
591f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
592f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
593db2a4165SFrank Mandarino 
594bb1c0478SJassi Brar machine_err:
595f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
596f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
597bb1c0478SJassi Brar 
598cb666e5bSLiam Girdwood codec_dai_err:
599f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
600f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
601db2a4165SFrank Mandarino 
602db2a4165SFrank Mandarino platform_err:
603f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
604f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
605db2a4165SFrank Mandarino out:
606db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
607db2a4165SFrank Mandarino 	return ret;
608db2a4165SFrank Mandarino }
609db2a4165SFrank Mandarino 
610db2a4165SFrank Mandarino /*
6113a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
612db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
613db2a4165SFrank Mandarino  * due to DAPM power cycling.
614db2a4165SFrank Mandarino  */
6154484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
616db2a4165SFrank Mandarino {
617f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
618f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
619f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
620db2a4165SFrank Mandarino 
621db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
622db2a4165SFrank Mandarino 
623f24368c2SMark Brown 	pr_debug("pop wq checking: %s status: %s waiting: %s\n",
624f0fba2adSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
625f0fba2adSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
626db2a4165SFrank Mandarino 		 codec_dai->pop_wait ? "yes" : "no");
627db2a4165SFrank Mandarino 
628db2a4165SFrank Mandarino 	/* are we waiting on this codec DAI stream */
629db2a4165SFrank Mandarino 	if (codec_dai->pop_wait == 1) {
630db2a4165SFrank Mandarino 		codec_dai->pop_wait = 0;
631f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
632f0fba2adSLiam Girdwood 			codec_dai->driver->playback.stream_name,
633db2a4165SFrank Mandarino 			SND_SOC_DAPM_STREAM_STOP);
634db2a4165SFrank Mandarino 	}
635f0fba2adSLiam Girdwood 
636db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
637db2a4165SFrank Mandarino }
638db2a4165SFrank Mandarino 
639db2a4165SFrank Mandarino /*
640db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
641db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
642db2a4165SFrank Mandarino  * shutdown.
643db2a4165SFrank Mandarino  */
644db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
645db2a4165SFrank Mandarino {
646db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
647f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
648f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
649f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
650f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
651db2a4165SFrank Mandarino 
652db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
653db2a4165SFrank Mandarino 
65414dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
655f0fba2adSLiam Girdwood 		cpu_dai->playback_active--;
656f0fba2adSLiam Girdwood 		codec_dai->playback_active--;
65714dc5734SJassi Brar 	} else {
658f0fba2adSLiam Girdwood 		cpu_dai->capture_active--;
659f0fba2adSLiam Girdwood 		codec_dai->capture_active--;
660db2a4165SFrank Mandarino 	}
66114dc5734SJassi Brar 
66214dc5734SJassi Brar 	cpu_dai->active--;
66314dc5734SJassi Brar 	codec_dai->active--;
664db2a4165SFrank Mandarino 	codec->active--;
665db2a4165SFrank Mandarino 
6666010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
6676010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
6686010b2daSMark Brown 	 */
6696010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6706010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
6716010b2daSMark Brown 
672f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
673f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
674db2a4165SFrank Mandarino 
675f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->shutdown)
676f0fba2adSLiam Girdwood 		codec_dai->driver->ops->shutdown(substream, codec_dai);
677db2a4165SFrank Mandarino 
678f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
679f0fba2adSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
680db2a4165SFrank Mandarino 
681f0fba2adSLiam Girdwood 	if (platform->driver->ops->close)
682f0fba2adSLiam Girdwood 		platform->driver->ops->close(substream);
683f0fba2adSLiam Girdwood 	cpu_dai->runtime = NULL;
684db2a4165SFrank Mandarino 
685db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
686db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
687cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
688f0fba2adSLiam Girdwood 		schedule_delayed_work(&rtd->delayed_work,
689f0fba2adSLiam Girdwood 			msecs_to_jiffies(rtd->pmdown_time));
690db2a4165SFrank Mandarino 	} else {
691db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
692f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
693f0fba2adSLiam Girdwood 			codec_dai->driver->capture.stream_name,
6940b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
695db2a4165SFrank Mandarino 	}
696db2a4165SFrank Mandarino 
697db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
698db2a4165SFrank Mandarino 	return 0;
699db2a4165SFrank Mandarino }
700db2a4165SFrank Mandarino 
701db2a4165SFrank Mandarino /*
702db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
703db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
704db2a4165SFrank Mandarino  * it can refer to the runtime info.
705db2a4165SFrank Mandarino  */
706db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
707db2a4165SFrank Mandarino {
708db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
709f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
710f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
711f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
712db2a4165SFrank Mandarino 	int ret = 0;
713db2a4165SFrank Mandarino 
714db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
715cb666e5bSLiam Girdwood 
716f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
717f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
718cb666e5bSLiam Girdwood 		if (ret < 0) {
719cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
720cb666e5bSLiam Girdwood 			goto out;
721cb666e5bSLiam Girdwood 		}
722cb666e5bSLiam Girdwood 	}
723cb666e5bSLiam Girdwood 
724f0fba2adSLiam Girdwood 	if (platform->driver->ops->prepare) {
725f0fba2adSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
726a71a468aSLiam Girdwood 		if (ret < 0) {
727a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
728db2a4165SFrank Mandarino 			goto out;
729db2a4165SFrank Mandarino 		}
730a71a468aSLiam Girdwood 	}
731db2a4165SFrank Mandarino 
732f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->prepare) {
733f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->prepare(substream, codec_dai);
734a71a468aSLiam Girdwood 		if (ret < 0) {
735a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
736db2a4165SFrank Mandarino 			goto out;
737db2a4165SFrank Mandarino 		}
738a71a468aSLiam Girdwood 	}
739db2a4165SFrank Mandarino 
740f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->prepare) {
741f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
742cb666e5bSLiam Girdwood 		if (ret < 0) {
743cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
744cb666e5bSLiam Girdwood 			goto out;
745cb666e5bSLiam Girdwood 		}
746cb666e5bSLiam Girdwood 	}
747db2a4165SFrank Mandarino 
748d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
749d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
750d45f6219SMark Brown 	    codec_dai->pop_wait) {
751cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
752f0fba2adSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
753db2a4165SFrank Mandarino 	}
754db2a4165SFrank Mandarino 
755db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
756f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
757f0fba2adSLiam Girdwood 					  codec_dai->driver->playback.stream_name,
758db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
759db2a4165SFrank Mandarino 	else
760f0fba2adSLiam Girdwood 		snd_soc_dapm_stream_event(rtd,
761f0fba2adSLiam Girdwood 					  codec_dai->driver->capture.stream_name,
762db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
7638c6529dbSLiam Girdwood 
7648c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
765db2a4165SFrank Mandarino 
766db2a4165SFrank Mandarino out:
767db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
768db2a4165SFrank Mandarino 	return ret;
769db2a4165SFrank Mandarino }
770db2a4165SFrank Mandarino 
771db2a4165SFrank Mandarino /*
772db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
773db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
774db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
775db2a4165SFrank Mandarino  */
776db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
777db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
778db2a4165SFrank Mandarino {
779db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
780f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
781f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
782f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
783db2a4165SFrank Mandarino 	int ret = 0;
784db2a4165SFrank Mandarino 
785db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
786db2a4165SFrank Mandarino 
787f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
788f0fba2adSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
789cb666e5bSLiam Girdwood 		if (ret < 0) {
790cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
791db2a4165SFrank Mandarino 			goto out;
792db2a4165SFrank Mandarino 		}
793cb666e5bSLiam Girdwood 	}
794db2a4165SFrank Mandarino 
795f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_params) {
796f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
797db2a4165SFrank Mandarino 		if (ret < 0) {
798db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
799cb666e5bSLiam Girdwood 				codec_dai->name);
800cb666e5bSLiam Girdwood 			goto codec_err;
801db2a4165SFrank Mandarino 		}
802db2a4165SFrank Mandarino 	}
803db2a4165SFrank Mandarino 
804f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_params) {
805f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
806db2a4165SFrank Mandarino 		if (ret < 0) {
8073ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
808cb666e5bSLiam Girdwood 				cpu_dai->name);
809db2a4165SFrank Mandarino 			goto interface_err;
810db2a4165SFrank Mandarino 		}
811db2a4165SFrank Mandarino 	}
812db2a4165SFrank Mandarino 
813f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_params) {
814f0fba2adSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
815db2a4165SFrank Mandarino 		if (ret < 0) {
8163ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
817db2a4165SFrank Mandarino 				platform->name);
818db2a4165SFrank Mandarino 			goto platform_err;
819db2a4165SFrank Mandarino 		}
820db2a4165SFrank Mandarino 	}
821db2a4165SFrank Mandarino 
822f0fba2adSLiam Girdwood 	rtd->rate = params_rate(params);
82306f409d7SMark Brown 
824db2a4165SFrank Mandarino out:
825db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
826db2a4165SFrank Mandarino 	return ret;
827db2a4165SFrank Mandarino 
828db2a4165SFrank Mandarino platform_err:
829f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
830f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
831db2a4165SFrank Mandarino 
832db2a4165SFrank Mandarino interface_err:
833f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
834f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
835cb666e5bSLiam Girdwood 
836cb666e5bSLiam Girdwood codec_err:
837f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
838f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
839db2a4165SFrank Mandarino 
840db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
841db2a4165SFrank Mandarino 	return ret;
842db2a4165SFrank Mandarino }
843db2a4165SFrank Mandarino 
844db2a4165SFrank Mandarino /*
845db2a4165SFrank Mandarino  * Free's resources allocated by hw_params, can be called multiple times
846db2a4165SFrank Mandarino  */
847db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
848db2a4165SFrank Mandarino {
849db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
850f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
851f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
852f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
853f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
854db2a4165SFrank Mandarino 
855db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
856db2a4165SFrank Mandarino 
857db2a4165SFrank Mandarino 	/* apply codec digital mute */
8588c6529dbSLiam Girdwood 	if (!codec->active)
8598c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
860db2a4165SFrank Mandarino 
861db2a4165SFrank Mandarino 	/* free any machine hw params */
862f0fba2adSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
863f0fba2adSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
864db2a4165SFrank Mandarino 
865db2a4165SFrank Mandarino 	/* free any DMA resources */
866f0fba2adSLiam Girdwood 	if (platform->driver->ops->hw_free)
867f0fba2adSLiam Girdwood 		platform->driver->ops->hw_free(substream);
868db2a4165SFrank Mandarino 
869db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
870f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->hw_free)
871f0fba2adSLiam Girdwood 		codec_dai->driver->ops->hw_free(substream, codec_dai);
872db2a4165SFrank Mandarino 
873f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->hw_free)
874f0fba2adSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
875db2a4165SFrank Mandarino 
876db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
877db2a4165SFrank Mandarino 	return 0;
878db2a4165SFrank Mandarino }
879db2a4165SFrank Mandarino 
880db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
881db2a4165SFrank Mandarino {
882db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
883f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
884f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
885f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
886db2a4165SFrank Mandarino 	int ret;
887db2a4165SFrank Mandarino 
888f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->trigger) {
889f0fba2adSLiam Girdwood 		ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
890db2a4165SFrank Mandarino 		if (ret < 0)
891db2a4165SFrank Mandarino 			return ret;
892db2a4165SFrank Mandarino 	}
893db2a4165SFrank Mandarino 
894f0fba2adSLiam Girdwood 	if (platform->driver->ops->trigger) {
895f0fba2adSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
896db2a4165SFrank Mandarino 		if (ret < 0)
897db2a4165SFrank Mandarino 			return ret;
898db2a4165SFrank Mandarino 	}
899db2a4165SFrank Mandarino 
900f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->trigger) {
901f0fba2adSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
902db2a4165SFrank Mandarino 		if (ret < 0)
903db2a4165SFrank Mandarino 			return ret;
904db2a4165SFrank Mandarino 	}
905db2a4165SFrank Mandarino 	return 0;
906db2a4165SFrank Mandarino }
907db2a4165SFrank Mandarino 
908377b6f62SPeter Ujfalusi /*
909377b6f62SPeter Ujfalusi  * soc level wrapper for pointer callback
910258020d0SPeter Ujfalusi  * If cpu_dai, codec_dai, platform driver has the delay callback, than
911258020d0SPeter Ujfalusi  * the runtime->delay will be updated accordingly.
912377b6f62SPeter Ujfalusi  */
913377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
914377b6f62SPeter Ujfalusi {
915377b6f62SPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
916f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
917f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
918f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
919258020d0SPeter Ujfalusi 	struct snd_pcm_runtime *runtime = substream->runtime;
920377b6f62SPeter Ujfalusi 	snd_pcm_uframes_t offset = 0;
921258020d0SPeter Ujfalusi 	snd_pcm_sframes_t delay = 0;
922377b6f62SPeter Ujfalusi 
923f0fba2adSLiam Girdwood 	if (platform->driver->ops->pointer)
924f0fba2adSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
925377b6f62SPeter Ujfalusi 
926f0fba2adSLiam Girdwood 	if (cpu_dai->driver->ops->delay)
927f0fba2adSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
928258020d0SPeter Ujfalusi 
929f0fba2adSLiam Girdwood 	if (codec_dai->driver->ops->delay)
930f0fba2adSLiam Girdwood 		delay += codec_dai->driver->ops->delay(substream, codec_dai);
931258020d0SPeter Ujfalusi 
932f0fba2adSLiam Girdwood 	if (platform->driver->delay)
933f0fba2adSLiam Girdwood 		delay += platform->driver->delay(substream, codec_dai);
934258020d0SPeter Ujfalusi 
935258020d0SPeter Ujfalusi 	runtime->delay = delay;
936258020d0SPeter Ujfalusi 
937377b6f62SPeter Ujfalusi 	return offset;
938377b6f62SPeter Ujfalusi }
939377b6f62SPeter Ujfalusi 
940db2a4165SFrank Mandarino /* ASoC PCM operations */
941db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
942db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
943db2a4165SFrank Mandarino 	.close		= soc_codec_close,
944db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
945db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
946db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
947db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
948377b6f62SPeter Ujfalusi 	.pointer	= soc_pcm_pointer,
949db2a4165SFrank Mandarino };
950db2a4165SFrank Mandarino 
951db2a4165SFrank Mandarino #ifdef CONFIG_PM
952db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
953416356fcSMark Brown static int soc_suspend(struct device *dev)
954db2a4165SFrank Mandarino {
955416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
956f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
957db2a4165SFrank Mandarino 	int i;
958db2a4165SFrank Mandarino 
959e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
960e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
961e3509ff0SDaniel Mack 	 */
962f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
963e3509ff0SDaniel Mack 		return 0;
964e3509ff0SDaniel Mack 
9656ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
9666ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
9676ed25978SAndy Green 	 */
968f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
969f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
970f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
9716ed25978SAndy Green 
9726ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
973f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
9746ed25978SAndy Green 
975db2a4165SFrank Mandarino 	/* mute any active DAC's */
976f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
977f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
978f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
9793efab7dcSMark Brown 
980f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
9813efab7dcSMark Brown 			continue;
9823efab7dcSMark Brown 
983f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
984f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
985db2a4165SFrank Mandarino 	}
986db2a4165SFrank Mandarino 
9874ccab3e7SLiam Girdwood 	/* suspend all pcms */
988f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
989f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
9903efab7dcSMark Brown 			continue;
9913efab7dcSMark Brown 
992f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
9933efab7dcSMark Brown 	}
9944ccab3e7SLiam Girdwood 
99587506549SMark Brown 	if (card->suspend_pre)
996416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
997db2a4165SFrank Mandarino 
998f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
999f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1000f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
10013efab7dcSMark Brown 
1002f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10033efab7dcSMark Brown 			continue;
10043efab7dcSMark Brown 
1005f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1006f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1007f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
1008f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
1009f0fba2adSLiam Girdwood 			platform->suspended = 1;
1010f0fba2adSLiam Girdwood 		}
1011db2a4165SFrank Mandarino 	}
1012db2a4165SFrank Mandarino 
1013db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
1014f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1015f0fba2adSLiam Girdwood 		run_delayed_work(&card->rtd[i].delayed_work);
1016f0fba2adSLiam Girdwood 		card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
1017f0fba2adSLiam Girdwood 	}
1018db2a4165SFrank Mandarino 
1019f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1020f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
10213efab7dcSMark Brown 
1022f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10233efab7dcSMark Brown 			continue;
10243efab7dcSMark Brown 
1025f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1026f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1027db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1028f0fba2adSLiam Girdwood 
1029f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1030f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1031db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
1032db2a4165SFrank Mandarino 	}
1033db2a4165SFrank Mandarino 
1034f0fba2adSLiam Girdwood 	/* suspend all CODECs */
1035f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1036f0fba2adSLiam Girdwood 		struct snd_soc_codec *codec = card->rtd[i].codec;
10371547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
10381547aba9SMark Brown 		 * bias _ON and should not be suspended. */
1039f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
10401547aba9SMark Brown 			switch (codec->bias_level) {
10411547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
10421547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1043f0fba2adSLiam Girdwood 				codec->driver->suspend(codec, PMSG_SUSPEND);
1044f0fba2adSLiam Girdwood 				codec->suspended = 1;
10451547aba9SMark Brown 				break;
10461547aba9SMark Brown 			default:
1047f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
10481547aba9SMark Brown 				break;
10491547aba9SMark Brown 			}
10501547aba9SMark Brown 		}
1051f0fba2adSLiam Girdwood 	}
1052db2a4165SFrank Mandarino 
1053f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1054f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
10553efab7dcSMark Brown 
1056f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10573efab7dcSMark Brown 			continue;
10583efab7dcSMark Brown 
1059f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1060f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
1061db2a4165SFrank Mandarino 	}
1062db2a4165SFrank Mandarino 
106387506549SMark Brown 	if (card->suspend_post)
1064416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
1065db2a4165SFrank Mandarino 
1066db2a4165SFrank Mandarino 	return 0;
1067db2a4165SFrank Mandarino }
1068db2a4165SFrank Mandarino 
10696ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
10706ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
10716ed25978SAndy Green  */
10726ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
1073db2a4165SFrank Mandarino {
1074f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
1075f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
1076f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
1077db2a4165SFrank Mandarino 	int i;
1078db2a4165SFrank Mandarino 
10796ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
10806ed25978SAndy Green 	 * so userspace apps are blocked from touching us
10816ed25978SAndy Green 	 */
10826ed25978SAndy Green 
1083f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
10846ed25978SAndy Green 
10859949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
1086f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
10879949788bSMark Brown 
108887506549SMark Brown 	if (card->resume_pre)
108987506549SMark Brown 		card->resume_pre(pdev);
1090db2a4165SFrank Mandarino 
1091f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
1092f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1093f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
10943efab7dcSMark Brown 
1095f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
10963efab7dcSMark Brown 			continue;
10973efab7dcSMark Brown 
1098f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1099f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1100db2a4165SFrank Mandarino 	}
1101db2a4165SFrank Mandarino 
1102f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1103f0fba2adSLiam Girdwood 		struct snd_soc_codec *codec = card->rtd[i].codec;
11041547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
11051547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
11061547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
11071547aba9SMark Brown 		 */
1108f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
11091547aba9SMark Brown 			switch (codec->bias_level) {
11101547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
11111547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
1112f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
1113f0fba2adSLiam Girdwood 				codec->suspended = 0;
11141547aba9SMark Brown 				break;
11151547aba9SMark Brown 			default:
1116f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
11171547aba9SMark Brown 				break;
11181547aba9SMark Brown 			}
11191547aba9SMark Brown 		}
1120f0fba2adSLiam Girdwood 	}
1121db2a4165SFrank Mandarino 
1122f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1123f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
11243efab7dcSMark Brown 
1125f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11263efab7dcSMark Brown 			continue;
11273efab7dcSMark Brown 
1128f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
1129f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1130db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1131f0fba2adSLiam Girdwood 
1132f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
1133f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1134db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1135db2a4165SFrank Mandarino 	}
1136db2a4165SFrank Mandarino 
11373ff3f64bSMark Brown 	/* unmute any active DACs */
1138f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1139f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1140f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
11413efab7dcSMark Brown 
1142f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11433efab7dcSMark Brown 			continue;
11443efab7dcSMark Brown 
1145f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
1146f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
1147db2a4165SFrank Mandarino 	}
1148db2a4165SFrank Mandarino 
1149f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1150f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1151f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
11523efab7dcSMark Brown 
1153f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
11543efab7dcSMark Brown 			continue;
11553efab7dcSMark Brown 
1156f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1157f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
1158f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
1159f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
1160f0fba2adSLiam Girdwood 			platform->suspended = 0;
1161f0fba2adSLiam Girdwood 		}
1162db2a4165SFrank Mandarino 	}
1163db2a4165SFrank Mandarino 
116487506549SMark Brown 	if (card->resume_post)
116587506549SMark Brown 		card->resume_post(pdev);
1166db2a4165SFrank Mandarino 
1167f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
11686ed25978SAndy Green 
11696ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
1170f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
11716ed25978SAndy Green }
11726ed25978SAndy Green 
11736ed25978SAndy Green /* powers up audio subsystem after a suspend */
1174416356fcSMark Brown static int soc_resume(struct device *dev)
11756ed25978SAndy Green {
1176416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1177f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1178f0fba2adSLiam Girdwood 	int i;
1179b9dd94a8SPeter Ujfalusi 
118064ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
118164ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
118264ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
118364ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
118464ab9baaSMark Brown 	 */
1185f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1186f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1187f0fba2adSLiam Girdwood 		if (cpu_dai->driver->ac97_control) {
1188f0fba2adSLiam Girdwood 			dev_dbg(dev, "Resuming AC97 immediately\n");
118964ab9baaSMark Brown 			soc_resume_deferred(&card->deferred_resume_work);
119064ab9baaSMark Brown 		} else {
1191f0fba2adSLiam Girdwood 			dev_dbg(dev, "Scheduling resume work\n");
11926308419aSMark Brown 			if (!schedule_work(&card->deferred_resume_work))
1193f0fba2adSLiam Girdwood 				dev_err(dev, "resume work item may be lost\n");
1194f0fba2adSLiam Girdwood 		}
119564ab9baaSMark Brown 	}
11966ed25978SAndy Green 
1197db2a4165SFrank Mandarino 	return 0;
1198db2a4165SFrank Mandarino }
1199db2a4165SFrank Mandarino #else
1200db2a4165SFrank Mandarino #define soc_suspend	NULL
1201db2a4165SFrank Mandarino #define soc_resume	NULL
1202db2a4165SFrank Mandarino #endif
1203db2a4165SFrank Mandarino 
120402a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
120502a06d30SBarry Song };
120602a06d30SBarry Song 
1207f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1208db2a4165SFrank Mandarino {
1209f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1210f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1211fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1212435c5e25SMark Brown 	struct snd_soc_platform *platform;
1213f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
1214db2a4165SFrank Mandarino 
1215f0fba2adSLiam Girdwood 	if (rtd->complete)
1216f0fba2adSLiam Girdwood 		return 1;
1217f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
12186308419aSMark Brown 
1219f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
1220f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
1221f0fba2adSLiam Girdwood 		goto find_codec;
1222435c5e25SMark Brown 	}
1223f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1224f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
1225f0fba2adSLiam Girdwood 		if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1226f0fba2adSLiam Girdwood 
1227f0fba2adSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
1228f0fba2adSLiam Girdwood 				return -ENODEV;
1229f0fba2adSLiam Girdwood 
1230f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
1231f0fba2adSLiam Girdwood 			goto find_codec;
1232f0fba2adSLiam Girdwood 		}
1233f0fba2adSLiam Girdwood 	}
1234f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
1235f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
1236f0fba2adSLiam Girdwood 
1237f0fba2adSLiam Girdwood find_codec:
1238f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
1239f0fba2adSLiam Girdwood 	if (rtd->codec) {
1240f0fba2adSLiam Girdwood 		goto find_platform;
1241c5af3a2eSMark Brown 	}
1242c5af3a2eSMark Brown 
1243f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
1244f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
1245f0fba2adSLiam Girdwood 		if (!strcmp(codec->name, dai_link->codec_name)) {
1246f0fba2adSLiam Girdwood 			rtd->codec = codec;
1247f0fba2adSLiam Girdwood 
1248f0fba2adSLiam Girdwood 			if (!try_module_get(codec->dev->driver->owner))
1249f0fba2adSLiam Girdwood 				return -ENODEV;
1250f0fba2adSLiam Girdwood 
1251f0fba2adSLiam Girdwood 			/* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1252f0fba2adSLiam Girdwood 			list_for_each_entry(codec_dai, &dai_list, list) {
1253f0fba2adSLiam Girdwood 				if (codec->dev == codec_dai->dev &&
1254f0fba2adSLiam Girdwood 						!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1255f0fba2adSLiam Girdwood 					rtd->codec_dai = codec_dai;
1256f0fba2adSLiam Girdwood 					goto find_platform;
1257435c5e25SMark Brown 				}
1258f0fba2adSLiam Girdwood 			}
1259f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1260f0fba2adSLiam Girdwood 					dai_link->codec_dai_name);
1261f0fba2adSLiam Girdwood 
1262f0fba2adSLiam Girdwood 			goto find_platform;
1263f0fba2adSLiam Girdwood 		}
1264f0fba2adSLiam Girdwood 	}
1265f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
1266f0fba2adSLiam Girdwood 			dai_link->codec_name);
1267f0fba2adSLiam Girdwood 
1268f0fba2adSLiam Girdwood find_platform:
1269f0fba2adSLiam Girdwood 	/* do we already have the CODEC DAI for this link ? */
1270f0fba2adSLiam Girdwood 	if (rtd->platform) {
1271f0fba2adSLiam Girdwood 		goto out;
1272f0fba2adSLiam Girdwood 	}
1273f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
1274f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
1275f0fba2adSLiam Girdwood 		if (!strcmp(platform->name, dai_link->platform_name)) {
1276f0fba2adSLiam Girdwood 
1277f0fba2adSLiam Girdwood 			if (!try_module_get(platform->dev->driver->owner))
1278f0fba2adSLiam Girdwood 				return -ENODEV;
1279f0fba2adSLiam Girdwood 
1280f0fba2adSLiam Girdwood 			rtd->platform = platform;
1281f0fba2adSLiam Girdwood 			goto out;
1282f0fba2adSLiam Girdwood 		}
1283435c5e25SMark Brown 	}
12846b05eda6SMark Brown 
1285f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
1286f0fba2adSLiam Girdwood 			dai_link->platform_name);
1287f0fba2adSLiam Girdwood 	return 0;
1288f0fba2adSLiam Girdwood 
1289f0fba2adSLiam Girdwood out:
1290f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
1291f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1292f0fba2adSLiam Girdwood 		rtd->complete = 1;
1293f0fba2adSLiam Girdwood 		card->num_rtd++;
1294f0fba2adSLiam Girdwood 	}
1295f0fba2adSLiam Girdwood 	return 1;
12966b05eda6SMark Brown }
12976b05eda6SMark Brown 
1298f0fba2adSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1299f0fba2adSLiam Girdwood {
1300f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1301f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1302f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1303f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1304f0fba2adSLiam Girdwood 	int err;
1305f0fba2adSLiam Girdwood 
1306f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1307f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
1308f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1309f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
1310f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
131102a06d30SBarry Song 	}
131202a06d30SBarry Song 
1313f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
1314f0fba2adSLiam Girdwood 	if (codec_dai && codec_dai->probed) {
1315f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1316f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1317f0fba2adSLiam Girdwood 			if (err < 0)
1318f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1319f0fba2adSLiam Girdwood 		}
1320f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1321f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1322f0fba2adSLiam Girdwood 	}
1323f0fba2adSLiam Girdwood 
1324f0fba2adSLiam Girdwood 	/* remove the platform */
1325f0fba2adSLiam Girdwood 	if (platform && platform->probed) {
1326f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
1327f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
1328f0fba2adSLiam Girdwood 			if (err < 0)
1329f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1330f0fba2adSLiam Girdwood 		}
1331f0fba2adSLiam Girdwood 		platform->probed = 0;
1332f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
1333f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
1334f0fba2adSLiam Girdwood 	}
1335f0fba2adSLiam Girdwood 
1336f0fba2adSLiam Girdwood 	/* remove the CODEC */
1337f0fba2adSLiam Girdwood 	if (codec && codec->probed) {
1338f0fba2adSLiam Girdwood 		if (codec->driver->remove) {
1339f0fba2adSLiam Girdwood 			err = codec->driver->remove(codec);
1340f0fba2adSLiam Girdwood 			if (err < 0)
1341f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1342f0fba2adSLiam Girdwood 		}
1343f0fba2adSLiam Girdwood 
1344f0fba2adSLiam Girdwood 		/* Make sure all DAPM widgets are freed */
1345f0fba2adSLiam Girdwood 		snd_soc_dapm_free(codec);
1346f0fba2adSLiam Girdwood 
1347f0fba2adSLiam Girdwood 		soc_cleanup_codec_debugfs(codec);
1348f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1349f0fba2adSLiam Girdwood 		codec->probed = 0;
1350f0fba2adSLiam Girdwood 		list_del(&codec->card_list);
1351f0fba2adSLiam Girdwood 		module_put(codec->dev->driver->owner);
1352f0fba2adSLiam Girdwood 	}
1353f0fba2adSLiam Girdwood 
1354f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
1355f0fba2adSLiam Girdwood 	if (cpu_dai && cpu_dai->probed) {
1356f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1357f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1358f0fba2adSLiam Girdwood 			if (err < 0)
1359f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1360f0fba2adSLiam Girdwood 		}
1361f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1362f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1363f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
1364f0fba2adSLiam Girdwood 	}
1365f0fba2adSLiam Girdwood }
1366f0fba2adSLiam Girdwood 
1367f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1368f0fba2adSLiam Girdwood 
1369f0fba2adSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1370f0fba2adSLiam Girdwood {
1371f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1372f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1373f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1374f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1375f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1376f0fba2adSLiam Girdwood 	int ret;
1377f0fba2adSLiam Girdwood 
1378f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1379f0fba2adSLiam Girdwood 
1380f0fba2adSLiam Girdwood 	/* config components */
1381f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1382f0fba2adSLiam Girdwood 	codec->card = card;
1383f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1384f0fba2adSLiam Girdwood 	rtd->card = card;
1385f0fba2adSLiam Girdwood 	rtd->dev.parent = card->dev;
1386f0fba2adSLiam Girdwood 	codec_dai->card = card;
1387f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1388f0fba2adSLiam Girdwood 
1389f0fba2adSLiam Girdwood 	/* set default power off timeout */
1390f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1391f0fba2adSLiam Girdwood 
1392f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
1393f0fba2adSLiam Girdwood 	if (!cpu_dai->probed) {
1394f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1395f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1396f0fba2adSLiam Girdwood 			if (ret < 0) {
1397f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1398f0fba2adSLiam Girdwood 						cpu_dai->name);
1399f0fba2adSLiam Girdwood 				return ret;
1400f0fba2adSLiam Girdwood 			}
1401f0fba2adSLiam Girdwood 		}
1402f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1403f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1404f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1405f0fba2adSLiam Girdwood 	}
1406f0fba2adSLiam Girdwood 
1407f0fba2adSLiam Girdwood 	/* probe the CODEC */
1408f0fba2adSLiam Girdwood 	if (!codec->probed) {
1409f0fba2adSLiam Girdwood 		if (codec->driver->probe) {
1410f0fba2adSLiam Girdwood 			ret = codec->driver->probe(codec);
1411f0fba2adSLiam Girdwood 			if (ret < 0) {
1412f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1413f0fba2adSLiam Girdwood 						codec->name);
1414f0fba2adSLiam Girdwood 				return ret;
1415f0fba2adSLiam Girdwood 			}
1416f0fba2adSLiam Girdwood 		}
141713cb61f8SMark Brown 
141813cb61f8SMark Brown 		soc_init_codec_debugfs(codec);
141913cb61f8SMark Brown 
1420f0fba2adSLiam Girdwood 		/* mark codec as probed and add to card codec list */
1421f0fba2adSLiam Girdwood 		codec->probed = 1;
1422f0fba2adSLiam Girdwood 		list_add(&codec->card_list, &card->codec_dev_list);
1423f0fba2adSLiam Girdwood 	}
1424f0fba2adSLiam Girdwood 
1425f0fba2adSLiam Girdwood 	/* probe the platform */
1426f0fba2adSLiam Girdwood 	if (!platform->probed) {
1427f0fba2adSLiam Girdwood 		if (platform->driver->probe) {
1428f0fba2adSLiam Girdwood 			ret = platform->driver->probe(platform);
1429f0fba2adSLiam Girdwood 			if (ret < 0) {
1430f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe platform %s\n",
1431f0fba2adSLiam Girdwood 						platform->name);
1432f0fba2adSLiam Girdwood 				return ret;
1433f0fba2adSLiam Girdwood 			}
1434f0fba2adSLiam Girdwood 		}
1435f0fba2adSLiam Girdwood 		/* mark platform as probed and add to card platform list */
1436f0fba2adSLiam Girdwood 		platform->probed = 1;
1437f0fba2adSLiam Girdwood 		list_add(&platform->card_list, &card->platform_dev_list);
1438f0fba2adSLiam Girdwood 	}
1439f0fba2adSLiam Girdwood 
1440f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
1441f0fba2adSLiam Girdwood 	if (!codec_dai->probed) {
1442f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1443f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1444f0fba2adSLiam Girdwood 			if (ret < 0) {
1445f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1446f0fba2adSLiam Girdwood 						codec_dai->name);
1447f0fba2adSLiam Girdwood 				return ret;
1448f0fba2adSLiam Girdwood 			}
1449f0fba2adSLiam Girdwood 		}
1450f0fba2adSLiam Girdwood 
1451f0fba2adSLiam Girdwood 		/* mark cpu_dai as probed and add to card cpu_dai list */
1452f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1453f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1454f0fba2adSLiam Girdwood 	}
1455f0fba2adSLiam Girdwood 
1456f0fba2adSLiam Girdwood 	/* DAPM dai link stream work */
1457f0fba2adSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1458f0fba2adSLiam Girdwood 
1459f0fba2adSLiam Girdwood 	/* now that all clients have probed, initialise the DAI link */
1460f0fba2adSLiam Girdwood 	if (dai_link->init) {
1461f0fba2adSLiam Girdwood 		ret = dai_link->init(rtd);
1462f0fba2adSLiam Girdwood 		if (ret < 0) {
1463f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1464f0fba2adSLiam Girdwood 			return ret;
1465f0fba2adSLiam Girdwood 		}
1466f0fba2adSLiam Girdwood 	}
1467f0fba2adSLiam Girdwood 
1468f0fba2adSLiam Girdwood 	/* Make sure all DAPM widgets are instantiated */
1469f0fba2adSLiam Girdwood 	snd_soc_dapm_new_widgets(codec);
1470f0fba2adSLiam Girdwood 	snd_soc_dapm_sync(codec);
1471f0fba2adSLiam Girdwood 
1472f0fba2adSLiam Girdwood 	/* register the rtd device */
1473f0fba2adSLiam Girdwood 	rtd->dev.release = rtd_release;
1474f0fba2adSLiam Girdwood 	rtd->dev.init_name = dai_link->name;
1475f0fba2adSLiam Girdwood 	ret = device_register(&rtd->dev);
1476f0fba2adSLiam Girdwood 	if (ret < 0) {
1477f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1478f0fba2adSLiam Girdwood 		return ret;
1479f0fba2adSLiam Girdwood 	}
1480f0fba2adSLiam Girdwood 
1481f0fba2adSLiam Girdwood 	rtd->dev_registered = 1;
1482f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1483f0fba2adSLiam Girdwood 	if (ret < 0)
1484f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1485f0fba2adSLiam Girdwood 
1486f0fba2adSLiam Girdwood 	/* add DAPM sysfs entries for this codec */
1487f0fba2adSLiam Girdwood 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1488f0fba2adSLiam Girdwood 	if (ret < 0)
1489f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1490f0fba2adSLiam Girdwood 
1491f0fba2adSLiam Girdwood 	/* add codec sysfs entries */
1492f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1493f0fba2adSLiam Girdwood 	if (ret < 0)
1494f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1495f0fba2adSLiam Girdwood 
1496f0fba2adSLiam Girdwood 	/* create the pcm */
1497f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1498f0fba2adSLiam Girdwood 	if (ret < 0) {
1499f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1500f0fba2adSLiam Girdwood 		return ret;
1501f0fba2adSLiam Girdwood 	}
1502f0fba2adSLiam Girdwood 
1503f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1504f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1505f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1506f0fba2adSLiam Girdwood 
1507f0fba2adSLiam Girdwood 	return 0;
1508f0fba2adSLiam Girdwood }
1509f0fba2adSLiam Girdwood 
1510f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1511f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1512f0fba2adSLiam Girdwood {
1513f0fba2adSLiam Girdwood 	int ret;
1514f0fba2adSLiam Girdwood 
1515f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1516f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
15176b05eda6SMark Brown 	 */
1518f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
15190562f788SMika Westerberg 		/*
15200562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
15210562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
15220562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
15230562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
15240562f788SMika Westerberg 		 *
15250562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
15260562f788SMika Westerberg 		 */
15270562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
15280562f788SMika Westerberg 			return 0;
1529f0fba2adSLiam Girdwood 
1530f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1531f0fba2adSLiam Girdwood 		if (ret < 0) {
1532f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1533f0fba2adSLiam Girdwood 			return ret;
1534435c5e25SMark Brown 		}
1535435c5e25SMark Brown 
1536f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1537f0fba2adSLiam Girdwood 	}
1538f0fba2adSLiam Girdwood 	return 0;
1539f0fba2adSLiam Girdwood }
1540435c5e25SMark Brown 
1541f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1542f0fba2adSLiam Girdwood {
1543f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1544f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1545f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1546f0fba2adSLiam Girdwood 	}
1547f0fba2adSLiam Girdwood }
1548f0fba2adSLiam Girdwood #endif
1549435c5e25SMark Brown 
1550f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1551f0fba2adSLiam Girdwood {
1552f0fba2adSLiam Girdwood 	struct platform_device *pdev = to_platform_device(card->dev);
1553f0fba2adSLiam Girdwood 	int ret, i;
155496dd3622SMark Brown 
1555f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1556f0fba2adSLiam Girdwood 
1557f0fba2adSLiam Girdwood 	if (card->instantiated) {
1558f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1559435c5e25SMark Brown 		return;
1560db2a4165SFrank Mandarino 	}
1561db2a4165SFrank Mandarino 
1562f0fba2adSLiam Girdwood 	/* bind DAIs */
1563f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1564f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1565f0fba2adSLiam Girdwood 
1566f0fba2adSLiam Girdwood 	/* bind completed ? */
1567f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1568f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1569f0fba2adSLiam Girdwood 		return;
1570db2a4165SFrank Mandarino 	}
1571db2a4165SFrank Mandarino 
1572f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1573f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1574f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1575f0fba2adSLiam Girdwood 	if (ret < 0) {
1576f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1577f0fba2adSLiam Girdwood 			card->name);
1578f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1579f0fba2adSLiam Girdwood 		return;
1580db2a4165SFrank Mandarino 	}
1581f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1582db2a4165SFrank Mandarino 
15831301a964SRandy Dunlap #ifdef CONFIG_PM
15846ed25978SAndy Green 	/* deferred resume work */
15856308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
15861301a964SRandy Dunlap #endif
15876ed25978SAndy Green 
1588f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1589f0fba2adSLiam Girdwood 	if (card->probe) {
1590f0fba2adSLiam Girdwood 		ret = card->probe(pdev);
1591f0fba2adSLiam Girdwood 		if (ret < 0)
1592f0fba2adSLiam Girdwood 			goto card_probe_error;
1593f0fba2adSLiam Girdwood 	}
1594f0fba2adSLiam Girdwood 
1595fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1596f0fba2adSLiam Girdwood 		ret = soc_probe_dai_link(card, i);
1597fe3e78e0SMark Brown 		if (ret < 0) {
1598321de0d0SMark Brown 			pr_err("asoc: failed to instantiate card %s: %d\n",
1599321de0d0SMark Brown 			       card->name, ret);
1600f0fba2adSLiam Girdwood 			goto probe_dai_err;
1601fe3e78e0SMark Brown 		}
1602fe3e78e0SMark Brown 	}
1603fe3e78e0SMark Brown 
1604f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1605fe3e78e0SMark Brown 		 "%s",  card->name);
1606f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1607f0fba2adSLiam Girdwood 		 "%s", card->name);
1608fe3e78e0SMark Brown 
1609f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1610fe3e78e0SMark Brown 	if (ret < 0) {
1611f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1612f0fba2adSLiam Girdwood 		goto probe_dai_err;
1613fe3e78e0SMark Brown 	}
1614fe3e78e0SMark Brown 
1615fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1616f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1617f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1618f0fba2adSLiam Girdwood 			ret = soc_register_ac97_dai_link(&card->rtd[i]);
1619fe3e78e0SMark Brown 			if (ret < 0) {
1620f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1621f0fba2adSLiam Girdwood 				goto probe_dai_err;
1622fe3e78e0SMark Brown 			}
1623fe3e78e0SMark Brown 		}
1624fe3e78e0SMark Brown #endif
1625fe3e78e0SMark Brown 
1626435c5e25SMark Brown 	card->instantiated = 1;
1627f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1628435c5e25SMark Brown 	return;
1629db2a4165SFrank Mandarino 
1630f0fba2adSLiam Girdwood probe_dai_err:
1631f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1632f0fba2adSLiam Girdwood 		soc_remove_dai_link(card, i);
1633fe3e78e0SMark Brown 
1634f0fba2adSLiam Girdwood card_probe_error:
163587506549SMark Brown 	if (card->remove)
163687506549SMark Brown 		card->remove(pdev);
1637f0fba2adSLiam Girdwood 
1638f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1639f0fba2adSLiam Girdwood 
1640f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1641435c5e25SMark Brown }
1642db2a4165SFrank Mandarino 
1643435c5e25SMark Brown /*
1644421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1645435c5e25SMark Brown  * client_mutex.
1646435c5e25SMark Brown  */
1647435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1648435c5e25SMark Brown {
1649435c5e25SMark Brown 	struct snd_soc_card *card;
1650435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1651435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1652435c5e25SMark Brown }
1653435c5e25SMark Brown 
1654435c5e25SMark Brown /* probes a new socdev */
1655435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1656435c5e25SMark Brown {
1657f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1658435c5e25SMark Brown 	int ret = 0;
1659435c5e25SMark Brown 
1660435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1661435c5e25SMark Brown 	card->dev = &pdev->dev;
1662f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->dai_dev_list);
1663f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->codec_dev_list);
1664f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&card->platform_dev_list);
1665f0fba2adSLiam Girdwood 
1666435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1667435c5e25SMark Brown 	if (ret != 0) {
1668435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1669db2a4165SFrank Mandarino 		return ret;
1670db2a4165SFrank Mandarino 	}
1671db2a4165SFrank Mandarino 
1672435c5e25SMark Brown 	return 0;
1673435c5e25SMark Brown }
1674435c5e25SMark Brown 
1675db2a4165SFrank Mandarino /* removes a socdev */
1676db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1677db2a4165SFrank Mandarino {
1678f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1679db2a4165SFrank Mandarino 	int i;
1680db2a4165SFrank Mandarino 
1681b2dfa62cSGuennadi Liakhovetski 		if (card->instantiated) {
1682965ac42cSLiam Girdwood 
1683f0fba2adSLiam Girdwood 		/* make sure any delayed work runs */
1684f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++) {
1685f0fba2adSLiam Girdwood 			struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1686f0fba2adSLiam Girdwood 			run_delayed_work(&rtd->delayed_work);
1687db2a4165SFrank Mandarino 		}
1688db2a4165SFrank Mandarino 
1689f0fba2adSLiam Girdwood 		/* remove and free each DAI */
1690f0fba2adSLiam Girdwood 		for (i = 0; i < card->num_rtd; i++)
1691f0fba2adSLiam Girdwood 			soc_remove_dai_link(card, i);
1692f0fba2adSLiam Girdwood 
1693f0fba2adSLiam Girdwood 		/* remove the card */
169487506549SMark Brown 		if (card->remove)
169587506549SMark Brown 			card->remove(pdev);
1696f0fba2adSLiam Girdwood 
1697f0fba2adSLiam Girdwood 		kfree(card->rtd);
1698f0fba2adSLiam Girdwood 		snd_card_free(card->snd_card);
1699b2dfa62cSGuennadi Liakhovetski 	}
1700c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1701db2a4165SFrank Mandarino 	return 0;
1702db2a4165SFrank Mandarino }
1703db2a4165SFrank Mandarino 
1704416356fcSMark Brown static int soc_poweroff(struct device *dev)
170551737470SMark Brown {
1706416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
1707f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1708f0fba2adSLiam Girdwood 	int i;
170951737470SMark Brown 
171051737470SMark Brown 	if (!card->instantiated)
1711416356fcSMark Brown 		return 0;
171251737470SMark Brown 
171351737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
171451737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1715f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1716f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1717f0fba2adSLiam Girdwood 		run_delayed_work(&rtd->delayed_work);
1718f0fba2adSLiam Girdwood 	}
171951737470SMark Brown 
1720f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1721416356fcSMark Brown 
1722416356fcSMark Brown 	return 0;
172351737470SMark Brown }
172451737470SMark Brown 
172547145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1726416356fcSMark Brown 	.suspend = soc_suspend,
1727416356fcSMark Brown 	.resume = soc_resume,
1728416356fcSMark Brown 	.poweroff = soc_poweroff,
1729416356fcSMark Brown };
1730416356fcSMark Brown 
1731db2a4165SFrank Mandarino /* ASoC platform driver */
1732db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1733db2a4165SFrank Mandarino 	.driver		= {
1734db2a4165SFrank Mandarino 		.name		= "soc-audio",
17358b45a209SKay Sievers 		.owner		= THIS_MODULE,
1736416356fcSMark Brown 		.pm		= &soc_pm_ops,
1737db2a4165SFrank Mandarino 	},
1738db2a4165SFrank Mandarino 	.probe		= soc_probe,
1739db2a4165SFrank Mandarino 	.remove		= soc_remove,
1740db2a4165SFrank Mandarino };
1741db2a4165SFrank Mandarino 
1742db2a4165SFrank Mandarino /* create a new pcm */
1743f0fba2adSLiam Girdwood static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1744db2a4165SFrank Mandarino {
1745f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1746f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1747f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1748f0fba2adSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1749db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1750db2a4165SFrank Mandarino 	char new_name[64];
1751db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1752db2a4165SFrank Mandarino 
1753db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
175440ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
1755f0fba2adSLiam Girdwood 			rtd->dai_link->stream_name, codec_dai->name, num);
1756db2a4165SFrank Mandarino 
1757f0fba2adSLiam Girdwood 	if (codec_dai->driver->playback.channels_min)
1758db2a4165SFrank Mandarino 		playback = 1;
1759f0fba2adSLiam Girdwood 	if (codec_dai->driver->capture.channels_min)
1760db2a4165SFrank Mandarino 		capture = 1;
1761db2a4165SFrank Mandarino 
1762f0fba2adSLiam Girdwood 	dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1763f0fba2adSLiam Girdwood 	ret = snd_pcm_new(rtd->card->snd_card, new_name,
1764f0fba2adSLiam Girdwood 			num, playback, capture, &pcm);
1765db2a4165SFrank Mandarino 	if (ret < 0) {
1766f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1767db2a4165SFrank Mandarino 		return ret;
1768db2a4165SFrank Mandarino 	}
1769db2a4165SFrank Mandarino 
1770f0fba2adSLiam Girdwood 	rtd->pcm = pcm;
1771db2a4165SFrank Mandarino 	pcm->private_data = rtd;
1772f0fba2adSLiam Girdwood 	soc_pcm_ops.mmap = platform->driver->ops->mmap;
1773f0fba2adSLiam Girdwood 	soc_pcm_ops.pointer = platform->driver->ops->pointer;
1774f0fba2adSLiam Girdwood 	soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1775f0fba2adSLiam Girdwood 	soc_pcm_ops.copy = platform->driver->ops->copy;
1776f0fba2adSLiam Girdwood 	soc_pcm_ops.silence = platform->driver->ops->silence;
1777f0fba2adSLiam Girdwood 	soc_pcm_ops.ack = platform->driver->ops->ack;
1778f0fba2adSLiam Girdwood 	soc_pcm_ops.page = platform->driver->ops->page;
1779db2a4165SFrank Mandarino 
1780db2a4165SFrank Mandarino 	if (playback)
1781db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1782db2a4165SFrank Mandarino 
1783db2a4165SFrank Mandarino 	if (capture)
1784db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1785db2a4165SFrank Mandarino 
1786f0fba2adSLiam Girdwood 	ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
1787db2a4165SFrank Mandarino 	if (ret < 0) {
1788db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1789db2a4165SFrank Mandarino 		return ret;
1790db2a4165SFrank Mandarino 	}
1791db2a4165SFrank Mandarino 
1792f0fba2adSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
1793db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1794db2a4165SFrank Mandarino 		cpu_dai->name);
1795db2a4165SFrank Mandarino 	return ret;
1796db2a4165SFrank Mandarino }
1797db2a4165SFrank Mandarino 
1798096e49d5SMark Brown /**
1799096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1800096e49d5SMark Brown  *
1801096e49d5SMark Brown  * @codec: CODEC to query.
1802096e49d5SMark Brown  * @reg: Register to query.
1803096e49d5SMark Brown  *
1804096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1805096e49d5SMark Brown  */
1806096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1807096e49d5SMark Brown {
1808f0fba2adSLiam Girdwood 	if (codec->driver->volatile_register)
1809f0fba2adSLiam Girdwood 		return codec->driver->volatile_register(reg);
1810096e49d5SMark Brown 	else
1811096e49d5SMark Brown 		return 0;
1812096e49d5SMark Brown }
1813096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1814096e49d5SMark Brown 
1815db2a4165SFrank Mandarino /**
1816db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1817db2a4165SFrank Mandarino  * @codec: audio codec
1818db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1819db2a4165SFrank Mandarino  * @num: AC97 codec number
1820db2a4165SFrank Mandarino  *
1821db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1822db2a4165SFrank Mandarino  */
1823db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1824db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1825db2a4165SFrank Mandarino {
1826db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1827db2a4165SFrank Mandarino 
1828db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1829db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1830db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1831db2a4165SFrank Mandarino 		return -ENOMEM;
1832db2a4165SFrank Mandarino 	}
1833db2a4165SFrank Mandarino 
1834db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1835db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1836db2a4165SFrank Mandarino 		kfree(codec->ac97);
1837db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1838db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1839db2a4165SFrank Mandarino 		return -ENOMEM;
1840db2a4165SFrank Mandarino 	}
1841db2a4165SFrank Mandarino 
1842db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1843db2a4165SFrank Mandarino 	codec->ac97->num = num;
18440562f788SMika Westerberg 
18450562f788SMika Westerberg 	/*
18460562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
18470562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
18480562f788SMika Westerberg 	 */
18490562f788SMika Westerberg 	codec->ac97_created = 1;
18500562f788SMika Westerberg 
1851db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1852db2a4165SFrank Mandarino 	return 0;
1853db2a4165SFrank Mandarino }
1854db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1855db2a4165SFrank Mandarino 
1856db2a4165SFrank Mandarino /**
1857db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1858db2a4165SFrank Mandarino  * @codec: audio codec
1859db2a4165SFrank Mandarino  *
1860db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1861db2a4165SFrank Mandarino  */
1862db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1863db2a4165SFrank Mandarino {
1864db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1865f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1866f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1867f0fba2adSLiam Girdwood #endif
1868db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1869db2a4165SFrank Mandarino 	kfree(codec->ac97);
1870db2a4165SFrank Mandarino 	codec->ac97 = NULL;
18710562f788SMika Westerberg 	codec->ac97_created = 0;
1872db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1873db2a4165SFrank Mandarino }
1874db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1875db2a4165SFrank Mandarino 
1876db2a4165SFrank Mandarino /**
1877db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1878db2a4165SFrank Mandarino  * @codec: audio codec
1879db2a4165SFrank Mandarino  * @reg: codec register
1880db2a4165SFrank Mandarino  * @mask: register mask
1881db2a4165SFrank Mandarino  * @value: new value
1882db2a4165SFrank Mandarino  *
1883db2a4165SFrank Mandarino  * Writes new register value.
1884db2a4165SFrank Mandarino  *
1885db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1886db2a4165SFrank Mandarino  */
1887db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
188846f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1889db2a4165SFrank Mandarino {
1890db2a4165SFrank Mandarino 	int change;
189146f5822fSDaniel Ribeiro 	unsigned int old, new;
1892db2a4165SFrank Mandarino 
1893db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1894db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1895db2a4165SFrank Mandarino 	change = old != new;
1896db2a4165SFrank Mandarino 	if (change)
1897db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1898db2a4165SFrank Mandarino 
1899db2a4165SFrank Mandarino 	return change;
1900db2a4165SFrank Mandarino }
1901db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1902db2a4165SFrank Mandarino 
1903db2a4165SFrank Mandarino /**
19046c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
19056c508c62SEero Nurkkala  * @codec: audio codec
19066c508c62SEero Nurkkala  * @reg: codec register
19076c508c62SEero Nurkkala  * @mask: register mask
19086c508c62SEero Nurkkala  * @value: new value
19096c508c62SEero Nurkkala  *
19106c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
19116c508c62SEero Nurkkala  *
19126c508c62SEero Nurkkala  * Returns 1 for change else 0.
19136c508c62SEero Nurkkala  */
1914dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
19156c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
19166c508c62SEero Nurkkala 			       unsigned int value)
19176c508c62SEero Nurkkala {
19186c508c62SEero Nurkkala 	int change;
19196c508c62SEero Nurkkala 
19206c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
19216c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
19226c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
19236c508c62SEero Nurkkala 
19246c508c62SEero Nurkkala 	return change;
19256c508c62SEero Nurkkala }
1926dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
19276c508c62SEero Nurkkala 
19286c508c62SEero Nurkkala /**
1929db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1930db2a4165SFrank Mandarino  * @codec: audio codec
1931db2a4165SFrank Mandarino  * @reg: codec register
1932db2a4165SFrank Mandarino  * @mask: register mask
1933db2a4165SFrank Mandarino  * @value: new value
1934db2a4165SFrank Mandarino  *
1935db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1936db2a4165SFrank Mandarino  * different from the old value.
1937db2a4165SFrank Mandarino  *
1938db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1939db2a4165SFrank Mandarino  */
1940db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
194146f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1942db2a4165SFrank Mandarino {
1943db2a4165SFrank Mandarino 	int change;
194446f5822fSDaniel Ribeiro 	unsigned int old, new;
1945db2a4165SFrank Mandarino 
1946db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1947db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1948db2a4165SFrank Mandarino 	change = old != new;
1949db2a4165SFrank Mandarino 
1950db2a4165SFrank Mandarino 	return change;
1951db2a4165SFrank Mandarino }
1952db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1953db2a4165SFrank Mandarino 
1954db2a4165SFrank Mandarino /**
1955db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1956db2a4165SFrank Mandarino  * @substream: the pcm substream
1957db2a4165SFrank Mandarino  * @hw: the hardware parameters
1958db2a4165SFrank Mandarino  *
1959db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1960db2a4165SFrank Mandarino  */
1961db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1962db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1963db2a4165SFrank Mandarino {
1964db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1965db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1966db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1967db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1968db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1969db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1970db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1971db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1972db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1973db2a4165SFrank Mandarino 	return 0;
1974db2a4165SFrank Mandarino }
1975db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1976db2a4165SFrank Mandarino 
1977db2a4165SFrank Mandarino /**
1978db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1979db2a4165SFrank Mandarino  * @_template: control template
1980db2a4165SFrank Mandarino  * @data: control private data
1981ac11a2b3SMark Brown  * @long_name: control long name
1982db2a4165SFrank Mandarino  *
1983db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1984db2a4165SFrank Mandarino  *
1985db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1986db2a4165SFrank Mandarino  */
1987db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1988db2a4165SFrank Mandarino 	void *data, char *long_name)
1989db2a4165SFrank Mandarino {
1990db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1991db2a4165SFrank Mandarino 
1992db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1993db2a4165SFrank Mandarino 	if (long_name)
1994db2a4165SFrank Mandarino 		template.name = long_name;
1995db2a4165SFrank Mandarino 	template.index = 0;
1996db2a4165SFrank Mandarino 
1997db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1998db2a4165SFrank Mandarino }
1999db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2000db2a4165SFrank Mandarino 
2001db2a4165SFrank Mandarino /**
20023e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
20033e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
20043e8e1952SIan Molton  * duplicating this code.
20053e8e1952SIan Molton  *
20063e8e1952SIan Molton  * @codec: codec to add controls to
20073e8e1952SIan Molton  * @controls: array of controls to add
20083e8e1952SIan Molton  * @num_controls: number of elements in the array
20093e8e1952SIan Molton  *
20103e8e1952SIan Molton  * Return 0 for success, else error.
20113e8e1952SIan Molton  */
20123e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
20133e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
20143e8e1952SIan Molton {
2015f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
20163e8e1952SIan Molton 	int err, i;
20173e8e1952SIan Molton 
20183e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
20193e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
20203e8e1952SIan Molton 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
20213e8e1952SIan Molton 		if (err < 0) {
2022082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2023082100dcSMark Brown 				codec->name, control->name, err);
20243e8e1952SIan Molton 			return err;
20253e8e1952SIan Molton 		}
20263e8e1952SIan Molton 	}
20273e8e1952SIan Molton 
20283e8e1952SIan Molton 	return 0;
20293e8e1952SIan Molton }
20303e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
20313e8e1952SIan Molton 
20323e8e1952SIan Molton /**
2033db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2034db2a4165SFrank Mandarino  * @kcontrol: mixer control
2035db2a4165SFrank Mandarino  * @uinfo: control element information
2036db2a4165SFrank Mandarino  *
2037db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2038db2a4165SFrank Mandarino  * mixer control.
2039db2a4165SFrank Mandarino  *
2040db2a4165SFrank Mandarino  * Returns 0 for success.
2041db2a4165SFrank Mandarino  */
2042db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2043db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2044db2a4165SFrank Mandarino {
2045db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2046db2a4165SFrank Mandarino 
2047db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2048db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2049f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2050db2a4165SFrank Mandarino 
2051f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2052f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2053db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2054db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2055db2a4165SFrank Mandarino 	return 0;
2056db2a4165SFrank Mandarino }
2057db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2058db2a4165SFrank Mandarino 
2059db2a4165SFrank Mandarino /**
2060db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2061db2a4165SFrank Mandarino  * @kcontrol: mixer control
2062ac11a2b3SMark Brown  * @ucontrol: control element information
2063db2a4165SFrank Mandarino  *
2064db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2065db2a4165SFrank Mandarino  *
2066db2a4165SFrank Mandarino  * Returns 0 for success.
2067db2a4165SFrank Mandarino  */
2068db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2069db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2070db2a4165SFrank Mandarino {
2071db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2072db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
207346f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2074db2a4165SFrank Mandarino 
2075f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2076db2a4165SFrank Mandarino 		;
2077db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
20783ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
20793ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2080db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2081db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2082db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2083db2a4165SFrank Mandarino 
2084db2a4165SFrank Mandarino 	return 0;
2085db2a4165SFrank Mandarino }
2086db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2087db2a4165SFrank Mandarino 
2088db2a4165SFrank Mandarino /**
2089db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2090db2a4165SFrank Mandarino  * @kcontrol: mixer control
2091ac11a2b3SMark Brown  * @ucontrol: control element information
2092db2a4165SFrank Mandarino  *
2093db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2094db2a4165SFrank Mandarino  *
2095db2a4165SFrank Mandarino  * Returns 0 for success.
2096db2a4165SFrank Mandarino  */
2097db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2098db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2099db2a4165SFrank Mandarino {
2100db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2101db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
210246f5822fSDaniel Ribeiro 	unsigned int val;
210346f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2104db2a4165SFrank Mandarino 
2105f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2106db2a4165SFrank Mandarino 		;
2107f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2108db2a4165SFrank Mandarino 		return -EINVAL;
2109db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2110db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2111db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2112f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2113db2a4165SFrank Mandarino 			return -EINVAL;
2114db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2115db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2116db2a4165SFrank Mandarino 	}
2117db2a4165SFrank Mandarino 
21186c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2119db2a4165SFrank Mandarino }
2120db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2121db2a4165SFrank Mandarino 
2122db2a4165SFrank Mandarino /**
21232e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
21242e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
21252e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
21262e72f8e3SPeter Ujfalusi  *
21272e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
21282e72f8e3SPeter Ujfalusi  *
21292e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
21302e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
21312e72f8e3SPeter Ujfalusi  *
21322e72f8e3SPeter Ujfalusi  * Returns 0 for success.
21332e72f8e3SPeter Ujfalusi  */
21342e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
21352e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
21362e72f8e3SPeter Ujfalusi {
21372e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
213874155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
213946f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
21402e72f8e3SPeter Ujfalusi 
21412e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
21422e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
21432e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
21442e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
21452e72f8e3SPeter Ujfalusi 			break;
21462e72f8e3SPeter Ujfalusi 	}
21472e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
21482e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
21492e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
21502e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
21512e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
21522e72f8e3SPeter Ujfalusi 				break;
21532e72f8e3SPeter Ujfalusi 		}
21542e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
21552e72f8e3SPeter Ujfalusi 	}
21562e72f8e3SPeter Ujfalusi 
21572e72f8e3SPeter Ujfalusi 	return 0;
21582e72f8e3SPeter Ujfalusi }
21592e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
21602e72f8e3SPeter Ujfalusi 
21612e72f8e3SPeter Ujfalusi /**
21622e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
21632e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
21642e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
21652e72f8e3SPeter Ujfalusi  *
21662e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
21672e72f8e3SPeter Ujfalusi  *
21682e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
21692e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
21702e72f8e3SPeter Ujfalusi  *
21712e72f8e3SPeter Ujfalusi  * Returns 0 for success.
21722e72f8e3SPeter Ujfalusi  */
21732e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
21742e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
21752e72f8e3SPeter Ujfalusi {
21762e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
217774155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
217846f5822fSDaniel Ribeiro 	unsigned int val;
217946f5822fSDaniel Ribeiro 	unsigned int mask;
21802e72f8e3SPeter Ujfalusi 
21812e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
21822e72f8e3SPeter Ujfalusi 		return -EINVAL;
21832e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
21842e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
21852e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
21862e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
21872e72f8e3SPeter Ujfalusi 			return -EINVAL;
21882e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
21892e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
21902e72f8e3SPeter Ujfalusi 	}
21912e72f8e3SPeter Ujfalusi 
21926c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
21932e72f8e3SPeter Ujfalusi }
21942e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
21952e72f8e3SPeter Ujfalusi 
21962e72f8e3SPeter Ujfalusi /**
2197db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2198db2a4165SFrank Mandarino  * @kcontrol: mixer control
2199db2a4165SFrank Mandarino  * @uinfo: control element information
2200db2a4165SFrank Mandarino  *
2201db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2202db2a4165SFrank Mandarino  * single mixer.
2203db2a4165SFrank Mandarino  *
2204db2a4165SFrank Mandarino  * Returns 0 for success.
2205db2a4165SFrank Mandarino  */
2206db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2207db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2208db2a4165SFrank Mandarino {
2209db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2210db2a4165SFrank Mandarino 
2211db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2212db2a4165SFrank Mandarino 	uinfo->count = 1;
2213f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2214db2a4165SFrank Mandarino 
2215f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2216f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2217db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2218db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2219db2a4165SFrank Mandarino 	return 0;
2220db2a4165SFrank Mandarino }
2221db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2222db2a4165SFrank Mandarino 
2223db2a4165SFrank Mandarino /**
2224db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2225db2a4165SFrank Mandarino  * @kcontrol: mixer control
2226db2a4165SFrank Mandarino  * @uinfo: control element information
2227db2a4165SFrank Mandarino  *
2228db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2229db2a4165SFrank Mandarino  *
2230db2a4165SFrank Mandarino  * Returns 0 for success.
2231db2a4165SFrank Mandarino  */
2232db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2233db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2234db2a4165SFrank Mandarino {
2235a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2236db2a4165SFrank Mandarino 
2237fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2238a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2239a7a4ac86SPhilipp Zabel 	else
2240a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2241a7a4ac86SPhilipp Zabel 
2242db2a4165SFrank Mandarino 	uinfo->count = 1;
2243db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2244a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2245db2a4165SFrank Mandarino 	return 0;
2246db2a4165SFrank Mandarino }
2247db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2248db2a4165SFrank Mandarino 
2249db2a4165SFrank Mandarino /**
2250db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2251db2a4165SFrank Mandarino  * @kcontrol: mixer control
2252db2a4165SFrank Mandarino  * @uinfo: control element information
2253db2a4165SFrank Mandarino  *
2254db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2255db2a4165SFrank Mandarino  *
2256db2a4165SFrank Mandarino  * Returns 0 for success.
2257db2a4165SFrank Mandarino  */
2258db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2259db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2260db2a4165SFrank Mandarino {
22614eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22624eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2263d11bb4a9SPeter Ujfalusi 	int platform_max;
2264762b8df7SMark Brown 	unsigned int shift = mc->shift;
2265815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2266db2a4165SFrank Mandarino 
2267d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2268d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2269d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2270d11bb4a9SPeter Ujfalusi 
2271d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2272a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2273a7a4ac86SPhilipp Zabel 	else
2274a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2275a7a4ac86SPhilipp Zabel 
2276db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2277db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2278d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2279db2a4165SFrank Mandarino 	return 0;
2280db2a4165SFrank Mandarino }
2281db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2282db2a4165SFrank Mandarino 
2283db2a4165SFrank Mandarino /**
2284db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2285db2a4165SFrank Mandarino  * @kcontrol: mixer control
2286ac11a2b3SMark Brown  * @ucontrol: control element information
2287db2a4165SFrank Mandarino  *
2288db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2289db2a4165SFrank Mandarino  *
2290db2a4165SFrank Mandarino  * Returns 0 for success.
2291db2a4165SFrank Mandarino  */
2292db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2293db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2294db2a4165SFrank Mandarino {
22954eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22964eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2297db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2298815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2299815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2300815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
23014eaa9819SJon Smirl 	int max = mc->max;
2302815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2303815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2304db2a4165SFrank Mandarino 
2305db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2306db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2307db2a4165SFrank Mandarino 	if (shift != rshift)
2308db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2309db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2310db2a4165SFrank Mandarino 	if (invert) {
2311db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2312a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2313db2a4165SFrank Mandarino 		if (shift != rshift)
2314db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2315a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2316db2a4165SFrank Mandarino 	}
2317db2a4165SFrank Mandarino 
2318db2a4165SFrank Mandarino 	return 0;
2319db2a4165SFrank Mandarino }
2320db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2321db2a4165SFrank Mandarino 
2322db2a4165SFrank Mandarino /**
2323db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2324db2a4165SFrank Mandarino  * @kcontrol: mixer control
2325ac11a2b3SMark Brown  * @ucontrol: control element information
2326db2a4165SFrank Mandarino  *
2327db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2328db2a4165SFrank Mandarino  *
2329db2a4165SFrank Mandarino  * Returns 0 for success.
2330db2a4165SFrank Mandarino  */
2331db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2332db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2333db2a4165SFrank Mandarino {
23344eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23354eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2336db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2337815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2338815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2339815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
23404eaa9819SJon Smirl 	int max = mc->max;
2341815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2342815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
234346f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2344db2a4165SFrank Mandarino 
2345db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2346db2a4165SFrank Mandarino 	if (invert)
2347a7a4ac86SPhilipp Zabel 		val = max - val;
2348db2a4165SFrank Mandarino 	val_mask = mask << shift;
2349db2a4165SFrank Mandarino 	val = val << shift;
2350db2a4165SFrank Mandarino 	if (shift != rshift) {
2351db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2352db2a4165SFrank Mandarino 		if (invert)
2353a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2354db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2355db2a4165SFrank Mandarino 		val |= val2 << rshift;
2356db2a4165SFrank Mandarino 	}
23576c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2358db2a4165SFrank Mandarino }
2359db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2360db2a4165SFrank Mandarino 
2361db2a4165SFrank Mandarino /**
2362db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2363db2a4165SFrank Mandarino  * @kcontrol: mixer control
2364db2a4165SFrank Mandarino  * @uinfo: control element information
2365db2a4165SFrank Mandarino  *
2366db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2367db2a4165SFrank Mandarino  * spans 2 codec registers.
2368db2a4165SFrank Mandarino  *
2369db2a4165SFrank Mandarino  * Returns 0 for success.
2370db2a4165SFrank Mandarino  */
2371db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2372db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2373db2a4165SFrank Mandarino {
23744eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23754eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2376d11bb4a9SPeter Ujfalusi 	int platform_max;
2377db2a4165SFrank Mandarino 
2378d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2379d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2380d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2381d11bb4a9SPeter Ujfalusi 
2382d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2383a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2384a7a4ac86SPhilipp Zabel 	else
2385a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2386a7a4ac86SPhilipp Zabel 
2387db2a4165SFrank Mandarino 	uinfo->count = 2;
2388db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2389d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2390db2a4165SFrank Mandarino 	return 0;
2391db2a4165SFrank Mandarino }
2392db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2393db2a4165SFrank Mandarino 
2394db2a4165SFrank Mandarino /**
2395db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2396db2a4165SFrank Mandarino  * @kcontrol: mixer control
2397ac11a2b3SMark Brown  * @ucontrol: control element information
2398db2a4165SFrank Mandarino  *
2399db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2400db2a4165SFrank Mandarino  *
2401db2a4165SFrank Mandarino  * Returns 0 for success.
2402db2a4165SFrank Mandarino  */
2403db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2404db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2405db2a4165SFrank Mandarino {
24064eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24074eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2408db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2409815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2410815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2411815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
24124eaa9819SJon Smirl 	int max = mc->max;
2413815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2414815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2415db2a4165SFrank Mandarino 
2416db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2417db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2418db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2419db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2420db2a4165SFrank Mandarino 	if (invert) {
2421db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2422a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2423db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2424a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2425db2a4165SFrank Mandarino 	}
2426db2a4165SFrank Mandarino 
2427db2a4165SFrank Mandarino 	return 0;
2428db2a4165SFrank Mandarino }
2429db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2430db2a4165SFrank Mandarino 
2431db2a4165SFrank Mandarino /**
2432db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2433db2a4165SFrank Mandarino  * @kcontrol: mixer control
2434ac11a2b3SMark Brown  * @ucontrol: control element information
2435db2a4165SFrank Mandarino  *
2436db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2437db2a4165SFrank Mandarino  *
2438db2a4165SFrank Mandarino  * Returns 0 for success.
2439db2a4165SFrank Mandarino  */
2440db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2441db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2442db2a4165SFrank Mandarino {
24434eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24444eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2445db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2446815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2447815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2448815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
24494eaa9819SJon Smirl 	int max = mc->max;
2450815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2451815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2452db2a4165SFrank Mandarino 	int err;
245346f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2454db2a4165SFrank Mandarino 
2455db2a4165SFrank Mandarino 	val_mask = mask << shift;
2456db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2457db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2458db2a4165SFrank Mandarino 
2459db2a4165SFrank Mandarino 	if (invert) {
2460a7a4ac86SPhilipp Zabel 		val = max - val;
2461a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2462db2a4165SFrank Mandarino 	}
2463db2a4165SFrank Mandarino 
2464db2a4165SFrank Mandarino 	val = val << shift;
2465db2a4165SFrank Mandarino 	val2 = val2 << shift;
2466db2a4165SFrank Mandarino 
24676c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
24683ff3f64bSMark Brown 	if (err < 0)
2469db2a4165SFrank Mandarino 		return err;
2470db2a4165SFrank Mandarino 
24716c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2472db2a4165SFrank Mandarino 	return err;
2473db2a4165SFrank Mandarino }
2474db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2475db2a4165SFrank Mandarino 
2476e13ac2e9SMark Brown /**
2477e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2478e13ac2e9SMark Brown  * @kcontrol: mixer control
2479e13ac2e9SMark Brown  * @uinfo: control element information
2480e13ac2e9SMark Brown  *
2481e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2482e13ac2e9SMark Brown  *
2483e13ac2e9SMark Brown  * Returns 0 for success.
2484e13ac2e9SMark Brown  */
2485e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2486e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2487e13ac2e9SMark Brown {
24884eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24894eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2490d11bb4a9SPeter Ujfalusi 	int platform_max;
24914eaa9819SJon Smirl 	int min = mc->min;
2492e13ac2e9SMark Brown 
2493d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2494d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2495d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2496d11bb4a9SPeter Ujfalusi 
2497e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2498e13ac2e9SMark Brown 	uinfo->count = 2;
2499e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2500d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2501e13ac2e9SMark Brown 	return 0;
2502e13ac2e9SMark Brown }
2503e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2504e13ac2e9SMark Brown 
2505e13ac2e9SMark Brown /**
2506e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2507e13ac2e9SMark Brown  * @kcontrol: mixer control
2508ac11a2b3SMark Brown  * @ucontrol: control element information
2509e13ac2e9SMark Brown  *
2510e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2511e13ac2e9SMark Brown  *
2512e13ac2e9SMark Brown  * Returns 0 for success.
2513e13ac2e9SMark Brown  */
2514e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2515e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2516e13ac2e9SMark Brown {
25174eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25184eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2519e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2520815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
25214eaa9819SJon Smirl 	int min = mc->min;
2522e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2523e13ac2e9SMark Brown 
2524e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2525e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2526e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2527e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2528e13ac2e9SMark Brown 	return 0;
2529e13ac2e9SMark Brown }
2530e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2531e13ac2e9SMark Brown 
2532e13ac2e9SMark Brown /**
2533e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2534e13ac2e9SMark Brown  * @kcontrol: mixer control
2535ac11a2b3SMark Brown  * @ucontrol: control element information
2536e13ac2e9SMark Brown  *
2537e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2538e13ac2e9SMark Brown  *
2539e13ac2e9SMark Brown  * Returns 0 for success.
2540e13ac2e9SMark Brown  */
2541e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2542e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2543e13ac2e9SMark Brown {
25444eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25454eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2546e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2547815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
25484eaa9819SJon Smirl 	int min = mc->min;
254946f5822fSDaniel Ribeiro 	unsigned int val;
2550e13ac2e9SMark Brown 
2551e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2552e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2553e13ac2e9SMark Brown 
25546c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2555e13ac2e9SMark Brown }
2556e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2557e13ac2e9SMark Brown 
25588c6529dbSLiam Girdwood /**
2559637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2560637d3847SPeter Ujfalusi  *
2561637d3847SPeter Ujfalusi  * @codec: where to look for the control
2562637d3847SPeter Ujfalusi  * @name: Name of the control
2563637d3847SPeter Ujfalusi  * @max: new maximum limit
2564637d3847SPeter Ujfalusi  *
2565637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2566637d3847SPeter Ujfalusi  */
2567637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2568637d3847SPeter Ujfalusi 	const char *name, int max)
2569637d3847SPeter Ujfalusi {
2570f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2571637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2572637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2573637d3847SPeter Ujfalusi 	int found = 0;
2574637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2575637d3847SPeter Ujfalusi 
2576637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2577637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2578637d3847SPeter Ujfalusi 		return -EINVAL;
2579637d3847SPeter Ujfalusi 
2580637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2581637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2582637d3847SPeter Ujfalusi 			found = 1;
2583637d3847SPeter Ujfalusi 			break;
2584637d3847SPeter Ujfalusi 		}
2585637d3847SPeter Ujfalusi 	}
2586637d3847SPeter Ujfalusi 	if (found) {
2587637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2588637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2589d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2590637d3847SPeter Ujfalusi 			ret = 0;
2591637d3847SPeter Ujfalusi 		}
2592637d3847SPeter Ujfalusi 	}
2593637d3847SPeter Ujfalusi 	return ret;
2594637d3847SPeter Ujfalusi }
2595637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2596637d3847SPeter Ujfalusi 
2597637d3847SPeter Ujfalusi /**
2598b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2599b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2600b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2601b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2602b6f4bb38Sapatard@mandriva.com  *
2603b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2604b6f4bb38Sapatard@mandriva.com  */
2605b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2606b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2607b6f4bb38Sapatard@mandriva.com {
2608b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2609b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2610b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2611b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2612b6f4bb38Sapatard@mandriva.com 
2613b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2614b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2615b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2616b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2617b6f4bb38Sapatard@mandriva.com 
2618b6f4bb38Sapatard@mandriva.com 	return 0;
2619b6f4bb38Sapatard@mandriva.com }
2620b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2621b6f4bb38Sapatard@mandriva.com 
2622b6f4bb38Sapatard@mandriva.com /**
2623b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2624b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2625b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2626b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2627b6f4bb38Sapatard@mandriva.com  *
2628b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2629b6f4bb38Sapatard@mandriva.com  */
2630b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2631b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2632b6f4bb38Sapatard@mandriva.com {
2633b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2634b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2635b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2636b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2637b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2638b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2639b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2640b6f4bb38Sapatard@mandriva.com 
264120630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
264220630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2643b6f4bb38Sapatard@mandriva.com 	return 0;
2644b6f4bb38Sapatard@mandriva.com }
2645b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2646b6f4bb38Sapatard@mandriva.com 
2647b6f4bb38Sapatard@mandriva.com /**
2648b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2649b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2650b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2651b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2652b6f4bb38Sapatard@mandriva.com  *
2653b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2654b6f4bb38Sapatard@mandriva.com  */
2655b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2656b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2657b6f4bb38Sapatard@mandriva.com {
2658b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2659b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2660b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2661b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2662b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2663b6f4bb38Sapatard@mandriva.com 	int ret;
2664b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2665b6f4bb38Sapatard@mandriva.com 
2666b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2667b6f4bb38Sapatard@mandriva.com 	val &= mask;
2668b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2669b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2670b6f4bb38Sapatard@mandriva.com 
2671b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2672b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2673b6f4bb38Sapatard@mandriva.com 
2674b6f4bb38Sapatard@mandriva.com 	ret = 0;
2675b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2676b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2677b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2678f1df5aecSMark Brown 			return ret;
2679b6f4bb38Sapatard@mandriva.com 	}
2680b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2681b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2682b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2683f1df5aecSMark Brown 			return ret;
2684b6f4bb38Sapatard@mandriva.com 	}
2685b6f4bb38Sapatard@mandriva.com 
2686b6f4bb38Sapatard@mandriva.com 	return 0;
2687b6f4bb38Sapatard@mandriva.com }
2688b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2689b6f4bb38Sapatard@mandriva.com 
2690b6f4bb38Sapatard@mandriva.com /**
26918c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
26928c6529dbSLiam Girdwood  * @dai: DAI
26938c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
26948c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
26958c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
26968c6529dbSLiam Girdwood  *
26978c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
26988c6529dbSLiam Girdwood  */
26998c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
27008c6529dbSLiam Girdwood 	unsigned int freq, int dir)
27018c6529dbSLiam Girdwood {
2702f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2703f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
27048c6529dbSLiam Girdwood 	else
27058c6529dbSLiam Girdwood 		return -EINVAL;
27068c6529dbSLiam Girdwood }
27078c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
27088c6529dbSLiam Girdwood 
27098c6529dbSLiam Girdwood /**
27108c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
27118c6529dbSLiam Girdwood  * @dai: DAI
2712ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
27138c6529dbSLiam Girdwood  * @div: new clock divisor.
27148c6529dbSLiam Girdwood  *
27158c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
27168c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
27178c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
27188c6529dbSLiam Girdwood  */
27198c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
27208c6529dbSLiam Girdwood 	int div_id, int div)
27218c6529dbSLiam Girdwood {
2722f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2723f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
27248c6529dbSLiam Girdwood 	else
27258c6529dbSLiam Girdwood 		return -EINVAL;
27268c6529dbSLiam Girdwood }
27278c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
27288c6529dbSLiam Girdwood 
27298c6529dbSLiam Girdwood /**
27308c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
27318c6529dbSLiam Girdwood  * @dai: DAI
27328c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
273385488037SMark Brown  * @source: DAI specific source for the PLL
27348c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
27358c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
27368c6529dbSLiam Girdwood  *
27378c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
27388c6529dbSLiam Girdwood  */
273985488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
274085488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
27418c6529dbSLiam Girdwood {
2742f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2743f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
274485488037SMark Brown 					 freq_in, freq_out);
27458c6529dbSLiam Girdwood 	else
27468c6529dbSLiam Girdwood 		return -EINVAL;
27478c6529dbSLiam Girdwood }
27488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
27498c6529dbSLiam Girdwood 
27508c6529dbSLiam Girdwood /**
27518c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
27528c6529dbSLiam Girdwood  * @dai: DAI
27538c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
27548c6529dbSLiam Girdwood  *
27558c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
27568c6529dbSLiam Girdwood  */
27578c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
27588c6529dbSLiam Girdwood {
2759f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
2760f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
27618c6529dbSLiam Girdwood 	else
27628c6529dbSLiam Girdwood 		return -EINVAL;
27638c6529dbSLiam Girdwood }
27648c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
27658c6529dbSLiam Girdwood 
27668c6529dbSLiam Girdwood /**
27678c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
27688c6529dbSLiam Girdwood  * @dai: DAI
2769a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2770a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
27718c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2772a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
27738c6529dbSLiam Girdwood  *
27748c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
27758c6529dbSLiam Girdwood  * specific.
27768c6529dbSLiam Girdwood  */
27778c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2778a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
27798c6529dbSLiam Girdwood {
2780f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2781f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2782a5479e38SDaniel Ribeiro 				slots, slot_width);
27838c6529dbSLiam Girdwood 	else
27848c6529dbSLiam Girdwood 		return -EINVAL;
27858c6529dbSLiam Girdwood }
27868c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
27878c6529dbSLiam Girdwood 
27888c6529dbSLiam Girdwood /**
2789472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2790472df3cbSBarry Song  * @dai: DAI
2791472df3cbSBarry Song  * @tx_num: how many TX channels
2792472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2793472df3cbSBarry Song  *           0~num-1 uses
2794472df3cbSBarry Song  * @rx_num: how many RX channels
2795472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2796472df3cbSBarry Song  *           0~num-1 uses
2797472df3cbSBarry Song  *
2798472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2799472df3cbSBarry Song  */
2800472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2801472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2802472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2803472df3cbSBarry Song {
2804f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2805f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2806472df3cbSBarry Song 			rx_num, rx_slot);
2807472df3cbSBarry Song 	else
2808472df3cbSBarry Song 		return -EINVAL;
2809472df3cbSBarry Song }
2810472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2811472df3cbSBarry Song 
2812472df3cbSBarry Song /**
28138c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
28148c6529dbSLiam Girdwood  * @dai: DAI
28158c6529dbSLiam Girdwood  * @tristate: tristate enable
28168c6529dbSLiam Girdwood  *
28178c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
28188c6529dbSLiam Girdwood  */
28198c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
28208c6529dbSLiam Girdwood {
2821f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2822f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
28238c6529dbSLiam Girdwood 	else
28248c6529dbSLiam Girdwood 		return -EINVAL;
28258c6529dbSLiam Girdwood }
28268c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
28278c6529dbSLiam Girdwood 
28288c6529dbSLiam Girdwood /**
28298c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
28308c6529dbSLiam Girdwood  * @dai: DAI
28318c6529dbSLiam Girdwood  * @mute: mute enable
28328c6529dbSLiam Girdwood  *
28338c6529dbSLiam Girdwood  * Mutes the DAI DAC.
28348c6529dbSLiam Girdwood  */
28358c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
28368c6529dbSLiam Girdwood {
2837f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
2838f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
28398c6529dbSLiam Girdwood 	else
28408c6529dbSLiam Girdwood 		return -EINVAL;
28418c6529dbSLiam Girdwood }
28428c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
28438c6529dbSLiam Girdwood 
2844c5af3a2eSMark Brown /**
2845c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2846c5af3a2eSMark Brown  *
2847ac11a2b3SMark Brown  * @card: Card to register
2848c5af3a2eSMark Brown  *
2849c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2850c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2851c5af3a2eSMark Brown  * registration APIs.
2852c5af3a2eSMark Brown  */
2853c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
2854c5af3a2eSMark Brown {
2855f0fba2adSLiam Girdwood 	int i;
2856f0fba2adSLiam Girdwood 
2857c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2858c5af3a2eSMark Brown 		return -EINVAL;
2859c5af3a2eSMark Brown 
2860f0fba2adSLiam Girdwood 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2861f0fba2adSLiam Girdwood 			GFP_KERNEL);
2862f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2863f0fba2adSLiam Girdwood 		return -ENOMEM;
2864f0fba2adSLiam Girdwood 
2865f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
2866f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
2867f0fba2adSLiam Girdwood 
2868c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2869c5af3a2eSMark Brown 	card->instantiated = 0;
2870f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2871c5af3a2eSMark Brown 
2872c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2873c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2874435c5e25SMark Brown 	snd_soc_instantiate_cards();
2875c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2876c5af3a2eSMark Brown 
2877c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2878c5af3a2eSMark Brown 
2879c5af3a2eSMark Brown 	return 0;
2880c5af3a2eSMark Brown }
2881c5af3a2eSMark Brown 
2882c5af3a2eSMark Brown /**
2883c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2884c5af3a2eSMark Brown  *
2885ac11a2b3SMark Brown  * @card: Card to unregister
2886c5af3a2eSMark Brown  *
2887c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2888c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2889c5af3a2eSMark Brown  * registration APIs.
2890c5af3a2eSMark Brown  */
2891c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
2892c5af3a2eSMark Brown {
2893c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2894c5af3a2eSMark Brown 	list_del(&card->list);
2895c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2896c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2897c5af3a2eSMark Brown 
2898c5af3a2eSMark Brown 	return 0;
2899c5af3a2eSMark Brown }
2900c5af3a2eSMark Brown 
2901f0fba2adSLiam Girdwood /*
2902f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2903f0fba2adSLiam Girdwood  * and sanitizing names.
2904f0fba2adSLiam Girdwood  */
2905f0fba2adSLiam Girdwood static inline char *fmt_single_name(struct device *dev, int *id)
2906f0fba2adSLiam Girdwood {
2907f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2908f0fba2adSLiam Girdwood 	int id1, id2;
2909f0fba2adSLiam Girdwood 
2910f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2911f0fba2adSLiam Girdwood 		return NULL;
2912f0fba2adSLiam Girdwood 
2913f0fba2adSLiam Girdwood 	strncpy(name, dev_name(dev), NAME_SIZE);
2914f0fba2adSLiam Girdwood 
2915f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2916f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2917f0fba2adSLiam Girdwood 	if (found) {
2918f0fba2adSLiam Girdwood 		/* get ID */
2919f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2920f0fba2adSLiam Girdwood 
2921f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2922f0fba2adSLiam Girdwood 			if (*id == -1)
2923f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2924f0fba2adSLiam Girdwood 		}
2925f0fba2adSLiam Girdwood 
2926f0fba2adSLiam Girdwood 	} else {
2927f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2928f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2929f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2930f0fba2adSLiam Girdwood 
2931f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
2932f0fba2adSLiam Girdwood 			*id = ((id1 && 0xffff) << 16) + id2;
2933f0fba2adSLiam Girdwood 
2934f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2935f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2936f0fba2adSLiam Girdwood 			strncpy(name, tmp, NAME_SIZE);
2937f0fba2adSLiam Girdwood 		} else
2938f0fba2adSLiam Girdwood 			*id = 0;
2939f0fba2adSLiam Girdwood 	}
2940f0fba2adSLiam Girdwood 
2941f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2942f0fba2adSLiam Girdwood }
2943f0fba2adSLiam Girdwood 
2944f0fba2adSLiam Girdwood /*
2945f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2946f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2947f0fba2adSLiam Girdwood  */
2948f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2949f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2950f0fba2adSLiam Girdwood {
2951f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
2952f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2953f0fba2adSLiam Girdwood 				dev_name(dev));
2954f0fba2adSLiam Girdwood 		return NULL;
2955f0fba2adSLiam Girdwood 	}
2956f0fba2adSLiam Girdwood 
2957f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2958f0fba2adSLiam Girdwood }
2959f0fba2adSLiam Girdwood 
29609115171aSMark Brown /**
29619115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
29629115171aSMark Brown  *
2963ac11a2b3SMark Brown  * @dai: DAI to register
29649115171aSMark Brown  */
2965f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
2966f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
29679115171aSMark Brown {
2968f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
29699115171aSMark Brown 
2970f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
29719115171aSMark Brown 
2972f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2973f0fba2adSLiam Girdwood 	if (dai == NULL)
2974f0fba2adSLiam Girdwood 			return -ENOMEM;
29756335d055SEric Miao 
2976f0fba2adSLiam Girdwood 	/* create DAI component name */
2977f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
2978f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
2979f0fba2adSLiam Girdwood 		kfree(dai);
2980f0fba2adSLiam Girdwood 		return -ENOMEM;
2981f0fba2adSLiam Girdwood 	}
2982f0fba2adSLiam Girdwood 
2983f0fba2adSLiam Girdwood 	dai->dev = dev;
2984f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
2985f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
2986f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
29879115171aSMark Brown 
29889115171aSMark Brown 	mutex_lock(&client_mutex);
29899115171aSMark Brown 	list_add(&dai->list, &dai_list);
2990435c5e25SMark Brown 	snd_soc_instantiate_cards();
29919115171aSMark Brown 	mutex_unlock(&client_mutex);
29929115171aSMark Brown 
29939115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
29949115171aSMark Brown 
29959115171aSMark Brown 	return 0;
29969115171aSMark Brown }
29979115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
29989115171aSMark Brown 
29999115171aSMark Brown /**
30009115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
30019115171aSMark Brown  *
3002ac11a2b3SMark Brown  * @dai: DAI to unregister
30039115171aSMark Brown  */
3004f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
30059115171aSMark Brown {
3006f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3007f0fba2adSLiam Girdwood 
3008f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3009f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3010f0fba2adSLiam Girdwood 			goto found;
3011f0fba2adSLiam Girdwood 	}
3012f0fba2adSLiam Girdwood 	return;
3013f0fba2adSLiam Girdwood 
3014f0fba2adSLiam Girdwood found:
30159115171aSMark Brown 	mutex_lock(&client_mutex);
30169115171aSMark Brown 	list_del(&dai->list);
30179115171aSMark Brown 	mutex_unlock(&client_mutex);
30189115171aSMark Brown 
30199115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3020f0fba2adSLiam Girdwood 	kfree(dai->name);
3021f0fba2adSLiam Girdwood 	kfree(dai);
30229115171aSMark Brown }
30239115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
30249115171aSMark Brown 
30259115171aSMark Brown /**
30269115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
30279115171aSMark Brown  *
3028ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3029ac11a2b3SMark Brown  * @count: Number of DAIs
30309115171aSMark Brown  */
3031f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3032f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
30339115171aSMark Brown {
3034f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3035f0fba2adSLiam Girdwood 	int i, ret = 0;
3036f0fba2adSLiam Girdwood 
3037720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
30389115171aSMark Brown 
30399115171aSMark Brown 	for (i = 0; i < count; i++) {
3040f0fba2adSLiam Girdwood 
3041f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3042f0fba2adSLiam Girdwood 		if (dai == NULL)
3043f0fba2adSLiam Girdwood 			return -ENOMEM;
3044f0fba2adSLiam Girdwood 
3045f0fba2adSLiam Girdwood 		/* create DAI component name */
3046f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3047f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3048f0fba2adSLiam Girdwood 			kfree(dai);
3049f0fba2adSLiam Girdwood 			ret = -EINVAL;
30509115171aSMark Brown 			goto err;
30519115171aSMark Brown 		}
30529115171aSMark Brown 
3053f0fba2adSLiam Girdwood 		dai->dev = dev;
3054f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
3055*0f9141c9SMark Brown 		if (dai->driver->id)
3056*0f9141c9SMark Brown 			dai->id = dai->driver->id;
3057*0f9141c9SMark Brown 		else
3058*0f9141c9SMark Brown 			dai->id = i;
3059f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3060f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3061f0fba2adSLiam Girdwood 
3062f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3063f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3064f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3065f0fba2adSLiam Girdwood 
3066f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3067f0fba2adSLiam Girdwood 	}
3068f0fba2adSLiam Girdwood 
3069f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
30709115171aSMark Brown 	return 0;
30719115171aSMark Brown 
30729115171aSMark Brown err:
30739115171aSMark Brown 	for (i--; i >= 0; i--)
3074f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
30759115171aSMark Brown 
30769115171aSMark Brown 	return ret;
30779115171aSMark Brown }
30789115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
30799115171aSMark Brown 
30809115171aSMark Brown /**
30819115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
30829115171aSMark Brown  *
3083ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3084ac11a2b3SMark Brown  * @count: Number of DAIs
30859115171aSMark Brown  */
3086f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
30879115171aSMark Brown {
30889115171aSMark Brown 	int i;
30899115171aSMark Brown 
30909115171aSMark Brown 	for (i = 0; i < count; i++)
3091f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
30929115171aSMark Brown }
30939115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
30949115171aSMark Brown 
309512a48a8cSMark Brown /**
309612a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
309712a48a8cSMark Brown  *
3098ac11a2b3SMark Brown  * @platform: platform to register
309912a48a8cSMark Brown  */
3100f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3101f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
310212a48a8cSMark Brown {
3103f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
310412a48a8cSMark Brown 
3105f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3106f0fba2adSLiam Girdwood 
3107f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3108f0fba2adSLiam Girdwood 	if (platform == NULL)
3109f0fba2adSLiam Girdwood 			return -ENOMEM;
3110f0fba2adSLiam Girdwood 
3111f0fba2adSLiam Girdwood 	/* create platform component name */
3112f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3113f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3114f0fba2adSLiam Girdwood 		kfree(platform);
3115f0fba2adSLiam Girdwood 		return -ENOMEM;
3116f0fba2adSLiam Girdwood 	}
3117f0fba2adSLiam Girdwood 
3118f0fba2adSLiam Girdwood 	platform->dev = dev;
3119f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
312012a48a8cSMark Brown 
312112a48a8cSMark Brown 	mutex_lock(&client_mutex);
312212a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3123435c5e25SMark Brown 	snd_soc_instantiate_cards();
312412a48a8cSMark Brown 	mutex_unlock(&client_mutex);
312512a48a8cSMark Brown 
312612a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
312712a48a8cSMark Brown 
312812a48a8cSMark Brown 	return 0;
312912a48a8cSMark Brown }
313012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
313112a48a8cSMark Brown 
313212a48a8cSMark Brown /**
313312a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
313412a48a8cSMark Brown  *
3135ac11a2b3SMark Brown  * @platform: platform to unregister
313612a48a8cSMark Brown  */
3137f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
313812a48a8cSMark Brown {
3139f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3140f0fba2adSLiam Girdwood 
3141f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3142f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3143f0fba2adSLiam Girdwood 			goto found;
3144f0fba2adSLiam Girdwood 	}
3145f0fba2adSLiam Girdwood 	return;
3146f0fba2adSLiam Girdwood 
3147f0fba2adSLiam Girdwood found:
314812a48a8cSMark Brown 	mutex_lock(&client_mutex);
314912a48a8cSMark Brown 	list_del(&platform->list);
315012a48a8cSMark Brown 	mutex_unlock(&client_mutex);
315112a48a8cSMark Brown 
315212a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3153f0fba2adSLiam Girdwood 	kfree(platform->name);
3154f0fba2adSLiam Girdwood 	kfree(platform);
315512a48a8cSMark Brown }
315612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
315712a48a8cSMark Brown 
3158151ab22cSMark Brown static u64 codec_format_map[] = {
3159151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3160151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3161151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3162151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3163151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3164151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3165151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3166151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3167151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3168151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3169151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3170151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3171151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3172151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3173151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3174151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3175151ab22cSMark Brown };
3176151ab22cSMark Brown 
3177151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3178151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3179151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3180151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3181151ab22cSMark Brown  */
3182151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3183151ab22cSMark Brown {
3184151ab22cSMark Brown 	int i;
3185151ab22cSMark Brown 
3186151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3187151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3188151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3189151ab22cSMark Brown }
3190151ab22cSMark Brown 
31910d0cf00aSMark Brown /**
31920d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
31930d0cf00aSMark Brown  *
3194ac11a2b3SMark Brown  * @codec: codec to register
31950d0cf00aSMark Brown  */
3196f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3197f0fba2adSLiam Girdwood 		struct snd_soc_codec_driver *codec_drv,
3198f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, int num_dai)
31990d0cf00aSMark Brown {
3200f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3201f0fba2adSLiam Girdwood 	int ret, i;
3202151ab22cSMark Brown 
3203f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
32040d0cf00aSMark Brown 
3205f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3206f0fba2adSLiam Girdwood 	if (codec == NULL)
3207f0fba2adSLiam Girdwood 		return -ENOMEM;
32080d0cf00aSMark Brown 
3209f0fba2adSLiam Girdwood 	/* create CODEC component name */
3210f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3211f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3212f0fba2adSLiam Girdwood 		kfree(codec);
3213f0fba2adSLiam Girdwood 		return -ENOMEM;
3214151ab22cSMark Brown 	}
3215151ab22cSMark Brown 
3216f0fba2adSLiam Girdwood 	/* allocate CODEC register cache */
3217f0fba2adSLiam Girdwood 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3218f0fba2adSLiam Girdwood 
3219f0fba2adSLiam Girdwood 		if (codec_drv->reg_cache_default)
3220f0fba2adSLiam Girdwood 			codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3221f0fba2adSLiam Girdwood 				codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3222f0fba2adSLiam Girdwood 		else
3223f0fba2adSLiam Girdwood 			codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3224f0fba2adSLiam Girdwood 				codec_drv->reg_word_size, GFP_KERNEL);
3225f0fba2adSLiam Girdwood 
3226f0fba2adSLiam Girdwood 		if (codec->reg_cache == NULL) {
3227f0fba2adSLiam Girdwood 			kfree(codec->name);
3228f0fba2adSLiam Girdwood 			kfree(codec);
3229f0fba2adSLiam Girdwood 			return -ENOMEM;
3230f0fba2adSLiam Girdwood 		}
3231f0fba2adSLiam Girdwood 	}
3232f0fba2adSLiam Girdwood 
3233f0fba2adSLiam Girdwood 	codec->dev = dev;
3234f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3235f0fba2adSLiam Girdwood 	codec->bias_level = SND_SOC_BIAS_OFF;
3236f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3237f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3238f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&codec->dapm_widgets);
3239f0fba2adSLiam Girdwood 	INIT_LIST_HEAD(&codec->dapm_paths);
3240f0fba2adSLiam Girdwood 
3241f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3242f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3243f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3244f0fba2adSLiam Girdwood 	}
3245f0fba2adSLiam Girdwood 
324626b01ccdSMark Brown 	/* register any DAIs */
324726b01ccdSMark Brown 	if (num_dai) {
3248f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3249f0fba2adSLiam Girdwood 		if (ret < 0)
3250f0fba2adSLiam Girdwood 			goto error;
325126b01ccdSMark Brown 	}
3252f0fba2adSLiam Girdwood 
32530d0cf00aSMark Brown 	mutex_lock(&client_mutex);
32540d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
32550d0cf00aSMark Brown 	snd_soc_instantiate_cards();
32560d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
32570d0cf00aSMark Brown 
32580d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
32590d0cf00aSMark Brown 	return 0;
3260f0fba2adSLiam Girdwood 
3261f0fba2adSLiam Girdwood error:
3262f0fba2adSLiam Girdwood 	for (i--; i >= 0; i--)
3263f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
3264f0fba2adSLiam Girdwood 
3265f0fba2adSLiam Girdwood 	if (codec->reg_cache)
3266f0fba2adSLiam Girdwood 		kfree(codec->reg_cache);
3267f0fba2adSLiam Girdwood 	kfree(codec->name);
3268f0fba2adSLiam Girdwood 	kfree(codec);
3269f0fba2adSLiam Girdwood 	return ret;
32700d0cf00aSMark Brown }
32710d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
32720d0cf00aSMark Brown 
32730d0cf00aSMark Brown /**
32740d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
32750d0cf00aSMark Brown  *
3276ac11a2b3SMark Brown  * @codec: codec to unregister
32770d0cf00aSMark Brown  */
3278f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
32790d0cf00aSMark Brown {
3280f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3281f0fba2adSLiam Girdwood 	int i;
3282f0fba2adSLiam Girdwood 
3283f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3284f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3285f0fba2adSLiam Girdwood 			goto found;
3286f0fba2adSLiam Girdwood 	}
3287f0fba2adSLiam Girdwood 	return;
3288f0fba2adSLiam Girdwood 
3289f0fba2adSLiam Girdwood found:
329026b01ccdSMark Brown 	if (codec->num_dai)
3291f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3292f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3293f0fba2adSLiam Girdwood 
32940d0cf00aSMark Brown 	mutex_lock(&client_mutex);
32950d0cf00aSMark Brown 	list_del(&codec->list);
32960d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
32970d0cf00aSMark Brown 
32980d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3299f0fba2adSLiam Girdwood 
3300f0fba2adSLiam Girdwood 	if (codec->reg_cache)
3301f0fba2adSLiam Girdwood 		kfree(codec->reg_cache);
3302f0fba2adSLiam Girdwood 	kfree(codec);
33030d0cf00aSMark Brown }
33040d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
33050d0cf00aSMark Brown 
3306c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3307db2a4165SFrank Mandarino {
3308384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3309384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
3310384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
3311384c89e2SMark Brown 		printk(KERN_WARNING
3312384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
3313384c89e2SMark Brown 		debugfs_root = NULL;
3314384c89e2SMark Brown 	}
3315c3c5a19aSMark Brown 
3316c3c5a19aSMark Brown 	if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3317c3c5a19aSMark Brown 				 &codec_list_fops))
3318c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3319c3c5a19aSMark Brown 
3320f3208780SMark Brown 	if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3321f3208780SMark Brown 				 &dai_list_fops))
3322f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
332319c7ac27SMark Brown 
332419c7ac27SMark Brown 	if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
332519c7ac27SMark Brown 				 &platform_list_fops))
332619c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3327384c89e2SMark Brown #endif
3328384c89e2SMark Brown 
3329db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3330db2a4165SFrank Mandarino }
33314abe8e16SMark Brown module_init(snd_soc_init);
3332db2a4165SFrank Mandarino 
33337d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3334db2a4165SFrank Mandarino {
3335384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
3336384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
3337384c89e2SMark Brown #endif
3338db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3339db2a4165SFrank Mandarino }
3340db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3341db2a4165SFrank Mandarino 
3342db2a4165SFrank Mandarino /* Module information */
3343d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3344db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3345db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
33468b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3347