xref: /openbmc/linux/sound/soc/soc-core.c (revision 4b1cfcb4f36dca33b4552d9612537e6c0ca73639)
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>
33f0e8ed85SMark Brown #include <linux/ctype.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
35474828a4SMarek Vasut #include <sound/ac97_codec.h>
36db2a4165SFrank Mandarino #include <sound/core.h>
373028eb8cSMark Brown #include <sound/jack.h>
38db2a4165SFrank Mandarino #include <sound/pcm.h>
39db2a4165SFrank Mandarino #include <sound/pcm_params.h>
40db2a4165SFrank Mandarino #include <sound/soc.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49db2a4165SFrank Mandarino 
50384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
518a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
528a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53384c89e2SMark Brown #endif
54384c89e2SMark Brown 
55c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
56c5af3a2eSMark Brown static LIST_HEAD(card_list);
579115171aSMark Brown static LIST_HEAD(dai_list);
5812a48a8cSMark Brown static LIST_HEAD(platform_list);
590d0cf00aSMark Brown static LIST_HEAD(codec_list);
60c5af3a2eSMark Brown 
61ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
62c5af3a2eSMark Brown 
63db2a4165SFrank Mandarino /*
64db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
65db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
66db2a4165SFrank Mandarino  * between two audio tracks.
67db2a4165SFrank Mandarino  */
68db2a4165SFrank Mandarino static int pmdown_time = 5000;
69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71db2a4165SFrank Mandarino 
722bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
732bc9a81eSDimitris Papastamos  * a particular given value */
742bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
752bc9a81eSDimitris Papastamos {
762bc9a81eSDimitris Papastamos 	int c = 0;
772bc9a81eSDimitris Papastamos 	int i;
782bc9a81eSDimitris Papastamos 
792bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
802bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
812bc9a81eSDimitris Papastamos 			break;
822bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
832bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
842bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
852bc9a81eSDimitris Papastamos 	else
862bc9a81eSDimitris Papastamos 		c /= 8;
872bc9a81eSDimitris Papastamos 	return c;
882bc9a81eSDimitris Papastamos }
892bc9a81eSDimitris Papastamos 
9013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
9113fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9313fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
942624d5faSMark Brown {
9500b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9600b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9713fd179fSDimitris Papastamos 	int ret;
9813fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
9913fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
10013fd179fSDimitris Papastamos 
10113fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
10213fd179fSDimitris Papastamos 	 * try to abuse this function */
10313fd179fSDimitris Papastamos 	WARN_ON(len > 63);
10413fd179fSDimitris Papastamos 
10513fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10613fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10713fd179fSDimitris Papastamos 		return -EINVAL;
10813fd179fSDimitris Papastamos 
10913fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
11013fd179fSDimitris Papastamos 	if (ret < 0) {
11113fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
11213fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
11313fd179fSDimitris Papastamos 	} else {
11413fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
11513fd179fSDimitris Papastamos 	}
11613fd179fSDimitris Papastamos 
11713fd179fSDimitris Papastamos 	/* prepare the buffer */
11813fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
11913fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
12013fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
12113fd179fSDimitris Papastamos 
12213fd179fSDimitris Papastamos 	return 0;
12313fd179fSDimitris Papastamos }
12413fd179fSDimitris Papastamos 
12513fd179fSDimitris Papastamos /* codec register dump */
12613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
12713fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
12813fd179fSDimitris Papastamos {
12913fd179fSDimitris Papastamos 	int i, step = 1;
1302bc9a81eSDimitris Papastamos 	int wordsize, regsize;
13113fd179fSDimitris Papastamos 	int len;
13213fd179fSDimitris Papastamos 	size_t total = 0;
13313fd179fSDimitris Papastamos 	loff_t p = 0;
1342bc9a81eSDimitris Papastamos 
13500b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
13600b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1372624d5faSMark Brown 
13813fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13913fd179fSDimitris Papastamos 
140f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1412624d5faSMark Brown 		return 0;
1422624d5faSMark Brown 
143f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
144f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1452624d5faSMark Brown 
146f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
147b92d150bSLars-Peter Clausen 		if (!snd_soc_codec_readable_register(codec, i))
1482624d5faSMark Brown 			continue;
149f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
150f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1512624d5faSMark Brown 							 PAGE_SIZE - count, i);
1525164d74dSMark Brown 		} else {
15313fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
15413fd179fSDimitris Papastamos 			 * entries for the default case */
15513fd179fSDimitris Papastamos 			if (p >= pos) {
15613fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1572624d5faSMark Brown 					break;
15813fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
15913fd179fSDimitris Papastamos 				total += len;
16013fd179fSDimitris Papastamos 			}
16113fd179fSDimitris Papastamos 			p += len;
16213fd179fSDimitris Papastamos 		}
1632624d5faSMark Brown 	}
1642624d5faSMark Brown 
16513fd179fSDimitris Papastamos 	total = min(total, count - 1);
1662624d5faSMark Brown 
16713fd179fSDimitris Papastamos 	return total;
1682624d5faSMark Brown }
16913fd179fSDimitris Papastamos 
1702624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1712624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1722624d5faSMark Brown {
173f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
174f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
175f0fba2adSLiam Girdwood 
17613fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1772624d5faSMark Brown }
1782624d5faSMark Brown 
1792624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1802624d5faSMark Brown 
181dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
182dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
183dbe21408SMark Brown {
184f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
185f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
186dbe21408SMark Brown 
187f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
188dbe21408SMark Brown }
189dbe21408SMark Brown 
190dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
191dbe21408SMark Brown 			       struct device_attribute *attr,
192dbe21408SMark Brown 			       const char *buf, size_t count)
193dbe21408SMark Brown {
194f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
195f0fba2adSLiam Girdwood 			container_of(dev, struct snd_soc_pcm_runtime, dev);
196c593b520SMark Brown 	int ret;
197dbe21408SMark Brown 
198c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
199c593b520SMark Brown 	if (ret)
200c593b520SMark Brown 		return ret;
201dbe21408SMark Brown 
202dbe21408SMark Brown 	return count;
203dbe21408SMark Brown }
204dbe21408SMark Brown 
205dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206dbe21408SMark Brown 
2072624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2082624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
2092624d5faSMark Brown {
2102624d5faSMark Brown 	file->private_data = inode->i_private;
2112624d5faSMark Brown 	return 0;
2122624d5faSMark Brown }
2132624d5faSMark Brown 
2142624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2152624d5faSMark Brown 				   size_t count, loff_t *ppos)
2162624d5faSMark Brown {
2172624d5faSMark Brown 	ssize_t ret;
2182624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
21913fd179fSDimitris Papastamos 	char *buf;
22013fd179fSDimitris Papastamos 
22113fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22213fd179fSDimitris Papastamos 		return -EINVAL;
22313fd179fSDimitris Papastamos 
22413fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2252624d5faSMark Brown 	if (!buf)
2262624d5faSMark Brown 		return -ENOMEM;
22713fd179fSDimitris Papastamos 
22813fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
22913fd179fSDimitris Papastamos 	if (ret >= 0) {
23013fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23113fd179fSDimitris Papastamos 			kfree(buf);
23213fd179fSDimitris Papastamos 			return -EFAULT;
23313fd179fSDimitris Papastamos 		}
23413fd179fSDimitris Papastamos 		*ppos += ret;
23513fd179fSDimitris Papastamos 	}
23613fd179fSDimitris Papastamos 
2372624d5faSMark Brown 	kfree(buf);
2382624d5faSMark Brown 	return ret;
2392624d5faSMark Brown }
2402624d5faSMark Brown 
2412624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2422624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2432624d5faSMark Brown {
2442624d5faSMark Brown 	char buf[32];
24534e268d8SStephen Boyd 	size_t buf_size;
2462624d5faSMark Brown 	char *start = buf;
2472624d5faSMark Brown 	unsigned long reg, value;
2482624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2492624d5faSMark Brown 
2502624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2512624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2522624d5faSMark Brown 		return -EFAULT;
2532624d5faSMark Brown 	buf[buf_size] = 0;
2542624d5faSMark Brown 
2552624d5faSMark Brown 	while (*start == ' ')
2562624d5faSMark Brown 		start++;
2572624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2582624d5faSMark Brown 	while (*start == ' ')
2592624d5faSMark Brown 		start++;
2602624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2612624d5faSMark Brown 		return -EINVAL;
2620d51a9cbSMark Brown 
2630d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2640d51a9cbSMark Brown 	add_taint(TAINT_USER);
2650d51a9cbSMark Brown 
266e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2672624d5faSMark Brown 	return buf_size;
2682624d5faSMark Brown }
2692624d5faSMark Brown 
2702624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2712624d5faSMark Brown 	.open = codec_reg_open_file,
2722624d5faSMark Brown 	.read = codec_reg_read_file,
2732624d5faSMark Brown 	.write = codec_reg_write_file,
2746038f373SArnd Bergmann 	.llseek = default_llseek,
2752624d5faSMark Brown };
2762624d5faSMark Brown 
2772624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2782624d5faSMark Brown {
279d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
280d6ce4cf3SJarkko Nikula 
2816ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
282d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2832624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2842624d5faSMark Brown 		printk(KERN_WARNING
2852624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2862624d5faSMark Brown 		return;
2872624d5faSMark Brown 	}
2882624d5faSMark Brown 
289aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
290aaee8ef1SMark Brown 			    &codec->cache_sync);
291aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
292aaee8ef1SMark Brown 			    &codec->cache_only);
293aaee8ef1SMark Brown 
2942624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2952624d5faSMark Brown 						 codec->debugfs_codec_root,
2962624d5faSMark Brown 						 codec, &codec_reg_fops);
2972624d5faSMark Brown 	if (!codec->debugfs_reg)
2982624d5faSMark Brown 		printk(KERN_WARNING
2992624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
3002624d5faSMark Brown 
3018eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
3022624d5faSMark Brown }
3032624d5faSMark Brown 
3042624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3052624d5faSMark Brown {
3062624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3072624d5faSMark Brown }
3082624d5faSMark Brown 
309c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
310c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
311c3c5a19aSMark Brown {
312c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3132b194f9dSMark Brown 	ssize_t len, ret = 0;
314c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
315c3c5a19aSMark Brown 
316c3c5a19aSMark Brown 	if (!buf)
317c3c5a19aSMark Brown 		return -ENOMEM;
318c3c5a19aSMark Brown 
3192b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3202b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
321c3c5a19aSMark Brown 			       codec->name);
3222b194f9dSMark Brown 		if (len >= 0)
3232b194f9dSMark Brown 			ret += len;
3242b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3252b194f9dSMark Brown 			ret = PAGE_SIZE;
3262b194f9dSMark Brown 			break;
3272b194f9dSMark Brown 		}
3282b194f9dSMark Brown 	}
329c3c5a19aSMark Brown 
330c3c5a19aSMark Brown 	if (ret >= 0)
331c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
332c3c5a19aSMark Brown 
333c3c5a19aSMark Brown 	kfree(buf);
334c3c5a19aSMark Brown 
335c3c5a19aSMark Brown 	return ret;
336c3c5a19aSMark Brown }
337c3c5a19aSMark Brown 
338c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
339c3c5a19aSMark Brown 	.read = codec_list_read_file,
340c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
341c3c5a19aSMark Brown };
342c3c5a19aSMark Brown 
343f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
344f3208780SMark Brown 				  size_t count, loff_t *ppos)
345f3208780SMark Brown {
346f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3472b194f9dSMark Brown 	ssize_t len, ret = 0;
348f3208780SMark Brown 	struct snd_soc_dai *dai;
349f3208780SMark Brown 
350f3208780SMark Brown 	if (!buf)
351f3208780SMark Brown 		return -ENOMEM;
352f3208780SMark Brown 
3532b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3542b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3552b194f9dSMark Brown 		if (len >= 0)
3562b194f9dSMark Brown 			ret += len;
3572b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3582b194f9dSMark Brown 			ret = PAGE_SIZE;
3592b194f9dSMark Brown 			break;
3602b194f9dSMark Brown 		}
3612b194f9dSMark Brown 	}
362f3208780SMark Brown 
363f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
364f3208780SMark Brown 
365f3208780SMark Brown 	kfree(buf);
366f3208780SMark Brown 
367f3208780SMark Brown 	return ret;
368f3208780SMark Brown }
369f3208780SMark Brown 
370f3208780SMark Brown static const struct file_operations dai_list_fops = {
371f3208780SMark Brown 	.read = dai_list_read_file,
372f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
373f3208780SMark Brown };
374f3208780SMark Brown 
37519c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
37619c7ac27SMark Brown 				       char __user *user_buf,
37719c7ac27SMark Brown 				       size_t count, loff_t *ppos)
37819c7ac27SMark Brown {
37919c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3802b194f9dSMark Brown 	ssize_t len, ret = 0;
38119c7ac27SMark Brown 	struct snd_soc_platform *platform;
38219c7ac27SMark Brown 
38319c7ac27SMark Brown 	if (!buf)
38419c7ac27SMark Brown 		return -ENOMEM;
38519c7ac27SMark Brown 
3862b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3872b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
38819c7ac27SMark Brown 			       platform->name);
3892b194f9dSMark Brown 		if (len >= 0)
3902b194f9dSMark Brown 			ret += len;
3912b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3922b194f9dSMark Brown 			ret = PAGE_SIZE;
3932b194f9dSMark Brown 			break;
3942b194f9dSMark Brown 		}
3952b194f9dSMark Brown 	}
39619c7ac27SMark Brown 
39719c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
39819c7ac27SMark Brown 
39919c7ac27SMark Brown 	kfree(buf);
40019c7ac27SMark Brown 
40119c7ac27SMark Brown 	return ret;
40219c7ac27SMark Brown }
40319c7ac27SMark Brown 
40419c7ac27SMark Brown static const struct file_operations platform_list_fops = {
40519c7ac27SMark Brown 	.read = platform_list_read_file,
40619c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
40719c7ac27SMark Brown };
40819c7ac27SMark Brown 
409a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
410a6052154SJarkko Nikula {
411a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4128a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4133a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
414a6052154SJarkko Nikula 		dev_warn(card->dev,
415a6052154SJarkko Nikula 			 "ASoC: Failed to create codec debugfs directory\n");
4163a45b867SJarkko Nikula 		return;
4173a45b867SJarkko Nikula 	}
4183a45b867SJarkko Nikula 
4193a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4203a45b867SJarkko Nikula 						    card->debugfs_card_root,
4213a45b867SJarkko Nikula 						    &card->pop_time);
4223a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4233a45b867SJarkko Nikula 		dev_warn(card->dev,
4243a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
425a6052154SJarkko Nikula }
426a6052154SJarkko Nikula 
427a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
428a6052154SJarkko Nikula {
429a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
430a6052154SJarkko Nikula }
431a6052154SJarkko Nikula 
4322624d5faSMark Brown #else
4332624d5faSMark Brown 
4342624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4352624d5faSMark Brown {
4362624d5faSMark Brown }
4372624d5faSMark Brown 
4382624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4392624d5faSMark Brown {
4402624d5faSMark Brown }
441b95fccbcSAxel Lin 
442b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
443b95fccbcSAxel Lin {
444b95fccbcSAxel Lin }
445b95fccbcSAxel Lin 
446b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
447b95fccbcSAxel Lin {
448b95fccbcSAxel Lin }
4492624d5faSMark Brown #endif
4502624d5faSMark Brown 
451db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
452db2a4165SFrank Mandarino /* unregister ac97 codec */
453db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
454db2a4165SFrank Mandarino {
455db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
456db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
457db2a4165SFrank Mandarino 	return 0;
458db2a4165SFrank Mandarino }
459db2a4165SFrank Mandarino 
460db2a4165SFrank Mandarino /* stop no dev release warning */
461db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
462db2a4165SFrank Mandarino 
463db2a4165SFrank Mandarino /* register ac97 codec to bus */
464db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
465db2a4165SFrank Mandarino {
466db2a4165SFrank Mandarino 	int err;
467db2a4165SFrank Mandarino 
468db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4694ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
470db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
471db2a4165SFrank Mandarino 
472bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
473f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
474db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
475db2a4165SFrank Mandarino 	if (err < 0) {
476db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
477db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
478db2a4165SFrank Mandarino 		return err;
479db2a4165SFrank Mandarino 	}
480db2a4165SFrank Mandarino 	return 0;
481db2a4165SFrank Mandarino }
482db2a4165SFrank Mandarino #endif
483db2a4165SFrank Mandarino 
4846f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
485db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4866f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
487db2a4165SFrank Mandarino {
4886f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
4892eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
490db2a4165SFrank Mandarino 	int i;
491db2a4165SFrank Mandarino 
492e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
493e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
494e3509ff0SDaniel Mack 	 */
495f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
496e3509ff0SDaniel Mack 		return 0;
497e3509ff0SDaniel Mack 
4986ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
4996ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5006ed25978SAndy Green 	 */
501f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
502f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
503f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5046ed25978SAndy Green 
5056ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
506f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5076ed25978SAndy Green 
508a00f90f9SMark Brown 	/* mute any active DACs */
509f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
510f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
511f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5123efab7dcSMark Brown 
513f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5143efab7dcSMark Brown 			continue;
5153efab7dcSMark Brown 
516f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
517f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
518db2a4165SFrank Mandarino 	}
519db2a4165SFrank Mandarino 
5204ccab3e7SLiam Girdwood 	/* suspend all pcms */
521f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
522f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5233efab7dcSMark Brown 			continue;
5243efab7dcSMark Brown 
525f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5263efab7dcSMark Brown 	}
5274ccab3e7SLiam Girdwood 
52887506549SMark Brown 	if (card->suspend_pre)
52970b2ac12SMark Brown 		card->suspend_pre(card);
530db2a4165SFrank Mandarino 
531f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
532f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
533f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5343efab7dcSMark Brown 
535f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5363efab7dcSMark Brown 			continue;
5373efab7dcSMark Brown 
538f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
539f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
540f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
541f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
542f0fba2adSLiam Girdwood 			platform->suspended = 1;
543f0fba2adSLiam Girdwood 		}
544db2a4165SFrank Mandarino 	}
545db2a4165SFrank Mandarino 
546db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
547f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5485b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
549ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
550f0fba2adSLiam Girdwood 	}
551db2a4165SFrank Mandarino 
552f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
553f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
5543efab7dcSMark Brown 
555f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5563efab7dcSMark Brown 			continue;
5573efab7dcSMark Brown 
558f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
559f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
560db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
561f0fba2adSLiam Girdwood 
562f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
563f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
564db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
565db2a4165SFrank Mandarino 	}
566db2a4165SFrank Mandarino 
567f0fba2adSLiam Girdwood 	/* suspend all CODECs */
5682eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
5691547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
5701547aba9SMark Brown 		 * bias _ON and should not be suspended. */
571f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
572ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
5731547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
5741547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
575f0fba2adSLiam Girdwood 				codec->driver->suspend(codec, PMSG_SUSPEND);
576f0fba2adSLiam Girdwood 				codec->suspended = 1;
5777be4ba24SMark Brown 				codec->cache_sync = 1;
5781547aba9SMark Brown 				break;
5791547aba9SMark Brown 			default:
580f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
5811547aba9SMark Brown 				break;
5821547aba9SMark Brown 			}
5831547aba9SMark Brown 		}
584f0fba2adSLiam Girdwood 	}
585db2a4165SFrank Mandarino 
586f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
587f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
5883efab7dcSMark Brown 
589f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5903efab7dcSMark Brown 			continue;
5913efab7dcSMark Brown 
592f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
593f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
594db2a4165SFrank Mandarino 	}
595db2a4165SFrank Mandarino 
59687506549SMark Brown 	if (card->suspend_post)
59770b2ac12SMark Brown 		card->suspend_post(card);
598db2a4165SFrank Mandarino 
599db2a4165SFrank Mandarino 	return 0;
600db2a4165SFrank Mandarino }
6016f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
602db2a4165SFrank Mandarino 
6036ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6046ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6056ed25978SAndy Green  */
6066ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
607db2a4165SFrank Mandarino {
608f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
609f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6102eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
611db2a4165SFrank Mandarino 	int i;
612db2a4165SFrank Mandarino 
6136ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6146ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6156ed25978SAndy Green 	 */
6166ed25978SAndy Green 
617f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
6186ed25978SAndy Green 
6199949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
620f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6219949788bSMark Brown 
62287506549SMark Brown 	if (card->resume_pre)
62370b2ac12SMark Brown 		card->resume_pre(card);
624db2a4165SFrank Mandarino 
625f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
626f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
627f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6283efab7dcSMark Brown 
629f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6303efab7dcSMark Brown 			continue;
6313efab7dcSMark Brown 
632f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
633f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
634db2a4165SFrank Mandarino 	}
635db2a4165SFrank Mandarino 
6362eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6371547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6381547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
6391547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
6401547aba9SMark Brown 		 */
641f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
642ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6431547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
6441547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
645f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
646f0fba2adSLiam Girdwood 				codec->suspended = 0;
6471547aba9SMark Brown 				break;
6481547aba9SMark Brown 			default:
649f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
6501547aba9SMark Brown 				break;
6511547aba9SMark Brown 			}
6521547aba9SMark Brown 		}
653f0fba2adSLiam Girdwood 	}
654db2a4165SFrank Mandarino 
655f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
656f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
6573efab7dcSMark Brown 
658f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6593efab7dcSMark Brown 			continue;
6603efab7dcSMark Brown 
661f0fba2adSLiam Girdwood 		if (driver->playback.stream_name != NULL)
662f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
663db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
664f0fba2adSLiam Girdwood 
665f0fba2adSLiam Girdwood 		if (driver->capture.stream_name != NULL)
666f0fba2adSLiam Girdwood 			snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
667db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
668db2a4165SFrank Mandarino 	}
669db2a4165SFrank Mandarino 
6703ff3f64bSMark Brown 	/* unmute any active DACs */
671f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
672f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
673f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
6743efab7dcSMark Brown 
675f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6763efab7dcSMark Brown 			continue;
6773efab7dcSMark Brown 
678f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
679f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
680db2a4165SFrank Mandarino 	}
681db2a4165SFrank Mandarino 
682f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
683f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
684f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
6853efab7dcSMark Brown 
686f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6873efab7dcSMark Brown 			continue;
6883efab7dcSMark Brown 
689f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
690f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
691f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
692f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
693f0fba2adSLiam Girdwood 			platform->suspended = 0;
694f0fba2adSLiam Girdwood 		}
695db2a4165SFrank Mandarino 	}
696db2a4165SFrank Mandarino 
69787506549SMark Brown 	if (card->resume_post)
69870b2ac12SMark Brown 		card->resume_post(card);
699db2a4165SFrank Mandarino 
700f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
7016ed25978SAndy Green 
7026ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
703f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7046ed25978SAndy Green }
7056ed25978SAndy Green 
7066ed25978SAndy Green /* powers up audio subsystem after a suspend */
7076f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7086ed25978SAndy Green {
7096f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
71082e14e8bSStephen Warren 	int i, ac97_control = 0;
711b9dd94a8SPeter Ujfalusi 
71264ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
71364ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
71464ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
71564ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
71664ab9baaSMark Brown 	 */
717f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
718f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
71982e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
72082e14e8bSStephen Warren 	}
72182e14e8bSStephen Warren 	if (ac97_control) {
722f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
72364ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
72464ab9baaSMark Brown 	} else {
725f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
7266308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
727f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
728f0fba2adSLiam Girdwood 	}
7296ed25978SAndy Green 
730db2a4165SFrank Mandarino 	return 0;
731db2a4165SFrank Mandarino }
7326f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
733db2a4165SFrank Mandarino #else
7346f8ab4acSMark Brown #define snd_soc_suspend NULL
7356f8ab4acSMark Brown #define snd_soc_resume NULL
736db2a4165SFrank Mandarino #endif
737db2a4165SFrank Mandarino 
73802a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
73902a06d30SBarry Song };
74002a06d30SBarry Song 
741f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
742db2a4165SFrank Mandarino {
743f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
744f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
745fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
746435c5e25SMark Brown 	struct snd_soc_platform *platform;
747f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
748848dd8beSMark Brown 	const char *platform_name;
749db2a4165SFrank Mandarino 
750f0fba2adSLiam Girdwood 	if (rtd->complete)
751f0fba2adSLiam Girdwood 		return 1;
752f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
7536308419aSMark Brown 
754f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
755f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
756f0fba2adSLiam Girdwood 		goto find_codec;
757435c5e25SMark Brown 	}
758f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
759f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
760f0fba2adSLiam Girdwood 		if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
761f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
762f0fba2adSLiam Girdwood 			goto find_codec;
763f0fba2adSLiam Girdwood 		}
764f0fba2adSLiam Girdwood 	}
765f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
766f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
767f0fba2adSLiam Girdwood 
768f0fba2adSLiam Girdwood find_codec:
769f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
770f0fba2adSLiam Girdwood 	if (rtd->codec) {
771f0fba2adSLiam Girdwood 		goto find_platform;
772c5af3a2eSMark Brown 	}
773c5af3a2eSMark Brown 
774f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
775f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
776f0fba2adSLiam Girdwood 		if (!strcmp(codec->name, dai_link->codec_name)) {
777f0fba2adSLiam Girdwood 			rtd->codec = codec;
778f0fba2adSLiam Girdwood 
779f0fba2adSLiam Girdwood 			/* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
780f0fba2adSLiam Girdwood 			list_for_each_entry(codec_dai, &dai_list, list) {
781f0fba2adSLiam Girdwood 				if (codec->dev == codec_dai->dev &&
782f0fba2adSLiam Girdwood 						!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
783f0fba2adSLiam Girdwood 					rtd->codec_dai = codec_dai;
784f0fba2adSLiam Girdwood 					goto find_platform;
785435c5e25SMark Brown 				}
786f0fba2adSLiam Girdwood 			}
787f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
788f0fba2adSLiam Girdwood 					dai_link->codec_dai_name);
789f0fba2adSLiam Girdwood 
790f0fba2adSLiam Girdwood 			goto find_platform;
791f0fba2adSLiam Girdwood 		}
792f0fba2adSLiam Girdwood 	}
793f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
794f0fba2adSLiam Girdwood 			dai_link->codec_name);
795f0fba2adSLiam Girdwood 
796f0fba2adSLiam Girdwood find_platform:
797848dd8beSMark Brown 	/* do we need a platform? */
798848dd8beSMark Brown 	if (rtd->platform)
799f0fba2adSLiam Girdwood 		goto out;
800848dd8beSMark Brown 
801848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
802848dd8beSMark Brown 	platform_name = dai_link->platform_name;
803848dd8beSMark Brown 	if (!platform_name)
804848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
805848dd8beSMark Brown 
806848dd8beSMark Brown 	/* no, then find one from the set of registered platforms */
807f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
808848dd8beSMark Brown 		if (!strcmp(platform->name, platform_name)) {
809f0fba2adSLiam Girdwood 			rtd->platform = platform;
810f0fba2adSLiam Girdwood 			goto out;
811f0fba2adSLiam Girdwood 		}
812435c5e25SMark Brown 	}
8136b05eda6SMark Brown 
814f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
815f0fba2adSLiam Girdwood 			dai_link->platform_name);
816f0fba2adSLiam Girdwood 	return 0;
817f0fba2adSLiam Girdwood 
818f0fba2adSLiam Girdwood out:
819f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
820f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
821f0fba2adSLiam Girdwood 		rtd->complete = 1;
822f0fba2adSLiam Girdwood 		card->num_rtd++;
823f0fba2adSLiam Girdwood 	}
824f0fba2adSLiam Girdwood 	return 1;
8256b05eda6SMark Brown }
8266b05eda6SMark Brown 
827589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
828589c3563SJarkko Nikula {
829589c3563SJarkko Nikula 	int err;
830589c3563SJarkko Nikula 
831589c3563SJarkko Nikula 	if (codec->driver->remove) {
832589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
833589c3563SJarkko Nikula 		if (err < 0)
834589c3563SJarkko Nikula 			dev_err(codec->dev,
835589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
836589c3563SJarkko Nikula 				codec->name, err);
837589c3563SJarkko Nikula 	}
838589c3563SJarkko Nikula 
839589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
840589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
841589c3563SJarkko Nikula 
842589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
843589c3563SJarkko Nikula 	codec->probed = 0;
844589c3563SJarkko Nikula 	list_del(&codec->card_list);
845589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
846589c3563SJarkko Nikula }
847589c3563SJarkko Nikula 
8480168bf0dSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
849f0fba2adSLiam Girdwood {
850f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
851f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
852f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
853f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
854f0fba2adSLiam Girdwood 	int err;
855f0fba2adSLiam Girdwood 
856f0fba2adSLiam Girdwood 	/* unregister the rtd device */
857f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
858f0fba2adSLiam Girdwood 		device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
859589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
860f0fba2adSLiam Girdwood 		device_unregister(&rtd->dev);
861f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
86202a06d30SBarry Song 	}
86302a06d30SBarry Song 
864f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
8650168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
8660168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
867f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
868f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
869f0fba2adSLiam Girdwood 			if (err < 0)
870f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
871f0fba2adSLiam Girdwood 		}
872f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
873f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
874f0fba2adSLiam Girdwood 	}
875f0fba2adSLiam Girdwood 
876f0fba2adSLiam Girdwood 	/* remove the platform */
8770168bf0dSLiam Girdwood 	if (platform && platform->probed &&
8780168bf0dSLiam Girdwood 			platform->driver->remove_order == order) {
879f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
880f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
881f0fba2adSLiam Girdwood 			if (err < 0)
882f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
883f0fba2adSLiam Girdwood 		}
884f0fba2adSLiam Girdwood 		platform->probed = 0;
885f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
886f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
887f0fba2adSLiam Girdwood 	}
888f0fba2adSLiam Girdwood 
889f0fba2adSLiam Girdwood 	/* remove the CODEC */
8900168bf0dSLiam Girdwood 	if (codec && codec->probed &&
8910168bf0dSLiam Girdwood 			codec->driver->remove_order == order)
892589c3563SJarkko Nikula 		soc_remove_codec(codec);
893f0fba2adSLiam Girdwood 
894f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
8950168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
8960168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
897f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
898f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
899f0fba2adSLiam Girdwood 			if (err < 0)
900f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
901f0fba2adSLiam Girdwood 		}
902f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
903f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
904f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
905f0fba2adSLiam Girdwood 	}
906f0fba2adSLiam Girdwood }
907f0fba2adSLiam Girdwood 
9080671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9090671fd8eSKuninori Morimoto {
9100168bf0dSLiam Girdwood 	int dai, order;
9110671fd8eSKuninori Morimoto 
9120168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9130168bf0dSLiam Girdwood 			order++) {
9140168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
9150168bf0dSLiam Girdwood 			soc_remove_dai_link(card, dai, order);
9160168bf0dSLiam Girdwood 	}
9170671fd8eSKuninori Morimoto 	card->num_rtd = 0;
9180671fd8eSKuninori Morimoto }
9190671fd8eSKuninori Morimoto 
920ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
921ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
922ead9b919SJarkko Nikula {
923ead9b919SJarkko Nikula 	int i;
924ead9b919SJarkko Nikula 
925ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
926ead9b919SJarkko Nikula 		return;
927ead9b919SJarkko Nikula 
928ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
929ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
930ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
931ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
932ead9b919SJarkko Nikula 			break;
933ead9b919SJarkko Nikula 		}
934ead9b919SJarkko Nikula 	}
935ead9b919SJarkko Nikula }
936ead9b919SJarkko Nikula 
937589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
938589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
939589c3563SJarkko Nikula {
940589c3563SJarkko Nikula 	int ret = 0;
94189b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
942589c3563SJarkko Nikula 
943589c3563SJarkko Nikula 	codec->card = card;
944589c3563SJarkko Nikula 	codec->dapm.card = card;
945589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
946589c3563SJarkko Nikula 
94770d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
94870d29331SJarkko Nikula 		return -ENODEV;
94970d29331SJarkko Nikula 
950d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
951d5d1e0beSLars-Peter Clausen 
95277530150SLars-Peter Clausen 	if (driver->dapm_widgets)
95377530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
95477530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
95577530150SLars-Peter Clausen 
95633c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
95733c5f969SMark Brown 
95889b95ac0SMark Brown 	if (driver->probe) {
95989b95ac0SMark Brown 		ret = driver->probe(codec);
960589c3563SJarkko Nikula 		if (ret < 0) {
961589c3563SJarkko Nikula 			dev_err(codec->dev,
962589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
963589c3563SJarkko Nikula 				codec->name, ret);
96470d29331SJarkko Nikula 			goto err_probe;
965589c3563SJarkko Nikula 		}
966589c3563SJarkko Nikula 	}
967589c3563SJarkko Nikula 
968b7af1dafSMark Brown 	if (driver->controls)
969b7af1dafSMark Brown 		snd_soc_add_controls(codec, driver->controls,
970b7af1dafSMark Brown 				     driver->num_controls);
97189b95ac0SMark Brown 	if (driver->dapm_routes)
97289b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
97389b95ac0SMark Brown 					driver->num_dapm_routes);
97489b95ac0SMark Brown 
975589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
976589c3563SJarkko Nikula 	codec->probed = 1;
977589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
9787be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
979589c3563SJarkko Nikula 
98070d29331SJarkko Nikula 	return 0;
98170d29331SJarkko Nikula 
98270d29331SJarkko Nikula err_probe:
983d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
98470d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
98570d29331SJarkko Nikula 
986589c3563SJarkko Nikula 	return ret;
987589c3563SJarkko Nikula }
988589c3563SJarkko Nikula 
989956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
990956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
991956245e9SLiam Girdwood {
992956245e9SLiam Girdwood 	int ret = 0;
993956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
994956245e9SLiam Girdwood 
995956245e9SLiam Girdwood 	platform->card = card;
996b7950641SLiam Girdwood 	platform->dapm.card = card;
997956245e9SLiam Girdwood 
998956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
999956245e9SLiam Girdwood 		return -ENODEV;
1000956245e9SLiam Girdwood 
1001cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1002cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1003cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1004cb2cf612SLiam Girdwood 
1005956245e9SLiam Girdwood 	if (driver->probe) {
1006956245e9SLiam Girdwood 		ret = driver->probe(platform);
1007956245e9SLiam Girdwood 		if (ret < 0) {
1008956245e9SLiam Girdwood 			dev_err(platform->dev,
1009956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1010956245e9SLiam Girdwood 				platform->name, ret);
1011956245e9SLiam Girdwood 			goto err_probe;
1012956245e9SLiam Girdwood 		}
1013956245e9SLiam Girdwood 	}
1014956245e9SLiam Girdwood 
1015cb2cf612SLiam Girdwood 	if (driver->controls)
1016cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1017cb2cf612SLiam Girdwood 				     driver->num_controls);
1018cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1019cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1020cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1021cb2cf612SLiam Girdwood 
1022956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1023956245e9SLiam Girdwood 	platform->probed = 1;
1024956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1025b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1026956245e9SLiam Girdwood 
1027956245e9SLiam Girdwood 	return 0;
1028956245e9SLiam Girdwood 
1029956245e9SLiam Girdwood err_probe:
1030956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1031956245e9SLiam Girdwood 
1032956245e9SLiam Girdwood 	return ret;
1033956245e9SLiam Girdwood }
1034956245e9SLiam Girdwood 
1035f0fba2adSLiam Girdwood static void rtd_release(struct device *dev) {}
1036f0fba2adSLiam Girdwood 
1037589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1038589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1039589c3563SJarkko Nikula 				   int num, int dailess)
1040589c3563SJarkko Nikula {
1041589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1042589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1043589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1044589c3563SJarkko Nikula 	const char *temp, *name;
1045589c3563SJarkko Nikula 	int ret = 0;
1046589c3563SJarkko Nikula 
1047589c3563SJarkko Nikula 	if (!dailess) {
1048589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1049589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1050589c3563SJarkko Nikula 		name = dai_link->name;
1051589c3563SJarkko Nikula 	} else {
1052589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1053589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1054589c3563SJarkko Nikula 		name = aux_dev->name;
1055589c3563SJarkko Nikula 	}
10560962bb21SJanusz Krzysztofik 	rtd->card = card;
1057589c3563SJarkko Nikula 
1058*4b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
1059*4b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
1060*4b1cfcb4SMark Brown 
1061589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1062589c3563SJarkko Nikula 	temp = codec->name_prefix;
1063589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1064589c3563SJarkko Nikula 
1065589c3563SJarkko Nikula 	/* do machine specific initialization */
1066589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1067589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1068589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1069589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1070589c3563SJarkko Nikula 	if (ret < 0) {
1071589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1072589c3563SJarkko Nikula 		return ret;
1073589c3563SJarkko Nikula 	}
1074589c3563SJarkko Nikula 	codec->name_prefix = temp;
1075589c3563SJarkko Nikula 
1076589c3563SJarkko Nikula 	/* register the rtd device */
1077589c3563SJarkko Nikula 	rtd->codec = codec;
1078589c3563SJarkko Nikula 	rtd->dev.parent = card->dev;
1079589c3563SJarkko Nikula 	rtd->dev.release = rtd_release;
1080589c3563SJarkko Nikula 	rtd->dev.init_name = name;
1081b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
1082589c3563SJarkko Nikula 	ret = device_register(&rtd->dev);
1083589c3563SJarkko Nikula 	if (ret < 0) {
1084589c3563SJarkko Nikula 		dev_err(card->dev,
1085589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1086589c3563SJarkko Nikula 		return ret;
1087589c3563SJarkko Nikula 	}
1088589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1089589c3563SJarkko Nikula 
1090589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
1091589c3563SJarkko Nikula 	ret = snd_soc_dapm_sys_add(&rtd->dev);
1092589c3563SJarkko Nikula 	if (ret < 0)
1093589c3563SJarkko Nikula 		dev_err(codec->dev,
1094589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1095589c3563SJarkko Nikula 			ret);
1096589c3563SJarkko Nikula 
1097589c3563SJarkko Nikula 	/* add codec sysfs entries */
1098589c3563SJarkko Nikula 	ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1099589c3563SJarkko Nikula 	if (ret < 0)
1100589c3563SJarkko Nikula 		dev_err(codec->dev,
1101589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1102589c3563SJarkko Nikula 
1103589c3563SJarkko Nikula 	return 0;
1104589c3563SJarkko Nikula }
1105589c3563SJarkko Nikula 
11060168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1107f0fba2adSLiam Girdwood {
1108f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1109f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1110f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1111f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1112f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1113f0fba2adSLiam Girdwood 	int ret;
1114f0fba2adSLiam Girdwood 
11150168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
11160168bf0dSLiam Girdwood 			card->name, num, order);
1117f0fba2adSLiam Girdwood 
1118f0fba2adSLiam Girdwood 	/* config components */
1119f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1120f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1121f0fba2adSLiam Girdwood 	codec_dai->card = card;
1122f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1123f0fba2adSLiam Girdwood 
1124f0fba2adSLiam Girdwood 	/* set default power off timeout */
1125f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1126f0fba2adSLiam Girdwood 
1127f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
11280168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
11290168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
113061b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
113161b61e3cSLiam Girdwood 			return -ENODEV;
113261b61e3cSLiam Girdwood 
1133f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1134f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1135f0fba2adSLiam Girdwood 			if (ret < 0) {
1136f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1137f0fba2adSLiam Girdwood 						cpu_dai->name);
113861b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1139f0fba2adSLiam Girdwood 				return ret;
1140f0fba2adSLiam Girdwood 			}
1141f0fba2adSLiam Girdwood 		}
1142f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
11431c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1144f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1145f0fba2adSLiam Girdwood 	}
1146f0fba2adSLiam Girdwood 
1147f0fba2adSLiam Girdwood 	/* probe the CODEC */
11480168bf0dSLiam Girdwood 	if (!codec->probed &&
11490168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1150589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1151589c3563SJarkko Nikula 		if (ret < 0)
1152f0fba2adSLiam Girdwood 			return ret;
1153f0fba2adSLiam Girdwood 	}
1154f0fba2adSLiam Girdwood 
1155f0fba2adSLiam Girdwood 	/* probe the platform */
11560168bf0dSLiam Girdwood 	if (!platform->probed &&
11570168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1158956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1159956245e9SLiam Girdwood 		if (ret < 0)
1160f0fba2adSLiam Girdwood 			return ret;
1161f0fba2adSLiam Girdwood 	}
1162f0fba2adSLiam Girdwood 
1163f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
11640168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1165f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1166f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1167f0fba2adSLiam Girdwood 			if (ret < 0) {
1168f0fba2adSLiam Girdwood 				printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1169f0fba2adSLiam Girdwood 						codec_dai->name);
1170f0fba2adSLiam Girdwood 				return ret;
1171f0fba2adSLiam Girdwood 			}
1172f0fba2adSLiam Girdwood 		}
1173f0fba2adSLiam Girdwood 
11741c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1175f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1176f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1177f0fba2adSLiam Girdwood 	}
1178f0fba2adSLiam Girdwood 
11790168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
11800168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
11810168bf0dSLiam Girdwood 		return 0;
11820168bf0dSLiam Girdwood 
1183589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1184589c3563SJarkko Nikula 	if (ret)
1185f0fba2adSLiam Girdwood 		return ret;
1186f0fba2adSLiam Girdwood 
1187f0fba2adSLiam Girdwood 	ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1188f0fba2adSLiam Girdwood 	if (ret < 0)
1189f0fba2adSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1190f0fba2adSLiam Girdwood 
1191f0fba2adSLiam Girdwood 	/* create the pcm */
1192f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1193f0fba2adSLiam Girdwood 	if (ret < 0) {
1194f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1195f0fba2adSLiam Girdwood 		return ret;
1196f0fba2adSLiam Girdwood 	}
1197f0fba2adSLiam Girdwood 
1198f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1199f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1200f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1201f0fba2adSLiam Girdwood 
1202f0fba2adSLiam Girdwood 	return 0;
1203f0fba2adSLiam Girdwood }
1204f0fba2adSLiam Girdwood 
1205f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1206f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1207f0fba2adSLiam Girdwood {
1208f0fba2adSLiam Girdwood 	int ret;
1209f0fba2adSLiam Girdwood 
1210f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1211f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
12126b05eda6SMark Brown 	 */
1213f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
12140562f788SMika Westerberg 		/*
12150562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
12160562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
12170562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
12180562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
12190562f788SMika Westerberg 		 *
12200562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
12210562f788SMika Westerberg 		 */
12220562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
12230562f788SMika Westerberg 			return 0;
1224f0fba2adSLiam Girdwood 
1225f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1226f0fba2adSLiam Girdwood 		if (ret < 0) {
1227f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1228f0fba2adSLiam Girdwood 			return ret;
1229435c5e25SMark Brown 		}
1230435c5e25SMark Brown 
1231f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1232f0fba2adSLiam Girdwood 	}
1233f0fba2adSLiam Girdwood 	return 0;
1234f0fba2adSLiam Girdwood }
1235435c5e25SMark Brown 
1236f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1237f0fba2adSLiam Girdwood {
1238f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1239f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1240f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1241f0fba2adSLiam Girdwood 	}
1242f0fba2adSLiam Girdwood }
1243f0fba2adSLiam Girdwood #endif
1244435c5e25SMark Brown 
12452eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
12462eea392dSJarkko Nikula {
12472eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
12482eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1249676ad98aSJarkko Nikula 	int ret = -ENODEV;
12502eea392dSJarkko Nikula 
12512eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
12522eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
12532eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
12542eea392dSJarkko Nikula 			if (codec->probed) {
12552eea392dSJarkko Nikula 				dev_err(codec->dev,
12562eea392dSJarkko Nikula 					"asoc: codec already probed");
12572eea392dSJarkko Nikula 				ret = -EBUSY;
12582eea392dSJarkko Nikula 				goto out;
12592eea392dSJarkko Nikula 			}
1260676ad98aSJarkko Nikula 			goto found;
12612eea392dSJarkko Nikula 		}
12622eea392dSJarkko Nikula 	}
1263676ad98aSJarkko Nikula 	/* codec not found */
1264676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1265676ad98aSJarkko Nikula 	goto out;
12662eea392dSJarkko Nikula 
1267676ad98aSJarkko Nikula found:
1268589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
12692eea392dSJarkko Nikula 	if (ret < 0)
1270589c3563SJarkko Nikula 		return ret;
12712eea392dSJarkko Nikula 
1272589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
12732eea392dSJarkko Nikula 
12742eea392dSJarkko Nikula out:
12752eea392dSJarkko Nikula 	return ret;
12762eea392dSJarkko Nikula }
12772eea392dSJarkko Nikula 
12782eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
12792eea392dSJarkko Nikula {
12802eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
12812eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
12822eea392dSJarkko Nikula 
12832eea392dSJarkko Nikula 	/* unregister the rtd device */
12842eea392dSJarkko Nikula 	if (rtd->dev_registered) {
1285589c3563SJarkko Nikula 		device_remove_file(&rtd->dev, &dev_attr_codec_reg);
12862eea392dSJarkko Nikula 		device_unregister(&rtd->dev);
12872eea392dSJarkko Nikula 		rtd->dev_registered = 0;
12882eea392dSJarkko Nikula 	}
12892eea392dSJarkko Nikula 
1290589c3563SJarkko Nikula 	if (codec && codec->probed)
1291589c3563SJarkko Nikula 		soc_remove_codec(codec);
12922eea392dSJarkko Nikula }
12932eea392dSJarkko Nikula 
1294fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1295fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1296fdf0f54dSDimitris Papastamos {
1297fdf0f54dSDimitris Papastamos 	int ret;
1298fdf0f54dSDimitris Papastamos 
1299fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1300fdf0f54dSDimitris Papastamos 		return 0;
1301fdf0f54dSDimitris Papastamos 
1302fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1303fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1304fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1305fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1306fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1307fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1308fdf0f54dSDimitris Papastamos 			ret);
1309fdf0f54dSDimitris Papastamos 		return ret;
1310fdf0f54dSDimitris Papastamos 	}
1311fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1312fdf0f54dSDimitris Papastamos 	return 0;
1313fdf0f54dSDimitris Papastamos }
1314fdf0f54dSDimitris Papastamos 
1315f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1316f0fba2adSLiam Girdwood {
1317fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1318fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1319fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
132075d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
13210168bf0dSLiam Girdwood 	int ret, i, order;
132296dd3622SMark Brown 
1323f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1324f0fba2adSLiam Girdwood 
1325f0fba2adSLiam Girdwood 	if (card->instantiated) {
1326f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1327435c5e25SMark Brown 		return;
1328db2a4165SFrank Mandarino 	}
1329db2a4165SFrank Mandarino 
1330f0fba2adSLiam Girdwood 	/* bind DAIs */
1331f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1332f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1333f0fba2adSLiam Girdwood 
1334f0fba2adSLiam Girdwood 	/* bind completed ? */
1335f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1336f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1337f0fba2adSLiam Girdwood 		return;
1338db2a4165SFrank Mandarino 	}
1339db2a4165SFrank Mandarino 
1340fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1341fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1342fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1343fdf0f54dSDimitris Papastamos 			continue;
1344861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1345861f2fafSDimitris Papastamos 		compress_type = 0;
1346fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1347fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1348fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1349fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1350fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1351fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1352fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1353fdf0f54dSDimitris Papastamos 					break;
1354fdf0f54dSDimitris Papastamos 			}
1355fdf0f54dSDimitris Papastamos 		}
1356fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1357fdf0f54dSDimitris Papastamos 		if (ret < 0) {
1358fdf0f54dSDimitris Papastamos 			mutex_unlock(&card->mutex);
1359fdf0f54dSDimitris Papastamos 			return;
1360fdf0f54dSDimitris Papastamos 		}
1361fdf0f54dSDimitris Papastamos 	}
1362fdf0f54dSDimitris Papastamos 
1363f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1364f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1365f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1366f0fba2adSLiam Girdwood 	if (ret < 0) {
1367f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1368f0fba2adSLiam Girdwood 			card->name);
1369f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1370f0fba2adSLiam Girdwood 		return;
1371db2a4165SFrank Mandarino 	}
1372f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1373db2a4165SFrank Mandarino 
1374e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1375e37a4970SMark Brown 	card->dapm.dev = card->dev;
1376e37a4970SMark Brown 	card->dapm.card = card;
1377e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1378e37a4970SMark Brown 
1379d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1380d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1381d5d1e0beSLars-Peter Clausen #endif
1382d5d1e0beSLars-Peter Clausen 
138388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
13846ed25978SAndy Green 	/* deferred resume work */
13856308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
13861301a964SRandy Dunlap #endif
13876ed25978SAndy Green 
13889a841ebbSMark Brown 	if (card->dapm_widgets)
13899a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
13909a841ebbSMark Brown 					  card->num_dapm_widgets);
13919a841ebbSMark Brown 
1392f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1393f0fba2adSLiam Girdwood 	if (card->probe) {
1394e7361ec4SMark Brown 		ret = card->probe(card);
1395f0fba2adSLiam Girdwood 		if (ret < 0)
1396f0fba2adSLiam Girdwood 			goto card_probe_error;
1397f0fba2adSLiam Girdwood 	}
1398f0fba2adSLiam Girdwood 
13990168bf0dSLiam Girdwood 	/* early DAI link probe */
14000168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
14010168bf0dSLiam Girdwood 			order++) {
1402fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
14030168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1404fe3e78e0SMark Brown 			if (ret < 0) {
1405321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1406321de0d0SMark Brown 			       card->name, ret);
1407f0fba2adSLiam Girdwood 				goto probe_dai_err;
1408fe3e78e0SMark Brown 			}
1409fe3e78e0SMark Brown 		}
14100168bf0dSLiam Girdwood 	}
1411fe3e78e0SMark Brown 
14122eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
14132eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
14142eea392dSJarkko Nikula 		if (ret < 0) {
14152eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
14162eea392dSJarkko Nikula 			       card->name, ret);
14172eea392dSJarkko Nikula 			goto probe_aux_dev_err;
14182eea392dSJarkko Nikula 		}
14192eea392dSJarkko Nikula 	}
14202eea392dSJarkko Nikula 
1421b7af1dafSMark Brown 	/* We should have a non-codec control add function but we don't */
1422b7af1dafSMark Brown 	if (card->controls)
1423b7af1dafSMark Brown 		snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1424b7af1dafSMark Brown 						      struct snd_soc_codec,
1425b7af1dafSMark Brown 						      card_list),
1426b7af1dafSMark Brown 				     card->controls,
1427b7af1dafSMark Brown 				     card->num_controls);
1428b7af1dafSMark Brown 
1429b8ad29deSMark Brown 	if (card->dapm_routes)
1430b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1431b8ad29deSMark Brown 					card->num_dapm_routes);
1432b8ad29deSMark Brown 
1433b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1434b90d2f90SMark Brown 
143575d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
143675d9ac46SMark Brown 		dai_link = &card->dai_link[i];
143775d9ac46SMark Brown 
143875d9ac46SMark Brown 		if (dai_link->dai_fmt) {
143975d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
144075d9ac46SMark Brown 						  dai_link->dai_fmt);
144175d9ac46SMark Brown 			if (ret != 0)
144275d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
144375d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
144475d9ac46SMark Brown 					 ret);
144575d9ac46SMark Brown 
144675d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
144775d9ac46SMark Brown 						  dai_link->dai_fmt);
144875d9ac46SMark Brown 			if (ret != 0)
144975d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
145075d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
145175d9ac46SMark Brown 					 ret);
145275d9ac46SMark Brown 		}
145375d9ac46SMark Brown 	}
145475d9ac46SMark Brown 
1455f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1456fe3e78e0SMark Brown 		 "%s", card->name);
1457f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
145822de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1459f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1460f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1461f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1462f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1463f0e8ed85SMark Brown 		case '_':
1464f0e8ed85SMark Brown 		case '-':
1465f0e8ed85SMark Brown 		case '\0':
1466f0e8ed85SMark Brown 			break;
1467f0e8ed85SMark Brown 		default:
1468f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1469f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1470f0e8ed85SMark Brown 			break;
1471f0e8ed85SMark Brown 		}
1472f0e8ed85SMark Brown 	}
1473fe3e78e0SMark Brown 
147428e9ad92SMark Brown 	if (card->late_probe) {
147528e9ad92SMark Brown 		ret = card->late_probe(card);
147628e9ad92SMark Brown 		if (ret < 0) {
147728e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
147828e9ad92SMark Brown 				card->name, ret);
147928e9ad92SMark Brown 			goto probe_aux_dev_err;
148028e9ad92SMark Brown 		}
148128e9ad92SMark Brown 	}
148228e9ad92SMark Brown 
14832dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
14842dc00213SMark Brown 
1485f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1486fe3e78e0SMark Brown 	if (ret < 0) {
1487f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
14886b3ed785SAxel Lin 		goto probe_aux_dev_err;
1489fe3e78e0SMark Brown 	}
1490fe3e78e0SMark Brown 
1491fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1492f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1493f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1494f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1495fe3e78e0SMark Brown 		if (ret < 0) {
1496f0fba2adSLiam Girdwood 			printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
14976b3ed785SAxel Lin 			while (--i >= 0)
14987d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
14996b3ed785SAxel Lin 			goto probe_aux_dev_err;
1500fe3e78e0SMark Brown 		}
1501fe3e78e0SMark Brown 	}
1502fe3e78e0SMark Brown #endif
1503fe3e78e0SMark Brown 
1504435c5e25SMark Brown 	card->instantiated = 1;
15054f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1506f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1507435c5e25SMark Brown 	return;
1508db2a4165SFrank Mandarino 
15092eea392dSJarkko Nikula probe_aux_dev_err:
15102eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
15112eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
15122eea392dSJarkko Nikula 
1513f0fba2adSLiam Girdwood probe_dai_err:
15140671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1515fe3e78e0SMark Brown 
1516f0fba2adSLiam Girdwood card_probe_error:
151787506549SMark Brown 	if (card->remove)
1518e7361ec4SMark Brown 		card->remove(card);
1519f0fba2adSLiam Girdwood 
1520f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1521f0fba2adSLiam Girdwood 
1522f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1523435c5e25SMark Brown }
1524db2a4165SFrank Mandarino 
1525435c5e25SMark Brown /*
1526421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1527435c5e25SMark Brown  * client_mutex.
1528435c5e25SMark Brown  */
1529435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1530435c5e25SMark Brown {
1531435c5e25SMark Brown 	struct snd_soc_card *card;
1532435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1533435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1534435c5e25SMark Brown }
1535435c5e25SMark Brown 
1536435c5e25SMark Brown /* probes a new socdev */
1537435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1538435c5e25SMark Brown {
1539f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1540435c5e25SMark Brown 	int ret = 0;
1541435c5e25SMark Brown 
154270a7ca34SVinod Koul 	/*
154370a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
154470a7ca34SVinod Koul 	 * we should not be here in that case so ret error
154570a7ca34SVinod Koul 	 */
154670a7ca34SVinod Koul 	if (!card)
154770a7ca34SVinod Koul 		return -EINVAL;
154870a7ca34SVinod Koul 
1549435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1550435c5e25SMark Brown 	card->dev = &pdev->dev;
1551f0fba2adSLiam Girdwood 
1552435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1553435c5e25SMark Brown 	if (ret != 0) {
1554435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1555db2a4165SFrank Mandarino 		return ret;
1556db2a4165SFrank Mandarino 	}
1557db2a4165SFrank Mandarino 
1558435c5e25SMark Brown 	return 0;
1559435c5e25SMark Brown }
1560435c5e25SMark Brown 
1561b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1562db2a4165SFrank Mandarino {
1563db2a4165SFrank Mandarino 	int i;
1564db2a4165SFrank Mandarino 
1565f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1566f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1567f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
15685b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1569db2a4165SFrank Mandarino 	}
1570db2a4165SFrank Mandarino 
15712eea392dSJarkko Nikula 	/* remove auxiliary devices */
15722eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
15732eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
15742eea392dSJarkko Nikula 
1575f0fba2adSLiam Girdwood 	/* remove and free each DAI */
15760671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1577f0fba2adSLiam Girdwood 
1578a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1579a6052154SJarkko Nikula 
1580f0fba2adSLiam Girdwood 	/* remove the card */
158187506549SMark Brown 	if (card->remove)
1582e7361ec4SMark Brown 		card->remove(card);
1583f0fba2adSLiam Girdwood 
15840aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
15850aaae527SLars-Peter Clausen 
1586f0fba2adSLiam Girdwood 	kfree(card->rtd);
1587f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1588b0e26485SVinod Koul 	return 0;
1589b0e26485SVinod Koul 
1590b2dfa62cSGuennadi Liakhovetski }
1591b0e26485SVinod Koul 
1592b0e26485SVinod Koul /* removes a socdev */
1593b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1594b0e26485SVinod Koul {
1595b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1596b0e26485SVinod Koul 
1597c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1598db2a4165SFrank Mandarino 	return 0;
1599db2a4165SFrank Mandarino }
1600db2a4165SFrank Mandarino 
16016f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
160251737470SMark Brown {
16036f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1604f0fba2adSLiam Girdwood 	int i;
160551737470SMark Brown 
160651737470SMark Brown 	if (!card->instantiated)
1607416356fcSMark Brown 		return 0;
160851737470SMark Brown 
160951737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
161051737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1611f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1612f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
16135b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1614f0fba2adSLiam Girdwood 	}
161551737470SMark Brown 
1616f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1617416356fcSMark Brown 
1618416356fcSMark Brown 	return 0;
161951737470SMark Brown }
16206f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
162151737470SMark Brown 
16226f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
16236f8ab4acSMark Brown 	.suspend = snd_soc_suspend,
16246f8ab4acSMark Brown 	.resume = snd_soc_resume,
16256f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1626416356fcSMark Brown };
1627deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1628416356fcSMark Brown 
1629db2a4165SFrank Mandarino /* ASoC platform driver */
1630db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1631db2a4165SFrank Mandarino 	.driver		= {
1632db2a4165SFrank Mandarino 		.name		= "soc-audio",
16338b45a209SKay Sievers 		.owner		= THIS_MODULE,
16346f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1635db2a4165SFrank Mandarino 	},
1636db2a4165SFrank Mandarino 	.probe		= soc_probe,
1637db2a4165SFrank Mandarino 	.remove		= soc_remove,
1638db2a4165SFrank Mandarino };
1639db2a4165SFrank Mandarino 
1640096e49d5SMark Brown /**
1641096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1642096e49d5SMark Brown  *
1643096e49d5SMark Brown  * @codec: CODEC to query.
1644096e49d5SMark Brown  * @reg: Register to query.
1645096e49d5SMark Brown  *
1646096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1647096e49d5SMark Brown  */
1648181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1649181e055eSMark Brown 				    unsigned int reg)
1650096e49d5SMark Brown {
16511500b7b5SDimitris Papastamos 	if (codec->volatile_register)
16521500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1653096e49d5SMark Brown 	else
1654096e49d5SMark Brown 		return 0;
1655096e49d5SMark Brown }
1656096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1657096e49d5SMark Brown 
1658db2a4165SFrank Mandarino /**
1659239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1660239c9706SDimitris Papastamos  *
1661239c9706SDimitris Papastamos  * @codec: CODEC to query.
1662239c9706SDimitris Papastamos  * @reg: Register to query.
1663239c9706SDimitris Papastamos  *
1664239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1665239c9706SDimitris Papastamos  */
1666239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1667239c9706SDimitris Papastamos 				    unsigned int reg)
1668239c9706SDimitris Papastamos {
1669239c9706SDimitris Papastamos 	if (codec->readable_register)
1670239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1671239c9706SDimitris Papastamos 	else
167263fa0a28SLars-Peter Clausen 		return 1;
1673239c9706SDimitris Papastamos }
1674239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1675239c9706SDimitris Papastamos 
1676239c9706SDimitris Papastamos /**
1677239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1678239c9706SDimitris Papastamos  *
1679239c9706SDimitris Papastamos  * @codec: CODEC to query.
1680239c9706SDimitris Papastamos  * @reg: Register to query.
1681239c9706SDimitris Papastamos  *
1682239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1683239c9706SDimitris Papastamos  */
1684239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1685239c9706SDimitris Papastamos 				    unsigned int reg)
1686239c9706SDimitris Papastamos {
1687239c9706SDimitris Papastamos 	if (codec->writable_register)
1688239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1689239c9706SDimitris Papastamos 	else
169063fa0a28SLars-Peter Clausen 		return 1;
1691239c9706SDimitris Papastamos }
1692239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1693239c9706SDimitris Papastamos 
1694f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1695f1442bc1SLiam Girdwood 					unsigned int reg)
1696f1442bc1SLiam Girdwood {
1697f1442bc1SLiam Girdwood 	unsigned int ret;
1698f1442bc1SLiam Girdwood 
1699f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1700f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1701f1442bc1SLiam Girdwood 		return -1;
1702f1442bc1SLiam Girdwood 	}
1703f1442bc1SLiam Girdwood 
1704f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1705f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1706a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1707f1442bc1SLiam Girdwood 
1708f1442bc1SLiam Girdwood 	return ret;
1709f1442bc1SLiam Girdwood }
1710f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1711f1442bc1SLiam Girdwood 
1712f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1713f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1714f1442bc1SLiam Girdwood {
1715f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1716f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1717f1442bc1SLiam Girdwood 		return -1;
1718f1442bc1SLiam Girdwood 	}
1719f1442bc1SLiam Girdwood 
1720f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1721a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
1722f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1723f1442bc1SLiam Girdwood }
1724f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1725f1442bc1SLiam Girdwood 
1726239c9706SDimitris Papastamos /**
1727db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1728db2a4165SFrank Mandarino  * @codec: audio codec
1729db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1730db2a4165SFrank Mandarino  * @num: AC97 codec number
1731db2a4165SFrank Mandarino  *
1732db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1733db2a4165SFrank Mandarino  */
1734db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1735db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1736db2a4165SFrank Mandarino {
1737db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1738db2a4165SFrank Mandarino 
1739db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1740db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1741db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1742db2a4165SFrank Mandarino 		return -ENOMEM;
1743db2a4165SFrank Mandarino 	}
1744db2a4165SFrank Mandarino 
1745db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1746db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1747db2a4165SFrank Mandarino 		kfree(codec->ac97);
1748db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1749db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1750db2a4165SFrank Mandarino 		return -ENOMEM;
1751db2a4165SFrank Mandarino 	}
1752db2a4165SFrank Mandarino 
1753db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1754db2a4165SFrank Mandarino 	codec->ac97->num = num;
17550562f788SMika Westerberg 
17560562f788SMika Westerberg 	/*
17570562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
17580562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
17590562f788SMika Westerberg 	 */
17600562f788SMika Westerberg 	codec->ac97_created = 1;
17610562f788SMika Westerberg 
1762db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1763db2a4165SFrank Mandarino 	return 0;
1764db2a4165SFrank Mandarino }
1765db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1766db2a4165SFrank Mandarino 
1767db2a4165SFrank Mandarino /**
1768db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1769db2a4165SFrank Mandarino  * @codec: audio codec
1770db2a4165SFrank Mandarino  *
1771db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1772db2a4165SFrank Mandarino  */
1773db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1774db2a4165SFrank Mandarino {
1775db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1776f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1777f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1778f0fba2adSLiam Girdwood #endif
1779db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1780db2a4165SFrank Mandarino 	kfree(codec->ac97);
1781db2a4165SFrank Mandarino 	codec->ac97 = NULL;
17820562f788SMika Westerberg 	codec->ac97_created = 0;
1783db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1784db2a4165SFrank Mandarino }
1785db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1786db2a4165SFrank Mandarino 
1787c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1788c3753707SMark Brown {
1789c3753707SMark Brown 	unsigned int ret;
1790c3753707SMark Brown 
1791c3acec26SMark Brown 	ret = codec->read(codec, reg);
1792c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1793a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1794c3753707SMark Brown 
1795c3753707SMark Brown 	return ret;
1796c3753707SMark Brown }
1797c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1798c3753707SMark Brown 
1799c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1800c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1801c3753707SMark Brown {
1802c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1803a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1804c3acec26SMark Brown 	return codec->write(codec, reg, val);
1805c3753707SMark Brown }
1806c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1807c3753707SMark Brown 
18085fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
18095fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
18105fb609d4SDimitris Papastamos {
18115fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
18125fb609d4SDimitris Papastamos }
18135fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
18145fb609d4SDimitris Papastamos 
1815db2a4165SFrank Mandarino /**
1816db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1817db2a4165SFrank Mandarino  * @codec: audio codec
1818db2a4165SFrank Mandarino  * @reg: codec register
1819db2a4165SFrank Mandarino  * @mask: register mask
1820db2a4165SFrank Mandarino  * @value: new value
1821db2a4165SFrank Mandarino  *
1822db2a4165SFrank Mandarino  * Writes new register value.
1823db2a4165SFrank Mandarino  *
1824180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
1825db2a4165SFrank Mandarino  */
1826db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
182746f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1828db2a4165SFrank Mandarino {
1829db2a4165SFrank Mandarino 	int change;
183046f5822fSDaniel Ribeiro 	unsigned int old, new;
1831180c329dSTimur Tabi 	int ret;
1832db2a4165SFrank Mandarino 
1833180c329dSTimur Tabi 	ret = snd_soc_read(codec, reg);
1834180c329dSTimur Tabi 	if (ret < 0)
1835180c329dSTimur Tabi 		return ret;
1836180c329dSTimur Tabi 
1837180c329dSTimur Tabi 	old = ret;
183878bf3c9aSMark Brown 	new = (old & ~mask) | (value & mask);
1839db2a4165SFrank Mandarino 	change = old != new;
1840180c329dSTimur Tabi 	if (change) {
1841180c329dSTimur Tabi 		ret = snd_soc_write(codec, reg, new);
1842180c329dSTimur Tabi 		if (ret < 0)
1843180c329dSTimur Tabi 			return ret;
1844180c329dSTimur Tabi 	}
1845db2a4165SFrank Mandarino 
1846db2a4165SFrank Mandarino 	return change;
1847db2a4165SFrank Mandarino }
1848db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1849db2a4165SFrank Mandarino 
1850db2a4165SFrank Mandarino /**
18516c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
18526c508c62SEero Nurkkala  * @codec: audio codec
18536c508c62SEero Nurkkala  * @reg: codec register
18546c508c62SEero Nurkkala  * @mask: register mask
18556c508c62SEero Nurkkala  * @value: new value
18566c508c62SEero Nurkkala  *
18576c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
18586c508c62SEero Nurkkala  *
18596c508c62SEero Nurkkala  * Returns 1 for change else 0.
18606c508c62SEero Nurkkala  */
1861dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
18626c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
18636c508c62SEero Nurkkala 			       unsigned int value)
18646c508c62SEero Nurkkala {
18656c508c62SEero Nurkkala 	int change;
18666c508c62SEero Nurkkala 
18676c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
18686c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
18696c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
18706c508c62SEero Nurkkala 
18716c508c62SEero Nurkkala 	return change;
18726c508c62SEero Nurkkala }
1873dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
18746c508c62SEero Nurkkala 
18756c508c62SEero Nurkkala /**
1876db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1877db2a4165SFrank Mandarino  * @codec: audio codec
1878db2a4165SFrank Mandarino  * @reg: codec register
1879db2a4165SFrank Mandarino  * @mask: register mask
1880db2a4165SFrank Mandarino  * @value: new value
1881db2a4165SFrank Mandarino  *
1882db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1883db2a4165SFrank Mandarino  * different from the old value.
1884db2a4165SFrank Mandarino  *
1885db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1886db2a4165SFrank Mandarino  */
1887db2a4165SFrank Mandarino int snd_soc_test_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 
1897db2a4165SFrank Mandarino 	return change;
1898db2a4165SFrank Mandarino }
1899db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1900db2a4165SFrank Mandarino 
1901db2a4165SFrank Mandarino /**
1902db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1903db2a4165SFrank Mandarino  * @substream: the pcm substream
1904db2a4165SFrank Mandarino  * @hw: the hardware parameters
1905db2a4165SFrank Mandarino  *
1906db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1907db2a4165SFrank Mandarino  */
1908db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1909db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1910db2a4165SFrank Mandarino {
1911db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1912db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1913db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1914db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1915db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1916db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1917db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1918db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1919db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1920db2a4165SFrank Mandarino 	return 0;
1921db2a4165SFrank Mandarino }
1922db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1923db2a4165SFrank Mandarino 
1924db2a4165SFrank Mandarino /**
1925db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1926db2a4165SFrank Mandarino  * @_template: control template
1927db2a4165SFrank Mandarino  * @data: control private data
1928ac11a2b3SMark Brown  * @long_name: control long name
1929efb7ac3fSMark Brown  * @prefix: control name prefix
1930db2a4165SFrank Mandarino  *
1931db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1932db2a4165SFrank Mandarino  *
1933db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1934db2a4165SFrank Mandarino  */
1935db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1936efb7ac3fSMark Brown 				  void *data, char *long_name,
1937efb7ac3fSMark Brown 				  const char *prefix)
1938db2a4165SFrank Mandarino {
1939db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1940efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
1941efb7ac3fSMark Brown 	char *name = NULL;
1942efb7ac3fSMark Brown 	int name_len;
1943db2a4165SFrank Mandarino 
1944db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1945db2a4165SFrank Mandarino 	template.index = 0;
1946db2a4165SFrank Mandarino 
1947efb7ac3fSMark Brown 	if (!long_name)
1948efb7ac3fSMark Brown 		long_name = template.name;
1949efb7ac3fSMark Brown 
1950efb7ac3fSMark Brown 	if (prefix) {
1951efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
195257cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
1953efb7ac3fSMark Brown 		if (!name)
1954efb7ac3fSMark Brown 			return NULL;
1955efb7ac3fSMark Brown 
1956efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
1957efb7ac3fSMark Brown 
1958efb7ac3fSMark Brown 		template.name = name;
1959efb7ac3fSMark Brown 	} else {
1960efb7ac3fSMark Brown 		template.name = long_name;
1961efb7ac3fSMark Brown 	}
1962efb7ac3fSMark Brown 
1963efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
1964efb7ac3fSMark Brown 
1965efb7ac3fSMark Brown 	kfree(name);
1966efb7ac3fSMark Brown 
1967efb7ac3fSMark Brown 	return kcontrol;
1968db2a4165SFrank Mandarino }
1969db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1970db2a4165SFrank Mandarino 
1971db2a4165SFrank Mandarino /**
19723e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
19733e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
19743e8e1952SIan Molton  * duplicating this code.
19753e8e1952SIan Molton  *
19763e8e1952SIan Molton  * @codec: codec to add controls to
19773e8e1952SIan Molton  * @controls: array of controls to add
19783e8e1952SIan Molton  * @num_controls: number of elements in the array
19793e8e1952SIan Molton  *
19803e8e1952SIan Molton  * Return 0 for success, else error.
19813e8e1952SIan Molton  */
19823e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
19833e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
19843e8e1952SIan Molton {
1985f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
19863e8e1952SIan Molton 	int err, i;
19873e8e1952SIan Molton 
19883e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
19893e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
1990efb7ac3fSMark Brown 		err = snd_ctl_add(card, snd_soc_cnew(control, codec,
1991efb7ac3fSMark Brown 						     control->name,
1992efb7ac3fSMark Brown 						     codec->name_prefix));
19933e8e1952SIan Molton 		if (err < 0) {
1994082100dcSMark Brown 			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
1995efb7ac3fSMark Brown 				codec->name, control->name, err);
19963e8e1952SIan Molton 			return err;
19973e8e1952SIan Molton 		}
19983e8e1952SIan Molton 	}
19993e8e1952SIan Molton 
20003e8e1952SIan Molton 	return 0;
20013e8e1952SIan Molton }
20023e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
20033e8e1952SIan Molton 
20043e8e1952SIan Molton /**
2005a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2006a491a5c8SLiam Girdwood  * Convienience function to add a list of controls.
2007a491a5c8SLiam Girdwood  *
2008a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2009a491a5c8SLiam Girdwood  * @controls: array of controls to add
2010a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2011a491a5c8SLiam Girdwood  *
2012a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2013a491a5c8SLiam Girdwood  */
2014a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2015a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2016a491a5c8SLiam Girdwood {
2017a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2018a491a5c8SLiam Girdwood 	int err, i;
2019a491a5c8SLiam Girdwood 
2020a491a5c8SLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2021a491a5c8SLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2022a491a5c8SLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2023a491a5c8SLiam Girdwood 				control->name, NULL));
2024a491a5c8SLiam Girdwood 		if (err < 0) {
2025a491a5c8SLiam Girdwood 			dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2026a491a5c8SLiam Girdwood 			return err;
2027a491a5c8SLiam Girdwood 		}
2028a491a5c8SLiam Girdwood 	}
2029a491a5c8SLiam Girdwood 
2030a491a5c8SLiam Girdwood 	return 0;
2031a491a5c8SLiam Girdwood }
2032a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2033a491a5c8SLiam Girdwood 
2034a491a5c8SLiam Girdwood /**
2035db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2036db2a4165SFrank Mandarino  * @kcontrol: mixer control
2037db2a4165SFrank Mandarino  * @uinfo: control element information
2038db2a4165SFrank Mandarino  *
2039db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2040db2a4165SFrank Mandarino  * mixer control.
2041db2a4165SFrank Mandarino  *
2042db2a4165SFrank Mandarino  * Returns 0 for success.
2043db2a4165SFrank Mandarino  */
2044db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2045db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2046db2a4165SFrank Mandarino {
2047db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2048db2a4165SFrank Mandarino 
2049db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2050db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2051f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2052db2a4165SFrank Mandarino 
2053f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2054f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2055db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2056db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2057db2a4165SFrank Mandarino 	return 0;
2058db2a4165SFrank Mandarino }
2059db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2060db2a4165SFrank Mandarino 
2061db2a4165SFrank Mandarino /**
2062db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2063db2a4165SFrank Mandarino  * @kcontrol: mixer control
2064ac11a2b3SMark Brown  * @ucontrol: control element information
2065db2a4165SFrank Mandarino  *
2066db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2067db2a4165SFrank Mandarino  *
2068db2a4165SFrank Mandarino  * Returns 0 for success.
2069db2a4165SFrank Mandarino  */
2070db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2071db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2072db2a4165SFrank Mandarino {
2073db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2074db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
207546f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2076db2a4165SFrank Mandarino 
2077f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2078db2a4165SFrank Mandarino 		;
2079db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
20803ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
20813ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2082db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2083db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2084db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2085db2a4165SFrank Mandarino 
2086db2a4165SFrank Mandarino 	return 0;
2087db2a4165SFrank Mandarino }
2088db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2089db2a4165SFrank Mandarino 
2090db2a4165SFrank Mandarino /**
2091db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2092db2a4165SFrank Mandarino  * @kcontrol: mixer control
2093ac11a2b3SMark Brown  * @ucontrol: control element information
2094db2a4165SFrank Mandarino  *
2095db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2096db2a4165SFrank Mandarino  *
2097db2a4165SFrank Mandarino  * Returns 0 for success.
2098db2a4165SFrank Mandarino  */
2099db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2100db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2101db2a4165SFrank Mandarino {
2102db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2103db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
210446f5822fSDaniel Ribeiro 	unsigned int val;
210546f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2106db2a4165SFrank Mandarino 
2107f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2108db2a4165SFrank Mandarino 		;
2109f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2110db2a4165SFrank Mandarino 		return -EINVAL;
2111db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2112db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2113db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2114f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2115db2a4165SFrank Mandarino 			return -EINVAL;
2116db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2117db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2118db2a4165SFrank Mandarino 	}
2119db2a4165SFrank Mandarino 
21206c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2121db2a4165SFrank Mandarino }
2122db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2123db2a4165SFrank Mandarino 
2124db2a4165SFrank Mandarino /**
21252e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
21262e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
21272e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
21282e72f8e3SPeter Ujfalusi  *
21292e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
21302e72f8e3SPeter Ujfalusi  *
21312e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
21322e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
21332e72f8e3SPeter Ujfalusi  *
21342e72f8e3SPeter Ujfalusi  * Returns 0 for success.
21352e72f8e3SPeter Ujfalusi  */
21362e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
21372e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
21382e72f8e3SPeter Ujfalusi {
21392e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
214074155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
214146f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
21422e72f8e3SPeter Ujfalusi 
21432e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
21442e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
21452e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
21462e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
21472e72f8e3SPeter Ujfalusi 			break;
21482e72f8e3SPeter Ujfalusi 	}
21492e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
21502e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
21512e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
21522e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
21532e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
21542e72f8e3SPeter Ujfalusi 				break;
21552e72f8e3SPeter Ujfalusi 		}
21562e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
21572e72f8e3SPeter Ujfalusi 	}
21582e72f8e3SPeter Ujfalusi 
21592e72f8e3SPeter Ujfalusi 	return 0;
21602e72f8e3SPeter Ujfalusi }
21612e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
21622e72f8e3SPeter Ujfalusi 
21632e72f8e3SPeter Ujfalusi /**
21642e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
21652e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
21662e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
21672e72f8e3SPeter Ujfalusi  *
21682e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
21692e72f8e3SPeter Ujfalusi  *
21702e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
21712e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
21722e72f8e3SPeter Ujfalusi  *
21732e72f8e3SPeter Ujfalusi  * Returns 0 for success.
21742e72f8e3SPeter Ujfalusi  */
21752e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
21762e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
21772e72f8e3SPeter Ujfalusi {
21782e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
217974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
218046f5822fSDaniel Ribeiro 	unsigned int val;
218146f5822fSDaniel Ribeiro 	unsigned int mask;
21822e72f8e3SPeter Ujfalusi 
21832e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
21842e72f8e3SPeter Ujfalusi 		return -EINVAL;
21852e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
21862e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
21872e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
21882e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
21892e72f8e3SPeter Ujfalusi 			return -EINVAL;
21902e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
21912e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
21922e72f8e3SPeter Ujfalusi 	}
21932e72f8e3SPeter Ujfalusi 
21946c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
21952e72f8e3SPeter Ujfalusi }
21962e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
21972e72f8e3SPeter Ujfalusi 
21982e72f8e3SPeter Ujfalusi /**
2199db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2200db2a4165SFrank Mandarino  * @kcontrol: mixer control
2201db2a4165SFrank Mandarino  * @uinfo: control element information
2202db2a4165SFrank Mandarino  *
2203db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2204db2a4165SFrank Mandarino  * single mixer.
2205db2a4165SFrank Mandarino  *
2206db2a4165SFrank Mandarino  * Returns 0 for success.
2207db2a4165SFrank Mandarino  */
2208db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2209db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2210db2a4165SFrank Mandarino {
2211db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2212db2a4165SFrank Mandarino 
2213db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2214db2a4165SFrank Mandarino 	uinfo->count = 1;
2215f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2216db2a4165SFrank Mandarino 
2217f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2218f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2219db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2220db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2221db2a4165SFrank Mandarino 	return 0;
2222db2a4165SFrank Mandarino }
2223db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2224db2a4165SFrank Mandarino 
2225db2a4165SFrank Mandarino /**
2226db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2227db2a4165SFrank Mandarino  * @kcontrol: mixer control
2228db2a4165SFrank Mandarino  * @uinfo: control element information
2229db2a4165SFrank Mandarino  *
2230db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2231db2a4165SFrank Mandarino  *
2232db2a4165SFrank Mandarino  * Returns 0 for success.
2233db2a4165SFrank Mandarino  */
2234db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2235db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2236db2a4165SFrank Mandarino {
2237a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2238db2a4165SFrank Mandarino 
2239fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2240a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2241a7a4ac86SPhilipp Zabel 	else
2242a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2243a7a4ac86SPhilipp Zabel 
2244db2a4165SFrank Mandarino 	uinfo->count = 1;
2245db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2246a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2247db2a4165SFrank Mandarino 	return 0;
2248db2a4165SFrank Mandarino }
2249db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2250db2a4165SFrank Mandarino 
2251db2a4165SFrank Mandarino /**
2252db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2253db2a4165SFrank Mandarino  * @kcontrol: mixer control
2254db2a4165SFrank Mandarino  * @uinfo: control element information
2255db2a4165SFrank Mandarino  *
2256e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2257e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2258db2a4165SFrank Mandarino  *
2259db2a4165SFrank Mandarino  * Returns 0 for success.
2260db2a4165SFrank Mandarino  */
2261db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2262db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2263db2a4165SFrank Mandarino {
22644eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22654eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2266d11bb4a9SPeter Ujfalusi 	int platform_max;
2267db2a4165SFrank Mandarino 
2268d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2269d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2270d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2271d11bb4a9SPeter Ujfalusi 
2272d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2273a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2274a7a4ac86SPhilipp Zabel 	else
2275a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2276a7a4ac86SPhilipp Zabel 
2277e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2278db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2279d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2280db2a4165SFrank Mandarino 	return 0;
2281db2a4165SFrank Mandarino }
2282db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2283db2a4165SFrank Mandarino 
2284db2a4165SFrank Mandarino /**
2285db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2286db2a4165SFrank Mandarino  * @kcontrol: mixer control
2287ac11a2b3SMark Brown  * @ucontrol: control element information
2288db2a4165SFrank Mandarino  *
2289f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2290f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2291db2a4165SFrank Mandarino  *
2292db2a4165SFrank Mandarino  * Returns 0 for success.
2293db2a4165SFrank Mandarino  */
2294db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2295db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2296db2a4165SFrank Mandarino {
22974eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22984eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2299db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2300815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2301f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2302815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2303815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
23044eaa9819SJon Smirl 	int max = mc->max;
2305815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2306815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2307db2a4165SFrank Mandarino 
2308db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2309db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2310f7915d99SPeter Ujfalusi 	if (invert)
2311db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2312a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2313f7915d99SPeter Ujfalusi 
2314f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2315f7915d99SPeter Ujfalusi 		if (reg == reg2)
2316f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2317f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2318f7915d99SPeter Ujfalusi 		else
2319f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2320f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2321f7915d99SPeter Ujfalusi 		if (invert)
2322db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2323a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2324db2a4165SFrank Mandarino 	}
2325db2a4165SFrank Mandarino 
2326db2a4165SFrank Mandarino 	return 0;
2327db2a4165SFrank Mandarino }
2328db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2329db2a4165SFrank Mandarino 
2330db2a4165SFrank Mandarino /**
2331db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2332db2a4165SFrank Mandarino  * @kcontrol: mixer control
2333ac11a2b3SMark Brown  * @ucontrol: control element information
2334db2a4165SFrank Mandarino  *
2335974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2336974815baSPeter Ujfalusi  * control that spans 2 registers.
2337db2a4165SFrank Mandarino  *
2338db2a4165SFrank Mandarino  * Returns 0 for success.
2339db2a4165SFrank Mandarino  */
2340db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2341db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2342db2a4165SFrank Mandarino {
23434eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23444eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2345db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2346815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2347974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2348815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2349815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
23504eaa9819SJon Smirl 	int max = mc->max;
2351815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2352815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2353974815baSPeter Ujfalusi 	int err;
2354974815baSPeter Ujfalusi 	bool type_2r = 0;
2355974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2356974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2357db2a4165SFrank Mandarino 
2358db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2359db2a4165SFrank Mandarino 	if (invert)
2360a7a4ac86SPhilipp Zabel 		val = max - val;
2361db2a4165SFrank Mandarino 	val_mask = mask << shift;
2362db2a4165SFrank Mandarino 	val = val << shift;
2363974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2364db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2365db2a4165SFrank Mandarino 		if (invert)
2366a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2367974815baSPeter Ujfalusi 		if (reg == reg2) {
2368db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2369db2a4165SFrank Mandarino 			val |= val2 << rshift;
2370974815baSPeter Ujfalusi 		} else {
2371db2a4165SFrank Mandarino 			val2 = val2 << shift;
2372974815baSPeter Ujfalusi 			type_2r = 1;
2373974815baSPeter Ujfalusi 		}
2374974815baSPeter Ujfalusi 	}
23756c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
23763ff3f64bSMark Brown 	if (err < 0)
2377db2a4165SFrank Mandarino 		return err;
2378db2a4165SFrank Mandarino 
2379974815baSPeter Ujfalusi 	if (type_2r)
23806c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2381974815baSPeter Ujfalusi 
2382db2a4165SFrank Mandarino 	return err;
2383db2a4165SFrank Mandarino }
2384974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2385db2a4165SFrank Mandarino 
2386e13ac2e9SMark Brown /**
2387e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2388e13ac2e9SMark Brown  * @kcontrol: mixer control
2389e13ac2e9SMark Brown  * @uinfo: control element information
2390e13ac2e9SMark Brown  *
2391e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2392e13ac2e9SMark Brown  *
2393e13ac2e9SMark Brown  * Returns 0 for success.
2394e13ac2e9SMark Brown  */
2395e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2396e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2397e13ac2e9SMark Brown {
23984eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23994eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2400d11bb4a9SPeter Ujfalusi 	int platform_max;
24014eaa9819SJon Smirl 	int min = mc->min;
2402e13ac2e9SMark Brown 
2403d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2404d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2405d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2406d11bb4a9SPeter Ujfalusi 
2407e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2408e13ac2e9SMark Brown 	uinfo->count = 2;
2409e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2410d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2411e13ac2e9SMark Brown 	return 0;
2412e13ac2e9SMark Brown }
2413e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2414e13ac2e9SMark Brown 
2415e13ac2e9SMark Brown /**
2416e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2417e13ac2e9SMark Brown  * @kcontrol: mixer control
2418ac11a2b3SMark Brown  * @ucontrol: control element information
2419e13ac2e9SMark Brown  *
2420e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2421e13ac2e9SMark Brown  *
2422e13ac2e9SMark Brown  * Returns 0 for success.
2423e13ac2e9SMark Brown  */
2424e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2425e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2426e13ac2e9SMark Brown {
24274eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24284eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2429e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2430815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
24314eaa9819SJon Smirl 	int min = mc->min;
2432e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2433e13ac2e9SMark Brown 
2434e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2435e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2436e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2437e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2438e13ac2e9SMark Brown 	return 0;
2439e13ac2e9SMark Brown }
2440e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2441e13ac2e9SMark Brown 
2442e13ac2e9SMark Brown /**
2443e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2444e13ac2e9SMark Brown  * @kcontrol: mixer control
2445ac11a2b3SMark Brown  * @ucontrol: control element information
2446e13ac2e9SMark Brown  *
2447e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2448e13ac2e9SMark Brown  *
2449e13ac2e9SMark Brown  * Returns 0 for success.
2450e13ac2e9SMark Brown  */
2451e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2452e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2453e13ac2e9SMark Brown {
24544eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24554eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2456e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2457815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
24584eaa9819SJon Smirl 	int min = mc->min;
245946f5822fSDaniel Ribeiro 	unsigned int val;
2460e13ac2e9SMark Brown 
2461e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2462e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2463e13ac2e9SMark Brown 
24646c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2465e13ac2e9SMark Brown }
2466e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2467e13ac2e9SMark Brown 
24688c6529dbSLiam Girdwood /**
2469637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2470637d3847SPeter Ujfalusi  *
2471637d3847SPeter Ujfalusi  * @codec: where to look for the control
2472637d3847SPeter Ujfalusi  * @name: Name of the control
2473637d3847SPeter Ujfalusi  * @max: new maximum limit
2474637d3847SPeter Ujfalusi  *
2475637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2476637d3847SPeter Ujfalusi  */
2477637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2478637d3847SPeter Ujfalusi 	const char *name, int max)
2479637d3847SPeter Ujfalusi {
2480f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2481637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2482637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2483637d3847SPeter Ujfalusi 	int found = 0;
2484637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2485637d3847SPeter Ujfalusi 
2486637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2487637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2488637d3847SPeter Ujfalusi 		return -EINVAL;
2489637d3847SPeter Ujfalusi 
2490637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2491637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2492637d3847SPeter Ujfalusi 			found = 1;
2493637d3847SPeter Ujfalusi 			break;
2494637d3847SPeter Ujfalusi 		}
2495637d3847SPeter Ujfalusi 	}
2496637d3847SPeter Ujfalusi 	if (found) {
2497637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2498637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2499d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2500637d3847SPeter Ujfalusi 			ret = 0;
2501637d3847SPeter Ujfalusi 		}
2502637d3847SPeter Ujfalusi 	}
2503637d3847SPeter Ujfalusi 	return ret;
2504637d3847SPeter Ujfalusi }
2505637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2506637d3847SPeter Ujfalusi 
2507637d3847SPeter Ujfalusi /**
2508b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2509b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2510b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2511b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2512b6f4bb38Sapatard@mandriva.com  *
2513b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2514b6f4bb38Sapatard@mandriva.com  */
2515b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2516b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2517b6f4bb38Sapatard@mandriva.com {
2518b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2519b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2520b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2521b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2522b6f4bb38Sapatard@mandriva.com 
2523b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2524b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2525b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2526b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2527b6f4bb38Sapatard@mandriva.com 
2528b6f4bb38Sapatard@mandriva.com 	return 0;
2529b6f4bb38Sapatard@mandriva.com }
2530b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2531b6f4bb38Sapatard@mandriva.com 
2532b6f4bb38Sapatard@mandriva.com /**
2533b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2534b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2535b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2536b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2537b6f4bb38Sapatard@mandriva.com  *
2538b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2539b6f4bb38Sapatard@mandriva.com  */
2540b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2541b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2542b6f4bb38Sapatard@mandriva.com {
2543b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2544b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2545b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2546b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2547b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2548b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2549b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2550b6f4bb38Sapatard@mandriva.com 
255120630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
255220630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2553b6f4bb38Sapatard@mandriva.com 	return 0;
2554b6f4bb38Sapatard@mandriva.com }
2555b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2556b6f4bb38Sapatard@mandriva.com 
2557b6f4bb38Sapatard@mandriva.com /**
2558b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2559b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2560b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2561b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2562b6f4bb38Sapatard@mandriva.com  *
2563b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2564b6f4bb38Sapatard@mandriva.com  */
2565b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2566b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2567b6f4bb38Sapatard@mandriva.com {
2568b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2569b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2570b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2571b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2572b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2573b6f4bb38Sapatard@mandriva.com 	int ret;
2574b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2575b6f4bb38Sapatard@mandriva.com 
2576b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2577b6f4bb38Sapatard@mandriva.com 	val &= mask;
2578b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2579b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2580b6f4bb38Sapatard@mandriva.com 
2581b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2582b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2583b6f4bb38Sapatard@mandriva.com 
2584b6f4bb38Sapatard@mandriva.com 	ret = 0;
2585b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2586b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2587b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2588f1df5aecSMark Brown 			return ret;
2589b6f4bb38Sapatard@mandriva.com 	}
2590b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2591b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2592b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2593f1df5aecSMark Brown 			return ret;
2594b6f4bb38Sapatard@mandriva.com 	}
2595b6f4bb38Sapatard@mandriva.com 
2596b6f4bb38Sapatard@mandriva.com 	return 0;
2597b6f4bb38Sapatard@mandriva.com }
2598b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2599b6f4bb38Sapatard@mandriva.com 
2600b6f4bb38Sapatard@mandriva.com /**
26018c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
26028c6529dbSLiam Girdwood  * @dai: DAI
26038c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
26048c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
26058c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
26068c6529dbSLiam Girdwood  *
26078c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
26088c6529dbSLiam Girdwood  */
26098c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
26108c6529dbSLiam Girdwood 	unsigned int freq, int dir)
26118c6529dbSLiam Girdwood {
2612f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2613f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2614ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2615da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2616ec4ee52aSMark Brown 						      freq, dir);
26178c6529dbSLiam Girdwood 	else
26188c6529dbSLiam Girdwood 		return -EINVAL;
26198c6529dbSLiam Girdwood }
26208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
26218c6529dbSLiam Girdwood 
26228c6529dbSLiam Girdwood /**
2623ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2624ec4ee52aSMark Brown  * @codec: CODEC
2625ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2626da1c6ea6SMark Brown  * @source: Source for the clock
2627ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2628ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2629ec4ee52aSMark Brown  *
2630ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2631ec4ee52aSMark Brown  */
2632ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2633da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2634ec4ee52aSMark Brown {
2635ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2636da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2637da1c6ea6SMark Brown 						 freq, dir);
2638ec4ee52aSMark Brown 	else
2639ec4ee52aSMark Brown 		return -EINVAL;
2640ec4ee52aSMark Brown }
2641ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2642ec4ee52aSMark Brown 
2643ec4ee52aSMark Brown /**
26448c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
26458c6529dbSLiam Girdwood  * @dai: DAI
2646ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
26478c6529dbSLiam Girdwood  * @div: new clock divisor.
26488c6529dbSLiam Girdwood  *
26498c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
26508c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
26518c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
26528c6529dbSLiam Girdwood  */
26538c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
26548c6529dbSLiam Girdwood 	int div_id, int div)
26558c6529dbSLiam Girdwood {
2656f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2657f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
26588c6529dbSLiam Girdwood 	else
26598c6529dbSLiam Girdwood 		return -EINVAL;
26608c6529dbSLiam Girdwood }
26618c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
26628c6529dbSLiam Girdwood 
26638c6529dbSLiam Girdwood /**
26648c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
26658c6529dbSLiam Girdwood  * @dai: DAI
26668c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
266785488037SMark Brown  * @source: DAI specific source for the PLL
26688c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
26698c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
26708c6529dbSLiam Girdwood  *
26718c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
26728c6529dbSLiam Girdwood  */
267385488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
267485488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
26758c6529dbSLiam Girdwood {
2676f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2677f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
267885488037SMark Brown 					 freq_in, freq_out);
2679ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2680ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2681ec4ee52aSMark Brown 						   freq_in, freq_out);
26828c6529dbSLiam Girdwood 	else
26838c6529dbSLiam Girdwood 		return -EINVAL;
26848c6529dbSLiam Girdwood }
26858c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
26868c6529dbSLiam Girdwood 
2687ec4ee52aSMark Brown /*
2688ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2689ec4ee52aSMark Brown  * @codec: CODEC
2690ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2691ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2692ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2693ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2694ec4ee52aSMark Brown  *
2695ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2696ec4ee52aSMark Brown  */
2697ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2698ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2699ec4ee52aSMark Brown {
2700ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2701ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2702ec4ee52aSMark Brown 					      freq_in, freq_out);
2703ec4ee52aSMark Brown 	else
2704ec4ee52aSMark Brown 		return -EINVAL;
2705ec4ee52aSMark Brown }
2706ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2707ec4ee52aSMark Brown 
27088c6529dbSLiam Girdwood /**
27098c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
27108c6529dbSLiam Girdwood  * @dai: DAI
27118c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
27128c6529dbSLiam Girdwood  *
27138c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
27148c6529dbSLiam Girdwood  */
27158c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
27168c6529dbSLiam Girdwood {
2717f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_fmt)
2718f0fba2adSLiam Girdwood 		return dai->driver->ops->set_fmt(dai, fmt);
27198c6529dbSLiam Girdwood 	else
27208c6529dbSLiam Girdwood 		return -EINVAL;
27218c6529dbSLiam Girdwood }
27228c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
27238c6529dbSLiam Girdwood 
27248c6529dbSLiam Girdwood /**
27258c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
27268c6529dbSLiam Girdwood  * @dai: DAI
2727a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2728a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
27298c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2730a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
27318c6529dbSLiam Girdwood  *
27328c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
27338c6529dbSLiam Girdwood  * specific.
27348c6529dbSLiam Girdwood  */
27358c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2736a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
27378c6529dbSLiam Girdwood {
2738f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2739f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2740a5479e38SDaniel Ribeiro 				slots, slot_width);
27418c6529dbSLiam Girdwood 	else
27428c6529dbSLiam Girdwood 		return -EINVAL;
27438c6529dbSLiam Girdwood }
27448c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
27458c6529dbSLiam Girdwood 
27468c6529dbSLiam Girdwood /**
2747472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2748472df3cbSBarry Song  * @dai: DAI
2749472df3cbSBarry Song  * @tx_num: how many TX channels
2750472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2751472df3cbSBarry Song  *           0~num-1 uses
2752472df3cbSBarry Song  * @rx_num: how many RX channels
2753472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2754472df3cbSBarry Song  *           0~num-1 uses
2755472df3cbSBarry Song  *
2756472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2757472df3cbSBarry Song  */
2758472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2759472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2760472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2761472df3cbSBarry Song {
2762f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2763f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2764472df3cbSBarry Song 			rx_num, rx_slot);
2765472df3cbSBarry Song 	else
2766472df3cbSBarry Song 		return -EINVAL;
2767472df3cbSBarry Song }
2768472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2769472df3cbSBarry Song 
2770472df3cbSBarry Song /**
27718c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
27728c6529dbSLiam Girdwood  * @dai: DAI
27738c6529dbSLiam Girdwood  * @tristate: tristate enable
27748c6529dbSLiam Girdwood  *
27758c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
27768c6529dbSLiam Girdwood  */
27778c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
27788c6529dbSLiam Girdwood {
2779f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2780f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
27818c6529dbSLiam Girdwood 	else
27828c6529dbSLiam Girdwood 		return -EINVAL;
27838c6529dbSLiam Girdwood }
27848c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
27858c6529dbSLiam Girdwood 
27868c6529dbSLiam Girdwood /**
27878c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
27888c6529dbSLiam Girdwood  * @dai: DAI
27898c6529dbSLiam Girdwood  * @mute: mute enable
27908c6529dbSLiam Girdwood  *
27918c6529dbSLiam Girdwood  * Mutes the DAI DAC.
27928c6529dbSLiam Girdwood  */
27938c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
27948c6529dbSLiam Girdwood {
2795f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
2796f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
27978c6529dbSLiam Girdwood 	else
27988c6529dbSLiam Girdwood 		return -EINVAL;
27998c6529dbSLiam Girdwood }
28008c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
28018c6529dbSLiam Girdwood 
2802c5af3a2eSMark Brown /**
2803c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2804c5af3a2eSMark Brown  *
2805ac11a2b3SMark Brown  * @card: Card to register
2806c5af3a2eSMark Brown  *
2807c5af3a2eSMark Brown  */
280870a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2809c5af3a2eSMark Brown {
2810f0fba2adSLiam Girdwood 	int i;
2811f0fba2adSLiam Girdwood 
2812c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2813c5af3a2eSMark Brown 		return -EINVAL;
2814c5af3a2eSMark Brown 
2815ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2816ed77cc12SMark Brown 
2817111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2818111c6419SStephen Warren 
2819150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
2820150dd2f8SVinod Koul 
28212eea392dSJarkko Nikula 	card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
28222eea392dSJarkko Nikula 			    (card->num_links + card->num_aux_devs),
2823f0fba2adSLiam Girdwood 			    GFP_KERNEL);
2824f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2825f0fba2adSLiam Girdwood 		return -ENOMEM;
28262eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
2827f0fba2adSLiam Girdwood 
2828f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
2829f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
2830f0fba2adSLiam Girdwood 
2831c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2832db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
2833c5af3a2eSMark Brown 	card->instantiated = 0;
2834f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2835c5af3a2eSMark Brown 
2836c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2837c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2838435c5e25SMark Brown 	snd_soc_instantiate_cards();
2839c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2840c5af3a2eSMark Brown 
2841c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2842c5af3a2eSMark Brown 
2843c5af3a2eSMark Brown 	return 0;
2844c5af3a2eSMark Brown }
284570a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2846c5af3a2eSMark Brown 
2847c5af3a2eSMark Brown /**
2848c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2849c5af3a2eSMark Brown  *
2850ac11a2b3SMark Brown  * @card: Card to unregister
2851c5af3a2eSMark Brown  *
2852c5af3a2eSMark Brown  */
285370a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2854c5af3a2eSMark Brown {
2855b0e26485SVinod Koul 	if (card->instantiated)
2856b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
2857c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2858c5af3a2eSMark Brown 	list_del(&card->list);
2859c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2860c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2861c5af3a2eSMark Brown 
2862c5af3a2eSMark Brown 	return 0;
2863c5af3a2eSMark Brown }
286470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2865c5af3a2eSMark Brown 
2866f0fba2adSLiam Girdwood /*
2867f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2868f0fba2adSLiam Girdwood  * and sanitizing names.
2869f0fba2adSLiam Girdwood  */
28700b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2871f0fba2adSLiam Girdwood {
2872f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2873f0fba2adSLiam Girdwood 	int id1, id2;
2874f0fba2adSLiam Girdwood 
2875f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2876f0fba2adSLiam Girdwood 		return NULL;
2877f0fba2adSLiam Girdwood 
287858818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2879f0fba2adSLiam Girdwood 
2880f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2881f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2882f0fba2adSLiam Girdwood 	if (found) {
2883f0fba2adSLiam Girdwood 		/* get ID */
2884f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2885f0fba2adSLiam Girdwood 
2886f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2887f0fba2adSLiam Girdwood 			if (*id == -1)
2888f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2889f0fba2adSLiam Girdwood 		}
2890f0fba2adSLiam Girdwood 
2891f0fba2adSLiam Girdwood 	} else {
2892f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2893f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2894f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2895f0fba2adSLiam Girdwood 
2896f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
289705899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2898f0fba2adSLiam Girdwood 
2899f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2900f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
290158818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2902f0fba2adSLiam Girdwood 		} else
2903f0fba2adSLiam Girdwood 			*id = 0;
2904f0fba2adSLiam Girdwood 	}
2905f0fba2adSLiam Girdwood 
2906f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2907f0fba2adSLiam Girdwood }
2908f0fba2adSLiam Girdwood 
2909f0fba2adSLiam Girdwood /*
2910f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2911f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2912f0fba2adSLiam Girdwood  */
2913f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2914f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2915f0fba2adSLiam Girdwood {
2916f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
2917f0fba2adSLiam Girdwood 		printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2918f0fba2adSLiam Girdwood 				dev_name(dev));
2919f0fba2adSLiam Girdwood 		return NULL;
2920f0fba2adSLiam Girdwood 	}
2921f0fba2adSLiam Girdwood 
2922f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2923f0fba2adSLiam Girdwood }
2924f0fba2adSLiam Girdwood 
29259115171aSMark Brown /**
29269115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
29279115171aSMark Brown  *
2928ac11a2b3SMark Brown  * @dai: DAI to register
29299115171aSMark Brown  */
2930f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
2931f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
29329115171aSMark Brown {
2933f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
29349115171aSMark Brown 
2935f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
29369115171aSMark Brown 
2937f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2938f0fba2adSLiam Girdwood 	if (dai == NULL)
2939f0fba2adSLiam Girdwood 		return -ENOMEM;
29406335d055SEric Miao 
2941f0fba2adSLiam Girdwood 	/* create DAI component name */
2942f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
2943f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
2944f0fba2adSLiam Girdwood 		kfree(dai);
2945f0fba2adSLiam Girdwood 		return -ENOMEM;
2946f0fba2adSLiam Girdwood 	}
2947f0fba2adSLiam Girdwood 
2948f0fba2adSLiam Girdwood 	dai->dev = dev;
2949f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
2950f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
2951f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
29529115171aSMark Brown 
29539115171aSMark Brown 	mutex_lock(&client_mutex);
29549115171aSMark Brown 	list_add(&dai->list, &dai_list);
2955435c5e25SMark Brown 	snd_soc_instantiate_cards();
29569115171aSMark Brown 	mutex_unlock(&client_mutex);
29579115171aSMark Brown 
29589115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
29599115171aSMark Brown 
29609115171aSMark Brown 	return 0;
29619115171aSMark Brown }
29629115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
29639115171aSMark Brown 
29649115171aSMark Brown /**
29659115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
29669115171aSMark Brown  *
2967ac11a2b3SMark Brown  * @dai: DAI to unregister
29689115171aSMark Brown  */
2969f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
29709115171aSMark Brown {
2971f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
2972f0fba2adSLiam Girdwood 
2973f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
2974f0fba2adSLiam Girdwood 		if (dev == dai->dev)
2975f0fba2adSLiam Girdwood 			goto found;
2976f0fba2adSLiam Girdwood 	}
2977f0fba2adSLiam Girdwood 	return;
2978f0fba2adSLiam Girdwood 
2979f0fba2adSLiam Girdwood found:
29809115171aSMark Brown 	mutex_lock(&client_mutex);
29819115171aSMark Brown 	list_del(&dai->list);
29829115171aSMark Brown 	mutex_unlock(&client_mutex);
29839115171aSMark Brown 
29849115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
2985f0fba2adSLiam Girdwood 	kfree(dai->name);
2986f0fba2adSLiam Girdwood 	kfree(dai);
29879115171aSMark Brown }
29889115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
29899115171aSMark Brown 
29909115171aSMark Brown /**
29919115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
29929115171aSMark Brown  *
2993ac11a2b3SMark Brown  * @dai: Array of DAIs to register
2994ac11a2b3SMark Brown  * @count: Number of DAIs
29959115171aSMark Brown  */
2996f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
2997f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
29989115171aSMark Brown {
2999f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3000f0fba2adSLiam Girdwood 	int i, ret = 0;
3001f0fba2adSLiam Girdwood 
3002720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
30039115171aSMark Brown 
30049115171aSMark Brown 	for (i = 0; i < count; i++) {
3005f0fba2adSLiam Girdwood 
3006f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3007c46e0079SAxel Lin 		if (dai == NULL) {
3008c46e0079SAxel Lin 			ret = -ENOMEM;
3009c46e0079SAxel Lin 			goto err;
3010c46e0079SAxel Lin 		}
3011f0fba2adSLiam Girdwood 
3012f0fba2adSLiam Girdwood 		/* create DAI component name */
3013f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3014f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3015f0fba2adSLiam Girdwood 			kfree(dai);
3016f0fba2adSLiam Girdwood 			ret = -EINVAL;
30179115171aSMark Brown 			goto err;
30189115171aSMark Brown 		}
30199115171aSMark Brown 
3020f0fba2adSLiam Girdwood 		dai->dev = dev;
3021f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
30220f9141c9SMark Brown 		if (dai->driver->id)
30230f9141c9SMark Brown 			dai->id = dai->driver->id;
30240f9141c9SMark Brown 		else
30250f9141c9SMark Brown 			dai->id = i;
3026f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3027f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3028f0fba2adSLiam Girdwood 
3029f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3030f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3031f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3032f0fba2adSLiam Girdwood 
3033f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3034f0fba2adSLiam Girdwood 	}
3035f0fba2adSLiam Girdwood 
30361dcb4f38SAxel Lin 	mutex_lock(&client_mutex);
3037f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
30381dcb4f38SAxel Lin 	mutex_unlock(&client_mutex);
30399115171aSMark Brown 	return 0;
30409115171aSMark Brown 
30419115171aSMark Brown err:
30429115171aSMark Brown 	for (i--; i >= 0; i--)
3043f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
30449115171aSMark Brown 
30459115171aSMark Brown 	return ret;
30469115171aSMark Brown }
30479115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
30489115171aSMark Brown 
30499115171aSMark Brown /**
30509115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
30519115171aSMark Brown  *
3052ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3053ac11a2b3SMark Brown  * @count: Number of DAIs
30549115171aSMark Brown  */
3055f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
30569115171aSMark Brown {
30579115171aSMark Brown 	int i;
30589115171aSMark Brown 
30599115171aSMark Brown 	for (i = 0; i < count; i++)
3060f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
30619115171aSMark Brown }
30629115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
30639115171aSMark Brown 
306412a48a8cSMark Brown /**
306512a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
306612a48a8cSMark Brown  *
3067ac11a2b3SMark Brown  * @platform: platform to register
306812a48a8cSMark Brown  */
3069f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3070f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
307112a48a8cSMark Brown {
3072f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
307312a48a8cSMark Brown 
3074f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3075f0fba2adSLiam Girdwood 
3076f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3077f0fba2adSLiam Girdwood 	if (platform == NULL)
3078f0fba2adSLiam Girdwood 		return -ENOMEM;
3079f0fba2adSLiam Girdwood 
3080f0fba2adSLiam Girdwood 	/* create platform component name */
3081f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3082f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3083f0fba2adSLiam Girdwood 		kfree(platform);
3084f0fba2adSLiam Girdwood 		return -ENOMEM;
3085f0fba2adSLiam Girdwood 	}
3086f0fba2adSLiam Girdwood 
3087f0fba2adSLiam Girdwood 	platform->dev = dev;
3088f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3089b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3090b7950641SLiam Girdwood 	platform->dapm.platform = platform;
309164a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
309212a48a8cSMark Brown 
309312a48a8cSMark Brown 	mutex_lock(&client_mutex);
309412a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3095435c5e25SMark Brown 	snd_soc_instantiate_cards();
309612a48a8cSMark Brown 	mutex_unlock(&client_mutex);
309712a48a8cSMark Brown 
309812a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
309912a48a8cSMark Brown 
310012a48a8cSMark Brown 	return 0;
310112a48a8cSMark Brown }
310212a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
310312a48a8cSMark Brown 
310412a48a8cSMark Brown /**
310512a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
310612a48a8cSMark Brown  *
3107ac11a2b3SMark Brown  * @platform: platform to unregister
310812a48a8cSMark Brown  */
3109f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
311012a48a8cSMark Brown {
3111f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3112f0fba2adSLiam Girdwood 
3113f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3114f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3115f0fba2adSLiam Girdwood 			goto found;
3116f0fba2adSLiam Girdwood 	}
3117f0fba2adSLiam Girdwood 	return;
3118f0fba2adSLiam Girdwood 
3119f0fba2adSLiam Girdwood found:
312012a48a8cSMark Brown 	mutex_lock(&client_mutex);
312112a48a8cSMark Brown 	list_del(&platform->list);
312212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
312312a48a8cSMark Brown 
312412a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3125f0fba2adSLiam Girdwood 	kfree(platform->name);
3126f0fba2adSLiam Girdwood 	kfree(platform);
312712a48a8cSMark Brown }
312812a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
312912a48a8cSMark Brown 
3130151ab22cSMark Brown static u64 codec_format_map[] = {
3131151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3132151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3133151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3134151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3135151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3136151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3137151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3138151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3139151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3140151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3141151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3142151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3143151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3144151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3145151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3146151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3147151ab22cSMark Brown };
3148151ab22cSMark Brown 
3149151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3150151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3151151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3152151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3153151ab22cSMark Brown  */
3154151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3155151ab22cSMark Brown {
3156151ab22cSMark Brown 	int i;
3157151ab22cSMark Brown 
3158151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3159151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3160151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3161151ab22cSMark Brown }
3162151ab22cSMark Brown 
31630d0cf00aSMark Brown /**
31640d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
31650d0cf00aSMark Brown  *
3166ac11a2b3SMark Brown  * @codec: codec to register
31670d0cf00aSMark Brown  */
3168f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3169001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3170001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3171001ae4c0SMark Brown 			   int num_dai)
31720d0cf00aSMark Brown {
31733335ddcaSDimitris Papastamos 	size_t reg_size;
3174f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3175f0fba2adSLiam Girdwood 	int ret, i;
3176151ab22cSMark Brown 
3177f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
31780d0cf00aSMark Brown 
3179f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3180f0fba2adSLiam Girdwood 	if (codec == NULL)
3181f0fba2adSLiam Girdwood 		return -ENOMEM;
31820d0cf00aSMark Brown 
3183f0fba2adSLiam Girdwood 	/* create CODEC component name */
3184f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3185f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3186f0fba2adSLiam Girdwood 		kfree(codec);
3187f0fba2adSLiam Girdwood 		return -ENOMEM;
3188151ab22cSMark Brown 	}
3189151ab22cSMark Brown 
319023bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
319123bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
319223bbce34SDimitris Papastamos 	else
319323bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
319423bbce34SDimitris Papastamos 
3195c3acec26SMark Brown 	codec->write = codec_drv->write;
3196c3acec26SMark Brown 	codec->read = codec_drv->read;
31971500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
31981500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
31998020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
3200ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3201ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3202ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3203474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
320464a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3205f0fba2adSLiam Girdwood 	codec->dev = dev;
3206f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3207f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3208f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3209f0fba2adSLiam Girdwood 
32107a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
32117a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
32123335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3213aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
32143335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
32153335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
32163335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
32173335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
32183335ddcaSDimitris Papastamos 		 * the cache.
32193335ddcaSDimitris Papastamos 		 */
32202aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
32213335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
32223335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
32233335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
32243335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3225fdf0f54dSDimitris Papastamos 				goto fail;
32267a30a3dbSDimitris Papastamos 			}
32277a30a3dbSDimitris Papastamos 		}
32282aa86323SDimitris Papastamos 	}
32297a30a3dbSDimitris Papastamos 
32301500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
32311500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
32321500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
32331500b7b5SDimitris Papastamos 		if (!codec->readable_register)
32341500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
32358020454cSDimitris Papastamos 		if (!codec->writable_register)
32368020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
32371500b7b5SDimitris Papastamos 	}
32381500b7b5SDimitris Papastamos 
3239f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3240f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3241f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3242f0fba2adSLiam Girdwood 	}
3243f0fba2adSLiam Girdwood 
324426b01ccdSMark Brown 	/* register any DAIs */
324526b01ccdSMark Brown 	if (num_dai) {
3246f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3247f0fba2adSLiam Girdwood 		if (ret < 0)
3248fdf0f54dSDimitris Papastamos 			goto fail;
324926b01ccdSMark Brown 	}
3250f0fba2adSLiam Girdwood 
32510d0cf00aSMark Brown 	mutex_lock(&client_mutex);
32520d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
32530d0cf00aSMark Brown 	snd_soc_instantiate_cards();
32540d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
32550d0cf00aSMark Brown 
32560d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
32570d0cf00aSMark Brown 	return 0;
3258f0fba2adSLiam Girdwood 
3259fdf0f54dSDimitris Papastamos fail:
32603335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
32613335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3262f0fba2adSLiam Girdwood 	kfree(codec->name);
3263f0fba2adSLiam Girdwood 	kfree(codec);
3264f0fba2adSLiam Girdwood 	return ret;
32650d0cf00aSMark Brown }
32660d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
32670d0cf00aSMark Brown 
32680d0cf00aSMark Brown /**
32690d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
32700d0cf00aSMark Brown  *
3271ac11a2b3SMark Brown  * @codec: codec to unregister
32720d0cf00aSMark Brown  */
3273f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
32740d0cf00aSMark Brown {
3275f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3276f0fba2adSLiam Girdwood 	int i;
3277f0fba2adSLiam Girdwood 
3278f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3279f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3280f0fba2adSLiam Girdwood 			goto found;
3281f0fba2adSLiam Girdwood 	}
3282f0fba2adSLiam Girdwood 	return;
3283f0fba2adSLiam Girdwood 
3284f0fba2adSLiam Girdwood found:
328526b01ccdSMark Brown 	if (codec->num_dai)
3286f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3287f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3288f0fba2adSLiam Girdwood 
32890d0cf00aSMark Brown 	mutex_lock(&client_mutex);
32900d0cf00aSMark Brown 	list_del(&codec->list);
32910d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
32920d0cf00aSMark Brown 
32930d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3294f0fba2adSLiam Girdwood 
32957a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
32963335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
32971aafcd4dSDimitris Papastamos 	kfree(codec->name);
3298f0fba2adSLiam Girdwood 	kfree(codec);
32990d0cf00aSMark Brown }
33000d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
33010d0cf00aSMark Brown 
3302c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3303db2a4165SFrank Mandarino {
3304384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
33058a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
33068a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3307384c89e2SMark Brown 		printk(KERN_WARNING
3308384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
33098a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3310384c89e2SMark Brown 	}
3311c3c5a19aSMark Brown 
33128a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3313c3c5a19aSMark Brown 				 &codec_list_fops))
3314c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3315c3c5a19aSMark Brown 
33168a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3317f3208780SMark Brown 				 &dai_list_fops))
3318f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
331919c7ac27SMark Brown 
33208a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
332119c7ac27SMark Brown 				 &platform_list_fops))
332219c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3323384c89e2SMark Brown #endif
3324384c89e2SMark Brown 
3325fb257897SMark Brown 	snd_soc_util_init();
3326fb257897SMark Brown 
3327db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3328db2a4165SFrank Mandarino }
33294abe8e16SMark Brown module_init(snd_soc_init);
3330db2a4165SFrank Mandarino 
33317d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3332db2a4165SFrank Mandarino {
3333fb257897SMark Brown 	snd_soc_util_exit();
3334fb257897SMark Brown 
3335384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
33368a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_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