xref: /openbmc/linux/sound/soc/soc-core.c (revision a7dbb603423d499acacefb5fad65d2b406f16370)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6f0fba2adSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7f0fba2adSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
80664d888SLiam Girdwood  *
9d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
100664d888SLiam Girdwood  *         with code, comments and ideas from :-
110664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
14db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
15db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
16db2a4165SFrank Mandarino  *  option) any later version.
17db2a4165SFrank Mandarino  *
18db2a4165SFrank Mandarino  *  TODO:
19db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
20db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
21db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
22db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
23db2a4165SFrank Mandarino  */
24db2a4165SFrank Mandarino 
25db2a4165SFrank Mandarino #include <linux/module.h>
26db2a4165SFrank Mandarino #include <linux/moduleparam.h>
27db2a4165SFrank Mandarino #include <linux/init.h>
28db2a4165SFrank Mandarino #include <linux/delay.h>
29db2a4165SFrank Mandarino #include <linux/pm.h>
30db2a4165SFrank Mandarino #include <linux/bitops.h>
3112ef193dSTroy Kisky #include <linux/debugfs.h>
32db2a4165SFrank Mandarino #include <linux/platform_device.h>
33f0e8ed85SMark Brown #include <linux/ctype.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
35bec4fa05SStephen Warren #include <linux/of.h>
36474828a4SMarek Vasut #include <sound/ac97_codec.h>
37db2a4165SFrank Mandarino #include <sound/core.h>
383028eb8cSMark Brown #include <sound/jack.h>
39db2a4165SFrank Mandarino #include <sound/pcm.h>
40db2a4165SFrank Mandarino #include <sound/pcm_params.h>
41db2a4165SFrank Mandarino #include <sound/soc.h>
42db2a4165SFrank Mandarino #include <sound/initval.h>
43db2a4165SFrank Mandarino 
44a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
45a8b1d34fSMark Brown #include <trace/events/asoc.h>
46a8b1d34fSMark Brown 
47f0fba2adSLiam Girdwood #define NAME_SIZE	32
48f0fba2adSLiam Girdwood 
49db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50db2a4165SFrank Mandarino 
51384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
528a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
538a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
54384c89e2SMark Brown #endif
55384c89e2SMark Brown 
56c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
57c5af3a2eSMark Brown static LIST_HEAD(card_list);
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 int codec_reg_open_file(struct inode *inode, struct file *file)
2052624d5faSMark Brown {
2062624d5faSMark Brown 	file->private_data = inode->i_private;
2072624d5faSMark Brown 	return 0;
2082624d5faSMark Brown }
2092624d5faSMark Brown 
2102624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2112624d5faSMark Brown 				   size_t count, loff_t *ppos)
2122624d5faSMark Brown {
2132624d5faSMark Brown 	ssize_t ret;
2142624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
21513fd179fSDimitris Papastamos 	char *buf;
21613fd179fSDimitris Papastamos 
21713fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
21813fd179fSDimitris Papastamos 		return -EINVAL;
21913fd179fSDimitris Papastamos 
22013fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2212624d5faSMark Brown 	if (!buf)
2222624d5faSMark Brown 		return -ENOMEM;
22313fd179fSDimitris Papastamos 
22413fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
22513fd179fSDimitris Papastamos 	if (ret >= 0) {
22613fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
22713fd179fSDimitris Papastamos 			kfree(buf);
22813fd179fSDimitris Papastamos 			return -EFAULT;
22913fd179fSDimitris Papastamos 		}
23013fd179fSDimitris Papastamos 		*ppos += ret;
23113fd179fSDimitris Papastamos 	}
23213fd179fSDimitris Papastamos 
2332624d5faSMark Brown 	kfree(buf);
2342624d5faSMark Brown 	return ret;
2352624d5faSMark Brown }
2362624d5faSMark Brown 
2372624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2382624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2392624d5faSMark Brown {
2402624d5faSMark Brown 	char buf[32];
24134e268d8SStephen Boyd 	size_t buf_size;
2422624d5faSMark Brown 	char *start = buf;
2432624d5faSMark Brown 	unsigned long reg, value;
2442624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2452624d5faSMark Brown 
2462624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2472624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2482624d5faSMark Brown 		return -EFAULT;
2492624d5faSMark Brown 	buf[buf_size] = 0;
2502624d5faSMark Brown 
2512624d5faSMark Brown 	while (*start == ' ')
2522624d5faSMark Brown 		start++;
2532624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2542624d5faSMark Brown 	while (*start == ' ')
2552624d5faSMark Brown 		start++;
2562624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2572624d5faSMark Brown 		return -EINVAL;
2580d51a9cbSMark Brown 
2590d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2600d51a9cbSMark Brown 	add_taint(TAINT_USER);
2610d51a9cbSMark Brown 
262e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2632624d5faSMark Brown 	return buf_size;
2642624d5faSMark Brown }
2652624d5faSMark Brown 
2662624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2672624d5faSMark Brown 	.open = codec_reg_open_file,
2682624d5faSMark Brown 	.read = codec_reg_read_file,
2692624d5faSMark Brown 	.write = codec_reg_write_file,
2706038f373SArnd Bergmann 	.llseek = default_llseek,
2712624d5faSMark Brown };
2722624d5faSMark Brown 
2732624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2742624d5faSMark Brown {
275d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276d6ce4cf3SJarkko Nikula 
2776ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2792624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
28064e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
2812624d5faSMark Brown 		return;
2822624d5faSMark Brown 	}
2832624d5faSMark Brown 
284aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
285aaee8ef1SMark Brown 			    &codec->cache_sync);
286aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
287aaee8ef1SMark Brown 			    &codec->cache_only);
288aaee8ef1SMark Brown 
2892624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2902624d5faSMark Brown 						 codec->debugfs_codec_root,
2912624d5faSMark Brown 						 codec, &codec_reg_fops);
2922624d5faSMark Brown 	if (!codec->debugfs_reg)
29364e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
2942624d5faSMark Brown 
2958eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
2962624d5faSMark Brown }
2972624d5faSMark Brown 
2982624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2992624d5faSMark Brown {
3002624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3012624d5faSMark Brown }
3022624d5faSMark Brown 
303731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
304731f1ab2SSebastien Guiriec {
305731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
306731f1ab2SSebastien Guiriec 
307731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
308731f1ab2SSebastien Guiriec 						       debugfs_card_root);
309731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
310731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
311731f1ab2SSebastien Guiriec 			"Failed to create platform debugfs directory\n");
312731f1ab2SSebastien Guiriec 		return;
313731f1ab2SSebastien Guiriec 	}
314731f1ab2SSebastien Guiriec 
315731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
316731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
317731f1ab2SSebastien Guiriec }
318731f1ab2SSebastien Guiriec 
319731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
320731f1ab2SSebastien Guiriec {
321731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
322731f1ab2SSebastien Guiriec }
323731f1ab2SSebastien Guiriec 
324c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
325c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
326c3c5a19aSMark Brown {
327c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3282b194f9dSMark Brown 	ssize_t len, ret = 0;
329c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
330c3c5a19aSMark Brown 
331c3c5a19aSMark Brown 	if (!buf)
332c3c5a19aSMark Brown 		return -ENOMEM;
333c3c5a19aSMark Brown 
3342b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3352b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
336c3c5a19aSMark Brown 			       codec->name);
3372b194f9dSMark Brown 		if (len >= 0)
3382b194f9dSMark Brown 			ret += len;
3392b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3402b194f9dSMark Brown 			ret = PAGE_SIZE;
3412b194f9dSMark Brown 			break;
3422b194f9dSMark Brown 		}
3432b194f9dSMark Brown 	}
344c3c5a19aSMark Brown 
345c3c5a19aSMark Brown 	if (ret >= 0)
346c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
347c3c5a19aSMark Brown 
348c3c5a19aSMark Brown 	kfree(buf);
349c3c5a19aSMark Brown 
350c3c5a19aSMark Brown 	return ret;
351c3c5a19aSMark Brown }
352c3c5a19aSMark Brown 
353c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
354c3c5a19aSMark Brown 	.read = codec_list_read_file,
355c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
356c3c5a19aSMark Brown };
357c3c5a19aSMark Brown 
358f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
359f3208780SMark Brown 				  size_t count, loff_t *ppos)
360f3208780SMark Brown {
361f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3622b194f9dSMark Brown 	ssize_t len, ret = 0;
363f3208780SMark Brown 	struct snd_soc_dai *dai;
364f3208780SMark Brown 
365f3208780SMark Brown 	if (!buf)
366f3208780SMark Brown 		return -ENOMEM;
367f3208780SMark Brown 
3682b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3692b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3702b194f9dSMark Brown 		if (len >= 0)
3712b194f9dSMark Brown 			ret += len;
3722b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3732b194f9dSMark Brown 			ret = PAGE_SIZE;
3742b194f9dSMark Brown 			break;
3752b194f9dSMark Brown 		}
3762b194f9dSMark Brown 	}
377f3208780SMark Brown 
378f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
379f3208780SMark Brown 
380f3208780SMark Brown 	kfree(buf);
381f3208780SMark Brown 
382f3208780SMark Brown 	return ret;
383f3208780SMark Brown }
384f3208780SMark Brown 
385f3208780SMark Brown static const struct file_operations dai_list_fops = {
386f3208780SMark Brown 	.read = dai_list_read_file,
387f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
388f3208780SMark Brown };
389f3208780SMark Brown 
39019c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
39119c7ac27SMark Brown 				       char __user *user_buf,
39219c7ac27SMark Brown 				       size_t count, loff_t *ppos)
39319c7ac27SMark Brown {
39419c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3952b194f9dSMark Brown 	ssize_t len, ret = 0;
39619c7ac27SMark Brown 	struct snd_soc_platform *platform;
39719c7ac27SMark Brown 
39819c7ac27SMark Brown 	if (!buf)
39919c7ac27SMark Brown 		return -ENOMEM;
40019c7ac27SMark Brown 
4012b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4022b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
40319c7ac27SMark Brown 			       platform->name);
4042b194f9dSMark Brown 		if (len >= 0)
4052b194f9dSMark Brown 			ret += len;
4062b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4072b194f9dSMark Brown 			ret = PAGE_SIZE;
4082b194f9dSMark Brown 			break;
4092b194f9dSMark Brown 		}
4102b194f9dSMark Brown 	}
41119c7ac27SMark Brown 
41219c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
41319c7ac27SMark Brown 
41419c7ac27SMark Brown 	kfree(buf);
41519c7ac27SMark Brown 
41619c7ac27SMark Brown 	return ret;
41719c7ac27SMark Brown }
41819c7ac27SMark Brown 
41919c7ac27SMark Brown static const struct file_operations platform_list_fops = {
42019c7ac27SMark Brown 	.read = platform_list_read_file,
42119c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
42219c7ac27SMark Brown };
42319c7ac27SMark Brown 
424a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
425a6052154SJarkko Nikula {
426a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4278a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4283a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
429a6052154SJarkko Nikula 		dev_warn(card->dev,
4307c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4313a45b867SJarkko Nikula 		return;
4323a45b867SJarkko Nikula 	}
4333a45b867SJarkko Nikula 
4343a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4353a45b867SJarkko Nikula 						    card->debugfs_card_root,
4363a45b867SJarkko Nikula 						    &card->pop_time);
4373a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4383a45b867SJarkko Nikula 		dev_warn(card->dev,
4393a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
440a6052154SJarkko Nikula }
441a6052154SJarkko Nikula 
442a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
443a6052154SJarkko Nikula {
444a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
445a6052154SJarkko Nikula }
446a6052154SJarkko Nikula 
4472624d5faSMark Brown #else
4482624d5faSMark Brown 
4492624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4502624d5faSMark Brown {
4512624d5faSMark Brown }
4522624d5faSMark Brown 
4532624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4542624d5faSMark Brown {
4552624d5faSMark Brown }
456b95fccbcSAxel Lin 
457731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
458731f1ab2SSebastien Guiriec {
459731f1ab2SSebastien Guiriec }
460731f1ab2SSebastien Guiriec 
461731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
462731f1ab2SSebastien Guiriec {
463731f1ab2SSebastien Guiriec }
464731f1ab2SSebastien Guiriec 
465b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
466b95fccbcSAxel Lin {
467b95fccbcSAxel Lin }
468b95fccbcSAxel Lin 
469b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
470b95fccbcSAxel Lin {
471b95fccbcSAxel Lin }
4722624d5faSMark Brown #endif
4732624d5faSMark Brown 
474db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
475db2a4165SFrank Mandarino /* unregister ac97 codec */
476db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
477db2a4165SFrank Mandarino {
478db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
479db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
480db2a4165SFrank Mandarino 	return 0;
481db2a4165SFrank Mandarino }
482db2a4165SFrank Mandarino 
483db2a4165SFrank Mandarino /* stop no dev release warning */
484db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
485db2a4165SFrank Mandarino 
486db2a4165SFrank Mandarino /* register ac97 codec to bus */
487db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
488db2a4165SFrank Mandarino {
489db2a4165SFrank Mandarino 	int err;
490db2a4165SFrank Mandarino 
491db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4924ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
493db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
494db2a4165SFrank Mandarino 
495bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
496f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
497db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
498db2a4165SFrank Mandarino 	if (err < 0) {
499db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
500db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
501db2a4165SFrank Mandarino 		return err;
502db2a4165SFrank Mandarino 	}
503db2a4165SFrank Mandarino 	return 0;
504db2a4165SFrank Mandarino }
505db2a4165SFrank Mandarino #endif
506db2a4165SFrank Mandarino 
5076f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
508db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5096f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
510db2a4165SFrank Mandarino {
5116f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5122eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
513db2a4165SFrank Mandarino 	int i;
514db2a4165SFrank Mandarino 
515e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
516e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
517e3509ff0SDaniel Mack 	 */
518f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
519e3509ff0SDaniel Mack 		return 0;
520e3509ff0SDaniel Mack 
5216ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5226ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5236ed25978SAndy Green 	 */
524f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
525f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
526f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5276ed25978SAndy Green 
5286ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
529f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5306ed25978SAndy Green 
531a00f90f9SMark Brown 	/* mute any active DACs */
532f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
533f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
534f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5353efab7dcSMark Brown 
536f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5373efab7dcSMark Brown 			continue;
5383efab7dcSMark Brown 
539f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
540f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
541db2a4165SFrank Mandarino 	}
542db2a4165SFrank Mandarino 
5434ccab3e7SLiam Girdwood 	/* suspend all pcms */
544f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
545f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5463efab7dcSMark Brown 			continue;
5473efab7dcSMark Brown 
548f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5493efab7dcSMark Brown 	}
5504ccab3e7SLiam Girdwood 
55187506549SMark Brown 	if (card->suspend_pre)
55270b2ac12SMark Brown 		card->suspend_pre(card);
553db2a4165SFrank Mandarino 
554f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
555f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
556f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5573efab7dcSMark Brown 
558f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5593efab7dcSMark Brown 			continue;
5603efab7dcSMark Brown 
561f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
562f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
563f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
564f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
565f0fba2adSLiam Girdwood 			platform->suspended = 1;
566f0fba2adSLiam Girdwood 		}
567db2a4165SFrank Mandarino 	}
568db2a4165SFrank Mandarino 
569db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
570f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5715b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
572ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
573f0fba2adSLiam Girdwood 	}
574db2a4165SFrank Mandarino 
575f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5767bd3a6f3SMark Brown 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
5773efab7dcSMark Brown 
578f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5793efab7dcSMark Brown 			continue;
5803efab7dcSMark Brown 
5817bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5827bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
5837bd3a6f3SMark Brown 					  codec_dai,
584db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
585f0fba2adSLiam Girdwood 
5867bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5877bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
5887bd3a6f3SMark Brown 					  codec_dai,
589db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
590db2a4165SFrank Mandarino 	}
591db2a4165SFrank Mandarino 
592f0fba2adSLiam Girdwood 	/* suspend all CODECs */
5932eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
5941547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
5951547aba9SMark Brown 		 * bias _ON and should not be suspended. */
596f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
597ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
5981547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
599125a25daSMark Brown 				/*
600125a25daSMark Brown 				 * If the CODEC is capable of idle
601125a25daSMark Brown 				 * bias off then being in STANDBY
602125a25daSMark Brown 				 * means it's doing something,
603125a25daSMark Brown 				 * otherwise fall through.
604125a25daSMark Brown 				 */
605125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
606125a25daSMark Brown 					dev_dbg(codec->dev,
607125a25daSMark Brown 						"idle_bias_off CODEC on over suspend\n");
608125a25daSMark Brown 					break;
609125a25daSMark Brown 				}
6101547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
61184b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
612f0fba2adSLiam Girdwood 				codec->suspended = 1;
6137be4ba24SMark Brown 				codec->cache_sync = 1;
6141547aba9SMark Brown 				break;
6151547aba9SMark Brown 			default:
616f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
6171547aba9SMark Brown 				break;
6181547aba9SMark Brown 			}
6191547aba9SMark Brown 		}
620f0fba2adSLiam Girdwood 	}
621db2a4165SFrank Mandarino 
622f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
623f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6243efab7dcSMark Brown 
625f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6263efab7dcSMark Brown 			continue;
6273efab7dcSMark Brown 
628f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
629f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
630db2a4165SFrank Mandarino 	}
631db2a4165SFrank Mandarino 
63287506549SMark Brown 	if (card->suspend_post)
63370b2ac12SMark Brown 		card->suspend_post(card);
634db2a4165SFrank Mandarino 
635db2a4165SFrank Mandarino 	return 0;
636db2a4165SFrank Mandarino }
6376f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
638db2a4165SFrank Mandarino 
6396ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6406ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6416ed25978SAndy Green  */
6426ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
643db2a4165SFrank Mandarino {
644f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
645f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6462eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
647db2a4165SFrank Mandarino 	int i;
648db2a4165SFrank Mandarino 
6496ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6506ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6516ed25978SAndy Green 	 */
6526ed25978SAndy Green 
653f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
6546ed25978SAndy Green 
6559949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
656f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6579949788bSMark Brown 
65887506549SMark Brown 	if (card->resume_pre)
65970b2ac12SMark Brown 		card->resume_pre(card);
660db2a4165SFrank Mandarino 
661f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
662f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
663f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6643efab7dcSMark Brown 
665f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6663efab7dcSMark Brown 			continue;
6673efab7dcSMark Brown 
668f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
669f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
670db2a4165SFrank Mandarino 	}
671db2a4165SFrank Mandarino 
6722eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6731547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6741547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
6751547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
6761547aba9SMark Brown 		 */
677f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
678ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6791547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
6801547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
681f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
682f0fba2adSLiam Girdwood 				codec->suspended = 0;
6831547aba9SMark Brown 				break;
6841547aba9SMark Brown 			default:
685f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
6861547aba9SMark Brown 				break;
6871547aba9SMark Brown 			}
6881547aba9SMark Brown 		}
689f0fba2adSLiam Girdwood 	}
690db2a4165SFrank Mandarino 
691f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6927bd3a6f3SMark Brown 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
6933efab7dcSMark Brown 
694f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6953efab7dcSMark Brown 			continue;
6963efab7dcSMark Brown 
6977bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6987bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK, codec_dai,
699db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
700f0fba2adSLiam Girdwood 
7017bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
7027bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE, codec_dai,
703db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
704db2a4165SFrank Mandarino 	}
705db2a4165SFrank Mandarino 
7063ff3f64bSMark Brown 	/* unmute any active DACs */
707f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
708f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
709f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7103efab7dcSMark Brown 
711f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7123efab7dcSMark Brown 			continue;
7133efab7dcSMark Brown 
714f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
715f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
716db2a4165SFrank Mandarino 	}
717db2a4165SFrank Mandarino 
718f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
719f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
720f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7213efab7dcSMark Brown 
722f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7233efab7dcSMark Brown 			continue;
7243efab7dcSMark Brown 
725f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
726f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
727f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
728f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
729f0fba2adSLiam Girdwood 			platform->suspended = 0;
730f0fba2adSLiam Girdwood 		}
731db2a4165SFrank Mandarino 	}
732db2a4165SFrank Mandarino 
73387506549SMark Brown 	if (card->resume_post)
73470b2ac12SMark Brown 		card->resume_post(card);
735db2a4165SFrank Mandarino 
736f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
7376ed25978SAndy Green 
7386ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
739f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7406ed25978SAndy Green }
7416ed25978SAndy Green 
7426ed25978SAndy Green /* powers up audio subsystem after a suspend */
7436f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7446ed25978SAndy Green {
7456f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
74682e14e8bSStephen Warren 	int i, ac97_control = 0;
747b9dd94a8SPeter Ujfalusi 
7485ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
7495ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
7505ff1ddf2SEric Miao 	 */
7515ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
7525ff1ddf2SEric Miao 		return 0;
7535ff1ddf2SEric Miao 
75464ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
75564ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
75664ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
75764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
75864ab9baaSMark Brown 	 */
759f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
760f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
76182e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
76282e14e8bSStephen Warren 	}
76382e14e8bSStephen Warren 	if (ac97_control) {
764f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
76564ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
76664ab9baaSMark Brown 	} else {
767f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
7686308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
769f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
770f0fba2adSLiam Girdwood 	}
7716ed25978SAndy Green 
772db2a4165SFrank Mandarino 	return 0;
773db2a4165SFrank Mandarino }
7746f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
775db2a4165SFrank Mandarino #else
7766f8ab4acSMark Brown #define snd_soc_suspend NULL
7776f8ab4acSMark Brown #define snd_soc_resume NULL
778db2a4165SFrank Mandarino #endif
779db2a4165SFrank Mandarino 
78085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
78102a06d30SBarry Song };
78202a06d30SBarry Song 
783f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
784db2a4165SFrank Mandarino {
785f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
786f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
787fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
788435c5e25SMark Brown 	struct snd_soc_platform *platform;
789f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
790848dd8beSMark Brown 	const char *platform_name;
791db2a4165SFrank Mandarino 
792f0fba2adSLiam Girdwood 	if (rtd->complete)
793f0fba2adSLiam Girdwood 		return 1;
794f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
7956308419aSMark Brown 
796f0fba2adSLiam Girdwood 	/* do we already have the CPU DAI for this link ? */
797f0fba2adSLiam Girdwood 	if (rtd->cpu_dai) {
798f0fba2adSLiam Girdwood 		goto find_codec;
799435c5e25SMark Brown 	}
800f0fba2adSLiam Girdwood 	/* no, then find CPU DAI from registered DAIs*/
801f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
8025a504963SStephen Warren 		if (dai_link->cpu_dai_of_node) {
8035a504963SStephen Warren 			if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
8045a504963SStephen Warren 				continue;
8055a504963SStephen Warren 		} else {
8062610ab77SStephen Warren 			if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
8072610ab77SStephen Warren 				continue;
8085a504963SStephen Warren 		}
8092610ab77SStephen Warren 
810f0fba2adSLiam Girdwood 		rtd->cpu_dai = cpu_dai;
811f0fba2adSLiam Girdwood 		goto find_codec;
812f0fba2adSLiam Girdwood 	}
813f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CPU DAI %s not registered\n",
814f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
815f0fba2adSLiam Girdwood 
816f0fba2adSLiam Girdwood find_codec:
817f0fba2adSLiam Girdwood 	/* do we already have the CODEC for this link ? */
818f0fba2adSLiam Girdwood 	if (rtd->codec) {
819f0fba2adSLiam Girdwood 		goto find_platform;
820c5af3a2eSMark Brown 	}
821c5af3a2eSMark Brown 
822f0fba2adSLiam Girdwood 	/* no, then find CODEC from registered CODECs*/
823f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8245a504963SStephen Warren 		if (dai_link->codec_of_node) {
8255a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8265a504963SStephen Warren 				continue;
8275a504963SStephen Warren 		} else {
8282610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8292610ab77SStephen Warren 				continue;
8305a504963SStephen Warren 		}
8312610ab77SStephen Warren 
832f0fba2adSLiam Girdwood 		rtd->codec = codec;
833f0fba2adSLiam Girdwood 
8342610ab77SStephen Warren 		/*
8352610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
8362610ab77SStephen Warren 		 * this CODEC
8372610ab77SStephen Warren 		 */
838f0fba2adSLiam Girdwood 		list_for_each_entry(codec_dai, &dai_list, list) {
839f0fba2adSLiam Girdwood 			if (codec->dev == codec_dai->dev &&
8402610ab77SStephen Warren 				!strcmp(codec_dai->name,
8412610ab77SStephen Warren 					dai_link->codec_dai_name)) {
8422610ab77SStephen Warren 
843f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
844f0fba2adSLiam Girdwood 				goto find_platform;
845435c5e25SMark Brown 			}
846f0fba2adSLiam Girdwood 		}
847f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CODEC DAI %s not registered\n",
848f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
849f0fba2adSLiam Girdwood 
850f0fba2adSLiam Girdwood 		goto find_platform;
851f0fba2adSLiam Girdwood 	}
852f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "CODEC %s not registered\n",
853f0fba2adSLiam Girdwood 			dai_link->codec_name);
854f0fba2adSLiam Girdwood 
855f0fba2adSLiam Girdwood find_platform:
856848dd8beSMark Brown 	/* do we need a platform? */
857848dd8beSMark Brown 	if (rtd->platform)
858f0fba2adSLiam Girdwood 		goto out;
859848dd8beSMark Brown 
860848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
861848dd8beSMark Brown 	platform_name = dai_link->platform_name;
8625a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
863848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
864848dd8beSMark Brown 
865848dd8beSMark Brown 	/* no, then find one from the set of registered platforms */
866f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
8675a504963SStephen Warren 		if (dai_link->platform_of_node) {
8685a504963SStephen Warren 			if (platform->dev->of_node !=
8695a504963SStephen Warren 			    dai_link->platform_of_node)
8705a504963SStephen Warren 				continue;
8715a504963SStephen Warren 		} else {
8722610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
8732610ab77SStephen Warren 				continue;
8745a504963SStephen Warren 		}
8752610ab77SStephen Warren 
876f0fba2adSLiam Girdwood 		rtd->platform = platform;
877f0fba2adSLiam Girdwood 		goto out;
878f0fba2adSLiam Girdwood 	}
8796b05eda6SMark Brown 
880f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "platform %s not registered\n",
881f0fba2adSLiam Girdwood 			dai_link->platform_name);
882f0fba2adSLiam Girdwood 	return 0;
883f0fba2adSLiam Girdwood 
884f0fba2adSLiam Girdwood out:
885f0fba2adSLiam Girdwood 	/* mark rtd as complete if we found all 4 of our client devices */
886f0fba2adSLiam Girdwood 	if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
887f0fba2adSLiam Girdwood 		rtd->complete = 1;
888f0fba2adSLiam Girdwood 		card->num_rtd++;
889f0fba2adSLiam Girdwood 	}
890f0fba2adSLiam Girdwood 	return 1;
8916b05eda6SMark Brown }
8926b05eda6SMark Brown 
893589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
894589c3563SJarkko Nikula {
895589c3563SJarkko Nikula 	int err;
896589c3563SJarkko Nikula 
897589c3563SJarkko Nikula 	if (codec->driver->remove) {
898589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
899589c3563SJarkko Nikula 		if (err < 0)
900589c3563SJarkko Nikula 			dev_err(codec->dev,
901589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
902589c3563SJarkko Nikula 				codec->name, err);
903589c3563SJarkko Nikula 	}
904589c3563SJarkko Nikula 
905589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
906589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
907589c3563SJarkko Nikula 
908589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
909589c3563SJarkko Nikula 	codec->probed = 0;
910589c3563SJarkko Nikula 	list_del(&codec->card_list);
911589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
912589c3563SJarkko Nikula }
913589c3563SJarkko Nikula 
9140168bf0dSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
915f0fba2adSLiam Girdwood {
916f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
917f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
918f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
919f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
920f0fba2adSLiam Girdwood 	int err;
921f0fba2adSLiam Girdwood 
922f0fba2adSLiam Girdwood 	/* unregister the rtd device */
923f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
92436ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
92536ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
92636ae1a96SMark Brown 		device_unregister(rtd->dev);
927f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
92802a06d30SBarry Song 	}
92902a06d30SBarry Song 
930f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9310168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
9320168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
933f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
934f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
935f0fba2adSLiam Girdwood 			if (err < 0)
9360837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9370837fc62SFabio Estevam 							codec_dai->name, err);
938f0fba2adSLiam Girdwood 		}
939f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
940f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
941f0fba2adSLiam Girdwood 	}
942f0fba2adSLiam Girdwood 
943f0fba2adSLiam Girdwood 	/* remove the platform */
9440168bf0dSLiam Girdwood 	if (platform && platform->probed &&
9450168bf0dSLiam Girdwood 			platform->driver->remove_order == order) {
946f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
947f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
948f0fba2adSLiam Girdwood 			if (err < 0)
9490837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9500837fc62SFabio Estevam 							platform->name, err);
951f0fba2adSLiam Girdwood 		}
952675c496bSLiam Girdwood 
953675c496bSLiam Girdwood 		/* Make sure all DAPM widgets are freed */
954675c496bSLiam Girdwood 		snd_soc_dapm_free(&platform->dapm);
955675c496bSLiam Girdwood 
956731f1ab2SSebastien Guiriec 		soc_cleanup_platform_debugfs(platform);
957f0fba2adSLiam Girdwood 		platform->probed = 0;
958f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
959f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
960f0fba2adSLiam Girdwood 	}
961f0fba2adSLiam Girdwood 
962f0fba2adSLiam Girdwood 	/* remove the CODEC */
9630168bf0dSLiam Girdwood 	if (codec && codec->probed &&
9640168bf0dSLiam Girdwood 			codec->driver->remove_order == order)
965589c3563SJarkko Nikula 		soc_remove_codec(codec);
966f0fba2adSLiam Girdwood 
967f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
9680168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
9690168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
970f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
971f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
972f0fba2adSLiam Girdwood 			if (err < 0)
9730837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9740837fc62SFabio Estevam 							cpu_dai->name, err);
975f0fba2adSLiam Girdwood 		}
976f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
977f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
978f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
979f0fba2adSLiam Girdwood 	}
980f0fba2adSLiam Girdwood }
981f0fba2adSLiam Girdwood 
9820671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9830671fd8eSKuninori Morimoto {
9840168bf0dSLiam Girdwood 	int dai, order;
9850671fd8eSKuninori Morimoto 
9860168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9870168bf0dSLiam Girdwood 			order++) {
9880168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
9890168bf0dSLiam Girdwood 			soc_remove_dai_link(card, dai, order);
9900168bf0dSLiam Girdwood 	}
9910671fd8eSKuninori Morimoto 	card->num_rtd = 0;
9920671fd8eSKuninori Morimoto }
9930671fd8eSKuninori Morimoto 
994ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
995ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
996ead9b919SJarkko Nikula {
997ead9b919SJarkko Nikula 	int i;
998ead9b919SJarkko Nikula 
999ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1000ead9b919SJarkko Nikula 		return;
1001ead9b919SJarkko Nikula 
1002ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1003ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1004ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1005ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1006ead9b919SJarkko Nikula 			break;
1007ead9b919SJarkko Nikula 		}
1008ead9b919SJarkko Nikula 	}
1009ead9b919SJarkko Nikula }
1010ead9b919SJarkko Nikula 
1011589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1012589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1013589c3563SJarkko Nikula {
1014589c3563SJarkko Nikula 	int ret = 0;
101589b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1016888df395SMark Brown 	struct snd_soc_dai *dai;
1017589c3563SJarkko Nikula 
1018589c3563SJarkko Nikula 	codec->card = card;
1019589c3563SJarkko Nikula 	codec->dapm.card = card;
1020589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1021589c3563SJarkko Nikula 
102270d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
102370d29331SJarkko Nikula 		return -ENODEV;
102470d29331SJarkko Nikula 
1025d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1026d5d1e0beSLars-Peter Clausen 
102777530150SLars-Peter Clausen 	if (driver->dapm_widgets)
102877530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
102977530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
103077530150SLars-Peter Clausen 
1031888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1032888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1033888df395SMark Brown 		if (dai->dev != codec->dev)
1034888df395SMark Brown 			continue;
1035888df395SMark Brown 
1036888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1037888df395SMark Brown 	}
1038888df395SMark Brown 
103933c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
104033c5f969SMark Brown 
104189b95ac0SMark Brown 	if (driver->probe) {
104289b95ac0SMark Brown 		ret = driver->probe(codec);
1043589c3563SJarkko Nikula 		if (ret < 0) {
1044589c3563SJarkko Nikula 			dev_err(codec->dev,
1045589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1046589c3563SJarkko Nikula 				codec->name, ret);
104770d29331SJarkko Nikula 			goto err_probe;
1048589c3563SJarkko Nikula 		}
1049589c3563SJarkko Nikula 	}
1050589c3563SJarkko Nikula 
1051b7af1dafSMark Brown 	if (driver->controls)
1052022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1053b7af1dafSMark Brown 				     driver->num_controls);
105489b95ac0SMark Brown 	if (driver->dapm_routes)
105589b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
105689b95ac0SMark Brown 					driver->num_dapm_routes);
105789b95ac0SMark Brown 
1058589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1059589c3563SJarkko Nikula 	codec->probed = 1;
1060589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
10617be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1062589c3563SJarkko Nikula 
106370d29331SJarkko Nikula 	return 0;
106470d29331SJarkko Nikula 
106570d29331SJarkko Nikula err_probe:
1066d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
106770d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
106870d29331SJarkko Nikula 
1069589c3563SJarkko Nikula 	return ret;
1070589c3563SJarkko Nikula }
1071589c3563SJarkko Nikula 
1072956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1073956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1074956245e9SLiam Girdwood {
1075956245e9SLiam Girdwood 	int ret = 0;
1076956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1077956245e9SLiam Girdwood 
1078956245e9SLiam Girdwood 	platform->card = card;
1079b7950641SLiam Girdwood 	platform->dapm.card = card;
1080956245e9SLiam Girdwood 
1081956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1082956245e9SLiam Girdwood 		return -ENODEV;
1083956245e9SLiam Girdwood 
1084731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1085731f1ab2SSebastien Guiriec 
1086cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1087cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1088cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1089cb2cf612SLiam Girdwood 
10903fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
10913fec6b6dSStephen Warren 
1092956245e9SLiam Girdwood 	if (driver->probe) {
1093956245e9SLiam Girdwood 		ret = driver->probe(platform);
1094956245e9SLiam Girdwood 		if (ret < 0) {
1095956245e9SLiam Girdwood 			dev_err(platform->dev,
1096956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1097956245e9SLiam Girdwood 				platform->name, ret);
1098956245e9SLiam Girdwood 			goto err_probe;
1099956245e9SLiam Girdwood 		}
1100956245e9SLiam Girdwood 	}
1101956245e9SLiam Girdwood 
1102cb2cf612SLiam Girdwood 	if (driver->controls)
1103cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1104cb2cf612SLiam Girdwood 				     driver->num_controls);
1105cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1106cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1107cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1108cb2cf612SLiam Girdwood 
1109956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1110956245e9SLiam Girdwood 	platform->probed = 1;
1111956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1112b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1113956245e9SLiam Girdwood 
1114956245e9SLiam Girdwood 	return 0;
1115956245e9SLiam Girdwood 
1116956245e9SLiam Girdwood err_probe:
111702db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1118956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1119956245e9SLiam Girdwood 
1120956245e9SLiam Girdwood 	return ret;
1121956245e9SLiam Girdwood }
1122956245e9SLiam Girdwood 
112336ae1a96SMark Brown static void rtd_release(struct device *dev)
112436ae1a96SMark Brown {
112536ae1a96SMark Brown 	kfree(dev);
112636ae1a96SMark Brown }
1127f0fba2adSLiam Girdwood 
1128589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1129589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1130589c3563SJarkko Nikula 				   int num, int dailess)
1131589c3563SJarkko Nikula {
1132589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1133589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1134589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1135589c3563SJarkko Nikula 	const char *temp, *name;
1136589c3563SJarkko Nikula 	int ret = 0;
1137589c3563SJarkko Nikula 
1138589c3563SJarkko Nikula 	if (!dailess) {
1139589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1140589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1141589c3563SJarkko Nikula 		name = dai_link->name;
1142589c3563SJarkko Nikula 	} else {
1143589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1144589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1145589c3563SJarkko Nikula 		name = aux_dev->name;
1146589c3563SJarkko Nikula 	}
11470962bb21SJanusz Krzysztofik 	rtd->card = card;
1148589c3563SJarkko Nikula 
11494b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
11504b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
11514b1cfcb4SMark Brown 
1152589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1153589c3563SJarkko Nikula 	temp = codec->name_prefix;
1154589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1155589c3563SJarkko Nikula 
1156589c3563SJarkko Nikula 	/* do machine specific initialization */
1157589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1158589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1159589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1160589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1161589c3563SJarkko Nikula 	if (ret < 0) {
1162589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1163589c3563SJarkko Nikula 		return ret;
1164589c3563SJarkko Nikula 	}
1165589c3563SJarkko Nikula 	codec->name_prefix = temp;
1166589c3563SJarkko Nikula 
1167589c3563SJarkko Nikula 	/* register the rtd device */
1168589c3563SJarkko Nikula 	rtd->codec = codec;
116936ae1a96SMark Brown 
117036ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
117136ae1a96SMark Brown 	if (!rtd->dev)
117236ae1a96SMark Brown 		return -ENOMEM;
117336ae1a96SMark Brown 	device_initialize(rtd->dev);
117436ae1a96SMark Brown 	rtd->dev->parent = card->dev;
117536ae1a96SMark Brown 	rtd->dev->release = rtd_release;
117636ae1a96SMark Brown 	rtd->dev->init_name = name;
117736ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1178b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
117936ae1a96SMark Brown 	ret = device_add(rtd->dev);
1180589c3563SJarkko Nikula 	if (ret < 0) {
1181589c3563SJarkko Nikula 		dev_err(card->dev,
1182589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1183589c3563SJarkko Nikula 		return ret;
1184589c3563SJarkko Nikula 	}
1185589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1186589c3563SJarkko Nikula 
1187589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
118836ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1189589c3563SJarkko Nikula 	if (ret < 0)
1190589c3563SJarkko Nikula 		dev_err(codec->dev,
1191589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1192589c3563SJarkko Nikula 			ret);
1193589c3563SJarkko Nikula 
1194589c3563SJarkko Nikula 	/* add codec sysfs entries */
119536ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1196589c3563SJarkko Nikula 	if (ret < 0)
1197589c3563SJarkko Nikula 		dev_err(codec->dev,
1198589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1199589c3563SJarkko Nikula 
1200589c3563SJarkko Nikula 	return 0;
1201589c3563SJarkko Nikula }
1202589c3563SJarkko Nikula 
12030168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1204f0fba2adSLiam Girdwood {
1205f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1206f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1207f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1208f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1209f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1210f0fba2adSLiam Girdwood 	int ret;
1211f0fba2adSLiam Girdwood 
12120168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
12130168bf0dSLiam Girdwood 			card->name, num, order);
1214f0fba2adSLiam Girdwood 
1215f0fba2adSLiam Girdwood 	/* config components */
1216f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1217f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1218f0fba2adSLiam Girdwood 	codec_dai->card = card;
1219f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1220f0fba2adSLiam Girdwood 
1221f0fba2adSLiam Girdwood 	/* set default power off timeout */
1222f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1223f0fba2adSLiam Girdwood 
1224f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
12250168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
12260168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
122761b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
122861b61e3cSLiam Girdwood 			return -ENODEV;
122961b61e3cSLiam Girdwood 
1230f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1231f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1232f0fba2adSLiam Girdwood 			if (ret < 0) {
12330837fc62SFabio Estevam 				pr_err("asoc: failed to probe CPU DAI %s: %d\n",
12340837fc62SFabio Estevam 							cpu_dai->name, ret);
123561b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1236f0fba2adSLiam Girdwood 				return ret;
1237f0fba2adSLiam Girdwood 			}
1238f0fba2adSLiam Girdwood 		}
1239f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
12401c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1241f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1242f0fba2adSLiam Girdwood 	}
1243f0fba2adSLiam Girdwood 
1244f0fba2adSLiam Girdwood 	/* probe the CODEC */
12450168bf0dSLiam Girdwood 	if (!codec->probed &&
12460168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1247589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1248589c3563SJarkko Nikula 		if (ret < 0)
1249f0fba2adSLiam Girdwood 			return ret;
1250f0fba2adSLiam Girdwood 	}
1251f0fba2adSLiam Girdwood 
1252f0fba2adSLiam Girdwood 	/* probe the platform */
12530168bf0dSLiam Girdwood 	if (!platform->probed &&
12540168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1255956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1256956245e9SLiam Girdwood 		if (ret < 0)
1257f0fba2adSLiam Girdwood 			return ret;
1258f0fba2adSLiam Girdwood 	}
1259f0fba2adSLiam Girdwood 
1260f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
12610168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1262f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1263f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1264f0fba2adSLiam Girdwood 			if (ret < 0) {
12650837fc62SFabio Estevam 				pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
12660837fc62SFabio Estevam 							codec_dai->name, ret);
1267f0fba2adSLiam Girdwood 				return ret;
1268f0fba2adSLiam Girdwood 			}
1269f0fba2adSLiam Girdwood 		}
1270f0fba2adSLiam Girdwood 
12711c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1272f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1273f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1274f0fba2adSLiam Girdwood 	}
1275f0fba2adSLiam Girdwood 
12760168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
12770168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
12780168bf0dSLiam Girdwood 		return 0;
12790168bf0dSLiam Girdwood 
1280589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1281589c3563SJarkko Nikula 	if (ret)
1282f0fba2adSLiam Girdwood 		return ret;
1283f0fba2adSLiam Girdwood 
128436ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1285f0fba2adSLiam Girdwood 	if (ret < 0)
12860837fc62SFabio Estevam 		pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1287f0fba2adSLiam Girdwood 
1288f0fba2adSLiam Girdwood 	/* create the pcm */
1289f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1290f0fba2adSLiam Girdwood 	if (ret < 0) {
12910837fc62SFabio Estevam 		pr_err("asoc: can't create pcm %s :%d\n",
12920837fc62SFabio Estevam 				dai_link->stream_name, ret);
1293f0fba2adSLiam Girdwood 		return ret;
1294f0fba2adSLiam Girdwood 	}
1295f0fba2adSLiam Girdwood 
1296f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1297f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1298f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1299f0fba2adSLiam Girdwood 
1300f0fba2adSLiam Girdwood 	return 0;
1301f0fba2adSLiam Girdwood }
1302f0fba2adSLiam Girdwood 
1303f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1304f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1305f0fba2adSLiam Girdwood {
1306f0fba2adSLiam Girdwood 	int ret;
1307f0fba2adSLiam Girdwood 
1308f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1309f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
13106b05eda6SMark Brown 	 */
1311f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
13120562f788SMika Westerberg 		/*
13130562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
13140562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
13150562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
13160562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
13170562f788SMika Westerberg 		 *
13180562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
13190562f788SMika Westerberg 		 */
13200562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
13210562f788SMika Westerberg 			return 0;
1322f0fba2adSLiam Girdwood 
1323f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1324f0fba2adSLiam Girdwood 		if (ret < 0) {
13250837fc62SFabio Estevam 			pr_err("asoc: AC97 device register failed:%d\n", ret);
1326f0fba2adSLiam Girdwood 			return ret;
1327435c5e25SMark Brown 		}
1328435c5e25SMark Brown 
1329f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1330f0fba2adSLiam Girdwood 	}
1331f0fba2adSLiam Girdwood 	return 0;
1332f0fba2adSLiam Girdwood }
1333435c5e25SMark Brown 
1334f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1335f0fba2adSLiam Girdwood {
1336f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1337f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1338f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1339f0fba2adSLiam Girdwood 	}
1340f0fba2adSLiam Girdwood }
1341f0fba2adSLiam Girdwood #endif
1342435c5e25SMark Brown 
13432eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
13442eea392dSJarkko Nikula {
13452eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
13462eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1347676ad98aSJarkko Nikula 	int ret = -ENODEV;
13482eea392dSJarkko Nikula 
13492eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
13502eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
13512eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
13522eea392dSJarkko Nikula 			if (codec->probed) {
13532eea392dSJarkko Nikula 				dev_err(codec->dev,
13542eea392dSJarkko Nikula 					"asoc: codec already probed");
13552eea392dSJarkko Nikula 				ret = -EBUSY;
13562eea392dSJarkko Nikula 				goto out;
13572eea392dSJarkko Nikula 			}
1358676ad98aSJarkko Nikula 			goto found;
13592eea392dSJarkko Nikula 		}
13602eea392dSJarkko Nikula 	}
1361676ad98aSJarkko Nikula 	/* codec not found */
1362676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1363676ad98aSJarkko Nikula 	goto out;
13642eea392dSJarkko Nikula 
1365676ad98aSJarkko Nikula found:
1366589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
13672eea392dSJarkko Nikula 	if (ret < 0)
1368589c3563SJarkko Nikula 		return ret;
13692eea392dSJarkko Nikula 
1370589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
13712eea392dSJarkko Nikula 
13722eea392dSJarkko Nikula out:
13732eea392dSJarkko Nikula 	return ret;
13742eea392dSJarkko Nikula }
13752eea392dSJarkko Nikula 
13762eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
13772eea392dSJarkko Nikula {
13782eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
13792eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
13802eea392dSJarkko Nikula 
13812eea392dSJarkko Nikula 	/* unregister the rtd device */
13822eea392dSJarkko Nikula 	if (rtd->dev_registered) {
138336ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
138436ae1a96SMark Brown 		device_del(rtd->dev);
13852eea392dSJarkko Nikula 		rtd->dev_registered = 0;
13862eea392dSJarkko Nikula 	}
13872eea392dSJarkko Nikula 
1388589c3563SJarkko Nikula 	if (codec && codec->probed)
1389589c3563SJarkko Nikula 		soc_remove_codec(codec);
13902eea392dSJarkko Nikula }
13912eea392dSJarkko Nikula 
1392fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1393fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1394fdf0f54dSDimitris Papastamos {
1395fdf0f54dSDimitris Papastamos 	int ret;
1396fdf0f54dSDimitris Papastamos 
1397fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1398fdf0f54dSDimitris Papastamos 		return 0;
1399fdf0f54dSDimitris Papastamos 
1400fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1401fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1402fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1403fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1404fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1405fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1406fdf0f54dSDimitris Papastamos 			ret);
1407fdf0f54dSDimitris Papastamos 		return ret;
1408fdf0f54dSDimitris Papastamos 	}
1409fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1410fdf0f54dSDimitris Papastamos 	return 0;
1411fdf0f54dSDimitris Papastamos }
1412fdf0f54dSDimitris Papastamos 
1413f0fba2adSLiam Girdwood static void snd_soc_instantiate_card(struct snd_soc_card *card)
1414f0fba2adSLiam Girdwood {
1415fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1416fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1417fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
141875d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
14190168bf0dSLiam Girdwood 	int ret, i, order;
142096dd3622SMark Brown 
1421f0fba2adSLiam Girdwood 	mutex_lock(&card->mutex);
1422f0fba2adSLiam Girdwood 
1423f0fba2adSLiam Girdwood 	if (card->instantiated) {
1424f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1425435c5e25SMark Brown 		return;
1426db2a4165SFrank Mandarino 	}
1427db2a4165SFrank Mandarino 
1428f0fba2adSLiam Girdwood 	/* bind DAIs */
1429f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
1430f0fba2adSLiam Girdwood 		soc_bind_dai_link(card, i);
1431f0fba2adSLiam Girdwood 
1432f0fba2adSLiam Girdwood 	/* bind completed ? */
1433f0fba2adSLiam Girdwood 	if (card->num_rtd != card->num_links) {
1434f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1435f0fba2adSLiam Girdwood 		return;
1436db2a4165SFrank Mandarino 	}
1437db2a4165SFrank Mandarino 
1438fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1439fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1440fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1441fdf0f54dSDimitris Papastamos 			continue;
1442861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1443861f2fafSDimitris Papastamos 		compress_type = 0;
1444fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1445fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1446fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1447fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1448fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1449fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1450fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1451fdf0f54dSDimitris Papastamos 					break;
1452fdf0f54dSDimitris Papastamos 			}
1453fdf0f54dSDimitris Papastamos 		}
1454fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1455fdf0f54dSDimitris Papastamos 		if (ret < 0) {
1456fdf0f54dSDimitris Papastamos 			mutex_unlock(&card->mutex);
1457fdf0f54dSDimitris Papastamos 			return;
1458fdf0f54dSDimitris Papastamos 		}
1459fdf0f54dSDimitris Papastamos 	}
1460fdf0f54dSDimitris Papastamos 
1461f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1462f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1463f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1464f0fba2adSLiam Girdwood 	if (ret < 0) {
14650837fc62SFabio Estevam 		pr_err("asoc: can't create sound card for card %s: %d\n",
14660837fc62SFabio Estevam 			card->name, ret);
1467f0fba2adSLiam Girdwood 		mutex_unlock(&card->mutex);
1468f0fba2adSLiam Girdwood 		return;
1469db2a4165SFrank Mandarino 	}
1470f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1471db2a4165SFrank Mandarino 
1472e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1473e37a4970SMark Brown 	card->dapm.dev = card->dev;
1474e37a4970SMark Brown 	card->dapm.card = card;
1475e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1476e37a4970SMark Brown 
1477d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1478d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1479d5d1e0beSLars-Peter Clausen #endif
1480d5d1e0beSLars-Peter Clausen 
148188ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
14826ed25978SAndy Green 	/* deferred resume work */
14836308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
14841301a964SRandy Dunlap #endif
14856ed25978SAndy Green 
14869a841ebbSMark Brown 	if (card->dapm_widgets)
14879a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
14889a841ebbSMark Brown 					  card->num_dapm_widgets);
14899a841ebbSMark Brown 
1490f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1491f0fba2adSLiam Girdwood 	if (card->probe) {
1492e7361ec4SMark Brown 		ret = card->probe(card);
1493f0fba2adSLiam Girdwood 		if (ret < 0)
1494f0fba2adSLiam Girdwood 			goto card_probe_error;
1495f0fba2adSLiam Girdwood 	}
1496f0fba2adSLiam Girdwood 
14970168bf0dSLiam Girdwood 	/* early DAI link probe */
14980168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
14990168bf0dSLiam Girdwood 			order++) {
1500fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
15010168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1502fe3e78e0SMark Brown 			if (ret < 0) {
1503321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1504321de0d0SMark Brown 			       card->name, ret);
1505f0fba2adSLiam Girdwood 				goto probe_dai_err;
1506fe3e78e0SMark Brown 			}
1507fe3e78e0SMark Brown 		}
15080168bf0dSLiam Girdwood 	}
1509fe3e78e0SMark Brown 
15102eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
15112eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
15122eea392dSJarkko Nikula 		if (ret < 0) {
15132eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
15142eea392dSJarkko Nikula 			       card->name, ret);
15152eea392dSJarkko Nikula 			goto probe_aux_dev_err;
15162eea392dSJarkko Nikula 		}
15172eea392dSJarkko Nikula 	}
15182eea392dSJarkko Nikula 
1519888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1520888df395SMark Brown 
1521b7af1dafSMark Brown 	if (card->controls)
1522022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1523b7af1dafSMark Brown 
1524b8ad29deSMark Brown 	if (card->dapm_routes)
1525b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1526b8ad29deSMark Brown 					card->num_dapm_routes);
1527b8ad29deSMark Brown 
1528b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1529b90d2f90SMark Brown 
153075d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
153175d9ac46SMark Brown 		dai_link = &card->dai_link[i];
153275d9ac46SMark Brown 
153375d9ac46SMark Brown 		if (dai_link->dai_fmt) {
153475d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
153575d9ac46SMark Brown 						  dai_link->dai_fmt);
15365e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
153775d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
153875d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
153975d9ac46SMark Brown 					 ret);
154075d9ac46SMark Brown 
154175d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
154275d9ac46SMark Brown 						  dai_link->dai_fmt);
15435e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
154475d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
154575d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
154675d9ac46SMark Brown 					 ret);
154775d9ac46SMark Brown 		}
154875d9ac46SMark Brown 	}
154975d9ac46SMark Brown 
1550f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1551fe3e78e0SMark Brown 		 "%s", card->name);
1552f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
155322de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1554f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1555f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1556f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1557f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1558f0e8ed85SMark Brown 		case '_':
1559f0e8ed85SMark Brown 		case '-':
1560f0e8ed85SMark Brown 		case '\0':
1561f0e8ed85SMark Brown 			break;
1562f0e8ed85SMark Brown 		default:
1563f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1564f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1565f0e8ed85SMark Brown 			break;
1566f0e8ed85SMark Brown 		}
1567f0e8ed85SMark Brown 	}
1568fe3e78e0SMark Brown 
156928e9ad92SMark Brown 	if (card->late_probe) {
157028e9ad92SMark Brown 		ret = card->late_probe(card);
157128e9ad92SMark Brown 		if (ret < 0) {
157228e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
157328e9ad92SMark Brown 				card->name, ret);
157428e9ad92SMark Brown 			goto probe_aux_dev_err;
157528e9ad92SMark Brown 		}
157628e9ad92SMark Brown 	}
157728e9ad92SMark Brown 
15782dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
15792dc00213SMark Brown 
15801633281bSStephen Warren 	if (card->fully_routed)
1581b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
15821633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
15831633281bSStephen Warren 
1584f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1585fe3e78e0SMark Brown 	if (ret < 0) {
15860837fc62SFabio Estevam 		pr_err("asoc: failed to register soundcard for %s: %d\n",
15870837fc62SFabio Estevam 							card->name, ret);
15886b3ed785SAxel Lin 		goto probe_aux_dev_err;
1589fe3e78e0SMark Brown 	}
1590fe3e78e0SMark Brown 
1591fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1592f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1593f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1594f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1595fe3e78e0SMark Brown 		if (ret < 0) {
15960837fc62SFabio Estevam 			pr_err("asoc: failed to register AC97 %s: %d\n",
15970837fc62SFabio Estevam 							card->name, ret);
15986b3ed785SAxel Lin 			while (--i >= 0)
15997d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
16006b3ed785SAxel Lin 			goto probe_aux_dev_err;
1601fe3e78e0SMark Brown 		}
1602fe3e78e0SMark Brown 	}
1603fe3e78e0SMark Brown #endif
1604fe3e78e0SMark Brown 
1605435c5e25SMark Brown 	card->instantiated = 1;
16064f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1607f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1608435c5e25SMark Brown 	return;
1609db2a4165SFrank Mandarino 
16102eea392dSJarkko Nikula probe_aux_dev_err:
16112eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16122eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16132eea392dSJarkko Nikula 
1614f0fba2adSLiam Girdwood probe_dai_err:
16150671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1616fe3e78e0SMark Brown 
1617f0fba2adSLiam Girdwood card_probe_error:
161887506549SMark Brown 	if (card->remove)
1619e7361ec4SMark Brown 		card->remove(card);
1620f0fba2adSLiam Girdwood 
1621f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1622f0fba2adSLiam Girdwood 
1623f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1624435c5e25SMark Brown }
1625db2a4165SFrank Mandarino 
1626435c5e25SMark Brown /*
1627421f91d2SUwe Kleine-König  * Attempt to initialise any uninitialised cards.  Must be called with
1628435c5e25SMark Brown  * client_mutex.
1629435c5e25SMark Brown  */
1630435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1631435c5e25SMark Brown {
1632435c5e25SMark Brown 	struct snd_soc_card *card;
1633435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1634435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1635435c5e25SMark Brown }
1636435c5e25SMark Brown 
1637435c5e25SMark Brown /* probes a new socdev */
1638435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1639435c5e25SMark Brown {
1640f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1641435c5e25SMark Brown 	int ret = 0;
1642435c5e25SMark Brown 
164370a7ca34SVinod Koul 	/*
164470a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
164570a7ca34SVinod Koul 	 * we should not be here in that case so ret error
164670a7ca34SVinod Koul 	 */
164770a7ca34SVinod Koul 	if (!card)
164870a7ca34SVinod Koul 		return -EINVAL;
164970a7ca34SVinod Koul 
1650fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1651fe4085e8SMark Brown 		 "ASoC machine %s should use snd_soc_register_card()\n",
1652fe4085e8SMark Brown 		 card->name);
1653fe4085e8SMark Brown 
1654435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1655435c5e25SMark Brown 	card->dev = &pdev->dev;
1656f0fba2adSLiam Girdwood 
1657435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1658435c5e25SMark Brown 	if (ret != 0) {
1659435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1660db2a4165SFrank Mandarino 		return ret;
1661db2a4165SFrank Mandarino 	}
1662db2a4165SFrank Mandarino 
1663435c5e25SMark Brown 	return 0;
1664435c5e25SMark Brown }
1665435c5e25SMark Brown 
1666b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1667db2a4165SFrank Mandarino {
1668db2a4165SFrank Mandarino 	int i;
1669db2a4165SFrank Mandarino 
1670f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1671f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1672f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
16735b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1674db2a4165SFrank Mandarino 	}
1675db2a4165SFrank Mandarino 
16762eea392dSJarkko Nikula 	/* remove auxiliary devices */
16772eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16782eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16792eea392dSJarkko Nikula 
1680f0fba2adSLiam Girdwood 	/* remove and free each DAI */
16810671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1682f0fba2adSLiam Girdwood 
1683a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1684a6052154SJarkko Nikula 
1685f0fba2adSLiam Girdwood 	/* remove the card */
168687506549SMark Brown 	if (card->remove)
1687e7361ec4SMark Brown 		card->remove(card);
1688f0fba2adSLiam Girdwood 
16890aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
16900aaae527SLars-Peter Clausen 
1691f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1692b0e26485SVinod Koul 	return 0;
1693b0e26485SVinod Koul 
1694b2dfa62cSGuennadi Liakhovetski }
1695b0e26485SVinod Koul 
1696b0e26485SVinod Koul /* removes a socdev */
1697b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1698b0e26485SVinod Koul {
1699b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1700b0e26485SVinod Koul 
1701c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1702db2a4165SFrank Mandarino 	return 0;
1703db2a4165SFrank Mandarino }
1704db2a4165SFrank Mandarino 
17056f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
170651737470SMark Brown {
17076f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1708f0fba2adSLiam Girdwood 	int i;
170951737470SMark Brown 
171051737470SMark Brown 	if (!card->instantiated)
1711416356fcSMark Brown 		return 0;
171251737470SMark Brown 
171351737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
171451737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1715f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1716f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17175b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1718f0fba2adSLiam Girdwood 	}
171951737470SMark Brown 
1720f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1721416356fcSMark Brown 
1722416356fcSMark Brown 	return 0;
172351737470SMark Brown }
17246f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
172551737470SMark Brown 
17266f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1727b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1728b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1729b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1730b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
17316f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1732b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1733416356fcSMark Brown };
1734deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1735416356fcSMark Brown 
1736db2a4165SFrank Mandarino /* ASoC platform driver */
1737db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1738db2a4165SFrank Mandarino 	.driver		= {
1739db2a4165SFrank Mandarino 		.name		= "soc-audio",
17408b45a209SKay Sievers 		.owner		= THIS_MODULE,
17416f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1742db2a4165SFrank Mandarino 	},
1743db2a4165SFrank Mandarino 	.probe		= soc_probe,
1744db2a4165SFrank Mandarino 	.remove		= soc_remove,
1745db2a4165SFrank Mandarino };
1746db2a4165SFrank Mandarino 
1747096e49d5SMark Brown /**
1748096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1749096e49d5SMark Brown  *
1750096e49d5SMark Brown  * @codec: CODEC to query.
1751096e49d5SMark Brown  * @reg: Register to query.
1752096e49d5SMark Brown  *
1753096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1754096e49d5SMark Brown  */
1755181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1756181e055eSMark Brown 				    unsigned int reg)
1757096e49d5SMark Brown {
17581500b7b5SDimitris Papastamos 	if (codec->volatile_register)
17591500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1760096e49d5SMark Brown 	else
1761096e49d5SMark Brown 		return 0;
1762096e49d5SMark Brown }
1763096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1764096e49d5SMark Brown 
1765db2a4165SFrank Mandarino /**
1766239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1767239c9706SDimitris Papastamos  *
1768239c9706SDimitris Papastamos  * @codec: CODEC to query.
1769239c9706SDimitris Papastamos  * @reg: Register to query.
1770239c9706SDimitris Papastamos  *
1771239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1772239c9706SDimitris Papastamos  */
1773239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1774239c9706SDimitris Papastamos 				    unsigned int reg)
1775239c9706SDimitris Papastamos {
1776239c9706SDimitris Papastamos 	if (codec->readable_register)
1777239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1778239c9706SDimitris Papastamos 	else
177963fa0a28SLars-Peter Clausen 		return 1;
1780239c9706SDimitris Papastamos }
1781239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1782239c9706SDimitris Papastamos 
1783239c9706SDimitris Papastamos /**
1784239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1785239c9706SDimitris Papastamos  *
1786239c9706SDimitris Papastamos  * @codec: CODEC to query.
1787239c9706SDimitris Papastamos  * @reg: Register to query.
1788239c9706SDimitris Papastamos  *
1789239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1790239c9706SDimitris Papastamos  */
1791239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1792239c9706SDimitris Papastamos 				    unsigned int reg)
1793239c9706SDimitris Papastamos {
1794239c9706SDimitris Papastamos 	if (codec->writable_register)
1795239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1796239c9706SDimitris Papastamos 	else
179763fa0a28SLars-Peter Clausen 		return 1;
1798239c9706SDimitris Papastamos }
1799239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1800239c9706SDimitris Papastamos 
1801f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1802f1442bc1SLiam Girdwood 					unsigned int reg)
1803f1442bc1SLiam Girdwood {
1804f1442bc1SLiam Girdwood 	unsigned int ret;
1805f1442bc1SLiam Girdwood 
1806f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1807f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1808f1442bc1SLiam Girdwood 		return -1;
1809f1442bc1SLiam Girdwood 	}
1810f1442bc1SLiam Girdwood 
1811f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1812f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1813a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1814f1442bc1SLiam Girdwood 
1815f1442bc1SLiam Girdwood 	return ret;
1816f1442bc1SLiam Girdwood }
1817f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1818f1442bc1SLiam Girdwood 
1819f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1820f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1821f1442bc1SLiam Girdwood {
1822f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1823f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1824f1442bc1SLiam Girdwood 		return -1;
1825f1442bc1SLiam Girdwood 	}
1826f1442bc1SLiam Girdwood 
1827f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1828a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
1829f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1830f1442bc1SLiam Girdwood }
1831f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1832f1442bc1SLiam Girdwood 
1833239c9706SDimitris Papastamos /**
1834db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1835db2a4165SFrank Mandarino  * @codec: audio codec
1836db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1837db2a4165SFrank Mandarino  * @num: AC97 codec number
1838db2a4165SFrank Mandarino  *
1839db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1840db2a4165SFrank Mandarino  */
1841db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1842db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1843db2a4165SFrank Mandarino {
1844db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1845db2a4165SFrank Mandarino 
1846db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1847db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1848db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1849db2a4165SFrank Mandarino 		return -ENOMEM;
1850db2a4165SFrank Mandarino 	}
1851db2a4165SFrank Mandarino 
1852db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1853db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1854db2a4165SFrank Mandarino 		kfree(codec->ac97);
1855db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1856db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1857db2a4165SFrank Mandarino 		return -ENOMEM;
1858db2a4165SFrank Mandarino 	}
1859db2a4165SFrank Mandarino 
1860db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1861db2a4165SFrank Mandarino 	codec->ac97->num = num;
18620562f788SMika Westerberg 
18630562f788SMika Westerberg 	/*
18640562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
18650562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
18660562f788SMika Westerberg 	 */
18670562f788SMika Westerberg 	codec->ac97_created = 1;
18680562f788SMika Westerberg 
1869db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1870db2a4165SFrank Mandarino 	return 0;
1871db2a4165SFrank Mandarino }
1872db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1873db2a4165SFrank Mandarino 
1874db2a4165SFrank Mandarino /**
1875db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1876db2a4165SFrank Mandarino  * @codec: audio codec
1877db2a4165SFrank Mandarino  *
1878db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1879db2a4165SFrank Mandarino  */
1880db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1881db2a4165SFrank Mandarino {
1882db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1883f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1884f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1885f0fba2adSLiam Girdwood #endif
1886db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1887db2a4165SFrank Mandarino 	kfree(codec->ac97);
1888db2a4165SFrank Mandarino 	codec->ac97 = NULL;
18890562f788SMika Westerberg 	codec->ac97_created = 0;
1890db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1891db2a4165SFrank Mandarino }
1892db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1893db2a4165SFrank Mandarino 
1894c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1895c3753707SMark Brown {
1896c3753707SMark Brown 	unsigned int ret;
1897c3753707SMark Brown 
1898c3acec26SMark Brown 	ret = codec->read(codec, reg);
1899c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1900a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1901c3753707SMark Brown 
1902c3753707SMark Brown 	return ret;
1903c3753707SMark Brown }
1904c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1905c3753707SMark Brown 
1906c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1907c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1908c3753707SMark Brown {
1909c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1910a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1911c3acec26SMark Brown 	return codec->write(codec, reg, val);
1912c3753707SMark Brown }
1913c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1914c3753707SMark Brown 
19155fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
19165fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
19175fb609d4SDimitris Papastamos {
19185fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
19195fb609d4SDimitris Papastamos }
19205fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
19215fb609d4SDimitris Papastamos 
1922db2a4165SFrank Mandarino /**
1923db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1924db2a4165SFrank Mandarino  * @codec: audio codec
1925db2a4165SFrank Mandarino  * @reg: codec register
1926db2a4165SFrank Mandarino  * @mask: register mask
1927db2a4165SFrank Mandarino  * @value: new value
1928db2a4165SFrank Mandarino  *
1929db2a4165SFrank Mandarino  * Writes new register value.
1930db2a4165SFrank Mandarino  *
1931180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
1932db2a4165SFrank Mandarino  */
1933db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
193446f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1935db2a4165SFrank Mandarino {
19368a713da8SMark Brown 	bool change;
193746f5822fSDaniel Ribeiro 	unsigned int old, new;
1938180c329dSTimur Tabi 	int ret;
1939db2a4165SFrank Mandarino 
19408a713da8SMark Brown 	if (codec->using_regmap) {
19418a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
19428a713da8SMark Brown 					       mask, value, &change);
19438a713da8SMark Brown 	} else {
1944180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
1945180c329dSTimur Tabi 		if (ret < 0)
1946180c329dSTimur Tabi 			return ret;
1947180c329dSTimur Tabi 
1948180c329dSTimur Tabi 		old = ret;
194978bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
1950db2a4165SFrank Mandarino 		change = old != new;
19518a713da8SMark Brown 		if (change)
1952180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
19538a713da8SMark Brown 	}
19548a713da8SMark Brown 
1955180c329dSTimur Tabi 	if (ret < 0)
1956180c329dSTimur Tabi 		return ret;
1957db2a4165SFrank Mandarino 
1958db2a4165SFrank Mandarino 	return change;
1959db2a4165SFrank Mandarino }
1960db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1961db2a4165SFrank Mandarino 
1962db2a4165SFrank Mandarino /**
19636c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
19646c508c62SEero Nurkkala  * @codec: audio codec
19656c508c62SEero Nurkkala  * @reg: codec register
19666c508c62SEero Nurkkala  * @mask: register mask
19676c508c62SEero Nurkkala  * @value: new value
19686c508c62SEero Nurkkala  *
19696c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
19706c508c62SEero Nurkkala  *
19716c508c62SEero Nurkkala  * Returns 1 for change else 0.
19726c508c62SEero Nurkkala  */
1973dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
19746c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
19756c508c62SEero Nurkkala 			       unsigned int value)
19766c508c62SEero Nurkkala {
19776c508c62SEero Nurkkala 	int change;
19786c508c62SEero Nurkkala 
19796c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
19806c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
19816c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
19826c508c62SEero Nurkkala 
19836c508c62SEero Nurkkala 	return change;
19846c508c62SEero Nurkkala }
1985dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
19866c508c62SEero Nurkkala 
19876c508c62SEero Nurkkala /**
1988db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1989db2a4165SFrank Mandarino  * @codec: audio codec
1990db2a4165SFrank Mandarino  * @reg: codec register
1991db2a4165SFrank Mandarino  * @mask: register mask
1992db2a4165SFrank Mandarino  * @value: new value
1993db2a4165SFrank Mandarino  *
1994db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1995db2a4165SFrank Mandarino  * different from the old value.
1996db2a4165SFrank Mandarino  *
1997db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1998db2a4165SFrank Mandarino  */
1999db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
200046f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2001db2a4165SFrank Mandarino {
2002db2a4165SFrank Mandarino 	int change;
200346f5822fSDaniel Ribeiro 	unsigned int old, new;
2004db2a4165SFrank Mandarino 
2005db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2006db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2007db2a4165SFrank Mandarino 	change = old != new;
2008db2a4165SFrank Mandarino 
2009db2a4165SFrank Mandarino 	return change;
2010db2a4165SFrank Mandarino }
2011db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2012db2a4165SFrank Mandarino 
2013db2a4165SFrank Mandarino /**
2014db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2015db2a4165SFrank Mandarino  * @substream: the pcm substream
2016db2a4165SFrank Mandarino  * @hw: the hardware parameters
2017db2a4165SFrank Mandarino  *
2018db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2019db2a4165SFrank Mandarino  */
2020db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2021db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2022db2a4165SFrank Mandarino {
2023db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2024db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2025db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2026db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2027db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2028db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2029db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2030db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2031db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2032db2a4165SFrank Mandarino 	return 0;
2033db2a4165SFrank Mandarino }
2034db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2035db2a4165SFrank Mandarino 
2036db2a4165SFrank Mandarino /**
2037db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2038db2a4165SFrank Mandarino  * @_template: control template
2039db2a4165SFrank Mandarino  * @data: control private data
2040ac11a2b3SMark Brown  * @long_name: control long name
2041efb7ac3fSMark Brown  * @prefix: control name prefix
2042db2a4165SFrank Mandarino  *
2043db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2044db2a4165SFrank Mandarino  *
2045db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2046db2a4165SFrank Mandarino  */
2047db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
20483056557fSMark Brown 				  void *data, const char *long_name,
2049efb7ac3fSMark Brown 				  const char *prefix)
2050db2a4165SFrank Mandarino {
2051db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2052efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2053efb7ac3fSMark Brown 	char *name = NULL;
2054efb7ac3fSMark Brown 	int name_len;
2055db2a4165SFrank Mandarino 
2056db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2057db2a4165SFrank Mandarino 	template.index = 0;
2058db2a4165SFrank Mandarino 
2059efb7ac3fSMark Brown 	if (!long_name)
2060efb7ac3fSMark Brown 		long_name = template.name;
2061efb7ac3fSMark Brown 
2062efb7ac3fSMark Brown 	if (prefix) {
2063efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
206457cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2065efb7ac3fSMark Brown 		if (!name)
2066efb7ac3fSMark Brown 			return NULL;
2067efb7ac3fSMark Brown 
2068efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2069efb7ac3fSMark Brown 
2070efb7ac3fSMark Brown 		template.name = name;
2071efb7ac3fSMark Brown 	} else {
2072efb7ac3fSMark Brown 		template.name = long_name;
2073efb7ac3fSMark Brown 	}
2074efb7ac3fSMark Brown 
2075efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2076efb7ac3fSMark Brown 
2077efb7ac3fSMark Brown 	kfree(name);
2078efb7ac3fSMark Brown 
2079efb7ac3fSMark Brown 	return kcontrol;
2080db2a4165SFrank Mandarino }
2081db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2082db2a4165SFrank Mandarino 
2083022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2084022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2085022658beSLiam Girdwood 	const char *prefix, void *data)
2086022658beSLiam Girdwood {
2087022658beSLiam Girdwood 	int err, i;
2088022658beSLiam Girdwood 
2089022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2090022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2091022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2092022658beSLiam Girdwood 						     control->name, prefix));
2093022658beSLiam Girdwood 		if (err < 0) {
2094022658beSLiam Girdwood 			dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2095022658beSLiam Girdwood 			return err;
2096022658beSLiam Girdwood 		}
2097022658beSLiam Girdwood 	}
2098022658beSLiam Girdwood 
2099022658beSLiam Girdwood 	return 0;
2100022658beSLiam Girdwood }
2101022658beSLiam Girdwood 
2102db2a4165SFrank Mandarino /**
2103022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2104022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
21053e8e1952SIan Molton  * duplicating this code.
21063e8e1952SIan Molton  *
21073e8e1952SIan Molton  * @codec: codec to add controls to
21083e8e1952SIan Molton  * @controls: array of controls to add
21093e8e1952SIan Molton  * @num_controls: number of elements in the array
21103e8e1952SIan Molton  *
21113e8e1952SIan Molton  * Return 0 for success, else error.
21123e8e1952SIan Molton  */
2113022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
21143e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
21153e8e1952SIan Molton {
2116f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
21173e8e1952SIan Molton 
2118022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2119022658beSLiam Girdwood 			codec->name_prefix, codec);
21203e8e1952SIan Molton }
2121022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
21223e8e1952SIan Molton 
21233e8e1952SIan Molton /**
2124a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2125022658beSLiam Girdwood  * Convenience function to add a list of controls.
2126a491a5c8SLiam Girdwood  *
2127a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2128a491a5c8SLiam Girdwood  * @controls: array of controls to add
2129a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2130a491a5c8SLiam Girdwood  *
2131a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2132a491a5c8SLiam Girdwood  */
2133a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2134a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2135a491a5c8SLiam Girdwood {
2136a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2137a491a5c8SLiam Girdwood 
2138022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2139022658beSLiam Girdwood 			NULL, platform);
2140a491a5c8SLiam Girdwood }
2141a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2142a491a5c8SLiam Girdwood 
2143a491a5c8SLiam Girdwood /**
2144022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2145022658beSLiam Girdwood  * Convenience function to add a list of controls.
2146022658beSLiam Girdwood  *
2147022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2148022658beSLiam Girdwood  * @controls: array of controls to add
2149022658beSLiam Girdwood  * @num_controls: number of elements in the array
2150022658beSLiam Girdwood  *
2151022658beSLiam Girdwood  * Return 0 for success, else error.
2152022658beSLiam Girdwood  */
2153022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2154022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2155022658beSLiam Girdwood {
2156022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2157022658beSLiam Girdwood 
2158022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2159022658beSLiam Girdwood 			NULL, soc_card);
2160022658beSLiam Girdwood }
2161022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2162022658beSLiam Girdwood 
2163022658beSLiam Girdwood /**
2164022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2165022658beSLiam Girdwood  * Convienience function to add a list of controls.
2166022658beSLiam Girdwood  *
2167022658beSLiam Girdwood  * @dai: DAI to add controls to
2168022658beSLiam Girdwood  * @controls: array of controls to add
2169022658beSLiam Girdwood  * @num_controls: number of elements in the array
2170022658beSLiam Girdwood  *
2171022658beSLiam Girdwood  * Return 0 for success, else error.
2172022658beSLiam Girdwood  */
2173022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2174022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2175022658beSLiam Girdwood {
2176022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2177022658beSLiam Girdwood 
2178022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2179022658beSLiam Girdwood 			NULL, dai);
2180022658beSLiam Girdwood }
2181022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2182022658beSLiam Girdwood 
2183022658beSLiam Girdwood /**
2184db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2185db2a4165SFrank Mandarino  * @kcontrol: mixer control
2186db2a4165SFrank Mandarino  * @uinfo: control element information
2187db2a4165SFrank Mandarino  *
2188db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2189db2a4165SFrank Mandarino  * mixer control.
2190db2a4165SFrank Mandarino  *
2191db2a4165SFrank Mandarino  * Returns 0 for success.
2192db2a4165SFrank Mandarino  */
2193db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2194db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2195db2a4165SFrank Mandarino {
2196db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2197db2a4165SFrank Mandarino 
2198db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2199db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2200f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2201db2a4165SFrank Mandarino 
2202f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2203f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2204db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2205db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2206db2a4165SFrank Mandarino 	return 0;
2207db2a4165SFrank Mandarino }
2208db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2209db2a4165SFrank Mandarino 
2210db2a4165SFrank Mandarino /**
2211db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2212db2a4165SFrank Mandarino  * @kcontrol: mixer control
2213ac11a2b3SMark Brown  * @ucontrol: control element information
2214db2a4165SFrank Mandarino  *
2215db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2216db2a4165SFrank Mandarino  *
2217db2a4165SFrank Mandarino  * Returns 0 for success.
2218db2a4165SFrank Mandarino  */
2219db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2220db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2221db2a4165SFrank Mandarino {
2222db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2223db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
222446f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2225db2a4165SFrank Mandarino 
2226f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2227db2a4165SFrank Mandarino 		;
2228db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
22293ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
22303ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2231db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2232db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2233db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2234db2a4165SFrank Mandarino 
2235db2a4165SFrank Mandarino 	return 0;
2236db2a4165SFrank Mandarino }
2237db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2238db2a4165SFrank Mandarino 
2239db2a4165SFrank Mandarino /**
2240db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2241db2a4165SFrank Mandarino  * @kcontrol: mixer control
2242ac11a2b3SMark Brown  * @ucontrol: control element information
2243db2a4165SFrank Mandarino  *
2244db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2245db2a4165SFrank Mandarino  *
2246db2a4165SFrank Mandarino  * Returns 0 for success.
2247db2a4165SFrank Mandarino  */
2248db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2249db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2250db2a4165SFrank Mandarino {
2251db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2252db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
225346f5822fSDaniel Ribeiro 	unsigned int val;
225446f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2255db2a4165SFrank Mandarino 
2256f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2257db2a4165SFrank Mandarino 		;
2258f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2259db2a4165SFrank Mandarino 		return -EINVAL;
2260db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2261db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2262db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2263f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2264db2a4165SFrank Mandarino 			return -EINVAL;
2265db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2266db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2267db2a4165SFrank Mandarino 	}
2268db2a4165SFrank Mandarino 
22696c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2270db2a4165SFrank Mandarino }
2271db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2272db2a4165SFrank Mandarino 
2273db2a4165SFrank Mandarino /**
22742e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
22752e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
22762e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
22772e72f8e3SPeter Ujfalusi  *
22782e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
22792e72f8e3SPeter Ujfalusi  *
22802e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
22812e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
22822e72f8e3SPeter Ujfalusi  *
22832e72f8e3SPeter Ujfalusi  * Returns 0 for success.
22842e72f8e3SPeter Ujfalusi  */
22852e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
22862e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
22872e72f8e3SPeter Ujfalusi {
22882e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
228974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
229046f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
22912e72f8e3SPeter Ujfalusi 
22922e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
22932e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
22942e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
22952e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
22962e72f8e3SPeter Ujfalusi 			break;
22972e72f8e3SPeter Ujfalusi 	}
22982e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
22992e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23002e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
23012e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
23022e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
23032e72f8e3SPeter Ujfalusi 				break;
23042e72f8e3SPeter Ujfalusi 		}
23052e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
23062e72f8e3SPeter Ujfalusi 	}
23072e72f8e3SPeter Ujfalusi 
23082e72f8e3SPeter Ujfalusi 	return 0;
23092e72f8e3SPeter Ujfalusi }
23102e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
23112e72f8e3SPeter Ujfalusi 
23122e72f8e3SPeter Ujfalusi /**
23132e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
23142e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23152e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23162e72f8e3SPeter Ujfalusi  *
23172e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
23182e72f8e3SPeter Ujfalusi  *
23192e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23202e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23212e72f8e3SPeter Ujfalusi  *
23222e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23232e72f8e3SPeter Ujfalusi  */
23242e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
23252e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23262e72f8e3SPeter Ujfalusi {
23272e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
232874155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
232946f5822fSDaniel Ribeiro 	unsigned int val;
233046f5822fSDaniel Ribeiro 	unsigned int mask;
23312e72f8e3SPeter Ujfalusi 
23322e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
23332e72f8e3SPeter Ujfalusi 		return -EINVAL;
23342e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
23352e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
23362e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23372e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
23382e72f8e3SPeter Ujfalusi 			return -EINVAL;
23392e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
23402e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
23412e72f8e3SPeter Ujfalusi 	}
23422e72f8e3SPeter Ujfalusi 
23436c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
23442e72f8e3SPeter Ujfalusi }
23452e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
23462e72f8e3SPeter Ujfalusi 
23472e72f8e3SPeter Ujfalusi /**
2348db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2349db2a4165SFrank Mandarino  * @kcontrol: mixer control
2350db2a4165SFrank Mandarino  * @uinfo: control element information
2351db2a4165SFrank Mandarino  *
2352db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2353db2a4165SFrank Mandarino  * single mixer.
2354db2a4165SFrank Mandarino  *
2355db2a4165SFrank Mandarino  * Returns 0 for success.
2356db2a4165SFrank Mandarino  */
2357db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2358db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2359db2a4165SFrank Mandarino {
2360db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2361db2a4165SFrank Mandarino 
2362db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2363db2a4165SFrank Mandarino 	uinfo->count = 1;
2364f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2365db2a4165SFrank Mandarino 
2366f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2367f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2368db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2369db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2370db2a4165SFrank Mandarino 	return 0;
2371db2a4165SFrank Mandarino }
2372db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2373db2a4165SFrank Mandarino 
2374db2a4165SFrank Mandarino /**
2375db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2376db2a4165SFrank Mandarino  * @kcontrol: mixer control
2377db2a4165SFrank Mandarino  * @uinfo: control element information
2378db2a4165SFrank Mandarino  *
2379db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2380db2a4165SFrank Mandarino  *
2381db2a4165SFrank Mandarino  * Returns 0 for success.
2382db2a4165SFrank Mandarino  */
2383db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2384db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2385db2a4165SFrank Mandarino {
2386a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2387db2a4165SFrank Mandarino 
2388fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2389a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2390a7a4ac86SPhilipp Zabel 	else
2391a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2392a7a4ac86SPhilipp Zabel 
2393db2a4165SFrank Mandarino 	uinfo->count = 1;
2394db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2395a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2396db2a4165SFrank Mandarino 	return 0;
2397db2a4165SFrank Mandarino }
2398db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2399db2a4165SFrank Mandarino 
2400db2a4165SFrank Mandarino /**
2401db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2402db2a4165SFrank Mandarino  * @kcontrol: mixer control
2403db2a4165SFrank Mandarino  * @uinfo: control element information
2404db2a4165SFrank Mandarino  *
2405e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2406e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2407db2a4165SFrank Mandarino  *
2408db2a4165SFrank Mandarino  * Returns 0 for success.
2409db2a4165SFrank Mandarino  */
2410db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2411db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2412db2a4165SFrank Mandarino {
24134eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24144eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2415d11bb4a9SPeter Ujfalusi 	int platform_max;
2416db2a4165SFrank Mandarino 
2417d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2418d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2419d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2420d11bb4a9SPeter Ujfalusi 
2421d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2422a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2423a7a4ac86SPhilipp Zabel 	else
2424a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2425a7a4ac86SPhilipp Zabel 
2426e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2427db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2428d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2429db2a4165SFrank Mandarino 	return 0;
2430db2a4165SFrank Mandarino }
2431db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2432db2a4165SFrank Mandarino 
2433db2a4165SFrank Mandarino /**
2434db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2435db2a4165SFrank Mandarino  * @kcontrol: mixer control
2436ac11a2b3SMark Brown  * @ucontrol: control element information
2437db2a4165SFrank Mandarino  *
2438f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2439f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2440db2a4165SFrank Mandarino  *
2441db2a4165SFrank Mandarino  * Returns 0 for success.
2442db2a4165SFrank Mandarino  */
2443db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2444db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2445db2a4165SFrank Mandarino {
24464eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24474eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2448db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2449815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2450f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2451815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2452815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
24534eaa9819SJon Smirl 	int max = mc->max;
2454815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2455815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2456db2a4165SFrank Mandarino 
2457db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2458db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2459f7915d99SPeter Ujfalusi 	if (invert)
2460db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2461a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2462f7915d99SPeter Ujfalusi 
2463f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2464f7915d99SPeter Ujfalusi 		if (reg == reg2)
2465f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2466f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2467f7915d99SPeter Ujfalusi 		else
2468f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2469f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2470f7915d99SPeter Ujfalusi 		if (invert)
2471db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2472a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2473db2a4165SFrank Mandarino 	}
2474db2a4165SFrank Mandarino 
2475db2a4165SFrank Mandarino 	return 0;
2476db2a4165SFrank Mandarino }
2477db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2478db2a4165SFrank Mandarino 
2479db2a4165SFrank Mandarino /**
2480db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2481db2a4165SFrank Mandarino  * @kcontrol: mixer control
2482ac11a2b3SMark Brown  * @ucontrol: control element information
2483db2a4165SFrank Mandarino  *
2484974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2485974815baSPeter Ujfalusi  * control that spans 2 registers.
2486db2a4165SFrank Mandarino  *
2487db2a4165SFrank Mandarino  * Returns 0 for success.
2488db2a4165SFrank Mandarino  */
2489db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2490db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2491db2a4165SFrank Mandarino {
24924eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24934eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2494db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2495815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2496974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2497815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2498815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
24994eaa9819SJon Smirl 	int max = mc->max;
2500815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2501815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2502974815baSPeter Ujfalusi 	int err;
2503974815baSPeter Ujfalusi 	bool type_2r = 0;
2504974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2505974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2506db2a4165SFrank Mandarino 
2507db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2508db2a4165SFrank Mandarino 	if (invert)
2509a7a4ac86SPhilipp Zabel 		val = max - val;
2510db2a4165SFrank Mandarino 	val_mask = mask << shift;
2511db2a4165SFrank Mandarino 	val = val << shift;
2512974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2513db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2514db2a4165SFrank Mandarino 		if (invert)
2515a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2516974815baSPeter Ujfalusi 		if (reg == reg2) {
2517db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2518db2a4165SFrank Mandarino 			val |= val2 << rshift;
2519974815baSPeter Ujfalusi 		} else {
2520db2a4165SFrank Mandarino 			val2 = val2 << shift;
2521974815baSPeter Ujfalusi 			type_2r = 1;
2522974815baSPeter Ujfalusi 		}
2523974815baSPeter Ujfalusi 	}
25246c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
25253ff3f64bSMark Brown 	if (err < 0)
2526db2a4165SFrank Mandarino 		return err;
2527db2a4165SFrank Mandarino 
2528974815baSPeter Ujfalusi 	if (type_2r)
25296c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2530974815baSPeter Ujfalusi 
2531db2a4165SFrank Mandarino 	return err;
2532db2a4165SFrank Mandarino }
2533974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2534db2a4165SFrank Mandarino 
2535e13ac2e9SMark Brown /**
2536e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2537e13ac2e9SMark Brown  * @kcontrol: mixer control
2538e13ac2e9SMark Brown  * @uinfo: control element information
2539e13ac2e9SMark Brown  *
2540e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2541e13ac2e9SMark Brown  *
2542e13ac2e9SMark Brown  * Returns 0 for success.
2543e13ac2e9SMark Brown  */
2544e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2545e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2546e13ac2e9SMark Brown {
25474eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25484eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2549d11bb4a9SPeter Ujfalusi 	int platform_max;
25504eaa9819SJon Smirl 	int min = mc->min;
2551e13ac2e9SMark Brown 
2552d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2553d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2554d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2555d11bb4a9SPeter Ujfalusi 
2556e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2557e13ac2e9SMark Brown 	uinfo->count = 2;
2558e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2559d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2560e13ac2e9SMark Brown 	return 0;
2561e13ac2e9SMark Brown }
2562e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2563e13ac2e9SMark Brown 
2564e13ac2e9SMark Brown /**
2565e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2566e13ac2e9SMark Brown  * @kcontrol: mixer control
2567ac11a2b3SMark Brown  * @ucontrol: control element information
2568e13ac2e9SMark Brown  *
2569e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2570e13ac2e9SMark Brown  *
2571e13ac2e9SMark Brown  * Returns 0 for success.
2572e13ac2e9SMark Brown  */
2573e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2574e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2575e13ac2e9SMark Brown {
25764eaa9819SJon Smirl 	struct soc_mixer_control *mc =
25774eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2578e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2579815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
25804eaa9819SJon Smirl 	int min = mc->min;
2581e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2582e13ac2e9SMark Brown 
2583e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2584e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2585e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2586e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2587e13ac2e9SMark Brown 	return 0;
2588e13ac2e9SMark Brown }
2589e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2590e13ac2e9SMark Brown 
2591e13ac2e9SMark Brown /**
2592e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2593e13ac2e9SMark Brown  * @kcontrol: mixer control
2594ac11a2b3SMark Brown  * @ucontrol: control element information
2595e13ac2e9SMark Brown  *
2596e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2597e13ac2e9SMark Brown  *
2598e13ac2e9SMark Brown  * Returns 0 for success.
2599e13ac2e9SMark Brown  */
2600e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2601e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2602e13ac2e9SMark Brown {
26034eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26044eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2605e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2606815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
26074eaa9819SJon Smirl 	int min = mc->min;
260846f5822fSDaniel Ribeiro 	unsigned int val;
2609e13ac2e9SMark Brown 
2610e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2611e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2612e13ac2e9SMark Brown 
26136c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2614e13ac2e9SMark Brown }
2615e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2616e13ac2e9SMark Brown 
26178c6529dbSLiam Girdwood /**
2618637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2619637d3847SPeter Ujfalusi  *
2620637d3847SPeter Ujfalusi  * @codec: where to look for the control
2621637d3847SPeter Ujfalusi  * @name: Name of the control
2622637d3847SPeter Ujfalusi  * @max: new maximum limit
2623637d3847SPeter Ujfalusi  *
2624637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2625637d3847SPeter Ujfalusi  */
2626637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2627637d3847SPeter Ujfalusi 	const char *name, int max)
2628637d3847SPeter Ujfalusi {
2629f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2630637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2631637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2632637d3847SPeter Ujfalusi 	int found = 0;
2633637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2634637d3847SPeter Ujfalusi 
2635637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2636637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2637637d3847SPeter Ujfalusi 		return -EINVAL;
2638637d3847SPeter Ujfalusi 
2639637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2640637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2641637d3847SPeter Ujfalusi 			found = 1;
2642637d3847SPeter Ujfalusi 			break;
2643637d3847SPeter Ujfalusi 		}
2644637d3847SPeter Ujfalusi 	}
2645637d3847SPeter Ujfalusi 	if (found) {
2646637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2647637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2648d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2649637d3847SPeter Ujfalusi 			ret = 0;
2650637d3847SPeter Ujfalusi 		}
2651637d3847SPeter Ujfalusi 	}
2652637d3847SPeter Ujfalusi 	return ret;
2653637d3847SPeter Ujfalusi }
2654637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2655637d3847SPeter Ujfalusi 
2656637d3847SPeter Ujfalusi /**
2657b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2658b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2659b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2660b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2661b6f4bb38Sapatard@mandriva.com  *
2662b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2663b6f4bb38Sapatard@mandriva.com  */
2664b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2665b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2666b6f4bb38Sapatard@mandriva.com {
2667b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2668b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2669b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2670b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2671b6f4bb38Sapatard@mandriva.com 
2672b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2673b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2674b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2675b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2676b6f4bb38Sapatard@mandriva.com 
2677b6f4bb38Sapatard@mandriva.com 	return 0;
2678b6f4bb38Sapatard@mandriva.com }
2679b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2680b6f4bb38Sapatard@mandriva.com 
2681b6f4bb38Sapatard@mandriva.com /**
2682b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2683b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2684b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2685b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2686b6f4bb38Sapatard@mandriva.com  *
2687b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2688b6f4bb38Sapatard@mandriva.com  */
2689b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2690b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2691b6f4bb38Sapatard@mandriva.com {
2692b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2693b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2694b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2695b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2696b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2697b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2698b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2699b6f4bb38Sapatard@mandriva.com 
270020630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
270120630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2702b6f4bb38Sapatard@mandriva.com 	return 0;
2703b6f4bb38Sapatard@mandriva.com }
2704b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2705b6f4bb38Sapatard@mandriva.com 
2706b6f4bb38Sapatard@mandriva.com /**
2707b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2708b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2709b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2710b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2711b6f4bb38Sapatard@mandriva.com  *
2712b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2713b6f4bb38Sapatard@mandriva.com  */
2714b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2715b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2716b6f4bb38Sapatard@mandriva.com {
2717b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2718b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2719b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2720b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2721b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2722b6f4bb38Sapatard@mandriva.com 	int ret;
2723b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2724b6f4bb38Sapatard@mandriva.com 
2725b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2726b6f4bb38Sapatard@mandriva.com 	val &= mask;
2727b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2728b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2729b6f4bb38Sapatard@mandriva.com 
2730b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2731b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2732b6f4bb38Sapatard@mandriva.com 
2733b6f4bb38Sapatard@mandriva.com 	ret = 0;
2734b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2735b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2736b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2737f1df5aecSMark Brown 			return ret;
2738b6f4bb38Sapatard@mandriva.com 	}
2739b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2740b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2741b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2742f1df5aecSMark Brown 			return ret;
2743b6f4bb38Sapatard@mandriva.com 	}
2744b6f4bb38Sapatard@mandriva.com 
2745b6f4bb38Sapatard@mandriva.com 	return 0;
2746b6f4bb38Sapatard@mandriva.com }
2747b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2748b6f4bb38Sapatard@mandriva.com 
274971d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
275071d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
275171d08516SMark Brown {
275271d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
275371d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
275471d08516SMark Brown 
275571d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
275671d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
275771d08516SMark Brown 
275871d08516SMark Brown 	return 0;
275971d08516SMark Brown }
276071d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
276171d08516SMark Brown 
276271d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
276371d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
276471d08516SMark Brown {
276571d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
276671d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
276771d08516SMark Brown 	int ret;
276871d08516SMark Brown 
276971d08516SMark Brown 	if (codec->using_regmap)
277071d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
277171d08516SMark Brown 				      ucontrol->value.bytes.data,
277271d08516SMark Brown 				      params->num_regs * codec->val_bytes);
277371d08516SMark Brown 	else
277471d08516SMark Brown 		ret = -EINVAL;
277571d08516SMark Brown 
2776f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
2777f831b055SMark Brown 	if (ret == 0 && params->mask) {
2778f831b055SMark Brown 		switch (codec->val_bytes) {
2779f831b055SMark Brown 		case 1:
2780f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
2781f831b055SMark Brown 			break;
2782f831b055SMark Brown 		case 2:
2783f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
2784f831b055SMark Brown 				&= ~params->mask;
2785f831b055SMark Brown 			break;
2786f831b055SMark Brown 		case 4:
2787f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
2788f831b055SMark Brown 				&= ~params->mask;
2789f831b055SMark Brown 			break;
2790f831b055SMark Brown 		default:
2791f831b055SMark Brown 			return -EINVAL;
2792f831b055SMark Brown 		}
2793f831b055SMark Brown 	}
2794f831b055SMark Brown 
279571d08516SMark Brown 	return ret;
279671d08516SMark Brown }
279771d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
279871d08516SMark Brown 
279971d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
280071d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
280171d08516SMark Brown {
280271d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
280371d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2804f831b055SMark Brown 	int ret, len;
2805f831b055SMark Brown 	unsigned int val;
2806f831b055SMark Brown 	void *data;
280771d08516SMark Brown 
2808f831b055SMark Brown 	if (!codec->using_regmap)
2809f831b055SMark Brown 		return -EINVAL;
2810f831b055SMark Brown 
2811f831b055SMark Brown 	data = ucontrol->value.bytes.data;
2812f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
2813f831b055SMark Brown 
2814f831b055SMark Brown 	/*
2815f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
2816f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
2817f831b055SMark Brown 	 * copy.
2818f831b055SMark Brown 	 */
2819f831b055SMark Brown 	if (params->mask) {
2820f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
2821f831b055SMark Brown 		if (ret != 0)
2822f831b055SMark Brown 			return ret;
2823f831b055SMark Brown 
2824f831b055SMark Brown 		val &= params->mask;
2825f831b055SMark Brown 
2826f831b055SMark Brown 		data = kmemdup(data, len, GFP_KERNEL);
2827f831b055SMark Brown 		if (!data)
2828f831b055SMark Brown 			return -ENOMEM;
2829f831b055SMark Brown 
2830f831b055SMark Brown 		switch (codec->val_bytes) {
2831f831b055SMark Brown 		case 1:
2832f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
2833f831b055SMark Brown 			((u8 *)data)[0] |= val;
2834f831b055SMark Brown 			break;
2835f831b055SMark Brown 		case 2:
2836f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2837f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
2838f831b055SMark Brown 			break;
2839f831b055SMark Brown 		case 4:
2840f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2841f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
2842f831b055SMark Brown 			break;
2843f831b055SMark Brown 		default:
2844f831b055SMark Brown 			return -EINVAL;
2845f831b055SMark Brown 		}
2846f831b055SMark Brown 	}
2847f831b055SMark Brown 
284871d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
2849f831b055SMark Brown 			       data, len);
2850f831b055SMark Brown 
2851f831b055SMark Brown 	if (params->mask)
2852f831b055SMark Brown 		kfree(data);
285371d08516SMark Brown 
285471d08516SMark Brown 	return ret;
285571d08516SMark Brown }
285671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
285771d08516SMark Brown 
2858b6f4bb38Sapatard@mandriva.com /**
28598c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
28608c6529dbSLiam Girdwood  * @dai: DAI
28618c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
28628c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
28638c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
28648c6529dbSLiam Girdwood  *
28658c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
28668c6529dbSLiam Girdwood  */
28678c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
28688c6529dbSLiam Girdwood 	unsigned int freq, int dir)
28698c6529dbSLiam Girdwood {
2870f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2871f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2872ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2873da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2874ec4ee52aSMark Brown 						      freq, dir);
28758c6529dbSLiam Girdwood 	else
28768c6529dbSLiam Girdwood 		return -EINVAL;
28778c6529dbSLiam Girdwood }
28788c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
28798c6529dbSLiam Girdwood 
28808c6529dbSLiam Girdwood /**
2881ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2882ec4ee52aSMark Brown  * @codec: CODEC
2883ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2884da1c6ea6SMark Brown  * @source: Source for the clock
2885ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2886ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2887ec4ee52aSMark Brown  *
2888ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2889ec4ee52aSMark Brown  */
2890ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2891da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2892ec4ee52aSMark Brown {
2893ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2894da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2895da1c6ea6SMark Brown 						 freq, dir);
2896ec4ee52aSMark Brown 	else
2897ec4ee52aSMark Brown 		return -EINVAL;
2898ec4ee52aSMark Brown }
2899ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2900ec4ee52aSMark Brown 
2901ec4ee52aSMark Brown /**
29028c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
29038c6529dbSLiam Girdwood  * @dai: DAI
2904ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
29058c6529dbSLiam Girdwood  * @div: new clock divisor.
29068c6529dbSLiam Girdwood  *
29078c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
29088c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
29098c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
29108c6529dbSLiam Girdwood  */
29118c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
29128c6529dbSLiam Girdwood 	int div_id, int div)
29138c6529dbSLiam Girdwood {
2914f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2915f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
29168c6529dbSLiam Girdwood 	else
29178c6529dbSLiam Girdwood 		return -EINVAL;
29188c6529dbSLiam Girdwood }
29198c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
29208c6529dbSLiam Girdwood 
29218c6529dbSLiam Girdwood /**
29228c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
29238c6529dbSLiam Girdwood  * @dai: DAI
29248c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
292585488037SMark Brown  * @source: DAI specific source for the PLL
29268c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
29278c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
29288c6529dbSLiam Girdwood  *
29298c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
29308c6529dbSLiam Girdwood  */
293185488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
293285488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
29338c6529dbSLiam Girdwood {
2934f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2935f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
293685488037SMark Brown 					 freq_in, freq_out);
2937ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2938ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2939ec4ee52aSMark Brown 						   freq_in, freq_out);
29408c6529dbSLiam Girdwood 	else
29418c6529dbSLiam Girdwood 		return -EINVAL;
29428c6529dbSLiam Girdwood }
29438c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
29448c6529dbSLiam Girdwood 
2945ec4ee52aSMark Brown /*
2946ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2947ec4ee52aSMark Brown  * @codec: CODEC
2948ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2949ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2950ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2951ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2952ec4ee52aSMark Brown  *
2953ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2954ec4ee52aSMark Brown  */
2955ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2956ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2957ec4ee52aSMark Brown {
2958ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2959ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2960ec4ee52aSMark Brown 					      freq_in, freq_out);
2961ec4ee52aSMark Brown 	else
2962ec4ee52aSMark Brown 		return -EINVAL;
2963ec4ee52aSMark Brown }
2964ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2965ec4ee52aSMark Brown 
29668c6529dbSLiam Girdwood /**
29678c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
29688c6529dbSLiam Girdwood  * @dai: DAI
29698c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
29708c6529dbSLiam Girdwood  *
29718c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
29728c6529dbSLiam Girdwood  */
29738c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
29748c6529dbSLiam Girdwood {
29755e4ba569SShawn Guo 	if (dai->driver == NULL)
29768c6529dbSLiam Girdwood 		return -EINVAL;
29775e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
29785e4ba569SShawn Guo 		return -ENOTSUPP;
29795e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
29808c6529dbSLiam Girdwood }
29818c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
29828c6529dbSLiam Girdwood 
29838c6529dbSLiam Girdwood /**
29848c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
29858c6529dbSLiam Girdwood  * @dai: DAI
2986a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2987a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
29888c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2989a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
29908c6529dbSLiam Girdwood  *
29918c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
29928c6529dbSLiam Girdwood  * specific.
29938c6529dbSLiam Girdwood  */
29948c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2995a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
29968c6529dbSLiam Girdwood {
2997f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2998f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2999a5479e38SDaniel Ribeiro 				slots, slot_width);
30008c6529dbSLiam Girdwood 	else
30018c6529dbSLiam Girdwood 		return -EINVAL;
30028c6529dbSLiam Girdwood }
30038c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
30048c6529dbSLiam Girdwood 
30058c6529dbSLiam Girdwood /**
3006472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3007472df3cbSBarry Song  * @dai: DAI
3008472df3cbSBarry Song  * @tx_num: how many TX channels
3009472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3010472df3cbSBarry Song  *           0~num-1 uses
3011472df3cbSBarry Song  * @rx_num: how many RX channels
3012472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3013472df3cbSBarry Song  *           0~num-1 uses
3014472df3cbSBarry Song  *
3015472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3016472df3cbSBarry Song  */
3017472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3018472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3019472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3020472df3cbSBarry Song {
3021f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3022f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3023472df3cbSBarry Song 			rx_num, rx_slot);
3024472df3cbSBarry Song 	else
3025472df3cbSBarry Song 		return -EINVAL;
3026472df3cbSBarry Song }
3027472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3028472df3cbSBarry Song 
3029472df3cbSBarry Song /**
30308c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
30318c6529dbSLiam Girdwood  * @dai: DAI
30328c6529dbSLiam Girdwood  * @tristate: tristate enable
30338c6529dbSLiam Girdwood  *
30348c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
30358c6529dbSLiam Girdwood  */
30368c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
30378c6529dbSLiam Girdwood {
3038f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3039f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
30408c6529dbSLiam Girdwood 	else
30418c6529dbSLiam Girdwood 		return -EINVAL;
30428c6529dbSLiam Girdwood }
30438c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
30448c6529dbSLiam Girdwood 
30458c6529dbSLiam Girdwood /**
30468c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
30478c6529dbSLiam Girdwood  * @dai: DAI
30488c6529dbSLiam Girdwood  * @mute: mute enable
30498c6529dbSLiam Girdwood  *
30508c6529dbSLiam Girdwood  * Mutes the DAI DAC.
30518c6529dbSLiam Girdwood  */
30528c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
30538c6529dbSLiam Girdwood {
3054f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3055f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
30568c6529dbSLiam Girdwood 	else
30578c6529dbSLiam Girdwood 		return -EINVAL;
30588c6529dbSLiam Girdwood }
30598c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
30608c6529dbSLiam Girdwood 
3061c5af3a2eSMark Brown /**
3062c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3063c5af3a2eSMark Brown  *
3064ac11a2b3SMark Brown  * @card: Card to register
3065c5af3a2eSMark Brown  *
3066c5af3a2eSMark Brown  */
306770a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3068c5af3a2eSMark Brown {
3069f0fba2adSLiam Girdwood 	int i;
3070f0fba2adSLiam Girdwood 
3071c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3072c5af3a2eSMark Brown 		return -EINVAL;
3073c5af3a2eSMark Brown 
30745a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
30755a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
30765a504963SStephen Warren 
30775a504963SStephen Warren 		/*
30785a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
30795a504963SStephen Warren 		 * not both or neither.
30805a504963SStephen Warren 		 */
30815a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
30825a504963SStephen Warren 			dev_err(card->dev,
30833b09bb82SLiam Girdwood 				"Neither/both codec name/of_node are set for %s\n",
30843b09bb82SLiam Girdwood 				link->name);
30855a504963SStephen Warren 			return -EINVAL;
30865a504963SStephen Warren 		}
30875a504963SStephen Warren 
30885a504963SStephen Warren 		/*
30895a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
30905a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
30915a504963SStephen Warren 		 */
30925a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
30935a504963SStephen Warren 			dev_err(card->dev,
30943b09bb82SLiam Girdwood 				"Both platform name/of_node are set for %s\n", link->name);
30955a504963SStephen Warren 			return -EINVAL;
30965a504963SStephen Warren 		}
30975a504963SStephen Warren 
30985a504963SStephen Warren 		/*
30995a504963SStephen Warren 		 * CPU DAI must be specified by 1 of name or OF node,
31005a504963SStephen Warren 		 * not both or neither.
31015a504963SStephen Warren 		 */
31025a504963SStephen Warren 		if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
31035a504963SStephen Warren 			dev_err(card->dev,
31043b09bb82SLiam Girdwood 				"Neither/both cpu_dai name/of_node are set for %s\n",
31053b09bb82SLiam Girdwood 				link->name);
31065a504963SStephen Warren 			return -EINVAL;
31075a504963SStephen Warren 		}
31085a504963SStephen Warren 	}
31095a504963SStephen Warren 
3110ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3111ed77cc12SMark Brown 
3112111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3113111c6419SStephen Warren 
3114150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3115150dd2f8SVinod Koul 
3116181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3117181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
31182eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3119f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3120f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3121f0fba2adSLiam Girdwood 		return -ENOMEM;
3122*a7dbb603SLiam Girdwood 	card->num_rtd = 0;
31232eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3124f0fba2adSLiam Girdwood 
3125f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3126f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3127f0fba2adSLiam Girdwood 
3128c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3129db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3130c5af3a2eSMark Brown 	card->instantiated = 0;
3131f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3132c5af3a2eSMark Brown 
3133c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3134c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
3135435c5e25SMark Brown 	snd_soc_instantiate_cards();
3136c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3137c5af3a2eSMark Brown 
3138c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3139c5af3a2eSMark Brown 
3140c5af3a2eSMark Brown 	return 0;
3141c5af3a2eSMark Brown }
314270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3143c5af3a2eSMark Brown 
3144c5af3a2eSMark Brown /**
3145c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3146c5af3a2eSMark Brown  *
3147ac11a2b3SMark Brown  * @card: Card to unregister
3148c5af3a2eSMark Brown  *
3149c5af3a2eSMark Brown  */
315070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3151c5af3a2eSMark Brown {
3152b0e26485SVinod Koul 	if (card->instantiated)
3153b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3154c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
3155c5af3a2eSMark Brown 	list_del(&card->list);
3156c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
3157c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3158c5af3a2eSMark Brown 
3159c5af3a2eSMark Brown 	return 0;
3160c5af3a2eSMark Brown }
316170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3162c5af3a2eSMark Brown 
3163f0fba2adSLiam Girdwood /*
3164f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3165f0fba2adSLiam Girdwood  * and sanitizing names.
3166f0fba2adSLiam Girdwood  */
31670b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3168f0fba2adSLiam Girdwood {
3169f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3170f0fba2adSLiam Girdwood 	int id1, id2;
3171f0fba2adSLiam Girdwood 
3172f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3173f0fba2adSLiam Girdwood 		return NULL;
3174f0fba2adSLiam Girdwood 
317558818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3176f0fba2adSLiam Girdwood 
3177f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3178f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3179f0fba2adSLiam Girdwood 	if (found) {
3180f0fba2adSLiam Girdwood 		/* get ID */
3181f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3182f0fba2adSLiam Girdwood 
3183f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3184f0fba2adSLiam Girdwood 			if (*id == -1)
3185f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3186f0fba2adSLiam Girdwood 		}
3187f0fba2adSLiam Girdwood 
3188f0fba2adSLiam Girdwood 	} else {
3189f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3190f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3191f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3192f0fba2adSLiam Girdwood 
3193f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
319405899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3195f0fba2adSLiam Girdwood 
3196f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3197f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
319858818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3199f0fba2adSLiam Girdwood 		} else
3200f0fba2adSLiam Girdwood 			*id = 0;
3201f0fba2adSLiam Girdwood 	}
3202f0fba2adSLiam Girdwood 
3203f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3204f0fba2adSLiam Girdwood }
3205f0fba2adSLiam Girdwood 
3206f0fba2adSLiam Girdwood /*
3207f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3208f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3209f0fba2adSLiam Girdwood  */
3210f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3211f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3212f0fba2adSLiam Girdwood {
3213f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
32140837fc62SFabio Estevam 		pr_err("asoc: error - multiple DAI %s registered with no name\n",
3215f0fba2adSLiam Girdwood 				dev_name(dev));
3216f0fba2adSLiam Girdwood 		return NULL;
3217f0fba2adSLiam Girdwood 	}
3218f0fba2adSLiam Girdwood 
3219f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3220f0fba2adSLiam Girdwood }
3221f0fba2adSLiam Girdwood 
32229115171aSMark Brown /**
32239115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
32249115171aSMark Brown  *
3225ac11a2b3SMark Brown  * @dai: DAI to register
32269115171aSMark Brown  */
3227f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3228f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
32299115171aSMark Brown {
3230f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
32319115171aSMark Brown 
3232f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
32339115171aSMark Brown 
3234f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3235f0fba2adSLiam Girdwood 	if (dai == NULL)
3236f0fba2adSLiam Girdwood 		return -ENOMEM;
32376335d055SEric Miao 
3238f0fba2adSLiam Girdwood 	/* create DAI component name */
3239f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3240f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3241f0fba2adSLiam Girdwood 		kfree(dai);
3242f0fba2adSLiam Girdwood 		return -ENOMEM;
3243f0fba2adSLiam Girdwood 	}
3244f0fba2adSLiam Girdwood 
3245f0fba2adSLiam Girdwood 	dai->dev = dev;
3246f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3247f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3248f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
32499115171aSMark Brown 
32509115171aSMark Brown 	mutex_lock(&client_mutex);
32519115171aSMark Brown 	list_add(&dai->list, &dai_list);
3252435c5e25SMark Brown 	snd_soc_instantiate_cards();
32539115171aSMark Brown 	mutex_unlock(&client_mutex);
32549115171aSMark Brown 
32559115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
32569115171aSMark Brown 
32579115171aSMark Brown 	return 0;
32589115171aSMark Brown }
32599115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
32609115171aSMark Brown 
32619115171aSMark Brown /**
32629115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
32639115171aSMark Brown  *
3264ac11a2b3SMark Brown  * @dai: DAI to unregister
32659115171aSMark Brown  */
3266f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
32679115171aSMark Brown {
3268f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3269f0fba2adSLiam Girdwood 
3270f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3271f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3272f0fba2adSLiam Girdwood 			goto found;
3273f0fba2adSLiam Girdwood 	}
3274f0fba2adSLiam Girdwood 	return;
3275f0fba2adSLiam Girdwood 
3276f0fba2adSLiam Girdwood found:
32779115171aSMark Brown 	mutex_lock(&client_mutex);
32789115171aSMark Brown 	list_del(&dai->list);
32799115171aSMark Brown 	mutex_unlock(&client_mutex);
32809115171aSMark Brown 
32819115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3282f0fba2adSLiam Girdwood 	kfree(dai->name);
3283f0fba2adSLiam Girdwood 	kfree(dai);
32849115171aSMark Brown }
32859115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
32869115171aSMark Brown 
32879115171aSMark Brown /**
32889115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
32899115171aSMark Brown  *
3290ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3291ac11a2b3SMark Brown  * @count: Number of DAIs
32929115171aSMark Brown  */
3293f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3294f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
32959115171aSMark Brown {
3296f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3297f0fba2adSLiam Girdwood 	int i, ret = 0;
3298f0fba2adSLiam Girdwood 
3299720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
33009115171aSMark Brown 
33019115171aSMark Brown 	for (i = 0; i < count; i++) {
3302f0fba2adSLiam Girdwood 
3303f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3304c46e0079SAxel Lin 		if (dai == NULL) {
3305c46e0079SAxel Lin 			ret = -ENOMEM;
3306c46e0079SAxel Lin 			goto err;
3307c46e0079SAxel Lin 		}
3308f0fba2adSLiam Girdwood 
3309f0fba2adSLiam Girdwood 		/* create DAI component name */
3310f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3311f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3312f0fba2adSLiam Girdwood 			kfree(dai);
3313f0fba2adSLiam Girdwood 			ret = -EINVAL;
33149115171aSMark Brown 			goto err;
33159115171aSMark Brown 		}
33169115171aSMark Brown 
3317f0fba2adSLiam Girdwood 		dai->dev = dev;
3318f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
33190f9141c9SMark Brown 		if (dai->driver->id)
33200f9141c9SMark Brown 			dai->id = dai->driver->id;
33210f9141c9SMark Brown 		else
33220f9141c9SMark Brown 			dai->id = i;
3323f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3324f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3325f0fba2adSLiam Girdwood 
3326f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3327f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3328f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3329f0fba2adSLiam Girdwood 
3330f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3331f0fba2adSLiam Girdwood 	}
3332f0fba2adSLiam Girdwood 
33331dcb4f38SAxel Lin 	mutex_lock(&client_mutex);
3334f0fba2adSLiam Girdwood 	snd_soc_instantiate_cards();
33351dcb4f38SAxel Lin 	mutex_unlock(&client_mutex);
33369115171aSMark Brown 	return 0;
33379115171aSMark Brown 
33389115171aSMark Brown err:
33399115171aSMark Brown 	for (i--; i >= 0; i--)
3340f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33419115171aSMark Brown 
33429115171aSMark Brown 	return ret;
33439115171aSMark Brown }
33449115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
33459115171aSMark Brown 
33469115171aSMark Brown /**
33479115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
33489115171aSMark Brown  *
3349ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3350ac11a2b3SMark Brown  * @count: Number of DAIs
33519115171aSMark Brown  */
3352f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
33539115171aSMark Brown {
33549115171aSMark Brown 	int i;
33559115171aSMark Brown 
33569115171aSMark Brown 	for (i = 0; i < count; i++)
3357f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33589115171aSMark Brown }
33599115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
33609115171aSMark Brown 
336112a48a8cSMark Brown /**
336212a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
336312a48a8cSMark Brown  *
3364ac11a2b3SMark Brown  * @platform: platform to register
336512a48a8cSMark Brown  */
3366f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3367f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
336812a48a8cSMark Brown {
3369f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
337012a48a8cSMark Brown 
3371f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3372f0fba2adSLiam Girdwood 
3373f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3374f0fba2adSLiam Girdwood 	if (platform == NULL)
3375f0fba2adSLiam Girdwood 		return -ENOMEM;
3376f0fba2adSLiam Girdwood 
3377f0fba2adSLiam Girdwood 	/* create platform component name */
3378f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3379f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3380f0fba2adSLiam Girdwood 		kfree(platform);
3381f0fba2adSLiam Girdwood 		return -ENOMEM;
3382f0fba2adSLiam Girdwood 	}
3383f0fba2adSLiam Girdwood 
3384f0fba2adSLiam Girdwood 	platform->dev = dev;
3385f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3386b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3387b7950641SLiam Girdwood 	platform->dapm.platform = platform;
338864a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3389cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
339012a48a8cSMark Brown 
339112a48a8cSMark Brown 	mutex_lock(&client_mutex);
339212a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
3393435c5e25SMark Brown 	snd_soc_instantiate_cards();
339412a48a8cSMark Brown 	mutex_unlock(&client_mutex);
339512a48a8cSMark Brown 
339612a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
339712a48a8cSMark Brown 
339812a48a8cSMark Brown 	return 0;
339912a48a8cSMark Brown }
340012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
340112a48a8cSMark Brown 
340212a48a8cSMark Brown /**
340312a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
340412a48a8cSMark Brown  *
3405ac11a2b3SMark Brown  * @platform: platform to unregister
340612a48a8cSMark Brown  */
3407f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
340812a48a8cSMark Brown {
3409f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3410f0fba2adSLiam Girdwood 
3411f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3412f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3413f0fba2adSLiam Girdwood 			goto found;
3414f0fba2adSLiam Girdwood 	}
3415f0fba2adSLiam Girdwood 	return;
3416f0fba2adSLiam Girdwood 
3417f0fba2adSLiam Girdwood found:
341812a48a8cSMark Brown 	mutex_lock(&client_mutex);
341912a48a8cSMark Brown 	list_del(&platform->list);
342012a48a8cSMark Brown 	mutex_unlock(&client_mutex);
342112a48a8cSMark Brown 
342212a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3423f0fba2adSLiam Girdwood 	kfree(platform->name);
3424f0fba2adSLiam Girdwood 	kfree(platform);
342512a48a8cSMark Brown }
342612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
342712a48a8cSMark Brown 
3428151ab22cSMark Brown static u64 codec_format_map[] = {
3429151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3430151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3431151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3432151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3433151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3434151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3435151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3436151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3437151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3438151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3439151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3440151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3441151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3442151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3443151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3444151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3445151ab22cSMark Brown };
3446151ab22cSMark Brown 
3447151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3448151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3449151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3450151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3451151ab22cSMark Brown  */
3452151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3453151ab22cSMark Brown {
3454151ab22cSMark Brown 	int i;
3455151ab22cSMark Brown 
3456151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3457151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3458151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3459151ab22cSMark Brown }
3460151ab22cSMark Brown 
34610d0cf00aSMark Brown /**
34620d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
34630d0cf00aSMark Brown  *
3464ac11a2b3SMark Brown  * @codec: codec to register
34650d0cf00aSMark Brown  */
3466f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3467001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3468001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3469001ae4c0SMark Brown 			   int num_dai)
34700d0cf00aSMark Brown {
34713335ddcaSDimitris Papastamos 	size_t reg_size;
3472f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3473f0fba2adSLiam Girdwood 	int ret, i;
3474151ab22cSMark Brown 
3475f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
34760d0cf00aSMark Brown 
3477f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3478f0fba2adSLiam Girdwood 	if (codec == NULL)
3479f0fba2adSLiam Girdwood 		return -ENOMEM;
34800d0cf00aSMark Brown 
3481f0fba2adSLiam Girdwood 	/* create CODEC component name */
3482f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3483f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3484f0fba2adSLiam Girdwood 		kfree(codec);
3485f0fba2adSLiam Girdwood 		return -ENOMEM;
3486151ab22cSMark Brown 	}
3487151ab22cSMark Brown 
348823bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
348923bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
349023bbce34SDimitris Papastamos 	else
349123bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
349223bbce34SDimitris Papastamos 
3493c3acec26SMark Brown 	codec->write = codec_drv->write;
3494c3acec26SMark Brown 	codec->read = codec_drv->read;
34951500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
34961500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
34978020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
34985124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3499ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3500ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3501ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3502474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
350364a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3504f0fba2adSLiam Girdwood 	codec->dev = dev;
3505f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3506f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3507f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3508f0fba2adSLiam Girdwood 
35097a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
35107a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
35113335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3512aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
35133335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
35143335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
35153335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
35163335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
35173335ddcaSDimitris Papastamos 		 * the cache.
35183335ddcaSDimitris Papastamos 		 */
35192aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
35203335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
35213335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
35223335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
35233335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3524fdf0f54dSDimitris Papastamos 				goto fail;
35257a30a3dbSDimitris Papastamos 			}
35267a30a3dbSDimitris Papastamos 		}
35272aa86323SDimitris Papastamos 	}
35287a30a3dbSDimitris Papastamos 
35291500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
35301500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
35311500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
35321500b7b5SDimitris Papastamos 		if (!codec->readable_register)
35331500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
35348020454cSDimitris Papastamos 		if (!codec->writable_register)
35358020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
35361500b7b5SDimitris Papastamos 	}
35371500b7b5SDimitris Papastamos 
3538f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3539f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3540f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3541f0fba2adSLiam Girdwood 	}
3542f0fba2adSLiam Girdwood 
354326b01ccdSMark Brown 	/* register any DAIs */
354426b01ccdSMark Brown 	if (num_dai) {
3545f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3546f0fba2adSLiam Girdwood 		if (ret < 0)
3547fdf0f54dSDimitris Papastamos 			goto fail;
354826b01ccdSMark Brown 	}
3549f0fba2adSLiam Girdwood 
35500d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35510d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
35520d0cf00aSMark Brown 	snd_soc_instantiate_cards();
35530d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35540d0cf00aSMark Brown 
35550d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
35560d0cf00aSMark Brown 	return 0;
3557f0fba2adSLiam Girdwood 
3558fdf0f54dSDimitris Papastamos fail:
35593335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35603335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3561f0fba2adSLiam Girdwood 	kfree(codec->name);
3562f0fba2adSLiam Girdwood 	kfree(codec);
3563f0fba2adSLiam Girdwood 	return ret;
35640d0cf00aSMark Brown }
35650d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
35660d0cf00aSMark Brown 
35670d0cf00aSMark Brown /**
35680d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
35690d0cf00aSMark Brown  *
3570ac11a2b3SMark Brown  * @codec: codec to unregister
35710d0cf00aSMark Brown  */
3572f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
35730d0cf00aSMark Brown {
3574f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3575f0fba2adSLiam Girdwood 	int i;
3576f0fba2adSLiam Girdwood 
3577f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3578f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3579f0fba2adSLiam Girdwood 			goto found;
3580f0fba2adSLiam Girdwood 	}
3581f0fba2adSLiam Girdwood 	return;
3582f0fba2adSLiam Girdwood 
3583f0fba2adSLiam Girdwood found:
358426b01ccdSMark Brown 	if (codec->num_dai)
3585f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3586f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3587f0fba2adSLiam Girdwood 
35880d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35890d0cf00aSMark Brown 	list_del(&codec->list);
35900d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35910d0cf00aSMark Brown 
35920d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3593f0fba2adSLiam Girdwood 
35947a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
35953335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35961aafcd4dSDimitris Papastamos 	kfree(codec->name);
3597f0fba2adSLiam Girdwood 	kfree(codec);
35980d0cf00aSMark Brown }
35990d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
36000d0cf00aSMark Brown 
3601bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3602bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3603bec4fa05SStephen Warren 			       const char *propname)
3604bec4fa05SStephen Warren {
3605bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
3606bec4fa05SStephen Warren 	int ret;
3607bec4fa05SStephen Warren 
3608bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3609bec4fa05SStephen Warren 	/*
3610bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3611bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3612bec4fa05SStephen Warren 	 * snd_soc_register_card.
3613bec4fa05SStephen Warren 	 */
3614bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3615bec4fa05SStephen Warren 		dev_err(card->dev,
3616bec4fa05SStephen Warren 			"Property '%s' could not be read: %d\n",
3617bec4fa05SStephen Warren 			propname, ret);
3618bec4fa05SStephen Warren 		return ret;
3619bec4fa05SStephen Warren 	}
3620bec4fa05SStephen Warren 
3621bec4fa05SStephen Warren 	return 0;
3622bec4fa05SStephen Warren }
3623bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3624bec4fa05SStephen Warren 
3625a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3626a4a54dd5SStephen Warren 				   const char *propname)
3627a4a54dd5SStephen Warren {
3628a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3629a4a54dd5SStephen Warren 	int num_routes;
3630a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3631a4a54dd5SStephen Warren 	int i, ret;
3632a4a54dd5SStephen Warren 
3633a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3634a4a54dd5SStephen Warren 	if (num_routes & 1) {
3635a4a54dd5SStephen Warren 		dev_err(card->dev,
3636a4a54dd5SStephen Warren 			"Property '%s's length is not even\n",
3637a4a54dd5SStephen Warren 			propname);
3638a4a54dd5SStephen Warren 		return -EINVAL;
3639a4a54dd5SStephen Warren 	}
3640a4a54dd5SStephen Warren 	num_routes /= 2;
3641a4a54dd5SStephen Warren 	if (!num_routes) {
3642a4a54dd5SStephen Warren 		dev_err(card->dev,
3643a4a54dd5SStephen Warren 			"Property '%s's length is zero\n",
3644a4a54dd5SStephen Warren 			propname);
3645a4a54dd5SStephen Warren 		return -EINVAL;
3646a4a54dd5SStephen Warren 	}
3647a4a54dd5SStephen Warren 
3648a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3649a4a54dd5SStephen Warren 			      GFP_KERNEL);
3650a4a54dd5SStephen Warren 	if (!routes) {
3651a4a54dd5SStephen Warren 		dev_err(card->dev,
3652a4a54dd5SStephen Warren 			"Could not allocate DAPM route table\n");
3653a4a54dd5SStephen Warren 		return -EINVAL;
3654a4a54dd5SStephen Warren 	}
3655a4a54dd5SStephen Warren 
3656a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3657a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3658a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
3659a4a54dd5SStephen Warren 		if (ret) {
3660a4a54dd5SStephen Warren 			dev_err(card->dev,
3661a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3662a4a54dd5SStephen Warren 				propname, 2 * i, ret);
3663a4a54dd5SStephen Warren 			return -EINVAL;
3664a4a54dd5SStephen Warren 		}
3665a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3666a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
3667a4a54dd5SStephen Warren 		if (ret) {
3668a4a54dd5SStephen Warren 			dev_err(card->dev,
3669a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3670a4a54dd5SStephen Warren 				propname, (2 * i) + 1, ret);
3671a4a54dd5SStephen Warren 			return -EINVAL;
3672a4a54dd5SStephen Warren 		}
3673a4a54dd5SStephen Warren 	}
3674a4a54dd5SStephen Warren 
3675a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
3676a4a54dd5SStephen Warren 	card->dapm_routes = routes;
3677a4a54dd5SStephen Warren 
3678a4a54dd5SStephen Warren 	return 0;
3679a4a54dd5SStephen Warren }
3680a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3681a4a54dd5SStephen Warren 
3682c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3683db2a4165SFrank Mandarino {
3684384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
36858a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
36868a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
36870837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
36888a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3689384c89e2SMark Brown 	}
3690c3c5a19aSMark Brown 
36918a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3692c3c5a19aSMark Brown 				 &codec_list_fops))
3693c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3694c3c5a19aSMark Brown 
36958a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3696f3208780SMark Brown 				 &dai_list_fops))
3697f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
369819c7ac27SMark Brown 
36998a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
370019c7ac27SMark Brown 				 &platform_list_fops))
370119c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3702384c89e2SMark Brown #endif
3703384c89e2SMark Brown 
3704fb257897SMark Brown 	snd_soc_util_init();
3705fb257897SMark Brown 
3706db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3707db2a4165SFrank Mandarino }
37084abe8e16SMark Brown module_init(snd_soc_init);
3709db2a4165SFrank Mandarino 
37107d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3711db2a4165SFrank Mandarino {
3712fb257897SMark Brown 	snd_soc_util_exit();
3713fb257897SMark Brown 
3714384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
37158a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3716384c89e2SMark Brown #endif
3717db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3718db2a4165SFrank Mandarino }
3719db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3720db2a4165SFrank Mandarino 
3721db2a4165SFrank Mandarino /* Module information */
3722d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3723db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3724db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
37258b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3726