xref: /openbmc/linux/sound/soc/soc-core.c (revision f86dcef87b771935c223334324a93c1d71f7a84c)
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>
4201d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
43db2a4165SFrank Mandarino #include <sound/initval.h>
44db2a4165SFrank Mandarino 
45a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
46a8b1d34fSMark Brown #include <trace/events/asoc.h>
47a8b1d34fSMark Brown 
48f0fba2adSLiam Girdwood #define NAME_SIZE	32
49f0fba2adSLiam Girdwood 
50db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51db2a4165SFrank Mandarino 
52384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
538a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
548a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
55384c89e2SMark Brown #endif
56384c89e2SMark Brown 
57c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
589115171aSMark Brown static LIST_HEAD(dai_list);
5912a48a8cSMark Brown static LIST_HEAD(platform_list);
600d0cf00aSMark Brown static LIST_HEAD(codec_list);
61c5af3a2eSMark Brown 
62db2a4165SFrank Mandarino /*
63db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
64db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
65db2a4165SFrank Mandarino  * between two audio tracks.
66db2a4165SFrank Mandarino  */
67db2a4165SFrank Mandarino static int pmdown_time = 5000;
68db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
69db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70db2a4165SFrank Mandarino 
712bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
722bc9a81eSDimitris Papastamos  * a particular given value */
732bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
742bc9a81eSDimitris Papastamos {
752bc9a81eSDimitris Papastamos 	int c = 0;
762bc9a81eSDimitris Papastamos 	int i;
772bc9a81eSDimitris Papastamos 
782bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
792bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
802bc9a81eSDimitris Papastamos 			break;
812bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
822bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
832bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
842bc9a81eSDimitris Papastamos 	else
852bc9a81eSDimitris Papastamos 		c /= 8;
862bc9a81eSDimitris Papastamos 	return c;
872bc9a81eSDimitris Papastamos }
882bc9a81eSDimitris Papastamos 
8913fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
9013fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9113fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9213fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
932624d5faSMark Brown {
9400b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9500b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9613fd179fSDimitris Papastamos 	int ret;
9713fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
9813fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
9913fd179fSDimitris Papastamos 
10013fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
10113fd179fSDimitris Papastamos 	 * try to abuse this function */
10213fd179fSDimitris Papastamos 	WARN_ON(len > 63);
10313fd179fSDimitris Papastamos 
10413fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10513fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10613fd179fSDimitris Papastamos 		return -EINVAL;
10713fd179fSDimitris Papastamos 
10813fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
10913fd179fSDimitris Papastamos 	if (ret < 0) {
11013fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
11113fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
11213fd179fSDimitris Papastamos 	} else {
11313fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
11413fd179fSDimitris Papastamos 	}
11513fd179fSDimitris Papastamos 
11613fd179fSDimitris Papastamos 	/* prepare the buffer */
11713fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
11813fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
11913fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
12013fd179fSDimitris Papastamos 
12113fd179fSDimitris Papastamos 	return 0;
12213fd179fSDimitris Papastamos }
12313fd179fSDimitris Papastamos 
12413fd179fSDimitris Papastamos /* codec register dump */
12513fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
12613fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
12713fd179fSDimitris Papastamos {
12813fd179fSDimitris Papastamos 	int i, step = 1;
1292bc9a81eSDimitris Papastamos 	int wordsize, regsize;
13013fd179fSDimitris Papastamos 	int len;
13113fd179fSDimitris Papastamos 	size_t total = 0;
13213fd179fSDimitris Papastamos 	loff_t p = 0;
1332bc9a81eSDimitris Papastamos 
13400b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
13500b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1362624d5faSMark Brown 
13713fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13813fd179fSDimitris Papastamos 
139f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1402624d5faSMark Brown 		return 0;
1412624d5faSMark Brown 
142f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
143f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1442624d5faSMark Brown 
145f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
146b92d150bSLars-Peter Clausen 		if (!snd_soc_codec_readable_register(codec, i))
1472624d5faSMark Brown 			continue;
148f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
149f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1502624d5faSMark Brown 							 PAGE_SIZE - count, i);
1515164d74dSMark Brown 		} else {
15213fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
15313fd179fSDimitris Papastamos 			 * entries for the default case */
15413fd179fSDimitris Papastamos 			if (p >= pos) {
15513fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1562624d5faSMark Brown 					break;
15713fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
15813fd179fSDimitris Papastamos 				total += len;
15913fd179fSDimitris Papastamos 			}
16013fd179fSDimitris Papastamos 			p += len;
16113fd179fSDimitris Papastamos 		}
1622624d5faSMark Brown 	}
1632624d5faSMark Brown 
16413fd179fSDimitris Papastamos 	total = min(total, count - 1);
1652624d5faSMark Brown 
16613fd179fSDimitris Papastamos 	return total;
1672624d5faSMark Brown }
16813fd179fSDimitris Papastamos 
1692624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1702624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1712624d5faSMark Brown {
17236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
173f0fba2adSLiam Girdwood 
17413fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1752624d5faSMark Brown }
1762624d5faSMark Brown 
1772624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1782624d5faSMark Brown 
179dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
180dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
181dbe21408SMark Brown {
18236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183dbe21408SMark Brown 
184f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
185dbe21408SMark Brown }
186dbe21408SMark Brown 
187dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
188dbe21408SMark Brown 			       struct device_attribute *attr,
189dbe21408SMark Brown 			       const char *buf, size_t count)
190dbe21408SMark Brown {
19136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
192c593b520SMark Brown 	int ret;
193dbe21408SMark Brown 
194c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195c593b520SMark Brown 	if (ret)
196c593b520SMark Brown 		return ret;
197dbe21408SMark Brown 
198dbe21408SMark Brown 	return count;
199dbe21408SMark Brown }
200dbe21408SMark Brown 
201dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202dbe21408SMark Brown 
2032624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2042624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2052624d5faSMark Brown 				   size_t count, loff_t *ppos)
2062624d5faSMark Brown {
2072624d5faSMark Brown 	ssize_t ret;
2082624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
20913fd179fSDimitris Papastamos 	char *buf;
21013fd179fSDimitris Papastamos 
21113fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
21213fd179fSDimitris Papastamos 		return -EINVAL;
21313fd179fSDimitris Papastamos 
21413fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2152624d5faSMark Brown 	if (!buf)
2162624d5faSMark Brown 		return -ENOMEM;
21713fd179fSDimitris Papastamos 
21813fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
21913fd179fSDimitris Papastamos 	if (ret >= 0) {
22013fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
22113fd179fSDimitris Papastamos 			kfree(buf);
22213fd179fSDimitris Papastamos 			return -EFAULT;
22313fd179fSDimitris Papastamos 		}
22413fd179fSDimitris Papastamos 		*ppos += ret;
22513fd179fSDimitris Papastamos 	}
22613fd179fSDimitris Papastamos 
2272624d5faSMark Brown 	kfree(buf);
2282624d5faSMark Brown 	return ret;
2292624d5faSMark Brown }
2302624d5faSMark Brown 
2312624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2322624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2332624d5faSMark Brown {
2342624d5faSMark Brown 	char buf[32];
23534e268d8SStephen Boyd 	size_t buf_size;
2362624d5faSMark Brown 	char *start = buf;
2372624d5faSMark Brown 	unsigned long reg, value;
2382624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2392624d5faSMark Brown 
2402624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2412624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2422624d5faSMark Brown 		return -EFAULT;
2432624d5faSMark Brown 	buf[buf_size] = 0;
2442624d5faSMark Brown 
2452624d5faSMark Brown 	while (*start == ' ')
2462624d5faSMark Brown 		start++;
2472624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2482624d5faSMark Brown 	while (*start == ' ')
2492624d5faSMark Brown 		start++;
2502624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2512624d5faSMark Brown 		return -EINVAL;
2520d51a9cbSMark Brown 
2530d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2540d51a9cbSMark Brown 	add_taint(TAINT_USER);
2550d51a9cbSMark Brown 
256e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2572624d5faSMark Brown 	return buf_size;
2582624d5faSMark Brown }
2592624d5faSMark Brown 
2602624d5faSMark Brown static const struct file_operations codec_reg_fops = {
261234e3405SStephen Boyd 	.open = simple_open,
2622624d5faSMark Brown 	.read = codec_reg_read_file,
2632624d5faSMark Brown 	.write = codec_reg_write_file,
2646038f373SArnd Bergmann 	.llseek = default_llseek,
2652624d5faSMark Brown };
2662624d5faSMark Brown 
2672624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2682624d5faSMark Brown {
269d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270d6ce4cf3SJarkko Nikula 
2716ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2732624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
27464e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
2752624d5faSMark Brown 		return;
2762624d5faSMark Brown 	}
2772624d5faSMark Brown 
278aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
279aaee8ef1SMark Brown 			    &codec->cache_sync);
280aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
281aaee8ef1SMark Brown 			    &codec->cache_only);
282aaee8ef1SMark Brown 
2832624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2842624d5faSMark Brown 						 codec->debugfs_codec_root,
2852624d5faSMark Brown 						 codec, &codec_reg_fops);
2862624d5faSMark Brown 	if (!codec->debugfs_reg)
28764e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
2882624d5faSMark Brown 
2898eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
2902624d5faSMark Brown }
2912624d5faSMark Brown 
2922624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2932624d5faSMark Brown {
2942624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2952624d5faSMark Brown }
2962624d5faSMark Brown 
297731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
298731f1ab2SSebastien Guiriec {
299731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
300731f1ab2SSebastien Guiriec 
301731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
302731f1ab2SSebastien Guiriec 						       debugfs_card_root);
303731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
304731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
305731f1ab2SSebastien Guiriec 			"Failed to create platform debugfs directory\n");
306731f1ab2SSebastien Guiriec 		return;
307731f1ab2SSebastien Guiriec 	}
308731f1ab2SSebastien Guiriec 
309731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
310731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
311731f1ab2SSebastien Guiriec }
312731f1ab2SSebastien Guiriec 
313731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
314731f1ab2SSebastien Guiriec {
315731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
316731f1ab2SSebastien Guiriec }
317731f1ab2SSebastien Guiriec 
318c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
319c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
320c3c5a19aSMark Brown {
321c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3222b194f9dSMark Brown 	ssize_t len, ret = 0;
323c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
324c3c5a19aSMark Brown 
325c3c5a19aSMark Brown 	if (!buf)
326c3c5a19aSMark Brown 		return -ENOMEM;
327c3c5a19aSMark Brown 
3282b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3292b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
330c3c5a19aSMark Brown 			       codec->name);
3312b194f9dSMark Brown 		if (len >= 0)
3322b194f9dSMark Brown 			ret += len;
3332b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3342b194f9dSMark Brown 			ret = PAGE_SIZE;
3352b194f9dSMark Brown 			break;
3362b194f9dSMark Brown 		}
3372b194f9dSMark Brown 	}
338c3c5a19aSMark Brown 
339c3c5a19aSMark Brown 	if (ret >= 0)
340c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
341c3c5a19aSMark Brown 
342c3c5a19aSMark Brown 	kfree(buf);
343c3c5a19aSMark Brown 
344c3c5a19aSMark Brown 	return ret;
345c3c5a19aSMark Brown }
346c3c5a19aSMark Brown 
347c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
348c3c5a19aSMark Brown 	.read = codec_list_read_file,
349c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
350c3c5a19aSMark Brown };
351c3c5a19aSMark Brown 
352f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
353f3208780SMark Brown 				  size_t count, loff_t *ppos)
354f3208780SMark Brown {
355f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3562b194f9dSMark Brown 	ssize_t len, ret = 0;
357f3208780SMark Brown 	struct snd_soc_dai *dai;
358f3208780SMark Brown 
359f3208780SMark Brown 	if (!buf)
360f3208780SMark Brown 		return -ENOMEM;
361f3208780SMark Brown 
3622b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3632b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3642b194f9dSMark Brown 		if (len >= 0)
3652b194f9dSMark Brown 			ret += len;
3662b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3672b194f9dSMark Brown 			ret = PAGE_SIZE;
3682b194f9dSMark Brown 			break;
3692b194f9dSMark Brown 		}
3702b194f9dSMark Brown 	}
371f3208780SMark Brown 
372f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
373f3208780SMark Brown 
374f3208780SMark Brown 	kfree(buf);
375f3208780SMark Brown 
376f3208780SMark Brown 	return ret;
377f3208780SMark Brown }
378f3208780SMark Brown 
379f3208780SMark Brown static const struct file_operations dai_list_fops = {
380f3208780SMark Brown 	.read = dai_list_read_file,
381f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
382f3208780SMark Brown };
383f3208780SMark Brown 
38419c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
38519c7ac27SMark Brown 				       char __user *user_buf,
38619c7ac27SMark Brown 				       size_t count, loff_t *ppos)
38719c7ac27SMark Brown {
38819c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3892b194f9dSMark Brown 	ssize_t len, ret = 0;
39019c7ac27SMark Brown 	struct snd_soc_platform *platform;
39119c7ac27SMark Brown 
39219c7ac27SMark Brown 	if (!buf)
39319c7ac27SMark Brown 		return -ENOMEM;
39419c7ac27SMark Brown 
3952b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
3962b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
39719c7ac27SMark Brown 			       platform->name);
3982b194f9dSMark Brown 		if (len >= 0)
3992b194f9dSMark Brown 			ret += len;
4002b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4012b194f9dSMark Brown 			ret = PAGE_SIZE;
4022b194f9dSMark Brown 			break;
4032b194f9dSMark Brown 		}
4042b194f9dSMark Brown 	}
40519c7ac27SMark Brown 
40619c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
40719c7ac27SMark Brown 
40819c7ac27SMark Brown 	kfree(buf);
40919c7ac27SMark Brown 
41019c7ac27SMark Brown 	return ret;
41119c7ac27SMark Brown }
41219c7ac27SMark Brown 
41319c7ac27SMark Brown static const struct file_operations platform_list_fops = {
41419c7ac27SMark Brown 	.read = platform_list_read_file,
41519c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
41619c7ac27SMark Brown };
41719c7ac27SMark Brown 
418a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
419a6052154SJarkko Nikula {
420a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4218a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4223a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
423a6052154SJarkko Nikula 		dev_warn(card->dev,
4247c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4253a45b867SJarkko Nikula 		return;
4263a45b867SJarkko Nikula 	}
4273a45b867SJarkko Nikula 
4283a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4293a45b867SJarkko Nikula 						    card->debugfs_card_root,
4303a45b867SJarkko Nikula 						    &card->pop_time);
4313a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4323a45b867SJarkko Nikula 		dev_warn(card->dev,
4333a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
434a6052154SJarkko Nikula }
435a6052154SJarkko Nikula 
436a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
437a6052154SJarkko Nikula {
438a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
439a6052154SJarkko Nikula }
440a6052154SJarkko Nikula 
4412624d5faSMark Brown #else
4422624d5faSMark Brown 
4432624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4442624d5faSMark Brown {
4452624d5faSMark Brown }
4462624d5faSMark Brown 
4472624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4482624d5faSMark Brown {
4492624d5faSMark Brown }
450b95fccbcSAxel Lin 
451731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
452731f1ab2SSebastien Guiriec {
453731f1ab2SSebastien Guiriec }
454731f1ab2SSebastien Guiriec 
455731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
456731f1ab2SSebastien Guiriec {
457731f1ab2SSebastien Guiriec }
458731f1ab2SSebastien Guiriec 
459b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
460b95fccbcSAxel Lin {
461b95fccbcSAxel Lin }
462b95fccbcSAxel Lin 
463b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
464b95fccbcSAxel Lin {
465b95fccbcSAxel Lin }
4662624d5faSMark Brown #endif
4672624d5faSMark Brown 
468db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
469db2a4165SFrank Mandarino /* unregister ac97 codec */
470db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
471db2a4165SFrank Mandarino {
472db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
473db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
474db2a4165SFrank Mandarino 	return 0;
475db2a4165SFrank Mandarino }
476db2a4165SFrank Mandarino 
477db2a4165SFrank Mandarino /* stop no dev release warning */
478db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
479db2a4165SFrank Mandarino 
480db2a4165SFrank Mandarino /* register ac97 codec to bus */
481db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
482db2a4165SFrank Mandarino {
483db2a4165SFrank Mandarino 	int err;
484db2a4165SFrank Mandarino 
485db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4864ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
487db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
488db2a4165SFrank Mandarino 
489bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
490f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
491db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
492db2a4165SFrank Mandarino 	if (err < 0) {
493db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
494db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
495db2a4165SFrank Mandarino 		return err;
496db2a4165SFrank Mandarino 	}
497db2a4165SFrank Mandarino 	return 0;
498db2a4165SFrank Mandarino }
499db2a4165SFrank Mandarino #endif
500db2a4165SFrank Mandarino 
5016f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
502db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5036f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
504db2a4165SFrank Mandarino {
5056f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5062eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
507db2a4165SFrank Mandarino 	int i;
508db2a4165SFrank Mandarino 
509e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
510e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
511e3509ff0SDaniel Mack 	 */
512f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
513e3509ff0SDaniel Mack 		return 0;
514e3509ff0SDaniel Mack 
5156ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5166ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5176ed25978SAndy Green 	 */
518f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
519f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
520f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5216ed25978SAndy Green 
5226ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
523f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5246ed25978SAndy Green 
525a00f90f9SMark Brown 	/* mute any active DACs */
526f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
527f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
528f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5293efab7dcSMark Brown 
530f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5313efab7dcSMark Brown 			continue;
5323efab7dcSMark Brown 
533f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
534f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
535db2a4165SFrank Mandarino 	}
536db2a4165SFrank Mandarino 
5374ccab3e7SLiam Girdwood 	/* suspend all pcms */
538f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
539f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5403efab7dcSMark Brown 			continue;
5413efab7dcSMark Brown 
542f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5433efab7dcSMark Brown 	}
5444ccab3e7SLiam Girdwood 
54587506549SMark Brown 	if (card->suspend_pre)
54670b2ac12SMark Brown 		card->suspend_pre(card);
547db2a4165SFrank Mandarino 
548f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
549f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
550f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5513efab7dcSMark Brown 
552f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5533efab7dcSMark Brown 			continue;
5543efab7dcSMark Brown 
555f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
556f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
557f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
558f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
559f0fba2adSLiam Girdwood 			platform->suspended = 1;
560f0fba2adSLiam Girdwood 		}
561db2a4165SFrank Mandarino 	}
562db2a4165SFrank Mandarino 
563db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
564f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5655b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
566ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
567f0fba2adSLiam Girdwood 	}
568db2a4165SFrank Mandarino 
569f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5703efab7dcSMark Brown 
571f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5723efab7dcSMark Brown 			continue;
5733efab7dcSMark Brown 
5747bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5757bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
576db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
577f0fba2adSLiam Girdwood 
5787bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5797bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
580db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
581db2a4165SFrank Mandarino 	}
582db2a4165SFrank Mandarino 
583f0fba2adSLiam Girdwood 	/* suspend all CODECs */
5842eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
5851547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
5861547aba9SMark Brown 		 * bias _ON and should not be suspended. */
587f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
588ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
5891547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
590125a25daSMark Brown 				/*
591125a25daSMark Brown 				 * If the CODEC is capable of idle
592125a25daSMark Brown 				 * bias off then being in STANDBY
593125a25daSMark Brown 				 * means it's doing something,
594125a25daSMark Brown 				 * otherwise fall through.
595125a25daSMark Brown 				 */
596125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
597125a25daSMark Brown 					dev_dbg(codec->dev,
598125a25daSMark Brown 						"idle_bias_off CODEC on over suspend\n");
599125a25daSMark Brown 					break;
600125a25daSMark Brown 				}
6011547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
60284b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
603f0fba2adSLiam Girdwood 				codec->suspended = 1;
6047be4ba24SMark Brown 				codec->cache_sync = 1;
6051547aba9SMark Brown 				break;
6061547aba9SMark Brown 			default:
607f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
6081547aba9SMark Brown 				break;
6091547aba9SMark Brown 			}
6101547aba9SMark Brown 		}
611f0fba2adSLiam Girdwood 	}
612db2a4165SFrank Mandarino 
613f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
614f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6153efab7dcSMark Brown 
616f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6173efab7dcSMark Brown 			continue;
6183efab7dcSMark Brown 
619f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
620f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
621db2a4165SFrank Mandarino 	}
622db2a4165SFrank Mandarino 
62387506549SMark Brown 	if (card->suspend_post)
62470b2ac12SMark Brown 		card->suspend_post(card);
625db2a4165SFrank Mandarino 
626db2a4165SFrank Mandarino 	return 0;
627db2a4165SFrank Mandarino }
6286f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
629db2a4165SFrank Mandarino 
6306ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6316ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6326ed25978SAndy Green  */
6336ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
634db2a4165SFrank Mandarino {
635f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
636f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6372eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
638db2a4165SFrank Mandarino 	int i;
639db2a4165SFrank Mandarino 
6406ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6416ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6426ed25978SAndy Green 	 */
6436ed25978SAndy Green 
644f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
6456ed25978SAndy Green 
6469949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
647f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6489949788bSMark Brown 
64987506549SMark Brown 	if (card->resume_pre)
65070b2ac12SMark Brown 		card->resume_pre(card);
651db2a4165SFrank Mandarino 
652f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
653f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
654f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6553efab7dcSMark Brown 
656f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6573efab7dcSMark Brown 			continue;
6583efab7dcSMark Brown 
659f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
660f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
661db2a4165SFrank Mandarino 	}
662db2a4165SFrank Mandarino 
6632eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6641547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6651547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
6661547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
6671547aba9SMark Brown 		 */
668f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
669ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6701547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
6711547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
672f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
673f0fba2adSLiam Girdwood 				codec->suspended = 0;
6741547aba9SMark Brown 				break;
6751547aba9SMark Brown 			default:
676f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
6771547aba9SMark Brown 				break;
6781547aba9SMark Brown 			}
6791547aba9SMark Brown 		}
680f0fba2adSLiam Girdwood 	}
681db2a4165SFrank Mandarino 
682f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6833efab7dcSMark Brown 
684f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6853efab7dcSMark Brown 			continue;
6863efab7dcSMark Brown 
6877bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
688d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
689db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
690f0fba2adSLiam Girdwood 
6917bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
692d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
693db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
694db2a4165SFrank Mandarino 	}
695db2a4165SFrank Mandarino 
6963ff3f64bSMark Brown 	/* unmute any active DACs */
697f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
698f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
699f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7003efab7dcSMark Brown 
701f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7023efab7dcSMark Brown 			continue;
7033efab7dcSMark Brown 
704f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
705f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
706db2a4165SFrank Mandarino 	}
707db2a4165SFrank Mandarino 
708f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
709f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
710f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7113efab7dcSMark Brown 
712f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7133efab7dcSMark Brown 			continue;
7143efab7dcSMark Brown 
715f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
716f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
717f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
718f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
719f0fba2adSLiam Girdwood 			platform->suspended = 0;
720f0fba2adSLiam Girdwood 		}
721db2a4165SFrank Mandarino 	}
722db2a4165SFrank Mandarino 
72387506549SMark Brown 	if (card->resume_post)
72470b2ac12SMark Brown 		card->resume_post(card);
725db2a4165SFrank Mandarino 
726f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
7276ed25978SAndy Green 
7286ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
729f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7306ed25978SAndy Green }
7316ed25978SAndy Green 
7326ed25978SAndy Green /* powers up audio subsystem after a suspend */
7336f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7346ed25978SAndy Green {
7356f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
73682e14e8bSStephen Warren 	int i, ac97_control = 0;
737b9dd94a8SPeter Ujfalusi 
7385ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
7395ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
7405ff1ddf2SEric Miao 	 */
7415ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
7425ff1ddf2SEric Miao 		return 0;
7435ff1ddf2SEric Miao 
74464ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
74564ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
74664ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
74764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
74864ab9baaSMark Brown 	 */
749f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
750f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
75182e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
75282e14e8bSStephen Warren 	}
75382e14e8bSStephen Warren 	if (ac97_control) {
754f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
75564ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
75664ab9baaSMark Brown 	} else {
757f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
7586308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
759f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
760f0fba2adSLiam Girdwood 	}
7616ed25978SAndy Green 
762db2a4165SFrank Mandarino 	return 0;
763db2a4165SFrank Mandarino }
7646f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
765db2a4165SFrank Mandarino #else
7666f8ab4acSMark Brown #define snd_soc_suspend NULL
7676f8ab4acSMark Brown #define snd_soc_resume NULL
768db2a4165SFrank Mandarino #endif
769db2a4165SFrank Mandarino 
77085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
77102a06d30SBarry Song };
77202a06d30SBarry Song 
773f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
774db2a4165SFrank Mandarino {
775f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
776f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
777fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
778435c5e25SMark Brown 	struct snd_soc_platform *platform;
779f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
780848dd8beSMark Brown 	const char *platform_name;
781db2a4165SFrank Mandarino 
782f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
7836308419aSMark Brown 
784b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
785f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
7865a504963SStephen Warren 		if (dai_link->cpu_dai_of_node) {
7875a504963SStephen Warren 			if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
7885a504963SStephen Warren 				continue;
7895a504963SStephen Warren 		} else {
7902610ab77SStephen Warren 			if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
7912610ab77SStephen Warren 				continue;
7925a504963SStephen Warren 		}
7932610ab77SStephen Warren 
794f0fba2adSLiam Girdwood 		rtd->cpu_dai = cpu_dai;
795f0fba2adSLiam Girdwood 	}
796b19e6e7bSMark Brown 
797b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
798f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CPU DAI %s not registered\n",
799f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
800b19e6e7bSMark Brown 		return -EPROBE_DEFER;
801c5af3a2eSMark Brown 	}
802c5af3a2eSMark Brown 
803b19e6e7bSMark Brown 	/* Find CODEC from registered CODECs */
804f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8055a504963SStephen Warren 		if (dai_link->codec_of_node) {
8065a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8075a504963SStephen Warren 				continue;
8085a504963SStephen Warren 		} else {
8092610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8102610ab77SStephen Warren 				continue;
8115a504963SStephen Warren 		}
8122610ab77SStephen Warren 
813f0fba2adSLiam Girdwood 		rtd->codec = codec;
814f0fba2adSLiam Girdwood 
8152610ab77SStephen Warren 		/*
8162610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
8172610ab77SStephen Warren 		 * this CODEC
8182610ab77SStephen Warren 		 */
819f0fba2adSLiam Girdwood 		list_for_each_entry(codec_dai, &dai_list, list) {
820f0fba2adSLiam Girdwood 			if (codec->dev == codec_dai->dev &&
8212610ab77SStephen Warren 				!strcmp(codec_dai->name,
8222610ab77SStephen Warren 					dai_link->codec_dai_name)) {
8232610ab77SStephen Warren 
824f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
825435c5e25SMark Brown 			}
826f0fba2adSLiam Girdwood 		}
827b19e6e7bSMark Brown 
828b19e6e7bSMark Brown 		if (!rtd->codec_dai) {
829f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
830f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
831b19e6e7bSMark Brown 			return -EPROBE_DEFER;
832f0fba2adSLiam Girdwood 		}
833b19e6e7bSMark Brown 	}
834b19e6e7bSMark Brown 
835b19e6e7bSMark Brown 	if (!rtd->codec) {
836f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CODEC %s not registered\n",
837f0fba2adSLiam Girdwood 			dai_link->codec_name);
838b19e6e7bSMark Brown 		return -EPROBE_DEFER;
839b19e6e7bSMark Brown 	}
840848dd8beSMark Brown 
841848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
842848dd8beSMark Brown 	platform_name = dai_link->platform_name;
8435a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
844848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
845848dd8beSMark Brown 
846b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
847f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
8485a504963SStephen Warren 		if (dai_link->platform_of_node) {
8495a504963SStephen Warren 			if (platform->dev->of_node !=
8505a504963SStephen Warren 			    dai_link->platform_of_node)
8515a504963SStephen Warren 				continue;
8525a504963SStephen Warren 		} else {
8532610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
8542610ab77SStephen Warren 				continue;
8555a504963SStephen Warren 		}
8562610ab77SStephen Warren 
857f0fba2adSLiam Girdwood 		rtd->platform = platform;
858f0fba2adSLiam Girdwood 	}
859b19e6e7bSMark Brown 	if (!rtd->platform) {
860f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "platform %s not registered\n",
861f0fba2adSLiam Girdwood 			dai_link->platform_name);
862b19e6e7bSMark Brown 		return -EPROBE_DEFER;
863f0fba2adSLiam Girdwood 	}
864b19e6e7bSMark Brown 
865b19e6e7bSMark Brown 	card->num_rtd++;
866b19e6e7bSMark Brown 
867b19e6e7bSMark Brown 	return 0;
8686b05eda6SMark Brown }
8696b05eda6SMark Brown 
870589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
871589c3563SJarkko Nikula {
872589c3563SJarkko Nikula 	int err;
873589c3563SJarkko Nikula 
874589c3563SJarkko Nikula 	if (codec->driver->remove) {
875589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
876589c3563SJarkko Nikula 		if (err < 0)
877589c3563SJarkko Nikula 			dev_err(codec->dev,
878589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
879589c3563SJarkko Nikula 				codec->name, err);
880589c3563SJarkko Nikula 	}
881589c3563SJarkko Nikula 
882589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
883589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
884589c3563SJarkko Nikula 
885589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
886589c3563SJarkko Nikula 	codec->probed = 0;
887589c3563SJarkko Nikula 	list_del(&codec->card_list);
888589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
889589c3563SJarkko Nikula }
890589c3563SJarkko Nikula 
8910168bf0dSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
892f0fba2adSLiam Girdwood {
893f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
894f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
895f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
896f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
897f0fba2adSLiam Girdwood 	int err;
898f0fba2adSLiam Girdwood 
899f0fba2adSLiam Girdwood 	/* unregister the rtd device */
900f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
90136ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
90236ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
90336ae1a96SMark Brown 		device_unregister(rtd->dev);
904f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
90502a06d30SBarry Song 	}
90602a06d30SBarry Song 
907f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9080168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
9090168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
910f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
911f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
912f0fba2adSLiam Girdwood 			if (err < 0)
9130837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9140837fc62SFabio Estevam 							codec_dai->name, err);
915f0fba2adSLiam Girdwood 		}
916f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
917f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
918f0fba2adSLiam Girdwood 	}
919f0fba2adSLiam Girdwood 
920f0fba2adSLiam Girdwood 	/* remove the platform */
9210168bf0dSLiam Girdwood 	if (platform && platform->probed &&
9220168bf0dSLiam Girdwood 			platform->driver->remove_order == order) {
923f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
924f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
925f0fba2adSLiam Girdwood 			if (err < 0)
9260837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9270837fc62SFabio Estevam 							platform->name, err);
928f0fba2adSLiam Girdwood 		}
929675c496bSLiam Girdwood 
930675c496bSLiam Girdwood 		/* Make sure all DAPM widgets are freed */
931675c496bSLiam Girdwood 		snd_soc_dapm_free(&platform->dapm);
932675c496bSLiam Girdwood 
933731f1ab2SSebastien Guiriec 		soc_cleanup_platform_debugfs(platform);
934f0fba2adSLiam Girdwood 		platform->probed = 0;
935f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
936f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
937f0fba2adSLiam Girdwood 	}
938f0fba2adSLiam Girdwood 
939f0fba2adSLiam Girdwood 	/* remove the CODEC */
9400168bf0dSLiam Girdwood 	if (codec && codec->probed &&
9410168bf0dSLiam Girdwood 			codec->driver->remove_order == order)
942589c3563SJarkko Nikula 		soc_remove_codec(codec);
943f0fba2adSLiam Girdwood 
944f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
9450168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
9460168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
947f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
948f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
949f0fba2adSLiam Girdwood 			if (err < 0)
9500837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9510837fc62SFabio Estevam 							cpu_dai->name, err);
952f0fba2adSLiam Girdwood 		}
953f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
954f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
955f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
956f0fba2adSLiam Girdwood 	}
957f0fba2adSLiam Girdwood }
958f0fba2adSLiam Girdwood 
9590671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9600671fd8eSKuninori Morimoto {
9610168bf0dSLiam Girdwood 	int dai, order;
9620671fd8eSKuninori Morimoto 
9630168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9640168bf0dSLiam Girdwood 			order++) {
9650168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
9660168bf0dSLiam Girdwood 			soc_remove_dai_link(card, dai, order);
9670168bf0dSLiam Girdwood 	}
9680671fd8eSKuninori Morimoto 	card->num_rtd = 0;
9690671fd8eSKuninori Morimoto }
9700671fd8eSKuninori Morimoto 
971ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
972ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
973ead9b919SJarkko Nikula {
974ead9b919SJarkko Nikula 	int i;
975ead9b919SJarkko Nikula 
976ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
977ead9b919SJarkko Nikula 		return;
978ead9b919SJarkko Nikula 
979ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
980ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
981ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
982ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
983ead9b919SJarkko Nikula 			break;
984ead9b919SJarkko Nikula 		}
985ead9b919SJarkko Nikula 	}
986ead9b919SJarkko Nikula }
987ead9b919SJarkko Nikula 
988589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
989589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
990589c3563SJarkko Nikula {
991589c3563SJarkko Nikula 	int ret = 0;
99289b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
993888df395SMark Brown 	struct snd_soc_dai *dai;
994589c3563SJarkko Nikula 
995589c3563SJarkko Nikula 	codec->card = card;
996589c3563SJarkko Nikula 	codec->dapm.card = card;
997589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
998589c3563SJarkko Nikula 
99970d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
100070d29331SJarkko Nikula 		return -ENODEV;
100170d29331SJarkko Nikula 
1002d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1003d5d1e0beSLars-Peter Clausen 
100477530150SLars-Peter Clausen 	if (driver->dapm_widgets)
100577530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
100677530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
100777530150SLars-Peter Clausen 
1008888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1009888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1010888df395SMark Brown 		if (dai->dev != codec->dev)
1011888df395SMark Brown 			continue;
1012888df395SMark Brown 
1013888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1014888df395SMark Brown 	}
1015888df395SMark Brown 
101633c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
101733c5f969SMark Brown 
101889b95ac0SMark Brown 	if (driver->probe) {
101989b95ac0SMark Brown 		ret = driver->probe(codec);
1020589c3563SJarkko Nikula 		if (ret < 0) {
1021589c3563SJarkko Nikula 			dev_err(codec->dev,
1022589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1023589c3563SJarkko Nikula 				codec->name, ret);
102470d29331SJarkko Nikula 			goto err_probe;
1025589c3563SJarkko Nikula 		}
1026589c3563SJarkko Nikula 	}
1027589c3563SJarkko Nikula 
1028b7af1dafSMark Brown 	if (driver->controls)
1029022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1030b7af1dafSMark Brown 				     driver->num_controls);
103189b95ac0SMark Brown 	if (driver->dapm_routes)
103289b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
103389b95ac0SMark Brown 					driver->num_dapm_routes);
103489b95ac0SMark Brown 
1035589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1036589c3563SJarkko Nikula 	codec->probed = 1;
1037589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
10387be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1039589c3563SJarkko Nikula 
104070d29331SJarkko Nikula 	return 0;
104170d29331SJarkko Nikula 
104270d29331SJarkko Nikula err_probe:
1043d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
104470d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
104570d29331SJarkko Nikula 
1046589c3563SJarkko Nikula 	return ret;
1047589c3563SJarkko Nikula }
1048589c3563SJarkko Nikula 
1049956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1050956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1051956245e9SLiam Girdwood {
1052956245e9SLiam Girdwood 	int ret = 0;
1053956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1054be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1055956245e9SLiam Girdwood 
1056956245e9SLiam Girdwood 	platform->card = card;
1057b7950641SLiam Girdwood 	platform->dapm.card = card;
1058956245e9SLiam Girdwood 
1059956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1060956245e9SLiam Girdwood 		return -ENODEV;
1061956245e9SLiam Girdwood 
1062731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1063731f1ab2SSebastien Guiriec 
1064cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1065cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1066cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1067cb2cf612SLiam Girdwood 
1068be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
1069be09ad90SLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
1070be09ad90SLiam Girdwood 		if (dai->dev != platform->dev)
1071be09ad90SLiam Girdwood 			continue;
1072be09ad90SLiam Girdwood 
1073be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1074be09ad90SLiam Girdwood 	}
1075be09ad90SLiam Girdwood 
10763fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
10773fec6b6dSStephen Warren 
1078956245e9SLiam Girdwood 	if (driver->probe) {
1079956245e9SLiam Girdwood 		ret = driver->probe(platform);
1080956245e9SLiam Girdwood 		if (ret < 0) {
1081956245e9SLiam Girdwood 			dev_err(platform->dev,
1082956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1083956245e9SLiam Girdwood 				platform->name, ret);
1084956245e9SLiam Girdwood 			goto err_probe;
1085956245e9SLiam Girdwood 		}
1086956245e9SLiam Girdwood 	}
1087956245e9SLiam Girdwood 
1088cb2cf612SLiam Girdwood 	if (driver->controls)
1089cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1090cb2cf612SLiam Girdwood 				     driver->num_controls);
1091cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1092cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1093cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1094cb2cf612SLiam Girdwood 
1095956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1096956245e9SLiam Girdwood 	platform->probed = 1;
1097956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1098b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1099956245e9SLiam Girdwood 
1100956245e9SLiam Girdwood 	return 0;
1101956245e9SLiam Girdwood 
1102956245e9SLiam Girdwood err_probe:
110302db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1104956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1105956245e9SLiam Girdwood 
1106956245e9SLiam Girdwood 	return ret;
1107956245e9SLiam Girdwood }
1108956245e9SLiam Girdwood 
110936ae1a96SMark Brown static void rtd_release(struct device *dev)
111036ae1a96SMark Brown {
111136ae1a96SMark Brown 	kfree(dev);
111236ae1a96SMark Brown }
1113f0fba2adSLiam Girdwood 
1114589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1115589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1116589c3563SJarkko Nikula 				   int num, int dailess)
1117589c3563SJarkko Nikula {
1118589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1119589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1120589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1121589c3563SJarkko Nikula 	const char *temp, *name;
1122589c3563SJarkko Nikula 	int ret = 0;
1123589c3563SJarkko Nikula 
1124589c3563SJarkko Nikula 	if (!dailess) {
1125589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1126589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1127589c3563SJarkko Nikula 		name = dai_link->name;
1128589c3563SJarkko Nikula 	} else {
1129589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1130589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1131589c3563SJarkko Nikula 		name = aux_dev->name;
1132589c3563SJarkko Nikula 	}
11330962bb21SJanusz Krzysztofik 	rtd->card = card;
1134589c3563SJarkko Nikula 
11354b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
11364b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
11374b1cfcb4SMark Brown 
1138589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1139589c3563SJarkko Nikula 	temp = codec->name_prefix;
1140589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1141589c3563SJarkko Nikula 
1142589c3563SJarkko Nikula 	/* do machine specific initialization */
1143589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1144589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1145589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1146589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1147589c3563SJarkko Nikula 	if (ret < 0) {
1148589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1149589c3563SJarkko Nikula 		return ret;
1150589c3563SJarkko Nikula 	}
1151589c3563SJarkko Nikula 	codec->name_prefix = temp;
1152589c3563SJarkko Nikula 
1153589c3563SJarkko Nikula 	/* register the rtd device */
1154589c3563SJarkko Nikula 	rtd->codec = codec;
115536ae1a96SMark Brown 
115636ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
115736ae1a96SMark Brown 	if (!rtd->dev)
115836ae1a96SMark Brown 		return -ENOMEM;
115936ae1a96SMark Brown 	device_initialize(rtd->dev);
116036ae1a96SMark Brown 	rtd->dev->parent = card->dev;
116136ae1a96SMark Brown 	rtd->dev->release = rtd_release;
116236ae1a96SMark Brown 	rtd->dev->init_name = name;
116336ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1164b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
116501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
116601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
116701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
116801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
116936ae1a96SMark Brown 	ret = device_add(rtd->dev);
1170589c3563SJarkko Nikula 	if (ret < 0) {
1171589c3563SJarkko Nikula 		dev_err(card->dev,
1172589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1173589c3563SJarkko Nikula 		return ret;
1174589c3563SJarkko Nikula 	}
1175589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1176589c3563SJarkko Nikula 
1177589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
117836ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1179589c3563SJarkko Nikula 	if (ret < 0)
1180589c3563SJarkko Nikula 		dev_err(codec->dev,
1181589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1182589c3563SJarkko Nikula 			ret);
1183589c3563SJarkko Nikula 
1184589c3563SJarkko Nikula 	/* add codec sysfs entries */
118536ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1186589c3563SJarkko Nikula 	if (ret < 0)
1187589c3563SJarkko Nikula 		dev_err(codec->dev,
1188589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1189589c3563SJarkko Nikula 
1190*f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1191*f86dcef8SLiam Girdwood 	/* add DPCM sysfs entries */
1192*f86dcef8SLiam Girdwood 	if (!dai_link->dynamic)
1193*f86dcef8SLiam Girdwood 		goto out;
1194*f86dcef8SLiam Girdwood 
1195*f86dcef8SLiam Girdwood 	ret = soc_dpcm_debugfs_add(rtd);
1196*f86dcef8SLiam Girdwood 	if (ret < 0)
1197*f86dcef8SLiam Girdwood 		dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1198*f86dcef8SLiam Girdwood 
1199*f86dcef8SLiam Girdwood out:
1200*f86dcef8SLiam Girdwood #endif
1201589c3563SJarkko Nikula 	return 0;
1202589c3563SJarkko Nikula }
1203589c3563SJarkko Nikula 
12040168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1205f0fba2adSLiam Girdwood {
1206f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1207f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1208f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1209f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1210c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1211c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1212c74184edSMark Brown 	struct snd_soc_dapm_widget *play_w, *capture_w;
1213f0fba2adSLiam Girdwood 	int ret;
1214f0fba2adSLiam Girdwood 
12150168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
12160168bf0dSLiam Girdwood 			card->name, num, order);
1217f0fba2adSLiam Girdwood 
1218f0fba2adSLiam Girdwood 	/* config components */
1219f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1220f0fba2adSLiam Girdwood 	codec_dai->card = card;
1221f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1222f0fba2adSLiam Girdwood 
1223f0fba2adSLiam Girdwood 	/* set default power off timeout */
1224f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1225f0fba2adSLiam Girdwood 
1226f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
12270168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
12280168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1229be09ad90SLiam Girdwood 		cpu_dai->dapm.card = card;
123061b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
123161b61e3cSLiam Girdwood 			return -ENODEV;
123261b61e3cSLiam Girdwood 
1233be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1234be09ad90SLiam Girdwood 
1235f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1236f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1237f0fba2adSLiam Girdwood 			if (ret < 0) {
12380837fc62SFabio Estevam 				pr_err("asoc: failed to probe CPU DAI %s: %d\n",
12390837fc62SFabio Estevam 							cpu_dai->name, ret);
124061b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1241f0fba2adSLiam Girdwood 				return ret;
1242f0fba2adSLiam Girdwood 			}
1243f0fba2adSLiam Girdwood 		}
1244f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
12451c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1246f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1247f0fba2adSLiam Girdwood 	}
1248f0fba2adSLiam Girdwood 
1249f0fba2adSLiam Girdwood 	/* probe the CODEC */
12500168bf0dSLiam Girdwood 	if (!codec->probed &&
12510168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1252589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1253589c3563SJarkko Nikula 		if (ret < 0)
1254f0fba2adSLiam Girdwood 			return ret;
1255f0fba2adSLiam Girdwood 	}
1256f0fba2adSLiam Girdwood 
1257f0fba2adSLiam Girdwood 	/* probe the platform */
12580168bf0dSLiam Girdwood 	if (!platform->probed &&
12590168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1260956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1261956245e9SLiam Girdwood 		if (ret < 0)
1262f0fba2adSLiam Girdwood 			return ret;
1263f0fba2adSLiam Girdwood 	}
1264f0fba2adSLiam Girdwood 
1265f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
12660168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1267f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1268f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1269f0fba2adSLiam Girdwood 			if (ret < 0) {
12700837fc62SFabio Estevam 				pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
12710837fc62SFabio Estevam 							codec_dai->name, ret);
1272f0fba2adSLiam Girdwood 				return ret;
1273f0fba2adSLiam Girdwood 			}
1274f0fba2adSLiam Girdwood 		}
1275f0fba2adSLiam Girdwood 
12761c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1277f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1278f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1279f0fba2adSLiam Girdwood 	}
1280f0fba2adSLiam Girdwood 
12810168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
12820168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
12830168bf0dSLiam Girdwood 		return 0;
12840168bf0dSLiam Girdwood 
1285589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1286589c3563SJarkko Nikula 	if (ret)
1287f0fba2adSLiam Girdwood 		return ret;
1288f0fba2adSLiam Girdwood 
128936ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1290f0fba2adSLiam Girdwood 	if (ret < 0)
12910837fc62SFabio Estevam 		pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1292f0fba2adSLiam Girdwood 
1293c74184edSMark Brown 	if (!dai_link->params) {
1294f0fba2adSLiam Girdwood 		/* create the pcm */
1295f0fba2adSLiam Girdwood 		ret = soc_new_pcm(rtd, num);
1296f0fba2adSLiam Girdwood 		if (ret < 0) {
12970837fc62SFabio Estevam 			pr_err("asoc: can't create pcm %s :%d\n",
12980837fc62SFabio Estevam 			       dai_link->stream_name, ret);
1299f0fba2adSLiam Girdwood 			return ret;
1300f0fba2adSLiam Girdwood 		}
1301c74184edSMark Brown 	} else {
1302c74184edSMark Brown 		/* link the DAI widgets */
1303c74184edSMark Brown 		play_w = codec_dai->playback_widget;
1304c74184edSMark Brown 		capture_w = cpu_dai->capture_widget;
1305c74184edSMark Brown 		if (play_w && capture_w) {
1306c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1307c74184edSMark Brown 						   capture_w, play_w);
1308c74184edSMark Brown 			if (ret != 0) {
1309c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1310c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1311c74184edSMark Brown 				return ret;
1312c74184edSMark Brown 			}
1313c74184edSMark Brown 		}
1314c74184edSMark Brown 
1315c74184edSMark Brown 		play_w = cpu_dai->playback_widget;
1316c74184edSMark Brown 		capture_w = codec_dai->capture_widget;
1317c74184edSMark Brown 		if (play_w && capture_w) {
1318c74184edSMark Brown 			ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1319c74184edSMark Brown 						   capture_w, play_w);
1320c74184edSMark Brown 			if (ret != 0) {
1321c74184edSMark Brown 				dev_err(card->dev, "Can't link %s to %s: %d\n",
1322c74184edSMark Brown 					play_w->name, capture_w->name, ret);
1323c74184edSMark Brown 				return ret;
1324c74184edSMark Brown 			}
1325c74184edSMark Brown 		}
1326c74184edSMark Brown 	}
1327f0fba2adSLiam Girdwood 
1328f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1329f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1330f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1331f0fba2adSLiam Girdwood 
1332f0fba2adSLiam Girdwood 	return 0;
1333f0fba2adSLiam Girdwood }
1334f0fba2adSLiam Girdwood 
1335f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1336f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1337f0fba2adSLiam Girdwood {
1338f0fba2adSLiam Girdwood 	int ret;
1339f0fba2adSLiam Girdwood 
1340f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1341f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
13426b05eda6SMark Brown 	 */
1343f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
13440562f788SMika Westerberg 		/*
13450562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
13460562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
13470562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
13480562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
13490562f788SMika Westerberg 		 *
13500562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
13510562f788SMika Westerberg 		 */
13520562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
13530562f788SMika Westerberg 			return 0;
1354f0fba2adSLiam Girdwood 
1355f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1356f0fba2adSLiam Girdwood 		if (ret < 0) {
13570837fc62SFabio Estevam 			pr_err("asoc: AC97 device register failed:%d\n", ret);
1358f0fba2adSLiam Girdwood 			return ret;
1359435c5e25SMark Brown 		}
1360435c5e25SMark Brown 
1361f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1362f0fba2adSLiam Girdwood 	}
1363f0fba2adSLiam Girdwood 	return 0;
1364f0fba2adSLiam Girdwood }
1365435c5e25SMark Brown 
1366f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1367f0fba2adSLiam Girdwood {
1368f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1369f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1370f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1371f0fba2adSLiam Girdwood 	}
1372f0fba2adSLiam Girdwood }
1373f0fba2adSLiam Girdwood #endif
1374435c5e25SMark Brown 
1375b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1376b19e6e7bSMark Brown {
1377b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1378b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1379b19e6e7bSMark Brown 
1380b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1381b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1382b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1383b19e6e7bSMark Brown 			return 0;
1384b19e6e7bSMark Brown 	}
1385b19e6e7bSMark Brown 
1386b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1387b19e6e7bSMark Brown }
1388b19e6e7bSMark Brown 
13892eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
13902eea392dSJarkko Nikula {
13912eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
13922eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1393676ad98aSJarkko Nikula 	int ret = -ENODEV;
13942eea392dSJarkko Nikula 
13952eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
13962eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
13972eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
13982eea392dSJarkko Nikula 			if (codec->probed) {
13992eea392dSJarkko Nikula 				dev_err(codec->dev,
14002eea392dSJarkko Nikula 					"asoc: codec already probed");
14012eea392dSJarkko Nikula 				ret = -EBUSY;
14022eea392dSJarkko Nikula 				goto out;
14032eea392dSJarkko Nikula 			}
1404676ad98aSJarkko Nikula 			goto found;
14052eea392dSJarkko Nikula 		}
14062eea392dSJarkko Nikula 	}
1407676ad98aSJarkko Nikula 	/* codec not found */
1408676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1409b19e6e7bSMark Brown 	return -EPROBE_DEFER;
14102eea392dSJarkko Nikula 
1411676ad98aSJarkko Nikula found:
1412589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
14132eea392dSJarkko Nikula 	if (ret < 0)
1414589c3563SJarkko Nikula 		return ret;
14152eea392dSJarkko Nikula 
1416589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
14172eea392dSJarkko Nikula 
14182eea392dSJarkko Nikula out:
14192eea392dSJarkko Nikula 	return ret;
14202eea392dSJarkko Nikula }
14212eea392dSJarkko Nikula 
14222eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
14232eea392dSJarkko Nikula {
14242eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14252eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
14262eea392dSJarkko Nikula 
14272eea392dSJarkko Nikula 	/* unregister the rtd device */
14282eea392dSJarkko Nikula 	if (rtd->dev_registered) {
142936ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
143036ae1a96SMark Brown 		device_del(rtd->dev);
14312eea392dSJarkko Nikula 		rtd->dev_registered = 0;
14322eea392dSJarkko Nikula 	}
14332eea392dSJarkko Nikula 
1434589c3563SJarkko Nikula 	if (codec && codec->probed)
1435589c3563SJarkko Nikula 		soc_remove_codec(codec);
14362eea392dSJarkko Nikula }
14372eea392dSJarkko Nikula 
1438fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1439fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1440fdf0f54dSDimitris Papastamos {
1441fdf0f54dSDimitris Papastamos 	int ret;
1442fdf0f54dSDimitris Papastamos 
1443fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1444fdf0f54dSDimitris Papastamos 		return 0;
1445fdf0f54dSDimitris Papastamos 
1446fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1447fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1448fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1449fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1450fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1451fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1452fdf0f54dSDimitris Papastamos 			ret);
1453fdf0f54dSDimitris Papastamos 		return ret;
1454fdf0f54dSDimitris Papastamos 	}
1455fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1456fdf0f54dSDimitris Papastamos 	return 0;
1457fdf0f54dSDimitris Papastamos }
1458fdf0f54dSDimitris Papastamos 
1459b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1460f0fba2adSLiam Girdwood {
1461fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1462fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1463fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
146475d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1465f04209a7SMark Brown 	int ret, i, order, dai_fmt;
146696dd3622SMark Brown 
146701b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1468f0fba2adSLiam Girdwood 
1469b19e6e7bSMark Brown 	/* bind DAIs */
1470b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1471b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1472b19e6e7bSMark Brown 		if (ret != 0)
1473b19e6e7bSMark Brown 			goto base_error;
1474db2a4165SFrank Mandarino 	}
1475db2a4165SFrank Mandarino 
1476b19e6e7bSMark Brown 	/* check aux_devs too */
1477b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1478b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1479b19e6e7bSMark Brown 		if (ret != 0)
1480b19e6e7bSMark Brown 			goto base_error;
1481db2a4165SFrank Mandarino 	}
1482db2a4165SFrank Mandarino 
1483fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1484fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1485fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1486fdf0f54dSDimitris Papastamos 			continue;
1487861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1488861f2fafSDimitris Papastamos 		compress_type = 0;
1489fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1490fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1491fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1492fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1493fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1494fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1495fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1496fdf0f54dSDimitris Papastamos 					break;
1497fdf0f54dSDimitris Papastamos 			}
1498fdf0f54dSDimitris Papastamos 		}
1499fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1500b19e6e7bSMark Brown 		if (ret < 0)
1501b19e6e7bSMark Brown 			goto base_error;
1502fdf0f54dSDimitris Papastamos 	}
1503fdf0f54dSDimitris Papastamos 
1504f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1505f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1506f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1507f0fba2adSLiam Girdwood 	if (ret < 0) {
15080837fc62SFabio Estevam 		pr_err("asoc: can't create sound card for card %s: %d\n",
15090837fc62SFabio Estevam 			card->name, ret);
1510b19e6e7bSMark Brown 		goto base_error;
1511db2a4165SFrank Mandarino 	}
1512f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1513db2a4165SFrank Mandarino 
1514e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1515e37a4970SMark Brown 	card->dapm.dev = card->dev;
1516e37a4970SMark Brown 	card->dapm.card = card;
1517e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1518e37a4970SMark Brown 
1519d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1520d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1521d5d1e0beSLars-Peter Clausen #endif
1522d5d1e0beSLars-Peter Clausen 
152388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
15246ed25978SAndy Green 	/* deferred resume work */
15256308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
15261301a964SRandy Dunlap #endif
15276ed25978SAndy Green 
15289a841ebbSMark Brown 	if (card->dapm_widgets)
15299a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
15309a841ebbSMark Brown 					  card->num_dapm_widgets);
15319a841ebbSMark Brown 
1532f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1533f0fba2adSLiam Girdwood 	if (card->probe) {
1534e7361ec4SMark Brown 		ret = card->probe(card);
1535f0fba2adSLiam Girdwood 		if (ret < 0)
1536f0fba2adSLiam Girdwood 			goto card_probe_error;
1537f0fba2adSLiam Girdwood 	}
1538f0fba2adSLiam Girdwood 
15390168bf0dSLiam Girdwood 	/* early DAI link probe */
15400168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
15410168bf0dSLiam Girdwood 			order++) {
1542fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
15430168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1544fe3e78e0SMark Brown 			if (ret < 0) {
1545321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1546321de0d0SMark Brown 			       card->name, ret);
1547f0fba2adSLiam Girdwood 				goto probe_dai_err;
1548fe3e78e0SMark Brown 			}
1549fe3e78e0SMark Brown 		}
15500168bf0dSLiam Girdwood 	}
1551fe3e78e0SMark Brown 
15522eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
15532eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
15542eea392dSJarkko Nikula 		if (ret < 0) {
15552eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
15562eea392dSJarkko Nikula 			       card->name, ret);
15572eea392dSJarkko Nikula 			goto probe_aux_dev_err;
15582eea392dSJarkko Nikula 		}
15592eea392dSJarkko Nikula 	}
15602eea392dSJarkko Nikula 
1561888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1562888df395SMark Brown 
1563b7af1dafSMark Brown 	if (card->controls)
1564022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1565b7af1dafSMark Brown 
1566b8ad29deSMark Brown 	if (card->dapm_routes)
1567b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1568b8ad29deSMark Brown 					card->num_dapm_routes);
1569b8ad29deSMark Brown 
1570b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1571b90d2f90SMark Brown 
157275d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
157375d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1574f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
157575d9ac46SMark Brown 
1576f04209a7SMark Brown 		if (dai_fmt) {
157775d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1578f04209a7SMark Brown 						  dai_fmt);
15795e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
158075d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
158175d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
158275d9ac46SMark Brown 					 ret);
1583f04209a7SMark Brown 		}
1584f04209a7SMark Brown 
1585f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1586f04209a7SMark Brown 		if (dai_fmt && dai_link->platform_name) {
1587f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1588f04209a7SMark Brown 						  dai_fmt);
1589f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1590f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1591f04209a7SMark Brown 					 "Failed to set DAI format: %d\n",
1592f04209a7SMark Brown 					 ret);
1593f04209a7SMark Brown 		} else if (dai_fmt) {
1594f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1595f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1596f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1597f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1598f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1599f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1600f04209a7SMark Brown 				break;
1601f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1602f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1603f04209a7SMark Brown 				break;
1604f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1605f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1606f04209a7SMark Brown 				break;
1607f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1608f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1609f04209a7SMark Brown 				break;
1610f04209a7SMark Brown 			}
161175d9ac46SMark Brown 
161275d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1613f04209a7SMark Brown 						  dai_fmt);
16145e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
161575d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
161675d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
161775d9ac46SMark Brown 					 ret);
161875d9ac46SMark Brown 		}
161975d9ac46SMark Brown 	}
162075d9ac46SMark Brown 
1621f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1622fe3e78e0SMark Brown 		 "%s", card->name);
1623f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
162422de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1625f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1626f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1627f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1628f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1629f0e8ed85SMark Brown 		case '_':
1630f0e8ed85SMark Brown 		case '-':
1631f0e8ed85SMark Brown 		case '\0':
1632f0e8ed85SMark Brown 			break;
1633f0e8ed85SMark Brown 		default:
1634f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1635f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1636f0e8ed85SMark Brown 			break;
1637f0e8ed85SMark Brown 		}
1638f0e8ed85SMark Brown 	}
1639fe3e78e0SMark Brown 
164028e9ad92SMark Brown 	if (card->late_probe) {
164128e9ad92SMark Brown 		ret = card->late_probe(card);
164228e9ad92SMark Brown 		if (ret < 0) {
164328e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
164428e9ad92SMark Brown 				card->name, ret);
164528e9ad92SMark Brown 			goto probe_aux_dev_err;
164628e9ad92SMark Brown 		}
164728e9ad92SMark Brown 	}
164828e9ad92SMark Brown 
16492dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
16502dc00213SMark Brown 
16511633281bSStephen Warren 	if (card->fully_routed)
1652b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
16531633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
16541633281bSStephen Warren 
1655f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1656fe3e78e0SMark Brown 	if (ret < 0) {
16570837fc62SFabio Estevam 		pr_err("asoc: failed to register soundcard for %s: %d\n",
16580837fc62SFabio Estevam 							card->name, ret);
16596b3ed785SAxel Lin 		goto probe_aux_dev_err;
1660fe3e78e0SMark Brown 	}
1661fe3e78e0SMark Brown 
1662fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1663f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1664f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1665f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1666fe3e78e0SMark Brown 		if (ret < 0) {
16670837fc62SFabio Estevam 			pr_err("asoc: failed to register AC97 %s: %d\n",
16680837fc62SFabio Estevam 							card->name, ret);
16696b3ed785SAxel Lin 			while (--i >= 0)
16707d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
16716b3ed785SAxel Lin 			goto probe_aux_dev_err;
1672fe3e78e0SMark Brown 		}
1673fe3e78e0SMark Brown 	}
1674fe3e78e0SMark Brown #endif
1675fe3e78e0SMark Brown 
1676435c5e25SMark Brown 	card->instantiated = 1;
16774f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1678f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1679b19e6e7bSMark Brown 
1680b19e6e7bSMark Brown 	return 0;
1681db2a4165SFrank Mandarino 
16822eea392dSJarkko Nikula probe_aux_dev_err:
16832eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16842eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16852eea392dSJarkko Nikula 
1686f0fba2adSLiam Girdwood probe_dai_err:
16870671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1688fe3e78e0SMark Brown 
1689f0fba2adSLiam Girdwood card_probe_error:
169087506549SMark Brown 	if (card->remove)
1691e7361ec4SMark Brown 		card->remove(card);
1692f0fba2adSLiam Girdwood 
1693f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1694f0fba2adSLiam Girdwood 
1695b19e6e7bSMark Brown base_error:
1696f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1697db2a4165SFrank Mandarino 
1698b19e6e7bSMark Brown 	return ret;
1699435c5e25SMark Brown }
1700435c5e25SMark Brown 
1701435c5e25SMark Brown /* probes a new socdev */
1702435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1703435c5e25SMark Brown {
1704f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1705435c5e25SMark Brown 	int ret = 0;
1706435c5e25SMark Brown 
170770a7ca34SVinod Koul 	/*
170870a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
170970a7ca34SVinod Koul 	 * we should not be here in that case so ret error
171070a7ca34SVinod Koul 	 */
171170a7ca34SVinod Koul 	if (!card)
171270a7ca34SVinod Koul 		return -EINVAL;
171370a7ca34SVinod Koul 
1714fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1715fe4085e8SMark Brown 		 "ASoC machine %s should use snd_soc_register_card()\n",
1716fe4085e8SMark Brown 		 card->name);
1717fe4085e8SMark Brown 
1718435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1719435c5e25SMark Brown 	card->dev = &pdev->dev;
1720f0fba2adSLiam Girdwood 
1721435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1722435c5e25SMark Brown 	if (ret != 0) {
1723435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1724db2a4165SFrank Mandarino 		return ret;
1725db2a4165SFrank Mandarino 	}
1726db2a4165SFrank Mandarino 
1727435c5e25SMark Brown 	return 0;
1728435c5e25SMark Brown }
1729435c5e25SMark Brown 
1730b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1731db2a4165SFrank Mandarino {
1732db2a4165SFrank Mandarino 	int i;
1733db2a4165SFrank Mandarino 
1734f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1735f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1736f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17375b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1738db2a4165SFrank Mandarino 	}
1739db2a4165SFrank Mandarino 
17402eea392dSJarkko Nikula 	/* remove auxiliary devices */
17412eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
17422eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
17432eea392dSJarkko Nikula 
1744f0fba2adSLiam Girdwood 	/* remove and free each DAI */
17450671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1746f0fba2adSLiam Girdwood 
1747a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1748a6052154SJarkko Nikula 
1749f0fba2adSLiam Girdwood 	/* remove the card */
175087506549SMark Brown 	if (card->remove)
1751e7361ec4SMark Brown 		card->remove(card);
1752f0fba2adSLiam Girdwood 
17530aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
17540aaae527SLars-Peter Clausen 
1755f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1756b0e26485SVinod Koul 	return 0;
1757b0e26485SVinod Koul 
1758b2dfa62cSGuennadi Liakhovetski }
1759b0e26485SVinod Koul 
1760b0e26485SVinod Koul /* removes a socdev */
1761b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1762b0e26485SVinod Koul {
1763b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1764b0e26485SVinod Koul 
1765c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1766db2a4165SFrank Mandarino 	return 0;
1767db2a4165SFrank Mandarino }
1768db2a4165SFrank Mandarino 
17696f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
177051737470SMark Brown {
17716f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1772f0fba2adSLiam Girdwood 	int i;
177351737470SMark Brown 
177451737470SMark Brown 	if (!card->instantiated)
1775416356fcSMark Brown 		return 0;
177651737470SMark Brown 
177751737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
177851737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1779f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1780f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17815b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1782f0fba2adSLiam Girdwood 	}
178351737470SMark Brown 
1784f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1785416356fcSMark Brown 
1786416356fcSMark Brown 	return 0;
178751737470SMark Brown }
17886f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
178951737470SMark Brown 
17906f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1791b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1792b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1793b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1794b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
17956f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1796b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1797416356fcSMark Brown };
1798deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1799416356fcSMark Brown 
1800db2a4165SFrank Mandarino /* ASoC platform driver */
1801db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1802db2a4165SFrank Mandarino 	.driver		= {
1803db2a4165SFrank Mandarino 		.name		= "soc-audio",
18048b45a209SKay Sievers 		.owner		= THIS_MODULE,
18056f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1806db2a4165SFrank Mandarino 	},
1807db2a4165SFrank Mandarino 	.probe		= soc_probe,
1808db2a4165SFrank Mandarino 	.remove		= soc_remove,
1809db2a4165SFrank Mandarino };
1810db2a4165SFrank Mandarino 
1811096e49d5SMark Brown /**
1812096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1813096e49d5SMark Brown  *
1814096e49d5SMark Brown  * @codec: CODEC to query.
1815096e49d5SMark Brown  * @reg: Register to query.
1816096e49d5SMark Brown  *
1817096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1818096e49d5SMark Brown  */
1819181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1820181e055eSMark Brown 				    unsigned int reg)
1821096e49d5SMark Brown {
18221500b7b5SDimitris Papastamos 	if (codec->volatile_register)
18231500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1824096e49d5SMark Brown 	else
1825096e49d5SMark Brown 		return 0;
1826096e49d5SMark Brown }
1827096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1828096e49d5SMark Brown 
1829db2a4165SFrank Mandarino /**
1830239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1831239c9706SDimitris Papastamos  *
1832239c9706SDimitris Papastamos  * @codec: CODEC to query.
1833239c9706SDimitris Papastamos  * @reg: Register to query.
1834239c9706SDimitris Papastamos  *
1835239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1836239c9706SDimitris Papastamos  */
1837239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1838239c9706SDimitris Papastamos 				    unsigned int reg)
1839239c9706SDimitris Papastamos {
1840239c9706SDimitris Papastamos 	if (codec->readable_register)
1841239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1842239c9706SDimitris Papastamos 	else
184363fa0a28SLars-Peter Clausen 		return 1;
1844239c9706SDimitris Papastamos }
1845239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1846239c9706SDimitris Papastamos 
1847239c9706SDimitris Papastamos /**
1848239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1849239c9706SDimitris Papastamos  *
1850239c9706SDimitris Papastamos  * @codec: CODEC to query.
1851239c9706SDimitris Papastamos  * @reg: Register to query.
1852239c9706SDimitris Papastamos  *
1853239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1854239c9706SDimitris Papastamos  */
1855239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1856239c9706SDimitris Papastamos 				    unsigned int reg)
1857239c9706SDimitris Papastamos {
1858239c9706SDimitris Papastamos 	if (codec->writable_register)
1859239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1860239c9706SDimitris Papastamos 	else
186163fa0a28SLars-Peter Clausen 		return 1;
1862239c9706SDimitris Papastamos }
1863239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1864239c9706SDimitris Papastamos 
1865f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1866f1442bc1SLiam Girdwood 					unsigned int reg)
1867f1442bc1SLiam Girdwood {
1868f1442bc1SLiam Girdwood 	unsigned int ret;
1869f1442bc1SLiam Girdwood 
1870f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1871f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1872f1442bc1SLiam Girdwood 		return -1;
1873f1442bc1SLiam Girdwood 	}
1874f1442bc1SLiam Girdwood 
1875f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1876f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1877a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1878f1442bc1SLiam Girdwood 
1879f1442bc1SLiam Girdwood 	return ret;
1880f1442bc1SLiam Girdwood }
1881f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1882f1442bc1SLiam Girdwood 
1883f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1884f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1885f1442bc1SLiam Girdwood {
1886f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1887f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1888f1442bc1SLiam Girdwood 		return -1;
1889f1442bc1SLiam Girdwood 	}
1890f1442bc1SLiam Girdwood 
1891f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1892a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
1893f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1894f1442bc1SLiam Girdwood }
1895f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1896f1442bc1SLiam Girdwood 
1897239c9706SDimitris Papastamos /**
1898db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1899db2a4165SFrank Mandarino  * @codec: audio codec
1900db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1901db2a4165SFrank Mandarino  * @num: AC97 codec number
1902db2a4165SFrank Mandarino  *
1903db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1904db2a4165SFrank Mandarino  */
1905db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1906db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1907db2a4165SFrank Mandarino {
1908db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1909db2a4165SFrank Mandarino 
1910db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1911db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1912db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1913db2a4165SFrank Mandarino 		return -ENOMEM;
1914db2a4165SFrank Mandarino 	}
1915db2a4165SFrank Mandarino 
1916db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1917db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1918db2a4165SFrank Mandarino 		kfree(codec->ac97);
1919db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1920db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1921db2a4165SFrank Mandarino 		return -ENOMEM;
1922db2a4165SFrank Mandarino 	}
1923db2a4165SFrank Mandarino 
1924db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1925db2a4165SFrank Mandarino 	codec->ac97->num = num;
19260562f788SMika Westerberg 
19270562f788SMika Westerberg 	/*
19280562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
19290562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
19300562f788SMika Westerberg 	 */
19310562f788SMika Westerberg 	codec->ac97_created = 1;
19320562f788SMika Westerberg 
1933db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1934db2a4165SFrank Mandarino 	return 0;
1935db2a4165SFrank Mandarino }
1936db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1937db2a4165SFrank Mandarino 
1938db2a4165SFrank Mandarino /**
1939db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1940db2a4165SFrank Mandarino  * @codec: audio codec
1941db2a4165SFrank Mandarino  *
1942db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1943db2a4165SFrank Mandarino  */
1944db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1945db2a4165SFrank Mandarino {
1946db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1947f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1948f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1949f0fba2adSLiam Girdwood #endif
1950db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1951db2a4165SFrank Mandarino 	kfree(codec->ac97);
1952db2a4165SFrank Mandarino 	codec->ac97 = NULL;
19530562f788SMika Westerberg 	codec->ac97_created = 0;
1954db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1955db2a4165SFrank Mandarino }
1956db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1957db2a4165SFrank Mandarino 
1958c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1959c3753707SMark Brown {
1960c3753707SMark Brown 	unsigned int ret;
1961c3753707SMark Brown 
1962c3acec26SMark Brown 	ret = codec->read(codec, reg);
1963c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1964a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1965c3753707SMark Brown 
1966c3753707SMark Brown 	return ret;
1967c3753707SMark Brown }
1968c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1969c3753707SMark Brown 
1970c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1971c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1972c3753707SMark Brown {
1973c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1974a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1975c3acec26SMark Brown 	return codec->write(codec, reg, val);
1976c3753707SMark Brown }
1977c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1978c3753707SMark Brown 
19795fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
19805fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
19815fb609d4SDimitris Papastamos {
19825fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
19835fb609d4SDimitris Papastamos }
19845fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
19855fb609d4SDimitris Papastamos 
1986db2a4165SFrank Mandarino /**
1987db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1988db2a4165SFrank Mandarino  * @codec: audio codec
1989db2a4165SFrank Mandarino  * @reg: codec register
1990db2a4165SFrank Mandarino  * @mask: register mask
1991db2a4165SFrank Mandarino  * @value: new value
1992db2a4165SFrank Mandarino  *
1993db2a4165SFrank Mandarino  * Writes new register value.
1994db2a4165SFrank Mandarino  *
1995180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
1996db2a4165SFrank Mandarino  */
1997db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
199846f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1999db2a4165SFrank Mandarino {
20008a713da8SMark Brown 	bool change;
200146f5822fSDaniel Ribeiro 	unsigned int old, new;
2002180c329dSTimur Tabi 	int ret;
2003db2a4165SFrank Mandarino 
20048a713da8SMark Brown 	if (codec->using_regmap) {
20058a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
20068a713da8SMark Brown 					       mask, value, &change);
20078a713da8SMark Brown 	} else {
2008180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
2009180c329dSTimur Tabi 		if (ret < 0)
2010180c329dSTimur Tabi 			return ret;
2011180c329dSTimur Tabi 
2012180c329dSTimur Tabi 		old = ret;
201378bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
2014db2a4165SFrank Mandarino 		change = old != new;
20158a713da8SMark Brown 		if (change)
2016180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
20178a713da8SMark Brown 	}
20188a713da8SMark Brown 
2019180c329dSTimur Tabi 	if (ret < 0)
2020180c329dSTimur Tabi 		return ret;
2021db2a4165SFrank Mandarino 
2022db2a4165SFrank Mandarino 	return change;
2023db2a4165SFrank Mandarino }
2024db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2025db2a4165SFrank Mandarino 
2026db2a4165SFrank Mandarino /**
20276c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
20286c508c62SEero Nurkkala  * @codec: audio codec
20296c508c62SEero Nurkkala  * @reg: codec register
20306c508c62SEero Nurkkala  * @mask: register mask
20316c508c62SEero Nurkkala  * @value: new value
20326c508c62SEero Nurkkala  *
20336c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
20346c508c62SEero Nurkkala  *
20356c508c62SEero Nurkkala  * Returns 1 for change else 0.
20366c508c62SEero Nurkkala  */
2037dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
20386c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
20396c508c62SEero Nurkkala 			       unsigned int value)
20406c508c62SEero Nurkkala {
20416c508c62SEero Nurkkala 	int change;
20426c508c62SEero Nurkkala 
20436c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
20446c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
20456c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
20466c508c62SEero Nurkkala 
20476c508c62SEero Nurkkala 	return change;
20486c508c62SEero Nurkkala }
2049dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
20506c508c62SEero Nurkkala 
20516c508c62SEero Nurkkala /**
2052db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2053db2a4165SFrank Mandarino  * @codec: audio codec
2054db2a4165SFrank Mandarino  * @reg: codec register
2055db2a4165SFrank Mandarino  * @mask: register mask
2056db2a4165SFrank Mandarino  * @value: new value
2057db2a4165SFrank Mandarino  *
2058db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2059db2a4165SFrank Mandarino  * different from the old value.
2060db2a4165SFrank Mandarino  *
2061db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2062db2a4165SFrank Mandarino  */
2063db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
206446f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2065db2a4165SFrank Mandarino {
2066db2a4165SFrank Mandarino 	int change;
206746f5822fSDaniel Ribeiro 	unsigned int old, new;
2068db2a4165SFrank Mandarino 
2069db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2070db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2071db2a4165SFrank Mandarino 	change = old != new;
2072db2a4165SFrank Mandarino 
2073db2a4165SFrank Mandarino 	return change;
2074db2a4165SFrank Mandarino }
2075db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2076db2a4165SFrank Mandarino 
2077db2a4165SFrank Mandarino /**
2078db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2079db2a4165SFrank Mandarino  * @substream: the pcm substream
2080db2a4165SFrank Mandarino  * @hw: the hardware parameters
2081db2a4165SFrank Mandarino  *
2082db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2083db2a4165SFrank Mandarino  */
2084db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2085db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2086db2a4165SFrank Mandarino {
2087db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2088db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2089db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2090db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2091db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2092db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2093db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2094db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2095db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2096db2a4165SFrank Mandarino 	return 0;
2097db2a4165SFrank Mandarino }
2098db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2099db2a4165SFrank Mandarino 
2100db2a4165SFrank Mandarino /**
2101db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2102db2a4165SFrank Mandarino  * @_template: control template
2103db2a4165SFrank Mandarino  * @data: control private data
2104ac11a2b3SMark Brown  * @long_name: control long name
2105efb7ac3fSMark Brown  * @prefix: control name prefix
2106db2a4165SFrank Mandarino  *
2107db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2108db2a4165SFrank Mandarino  *
2109db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2110db2a4165SFrank Mandarino  */
2111db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21123056557fSMark Brown 				  void *data, const char *long_name,
2113efb7ac3fSMark Brown 				  const char *prefix)
2114db2a4165SFrank Mandarino {
2115db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2116efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2117efb7ac3fSMark Brown 	char *name = NULL;
2118efb7ac3fSMark Brown 	int name_len;
2119db2a4165SFrank Mandarino 
2120db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2121db2a4165SFrank Mandarino 	template.index = 0;
2122db2a4165SFrank Mandarino 
2123efb7ac3fSMark Brown 	if (!long_name)
2124efb7ac3fSMark Brown 		long_name = template.name;
2125efb7ac3fSMark Brown 
2126efb7ac3fSMark Brown 	if (prefix) {
2127efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
212857cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2129efb7ac3fSMark Brown 		if (!name)
2130efb7ac3fSMark Brown 			return NULL;
2131efb7ac3fSMark Brown 
2132efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2133efb7ac3fSMark Brown 
2134efb7ac3fSMark Brown 		template.name = name;
2135efb7ac3fSMark Brown 	} else {
2136efb7ac3fSMark Brown 		template.name = long_name;
2137efb7ac3fSMark Brown 	}
2138efb7ac3fSMark Brown 
2139efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2140efb7ac3fSMark Brown 
2141efb7ac3fSMark Brown 	kfree(name);
2142efb7ac3fSMark Brown 
2143efb7ac3fSMark Brown 	return kcontrol;
2144db2a4165SFrank Mandarino }
2145db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2146db2a4165SFrank Mandarino 
2147022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2148022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2149022658beSLiam Girdwood 	const char *prefix, void *data)
2150022658beSLiam Girdwood {
2151022658beSLiam Girdwood 	int err, i;
2152022658beSLiam Girdwood 
2153022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2154022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2155022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2156022658beSLiam Girdwood 						     control->name, prefix));
2157022658beSLiam Girdwood 		if (err < 0) {
2158022658beSLiam Girdwood 			dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2159022658beSLiam Girdwood 			return err;
2160022658beSLiam Girdwood 		}
2161022658beSLiam Girdwood 	}
2162022658beSLiam Girdwood 
2163022658beSLiam Girdwood 	return 0;
2164022658beSLiam Girdwood }
2165022658beSLiam Girdwood 
2166db2a4165SFrank Mandarino /**
2167022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2168022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
21693e8e1952SIan Molton  * duplicating this code.
21703e8e1952SIan Molton  *
21713e8e1952SIan Molton  * @codec: codec to add controls to
21723e8e1952SIan Molton  * @controls: array of controls to add
21733e8e1952SIan Molton  * @num_controls: number of elements in the array
21743e8e1952SIan Molton  *
21753e8e1952SIan Molton  * Return 0 for success, else error.
21763e8e1952SIan Molton  */
2177022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
21783e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
21793e8e1952SIan Molton {
2180f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
21813e8e1952SIan Molton 
2182022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2183022658beSLiam Girdwood 			codec->name_prefix, codec);
21843e8e1952SIan Molton }
2185022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
21863e8e1952SIan Molton 
21873e8e1952SIan Molton /**
2188a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2189022658beSLiam Girdwood  * Convenience function to add a list of controls.
2190a491a5c8SLiam Girdwood  *
2191a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2192a491a5c8SLiam Girdwood  * @controls: array of controls to add
2193a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2194a491a5c8SLiam Girdwood  *
2195a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2196a491a5c8SLiam Girdwood  */
2197a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2198a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2199a491a5c8SLiam Girdwood {
2200a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2201a491a5c8SLiam Girdwood 
2202022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2203022658beSLiam Girdwood 			NULL, platform);
2204a491a5c8SLiam Girdwood }
2205a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2206a491a5c8SLiam Girdwood 
2207a491a5c8SLiam Girdwood /**
2208022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2209022658beSLiam Girdwood  * Convenience function to add a list of controls.
2210022658beSLiam Girdwood  *
2211022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2212022658beSLiam Girdwood  * @controls: array of controls to add
2213022658beSLiam Girdwood  * @num_controls: number of elements in the array
2214022658beSLiam Girdwood  *
2215022658beSLiam Girdwood  * Return 0 for success, else error.
2216022658beSLiam Girdwood  */
2217022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2218022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2219022658beSLiam Girdwood {
2220022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2221022658beSLiam Girdwood 
2222022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2223022658beSLiam Girdwood 			NULL, soc_card);
2224022658beSLiam Girdwood }
2225022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2226022658beSLiam Girdwood 
2227022658beSLiam Girdwood /**
2228022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2229022658beSLiam Girdwood  * Convienience function to add a list of controls.
2230022658beSLiam Girdwood  *
2231022658beSLiam Girdwood  * @dai: DAI to add controls to
2232022658beSLiam Girdwood  * @controls: array of controls to add
2233022658beSLiam Girdwood  * @num_controls: number of elements in the array
2234022658beSLiam Girdwood  *
2235022658beSLiam Girdwood  * Return 0 for success, else error.
2236022658beSLiam Girdwood  */
2237022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2238022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2239022658beSLiam Girdwood {
2240022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2241022658beSLiam Girdwood 
2242022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2243022658beSLiam Girdwood 			NULL, dai);
2244022658beSLiam Girdwood }
2245022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2246022658beSLiam Girdwood 
2247022658beSLiam Girdwood /**
2248db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2249db2a4165SFrank Mandarino  * @kcontrol: mixer control
2250db2a4165SFrank Mandarino  * @uinfo: control element information
2251db2a4165SFrank Mandarino  *
2252db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2253db2a4165SFrank Mandarino  * mixer control.
2254db2a4165SFrank Mandarino  *
2255db2a4165SFrank Mandarino  * Returns 0 for success.
2256db2a4165SFrank Mandarino  */
2257db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2258db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2259db2a4165SFrank Mandarino {
2260db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2261db2a4165SFrank Mandarino 
2262db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2263db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2264f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2265db2a4165SFrank Mandarino 
2266f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2267f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2268db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2269db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2270db2a4165SFrank Mandarino 	return 0;
2271db2a4165SFrank Mandarino }
2272db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2273db2a4165SFrank Mandarino 
2274db2a4165SFrank Mandarino /**
2275db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2276db2a4165SFrank Mandarino  * @kcontrol: mixer control
2277ac11a2b3SMark Brown  * @ucontrol: control element information
2278db2a4165SFrank Mandarino  *
2279db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2280db2a4165SFrank Mandarino  *
2281db2a4165SFrank Mandarino  * Returns 0 for success.
2282db2a4165SFrank Mandarino  */
2283db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2284db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2285db2a4165SFrank Mandarino {
2286db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2287db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
228846f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2289db2a4165SFrank Mandarino 
2290f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2291db2a4165SFrank Mandarino 		;
2292db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
22933ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
22943ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2295db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2296db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2297db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2298db2a4165SFrank Mandarino 
2299db2a4165SFrank Mandarino 	return 0;
2300db2a4165SFrank Mandarino }
2301db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2302db2a4165SFrank Mandarino 
2303db2a4165SFrank Mandarino /**
2304db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2305db2a4165SFrank Mandarino  * @kcontrol: mixer control
2306ac11a2b3SMark Brown  * @ucontrol: control element information
2307db2a4165SFrank Mandarino  *
2308db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2309db2a4165SFrank Mandarino  *
2310db2a4165SFrank Mandarino  * Returns 0 for success.
2311db2a4165SFrank Mandarino  */
2312db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2313db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2314db2a4165SFrank Mandarino {
2315db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2316db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
231746f5822fSDaniel Ribeiro 	unsigned int val;
231846f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2319db2a4165SFrank Mandarino 
2320f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2321db2a4165SFrank Mandarino 		;
2322f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2323db2a4165SFrank Mandarino 		return -EINVAL;
2324db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2325db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2326db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2327f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2328db2a4165SFrank Mandarino 			return -EINVAL;
2329db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2330db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2331db2a4165SFrank Mandarino 	}
2332db2a4165SFrank Mandarino 
23336c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2334db2a4165SFrank Mandarino }
2335db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2336db2a4165SFrank Mandarino 
2337db2a4165SFrank Mandarino /**
23382e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
23392e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23402e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23412e72f8e3SPeter Ujfalusi  *
23422e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
23432e72f8e3SPeter Ujfalusi  *
23442e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23452e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23462e72f8e3SPeter Ujfalusi  *
23472e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23482e72f8e3SPeter Ujfalusi  */
23492e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
23502e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23512e72f8e3SPeter Ujfalusi {
23522e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
235374155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
235446f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
23552e72f8e3SPeter Ujfalusi 
23562e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
23572e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
23582e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
23592e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
23602e72f8e3SPeter Ujfalusi 			break;
23612e72f8e3SPeter Ujfalusi 	}
23622e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
23632e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23642e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
23652e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
23662e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
23672e72f8e3SPeter Ujfalusi 				break;
23682e72f8e3SPeter Ujfalusi 		}
23692e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
23702e72f8e3SPeter Ujfalusi 	}
23712e72f8e3SPeter Ujfalusi 
23722e72f8e3SPeter Ujfalusi 	return 0;
23732e72f8e3SPeter Ujfalusi }
23742e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
23752e72f8e3SPeter Ujfalusi 
23762e72f8e3SPeter Ujfalusi /**
23772e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
23782e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23792e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23802e72f8e3SPeter Ujfalusi  *
23812e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
23822e72f8e3SPeter Ujfalusi  *
23832e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23842e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23852e72f8e3SPeter Ujfalusi  *
23862e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23872e72f8e3SPeter Ujfalusi  */
23882e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
23892e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23902e72f8e3SPeter Ujfalusi {
23912e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
239274155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
239346f5822fSDaniel Ribeiro 	unsigned int val;
239446f5822fSDaniel Ribeiro 	unsigned int mask;
23952e72f8e3SPeter Ujfalusi 
23962e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
23972e72f8e3SPeter Ujfalusi 		return -EINVAL;
23982e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
23992e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
24002e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
24012e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
24022e72f8e3SPeter Ujfalusi 			return -EINVAL;
24032e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
24042e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
24052e72f8e3SPeter Ujfalusi 	}
24062e72f8e3SPeter Ujfalusi 
24076c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
24082e72f8e3SPeter Ujfalusi }
24092e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
24102e72f8e3SPeter Ujfalusi 
24112e72f8e3SPeter Ujfalusi /**
2412db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2413db2a4165SFrank Mandarino  * @kcontrol: mixer control
2414db2a4165SFrank Mandarino  * @uinfo: control element information
2415db2a4165SFrank Mandarino  *
2416db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2417db2a4165SFrank Mandarino  * single mixer.
2418db2a4165SFrank Mandarino  *
2419db2a4165SFrank Mandarino  * Returns 0 for success.
2420db2a4165SFrank Mandarino  */
2421db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2422db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2423db2a4165SFrank Mandarino {
2424db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2425db2a4165SFrank Mandarino 
2426db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2427db2a4165SFrank Mandarino 	uinfo->count = 1;
2428f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2429db2a4165SFrank Mandarino 
2430f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2431f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2432db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2433db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2434db2a4165SFrank Mandarino 	return 0;
2435db2a4165SFrank Mandarino }
2436db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2437db2a4165SFrank Mandarino 
2438db2a4165SFrank Mandarino /**
2439db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2440db2a4165SFrank Mandarino  * @kcontrol: mixer control
2441db2a4165SFrank Mandarino  * @uinfo: control element information
2442db2a4165SFrank Mandarino  *
2443db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2444db2a4165SFrank Mandarino  *
2445db2a4165SFrank Mandarino  * Returns 0 for success.
2446db2a4165SFrank Mandarino  */
2447db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2448db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2449db2a4165SFrank Mandarino {
2450a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2451db2a4165SFrank Mandarino 
2452fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2453a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2454a7a4ac86SPhilipp Zabel 	else
2455a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2456a7a4ac86SPhilipp Zabel 
2457db2a4165SFrank Mandarino 	uinfo->count = 1;
2458db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2459a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2460db2a4165SFrank Mandarino 	return 0;
2461db2a4165SFrank Mandarino }
2462db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2463db2a4165SFrank Mandarino 
2464db2a4165SFrank Mandarino /**
2465db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2466db2a4165SFrank Mandarino  * @kcontrol: mixer control
2467db2a4165SFrank Mandarino  * @uinfo: control element information
2468db2a4165SFrank Mandarino  *
2469e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2470e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2471db2a4165SFrank Mandarino  *
2472db2a4165SFrank Mandarino  * Returns 0 for success.
2473db2a4165SFrank Mandarino  */
2474db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2475db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2476db2a4165SFrank Mandarino {
24774eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24784eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2479d11bb4a9SPeter Ujfalusi 	int platform_max;
2480db2a4165SFrank Mandarino 
2481d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2482d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2483d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2484d11bb4a9SPeter Ujfalusi 
2485d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2486a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2487a7a4ac86SPhilipp Zabel 	else
2488a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2489a7a4ac86SPhilipp Zabel 
2490e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2491db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2492d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2493db2a4165SFrank Mandarino 	return 0;
2494db2a4165SFrank Mandarino }
2495db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2496db2a4165SFrank Mandarino 
2497db2a4165SFrank Mandarino /**
2498db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2499db2a4165SFrank Mandarino  * @kcontrol: mixer control
2500ac11a2b3SMark Brown  * @ucontrol: control element information
2501db2a4165SFrank Mandarino  *
2502f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2503f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2504db2a4165SFrank Mandarino  *
2505db2a4165SFrank Mandarino  * Returns 0 for success.
2506db2a4165SFrank Mandarino  */
2507db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2508db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2509db2a4165SFrank Mandarino {
25104eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25114eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2512db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2513815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2514f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2515815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2516815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25174eaa9819SJon Smirl 	int max = mc->max;
2518815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2519815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2520db2a4165SFrank Mandarino 
2521db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2522db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2523f7915d99SPeter Ujfalusi 	if (invert)
2524db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2525a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2526f7915d99SPeter Ujfalusi 
2527f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2528f7915d99SPeter Ujfalusi 		if (reg == reg2)
2529f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2530f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2531f7915d99SPeter Ujfalusi 		else
2532f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2533f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2534f7915d99SPeter Ujfalusi 		if (invert)
2535db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2536a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2537db2a4165SFrank Mandarino 	}
2538db2a4165SFrank Mandarino 
2539db2a4165SFrank Mandarino 	return 0;
2540db2a4165SFrank Mandarino }
2541db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2542db2a4165SFrank Mandarino 
2543db2a4165SFrank Mandarino /**
2544db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2545db2a4165SFrank Mandarino  * @kcontrol: mixer control
2546ac11a2b3SMark Brown  * @ucontrol: control element information
2547db2a4165SFrank Mandarino  *
2548974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2549974815baSPeter Ujfalusi  * control that spans 2 registers.
2550db2a4165SFrank Mandarino  *
2551db2a4165SFrank Mandarino  * Returns 0 for success.
2552db2a4165SFrank Mandarino  */
2553db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2554db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2555db2a4165SFrank Mandarino {
25564eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25574eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2558db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2559815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2560974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2561815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2562815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
25634eaa9819SJon Smirl 	int max = mc->max;
2564815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2565815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2566974815baSPeter Ujfalusi 	int err;
2567974815baSPeter Ujfalusi 	bool type_2r = 0;
2568974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2569974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2570db2a4165SFrank Mandarino 
2571db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2572db2a4165SFrank Mandarino 	if (invert)
2573a7a4ac86SPhilipp Zabel 		val = max - val;
2574db2a4165SFrank Mandarino 	val_mask = mask << shift;
2575db2a4165SFrank Mandarino 	val = val << shift;
2576974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2577db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2578db2a4165SFrank Mandarino 		if (invert)
2579a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2580974815baSPeter Ujfalusi 		if (reg == reg2) {
2581db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2582db2a4165SFrank Mandarino 			val |= val2 << rshift;
2583974815baSPeter Ujfalusi 		} else {
2584db2a4165SFrank Mandarino 			val2 = val2 << shift;
2585974815baSPeter Ujfalusi 			type_2r = 1;
2586974815baSPeter Ujfalusi 		}
2587974815baSPeter Ujfalusi 	}
25886c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
25893ff3f64bSMark Brown 	if (err < 0)
2590db2a4165SFrank Mandarino 		return err;
2591db2a4165SFrank Mandarino 
2592974815baSPeter Ujfalusi 	if (type_2r)
25936c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2594974815baSPeter Ujfalusi 
2595db2a4165SFrank Mandarino 	return err;
2596db2a4165SFrank Mandarino }
2597974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2598db2a4165SFrank Mandarino 
2599e13ac2e9SMark Brown /**
26001d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
26011d99f243SBrian Austin  * @kcontrol: mixer control
26021d99f243SBrian Austin  * @ucontrol: control element information
26031d99f243SBrian Austin  *
26041d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
26051d99f243SBrian Austin  * control that spans 2 registers.
26061d99f243SBrian Austin  *
26071d99f243SBrian Austin  * Returns 0 for success.
26081d99f243SBrian Austin  */
26091d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
26101d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
26111d99f243SBrian Austin {
26121d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
26131d99f243SBrian Austin 	struct soc_mixer_control *mc =
26141d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
26151d99f243SBrian Austin 
26161d99f243SBrian Austin 	unsigned int reg = mc->reg;
26171d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
26181d99f243SBrian Austin 	unsigned int shift = mc->shift;
26191d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
26201d99f243SBrian Austin 	int max = mc->max;
26211d99f243SBrian Austin 	int min = mc->min;
26221d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
26231d99f243SBrian Austin 
26241d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
26251d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
26261d99f243SBrian Austin 
26271d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
26281d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
26291d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
26301d99f243SBrian Austin 
26311d99f243SBrian Austin 	return 0;
26321d99f243SBrian Austin }
26331d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
26341d99f243SBrian Austin 
26351d99f243SBrian Austin /**
26361d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
26371d99f243SBrian Austin  * @kcontrol: mixer control
26381d99f243SBrian Austin  * @uinfo: control element information
26391d99f243SBrian Austin  *
26401d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
26411d99f243SBrian Austin  *
26421d99f243SBrian Austin  * Returns 0 for success.
26431d99f243SBrian Austin  */
26441d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
26451d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
26461d99f243SBrian Austin {
26471d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
26481d99f243SBrian Austin 	struct soc_mixer_control *mc =
26491d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
26501d99f243SBrian Austin 
26511d99f243SBrian Austin 	unsigned int reg = mc->reg;
26521d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
26531d99f243SBrian Austin 	unsigned int shift = mc->shift;
26541d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
26551d99f243SBrian Austin 	int max = mc->max;
26561d99f243SBrian Austin 	int min = mc->min;
26571d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
265827f1d759SBrian Austin 	int err = 0;
26591d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
26601d99f243SBrian Austin 
26611d99f243SBrian Austin 	val_mask = mask << shift;
26621d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
26631d99f243SBrian Austin 	val = val << shift;
26641d99f243SBrian Austin 
26651d99f243SBrian Austin 	if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
26661d99f243SBrian Austin 			return err;
26671d99f243SBrian Austin 
26681d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
26691d99f243SBrian Austin 		val_mask = mask << rshift;
26701d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
26711d99f243SBrian Austin 		val2 = val2 << rshift;
26721d99f243SBrian Austin 
26731d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
26741d99f243SBrian Austin 			return err;
26751d99f243SBrian Austin 	}
26761d99f243SBrian Austin 	return 0;
26771d99f243SBrian Austin }
26781d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
26791d99f243SBrian Austin 
26801d99f243SBrian Austin /**
2681e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2682e13ac2e9SMark Brown  * @kcontrol: mixer control
2683e13ac2e9SMark Brown  * @uinfo: control element information
2684e13ac2e9SMark Brown  *
2685e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2686e13ac2e9SMark Brown  *
2687e13ac2e9SMark Brown  * Returns 0 for success.
2688e13ac2e9SMark Brown  */
2689e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2690e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2691e13ac2e9SMark Brown {
26924eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26934eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2694d11bb4a9SPeter Ujfalusi 	int platform_max;
26954eaa9819SJon Smirl 	int min = mc->min;
2696e13ac2e9SMark Brown 
2697d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2698d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2699d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2700d11bb4a9SPeter Ujfalusi 
2701e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2702e13ac2e9SMark Brown 	uinfo->count = 2;
2703e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2704d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2705e13ac2e9SMark Brown 	return 0;
2706e13ac2e9SMark Brown }
2707e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2708e13ac2e9SMark Brown 
2709e13ac2e9SMark Brown /**
2710e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2711e13ac2e9SMark Brown  * @kcontrol: mixer control
2712ac11a2b3SMark Brown  * @ucontrol: control element information
2713e13ac2e9SMark Brown  *
2714e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2715e13ac2e9SMark Brown  *
2716e13ac2e9SMark Brown  * Returns 0 for success.
2717e13ac2e9SMark Brown  */
2718e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2719e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2720e13ac2e9SMark Brown {
27214eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27224eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2723e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2724815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27254eaa9819SJon Smirl 	int min = mc->min;
2726e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2727e13ac2e9SMark Brown 
2728e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2729e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2730e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2731e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2732e13ac2e9SMark Brown 	return 0;
2733e13ac2e9SMark Brown }
2734e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2735e13ac2e9SMark Brown 
2736e13ac2e9SMark Brown /**
2737e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2738e13ac2e9SMark Brown  * @kcontrol: mixer control
2739ac11a2b3SMark Brown  * @ucontrol: control element information
2740e13ac2e9SMark Brown  *
2741e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2742e13ac2e9SMark Brown  *
2743e13ac2e9SMark Brown  * Returns 0 for success.
2744e13ac2e9SMark Brown  */
2745e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2746e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2747e13ac2e9SMark Brown {
27484eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27494eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2750e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2751815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
27524eaa9819SJon Smirl 	int min = mc->min;
275346f5822fSDaniel Ribeiro 	unsigned int val;
2754e13ac2e9SMark Brown 
2755e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2756e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2757e13ac2e9SMark Brown 
27586c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2759e13ac2e9SMark Brown }
2760e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2761e13ac2e9SMark Brown 
27628c6529dbSLiam Girdwood /**
2763637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2764637d3847SPeter Ujfalusi  *
2765637d3847SPeter Ujfalusi  * @codec: where to look for the control
2766637d3847SPeter Ujfalusi  * @name: Name of the control
2767637d3847SPeter Ujfalusi  * @max: new maximum limit
2768637d3847SPeter Ujfalusi  *
2769637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2770637d3847SPeter Ujfalusi  */
2771637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2772637d3847SPeter Ujfalusi 	const char *name, int max)
2773637d3847SPeter Ujfalusi {
2774f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2775637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2776637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2777637d3847SPeter Ujfalusi 	int found = 0;
2778637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2779637d3847SPeter Ujfalusi 
2780637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2781637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2782637d3847SPeter Ujfalusi 		return -EINVAL;
2783637d3847SPeter Ujfalusi 
2784637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2785637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2786637d3847SPeter Ujfalusi 			found = 1;
2787637d3847SPeter Ujfalusi 			break;
2788637d3847SPeter Ujfalusi 		}
2789637d3847SPeter Ujfalusi 	}
2790637d3847SPeter Ujfalusi 	if (found) {
2791637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2792637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2793d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2794637d3847SPeter Ujfalusi 			ret = 0;
2795637d3847SPeter Ujfalusi 		}
2796637d3847SPeter Ujfalusi 	}
2797637d3847SPeter Ujfalusi 	return ret;
2798637d3847SPeter Ujfalusi }
2799637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2800637d3847SPeter Ujfalusi 
280171d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
280271d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
280371d08516SMark Brown {
280471d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
280571d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
280671d08516SMark Brown 
280771d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
280871d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
280971d08516SMark Brown 
281071d08516SMark Brown 	return 0;
281171d08516SMark Brown }
281271d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
281371d08516SMark Brown 
281471d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
281571d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
281671d08516SMark Brown {
281771d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
281871d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
281971d08516SMark Brown 	int ret;
282071d08516SMark Brown 
282171d08516SMark Brown 	if (codec->using_regmap)
282271d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
282371d08516SMark Brown 				      ucontrol->value.bytes.data,
282471d08516SMark Brown 				      params->num_regs * codec->val_bytes);
282571d08516SMark Brown 	else
282671d08516SMark Brown 		ret = -EINVAL;
282771d08516SMark Brown 
2828f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
2829f831b055SMark Brown 	if (ret == 0 && params->mask) {
2830f831b055SMark Brown 		switch (codec->val_bytes) {
2831f831b055SMark Brown 		case 1:
2832f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
2833f831b055SMark Brown 			break;
2834f831b055SMark Brown 		case 2:
2835f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
2836f831b055SMark Brown 				&= ~params->mask;
2837f831b055SMark Brown 			break;
2838f831b055SMark Brown 		case 4:
2839f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
2840f831b055SMark Brown 				&= ~params->mask;
2841f831b055SMark Brown 			break;
2842f831b055SMark Brown 		default:
2843f831b055SMark Brown 			return -EINVAL;
2844f831b055SMark Brown 		}
2845f831b055SMark Brown 	}
2846f831b055SMark Brown 
284771d08516SMark Brown 	return ret;
284871d08516SMark Brown }
284971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
285071d08516SMark Brown 
285171d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
285271d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
285371d08516SMark Brown {
285471d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
285571d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2856f831b055SMark Brown 	int ret, len;
2857f831b055SMark Brown 	unsigned int val;
2858f831b055SMark Brown 	void *data;
285971d08516SMark Brown 
2860f831b055SMark Brown 	if (!codec->using_regmap)
2861f831b055SMark Brown 		return -EINVAL;
2862f831b055SMark Brown 
2863f831b055SMark Brown 	data = ucontrol->value.bytes.data;
2864f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
2865f831b055SMark Brown 
2866f831b055SMark Brown 	/*
2867f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
2868f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
2869f831b055SMark Brown 	 * copy.
2870f831b055SMark Brown 	 */
2871f831b055SMark Brown 	if (params->mask) {
2872f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
2873f831b055SMark Brown 		if (ret != 0)
2874f831b055SMark Brown 			return ret;
2875f831b055SMark Brown 
2876f831b055SMark Brown 		val &= params->mask;
2877f831b055SMark Brown 
2878f831b055SMark Brown 		data = kmemdup(data, len, GFP_KERNEL);
2879f831b055SMark Brown 		if (!data)
2880f831b055SMark Brown 			return -ENOMEM;
2881f831b055SMark Brown 
2882f831b055SMark Brown 		switch (codec->val_bytes) {
2883f831b055SMark Brown 		case 1:
2884f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
2885f831b055SMark Brown 			((u8 *)data)[0] |= val;
2886f831b055SMark Brown 			break;
2887f831b055SMark Brown 		case 2:
2888f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2889f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
2890f831b055SMark Brown 			break;
2891f831b055SMark Brown 		case 4:
2892f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2893f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
2894f831b055SMark Brown 			break;
2895f831b055SMark Brown 		default:
2896f831b055SMark Brown 			return -EINVAL;
2897f831b055SMark Brown 		}
2898f831b055SMark Brown 	}
2899f831b055SMark Brown 
290071d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
2901f831b055SMark Brown 			       data, len);
2902f831b055SMark Brown 
2903f831b055SMark Brown 	if (params->mask)
2904f831b055SMark Brown 		kfree(data);
290571d08516SMark Brown 
290671d08516SMark Brown 	return ret;
290771d08516SMark Brown }
290871d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
290971d08516SMark Brown 
2910b6f4bb38Sapatard@mandriva.com /**
29114183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
29124183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
29134183eed2SKristoffer KARLSSON  * @uinfo: control element information
29144183eed2SKristoffer KARLSSON  *
29154183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
29164183eed2SKristoffer KARLSSON  * span multiple codec registers which together
29174183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
29184183eed2SKristoffer KARLSSON  *
29194183eed2SKristoffer KARLSSON  * Returns 0 for success.
29204183eed2SKristoffer KARLSSON  */
29214183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
29224183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
29234183eed2SKristoffer KARLSSON {
29244183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
29254183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
29264183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29274183eed2SKristoffer KARLSSON 	uinfo->count = 1;
29284183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
29294183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
29304183eed2SKristoffer KARLSSON 
29314183eed2SKristoffer KARLSSON 	return 0;
29324183eed2SKristoffer KARLSSON }
29334183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
29344183eed2SKristoffer KARLSSON 
29354183eed2SKristoffer KARLSSON /**
29364183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
29374183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
29384183eed2SKristoffer KARLSSON  * @ucontrol: control element information
29394183eed2SKristoffer KARLSSON  *
29404183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
29414183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
29424183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
29434183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
29444183eed2SKristoffer KARLSSON  * across the multiple codec registers.
29454183eed2SKristoffer KARLSSON  *
29464183eed2SKristoffer KARLSSON  * Returns 0 for success.
29474183eed2SKristoffer KARLSSON  */
29484183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
29494183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
29504183eed2SKristoffer KARLSSON {
29514183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
29524183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
29534183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29544183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
29554183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
29564183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
29574183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
29584183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
29594183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
29604183eed2SKristoffer KARLSSON 	long min = mc->min;
29614183eed2SKristoffer KARLSSON 	long max = mc->max;
29624183eed2SKristoffer KARLSSON 	long val = 0;
29634183eed2SKristoffer KARLSSON 	unsigned long regval;
29644183eed2SKristoffer KARLSSON 	unsigned int i;
29654183eed2SKristoffer KARLSSON 
29664183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
29674183eed2SKristoffer KARLSSON 		regval = snd_soc_read(codec, regbase+i) & regwmask;
29684183eed2SKristoffer KARLSSON 		val |= regval << (regwshift*(regcount-i-1));
29694183eed2SKristoffer KARLSSON 	}
29704183eed2SKristoffer KARLSSON 	val &= mask;
29714183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
29724183eed2SKristoffer KARLSSON 		val |= ~mask;
29734183eed2SKristoffer KARLSSON 	if (invert)
29744183eed2SKristoffer KARLSSON 		val = max - val;
29754183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
29764183eed2SKristoffer KARLSSON 
29774183eed2SKristoffer KARLSSON 	return 0;
29784183eed2SKristoffer KARLSSON }
29794183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
29804183eed2SKristoffer KARLSSON 
29814183eed2SKristoffer KARLSSON /**
29824183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
29834183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
29844183eed2SKristoffer KARLSSON  * @ucontrol: control element information
29854183eed2SKristoffer KARLSSON  *
29864183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
29874183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
29884183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
29894183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
29904183eed2SKristoffer KARLSSON  * across the multiple codec registers.
29914183eed2SKristoffer KARLSSON  *
29924183eed2SKristoffer KARLSSON  * Returns 0 for success.
29934183eed2SKristoffer KARLSSON  */
29944183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
29954183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
29964183eed2SKristoffer KARLSSON {
29974183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
29984183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
29994183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
30004183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
30014183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
30024183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
30034183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
30044183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
30054183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
30064183eed2SKristoffer KARLSSON 	long max = mc->max;
30074183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
30084183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
30094183eed2SKristoffer KARLSSON 	int err;
30104183eed2SKristoffer KARLSSON 
30114183eed2SKristoffer KARLSSON 	if (invert)
30124183eed2SKristoffer KARLSSON 		val = max - val;
30134183eed2SKristoffer KARLSSON 	val &= mask;
30144183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
30154183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
30164183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
30174183eed2SKristoffer KARLSSON 		err = snd_soc_update_bits_locked(codec, regbase+i,
30184183eed2SKristoffer KARLSSON 				regmask, regval);
30194183eed2SKristoffer KARLSSON 		if (err < 0)
30204183eed2SKristoffer KARLSSON 			return err;
30214183eed2SKristoffer KARLSSON 	}
30224183eed2SKristoffer KARLSSON 
30234183eed2SKristoffer KARLSSON 	return 0;
30244183eed2SKristoffer KARLSSON }
30254183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
30264183eed2SKristoffer KARLSSON 
30274183eed2SKristoffer KARLSSON /**
3028dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3029dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3030dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3031dd7b10b3SKristoffer KARLSSON  *
3032dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3033dd7b10b3SKristoffer KARLSSON  *
3034dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3035dd7b10b3SKristoffer KARLSSON  */
3036dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3037dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3038dd7b10b3SKristoffer KARLSSON {
3039dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3040dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3041dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3042dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3043dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3044dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3045dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3046dd7b10b3SKristoffer KARLSSON 	unsigned int val = snd_soc_read(codec, reg) & mask;
3047dd7b10b3SKristoffer KARLSSON 
3048dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3049dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3050dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3051dd7b10b3SKristoffer KARLSSON 
3052dd7b10b3SKristoffer KARLSSON 	return 0;
3053dd7b10b3SKristoffer KARLSSON }
3054dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3055dd7b10b3SKristoffer KARLSSON 
3056dd7b10b3SKristoffer KARLSSON /**
3057dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3058dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3059dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3060dd7b10b3SKristoffer KARLSSON  *
3061dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3062dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3063dd7b10b3SKristoffer KARLSSON  *
3064dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3065dd7b10b3SKristoffer KARLSSON  */
3066dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3067dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3068dd7b10b3SKristoffer KARLSSON {
3069dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3070dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3071dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3072dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3073dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3074dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3075dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3076dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3077dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3078dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3079dd7b10b3SKristoffer KARLSSON 	int err;
3080dd7b10b3SKristoffer KARLSSON 
3081dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3082dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3083dd7b10b3SKristoffer KARLSSON 		return err;
3084dd7b10b3SKristoffer KARLSSON 
3085dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3086dd7b10b3SKristoffer KARLSSON 	return err;
3087dd7b10b3SKristoffer KARLSSON }
3088dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3089dd7b10b3SKristoffer KARLSSON 
3090dd7b10b3SKristoffer KARLSSON /**
30918c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
30928c6529dbSLiam Girdwood  * @dai: DAI
30938c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
30948c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
30958c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
30968c6529dbSLiam Girdwood  *
30978c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
30988c6529dbSLiam Girdwood  */
30998c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
31008c6529dbSLiam Girdwood 	unsigned int freq, int dir)
31018c6529dbSLiam Girdwood {
3102f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3103f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3104ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3105da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3106ec4ee52aSMark Brown 						      freq, dir);
31078c6529dbSLiam Girdwood 	else
31088c6529dbSLiam Girdwood 		return -EINVAL;
31098c6529dbSLiam Girdwood }
31108c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
31118c6529dbSLiam Girdwood 
31128c6529dbSLiam Girdwood /**
3113ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3114ec4ee52aSMark Brown  * @codec: CODEC
3115ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3116da1c6ea6SMark Brown  * @source: Source for the clock
3117ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3118ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3119ec4ee52aSMark Brown  *
3120ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3121ec4ee52aSMark Brown  */
3122ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3123da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3124ec4ee52aSMark Brown {
3125ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3126da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3127da1c6ea6SMark Brown 						 freq, dir);
3128ec4ee52aSMark Brown 	else
3129ec4ee52aSMark Brown 		return -EINVAL;
3130ec4ee52aSMark Brown }
3131ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3132ec4ee52aSMark Brown 
3133ec4ee52aSMark Brown /**
31348c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
31358c6529dbSLiam Girdwood  * @dai: DAI
3136ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
31378c6529dbSLiam Girdwood  * @div: new clock divisor.
31388c6529dbSLiam Girdwood  *
31398c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
31408c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
31418c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
31428c6529dbSLiam Girdwood  */
31438c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
31448c6529dbSLiam Girdwood 	int div_id, int div)
31458c6529dbSLiam Girdwood {
3146f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3147f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
31488c6529dbSLiam Girdwood 	else
31498c6529dbSLiam Girdwood 		return -EINVAL;
31508c6529dbSLiam Girdwood }
31518c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
31528c6529dbSLiam Girdwood 
31538c6529dbSLiam Girdwood /**
31548c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
31558c6529dbSLiam Girdwood  * @dai: DAI
31568c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
315785488037SMark Brown  * @source: DAI specific source for the PLL
31588c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
31598c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
31608c6529dbSLiam Girdwood  *
31618c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
31628c6529dbSLiam Girdwood  */
316385488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
316485488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
31658c6529dbSLiam Girdwood {
3166f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3167f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
316885488037SMark Brown 					 freq_in, freq_out);
3169ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3170ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3171ec4ee52aSMark Brown 						   freq_in, freq_out);
31728c6529dbSLiam Girdwood 	else
31738c6529dbSLiam Girdwood 		return -EINVAL;
31748c6529dbSLiam Girdwood }
31758c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
31768c6529dbSLiam Girdwood 
3177ec4ee52aSMark Brown /*
3178ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3179ec4ee52aSMark Brown  * @codec: CODEC
3180ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3181ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3182ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3183ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3184ec4ee52aSMark Brown  *
3185ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3186ec4ee52aSMark Brown  */
3187ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3188ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3189ec4ee52aSMark Brown {
3190ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3191ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3192ec4ee52aSMark Brown 					      freq_in, freq_out);
3193ec4ee52aSMark Brown 	else
3194ec4ee52aSMark Brown 		return -EINVAL;
3195ec4ee52aSMark Brown }
3196ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3197ec4ee52aSMark Brown 
31988c6529dbSLiam Girdwood /**
31998c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
32008c6529dbSLiam Girdwood  * @dai: DAI
32018c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
32028c6529dbSLiam Girdwood  *
32038c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
32048c6529dbSLiam Girdwood  */
32058c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
32068c6529dbSLiam Girdwood {
32075e4ba569SShawn Guo 	if (dai->driver == NULL)
32088c6529dbSLiam Girdwood 		return -EINVAL;
32095e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
32105e4ba569SShawn Guo 		return -ENOTSUPP;
32115e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
32128c6529dbSLiam Girdwood }
32138c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
32148c6529dbSLiam Girdwood 
32158c6529dbSLiam Girdwood /**
32168c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
32178c6529dbSLiam Girdwood  * @dai: DAI
3218a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3219a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
32208c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3221a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
32228c6529dbSLiam Girdwood  *
32238c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
32248c6529dbSLiam Girdwood  * specific.
32258c6529dbSLiam Girdwood  */
32268c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3227a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
32288c6529dbSLiam Girdwood {
3229f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3230f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3231a5479e38SDaniel Ribeiro 				slots, slot_width);
32328c6529dbSLiam Girdwood 	else
32338c6529dbSLiam Girdwood 		return -EINVAL;
32348c6529dbSLiam Girdwood }
32358c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
32368c6529dbSLiam Girdwood 
32378c6529dbSLiam Girdwood /**
3238472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3239472df3cbSBarry Song  * @dai: DAI
3240472df3cbSBarry Song  * @tx_num: how many TX channels
3241472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3242472df3cbSBarry Song  *           0~num-1 uses
3243472df3cbSBarry Song  * @rx_num: how many RX channels
3244472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3245472df3cbSBarry Song  *           0~num-1 uses
3246472df3cbSBarry Song  *
3247472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3248472df3cbSBarry Song  */
3249472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3250472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3251472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3252472df3cbSBarry Song {
3253f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3254f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3255472df3cbSBarry Song 			rx_num, rx_slot);
3256472df3cbSBarry Song 	else
3257472df3cbSBarry Song 		return -EINVAL;
3258472df3cbSBarry Song }
3259472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3260472df3cbSBarry Song 
3261472df3cbSBarry Song /**
32628c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
32638c6529dbSLiam Girdwood  * @dai: DAI
32648c6529dbSLiam Girdwood  * @tristate: tristate enable
32658c6529dbSLiam Girdwood  *
32668c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
32678c6529dbSLiam Girdwood  */
32688c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
32698c6529dbSLiam Girdwood {
3270f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3271f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
32728c6529dbSLiam Girdwood 	else
32738c6529dbSLiam Girdwood 		return -EINVAL;
32748c6529dbSLiam Girdwood }
32758c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
32768c6529dbSLiam Girdwood 
32778c6529dbSLiam Girdwood /**
32788c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
32798c6529dbSLiam Girdwood  * @dai: DAI
32808c6529dbSLiam Girdwood  * @mute: mute enable
32818c6529dbSLiam Girdwood  *
32828c6529dbSLiam Girdwood  * Mutes the DAI DAC.
32838c6529dbSLiam Girdwood  */
32848c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
32858c6529dbSLiam Girdwood {
3286f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3287f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
32888c6529dbSLiam Girdwood 	else
328904570c62SMark Brown 		return -ENOTSUPP;
32908c6529dbSLiam Girdwood }
32918c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
32928c6529dbSLiam Girdwood 
3293c5af3a2eSMark Brown /**
3294c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3295c5af3a2eSMark Brown  *
3296ac11a2b3SMark Brown  * @card: Card to register
3297c5af3a2eSMark Brown  *
3298c5af3a2eSMark Brown  */
329970a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3300c5af3a2eSMark Brown {
3301b19e6e7bSMark Brown 	int i, ret;
3302f0fba2adSLiam Girdwood 
3303c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3304c5af3a2eSMark Brown 		return -EINVAL;
3305c5af3a2eSMark Brown 
33065a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
33075a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
33085a504963SStephen Warren 
33095a504963SStephen Warren 		/*
33105a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
33115a504963SStephen Warren 		 * not both or neither.
33125a504963SStephen Warren 		 */
33135a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
33145a504963SStephen Warren 			dev_err(card->dev,
33153b09bb82SLiam Girdwood 				"Neither/both codec name/of_node are set for %s\n",
33163b09bb82SLiam Girdwood 				link->name);
33175a504963SStephen Warren 			return -EINVAL;
33185a504963SStephen Warren 		}
33195a504963SStephen Warren 
33205a504963SStephen Warren 		/*
33215a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
33225a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
33235a504963SStephen Warren 		 */
33245a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
33255a504963SStephen Warren 			dev_err(card->dev,
33263b09bb82SLiam Girdwood 				"Both platform name/of_node are set for %s\n", link->name);
33275a504963SStephen Warren 			return -EINVAL;
33285a504963SStephen Warren 		}
33295a504963SStephen Warren 
33305a504963SStephen Warren 		/*
33315a504963SStephen Warren 		 * CPU DAI must be specified by 1 of name or OF node,
33325a504963SStephen Warren 		 * not both or neither.
33335a504963SStephen Warren 		 */
33345a504963SStephen Warren 		if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
33355a504963SStephen Warren 			dev_err(card->dev,
33363b09bb82SLiam Girdwood 				"Neither/both cpu_dai name/of_node are set for %s\n",
33373b09bb82SLiam Girdwood 				link->name);
33385a504963SStephen Warren 			return -EINVAL;
33395a504963SStephen Warren 		}
33405a504963SStephen Warren 	}
33415a504963SStephen Warren 
3342ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3343ed77cc12SMark Brown 
3344111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3345111c6419SStephen Warren 
3346150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3347150dd2f8SVinod Koul 
3348181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3349181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
33502eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3351f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3352f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3353f0fba2adSLiam Girdwood 		return -ENOMEM;
33542eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3355f0fba2adSLiam Girdwood 
3356f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3357f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3358f0fba2adSLiam Girdwood 
3359c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3360db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3361c5af3a2eSMark Brown 	card->instantiated = 0;
3362f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3363a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3364c5af3a2eSMark Brown 
3365b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3366b19e6e7bSMark Brown 	if (ret != 0)
3367b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3368c5af3a2eSMark Brown 
3369b19e6e7bSMark Brown 	return ret;
3370c5af3a2eSMark Brown }
337170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3372c5af3a2eSMark Brown 
3373c5af3a2eSMark Brown /**
3374c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3375c5af3a2eSMark Brown  *
3376ac11a2b3SMark Brown  * @card: Card to unregister
3377c5af3a2eSMark Brown  *
3378c5af3a2eSMark Brown  */
337970a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3380c5af3a2eSMark Brown {
3381b0e26485SVinod Koul 	if (card->instantiated)
3382b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3383c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3384c5af3a2eSMark Brown 
3385c5af3a2eSMark Brown 	return 0;
3386c5af3a2eSMark Brown }
338770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3388c5af3a2eSMark Brown 
3389f0fba2adSLiam Girdwood /*
3390f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3391f0fba2adSLiam Girdwood  * and sanitizing names.
3392f0fba2adSLiam Girdwood  */
33930b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3394f0fba2adSLiam Girdwood {
3395f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3396f0fba2adSLiam Girdwood 	int id1, id2;
3397f0fba2adSLiam Girdwood 
3398f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3399f0fba2adSLiam Girdwood 		return NULL;
3400f0fba2adSLiam Girdwood 
340158818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3402f0fba2adSLiam Girdwood 
3403f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3404f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3405f0fba2adSLiam Girdwood 	if (found) {
3406f0fba2adSLiam Girdwood 		/* get ID */
3407f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3408f0fba2adSLiam Girdwood 
3409f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3410f0fba2adSLiam Girdwood 			if (*id == -1)
3411f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3412f0fba2adSLiam Girdwood 		}
3413f0fba2adSLiam Girdwood 
3414f0fba2adSLiam Girdwood 	} else {
3415f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3416f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3417f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3418f0fba2adSLiam Girdwood 
3419f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
342005899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3421f0fba2adSLiam Girdwood 
3422f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3423f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
342458818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3425f0fba2adSLiam Girdwood 		} else
3426f0fba2adSLiam Girdwood 			*id = 0;
3427f0fba2adSLiam Girdwood 	}
3428f0fba2adSLiam Girdwood 
3429f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3430f0fba2adSLiam Girdwood }
3431f0fba2adSLiam Girdwood 
3432f0fba2adSLiam Girdwood /*
3433f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3434f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3435f0fba2adSLiam Girdwood  */
3436f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3437f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3438f0fba2adSLiam Girdwood {
3439f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
34400837fc62SFabio Estevam 		pr_err("asoc: error - multiple DAI %s registered with no name\n",
3441f0fba2adSLiam Girdwood 				dev_name(dev));
3442f0fba2adSLiam Girdwood 		return NULL;
3443f0fba2adSLiam Girdwood 	}
3444f0fba2adSLiam Girdwood 
3445f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3446f0fba2adSLiam Girdwood }
3447f0fba2adSLiam Girdwood 
34489115171aSMark Brown /**
34499115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
34509115171aSMark Brown  *
3451ac11a2b3SMark Brown  * @dai: DAI to register
34529115171aSMark Brown  */
3453f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3454f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
34559115171aSMark Brown {
3456054880feSMark Brown 	struct snd_soc_codec *codec;
3457f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
34589115171aSMark Brown 
3459f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
34609115171aSMark Brown 
3461f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3462f0fba2adSLiam Girdwood 	if (dai == NULL)
3463f0fba2adSLiam Girdwood 		return -ENOMEM;
34646335d055SEric Miao 
3465f0fba2adSLiam Girdwood 	/* create DAI component name */
3466f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3467f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3468f0fba2adSLiam Girdwood 		kfree(dai);
3469f0fba2adSLiam Girdwood 		return -ENOMEM;
3470f0fba2adSLiam Girdwood 	}
3471f0fba2adSLiam Girdwood 
3472f0fba2adSLiam Girdwood 	dai->dev = dev;
3473f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3474be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3475f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3476f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
34779115171aSMark Brown 
34789115171aSMark Brown 	mutex_lock(&client_mutex);
3479054880feSMark Brown 
3480054880feSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3481054880feSMark Brown 		if (codec->dev == dev) {
3482054880feSMark Brown 			dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3483054880feSMark Brown 				dai->name, codec->name);
3484054880feSMark Brown 			dai->codec = codec;
3485054880feSMark Brown 			break;
3486054880feSMark Brown 		}
3487054880feSMark Brown 	}
3488054880feSMark Brown 
34899115171aSMark Brown 	list_add(&dai->list, &dai_list);
3490054880feSMark Brown 
34919115171aSMark Brown 	mutex_unlock(&client_mutex);
34929115171aSMark Brown 
34939115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
34949115171aSMark Brown 
34959115171aSMark Brown 	return 0;
34969115171aSMark Brown }
34979115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
34989115171aSMark Brown 
34999115171aSMark Brown /**
35009115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
35019115171aSMark Brown  *
3502ac11a2b3SMark Brown  * @dai: DAI to unregister
35039115171aSMark Brown  */
3504f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
35059115171aSMark Brown {
3506f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3507f0fba2adSLiam Girdwood 
3508f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3509f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3510f0fba2adSLiam Girdwood 			goto found;
3511f0fba2adSLiam Girdwood 	}
3512f0fba2adSLiam Girdwood 	return;
3513f0fba2adSLiam Girdwood 
3514f0fba2adSLiam Girdwood found:
35159115171aSMark Brown 	mutex_lock(&client_mutex);
35169115171aSMark Brown 	list_del(&dai->list);
35179115171aSMark Brown 	mutex_unlock(&client_mutex);
35189115171aSMark Brown 
35199115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3520f0fba2adSLiam Girdwood 	kfree(dai->name);
3521f0fba2adSLiam Girdwood 	kfree(dai);
35229115171aSMark Brown }
35239115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
35249115171aSMark Brown 
35259115171aSMark Brown /**
35269115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
35279115171aSMark Brown  *
3528ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3529ac11a2b3SMark Brown  * @count: Number of DAIs
35309115171aSMark Brown  */
3531f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3532f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
35339115171aSMark Brown {
3534054880feSMark Brown 	struct snd_soc_codec *codec;
3535f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3536f0fba2adSLiam Girdwood 	int i, ret = 0;
3537f0fba2adSLiam Girdwood 
3538720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
35399115171aSMark Brown 
35409115171aSMark Brown 	for (i = 0; i < count; i++) {
3541f0fba2adSLiam Girdwood 
3542f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3543c46e0079SAxel Lin 		if (dai == NULL) {
3544c46e0079SAxel Lin 			ret = -ENOMEM;
3545c46e0079SAxel Lin 			goto err;
3546c46e0079SAxel Lin 		}
3547f0fba2adSLiam Girdwood 
3548f0fba2adSLiam Girdwood 		/* create DAI component name */
3549f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3550f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3551f0fba2adSLiam Girdwood 			kfree(dai);
3552f0fba2adSLiam Girdwood 			ret = -EINVAL;
35539115171aSMark Brown 			goto err;
35549115171aSMark Brown 		}
35559115171aSMark Brown 
3556f0fba2adSLiam Girdwood 		dai->dev = dev;
3557f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
35580f9141c9SMark Brown 		if (dai->driver->id)
35590f9141c9SMark Brown 			dai->id = dai->driver->id;
35600f9141c9SMark Brown 		else
35610f9141c9SMark Brown 			dai->id = i;
3562be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3563f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3564f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3565f0fba2adSLiam Girdwood 
3566f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3567054880feSMark Brown 
3568054880feSMark Brown 		list_for_each_entry(codec, &codec_list, list) {
3569054880feSMark Brown 			if (codec->dev == dev) {
3570054880feSMark Brown 				dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3571054880feSMark Brown 					dai->name, codec->name);
3572054880feSMark Brown 				dai->codec = codec;
3573054880feSMark Brown 				break;
3574054880feSMark Brown 			}
3575054880feSMark Brown 		}
3576054880feSMark Brown 
3577f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3578054880feSMark Brown 
3579f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3580f0fba2adSLiam Girdwood 
3581f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3582f0fba2adSLiam Girdwood 	}
3583f0fba2adSLiam Girdwood 
35849115171aSMark Brown 	return 0;
35859115171aSMark Brown 
35869115171aSMark Brown err:
35879115171aSMark Brown 	for (i--; i >= 0; i--)
3588f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
35899115171aSMark Brown 
35909115171aSMark Brown 	return ret;
35919115171aSMark Brown }
35929115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
35939115171aSMark Brown 
35949115171aSMark Brown /**
35959115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
35969115171aSMark Brown  *
3597ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3598ac11a2b3SMark Brown  * @count: Number of DAIs
35999115171aSMark Brown  */
3600f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
36019115171aSMark Brown {
36029115171aSMark Brown 	int i;
36039115171aSMark Brown 
36049115171aSMark Brown 	for (i = 0; i < count; i++)
3605f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
36069115171aSMark Brown }
36079115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
36089115171aSMark Brown 
360912a48a8cSMark Brown /**
361012a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
361112a48a8cSMark Brown  *
3612ac11a2b3SMark Brown  * @platform: platform to register
361312a48a8cSMark Brown  */
3614f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3615f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
361612a48a8cSMark Brown {
3617f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
361812a48a8cSMark Brown 
3619f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3620f0fba2adSLiam Girdwood 
3621f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3622f0fba2adSLiam Girdwood 	if (platform == NULL)
3623f0fba2adSLiam Girdwood 		return -ENOMEM;
3624f0fba2adSLiam Girdwood 
3625f0fba2adSLiam Girdwood 	/* create platform component name */
3626f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3627f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3628f0fba2adSLiam Girdwood 		kfree(platform);
3629f0fba2adSLiam Girdwood 		return -ENOMEM;
3630f0fba2adSLiam Girdwood 	}
3631f0fba2adSLiam Girdwood 
3632f0fba2adSLiam Girdwood 	platform->dev = dev;
3633f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3634b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3635b7950641SLiam Girdwood 	platform->dapm.platform = platform;
363664a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3637cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
363812a48a8cSMark Brown 
363912a48a8cSMark Brown 	mutex_lock(&client_mutex);
364012a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
364112a48a8cSMark Brown 	mutex_unlock(&client_mutex);
364212a48a8cSMark Brown 
364312a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
364412a48a8cSMark Brown 
364512a48a8cSMark Brown 	return 0;
364612a48a8cSMark Brown }
364712a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
364812a48a8cSMark Brown 
364912a48a8cSMark Brown /**
365012a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
365112a48a8cSMark Brown  *
3652ac11a2b3SMark Brown  * @platform: platform to unregister
365312a48a8cSMark Brown  */
3654f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
365512a48a8cSMark Brown {
3656f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3657f0fba2adSLiam Girdwood 
3658f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3659f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3660f0fba2adSLiam Girdwood 			goto found;
3661f0fba2adSLiam Girdwood 	}
3662f0fba2adSLiam Girdwood 	return;
3663f0fba2adSLiam Girdwood 
3664f0fba2adSLiam Girdwood found:
366512a48a8cSMark Brown 	mutex_lock(&client_mutex);
366612a48a8cSMark Brown 	list_del(&platform->list);
366712a48a8cSMark Brown 	mutex_unlock(&client_mutex);
366812a48a8cSMark Brown 
366912a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3670f0fba2adSLiam Girdwood 	kfree(platform->name);
3671f0fba2adSLiam Girdwood 	kfree(platform);
367212a48a8cSMark Brown }
367312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
367412a48a8cSMark Brown 
3675151ab22cSMark Brown static u64 codec_format_map[] = {
3676151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3677151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3678151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3679151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3680151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3681151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3682151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3683151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3684151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3685151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3686151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3687151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3688151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3689151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3690151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3691151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3692151ab22cSMark Brown };
3693151ab22cSMark Brown 
3694151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3695151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3696151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3697151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3698151ab22cSMark Brown  */
3699151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3700151ab22cSMark Brown {
3701151ab22cSMark Brown 	int i;
3702151ab22cSMark Brown 
3703151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3704151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3705151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3706151ab22cSMark Brown }
3707151ab22cSMark Brown 
37080d0cf00aSMark Brown /**
37090d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
37100d0cf00aSMark Brown  *
3711ac11a2b3SMark Brown  * @codec: codec to register
37120d0cf00aSMark Brown  */
3713f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3714001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3715001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3716001ae4c0SMark Brown 			   int num_dai)
37170d0cf00aSMark Brown {
37183335ddcaSDimitris Papastamos 	size_t reg_size;
3719f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3720f0fba2adSLiam Girdwood 	int ret, i;
3721151ab22cSMark Brown 
3722f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
37230d0cf00aSMark Brown 
3724f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3725f0fba2adSLiam Girdwood 	if (codec == NULL)
3726f0fba2adSLiam Girdwood 		return -ENOMEM;
37270d0cf00aSMark Brown 
3728f0fba2adSLiam Girdwood 	/* create CODEC component name */
3729f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3730f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3731f0fba2adSLiam Girdwood 		kfree(codec);
3732f0fba2adSLiam Girdwood 		return -ENOMEM;
3733151ab22cSMark Brown 	}
3734151ab22cSMark Brown 
373523bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
373623bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
373723bbce34SDimitris Papastamos 	else
373823bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
373923bbce34SDimitris Papastamos 
3740c3acec26SMark Brown 	codec->write = codec_drv->write;
3741c3acec26SMark Brown 	codec->read = codec_drv->read;
37421500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
37431500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
37448020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
37455124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3746ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3747ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3748ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3749474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
375064a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3751f0fba2adSLiam Girdwood 	codec->dev = dev;
3752f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3753f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3754f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3755f0fba2adSLiam Girdwood 
37567a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
37577a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
37583335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3759aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
37603335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
37613335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
37623335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
37633335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
37643335ddcaSDimitris Papastamos 		 * the cache.
37653335ddcaSDimitris Papastamos 		 */
37662aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
37673335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
37683335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
37693335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
37703335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3771fdf0f54dSDimitris Papastamos 				goto fail;
37727a30a3dbSDimitris Papastamos 			}
37737a30a3dbSDimitris Papastamos 		}
37742aa86323SDimitris Papastamos 	}
37757a30a3dbSDimitris Papastamos 
37761500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
37771500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
37781500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
37791500b7b5SDimitris Papastamos 		if (!codec->readable_register)
37801500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
37818020454cSDimitris Papastamos 		if (!codec->writable_register)
37828020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
37831500b7b5SDimitris Papastamos 	}
37841500b7b5SDimitris Papastamos 
3785f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3786f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3787f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3788f0fba2adSLiam Girdwood 	}
3789f0fba2adSLiam Girdwood 
3790054880feSMark Brown 	mutex_lock(&client_mutex);
3791054880feSMark Brown 	list_add(&codec->list, &codec_list);
3792054880feSMark Brown 	mutex_unlock(&client_mutex);
3793054880feSMark Brown 
379426b01ccdSMark Brown 	/* register any DAIs */
379526b01ccdSMark Brown 	if (num_dai) {
3796f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3797f0fba2adSLiam Girdwood 		if (ret < 0)
3798054880feSMark Brown 			dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3799054880feSMark Brown 				ret);
380026b01ccdSMark Brown 	}
3801f0fba2adSLiam Girdwood 
38020d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
38030d0cf00aSMark Brown 	return 0;
3804f0fba2adSLiam Girdwood 
3805fdf0f54dSDimitris Papastamos fail:
38063335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
38073335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3808f0fba2adSLiam Girdwood 	kfree(codec->name);
3809f0fba2adSLiam Girdwood 	kfree(codec);
3810f0fba2adSLiam Girdwood 	return ret;
38110d0cf00aSMark Brown }
38120d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
38130d0cf00aSMark Brown 
38140d0cf00aSMark Brown /**
38150d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
38160d0cf00aSMark Brown  *
3817ac11a2b3SMark Brown  * @codec: codec to unregister
38180d0cf00aSMark Brown  */
3819f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
38200d0cf00aSMark Brown {
3821f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3822f0fba2adSLiam Girdwood 	int i;
3823f0fba2adSLiam Girdwood 
3824f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3825f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3826f0fba2adSLiam Girdwood 			goto found;
3827f0fba2adSLiam Girdwood 	}
3828f0fba2adSLiam Girdwood 	return;
3829f0fba2adSLiam Girdwood 
3830f0fba2adSLiam Girdwood found:
383126b01ccdSMark Brown 	if (codec->num_dai)
3832f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3833f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3834f0fba2adSLiam Girdwood 
38350d0cf00aSMark Brown 	mutex_lock(&client_mutex);
38360d0cf00aSMark Brown 	list_del(&codec->list);
38370d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
38380d0cf00aSMark Brown 
38390d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3840f0fba2adSLiam Girdwood 
38417a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
38423335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
38431aafcd4dSDimitris Papastamos 	kfree(codec->name);
3844f0fba2adSLiam Girdwood 	kfree(codec);
38450d0cf00aSMark Brown }
38460d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
38470d0cf00aSMark Brown 
3848bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3849bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3850bec4fa05SStephen Warren 			       const char *propname)
3851bec4fa05SStephen Warren {
3852bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
3853bec4fa05SStephen Warren 	int ret;
3854bec4fa05SStephen Warren 
3855bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3856bec4fa05SStephen Warren 	/*
3857bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3858bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3859bec4fa05SStephen Warren 	 * snd_soc_register_card.
3860bec4fa05SStephen Warren 	 */
3861bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3862bec4fa05SStephen Warren 		dev_err(card->dev,
3863bec4fa05SStephen Warren 			"Property '%s' could not be read: %d\n",
3864bec4fa05SStephen Warren 			propname, ret);
3865bec4fa05SStephen Warren 		return ret;
3866bec4fa05SStephen Warren 	}
3867bec4fa05SStephen Warren 
3868bec4fa05SStephen Warren 	return 0;
3869bec4fa05SStephen Warren }
3870bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3871bec4fa05SStephen Warren 
3872a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3873a4a54dd5SStephen Warren 				   const char *propname)
3874a4a54dd5SStephen Warren {
3875a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3876a4a54dd5SStephen Warren 	int num_routes;
3877a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3878a4a54dd5SStephen Warren 	int i, ret;
3879a4a54dd5SStephen Warren 
3880a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3881a4a54dd5SStephen Warren 	if (num_routes & 1) {
3882a4a54dd5SStephen Warren 		dev_err(card->dev,
3883a4a54dd5SStephen Warren 			"Property '%s's length is not even\n",
3884a4a54dd5SStephen Warren 			propname);
3885a4a54dd5SStephen Warren 		return -EINVAL;
3886a4a54dd5SStephen Warren 	}
3887a4a54dd5SStephen Warren 	num_routes /= 2;
3888a4a54dd5SStephen Warren 	if (!num_routes) {
3889a4a54dd5SStephen Warren 		dev_err(card->dev,
3890a4a54dd5SStephen Warren 			"Property '%s's length is zero\n",
3891a4a54dd5SStephen Warren 			propname);
3892a4a54dd5SStephen Warren 		return -EINVAL;
3893a4a54dd5SStephen Warren 	}
3894a4a54dd5SStephen Warren 
3895a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3896a4a54dd5SStephen Warren 			      GFP_KERNEL);
3897a4a54dd5SStephen Warren 	if (!routes) {
3898a4a54dd5SStephen Warren 		dev_err(card->dev,
3899a4a54dd5SStephen Warren 			"Could not allocate DAPM route table\n");
3900a4a54dd5SStephen Warren 		return -EINVAL;
3901a4a54dd5SStephen Warren 	}
3902a4a54dd5SStephen Warren 
3903a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3904a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3905a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
3906a4a54dd5SStephen Warren 		if (ret) {
3907a4a54dd5SStephen Warren 			dev_err(card->dev,
3908a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3909a4a54dd5SStephen Warren 				propname, 2 * i, ret);
3910a4a54dd5SStephen Warren 			return -EINVAL;
3911a4a54dd5SStephen Warren 		}
3912a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3913a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
3914a4a54dd5SStephen Warren 		if (ret) {
3915a4a54dd5SStephen Warren 			dev_err(card->dev,
3916a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3917a4a54dd5SStephen Warren 				propname, (2 * i) + 1, ret);
3918a4a54dd5SStephen Warren 			return -EINVAL;
3919a4a54dd5SStephen Warren 		}
3920a4a54dd5SStephen Warren 	}
3921a4a54dd5SStephen Warren 
3922a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
3923a4a54dd5SStephen Warren 	card->dapm_routes = routes;
3924a4a54dd5SStephen Warren 
3925a4a54dd5SStephen Warren 	return 0;
3926a4a54dd5SStephen Warren }
3927a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3928a4a54dd5SStephen Warren 
3929c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3930db2a4165SFrank Mandarino {
3931384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
39328a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
39338a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
39340837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
39358a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3936384c89e2SMark Brown 	}
3937c3c5a19aSMark Brown 
39388a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3939c3c5a19aSMark Brown 				 &codec_list_fops))
3940c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3941c3c5a19aSMark Brown 
39428a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3943f3208780SMark Brown 				 &dai_list_fops))
3944f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
394519c7ac27SMark Brown 
39468a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
394719c7ac27SMark Brown 				 &platform_list_fops))
394819c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3949384c89e2SMark Brown #endif
3950384c89e2SMark Brown 
3951fb257897SMark Brown 	snd_soc_util_init();
3952fb257897SMark Brown 
3953db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3954db2a4165SFrank Mandarino }
39554abe8e16SMark Brown module_init(snd_soc_init);
3956db2a4165SFrank Mandarino 
39577d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3958db2a4165SFrank Mandarino {
3959fb257897SMark Brown 	snd_soc_util_exit();
3960fb257897SMark Brown 
3961384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
39628a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3963384c89e2SMark Brown #endif
3964db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3965db2a4165SFrank Mandarino }
3966db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3967db2a4165SFrank Mandarino 
3968db2a4165SFrank Mandarino /* Module information */
3969d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3970db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3971db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
39728b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3973