xref: /openbmc/linux/sound/soc/soc-core.c (revision 054880febeb890b24d705240384856ea6b3ccf7b)
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>
35bec4fa05SStephen Warren #include <linux/of.h>
36474828a4SMarek Vasut #include <sound/ac97_codec.h>
37db2a4165SFrank Mandarino #include <sound/core.h>
383028eb8cSMark Brown #include <sound/jack.h>
39db2a4165SFrank Mandarino #include <sound/pcm.h>
40db2a4165SFrank Mandarino #include <sound/pcm_params.h>
41db2a4165SFrank Mandarino #include <sound/soc.h>
42db2a4165SFrank Mandarino #include <sound/initval.h>
43db2a4165SFrank Mandarino 
44a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
45a8b1d34fSMark Brown #include <trace/events/asoc.h>
46a8b1d34fSMark Brown 
47f0fba2adSLiam Girdwood #define NAME_SIZE	32
48f0fba2adSLiam Girdwood 
49db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50db2a4165SFrank Mandarino 
51384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
528a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
538a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
54384c89e2SMark Brown #endif
55384c89e2SMark Brown 
56c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
579115171aSMark Brown static LIST_HEAD(dai_list);
5812a48a8cSMark Brown static LIST_HEAD(platform_list);
590d0cf00aSMark Brown static LIST_HEAD(codec_list);
60c5af3a2eSMark Brown 
61db2a4165SFrank Mandarino /*
62db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
63db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
64db2a4165SFrank Mandarino  * between two audio tracks.
65db2a4165SFrank Mandarino  */
66db2a4165SFrank Mandarino static int pmdown_time = 5000;
67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69db2a4165SFrank Mandarino 
702bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
712bc9a81eSDimitris Papastamos  * a particular given value */
722bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
732bc9a81eSDimitris Papastamos {
742bc9a81eSDimitris Papastamos 	int c = 0;
752bc9a81eSDimitris Papastamos 	int i;
762bc9a81eSDimitris Papastamos 
772bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
782bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
792bc9a81eSDimitris Papastamos 			break;
802bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
812bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
822bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
832bc9a81eSDimitris Papastamos 	else
842bc9a81eSDimitris Papastamos 		c /= 8;
852bc9a81eSDimitris Papastamos 	return c;
862bc9a81eSDimitris Papastamos }
872bc9a81eSDimitris Papastamos 
8813fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
8913fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9013fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9113fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
922624d5faSMark Brown {
9300b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9400b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9513fd179fSDimitris Papastamos 	int ret;
9613fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
9713fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
9813fd179fSDimitris Papastamos 
9913fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
10013fd179fSDimitris Papastamos 	 * try to abuse this function */
10113fd179fSDimitris Papastamos 	WARN_ON(len > 63);
10213fd179fSDimitris Papastamos 
10313fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10413fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10513fd179fSDimitris Papastamos 		return -EINVAL;
10613fd179fSDimitris Papastamos 
10713fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
10813fd179fSDimitris Papastamos 	if (ret < 0) {
10913fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
11013fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
11113fd179fSDimitris Papastamos 	} else {
11213fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
11313fd179fSDimitris Papastamos 	}
11413fd179fSDimitris Papastamos 
11513fd179fSDimitris Papastamos 	/* prepare the buffer */
11613fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
11713fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
11813fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
11913fd179fSDimitris Papastamos 
12013fd179fSDimitris Papastamos 	return 0;
12113fd179fSDimitris Papastamos }
12213fd179fSDimitris Papastamos 
12313fd179fSDimitris Papastamos /* codec register dump */
12413fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
12513fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
12613fd179fSDimitris Papastamos {
12713fd179fSDimitris Papastamos 	int i, step = 1;
1282bc9a81eSDimitris Papastamos 	int wordsize, regsize;
12913fd179fSDimitris Papastamos 	int len;
13013fd179fSDimitris Papastamos 	size_t total = 0;
13113fd179fSDimitris Papastamos 	loff_t p = 0;
1322bc9a81eSDimitris Papastamos 
13300b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
13400b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1352624d5faSMark Brown 
13613fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13713fd179fSDimitris Papastamos 
138f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1392624d5faSMark Brown 		return 0;
1402624d5faSMark Brown 
141f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
142f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1432624d5faSMark Brown 
144f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
145b92d150bSLars-Peter Clausen 		if (!snd_soc_codec_readable_register(codec, i))
1462624d5faSMark Brown 			continue;
147f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
148f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1492624d5faSMark Brown 							 PAGE_SIZE - count, i);
1505164d74dSMark Brown 		} else {
15113fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
15213fd179fSDimitris Papastamos 			 * entries for the default case */
15313fd179fSDimitris Papastamos 			if (p >= pos) {
15413fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1552624d5faSMark Brown 					break;
15613fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
15713fd179fSDimitris Papastamos 				total += len;
15813fd179fSDimitris Papastamos 			}
15913fd179fSDimitris Papastamos 			p += len;
16013fd179fSDimitris Papastamos 		}
1612624d5faSMark Brown 	}
1622624d5faSMark Brown 
16313fd179fSDimitris Papastamos 	total = min(total, count - 1);
1642624d5faSMark Brown 
16513fd179fSDimitris Papastamos 	return total;
1662624d5faSMark Brown }
16713fd179fSDimitris Papastamos 
1682624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1692624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1702624d5faSMark Brown {
17136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
172f0fba2adSLiam Girdwood 
17313fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1742624d5faSMark Brown }
1752624d5faSMark Brown 
1762624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1772624d5faSMark Brown 
178dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
179dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
180dbe21408SMark Brown {
18136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
182dbe21408SMark Brown 
183f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
184dbe21408SMark Brown }
185dbe21408SMark Brown 
186dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
187dbe21408SMark Brown 			       struct device_attribute *attr,
188dbe21408SMark Brown 			       const char *buf, size_t count)
189dbe21408SMark Brown {
19036ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
191c593b520SMark Brown 	int ret;
192dbe21408SMark Brown 
193c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
194c593b520SMark Brown 	if (ret)
195c593b520SMark Brown 		return ret;
196dbe21408SMark Brown 
197dbe21408SMark Brown 	return count;
198dbe21408SMark Brown }
199dbe21408SMark Brown 
200dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
201dbe21408SMark Brown 
2022624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2032624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2042624d5faSMark Brown 				   size_t count, loff_t *ppos)
2052624d5faSMark Brown {
2062624d5faSMark Brown 	ssize_t ret;
2072624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
20813fd179fSDimitris Papastamos 	char *buf;
20913fd179fSDimitris Papastamos 
21013fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
21113fd179fSDimitris Papastamos 		return -EINVAL;
21213fd179fSDimitris Papastamos 
21313fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2142624d5faSMark Brown 	if (!buf)
2152624d5faSMark Brown 		return -ENOMEM;
21613fd179fSDimitris Papastamos 
21713fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
21813fd179fSDimitris Papastamos 	if (ret >= 0) {
21913fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
22013fd179fSDimitris Papastamos 			kfree(buf);
22113fd179fSDimitris Papastamos 			return -EFAULT;
22213fd179fSDimitris Papastamos 		}
22313fd179fSDimitris Papastamos 		*ppos += ret;
22413fd179fSDimitris Papastamos 	}
22513fd179fSDimitris Papastamos 
2262624d5faSMark Brown 	kfree(buf);
2272624d5faSMark Brown 	return ret;
2282624d5faSMark Brown }
2292624d5faSMark Brown 
2302624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2312624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2322624d5faSMark Brown {
2332624d5faSMark Brown 	char buf[32];
23434e268d8SStephen Boyd 	size_t buf_size;
2352624d5faSMark Brown 	char *start = buf;
2362624d5faSMark Brown 	unsigned long reg, value;
2372624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2382624d5faSMark Brown 
2392624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2402624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2412624d5faSMark Brown 		return -EFAULT;
2422624d5faSMark Brown 	buf[buf_size] = 0;
2432624d5faSMark Brown 
2442624d5faSMark Brown 	while (*start == ' ')
2452624d5faSMark Brown 		start++;
2462624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2472624d5faSMark Brown 	while (*start == ' ')
2482624d5faSMark Brown 		start++;
2492624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2502624d5faSMark Brown 		return -EINVAL;
2510d51a9cbSMark Brown 
2520d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2530d51a9cbSMark Brown 	add_taint(TAINT_USER);
2540d51a9cbSMark Brown 
255e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2562624d5faSMark Brown 	return buf_size;
2572624d5faSMark Brown }
2582624d5faSMark Brown 
2592624d5faSMark Brown static const struct file_operations codec_reg_fops = {
260234e3405SStephen Boyd 	.open = simple_open,
2612624d5faSMark Brown 	.read = codec_reg_read_file,
2622624d5faSMark Brown 	.write = codec_reg_write_file,
2636038f373SArnd Bergmann 	.llseek = default_llseek,
2642624d5faSMark Brown };
2652624d5faSMark Brown 
2662624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2672624d5faSMark Brown {
268d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
269d6ce4cf3SJarkko Nikula 
2706ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
271d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2722624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
27364e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
2742624d5faSMark Brown 		return;
2752624d5faSMark Brown 	}
2762624d5faSMark Brown 
277aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
278aaee8ef1SMark Brown 			    &codec->cache_sync);
279aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
280aaee8ef1SMark Brown 			    &codec->cache_only);
281aaee8ef1SMark Brown 
2822624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2832624d5faSMark Brown 						 codec->debugfs_codec_root,
2842624d5faSMark Brown 						 codec, &codec_reg_fops);
2852624d5faSMark Brown 	if (!codec->debugfs_reg)
28664e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
2872624d5faSMark Brown 
2888eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
2892624d5faSMark Brown }
2902624d5faSMark Brown 
2912624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2922624d5faSMark Brown {
2932624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2942624d5faSMark Brown }
2952624d5faSMark Brown 
296731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
297731f1ab2SSebastien Guiriec {
298731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
299731f1ab2SSebastien Guiriec 
300731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
301731f1ab2SSebastien Guiriec 						       debugfs_card_root);
302731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
303731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
304731f1ab2SSebastien Guiriec 			"Failed to create platform debugfs directory\n");
305731f1ab2SSebastien Guiriec 		return;
306731f1ab2SSebastien Guiriec 	}
307731f1ab2SSebastien Guiriec 
308731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
309731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
310731f1ab2SSebastien Guiriec }
311731f1ab2SSebastien Guiriec 
312731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
313731f1ab2SSebastien Guiriec {
314731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
315731f1ab2SSebastien Guiriec }
316731f1ab2SSebastien Guiriec 
317c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
318c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
319c3c5a19aSMark Brown {
320c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3212b194f9dSMark Brown 	ssize_t len, ret = 0;
322c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
323c3c5a19aSMark Brown 
324c3c5a19aSMark Brown 	if (!buf)
325c3c5a19aSMark Brown 		return -ENOMEM;
326c3c5a19aSMark Brown 
3272b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3282b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
329c3c5a19aSMark Brown 			       codec->name);
3302b194f9dSMark Brown 		if (len >= 0)
3312b194f9dSMark Brown 			ret += len;
3322b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3332b194f9dSMark Brown 			ret = PAGE_SIZE;
3342b194f9dSMark Brown 			break;
3352b194f9dSMark Brown 		}
3362b194f9dSMark Brown 	}
337c3c5a19aSMark Brown 
338c3c5a19aSMark Brown 	if (ret >= 0)
339c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
340c3c5a19aSMark Brown 
341c3c5a19aSMark Brown 	kfree(buf);
342c3c5a19aSMark Brown 
343c3c5a19aSMark Brown 	return ret;
344c3c5a19aSMark Brown }
345c3c5a19aSMark Brown 
346c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
347c3c5a19aSMark Brown 	.read = codec_list_read_file,
348c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
349c3c5a19aSMark Brown };
350c3c5a19aSMark Brown 
351f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
352f3208780SMark Brown 				  size_t count, loff_t *ppos)
353f3208780SMark Brown {
354f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3552b194f9dSMark Brown 	ssize_t len, ret = 0;
356f3208780SMark Brown 	struct snd_soc_dai *dai;
357f3208780SMark Brown 
358f3208780SMark Brown 	if (!buf)
359f3208780SMark Brown 		return -ENOMEM;
360f3208780SMark Brown 
3612b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3622b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3632b194f9dSMark Brown 		if (len >= 0)
3642b194f9dSMark Brown 			ret += len;
3652b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3662b194f9dSMark Brown 			ret = PAGE_SIZE;
3672b194f9dSMark Brown 			break;
3682b194f9dSMark Brown 		}
3692b194f9dSMark Brown 	}
370f3208780SMark Brown 
371f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
372f3208780SMark Brown 
373f3208780SMark Brown 	kfree(buf);
374f3208780SMark Brown 
375f3208780SMark Brown 	return ret;
376f3208780SMark Brown }
377f3208780SMark Brown 
378f3208780SMark Brown static const struct file_operations dai_list_fops = {
379f3208780SMark Brown 	.read = dai_list_read_file,
380f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
381f3208780SMark Brown };
382f3208780SMark Brown 
38319c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
38419c7ac27SMark Brown 				       char __user *user_buf,
38519c7ac27SMark Brown 				       size_t count, loff_t *ppos)
38619c7ac27SMark Brown {
38719c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3882b194f9dSMark Brown 	ssize_t len, ret = 0;
38919c7ac27SMark Brown 	struct snd_soc_platform *platform;
39019c7ac27SMark Brown 
39119c7ac27SMark Brown 	if (!buf)
39219c7ac27SMark Brown 		return -ENOMEM;
39319c7ac27SMark Brown 
3942b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3952b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
39619c7ac27SMark Brown 			       platform->name);
3972b194f9dSMark Brown 		if (len >= 0)
3982b194f9dSMark Brown 			ret += len;
3992b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4002b194f9dSMark Brown 			ret = PAGE_SIZE;
4012b194f9dSMark Brown 			break;
4022b194f9dSMark Brown 		}
4032b194f9dSMark Brown 	}
40419c7ac27SMark Brown 
40519c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
40619c7ac27SMark Brown 
40719c7ac27SMark Brown 	kfree(buf);
40819c7ac27SMark Brown 
40919c7ac27SMark Brown 	return ret;
41019c7ac27SMark Brown }
41119c7ac27SMark Brown 
41219c7ac27SMark Brown static const struct file_operations platform_list_fops = {
41319c7ac27SMark Brown 	.read = platform_list_read_file,
41419c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
41519c7ac27SMark Brown };
41619c7ac27SMark Brown 
417a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
418a6052154SJarkko Nikula {
419a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4208a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4213a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
422a6052154SJarkko Nikula 		dev_warn(card->dev,
4237c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4243a45b867SJarkko Nikula 		return;
4253a45b867SJarkko Nikula 	}
4263a45b867SJarkko Nikula 
4273a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4283a45b867SJarkko Nikula 						    card->debugfs_card_root,
4293a45b867SJarkko Nikula 						    &card->pop_time);
4303a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4313a45b867SJarkko Nikula 		dev_warn(card->dev,
4323a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
433a6052154SJarkko Nikula }
434a6052154SJarkko Nikula 
435a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
436a6052154SJarkko Nikula {
437a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
438a6052154SJarkko Nikula }
439a6052154SJarkko Nikula 
4402624d5faSMark Brown #else
4412624d5faSMark Brown 
4422624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4432624d5faSMark Brown {
4442624d5faSMark Brown }
4452624d5faSMark Brown 
4462624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4472624d5faSMark Brown {
4482624d5faSMark Brown }
449b95fccbcSAxel Lin 
450731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
451731f1ab2SSebastien Guiriec {
452731f1ab2SSebastien Guiriec }
453731f1ab2SSebastien Guiriec 
454731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
455731f1ab2SSebastien Guiriec {
456731f1ab2SSebastien Guiriec }
457731f1ab2SSebastien Guiriec 
458b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
459b95fccbcSAxel Lin {
460b95fccbcSAxel Lin }
461b95fccbcSAxel Lin 
462b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
463b95fccbcSAxel Lin {
464b95fccbcSAxel Lin }
4652624d5faSMark Brown #endif
4662624d5faSMark Brown 
467db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
468db2a4165SFrank Mandarino /* unregister ac97 codec */
469db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
470db2a4165SFrank Mandarino {
471db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
472db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
473db2a4165SFrank Mandarino 	return 0;
474db2a4165SFrank Mandarino }
475db2a4165SFrank Mandarino 
476db2a4165SFrank Mandarino /* stop no dev release warning */
477db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
478db2a4165SFrank Mandarino 
479db2a4165SFrank Mandarino /* register ac97 codec to bus */
480db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
481db2a4165SFrank Mandarino {
482db2a4165SFrank Mandarino 	int err;
483db2a4165SFrank Mandarino 
484db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4854ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
486db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
487db2a4165SFrank Mandarino 
488bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
489f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
490db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
491db2a4165SFrank Mandarino 	if (err < 0) {
492db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
493db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
494db2a4165SFrank Mandarino 		return err;
495db2a4165SFrank Mandarino 	}
496db2a4165SFrank Mandarino 	return 0;
497db2a4165SFrank Mandarino }
498db2a4165SFrank Mandarino #endif
499db2a4165SFrank Mandarino 
5006f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
501db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5026f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
503db2a4165SFrank Mandarino {
5046f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5052eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
506db2a4165SFrank Mandarino 	int i;
507db2a4165SFrank Mandarino 
508e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
509e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
510e3509ff0SDaniel Mack 	 */
511f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
512e3509ff0SDaniel Mack 		return 0;
513e3509ff0SDaniel Mack 
5146ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5156ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5166ed25978SAndy Green 	 */
517f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
518f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
519f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5206ed25978SAndy Green 
5216ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
522f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5236ed25978SAndy Green 
524a00f90f9SMark Brown 	/* mute any active DACs */
525f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
526f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
527f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5283efab7dcSMark Brown 
529f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5303efab7dcSMark Brown 			continue;
5313efab7dcSMark Brown 
532f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
533f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
534db2a4165SFrank Mandarino 	}
535db2a4165SFrank Mandarino 
5364ccab3e7SLiam Girdwood 	/* suspend all pcms */
537f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
538f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5393efab7dcSMark Brown 			continue;
5403efab7dcSMark Brown 
541f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5423efab7dcSMark Brown 	}
5434ccab3e7SLiam Girdwood 
54487506549SMark Brown 	if (card->suspend_pre)
54570b2ac12SMark Brown 		card->suspend_pre(card);
546db2a4165SFrank Mandarino 
547f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
548f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
549f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5503efab7dcSMark Brown 
551f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5523efab7dcSMark Brown 			continue;
5533efab7dcSMark Brown 
554f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
555f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
556f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
557f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
558f0fba2adSLiam Girdwood 			platform->suspended = 1;
559f0fba2adSLiam Girdwood 		}
560db2a4165SFrank Mandarino 	}
561db2a4165SFrank Mandarino 
562db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
563f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5645b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
565ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
566f0fba2adSLiam Girdwood 	}
567db2a4165SFrank Mandarino 
568f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5693efab7dcSMark Brown 
570f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5713efab7dcSMark Brown 			continue;
5723efab7dcSMark Brown 
5737bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5747bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
575db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
576f0fba2adSLiam Girdwood 
5777bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5787bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
579db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
580db2a4165SFrank Mandarino 	}
581db2a4165SFrank Mandarino 
582f0fba2adSLiam Girdwood 	/* suspend all CODECs */
5832eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
5841547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
5851547aba9SMark Brown 		 * bias _ON and should not be suspended. */
586f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
587ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
5881547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
589125a25daSMark Brown 				/*
590125a25daSMark Brown 				 * If the CODEC is capable of idle
591125a25daSMark Brown 				 * bias off then being in STANDBY
592125a25daSMark Brown 				 * means it's doing something,
593125a25daSMark Brown 				 * otherwise fall through.
594125a25daSMark Brown 				 */
595125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
596125a25daSMark Brown 					dev_dbg(codec->dev,
597125a25daSMark Brown 						"idle_bias_off CODEC on over suspend\n");
598125a25daSMark Brown 					break;
599125a25daSMark Brown 				}
6001547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
60184b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
602f0fba2adSLiam Girdwood 				codec->suspended = 1;
6037be4ba24SMark Brown 				codec->cache_sync = 1;
6041547aba9SMark Brown 				break;
6051547aba9SMark Brown 			default:
606f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
6071547aba9SMark Brown 				break;
6081547aba9SMark Brown 			}
6091547aba9SMark Brown 		}
610f0fba2adSLiam Girdwood 	}
611db2a4165SFrank Mandarino 
612f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
613f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6143efab7dcSMark Brown 
615f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6163efab7dcSMark Brown 			continue;
6173efab7dcSMark Brown 
618f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
619f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
620db2a4165SFrank Mandarino 	}
621db2a4165SFrank Mandarino 
62287506549SMark Brown 	if (card->suspend_post)
62370b2ac12SMark Brown 		card->suspend_post(card);
624db2a4165SFrank Mandarino 
625db2a4165SFrank Mandarino 	return 0;
626db2a4165SFrank Mandarino }
6276f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
628db2a4165SFrank Mandarino 
6296ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6306ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6316ed25978SAndy Green  */
6326ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
633db2a4165SFrank Mandarino {
634f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
635f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6362eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
637db2a4165SFrank Mandarino 	int i;
638db2a4165SFrank Mandarino 
6396ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6406ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6416ed25978SAndy Green 	 */
6426ed25978SAndy Green 
643f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
6446ed25978SAndy Green 
6459949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
646f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6479949788bSMark Brown 
64887506549SMark Brown 	if (card->resume_pre)
64970b2ac12SMark Brown 		card->resume_pre(card);
650db2a4165SFrank Mandarino 
651f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
652f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
653f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6543efab7dcSMark Brown 
655f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6563efab7dcSMark Brown 			continue;
6573efab7dcSMark Brown 
658f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
659f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
660db2a4165SFrank Mandarino 	}
661db2a4165SFrank Mandarino 
6622eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6631547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6641547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
6651547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
6661547aba9SMark Brown 		 */
667f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
668ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6691547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
6701547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
671f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
672f0fba2adSLiam Girdwood 				codec->suspended = 0;
6731547aba9SMark Brown 				break;
6741547aba9SMark Brown 			default:
675f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
6761547aba9SMark Brown 				break;
6771547aba9SMark Brown 			}
6781547aba9SMark Brown 		}
679f0fba2adSLiam Girdwood 	}
680db2a4165SFrank Mandarino 
681f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6823efab7dcSMark Brown 
683f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6843efab7dcSMark Brown 			continue;
6853efab7dcSMark Brown 
6867bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
687d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
688db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
689f0fba2adSLiam Girdwood 
6907bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
691d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
692db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
693db2a4165SFrank Mandarino 	}
694db2a4165SFrank Mandarino 
6953ff3f64bSMark Brown 	/* unmute any active DACs */
696f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
697f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
698f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
6993efab7dcSMark Brown 
700f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7013efab7dcSMark Brown 			continue;
7023efab7dcSMark Brown 
703f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
704f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
705db2a4165SFrank Mandarino 	}
706db2a4165SFrank Mandarino 
707f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
708f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
709f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7103efab7dcSMark Brown 
711f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7123efab7dcSMark Brown 			continue;
7133efab7dcSMark Brown 
714f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
715f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
716f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
717f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
718f0fba2adSLiam Girdwood 			platform->suspended = 0;
719f0fba2adSLiam Girdwood 		}
720db2a4165SFrank Mandarino 	}
721db2a4165SFrank Mandarino 
72287506549SMark Brown 	if (card->resume_post)
72370b2ac12SMark Brown 		card->resume_post(card);
724db2a4165SFrank Mandarino 
725f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
7266ed25978SAndy Green 
7276ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
728f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7296ed25978SAndy Green }
7306ed25978SAndy Green 
7316ed25978SAndy Green /* powers up audio subsystem after a suspend */
7326f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7336ed25978SAndy Green {
7346f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
73582e14e8bSStephen Warren 	int i, ac97_control = 0;
736b9dd94a8SPeter Ujfalusi 
7375ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
7385ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
7395ff1ddf2SEric Miao 	 */
7405ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
7415ff1ddf2SEric Miao 		return 0;
7425ff1ddf2SEric Miao 
74364ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
74464ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
74564ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
74664ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
74764ab9baaSMark Brown 	 */
748f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
749f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
75082e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
75182e14e8bSStephen Warren 	}
75282e14e8bSStephen Warren 	if (ac97_control) {
753f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
75464ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
75564ab9baaSMark Brown 	} else {
756f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
7576308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
758f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
759f0fba2adSLiam Girdwood 	}
7606ed25978SAndy Green 
761db2a4165SFrank Mandarino 	return 0;
762db2a4165SFrank Mandarino }
7636f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
764db2a4165SFrank Mandarino #else
7656f8ab4acSMark Brown #define snd_soc_suspend NULL
7666f8ab4acSMark Brown #define snd_soc_resume NULL
767db2a4165SFrank Mandarino #endif
768db2a4165SFrank Mandarino 
76985e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
77002a06d30SBarry Song };
77102a06d30SBarry Song 
772f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
773db2a4165SFrank Mandarino {
774f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
775f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
776fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
777435c5e25SMark Brown 	struct snd_soc_platform *platform;
778f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
779848dd8beSMark Brown 	const char *platform_name;
780db2a4165SFrank Mandarino 
781f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
7826308419aSMark Brown 
783b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
784f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
7855a504963SStephen Warren 		if (dai_link->cpu_dai_of_node) {
7865a504963SStephen Warren 			if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
7875a504963SStephen Warren 				continue;
7885a504963SStephen Warren 		} else {
7892610ab77SStephen Warren 			if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
7902610ab77SStephen Warren 				continue;
7915a504963SStephen Warren 		}
7922610ab77SStephen Warren 
793f0fba2adSLiam Girdwood 		rtd->cpu_dai = cpu_dai;
794f0fba2adSLiam Girdwood 	}
795b19e6e7bSMark Brown 
796b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
797f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CPU DAI %s not registered\n",
798f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
799b19e6e7bSMark Brown 		return -EPROBE_DEFER;
800c5af3a2eSMark Brown 	}
801c5af3a2eSMark Brown 
802b19e6e7bSMark Brown 	/* Find CODEC from registered CODECs */
803f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8045a504963SStephen Warren 		if (dai_link->codec_of_node) {
8055a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8065a504963SStephen Warren 				continue;
8075a504963SStephen Warren 		} else {
8082610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8092610ab77SStephen Warren 				continue;
8105a504963SStephen Warren 		}
8112610ab77SStephen Warren 
812f0fba2adSLiam Girdwood 		rtd->codec = codec;
813f0fba2adSLiam Girdwood 
8142610ab77SStephen Warren 		/*
8152610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
8162610ab77SStephen Warren 		 * this CODEC
8172610ab77SStephen Warren 		 */
818f0fba2adSLiam Girdwood 		list_for_each_entry(codec_dai, &dai_list, list) {
819f0fba2adSLiam Girdwood 			if (codec->dev == codec_dai->dev &&
8202610ab77SStephen Warren 				!strcmp(codec_dai->name,
8212610ab77SStephen Warren 					dai_link->codec_dai_name)) {
8222610ab77SStephen Warren 
823f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
824435c5e25SMark Brown 			}
825f0fba2adSLiam Girdwood 		}
826b19e6e7bSMark Brown 
827b19e6e7bSMark Brown 		if (!rtd->codec_dai) {
828f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
829f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
830b19e6e7bSMark Brown 			return -EPROBE_DEFER;
831f0fba2adSLiam Girdwood 		}
832b19e6e7bSMark Brown 	}
833b19e6e7bSMark Brown 
834b19e6e7bSMark Brown 	if (!rtd->codec) {
835f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CODEC %s not registered\n",
836f0fba2adSLiam Girdwood 			dai_link->codec_name);
837b19e6e7bSMark Brown 		return -EPROBE_DEFER;
838b19e6e7bSMark Brown 	}
839848dd8beSMark Brown 
840848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
841848dd8beSMark Brown 	platform_name = dai_link->platform_name;
8425a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
843848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
844848dd8beSMark Brown 
845b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
846f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
8475a504963SStephen Warren 		if (dai_link->platform_of_node) {
8485a504963SStephen Warren 			if (platform->dev->of_node !=
8495a504963SStephen Warren 			    dai_link->platform_of_node)
8505a504963SStephen Warren 				continue;
8515a504963SStephen Warren 		} else {
8522610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
8532610ab77SStephen Warren 				continue;
8545a504963SStephen Warren 		}
8552610ab77SStephen Warren 
856f0fba2adSLiam Girdwood 		rtd->platform = platform;
857f0fba2adSLiam Girdwood 	}
858b19e6e7bSMark Brown 	if (!rtd->platform) {
859f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "platform %s not registered\n",
860f0fba2adSLiam Girdwood 			dai_link->platform_name);
861b19e6e7bSMark Brown 		return -EPROBE_DEFER;
862f0fba2adSLiam Girdwood 	}
863b19e6e7bSMark Brown 
864b19e6e7bSMark Brown 	card->num_rtd++;
865b19e6e7bSMark Brown 
866b19e6e7bSMark Brown 	return 0;
8676b05eda6SMark Brown }
8686b05eda6SMark Brown 
869589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
870589c3563SJarkko Nikula {
871589c3563SJarkko Nikula 	int err;
872589c3563SJarkko Nikula 
873589c3563SJarkko Nikula 	if (codec->driver->remove) {
874589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
875589c3563SJarkko Nikula 		if (err < 0)
876589c3563SJarkko Nikula 			dev_err(codec->dev,
877589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
878589c3563SJarkko Nikula 				codec->name, err);
879589c3563SJarkko Nikula 	}
880589c3563SJarkko Nikula 
881589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
882589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
883589c3563SJarkko Nikula 
884589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
885589c3563SJarkko Nikula 	codec->probed = 0;
886589c3563SJarkko Nikula 	list_del(&codec->card_list);
887589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
888589c3563SJarkko Nikula }
889589c3563SJarkko Nikula 
8900168bf0dSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
891f0fba2adSLiam Girdwood {
892f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
893f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
894f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
895f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
896f0fba2adSLiam Girdwood 	int err;
897f0fba2adSLiam Girdwood 
898f0fba2adSLiam Girdwood 	/* unregister the rtd device */
899f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
90036ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
90136ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
90236ae1a96SMark Brown 		device_unregister(rtd->dev);
903f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
90402a06d30SBarry Song 	}
90502a06d30SBarry Song 
906f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9070168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
9080168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
909f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
910f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
911f0fba2adSLiam Girdwood 			if (err < 0)
9120837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9130837fc62SFabio Estevam 							codec_dai->name, err);
914f0fba2adSLiam Girdwood 		}
915f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
916f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
917f0fba2adSLiam Girdwood 	}
918f0fba2adSLiam Girdwood 
919f0fba2adSLiam Girdwood 	/* remove the platform */
9200168bf0dSLiam Girdwood 	if (platform && platform->probed &&
9210168bf0dSLiam Girdwood 			platform->driver->remove_order == order) {
922f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
923f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
924f0fba2adSLiam Girdwood 			if (err < 0)
9250837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9260837fc62SFabio Estevam 							platform->name, err);
927f0fba2adSLiam Girdwood 		}
928675c496bSLiam Girdwood 
929675c496bSLiam Girdwood 		/* Make sure all DAPM widgets are freed */
930675c496bSLiam Girdwood 		snd_soc_dapm_free(&platform->dapm);
931675c496bSLiam Girdwood 
932731f1ab2SSebastien Guiriec 		soc_cleanup_platform_debugfs(platform);
933f0fba2adSLiam Girdwood 		platform->probed = 0;
934f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
935f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
936f0fba2adSLiam Girdwood 	}
937f0fba2adSLiam Girdwood 
938f0fba2adSLiam Girdwood 	/* remove the CODEC */
9390168bf0dSLiam Girdwood 	if (codec && codec->probed &&
9400168bf0dSLiam Girdwood 			codec->driver->remove_order == order)
941589c3563SJarkko Nikula 		soc_remove_codec(codec);
942f0fba2adSLiam Girdwood 
943f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
9440168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
9450168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
946f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
947f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
948f0fba2adSLiam Girdwood 			if (err < 0)
9490837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9500837fc62SFabio Estevam 							cpu_dai->name, err);
951f0fba2adSLiam Girdwood 		}
952f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
953f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
954f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
955f0fba2adSLiam Girdwood 	}
956f0fba2adSLiam Girdwood }
957f0fba2adSLiam Girdwood 
9580671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9590671fd8eSKuninori Morimoto {
9600168bf0dSLiam Girdwood 	int dai, order;
9610671fd8eSKuninori Morimoto 
9620168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9630168bf0dSLiam Girdwood 			order++) {
9640168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
9650168bf0dSLiam Girdwood 			soc_remove_dai_link(card, dai, order);
9660168bf0dSLiam Girdwood 	}
9670671fd8eSKuninori Morimoto 	card->num_rtd = 0;
9680671fd8eSKuninori Morimoto }
9690671fd8eSKuninori Morimoto 
970ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
971ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
972ead9b919SJarkko Nikula {
973ead9b919SJarkko Nikula 	int i;
974ead9b919SJarkko Nikula 
975ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
976ead9b919SJarkko Nikula 		return;
977ead9b919SJarkko Nikula 
978ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
979ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
980ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
981ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
982ead9b919SJarkko Nikula 			break;
983ead9b919SJarkko Nikula 		}
984ead9b919SJarkko Nikula 	}
985ead9b919SJarkko Nikula }
986ead9b919SJarkko Nikula 
987589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
988589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
989589c3563SJarkko Nikula {
990589c3563SJarkko Nikula 	int ret = 0;
99189b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
992888df395SMark Brown 	struct snd_soc_dai *dai;
993589c3563SJarkko Nikula 
994589c3563SJarkko Nikula 	codec->card = card;
995589c3563SJarkko Nikula 	codec->dapm.card = card;
996589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
997589c3563SJarkko Nikula 
99870d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
99970d29331SJarkko Nikula 		return -ENODEV;
100070d29331SJarkko Nikula 
1001d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1002d5d1e0beSLars-Peter Clausen 
100377530150SLars-Peter Clausen 	if (driver->dapm_widgets)
100477530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
100577530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
100677530150SLars-Peter Clausen 
1007888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1008888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1009888df395SMark Brown 		if (dai->dev != codec->dev)
1010888df395SMark Brown 			continue;
1011888df395SMark Brown 
1012888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1013888df395SMark Brown 	}
1014888df395SMark Brown 
101533c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
101633c5f969SMark Brown 
101789b95ac0SMark Brown 	if (driver->probe) {
101889b95ac0SMark Brown 		ret = driver->probe(codec);
1019589c3563SJarkko Nikula 		if (ret < 0) {
1020589c3563SJarkko Nikula 			dev_err(codec->dev,
1021589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1022589c3563SJarkko Nikula 				codec->name, ret);
102370d29331SJarkko Nikula 			goto err_probe;
1024589c3563SJarkko Nikula 		}
1025589c3563SJarkko Nikula 	}
1026589c3563SJarkko Nikula 
1027b7af1dafSMark Brown 	if (driver->controls)
1028022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1029b7af1dafSMark Brown 				     driver->num_controls);
103089b95ac0SMark Brown 	if (driver->dapm_routes)
103189b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
103289b95ac0SMark Brown 					driver->num_dapm_routes);
103389b95ac0SMark Brown 
1034589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1035589c3563SJarkko Nikula 	codec->probed = 1;
1036589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
10377be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1038589c3563SJarkko Nikula 
103970d29331SJarkko Nikula 	return 0;
104070d29331SJarkko Nikula 
104170d29331SJarkko Nikula err_probe:
1042d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
104370d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
104470d29331SJarkko Nikula 
1045589c3563SJarkko Nikula 	return ret;
1046589c3563SJarkko Nikula }
1047589c3563SJarkko Nikula 
1048956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1049956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1050956245e9SLiam Girdwood {
1051956245e9SLiam Girdwood 	int ret = 0;
1052956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1053be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1054956245e9SLiam Girdwood 
1055956245e9SLiam Girdwood 	platform->card = card;
1056b7950641SLiam Girdwood 	platform->dapm.card = card;
1057956245e9SLiam Girdwood 
1058956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1059956245e9SLiam Girdwood 		return -ENODEV;
1060956245e9SLiam Girdwood 
1061731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1062731f1ab2SSebastien Guiriec 
1063cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1064cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1065cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1066cb2cf612SLiam Girdwood 
1067be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
1068be09ad90SLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
1069be09ad90SLiam Girdwood 		if (dai->dev != platform->dev)
1070be09ad90SLiam Girdwood 			continue;
1071be09ad90SLiam Girdwood 
1072be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1073be09ad90SLiam Girdwood 	}
1074be09ad90SLiam Girdwood 
1075956245e9SLiam Girdwood 	if (driver->probe) {
1076956245e9SLiam Girdwood 		ret = driver->probe(platform);
1077956245e9SLiam Girdwood 		if (ret < 0) {
1078956245e9SLiam Girdwood 			dev_err(platform->dev,
1079956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1080956245e9SLiam Girdwood 				platform->name, ret);
1081956245e9SLiam Girdwood 			goto err_probe;
1082956245e9SLiam Girdwood 		}
1083956245e9SLiam Girdwood 	}
1084956245e9SLiam Girdwood 
1085cb2cf612SLiam Girdwood 	if (driver->controls)
1086cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1087cb2cf612SLiam Girdwood 				     driver->num_controls);
1088cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1089cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1090cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1091cb2cf612SLiam Girdwood 
1092956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1093956245e9SLiam Girdwood 	platform->probed = 1;
1094956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1095b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1096956245e9SLiam Girdwood 
1097956245e9SLiam Girdwood 	return 0;
1098956245e9SLiam Girdwood 
1099956245e9SLiam Girdwood err_probe:
110002db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1101956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1102956245e9SLiam Girdwood 
1103956245e9SLiam Girdwood 	return ret;
1104956245e9SLiam Girdwood }
1105956245e9SLiam Girdwood 
110636ae1a96SMark Brown static void rtd_release(struct device *dev)
110736ae1a96SMark Brown {
110836ae1a96SMark Brown 	kfree(dev);
110936ae1a96SMark Brown }
1110f0fba2adSLiam Girdwood 
1111589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1112589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1113589c3563SJarkko Nikula 				   int num, int dailess)
1114589c3563SJarkko Nikula {
1115589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1116589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1117589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1118589c3563SJarkko Nikula 	const char *temp, *name;
1119589c3563SJarkko Nikula 	int ret = 0;
1120589c3563SJarkko Nikula 
1121589c3563SJarkko Nikula 	if (!dailess) {
1122589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1123589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1124589c3563SJarkko Nikula 		name = dai_link->name;
1125589c3563SJarkko Nikula 	} else {
1126589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1127589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1128589c3563SJarkko Nikula 		name = aux_dev->name;
1129589c3563SJarkko Nikula 	}
11300962bb21SJanusz Krzysztofik 	rtd->card = card;
1131589c3563SJarkko Nikula 
11324b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
11334b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
11344b1cfcb4SMark Brown 
1135589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1136589c3563SJarkko Nikula 	temp = codec->name_prefix;
1137589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1138589c3563SJarkko Nikula 
1139589c3563SJarkko Nikula 	/* do machine specific initialization */
1140589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1141589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1142589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1143589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1144589c3563SJarkko Nikula 	if (ret < 0) {
1145589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1146589c3563SJarkko Nikula 		return ret;
1147589c3563SJarkko Nikula 	}
1148589c3563SJarkko Nikula 	codec->name_prefix = temp;
1149589c3563SJarkko Nikula 
1150589c3563SJarkko Nikula 	/* register the rtd device */
1151589c3563SJarkko Nikula 	rtd->codec = codec;
115236ae1a96SMark Brown 
115336ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
115436ae1a96SMark Brown 	if (!rtd->dev)
115536ae1a96SMark Brown 		return -ENOMEM;
115636ae1a96SMark Brown 	device_initialize(rtd->dev);
115736ae1a96SMark Brown 	rtd->dev->parent = card->dev;
115836ae1a96SMark Brown 	rtd->dev->release = rtd_release;
115936ae1a96SMark Brown 	rtd->dev->init_name = name;
116036ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1161b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
116236ae1a96SMark Brown 	ret = device_add(rtd->dev);
1163589c3563SJarkko Nikula 	if (ret < 0) {
1164589c3563SJarkko Nikula 		dev_err(card->dev,
1165589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1166589c3563SJarkko Nikula 		return ret;
1167589c3563SJarkko Nikula 	}
1168589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1169589c3563SJarkko Nikula 
1170589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
117136ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1172589c3563SJarkko Nikula 	if (ret < 0)
1173589c3563SJarkko Nikula 		dev_err(codec->dev,
1174589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1175589c3563SJarkko Nikula 			ret);
1176589c3563SJarkko Nikula 
1177589c3563SJarkko Nikula 	/* add codec sysfs entries */
117836ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1179589c3563SJarkko Nikula 	if (ret < 0)
1180589c3563SJarkko Nikula 		dev_err(codec->dev,
1181589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1182589c3563SJarkko Nikula 
1183589c3563SJarkko Nikula 	return 0;
1184589c3563SJarkko Nikula }
1185589c3563SJarkko Nikula 
11860168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1187f0fba2adSLiam Girdwood {
1188f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1189f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1190f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1191f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1192f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1193f0fba2adSLiam Girdwood 	int ret;
1194f0fba2adSLiam Girdwood 
11950168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
11960168bf0dSLiam Girdwood 			card->name, num, order);
1197f0fba2adSLiam Girdwood 
1198f0fba2adSLiam Girdwood 	/* config components */
1199f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1200f0fba2adSLiam Girdwood 	codec_dai->card = card;
1201f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1202f0fba2adSLiam Girdwood 
1203f0fba2adSLiam Girdwood 	/* set default power off timeout */
1204f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1205f0fba2adSLiam Girdwood 
1206f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
12070168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
12080168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1209be09ad90SLiam Girdwood 		cpu_dai->dapm.card = card;
121061b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
121161b61e3cSLiam Girdwood 			return -ENODEV;
121261b61e3cSLiam Girdwood 
1213be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1214be09ad90SLiam Girdwood 
1215f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1216f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1217f0fba2adSLiam Girdwood 			if (ret < 0) {
12180837fc62SFabio Estevam 				pr_err("asoc: failed to probe CPU DAI %s: %d\n",
12190837fc62SFabio Estevam 							cpu_dai->name, ret);
122061b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1221f0fba2adSLiam Girdwood 				return ret;
1222f0fba2adSLiam Girdwood 			}
1223f0fba2adSLiam Girdwood 		}
1224f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
12251c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1226f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1227f0fba2adSLiam Girdwood 	}
1228f0fba2adSLiam Girdwood 
1229f0fba2adSLiam Girdwood 	/* probe the CODEC */
12300168bf0dSLiam Girdwood 	if (!codec->probed &&
12310168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1232589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1233589c3563SJarkko Nikula 		if (ret < 0)
1234f0fba2adSLiam Girdwood 			return ret;
1235f0fba2adSLiam Girdwood 	}
1236f0fba2adSLiam Girdwood 
1237f0fba2adSLiam Girdwood 	/* probe the platform */
12380168bf0dSLiam Girdwood 	if (!platform->probed &&
12390168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1240956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1241956245e9SLiam Girdwood 		if (ret < 0)
1242f0fba2adSLiam Girdwood 			return ret;
1243f0fba2adSLiam Girdwood 	}
1244f0fba2adSLiam Girdwood 
1245f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
12460168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1247f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1248f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1249f0fba2adSLiam Girdwood 			if (ret < 0) {
12500837fc62SFabio Estevam 				pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
12510837fc62SFabio Estevam 							codec_dai->name, ret);
1252f0fba2adSLiam Girdwood 				return ret;
1253f0fba2adSLiam Girdwood 			}
1254f0fba2adSLiam Girdwood 		}
1255f0fba2adSLiam Girdwood 
12561c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1257f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1258f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1259f0fba2adSLiam Girdwood 	}
1260f0fba2adSLiam Girdwood 
12610168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
12620168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
12630168bf0dSLiam Girdwood 		return 0;
12640168bf0dSLiam Girdwood 
1265589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1266589c3563SJarkko Nikula 	if (ret)
1267f0fba2adSLiam Girdwood 		return ret;
1268f0fba2adSLiam Girdwood 
126936ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1270f0fba2adSLiam Girdwood 	if (ret < 0)
12710837fc62SFabio Estevam 		pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1272f0fba2adSLiam Girdwood 
1273f0fba2adSLiam Girdwood 	/* create the pcm */
1274f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1275f0fba2adSLiam Girdwood 	if (ret < 0) {
12760837fc62SFabio Estevam 		pr_err("asoc: can't create pcm %s :%d\n",
12770837fc62SFabio Estevam 				dai_link->stream_name, ret);
1278f0fba2adSLiam Girdwood 		return ret;
1279f0fba2adSLiam Girdwood 	}
1280f0fba2adSLiam Girdwood 
1281f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1282f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1283f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1284f0fba2adSLiam Girdwood 
1285f0fba2adSLiam Girdwood 	return 0;
1286f0fba2adSLiam Girdwood }
1287f0fba2adSLiam Girdwood 
1288f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1289f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1290f0fba2adSLiam Girdwood {
1291f0fba2adSLiam Girdwood 	int ret;
1292f0fba2adSLiam Girdwood 
1293f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1294f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
12956b05eda6SMark Brown 	 */
1296f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
12970562f788SMika Westerberg 		/*
12980562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
12990562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
13000562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
13010562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
13020562f788SMika Westerberg 		 *
13030562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
13040562f788SMika Westerberg 		 */
13050562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
13060562f788SMika Westerberg 			return 0;
1307f0fba2adSLiam Girdwood 
1308f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1309f0fba2adSLiam Girdwood 		if (ret < 0) {
13100837fc62SFabio Estevam 			pr_err("asoc: AC97 device register failed:%d\n", ret);
1311f0fba2adSLiam Girdwood 			return ret;
1312435c5e25SMark Brown 		}
1313435c5e25SMark Brown 
1314f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1315f0fba2adSLiam Girdwood 	}
1316f0fba2adSLiam Girdwood 	return 0;
1317f0fba2adSLiam Girdwood }
1318435c5e25SMark Brown 
1319f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1320f0fba2adSLiam Girdwood {
1321f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1322f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1323f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1324f0fba2adSLiam Girdwood 	}
1325f0fba2adSLiam Girdwood }
1326f0fba2adSLiam Girdwood #endif
1327435c5e25SMark Brown 
1328b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1329b19e6e7bSMark Brown {
1330b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1331b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1332b19e6e7bSMark Brown 
1333b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1334b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1335b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1336b19e6e7bSMark Brown 			return 0;
1337b19e6e7bSMark Brown 	}
1338b19e6e7bSMark Brown 
1339b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1340b19e6e7bSMark Brown }
1341b19e6e7bSMark Brown 
13422eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
13432eea392dSJarkko Nikula {
13442eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
13452eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1346676ad98aSJarkko Nikula 	int ret = -ENODEV;
13472eea392dSJarkko Nikula 
13482eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
13492eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
13502eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
13512eea392dSJarkko Nikula 			if (codec->probed) {
13522eea392dSJarkko Nikula 				dev_err(codec->dev,
13532eea392dSJarkko Nikula 					"asoc: codec already probed");
13542eea392dSJarkko Nikula 				ret = -EBUSY;
13552eea392dSJarkko Nikula 				goto out;
13562eea392dSJarkko Nikula 			}
1357676ad98aSJarkko Nikula 			goto found;
13582eea392dSJarkko Nikula 		}
13592eea392dSJarkko Nikula 	}
1360676ad98aSJarkko Nikula 	/* codec not found */
1361676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1362b19e6e7bSMark Brown 	return -EPROBE_DEFER;
13632eea392dSJarkko Nikula 
1364676ad98aSJarkko Nikula found:
1365589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
13662eea392dSJarkko Nikula 	if (ret < 0)
1367589c3563SJarkko Nikula 		return ret;
13682eea392dSJarkko Nikula 
1369589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
13702eea392dSJarkko Nikula 
13712eea392dSJarkko Nikula out:
13722eea392dSJarkko Nikula 	return ret;
13732eea392dSJarkko Nikula }
13742eea392dSJarkko Nikula 
13752eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
13762eea392dSJarkko Nikula {
13772eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
13782eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
13792eea392dSJarkko Nikula 
13802eea392dSJarkko Nikula 	/* unregister the rtd device */
13812eea392dSJarkko Nikula 	if (rtd->dev_registered) {
138236ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
138336ae1a96SMark Brown 		device_del(rtd->dev);
13842eea392dSJarkko Nikula 		rtd->dev_registered = 0;
13852eea392dSJarkko Nikula 	}
13862eea392dSJarkko Nikula 
1387589c3563SJarkko Nikula 	if (codec && codec->probed)
1388589c3563SJarkko Nikula 		soc_remove_codec(codec);
13892eea392dSJarkko Nikula }
13902eea392dSJarkko Nikula 
1391fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1392fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1393fdf0f54dSDimitris Papastamos {
1394fdf0f54dSDimitris Papastamos 	int ret;
1395fdf0f54dSDimitris Papastamos 
1396fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1397fdf0f54dSDimitris Papastamos 		return 0;
1398fdf0f54dSDimitris Papastamos 
1399fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1400fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1401fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1402fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1403fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1404fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1405fdf0f54dSDimitris Papastamos 			ret);
1406fdf0f54dSDimitris Papastamos 		return ret;
1407fdf0f54dSDimitris Papastamos 	}
1408fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1409fdf0f54dSDimitris Papastamos 	return 0;
1410fdf0f54dSDimitris Papastamos }
1411fdf0f54dSDimitris Papastamos 
1412b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1413f0fba2adSLiam Girdwood {
1414fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1415fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1416fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
141775d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1418f04209a7SMark Brown 	int ret, i, order, dai_fmt;
141996dd3622SMark Brown 
142001b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1421f0fba2adSLiam Girdwood 
1422b19e6e7bSMark Brown 	/* bind DAIs */
1423b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1424b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1425b19e6e7bSMark Brown 		if (ret != 0)
1426b19e6e7bSMark Brown 			goto base_error;
1427db2a4165SFrank Mandarino 	}
1428db2a4165SFrank Mandarino 
1429b19e6e7bSMark Brown 	/* check aux_devs too */
1430b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1431b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1432b19e6e7bSMark Brown 		if (ret != 0)
1433b19e6e7bSMark Brown 			goto base_error;
1434db2a4165SFrank Mandarino 	}
1435db2a4165SFrank Mandarino 
1436fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1437fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1438fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1439fdf0f54dSDimitris Papastamos 			continue;
1440861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1441861f2fafSDimitris Papastamos 		compress_type = 0;
1442fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1443fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1444fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1445fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1446fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1447fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1448fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1449fdf0f54dSDimitris Papastamos 					break;
1450fdf0f54dSDimitris Papastamos 			}
1451fdf0f54dSDimitris Papastamos 		}
1452fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1453b19e6e7bSMark Brown 		if (ret < 0)
1454b19e6e7bSMark Brown 			goto base_error;
1455fdf0f54dSDimitris Papastamos 	}
1456fdf0f54dSDimitris Papastamos 
1457f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1458f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1459f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1460f0fba2adSLiam Girdwood 	if (ret < 0) {
14610837fc62SFabio Estevam 		pr_err("asoc: can't create sound card for card %s: %d\n",
14620837fc62SFabio Estevam 			card->name, ret);
1463b19e6e7bSMark Brown 		goto base_error;
1464db2a4165SFrank Mandarino 	}
1465f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1466db2a4165SFrank Mandarino 
1467e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1468e37a4970SMark Brown 	card->dapm.dev = card->dev;
1469e37a4970SMark Brown 	card->dapm.card = card;
1470e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1471e37a4970SMark Brown 
1472d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1473d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1474d5d1e0beSLars-Peter Clausen #endif
1475d5d1e0beSLars-Peter Clausen 
147688ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
14776ed25978SAndy Green 	/* deferred resume work */
14786308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
14791301a964SRandy Dunlap #endif
14806ed25978SAndy Green 
14819a841ebbSMark Brown 	if (card->dapm_widgets)
14829a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
14839a841ebbSMark Brown 					  card->num_dapm_widgets);
14849a841ebbSMark Brown 
1485f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1486f0fba2adSLiam Girdwood 	if (card->probe) {
1487e7361ec4SMark Brown 		ret = card->probe(card);
1488f0fba2adSLiam Girdwood 		if (ret < 0)
1489f0fba2adSLiam Girdwood 			goto card_probe_error;
1490f0fba2adSLiam Girdwood 	}
1491f0fba2adSLiam Girdwood 
14920168bf0dSLiam Girdwood 	/* early DAI link probe */
14930168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
14940168bf0dSLiam Girdwood 			order++) {
1495fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
14960168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1497fe3e78e0SMark Brown 			if (ret < 0) {
1498321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1499321de0d0SMark Brown 			       card->name, ret);
1500f0fba2adSLiam Girdwood 				goto probe_dai_err;
1501fe3e78e0SMark Brown 			}
1502fe3e78e0SMark Brown 		}
15030168bf0dSLiam Girdwood 	}
1504fe3e78e0SMark Brown 
15052eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
15062eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
15072eea392dSJarkko Nikula 		if (ret < 0) {
15082eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
15092eea392dSJarkko Nikula 			       card->name, ret);
15102eea392dSJarkko Nikula 			goto probe_aux_dev_err;
15112eea392dSJarkko Nikula 		}
15122eea392dSJarkko Nikula 	}
15132eea392dSJarkko Nikula 
1514888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1515888df395SMark Brown 
1516b7af1dafSMark Brown 	if (card->controls)
1517022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1518b7af1dafSMark Brown 
1519b8ad29deSMark Brown 	if (card->dapm_routes)
1520b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1521b8ad29deSMark Brown 					card->num_dapm_routes);
1522b8ad29deSMark Brown 
1523b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1524b90d2f90SMark Brown 
152575d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
152675d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1527f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
152875d9ac46SMark Brown 
1529f04209a7SMark Brown 		if (dai_fmt) {
153075d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1531f04209a7SMark Brown 						  dai_fmt);
15325e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
153375d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
153475d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
153575d9ac46SMark Brown 					 ret);
1536f04209a7SMark Brown 		}
1537f04209a7SMark Brown 
1538f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1539f04209a7SMark Brown 		if (dai_fmt && dai_link->platform_name) {
1540f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1541f04209a7SMark Brown 						  dai_fmt);
1542f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1543f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1544f04209a7SMark Brown 					 "Failed to set DAI format: %d\n",
1545f04209a7SMark Brown 					 ret);
1546f04209a7SMark Brown 		} else if (dai_fmt) {
1547f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1548f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1549f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1550f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1551f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1552f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1553f04209a7SMark Brown 				break;
1554f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1555f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1556f04209a7SMark Brown 				break;
1557f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1558f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1559f04209a7SMark Brown 				break;
1560f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1561f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1562f04209a7SMark Brown 				break;
1563f04209a7SMark Brown 			}
156475d9ac46SMark Brown 
156575d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1566f04209a7SMark Brown 						  dai_fmt);
15675e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
156875d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
156975d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
157075d9ac46SMark Brown 					 ret);
157175d9ac46SMark Brown 		}
157275d9ac46SMark Brown 	}
157375d9ac46SMark Brown 
1574f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1575fe3e78e0SMark Brown 		 "%s", card->name);
1576f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
157722de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1578f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1579f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1580f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1581f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1582f0e8ed85SMark Brown 		case '_':
1583f0e8ed85SMark Brown 		case '-':
1584f0e8ed85SMark Brown 		case '\0':
1585f0e8ed85SMark Brown 			break;
1586f0e8ed85SMark Brown 		default:
1587f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1588f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1589f0e8ed85SMark Brown 			break;
1590f0e8ed85SMark Brown 		}
1591f0e8ed85SMark Brown 	}
1592fe3e78e0SMark Brown 
159328e9ad92SMark Brown 	if (card->late_probe) {
159428e9ad92SMark Brown 		ret = card->late_probe(card);
159528e9ad92SMark Brown 		if (ret < 0) {
159628e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
159728e9ad92SMark Brown 				card->name, ret);
159828e9ad92SMark Brown 			goto probe_aux_dev_err;
159928e9ad92SMark Brown 		}
160028e9ad92SMark Brown 	}
160128e9ad92SMark Brown 
16022dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
16032dc00213SMark Brown 
16041633281bSStephen Warren 	if (card->fully_routed)
1605b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
16061633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
16071633281bSStephen Warren 
1608f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1609fe3e78e0SMark Brown 	if (ret < 0) {
16100837fc62SFabio Estevam 		pr_err("asoc: failed to register soundcard for %s: %d\n",
16110837fc62SFabio Estevam 							card->name, ret);
16126b3ed785SAxel Lin 		goto probe_aux_dev_err;
1613fe3e78e0SMark Brown 	}
1614fe3e78e0SMark Brown 
1615fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1616f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1617f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1618f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1619fe3e78e0SMark Brown 		if (ret < 0) {
16200837fc62SFabio Estevam 			pr_err("asoc: failed to register AC97 %s: %d\n",
16210837fc62SFabio Estevam 							card->name, ret);
16226b3ed785SAxel Lin 			while (--i >= 0)
16237d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
16246b3ed785SAxel Lin 			goto probe_aux_dev_err;
1625fe3e78e0SMark Brown 		}
1626fe3e78e0SMark Brown 	}
1627fe3e78e0SMark Brown #endif
1628fe3e78e0SMark Brown 
1629435c5e25SMark Brown 	card->instantiated = 1;
16304f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1631f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1632b19e6e7bSMark Brown 
1633b19e6e7bSMark Brown 	return 0;
1634db2a4165SFrank Mandarino 
16352eea392dSJarkko Nikula probe_aux_dev_err:
16362eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16372eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16382eea392dSJarkko Nikula 
1639f0fba2adSLiam Girdwood probe_dai_err:
16400671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1641fe3e78e0SMark Brown 
1642f0fba2adSLiam Girdwood card_probe_error:
164387506549SMark Brown 	if (card->remove)
1644e7361ec4SMark Brown 		card->remove(card);
1645f0fba2adSLiam Girdwood 
1646f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1647f0fba2adSLiam Girdwood 
1648b19e6e7bSMark Brown base_error:
1649f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1650db2a4165SFrank Mandarino 
1651b19e6e7bSMark Brown 	return ret;
1652435c5e25SMark Brown }
1653435c5e25SMark Brown 
1654435c5e25SMark Brown /* probes a new socdev */
1655435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1656435c5e25SMark Brown {
1657f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1658435c5e25SMark Brown 	int ret = 0;
1659435c5e25SMark Brown 
166070a7ca34SVinod Koul 	/*
166170a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
166270a7ca34SVinod Koul 	 * we should not be here in that case so ret error
166370a7ca34SVinod Koul 	 */
166470a7ca34SVinod Koul 	if (!card)
166570a7ca34SVinod Koul 		return -EINVAL;
166670a7ca34SVinod Koul 
1667fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1668fe4085e8SMark Brown 		 "ASoC machine %s should use snd_soc_register_card()\n",
1669fe4085e8SMark Brown 		 card->name);
1670fe4085e8SMark Brown 
1671435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1672435c5e25SMark Brown 	card->dev = &pdev->dev;
1673f0fba2adSLiam Girdwood 
1674435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1675435c5e25SMark Brown 	if (ret != 0) {
1676435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1677db2a4165SFrank Mandarino 		return ret;
1678db2a4165SFrank Mandarino 	}
1679db2a4165SFrank Mandarino 
1680435c5e25SMark Brown 	return 0;
1681435c5e25SMark Brown }
1682435c5e25SMark Brown 
1683b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1684db2a4165SFrank Mandarino {
1685db2a4165SFrank Mandarino 	int i;
1686db2a4165SFrank Mandarino 
1687f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1688f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1689f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
16905b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1691db2a4165SFrank Mandarino 	}
1692db2a4165SFrank Mandarino 
16932eea392dSJarkko Nikula 	/* remove auxiliary devices */
16942eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16952eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16962eea392dSJarkko Nikula 
1697f0fba2adSLiam Girdwood 	/* remove and free each DAI */
16980671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1699f0fba2adSLiam Girdwood 
1700a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1701a6052154SJarkko Nikula 
1702f0fba2adSLiam Girdwood 	/* remove the card */
170387506549SMark Brown 	if (card->remove)
1704e7361ec4SMark Brown 		card->remove(card);
1705f0fba2adSLiam Girdwood 
17060aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
17070aaae527SLars-Peter Clausen 
1708f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1709b0e26485SVinod Koul 	return 0;
1710b0e26485SVinod Koul 
1711b2dfa62cSGuennadi Liakhovetski }
1712b0e26485SVinod Koul 
1713b0e26485SVinod Koul /* removes a socdev */
1714b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1715b0e26485SVinod Koul {
1716b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1717b0e26485SVinod Koul 
1718c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1719db2a4165SFrank Mandarino 	return 0;
1720db2a4165SFrank Mandarino }
1721db2a4165SFrank Mandarino 
17226f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
172351737470SMark Brown {
17246f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1725f0fba2adSLiam Girdwood 	int i;
172651737470SMark Brown 
172751737470SMark Brown 	if (!card->instantiated)
1728416356fcSMark Brown 		return 0;
172951737470SMark Brown 
173051737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
173151737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1732f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1733f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17345b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1735f0fba2adSLiam Girdwood 	}
173651737470SMark Brown 
1737f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1738416356fcSMark Brown 
1739416356fcSMark Brown 	return 0;
174051737470SMark Brown }
17416f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
174251737470SMark Brown 
17436f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1744b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1745b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1746b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1747b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
17486f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1749b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1750416356fcSMark Brown };
1751deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1752416356fcSMark Brown 
1753db2a4165SFrank Mandarino /* ASoC platform driver */
1754db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1755db2a4165SFrank Mandarino 	.driver		= {
1756db2a4165SFrank Mandarino 		.name		= "soc-audio",
17578b45a209SKay Sievers 		.owner		= THIS_MODULE,
17586f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1759db2a4165SFrank Mandarino 	},
1760db2a4165SFrank Mandarino 	.probe		= soc_probe,
1761db2a4165SFrank Mandarino 	.remove		= soc_remove,
1762db2a4165SFrank Mandarino };
1763db2a4165SFrank Mandarino 
1764096e49d5SMark Brown /**
1765096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1766096e49d5SMark Brown  *
1767096e49d5SMark Brown  * @codec: CODEC to query.
1768096e49d5SMark Brown  * @reg: Register to query.
1769096e49d5SMark Brown  *
1770096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1771096e49d5SMark Brown  */
1772181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1773181e055eSMark Brown 				    unsigned int reg)
1774096e49d5SMark Brown {
17751500b7b5SDimitris Papastamos 	if (codec->volatile_register)
17761500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1777096e49d5SMark Brown 	else
1778096e49d5SMark Brown 		return 0;
1779096e49d5SMark Brown }
1780096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1781096e49d5SMark Brown 
1782db2a4165SFrank Mandarino /**
1783239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1784239c9706SDimitris Papastamos  *
1785239c9706SDimitris Papastamos  * @codec: CODEC to query.
1786239c9706SDimitris Papastamos  * @reg: Register to query.
1787239c9706SDimitris Papastamos  *
1788239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1789239c9706SDimitris Papastamos  */
1790239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1791239c9706SDimitris Papastamos 				    unsigned int reg)
1792239c9706SDimitris Papastamos {
1793239c9706SDimitris Papastamos 	if (codec->readable_register)
1794239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1795239c9706SDimitris Papastamos 	else
179663fa0a28SLars-Peter Clausen 		return 1;
1797239c9706SDimitris Papastamos }
1798239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1799239c9706SDimitris Papastamos 
1800239c9706SDimitris Papastamos /**
1801239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1802239c9706SDimitris Papastamos  *
1803239c9706SDimitris Papastamos  * @codec: CODEC to query.
1804239c9706SDimitris Papastamos  * @reg: Register to query.
1805239c9706SDimitris Papastamos  *
1806239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1807239c9706SDimitris Papastamos  */
1808239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1809239c9706SDimitris Papastamos 				    unsigned int reg)
1810239c9706SDimitris Papastamos {
1811239c9706SDimitris Papastamos 	if (codec->writable_register)
1812239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1813239c9706SDimitris Papastamos 	else
181463fa0a28SLars-Peter Clausen 		return 1;
1815239c9706SDimitris Papastamos }
1816239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1817239c9706SDimitris Papastamos 
1818f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1819f1442bc1SLiam Girdwood 					unsigned int reg)
1820f1442bc1SLiam Girdwood {
1821f1442bc1SLiam Girdwood 	unsigned int ret;
1822f1442bc1SLiam Girdwood 
1823f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1824f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1825f1442bc1SLiam Girdwood 		return -1;
1826f1442bc1SLiam Girdwood 	}
1827f1442bc1SLiam Girdwood 
1828f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1829f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1830a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1831f1442bc1SLiam Girdwood 
1832f1442bc1SLiam Girdwood 	return ret;
1833f1442bc1SLiam Girdwood }
1834f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1835f1442bc1SLiam Girdwood 
1836f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1837f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1838f1442bc1SLiam Girdwood {
1839f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1840f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1841f1442bc1SLiam Girdwood 		return -1;
1842f1442bc1SLiam Girdwood 	}
1843f1442bc1SLiam Girdwood 
1844f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1845a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
1846f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1847f1442bc1SLiam Girdwood }
1848f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1849f1442bc1SLiam Girdwood 
1850239c9706SDimitris Papastamos /**
1851db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1852db2a4165SFrank Mandarino  * @codec: audio codec
1853db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1854db2a4165SFrank Mandarino  * @num: AC97 codec number
1855db2a4165SFrank Mandarino  *
1856db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1857db2a4165SFrank Mandarino  */
1858db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1859db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1860db2a4165SFrank Mandarino {
1861db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1862db2a4165SFrank Mandarino 
1863db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1864db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1865db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1866db2a4165SFrank Mandarino 		return -ENOMEM;
1867db2a4165SFrank Mandarino 	}
1868db2a4165SFrank Mandarino 
1869db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1870db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1871db2a4165SFrank Mandarino 		kfree(codec->ac97);
1872db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1873db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1874db2a4165SFrank Mandarino 		return -ENOMEM;
1875db2a4165SFrank Mandarino 	}
1876db2a4165SFrank Mandarino 
1877db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1878db2a4165SFrank Mandarino 	codec->ac97->num = num;
18790562f788SMika Westerberg 
18800562f788SMika Westerberg 	/*
18810562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
18820562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
18830562f788SMika Westerberg 	 */
18840562f788SMika Westerberg 	codec->ac97_created = 1;
18850562f788SMika Westerberg 
1886db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1887db2a4165SFrank Mandarino 	return 0;
1888db2a4165SFrank Mandarino }
1889db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1890db2a4165SFrank Mandarino 
1891db2a4165SFrank Mandarino /**
1892db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1893db2a4165SFrank Mandarino  * @codec: audio codec
1894db2a4165SFrank Mandarino  *
1895db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1896db2a4165SFrank Mandarino  */
1897db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1898db2a4165SFrank Mandarino {
1899db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1900f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1901f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1902f0fba2adSLiam Girdwood #endif
1903db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1904db2a4165SFrank Mandarino 	kfree(codec->ac97);
1905db2a4165SFrank Mandarino 	codec->ac97 = NULL;
19060562f788SMika Westerberg 	codec->ac97_created = 0;
1907db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1908db2a4165SFrank Mandarino }
1909db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1910db2a4165SFrank Mandarino 
1911c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1912c3753707SMark Brown {
1913c3753707SMark Brown 	unsigned int ret;
1914c3753707SMark Brown 
1915c3acec26SMark Brown 	ret = codec->read(codec, reg);
1916c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1917a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1918c3753707SMark Brown 
1919c3753707SMark Brown 	return ret;
1920c3753707SMark Brown }
1921c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1922c3753707SMark Brown 
1923c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1924c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1925c3753707SMark Brown {
1926c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1927a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1928c3acec26SMark Brown 	return codec->write(codec, reg, val);
1929c3753707SMark Brown }
1930c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1931c3753707SMark Brown 
19325fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
19335fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
19345fb609d4SDimitris Papastamos {
19355fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
19365fb609d4SDimitris Papastamos }
19375fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
19385fb609d4SDimitris Papastamos 
1939db2a4165SFrank Mandarino /**
1940db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1941db2a4165SFrank Mandarino  * @codec: audio codec
1942db2a4165SFrank Mandarino  * @reg: codec register
1943db2a4165SFrank Mandarino  * @mask: register mask
1944db2a4165SFrank Mandarino  * @value: new value
1945db2a4165SFrank Mandarino  *
1946db2a4165SFrank Mandarino  * Writes new register value.
1947db2a4165SFrank Mandarino  *
1948180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
1949db2a4165SFrank Mandarino  */
1950db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
195146f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1952db2a4165SFrank Mandarino {
19538a713da8SMark Brown 	bool change;
195446f5822fSDaniel Ribeiro 	unsigned int old, new;
1955180c329dSTimur Tabi 	int ret;
1956db2a4165SFrank Mandarino 
19578a713da8SMark Brown 	if (codec->using_regmap) {
19588a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
19598a713da8SMark Brown 					       mask, value, &change);
19608a713da8SMark Brown 	} else {
1961180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
1962180c329dSTimur Tabi 		if (ret < 0)
1963180c329dSTimur Tabi 			return ret;
1964180c329dSTimur Tabi 
1965180c329dSTimur Tabi 		old = ret;
196678bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
1967db2a4165SFrank Mandarino 		change = old != new;
19688a713da8SMark Brown 		if (change)
1969180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
19708a713da8SMark Brown 	}
19718a713da8SMark Brown 
1972180c329dSTimur Tabi 	if (ret < 0)
1973180c329dSTimur Tabi 		return ret;
1974db2a4165SFrank Mandarino 
1975db2a4165SFrank Mandarino 	return change;
1976db2a4165SFrank Mandarino }
1977db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1978db2a4165SFrank Mandarino 
1979db2a4165SFrank Mandarino /**
19806c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
19816c508c62SEero Nurkkala  * @codec: audio codec
19826c508c62SEero Nurkkala  * @reg: codec register
19836c508c62SEero Nurkkala  * @mask: register mask
19846c508c62SEero Nurkkala  * @value: new value
19856c508c62SEero Nurkkala  *
19866c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
19876c508c62SEero Nurkkala  *
19886c508c62SEero Nurkkala  * Returns 1 for change else 0.
19896c508c62SEero Nurkkala  */
1990dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
19916c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
19926c508c62SEero Nurkkala 			       unsigned int value)
19936c508c62SEero Nurkkala {
19946c508c62SEero Nurkkala 	int change;
19956c508c62SEero Nurkkala 
19966c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
19976c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
19986c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
19996c508c62SEero Nurkkala 
20006c508c62SEero Nurkkala 	return change;
20016c508c62SEero Nurkkala }
2002dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
20036c508c62SEero Nurkkala 
20046c508c62SEero Nurkkala /**
2005db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2006db2a4165SFrank Mandarino  * @codec: audio codec
2007db2a4165SFrank Mandarino  * @reg: codec register
2008db2a4165SFrank Mandarino  * @mask: register mask
2009db2a4165SFrank Mandarino  * @value: new value
2010db2a4165SFrank Mandarino  *
2011db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2012db2a4165SFrank Mandarino  * different from the old value.
2013db2a4165SFrank Mandarino  *
2014db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2015db2a4165SFrank Mandarino  */
2016db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
201746f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2018db2a4165SFrank Mandarino {
2019db2a4165SFrank Mandarino 	int change;
202046f5822fSDaniel Ribeiro 	unsigned int old, new;
2021db2a4165SFrank Mandarino 
2022db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2023db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2024db2a4165SFrank Mandarino 	change = old != new;
2025db2a4165SFrank Mandarino 
2026db2a4165SFrank Mandarino 	return change;
2027db2a4165SFrank Mandarino }
2028db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2029db2a4165SFrank Mandarino 
2030db2a4165SFrank Mandarino /**
2031db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2032db2a4165SFrank Mandarino  * @substream: the pcm substream
2033db2a4165SFrank Mandarino  * @hw: the hardware parameters
2034db2a4165SFrank Mandarino  *
2035db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2036db2a4165SFrank Mandarino  */
2037db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2038db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2039db2a4165SFrank Mandarino {
2040db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2041db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2042db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2043db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2044db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2045db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2046db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2047db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2048db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2049db2a4165SFrank Mandarino 	return 0;
2050db2a4165SFrank Mandarino }
2051db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2052db2a4165SFrank Mandarino 
2053db2a4165SFrank Mandarino /**
2054db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2055db2a4165SFrank Mandarino  * @_template: control template
2056db2a4165SFrank Mandarino  * @data: control private data
2057ac11a2b3SMark Brown  * @long_name: control long name
2058efb7ac3fSMark Brown  * @prefix: control name prefix
2059db2a4165SFrank Mandarino  *
2060db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2061db2a4165SFrank Mandarino  *
2062db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2063db2a4165SFrank Mandarino  */
2064db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
20653056557fSMark Brown 				  void *data, const char *long_name,
2066efb7ac3fSMark Brown 				  const char *prefix)
2067db2a4165SFrank Mandarino {
2068db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2069efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2070efb7ac3fSMark Brown 	char *name = NULL;
2071efb7ac3fSMark Brown 	int name_len;
2072db2a4165SFrank Mandarino 
2073db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2074db2a4165SFrank Mandarino 	template.index = 0;
2075db2a4165SFrank Mandarino 
2076efb7ac3fSMark Brown 	if (!long_name)
2077efb7ac3fSMark Brown 		long_name = template.name;
2078efb7ac3fSMark Brown 
2079efb7ac3fSMark Brown 	if (prefix) {
2080efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
208157cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2082efb7ac3fSMark Brown 		if (!name)
2083efb7ac3fSMark Brown 			return NULL;
2084efb7ac3fSMark Brown 
2085efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2086efb7ac3fSMark Brown 
2087efb7ac3fSMark Brown 		template.name = name;
2088efb7ac3fSMark Brown 	} else {
2089efb7ac3fSMark Brown 		template.name = long_name;
2090efb7ac3fSMark Brown 	}
2091efb7ac3fSMark Brown 
2092efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2093efb7ac3fSMark Brown 
2094efb7ac3fSMark Brown 	kfree(name);
2095efb7ac3fSMark Brown 
2096efb7ac3fSMark Brown 	return kcontrol;
2097db2a4165SFrank Mandarino }
2098db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2099db2a4165SFrank Mandarino 
2100022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2101022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2102022658beSLiam Girdwood 	const char *prefix, void *data)
2103022658beSLiam Girdwood {
2104022658beSLiam Girdwood 	int err, i;
2105022658beSLiam Girdwood 
2106022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2107022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2108022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2109022658beSLiam Girdwood 						     control->name, prefix));
2110022658beSLiam Girdwood 		if (err < 0) {
2111022658beSLiam Girdwood 			dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2112022658beSLiam Girdwood 			return err;
2113022658beSLiam Girdwood 		}
2114022658beSLiam Girdwood 	}
2115022658beSLiam Girdwood 
2116022658beSLiam Girdwood 	return 0;
2117022658beSLiam Girdwood }
2118022658beSLiam Girdwood 
2119db2a4165SFrank Mandarino /**
2120022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2121022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
21223e8e1952SIan Molton  * duplicating this code.
21233e8e1952SIan Molton  *
21243e8e1952SIan Molton  * @codec: codec to add controls to
21253e8e1952SIan Molton  * @controls: array of controls to add
21263e8e1952SIan Molton  * @num_controls: number of elements in the array
21273e8e1952SIan Molton  *
21283e8e1952SIan Molton  * Return 0 for success, else error.
21293e8e1952SIan Molton  */
2130022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
21313e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
21323e8e1952SIan Molton {
2133f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
21343e8e1952SIan Molton 
2135022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2136022658beSLiam Girdwood 			codec->name_prefix, codec);
21373e8e1952SIan Molton }
2138022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
21393e8e1952SIan Molton 
21403e8e1952SIan Molton /**
2141a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2142022658beSLiam Girdwood  * Convenience function to add a list of controls.
2143a491a5c8SLiam Girdwood  *
2144a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2145a491a5c8SLiam Girdwood  * @controls: array of controls to add
2146a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2147a491a5c8SLiam Girdwood  *
2148a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2149a491a5c8SLiam Girdwood  */
2150a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2151a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2152a491a5c8SLiam Girdwood {
2153a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2154a491a5c8SLiam Girdwood 
2155022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2156022658beSLiam Girdwood 			NULL, platform);
2157a491a5c8SLiam Girdwood }
2158a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2159a491a5c8SLiam Girdwood 
2160a491a5c8SLiam Girdwood /**
2161022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2162022658beSLiam Girdwood  * Convenience function to add a list of controls.
2163022658beSLiam Girdwood  *
2164022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2165022658beSLiam Girdwood  * @controls: array of controls to add
2166022658beSLiam Girdwood  * @num_controls: number of elements in the array
2167022658beSLiam Girdwood  *
2168022658beSLiam Girdwood  * Return 0 for success, else error.
2169022658beSLiam Girdwood  */
2170022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2171022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2172022658beSLiam Girdwood {
2173022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2174022658beSLiam Girdwood 
2175022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2176022658beSLiam Girdwood 			NULL, soc_card);
2177022658beSLiam Girdwood }
2178022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2179022658beSLiam Girdwood 
2180022658beSLiam Girdwood /**
2181022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2182022658beSLiam Girdwood  * Convienience function to add a list of controls.
2183022658beSLiam Girdwood  *
2184022658beSLiam Girdwood  * @dai: DAI to add controls to
2185022658beSLiam Girdwood  * @controls: array of controls to add
2186022658beSLiam Girdwood  * @num_controls: number of elements in the array
2187022658beSLiam Girdwood  *
2188022658beSLiam Girdwood  * Return 0 for success, else error.
2189022658beSLiam Girdwood  */
2190022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2191022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2192022658beSLiam Girdwood {
2193022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2194022658beSLiam Girdwood 
2195022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2196022658beSLiam Girdwood 			NULL, dai);
2197022658beSLiam Girdwood }
2198022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2199022658beSLiam Girdwood 
2200022658beSLiam Girdwood /**
2201db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2202db2a4165SFrank Mandarino  * @kcontrol: mixer control
2203db2a4165SFrank Mandarino  * @uinfo: control element information
2204db2a4165SFrank Mandarino  *
2205db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2206db2a4165SFrank Mandarino  * mixer control.
2207db2a4165SFrank Mandarino  *
2208db2a4165SFrank Mandarino  * Returns 0 for success.
2209db2a4165SFrank Mandarino  */
2210db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2211db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2212db2a4165SFrank Mandarino {
2213db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2214db2a4165SFrank Mandarino 
2215db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2216db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2217f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2218db2a4165SFrank Mandarino 
2219f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2220f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2221db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2222db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2223db2a4165SFrank Mandarino 	return 0;
2224db2a4165SFrank Mandarino }
2225db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2226db2a4165SFrank Mandarino 
2227db2a4165SFrank Mandarino /**
2228db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2229db2a4165SFrank Mandarino  * @kcontrol: mixer control
2230ac11a2b3SMark Brown  * @ucontrol: control element information
2231db2a4165SFrank Mandarino  *
2232db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2233db2a4165SFrank Mandarino  *
2234db2a4165SFrank Mandarino  * Returns 0 for success.
2235db2a4165SFrank Mandarino  */
2236db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2237db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2238db2a4165SFrank Mandarino {
2239db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2240db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
224146f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2242db2a4165SFrank Mandarino 
2243f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2244db2a4165SFrank Mandarino 		;
2245db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
22463ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
22473ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2248db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2249db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2250db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2251db2a4165SFrank Mandarino 
2252db2a4165SFrank Mandarino 	return 0;
2253db2a4165SFrank Mandarino }
2254db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2255db2a4165SFrank Mandarino 
2256db2a4165SFrank Mandarino /**
2257db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2258db2a4165SFrank Mandarino  * @kcontrol: mixer control
2259ac11a2b3SMark Brown  * @ucontrol: control element information
2260db2a4165SFrank Mandarino  *
2261db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2262db2a4165SFrank Mandarino  *
2263db2a4165SFrank Mandarino  * Returns 0 for success.
2264db2a4165SFrank Mandarino  */
2265db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2266db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2267db2a4165SFrank Mandarino {
2268db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2269db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
227046f5822fSDaniel Ribeiro 	unsigned int val;
227146f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2272db2a4165SFrank Mandarino 
2273f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2274db2a4165SFrank Mandarino 		;
2275f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2276db2a4165SFrank Mandarino 		return -EINVAL;
2277db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2278db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2279db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2280f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2281db2a4165SFrank Mandarino 			return -EINVAL;
2282db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2283db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2284db2a4165SFrank Mandarino 	}
2285db2a4165SFrank Mandarino 
22866c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2287db2a4165SFrank Mandarino }
2288db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2289db2a4165SFrank Mandarino 
2290db2a4165SFrank Mandarino /**
22912e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
22922e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
22932e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
22942e72f8e3SPeter Ujfalusi  *
22952e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
22962e72f8e3SPeter Ujfalusi  *
22972e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
22982e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
22992e72f8e3SPeter Ujfalusi  *
23002e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23012e72f8e3SPeter Ujfalusi  */
23022e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
23032e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23042e72f8e3SPeter Ujfalusi {
23052e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
230674155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
230746f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
23082e72f8e3SPeter Ujfalusi 
23092e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
23102e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
23112e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
23122e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
23132e72f8e3SPeter Ujfalusi 			break;
23142e72f8e3SPeter Ujfalusi 	}
23152e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
23162e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23172e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
23182e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
23192e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
23202e72f8e3SPeter Ujfalusi 				break;
23212e72f8e3SPeter Ujfalusi 		}
23222e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
23232e72f8e3SPeter Ujfalusi 	}
23242e72f8e3SPeter Ujfalusi 
23252e72f8e3SPeter Ujfalusi 	return 0;
23262e72f8e3SPeter Ujfalusi }
23272e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
23282e72f8e3SPeter Ujfalusi 
23292e72f8e3SPeter Ujfalusi /**
23302e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
23312e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23322e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23332e72f8e3SPeter Ujfalusi  *
23342e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
23352e72f8e3SPeter Ujfalusi  *
23362e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23372e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23382e72f8e3SPeter Ujfalusi  *
23392e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23402e72f8e3SPeter Ujfalusi  */
23412e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
23422e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23432e72f8e3SPeter Ujfalusi {
23442e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
234574155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
234646f5822fSDaniel Ribeiro 	unsigned int val;
234746f5822fSDaniel Ribeiro 	unsigned int mask;
23482e72f8e3SPeter Ujfalusi 
23492e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
23502e72f8e3SPeter Ujfalusi 		return -EINVAL;
23512e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
23522e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
23532e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23542e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
23552e72f8e3SPeter Ujfalusi 			return -EINVAL;
23562e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
23572e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
23582e72f8e3SPeter Ujfalusi 	}
23592e72f8e3SPeter Ujfalusi 
23606c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
23612e72f8e3SPeter Ujfalusi }
23622e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
23632e72f8e3SPeter Ujfalusi 
23642e72f8e3SPeter Ujfalusi /**
2365db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2366db2a4165SFrank Mandarino  * @kcontrol: mixer control
2367db2a4165SFrank Mandarino  * @uinfo: control element information
2368db2a4165SFrank Mandarino  *
2369db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2370db2a4165SFrank Mandarino  * single mixer.
2371db2a4165SFrank Mandarino  *
2372db2a4165SFrank Mandarino  * Returns 0 for success.
2373db2a4165SFrank Mandarino  */
2374db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2375db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2376db2a4165SFrank Mandarino {
2377db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2378db2a4165SFrank Mandarino 
2379db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2380db2a4165SFrank Mandarino 	uinfo->count = 1;
2381f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2382db2a4165SFrank Mandarino 
2383f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2384f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2385db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2386db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2387db2a4165SFrank Mandarino 	return 0;
2388db2a4165SFrank Mandarino }
2389db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2390db2a4165SFrank Mandarino 
2391db2a4165SFrank Mandarino /**
2392db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2393db2a4165SFrank Mandarino  * @kcontrol: mixer control
2394db2a4165SFrank Mandarino  * @uinfo: control element information
2395db2a4165SFrank Mandarino  *
2396db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2397db2a4165SFrank Mandarino  *
2398db2a4165SFrank Mandarino  * Returns 0 for success.
2399db2a4165SFrank Mandarino  */
2400db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2401db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2402db2a4165SFrank Mandarino {
2403a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2404db2a4165SFrank Mandarino 
2405fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2406a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2407a7a4ac86SPhilipp Zabel 	else
2408a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2409a7a4ac86SPhilipp Zabel 
2410db2a4165SFrank Mandarino 	uinfo->count = 1;
2411db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2412a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2413db2a4165SFrank Mandarino 	return 0;
2414db2a4165SFrank Mandarino }
2415db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2416db2a4165SFrank Mandarino 
2417db2a4165SFrank Mandarino /**
2418db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2419db2a4165SFrank Mandarino  * @kcontrol: mixer control
2420db2a4165SFrank Mandarino  * @uinfo: control element information
2421db2a4165SFrank Mandarino  *
2422e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2423e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2424db2a4165SFrank Mandarino  *
2425db2a4165SFrank Mandarino  * Returns 0 for success.
2426db2a4165SFrank Mandarino  */
2427db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2428db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2429db2a4165SFrank Mandarino {
24304eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24314eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2432d11bb4a9SPeter Ujfalusi 	int platform_max;
2433db2a4165SFrank Mandarino 
2434d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2435d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2436d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2437d11bb4a9SPeter Ujfalusi 
2438d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2439a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2440a7a4ac86SPhilipp Zabel 	else
2441a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2442a7a4ac86SPhilipp Zabel 
2443e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2444db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2445d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2446db2a4165SFrank Mandarino 	return 0;
2447db2a4165SFrank Mandarino }
2448db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2449db2a4165SFrank Mandarino 
2450db2a4165SFrank Mandarino /**
2451db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2452db2a4165SFrank Mandarino  * @kcontrol: mixer control
2453ac11a2b3SMark Brown  * @ucontrol: control element information
2454db2a4165SFrank Mandarino  *
2455f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2456f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2457db2a4165SFrank Mandarino  *
2458db2a4165SFrank Mandarino  * Returns 0 for success.
2459db2a4165SFrank Mandarino  */
2460db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2461db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2462db2a4165SFrank Mandarino {
24634eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24644eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2465db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2466815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2467f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2468815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2469815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
24704eaa9819SJon Smirl 	int max = mc->max;
2471815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2472815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2473db2a4165SFrank Mandarino 
2474db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2475db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2476f7915d99SPeter Ujfalusi 	if (invert)
2477db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2478a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2479f7915d99SPeter Ujfalusi 
2480f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2481f7915d99SPeter Ujfalusi 		if (reg == reg2)
2482f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2483f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2484f7915d99SPeter Ujfalusi 		else
2485f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2486f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2487f7915d99SPeter Ujfalusi 		if (invert)
2488db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2489a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2490db2a4165SFrank Mandarino 	}
2491db2a4165SFrank Mandarino 
2492db2a4165SFrank Mandarino 	return 0;
2493db2a4165SFrank Mandarino }
2494db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2495db2a4165SFrank Mandarino 
2496db2a4165SFrank Mandarino /**
2497db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2498db2a4165SFrank Mandarino  * @kcontrol: mixer control
2499ac11a2b3SMark Brown  * @ucontrol: control element information
2500db2a4165SFrank Mandarino  *
2501974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2502974815baSPeter Ujfalusi  * control that spans 2 registers.
2503db2a4165SFrank Mandarino  *
2504db2a4165SFrank Mandarino  * Returns 0 for success.
2505db2a4165SFrank Mandarino  */
2506db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2507db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2508db2a4165SFrank Mandarino {
25094eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25104eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2511db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2512815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2513974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2514815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2515815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25164eaa9819SJon Smirl 	int max = mc->max;
2517815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2518815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2519974815baSPeter Ujfalusi 	int err;
2520974815baSPeter Ujfalusi 	bool type_2r = 0;
2521974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2522974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2523db2a4165SFrank Mandarino 
2524db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2525db2a4165SFrank Mandarino 	if (invert)
2526a7a4ac86SPhilipp Zabel 		val = max - val;
2527db2a4165SFrank Mandarino 	val_mask = mask << shift;
2528db2a4165SFrank Mandarino 	val = val << shift;
2529974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2530db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2531db2a4165SFrank Mandarino 		if (invert)
2532a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2533974815baSPeter Ujfalusi 		if (reg == reg2) {
2534db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2535db2a4165SFrank Mandarino 			val |= val2 << rshift;
2536974815baSPeter Ujfalusi 		} else {
2537db2a4165SFrank Mandarino 			val2 = val2 << shift;
2538974815baSPeter Ujfalusi 			type_2r = 1;
2539974815baSPeter Ujfalusi 		}
2540974815baSPeter Ujfalusi 	}
25416c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
25423ff3f64bSMark Brown 	if (err < 0)
2543db2a4165SFrank Mandarino 		return err;
2544db2a4165SFrank Mandarino 
2545974815baSPeter Ujfalusi 	if (type_2r)
25466c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2547974815baSPeter Ujfalusi 
2548db2a4165SFrank Mandarino 	return err;
2549db2a4165SFrank Mandarino }
2550974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2551db2a4165SFrank Mandarino 
2552e13ac2e9SMark Brown /**
25531d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
25541d99f243SBrian Austin  * @kcontrol: mixer control
25551d99f243SBrian Austin  * @ucontrol: control element information
25561d99f243SBrian Austin  *
25571d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
25581d99f243SBrian Austin  * control that spans 2 registers.
25591d99f243SBrian Austin  *
25601d99f243SBrian Austin  * Returns 0 for success.
25611d99f243SBrian Austin  */
25621d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
25631d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
25641d99f243SBrian Austin {
25651d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
25661d99f243SBrian Austin 	struct soc_mixer_control *mc =
25671d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
25681d99f243SBrian Austin 
25691d99f243SBrian Austin 	unsigned int reg = mc->reg;
25701d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
25711d99f243SBrian Austin 	unsigned int shift = mc->shift;
25721d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
25731d99f243SBrian Austin 	int max = mc->max;
25741d99f243SBrian Austin 	int min = mc->min;
25751d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
25761d99f243SBrian Austin 
25771d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
25781d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
25791d99f243SBrian Austin 
25801d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
25811d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
25821d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
25831d99f243SBrian Austin 
25841d99f243SBrian Austin 	return 0;
25851d99f243SBrian Austin }
25861d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
25871d99f243SBrian Austin 
25881d99f243SBrian Austin /**
25891d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
25901d99f243SBrian Austin  * @kcontrol: mixer control
25911d99f243SBrian Austin  * @uinfo: control element information
25921d99f243SBrian Austin  *
25931d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
25941d99f243SBrian Austin  *
25951d99f243SBrian Austin  * Returns 0 for success.
25961d99f243SBrian Austin  */
25971d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
25981d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
25991d99f243SBrian Austin {
26001d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
26011d99f243SBrian Austin 	struct soc_mixer_control *mc =
26021d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
26031d99f243SBrian Austin 
26041d99f243SBrian Austin 	unsigned int reg = mc->reg;
26051d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
26061d99f243SBrian Austin 	unsigned int shift = mc->shift;
26071d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
26081d99f243SBrian Austin 	int max = mc->max;
26091d99f243SBrian Austin 	int min = mc->min;
26101d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
261127f1d759SBrian Austin 	int err = 0;
26121d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
26131d99f243SBrian Austin 
26141d99f243SBrian Austin 	val_mask = mask << shift;
26151d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
26161d99f243SBrian Austin 	val = val << shift;
26171d99f243SBrian Austin 
26181d99f243SBrian Austin 	if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
26191d99f243SBrian Austin 			return err;
26201d99f243SBrian Austin 
26211d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
26221d99f243SBrian Austin 		val_mask = mask << rshift;
26231d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
26241d99f243SBrian Austin 		val2 = val2 << rshift;
26251d99f243SBrian Austin 
26261d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
26271d99f243SBrian Austin 			return err;
26281d99f243SBrian Austin 	}
26291d99f243SBrian Austin 	return 0;
26301d99f243SBrian Austin }
26311d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
26321d99f243SBrian Austin 
26331d99f243SBrian Austin /**
2634e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2635e13ac2e9SMark Brown  * @kcontrol: mixer control
2636e13ac2e9SMark Brown  * @uinfo: control element information
2637e13ac2e9SMark Brown  *
2638e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2639e13ac2e9SMark Brown  *
2640e13ac2e9SMark Brown  * Returns 0 for success.
2641e13ac2e9SMark Brown  */
2642e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2643e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2644e13ac2e9SMark Brown {
26454eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26464eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2647d11bb4a9SPeter Ujfalusi 	int platform_max;
26484eaa9819SJon Smirl 	int min = mc->min;
2649e13ac2e9SMark Brown 
2650d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2651d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2652d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2653d11bb4a9SPeter Ujfalusi 
2654e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2655e13ac2e9SMark Brown 	uinfo->count = 2;
2656e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2657d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2658e13ac2e9SMark Brown 	return 0;
2659e13ac2e9SMark Brown }
2660e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2661e13ac2e9SMark Brown 
2662e13ac2e9SMark Brown /**
2663e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2664e13ac2e9SMark Brown  * @kcontrol: mixer control
2665ac11a2b3SMark Brown  * @ucontrol: control element information
2666e13ac2e9SMark Brown  *
2667e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2668e13ac2e9SMark Brown  *
2669e13ac2e9SMark Brown  * Returns 0 for success.
2670e13ac2e9SMark Brown  */
2671e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2672e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2673e13ac2e9SMark Brown {
26744eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26754eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2676e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2677815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
26784eaa9819SJon Smirl 	int min = mc->min;
2679e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2680e13ac2e9SMark Brown 
2681e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2682e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2683e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2684e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2685e13ac2e9SMark Brown 	return 0;
2686e13ac2e9SMark Brown }
2687e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2688e13ac2e9SMark Brown 
2689e13ac2e9SMark Brown /**
2690e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2691e13ac2e9SMark Brown  * @kcontrol: mixer control
2692ac11a2b3SMark Brown  * @ucontrol: control element information
2693e13ac2e9SMark Brown  *
2694e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2695e13ac2e9SMark Brown  *
2696e13ac2e9SMark Brown  * Returns 0 for success.
2697e13ac2e9SMark Brown  */
2698e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2699e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2700e13ac2e9SMark Brown {
27014eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27024eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2703e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2704815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27054eaa9819SJon Smirl 	int min = mc->min;
270646f5822fSDaniel Ribeiro 	unsigned int val;
2707e13ac2e9SMark Brown 
2708e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2709e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2710e13ac2e9SMark Brown 
27116c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2712e13ac2e9SMark Brown }
2713e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2714e13ac2e9SMark Brown 
27158c6529dbSLiam Girdwood /**
2716637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2717637d3847SPeter Ujfalusi  *
2718637d3847SPeter Ujfalusi  * @codec: where to look for the control
2719637d3847SPeter Ujfalusi  * @name: Name of the control
2720637d3847SPeter Ujfalusi  * @max: new maximum limit
2721637d3847SPeter Ujfalusi  *
2722637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2723637d3847SPeter Ujfalusi  */
2724637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2725637d3847SPeter Ujfalusi 	const char *name, int max)
2726637d3847SPeter Ujfalusi {
2727f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2728637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2729637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2730637d3847SPeter Ujfalusi 	int found = 0;
2731637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2732637d3847SPeter Ujfalusi 
2733637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2734637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2735637d3847SPeter Ujfalusi 		return -EINVAL;
2736637d3847SPeter Ujfalusi 
2737637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2738637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2739637d3847SPeter Ujfalusi 			found = 1;
2740637d3847SPeter Ujfalusi 			break;
2741637d3847SPeter Ujfalusi 		}
2742637d3847SPeter Ujfalusi 	}
2743637d3847SPeter Ujfalusi 	if (found) {
2744637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2745637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2746d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2747637d3847SPeter Ujfalusi 			ret = 0;
2748637d3847SPeter Ujfalusi 		}
2749637d3847SPeter Ujfalusi 	}
2750637d3847SPeter Ujfalusi 	return ret;
2751637d3847SPeter Ujfalusi }
2752637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2753637d3847SPeter Ujfalusi 
275471d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
275571d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
275671d08516SMark Brown {
275771d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
275871d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
275971d08516SMark Brown 
276071d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
276171d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
276271d08516SMark Brown 
276371d08516SMark Brown 	return 0;
276471d08516SMark Brown }
276571d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
276671d08516SMark Brown 
276771d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
276871d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
276971d08516SMark Brown {
277071d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
277171d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
277271d08516SMark Brown 	int ret;
277371d08516SMark Brown 
277471d08516SMark Brown 	if (codec->using_regmap)
277571d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
277671d08516SMark Brown 				      ucontrol->value.bytes.data,
277771d08516SMark Brown 				      params->num_regs * codec->val_bytes);
277871d08516SMark Brown 	else
277971d08516SMark Brown 		ret = -EINVAL;
278071d08516SMark Brown 
2781f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
2782f831b055SMark Brown 	if (ret == 0 && params->mask) {
2783f831b055SMark Brown 		switch (codec->val_bytes) {
2784f831b055SMark Brown 		case 1:
2785f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
2786f831b055SMark Brown 			break;
2787f831b055SMark Brown 		case 2:
2788f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
2789f831b055SMark Brown 				&= ~params->mask;
2790f831b055SMark Brown 			break;
2791f831b055SMark Brown 		case 4:
2792f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
2793f831b055SMark Brown 				&= ~params->mask;
2794f831b055SMark Brown 			break;
2795f831b055SMark Brown 		default:
2796f831b055SMark Brown 			return -EINVAL;
2797f831b055SMark Brown 		}
2798f831b055SMark Brown 	}
2799f831b055SMark Brown 
280071d08516SMark Brown 	return ret;
280171d08516SMark Brown }
280271d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
280371d08516SMark Brown 
280471d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
280571d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
280671d08516SMark Brown {
280771d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
280871d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2809f831b055SMark Brown 	int ret, len;
2810f831b055SMark Brown 	unsigned int val;
2811f831b055SMark Brown 	void *data;
281271d08516SMark Brown 
2813f831b055SMark Brown 	if (!codec->using_regmap)
2814f831b055SMark Brown 		return -EINVAL;
2815f831b055SMark Brown 
2816f831b055SMark Brown 	data = ucontrol->value.bytes.data;
2817f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
2818f831b055SMark Brown 
2819f831b055SMark Brown 	/*
2820f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
2821f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
2822f831b055SMark Brown 	 * copy.
2823f831b055SMark Brown 	 */
2824f831b055SMark Brown 	if (params->mask) {
2825f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
2826f831b055SMark Brown 		if (ret != 0)
2827f831b055SMark Brown 			return ret;
2828f831b055SMark Brown 
2829f831b055SMark Brown 		val &= params->mask;
2830f831b055SMark Brown 
2831f831b055SMark Brown 		data = kmemdup(data, len, GFP_KERNEL);
2832f831b055SMark Brown 		if (!data)
2833f831b055SMark Brown 			return -ENOMEM;
2834f831b055SMark Brown 
2835f831b055SMark Brown 		switch (codec->val_bytes) {
2836f831b055SMark Brown 		case 1:
2837f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
2838f831b055SMark Brown 			((u8 *)data)[0] |= val;
2839f831b055SMark Brown 			break;
2840f831b055SMark Brown 		case 2:
2841f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2842f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
2843f831b055SMark Brown 			break;
2844f831b055SMark Brown 		case 4:
2845f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2846f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
2847f831b055SMark Brown 			break;
2848f831b055SMark Brown 		default:
2849f831b055SMark Brown 			return -EINVAL;
2850f831b055SMark Brown 		}
2851f831b055SMark Brown 	}
2852f831b055SMark Brown 
285371d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
2854f831b055SMark Brown 			       data, len);
2855f831b055SMark Brown 
2856f831b055SMark Brown 	if (params->mask)
2857f831b055SMark Brown 		kfree(data);
285871d08516SMark Brown 
285971d08516SMark Brown 	return ret;
286071d08516SMark Brown }
286171d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
286271d08516SMark Brown 
2863b6f4bb38Sapatard@mandriva.com /**
28648c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
28658c6529dbSLiam Girdwood  * @dai: DAI
28668c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
28678c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
28688c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
28698c6529dbSLiam Girdwood  *
28708c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
28718c6529dbSLiam Girdwood  */
28728c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
28738c6529dbSLiam Girdwood 	unsigned int freq, int dir)
28748c6529dbSLiam Girdwood {
2875f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2876f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2877ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2878da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2879ec4ee52aSMark Brown 						      freq, dir);
28808c6529dbSLiam Girdwood 	else
28818c6529dbSLiam Girdwood 		return -EINVAL;
28828c6529dbSLiam Girdwood }
28838c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
28848c6529dbSLiam Girdwood 
28858c6529dbSLiam Girdwood /**
2886ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2887ec4ee52aSMark Brown  * @codec: CODEC
2888ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2889da1c6ea6SMark Brown  * @source: Source for the clock
2890ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2891ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2892ec4ee52aSMark Brown  *
2893ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2894ec4ee52aSMark Brown  */
2895ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2896da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2897ec4ee52aSMark Brown {
2898ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2899da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2900da1c6ea6SMark Brown 						 freq, dir);
2901ec4ee52aSMark Brown 	else
2902ec4ee52aSMark Brown 		return -EINVAL;
2903ec4ee52aSMark Brown }
2904ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2905ec4ee52aSMark Brown 
2906ec4ee52aSMark Brown /**
29078c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
29088c6529dbSLiam Girdwood  * @dai: DAI
2909ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
29108c6529dbSLiam Girdwood  * @div: new clock divisor.
29118c6529dbSLiam Girdwood  *
29128c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
29138c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
29148c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
29158c6529dbSLiam Girdwood  */
29168c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
29178c6529dbSLiam Girdwood 	int div_id, int div)
29188c6529dbSLiam Girdwood {
2919f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2920f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
29218c6529dbSLiam Girdwood 	else
29228c6529dbSLiam Girdwood 		return -EINVAL;
29238c6529dbSLiam Girdwood }
29248c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
29258c6529dbSLiam Girdwood 
29268c6529dbSLiam Girdwood /**
29278c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
29288c6529dbSLiam Girdwood  * @dai: DAI
29298c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
293085488037SMark Brown  * @source: DAI specific source for the PLL
29318c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
29328c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
29338c6529dbSLiam Girdwood  *
29348c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
29358c6529dbSLiam Girdwood  */
293685488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
293785488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
29388c6529dbSLiam Girdwood {
2939f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2940f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
294185488037SMark Brown 					 freq_in, freq_out);
2942ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2943ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2944ec4ee52aSMark Brown 						   freq_in, freq_out);
29458c6529dbSLiam Girdwood 	else
29468c6529dbSLiam Girdwood 		return -EINVAL;
29478c6529dbSLiam Girdwood }
29488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
29498c6529dbSLiam Girdwood 
2950ec4ee52aSMark Brown /*
2951ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2952ec4ee52aSMark Brown  * @codec: CODEC
2953ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2954ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2955ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2956ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2957ec4ee52aSMark Brown  *
2958ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2959ec4ee52aSMark Brown  */
2960ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2961ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2962ec4ee52aSMark Brown {
2963ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2964ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2965ec4ee52aSMark Brown 					      freq_in, freq_out);
2966ec4ee52aSMark Brown 	else
2967ec4ee52aSMark Brown 		return -EINVAL;
2968ec4ee52aSMark Brown }
2969ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2970ec4ee52aSMark Brown 
29718c6529dbSLiam Girdwood /**
29728c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
29738c6529dbSLiam Girdwood  * @dai: DAI
29748c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
29758c6529dbSLiam Girdwood  *
29768c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
29778c6529dbSLiam Girdwood  */
29788c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
29798c6529dbSLiam Girdwood {
29805e4ba569SShawn Guo 	if (dai->driver == NULL)
29818c6529dbSLiam Girdwood 		return -EINVAL;
29825e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
29835e4ba569SShawn Guo 		return -ENOTSUPP;
29845e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
29858c6529dbSLiam Girdwood }
29868c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
29878c6529dbSLiam Girdwood 
29888c6529dbSLiam Girdwood /**
29898c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
29908c6529dbSLiam Girdwood  * @dai: DAI
2991a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2992a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
29938c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2994a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
29958c6529dbSLiam Girdwood  *
29968c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
29978c6529dbSLiam Girdwood  * specific.
29988c6529dbSLiam Girdwood  */
29998c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3000a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
30018c6529dbSLiam Girdwood {
3002f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3003f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3004a5479e38SDaniel Ribeiro 				slots, slot_width);
30058c6529dbSLiam Girdwood 	else
30068c6529dbSLiam Girdwood 		return -EINVAL;
30078c6529dbSLiam Girdwood }
30088c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
30098c6529dbSLiam Girdwood 
30108c6529dbSLiam Girdwood /**
3011472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3012472df3cbSBarry Song  * @dai: DAI
3013472df3cbSBarry Song  * @tx_num: how many TX channels
3014472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3015472df3cbSBarry Song  *           0~num-1 uses
3016472df3cbSBarry Song  * @rx_num: how many RX channels
3017472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3018472df3cbSBarry Song  *           0~num-1 uses
3019472df3cbSBarry Song  *
3020472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3021472df3cbSBarry Song  */
3022472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3023472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3024472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3025472df3cbSBarry Song {
3026f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3027f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3028472df3cbSBarry Song 			rx_num, rx_slot);
3029472df3cbSBarry Song 	else
3030472df3cbSBarry Song 		return -EINVAL;
3031472df3cbSBarry Song }
3032472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3033472df3cbSBarry Song 
3034472df3cbSBarry Song /**
30358c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
30368c6529dbSLiam Girdwood  * @dai: DAI
30378c6529dbSLiam Girdwood  * @tristate: tristate enable
30388c6529dbSLiam Girdwood  *
30398c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
30408c6529dbSLiam Girdwood  */
30418c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
30428c6529dbSLiam Girdwood {
3043f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3044f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
30458c6529dbSLiam Girdwood 	else
30468c6529dbSLiam Girdwood 		return -EINVAL;
30478c6529dbSLiam Girdwood }
30488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
30498c6529dbSLiam Girdwood 
30508c6529dbSLiam Girdwood /**
30518c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
30528c6529dbSLiam Girdwood  * @dai: DAI
30538c6529dbSLiam Girdwood  * @mute: mute enable
30548c6529dbSLiam Girdwood  *
30558c6529dbSLiam Girdwood  * Mutes the DAI DAC.
30568c6529dbSLiam Girdwood  */
30578c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
30588c6529dbSLiam Girdwood {
3059f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3060f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
30618c6529dbSLiam Girdwood 	else
306204570c62SMark Brown 		return -ENOTSUPP;
30638c6529dbSLiam Girdwood }
30648c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
30658c6529dbSLiam Girdwood 
3066c5af3a2eSMark Brown /**
3067c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3068c5af3a2eSMark Brown  *
3069ac11a2b3SMark Brown  * @card: Card to register
3070c5af3a2eSMark Brown  *
3071c5af3a2eSMark Brown  */
307270a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3073c5af3a2eSMark Brown {
3074b19e6e7bSMark Brown 	int i, ret;
3075f0fba2adSLiam Girdwood 
3076c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3077c5af3a2eSMark Brown 		return -EINVAL;
3078c5af3a2eSMark Brown 
30795a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
30805a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
30815a504963SStephen Warren 
30825a504963SStephen Warren 		/*
30835a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
30845a504963SStephen Warren 		 * not both or neither.
30855a504963SStephen Warren 		 */
30865a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
30875a504963SStephen Warren 			dev_err(card->dev,
30883b09bb82SLiam Girdwood 				"Neither/both codec name/of_node are set for %s\n",
30893b09bb82SLiam Girdwood 				link->name);
30905a504963SStephen Warren 			return -EINVAL;
30915a504963SStephen Warren 		}
30925a504963SStephen Warren 
30935a504963SStephen Warren 		/*
30945a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
30955a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
30965a504963SStephen Warren 		 */
30975a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
30985a504963SStephen Warren 			dev_err(card->dev,
30993b09bb82SLiam Girdwood 				"Both platform name/of_node are set for %s\n", link->name);
31005a504963SStephen Warren 			return -EINVAL;
31015a504963SStephen Warren 		}
31025a504963SStephen Warren 
31035a504963SStephen Warren 		/*
31045a504963SStephen Warren 		 * CPU DAI must be specified by 1 of name or OF node,
31055a504963SStephen Warren 		 * not both or neither.
31065a504963SStephen Warren 		 */
31075a504963SStephen Warren 		if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
31085a504963SStephen Warren 			dev_err(card->dev,
31093b09bb82SLiam Girdwood 				"Neither/both cpu_dai name/of_node are set for %s\n",
31103b09bb82SLiam Girdwood 				link->name);
31115a504963SStephen Warren 			return -EINVAL;
31125a504963SStephen Warren 		}
31135a504963SStephen Warren 	}
31145a504963SStephen Warren 
3115ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3116ed77cc12SMark Brown 
3117111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3118111c6419SStephen Warren 
3119150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3120150dd2f8SVinod Koul 
3121181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3122181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
31232eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3124f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3125f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3126f0fba2adSLiam Girdwood 		return -ENOMEM;
31272eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3128f0fba2adSLiam Girdwood 
3129f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3130f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3131f0fba2adSLiam Girdwood 
3132c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3133db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3134c5af3a2eSMark Brown 	card->instantiated = 0;
3135f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3136a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3137c5af3a2eSMark Brown 
3138b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3139b19e6e7bSMark Brown 	if (ret != 0)
3140b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3141c5af3a2eSMark Brown 
3142b19e6e7bSMark Brown 	return ret;
3143c5af3a2eSMark Brown }
314470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3145c5af3a2eSMark Brown 
3146c5af3a2eSMark Brown /**
3147c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3148c5af3a2eSMark Brown  *
3149ac11a2b3SMark Brown  * @card: Card to unregister
3150c5af3a2eSMark Brown  *
3151c5af3a2eSMark Brown  */
315270a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3153c5af3a2eSMark Brown {
3154b0e26485SVinod Koul 	if (card->instantiated)
3155b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3156c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3157c5af3a2eSMark Brown 
3158c5af3a2eSMark Brown 	return 0;
3159c5af3a2eSMark Brown }
316070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3161c5af3a2eSMark Brown 
3162f0fba2adSLiam Girdwood /*
3163f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3164f0fba2adSLiam Girdwood  * and sanitizing names.
3165f0fba2adSLiam Girdwood  */
31660b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3167f0fba2adSLiam Girdwood {
3168f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3169f0fba2adSLiam Girdwood 	int id1, id2;
3170f0fba2adSLiam Girdwood 
3171f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3172f0fba2adSLiam Girdwood 		return NULL;
3173f0fba2adSLiam Girdwood 
317458818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3175f0fba2adSLiam Girdwood 
3176f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3177f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3178f0fba2adSLiam Girdwood 	if (found) {
3179f0fba2adSLiam Girdwood 		/* get ID */
3180f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3181f0fba2adSLiam Girdwood 
3182f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3183f0fba2adSLiam Girdwood 			if (*id == -1)
3184f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3185f0fba2adSLiam Girdwood 		}
3186f0fba2adSLiam Girdwood 
3187f0fba2adSLiam Girdwood 	} else {
3188f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3189f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3190f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3191f0fba2adSLiam Girdwood 
3192f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
319305899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3194f0fba2adSLiam Girdwood 
3195f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3196f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
319758818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3198f0fba2adSLiam Girdwood 		} else
3199f0fba2adSLiam Girdwood 			*id = 0;
3200f0fba2adSLiam Girdwood 	}
3201f0fba2adSLiam Girdwood 
3202f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3203f0fba2adSLiam Girdwood }
3204f0fba2adSLiam Girdwood 
3205f0fba2adSLiam Girdwood /*
3206f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3207f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3208f0fba2adSLiam Girdwood  */
3209f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3210f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3211f0fba2adSLiam Girdwood {
3212f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
32130837fc62SFabio Estevam 		pr_err("asoc: error - multiple DAI %s registered with no name\n",
3214f0fba2adSLiam Girdwood 				dev_name(dev));
3215f0fba2adSLiam Girdwood 		return NULL;
3216f0fba2adSLiam Girdwood 	}
3217f0fba2adSLiam Girdwood 
3218f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3219f0fba2adSLiam Girdwood }
3220f0fba2adSLiam Girdwood 
32219115171aSMark Brown /**
32229115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
32239115171aSMark Brown  *
3224ac11a2b3SMark Brown  * @dai: DAI to register
32259115171aSMark Brown  */
3226f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3227f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
32289115171aSMark Brown {
3229*054880feSMark Brown 	struct snd_soc_codec *codec;
3230f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
32319115171aSMark Brown 
3232f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
32339115171aSMark Brown 
3234f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3235f0fba2adSLiam Girdwood 	if (dai == NULL)
3236f0fba2adSLiam Girdwood 		return -ENOMEM;
32376335d055SEric Miao 
3238f0fba2adSLiam Girdwood 	/* create DAI component name */
3239f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3240f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3241f0fba2adSLiam Girdwood 		kfree(dai);
3242f0fba2adSLiam Girdwood 		return -ENOMEM;
3243f0fba2adSLiam Girdwood 	}
3244f0fba2adSLiam Girdwood 
3245f0fba2adSLiam Girdwood 	dai->dev = dev;
3246f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3247be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3248f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3249f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
32509115171aSMark Brown 
32519115171aSMark Brown 	mutex_lock(&client_mutex);
3252*054880feSMark Brown 
3253*054880feSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3254*054880feSMark Brown 		if (codec->dev == dev) {
3255*054880feSMark Brown 			dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3256*054880feSMark Brown 				dai->name, codec->name);
3257*054880feSMark Brown 			dai->codec = codec;
3258*054880feSMark Brown 			break;
3259*054880feSMark Brown 		}
3260*054880feSMark Brown 	}
3261*054880feSMark Brown 
32629115171aSMark Brown 	list_add(&dai->list, &dai_list);
3263*054880feSMark Brown 
32649115171aSMark Brown 	mutex_unlock(&client_mutex);
32659115171aSMark Brown 
32669115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
32679115171aSMark Brown 
32689115171aSMark Brown 	return 0;
32699115171aSMark Brown }
32709115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
32719115171aSMark Brown 
32729115171aSMark Brown /**
32739115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
32749115171aSMark Brown  *
3275ac11a2b3SMark Brown  * @dai: DAI to unregister
32769115171aSMark Brown  */
3277f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
32789115171aSMark Brown {
3279f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3280f0fba2adSLiam Girdwood 
3281f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3282f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3283f0fba2adSLiam Girdwood 			goto found;
3284f0fba2adSLiam Girdwood 	}
3285f0fba2adSLiam Girdwood 	return;
3286f0fba2adSLiam Girdwood 
3287f0fba2adSLiam Girdwood found:
32889115171aSMark Brown 	mutex_lock(&client_mutex);
32899115171aSMark Brown 	list_del(&dai->list);
32909115171aSMark Brown 	mutex_unlock(&client_mutex);
32919115171aSMark Brown 
32929115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3293f0fba2adSLiam Girdwood 	kfree(dai->name);
3294f0fba2adSLiam Girdwood 	kfree(dai);
32959115171aSMark Brown }
32969115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
32979115171aSMark Brown 
32989115171aSMark Brown /**
32999115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
33009115171aSMark Brown  *
3301ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3302ac11a2b3SMark Brown  * @count: Number of DAIs
33039115171aSMark Brown  */
3304f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3305f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
33069115171aSMark Brown {
3307*054880feSMark Brown 	struct snd_soc_codec *codec;
3308f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3309f0fba2adSLiam Girdwood 	int i, ret = 0;
3310f0fba2adSLiam Girdwood 
3311720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
33129115171aSMark Brown 
33139115171aSMark Brown 	for (i = 0; i < count; i++) {
3314f0fba2adSLiam Girdwood 
3315f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3316c46e0079SAxel Lin 		if (dai == NULL) {
3317c46e0079SAxel Lin 			ret = -ENOMEM;
3318c46e0079SAxel Lin 			goto err;
3319c46e0079SAxel Lin 		}
3320f0fba2adSLiam Girdwood 
3321f0fba2adSLiam Girdwood 		/* create DAI component name */
3322f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3323f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3324f0fba2adSLiam Girdwood 			kfree(dai);
3325f0fba2adSLiam Girdwood 			ret = -EINVAL;
33269115171aSMark Brown 			goto err;
33279115171aSMark Brown 		}
33289115171aSMark Brown 
3329f0fba2adSLiam Girdwood 		dai->dev = dev;
3330f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
33310f9141c9SMark Brown 		if (dai->driver->id)
33320f9141c9SMark Brown 			dai->id = dai->driver->id;
33330f9141c9SMark Brown 		else
33340f9141c9SMark Brown 			dai->id = i;
3335be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3336f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3337f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3338f0fba2adSLiam Girdwood 
3339f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3340*054880feSMark Brown 
3341*054880feSMark Brown 		list_for_each_entry(codec, &codec_list, list) {
3342*054880feSMark Brown 			if (codec->dev == dev) {
3343*054880feSMark Brown 				dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3344*054880feSMark Brown 					dai->name, codec->name);
3345*054880feSMark Brown 				dai->codec = codec;
3346*054880feSMark Brown 				break;
3347*054880feSMark Brown 			}
3348*054880feSMark Brown 		}
3349*054880feSMark Brown 
3350f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3351*054880feSMark Brown 
3352f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3353f0fba2adSLiam Girdwood 
3354f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3355f0fba2adSLiam Girdwood 	}
3356f0fba2adSLiam Girdwood 
33579115171aSMark Brown 	return 0;
33589115171aSMark Brown 
33599115171aSMark Brown err:
33609115171aSMark Brown 	for (i--; i >= 0; i--)
3361f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33629115171aSMark Brown 
33639115171aSMark Brown 	return ret;
33649115171aSMark Brown }
33659115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
33669115171aSMark Brown 
33679115171aSMark Brown /**
33689115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
33699115171aSMark Brown  *
3370ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3371ac11a2b3SMark Brown  * @count: Number of DAIs
33729115171aSMark Brown  */
3373f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
33749115171aSMark Brown {
33759115171aSMark Brown 	int i;
33769115171aSMark Brown 
33779115171aSMark Brown 	for (i = 0; i < count; i++)
3378f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33799115171aSMark Brown }
33809115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
33819115171aSMark Brown 
338212a48a8cSMark Brown /**
338312a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
338412a48a8cSMark Brown  *
3385ac11a2b3SMark Brown  * @platform: platform to register
338612a48a8cSMark Brown  */
3387f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3388f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
338912a48a8cSMark Brown {
3390f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
339112a48a8cSMark Brown 
3392f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3393f0fba2adSLiam Girdwood 
3394f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3395f0fba2adSLiam Girdwood 	if (platform == NULL)
3396f0fba2adSLiam Girdwood 		return -ENOMEM;
3397f0fba2adSLiam Girdwood 
3398f0fba2adSLiam Girdwood 	/* create platform component name */
3399f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3400f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3401f0fba2adSLiam Girdwood 		kfree(platform);
3402f0fba2adSLiam Girdwood 		return -ENOMEM;
3403f0fba2adSLiam Girdwood 	}
3404f0fba2adSLiam Girdwood 
3405f0fba2adSLiam Girdwood 	platform->dev = dev;
3406f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3407b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3408b7950641SLiam Girdwood 	platform->dapm.platform = platform;
340964a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3410cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
341112a48a8cSMark Brown 
341212a48a8cSMark Brown 	mutex_lock(&client_mutex);
341312a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
341412a48a8cSMark Brown 	mutex_unlock(&client_mutex);
341512a48a8cSMark Brown 
341612a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
341712a48a8cSMark Brown 
341812a48a8cSMark Brown 	return 0;
341912a48a8cSMark Brown }
342012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
342112a48a8cSMark Brown 
342212a48a8cSMark Brown /**
342312a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
342412a48a8cSMark Brown  *
3425ac11a2b3SMark Brown  * @platform: platform to unregister
342612a48a8cSMark Brown  */
3427f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
342812a48a8cSMark Brown {
3429f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3430f0fba2adSLiam Girdwood 
3431f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3432f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3433f0fba2adSLiam Girdwood 			goto found;
3434f0fba2adSLiam Girdwood 	}
3435f0fba2adSLiam Girdwood 	return;
3436f0fba2adSLiam Girdwood 
3437f0fba2adSLiam Girdwood found:
343812a48a8cSMark Brown 	mutex_lock(&client_mutex);
343912a48a8cSMark Brown 	list_del(&platform->list);
344012a48a8cSMark Brown 	mutex_unlock(&client_mutex);
344112a48a8cSMark Brown 
344212a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3443f0fba2adSLiam Girdwood 	kfree(platform->name);
3444f0fba2adSLiam Girdwood 	kfree(platform);
344512a48a8cSMark Brown }
344612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
344712a48a8cSMark Brown 
3448151ab22cSMark Brown static u64 codec_format_map[] = {
3449151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3450151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3451151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3452151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3453151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3454151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3455151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3456151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3457151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3458151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3459151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3460151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3461151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3462151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3463151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3464151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3465151ab22cSMark Brown };
3466151ab22cSMark Brown 
3467151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3468151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3469151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3470151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3471151ab22cSMark Brown  */
3472151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3473151ab22cSMark Brown {
3474151ab22cSMark Brown 	int i;
3475151ab22cSMark Brown 
3476151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3477151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3478151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3479151ab22cSMark Brown }
3480151ab22cSMark Brown 
34810d0cf00aSMark Brown /**
34820d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
34830d0cf00aSMark Brown  *
3484ac11a2b3SMark Brown  * @codec: codec to register
34850d0cf00aSMark Brown  */
3486f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3487001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3488001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3489001ae4c0SMark Brown 			   int num_dai)
34900d0cf00aSMark Brown {
34913335ddcaSDimitris Papastamos 	size_t reg_size;
3492f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3493f0fba2adSLiam Girdwood 	int ret, i;
3494151ab22cSMark Brown 
3495f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
34960d0cf00aSMark Brown 
3497f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3498f0fba2adSLiam Girdwood 	if (codec == NULL)
3499f0fba2adSLiam Girdwood 		return -ENOMEM;
35000d0cf00aSMark Brown 
3501f0fba2adSLiam Girdwood 	/* create CODEC component name */
3502f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3503f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3504f0fba2adSLiam Girdwood 		kfree(codec);
3505f0fba2adSLiam Girdwood 		return -ENOMEM;
3506151ab22cSMark Brown 	}
3507151ab22cSMark Brown 
350823bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
350923bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
351023bbce34SDimitris Papastamos 	else
351123bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
351223bbce34SDimitris Papastamos 
3513c3acec26SMark Brown 	codec->write = codec_drv->write;
3514c3acec26SMark Brown 	codec->read = codec_drv->read;
35151500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
35161500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
35178020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
35185124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3519ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3520ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3521ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3522474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
352364a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3524f0fba2adSLiam Girdwood 	codec->dev = dev;
3525f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3526f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3527f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3528f0fba2adSLiam Girdwood 
35297a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
35307a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
35313335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3532aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
35333335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
35343335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
35353335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
35363335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
35373335ddcaSDimitris Papastamos 		 * the cache.
35383335ddcaSDimitris Papastamos 		 */
35392aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
35403335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
35413335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
35423335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
35433335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3544fdf0f54dSDimitris Papastamos 				goto fail;
35457a30a3dbSDimitris Papastamos 			}
35467a30a3dbSDimitris Papastamos 		}
35472aa86323SDimitris Papastamos 	}
35487a30a3dbSDimitris Papastamos 
35491500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
35501500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
35511500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
35521500b7b5SDimitris Papastamos 		if (!codec->readable_register)
35531500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
35548020454cSDimitris Papastamos 		if (!codec->writable_register)
35558020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
35561500b7b5SDimitris Papastamos 	}
35571500b7b5SDimitris Papastamos 
3558f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3559f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3560f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3561f0fba2adSLiam Girdwood 	}
3562f0fba2adSLiam Girdwood 
3563*054880feSMark Brown 	mutex_lock(&client_mutex);
3564*054880feSMark Brown 	list_add(&codec->list, &codec_list);
3565*054880feSMark Brown 	mutex_unlock(&client_mutex);
3566*054880feSMark Brown 
356726b01ccdSMark Brown 	/* register any DAIs */
356826b01ccdSMark Brown 	if (num_dai) {
3569f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3570f0fba2adSLiam Girdwood 		if (ret < 0)
3571*054880feSMark Brown 			dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3572*054880feSMark Brown 				ret);
357326b01ccdSMark Brown 	}
3574f0fba2adSLiam Girdwood 
35750d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
35760d0cf00aSMark Brown 	return 0;
3577f0fba2adSLiam Girdwood 
3578fdf0f54dSDimitris Papastamos fail:
35793335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35803335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3581f0fba2adSLiam Girdwood 	kfree(codec->name);
3582f0fba2adSLiam Girdwood 	kfree(codec);
3583f0fba2adSLiam Girdwood 	return ret;
35840d0cf00aSMark Brown }
35850d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
35860d0cf00aSMark Brown 
35870d0cf00aSMark Brown /**
35880d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
35890d0cf00aSMark Brown  *
3590ac11a2b3SMark Brown  * @codec: codec to unregister
35910d0cf00aSMark Brown  */
3592f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
35930d0cf00aSMark Brown {
3594f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3595f0fba2adSLiam Girdwood 	int i;
3596f0fba2adSLiam Girdwood 
3597f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3598f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3599f0fba2adSLiam Girdwood 			goto found;
3600f0fba2adSLiam Girdwood 	}
3601f0fba2adSLiam Girdwood 	return;
3602f0fba2adSLiam Girdwood 
3603f0fba2adSLiam Girdwood found:
360426b01ccdSMark Brown 	if (codec->num_dai)
3605f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3606f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3607f0fba2adSLiam Girdwood 
36080d0cf00aSMark Brown 	mutex_lock(&client_mutex);
36090d0cf00aSMark Brown 	list_del(&codec->list);
36100d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
36110d0cf00aSMark Brown 
36120d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3613f0fba2adSLiam Girdwood 
36147a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
36153335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
36161aafcd4dSDimitris Papastamos 	kfree(codec->name);
3617f0fba2adSLiam Girdwood 	kfree(codec);
36180d0cf00aSMark Brown }
36190d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
36200d0cf00aSMark Brown 
3621bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3622bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3623bec4fa05SStephen Warren 			       const char *propname)
3624bec4fa05SStephen Warren {
3625bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
3626bec4fa05SStephen Warren 	int ret;
3627bec4fa05SStephen Warren 
3628bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3629bec4fa05SStephen Warren 	/*
3630bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3631bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3632bec4fa05SStephen Warren 	 * snd_soc_register_card.
3633bec4fa05SStephen Warren 	 */
3634bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3635bec4fa05SStephen Warren 		dev_err(card->dev,
3636bec4fa05SStephen Warren 			"Property '%s' could not be read: %d\n",
3637bec4fa05SStephen Warren 			propname, ret);
3638bec4fa05SStephen Warren 		return ret;
3639bec4fa05SStephen Warren 	}
3640bec4fa05SStephen Warren 
3641bec4fa05SStephen Warren 	return 0;
3642bec4fa05SStephen Warren }
3643bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3644bec4fa05SStephen Warren 
3645a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3646a4a54dd5SStephen Warren 				   const char *propname)
3647a4a54dd5SStephen Warren {
3648a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3649a4a54dd5SStephen Warren 	int num_routes;
3650a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3651a4a54dd5SStephen Warren 	int i, ret;
3652a4a54dd5SStephen Warren 
3653a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3654a4a54dd5SStephen Warren 	if (num_routes & 1) {
3655a4a54dd5SStephen Warren 		dev_err(card->dev,
3656a4a54dd5SStephen Warren 			"Property '%s's length is not even\n",
3657a4a54dd5SStephen Warren 			propname);
3658a4a54dd5SStephen Warren 		return -EINVAL;
3659a4a54dd5SStephen Warren 	}
3660a4a54dd5SStephen Warren 	num_routes /= 2;
3661a4a54dd5SStephen Warren 	if (!num_routes) {
3662a4a54dd5SStephen Warren 		dev_err(card->dev,
3663a4a54dd5SStephen Warren 			"Property '%s's length is zero\n",
3664a4a54dd5SStephen Warren 			propname);
3665a4a54dd5SStephen Warren 		return -EINVAL;
3666a4a54dd5SStephen Warren 	}
3667a4a54dd5SStephen Warren 
3668a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3669a4a54dd5SStephen Warren 			      GFP_KERNEL);
3670a4a54dd5SStephen Warren 	if (!routes) {
3671a4a54dd5SStephen Warren 		dev_err(card->dev,
3672a4a54dd5SStephen Warren 			"Could not allocate DAPM route table\n");
3673a4a54dd5SStephen Warren 		return -EINVAL;
3674a4a54dd5SStephen Warren 	}
3675a4a54dd5SStephen Warren 
3676a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3677a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3678a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
3679a4a54dd5SStephen Warren 		if (ret) {
3680a4a54dd5SStephen Warren 			dev_err(card->dev,
3681a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3682a4a54dd5SStephen Warren 				propname, 2 * i, ret);
3683a4a54dd5SStephen Warren 			return -EINVAL;
3684a4a54dd5SStephen Warren 		}
3685a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3686a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
3687a4a54dd5SStephen Warren 		if (ret) {
3688a4a54dd5SStephen Warren 			dev_err(card->dev,
3689a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3690a4a54dd5SStephen Warren 				propname, (2 * i) + 1, ret);
3691a4a54dd5SStephen Warren 			return -EINVAL;
3692a4a54dd5SStephen Warren 		}
3693a4a54dd5SStephen Warren 	}
3694a4a54dd5SStephen Warren 
3695a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
3696a4a54dd5SStephen Warren 	card->dapm_routes = routes;
3697a4a54dd5SStephen Warren 
3698a4a54dd5SStephen Warren 	return 0;
3699a4a54dd5SStephen Warren }
3700a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3701a4a54dd5SStephen Warren 
3702c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3703db2a4165SFrank Mandarino {
3704384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
37058a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
37068a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
37070837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
37088a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3709384c89e2SMark Brown 	}
3710c3c5a19aSMark Brown 
37118a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3712c3c5a19aSMark Brown 				 &codec_list_fops))
3713c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3714c3c5a19aSMark Brown 
37158a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3716f3208780SMark Brown 				 &dai_list_fops))
3717f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
371819c7ac27SMark Brown 
37198a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
372019c7ac27SMark Brown 				 &platform_list_fops))
372119c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3722384c89e2SMark Brown #endif
3723384c89e2SMark Brown 
3724fb257897SMark Brown 	snd_soc_util_init();
3725fb257897SMark Brown 
3726db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3727db2a4165SFrank Mandarino }
37284abe8e16SMark Brown module_init(snd_soc_init);
3729db2a4165SFrank Mandarino 
37307d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3731db2a4165SFrank Mandarino {
3732fb257897SMark Brown 	snd_soc_util_exit();
3733fb257897SMark Brown 
3734384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
37358a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3736384c89e2SMark Brown #endif
3737db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3738db2a4165SFrank Mandarino }
3739db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3740db2a4165SFrank Mandarino 
3741db2a4165SFrank Mandarino /* Module information */
3742d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3743db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3744db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
37458b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3746