xref: /openbmc/linux/sound/soc/soc-core.c (revision 27f1d75921e7273e484cc2fab132d9fcbe9f845d)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6f0fba2adSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7f0fba2adSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
80664d888SLiam Girdwood  *
9d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
100664d888SLiam Girdwood  *         with code, comments and ideas from :-
110664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
14db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
15db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
16db2a4165SFrank Mandarino  *  option) any later version.
17db2a4165SFrank Mandarino  *
18db2a4165SFrank Mandarino  *  TODO:
19db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
20db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
21db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
22db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
23db2a4165SFrank Mandarino  */
24db2a4165SFrank Mandarino 
25db2a4165SFrank Mandarino #include <linux/module.h>
26db2a4165SFrank Mandarino #include <linux/moduleparam.h>
27db2a4165SFrank Mandarino #include <linux/init.h>
28db2a4165SFrank Mandarino #include <linux/delay.h>
29db2a4165SFrank Mandarino #include <linux/pm.h>
30db2a4165SFrank Mandarino #include <linux/bitops.h>
3112ef193dSTroy Kisky #include <linux/debugfs.h>
32db2a4165SFrank Mandarino #include <linux/platform_device.h>
33f0e8ed85SMark Brown #include <linux/ctype.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
35bec4fa05SStephen Warren #include <linux/of.h>
36474828a4SMarek Vasut #include <sound/ac97_codec.h>
37db2a4165SFrank Mandarino #include <sound/core.h>
383028eb8cSMark Brown #include <sound/jack.h>
39db2a4165SFrank Mandarino #include <sound/pcm.h>
40db2a4165SFrank Mandarino #include <sound/pcm_params.h>
41db2a4165SFrank Mandarino #include <sound/soc.h>
42db2a4165SFrank Mandarino #include <sound/initval.h>
43db2a4165SFrank Mandarino 
44a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
45a8b1d34fSMark Brown #include <trace/events/asoc.h>
46a8b1d34fSMark Brown 
47f0fba2adSLiam Girdwood #define NAME_SIZE	32
48f0fba2adSLiam Girdwood 
49db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50db2a4165SFrank Mandarino 
51384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
528a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
538a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
54384c89e2SMark Brown #endif
55384c89e2SMark Brown 
56c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
579115171aSMark Brown static LIST_HEAD(dai_list);
5812a48a8cSMark Brown static LIST_HEAD(platform_list);
590d0cf00aSMark Brown static LIST_HEAD(codec_list);
60c5af3a2eSMark Brown 
61db2a4165SFrank Mandarino /*
62db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
63db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
64db2a4165SFrank Mandarino  * between two audio tracks.
65db2a4165SFrank Mandarino  */
66db2a4165SFrank Mandarino static int pmdown_time = 5000;
67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69db2a4165SFrank Mandarino 
702bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
712bc9a81eSDimitris Papastamos  * a particular given value */
722bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
732bc9a81eSDimitris Papastamos {
742bc9a81eSDimitris Papastamos 	int c = 0;
752bc9a81eSDimitris Papastamos 	int i;
762bc9a81eSDimitris Papastamos 
772bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
782bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
792bc9a81eSDimitris Papastamos 			break;
802bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
812bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
822bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
832bc9a81eSDimitris Papastamos 	else
842bc9a81eSDimitris Papastamos 		c /= 8;
852bc9a81eSDimitris Papastamos 	return c;
862bc9a81eSDimitris Papastamos }
872bc9a81eSDimitris Papastamos 
8813fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
8913fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
9013fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
9113fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
922624d5faSMark Brown {
9300b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
9400b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
9513fd179fSDimitris Papastamos 	int ret;
9613fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
9713fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
9813fd179fSDimitris Papastamos 
9913fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
10013fd179fSDimitris Papastamos 	 * try to abuse this function */
10113fd179fSDimitris Papastamos 	WARN_ON(len > 63);
10213fd179fSDimitris Papastamos 
10313fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
10413fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
10513fd179fSDimitris Papastamos 		return -EINVAL;
10613fd179fSDimitris Papastamos 
10713fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
10813fd179fSDimitris Papastamos 	if (ret < 0) {
10913fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
11013fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
11113fd179fSDimitris Papastamos 	} else {
11213fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
11313fd179fSDimitris Papastamos 	}
11413fd179fSDimitris Papastamos 
11513fd179fSDimitris Papastamos 	/* prepare the buffer */
11613fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
11713fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
11813fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
11913fd179fSDimitris Papastamos 
12013fd179fSDimitris Papastamos 	return 0;
12113fd179fSDimitris Papastamos }
12213fd179fSDimitris Papastamos 
12313fd179fSDimitris Papastamos /* codec register dump */
12413fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
12513fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
12613fd179fSDimitris Papastamos {
12713fd179fSDimitris Papastamos 	int i, step = 1;
1282bc9a81eSDimitris Papastamos 	int wordsize, regsize;
12913fd179fSDimitris Papastamos 	int len;
13013fd179fSDimitris Papastamos 	size_t total = 0;
13113fd179fSDimitris Papastamos 	loff_t p = 0;
1322bc9a81eSDimitris Papastamos 
13300b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
13400b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1352624d5faSMark Brown 
13613fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
13713fd179fSDimitris Papastamos 
138f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1392624d5faSMark Brown 		return 0;
1402624d5faSMark Brown 
141f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
142f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1432624d5faSMark Brown 
144f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
145b92d150bSLars-Peter Clausen 		if (!snd_soc_codec_readable_register(codec, i))
1462624d5faSMark Brown 			continue;
147f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
148f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1492624d5faSMark Brown 							 PAGE_SIZE - count, i);
1505164d74dSMark Brown 		} else {
15113fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
15213fd179fSDimitris Papastamos 			 * entries for the default case */
15313fd179fSDimitris Papastamos 			if (p >= pos) {
15413fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1552624d5faSMark Brown 					break;
15613fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
15713fd179fSDimitris Papastamos 				total += len;
15813fd179fSDimitris Papastamos 			}
15913fd179fSDimitris Papastamos 			p += len;
16013fd179fSDimitris Papastamos 		}
1612624d5faSMark Brown 	}
1622624d5faSMark Brown 
16313fd179fSDimitris Papastamos 	total = min(total, count - 1);
1642624d5faSMark Brown 
16513fd179fSDimitris Papastamos 	return total;
1662624d5faSMark Brown }
16713fd179fSDimitris Papastamos 
1682624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1692624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1702624d5faSMark Brown {
17136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
172f0fba2adSLiam Girdwood 
17313fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1742624d5faSMark Brown }
1752624d5faSMark Brown 
1762624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1772624d5faSMark Brown 
178dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
179dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
180dbe21408SMark Brown {
18136ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
182dbe21408SMark Brown 
183f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
184dbe21408SMark Brown }
185dbe21408SMark Brown 
186dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
187dbe21408SMark Brown 			       struct device_attribute *attr,
188dbe21408SMark Brown 			       const char *buf, size_t count)
189dbe21408SMark Brown {
19036ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
191c593b520SMark Brown 	int ret;
192dbe21408SMark Brown 
193c593b520SMark Brown 	ret = strict_strtol(buf, 10, &rtd->pmdown_time);
194c593b520SMark Brown 	if (ret)
195c593b520SMark Brown 		return ret;
196dbe21408SMark Brown 
197dbe21408SMark Brown 	return count;
198dbe21408SMark Brown }
199dbe21408SMark Brown 
200dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
201dbe21408SMark Brown 
2022624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2032624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
2042624d5faSMark Brown {
2052624d5faSMark Brown 	file->private_data = inode->i_private;
2062624d5faSMark Brown 	return 0;
2072624d5faSMark Brown }
2082624d5faSMark Brown 
2092624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2102624d5faSMark Brown 				   size_t count, loff_t *ppos)
2112624d5faSMark Brown {
2122624d5faSMark Brown 	ssize_t ret;
2132624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
21413fd179fSDimitris Papastamos 	char *buf;
21513fd179fSDimitris Papastamos 
21613fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
21713fd179fSDimitris Papastamos 		return -EINVAL;
21813fd179fSDimitris Papastamos 
21913fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2202624d5faSMark Brown 	if (!buf)
2212624d5faSMark Brown 		return -ENOMEM;
22213fd179fSDimitris Papastamos 
22313fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
22413fd179fSDimitris Papastamos 	if (ret >= 0) {
22513fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
22613fd179fSDimitris Papastamos 			kfree(buf);
22713fd179fSDimitris Papastamos 			return -EFAULT;
22813fd179fSDimitris Papastamos 		}
22913fd179fSDimitris Papastamos 		*ppos += ret;
23013fd179fSDimitris Papastamos 	}
23113fd179fSDimitris Papastamos 
2322624d5faSMark Brown 	kfree(buf);
2332624d5faSMark Brown 	return ret;
2342624d5faSMark Brown }
2352624d5faSMark Brown 
2362624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2372624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2382624d5faSMark Brown {
2392624d5faSMark Brown 	char buf[32];
24034e268d8SStephen Boyd 	size_t buf_size;
2412624d5faSMark Brown 	char *start = buf;
2422624d5faSMark Brown 	unsigned long reg, value;
2432624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2442624d5faSMark Brown 
2452624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2462624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2472624d5faSMark Brown 		return -EFAULT;
2482624d5faSMark Brown 	buf[buf_size] = 0;
2492624d5faSMark Brown 
2502624d5faSMark Brown 	while (*start == ' ')
2512624d5faSMark Brown 		start++;
2522624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2532624d5faSMark Brown 	while (*start == ' ')
2542624d5faSMark Brown 		start++;
2552624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2562624d5faSMark Brown 		return -EINVAL;
2570d51a9cbSMark Brown 
2580d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
2590d51a9cbSMark Brown 	add_taint(TAINT_USER);
2600d51a9cbSMark Brown 
261e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2622624d5faSMark Brown 	return buf_size;
2632624d5faSMark Brown }
2642624d5faSMark Brown 
2652624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2662624d5faSMark Brown 	.open = codec_reg_open_file,
2672624d5faSMark Brown 	.read = codec_reg_read_file,
2682624d5faSMark Brown 	.write = codec_reg_write_file,
2696038f373SArnd Bergmann 	.llseek = default_llseek,
2702624d5faSMark Brown };
2712624d5faSMark Brown 
2722624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2732624d5faSMark Brown {
274d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
275d6ce4cf3SJarkko Nikula 
2766ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
277d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2782624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
27964e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
2802624d5faSMark Brown 		return;
2812624d5faSMark Brown 	}
2822624d5faSMark Brown 
283aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
284aaee8ef1SMark Brown 			    &codec->cache_sync);
285aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
286aaee8ef1SMark Brown 			    &codec->cache_only);
287aaee8ef1SMark Brown 
2882624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2892624d5faSMark Brown 						 codec->debugfs_codec_root,
2902624d5faSMark Brown 						 codec, &codec_reg_fops);
2912624d5faSMark Brown 	if (!codec->debugfs_reg)
29264e60f9fSLiam Girdwood 		dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
2932624d5faSMark Brown 
2948eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
2952624d5faSMark Brown }
2962624d5faSMark Brown 
2972624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2982624d5faSMark Brown {
2992624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3002624d5faSMark Brown }
3012624d5faSMark Brown 
302731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
303731f1ab2SSebastien Guiriec {
304731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
305731f1ab2SSebastien Guiriec 
306731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
307731f1ab2SSebastien Guiriec 						       debugfs_card_root);
308731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
309731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
310731f1ab2SSebastien Guiriec 			"Failed to create platform debugfs directory\n");
311731f1ab2SSebastien Guiriec 		return;
312731f1ab2SSebastien Guiriec 	}
313731f1ab2SSebastien Guiriec 
314731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
315731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
316731f1ab2SSebastien Guiriec }
317731f1ab2SSebastien Guiriec 
318731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
319731f1ab2SSebastien Guiriec {
320731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
321731f1ab2SSebastien Guiriec }
322731f1ab2SSebastien Guiriec 
323c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
325c3c5a19aSMark Brown {
326c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3272b194f9dSMark Brown 	ssize_t len, ret = 0;
328c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
329c3c5a19aSMark Brown 
330c3c5a19aSMark Brown 	if (!buf)
331c3c5a19aSMark Brown 		return -ENOMEM;
332c3c5a19aSMark Brown 
3332b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3342b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
335c3c5a19aSMark Brown 			       codec->name);
3362b194f9dSMark Brown 		if (len >= 0)
3372b194f9dSMark Brown 			ret += len;
3382b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3392b194f9dSMark Brown 			ret = PAGE_SIZE;
3402b194f9dSMark Brown 			break;
3412b194f9dSMark Brown 		}
3422b194f9dSMark Brown 	}
343c3c5a19aSMark Brown 
344c3c5a19aSMark Brown 	if (ret >= 0)
345c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
346c3c5a19aSMark Brown 
347c3c5a19aSMark Brown 	kfree(buf);
348c3c5a19aSMark Brown 
349c3c5a19aSMark Brown 	return ret;
350c3c5a19aSMark Brown }
351c3c5a19aSMark Brown 
352c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
353c3c5a19aSMark Brown 	.read = codec_list_read_file,
354c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
355c3c5a19aSMark Brown };
356c3c5a19aSMark Brown 
357f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358f3208780SMark Brown 				  size_t count, loff_t *ppos)
359f3208780SMark Brown {
360f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3612b194f9dSMark Brown 	ssize_t len, ret = 0;
362f3208780SMark Brown 	struct snd_soc_dai *dai;
363f3208780SMark Brown 
364f3208780SMark Brown 	if (!buf)
365f3208780SMark Brown 		return -ENOMEM;
366f3208780SMark Brown 
3672b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3682b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3692b194f9dSMark Brown 		if (len >= 0)
3702b194f9dSMark Brown 			ret += len;
3712b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3722b194f9dSMark Brown 			ret = PAGE_SIZE;
3732b194f9dSMark Brown 			break;
3742b194f9dSMark Brown 		}
3752b194f9dSMark Brown 	}
376f3208780SMark Brown 
377f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
378f3208780SMark Brown 
379f3208780SMark Brown 	kfree(buf);
380f3208780SMark Brown 
381f3208780SMark Brown 	return ret;
382f3208780SMark Brown }
383f3208780SMark Brown 
384f3208780SMark Brown static const struct file_operations dai_list_fops = {
385f3208780SMark Brown 	.read = dai_list_read_file,
386f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
387f3208780SMark Brown };
388f3208780SMark Brown 
38919c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
39019c7ac27SMark Brown 				       char __user *user_buf,
39119c7ac27SMark Brown 				       size_t count, loff_t *ppos)
39219c7ac27SMark Brown {
39319c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3942b194f9dSMark Brown 	ssize_t len, ret = 0;
39519c7ac27SMark Brown 	struct snd_soc_platform *platform;
39619c7ac27SMark Brown 
39719c7ac27SMark Brown 	if (!buf)
39819c7ac27SMark Brown 		return -ENOMEM;
39919c7ac27SMark Brown 
4002b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4012b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
40219c7ac27SMark Brown 			       platform->name);
4032b194f9dSMark Brown 		if (len >= 0)
4042b194f9dSMark Brown 			ret += len;
4052b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4062b194f9dSMark Brown 			ret = PAGE_SIZE;
4072b194f9dSMark Brown 			break;
4082b194f9dSMark Brown 		}
4092b194f9dSMark Brown 	}
41019c7ac27SMark Brown 
41119c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
41219c7ac27SMark Brown 
41319c7ac27SMark Brown 	kfree(buf);
41419c7ac27SMark Brown 
41519c7ac27SMark Brown 	return ret;
41619c7ac27SMark Brown }
41719c7ac27SMark Brown 
41819c7ac27SMark Brown static const struct file_operations platform_list_fops = {
41919c7ac27SMark Brown 	.read = platform_list_read_file,
42019c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
42119c7ac27SMark Brown };
42219c7ac27SMark Brown 
423a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
424a6052154SJarkko Nikula {
425a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4268a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4273a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
428a6052154SJarkko Nikula 		dev_warn(card->dev,
4297c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4303a45b867SJarkko Nikula 		return;
4313a45b867SJarkko Nikula 	}
4323a45b867SJarkko Nikula 
4333a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4343a45b867SJarkko Nikula 						    card->debugfs_card_root,
4353a45b867SJarkko Nikula 						    &card->pop_time);
4363a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4373a45b867SJarkko Nikula 		dev_warn(card->dev,
4383a45b867SJarkko Nikula 		       "Failed to create pop time debugfs file\n");
439a6052154SJarkko Nikula }
440a6052154SJarkko Nikula 
441a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
442a6052154SJarkko Nikula {
443a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
444a6052154SJarkko Nikula }
445a6052154SJarkko Nikula 
4462624d5faSMark Brown #else
4472624d5faSMark Brown 
4482624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4492624d5faSMark Brown {
4502624d5faSMark Brown }
4512624d5faSMark Brown 
4522624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4532624d5faSMark Brown {
4542624d5faSMark Brown }
455b95fccbcSAxel Lin 
456731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
457731f1ab2SSebastien Guiriec {
458731f1ab2SSebastien Guiriec }
459731f1ab2SSebastien Guiriec 
460731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
461731f1ab2SSebastien Guiriec {
462731f1ab2SSebastien Guiriec }
463731f1ab2SSebastien Guiriec 
464b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465b95fccbcSAxel Lin {
466b95fccbcSAxel Lin }
467b95fccbcSAxel Lin 
468b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469b95fccbcSAxel Lin {
470b95fccbcSAxel Lin }
4712624d5faSMark Brown #endif
4722624d5faSMark Brown 
473db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
474db2a4165SFrank Mandarino /* unregister ac97 codec */
475db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
476db2a4165SFrank Mandarino {
477db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
478db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
479db2a4165SFrank Mandarino 	return 0;
480db2a4165SFrank Mandarino }
481db2a4165SFrank Mandarino 
482db2a4165SFrank Mandarino /* stop no dev release warning */
483db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
484db2a4165SFrank Mandarino 
485db2a4165SFrank Mandarino /* register ac97 codec to bus */
486db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
487db2a4165SFrank Mandarino {
488db2a4165SFrank Mandarino 	int err;
489db2a4165SFrank Mandarino 
490db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
4914ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
492db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
493db2a4165SFrank Mandarino 
494bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
495f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
496db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
497db2a4165SFrank Mandarino 	if (err < 0) {
498db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
499db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
500db2a4165SFrank Mandarino 		return err;
501db2a4165SFrank Mandarino 	}
502db2a4165SFrank Mandarino 	return 0;
503db2a4165SFrank Mandarino }
504db2a4165SFrank Mandarino #endif
505db2a4165SFrank Mandarino 
5066f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
507db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5086f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
509db2a4165SFrank Mandarino {
5106f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5112eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
512db2a4165SFrank Mandarino 	int i;
513db2a4165SFrank Mandarino 
514e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
515e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
516e3509ff0SDaniel Mack 	 */
517f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
518e3509ff0SDaniel Mack 		return 0;
519e3509ff0SDaniel Mack 
5206ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5216ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5226ed25978SAndy Green 	 */
523f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
524f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
525f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5266ed25978SAndy Green 
5276ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
528f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5296ed25978SAndy Green 
530a00f90f9SMark Brown 	/* mute any active DACs */
531f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
532f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
533f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5343efab7dcSMark Brown 
535f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5363efab7dcSMark Brown 			continue;
5373efab7dcSMark Brown 
538f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
539f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
540db2a4165SFrank Mandarino 	}
541db2a4165SFrank Mandarino 
5424ccab3e7SLiam Girdwood 	/* suspend all pcms */
543f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
544f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5453efab7dcSMark Brown 			continue;
5463efab7dcSMark Brown 
547f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5483efab7dcSMark Brown 	}
5494ccab3e7SLiam Girdwood 
55087506549SMark Brown 	if (card->suspend_pre)
55170b2ac12SMark Brown 		card->suspend_pre(card);
552db2a4165SFrank Mandarino 
553f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
554f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
555f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
5563efab7dcSMark Brown 
557f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5583efab7dcSMark Brown 			continue;
5593efab7dcSMark Brown 
560f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
561f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
562f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
563f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
564f0fba2adSLiam Girdwood 			platform->suspended = 1;
565f0fba2adSLiam Girdwood 		}
566db2a4165SFrank Mandarino 	}
567db2a4165SFrank Mandarino 
568db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
569f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5705b84ba26STejun Heo 		flush_delayed_work_sync(&card->rtd[i].delayed_work);
571ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
572f0fba2adSLiam Girdwood 	}
573db2a4165SFrank Mandarino 
574f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5753efab7dcSMark Brown 
576f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5773efab7dcSMark Brown 			continue;
5783efab7dcSMark Brown 
5797bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5807bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
581db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
582f0fba2adSLiam Girdwood 
5837bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
5847bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
585db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
586db2a4165SFrank Mandarino 	}
587db2a4165SFrank Mandarino 
588f0fba2adSLiam Girdwood 	/* suspend all CODECs */
5892eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
5901547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
5911547aba9SMark Brown 		 * bias _ON and should not be suspended. */
592f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
593ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
5941547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
595125a25daSMark Brown 				/*
596125a25daSMark Brown 				 * If the CODEC is capable of idle
597125a25daSMark Brown 				 * bias off then being in STANDBY
598125a25daSMark Brown 				 * means it's doing something,
599125a25daSMark Brown 				 * otherwise fall through.
600125a25daSMark Brown 				 */
601125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
602125a25daSMark Brown 					dev_dbg(codec->dev,
603125a25daSMark Brown 						"idle_bias_off CODEC on over suspend\n");
604125a25daSMark Brown 					break;
605125a25daSMark Brown 				}
6061547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
60784b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
608f0fba2adSLiam Girdwood 				codec->suspended = 1;
6097be4ba24SMark Brown 				codec->cache_sync = 1;
6101547aba9SMark Brown 				break;
6111547aba9SMark Brown 			default:
612f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC is on over suspend\n");
6131547aba9SMark Brown 				break;
6141547aba9SMark Brown 			}
6151547aba9SMark Brown 		}
616f0fba2adSLiam Girdwood 	}
617db2a4165SFrank Mandarino 
618f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
619f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6203efab7dcSMark Brown 
621f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6223efab7dcSMark Brown 			continue;
6233efab7dcSMark Brown 
624f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
625f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
626db2a4165SFrank Mandarino 	}
627db2a4165SFrank Mandarino 
62887506549SMark Brown 	if (card->suspend_post)
62970b2ac12SMark Brown 		card->suspend_post(card);
630db2a4165SFrank Mandarino 
631db2a4165SFrank Mandarino 	return 0;
632db2a4165SFrank Mandarino }
6336f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
634db2a4165SFrank Mandarino 
6356ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6366ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6376ed25978SAndy Green  */
6386ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
639db2a4165SFrank Mandarino {
640f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
641f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6422eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
643db2a4165SFrank Mandarino 	int i;
644db2a4165SFrank Mandarino 
6456ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6466ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6476ed25978SAndy Green 	 */
6486ed25978SAndy Green 
649f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "starting resume work\n");
6506ed25978SAndy Green 
6519949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
652f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6539949788bSMark Brown 
65487506549SMark Brown 	if (card->resume_pre)
65570b2ac12SMark Brown 		card->resume_pre(card);
656db2a4165SFrank Mandarino 
657f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
658f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
659f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6603efab7dcSMark Brown 
661f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6623efab7dcSMark Brown 			continue;
6633efab7dcSMark Brown 
664f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
665f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
666db2a4165SFrank Mandarino 	}
667db2a4165SFrank Mandarino 
6682eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6691547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
6701547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
6711547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
6721547aba9SMark Brown 		 */
673f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
674ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6751547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
6761547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
677f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
678f0fba2adSLiam Girdwood 				codec->suspended = 0;
6791547aba9SMark Brown 				break;
6801547aba9SMark Brown 			default:
681f0fba2adSLiam Girdwood 				dev_dbg(codec->dev, "CODEC was on over suspend\n");
6821547aba9SMark Brown 				break;
6831547aba9SMark Brown 			}
6841547aba9SMark Brown 		}
685f0fba2adSLiam Girdwood 	}
686db2a4165SFrank Mandarino 
687f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6883efab7dcSMark Brown 
689f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6903efab7dcSMark Brown 			continue;
6913efab7dcSMark Brown 
6927bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
693d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
694db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
695f0fba2adSLiam Girdwood 
6967bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
697d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
698db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
699db2a4165SFrank Mandarino 	}
700db2a4165SFrank Mandarino 
7013ff3f64bSMark Brown 	/* unmute any active DACs */
702f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
703f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
704f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7053efab7dcSMark Brown 
706f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7073efab7dcSMark Brown 			continue;
7083efab7dcSMark Brown 
709f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
710f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
711db2a4165SFrank Mandarino 	}
712db2a4165SFrank Mandarino 
713f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
714f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
715f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7163efab7dcSMark Brown 
717f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7183efab7dcSMark Brown 			continue;
7193efab7dcSMark Brown 
720f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
721f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
722f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
723f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
724f0fba2adSLiam Girdwood 			platform->suspended = 0;
725f0fba2adSLiam Girdwood 		}
726db2a4165SFrank Mandarino 	}
727db2a4165SFrank Mandarino 
72887506549SMark Brown 	if (card->resume_post)
72970b2ac12SMark Brown 		card->resume_post(card);
730db2a4165SFrank Mandarino 
731f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "resume work completed\n");
7326ed25978SAndy Green 
7336ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
734f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7356ed25978SAndy Green }
7366ed25978SAndy Green 
7376ed25978SAndy Green /* powers up audio subsystem after a suspend */
7386f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7396ed25978SAndy Green {
7406f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
74182e14e8bSStephen Warren 	int i, ac97_control = 0;
742b9dd94a8SPeter Ujfalusi 
7435ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
7445ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
7455ff1ddf2SEric Miao 	 */
7465ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
7475ff1ddf2SEric Miao 		return 0;
7485ff1ddf2SEric Miao 
74964ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
75064ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
75164ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
75264ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
75364ab9baaSMark Brown 	 */
754f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
755f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
75682e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
75782e14e8bSStephen Warren 	}
75882e14e8bSStephen Warren 	if (ac97_control) {
759f0fba2adSLiam Girdwood 		dev_dbg(dev, "Resuming AC97 immediately\n");
76064ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
76164ab9baaSMark Brown 	} else {
762f0fba2adSLiam Girdwood 		dev_dbg(dev, "Scheduling resume work\n");
7636308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
764f0fba2adSLiam Girdwood 			dev_err(dev, "resume work item may be lost\n");
765f0fba2adSLiam Girdwood 	}
7666ed25978SAndy Green 
767db2a4165SFrank Mandarino 	return 0;
768db2a4165SFrank Mandarino }
7696f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
770db2a4165SFrank Mandarino #else
7716f8ab4acSMark Brown #define snd_soc_suspend NULL
7726f8ab4acSMark Brown #define snd_soc_resume NULL
773db2a4165SFrank Mandarino #endif
774db2a4165SFrank Mandarino 
77585e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
77602a06d30SBarry Song };
77702a06d30SBarry Song 
778f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
779db2a4165SFrank Mandarino {
780f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
781f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
782fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
783435c5e25SMark Brown 	struct snd_soc_platform *platform;
784f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
785848dd8beSMark Brown 	const char *platform_name;
786db2a4165SFrank Mandarino 
787f0fba2adSLiam Girdwood 	dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
7886308419aSMark Brown 
789b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
790f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
7915a504963SStephen Warren 		if (dai_link->cpu_dai_of_node) {
7925a504963SStephen Warren 			if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
7935a504963SStephen Warren 				continue;
7945a504963SStephen Warren 		} else {
7952610ab77SStephen Warren 			if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
7962610ab77SStephen Warren 				continue;
7975a504963SStephen Warren 		}
7982610ab77SStephen Warren 
799f0fba2adSLiam Girdwood 		rtd->cpu_dai = cpu_dai;
800f0fba2adSLiam Girdwood 	}
801b19e6e7bSMark Brown 
802b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
803f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CPU DAI %s not registered\n",
804f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
805b19e6e7bSMark Brown 		return -EPROBE_DEFER;
806c5af3a2eSMark Brown 	}
807c5af3a2eSMark Brown 
808b19e6e7bSMark Brown 	/* Find CODEC from registered CODECs */
809f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8105a504963SStephen Warren 		if (dai_link->codec_of_node) {
8115a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8125a504963SStephen Warren 				continue;
8135a504963SStephen Warren 		} else {
8142610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8152610ab77SStephen Warren 				continue;
8165a504963SStephen Warren 		}
8172610ab77SStephen Warren 
818f0fba2adSLiam Girdwood 		rtd->codec = codec;
819f0fba2adSLiam Girdwood 
8202610ab77SStephen Warren 		/*
8212610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
8222610ab77SStephen Warren 		 * this CODEC
8232610ab77SStephen Warren 		 */
824f0fba2adSLiam Girdwood 		list_for_each_entry(codec_dai, &dai_list, list) {
825f0fba2adSLiam Girdwood 			if (codec->dev == codec_dai->dev &&
8262610ab77SStephen Warren 				!strcmp(codec_dai->name,
8272610ab77SStephen Warren 					dai_link->codec_dai_name)) {
8282610ab77SStephen Warren 
829f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
830435c5e25SMark Brown 			}
831f0fba2adSLiam Girdwood 		}
832b19e6e7bSMark Brown 
833b19e6e7bSMark Brown 		if (!rtd->codec_dai) {
834f0fba2adSLiam Girdwood 			dev_dbg(card->dev, "CODEC DAI %s not registered\n",
835f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
836b19e6e7bSMark Brown 			return -EPROBE_DEFER;
837f0fba2adSLiam Girdwood 		}
838b19e6e7bSMark Brown 	}
839b19e6e7bSMark Brown 
840b19e6e7bSMark Brown 	if (!rtd->codec) {
841f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "CODEC %s not registered\n",
842f0fba2adSLiam Girdwood 			dai_link->codec_name);
843b19e6e7bSMark Brown 		return -EPROBE_DEFER;
844b19e6e7bSMark Brown 	}
845848dd8beSMark Brown 
846848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
847848dd8beSMark Brown 	platform_name = dai_link->platform_name;
8485a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
849848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
850848dd8beSMark Brown 
851b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
852f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
8535a504963SStephen Warren 		if (dai_link->platform_of_node) {
8545a504963SStephen Warren 			if (platform->dev->of_node !=
8555a504963SStephen Warren 			    dai_link->platform_of_node)
8565a504963SStephen Warren 				continue;
8575a504963SStephen Warren 		} else {
8582610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
8592610ab77SStephen Warren 				continue;
8605a504963SStephen Warren 		}
8612610ab77SStephen Warren 
862f0fba2adSLiam Girdwood 		rtd->platform = platform;
863f0fba2adSLiam Girdwood 	}
864b19e6e7bSMark Brown 	if (!rtd->platform) {
865f0fba2adSLiam Girdwood 		dev_dbg(card->dev, "platform %s not registered\n",
866f0fba2adSLiam Girdwood 			dai_link->platform_name);
867b19e6e7bSMark Brown 		return -EPROBE_DEFER;
868f0fba2adSLiam Girdwood 	}
869b19e6e7bSMark Brown 
870b19e6e7bSMark Brown 	card->num_rtd++;
871b19e6e7bSMark Brown 
872b19e6e7bSMark Brown 	return 0;
8736b05eda6SMark Brown }
8746b05eda6SMark Brown 
875589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
876589c3563SJarkko Nikula {
877589c3563SJarkko Nikula 	int err;
878589c3563SJarkko Nikula 
879589c3563SJarkko Nikula 	if (codec->driver->remove) {
880589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
881589c3563SJarkko Nikula 		if (err < 0)
882589c3563SJarkko Nikula 			dev_err(codec->dev,
883589c3563SJarkko Nikula 				"asoc: failed to remove %s: %d\n",
884589c3563SJarkko Nikula 				codec->name, err);
885589c3563SJarkko Nikula 	}
886589c3563SJarkko Nikula 
887589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
888589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
889589c3563SJarkko Nikula 
890589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
891589c3563SJarkko Nikula 	codec->probed = 0;
892589c3563SJarkko Nikula 	list_del(&codec->card_list);
893589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
894589c3563SJarkko Nikula }
895589c3563SJarkko Nikula 
8960168bf0dSLiam Girdwood static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
897f0fba2adSLiam Girdwood {
898f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
899f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
900f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
901f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
902f0fba2adSLiam Girdwood 	int err;
903f0fba2adSLiam Girdwood 
904f0fba2adSLiam Girdwood 	/* unregister the rtd device */
905f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
90636ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
90736ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
90836ae1a96SMark Brown 		device_unregister(rtd->dev);
909f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
91002a06d30SBarry Song 	}
91102a06d30SBarry Song 
912f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9130168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
9140168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
915f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
916f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
917f0fba2adSLiam Girdwood 			if (err < 0)
9180837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9190837fc62SFabio Estevam 							codec_dai->name, err);
920f0fba2adSLiam Girdwood 		}
921f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
922f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
923f0fba2adSLiam Girdwood 	}
924f0fba2adSLiam Girdwood 
925f0fba2adSLiam Girdwood 	/* remove the platform */
9260168bf0dSLiam Girdwood 	if (platform && platform->probed &&
9270168bf0dSLiam Girdwood 			platform->driver->remove_order == order) {
928f0fba2adSLiam Girdwood 		if (platform->driver->remove) {
929f0fba2adSLiam Girdwood 			err = platform->driver->remove(platform);
930f0fba2adSLiam Girdwood 			if (err < 0)
9310837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9320837fc62SFabio Estevam 							platform->name, err);
933f0fba2adSLiam Girdwood 		}
934675c496bSLiam Girdwood 
935675c496bSLiam Girdwood 		/* Make sure all DAPM widgets are freed */
936675c496bSLiam Girdwood 		snd_soc_dapm_free(&platform->dapm);
937675c496bSLiam Girdwood 
938731f1ab2SSebastien Guiriec 		soc_cleanup_platform_debugfs(platform);
939f0fba2adSLiam Girdwood 		platform->probed = 0;
940f0fba2adSLiam Girdwood 		list_del(&platform->card_list);
941f0fba2adSLiam Girdwood 		module_put(platform->dev->driver->owner);
942f0fba2adSLiam Girdwood 	}
943f0fba2adSLiam Girdwood 
944f0fba2adSLiam Girdwood 	/* remove the CODEC */
9450168bf0dSLiam Girdwood 	if (codec && codec->probed &&
9460168bf0dSLiam Girdwood 			codec->driver->remove_order == order)
947589c3563SJarkko Nikula 		soc_remove_codec(codec);
948f0fba2adSLiam Girdwood 
949f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
9500168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
9510168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
952f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
953f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
954f0fba2adSLiam Girdwood 			if (err < 0)
9550837fc62SFabio Estevam 				pr_err("asoc: failed to remove %s: %d\n",
9560837fc62SFabio Estevam 							cpu_dai->name, err);
957f0fba2adSLiam Girdwood 		}
958f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
959f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
960f0fba2adSLiam Girdwood 		module_put(cpu_dai->dev->driver->owner);
961f0fba2adSLiam Girdwood 	}
962f0fba2adSLiam Girdwood }
963f0fba2adSLiam Girdwood 
9640671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9650671fd8eSKuninori Morimoto {
9660168bf0dSLiam Girdwood 	int dai, order;
9670671fd8eSKuninori Morimoto 
9680168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9690168bf0dSLiam Girdwood 			order++) {
9700168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
9710168bf0dSLiam Girdwood 			soc_remove_dai_link(card, dai, order);
9720168bf0dSLiam Girdwood 	}
9730671fd8eSKuninori Morimoto 	card->num_rtd = 0;
9740671fd8eSKuninori Morimoto }
9750671fd8eSKuninori Morimoto 
976ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
977ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
978ead9b919SJarkko Nikula {
979ead9b919SJarkko Nikula 	int i;
980ead9b919SJarkko Nikula 
981ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
982ead9b919SJarkko Nikula 		return;
983ead9b919SJarkko Nikula 
984ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
985ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
986ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
987ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
988ead9b919SJarkko Nikula 			break;
989ead9b919SJarkko Nikula 		}
990ead9b919SJarkko Nikula 	}
991ead9b919SJarkko Nikula }
992ead9b919SJarkko Nikula 
993589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
994589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
995589c3563SJarkko Nikula {
996589c3563SJarkko Nikula 	int ret = 0;
99789b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
998888df395SMark Brown 	struct snd_soc_dai *dai;
999589c3563SJarkko Nikula 
1000589c3563SJarkko Nikula 	codec->card = card;
1001589c3563SJarkko Nikula 	codec->dapm.card = card;
1002589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1003589c3563SJarkko Nikula 
100470d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
100570d29331SJarkko Nikula 		return -ENODEV;
100670d29331SJarkko Nikula 
1007d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1008d5d1e0beSLars-Peter Clausen 
100977530150SLars-Peter Clausen 	if (driver->dapm_widgets)
101077530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
101177530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
101277530150SLars-Peter Clausen 
1013888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1014888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1015888df395SMark Brown 		if (dai->dev != codec->dev)
1016888df395SMark Brown 			continue;
1017888df395SMark Brown 
1018888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1019888df395SMark Brown 	}
1020888df395SMark Brown 
102133c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
102233c5f969SMark Brown 
102389b95ac0SMark Brown 	if (driver->probe) {
102489b95ac0SMark Brown 		ret = driver->probe(codec);
1025589c3563SJarkko Nikula 		if (ret < 0) {
1026589c3563SJarkko Nikula 			dev_err(codec->dev,
1027589c3563SJarkko Nikula 				"asoc: failed to probe CODEC %s: %d\n",
1028589c3563SJarkko Nikula 				codec->name, ret);
102970d29331SJarkko Nikula 			goto err_probe;
1030589c3563SJarkko Nikula 		}
1031589c3563SJarkko Nikula 	}
1032589c3563SJarkko Nikula 
1033b7af1dafSMark Brown 	if (driver->controls)
1034022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1035b7af1dafSMark Brown 				     driver->num_controls);
103689b95ac0SMark Brown 	if (driver->dapm_routes)
103789b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
103889b95ac0SMark Brown 					driver->num_dapm_routes);
103989b95ac0SMark Brown 
1040589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1041589c3563SJarkko Nikula 	codec->probed = 1;
1042589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
10437be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1044589c3563SJarkko Nikula 
104570d29331SJarkko Nikula 	return 0;
104670d29331SJarkko Nikula 
104770d29331SJarkko Nikula err_probe:
1048d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
104970d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
105070d29331SJarkko Nikula 
1051589c3563SJarkko Nikula 	return ret;
1052589c3563SJarkko Nikula }
1053589c3563SJarkko Nikula 
1054956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1055956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1056956245e9SLiam Girdwood {
1057956245e9SLiam Girdwood 	int ret = 0;
1058956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1059be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1060956245e9SLiam Girdwood 
1061956245e9SLiam Girdwood 	platform->card = card;
1062b7950641SLiam Girdwood 	platform->dapm.card = card;
1063956245e9SLiam Girdwood 
1064956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1065956245e9SLiam Girdwood 		return -ENODEV;
1066956245e9SLiam Girdwood 
1067731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1068731f1ab2SSebastien Guiriec 
1069cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1070cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1071cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1072cb2cf612SLiam Girdwood 
1073be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
1074be09ad90SLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
1075be09ad90SLiam Girdwood 		if (dai->dev != platform->dev)
1076be09ad90SLiam Girdwood 			continue;
1077be09ad90SLiam Girdwood 
1078be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1079be09ad90SLiam Girdwood 	}
1080be09ad90SLiam Girdwood 
1081956245e9SLiam Girdwood 	if (driver->probe) {
1082956245e9SLiam Girdwood 		ret = driver->probe(platform);
1083956245e9SLiam Girdwood 		if (ret < 0) {
1084956245e9SLiam Girdwood 			dev_err(platform->dev,
1085956245e9SLiam Girdwood 				"asoc: failed to probe platform %s: %d\n",
1086956245e9SLiam Girdwood 				platform->name, ret);
1087956245e9SLiam Girdwood 			goto err_probe;
1088956245e9SLiam Girdwood 		}
1089956245e9SLiam Girdwood 	}
1090956245e9SLiam Girdwood 
1091cb2cf612SLiam Girdwood 	if (driver->controls)
1092cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1093cb2cf612SLiam Girdwood 				     driver->num_controls);
1094cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1095cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1096cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1097cb2cf612SLiam Girdwood 
1098956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1099956245e9SLiam Girdwood 	platform->probed = 1;
1100956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1101b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1102956245e9SLiam Girdwood 
1103956245e9SLiam Girdwood 	return 0;
1104956245e9SLiam Girdwood 
1105956245e9SLiam Girdwood err_probe:
110602db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1107956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1108956245e9SLiam Girdwood 
1109956245e9SLiam Girdwood 	return ret;
1110956245e9SLiam Girdwood }
1111956245e9SLiam Girdwood 
111236ae1a96SMark Brown static void rtd_release(struct device *dev)
111336ae1a96SMark Brown {
111436ae1a96SMark Brown 	kfree(dev);
111536ae1a96SMark Brown }
1116f0fba2adSLiam Girdwood 
1117589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1118589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1119589c3563SJarkko Nikula 				   int num, int dailess)
1120589c3563SJarkko Nikula {
1121589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1122589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1123589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1124589c3563SJarkko Nikula 	const char *temp, *name;
1125589c3563SJarkko Nikula 	int ret = 0;
1126589c3563SJarkko Nikula 
1127589c3563SJarkko Nikula 	if (!dailess) {
1128589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1129589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1130589c3563SJarkko Nikula 		name = dai_link->name;
1131589c3563SJarkko Nikula 	} else {
1132589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1133589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1134589c3563SJarkko Nikula 		name = aux_dev->name;
1135589c3563SJarkko Nikula 	}
11360962bb21SJanusz Krzysztofik 	rtd->card = card;
1137589c3563SJarkko Nikula 
11384b1cfcb4SMark Brown 	/* Make sure all DAPM widgets are instantiated */
11394b1cfcb4SMark Brown 	snd_soc_dapm_new_widgets(&codec->dapm);
11404b1cfcb4SMark Brown 
1141589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1142589c3563SJarkko Nikula 	temp = codec->name_prefix;
1143589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1144589c3563SJarkko Nikula 
1145589c3563SJarkko Nikula 	/* do machine specific initialization */
1146589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1147589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1148589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1149589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1150589c3563SJarkko Nikula 	if (ret < 0) {
1151589c3563SJarkko Nikula 		dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1152589c3563SJarkko Nikula 		return ret;
1153589c3563SJarkko Nikula 	}
1154589c3563SJarkko Nikula 	codec->name_prefix = temp;
1155589c3563SJarkko Nikula 
1156589c3563SJarkko Nikula 	/* register the rtd device */
1157589c3563SJarkko Nikula 	rtd->codec = codec;
115836ae1a96SMark Brown 
115936ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
116036ae1a96SMark Brown 	if (!rtd->dev)
116136ae1a96SMark Brown 		return -ENOMEM;
116236ae1a96SMark Brown 	device_initialize(rtd->dev);
116336ae1a96SMark Brown 	rtd->dev->parent = card->dev;
116436ae1a96SMark Brown 	rtd->dev->release = rtd_release;
116536ae1a96SMark Brown 	rtd->dev->init_name = name;
116636ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1167b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
116836ae1a96SMark Brown 	ret = device_add(rtd->dev);
1169589c3563SJarkko Nikula 	if (ret < 0) {
1170589c3563SJarkko Nikula 		dev_err(card->dev,
1171589c3563SJarkko Nikula 			"asoc: failed to register runtime device: %d\n", ret);
1172589c3563SJarkko Nikula 		return ret;
1173589c3563SJarkko Nikula 	}
1174589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1175589c3563SJarkko Nikula 
1176589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
117736ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1178589c3563SJarkko Nikula 	if (ret < 0)
1179589c3563SJarkko Nikula 		dev_err(codec->dev,
1180589c3563SJarkko Nikula 			"asoc: failed to add codec dapm sysfs entries: %d\n",
1181589c3563SJarkko Nikula 			ret);
1182589c3563SJarkko Nikula 
1183589c3563SJarkko Nikula 	/* add codec sysfs entries */
118436ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1185589c3563SJarkko Nikula 	if (ret < 0)
1186589c3563SJarkko Nikula 		dev_err(codec->dev,
1187589c3563SJarkko Nikula 			"asoc: failed to add codec sysfs files: %d\n", ret);
1188589c3563SJarkko Nikula 
1189589c3563SJarkko Nikula 	return 0;
1190589c3563SJarkko Nikula }
1191589c3563SJarkko Nikula 
11920168bf0dSLiam Girdwood static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1193f0fba2adSLiam Girdwood {
1194f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1195f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1196f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1197f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1198f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1199f0fba2adSLiam Girdwood 	int ret;
1200f0fba2adSLiam Girdwood 
12010168bf0dSLiam Girdwood 	dev_dbg(card->dev, "probe %s dai link %d late %d\n",
12020168bf0dSLiam Girdwood 			card->name, num, order);
1203f0fba2adSLiam Girdwood 
1204f0fba2adSLiam Girdwood 	/* config components */
1205f0fba2adSLiam Girdwood 	codec_dai->codec = codec;
1206f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1207f0fba2adSLiam Girdwood 	codec_dai->card = card;
1208f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1209f0fba2adSLiam Girdwood 
1210f0fba2adSLiam Girdwood 	/* set default power off timeout */
1211f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1212f0fba2adSLiam Girdwood 
1213f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
12140168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
12150168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1216be09ad90SLiam Girdwood 		cpu_dai->dapm.card = card;
121761b61e3cSLiam Girdwood 		if (!try_module_get(cpu_dai->dev->driver->owner))
121861b61e3cSLiam Girdwood 			return -ENODEV;
121961b61e3cSLiam Girdwood 
1220be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1221be09ad90SLiam Girdwood 
1222f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1223f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1224f0fba2adSLiam Girdwood 			if (ret < 0) {
12250837fc62SFabio Estevam 				pr_err("asoc: failed to probe CPU DAI %s: %d\n",
12260837fc62SFabio Estevam 							cpu_dai->name, ret);
122761b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1228f0fba2adSLiam Girdwood 				return ret;
1229f0fba2adSLiam Girdwood 			}
1230f0fba2adSLiam Girdwood 		}
1231f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
12321c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1233f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1234f0fba2adSLiam Girdwood 	}
1235f0fba2adSLiam Girdwood 
1236f0fba2adSLiam Girdwood 	/* probe the CODEC */
12370168bf0dSLiam Girdwood 	if (!codec->probed &&
12380168bf0dSLiam Girdwood 			codec->driver->probe_order == order) {
1239589c3563SJarkko Nikula 		ret = soc_probe_codec(card, codec);
1240589c3563SJarkko Nikula 		if (ret < 0)
1241f0fba2adSLiam Girdwood 			return ret;
1242f0fba2adSLiam Girdwood 	}
1243f0fba2adSLiam Girdwood 
1244f0fba2adSLiam Girdwood 	/* probe the platform */
12450168bf0dSLiam Girdwood 	if (!platform->probed &&
12460168bf0dSLiam Girdwood 			platform->driver->probe_order == order) {
1247956245e9SLiam Girdwood 		ret = soc_probe_platform(card, platform);
1248956245e9SLiam Girdwood 		if (ret < 0)
1249f0fba2adSLiam Girdwood 			return ret;
1250f0fba2adSLiam Girdwood 	}
1251f0fba2adSLiam Girdwood 
1252f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
12530168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1254f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1255f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1256f0fba2adSLiam Girdwood 			if (ret < 0) {
12570837fc62SFabio Estevam 				pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
12580837fc62SFabio Estevam 							codec_dai->name, ret);
1259f0fba2adSLiam Girdwood 				return ret;
1260f0fba2adSLiam Girdwood 			}
1261f0fba2adSLiam Girdwood 		}
1262f0fba2adSLiam Girdwood 
12631c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1264f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1265f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1266f0fba2adSLiam Girdwood 	}
1267f0fba2adSLiam Girdwood 
12680168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
12690168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
12700168bf0dSLiam Girdwood 		return 0;
12710168bf0dSLiam Girdwood 
1272589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1273589c3563SJarkko Nikula 	if (ret)
1274f0fba2adSLiam Girdwood 		return ret;
1275f0fba2adSLiam Girdwood 
127636ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1277f0fba2adSLiam Girdwood 	if (ret < 0)
12780837fc62SFabio Estevam 		pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1279f0fba2adSLiam Girdwood 
1280f0fba2adSLiam Girdwood 	/* create the pcm */
1281f0fba2adSLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1282f0fba2adSLiam Girdwood 	if (ret < 0) {
12830837fc62SFabio Estevam 		pr_err("asoc: can't create pcm %s :%d\n",
12840837fc62SFabio Estevam 				dai_link->stream_name, ret);
1285f0fba2adSLiam Girdwood 		return ret;
1286f0fba2adSLiam Girdwood 	}
1287f0fba2adSLiam Girdwood 
1288f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1289f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1290f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1291f0fba2adSLiam Girdwood 
1292f0fba2adSLiam Girdwood 	return 0;
1293f0fba2adSLiam Girdwood }
1294f0fba2adSLiam Girdwood 
1295f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1296f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1297f0fba2adSLiam Girdwood {
1298f0fba2adSLiam Girdwood 	int ret;
1299f0fba2adSLiam Girdwood 
1300f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1301f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
13026b05eda6SMark Brown 	 */
1303f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
13040562f788SMika Westerberg 		/*
13050562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
13060562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
13070562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
13080562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
13090562f788SMika Westerberg 		 *
13100562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
13110562f788SMika Westerberg 		 */
13120562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
13130562f788SMika Westerberg 			return 0;
1314f0fba2adSLiam Girdwood 
1315f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1316f0fba2adSLiam Girdwood 		if (ret < 0) {
13170837fc62SFabio Estevam 			pr_err("asoc: AC97 device register failed:%d\n", ret);
1318f0fba2adSLiam Girdwood 			return ret;
1319435c5e25SMark Brown 		}
1320435c5e25SMark Brown 
1321f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1322f0fba2adSLiam Girdwood 	}
1323f0fba2adSLiam Girdwood 	return 0;
1324f0fba2adSLiam Girdwood }
1325435c5e25SMark Brown 
1326f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1327f0fba2adSLiam Girdwood {
1328f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1329f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1330f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1331f0fba2adSLiam Girdwood 	}
1332f0fba2adSLiam Girdwood }
1333f0fba2adSLiam Girdwood #endif
1334435c5e25SMark Brown 
1335b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1336b19e6e7bSMark Brown {
1337b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1338b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1339b19e6e7bSMark Brown 
1340b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1341b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1342b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1343b19e6e7bSMark Brown 			return 0;
1344b19e6e7bSMark Brown 	}
1345b19e6e7bSMark Brown 
1346b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1347b19e6e7bSMark Brown }
1348b19e6e7bSMark Brown 
13492eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
13502eea392dSJarkko Nikula {
13512eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
13522eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1353676ad98aSJarkko Nikula 	int ret = -ENODEV;
13542eea392dSJarkko Nikula 
13552eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
13562eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
13572eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
13582eea392dSJarkko Nikula 			if (codec->probed) {
13592eea392dSJarkko Nikula 				dev_err(codec->dev,
13602eea392dSJarkko Nikula 					"asoc: codec already probed");
13612eea392dSJarkko Nikula 				ret = -EBUSY;
13622eea392dSJarkko Nikula 				goto out;
13632eea392dSJarkko Nikula 			}
1364676ad98aSJarkko Nikula 			goto found;
13652eea392dSJarkko Nikula 		}
13662eea392dSJarkko Nikula 	}
1367676ad98aSJarkko Nikula 	/* codec not found */
1368676ad98aSJarkko Nikula 	dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1369b19e6e7bSMark Brown 	return -EPROBE_DEFER;
13702eea392dSJarkko Nikula 
1371676ad98aSJarkko Nikula found:
1372589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
13732eea392dSJarkko Nikula 	if (ret < 0)
1374589c3563SJarkko Nikula 		return ret;
13752eea392dSJarkko Nikula 
1376589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
13772eea392dSJarkko Nikula 
13782eea392dSJarkko Nikula out:
13792eea392dSJarkko Nikula 	return ret;
13802eea392dSJarkko Nikula }
13812eea392dSJarkko Nikula 
13822eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
13832eea392dSJarkko Nikula {
13842eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
13852eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
13862eea392dSJarkko Nikula 
13872eea392dSJarkko Nikula 	/* unregister the rtd device */
13882eea392dSJarkko Nikula 	if (rtd->dev_registered) {
138936ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
139036ae1a96SMark Brown 		device_del(rtd->dev);
13912eea392dSJarkko Nikula 		rtd->dev_registered = 0;
13922eea392dSJarkko Nikula 	}
13932eea392dSJarkko Nikula 
1394589c3563SJarkko Nikula 	if (codec && codec->probed)
1395589c3563SJarkko Nikula 		soc_remove_codec(codec);
13962eea392dSJarkko Nikula }
13972eea392dSJarkko Nikula 
1398fdf0f54dSDimitris Papastamos static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1399fdf0f54dSDimitris Papastamos 				    enum snd_soc_compress_type compress_type)
1400fdf0f54dSDimitris Papastamos {
1401fdf0f54dSDimitris Papastamos 	int ret;
1402fdf0f54dSDimitris Papastamos 
1403fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1404fdf0f54dSDimitris Papastamos 		return 0;
1405fdf0f54dSDimitris Papastamos 
1406fdf0f54dSDimitris Papastamos 	/* override the compress_type if necessary */
1407fdf0f54dSDimitris Papastamos 	if (compress_type && codec->compress_type != compress_type)
1408fdf0f54dSDimitris Papastamos 		codec->compress_type = compress_type;
1409fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1410fdf0f54dSDimitris Papastamos 	if (ret < 0) {
1411fdf0f54dSDimitris Papastamos 		dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1412fdf0f54dSDimitris Papastamos 			ret);
1413fdf0f54dSDimitris Papastamos 		return ret;
1414fdf0f54dSDimitris Papastamos 	}
1415fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1416fdf0f54dSDimitris Papastamos 	return 0;
1417fdf0f54dSDimitris Papastamos }
1418fdf0f54dSDimitris Papastamos 
1419b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1420f0fba2adSLiam Girdwood {
1421fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
1422fdf0f54dSDimitris Papastamos 	struct snd_soc_codec_conf *codec_conf;
1423fdf0f54dSDimitris Papastamos 	enum snd_soc_compress_type compress_type;
142475d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
14250168bf0dSLiam Girdwood 	int ret, i, order;
142696dd3622SMark Brown 
142701b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1428f0fba2adSLiam Girdwood 
1429b19e6e7bSMark Brown 	/* bind DAIs */
1430b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1431b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1432b19e6e7bSMark Brown 		if (ret != 0)
1433b19e6e7bSMark Brown 			goto base_error;
1434db2a4165SFrank Mandarino 	}
1435db2a4165SFrank Mandarino 
1436b19e6e7bSMark Brown 	/* check aux_devs too */
1437b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1438b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1439b19e6e7bSMark Brown 		if (ret != 0)
1440b19e6e7bSMark Brown 			goto base_error;
1441db2a4165SFrank Mandarino 	}
1442db2a4165SFrank Mandarino 
1443fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1444fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1445fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1446fdf0f54dSDimitris Papastamos 			continue;
1447861f2fafSDimitris Papastamos 		/* by default we don't override the compress_type */
1448861f2fafSDimitris Papastamos 		compress_type = 0;
1449fdf0f54dSDimitris Papastamos 		/* check to see if we need to override the compress_type */
1450fdf0f54dSDimitris Papastamos 		for (i = 0; i < card->num_configs; ++i) {
1451fdf0f54dSDimitris Papastamos 			codec_conf = &card->codec_conf[i];
1452fdf0f54dSDimitris Papastamos 			if (!strcmp(codec->name, codec_conf->dev_name)) {
1453fdf0f54dSDimitris Papastamos 				compress_type = codec_conf->compress_type;
1454fdf0f54dSDimitris Papastamos 				if (compress_type && compress_type
1455fdf0f54dSDimitris Papastamos 				    != codec->compress_type)
1456fdf0f54dSDimitris Papastamos 					break;
1457fdf0f54dSDimitris Papastamos 			}
1458fdf0f54dSDimitris Papastamos 		}
1459fdf0f54dSDimitris Papastamos 		ret = snd_soc_init_codec_cache(codec, compress_type);
1460b19e6e7bSMark Brown 		if (ret < 0)
1461b19e6e7bSMark Brown 			goto base_error;
1462fdf0f54dSDimitris Papastamos 	}
1463fdf0f54dSDimitris Papastamos 
1464f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1465f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1466f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1467f0fba2adSLiam Girdwood 	if (ret < 0) {
14680837fc62SFabio Estevam 		pr_err("asoc: can't create sound card for card %s: %d\n",
14690837fc62SFabio Estevam 			card->name, ret);
1470b19e6e7bSMark Brown 		goto base_error;
1471db2a4165SFrank Mandarino 	}
1472f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1473db2a4165SFrank Mandarino 
1474e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1475e37a4970SMark Brown 	card->dapm.dev = card->dev;
1476e37a4970SMark Brown 	card->dapm.card = card;
1477e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1478e37a4970SMark Brown 
1479d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1480d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1481d5d1e0beSLars-Peter Clausen #endif
1482d5d1e0beSLars-Peter Clausen 
148388ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
14846ed25978SAndy Green 	/* deferred resume work */
14856308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
14861301a964SRandy Dunlap #endif
14876ed25978SAndy Green 
14889a841ebbSMark Brown 	if (card->dapm_widgets)
14899a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
14909a841ebbSMark Brown 					  card->num_dapm_widgets);
14919a841ebbSMark Brown 
1492f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1493f0fba2adSLiam Girdwood 	if (card->probe) {
1494e7361ec4SMark Brown 		ret = card->probe(card);
1495f0fba2adSLiam Girdwood 		if (ret < 0)
1496f0fba2adSLiam Girdwood 			goto card_probe_error;
1497f0fba2adSLiam Girdwood 	}
1498f0fba2adSLiam Girdwood 
14990168bf0dSLiam Girdwood 	/* early DAI link probe */
15000168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
15010168bf0dSLiam Girdwood 			order++) {
1502fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
15030168bf0dSLiam Girdwood 			ret = soc_probe_dai_link(card, i, order);
1504fe3e78e0SMark Brown 			if (ret < 0) {
1505321de0d0SMark Brown 				pr_err("asoc: failed to instantiate card %s: %d\n",
1506321de0d0SMark Brown 			       card->name, ret);
1507f0fba2adSLiam Girdwood 				goto probe_dai_err;
1508fe3e78e0SMark Brown 			}
1509fe3e78e0SMark Brown 		}
15100168bf0dSLiam Girdwood 	}
1511fe3e78e0SMark Brown 
15122eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
15132eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
15142eea392dSJarkko Nikula 		if (ret < 0) {
15152eea392dSJarkko Nikula 			pr_err("asoc: failed to add auxiliary devices %s: %d\n",
15162eea392dSJarkko Nikula 			       card->name, ret);
15172eea392dSJarkko Nikula 			goto probe_aux_dev_err;
15182eea392dSJarkko Nikula 		}
15192eea392dSJarkko Nikula 	}
15202eea392dSJarkko Nikula 
1521888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1522888df395SMark Brown 
1523b7af1dafSMark Brown 	if (card->controls)
1524022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1525b7af1dafSMark Brown 
1526b8ad29deSMark Brown 	if (card->dapm_routes)
1527b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1528b8ad29deSMark Brown 					card->num_dapm_routes);
1529b8ad29deSMark Brown 
1530b90d2f90SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
1531b90d2f90SMark Brown 
153275d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
153375d9ac46SMark Brown 		dai_link = &card->dai_link[i];
153475d9ac46SMark Brown 
153575d9ac46SMark Brown 		if (dai_link->dai_fmt) {
153675d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
153775d9ac46SMark Brown 						  dai_link->dai_fmt);
15385e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
153975d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
154075d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
154175d9ac46SMark Brown 					 ret);
154275d9ac46SMark Brown 
154375d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
154475d9ac46SMark Brown 						  dai_link->dai_fmt);
15455e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
154675d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
154775d9ac46SMark Brown 					 "Failed to set DAI format: %d\n",
154875d9ac46SMark Brown 					 ret);
154975d9ac46SMark Brown 		}
155075d9ac46SMark Brown 	}
155175d9ac46SMark Brown 
1552f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1553fe3e78e0SMark Brown 		 "%s", card->name);
1554f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
155522de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1556f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1557f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1558f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1559f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1560f0e8ed85SMark Brown 		case '_':
1561f0e8ed85SMark Brown 		case '-':
1562f0e8ed85SMark Brown 		case '\0':
1563f0e8ed85SMark Brown 			break;
1564f0e8ed85SMark Brown 		default:
1565f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1566f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1567f0e8ed85SMark Brown 			break;
1568f0e8ed85SMark Brown 		}
1569f0e8ed85SMark Brown 	}
1570fe3e78e0SMark Brown 
157128e9ad92SMark Brown 	if (card->late_probe) {
157228e9ad92SMark Brown 		ret = card->late_probe(card);
157328e9ad92SMark Brown 		if (ret < 0) {
157428e9ad92SMark Brown 			dev_err(card->dev, "%s late_probe() failed: %d\n",
157528e9ad92SMark Brown 				card->name, ret);
157628e9ad92SMark Brown 			goto probe_aux_dev_err;
157728e9ad92SMark Brown 		}
157828e9ad92SMark Brown 	}
157928e9ad92SMark Brown 
15802dc00213SMark Brown 	snd_soc_dapm_new_widgets(&card->dapm);
15812dc00213SMark Brown 
15821633281bSStephen Warren 	if (card->fully_routed)
1583b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
15841633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
15851633281bSStephen Warren 
1586f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1587fe3e78e0SMark Brown 	if (ret < 0) {
15880837fc62SFabio Estevam 		pr_err("asoc: failed to register soundcard for %s: %d\n",
15890837fc62SFabio Estevam 							card->name, ret);
15906b3ed785SAxel Lin 		goto probe_aux_dev_err;
1591fe3e78e0SMark Brown 	}
1592fe3e78e0SMark Brown 
1593fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1594f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1595f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1596f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1597fe3e78e0SMark Brown 		if (ret < 0) {
15980837fc62SFabio Estevam 			pr_err("asoc: failed to register AC97 %s: %d\n",
15990837fc62SFabio Estevam 							card->name, ret);
16006b3ed785SAxel Lin 			while (--i >= 0)
16017d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
16026b3ed785SAxel Lin 			goto probe_aux_dev_err;
1603fe3e78e0SMark Brown 		}
1604fe3e78e0SMark Brown 	}
1605fe3e78e0SMark Brown #endif
1606fe3e78e0SMark Brown 
1607435c5e25SMark Brown 	card->instantiated = 1;
16084f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1609f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1610b19e6e7bSMark Brown 
1611b19e6e7bSMark Brown 	return 0;
1612db2a4165SFrank Mandarino 
16132eea392dSJarkko Nikula probe_aux_dev_err:
16142eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16152eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16162eea392dSJarkko Nikula 
1617f0fba2adSLiam Girdwood probe_dai_err:
16180671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1619fe3e78e0SMark Brown 
1620f0fba2adSLiam Girdwood card_probe_error:
162187506549SMark Brown 	if (card->remove)
1622e7361ec4SMark Brown 		card->remove(card);
1623f0fba2adSLiam Girdwood 
1624f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1625f0fba2adSLiam Girdwood 
1626b19e6e7bSMark Brown base_error:
1627f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1628db2a4165SFrank Mandarino 
1629b19e6e7bSMark Brown 	return ret;
1630435c5e25SMark Brown }
1631435c5e25SMark Brown 
1632435c5e25SMark Brown /* probes a new socdev */
1633435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1634435c5e25SMark Brown {
1635f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1636435c5e25SMark Brown 	int ret = 0;
1637435c5e25SMark Brown 
163870a7ca34SVinod Koul 	/*
163970a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
164070a7ca34SVinod Koul 	 * we should not be here in that case so ret error
164170a7ca34SVinod Koul 	 */
164270a7ca34SVinod Koul 	if (!card)
164370a7ca34SVinod Koul 		return -EINVAL;
164470a7ca34SVinod Koul 
1645fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1646fe4085e8SMark Brown 		 "ASoC machine %s should use snd_soc_register_card()\n",
1647fe4085e8SMark Brown 		 card->name);
1648fe4085e8SMark Brown 
1649435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1650435c5e25SMark Brown 	card->dev = &pdev->dev;
1651f0fba2adSLiam Girdwood 
1652435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1653435c5e25SMark Brown 	if (ret != 0) {
1654435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1655db2a4165SFrank Mandarino 		return ret;
1656db2a4165SFrank Mandarino 	}
1657db2a4165SFrank Mandarino 
1658435c5e25SMark Brown 	return 0;
1659435c5e25SMark Brown }
1660435c5e25SMark Brown 
1661b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1662db2a4165SFrank Mandarino {
1663db2a4165SFrank Mandarino 	int i;
1664db2a4165SFrank Mandarino 
1665f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1666f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1667f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
16685b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1669db2a4165SFrank Mandarino 	}
1670db2a4165SFrank Mandarino 
16712eea392dSJarkko Nikula 	/* remove auxiliary devices */
16722eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
16732eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
16742eea392dSJarkko Nikula 
1675f0fba2adSLiam Girdwood 	/* remove and free each DAI */
16760671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1677f0fba2adSLiam Girdwood 
1678a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1679a6052154SJarkko Nikula 
1680f0fba2adSLiam Girdwood 	/* remove the card */
168187506549SMark Brown 	if (card->remove)
1682e7361ec4SMark Brown 		card->remove(card);
1683f0fba2adSLiam Girdwood 
16840aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
16850aaae527SLars-Peter Clausen 
1686f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1687b0e26485SVinod Koul 	return 0;
1688b0e26485SVinod Koul 
1689b2dfa62cSGuennadi Liakhovetski }
1690b0e26485SVinod Koul 
1691b0e26485SVinod Koul /* removes a socdev */
1692b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1693b0e26485SVinod Koul {
1694b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1695b0e26485SVinod Koul 
1696c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1697db2a4165SFrank Mandarino 	return 0;
1698db2a4165SFrank Mandarino }
1699db2a4165SFrank Mandarino 
17006f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
170151737470SMark Brown {
17026f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1703f0fba2adSLiam Girdwood 	int i;
170451737470SMark Brown 
170551737470SMark Brown 	if (!card->instantiated)
1706416356fcSMark Brown 		return 0;
170751737470SMark Brown 
170851737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
170951737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1710f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1711f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
17125b84ba26STejun Heo 		flush_delayed_work_sync(&rtd->delayed_work);
1713f0fba2adSLiam Girdwood 	}
171451737470SMark Brown 
1715f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1716416356fcSMark Brown 
1717416356fcSMark Brown 	return 0;
171851737470SMark Brown }
17196f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
172051737470SMark Brown 
17216f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1722b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1723b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1724b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1725b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
17266f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1727b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1728416356fcSMark Brown };
1729deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1730416356fcSMark Brown 
1731db2a4165SFrank Mandarino /* ASoC platform driver */
1732db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1733db2a4165SFrank Mandarino 	.driver		= {
1734db2a4165SFrank Mandarino 		.name		= "soc-audio",
17358b45a209SKay Sievers 		.owner		= THIS_MODULE,
17366f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1737db2a4165SFrank Mandarino 	},
1738db2a4165SFrank Mandarino 	.probe		= soc_probe,
1739db2a4165SFrank Mandarino 	.remove		= soc_remove,
1740db2a4165SFrank Mandarino };
1741db2a4165SFrank Mandarino 
1742096e49d5SMark Brown /**
1743096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1744096e49d5SMark Brown  *
1745096e49d5SMark Brown  * @codec: CODEC to query.
1746096e49d5SMark Brown  * @reg: Register to query.
1747096e49d5SMark Brown  *
1748096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1749096e49d5SMark Brown  */
1750181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1751181e055eSMark Brown 				    unsigned int reg)
1752096e49d5SMark Brown {
17531500b7b5SDimitris Papastamos 	if (codec->volatile_register)
17541500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1755096e49d5SMark Brown 	else
1756096e49d5SMark Brown 		return 0;
1757096e49d5SMark Brown }
1758096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1759096e49d5SMark Brown 
1760db2a4165SFrank Mandarino /**
1761239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
1762239c9706SDimitris Papastamos  *
1763239c9706SDimitris Papastamos  * @codec: CODEC to query.
1764239c9706SDimitris Papastamos  * @reg: Register to query.
1765239c9706SDimitris Papastamos  *
1766239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
1767239c9706SDimitris Papastamos  */
1768239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1769239c9706SDimitris Papastamos 				    unsigned int reg)
1770239c9706SDimitris Papastamos {
1771239c9706SDimitris Papastamos 	if (codec->readable_register)
1772239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
1773239c9706SDimitris Papastamos 	else
177463fa0a28SLars-Peter Clausen 		return 1;
1775239c9706SDimitris Papastamos }
1776239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1777239c9706SDimitris Papastamos 
1778239c9706SDimitris Papastamos /**
1779239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
1780239c9706SDimitris Papastamos  *
1781239c9706SDimitris Papastamos  * @codec: CODEC to query.
1782239c9706SDimitris Papastamos  * @reg: Register to query.
1783239c9706SDimitris Papastamos  *
1784239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
1785239c9706SDimitris Papastamos  */
1786239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1787239c9706SDimitris Papastamos 				    unsigned int reg)
1788239c9706SDimitris Papastamos {
1789239c9706SDimitris Papastamos 	if (codec->writable_register)
1790239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
1791239c9706SDimitris Papastamos 	else
179263fa0a28SLars-Peter Clausen 		return 1;
1793239c9706SDimitris Papastamos }
1794239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1795239c9706SDimitris Papastamos 
1796f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
1797f1442bc1SLiam Girdwood 					unsigned int reg)
1798f1442bc1SLiam Girdwood {
1799f1442bc1SLiam Girdwood 	unsigned int ret;
1800f1442bc1SLiam Girdwood 
1801f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
1802f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no read back\n");
1803f1442bc1SLiam Girdwood 		return -1;
1804f1442bc1SLiam Girdwood 	}
1805f1442bc1SLiam Girdwood 
1806f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
1807f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1808a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
1809f1442bc1SLiam Girdwood 
1810f1442bc1SLiam Girdwood 	return ret;
1811f1442bc1SLiam Girdwood }
1812f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1813f1442bc1SLiam Girdwood 
1814f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
1815f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
1816f1442bc1SLiam Girdwood {
1817f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
1818f1442bc1SLiam Girdwood 		dev_err(platform->dev, "platform has no write back\n");
1819f1442bc1SLiam Girdwood 		return -1;
1820f1442bc1SLiam Girdwood 	}
1821f1442bc1SLiam Girdwood 
1822f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1823a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
1824f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
1825f1442bc1SLiam Girdwood }
1826f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1827f1442bc1SLiam Girdwood 
1828239c9706SDimitris Papastamos /**
1829db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1830db2a4165SFrank Mandarino  * @codec: audio codec
1831db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1832db2a4165SFrank Mandarino  * @num: AC97 codec number
1833db2a4165SFrank Mandarino  *
1834db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1835db2a4165SFrank Mandarino  */
1836db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1837db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1838db2a4165SFrank Mandarino {
1839db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1840db2a4165SFrank Mandarino 
1841db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1842db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1843db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1844db2a4165SFrank Mandarino 		return -ENOMEM;
1845db2a4165SFrank Mandarino 	}
1846db2a4165SFrank Mandarino 
1847db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1848db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1849db2a4165SFrank Mandarino 		kfree(codec->ac97);
1850db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1851db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1852db2a4165SFrank Mandarino 		return -ENOMEM;
1853db2a4165SFrank Mandarino 	}
1854db2a4165SFrank Mandarino 
1855db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1856db2a4165SFrank Mandarino 	codec->ac97->num = num;
18570562f788SMika Westerberg 
18580562f788SMika Westerberg 	/*
18590562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
18600562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
18610562f788SMika Westerberg 	 */
18620562f788SMika Westerberg 	codec->ac97_created = 1;
18630562f788SMika Westerberg 
1864db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1865db2a4165SFrank Mandarino 	return 0;
1866db2a4165SFrank Mandarino }
1867db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1868db2a4165SFrank Mandarino 
1869db2a4165SFrank Mandarino /**
1870db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1871db2a4165SFrank Mandarino  * @codec: audio codec
1872db2a4165SFrank Mandarino  *
1873db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1874db2a4165SFrank Mandarino  */
1875db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1876db2a4165SFrank Mandarino {
1877db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1878f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1879f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
1880f0fba2adSLiam Girdwood #endif
1881db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1882db2a4165SFrank Mandarino 	kfree(codec->ac97);
1883db2a4165SFrank Mandarino 	codec->ac97 = NULL;
18840562f788SMika Westerberg 	codec->ac97_created = 0;
1885db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1886db2a4165SFrank Mandarino }
1887db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1888db2a4165SFrank Mandarino 
1889c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1890c3753707SMark Brown {
1891c3753707SMark Brown 	unsigned int ret;
1892c3753707SMark Brown 
1893c3acec26SMark Brown 	ret = codec->read(codec, reg);
1894c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1895a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
1896c3753707SMark Brown 
1897c3753707SMark Brown 	return ret;
1898c3753707SMark Brown }
1899c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
1900c3753707SMark Brown 
1901c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
1902c3753707SMark Brown 			   unsigned int reg, unsigned int val)
1903c3753707SMark Brown {
1904c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1905a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
1906c3acec26SMark Brown 	return codec->write(codec, reg, val);
1907c3753707SMark Brown }
1908c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
1909c3753707SMark Brown 
19105fb609d4SDimitris Papastamos unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
19115fb609d4SDimitris Papastamos 				    unsigned int reg, const void *data, size_t len)
19125fb609d4SDimitris Papastamos {
19135fb609d4SDimitris Papastamos 	return codec->bulk_write_raw(codec, reg, data, len);
19145fb609d4SDimitris Papastamos }
19155fb609d4SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
19165fb609d4SDimitris Papastamos 
1917db2a4165SFrank Mandarino /**
1918db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1919db2a4165SFrank Mandarino  * @codec: audio codec
1920db2a4165SFrank Mandarino  * @reg: codec register
1921db2a4165SFrank Mandarino  * @mask: register mask
1922db2a4165SFrank Mandarino  * @value: new value
1923db2a4165SFrank Mandarino  *
1924db2a4165SFrank Mandarino  * Writes new register value.
1925db2a4165SFrank Mandarino  *
1926180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
1927db2a4165SFrank Mandarino  */
1928db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
192946f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1930db2a4165SFrank Mandarino {
19318a713da8SMark Brown 	bool change;
193246f5822fSDaniel Ribeiro 	unsigned int old, new;
1933180c329dSTimur Tabi 	int ret;
1934db2a4165SFrank Mandarino 
19358a713da8SMark Brown 	if (codec->using_regmap) {
19368a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
19378a713da8SMark Brown 					       mask, value, &change);
19388a713da8SMark Brown 	} else {
1939180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
1940180c329dSTimur Tabi 		if (ret < 0)
1941180c329dSTimur Tabi 			return ret;
1942180c329dSTimur Tabi 
1943180c329dSTimur Tabi 		old = ret;
194478bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
1945db2a4165SFrank Mandarino 		change = old != new;
19468a713da8SMark Brown 		if (change)
1947180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
19488a713da8SMark Brown 	}
19498a713da8SMark Brown 
1950180c329dSTimur Tabi 	if (ret < 0)
1951180c329dSTimur Tabi 		return ret;
1952db2a4165SFrank Mandarino 
1953db2a4165SFrank Mandarino 	return change;
1954db2a4165SFrank Mandarino }
1955db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1956db2a4165SFrank Mandarino 
1957db2a4165SFrank Mandarino /**
19586c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
19596c508c62SEero Nurkkala  * @codec: audio codec
19606c508c62SEero Nurkkala  * @reg: codec register
19616c508c62SEero Nurkkala  * @mask: register mask
19626c508c62SEero Nurkkala  * @value: new value
19636c508c62SEero Nurkkala  *
19646c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
19656c508c62SEero Nurkkala  *
19666c508c62SEero Nurkkala  * Returns 1 for change else 0.
19676c508c62SEero Nurkkala  */
1968dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
19696c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
19706c508c62SEero Nurkkala 			       unsigned int value)
19716c508c62SEero Nurkkala {
19726c508c62SEero Nurkkala 	int change;
19736c508c62SEero Nurkkala 
19746c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
19756c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
19766c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
19776c508c62SEero Nurkkala 
19786c508c62SEero Nurkkala 	return change;
19796c508c62SEero Nurkkala }
1980dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
19816c508c62SEero Nurkkala 
19826c508c62SEero Nurkkala /**
1983db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1984db2a4165SFrank Mandarino  * @codec: audio codec
1985db2a4165SFrank Mandarino  * @reg: codec register
1986db2a4165SFrank Mandarino  * @mask: register mask
1987db2a4165SFrank Mandarino  * @value: new value
1988db2a4165SFrank Mandarino  *
1989db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1990db2a4165SFrank Mandarino  * different from the old value.
1991db2a4165SFrank Mandarino  *
1992db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1993db2a4165SFrank Mandarino  */
1994db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
199546f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1996db2a4165SFrank Mandarino {
1997db2a4165SFrank Mandarino 	int change;
199846f5822fSDaniel Ribeiro 	unsigned int old, new;
1999db2a4165SFrank Mandarino 
2000db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2001db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2002db2a4165SFrank Mandarino 	change = old != new;
2003db2a4165SFrank Mandarino 
2004db2a4165SFrank Mandarino 	return change;
2005db2a4165SFrank Mandarino }
2006db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2007db2a4165SFrank Mandarino 
2008db2a4165SFrank Mandarino /**
2009db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2010db2a4165SFrank Mandarino  * @substream: the pcm substream
2011db2a4165SFrank Mandarino  * @hw: the hardware parameters
2012db2a4165SFrank Mandarino  *
2013db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
2014db2a4165SFrank Mandarino  */
2015db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2016db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
2017db2a4165SFrank Mandarino {
2018db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
2019db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
2020db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
2021db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
2022db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
2023db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
2024db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
2025db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2026db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
2027db2a4165SFrank Mandarino 	return 0;
2028db2a4165SFrank Mandarino }
2029db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2030db2a4165SFrank Mandarino 
2031db2a4165SFrank Mandarino /**
2032db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2033db2a4165SFrank Mandarino  * @_template: control template
2034db2a4165SFrank Mandarino  * @data: control private data
2035ac11a2b3SMark Brown  * @long_name: control long name
2036efb7ac3fSMark Brown  * @prefix: control name prefix
2037db2a4165SFrank Mandarino  *
2038db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2039db2a4165SFrank Mandarino  *
2040db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2041db2a4165SFrank Mandarino  */
2042db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
20433056557fSMark Brown 				  void *data, const char *long_name,
2044efb7ac3fSMark Brown 				  const char *prefix)
2045db2a4165SFrank Mandarino {
2046db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2047efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2048efb7ac3fSMark Brown 	char *name = NULL;
2049efb7ac3fSMark Brown 	int name_len;
2050db2a4165SFrank Mandarino 
2051db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2052db2a4165SFrank Mandarino 	template.index = 0;
2053db2a4165SFrank Mandarino 
2054efb7ac3fSMark Brown 	if (!long_name)
2055efb7ac3fSMark Brown 		long_name = template.name;
2056efb7ac3fSMark Brown 
2057efb7ac3fSMark Brown 	if (prefix) {
2058efb7ac3fSMark Brown 		name_len = strlen(long_name) + strlen(prefix) + 2;
205957cf9d45SAxel Lin 		name = kmalloc(name_len, GFP_KERNEL);
2060efb7ac3fSMark Brown 		if (!name)
2061efb7ac3fSMark Brown 			return NULL;
2062efb7ac3fSMark Brown 
2063efb7ac3fSMark Brown 		snprintf(name, name_len, "%s %s", prefix, long_name);
2064efb7ac3fSMark Brown 
2065efb7ac3fSMark Brown 		template.name = name;
2066efb7ac3fSMark Brown 	} else {
2067efb7ac3fSMark Brown 		template.name = long_name;
2068efb7ac3fSMark Brown 	}
2069efb7ac3fSMark Brown 
2070efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2071efb7ac3fSMark Brown 
2072efb7ac3fSMark Brown 	kfree(name);
2073efb7ac3fSMark Brown 
2074efb7ac3fSMark Brown 	return kcontrol;
2075db2a4165SFrank Mandarino }
2076db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2077db2a4165SFrank Mandarino 
2078022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2079022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2080022658beSLiam Girdwood 	const char *prefix, void *data)
2081022658beSLiam Girdwood {
2082022658beSLiam Girdwood 	int err, i;
2083022658beSLiam Girdwood 
2084022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2085022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2086022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2087022658beSLiam Girdwood 						     control->name, prefix));
2088022658beSLiam Girdwood 		if (err < 0) {
2089022658beSLiam Girdwood 			dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2090022658beSLiam Girdwood 			return err;
2091022658beSLiam Girdwood 		}
2092022658beSLiam Girdwood 	}
2093022658beSLiam Girdwood 
2094022658beSLiam Girdwood 	return 0;
2095022658beSLiam Girdwood }
2096022658beSLiam Girdwood 
2097db2a4165SFrank Mandarino /**
2098022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2099022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
21003e8e1952SIan Molton  * duplicating this code.
21013e8e1952SIan Molton  *
21023e8e1952SIan Molton  * @codec: codec to add controls to
21033e8e1952SIan Molton  * @controls: array of controls to add
21043e8e1952SIan Molton  * @num_controls: number of elements in the array
21053e8e1952SIan Molton  *
21063e8e1952SIan Molton  * Return 0 for success, else error.
21073e8e1952SIan Molton  */
2108022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
21093e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
21103e8e1952SIan Molton {
2111f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
21123e8e1952SIan Molton 
2113022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2114022658beSLiam Girdwood 			codec->name_prefix, codec);
21153e8e1952SIan Molton }
2116022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
21173e8e1952SIan Molton 
21183e8e1952SIan Molton /**
2119a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2120022658beSLiam Girdwood  * Convenience function to add a list of controls.
2121a491a5c8SLiam Girdwood  *
2122a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2123a491a5c8SLiam Girdwood  * @controls: array of controls to add
2124a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2125a491a5c8SLiam Girdwood  *
2126a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2127a491a5c8SLiam Girdwood  */
2128a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2129a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2130a491a5c8SLiam Girdwood {
2131a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2132a491a5c8SLiam Girdwood 
2133022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2134022658beSLiam Girdwood 			NULL, platform);
2135a491a5c8SLiam Girdwood }
2136a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2137a491a5c8SLiam Girdwood 
2138a491a5c8SLiam Girdwood /**
2139022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2140022658beSLiam Girdwood  * Convenience function to add a list of controls.
2141022658beSLiam Girdwood  *
2142022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2143022658beSLiam Girdwood  * @controls: array of controls to add
2144022658beSLiam Girdwood  * @num_controls: number of elements in the array
2145022658beSLiam Girdwood  *
2146022658beSLiam Girdwood  * Return 0 for success, else error.
2147022658beSLiam Girdwood  */
2148022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2149022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2150022658beSLiam Girdwood {
2151022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2152022658beSLiam Girdwood 
2153022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2154022658beSLiam Girdwood 			NULL, soc_card);
2155022658beSLiam Girdwood }
2156022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2157022658beSLiam Girdwood 
2158022658beSLiam Girdwood /**
2159022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2160022658beSLiam Girdwood  * Convienience function to add a list of controls.
2161022658beSLiam Girdwood  *
2162022658beSLiam Girdwood  * @dai: DAI to add controls to
2163022658beSLiam Girdwood  * @controls: array of controls to add
2164022658beSLiam Girdwood  * @num_controls: number of elements in the array
2165022658beSLiam Girdwood  *
2166022658beSLiam Girdwood  * Return 0 for success, else error.
2167022658beSLiam Girdwood  */
2168022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2169022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2170022658beSLiam Girdwood {
2171022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2172022658beSLiam Girdwood 
2173022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2174022658beSLiam Girdwood 			NULL, dai);
2175022658beSLiam Girdwood }
2176022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2177022658beSLiam Girdwood 
2178022658beSLiam Girdwood /**
2179db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2180db2a4165SFrank Mandarino  * @kcontrol: mixer control
2181db2a4165SFrank Mandarino  * @uinfo: control element information
2182db2a4165SFrank Mandarino  *
2183db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2184db2a4165SFrank Mandarino  * mixer control.
2185db2a4165SFrank Mandarino  *
2186db2a4165SFrank Mandarino  * Returns 0 for success.
2187db2a4165SFrank Mandarino  */
2188db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2189db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2190db2a4165SFrank Mandarino {
2191db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2192db2a4165SFrank Mandarino 
2193db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2194db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2195f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2196db2a4165SFrank Mandarino 
2197f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2198f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2199db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2200db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2201db2a4165SFrank Mandarino 	return 0;
2202db2a4165SFrank Mandarino }
2203db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2204db2a4165SFrank Mandarino 
2205db2a4165SFrank Mandarino /**
2206db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2207db2a4165SFrank Mandarino  * @kcontrol: mixer control
2208ac11a2b3SMark Brown  * @ucontrol: control element information
2209db2a4165SFrank Mandarino  *
2210db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2211db2a4165SFrank Mandarino  *
2212db2a4165SFrank Mandarino  * Returns 0 for success.
2213db2a4165SFrank Mandarino  */
2214db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2215db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2216db2a4165SFrank Mandarino {
2217db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2218db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
221946f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
2220db2a4165SFrank Mandarino 
2221f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2222db2a4165SFrank Mandarino 		;
2223db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
22243ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
22253ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
2226db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2227db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
2228db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
2229db2a4165SFrank Mandarino 
2230db2a4165SFrank Mandarino 	return 0;
2231db2a4165SFrank Mandarino }
2232db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2233db2a4165SFrank Mandarino 
2234db2a4165SFrank Mandarino /**
2235db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2236db2a4165SFrank Mandarino  * @kcontrol: mixer control
2237ac11a2b3SMark Brown  * @ucontrol: control element information
2238db2a4165SFrank Mandarino  *
2239db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2240db2a4165SFrank Mandarino  *
2241db2a4165SFrank Mandarino  * Returns 0 for success.
2242db2a4165SFrank Mandarino  */
2243db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2244db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2245db2a4165SFrank Mandarino {
2246db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2247db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
224846f5822fSDaniel Ribeiro 	unsigned int val;
224946f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
2250db2a4165SFrank Mandarino 
2251f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2252db2a4165SFrank Mandarino 		;
2253f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2254db2a4165SFrank Mandarino 		return -EINVAL;
2255db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
2256db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
2257db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2258f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2259db2a4165SFrank Mandarino 			return -EINVAL;
2260db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2261db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
2262db2a4165SFrank Mandarino 	}
2263db2a4165SFrank Mandarino 
22646c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2265db2a4165SFrank Mandarino }
2266db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2267db2a4165SFrank Mandarino 
2268db2a4165SFrank Mandarino /**
22692e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
22702e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
22712e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
22722e72f8e3SPeter Ujfalusi  *
22732e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
22742e72f8e3SPeter Ujfalusi  *
22752e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
22762e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
22772e72f8e3SPeter Ujfalusi  *
22782e72f8e3SPeter Ujfalusi  * Returns 0 for success.
22792e72f8e3SPeter Ujfalusi  */
22802e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
22812e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
22822e72f8e3SPeter Ujfalusi {
22832e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
228474155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
228546f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
22862e72f8e3SPeter Ujfalusi 
22872e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
22882e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
22892e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
22902e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
22912e72f8e3SPeter Ujfalusi 			break;
22922e72f8e3SPeter Ujfalusi 	}
22932e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
22942e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
22952e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
22962e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
22972e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
22982e72f8e3SPeter Ujfalusi 				break;
22992e72f8e3SPeter Ujfalusi 		}
23002e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
23012e72f8e3SPeter Ujfalusi 	}
23022e72f8e3SPeter Ujfalusi 
23032e72f8e3SPeter Ujfalusi 	return 0;
23042e72f8e3SPeter Ujfalusi }
23052e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
23062e72f8e3SPeter Ujfalusi 
23072e72f8e3SPeter Ujfalusi /**
23082e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
23092e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
23102e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
23112e72f8e3SPeter Ujfalusi  *
23122e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
23132e72f8e3SPeter Ujfalusi  *
23142e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
23152e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
23162e72f8e3SPeter Ujfalusi  *
23172e72f8e3SPeter Ujfalusi  * Returns 0 for success.
23182e72f8e3SPeter Ujfalusi  */
23192e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
23202e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
23212e72f8e3SPeter Ujfalusi {
23222e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
232374155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
232446f5822fSDaniel Ribeiro 	unsigned int val;
232546f5822fSDaniel Ribeiro 	unsigned int mask;
23262e72f8e3SPeter Ujfalusi 
23272e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
23282e72f8e3SPeter Ujfalusi 		return -EINVAL;
23292e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
23302e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
23312e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
23322e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
23332e72f8e3SPeter Ujfalusi 			return -EINVAL;
23342e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
23352e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
23362e72f8e3SPeter Ujfalusi 	}
23372e72f8e3SPeter Ujfalusi 
23386c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
23392e72f8e3SPeter Ujfalusi }
23402e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
23412e72f8e3SPeter Ujfalusi 
23422e72f8e3SPeter Ujfalusi /**
2343db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2344db2a4165SFrank Mandarino  * @kcontrol: mixer control
2345db2a4165SFrank Mandarino  * @uinfo: control element information
2346db2a4165SFrank Mandarino  *
2347db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
2348db2a4165SFrank Mandarino  * single mixer.
2349db2a4165SFrank Mandarino  *
2350db2a4165SFrank Mandarino  * Returns 0 for success.
2351db2a4165SFrank Mandarino  */
2352db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2353db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2354db2a4165SFrank Mandarino {
2355db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2356db2a4165SFrank Mandarino 
2357db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2358db2a4165SFrank Mandarino 	uinfo->count = 1;
2359f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2360db2a4165SFrank Mandarino 
2361f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2362f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2363db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
2364db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
2365db2a4165SFrank Mandarino 	return 0;
2366db2a4165SFrank Mandarino }
2367db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2368db2a4165SFrank Mandarino 
2369db2a4165SFrank Mandarino /**
2370db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
2371db2a4165SFrank Mandarino  * @kcontrol: mixer control
2372db2a4165SFrank Mandarino  * @uinfo: control element information
2373db2a4165SFrank Mandarino  *
2374db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
2375db2a4165SFrank Mandarino  *
2376db2a4165SFrank Mandarino  * Returns 0 for success.
2377db2a4165SFrank Mandarino  */
2378db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2379db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2380db2a4165SFrank Mandarino {
2381a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2382db2a4165SFrank Mandarino 
2383fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2384a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2385a7a4ac86SPhilipp Zabel 	else
2386a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2387a7a4ac86SPhilipp Zabel 
2388db2a4165SFrank Mandarino 	uinfo->count = 1;
2389db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2390a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2391db2a4165SFrank Mandarino 	return 0;
2392db2a4165SFrank Mandarino }
2393db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2394db2a4165SFrank Mandarino 
2395db2a4165SFrank Mandarino /**
2396db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2397db2a4165SFrank Mandarino  * @kcontrol: mixer control
2398db2a4165SFrank Mandarino  * @uinfo: control element information
2399db2a4165SFrank Mandarino  *
2400e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2401e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2402db2a4165SFrank Mandarino  *
2403db2a4165SFrank Mandarino  * Returns 0 for success.
2404db2a4165SFrank Mandarino  */
2405db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2406db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2407db2a4165SFrank Mandarino {
24084eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24094eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2410d11bb4a9SPeter Ujfalusi 	int platform_max;
2411db2a4165SFrank Mandarino 
2412d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2413d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2414d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2415d11bb4a9SPeter Ujfalusi 
2416d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2417a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2418a7a4ac86SPhilipp Zabel 	else
2419a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2420a7a4ac86SPhilipp Zabel 
2421e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2422db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2423d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2424db2a4165SFrank Mandarino 	return 0;
2425db2a4165SFrank Mandarino }
2426db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2427db2a4165SFrank Mandarino 
2428db2a4165SFrank Mandarino /**
2429db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2430db2a4165SFrank Mandarino  * @kcontrol: mixer control
2431ac11a2b3SMark Brown  * @ucontrol: control element information
2432db2a4165SFrank Mandarino  *
2433f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2434f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2435db2a4165SFrank Mandarino  *
2436db2a4165SFrank Mandarino  * Returns 0 for success.
2437db2a4165SFrank Mandarino  */
2438db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2439db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2440db2a4165SFrank Mandarino {
24414eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24424eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2443db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2444815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2445f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2446815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2447815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
24484eaa9819SJon Smirl 	int max = mc->max;
2449815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2450815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2451db2a4165SFrank Mandarino 
2452db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2453db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2454f7915d99SPeter Ujfalusi 	if (invert)
2455db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2456a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2457f7915d99SPeter Ujfalusi 
2458f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2459f7915d99SPeter Ujfalusi 		if (reg == reg2)
2460f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2461f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2462f7915d99SPeter Ujfalusi 		else
2463f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2464f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2465f7915d99SPeter Ujfalusi 		if (invert)
2466db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2467a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2468db2a4165SFrank Mandarino 	}
2469db2a4165SFrank Mandarino 
2470db2a4165SFrank Mandarino 	return 0;
2471db2a4165SFrank Mandarino }
2472db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2473db2a4165SFrank Mandarino 
2474db2a4165SFrank Mandarino /**
2475db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2476db2a4165SFrank Mandarino  * @kcontrol: mixer control
2477ac11a2b3SMark Brown  * @ucontrol: control element information
2478db2a4165SFrank Mandarino  *
2479974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2480974815baSPeter Ujfalusi  * control that spans 2 registers.
2481db2a4165SFrank Mandarino  *
2482db2a4165SFrank Mandarino  * Returns 0 for success.
2483db2a4165SFrank Mandarino  */
2484db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2485db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2486db2a4165SFrank Mandarino {
24874eaa9819SJon Smirl 	struct soc_mixer_control *mc =
24884eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2489db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2490815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2491974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2492815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2493815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
24944eaa9819SJon Smirl 	int max = mc->max;
2495815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2496815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2497974815baSPeter Ujfalusi 	int err;
2498974815baSPeter Ujfalusi 	bool type_2r = 0;
2499974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2500974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2501db2a4165SFrank Mandarino 
2502db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2503db2a4165SFrank Mandarino 	if (invert)
2504a7a4ac86SPhilipp Zabel 		val = max - val;
2505db2a4165SFrank Mandarino 	val_mask = mask << shift;
2506db2a4165SFrank Mandarino 	val = val << shift;
2507974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2508db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2509db2a4165SFrank Mandarino 		if (invert)
2510a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2511974815baSPeter Ujfalusi 		if (reg == reg2) {
2512db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2513db2a4165SFrank Mandarino 			val |= val2 << rshift;
2514974815baSPeter Ujfalusi 		} else {
2515db2a4165SFrank Mandarino 			val2 = val2 << shift;
2516974815baSPeter Ujfalusi 			type_2r = 1;
2517974815baSPeter Ujfalusi 		}
2518974815baSPeter Ujfalusi 	}
25196c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
25203ff3f64bSMark Brown 	if (err < 0)
2521db2a4165SFrank Mandarino 		return err;
2522db2a4165SFrank Mandarino 
2523974815baSPeter Ujfalusi 	if (type_2r)
25246c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2525974815baSPeter Ujfalusi 
2526db2a4165SFrank Mandarino 	return err;
2527db2a4165SFrank Mandarino }
2528974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2529db2a4165SFrank Mandarino 
2530e13ac2e9SMark Brown /**
25311d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
25321d99f243SBrian Austin  * @kcontrol: mixer control
25331d99f243SBrian Austin  * @ucontrol: control element information
25341d99f243SBrian Austin  *
25351d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
25361d99f243SBrian Austin  * control that spans 2 registers.
25371d99f243SBrian Austin  *
25381d99f243SBrian Austin  * Returns 0 for success.
25391d99f243SBrian Austin  */
25401d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
25411d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
25421d99f243SBrian Austin {
25431d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
25441d99f243SBrian Austin 	struct soc_mixer_control *mc =
25451d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
25461d99f243SBrian Austin 
25471d99f243SBrian Austin 	unsigned int reg = mc->reg;
25481d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
25491d99f243SBrian Austin 	unsigned int shift = mc->shift;
25501d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
25511d99f243SBrian Austin 	int max = mc->max;
25521d99f243SBrian Austin 	int min = mc->min;
25531d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
25541d99f243SBrian Austin 
25551d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
25561d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
25571d99f243SBrian Austin 
25581d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
25591d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
25601d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
25611d99f243SBrian Austin 
25621d99f243SBrian Austin 	return 0;
25631d99f243SBrian Austin }
25641d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
25651d99f243SBrian Austin 
25661d99f243SBrian Austin /**
25671d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
25681d99f243SBrian Austin  * @kcontrol: mixer control
25691d99f243SBrian Austin  * @uinfo: control element information
25701d99f243SBrian Austin  *
25711d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
25721d99f243SBrian Austin  *
25731d99f243SBrian Austin  * Returns 0 for success.
25741d99f243SBrian Austin  */
25751d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
25761d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
25771d99f243SBrian Austin {
25781d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
25791d99f243SBrian Austin 	struct soc_mixer_control *mc =
25801d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
25811d99f243SBrian Austin 
25821d99f243SBrian Austin 	unsigned int reg = mc->reg;
25831d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
25841d99f243SBrian Austin 	unsigned int shift = mc->shift;
25851d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
25861d99f243SBrian Austin 	int max = mc->max;
25871d99f243SBrian Austin 	int min = mc->min;
25881d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
2589*27f1d759SBrian Austin 	int err = 0;
25901d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
25911d99f243SBrian Austin 
25921d99f243SBrian Austin 	val_mask = mask << shift;
25931d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
25941d99f243SBrian Austin 	val = val << shift;
25951d99f243SBrian Austin 
25961d99f243SBrian Austin 	if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
25971d99f243SBrian Austin 			return err;
25981d99f243SBrian Austin 
25991d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
26001d99f243SBrian Austin 		val_mask = mask << rshift;
26011d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
26021d99f243SBrian Austin 		val2 = val2 << rshift;
26031d99f243SBrian Austin 
26041d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
26051d99f243SBrian Austin 			return err;
26061d99f243SBrian Austin 	}
26071d99f243SBrian Austin 	return 0;
26081d99f243SBrian Austin }
26091d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
26101d99f243SBrian Austin 
26111d99f243SBrian Austin /**
2612e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2613e13ac2e9SMark Brown  * @kcontrol: mixer control
2614e13ac2e9SMark Brown  * @uinfo: control element information
2615e13ac2e9SMark Brown  *
2616e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2617e13ac2e9SMark Brown  *
2618e13ac2e9SMark Brown  * Returns 0 for success.
2619e13ac2e9SMark Brown  */
2620e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2621e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2622e13ac2e9SMark Brown {
26234eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26244eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2625d11bb4a9SPeter Ujfalusi 	int platform_max;
26264eaa9819SJon Smirl 	int min = mc->min;
2627e13ac2e9SMark Brown 
2628d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2629d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2630d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2631d11bb4a9SPeter Ujfalusi 
2632e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2633e13ac2e9SMark Brown 	uinfo->count = 2;
2634e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2635d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2636e13ac2e9SMark Brown 	return 0;
2637e13ac2e9SMark Brown }
2638e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2639e13ac2e9SMark Brown 
2640e13ac2e9SMark Brown /**
2641e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2642e13ac2e9SMark Brown  * @kcontrol: mixer control
2643ac11a2b3SMark Brown  * @ucontrol: control element information
2644e13ac2e9SMark Brown  *
2645e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2646e13ac2e9SMark Brown  *
2647e13ac2e9SMark Brown  * Returns 0 for success.
2648e13ac2e9SMark Brown  */
2649e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2650e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2651e13ac2e9SMark Brown {
26524eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26534eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2654e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2655815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
26564eaa9819SJon Smirl 	int min = mc->min;
2657e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2658e13ac2e9SMark Brown 
2659e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2660e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2661e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2662e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2663e13ac2e9SMark Brown 	return 0;
2664e13ac2e9SMark Brown }
2665e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2666e13ac2e9SMark Brown 
2667e13ac2e9SMark Brown /**
2668e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2669e13ac2e9SMark Brown  * @kcontrol: mixer control
2670ac11a2b3SMark Brown  * @ucontrol: control element information
2671e13ac2e9SMark Brown  *
2672e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2673e13ac2e9SMark Brown  *
2674e13ac2e9SMark Brown  * Returns 0 for success.
2675e13ac2e9SMark Brown  */
2676e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2677e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2678e13ac2e9SMark Brown {
26794eaa9819SJon Smirl 	struct soc_mixer_control *mc =
26804eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2681e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2682815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
26834eaa9819SJon Smirl 	int min = mc->min;
268446f5822fSDaniel Ribeiro 	unsigned int val;
2685e13ac2e9SMark Brown 
2686e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2687e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2688e13ac2e9SMark Brown 
26896c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2690e13ac2e9SMark Brown }
2691e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2692e13ac2e9SMark Brown 
26938c6529dbSLiam Girdwood /**
2694637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2695637d3847SPeter Ujfalusi  *
2696637d3847SPeter Ujfalusi  * @codec: where to look for the control
2697637d3847SPeter Ujfalusi  * @name: Name of the control
2698637d3847SPeter Ujfalusi  * @max: new maximum limit
2699637d3847SPeter Ujfalusi  *
2700637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2701637d3847SPeter Ujfalusi  */
2702637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2703637d3847SPeter Ujfalusi 	const char *name, int max)
2704637d3847SPeter Ujfalusi {
2705f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
2706637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2707637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2708637d3847SPeter Ujfalusi 	int found = 0;
2709637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2710637d3847SPeter Ujfalusi 
2711637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2712637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2713637d3847SPeter Ujfalusi 		return -EINVAL;
2714637d3847SPeter Ujfalusi 
2715637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2716637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2717637d3847SPeter Ujfalusi 			found = 1;
2718637d3847SPeter Ujfalusi 			break;
2719637d3847SPeter Ujfalusi 		}
2720637d3847SPeter Ujfalusi 	}
2721637d3847SPeter Ujfalusi 	if (found) {
2722637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2723637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2724d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2725637d3847SPeter Ujfalusi 			ret = 0;
2726637d3847SPeter Ujfalusi 		}
2727637d3847SPeter Ujfalusi 	}
2728637d3847SPeter Ujfalusi 	return ret;
2729637d3847SPeter Ujfalusi }
2730637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2731637d3847SPeter Ujfalusi 
273271d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
273371d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
273471d08516SMark Brown {
273571d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
273671d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
273771d08516SMark Brown 
273871d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
273971d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
274071d08516SMark Brown 
274171d08516SMark Brown 	return 0;
274271d08516SMark Brown }
274371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
274471d08516SMark Brown 
274571d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
274671d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
274771d08516SMark Brown {
274871d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
274971d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
275071d08516SMark Brown 	int ret;
275171d08516SMark Brown 
275271d08516SMark Brown 	if (codec->using_regmap)
275371d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
275471d08516SMark Brown 				      ucontrol->value.bytes.data,
275571d08516SMark Brown 				      params->num_regs * codec->val_bytes);
275671d08516SMark Brown 	else
275771d08516SMark Brown 		ret = -EINVAL;
275871d08516SMark Brown 
2759f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
2760f831b055SMark Brown 	if (ret == 0 && params->mask) {
2761f831b055SMark Brown 		switch (codec->val_bytes) {
2762f831b055SMark Brown 		case 1:
2763f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
2764f831b055SMark Brown 			break;
2765f831b055SMark Brown 		case 2:
2766f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
2767f831b055SMark Brown 				&= ~params->mask;
2768f831b055SMark Brown 			break;
2769f831b055SMark Brown 		case 4:
2770f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
2771f831b055SMark Brown 				&= ~params->mask;
2772f831b055SMark Brown 			break;
2773f831b055SMark Brown 		default:
2774f831b055SMark Brown 			return -EINVAL;
2775f831b055SMark Brown 		}
2776f831b055SMark Brown 	}
2777f831b055SMark Brown 
277871d08516SMark Brown 	return ret;
277971d08516SMark Brown }
278071d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
278171d08516SMark Brown 
278271d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
278371d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
278471d08516SMark Brown {
278571d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
278671d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2787f831b055SMark Brown 	int ret, len;
2788f831b055SMark Brown 	unsigned int val;
2789f831b055SMark Brown 	void *data;
279071d08516SMark Brown 
2791f831b055SMark Brown 	if (!codec->using_regmap)
2792f831b055SMark Brown 		return -EINVAL;
2793f831b055SMark Brown 
2794f831b055SMark Brown 	data = ucontrol->value.bytes.data;
2795f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
2796f831b055SMark Brown 
2797f831b055SMark Brown 	/*
2798f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
2799f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
2800f831b055SMark Brown 	 * copy.
2801f831b055SMark Brown 	 */
2802f831b055SMark Brown 	if (params->mask) {
2803f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
2804f831b055SMark Brown 		if (ret != 0)
2805f831b055SMark Brown 			return ret;
2806f831b055SMark Brown 
2807f831b055SMark Brown 		val &= params->mask;
2808f831b055SMark Brown 
2809f831b055SMark Brown 		data = kmemdup(data, len, GFP_KERNEL);
2810f831b055SMark Brown 		if (!data)
2811f831b055SMark Brown 			return -ENOMEM;
2812f831b055SMark Brown 
2813f831b055SMark Brown 		switch (codec->val_bytes) {
2814f831b055SMark Brown 		case 1:
2815f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
2816f831b055SMark Brown 			((u8 *)data)[0] |= val;
2817f831b055SMark Brown 			break;
2818f831b055SMark Brown 		case 2:
2819f831b055SMark Brown 			((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2820f831b055SMark Brown 			((u16 *)data)[0] |= cpu_to_be16(val);
2821f831b055SMark Brown 			break;
2822f831b055SMark Brown 		case 4:
2823f831b055SMark Brown 			((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2824f831b055SMark Brown 			((u32 *)data)[0] |= cpu_to_be32(val);
2825f831b055SMark Brown 			break;
2826f831b055SMark Brown 		default:
2827f831b055SMark Brown 			return -EINVAL;
2828f831b055SMark Brown 		}
2829f831b055SMark Brown 	}
2830f831b055SMark Brown 
283171d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
2832f831b055SMark Brown 			       data, len);
2833f831b055SMark Brown 
2834f831b055SMark Brown 	if (params->mask)
2835f831b055SMark Brown 		kfree(data);
283671d08516SMark Brown 
283771d08516SMark Brown 	return ret;
283871d08516SMark Brown }
283971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
284071d08516SMark Brown 
2841b6f4bb38Sapatard@mandriva.com /**
28428c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
28438c6529dbSLiam Girdwood  * @dai: DAI
28448c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
28458c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
28468c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
28478c6529dbSLiam Girdwood  *
28488c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
28498c6529dbSLiam Girdwood  */
28508c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
28518c6529dbSLiam Girdwood 	unsigned int freq, int dir)
28528c6529dbSLiam Girdwood {
2853f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2854f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2855ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2856da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2857ec4ee52aSMark Brown 						      freq, dir);
28588c6529dbSLiam Girdwood 	else
28598c6529dbSLiam Girdwood 		return -EINVAL;
28608c6529dbSLiam Girdwood }
28618c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
28628c6529dbSLiam Girdwood 
28638c6529dbSLiam Girdwood /**
2864ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2865ec4ee52aSMark Brown  * @codec: CODEC
2866ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2867da1c6ea6SMark Brown  * @source: Source for the clock
2868ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2869ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2870ec4ee52aSMark Brown  *
2871ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2872ec4ee52aSMark Brown  */
2873ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2874da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2875ec4ee52aSMark Brown {
2876ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2877da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2878da1c6ea6SMark Brown 						 freq, dir);
2879ec4ee52aSMark Brown 	else
2880ec4ee52aSMark Brown 		return -EINVAL;
2881ec4ee52aSMark Brown }
2882ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2883ec4ee52aSMark Brown 
2884ec4ee52aSMark Brown /**
28858c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
28868c6529dbSLiam Girdwood  * @dai: DAI
2887ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
28888c6529dbSLiam Girdwood  * @div: new clock divisor.
28898c6529dbSLiam Girdwood  *
28908c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
28918c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
28928c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
28938c6529dbSLiam Girdwood  */
28948c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
28958c6529dbSLiam Girdwood 	int div_id, int div)
28968c6529dbSLiam Girdwood {
2897f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2898f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
28998c6529dbSLiam Girdwood 	else
29008c6529dbSLiam Girdwood 		return -EINVAL;
29018c6529dbSLiam Girdwood }
29028c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
29038c6529dbSLiam Girdwood 
29048c6529dbSLiam Girdwood /**
29058c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
29068c6529dbSLiam Girdwood  * @dai: DAI
29078c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
290885488037SMark Brown  * @source: DAI specific source for the PLL
29098c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
29108c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
29118c6529dbSLiam Girdwood  *
29128c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
29138c6529dbSLiam Girdwood  */
291485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
291585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
29168c6529dbSLiam Girdwood {
2917f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2918f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
291985488037SMark Brown 					 freq_in, freq_out);
2920ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2921ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2922ec4ee52aSMark Brown 						   freq_in, freq_out);
29238c6529dbSLiam Girdwood 	else
29248c6529dbSLiam Girdwood 		return -EINVAL;
29258c6529dbSLiam Girdwood }
29268c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
29278c6529dbSLiam Girdwood 
2928ec4ee52aSMark Brown /*
2929ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2930ec4ee52aSMark Brown  * @codec: CODEC
2931ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2932ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2933ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2934ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2935ec4ee52aSMark Brown  *
2936ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2937ec4ee52aSMark Brown  */
2938ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2939ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2940ec4ee52aSMark Brown {
2941ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2942ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2943ec4ee52aSMark Brown 					      freq_in, freq_out);
2944ec4ee52aSMark Brown 	else
2945ec4ee52aSMark Brown 		return -EINVAL;
2946ec4ee52aSMark Brown }
2947ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2948ec4ee52aSMark Brown 
29498c6529dbSLiam Girdwood /**
29508c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
29518c6529dbSLiam Girdwood  * @dai: DAI
29528c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
29538c6529dbSLiam Girdwood  *
29548c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
29558c6529dbSLiam Girdwood  */
29568c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
29578c6529dbSLiam Girdwood {
29585e4ba569SShawn Guo 	if (dai->driver == NULL)
29598c6529dbSLiam Girdwood 		return -EINVAL;
29605e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
29615e4ba569SShawn Guo 		return -ENOTSUPP;
29625e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
29638c6529dbSLiam Girdwood }
29648c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
29658c6529dbSLiam Girdwood 
29668c6529dbSLiam Girdwood /**
29678c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
29688c6529dbSLiam Girdwood  * @dai: DAI
2969a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2970a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
29718c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2972a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
29738c6529dbSLiam Girdwood  *
29748c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
29758c6529dbSLiam Girdwood  * specific.
29768c6529dbSLiam Girdwood  */
29778c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2978a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
29798c6529dbSLiam Girdwood {
2980f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2981f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2982a5479e38SDaniel Ribeiro 				slots, slot_width);
29838c6529dbSLiam Girdwood 	else
29848c6529dbSLiam Girdwood 		return -EINVAL;
29858c6529dbSLiam Girdwood }
29868c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
29878c6529dbSLiam Girdwood 
29888c6529dbSLiam Girdwood /**
2989472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2990472df3cbSBarry Song  * @dai: DAI
2991472df3cbSBarry Song  * @tx_num: how many TX channels
2992472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2993472df3cbSBarry Song  *           0~num-1 uses
2994472df3cbSBarry Song  * @rx_num: how many RX channels
2995472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2996472df3cbSBarry Song  *           0~num-1 uses
2997472df3cbSBarry Song  *
2998472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2999472df3cbSBarry Song  */
3000472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3001472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3002472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3003472df3cbSBarry Song {
3004f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3005f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3006472df3cbSBarry Song 			rx_num, rx_slot);
3007472df3cbSBarry Song 	else
3008472df3cbSBarry Song 		return -EINVAL;
3009472df3cbSBarry Song }
3010472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3011472df3cbSBarry Song 
3012472df3cbSBarry Song /**
30138c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
30148c6529dbSLiam Girdwood  * @dai: DAI
30158c6529dbSLiam Girdwood  * @tristate: tristate enable
30168c6529dbSLiam Girdwood  *
30178c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
30188c6529dbSLiam Girdwood  */
30198c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
30208c6529dbSLiam Girdwood {
3021f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3022f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
30238c6529dbSLiam Girdwood 	else
30248c6529dbSLiam Girdwood 		return -EINVAL;
30258c6529dbSLiam Girdwood }
30268c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
30278c6529dbSLiam Girdwood 
30288c6529dbSLiam Girdwood /**
30298c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
30308c6529dbSLiam Girdwood  * @dai: DAI
30318c6529dbSLiam Girdwood  * @mute: mute enable
30328c6529dbSLiam Girdwood  *
30338c6529dbSLiam Girdwood  * Mutes the DAI DAC.
30348c6529dbSLiam Girdwood  */
30358c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
30368c6529dbSLiam Girdwood {
3037f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->digital_mute)
3038f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
30398c6529dbSLiam Girdwood 	else
30408c6529dbSLiam Girdwood 		return -EINVAL;
30418c6529dbSLiam Girdwood }
30428c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
30438c6529dbSLiam Girdwood 
3044c5af3a2eSMark Brown /**
3045c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3046c5af3a2eSMark Brown  *
3047ac11a2b3SMark Brown  * @card: Card to register
3048c5af3a2eSMark Brown  *
3049c5af3a2eSMark Brown  */
305070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3051c5af3a2eSMark Brown {
3052b19e6e7bSMark Brown 	int i, ret;
3053f0fba2adSLiam Girdwood 
3054c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3055c5af3a2eSMark Brown 		return -EINVAL;
3056c5af3a2eSMark Brown 
30575a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
30585a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
30595a504963SStephen Warren 
30605a504963SStephen Warren 		/*
30615a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
30625a504963SStephen Warren 		 * not both or neither.
30635a504963SStephen Warren 		 */
30645a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
30655a504963SStephen Warren 			dev_err(card->dev,
30663b09bb82SLiam Girdwood 				"Neither/both codec name/of_node are set for %s\n",
30673b09bb82SLiam Girdwood 				link->name);
30685a504963SStephen Warren 			return -EINVAL;
30695a504963SStephen Warren 		}
30705a504963SStephen Warren 
30715a504963SStephen Warren 		/*
30725a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
30735a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
30745a504963SStephen Warren 		 */
30755a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
30765a504963SStephen Warren 			dev_err(card->dev,
30773b09bb82SLiam Girdwood 				"Both platform name/of_node are set for %s\n", link->name);
30785a504963SStephen Warren 			return -EINVAL;
30795a504963SStephen Warren 		}
30805a504963SStephen Warren 
30815a504963SStephen Warren 		/*
30825a504963SStephen Warren 		 * CPU DAI must be specified by 1 of name or OF node,
30835a504963SStephen Warren 		 * not both or neither.
30845a504963SStephen Warren 		 */
30855a504963SStephen Warren 		if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
30865a504963SStephen Warren 			dev_err(card->dev,
30873b09bb82SLiam Girdwood 				"Neither/both cpu_dai name/of_node are set for %s\n",
30883b09bb82SLiam Girdwood 				link->name);
30895a504963SStephen Warren 			return -EINVAL;
30905a504963SStephen Warren 		}
30915a504963SStephen Warren 	}
30925a504963SStephen Warren 
3093ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3094ed77cc12SMark Brown 
3095111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3096111c6419SStephen Warren 
3097150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3098150dd2f8SVinod Koul 
3099181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3100181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
31012eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3102f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3103f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3104f0fba2adSLiam Girdwood 		return -ENOMEM;
31052eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3106f0fba2adSLiam Girdwood 
3107f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3108f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3109f0fba2adSLiam Girdwood 
3110c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3111db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3112c5af3a2eSMark Brown 	card->instantiated = 0;
3113f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3114a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3115c5af3a2eSMark Brown 
3116b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3117b19e6e7bSMark Brown 	if (ret != 0)
3118b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3119c5af3a2eSMark Brown 
3120b19e6e7bSMark Brown 	return ret;
3121c5af3a2eSMark Brown }
312270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3123c5af3a2eSMark Brown 
3124c5af3a2eSMark Brown /**
3125c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3126c5af3a2eSMark Brown  *
3127ac11a2b3SMark Brown  * @card: Card to unregister
3128c5af3a2eSMark Brown  *
3129c5af3a2eSMark Brown  */
313070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3131c5af3a2eSMark Brown {
3132b0e26485SVinod Koul 	if (card->instantiated)
3133b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3134c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3135c5af3a2eSMark Brown 
3136c5af3a2eSMark Brown 	return 0;
3137c5af3a2eSMark Brown }
313870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3139c5af3a2eSMark Brown 
3140f0fba2adSLiam Girdwood /*
3141f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3142f0fba2adSLiam Girdwood  * and sanitizing names.
3143f0fba2adSLiam Girdwood  */
31440b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3145f0fba2adSLiam Girdwood {
3146f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3147f0fba2adSLiam Girdwood 	int id1, id2;
3148f0fba2adSLiam Girdwood 
3149f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3150f0fba2adSLiam Girdwood 		return NULL;
3151f0fba2adSLiam Girdwood 
315258818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3153f0fba2adSLiam Girdwood 
3154f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3155f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3156f0fba2adSLiam Girdwood 	if (found) {
3157f0fba2adSLiam Girdwood 		/* get ID */
3158f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3159f0fba2adSLiam Girdwood 
3160f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3161f0fba2adSLiam Girdwood 			if (*id == -1)
3162f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3163f0fba2adSLiam Girdwood 		}
3164f0fba2adSLiam Girdwood 
3165f0fba2adSLiam Girdwood 	} else {
3166f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3167f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3168f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3169f0fba2adSLiam Girdwood 
3170f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
317105899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3172f0fba2adSLiam Girdwood 
3173f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3174f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
317558818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3176f0fba2adSLiam Girdwood 		} else
3177f0fba2adSLiam Girdwood 			*id = 0;
3178f0fba2adSLiam Girdwood 	}
3179f0fba2adSLiam Girdwood 
3180f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3181f0fba2adSLiam Girdwood }
3182f0fba2adSLiam Girdwood 
3183f0fba2adSLiam Girdwood /*
3184f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3185f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3186f0fba2adSLiam Girdwood  */
3187f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3188f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3189f0fba2adSLiam Girdwood {
3190f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
31910837fc62SFabio Estevam 		pr_err("asoc: error - multiple DAI %s registered with no name\n",
3192f0fba2adSLiam Girdwood 				dev_name(dev));
3193f0fba2adSLiam Girdwood 		return NULL;
3194f0fba2adSLiam Girdwood 	}
3195f0fba2adSLiam Girdwood 
3196f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3197f0fba2adSLiam Girdwood }
3198f0fba2adSLiam Girdwood 
31999115171aSMark Brown /**
32009115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
32019115171aSMark Brown  *
3202ac11a2b3SMark Brown  * @dai: DAI to register
32039115171aSMark Brown  */
3204f0fba2adSLiam Girdwood int snd_soc_register_dai(struct device *dev,
3205f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
32069115171aSMark Brown {
3207f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
32089115171aSMark Brown 
3209f0fba2adSLiam Girdwood 	dev_dbg(dev, "dai register %s\n", dev_name(dev));
32109115171aSMark Brown 
3211f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3212f0fba2adSLiam Girdwood 	if (dai == NULL)
3213f0fba2adSLiam Girdwood 		return -ENOMEM;
32146335d055SEric Miao 
3215f0fba2adSLiam Girdwood 	/* create DAI component name */
3216f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3217f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3218f0fba2adSLiam Girdwood 		kfree(dai);
3219f0fba2adSLiam Girdwood 		return -ENOMEM;
3220f0fba2adSLiam Girdwood 	}
3221f0fba2adSLiam Girdwood 
3222f0fba2adSLiam Girdwood 	dai->dev = dev;
3223f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3224be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3225f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3226f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
32279115171aSMark Brown 
32289115171aSMark Brown 	mutex_lock(&client_mutex);
32299115171aSMark Brown 	list_add(&dai->list, &dai_list);
32309115171aSMark Brown 	mutex_unlock(&client_mutex);
32319115171aSMark Brown 
32329115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
32339115171aSMark Brown 
32349115171aSMark Brown 	return 0;
32359115171aSMark Brown }
32369115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
32379115171aSMark Brown 
32389115171aSMark Brown /**
32399115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
32409115171aSMark Brown  *
3241ac11a2b3SMark Brown  * @dai: DAI to unregister
32429115171aSMark Brown  */
3243f0fba2adSLiam Girdwood void snd_soc_unregister_dai(struct device *dev)
32449115171aSMark Brown {
3245f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3246f0fba2adSLiam Girdwood 
3247f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3248f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3249f0fba2adSLiam Girdwood 			goto found;
3250f0fba2adSLiam Girdwood 	}
3251f0fba2adSLiam Girdwood 	return;
3252f0fba2adSLiam Girdwood 
3253f0fba2adSLiam Girdwood found:
32549115171aSMark Brown 	mutex_lock(&client_mutex);
32559115171aSMark Brown 	list_del(&dai->list);
32569115171aSMark Brown 	mutex_unlock(&client_mutex);
32579115171aSMark Brown 
32589115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
3259f0fba2adSLiam Girdwood 	kfree(dai->name);
3260f0fba2adSLiam Girdwood 	kfree(dai);
32619115171aSMark Brown }
32629115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
32639115171aSMark Brown 
32649115171aSMark Brown /**
32659115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
32669115171aSMark Brown  *
3267ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3268ac11a2b3SMark Brown  * @count: Number of DAIs
32699115171aSMark Brown  */
3270f0fba2adSLiam Girdwood int snd_soc_register_dais(struct device *dev,
3271f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
32729115171aSMark Brown {
3273f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3274f0fba2adSLiam Girdwood 	int i, ret = 0;
3275f0fba2adSLiam Girdwood 
3276720ffa4cSLiam Girdwood 	dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
32779115171aSMark Brown 
32789115171aSMark Brown 	for (i = 0; i < count; i++) {
3279f0fba2adSLiam Girdwood 
3280f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3281c46e0079SAxel Lin 		if (dai == NULL) {
3282c46e0079SAxel Lin 			ret = -ENOMEM;
3283c46e0079SAxel Lin 			goto err;
3284c46e0079SAxel Lin 		}
3285f0fba2adSLiam Girdwood 
3286f0fba2adSLiam Girdwood 		/* create DAI component name */
3287f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3288f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3289f0fba2adSLiam Girdwood 			kfree(dai);
3290f0fba2adSLiam Girdwood 			ret = -EINVAL;
32919115171aSMark Brown 			goto err;
32929115171aSMark Brown 		}
32939115171aSMark Brown 
3294f0fba2adSLiam Girdwood 		dai->dev = dev;
3295f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
32960f9141c9SMark Brown 		if (dai->driver->id)
32970f9141c9SMark Brown 			dai->id = dai->driver->id;
32980f9141c9SMark Brown 		else
32990f9141c9SMark Brown 			dai->id = i;
3300be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
3301f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
3302f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
3303f0fba2adSLiam Girdwood 
3304f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
3305f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
3306f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
3307f0fba2adSLiam Girdwood 
3308f0fba2adSLiam Girdwood 		pr_debug("Registered DAI '%s'\n", dai->name);
3309f0fba2adSLiam Girdwood 	}
3310f0fba2adSLiam Girdwood 
33119115171aSMark Brown 	return 0;
33129115171aSMark Brown 
33139115171aSMark Brown err:
33149115171aSMark Brown 	for (i--; i >= 0; i--)
3315f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33169115171aSMark Brown 
33179115171aSMark Brown 	return ret;
33189115171aSMark Brown }
33199115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
33209115171aSMark Brown 
33219115171aSMark Brown /**
33229115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
33239115171aSMark Brown  *
3324ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
3325ac11a2b3SMark Brown  * @count: Number of DAIs
33269115171aSMark Brown  */
3327f0fba2adSLiam Girdwood void snd_soc_unregister_dais(struct device *dev, size_t count)
33289115171aSMark Brown {
33299115171aSMark Brown 	int i;
33309115171aSMark Brown 
33319115171aSMark Brown 	for (i = 0; i < count; i++)
3332f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
33339115171aSMark Brown }
33349115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
33359115171aSMark Brown 
333612a48a8cSMark Brown /**
333712a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
333812a48a8cSMark Brown  *
3339ac11a2b3SMark Brown  * @platform: platform to register
334012a48a8cSMark Brown  */
3341f0fba2adSLiam Girdwood int snd_soc_register_platform(struct device *dev,
3342f0fba2adSLiam Girdwood 		struct snd_soc_platform_driver *platform_drv)
334312a48a8cSMark Brown {
3344f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
334512a48a8cSMark Brown 
3346f0fba2adSLiam Girdwood 	dev_dbg(dev, "platform register %s\n", dev_name(dev));
3347f0fba2adSLiam Girdwood 
3348f0fba2adSLiam Girdwood 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3349f0fba2adSLiam Girdwood 	if (platform == NULL)
3350f0fba2adSLiam Girdwood 		return -ENOMEM;
3351f0fba2adSLiam Girdwood 
3352f0fba2adSLiam Girdwood 	/* create platform component name */
3353f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
3354f0fba2adSLiam Girdwood 	if (platform->name == NULL) {
3355f0fba2adSLiam Girdwood 		kfree(platform);
3356f0fba2adSLiam Girdwood 		return -ENOMEM;
3357f0fba2adSLiam Girdwood 	}
3358f0fba2adSLiam Girdwood 
3359f0fba2adSLiam Girdwood 	platform->dev = dev;
3360f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3361b7950641SLiam Girdwood 	platform->dapm.dev = dev;
3362b7950641SLiam Girdwood 	platform->dapm.platform = platform;
336364a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
3364cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
336512a48a8cSMark Brown 
336612a48a8cSMark Brown 	mutex_lock(&client_mutex);
336712a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
336812a48a8cSMark Brown 	mutex_unlock(&client_mutex);
336912a48a8cSMark Brown 
337012a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
337112a48a8cSMark Brown 
337212a48a8cSMark Brown 	return 0;
337312a48a8cSMark Brown }
337412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
337512a48a8cSMark Brown 
337612a48a8cSMark Brown /**
337712a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
337812a48a8cSMark Brown  *
3379ac11a2b3SMark Brown  * @platform: platform to unregister
338012a48a8cSMark Brown  */
3381f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
338212a48a8cSMark Brown {
3383f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3384f0fba2adSLiam Girdwood 
3385f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
3386f0fba2adSLiam Girdwood 		if (dev == platform->dev)
3387f0fba2adSLiam Girdwood 			goto found;
3388f0fba2adSLiam Girdwood 	}
3389f0fba2adSLiam Girdwood 	return;
3390f0fba2adSLiam Girdwood 
3391f0fba2adSLiam Girdwood found:
339212a48a8cSMark Brown 	mutex_lock(&client_mutex);
339312a48a8cSMark Brown 	list_del(&platform->list);
339412a48a8cSMark Brown 	mutex_unlock(&client_mutex);
339512a48a8cSMark Brown 
339612a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
3397f0fba2adSLiam Girdwood 	kfree(platform->name);
3398f0fba2adSLiam Girdwood 	kfree(platform);
339912a48a8cSMark Brown }
340012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
340112a48a8cSMark Brown 
3402151ab22cSMark Brown static u64 codec_format_map[] = {
3403151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3404151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3405151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3406151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3407151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3408151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3409151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3410151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3411151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3412151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3413151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3414151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3415151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3416151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3417151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3418151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3419151ab22cSMark Brown };
3420151ab22cSMark Brown 
3421151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3422151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3423151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3424151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3425151ab22cSMark Brown  */
3426151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3427151ab22cSMark Brown {
3428151ab22cSMark Brown 	int i;
3429151ab22cSMark Brown 
3430151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3431151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3432151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3433151ab22cSMark Brown }
3434151ab22cSMark Brown 
34350d0cf00aSMark Brown /**
34360d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
34370d0cf00aSMark Brown  *
3438ac11a2b3SMark Brown  * @codec: codec to register
34390d0cf00aSMark Brown  */
3440f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3441001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3442001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3443001ae4c0SMark Brown 			   int num_dai)
34440d0cf00aSMark Brown {
34453335ddcaSDimitris Papastamos 	size_t reg_size;
3446f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3447f0fba2adSLiam Girdwood 	int ret, i;
3448151ab22cSMark Brown 
3449f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
34500d0cf00aSMark Brown 
3451f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3452f0fba2adSLiam Girdwood 	if (codec == NULL)
3453f0fba2adSLiam Girdwood 		return -ENOMEM;
34540d0cf00aSMark Brown 
3455f0fba2adSLiam Girdwood 	/* create CODEC component name */
3456f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
3457f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
3458f0fba2adSLiam Girdwood 		kfree(codec);
3459f0fba2adSLiam Girdwood 		return -ENOMEM;
3460151ab22cSMark Brown 	}
3461151ab22cSMark Brown 
346223bbce34SDimitris Papastamos 	if (codec_drv->compress_type)
346323bbce34SDimitris Papastamos 		codec->compress_type = codec_drv->compress_type;
346423bbce34SDimitris Papastamos 	else
346523bbce34SDimitris Papastamos 		codec->compress_type = SND_SOC_FLAT_COMPRESSION;
346623bbce34SDimitris Papastamos 
3467c3acec26SMark Brown 	codec->write = codec_drv->write;
3468c3acec26SMark Brown 	codec->read = codec_drv->read;
34691500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
34701500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
34718020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
34725124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3473ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3474ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
3475ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
3476474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
347764a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
3478f0fba2adSLiam Girdwood 	codec->dev = dev;
3479f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3480f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
3481f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3482f0fba2adSLiam Girdwood 
34837a30a3dbSDimitris Papastamos 	/* allocate CODEC register cache */
34847a30a3dbSDimitris Papastamos 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
34853335ddcaSDimitris Papastamos 		reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3486aea170a0SDimitris Papastamos 		codec->reg_size = reg_size;
34873335ddcaSDimitris Papastamos 		/* it is necessary to make a copy of the default register cache
34883335ddcaSDimitris Papastamos 		 * because in the case of using a compression type that requires
34893335ddcaSDimitris Papastamos 		 * the default register cache to be marked as __devinitconst the
34903335ddcaSDimitris Papastamos 		 * kernel might have freed the array by the time we initialize
34913335ddcaSDimitris Papastamos 		 * the cache.
34923335ddcaSDimitris Papastamos 		 */
34932aa86323SDimitris Papastamos 		if (codec_drv->reg_cache_default) {
34943335ddcaSDimitris Papastamos 			codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
34953335ddcaSDimitris Papastamos 						      reg_size, GFP_KERNEL);
34963335ddcaSDimitris Papastamos 			if (!codec->reg_def_copy) {
34973335ddcaSDimitris Papastamos 				ret = -ENOMEM;
3498fdf0f54dSDimitris Papastamos 				goto fail;
34997a30a3dbSDimitris Papastamos 			}
35007a30a3dbSDimitris Papastamos 		}
35012aa86323SDimitris Papastamos 	}
35027a30a3dbSDimitris Papastamos 
35031500b7b5SDimitris Papastamos 	if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
35041500b7b5SDimitris Papastamos 		if (!codec->volatile_register)
35051500b7b5SDimitris Papastamos 			codec->volatile_register = snd_soc_default_volatile_register;
35061500b7b5SDimitris Papastamos 		if (!codec->readable_register)
35071500b7b5SDimitris Papastamos 			codec->readable_register = snd_soc_default_readable_register;
35088020454cSDimitris Papastamos 		if (!codec->writable_register)
35098020454cSDimitris Papastamos 			codec->writable_register = snd_soc_default_writable_register;
35101500b7b5SDimitris Papastamos 	}
35111500b7b5SDimitris Papastamos 
3512f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3513f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3514f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3515f0fba2adSLiam Girdwood 	}
3516f0fba2adSLiam Girdwood 
351726b01ccdSMark Brown 	/* register any DAIs */
351826b01ccdSMark Brown 	if (num_dai) {
3519f0fba2adSLiam Girdwood 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3520f0fba2adSLiam Girdwood 		if (ret < 0)
3521fdf0f54dSDimitris Papastamos 			goto fail;
352226b01ccdSMark Brown 	}
3523f0fba2adSLiam Girdwood 
35240d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35250d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
35260d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35270d0cf00aSMark Brown 
35280d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
35290d0cf00aSMark Brown 	return 0;
3530f0fba2adSLiam Girdwood 
3531fdf0f54dSDimitris Papastamos fail:
35323335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35333335ddcaSDimitris Papastamos 	codec->reg_def_copy = NULL;
3534f0fba2adSLiam Girdwood 	kfree(codec->name);
3535f0fba2adSLiam Girdwood 	kfree(codec);
3536f0fba2adSLiam Girdwood 	return ret;
35370d0cf00aSMark Brown }
35380d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
35390d0cf00aSMark Brown 
35400d0cf00aSMark Brown /**
35410d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
35420d0cf00aSMark Brown  *
3543ac11a2b3SMark Brown  * @codec: codec to unregister
35440d0cf00aSMark Brown  */
3545f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
35460d0cf00aSMark Brown {
3547f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3548f0fba2adSLiam Girdwood 	int i;
3549f0fba2adSLiam Girdwood 
3550f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3551f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3552f0fba2adSLiam Girdwood 			goto found;
3553f0fba2adSLiam Girdwood 	}
3554f0fba2adSLiam Girdwood 	return;
3555f0fba2adSLiam Girdwood 
3556f0fba2adSLiam Girdwood found:
355726b01ccdSMark Brown 	if (codec->num_dai)
3558f0fba2adSLiam Girdwood 		for (i = 0; i < codec->num_dai; i++)
3559f0fba2adSLiam Girdwood 			snd_soc_unregister_dai(dev);
3560f0fba2adSLiam Girdwood 
35610d0cf00aSMark Brown 	mutex_lock(&client_mutex);
35620d0cf00aSMark Brown 	list_del(&codec->list);
35630d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
35640d0cf00aSMark Brown 
35650d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
3566f0fba2adSLiam Girdwood 
35677a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
35683335ddcaSDimitris Papastamos 	kfree(codec->reg_def_copy);
35691aafcd4dSDimitris Papastamos 	kfree(codec->name);
3570f0fba2adSLiam Girdwood 	kfree(codec);
35710d0cf00aSMark Brown }
35720d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
35730d0cf00aSMark Brown 
3574bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3575bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3576bec4fa05SStephen Warren 			       const char *propname)
3577bec4fa05SStephen Warren {
3578bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
3579bec4fa05SStephen Warren 	int ret;
3580bec4fa05SStephen Warren 
3581bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3582bec4fa05SStephen Warren 	/*
3583bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3584bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3585bec4fa05SStephen Warren 	 * snd_soc_register_card.
3586bec4fa05SStephen Warren 	 */
3587bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3588bec4fa05SStephen Warren 		dev_err(card->dev,
3589bec4fa05SStephen Warren 			"Property '%s' could not be read: %d\n",
3590bec4fa05SStephen Warren 			propname, ret);
3591bec4fa05SStephen Warren 		return ret;
3592bec4fa05SStephen Warren 	}
3593bec4fa05SStephen Warren 
3594bec4fa05SStephen Warren 	return 0;
3595bec4fa05SStephen Warren }
3596bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3597bec4fa05SStephen Warren 
3598a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3599a4a54dd5SStephen Warren 				   const char *propname)
3600a4a54dd5SStephen Warren {
3601a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3602a4a54dd5SStephen Warren 	int num_routes;
3603a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3604a4a54dd5SStephen Warren 	int i, ret;
3605a4a54dd5SStephen Warren 
3606a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3607a4a54dd5SStephen Warren 	if (num_routes & 1) {
3608a4a54dd5SStephen Warren 		dev_err(card->dev,
3609a4a54dd5SStephen Warren 			"Property '%s's length is not even\n",
3610a4a54dd5SStephen Warren 			propname);
3611a4a54dd5SStephen Warren 		return -EINVAL;
3612a4a54dd5SStephen Warren 	}
3613a4a54dd5SStephen Warren 	num_routes /= 2;
3614a4a54dd5SStephen Warren 	if (!num_routes) {
3615a4a54dd5SStephen Warren 		dev_err(card->dev,
3616a4a54dd5SStephen Warren 			"Property '%s's length is zero\n",
3617a4a54dd5SStephen Warren 			propname);
3618a4a54dd5SStephen Warren 		return -EINVAL;
3619a4a54dd5SStephen Warren 	}
3620a4a54dd5SStephen Warren 
3621a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3622a4a54dd5SStephen Warren 			      GFP_KERNEL);
3623a4a54dd5SStephen Warren 	if (!routes) {
3624a4a54dd5SStephen Warren 		dev_err(card->dev,
3625a4a54dd5SStephen Warren 			"Could not allocate DAPM route table\n");
3626a4a54dd5SStephen Warren 		return -EINVAL;
3627a4a54dd5SStephen Warren 	}
3628a4a54dd5SStephen Warren 
3629a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3630a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3631a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
3632a4a54dd5SStephen Warren 		if (ret) {
3633a4a54dd5SStephen Warren 			dev_err(card->dev,
3634a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3635a4a54dd5SStephen Warren 				propname, 2 * i, ret);
3636a4a54dd5SStephen Warren 			return -EINVAL;
3637a4a54dd5SStephen Warren 		}
3638a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3639a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
3640a4a54dd5SStephen Warren 		if (ret) {
3641a4a54dd5SStephen Warren 			dev_err(card->dev,
3642a4a54dd5SStephen Warren 				"Property '%s' index %d could not be read: %d\n",
3643a4a54dd5SStephen Warren 				propname, (2 * i) + 1, ret);
3644a4a54dd5SStephen Warren 			return -EINVAL;
3645a4a54dd5SStephen Warren 		}
3646a4a54dd5SStephen Warren 	}
3647a4a54dd5SStephen Warren 
3648a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
3649a4a54dd5SStephen Warren 	card->dapm_routes = routes;
3650a4a54dd5SStephen Warren 
3651a4a54dd5SStephen Warren 	return 0;
3652a4a54dd5SStephen Warren }
3653a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3654a4a54dd5SStephen Warren 
3655c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3656db2a4165SFrank Mandarino {
3657384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
36588a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
36598a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
36600837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
36618a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3662384c89e2SMark Brown 	}
3663c3c5a19aSMark Brown 
36648a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3665c3c5a19aSMark Brown 				 &codec_list_fops))
3666c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3667c3c5a19aSMark Brown 
36688a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3669f3208780SMark Brown 				 &dai_list_fops))
3670f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
367119c7ac27SMark Brown 
36728a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
367319c7ac27SMark Brown 				 &platform_list_fops))
367419c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3675384c89e2SMark Brown #endif
3676384c89e2SMark Brown 
3677fb257897SMark Brown 	snd_soc_util_init();
3678fb257897SMark Brown 
3679db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3680db2a4165SFrank Mandarino }
36814abe8e16SMark Brown module_init(snd_soc_init);
3682db2a4165SFrank Mandarino 
36837d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3684db2a4165SFrank Mandarino {
3685fb257897SMark Brown 	snd_soc_util_exit();
3686fb257897SMark Brown 
3687384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
36888a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3689384c89e2SMark Brown #endif
3690db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3691db2a4165SFrank Mandarino }
3692db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3693db2a4165SFrank Mandarino 
3694db2a4165SFrank Mandarino /* Module information */
3695d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3696db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3697db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
36988b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3699