xref: /openbmc/linux/sound/soc/soc-core.c (revision dd7b10b30c40dddb9750926d78cfe89c0cd8434d)
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 
10753fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
10763fec6b6dSStephen Warren 
1077956245e9SLiam Girdwood 	if (driver->probe) {
1078956245e9SLiam Girdwood 		ret = driver->probe(platform);
1079956245e9SLiam Girdwood 		if (ret < 0) {
1080956245e9SLiam Girdwood 			dev_err(platform->dev,
1081956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1082956245e9SLiam Girdwood 				platform->name, ret);
1083956245e9SLiam Girdwood 			goto err_probe;
1084956245e9SLiam Girdwood 		}
1085956245e9SLiam Girdwood 	}
1086956245e9SLiam Girdwood 
1087cb2cf612SLiam Girdwood 	if (driver->controls)
1088cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1089cb2cf612SLiam Girdwood 				     driver->num_controls);
1090cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1091cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1092cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1093cb2cf612SLiam Girdwood 
1094956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1095956245e9SLiam Girdwood 	platform->probed = 1;
1096956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1097b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1098956245e9SLiam Girdwood 
1099956245e9SLiam Girdwood 	return 0;
1100956245e9SLiam Girdwood 
1101956245e9SLiam Girdwood err_probe:
110202db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1103956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1104956245e9SLiam Girdwood 
1105956245e9SLiam Girdwood 	return ret;
1106956245e9SLiam Girdwood }
1107956245e9SLiam Girdwood 
110836ae1a96SMark Brown static void rtd_release(struct device *dev)
110936ae1a96SMark Brown {
111036ae1a96SMark Brown 	kfree(dev);
111136ae1a96SMark Brown }
1112f0fba2adSLiam Girdwood 
1113589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1114589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1115589c3563SJarkko Nikula 				   int num, int dailess)
1116589c3563SJarkko Nikula {
1117589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1118589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1119589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1120589c3563SJarkko Nikula 	const char *temp, *name;
1121589c3563SJarkko Nikula 	int ret = 0;
1122589c3563SJarkko Nikula 
1123589c3563SJarkko Nikula 	if (!dailess) {
1124589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1125589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1126589c3563SJarkko Nikula 		name = dai_link->name;
1127589c3563SJarkko Nikula 	} else {
1128589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1129589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1130589c3563SJarkko Nikula 		name = aux_dev->name;
1131589c3563SJarkko Nikula 	}
11320962bb21SJanusz Krzysztofik 	rtd->card = card;
1133589c3563SJarkko Nikula 
11344b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
11354b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
11364b1cfcb4SMark Brown 
1137589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1138589c3563SJarkko Nikula 	temp = codec->name_prefix;
1139589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1140589c3563SJarkko Nikula 
1141589c3563SJarkko Nikula 	/* do machine specific initialization */
1142589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1143589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1144589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1145589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1146589c3563SJarkko Nikula 	if (ret < 0) {
1147589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1148589c3563SJarkko Nikula 		return ret;
1149589c3563SJarkko Nikula 	}
1150589c3563SJarkko Nikula 	codec->name_prefix = temp;
1151589c3563SJarkko Nikula 
1152589c3563SJarkko Nikula 	/* register the rtd device */
1153589c3563SJarkko Nikula 	rtd->codec = codec;
115436ae1a96SMark Brown 
115536ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
115636ae1a96SMark Brown 	if (!rtd->dev)
115736ae1a96SMark Brown 		return -ENOMEM;
115836ae1a96SMark Brown 	device_initialize(rtd->dev);
115936ae1a96SMark Brown 	rtd->dev->parent = card->dev;
116036ae1a96SMark Brown 	rtd->dev->release = rtd_release;
116136ae1a96SMark Brown 	rtd->dev->init_name = name;
116236ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1163b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
116436ae1a96SMark Brown 	ret = device_add(rtd->dev);
1165589c3563SJarkko Nikula 	if (ret < 0) {
1166589c3563SJarkko Nikula 		dev_err(card->dev,
1167589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1168589c3563SJarkko Nikula 		return ret;
1169589c3563SJarkko Nikula 	}
1170589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1171589c3563SJarkko Nikula 
1172589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
117336ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1174589c3563SJarkko Nikula 	if (ret < 0)
1175589c3563SJarkko Nikula 		dev_err(codec->dev,
1176589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1177589c3563SJarkko Nikula 			ret);
1178589c3563SJarkko Nikula 
1179589c3563SJarkko Nikula 	/* add codec sysfs entries */
118036ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1181589c3563SJarkko Nikula 	if (ret < 0)
1182589c3563SJarkko Nikula 		dev_err(codec->dev,
1183589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1184589c3563SJarkko Nikula 
1185589c3563SJarkko Nikula 	return 0;
1186589c3563SJarkko Nikula }
1187589c3563SJarkko Nikula 
11880168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1189f0fba2adSLiam Girdwood {
1190f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1191f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1192f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1193f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1194c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1195c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1196c74184edSMark Brown 	struct snd_soc_dapm_widget *play_w, *capture_w;
1197f0fba2adSLiam Girdwood 	int ret;
1198f0fba2adSLiam Girdwood 
11990168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
12000168bf0dSLiam Girdwood 			card->name, num, order);
1201f0fba2adSLiam Girdwood 
1202f0fba2adSLiam Girdwood 	/* config components */
1203f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1204f0fba2adSLiam Girdwood 	codec_dai->card = card;
1205f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1206f0fba2adSLiam Girdwood 
1207f0fba2adSLiam Girdwood 	/* set default power off timeout */
1208f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1209f0fba2adSLiam Girdwood 
1210f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
12110168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
12120168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1213be09ad90SLiam Girdwood 		cpu_dai->dapm.card = card;
121461b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
121561b61e3cSLiam Girdwood 			return -ENODEV;
121661b61e3cSLiam Girdwood 
1217be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1218be09ad90SLiam Girdwood 
1219f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1220f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1221f0fba2adSLiam Girdwood 			if (ret < 0) {
12220837fc62SFabio Estevam 				pr_err("asoc: failed to probe CPU DAI %s: %d\n",
12230837fc62SFabio Estevam 							cpu_dai->name, ret);
122461b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1225f0fba2adSLiam Girdwood 				return ret;
1226f0fba2adSLiam Girdwood 			}
1227f0fba2adSLiam Girdwood 		}
1228f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
12291c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1230f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1231f0fba2adSLiam Girdwood 	}
1232f0fba2adSLiam Girdwood 
1233f0fba2adSLiam Girdwood 	/* probe the CODEC */
12340168bf0dSLiam Girdwood 	if (!codec->probed &&
12350168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1236589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1237589c3563SJarkko Nikula 		if (ret < 0)
1238f0fba2adSLiam Girdwood 			return ret;
1239f0fba2adSLiam Girdwood 	}
1240f0fba2adSLiam Girdwood 
1241f0fba2adSLiam Girdwood 	/* probe the platform */
12420168bf0dSLiam Girdwood 	if (!platform->probed &&
12430168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1244956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1245956245e9SLiam Girdwood 		if (ret < 0)
1246f0fba2adSLiam Girdwood 			return ret;
1247f0fba2adSLiam Girdwood 	}
1248f0fba2adSLiam Girdwood 
1249f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
12500168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1251f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1252f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1253f0fba2adSLiam Girdwood 			if (ret < 0) {
12540837fc62SFabio Estevam 				pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
12550837fc62SFabio Estevam 							codec_dai->name, ret);
1256f0fba2adSLiam Girdwood 				return ret;
1257f0fba2adSLiam Girdwood 			}
1258f0fba2adSLiam Girdwood 		}
1259f0fba2adSLiam Girdwood 
12601c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1261f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1262f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1263f0fba2adSLiam Girdwood 	}
1264f0fba2adSLiam Girdwood 
12650168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
12660168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
12670168bf0dSLiam Girdwood 		return 0;
12680168bf0dSLiam Girdwood 
1269589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1270589c3563SJarkko Nikula 	if (ret)
1271f0fba2adSLiam Girdwood 		return ret;
1272f0fba2adSLiam Girdwood 
127336ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1274f0fba2adSLiam Girdwood 	if (ret < 0)
12750837fc62SFabio Estevam 		pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1276f0fba2adSLiam Girdwood 
1277c74184edSMark Brown 	if (!dai_link->params) {
1278f0fba2adSLiam Girdwood 		/* create the pcm */
1279f0fba2adSLiam Girdwood 		ret = soc_new_pcm(rtd, num);
1280f0fba2adSLiam Girdwood 		if (ret < 0) {
12810837fc62SFabio Estevam 			pr_err("asoc: can't create pcm %s :%d\n",
12820837fc62SFabio Estevam 			       dai_link->stream_name, ret);
1283f0fba2adSLiam Girdwood 			return ret;
1284f0fba2adSLiam Girdwood 		}
1285c74184edSMark Brown 	} else {
1286c74184edSMark Brown 		/* link the DAI widgets */
1287c74184edSMark Brown 		play_w = codec_dai->playback_widget;
1288c74184edSMark Brown 		capture_w = cpu_dai->capture_widget;
1289c74184edSMark Brown 		if (play_w && capture_w) {
1290c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1291c74184edSMark Brown 						   capture_w, play_w);
1292c74184edSMark Brown 			if (ret != 0) {
1293c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1294c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1295c74184edSMark Brown 				return ret;
1296c74184edSMark Brown 			}
1297c74184edSMark Brown 		}
1298c74184edSMark Brown 
1299c74184edSMark Brown 		play_w = cpu_dai->playback_widget;
1300c74184edSMark Brown 		capture_w = codec_dai->capture_widget;
1301c74184edSMark Brown 		if (play_w && capture_w) {
1302c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1303c74184edSMark Brown 						   capture_w, play_w);
1304c74184edSMark Brown 			if (ret != 0) {
1305c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1306c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1307c74184edSMark Brown 				return ret;
1308c74184edSMark Brown 			}
1309c74184edSMark Brown 		}
1310c74184edSMark Brown 	}
1311f0fba2adSLiam Girdwood 
1312f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1313f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1314f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1315f0fba2adSLiam Girdwood 
1316f0fba2adSLiam Girdwood 	return 0;
1317f0fba2adSLiam Girdwood }
1318f0fba2adSLiam Girdwood 
1319f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1320f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1321f0fba2adSLiam Girdwood {
1322f0fba2adSLiam Girdwood 	int ret;
1323f0fba2adSLiam Girdwood 
1324f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1325f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
13266b05eda6SMark Brown 	 */
1327f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
13280562f788SMika Westerberg 		/*
13290562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
13300562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
13310562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
13320562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
13330562f788SMika Westerberg 		 *
13340562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
13350562f788SMika Westerberg 		 */
13360562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
13370562f788SMika Westerberg 			return 0;
1338f0fba2adSLiam Girdwood 
1339f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1340f0fba2adSLiam Girdwood 		if (ret < 0) {
13410837fc62SFabio Estevam 			pr_err("asoc: AC97 device register failed:%d\n", ret);
1342f0fba2adSLiam Girdwood 			return ret;
1343435c5e25SMark Brown 		}
1344435c5e25SMark Brown 
1345f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1346f0fba2adSLiam Girdwood 	}
1347f0fba2adSLiam Girdwood 	return 0;
1348f0fba2adSLiam Girdwood }
1349435c5e25SMark Brown 
1350f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1351f0fba2adSLiam Girdwood {
1352f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1353f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1354f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1355f0fba2adSLiam Girdwood 	}
1356f0fba2adSLiam Girdwood }
1357f0fba2adSLiam Girdwood #endif
1358435c5e25SMark Brown 
1359b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1360b19e6e7bSMark Brown {
1361b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1362b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1363b19e6e7bSMark Brown 
1364b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1365b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1366b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1367b19e6e7bSMark Brown 			return 0;
1368b19e6e7bSMark Brown 	}
1369b19e6e7bSMark Brown 
1370b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1371b19e6e7bSMark Brown }
1372b19e6e7bSMark Brown 
13732eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
13742eea392dSJarkko Nikula {
13752eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
13762eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1377676ad98aSJarkko Nikula 	int ret = -ENODEV;
13782eea392dSJarkko Nikula 
13792eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
13802eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
13812eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
13822eea392dSJarkko Nikula 			if (codec->probed) {
13832eea392dSJarkko Nikula 				dev_err(codec->dev,
13842eea392dSJarkko Nikula 					"asoc: codec already probed");
13852eea392dSJarkko Nikula 				ret = -EBUSY;
13862eea392dSJarkko Nikula 				goto out;
13872eea392dSJarkko Nikula 			}
1388676ad98aSJarkko Nikula 			goto found;
13892eea392dSJarkko Nikula 		}
13902eea392dSJarkko Nikula 	}
1391676ad98aSJarkko Nikula 	/* codec not found */
1392676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1393b19e6e7bSMark Brown 	return -EPROBE_DEFER;
13942eea392dSJarkko Nikula 
1395676ad98aSJarkko Nikula found:
1396589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
13972eea392dSJarkko Nikula 	if (ret < 0)
1398589c3563SJarkko Nikula 		return ret;
13992eea392dSJarkko Nikula 
1400589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
14012eea392dSJarkko Nikula 
14022eea392dSJarkko Nikula out:
14032eea392dSJarkko Nikula 	return ret;
14042eea392dSJarkko Nikula }
14052eea392dSJarkko Nikula 
14062eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
14072eea392dSJarkko Nikula {
14082eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14092eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
14102eea392dSJarkko Nikula 
14112eea392dSJarkko Nikula 	/* unregister the rtd device */
14122eea392dSJarkko Nikula 	if (rtd->dev_registered) {
141336ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
141436ae1a96SMark Brown 		device_del(rtd->dev);
14152eea392dSJarkko Nikula 		rtd->dev_registered = 0;
14162eea392dSJarkko Nikula 	}
14172eea392dSJarkko Nikula 
1418589c3563SJarkko Nikula 	if (codec && codec->probed)
1419589c3563SJarkko Nikula 		soc_remove_codec(codec);
14202eea392dSJarkko Nikula }
14212eea392dSJarkko Nikula 
1422fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1423fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1424fdf0f54dSDimitris Papastamos {
1425fdf0f54dSDimitris Papastamos 	int ret;
1426fdf0f54dSDimitris Papastamos 
1427fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1428fdf0f54dSDimitris Papastamos 		return 0;
1429fdf0f54dSDimitris Papastamos 
1430fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1431fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1432fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1433fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1434fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1435fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1436fdf0f54dSDimitris Papastamos 			ret);
1437fdf0f54dSDimitris Papastamos 		return ret;
1438fdf0f54dSDimitris Papastamos 	}
1439fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1440fdf0f54dSDimitris Papastamos 	return 0;
1441fdf0f54dSDimitris Papastamos }
1442fdf0f54dSDimitris Papastamos 
1443b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1444f0fba2adSLiam Girdwood {
1445fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1446fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1447fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
144875d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1449f04209a7SMark Brown 	int ret, i, order, dai_fmt;
145096dd3622SMark Brown 
145101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1452f0fba2adSLiam Girdwood 
1453b19e6e7bSMark Brown 	/* bind DAIs */
1454b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1455b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1456b19e6e7bSMark Brown 		if (ret != 0)
1457b19e6e7bSMark Brown 			goto base_error;
1458db2a4165SFrank Mandarino 	}
1459db2a4165SFrank Mandarino 
1460b19e6e7bSMark Brown 	/* check aux_devs too */
1461b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1462b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1463b19e6e7bSMark Brown 		if (ret != 0)
1464b19e6e7bSMark Brown 			goto base_error;
1465db2a4165SFrank Mandarino 	}
1466db2a4165SFrank Mandarino 
1467fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1468fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1469fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1470fdf0f54dSDimitris Papastamos 			continue;
1471861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1472861f2fafSDimitris Papastamos 		compress_type = 0;
1473fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1474fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1475fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1476fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1477fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1478fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1479fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1480fdf0f54dSDimitris Papastamos 					break;
1481fdf0f54dSDimitris Papastamos 			}
1482fdf0f54dSDimitris Papastamos 		}
1483fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1484b19e6e7bSMark Brown 		if (ret < 0)
1485b19e6e7bSMark Brown 			goto base_error;
1486fdf0f54dSDimitris Papastamos 	}
1487fdf0f54dSDimitris Papastamos 
1488f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1489f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1490f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1491f0fba2adSLiam Girdwood 	if (ret < 0) {
14920837fc62SFabio Estevam 		pr_err("asoc: can't create sound card for card %s: %d\n",
14930837fc62SFabio Estevam 			card->name, ret);
1494b19e6e7bSMark Brown 		goto base_error;
1495db2a4165SFrank Mandarino 	}
1496f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1497db2a4165SFrank Mandarino 
1498e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1499e37a4970SMark Brown 	card->dapm.dev = card->dev;
1500e37a4970SMark Brown 	card->dapm.card = card;
1501e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1502e37a4970SMark Brown 
1503d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1504d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1505d5d1e0beSLars-Peter Clausen #endif
1506d5d1e0beSLars-Peter Clausen 
150788ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
15086ed25978SAndy Green 	/* deferred resume work */
15096308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
15101301a964SRandy Dunlap #endif
15116ed25978SAndy Green 
15129a841ebbSMark Brown 	if (card->dapm_widgets)
15139a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
15149a841ebbSMark Brown 					  card->num_dapm_widgets);
15159a841ebbSMark Brown 
1516f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1517f0fba2adSLiam Girdwood 	if (card->probe) {
1518e7361ec4SMark Brown 		ret = card->probe(card);
1519f0fba2adSLiam Girdwood 		if (ret < 0)
1520f0fba2adSLiam Girdwood 			goto card_probe_error;
1521f0fba2adSLiam Girdwood 	}
1522f0fba2adSLiam Girdwood 
15230168bf0dSLiam Girdwood 	/* early DAI link probe */
15240168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
15250168bf0dSLiam Girdwood 			order++) {
1526fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
15270168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1528fe3e78e0SMark Brown 			if (ret < 0) {
1529321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1530321de0d0SMark Brown 			       card->name, ret);
1531f0fba2adSLiam Girdwood 				goto probe_dai_err;
1532fe3e78e0SMark Brown 			}
1533fe3e78e0SMark Brown 		}
15340168bf0dSLiam Girdwood 	}
1535fe3e78e0SMark Brown 
15362eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
15372eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
15382eea392dSJarkko Nikula 		if (ret < 0) {
15392eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
15402eea392dSJarkko Nikula 			       card->name, ret);
15412eea392dSJarkko Nikula 			goto probe_aux_dev_err;
15422eea392dSJarkko Nikula 		}
15432eea392dSJarkko Nikula 	}
15442eea392dSJarkko Nikula 
1545888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1546888df395SMark Brown 
1547b7af1dafSMark Brown 	if (card->controls)
1548022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1549b7af1dafSMark Brown 
1550b8ad29deSMark Brown 	if (card->dapm_routes)
1551b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1552b8ad29deSMark Brown 					card->num_dapm_routes);
1553b8ad29deSMark Brown 
1554b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1555b90d2f90SMark Brown 
155675d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
155775d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1558f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
155975d9ac46SMark Brown 
1560f04209a7SMark Brown 		if (dai_fmt) {
156175d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1562f04209a7SMark Brown 						  dai_fmt);
15635e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
156475d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
156575d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
156675d9ac46SMark Brown 					 ret);
1567f04209a7SMark Brown 		}
1568f04209a7SMark Brown 
1569f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1570f04209a7SMark Brown 		if (dai_fmt && dai_link->platform_name) {
1571f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1572f04209a7SMark Brown 						  dai_fmt);
1573f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1574f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1575f04209a7SMark Brown 					 "Failed to set DAI format: %d\n",
1576f04209a7SMark Brown 					 ret);
1577f04209a7SMark Brown 		} else if (dai_fmt) {
1578f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1579f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1580f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1581f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1582f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1583f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1584f04209a7SMark Brown 				break;
1585f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1586f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1587f04209a7SMark Brown 				break;
1588f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1589f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1590f04209a7SMark Brown 				break;
1591f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1592f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1593f04209a7SMark Brown 				break;
1594f04209a7SMark Brown 			}
159575d9ac46SMark Brown 
159675d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1597f04209a7SMark Brown 						  dai_fmt);
15985e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
159975d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
160075d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
160175d9ac46SMark Brown 					 ret);
160275d9ac46SMark Brown 		}
160375d9ac46SMark Brown 	}
160475d9ac46SMark Brown 
1605f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1606fe3e78e0SMark Brown 		 "%s", card->name);
1607f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
160822de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1609f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1610f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1611f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1612f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1613f0e8ed85SMark Brown 		case '_':
1614f0e8ed85SMark Brown 		case '-':
1615f0e8ed85SMark Brown 		case '\0':
1616f0e8ed85SMark Brown 			break;
1617f0e8ed85SMark Brown 		default:
1618f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1619f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1620f0e8ed85SMark Brown 			break;
1621f0e8ed85SMark Brown 		}
1622f0e8ed85SMark Brown 	}
1623fe3e78e0SMark Brown 
162428e9ad92SMark Brown 	if (card->late_probe) {
162528e9ad92SMark Brown 		ret = card->late_probe(card);
162628e9ad92SMark Brown 		if (ret < 0) {
162728e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
162828e9ad92SMark Brown 				card->name, ret);
162928e9ad92SMark Brown 			goto probe_aux_dev_err;
163028e9ad92SMark Brown 		}
163128e9ad92SMark Brown 	}
163228e9ad92SMark Brown 
16332dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
16342dc00213SMark Brown 
16351633281bSStephen Warren 	if (card->fully_routed)
1636b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
16371633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
16381633281bSStephen Warren 
1639f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1640fe3e78e0SMark Brown 	if (ret < 0) {
16410837fc62SFabio Estevam 		pr_err("asoc: failed to register soundcard for %s: %d\n",
16420837fc62SFabio Estevam 							card->name, ret);
16436b3ed785SAxel Lin 		goto probe_aux_dev_err;
1644fe3e78e0SMark Brown 	}
1645fe3e78e0SMark Brown 
1646fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1647f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1648f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1649f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1650fe3e78e0SMark Brown 		if (ret < 0) {
16510837fc62SFabio Estevam 			pr_err("asoc: failed to register AC97 %s: %d\n",
16520837fc62SFabio Estevam 							card->name, ret);
16536b3ed785SAxel Lin 			while (--i >= 0)
16547d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
16556b3ed785SAxel Lin 			goto probe_aux_dev_err;
1656fe3e78e0SMark Brown 		}
1657fe3e78e0SMark Brown 	}
1658fe3e78e0SMark Brown #endif
1659fe3e78e0SMark Brown 
1660435c5e25SMark Brown 	card->instantiated = 1;
16614f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1662f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1663b19e6e7bSMark Brown 
1664b19e6e7bSMark Brown 	return 0;
1665db2a4165SFrank Mandarino 
16662eea392dSJarkko Nikula probe_aux_dev_err:
16672eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16682eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16692eea392dSJarkko Nikula 
1670f0fba2adSLiam Girdwood probe_dai_err:
16710671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1672fe3e78e0SMark Brown 
1673f0fba2adSLiam Girdwood card_probe_error:
167487506549SMark Brown 	if (card->remove)
1675e7361ec4SMark Brown 		card->remove(card);
1676f0fba2adSLiam Girdwood 
1677f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1678f0fba2adSLiam Girdwood 
1679b19e6e7bSMark Brown base_error:
1680f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1681db2a4165SFrank Mandarino 
1682b19e6e7bSMark Brown 	return ret;
1683435c5e25SMark Brown }
1684435c5e25SMark Brown 
1685435c5e25SMark Brown /* probes a new socdev */
1686435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1687435c5e25SMark Brown {
1688f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1689435c5e25SMark Brown 	int ret = 0;
1690435c5e25SMark Brown 
169170a7ca34SVinod Koul 	/*
169270a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
169370a7ca34SVinod Koul 	 * we should not be here in that case so ret error
169470a7ca34SVinod Koul 	 */
169570a7ca34SVinod Koul 	if (!card)
169670a7ca34SVinod Koul 		return -EINVAL;
169770a7ca34SVinod Koul 
1698fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1699fe4085e8SMark Brown 		 "ASoC machine %s should use snd_soc_register_card()\n",
1700fe4085e8SMark Brown 		 card->name);
1701fe4085e8SMark Brown 
1702435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1703435c5e25SMark Brown 	card->dev = &pdev->dev;
1704f0fba2adSLiam Girdwood 
1705435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1706435c5e25SMark Brown 	if (ret != 0) {
1707435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1708db2a4165SFrank Mandarino 		return ret;
1709db2a4165SFrank Mandarino 	}
1710db2a4165SFrank Mandarino 
1711435c5e25SMark Brown 	return 0;
1712435c5e25SMark Brown }
1713435c5e25SMark Brown 
1714b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1715db2a4165SFrank Mandarino {
1716db2a4165SFrank Mandarino 	int i;
1717db2a4165SFrank Mandarino 
1718f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1719f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1720f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17215b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1722db2a4165SFrank Mandarino 	}
1723db2a4165SFrank Mandarino 
17242eea392dSJarkko Nikula 	/* remove auxiliary devices */
17252eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17262eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17272eea392dSJarkko Nikula 
1728f0fba2adSLiam Girdwood 	/* remove and free each DAI */
17290671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1730f0fba2adSLiam Girdwood 
1731a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1732a6052154SJarkko Nikula 
1733f0fba2adSLiam Girdwood 	/* remove the card */
173487506549SMark Brown 	if (card->remove)
1735e7361ec4SMark Brown 		card->remove(card);
1736f0fba2adSLiam Girdwood 
17370aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
17380aaae527SLars-Peter Clausen 
1739f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1740b0e26485SVinod Koul 	return 0;
1741b0e26485SVinod Koul 
1742b2dfa62cSGuennadi Liakhovetski }
1743b0e26485SVinod Koul 
1744b0e26485SVinod Koul /* removes a socdev */
1745b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1746b0e26485SVinod Koul {
1747b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1748b0e26485SVinod Koul 
1749c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1750db2a4165SFrank Mandarino 	return 0;
1751db2a4165SFrank Mandarino }
1752db2a4165SFrank Mandarino 
17536f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
175451737470SMark Brown {
17556f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1756f0fba2adSLiam Girdwood 	int i;
175751737470SMark Brown 
175851737470SMark Brown 	if (!card->instantiated)
1759416356fcSMark Brown 		return 0;
176051737470SMark Brown 
176151737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
176251737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1763f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1764f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17655b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1766f0fba2adSLiam Girdwood 	}
176751737470SMark Brown 
1768f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1769416356fcSMark Brown 
1770416356fcSMark Brown 	return 0;
177151737470SMark Brown }
17726f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
177351737470SMark Brown 
17746f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1775b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1776b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1777b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1778b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
17796f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1780b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1781416356fcSMark Brown };
1782deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1783416356fcSMark Brown 
1784db2a4165SFrank Mandarino /* ASoC platform driver */
1785db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1786db2a4165SFrank Mandarino 	.driver		= {
1787db2a4165SFrank Mandarino 		.name		= "soc-audio",
17888b45a209SKay Sievers 		.owner		= THIS_MODULE,
17896f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1790db2a4165SFrank Mandarino 	},
1791db2a4165SFrank Mandarino 	.probe		= soc_probe,
1792db2a4165SFrank Mandarino 	.remove		= soc_remove,
1793db2a4165SFrank Mandarino };
1794db2a4165SFrank Mandarino 
1795096e49d5SMark Brown /**
1796096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1797096e49d5SMark Brown  *
1798096e49d5SMark Brown  * @codec: CODEC to query.
1799096e49d5SMark Brown  * @reg: Register to query.
1800096e49d5SMark Brown  *
1801096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1802096e49d5SMark Brown  */
1803181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1804181e055eSMark Brown 				    unsigned int reg)
1805096e49d5SMark Brown {
18061500b7b5SDimitris Papastamos 	if (codec->volatile_register)
18071500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1808096e49d5SMark Brown 	else
1809096e49d5SMark Brown 		return 0;
1810096e49d5SMark Brown }
1811096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1812096e49d5SMark Brown 
1813db2a4165SFrank Mandarino /**
1814239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1815239c9706SDimitris Papastamos  *
1816239c9706SDimitris Papastamos  * @codec: CODEC to query.
1817239c9706SDimitris Papastamos  * @reg: Register to query.
1818239c9706SDimitris Papastamos  *
1819239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1820239c9706SDimitris Papastamos  */
1821239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1822239c9706SDimitris Papastamos 				    unsigned int reg)
1823239c9706SDimitris Papastamos {
1824239c9706SDimitris Papastamos 	if (codec->readable_register)
1825239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1826239c9706SDimitris Papastamos 	else
182763fa0a28SLars-Peter Clausen 		return 1;
1828239c9706SDimitris Papastamos }
1829239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1830239c9706SDimitris Papastamos 
1831239c9706SDimitris Papastamos /**
1832239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1833239c9706SDimitris Papastamos  *
1834239c9706SDimitris Papastamos  * @codec: CODEC to query.
1835239c9706SDimitris Papastamos  * @reg: Register to query.
1836239c9706SDimitris Papastamos  *
1837239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1838239c9706SDimitris Papastamos  */
1839239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1840239c9706SDimitris Papastamos 				    unsigned int reg)
1841239c9706SDimitris Papastamos {
1842239c9706SDimitris Papastamos 	if (codec->writable_register)
1843239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1844239c9706SDimitris Papastamos 	else
184563fa0a28SLars-Peter Clausen 		return 1;
1846239c9706SDimitris Papastamos }
1847239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1848239c9706SDimitris Papastamos 
1849f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1850f1442bc1SLiam Girdwood 					unsigned int reg)
1851f1442bc1SLiam Girdwood {
1852f1442bc1SLiam Girdwood 	unsigned int ret;
1853f1442bc1SLiam Girdwood 
1854f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1855f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1856f1442bc1SLiam Girdwood 		return -1;
1857f1442bc1SLiam Girdwood 	}
1858f1442bc1SLiam Girdwood 
1859f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1860f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1861a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1862f1442bc1SLiam Girdwood 
1863f1442bc1SLiam Girdwood 	return ret;
1864f1442bc1SLiam Girdwood }
1865f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1866f1442bc1SLiam Girdwood 
1867f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1868f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1869f1442bc1SLiam Girdwood {
1870f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1871f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1872f1442bc1SLiam Girdwood 		return -1;
1873f1442bc1SLiam Girdwood 	}
1874f1442bc1SLiam Girdwood 
1875f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1876a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
1877f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1878f1442bc1SLiam Girdwood }
1879f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1880f1442bc1SLiam Girdwood 
1881239c9706SDimitris Papastamos /**
1882db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1883db2a4165SFrank Mandarino  * @codec: audio codec
1884db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1885db2a4165SFrank Mandarino  * @num: AC97 codec number
1886db2a4165SFrank Mandarino  *
1887db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1888db2a4165SFrank Mandarino  */
1889db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1890db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1891db2a4165SFrank Mandarino {
1892db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1893db2a4165SFrank Mandarino 
1894db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1895db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1896db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1897db2a4165SFrank Mandarino 		return -ENOMEM;
1898db2a4165SFrank Mandarino 	}
1899db2a4165SFrank Mandarino 
1900db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1901db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1902db2a4165SFrank Mandarino 		kfree(codec->ac97);
1903db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1904db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1905db2a4165SFrank Mandarino 		return -ENOMEM;
1906db2a4165SFrank Mandarino 	}
1907db2a4165SFrank Mandarino 
1908db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1909db2a4165SFrank Mandarino 	codec->ac97->num = num;
19100562f788SMika Westerberg 
19110562f788SMika Westerberg 	/*
19120562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
19130562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
19140562f788SMika Westerberg 	 */
19150562f788SMika Westerberg 	codec->ac97_created = 1;
19160562f788SMika Westerberg 
1917db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1918db2a4165SFrank Mandarino 	return 0;
1919db2a4165SFrank Mandarino }
1920db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1921db2a4165SFrank Mandarino 
1922db2a4165SFrank Mandarino /**
1923db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1924db2a4165SFrank Mandarino  * @codec: audio codec
1925db2a4165SFrank Mandarino  *
1926db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1927db2a4165SFrank Mandarino  */
1928db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1929db2a4165SFrank Mandarino {
1930db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1931f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1932f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1933f0fba2adSLiam Girdwood #endif
1934db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1935db2a4165SFrank Mandarino 	kfree(codec->ac97);
1936db2a4165SFrank Mandarino 	codec->ac97 = NULL;
19370562f788SMika Westerberg 	codec->ac97_created = 0;
1938db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1939db2a4165SFrank Mandarino }
1940db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1941db2a4165SFrank Mandarino 
1942c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1943c3753707SMark Brown {
1944c3753707SMark Brown 	unsigned int ret;
1945c3753707SMark Brown 
1946c3acec26SMark Brown 	ret = codec->read(codec, reg);
1947c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1948a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1949c3753707SMark Brown 
1950c3753707SMark Brown 	return ret;
1951c3753707SMark Brown }
1952c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1953c3753707SMark Brown 
1954c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1955c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1956c3753707SMark Brown {
1957c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1958a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1959c3acec26SMark Brown 	return codec->write(codec, reg, val);
1960c3753707SMark Brown }
1961c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1962c3753707SMark Brown 
19635fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
19645fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
19655fb609d4SDimitris Papastamos {
19665fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
19675fb609d4SDimitris Papastamos }
19685fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
19695fb609d4SDimitris Papastamos 
1970db2a4165SFrank Mandarino /**
1971db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1972db2a4165SFrank Mandarino  * @codec: audio codec
1973db2a4165SFrank Mandarino  * @reg: codec register
1974db2a4165SFrank Mandarino  * @mask: register mask
1975db2a4165SFrank Mandarino  * @value: new value
1976db2a4165SFrank Mandarino  *
1977db2a4165SFrank Mandarino  * Writes new register value.
1978db2a4165SFrank Mandarino  *
1979180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
1980db2a4165SFrank Mandarino  */
1981db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
198246f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1983db2a4165SFrank Mandarino {
19848a713da8SMark Brown 	bool change;
198546f5822fSDaniel Ribeiro 	unsigned int old, new;
1986180c329dSTimur Tabi 	int ret;
1987db2a4165SFrank Mandarino 
19888a713da8SMark Brown 	if (codec->using_regmap) {
19898a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
19908a713da8SMark Brown 					       mask, value, &change);
19918a713da8SMark Brown 	} else {
1992180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
1993180c329dSTimur Tabi 		if (ret < 0)
1994180c329dSTimur Tabi 			return ret;
1995180c329dSTimur Tabi 
1996180c329dSTimur Tabi 		old = ret;
199778bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
1998db2a4165SFrank Mandarino 		change = old != new;
19998a713da8SMark Brown 		if (change)
2000180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
20018a713da8SMark Brown 	}
20028a713da8SMark Brown 
2003180c329dSTimur Tabi 	if (ret < 0)
2004180c329dSTimur Tabi 		return ret;
2005db2a4165SFrank Mandarino 
2006db2a4165SFrank Mandarino 	return change;
2007db2a4165SFrank Mandarino }
2008db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2009db2a4165SFrank Mandarino 
2010db2a4165SFrank Mandarino /**
20116c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
20126c508c62SEero Nurkkala  * @codec: audio codec
20136c508c62SEero Nurkkala  * @reg: codec register
20146c508c62SEero Nurkkala  * @mask: register mask
20156c508c62SEero Nurkkala  * @value: new value
20166c508c62SEero Nurkkala  *
20176c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
20186c508c62SEero Nurkkala  *
20196c508c62SEero Nurkkala  * Returns 1 for change else 0.
20206c508c62SEero Nurkkala  */
2021dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
20226c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
20236c508c62SEero Nurkkala 			       unsigned int value)
20246c508c62SEero Nurkkala {
20256c508c62SEero Nurkkala 	int change;
20266c508c62SEero Nurkkala 
20276c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
20286c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
20296c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
20306c508c62SEero Nurkkala 
20316c508c62SEero Nurkkala 	return change;
20326c508c62SEero Nurkkala }
2033dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
20346c508c62SEero Nurkkala 
20356c508c62SEero Nurkkala /**
2036db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2037db2a4165SFrank Mandarino  * @codec: audio codec
2038db2a4165SFrank Mandarino  * @reg: codec register
2039db2a4165SFrank Mandarino  * @mask: register mask
2040db2a4165SFrank Mandarino  * @value: new value
2041db2a4165SFrank Mandarino  *
2042db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2043db2a4165SFrank Mandarino  * different from the old value.
2044db2a4165SFrank Mandarino  *
2045db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2046db2a4165SFrank Mandarino  */
2047db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
204846f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2049db2a4165SFrank Mandarino {
2050db2a4165SFrank Mandarino 	int change;
205146f5822fSDaniel Ribeiro 	unsigned int old, new;
2052db2a4165SFrank Mandarino 
2053db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2054db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2055db2a4165SFrank Mandarino 	change = old != new;
2056db2a4165SFrank Mandarino 
2057db2a4165SFrank Mandarino 	return change;
2058db2a4165SFrank Mandarino }
2059db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2060db2a4165SFrank Mandarino 
2061db2a4165SFrank Mandarino /**
2062db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2063db2a4165SFrank Mandarino  * @substream: the pcm substream
2064db2a4165SFrank Mandarino  * @hw: the hardware parameters
2065db2a4165SFrank Mandarino  *
2066db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2067db2a4165SFrank Mandarino  */
2068db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2069db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2070db2a4165SFrank Mandarino {
2071db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2072db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2073db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2074db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2075db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2076db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2077db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2078db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2079db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2080db2a4165SFrank Mandarino 	return 0;
2081db2a4165SFrank Mandarino }
2082db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2083db2a4165SFrank Mandarino 
2084db2a4165SFrank Mandarino /**
2085db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2086db2a4165SFrank Mandarino  * @_template: control template
2087db2a4165SFrank Mandarino  * @data: control private data
2088ac11a2b3SMark Brown  * @long_name: control long name
2089efb7ac3fSMark Brown  * @prefix: control name prefix
2090db2a4165SFrank Mandarino  *
2091db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2092db2a4165SFrank Mandarino  *
2093db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2094db2a4165SFrank Mandarino  */
2095db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
20963056557fSMark Brown 				  void *data, const char *long_name,
2097efb7ac3fSMark Brown 				  const char *prefix)
2098db2a4165SFrank Mandarino {
2099db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2100efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2101efb7ac3fSMark Brown 	char *name = NULL;
2102efb7ac3fSMark Brown 	int name_len;
2103db2a4165SFrank Mandarino 
2104db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2105db2a4165SFrank Mandarino 	template.index = 0;
2106db2a4165SFrank Mandarino 
2107efb7ac3fSMark Brown 	if (!long_name)
2108efb7ac3fSMark Brown 		long_name = template.name;
2109efb7ac3fSMark Brown 
2110efb7ac3fSMark Brown 	if (prefix) {
2111efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
211257cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2113efb7ac3fSMark Brown 		if (!name)
2114efb7ac3fSMark Brown 			return NULL;
2115efb7ac3fSMark Brown 
2116efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2117efb7ac3fSMark Brown 
2118efb7ac3fSMark Brown 		template.name = name;
2119efb7ac3fSMark Brown 	} else {
2120efb7ac3fSMark Brown 		template.name = long_name;
2121efb7ac3fSMark Brown 	}
2122efb7ac3fSMark Brown 
2123efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2124efb7ac3fSMark Brown 
2125efb7ac3fSMark Brown 	kfree(name);
2126efb7ac3fSMark Brown 
2127efb7ac3fSMark Brown 	return kcontrol;
2128db2a4165SFrank Mandarino }
2129db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2130db2a4165SFrank Mandarino 
2131022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2132022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2133022658beSLiam Girdwood 	const char *prefix, void *data)
2134022658beSLiam Girdwood {
2135022658beSLiam Girdwood 	int err, i;
2136022658beSLiam Girdwood 
2137022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2138022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2139022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2140022658beSLiam Girdwood 						     control->name, prefix));
2141022658beSLiam Girdwood 		if (err < 0) {
2142022658beSLiam Girdwood 			dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2143022658beSLiam Girdwood 			return err;
2144022658beSLiam Girdwood 		}
2145022658beSLiam Girdwood 	}
2146022658beSLiam Girdwood 
2147022658beSLiam Girdwood 	return 0;
2148022658beSLiam Girdwood }
2149022658beSLiam Girdwood 
2150db2a4165SFrank Mandarino /**
2151022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2152022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
21533e8e1952SIan Molton  * duplicating this code.
21543e8e1952SIan Molton  *
21553e8e1952SIan Molton  * @codec: codec to add controls to
21563e8e1952SIan Molton  * @controls: array of controls to add
21573e8e1952SIan Molton  * @num_controls: number of elements in the array
21583e8e1952SIan Molton  *
21593e8e1952SIan Molton  * Return 0 for success, else error.
21603e8e1952SIan Molton  */
2161022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
21623e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
21633e8e1952SIan Molton {
2164f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
21653e8e1952SIan Molton 
2166022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2167022658beSLiam Girdwood 			codec->name_prefix, codec);
21683e8e1952SIan Molton }
2169022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
21703e8e1952SIan Molton 
21713e8e1952SIan Molton /**
2172a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2173022658beSLiam Girdwood  * Convenience function to add a list of controls.
2174a491a5c8SLiam Girdwood  *
2175a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2176a491a5c8SLiam Girdwood  * @controls: array of controls to add
2177a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2178a491a5c8SLiam Girdwood  *
2179a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2180a491a5c8SLiam Girdwood  */
2181a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2182a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2183a491a5c8SLiam Girdwood {
2184a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2185a491a5c8SLiam Girdwood 
2186022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2187022658beSLiam Girdwood 			NULL, platform);
2188a491a5c8SLiam Girdwood }
2189a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2190a491a5c8SLiam Girdwood 
2191a491a5c8SLiam Girdwood /**
2192022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2193022658beSLiam Girdwood  * Convenience function to add a list of controls.
2194022658beSLiam Girdwood  *
2195022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2196022658beSLiam Girdwood  * @controls: array of controls to add
2197022658beSLiam Girdwood  * @num_controls: number of elements in the array
2198022658beSLiam Girdwood  *
2199022658beSLiam Girdwood  * Return 0 for success, else error.
2200022658beSLiam Girdwood  */
2201022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2202022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2203022658beSLiam Girdwood {
2204022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2205022658beSLiam Girdwood 
2206022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2207022658beSLiam Girdwood 			NULL, soc_card);
2208022658beSLiam Girdwood }
2209022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2210022658beSLiam Girdwood 
2211022658beSLiam Girdwood /**
2212022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2213022658beSLiam Girdwood  * Convienience function to add a list of controls.
2214022658beSLiam Girdwood  *
2215022658beSLiam Girdwood  * @dai: DAI to add controls to
2216022658beSLiam Girdwood  * @controls: array of controls to add
2217022658beSLiam Girdwood  * @num_controls: number of elements in the array
2218022658beSLiam Girdwood  *
2219022658beSLiam Girdwood  * Return 0 for success, else error.
2220022658beSLiam Girdwood  */
2221022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2222022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2223022658beSLiam Girdwood {
2224022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2225022658beSLiam Girdwood 
2226022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2227022658beSLiam Girdwood 			NULL, dai);
2228022658beSLiam Girdwood }
2229022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2230022658beSLiam Girdwood 
2231022658beSLiam Girdwood /**
2232db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2233db2a4165SFrank Mandarino  * @kcontrol: mixer control
2234db2a4165SFrank Mandarino  * @uinfo: control element information
2235db2a4165SFrank Mandarino  *
2236db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2237db2a4165SFrank Mandarino  * mixer control.
2238db2a4165SFrank Mandarino  *
2239db2a4165SFrank Mandarino  * Returns 0 for success.
2240db2a4165SFrank Mandarino  */
2241db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2242db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2243db2a4165SFrank Mandarino {
2244db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2245db2a4165SFrank Mandarino 
2246db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2247db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2248f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2249db2a4165SFrank Mandarino 
2250f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2251f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2252db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2253db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2254db2a4165SFrank Mandarino 	return 0;
2255db2a4165SFrank Mandarino }
2256db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2257db2a4165SFrank Mandarino 
2258db2a4165SFrank Mandarino /**
2259db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2260db2a4165SFrank Mandarino  * @kcontrol: mixer control
2261ac11a2b3SMark Brown  * @ucontrol: control element information
2262db2a4165SFrank Mandarino  *
2263db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2264db2a4165SFrank Mandarino  *
2265db2a4165SFrank Mandarino  * Returns 0 for success.
2266db2a4165SFrank Mandarino  */
2267db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2268db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2269db2a4165SFrank Mandarino {
2270db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2271db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
227246f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2273db2a4165SFrank Mandarino 
2274f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2275db2a4165SFrank Mandarino 		;
2276db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
22773ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
22783ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2279db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2280db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2281db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2282db2a4165SFrank Mandarino 
2283db2a4165SFrank Mandarino 	return 0;
2284db2a4165SFrank Mandarino }
2285db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2286db2a4165SFrank Mandarino 
2287db2a4165SFrank Mandarino /**
2288db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2289db2a4165SFrank Mandarino  * @kcontrol: mixer control
2290ac11a2b3SMark Brown  * @ucontrol: control element information
2291db2a4165SFrank Mandarino  *
2292db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2293db2a4165SFrank Mandarino  *
2294db2a4165SFrank Mandarino  * Returns 0 for success.
2295db2a4165SFrank Mandarino  */
2296db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2297db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2298db2a4165SFrank Mandarino {
2299db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2300db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
230146f5822fSDaniel Ribeiro 	unsigned int val;
230246f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2303db2a4165SFrank Mandarino 
2304f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2305db2a4165SFrank Mandarino 		;
2306f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2307db2a4165SFrank Mandarino 		return -EINVAL;
2308db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2309db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2310db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2311f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2312db2a4165SFrank Mandarino 			return -EINVAL;
2313db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2314db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2315db2a4165SFrank Mandarino 	}
2316db2a4165SFrank Mandarino 
23176c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2318db2a4165SFrank Mandarino }
2319db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2320db2a4165SFrank Mandarino 
2321db2a4165SFrank Mandarino /**
23222e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
23232e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23242e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23252e72f8e3SPeter Ujfalusi  *
23262e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
23272e72f8e3SPeter Ujfalusi  *
23282e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23292e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23302e72f8e3SPeter Ujfalusi  *
23312e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23322e72f8e3SPeter Ujfalusi  */
23332e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
23342e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23352e72f8e3SPeter Ujfalusi {
23362e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
233774155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
233846f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
23392e72f8e3SPeter Ujfalusi 
23402e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
23412e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
23422e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
23432e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
23442e72f8e3SPeter Ujfalusi 			break;
23452e72f8e3SPeter Ujfalusi 	}
23462e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
23472e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23482e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
23492e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
23502e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
23512e72f8e3SPeter Ujfalusi 				break;
23522e72f8e3SPeter Ujfalusi 		}
23532e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
23542e72f8e3SPeter Ujfalusi 	}
23552e72f8e3SPeter Ujfalusi 
23562e72f8e3SPeter Ujfalusi 	return 0;
23572e72f8e3SPeter Ujfalusi }
23582e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
23592e72f8e3SPeter Ujfalusi 
23602e72f8e3SPeter Ujfalusi /**
23612e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
23622e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23632e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23642e72f8e3SPeter Ujfalusi  *
23652e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
23662e72f8e3SPeter Ujfalusi  *
23672e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23682e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23692e72f8e3SPeter Ujfalusi  *
23702e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23712e72f8e3SPeter Ujfalusi  */
23722e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
23732e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23742e72f8e3SPeter Ujfalusi {
23752e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
237674155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
237746f5822fSDaniel Ribeiro 	unsigned int val;
237846f5822fSDaniel Ribeiro 	unsigned int mask;
23792e72f8e3SPeter Ujfalusi 
23802e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
23812e72f8e3SPeter Ujfalusi 		return -EINVAL;
23822e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
23832e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
23842e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23852e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
23862e72f8e3SPeter Ujfalusi 			return -EINVAL;
23872e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
23882e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
23892e72f8e3SPeter Ujfalusi 	}
23902e72f8e3SPeter Ujfalusi 
23916c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
23922e72f8e3SPeter Ujfalusi }
23932e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
23942e72f8e3SPeter Ujfalusi 
23952e72f8e3SPeter Ujfalusi /**
2396db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2397db2a4165SFrank Mandarino  * @kcontrol: mixer control
2398db2a4165SFrank Mandarino  * @uinfo: control element information
2399db2a4165SFrank Mandarino  *
2400db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2401db2a4165SFrank Mandarino  * single mixer.
2402db2a4165SFrank Mandarino  *
2403db2a4165SFrank Mandarino  * Returns 0 for success.
2404db2a4165SFrank Mandarino  */
2405db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2406db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2407db2a4165SFrank Mandarino {
2408db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2409db2a4165SFrank Mandarino 
2410db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2411db2a4165SFrank Mandarino 	uinfo->count = 1;
2412f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2413db2a4165SFrank Mandarino 
2414f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2415f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2416db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2417db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2418db2a4165SFrank Mandarino 	return 0;
2419db2a4165SFrank Mandarino }
2420db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2421db2a4165SFrank Mandarino 
2422db2a4165SFrank Mandarino /**
2423db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2424db2a4165SFrank Mandarino  * @kcontrol: mixer control
2425db2a4165SFrank Mandarino  * @uinfo: control element information
2426db2a4165SFrank Mandarino  *
2427db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2428db2a4165SFrank Mandarino  *
2429db2a4165SFrank Mandarino  * Returns 0 for success.
2430db2a4165SFrank Mandarino  */
2431db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2432db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2433db2a4165SFrank Mandarino {
2434a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2435db2a4165SFrank Mandarino 
2436fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2437a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2438a7a4ac86SPhilipp Zabel 	else
2439a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2440a7a4ac86SPhilipp Zabel 
2441db2a4165SFrank Mandarino 	uinfo->count = 1;
2442db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2443a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2444db2a4165SFrank Mandarino 	return 0;
2445db2a4165SFrank Mandarino }
2446db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2447db2a4165SFrank Mandarino 
2448db2a4165SFrank Mandarino /**
2449db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2450db2a4165SFrank Mandarino  * @kcontrol: mixer control
2451db2a4165SFrank Mandarino  * @uinfo: control element information
2452db2a4165SFrank Mandarino  *
2453e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2454e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2455db2a4165SFrank Mandarino  *
2456db2a4165SFrank Mandarino  * Returns 0 for success.
2457db2a4165SFrank Mandarino  */
2458db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2459db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2460db2a4165SFrank Mandarino {
24614eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24624eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2463d11bb4a9SPeter Ujfalusi 	int platform_max;
2464db2a4165SFrank Mandarino 
2465d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2466d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2467d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2468d11bb4a9SPeter Ujfalusi 
2469d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2470a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2471a7a4ac86SPhilipp Zabel 	else
2472a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2473a7a4ac86SPhilipp Zabel 
2474e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2475db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2476d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2477db2a4165SFrank Mandarino 	return 0;
2478db2a4165SFrank Mandarino }
2479db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2480db2a4165SFrank Mandarino 
2481db2a4165SFrank Mandarino /**
2482db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2483db2a4165SFrank Mandarino  * @kcontrol: mixer control
2484ac11a2b3SMark Brown  * @ucontrol: control element information
2485db2a4165SFrank Mandarino  *
2486f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2487f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2488db2a4165SFrank Mandarino  *
2489db2a4165SFrank Mandarino  * Returns 0 for success.
2490db2a4165SFrank Mandarino  */
2491db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2492db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2493db2a4165SFrank Mandarino {
24944eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24954eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2496db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2497815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2498f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2499815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2500815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25014eaa9819SJon Smirl 	int max = mc->max;
2502815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2503815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2504db2a4165SFrank Mandarino 
2505db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2506db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2507f7915d99SPeter Ujfalusi 	if (invert)
2508db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2509a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2510f7915d99SPeter Ujfalusi 
2511f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2512f7915d99SPeter Ujfalusi 		if (reg == reg2)
2513f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2514f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2515f7915d99SPeter Ujfalusi 		else
2516f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2517f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2518f7915d99SPeter Ujfalusi 		if (invert)
2519db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2520a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2521db2a4165SFrank Mandarino 	}
2522db2a4165SFrank Mandarino 
2523db2a4165SFrank Mandarino 	return 0;
2524db2a4165SFrank Mandarino }
2525db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2526db2a4165SFrank Mandarino 
2527db2a4165SFrank Mandarino /**
2528db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2529db2a4165SFrank Mandarino  * @kcontrol: mixer control
2530ac11a2b3SMark Brown  * @ucontrol: control element information
2531db2a4165SFrank Mandarino  *
2532974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2533974815baSPeter Ujfalusi  * control that spans 2 registers.
2534db2a4165SFrank Mandarino  *
2535db2a4165SFrank Mandarino  * Returns 0 for success.
2536db2a4165SFrank Mandarino  */
2537db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2538db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2539db2a4165SFrank Mandarino {
25404eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25414eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2542db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2543815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2544974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2545815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2546815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25474eaa9819SJon Smirl 	int max = mc->max;
2548815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2549815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2550974815baSPeter Ujfalusi 	int err;
2551974815baSPeter Ujfalusi 	bool type_2r = 0;
2552974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2553974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2554db2a4165SFrank Mandarino 
2555db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2556db2a4165SFrank Mandarino 	if (invert)
2557a7a4ac86SPhilipp Zabel 		val = max - val;
2558db2a4165SFrank Mandarino 	val_mask = mask << shift;
2559db2a4165SFrank Mandarino 	val = val << shift;
2560974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2561db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2562db2a4165SFrank Mandarino 		if (invert)
2563a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2564974815baSPeter Ujfalusi 		if (reg == reg2) {
2565db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2566db2a4165SFrank Mandarino 			val |= val2 << rshift;
2567974815baSPeter Ujfalusi 		} else {
2568db2a4165SFrank Mandarino 			val2 = val2 << shift;
2569974815baSPeter Ujfalusi 			type_2r = 1;
2570974815baSPeter Ujfalusi 		}
2571974815baSPeter Ujfalusi 	}
25726c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
25733ff3f64bSMark Brown 	if (err < 0)
2574db2a4165SFrank Mandarino 		return err;
2575db2a4165SFrank Mandarino 
2576974815baSPeter Ujfalusi 	if (type_2r)
25776c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2578974815baSPeter Ujfalusi 
2579db2a4165SFrank Mandarino 	return err;
2580db2a4165SFrank Mandarino }
2581974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2582db2a4165SFrank Mandarino 
2583e13ac2e9SMark Brown /**
25841d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
25851d99f243SBrian Austin  * @kcontrol: mixer control
25861d99f243SBrian Austin  * @ucontrol: control element information
25871d99f243SBrian Austin  *
25881d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
25891d99f243SBrian Austin  * control that spans 2 registers.
25901d99f243SBrian Austin  *
25911d99f243SBrian Austin  * Returns 0 for success.
25921d99f243SBrian Austin  */
25931d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
25941d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
25951d99f243SBrian Austin {
25961d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
25971d99f243SBrian Austin 	struct soc_mixer_control *mc =
25981d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
25991d99f243SBrian Austin 
26001d99f243SBrian Austin 	unsigned int reg = mc->reg;
26011d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
26021d99f243SBrian Austin 	unsigned int shift = mc->shift;
26031d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
26041d99f243SBrian Austin 	int max = mc->max;
26051d99f243SBrian Austin 	int min = mc->min;
26061d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
26071d99f243SBrian Austin 
26081d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
26091d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
26101d99f243SBrian Austin 
26111d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
26121d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
26131d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
26141d99f243SBrian Austin 
26151d99f243SBrian Austin 	return 0;
26161d99f243SBrian Austin }
26171d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
26181d99f243SBrian Austin 
26191d99f243SBrian Austin /**
26201d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
26211d99f243SBrian Austin  * @kcontrol: mixer control
26221d99f243SBrian Austin  * @uinfo: control element information
26231d99f243SBrian Austin  *
26241d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
26251d99f243SBrian Austin  *
26261d99f243SBrian Austin  * Returns 0 for success.
26271d99f243SBrian Austin  */
26281d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
26291d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
26301d99f243SBrian Austin {
26311d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
26321d99f243SBrian Austin 	struct soc_mixer_control *mc =
26331d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
26341d99f243SBrian Austin 
26351d99f243SBrian Austin 	unsigned int reg = mc->reg;
26361d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
26371d99f243SBrian Austin 	unsigned int shift = mc->shift;
26381d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
26391d99f243SBrian Austin 	int max = mc->max;
26401d99f243SBrian Austin 	int min = mc->min;
26411d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
264227f1d759SBrian Austin 	int err = 0;
26431d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
26441d99f243SBrian Austin 
26451d99f243SBrian Austin 	val_mask = mask << shift;
26461d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
26471d99f243SBrian Austin 	val = val << shift;
26481d99f243SBrian Austin 
26491d99f243SBrian Austin 	if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
26501d99f243SBrian Austin 			return err;
26511d99f243SBrian Austin 
26521d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
26531d99f243SBrian Austin 		val_mask = mask << rshift;
26541d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
26551d99f243SBrian Austin 		val2 = val2 << rshift;
26561d99f243SBrian Austin 
26571d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
26581d99f243SBrian Austin 			return err;
26591d99f243SBrian Austin 	}
26601d99f243SBrian Austin 	return 0;
26611d99f243SBrian Austin }
26621d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
26631d99f243SBrian Austin 
26641d99f243SBrian Austin /**
2665e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2666e13ac2e9SMark Brown  * @kcontrol: mixer control
2667e13ac2e9SMark Brown  * @uinfo: control element information
2668e13ac2e9SMark Brown  *
2669e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2670e13ac2e9SMark Brown  *
2671e13ac2e9SMark Brown  * Returns 0 for success.
2672e13ac2e9SMark Brown  */
2673e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2674e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2675e13ac2e9SMark Brown {
26764eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26774eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2678d11bb4a9SPeter Ujfalusi 	int platform_max;
26794eaa9819SJon Smirl 	int min = mc->min;
2680e13ac2e9SMark Brown 
2681d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2682d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2683d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2684d11bb4a9SPeter Ujfalusi 
2685e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2686e13ac2e9SMark Brown 	uinfo->count = 2;
2687e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2688d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2689e13ac2e9SMark Brown 	return 0;
2690e13ac2e9SMark Brown }
2691e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2692e13ac2e9SMark Brown 
2693e13ac2e9SMark Brown /**
2694e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2695e13ac2e9SMark Brown  * @kcontrol: mixer control
2696ac11a2b3SMark Brown  * @ucontrol: control element information
2697e13ac2e9SMark Brown  *
2698e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2699e13ac2e9SMark Brown  *
2700e13ac2e9SMark Brown  * Returns 0 for success.
2701e13ac2e9SMark Brown  */
2702e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2703e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2704e13ac2e9SMark Brown {
27054eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27064eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2707e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2708815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27094eaa9819SJon Smirl 	int min = mc->min;
2710e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2711e13ac2e9SMark Brown 
2712e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2713e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2714e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2715e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2716e13ac2e9SMark Brown 	return 0;
2717e13ac2e9SMark Brown }
2718e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2719e13ac2e9SMark Brown 
2720e13ac2e9SMark Brown /**
2721e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2722e13ac2e9SMark Brown  * @kcontrol: mixer control
2723ac11a2b3SMark Brown  * @ucontrol: control element information
2724e13ac2e9SMark Brown  *
2725e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2726e13ac2e9SMark Brown  *
2727e13ac2e9SMark Brown  * Returns 0 for success.
2728e13ac2e9SMark Brown  */
2729e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2730e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2731e13ac2e9SMark Brown {
27324eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27334eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2734e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2735815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27364eaa9819SJon Smirl 	int min = mc->min;
273746f5822fSDaniel Ribeiro 	unsigned int val;
2738e13ac2e9SMark Brown 
2739e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2740e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2741e13ac2e9SMark Brown 
27426c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2743e13ac2e9SMark Brown }
2744e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2745e13ac2e9SMark Brown 
27468c6529dbSLiam Girdwood /**
2747637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2748637d3847SPeter Ujfalusi  *
2749637d3847SPeter Ujfalusi  * @codec: where to look for the control
2750637d3847SPeter Ujfalusi  * @name: Name of the control
2751637d3847SPeter Ujfalusi  * @max: new maximum limit
2752637d3847SPeter Ujfalusi  *
2753637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2754637d3847SPeter Ujfalusi  */
2755637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2756637d3847SPeter Ujfalusi 	const char *name, int max)
2757637d3847SPeter Ujfalusi {
2758f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2759637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2760637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2761637d3847SPeter Ujfalusi 	int found = 0;
2762637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2763637d3847SPeter Ujfalusi 
2764637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2765637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2766637d3847SPeter Ujfalusi 		return -EINVAL;
2767637d3847SPeter Ujfalusi 
2768637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2769637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2770637d3847SPeter Ujfalusi 			found = 1;
2771637d3847SPeter Ujfalusi 			break;
2772637d3847SPeter Ujfalusi 		}
2773637d3847SPeter Ujfalusi 	}
2774637d3847SPeter Ujfalusi 	if (found) {
2775637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2776637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2777d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2778637d3847SPeter Ujfalusi 			ret = 0;
2779637d3847SPeter Ujfalusi 		}
2780637d3847SPeter Ujfalusi 	}
2781637d3847SPeter Ujfalusi 	return ret;
2782637d3847SPeter Ujfalusi }
2783637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2784637d3847SPeter Ujfalusi 
278571d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
278671d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
278771d08516SMark Brown {
278871d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
278971d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
279071d08516SMark Brown 
279171d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
279271d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
279371d08516SMark Brown 
279471d08516SMark Brown 	return 0;
279571d08516SMark Brown }
279671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
279771d08516SMark Brown 
279871d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
279971d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
280071d08516SMark Brown {
280171d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
280271d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
280371d08516SMark Brown 	int ret;
280471d08516SMark Brown 
280571d08516SMark Brown 	if (codec->using_regmap)
280671d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
280771d08516SMark Brown 				      ucontrol->value.bytes.data,
280871d08516SMark Brown 				      params->num_regs * codec->val_bytes);
280971d08516SMark Brown 	else
281071d08516SMark Brown 		ret = -EINVAL;
281171d08516SMark Brown 
2812f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
2813f831b055SMark Brown 	if (ret == 0 && params->mask) {
2814f831b055SMark Brown 		switch (codec->val_bytes) {
2815f831b055SMark Brown 		case 1:
2816f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
2817f831b055SMark Brown 			break;
2818f831b055SMark Brown 		case 2:
2819f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
2820f831b055SMark Brown 				&= ~params->mask;
2821f831b055SMark Brown 			break;
2822f831b055SMark Brown 		case 4:
2823f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
2824f831b055SMark Brown 				&= ~params->mask;
2825f831b055SMark Brown 			break;
2826f831b055SMark Brown 		default:
2827f831b055SMark Brown 			return -EINVAL;
2828f831b055SMark Brown 		}
2829f831b055SMark Brown 	}
2830f831b055SMark Brown 
283171d08516SMark Brown 	return ret;
283271d08516SMark Brown }
283371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
283471d08516SMark Brown 
283571d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
283671d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
283771d08516SMark Brown {
283871d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
283971d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2840f831b055SMark Brown 	int ret, len;
2841f831b055SMark Brown 	unsigned int val;
2842f831b055SMark Brown 	void *data;
284371d08516SMark Brown 
2844f831b055SMark Brown 	if (!codec->using_regmap)
2845f831b055SMark Brown 		return -EINVAL;
2846f831b055SMark Brown 
2847f831b055SMark Brown 	data = ucontrol->value.bytes.data;
2848f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
2849f831b055SMark Brown 
2850f831b055SMark Brown 	/*
2851f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
2852f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
2853f831b055SMark Brown 	 * copy.
2854f831b055SMark Brown 	 */
2855f831b055SMark Brown 	if (params->mask) {
2856f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
2857f831b055SMark Brown 		if (ret != 0)
2858f831b055SMark Brown 			return ret;
2859f831b055SMark Brown 
2860f831b055SMark Brown 		val &= params->mask;
2861f831b055SMark Brown 
2862f831b055SMark Brown 		data = kmemdup(data, len, GFP_KERNEL);
2863f831b055SMark Brown 		if (!data)
2864f831b055SMark Brown 			return -ENOMEM;
2865f831b055SMark Brown 
2866f831b055SMark Brown 		switch (codec->val_bytes) {
2867f831b055SMark Brown 		case 1:
2868f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
2869f831b055SMark Brown 			((u8 *)data)[0] |= val;
2870f831b055SMark Brown 			break;
2871f831b055SMark Brown 		case 2:
2872f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2873f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
2874f831b055SMark Brown 			break;
2875f831b055SMark Brown 		case 4:
2876f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2877f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
2878f831b055SMark Brown 			break;
2879f831b055SMark Brown 		default:
2880f831b055SMark Brown 			return -EINVAL;
2881f831b055SMark Brown 		}
2882f831b055SMark Brown 	}
2883f831b055SMark Brown 
288471d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
2885f831b055SMark Brown 			       data, len);
2886f831b055SMark Brown 
2887f831b055SMark Brown 	if (params->mask)
2888f831b055SMark Brown 		kfree(data);
288971d08516SMark Brown 
289071d08516SMark Brown 	return ret;
289171d08516SMark Brown }
289271d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
289371d08516SMark Brown 
2894b6f4bb38Sapatard@mandriva.com /**
28954183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
28964183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
28974183eed2SKristoffer KARLSSON  * @uinfo: control element information
28984183eed2SKristoffer KARLSSON  *
28994183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
29004183eed2SKristoffer KARLSSON  * span multiple codec registers which together
29014183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
29024183eed2SKristoffer KARLSSON  *
29034183eed2SKristoffer KARLSSON  * Returns 0 for success.
29044183eed2SKristoffer KARLSSON  */
29054183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
29064183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
29074183eed2SKristoffer KARLSSON {
29084183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
29094183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
29104183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29114183eed2SKristoffer KARLSSON 	uinfo->count = 1;
29124183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
29134183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
29144183eed2SKristoffer KARLSSON 
29154183eed2SKristoffer KARLSSON 	return 0;
29164183eed2SKristoffer KARLSSON }
29174183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
29184183eed2SKristoffer KARLSSON 
29194183eed2SKristoffer KARLSSON /**
29204183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
29214183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
29224183eed2SKristoffer KARLSSON  * @ucontrol: control element information
29234183eed2SKristoffer KARLSSON  *
29244183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
29254183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
29264183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
29274183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
29284183eed2SKristoffer KARLSSON  * across the multiple codec registers.
29294183eed2SKristoffer KARLSSON  *
29304183eed2SKristoffer KARLSSON  * Returns 0 for success.
29314183eed2SKristoffer KARLSSON  */
29324183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
29334183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
29344183eed2SKristoffer KARLSSON {
29354183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
29364183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
29374183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29384183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
29394183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
29404183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
29414183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
29424183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
29434183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
29444183eed2SKristoffer KARLSSON 	long min = mc->min;
29454183eed2SKristoffer KARLSSON 	long max = mc->max;
29464183eed2SKristoffer KARLSSON 	long val = 0;
29474183eed2SKristoffer KARLSSON 	unsigned long regval;
29484183eed2SKristoffer KARLSSON 	unsigned int i;
29494183eed2SKristoffer KARLSSON 
29504183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
29514183eed2SKristoffer KARLSSON 		regval = snd_soc_read(codec, regbase+i) & regwmask;
29524183eed2SKristoffer KARLSSON 		val |= regval << (regwshift*(regcount-i-1));
29534183eed2SKristoffer KARLSSON 	}
29544183eed2SKristoffer KARLSSON 	val &= mask;
29554183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
29564183eed2SKristoffer KARLSSON 		val |= ~mask;
29574183eed2SKristoffer KARLSSON 	if (invert)
29584183eed2SKristoffer KARLSSON 		val = max - val;
29594183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
29604183eed2SKristoffer KARLSSON 
29614183eed2SKristoffer KARLSSON 	return 0;
29624183eed2SKristoffer KARLSSON }
29634183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
29644183eed2SKristoffer KARLSSON 
29654183eed2SKristoffer KARLSSON /**
29664183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
29674183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
29684183eed2SKristoffer KARLSSON  * @ucontrol: control element information
29694183eed2SKristoffer KARLSSON  *
29704183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
29714183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
29724183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
29734183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
29744183eed2SKristoffer KARLSSON  * across the multiple codec registers.
29754183eed2SKristoffer KARLSSON  *
29764183eed2SKristoffer KARLSSON  * Returns 0 for success.
29774183eed2SKristoffer KARLSSON  */
29784183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
29794183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
29804183eed2SKristoffer KARLSSON {
29814183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
29824183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
29834183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29844183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
29854183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
29864183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
29874183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
29884183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
29894183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
29904183eed2SKristoffer KARLSSON 	long min = mc->min;
29914183eed2SKristoffer KARLSSON 	long max = mc->max;
29924183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
29934183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
29944183eed2SKristoffer KARLSSON 	int err;
29954183eed2SKristoffer KARLSSON 
29964183eed2SKristoffer KARLSSON 	if (invert)
29974183eed2SKristoffer KARLSSON 		val = max - val;
29984183eed2SKristoffer KARLSSON 	val &= mask;
29994183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
30004183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
30014183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
30024183eed2SKristoffer KARLSSON 		err = snd_soc_update_bits_locked(codec, regbase+i,
30034183eed2SKristoffer KARLSSON 				regmask, regval);
30044183eed2SKristoffer KARLSSON 		if (err < 0)
30054183eed2SKristoffer KARLSSON 			return err;
30064183eed2SKristoffer KARLSSON 	}
30074183eed2SKristoffer KARLSSON 
30084183eed2SKristoffer KARLSSON 	return 0;
30094183eed2SKristoffer KARLSSON }
30104183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
30114183eed2SKristoffer KARLSSON 
30124183eed2SKristoffer KARLSSON /**
3013*dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3014*dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3015*dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3016*dd7b10b3SKristoffer KARLSSON  *
3017*dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3018*dd7b10b3SKristoffer KARLSSON  *
3019*dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3020*dd7b10b3SKristoffer KARLSSON  */
3021*dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3022*dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3023*dd7b10b3SKristoffer KARLSSON {
3024*dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3025*dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3026*dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3027*dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3028*dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3029*dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3030*dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3031*dd7b10b3SKristoffer KARLSSON 	unsigned int val = snd_soc_read(codec, reg) & mask;
3032*dd7b10b3SKristoffer KARLSSON 
3033*dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3034*dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3035*dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3036*dd7b10b3SKristoffer KARLSSON 
3037*dd7b10b3SKristoffer KARLSSON 	return 0;
3038*dd7b10b3SKristoffer KARLSSON }
3039*dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3040*dd7b10b3SKristoffer KARLSSON 
3041*dd7b10b3SKristoffer KARLSSON /**
3042*dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3043*dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3044*dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3045*dd7b10b3SKristoffer KARLSSON  *
3046*dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3047*dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3048*dd7b10b3SKristoffer KARLSSON  *
3049*dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3050*dd7b10b3SKristoffer KARLSSON  */
3051*dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3052*dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3053*dd7b10b3SKristoffer KARLSSON {
3054*dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3055*dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3056*dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3057*dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3058*dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3059*dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3060*dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3061*dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3062*dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3063*dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3064*dd7b10b3SKristoffer KARLSSON 	int err;
3065*dd7b10b3SKristoffer KARLSSON 
3066*dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3067*dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3068*dd7b10b3SKristoffer KARLSSON 		return err;
3069*dd7b10b3SKristoffer KARLSSON 
3070*dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3071*dd7b10b3SKristoffer KARLSSON 	return err;
3072*dd7b10b3SKristoffer KARLSSON }
3073*dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3074*dd7b10b3SKristoffer KARLSSON 
3075*dd7b10b3SKristoffer KARLSSON /**
30768c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
30778c6529dbSLiam Girdwood  * @dai: DAI
30788c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
30798c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
30808c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
30818c6529dbSLiam Girdwood  *
30828c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
30838c6529dbSLiam Girdwood  */
30848c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
30858c6529dbSLiam Girdwood 	unsigned int freq, int dir)
30868c6529dbSLiam Girdwood {
3087f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3088f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3089ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3090da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3091ec4ee52aSMark Brown 						      freq, dir);
30928c6529dbSLiam Girdwood 	else
30938c6529dbSLiam Girdwood 		return -EINVAL;
30948c6529dbSLiam Girdwood }
30958c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
30968c6529dbSLiam Girdwood 
30978c6529dbSLiam Girdwood /**
3098ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3099ec4ee52aSMark Brown  * @codec: CODEC
3100ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3101da1c6ea6SMark Brown  * @source: Source for the clock
3102ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3103ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3104ec4ee52aSMark Brown  *
3105ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3106ec4ee52aSMark Brown  */
3107ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3108da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3109ec4ee52aSMark Brown {
3110ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3111da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3112da1c6ea6SMark Brown 						 freq, dir);
3113ec4ee52aSMark Brown 	else
3114ec4ee52aSMark Brown 		return -EINVAL;
3115ec4ee52aSMark Brown }
3116ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3117ec4ee52aSMark Brown 
3118ec4ee52aSMark Brown /**
31198c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
31208c6529dbSLiam Girdwood  * @dai: DAI
3121ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
31228c6529dbSLiam Girdwood  * @div: new clock divisor.
31238c6529dbSLiam Girdwood  *
31248c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
31258c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
31268c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
31278c6529dbSLiam Girdwood  */
31288c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
31298c6529dbSLiam Girdwood 	int div_id, int div)
31308c6529dbSLiam Girdwood {
3131f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3132f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
31338c6529dbSLiam Girdwood 	else
31348c6529dbSLiam Girdwood 		return -EINVAL;
31358c6529dbSLiam Girdwood }
31368c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
31378c6529dbSLiam Girdwood 
31388c6529dbSLiam Girdwood /**
31398c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
31408c6529dbSLiam Girdwood  * @dai: DAI
31418c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
314285488037SMark Brown  * @source: DAI specific source for the PLL
31438c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
31448c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
31458c6529dbSLiam Girdwood  *
31468c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
31478c6529dbSLiam Girdwood  */
314885488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
314985488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
31508c6529dbSLiam Girdwood {
3151f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3152f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
315385488037SMark Brown 					 freq_in, freq_out);
3154ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3155ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3156ec4ee52aSMark Brown 						   freq_in, freq_out);
31578c6529dbSLiam Girdwood 	else
31588c6529dbSLiam Girdwood 		return -EINVAL;
31598c6529dbSLiam Girdwood }
31608c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
31618c6529dbSLiam Girdwood 
3162ec4ee52aSMark Brown /*
3163ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3164ec4ee52aSMark Brown  * @codec: CODEC
3165ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3166ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3167ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3168ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3169ec4ee52aSMark Brown  *
3170ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3171ec4ee52aSMark Brown  */
3172ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3173ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3174ec4ee52aSMark Brown {
3175ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3176ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3177ec4ee52aSMark Brown 					      freq_in, freq_out);
3178ec4ee52aSMark Brown 	else
3179ec4ee52aSMark Brown 		return -EINVAL;
3180ec4ee52aSMark Brown }
3181ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3182ec4ee52aSMark Brown 
31838c6529dbSLiam Girdwood /**
31848c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
31858c6529dbSLiam Girdwood  * @dai: DAI
31868c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
31878c6529dbSLiam Girdwood  *
31888c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
31898c6529dbSLiam Girdwood  */
31908c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
31918c6529dbSLiam Girdwood {
31925e4ba569SShawn Guo 	if (dai->driver == NULL)
31938c6529dbSLiam Girdwood 		return -EINVAL;
31945e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
31955e4ba569SShawn Guo 		return -ENOTSUPP;
31965e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
31978c6529dbSLiam Girdwood }
31988c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
31998c6529dbSLiam Girdwood 
32008c6529dbSLiam Girdwood /**
32018c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
32028c6529dbSLiam Girdwood  * @dai: DAI
3203a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3204a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
32058c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3206a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
32078c6529dbSLiam Girdwood  *
32088c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
32098c6529dbSLiam Girdwood  * specific.
32108c6529dbSLiam Girdwood  */
32118c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3212a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
32138c6529dbSLiam Girdwood {
3214f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3215f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3216a5479e38SDaniel Ribeiro 				slots, slot_width);
32178c6529dbSLiam Girdwood 	else
32188c6529dbSLiam Girdwood 		return -EINVAL;
32198c6529dbSLiam Girdwood }
32208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
32218c6529dbSLiam Girdwood 
32228c6529dbSLiam Girdwood /**
3223472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3224472df3cbSBarry Song  * @dai: DAI
3225472df3cbSBarry Song  * @tx_num: how many TX channels
3226472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3227472df3cbSBarry Song  *           0~num-1 uses
3228472df3cbSBarry Song  * @rx_num: how many RX channels
3229472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3230472df3cbSBarry Song  *           0~num-1 uses
3231472df3cbSBarry Song  *
3232472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3233472df3cbSBarry Song  */
3234472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3235472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3236472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3237472df3cbSBarry Song {
3238f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3239f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3240472df3cbSBarry Song 			rx_num, rx_slot);
3241472df3cbSBarry Song 	else
3242472df3cbSBarry Song 		return -EINVAL;
3243472df3cbSBarry Song }
3244472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3245472df3cbSBarry Song 
3246472df3cbSBarry Song /**
32478c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
32488c6529dbSLiam Girdwood  * @dai: DAI
32498c6529dbSLiam Girdwood  * @tristate: tristate enable
32508c6529dbSLiam Girdwood  *
32518c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
32528c6529dbSLiam Girdwood  */
32538c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
32548c6529dbSLiam Girdwood {
3255f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3256f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
32578c6529dbSLiam Girdwood 	else
32588c6529dbSLiam Girdwood 		return -EINVAL;
32598c6529dbSLiam Girdwood }
32608c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
32618c6529dbSLiam Girdwood 
32628c6529dbSLiam Girdwood /**
32638c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
32648c6529dbSLiam Girdwood  * @dai: DAI
32658c6529dbSLiam Girdwood  * @mute: mute enable
32668c6529dbSLiam Girdwood  *
32678c6529dbSLiam Girdwood  * Mutes the DAI DAC.
32688c6529dbSLiam Girdwood  */
32698c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
32708c6529dbSLiam Girdwood {
3271f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3272f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
32738c6529dbSLiam Girdwood 	else
327404570c62SMark Brown 		return -ENOTSUPP;
32758c6529dbSLiam Girdwood }
32768c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
32778c6529dbSLiam Girdwood 
3278c5af3a2eSMark Brown /**
3279c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3280c5af3a2eSMark Brown  *
3281ac11a2b3SMark Brown  * @card: Card to register
3282c5af3a2eSMark Brown  *
3283c5af3a2eSMark Brown  */
328470a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3285c5af3a2eSMark Brown {
3286b19e6e7bSMark Brown 	int i, ret;
3287f0fba2adSLiam Girdwood 
3288c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3289c5af3a2eSMark Brown 		return -EINVAL;
3290c5af3a2eSMark Brown 
32915a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
32925a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
32935a504963SStephen Warren 
32945a504963SStephen Warren 		/*
32955a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
32965a504963SStephen Warren 		 * not both or neither.
32975a504963SStephen Warren 		 */
32985a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
32995a504963SStephen Warren 			dev_err(card->dev,
33003b09bb82SLiam Girdwood 				"Neither/both codec name/of_node are set for %s\n",
33013b09bb82SLiam Girdwood 				link->name);
33025a504963SStephen Warren 			return -EINVAL;
33035a504963SStephen Warren 		}
33045a504963SStephen Warren 
33055a504963SStephen Warren 		/*
33065a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
33075a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
33085a504963SStephen Warren 		 */
33095a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
33105a504963SStephen Warren 			dev_err(card->dev,
33113b09bb82SLiam Girdwood 				"Both platform name/of_node are set for %s\n", link->name);
33125a504963SStephen Warren 			return -EINVAL;
33135a504963SStephen Warren 		}
33145a504963SStephen Warren 
33155a504963SStephen Warren 		/*
33165a504963SStephen Warren 		 * CPU DAI must be specified by 1 of name or OF node,
33175a504963SStephen Warren 		 * not both or neither.
33185a504963SStephen Warren 		 */
33195a504963SStephen Warren 		if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
33205a504963SStephen Warren 			dev_err(card->dev,
33213b09bb82SLiam Girdwood 				"Neither/both cpu_dai name/of_node are set for %s\n",
33223b09bb82SLiam Girdwood 				link->name);
33235a504963SStephen Warren 			return -EINVAL;
33245a504963SStephen Warren 		}
33255a504963SStephen Warren 	}
33265a504963SStephen Warren 
3327ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3328ed77cc12SMark Brown 
3329111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3330111c6419SStephen Warren 
3331150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3332150dd2f8SVinod Koul 
3333181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3334181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
33352eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3336f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3337f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3338f0fba2adSLiam Girdwood 		return -ENOMEM;
33392eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3340f0fba2adSLiam Girdwood 
3341f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3342f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3343f0fba2adSLiam Girdwood 
3344c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3345db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3346c5af3a2eSMark Brown 	card->instantiated = 0;
3347f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3348a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3349c5af3a2eSMark Brown 
3350b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3351b19e6e7bSMark Brown 	if (ret != 0)
3352b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3353c5af3a2eSMark Brown 
3354b19e6e7bSMark Brown 	return ret;
3355c5af3a2eSMark Brown }
335670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3357c5af3a2eSMark Brown 
3358c5af3a2eSMark Brown /**
3359c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3360c5af3a2eSMark Brown  *
3361ac11a2b3SMark Brown  * @card: Card to unregister
3362c5af3a2eSMark Brown  *
3363c5af3a2eSMark Brown  */
336470a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3365c5af3a2eSMark Brown {
3366b0e26485SVinod Koul 	if (card->instantiated)
3367b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3368c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3369c5af3a2eSMark Brown 
3370c5af3a2eSMark Brown 	return 0;
3371c5af3a2eSMark Brown }
337270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3373c5af3a2eSMark Brown 
3374f0fba2adSLiam Girdwood /*
3375f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3376f0fba2adSLiam Girdwood  * and sanitizing names.
3377f0fba2adSLiam Girdwood  */
33780b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3379f0fba2adSLiam Girdwood {
3380f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3381f0fba2adSLiam Girdwood 	int id1, id2;
3382f0fba2adSLiam Girdwood 
3383f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3384f0fba2adSLiam Girdwood 		return NULL;
3385f0fba2adSLiam Girdwood 
338658818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3387f0fba2adSLiam Girdwood 
3388f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3389f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3390f0fba2adSLiam Girdwood 	if (found) {
3391f0fba2adSLiam Girdwood 		/* get ID */
3392f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3393f0fba2adSLiam Girdwood 
3394f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3395f0fba2adSLiam Girdwood 			if (*id == -1)
3396f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3397f0fba2adSLiam Girdwood 		}
3398f0fba2adSLiam Girdwood 
3399f0fba2adSLiam Girdwood 	} else {
3400f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3401f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3402f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3403f0fba2adSLiam Girdwood 
3404f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
340505899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3406f0fba2adSLiam Girdwood 
3407f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3408f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
340958818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3410f0fba2adSLiam Girdwood 		} else
3411f0fba2adSLiam Girdwood 			*id = 0;
3412f0fba2adSLiam Girdwood 	}
3413f0fba2adSLiam Girdwood 
3414f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3415f0fba2adSLiam Girdwood }
3416f0fba2adSLiam Girdwood 
3417f0fba2adSLiam Girdwood /*
3418f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3419f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3420f0fba2adSLiam Girdwood  */
3421f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3422f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3423f0fba2adSLiam Girdwood {
3424f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
34250837fc62SFabio Estevam 		pr_err("asoc: error - multiple DAI %s registered with no name\n",
3426f0fba2adSLiam Girdwood 				dev_name(dev));
3427f0fba2adSLiam Girdwood 		return NULL;
3428f0fba2adSLiam Girdwood 	}
3429f0fba2adSLiam Girdwood 
3430f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3431f0fba2adSLiam Girdwood }
3432f0fba2adSLiam Girdwood 
34339115171aSMark Brown /**
34349115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
34359115171aSMark Brown  *
3436ac11a2b3SMark Brown  * @dai: DAI to register
34379115171aSMark Brown  */
3438f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3439f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
34409115171aSMark Brown {
3441054880feSMark Brown 	struct snd_soc_codec *codec;
3442f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
34439115171aSMark Brown 
3444f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
34459115171aSMark Brown 
3446f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3447f0fba2adSLiam Girdwood 	if (dai == NULL)
3448f0fba2adSLiam Girdwood 		return -ENOMEM;
34496335d055SEric Miao 
3450f0fba2adSLiam Girdwood 	/* create DAI component name */
3451f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3452f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3453f0fba2adSLiam Girdwood 		kfree(dai);
3454f0fba2adSLiam Girdwood 		return -ENOMEM;
3455f0fba2adSLiam Girdwood 	}
3456f0fba2adSLiam Girdwood 
3457f0fba2adSLiam Girdwood 	dai->dev = dev;
3458f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3459be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3460f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3461f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
34629115171aSMark Brown 
34639115171aSMark Brown 	mutex_lock(&client_mutex);
3464054880feSMark Brown 
3465054880feSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3466054880feSMark Brown 		if (codec->dev == dev) {
3467054880feSMark Brown 			dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3468054880feSMark Brown 				dai->name, codec->name);
3469054880feSMark Brown 			dai->codec = codec;
3470054880feSMark Brown 			break;
3471054880feSMark Brown 		}
3472054880feSMark Brown 	}
3473054880feSMark Brown 
34749115171aSMark Brown 	list_add(&dai->list, &dai_list);
3475054880feSMark Brown 
34769115171aSMark Brown 	mutex_unlock(&client_mutex);
34779115171aSMark Brown 
34789115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
34799115171aSMark Brown 
34809115171aSMark Brown 	return 0;
34819115171aSMark Brown }
34829115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
34839115171aSMark Brown 
34849115171aSMark Brown /**
34859115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
34869115171aSMark Brown  *
3487ac11a2b3SMark Brown  * @dai: DAI to unregister
34889115171aSMark Brown  */
3489f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
34909115171aSMark Brown {
3491f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3492f0fba2adSLiam Girdwood 
3493f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3494f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3495f0fba2adSLiam Girdwood 			goto found;
3496f0fba2adSLiam Girdwood 	}
3497f0fba2adSLiam Girdwood 	return;
3498f0fba2adSLiam Girdwood 
3499f0fba2adSLiam Girdwood found:
35009115171aSMark Brown 	mutex_lock(&client_mutex);
35019115171aSMark Brown 	list_del(&dai->list);
35029115171aSMark Brown 	mutex_unlock(&client_mutex);
35039115171aSMark Brown 
35049115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3505f0fba2adSLiam Girdwood 	kfree(dai->name);
3506f0fba2adSLiam Girdwood 	kfree(dai);
35079115171aSMark Brown }
35089115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
35099115171aSMark Brown 
35109115171aSMark Brown /**
35119115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
35129115171aSMark Brown  *
3513ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3514ac11a2b3SMark Brown  * @count: Number of DAIs
35159115171aSMark Brown  */
3516f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3517f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
35189115171aSMark Brown {
3519054880feSMark Brown 	struct snd_soc_codec *codec;
3520f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3521f0fba2adSLiam Girdwood 	int i, ret = 0;
3522f0fba2adSLiam Girdwood 
3523720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
35249115171aSMark Brown 
35259115171aSMark Brown 	for (i = 0; i < count; i++) {
3526f0fba2adSLiam Girdwood 
3527f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3528c46e0079SAxel Lin 		if (dai == NULL) {
3529c46e0079SAxel Lin 			ret = -ENOMEM;
3530c46e0079SAxel Lin 			goto err;
3531c46e0079SAxel Lin 		}
3532f0fba2adSLiam Girdwood 
3533f0fba2adSLiam Girdwood 		/* create DAI component name */
3534f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3535f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3536f0fba2adSLiam Girdwood 			kfree(dai);
3537f0fba2adSLiam Girdwood 			ret = -EINVAL;
35389115171aSMark Brown 			goto err;
35399115171aSMark Brown 		}
35409115171aSMark Brown 
3541f0fba2adSLiam Girdwood 		dai->dev = dev;
3542f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
35430f9141c9SMark Brown 		if (dai->driver->id)
35440f9141c9SMark Brown 			dai->id = dai->driver->id;
35450f9141c9SMark Brown 		else
35460f9141c9SMark Brown 			dai->id = i;
3547be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3548f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3549f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3550f0fba2adSLiam Girdwood 
3551f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3552054880feSMark Brown 
3553054880feSMark Brown 		list_for_each_entry(codec, &codec_list, list) {
3554054880feSMark Brown 			if (codec->dev == dev) {
3555054880feSMark Brown 				dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3556054880feSMark Brown 					dai->name, codec->name);
3557054880feSMark Brown 				dai->codec = codec;
3558054880feSMark Brown 				break;
3559054880feSMark Brown 			}
3560054880feSMark Brown 		}
3561054880feSMark Brown 
3562f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3563054880feSMark Brown 
3564f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3565f0fba2adSLiam Girdwood 
3566f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3567f0fba2adSLiam Girdwood 	}
3568f0fba2adSLiam Girdwood 
35699115171aSMark Brown 	return 0;
35709115171aSMark Brown 
35719115171aSMark Brown err:
35729115171aSMark Brown 	for (i--; i >= 0; i--)
3573f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
35749115171aSMark Brown 
35759115171aSMark Brown 	return ret;
35769115171aSMark Brown }
35779115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
35789115171aSMark Brown 
35799115171aSMark Brown /**
35809115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
35819115171aSMark Brown  *
3582ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3583ac11a2b3SMark Brown  * @count: Number of DAIs
35849115171aSMark Brown  */
3585f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
35869115171aSMark Brown {
35879115171aSMark Brown 	int i;
35889115171aSMark Brown 
35899115171aSMark Brown 	for (i = 0; i < count; i++)
3590f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
35919115171aSMark Brown }
35929115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
35939115171aSMark Brown 
359412a48a8cSMark Brown /**
359512a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
359612a48a8cSMark Brown  *
3597ac11a2b3SMark Brown  * @platform: platform to register
359812a48a8cSMark Brown  */
3599f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3600f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
360112a48a8cSMark Brown {
3602f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
360312a48a8cSMark Brown 
3604f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3605f0fba2adSLiam Girdwood 
3606f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3607f0fba2adSLiam Girdwood 	if (platform == NULL)
3608f0fba2adSLiam Girdwood 		return -ENOMEM;
3609f0fba2adSLiam Girdwood 
3610f0fba2adSLiam Girdwood 	/* create platform component name */
3611f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3612f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3613f0fba2adSLiam Girdwood 		kfree(platform);
3614f0fba2adSLiam Girdwood 		return -ENOMEM;
3615f0fba2adSLiam Girdwood 	}
3616f0fba2adSLiam Girdwood 
3617f0fba2adSLiam Girdwood 	platform->dev = dev;
3618f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3619b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3620b7950641SLiam Girdwood 	platform->dapm.platform = platform;
362164a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3622cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
362312a48a8cSMark Brown 
362412a48a8cSMark Brown 	mutex_lock(&client_mutex);
362512a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
362612a48a8cSMark Brown 	mutex_unlock(&client_mutex);
362712a48a8cSMark Brown 
362812a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
362912a48a8cSMark Brown 
363012a48a8cSMark Brown 	return 0;
363112a48a8cSMark Brown }
363212a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
363312a48a8cSMark Brown 
363412a48a8cSMark Brown /**
363512a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
363612a48a8cSMark Brown  *
3637ac11a2b3SMark Brown  * @platform: platform to unregister
363812a48a8cSMark Brown  */
3639f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
364012a48a8cSMark Brown {
3641f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3642f0fba2adSLiam Girdwood 
3643f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3644f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3645f0fba2adSLiam Girdwood 			goto found;
3646f0fba2adSLiam Girdwood 	}
3647f0fba2adSLiam Girdwood 	return;
3648f0fba2adSLiam Girdwood 
3649f0fba2adSLiam Girdwood found:
365012a48a8cSMark Brown 	mutex_lock(&client_mutex);
365112a48a8cSMark Brown 	list_del(&platform->list);
365212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
365312a48a8cSMark Brown 
365412a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3655f0fba2adSLiam Girdwood 	kfree(platform->name);
3656f0fba2adSLiam Girdwood 	kfree(platform);
365712a48a8cSMark Brown }
365812a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
365912a48a8cSMark Brown 
3660151ab22cSMark Brown static u64 codec_format_map[] = {
3661151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3662151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3663151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3664151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3665151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3666151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3667151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3668151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3669151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3670151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3671151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3672151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3673151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3674151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3675151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3676151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3677151ab22cSMark Brown };
3678151ab22cSMark Brown 
3679151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3680151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3681151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3682151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3683151ab22cSMark Brown  */
3684151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3685151ab22cSMark Brown {
3686151ab22cSMark Brown 	int i;
3687151ab22cSMark Brown 
3688151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3689151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3690151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3691151ab22cSMark Brown }
3692151ab22cSMark Brown 
36930d0cf00aSMark Brown /**
36940d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
36950d0cf00aSMark Brown  *
3696ac11a2b3SMark Brown  * @codec: codec to register
36970d0cf00aSMark Brown  */
3698f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3699001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3700001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3701001ae4c0SMark Brown 			   int num_dai)
37020d0cf00aSMark Brown {
37033335ddcaSDimitris Papastamos 	size_t reg_size;
3704f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3705f0fba2adSLiam Girdwood 	int ret, i;
3706151ab22cSMark Brown 
3707f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
37080d0cf00aSMark Brown 
3709f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3710f0fba2adSLiam Girdwood 	if (codec == NULL)
3711f0fba2adSLiam Girdwood 		return -ENOMEM;
37120d0cf00aSMark Brown 
3713f0fba2adSLiam Girdwood 	/* create CODEC component name */
3714f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3715f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3716f0fba2adSLiam Girdwood 		kfree(codec);
3717f0fba2adSLiam Girdwood 		return -ENOMEM;
3718151ab22cSMark Brown 	}
3719151ab22cSMark Brown 
372023bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
372123bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
372223bbce34SDimitris Papastamos 	else
372323bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
372423bbce34SDimitris Papastamos 
3725c3acec26SMark Brown 	codec->write = codec_drv->write;
3726c3acec26SMark Brown 	codec->read = codec_drv->read;
37271500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
37281500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
37298020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
37305124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3731ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3732ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3733ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3734474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
373564a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3736f0fba2adSLiam Girdwood 	codec->dev = dev;
3737f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3738f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3739f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3740f0fba2adSLiam Girdwood 
37417a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
37427a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
37433335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3744aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
37453335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
37463335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
37473335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
37483335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
37493335ddcaSDimitris Papastamos 		 * the cache.
37503335ddcaSDimitris Papastamos 		 */
37512aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
37523335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
37533335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
37543335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
37553335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3756fdf0f54dSDimitris Papastamos 				goto fail;
37577a30a3dbSDimitris Papastamos 			}
37587a30a3dbSDimitris Papastamos 		}
37592aa86323SDimitris Papastamos 	}
37607a30a3dbSDimitris Papastamos 
37611500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
37621500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
37631500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
37641500b7b5SDimitris Papastamos 		if (!codec->readable_register)
37651500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
37668020454cSDimitris Papastamos 		if (!codec->writable_register)
37678020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
37681500b7b5SDimitris Papastamos 	}
37691500b7b5SDimitris Papastamos 
3770f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3771f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3772f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3773f0fba2adSLiam Girdwood 	}
3774f0fba2adSLiam Girdwood 
3775054880feSMark Brown 	mutex_lock(&client_mutex);
3776054880feSMark Brown 	list_add(&codec->list, &codec_list);
3777054880feSMark Brown 	mutex_unlock(&client_mutex);
3778054880feSMark Brown 
377926b01ccdSMark Brown 	/* register any DAIs */
378026b01ccdSMark Brown 	if (num_dai) {
3781f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3782f0fba2adSLiam Girdwood 		if (ret < 0)
3783054880feSMark Brown 			dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3784054880feSMark Brown 				ret);
378526b01ccdSMark Brown 	}
3786f0fba2adSLiam Girdwood 
37870d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
37880d0cf00aSMark Brown 	return 0;
3789f0fba2adSLiam Girdwood 
3790fdf0f54dSDimitris Papastamos fail:
37913335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
37923335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3793f0fba2adSLiam Girdwood 	kfree(codec->name);
3794f0fba2adSLiam Girdwood 	kfree(codec);
3795f0fba2adSLiam Girdwood 	return ret;
37960d0cf00aSMark Brown }
37970d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
37980d0cf00aSMark Brown 
37990d0cf00aSMark Brown /**
38000d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
38010d0cf00aSMark Brown  *
3802ac11a2b3SMark Brown  * @codec: codec to unregister
38030d0cf00aSMark Brown  */
3804f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
38050d0cf00aSMark Brown {
3806f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3807f0fba2adSLiam Girdwood 	int i;
3808f0fba2adSLiam Girdwood 
3809f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3810f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3811f0fba2adSLiam Girdwood 			goto found;
3812f0fba2adSLiam Girdwood 	}
3813f0fba2adSLiam Girdwood 	return;
3814f0fba2adSLiam Girdwood 
3815f0fba2adSLiam Girdwood found:
381626b01ccdSMark Brown 	if (codec->num_dai)
3817f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3818f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3819f0fba2adSLiam Girdwood 
38200d0cf00aSMark Brown 	mutex_lock(&client_mutex);
38210d0cf00aSMark Brown 	list_del(&codec->list);
38220d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
38230d0cf00aSMark Brown 
38240d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3825f0fba2adSLiam Girdwood 
38267a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
38273335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
38281aafcd4dSDimitris Papastamos 	kfree(codec->name);
3829f0fba2adSLiam Girdwood 	kfree(codec);
38300d0cf00aSMark Brown }
38310d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
38320d0cf00aSMark Brown 
3833bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3834bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3835bec4fa05SStephen Warren 			       const char *propname)
3836bec4fa05SStephen Warren {
3837bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
3838bec4fa05SStephen Warren 	int ret;
3839bec4fa05SStephen Warren 
3840bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3841bec4fa05SStephen Warren 	/*
3842bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3843bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3844bec4fa05SStephen Warren 	 * snd_soc_register_card.
3845bec4fa05SStephen Warren 	 */
3846bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3847bec4fa05SStephen Warren 		dev_err(card->dev,
3848bec4fa05SStephen Warren 			"Property '%s' could not be read: %d\n",
3849bec4fa05SStephen Warren 			propname, ret);
3850bec4fa05SStephen Warren 		return ret;
3851bec4fa05SStephen Warren 	}
3852bec4fa05SStephen Warren 
3853bec4fa05SStephen Warren 	return 0;
3854bec4fa05SStephen Warren }
3855bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3856bec4fa05SStephen Warren 
3857a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3858a4a54dd5SStephen Warren 				   const char *propname)
3859a4a54dd5SStephen Warren {
3860a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3861a4a54dd5SStephen Warren 	int num_routes;
3862a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3863a4a54dd5SStephen Warren 	int i, ret;
3864a4a54dd5SStephen Warren 
3865a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3866a4a54dd5SStephen Warren 	if (num_routes & 1) {
3867a4a54dd5SStephen Warren 		dev_err(card->dev,
3868a4a54dd5SStephen Warren 			"Property '%s's length is not even\n",
3869a4a54dd5SStephen Warren 			propname);
3870a4a54dd5SStephen Warren 		return -EINVAL;
3871a4a54dd5SStephen Warren 	}
3872a4a54dd5SStephen Warren 	num_routes /= 2;
3873a4a54dd5SStephen Warren 	if (!num_routes) {
3874a4a54dd5SStephen Warren 		dev_err(card->dev,
3875a4a54dd5SStephen Warren 			"Property '%s's length is zero\n",
3876a4a54dd5SStephen Warren 			propname);
3877a4a54dd5SStephen Warren 		return -EINVAL;
3878a4a54dd5SStephen Warren 	}
3879a4a54dd5SStephen Warren 
3880a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3881a4a54dd5SStephen Warren 			      GFP_KERNEL);
3882a4a54dd5SStephen Warren 	if (!routes) {
3883a4a54dd5SStephen Warren 		dev_err(card->dev,
3884a4a54dd5SStephen Warren 			"Could not allocate DAPM route table\n");
3885a4a54dd5SStephen Warren 		return -EINVAL;
3886a4a54dd5SStephen Warren 	}
3887a4a54dd5SStephen Warren 
3888a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3889a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3890a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
3891a4a54dd5SStephen Warren 		if (ret) {
3892a4a54dd5SStephen Warren 			dev_err(card->dev,
3893a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3894a4a54dd5SStephen Warren 				propname, 2 * i, ret);
3895a4a54dd5SStephen Warren 			return -EINVAL;
3896a4a54dd5SStephen Warren 		}
3897a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3898a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
3899a4a54dd5SStephen Warren 		if (ret) {
3900a4a54dd5SStephen Warren 			dev_err(card->dev,
3901a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3902a4a54dd5SStephen Warren 				propname, (2 * i) + 1, ret);
3903a4a54dd5SStephen Warren 			return -EINVAL;
3904a4a54dd5SStephen Warren 		}
3905a4a54dd5SStephen Warren 	}
3906a4a54dd5SStephen Warren 
3907a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
3908a4a54dd5SStephen Warren 	card->dapm_routes = routes;
3909a4a54dd5SStephen Warren 
3910a4a54dd5SStephen Warren 	return 0;
3911a4a54dd5SStephen Warren }
3912a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3913a4a54dd5SStephen Warren 
3914c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3915db2a4165SFrank Mandarino {
3916384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
39178a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
39188a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
39190837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
39208a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3921384c89e2SMark Brown 	}
3922c3c5a19aSMark Brown 
39238a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3924c3c5a19aSMark Brown 				 &codec_list_fops))
3925c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3926c3c5a19aSMark Brown 
39278a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3928f3208780SMark Brown 				 &dai_list_fops))
3929f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
393019c7ac27SMark Brown 
39318a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
393219c7ac27SMark Brown 				 &platform_list_fops))
393319c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3934384c89e2SMark Brown #endif
3935384c89e2SMark Brown 
3936fb257897SMark Brown 	snd_soc_util_init();
3937fb257897SMark Brown 
3938db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3939db2a4165SFrank Mandarino }
39404abe8e16SMark Brown module_init(snd_soc_init);
3941db2a4165SFrank Mandarino 
39427d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3943db2a4165SFrank Mandarino {
3944fb257897SMark Brown 	snd_soc_util_exit();
3945fb257897SMark Brown 
3946384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
39478a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3948384c89e2SMark Brown #endif
3949db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3950db2a4165SFrank Mandarino }
3951db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3952db2a4165SFrank Mandarino 
3953db2a4165SFrank Mandarino /* Module information */
3954d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3955db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3956db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
39578b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3958