xref: /openbmc/linux/sound/soc/soc-core.c (revision 6479f15ad7227da99b4bfc2a465cc50a28ff1b5a)
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>
33741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
34f0e8ed85SMark Brown #include <linux/ctype.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
36bec4fa05SStephen Warren #include <linux/of.h>
37741a509fSMarkus Pargmann #include <linux/gpio.h>
38741a509fSMarkus Pargmann #include <linux/of_gpio.h>
39474828a4SMarek Vasut #include <sound/ac97_codec.h>
40db2a4165SFrank Mandarino #include <sound/core.h>
413028eb8cSMark Brown #include <sound/jack.h>
42db2a4165SFrank Mandarino #include <sound/pcm.h>
43db2a4165SFrank Mandarino #include <sound/pcm_params.h>
44db2a4165SFrank Mandarino #include <sound/soc.h>
4501d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
46db2a4165SFrank Mandarino #include <sound/initval.h>
47db2a4165SFrank Mandarino 
48a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
49a8b1d34fSMark Brown #include <trace/events/asoc.h>
50a8b1d34fSMark Brown 
51f0fba2adSLiam Girdwood #define NAME_SIZE	32
52f0fba2adSLiam Girdwood 
53384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
548a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
558a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56384c89e2SMark Brown #endif
57384c89e2SMark Brown 
58c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5912a48a8cSMark Brown static LIST_HEAD(platform_list);
600d0cf00aSMark Brown static LIST_HEAD(codec_list);
61030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
62c5af3a2eSMark Brown 
63db2a4165SFrank Mandarino /*
64db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
65db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
66db2a4165SFrank Mandarino  * between two audio tracks.
67db2a4165SFrank Mandarino  */
68db2a4165SFrank Mandarino static int pmdown_time = 5000;
69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71db2a4165SFrank Mandarino 
72741a509fSMarkus Pargmann struct snd_ac97_reset_cfg {
73741a509fSMarkus Pargmann 	struct pinctrl *pctl;
74741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_reset;
75741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_warm_reset;
76741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_run;
77741a509fSMarkus Pargmann 	int gpio_sdata;
78741a509fSMarkus Pargmann 	int gpio_sync;
79741a509fSMarkus Pargmann 	int gpio_reset;
80741a509fSMarkus Pargmann };
81741a509fSMarkus Pargmann 
822bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
832bc9a81eSDimitris Papastamos  * a particular given value */
842bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
852bc9a81eSDimitris Papastamos {
862bc9a81eSDimitris Papastamos 	int c = 0;
872bc9a81eSDimitris Papastamos 	int i;
882bc9a81eSDimitris Papastamos 
892bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
902bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
912bc9a81eSDimitris Papastamos 			break;
922bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
932bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
942bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
952bc9a81eSDimitris Papastamos 	else
962bc9a81eSDimitris Papastamos 		c /= 8;
972bc9a81eSDimitris Papastamos 	return c;
982bc9a81eSDimitris Papastamos }
992bc9a81eSDimitris Papastamos 
10013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
10113fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
10213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
10313fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
1042624d5faSMark Brown {
10500b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
10600b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
10713fd179fSDimitris Papastamos 	int ret;
10813fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
10913fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
11013fd179fSDimitris Papastamos 
11113fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
11213fd179fSDimitris Papastamos 	 * try to abuse this function */
11313fd179fSDimitris Papastamos 	WARN_ON(len > 63);
11413fd179fSDimitris Papastamos 
11513fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
11613fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
11713fd179fSDimitris Papastamos 		return -EINVAL;
11813fd179fSDimitris Papastamos 
11913fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
12013fd179fSDimitris Papastamos 	if (ret < 0) {
12113fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
12213fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
12313fd179fSDimitris Papastamos 	} else {
12413fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
12513fd179fSDimitris Papastamos 	}
12613fd179fSDimitris Papastamos 
12713fd179fSDimitris Papastamos 	/* prepare the buffer */
12813fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
12913fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
13013fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
13113fd179fSDimitris Papastamos 
13213fd179fSDimitris Papastamos 	return 0;
13313fd179fSDimitris Papastamos }
13413fd179fSDimitris Papastamos 
13513fd179fSDimitris Papastamos /* codec register dump */
13613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
13713fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
13813fd179fSDimitris Papastamos {
13913fd179fSDimitris Papastamos 	int i, step = 1;
1402bc9a81eSDimitris Papastamos 	int wordsize, regsize;
14113fd179fSDimitris Papastamos 	int len;
14213fd179fSDimitris Papastamos 	size_t total = 0;
14313fd179fSDimitris Papastamos 	loff_t p = 0;
1442bc9a81eSDimitris Papastamos 
14500b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
14600b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1472624d5faSMark Brown 
14813fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
14913fd179fSDimitris Papastamos 
150f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1512624d5faSMark Brown 		return 0;
1522624d5faSMark Brown 
153f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
154f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1552624d5faSMark Brown 
156f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157b92d150bSLars-Peter Clausen 		if (!snd_soc_codec_readable_register(codec, i))
1582624d5faSMark Brown 			continue;
159f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
160f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1612624d5faSMark Brown 							 PAGE_SIZE - count, i);
1625164d74dSMark Brown 		} else {
16313fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
16413fd179fSDimitris Papastamos 			 * entries for the default case */
16513fd179fSDimitris Papastamos 			if (p >= pos) {
16613fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1672624d5faSMark Brown 					break;
16813fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
16913fd179fSDimitris Papastamos 				total += len;
17013fd179fSDimitris Papastamos 			}
17113fd179fSDimitris Papastamos 			p += len;
17213fd179fSDimitris Papastamos 		}
1732624d5faSMark Brown 	}
1742624d5faSMark Brown 
17513fd179fSDimitris Papastamos 	total = min(total, count - 1);
1762624d5faSMark Brown 
17713fd179fSDimitris Papastamos 	return total;
1782624d5faSMark Brown }
17913fd179fSDimitris Papastamos 
1802624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1812624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1822624d5faSMark Brown {
18336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
184f0fba2adSLiam Girdwood 
18513fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1862624d5faSMark Brown }
1872624d5faSMark Brown 
1882624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1892624d5faSMark Brown 
190dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
191dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
192dbe21408SMark Brown {
19336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
194dbe21408SMark Brown 
195f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
196dbe21408SMark Brown }
197dbe21408SMark Brown 
198dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
199dbe21408SMark Brown 			       struct device_attribute *attr,
200dbe21408SMark Brown 			       const char *buf, size_t count)
201dbe21408SMark Brown {
20236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
203c593b520SMark Brown 	int ret;
204dbe21408SMark Brown 
205b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
206c593b520SMark Brown 	if (ret)
207c593b520SMark Brown 		return ret;
208dbe21408SMark Brown 
209dbe21408SMark Brown 	return count;
210dbe21408SMark Brown }
211dbe21408SMark Brown 
212dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
213dbe21408SMark Brown 
2142624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2152624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2162624d5faSMark Brown 				   size_t count, loff_t *ppos)
2172624d5faSMark Brown {
2182624d5faSMark Brown 	ssize_t ret;
2192624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
22013fd179fSDimitris Papastamos 	char *buf;
22113fd179fSDimitris Papastamos 
22213fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22313fd179fSDimitris Papastamos 		return -EINVAL;
22413fd179fSDimitris Papastamos 
22513fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2262624d5faSMark Brown 	if (!buf)
2272624d5faSMark Brown 		return -ENOMEM;
22813fd179fSDimitris Papastamos 
22913fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
23013fd179fSDimitris Papastamos 	if (ret >= 0) {
23113fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23213fd179fSDimitris Papastamos 			kfree(buf);
23313fd179fSDimitris Papastamos 			return -EFAULT;
23413fd179fSDimitris Papastamos 		}
23513fd179fSDimitris Papastamos 		*ppos += ret;
23613fd179fSDimitris Papastamos 	}
23713fd179fSDimitris Papastamos 
2382624d5faSMark Brown 	kfree(buf);
2392624d5faSMark Brown 	return ret;
2402624d5faSMark Brown }
2412624d5faSMark Brown 
2422624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2432624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2442624d5faSMark Brown {
2452624d5faSMark Brown 	char buf[32];
24634e268d8SStephen Boyd 	size_t buf_size;
2472624d5faSMark Brown 	char *start = buf;
2482624d5faSMark Brown 	unsigned long reg, value;
2492624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
250b785a492SJingoo Han 	int ret;
2512624d5faSMark Brown 
2522624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2532624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2542624d5faSMark Brown 		return -EFAULT;
2552624d5faSMark Brown 	buf[buf_size] = 0;
2562624d5faSMark Brown 
2572624d5faSMark Brown 	while (*start == ' ')
2582624d5faSMark Brown 		start++;
2592624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2602624d5faSMark Brown 	while (*start == ' ')
2612624d5faSMark Brown 		start++;
262b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
263b785a492SJingoo Han 	if (ret)
264b785a492SJingoo Han 		return ret;
2650d51a9cbSMark Brown 
2660d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
267373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2680d51a9cbSMark Brown 
269e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2702624d5faSMark Brown 	return buf_size;
2712624d5faSMark Brown }
2722624d5faSMark Brown 
2732624d5faSMark Brown static const struct file_operations codec_reg_fops = {
274234e3405SStephen Boyd 	.open = simple_open,
2752624d5faSMark Brown 	.read = codec_reg_read_file,
2762624d5faSMark Brown 	.write = codec_reg_write_file,
2776038f373SArnd Bergmann 	.llseek = default_llseek,
2782624d5faSMark Brown };
2792624d5faSMark Brown 
2802624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2812624d5faSMark Brown {
282d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
283d6ce4cf3SJarkko Nikula 
2846ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
285d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2862624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
28710e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
28810e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec debugfs directory\n");
2892624d5faSMark Brown 		return;
2902624d5faSMark Brown 	}
2912624d5faSMark Brown 
292aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
293aaee8ef1SMark Brown 			    &codec->cache_sync);
294aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
295aaee8ef1SMark Brown 			    &codec->cache_only);
296aaee8ef1SMark Brown 
2972624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2982624d5faSMark Brown 						 codec->debugfs_codec_root,
2992624d5faSMark Brown 						 codec, &codec_reg_fops);
3002624d5faSMark Brown 	if (!codec->debugfs_reg)
30110e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
30210e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
3032624d5faSMark Brown 
3048eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
3052624d5faSMark Brown }
3062624d5faSMark Brown 
3072624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3082624d5faSMark Brown {
3092624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3102624d5faSMark Brown }
3112624d5faSMark Brown 
312731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
313731f1ab2SSebastien Guiriec {
314731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
315731f1ab2SSebastien Guiriec 
316731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
317731f1ab2SSebastien Guiriec 						       debugfs_card_root);
318731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
319731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
320f110bfc7SLiam Girdwood 			"ASoC: Failed to create platform debugfs directory\n");
321731f1ab2SSebastien Guiriec 		return;
322731f1ab2SSebastien Guiriec 	}
323731f1ab2SSebastien Guiriec 
324731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
325731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
326731f1ab2SSebastien Guiriec }
327731f1ab2SSebastien Guiriec 
328731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
329731f1ab2SSebastien Guiriec {
330731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
331731f1ab2SSebastien Guiriec }
332731f1ab2SSebastien Guiriec 
333c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
334c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
335c3c5a19aSMark Brown {
336c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3372b194f9dSMark Brown 	ssize_t len, ret = 0;
338c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
339c3c5a19aSMark Brown 
340c3c5a19aSMark Brown 	if (!buf)
341c3c5a19aSMark Brown 		return -ENOMEM;
342c3c5a19aSMark Brown 
3432b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3442b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
345c3c5a19aSMark Brown 			       codec->name);
3462b194f9dSMark Brown 		if (len >= 0)
3472b194f9dSMark Brown 			ret += len;
3482b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3492b194f9dSMark Brown 			ret = PAGE_SIZE;
3502b194f9dSMark Brown 			break;
3512b194f9dSMark Brown 		}
3522b194f9dSMark Brown 	}
353c3c5a19aSMark Brown 
354c3c5a19aSMark Brown 	if (ret >= 0)
355c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
356c3c5a19aSMark Brown 
357c3c5a19aSMark Brown 	kfree(buf);
358c3c5a19aSMark Brown 
359c3c5a19aSMark Brown 	return ret;
360c3c5a19aSMark Brown }
361c3c5a19aSMark Brown 
362c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
363c3c5a19aSMark Brown 	.read = codec_list_read_file,
364c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
365c3c5a19aSMark Brown };
366c3c5a19aSMark Brown 
367f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
368f3208780SMark Brown 				  size_t count, loff_t *ppos)
369f3208780SMark Brown {
370f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3712b194f9dSMark Brown 	ssize_t len, ret = 0;
3721438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
373f3208780SMark Brown 	struct snd_soc_dai *dai;
374f3208780SMark Brown 
375f3208780SMark Brown 	if (!buf)
376f3208780SMark Brown 		return -ENOMEM;
377f3208780SMark Brown 
3781438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3791438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3801438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3811438c2f6SLars-Peter Clausen 				dai->name);
3822b194f9dSMark Brown 			if (len >= 0)
3832b194f9dSMark Brown 				ret += len;
3842b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3852b194f9dSMark Brown 				ret = PAGE_SIZE;
3862b194f9dSMark Brown 				break;
3872b194f9dSMark Brown 			}
3882b194f9dSMark Brown 		}
3891438c2f6SLars-Peter Clausen 	}
390f3208780SMark Brown 
391f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
392f3208780SMark Brown 
393f3208780SMark Brown 	kfree(buf);
394f3208780SMark Brown 
395f3208780SMark Brown 	return ret;
396f3208780SMark Brown }
397f3208780SMark Brown 
398f3208780SMark Brown static const struct file_operations dai_list_fops = {
399f3208780SMark Brown 	.read = dai_list_read_file,
400f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
401f3208780SMark Brown };
402f3208780SMark Brown 
40319c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
40419c7ac27SMark Brown 				       char __user *user_buf,
40519c7ac27SMark Brown 				       size_t count, loff_t *ppos)
40619c7ac27SMark Brown {
40719c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4082b194f9dSMark Brown 	ssize_t len, ret = 0;
40919c7ac27SMark Brown 	struct snd_soc_platform *platform;
41019c7ac27SMark Brown 
41119c7ac27SMark Brown 	if (!buf)
41219c7ac27SMark Brown 		return -ENOMEM;
41319c7ac27SMark Brown 
4142b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4152b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
41619c7ac27SMark Brown 			       platform->name);
4172b194f9dSMark Brown 		if (len >= 0)
4182b194f9dSMark Brown 			ret += len;
4192b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4202b194f9dSMark Brown 			ret = PAGE_SIZE;
4212b194f9dSMark Brown 			break;
4222b194f9dSMark Brown 		}
4232b194f9dSMark Brown 	}
42419c7ac27SMark Brown 
42519c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
42619c7ac27SMark Brown 
42719c7ac27SMark Brown 	kfree(buf);
42819c7ac27SMark Brown 
42919c7ac27SMark Brown 	return ret;
43019c7ac27SMark Brown }
43119c7ac27SMark Brown 
43219c7ac27SMark Brown static const struct file_operations platform_list_fops = {
43319c7ac27SMark Brown 	.read = platform_list_read_file,
43419c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
43519c7ac27SMark Brown };
43619c7ac27SMark Brown 
437a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
438a6052154SJarkko Nikula {
439a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4408a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4413a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
442a6052154SJarkko Nikula 		dev_warn(card->dev,
4437c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4443a45b867SJarkko Nikula 		return;
4453a45b867SJarkko Nikula 	}
4463a45b867SJarkko Nikula 
4473a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4483a45b867SJarkko Nikula 						    card->debugfs_card_root,
4493a45b867SJarkko Nikula 						    &card->pop_time);
4503a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4513a45b867SJarkko Nikula 		dev_warn(card->dev,
452f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
453a6052154SJarkko Nikula }
454a6052154SJarkko Nikula 
455a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
456a6052154SJarkko Nikula {
457a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
458a6052154SJarkko Nikula }
459a6052154SJarkko Nikula 
4602624d5faSMark Brown #else
4612624d5faSMark Brown 
4622624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4632624d5faSMark Brown {
4642624d5faSMark Brown }
4652624d5faSMark Brown 
4662624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4672624d5faSMark Brown {
4682624d5faSMark Brown }
469b95fccbcSAxel Lin 
470731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
471731f1ab2SSebastien Guiriec {
472731f1ab2SSebastien Guiriec }
473731f1ab2SSebastien Guiriec 
474731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
475731f1ab2SSebastien Guiriec {
476731f1ab2SSebastien Guiriec }
477731f1ab2SSebastien Guiriec 
478b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
479b95fccbcSAxel Lin {
480b95fccbcSAxel Lin }
481b95fccbcSAxel Lin 
482b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
483b95fccbcSAxel Lin {
484b95fccbcSAxel Lin }
4852624d5faSMark Brown #endif
4862624d5faSMark Brown 
48747c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
48847c88fffSLiam Girdwood 		const char *dai_link, int stream)
48947c88fffSLiam Girdwood {
49047c88fffSLiam Girdwood 	int i;
49147c88fffSLiam Girdwood 
49247c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
49347c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
49447c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
49547c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
49647c88fffSLiam Girdwood 	}
497f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
49847c88fffSLiam Girdwood 	return NULL;
49947c88fffSLiam Girdwood }
50047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
50147c88fffSLiam Girdwood 
50247c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
50347c88fffSLiam Girdwood 		const char *dai_link)
50447c88fffSLiam Girdwood {
50547c88fffSLiam Girdwood 	int i;
50647c88fffSLiam Girdwood 
50747c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
50847c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
50947c88fffSLiam Girdwood 			return &card->rtd[i];
51047c88fffSLiam Girdwood 	}
511f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
51247c88fffSLiam Girdwood 	return NULL;
51347c88fffSLiam Girdwood }
51447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
51547c88fffSLiam Girdwood 
516db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
517db2a4165SFrank Mandarino /* unregister ac97 codec */
518db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
519db2a4165SFrank Mandarino {
520db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
521db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
522db2a4165SFrank Mandarino 	return 0;
523db2a4165SFrank Mandarino }
524db2a4165SFrank Mandarino 
525db2a4165SFrank Mandarino /* stop no dev release warning */
526db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
527db2a4165SFrank Mandarino 
528db2a4165SFrank Mandarino /* register ac97 codec to bus */
529db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
530db2a4165SFrank Mandarino {
531db2a4165SFrank Mandarino 	int err;
532db2a4165SFrank Mandarino 
533db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
5344ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
535db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
536db2a4165SFrank Mandarino 
537bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
538f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
539db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
540db2a4165SFrank Mandarino 	if (err < 0) {
541f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
542db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
543db2a4165SFrank Mandarino 		return err;
544db2a4165SFrank Mandarino 	}
545db2a4165SFrank Mandarino 	return 0;
546db2a4165SFrank Mandarino }
547db2a4165SFrank Mandarino #endif
548db2a4165SFrank Mandarino 
5499d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5509d58a077SRichard Fitzgerald {
5519d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5529d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5539d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5549d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5559d58a077SRichard Fitzgerald 	 */
5569d58a077SRichard Fitzgerald }
5579d58a077SRichard Fitzgerald 
5586f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
559db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5606f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
561db2a4165SFrank Mandarino {
5626f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5632eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
564db2a4165SFrank Mandarino 	int i;
565db2a4165SFrank Mandarino 
566e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
567e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
568e3509ff0SDaniel Mack 	 */
569f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
570e3509ff0SDaniel Mack 		return 0;
571e3509ff0SDaniel Mack 
5726ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5736ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5746ed25978SAndy Green 	 */
575f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
576f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
577f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5786ed25978SAndy Green 
5796ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
580f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5816ed25978SAndy Green 
582a00f90f9SMark Brown 	/* mute any active DACs */
583f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
584f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
585f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5863efab7dcSMark Brown 
587f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5883efab7dcSMark Brown 			continue;
5893efab7dcSMark Brown 
590f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
591f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
592db2a4165SFrank Mandarino 	}
593db2a4165SFrank Mandarino 
5944ccab3e7SLiam Girdwood 	/* suspend all pcms */
595f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
596f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5973efab7dcSMark Brown 			continue;
5983efab7dcSMark Brown 
599f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
6003efab7dcSMark Brown 	}
6014ccab3e7SLiam Girdwood 
60287506549SMark Brown 	if (card->suspend_pre)
60370b2ac12SMark Brown 		card->suspend_pre(card);
604db2a4165SFrank Mandarino 
605f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
606f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
607f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
6083efab7dcSMark Brown 
609f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6103efab7dcSMark Brown 			continue;
6113efab7dcSMark Brown 
612f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
613f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
614f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
615f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
616f0fba2adSLiam Girdwood 			platform->suspended = 1;
617f0fba2adSLiam Girdwood 		}
618db2a4165SFrank Mandarino 	}
619db2a4165SFrank Mandarino 
620db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
621f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
62243829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
623ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
624f0fba2adSLiam Girdwood 	}
625db2a4165SFrank Mandarino 
626f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6273efab7dcSMark Brown 
628f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6293efab7dcSMark Brown 			continue;
6303efab7dcSMark Brown 
6317bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6327bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
633db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
634f0fba2adSLiam Girdwood 
6357bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6367bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
637db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
638db2a4165SFrank Mandarino 	}
639db2a4165SFrank Mandarino 
640e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
641e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
642e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
643e2d32ff6SMark Brown 
644f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6452eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6461547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6471547aba9SMark Brown 		 * bias _ON and should not be suspended. */
648f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
649ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6501547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
651125a25daSMark Brown 				/*
652125a25daSMark Brown 				 * If the CODEC is capable of idle
653125a25daSMark Brown 				 * bias off then being in STANDBY
654125a25daSMark Brown 				 * means it's doing something,
655125a25daSMark Brown 				 * otherwise fall through.
656125a25daSMark Brown 				 */
657125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
658125a25daSMark Brown 					dev_dbg(codec->dev,
65910e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
660125a25daSMark Brown 					break;
661125a25daSMark Brown 				}
6621547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
66384b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
664f0fba2adSLiam Girdwood 				codec->suspended = 1;
6657be4ba24SMark Brown 				codec->cache_sync = 1;
666da8b8e0fSMark Brown 				if (codec->using_regmap)
667da8b8e0fSMark Brown 					regcache_mark_dirty(codec->control_data);
668988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
669988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6701547aba9SMark Brown 				break;
6711547aba9SMark Brown 			default:
67210e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
67310e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6741547aba9SMark Brown 				break;
6751547aba9SMark Brown 			}
6761547aba9SMark Brown 		}
677f0fba2adSLiam Girdwood 	}
678db2a4165SFrank Mandarino 
679f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
680f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6813efab7dcSMark Brown 
682f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6833efab7dcSMark Brown 			continue;
6843efab7dcSMark Brown 
685f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
686f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
687988e8cc4SNicolin Chen 
688988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
689988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
690db2a4165SFrank Mandarino 	}
691db2a4165SFrank Mandarino 
69287506549SMark Brown 	if (card->suspend_post)
69370b2ac12SMark Brown 		card->suspend_post(card);
694db2a4165SFrank Mandarino 
695db2a4165SFrank Mandarino 	return 0;
696db2a4165SFrank Mandarino }
6976f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
698db2a4165SFrank Mandarino 
6996ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
7006ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
7016ed25978SAndy Green  */
7026ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
703db2a4165SFrank Mandarino {
704f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
705f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7062eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
707db2a4165SFrank Mandarino 	int i;
708db2a4165SFrank Mandarino 
7096ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7106ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7116ed25978SAndy Green 	 */
7126ed25978SAndy Green 
713f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7146ed25978SAndy Green 
7159949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
716f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7179949788bSMark Brown 
71887506549SMark Brown 	if (card->resume_pre)
71970b2ac12SMark Brown 		card->resume_pre(card);
720db2a4165SFrank Mandarino 
721f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
722f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
723f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7243efab7dcSMark Brown 
725f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7263efab7dcSMark Brown 			continue;
7273efab7dcSMark Brown 
728f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
729f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
730db2a4165SFrank Mandarino 	}
731db2a4165SFrank Mandarino 
7322eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7331547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7341547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7351547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7361547aba9SMark Brown 		 */
737f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
738ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7391547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7401547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
741f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
742f0fba2adSLiam Girdwood 				codec->suspended = 0;
7431547aba9SMark Brown 				break;
7441547aba9SMark Brown 			default:
74510e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
74610e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7471547aba9SMark Brown 				break;
7481547aba9SMark Brown 			}
7491547aba9SMark Brown 		}
750f0fba2adSLiam Girdwood 	}
751db2a4165SFrank Mandarino 
752f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7533efab7dcSMark Brown 
754f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7553efab7dcSMark Brown 			continue;
7563efab7dcSMark Brown 
7577bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
758d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
759db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
760f0fba2adSLiam Girdwood 
7617bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
762d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
763db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
764db2a4165SFrank Mandarino 	}
765db2a4165SFrank Mandarino 
7663ff3f64bSMark Brown 	/* unmute any active DACs */
767f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
768f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
769f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7703efab7dcSMark Brown 
771f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7723efab7dcSMark Brown 			continue;
7733efab7dcSMark Brown 
774f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
775f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
776db2a4165SFrank Mandarino 	}
777db2a4165SFrank Mandarino 
778f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
779f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
780f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7813efab7dcSMark Brown 
782f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7833efab7dcSMark Brown 			continue;
7843efab7dcSMark Brown 
785f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
786f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
787f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
788f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
789f0fba2adSLiam Girdwood 			platform->suspended = 0;
790f0fba2adSLiam Girdwood 		}
791db2a4165SFrank Mandarino 	}
792db2a4165SFrank Mandarino 
79387506549SMark Brown 	if (card->resume_post)
79470b2ac12SMark Brown 		card->resume_post(card);
795db2a4165SFrank Mandarino 
796f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7976ed25978SAndy Green 
7986ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
799f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
800e2d32ff6SMark Brown 
801e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
802e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
803e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8046ed25978SAndy Green }
8056ed25978SAndy Green 
8066ed25978SAndy Green /* powers up audio subsystem after a suspend */
8076f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8086ed25978SAndy Green {
8096f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
81082e14e8bSStephen Warren 	int i, ac97_control = 0;
811b9dd94a8SPeter Ujfalusi 
8125ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
8135ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
8145ff1ddf2SEric Miao 	 */
8155ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
8165ff1ddf2SEric Miao 		return 0;
8175ff1ddf2SEric Miao 
818988e8cc4SNicolin Chen 	/* activate pins from sleep state */
819988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
820988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
821988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
822988e8cc4SNicolin Chen 		if (cpu_dai->active)
823988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
824988e8cc4SNicolin Chen 		if (codec_dai->active)
825988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(codec_dai->dev);
826988e8cc4SNicolin Chen 	}
827988e8cc4SNicolin Chen 
82864ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
82964ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
83064ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
83164ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
83264ab9baaSMark Brown 	 */
833f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
834f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
83582e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
83682e14e8bSStephen Warren 	}
83782e14e8bSStephen Warren 	if (ac97_control) {
838f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
83964ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
84064ab9baaSMark Brown 	} else {
841f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8426308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
843f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
844f0fba2adSLiam Girdwood 	}
8456ed25978SAndy Green 
846db2a4165SFrank Mandarino 	return 0;
847db2a4165SFrank Mandarino }
8486f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
849db2a4165SFrank Mandarino #else
8506f8ab4acSMark Brown #define snd_soc_suspend NULL
8516f8ab4acSMark Brown #define snd_soc_resume NULL
852db2a4165SFrank Mandarino #endif
853db2a4165SFrank Mandarino 
85485e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
85502a06d30SBarry Song };
85602a06d30SBarry Song 
857f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
858db2a4165SFrank Mandarino {
859f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
860f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
8611438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
862fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
863435c5e25SMark Brown 	struct snd_soc_platform *platform;
864f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
865848dd8beSMark Brown 	const char *platform_name;
866db2a4165SFrank Mandarino 
867f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
8686308419aSMark Brown 
869b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
8701438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
871bc92657aSStephen Warren 		if (dai_link->cpu_of_node &&
8721438c2f6SLars-Peter Clausen 			component->dev->of_node != dai_link->cpu_of_node)
8735a504963SStephen Warren 			continue;
874bc92657aSStephen Warren 		if (dai_link->cpu_name &&
8751438c2f6SLars-Peter Clausen 			strcmp(dev_name(component->dev), dai_link->cpu_name))
8762610ab77SStephen Warren 			continue;
8771438c2f6SLars-Peter Clausen 		list_for_each_entry(cpu_dai, &component->dai_list, list) {
878bc92657aSStephen Warren 			if (dai_link->cpu_dai_name &&
879bc92657aSStephen Warren 				strcmp(cpu_dai->name, dai_link->cpu_dai_name))
880bc92657aSStephen Warren 				continue;
8812610ab77SStephen Warren 
882f0fba2adSLiam Girdwood 			rtd->cpu_dai = cpu_dai;
883f0fba2adSLiam Girdwood 		}
8841438c2f6SLars-Peter Clausen 	}
885b19e6e7bSMark Brown 
886b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
887f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
888f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
889b19e6e7bSMark Brown 		return -EPROBE_DEFER;
890c5af3a2eSMark Brown 	}
891c5af3a2eSMark Brown 
892b19e6e7bSMark Brown 	/* Find CODEC from registered CODECs */
893f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8945a504963SStephen Warren 		if (dai_link->codec_of_node) {
8955a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8965a504963SStephen Warren 				continue;
8975a504963SStephen Warren 		} else {
8982610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8992610ab77SStephen Warren 				continue;
9005a504963SStephen Warren 		}
9012610ab77SStephen Warren 
902f0fba2adSLiam Girdwood 		rtd->codec = codec;
903f0fba2adSLiam Girdwood 
9042610ab77SStephen Warren 		/*
9052610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
9062610ab77SStephen Warren 		 * this CODEC
9072610ab77SStephen Warren 		 */
9081438c2f6SLars-Peter Clausen 		list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
9091438c2f6SLars-Peter Clausen 			if (!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
910f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
9111438c2f6SLars-Peter Clausen 				break;
912435c5e25SMark Brown 			}
913f0fba2adSLiam Girdwood 		}
914b19e6e7bSMark Brown 
915b19e6e7bSMark Brown 		if (!rtd->codec_dai) {
916f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
917f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
918b19e6e7bSMark Brown 			return -EPROBE_DEFER;
919f0fba2adSLiam Girdwood 		}
920b19e6e7bSMark Brown 	}
921b19e6e7bSMark Brown 
922b19e6e7bSMark Brown 	if (!rtd->codec) {
923f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CODEC %s not registered\n",
924f0fba2adSLiam Girdwood 			dai_link->codec_name);
925b19e6e7bSMark Brown 		return -EPROBE_DEFER;
926b19e6e7bSMark Brown 	}
927848dd8beSMark Brown 
928848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
929848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9305a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
931848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
932848dd8beSMark Brown 
933b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
934f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9355a504963SStephen Warren 		if (dai_link->platform_of_node) {
9365a504963SStephen Warren 			if (platform->dev->of_node !=
9375a504963SStephen Warren 			    dai_link->platform_of_node)
9385a504963SStephen Warren 				continue;
9395a504963SStephen Warren 		} else {
9402610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
9412610ab77SStephen Warren 				continue;
9425a504963SStephen Warren 		}
9432610ab77SStephen Warren 
944f0fba2adSLiam Girdwood 		rtd->platform = platform;
945f0fba2adSLiam Girdwood 	}
946b19e6e7bSMark Brown 	if (!rtd->platform) {
947f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
948f0fba2adSLiam Girdwood 			dai_link->platform_name);
949b19e6e7bSMark Brown 		return -EPROBE_DEFER;
950f0fba2adSLiam Girdwood 	}
951b19e6e7bSMark Brown 
952b19e6e7bSMark Brown 	card->num_rtd++;
953b19e6e7bSMark Brown 
954b19e6e7bSMark Brown 	return 0;
9556b05eda6SMark Brown }
9566b05eda6SMark Brown 
957d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform)
958d12cd198SStephen Warren {
959d12cd198SStephen Warren 	int ret;
960d12cd198SStephen Warren 
961d12cd198SStephen Warren 	if (platform->driver->remove) {
962d12cd198SStephen Warren 		ret = platform->driver->remove(platform);
963d12cd198SStephen Warren 		if (ret < 0)
964f110bfc7SLiam Girdwood 			dev_err(platform->dev, "ASoC: failed to remove %d\n",
965f110bfc7SLiam Girdwood 				ret);
966d12cd198SStephen Warren 	}
967d12cd198SStephen Warren 
968d12cd198SStephen Warren 	/* Make sure all DAPM widgets are freed */
969d12cd198SStephen Warren 	snd_soc_dapm_free(&platform->dapm);
970d12cd198SStephen Warren 
971d12cd198SStephen Warren 	soc_cleanup_platform_debugfs(platform);
972d12cd198SStephen Warren 	platform->probed = 0;
973d12cd198SStephen Warren 	list_del(&platform->card_list);
974d12cd198SStephen Warren 	module_put(platform->dev->driver->owner);
975d12cd198SStephen Warren 
976d12cd198SStephen Warren 	return 0;
977d12cd198SStephen Warren }
978d12cd198SStephen Warren 
979589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
980589c3563SJarkko Nikula {
981589c3563SJarkko Nikula 	int err;
982589c3563SJarkko Nikula 
983589c3563SJarkko Nikula 	if (codec->driver->remove) {
984589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
985589c3563SJarkko Nikula 		if (err < 0)
986f110bfc7SLiam Girdwood 			dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
987589c3563SJarkko Nikula 	}
988589c3563SJarkko Nikula 
989589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
990589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
991589c3563SJarkko Nikula 
992589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
993589c3563SJarkko Nikula 	codec->probed = 0;
994589c3563SJarkko Nikula 	list_del(&codec->card_list);
995589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
996589c3563SJarkko Nikula }
997589c3563SJarkko Nikula 
99862ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
999f0fba2adSLiam Girdwood {
1000f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1001f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1002f0fba2adSLiam Girdwood 	int err;
1003f0fba2adSLiam Girdwood 
1004f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1005f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
100636ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
100736ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
100836ae1a96SMark Brown 		device_unregister(rtd->dev);
1009f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
101002a06d30SBarry Song 	}
101102a06d30SBarry Song 
1012f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
10130168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
10140168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
1015f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1016f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1017f0fba2adSLiam Girdwood 			if (err < 0)
1018f110bfc7SLiam Girdwood 				dev_err(codec_dai->dev,
1019f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
10200837fc62SFabio Estevam 					codec_dai->name, err);
1021f0fba2adSLiam Girdwood 		}
1022f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1023f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1024f0fba2adSLiam Girdwood 	}
1025f0fba2adSLiam Girdwood 
1026f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
10270168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
10280168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
1029f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1030f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1031f0fba2adSLiam Girdwood 			if (err < 0)
1032f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1033f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
10340837fc62SFabio Estevam 					cpu_dai->name, err);
1035f0fba2adSLiam Girdwood 		}
1036f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1037f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1038a9db7dbeSStephen Warren 
103918d75644SStephen Warren 		if (!cpu_dai->codec) {
104018d75644SStephen Warren 			snd_soc_dapm_free(&cpu_dai->dapm);
1041f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
1042f0fba2adSLiam Girdwood 		}
1043f0fba2adSLiam Girdwood 	}
104418d75644SStephen Warren }
1045f0fba2adSLiam Girdwood 
104662ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
104762ae68faSStephen Warren 				       int order)
104862ae68faSStephen Warren {
104962ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
105062ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105162ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
105262ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
105362ae68faSStephen Warren 	struct snd_soc_codec *codec;
105462ae68faSStephen Warren 
105562ae68faSStephen Warren 	/* remove the platform */
105662ae68faSStephen Warren 	if (platform && platform->probed &&
105762ae68faSStephen Warren 	    platform->driver->remove_order == order) {
105862ae68faSStephen Warren 		soc_remove_platform(platform);
105962ae68faSStephen Warren 	}
106062ae68faSStephen Warren 
106162ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
106262ae68faSStephen Warren 	if (codec_dai) {
106362ae68faSStephen Warren 		codec = codec_dai->codec;
106462ae68faSStephen Warren 		if (codec && codec->probed &&
106562ae68faSStephen Warren 		    codec->driver->remove_order == order)
106662ae68faSStephen Warren 			soc_remove_codec(codec);
106762ae68faSStephen Warren 	}
106862ae68faSStephen Warren 
106962ae68faSStephen Warren 	/* remove any CPU-side CODEC */
107062ae68faSStephen Warren 	if (cpu_dai) {
107162ae68faSStephen Warren 		codec = cpu_dai->codec;
107262ae68faSStephen Warren 		if (codec && codec->probed &&
107362ae68faSStephen Warren 		    codec->driver->remove_order == order)
107462ae68faSStephen Warren 			soc_remove_codec(codec);
107562ae68faSStephen Warren 	}
107662ae68faSStephen Warren }
107762ae68faSStephen Warren 
10780671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10790671fd8eSKuninori Morimoto {
10800168bf0dSLiam Girdwood 	int dai, order;
10810671fd8eSKuninori Morimoto 
10820168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10830168bf0dSLiam Girdwood 			order++) {
10840168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
108562ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10860168bf0dSLiam Girdwood 	}
108762ae68faSStephen Warren 
108862ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
108962ae68faSStephen Warren 			order++) {
109062ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
109162ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
109262ae68faSStephen Warren 	}
109362ae68faSStephen Warren 
10940671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10950671fd8eSKuninori Morimoto }
10960671fd8eSKuninori Morimoto 
1097ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1098ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1099ead9b919SJarkko Nikula {
1100ead9b919SJarkko Nikula 	int i;
1101ead9b919SJarkko Nikula 
1102ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1103ead9b919SJarkko Nikula 		return;
1104ead9b919SJarkko Nikula 
1105ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1106ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1107ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1108ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1109ead9b919SJarkko Nikula 			break;
1110ead9b919SJarkko Nikula 		}
1111ead9b919SJarkko Nikula 	}
1112ead9b919SJarkko Nikula }
1113ead9b919SJarkko Nikula 
1114589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1115589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1116589c3563SJarkko Nikula {
1117589c3563SJarkko Nikula 	int ret = 0;
111889b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1119888df395SMark Brown 	struct snd_soc_dai *dai;
1120589c3563SJarkko Nikula 
1121589c3563SJarkko Nikula 	codec->card = card;
1122589c3563SJarkko Nikula 	codec->dapm.card = card;
1123589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1124589c3563SJarkko Nikula 
112570d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
112670d29331SJarkko Nikula 		return -ENODEV;
112770d29331SJarkko Nikula 
1128d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1129d5d1e0beSLars-Peter Clausen 
113077530150SLars-Peter Clausen 	if (driver->dapm_widgets)
113177530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
113277530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
113377530150SLars-Peter Clausen 
1134888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
11351438c2f6SLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
1136888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1137888df395SMark Brown 
113833c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
113933c5f969SMark Brown 
1140a32c17b8SXiubo Li 	if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1141fe2265e4SXiubo Li 		/* Set the default I/O up try regmap */
1142a32c17b8SXiubo Li 		ret = snd_soc_codec_set_cache_io(codec, NULL);
1143a32c17b8SXiubo Li 		if (ret < 0) {
1144a32c17b8SXiubo Li 			dev_err(codec->dev,
1145a32c17b8SXiubo Li 				"Failed to set cache I/O: %d\n", ret);
1146a32c17b8SXiubo Li 			goto err_probe;
1147a32c17b8SXiubo Li 		}
1148a32c17b8SXiubo Li 	}
1149fe2265e4SXiubo Li 
115089b95ac0SMark Brown 	if (driver->probe) {
115189b95ac0SMark Brown 		ret = driver->probe(codec);
1152589c3563SJarkko Nikula 		if (ret < 0) {
1153589c3563SJarkko Nikula 			dev_err(codec->dev,
1154f110bfc7SLiam Girdwood 				"ASoC: failed to probe CODEC %d\n", ret);
115570d29331SJarkko Nikula 			goto err_probe;
1156589c3563SJarkko Nikula 		}
1157ff541f4bSChuansheng Liu 		WARN(codec->dapm.idle_bias_off &&
1158ff541f4bSChuansheng Liu 			codec->dapm.bias_level != SND_SOC_BIAS_OFF,
115910e8aa9aSMichał Mirosław 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
116010e8aa9aSMichał Mirosław 			codec->name);
1161589c3563SJarkko Nikula 	}
1162589c3563SJarkko Nikula 
1163b7af1dafSMark Brown 	if (driver->controls)
1164022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1165b7af1dafSMark Brown 				     driver->num_controls);
116689b95ac0SMark Brown 	if (driver->dapm_routes)
116789b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
116889b95ac0SMark Brown 					driver->num_dapm_routes);
116989b95ac0SMark Brown 
1170589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1171589c3563SJarkko Nikula 	codec->probed = 1;
1172589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
11737be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1174589c3563SJarkko Nikula 
117570d29331SJarkko Nikula 	return 0;
117670d29331SJarkko Nikula 
117770d29331SJarkko Nikula err_probe:
1178d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
117970d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
118070d29331SJarkko Nikula 
1181589c3563SJarkko Nikula 	return ret;
1182589c3563SJarkko Nikula }
1183589c3563SJarkko Nikula 
1184956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1185956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1186956245e9SLiam Girdwood {
1187956245e9SLiam Girdwood 	int ret = 0;
1188956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
11891438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
1190be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1191956245e9SLiam Girdwood 
1192956245e9SLiam Girdwood 	platform->card = card;
1193b7950641SLiam Girdwood 	platform->dapm.card = card;
1194956245e9SLiam Girdwood 
1195956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1196956245e9SLiam Girdwood 		return -ENODEV;
1197956245e9SLiam Girdwood 
1198731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1199731f1ab2SSebastien Guiriec 
1200cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1201cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1202cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1203cb2cf612SLiam Girdwood 
1204be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
12051438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
12061438c2f6SLars-Peter Clausen 		if (component->dev != platform->dev)
1207be09ad90SLiam Girdwood 			continue;
12081438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list)
1209be09ad90SLiam Girdwood 			snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1210be09ad90SLiam Girdwood 	}
1211be09ad90SLiam Girdwood 
12123fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
12133fec6b6dSStephen Warren 
1214956245e9SLiam Girdwood 	if (driver->probe) {
1215956245e9SLiam Girdwood 		ret = driver->probe(platform);
1216956245e9SLiam Girdwood 		if (ret < 0) {
1217956245e9SLiam Girdwood 			dev_err(platform->dev,
1218f110bfc7SLiam Girdwood 				"ASoC: failed to probe platform %d\n", ret);
1219956245e9SLiam Girdwood 			goto err_probe;
1220956245e9SLiam Girdwood 		}
1221956245e9SLiam Girdwood 	}
1222956245e9SLiam Girdwood 
1223cb2cf612SLiam Girdwood 	if (driver->controls)
1224cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1225cb2cf612SLiam Girdwood 				     driver->num_controls);
1226cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1227cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1228cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1229cb2cf612SLiam Girdwood 
1230956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1231956245e9SLiam Girdwood 	platform->probed = 1;
1232956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1233b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1234956245e9SLiam Girdwood 
1235956245e9SLiam Girdwood 	return 0;
1236956245e9SLiam Girdwood 
1237956245e9SLiam Girdwood err_probe:
123802db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1239956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1240956245e9SLiam Girdwood 
1241956245e9SLiam Girdwood 	return ret;
1242956245e9SLiam Girdwood }
1243956245e9SLiam Girdwood 
124436ae1a96SMark Brown static void rtd_release(struct device *dev)
124536ae1a96SMark Brown {
124636ae1a96SMark Brown 	kfree(dev);
124736ae1a96SMark Brown }
1248f0fba2adSLiam Girdwood 
1249589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1250589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1251589c3563SJarkko Nikula 				   int num, int dailess)
1252589c3563SJarkko Nikula {
1253589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1254589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1255589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1256*6479f15aSLars-Peter Clausen 	const char *name;
1257589c3563SJarkko Nikula 	int ret = 0;
1258589c3563SJarkko Nikula 
1259589c3563SJarkko Nikula 	if (!dailess) {
1260589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1261589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1262589c3563SJarkko Nikula 		name = dai_link->name;
1263589c3563SJarkko Nikula 	} else {
1264589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1265589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1266589c3563SJarkko Nikula 		name = aux_dev->name;
1267589c3563SJarkko Nikula 	}
12680962bb21SJanusz Krzysztofik 	rtd->card = card;
1269589c3563SJarkko Nikula 
1270589c3563SJarkko Nikula 	/* do machine specific initialization */
1271589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1272589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1273589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1274589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1275589c3563SJarkko Nikula 	if (ret < 0) {
1276f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1277589c3563SJarkko Nikula 		return ret;
1278589c3563SJarkko Nikula 	}
1279589c3563SJarkko Nikula 
1280589c3563SJarkko Nikula 	/* register the rtd device */
1281589c3563SJarkko Nikula 	rtd->codec = codec;
128236ae1a96SMark Brown 
128336ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
128436ae1a96SMark Brown 	if (!rtd->dev)
128536ae1a96SMark Brown 		return -ENOMEM;
128636ae1a96SMark Brown 	device_initialize(rtd->dev);
128736ae1a96SMark Brown 	rtd->dev->parent = card->dev;
128836ae1a96SMark Brown 	rtd->dev->release = rtd_release;
128936ae1a96SMark Brown 	rtd->dev->init_name = name;
129036ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1291b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
129201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
129301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
129401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
129501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
129636ae1a96SMark Brown 	ret = device_add(rtd->dev);
1297589c3563SJarkko Nikula 	if (ret < 0) {
1298865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1299865df9cbSChuansheng Liu 		put_device(rtd->dev);
1300589c3563SJarkko Nikula 		dev_err(card->dev,
1301f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1302589c3563SJarkko Nikula 		return ret;
1303589c3563SJarkko Nikula 	}
1304589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1305589c3563SJarkko Nikula 
1306589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
130736ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1308589c3563SJarkko Nikula 	if (ret < 0)
1309589c3563SJarkko Nikula 		dev_err(codec->dev,
1310f110bfc7SLiam Girdwood 			"ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1311589c3563SJarkko Nikula 
1312589c3563SJarkko Nikula 	/* add codec sysfs entries */
131336ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1314589c3563SJarkko Nikula 	if (ret < 0)
1315589c3563SJarkko Nikula 		dev_err(codec->dev,
1316f110bfc7SLiam Girdwood 			"ASoC: failed to add codec sysfs files: %d\n", ret);
1317589c3563SJarkko Nikula 
1318f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1319f86dcef8SLiam Girdwood 	/* add DPCM sysfs entries */
1320cd0f8911SLiam Girdwood 	if (!dailess && !dai_link->dynamic)
1321f86dcef8SLiam Girdwood 		goto out;
1322f86dcef8SLiam Girdwood 
1323f86dcef8SLiam Girdwood 	ret = soc_dpcm_debugfs_add(rtd);
1324f86dcef8SLiam Girdwood 	if (ret < 0)
1325f110bfc7SLiam Girdwood 		dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1326f86dcef8SLiam Girdwood 
1327f86dcef8SLiam Girdwood out:
1328f86dcef8SLiam Girdwood #endif
1329589c3563SJarkko Nikula 	return 0;
1330589c3563SJarkko Nikula }
1331589c3563SJarkko Nikula 
133262ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
133362ae68faSStephen Warren 				     int order)
133462ae68faSStephen Warren {
133562ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
133662ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
133762ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
133862ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
133962ae68faSStephen Warren 	int ret;
134062ae68faSStephen Warren 
134162ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
134262ae68faSStephen Warren 	if (cpu_dai->codec &&
134362ae68faSStephen Warren 	    !cpu_dai->codec->probed &&
134462ae68faSStephen Warren 	    cpu_dai->codec->driver->probe_order == order) {
134562ae68faSStephen Warren 		ret = soc_probe_codec(card, cpu_dai->codec);
134662ae68faSStephen Warren 		if (ret < 0)
134762ae68faSStephen Warren 			return ret;
134862ae68faSStephen Warren 	}
134962ae68faSStephen Warren 
135062ae68faSStephen Warren 	/* probe the CODEC-side component */
135162ae68faSStephen Warren 	if (!codec_dai->codec->probed &&
135262ae68faSStephen Warren 	    codec_dai->codec->driver->probe_order == order) {
135362ae68faSStephen Warren 		ret = soc_probe_codec(card, codec_dai->codec);
135462ae68faSStephen Warren 		if (ret < 0)
135562ae68faSStephen Warren 			return ret;
135662ae68faSStephen Warren 	}
135762ae68faSStephen Warren 
135862ae68faSStephen Warren 	/* probe the platform */
135962ae68faSStephen Warren 	if (!platform->probed &&
136062ae68faSStephen Warren 	    platform->driver->probe_order == order) {
136162ae68faSStephen Warren 		ret = soc_probe_platform(card, platform);
136262ae68faSStephen Warren 		if (ret < 0)
136362ae68faSStephen Warren 			return ret;
136462ae68faSStephen Warren 	}
136562ae68faSStephen Warren 
136662ae68faSStephen Warren 	return 0;
136762ae68faSStephen Warren }
136862ae68faSStephen Warren 
136962ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1370f0fba2adSLiam Girdwood {
1371f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1372f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1373f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1374f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1375c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1376c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1377c74184edSMark Brown 	struct snd_soc_dapm_widget *play_w, *capture_w;
1378f0fba2adSLiam Girdwood 	int ret;
1379f0fba2adSLiam Girdwood 
1380f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
13810168bf0dSLiam Girdwood 			card->name, num, order);
1382f0fba2adSLiam Girdwood 
1383f0fba2adSLiam Girdwood 	/* config components */
1384f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1385f0fba2adSLiam Girdwood 	codec_dai->card = card;
1386f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1387f0fba2adSLiam Girdwood 
1388f0fba2adSLiam Girdwood 	/* set default power off timeout */
1389f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1390f0fba2adSLiam Girdwood 
1391f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
13920168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
13930168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1394a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
1395be09ad90SLiam Girdwood 			cpu_dai->dapm.card = card;
139661b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
139761b61e3cSLiam Girdwood 				return -ENODEV;
139861b61e3cSLiam Girdwood 
139918d75644SStephen Warren 			list_add(&cpu_dai->dapm.list, &card->dapm_list);
1400a9db7dbeSStephen Warren 		}
1401be09ad90SLiam Girdwood 
1402f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1403f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1404f0fba2adSLiam Girdwood 			if (ret < 0) {
1405f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1406f110bfc7SLiam Girdwood 					"ASoC: failed to probe CPU DAI %s: %d\n",
14070837fc62SFabio Estevam 					cpu_dai->name, ret);
140861b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1409f0fba2adSLiam Girdwood 				return ret;
1410f0fba2adSLiam Girdwood 			}
1411f0fba2adSLiam Girdwood 		}
1412f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
14131c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1414f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1415f0fba2adSLiam Girdwood 	}
1416f0fba2adSLiam Girdwood 
1417f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14180168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1419f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1420f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1421f0fba2adSLiam Girdwood 			if (ret < 0) {
1422f110bfc7SLiam Girdwood 				dev_err(codec_dai->dev,
1423f110bfc7SLiam Girdwood 					"ASoC: failed to probe CODEC DAI %s: %d\n",
14240837fc62SFabio Estevam 					codec_dai->name, ret);
1425f0fba2adSLiam Girdwood 				return ret;
1426f0fba2adSLiam Girdwood 			}
1427f0fba2adSLiam Girdwood 		}
1428f0fba2adSLiam Girdwood 
14291c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1430f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1431f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1432f0fba2adSLiam Girdwood 	}
1433f0fba2adSLiam Girdwood 
14340168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14350168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14360168bf0dSLiam Girdwood 		return 0;
14370168bf0dSLiam Girdwood 
1438589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1439589c3563SJarkko Nikula 	if (ret)
1440f0fba2adSLiam Girdwood 		return ret;
1441f0fba2adSLiam Girdwood 
144236ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1443f0fba2adSLiam Girdwood 	if (ret < 0)
1444f110bfc7SLiam Girdwood 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1445f110bfc7SLiam Girdwood 			ret);
1446f0fba2adSLiam Girdwood 
14471245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
14481245b700SNamarta Kohli 		/*create compress_device"*/
14491245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
14501245b700SNamarta Kohli 		if (ret < 0) {
1451f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
14521245b700SNamarta Kohli 					 dai_link->stream_name);
14531245b700SNamarta Kohli 			return ret;
14541245b700SNamarta Kohli 		}
14551245b700SNamarta Kohli 	} else {
14561245b700SNamarta Kohli 
1457c74184edSMark Brown 		if (!dai_link->params) {
1458f0fba2adSLiam Girdwood 			/* create the pcm */
1459f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1460f0fba2adSLiam Girdwood 			if (ret < 0) {
1461f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
14620837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1463f0fba2adSLiam Girdwood 				return ret;
1464f0fba2adSLiam Girdwood 			}
1465c74184edSMark Brown 		} else {
14669d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
14679d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
14689d58a077SRichard Fitzgerald 
1469c74184edSMark Brown 			/* link the DAI widgets */
1470c74184edSMark Brown 			play_w = codec_dai->playback_widget;
1471c74184edSMark Brown 			capture_w = cpu_dai->capture_widget;
1472c74184edSMark Brown 			if (play_w && capture_w) {
1473c74184edSMark Brown 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1474c74184edSMark Brown 						   capture_w, play_w);
1475c74184edSMark Brown 				if (ret != 0) {
1476f110bfc7SLiam Girdwood 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1477c74184edSMark Brown 						play_w->name, capture_w->name, ret);
1478c74184edSMark Brown 					return ret;
1479c74184edSMark Brown 				}
1480c74184edSMark Brown 			}
1481c74184edSMark Brown 
1482c74184edSMark Brown 			play_w = cpu_dai->playback_widget;
1483c74184edSMark Brown 			capture_w = codec_dai->capture_widget;
1484c74184edSMark Brown 			if (play_w && capture_w) {
1485c74184edSMark Brown 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1486c74184edSMark Brown 						   capture_w, play_w);
1487c74184edSMark Brown 				if (ret != 0) {
1488f110bfc7SLiam Girdwood 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1489c74184edSMark Brown 						play_w->name, capture_w->name, ret);
1490c74184edSMark Brown 					return ret;
1491c74184edSMark Brown 				}
1492c74184edSMark Brown 			}
1493c74184edSMark Brown 		}
14941245b700SNamarta Kohli 	}
1495f0fba2adSLiam Girdwood 
1496f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1497f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1498f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1499f0fba2adSLiam Girdwood 
1500f0fba2adSLiam Girdwood 	return 0;
1501f0fba2adSLiam Girdwood }
1502f0fba2adSLiam Girdwood 
1503f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1504f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1505f0fba2adSLiam Girdwood {
1506f0fba2adSLiam Girdwood 	int ret;
1507f0fba2adSLiam Girdwood 
1508f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1509f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
15106b05eda6SMark Brown 	 */
1511f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
15120562f788SMika Westerberg 		/*
15130562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
15140562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
15150562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
15160562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
15170562f788SMika Westerberg 		 *
15180562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
15190562f788SMika Westerberg 		 */
15200562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
15210562f788SMika Westerberg 			return 0;
1522f0fba2adSLiam Girdwood 
1523f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1524f0fba2adSLiam Girdwood 		if (ret < 0) {
1525f110bfc7SLiam Girdwood 			dev_err(rtd->codec->dev,
1526f110bfc7SLiam Girdwood 				"ASoC: AC97 device register failed: %d\n", ret);
1527f0fba2adSLiam Girdwood 			return ret;
1528435c5e25SMark Brown 		}
1529435c5e25SMark Brown 
1530f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1531f0fba2adSLiam Girdwood 	}
1532f0fba2adSLiam Girdwood 	return 0;
1533f0fba2adSLiam Girdwood }
1534435c5e25SMark Brown 
1535f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1536f0fba2adSLiam Girdwood {
1537f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1538f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1539f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1540f0fba2adSLiam Girdwood 	}
1541f0fba2adSLiam Girdwood }
1542f0fba2adSLiam Girdwood #endif
1543435c5e25SMark Brown 
1544b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1545b19e6e7bSMark Brown {
1546b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1547b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1548b19e6e7bSMark Brown 
1549b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1550b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1551b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1552b19e6e7bSMark Brown 			return 0;
1553b19e6e7bSMark Brown 	}
1554b19e6e7bSMark Brown 
1555f110bfc7SLiam Girdwood 	dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1556fb099cb7SMark Brown 
1557b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1558b19e6e7bSMark Brown }
1559b19e6e7bSMark Brown 
15602eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
15612eea392dSJarkko Nikula {
15622eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
15632eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1564676ad98aSJarkko Nikula 	int ret = -ENODEV;
15652eea392dSJarkko Nikula 
15662eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
15672eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
15682eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
15692eea392dSJarkko Nikula 			if (codec->probed) {
15702eea392dSJarkko Nikula 				dev_err(codec->dev,
1571f110bfc7SLiam Girdwood 					"ASoC: codec already probed");
15722eea392dSJarkko Nikula 				ret = -EBUSY;
15732eea392dSJarkko Nikula 				goto out;
15742eea392dSJarkko Nikula 			}
1575676ad98aSJarkko Nikula 			goto found;
15762eea392dSJarkko Nikula 		}
15772eea392dSJarkko Nikula 	}
1578676ad98aSJarkko Nikula 	/* codec not found */
1579f110bfc7SLiam Girdwood 	dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1580b19e6e7bSMark Brown 	return -EPROBE_DEFER;
15812eea392dSJarkko Nikula 
1582676ad98aSJarkko Nikula found:
1583589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
15842eea392dSJarkko Nikula 	if (ret < 0)
1585589c3563SJarkko Nikula 		return ret;
15862eea392dSJarkko Nikula 
1587589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
15882eea392dSJarkko Nikula 
15892eea392dSJarkko Nikula out:
15902eea392dSJarkko Nikula 	return ret;
15912eea392dSJarkko Nikula }
15922eea392dSJarkko Nikula 
15932eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
15942eea392dSJarkko Nikula {
15952eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
15962eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
15972eea392dSJarkko Nikula 
15982eea392dSJarkko Nikula 	/* unregister the rtd device */
15992eea392dSJarkko Nikula 	if (rtd->dev_registered) {
160036ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1601d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
16022eea392dSJarkko Nikula 		rtd->dev_registered = 0;
16032eea392dSJarkko Nikula 	}
16042eea392dSJarkko Nikula 
1605589c3563SJarkko Nikula 	if (codec && codec->probed)
1606589c3563SJarkko Nikula 		soc_remove_codec(codec);
16072eea392dSJarkko Nikula }
16082eea392dSJarkko Nikula 
1609f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1610fdf0f54dSDimitris Papastamos {
1611fdf0f54dSDimitris Papastamos 	int ret;
1612fdf0f54dSDimitris Papastamos 
1613fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1614fdf0f54dSDimitris Papastamos 		return 0;
1615fdf0f54dSDimitris Papastamos 
1616fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1617fdf0f54dSDimitris Papastamos 	if (ret < 0) {
161810e8aa9aSMichał Mirosław 		dev_err(codec->dev,
161910e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
162010e8aa9aSMichał Mirosław 			ret);
1621fdf0f54dSDimitris Papastamos 		return ret;
1622fdf0f54dSDimitris Papastamos 	}
1623fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1624fdf0f54dSDimitris Papastamos 	return 0;
1625fdf0f54dSDimitris Papastamos }
1626fdf0f54dSDimitris Papastamos 
1627b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1628f0fba2adSLiam Girdwood {
1629fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
163075d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1631f04209a7SMark Brown 	int ret, i, order, dai_fmt;
163296dd3622SMark Brown 
163301b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1634f0fba2adSLiam Girdwood 
1635b19e6e7bSMark Brown 	/* bind DAIs */
1636b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1637b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1638b19e6e7bSMark Brown 		if (ret != 0)
1639b19e6e7bSMark Brown 			goto base_error;
1640db2a4165SFrank Mandarino 	}
1641db2a4165SFrank Mandarino 
1642b19e6e7bSMark Brown 	/* check aux_devs too */
1643b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1644b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1645b19e6e7bSMark Brown 		if (ret != 0)
1646b19e6e7bSMark Brown 			goto base_error;
1647db2a4165SFrank Mandarino 	}
1648db2a4165SFrank Mandarino 
1649fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1650fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1651fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1652fdf0f54dSDimitris Papastamos 			continue;
1653f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1654b19e6e7bSMark Brown 		if (ret < 0)
1655b19e6e7bSMark Brown 			goto base_error;
1656fdf0f54dSDimitris Papastamos 	}
1657fdf0f54dSDimitris Papastamos 
1658f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1659f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1660f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1661f0fba2adSLiam Girdwood 	if (ret < 0) {
166210e8aa9aSMichał Mirosław 		dev_err(card->dev,
166310e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
166410e8aa9aSMichał Mirosław 			card->name, ret);
1665b19e6e7bSMark Brown 		goto base_error;
1666db2a4165SFrank Mandarino 	}
1667f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1668db2a4165SFrank Mandarino 
1669e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1670e37a4970SMark Brown 	card->dapm.dev = card->dev;
1671e37a4970SMark Brown 	card->dapm.card = card;
1672e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1673e37a4970SMark Brown 
1674d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1675d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1676d5d1e0beSLars-Peter Clausen #endif
1677d5d1e0beSLars-Peter Clausen 
167888ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
16796ed25978SAndy Green 	/* deferred resume work */
16806308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16811301a964SRandy Dunlap #endif
16826ed25978SAndy Green 
16839a841ebbSMark Brown 	if (card->dapm_widgets)
16849a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
16859a841ebbSMark Brown 					  card->num_dapm_widgets);
16869a841ebbSMark Brown 
1687f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1688f0fba2adSLiam Girdwood 	if (card->probe) {
1689e7361ec4SMark Brown 		ret = card->probe(card);
1690f0fba2adSLiam Girdwood 		if (ret < 0)
1691f0fba2adSLiam Girdwood 			goto card_probe_error;
1692f0fba2adSLiam Girdwood 	}
1693f0fba2adSLiam Girdwood 
169462ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
16950168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
16960168bf0dSLiam Girdwood 			order++) {
1697fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
169862ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
169962ae68faSStephen Warren 			if (ret < 0) {
1700f110bfc7SLiam Girdwood 				dev_err(card->dev,
1701f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1702f110bfc7SLiam Girdwood 					ret);
170362ae68faSStephen Warren 				goto probe_dai_err;
170462ae68faSStephen Warren 			}
170562ae68faSStephen Warren 		}
170662ae68faSStephen Warren 	}
170762ae68faSStephen Warren 
170862ae68faSStephen Warren 	/* probe all DAI links on this card */
170962ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
171062ae68faSStephen Warren 			order++) {
171162ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
171262ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1713fe3e78e0SMark Brown 			if (ret < 0) {
1714f110bfc7SLiam Girdwood 				dev_err(card->dev,
1715f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1716f110bfc7SLiam Girdwood 					ret);
1717f0fba2adSLiam Girdwood 				goto probe_dai_err;
1718fe3e78e0SMark Brown 			}
1719fe3e78e0SMark Brown 		}
17200168bf0dSLiam Girdwood 	}
1721fe3e78e0SMark Brown 
17222eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
17232eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
17242eea392dSJarkko Nikula 		if (ret < 0) {
1725f110bfc7SLiam Girdwood 			dev_err(card->dev,
1726f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1727f110bfc7SLiam Girdwood 				ret);
17282eea392dSJarkko Nikula 			goto probe_aux_dev_err;
17292eea392dSJarkko Nikula 		}
17302eea392dSJarkko Nikula 	}
17312eea392dSJarkko Nikula 
1732888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1733b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1734888df395SMark Brown 
1735b7af1dafSMark Brown 	if (card->controls)
1736022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1737b7af1dafSMark Brown 
1738b8ad29deSMark Brown 	if (card->dapm_routes)
1739b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1740b8ad29deSMark Brown 					card->num_dapm_routes);
1741b8ad29deSMark Brown 
174275d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
174375d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1744f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
174575d9ac46SMark Brown 
1746f04209a7SMark Brown 		if (dai_fmt) {
174775d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1748f04209a7SMark Brown 						  dai_fmt);
17495e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
175075d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
1751f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
175275d9ac46SMark Brown 					 ret);
1753f04209a7SMark Brown 		}
1754f04209a7SMark Brown 
1755f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1756fe33d4c5SStephen Warren 		if (dai_fmt &&
1757fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1758f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1759f04209a7SMark Brown 						  dai_fmt);
1760f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1761f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1762f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
1763f04209a7SMark Brown 					 ret);
1764f04209a7SMark Brown 		} else if (dai_fmt) {
1765f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1766f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1767f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1768f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1769f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1770f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1771f04209a7SMark Brown 				break;
1772f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1773f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1774f04209a7SMark Brown 				break;
1775f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1776f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1777f04209a7SMark Brown 				break;
1778f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1779f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1780f04209a7SMark Brown 				break;
1781f04209a7SMark Brown 			}
178275d9ac46SMark Brown 
178375d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1784f04209a7SMark Brown 						  dai_fmt);
17855e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
178675d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1787f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
178875d9ac46SMark Brown 					 ret);
178975d9ac46SMark Brown 		}
179075d9ac46SMark Brown 	}
179175d9ac46SMark Brown 
1792f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1793fe3e78e0SMark Brown 		 "%s", card->name);
1794f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
179522de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1796f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1797f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1798f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1799f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1800f0e8ed85SMark Brown 		case '_':
1801f0e8ed85SMark Brown 		case '-':
1802f0e8ed85SMark Brown 		case '\0':
1803f0e8ed85SMark Brown 			break;
1804f0e8ed85SMark Brown 		default:
1805f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1806f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1807f0e8ed85SMark Brown 			break;
1808f0e8ed85SMark Brown 		}
1809f0e8ed85SMark Brown 	}
1810fe3e78e0SMark Brown 
181128e9ad92SMark Brown 	if (card->late_probe) {
181228e9ad92SMark Brown 		ret = card->late_probe(card);
181328e9ad92SMark Brown 		if (ret < 0) {
1814f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
181528e9ad92SMark Brown 				card->name, ret);
181628e9ad92SMark Brown 			goto probe_aux_dev_err;
181728e9ad92SMark Brown 		}
181828e9ad92SMark Brown 	}
181928e9ad92SMark Brown 
18201633281bSStephen Warren 	if (card->fully_routed)
1821b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
18221633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
18231633281bSStephen Warren 
1824824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
18258c193b8dSLars-Peter Clausen 
1826f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1827fe3e78e0SMark Brown 	if (ret < 0) {
1828f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1829f110bfc7SLiam Girdwood 				ret);
18306b3ed785SAxel Lin 		goto probe_aux_dev_err;
1831fe3e78e0SMark Brown 	}
1832fe3e78e0SMark Brown 
1833fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1834f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1835f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1836f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1837fe3e78e0SMark Brown 		if (ret < 0) {
183810e8aa9aSMichał Mirosław 			dev_err(card->dev,
183910e8aa9aSMichał Mirosław 				"ASoC: failed to register AC97: %d\n", ret);
18406b3ed785SAxel Lin 			while (--i >= 0)
18417d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
18426b3ed785SAxel Lin 			goto probe_aux_dev_err;
1843fe3e78e0SMark Brown 		}
1844fe3e78e0SMark Brown 	}
1845fe3e78e0SMark Brown #endif
1846fe3e78e0SMark Brown 
1847435c5e25SMark Brown 	card->instantiated = 1;
18484f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1849f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1850b19e6e7bSMark Brown 
1851b19e6e7bSMark Brown 	return 0;
1852db2a4165SFrank Mandarino 
18532eea392dSJarkko Nikula probe_aux_dev_err:
18542eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18552eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18562eea392dSJarkko Nikula 
1857f0fba2adSLiam Girdwood probe_dai_err:
18580671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1859fe3e78e0SMark Brown 
1860f0fba2adSLiam Girdwood card_probe_error:
186187506549SMark Brown 	if (card->remove)
1862e7361ec4SMark Brown 		card->remove(card);
1863f0fba2adSLiam Girdwood 
1864f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1865f0fba2adSLiam Girdwood 
1866b19e6e7bSMark Brown base_error:
1867f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1868db2a4165SFrank Mandarino 
1869b19e6e7bSMark Brown 	return ret;
1870435c5e25SMark Brown }
1871435c5e25SMark Brown 
1872435c5e25SMark Brown /* probes a new socdev */
1873435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1874435c5e25SMark Brown {
1875f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1876435c5e25SMark Brown 
187770a7ca34SVinod Koul 	/*
187870a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
187970a7ca34SVinod Koul 	 * we should not be here in that case so ret error
188070a7ca34SVinod Koul 	 */
188170a7ca34SVinod Koul 	if (!card)
188270a7ca34SVinod Koul 		return -EINVAL;
188370a7ca34SVinod Koul 
1884fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1885f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1886fe4085e8SMark Brown 		 card->name);
1887fe4085e8SMark Brown 
1888435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1889435c5e25SMark Brown 	card->dev = &pdev->dev;
1890f0fba2adSLiam Girdwood 
189128d528c8SMark Brown 	return snd_soc_register_card(card);
1892435c5e25SMark Brown }
1893435c5e25SMark Brown 
1894b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1895db2a4165SFrank Mandarino {
1896db2a4165SFrank Mandarino 	int i;
1897db2a4165SFrank Mandarino 
1898f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1899f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1900f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
190143829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1902db2a4165SFrank Mandarino 	}
1903db2a4165SFrank Mandarino 
19042eea392dSJarkko Nikula 	/* remove auxiliary devices */
19052eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
19062eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
19072eea392dSJarkko Nikula 
1908f0fba2adSLiam Girdwood 	/* remove and free each DAI */
19090671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1910f0fba2adSLiam Girdwood 
1911a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1912a6052154SJarkko Nikula 
1913f0fba2adSLiam Girdwood 	/* remove the card */
191487506549SMark Brown 	if (card->remove)
1915e7361ec4SMark Brown 		card->remove(card);
1916f0fba2adSLiam Girdwood 
19170aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
19180aaae527SLars-Peter Clausen 
1919f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1920b0e26485SVinod Koul 	return 0;
1921b0e26485SVinod Koul 
1922b2dfa62cSGuennadi Liakhovetski }
1923b0e26485SVinod Koul 
1924b0e26485SVinod Koul /* removes a socdev */
1925b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1926b0e26485SVinod Koul {
1927b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1928b0e26485SVinod Koul 
1929c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1930db2a4165SFrank Mandarino 	return 0;
1931db2a4165SFrank Mandarino }
1932db2a4165SFrank Mandarino 
19336f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
193451737470SMark Brown {
19356f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1936f0fba2adSLiam Girdwood 	int i;
193751737470SMark Brown 
193851737470SMark Brown 	if (!card->instantiated)
1939416356fcSMark Brown 		return 0;
194051737470SMark Brown 
194151737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
194251737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1943f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1944f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
194543829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1946f0fba2adSLiam Girdwood 	}
194751737470SMark Brown 
1948f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1949416356fcSMark Brown 
1950988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
1951988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
1952988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1953988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
1954988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(codec_dai->dev);
1955988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
1956988e8cc4SNicolin Chen 	}
1957988e8cc4SNicolin Chen 
1958416356fcSMark Brown 	return 0;
195951737470SMark Brown }
19606f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
196151737470SMark Brown 
19626f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1963b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1964b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1965b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1966b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
19676f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1968b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1969416356fcSMark Brown };
1970deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1971416356fcSMark Brown 
1972db2a4165SFrank Mandarino /* ASoC platform driver */
1973db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1974db2a4165SFrank Mandarino 	.driver		= {
1975db2a4165SFrank Mandarino 		.name		= "soc-audio",
19768b45a209SKay Sievers 		.owner		= THIS_MODULE,
19776f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1978db2a4165SFrank Mandarino 	},
1979db2a4165SFrank Mandarino 	.probe		= soc_probe,
1980db2a4165SFrank Mandarino 	.remove		= soc_remove,
1981db2a4165SFrank Mandarino };
1982db2a4165SFrank Mandarino 
1983096e49d5SMark Brown /**
1984096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1985096e49d5SMark Brown  *
1986096e49d5SMark Brown  * @codec: CODEC to query.
1987096e49d5SMark Brown  * @reg: Register to query.
1988096e49d5SMark Brown  *
1989096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1990096e49d5SMark Brown  */
1991181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1992181e055eSMark Brown 				    unsigned int reg)
1993096e49d5SMark Brown {
19941500b7b5SDimitris Papastamos 	if (codec->volatile_register)
19951500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1996096e49d5SMark Brown 	else
1997096e49d5SMark Brown 		return 0;
1998096e49d5SMark Brown }
1999096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2000096e49d5SMark Brown 
2001db2a4165SFrank Mandarino /**
2002239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
2003239c9706SDimitris Papastamos  *
2004239c9706SDimitris Papastamos  * @codec: CODEC to query.
2005239c9706SDimitris Papastamos  * @reg: Register to query.
2006239c9706SDimitris Papastamos  *
2007239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
2008239c9706SDimitris Papastamos  */
2009239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2010239c9706SDimitris Papastamos 				    unsigned int reg)
2011239c9706SDimitris Papastamos {
2012239c9706SDimitris Papastamos 	if (codec->readable_register)
2013239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
2014239c9706SDimitris Papastamos 	else
201563fa0a28SLars-Peter Clausen 		return 1;
2016239c9706SDimitris Papastamos }
2017239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2018239c9706SDimitris Papastamos 
2019239c9706SDimitris Papastamos /**
2020239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
2021239c9706SDimitris Papastamos  *
2022239c9706SDimitris Papastamos  * @codec: CODEC to query.
2023239c9706SDimitris Papastamos  * @reg: Register to query.
2024239c9706SDimitris Papastamos  *
2025239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
2026239c9706SDimitris Papastamos  */
2027239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2028239c9706SDimitris Papastamos 				    unsigned int reg)
2029239c9706SDimitris Papastamos {
2030239c9706SDimitris Papastamos 	if (codec->writable_register)
2031239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
2032239c9706SDimitris Papastamos 	else
203363fa0a28SLars-Peter Clausen 		return 1;
2034239c9706SDimitris Papastamos }
2035239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2036239c9706SDimitris Papastamos 
2037f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
2038f1442bc1SLiam Girdwood 					unsigned int reg)
2039f1442bc1SLiam Girdwood {
2040f1442bc1SLiam Girdwood 	unsigned int ret;
2041f1442bc1SLiam Girdwood 
2042f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
2043f110bfc7SLiam Girdwood 		dev_err(platform->dev, "ASoC: platform has no read back\n");
2044f1442bc1SLiam Girdwood 		return -1;
2045f1442bc1SLiam Girdwood 	}
2046f1442bc1SLiam Girdwood 
2047f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
2048f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2049a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
2050f1442bc1SLiam Girdwood 
2051f1442bc1SLiam Girdwood 	return ret;
2052f1442bc1SLiam Girdwood }
2053f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2054f1442bc1SLiam Girdwood 
2055f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
2056f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
2057f1442bc1SLiam Girdwood {
2058f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
2059f110bfc7SLiam Girdwood 		dev_err(platform->dev, "ASoC: platform has no write back\n");
2060f1442bc1SLiam Girdwood 		return -1;
2061f1442bc1SLiam Girdwood 	}
2062f1442bc1SLiam Girdwood 
2063f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2064a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
2065f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
2066f1442bc1SLiam Girdwood }
2067f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2068f1442bc1SLiam Girdwood 
2069239c9706SDimitris Papastamos /**
2070db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2071db2a4165SFrank Mandarino  * @codec: audio codec
2072db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2073db2a4165SFrank Mandarino  * @num: AC97 codec number
2074db2a4165SFrank Mandarino  *
2075db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2076db2a4165SFrank Mandarino  */
2077db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2078db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2079db2a4165SFrank Mandarino {
2080db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2081db2a4165SFrank Mandarino 
2082db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2083db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2084db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2085db2a4165SFrank Mandarino 		return -ENOMEM;
2086db2a4165SFrank Mandarino 	}
2087db2a4165SFrank Mandarino 
2088db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2089db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2090db2a4165SFrank Mandarino 		kfree(codec->ac97);
2091db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2092db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2093db2a4165SFrank Mandarino 		return -ENOMEM;
2094db2a4165SFrank Mandarino 	}
2095db2a4165SFrank Mandarino 
2096db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2097db2a4165SFrank Mandarino 	codec->ac97->num = num;
20980562f788SMika Westerberg 
20990562f788SMika Westerberg 	/*
21000562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
21010562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
21020562f788SMika Westerberg 	 */
21030562f788SMika Westerberg 	codec->ac97_created = 1;
21040562f788SMika Westerberg 
2105db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2106db2a4165SFrank Mandarino 	return 0;
2107db2a4165SFrank Mandarino }
2108db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2109db2a4165SFrank Mandarino 
2110741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2111741a509fSMarkus Pargmann 
2112741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2113741a509fSMarkus Pargmann {
2114741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2115741a509fSMarkus Pargmann 
2116741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2117741a509fSMarkus Pargmann 
2118741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2119741a509fSMarkus Pargmann 
2120741a509fSMarkus Pargmann 	udelay(10);
2121741a509fSMarkus Pargmann 
2122741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2123741a509fSMarkus Pargmann 
2124741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2125741a509fSMarkus Pargmann 	msleep(2);
2126741a509fSMarkus Pargmann }
2127741a509fSMarkus Pargmann 
2128741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2129741a509fSMarkus Pargmann {
2130741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2131741a509fSMarkus Pargmann 
2132741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2133741a509fSMarkus Pargmann 
2134741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2135741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2136741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2137741a509fSMarkus Pargmann 
2138741a509fSMarkus Pargmann 	udelay(10);
2139741a509fSMarkus Pargmann 
2140741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2141741a509fSMarkus Pargmann 
2142741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2143741a509fSMarkus Pargmann 	msleep(2);
2144741a509fSMarkus Pargmann }
2145741a509fSMarkus Pargmann 
2146741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev,
2147741a509fSMarkus Pargmann 		struct snd_ac97_reset_cfg *cfg)
2148741a509fSMarkus Pargmann {
2149741a509fSMarkus Pargmann 	struct pinctrl *p;
2150741a509fSMarkus Pargmann 	struct pinctrl_state *state;
2151741a509fSMarkus Pargmann 	int gpio;
2152741a509fSMarkus Pargmann 	int ret;
2153741a509fSMarkus Pargmann 
2154741a509fSMarkus Pargmann 	p = devm_pinctrl_get(dev);
2155741a509fSMarkus Pargmann 	if (IS_ERR(p)) {
2156741a509fSMarkus Pargmann 		dev_err(dev, "Failed to get pinctrl\n");
2157741a509fSMarkus Pargmann 		return PTR_RET(p);
2158741a509fSMarkus Pargmann 	}
2159741a509fSMarkus Pargmann 	cfg->pctl = p;
2160741a509fSMarkus Pargmann 
2161741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-reset");
2162741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2163741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2164741a509fSMarkus Pargmann 		return PTR_RET(state);
2165741a509fSMarkus Pargmann 	}
2166741a509fSMarkus Pargmann 	cfg->pstate_reset = state;
2167741a509fSMarkus Pargmann 
2168741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
2169741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2170741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2171741a509fSMarkus Pargmann 		return PTR_RET(state);
2172741a509fSMarkus Pargmann 	}
2173741a509fSMarkus Pargmann 	cfg->pstate_warm_reset = state;
2174741a509fSMarkus Pargmann 
2175741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-running");
2176741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2177741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
2178741a509fSMarkus Pargmann 		return PTR_RET(state);
2179741a509fSMarkus Pargmann 	}
2180741a509fSMarkus Pargmann 	cfg->pstate_run = state;
2181741a509fSMarkus Pargmann 
2182741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2183741a509fSMarkus Pargmann 	if (gpio < 0) {
2184741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sync gpio\n");
2185741a509fSMarkus Pargmann 		return gpio;
2186741a509fSMarkus Pargmann 	}
2187741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2188741a509fSMarkus Pargmann 	if (ret) {
2189741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
2190741a509fSMarkus Pargmann 		return ret;
2191741a509fSMarkus Pargmann 	}
2192741a509fSMarkus Pargmann 	cfg->gpio_sync = gpio;
2193741a509fSMarkus Pargmann 
2194741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2195741a509fSMarkus Pargmann 	if (gpio < 0) {
2196741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2197741a509fSMarkus Pargmann 		return gpio;
2198741a509fSMarkus Pargmann 	}
2199741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2200741a509fSMarkus Pargmann 	if (ret) {
2201741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2202741a509fSMarkus Pargmann 		return ret;
2203741a509fSMarkus Pargmann 	}
2204741a509fSMarkus Pargmann 	cfg->gpio_sdata = gpio;
2205741a509fSMarkus Pargmann 
2206741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2207741a509fSMarkus Pargmann 	if (gpio < 0) {
2208741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-reset gpio\n");
2209741a509fSMarkus Pargmann 		return gpio;
2210741a509fSMarkus Pargmann 	}
2211741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2212741a509fSMarkus Pargmann 	if (ret) {
2213741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
2214741a509fSMarkus Pargmann 		return ret;
2215741a509fSMarkus Pargmann 	}
2216741a509fSMarkus Pargmann 	cfg->gpio_reset = gpio;
2217741a509fSMarkus Pargmann 
2218741a509fSMarkus Pargmann 	return 0;
2219741a509fSMarkus Pargmann }
2220741a509fSMarkus Pargmann 
2221b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops;
2222f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops);
2223b047e1ccSMark Brown 
2224b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2225b047e1ccSMark Brown {
2226b047e1ccSMark Brown 	if (ops == soc_ac97_ops)
2227b047e1ccSMark Brown 		return 0;
2228b047e1ccSMark Brown 
2229b047e1ccSMark Brown 	if (soc_ac97_ops && ops)
2230b047e1ccSMark Brown 		return -EBUSY;
2231b047e1ccSMark Brown 
2232b047e1ccSMark Brown 	soc_ac97_ops = ops;
2233b047e1ccSMark Brown 
2234b047e1ccSMark Brown 	return 0;
2235b047e1ccSMark Brown }
2236b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2237b047e1ccSMark Brown 
2238db2a4165SFrank Mandarino /**
2239741a509fSMarkus Pargmann  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2240741a509fSMarkus Pargmann  *
2241741a509fSMarkus Pargmann  * This function sets the reset and warm_reset properties of ops and parses
2242741a509fSMarkus Pargmann  * the device node of pdev to get pinctrl states and gpio numbers to use.
2243741a509fSMarkus Pargmann  */
2244741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2245741a509fSMarkus Pargmann 		struct platform_device *pdev)
2246741a509fSMarkus Pargmann {
2247741a509fSMarkus Pargmann 	struct device *dev = &pdev->dev;
2248741a509fSMarkus Pargmann 	struct snd_ac97_reset_cfg cfg;
2249741a509fSMarkus Pargmann 	int ret;
2250741a509fSMarkus Pargmann 
2251741a509fSMarkus Pargmann 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2252741a509fSMarkus Pargmann 	if (ret)
2253741a509fSMarkus Pargmann 		return ret;
2254741a509fSMarkus Pargmann 
2255741a509fSMarkus Pargmann 	ret = snd_soc_set_ac97_ops(ops);
2256741a509fSMarkus Pargmann 	if (ret)
2257741a509fSMarkus Pargmann 		return ret;
2258741a509fSMarkus Pargmann 
2259741a509fSMarkus Pargmann 	ops->warm_reset = snd_soc_ac97_warm_reset;
2260741a509fSMarkus Pargmann 	ops->reset = snd_soc_ac97_reset;
2261741a509fSMarkus Pargmann 
2262741a509fSMarkus Pargmann 	snd_ac97_rst_cfg = cfg;
2263741a509fSMarkus Pargmann 	return 0;
2264741a509fSMarkus Pargmann }
2265741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2266741a509fSMarkus Pargmann 
2267741a509fSMarkus Pargmann /**
2268db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2269db2a4165SFrank Mandarino  * @codec: audio codec
2270db2a4165SFrank Mandarino  *
2271db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2272db2a4165SFrank Mandarino  */
2273db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2274db2a4165SFrank Mandarino {
2275db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2276f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2277f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2278f0fba2adSLiam Girdwood #endif
2279db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2280db2a4165SFrank Mandarino 	kfree(codec->ac97);
2281db2a4165SFrank Mandarino 	codec->ac97 = NULL;
22820562f788SMika Westerberg 	codec->ac97_created = 0;
2283db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2284db2a4165SFrank Mandarino }
2285db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2286db2a4165SFrank Mandarino 
2287c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2288c3753707SMark Brown {
2289c3753707SMark Brown 	unsigned int ret;
2290c3753707SMark Brown 
2291c3acec26SMark Brown 	ret = codec->read(codec, reg);
2292c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2293a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2294c3753707SMark Brown 
2295c3753707SMark Brown 	return ret;
2296c3753707SMark Brown }
2297c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2298c3753707SMark Brown 
2299c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2300c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2301c3753707SMark Brown {
2302c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2303a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2304c3acec26SMark Brown 	return codec->write(codec, reg, val);
2305c3753707SMark Brown }
2306c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2307c3753707SMark Brown 
2308db2a4165SFrank Mandarino /**
2309db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2310db2a4165SFrank Mandarino  * @codec: audio codec
2311db2a4165SFrank Mandarino  * @reg: codec register
2312db2a4165SFrank Mandarino  * @mask: register mask
2313db2a4165SFrank Mandarino  * @value: new value
2314db2a4165SFrank Mandarino  *
2315db2a4165SFrank Mandarino  * Writes new register value.
2316db2a4165SFrank Mandarino  *
2317180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
2318db2a4165SFrank Mandarino  */
2319db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
232046f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2321db2a4165SFrank Mandarino {
23228a713da8SMark Brown 	bool change;
232346f5822fSDaniel Ribeiro 	unsigned int old, new;
2324180c329dSTimur Tabi 	int ret;
2325db2a4165SFrank Mandarino 
23268a713da8SMark Brown 	if (codec->using_regmap) {
23278a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
23288a713da8SMark Brown 					       mask, value, &change);
23298a713da8SMark Brown 	} else {
2330180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
2331180c329dSTimur Tabi 		if (ret < 0)
2332180c329dSTimur Tabi 			return ret;
2333180c329dSTimur Tabi 
2334180c329dSTimur Tabi 		old = ret;
233578bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
2336db2a4165SFrank Mandarino 		change = old != new;
23378a713da8SMark Brown 		if (change)
2338180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
23398a713da8SMark Brown 	}
23408a713da8SMark Brown 
2341180c329dSTimur Tabi 	if (ret < 0)
2342180c329dSTimur Tabi 		return ret;
2343db2a4165SFrank Mandarino 
2344db2a4165SFrank Mandarino 	return change;
2345db2a4165SFrank Mandarino }
2346db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2347db2a4165SFrank Mandarino 
2348db2a4165SFrank Mandarino /**
23496c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
23506c508c62SEero Nurkkala  * @codec: audio codec
23516c508c62SEero Nurkkala  * @reg: codec register
23526c508c62SEero Nurkkala  * @mask: register mask
23536c508c62SEero Nurkkala  * @value: new value
23546c508c62SEero Nurkkala  *
23556c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
23566c508c62SEero Nurkkala  *
23576c508c62SEero Nurkkala  * Returns 1 for change else 0.
23586c508c62SEero Nurkkala  */
2359dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
23606c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
23616c508c62SEero Nurkkala 			       unsigned int value)
23626c508c62SEero Nurkkala {
23636c508c62SEero Nurkkala 	int change;
23646c508c62SEero Nurkkala 
23656c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
23666c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
23676c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
23686c508c62SEero Nurkkala 
23696c508c62SEero Nurkkala 	return change;
23706c508c62SEero Nurkkala }
2371dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
23726c508c62SEero Nurkkala 
23736c508c62SEero Nurkkala /**
2374db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2375db2a4165SFrank Mandarino  * @codec: audio codec
2376db2a4165SFrank Mandarino  * @reg: codec register
2377db2a4165SFrank Mandarino  * @mask: register mask
2378db2a4165SFrank Mandarino  * @value: new value
2379db2a4165SFrank Mandarino  *
2380db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2381db2a4165SFrank Mandarino  * different from the old value.
2382db2a4165SFrank Mandarino  *
2383db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2384db2a4165SFrank Mandarino  */
2385db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
238646f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2387db2a4165SFrank Mandarino {
2388db2a4165SFrank Mandarino 	int change;
238946f5822fSDaniel Ribeiro 	unsigned int old, new;
2390db2a4165SFrank Mandarino 
2391db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2392db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2393db2a4165SFrank Mandarino 	change = old != new;
2394db2a4165SFrank Mandarino 
2395db2a4165SFrank Mandarino 	return change;
2396db2a4165SFrank Mandarino }
2397db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2398db2a4165SFrank Mandarino 
2399db2a4165SFrank Mandarino /**
2400db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2401db2a4165SFrank Mandarino  * @_template: control template
2402db2a4165SFrank Mandarino  * @data: control private data
2403ac11a2b3SMark Brown  * @long_name: control long name
2404efb7ac3fSMark Brown  * @prefix: control name prefix
2405db2a4165SFrank Mandarino  *
2406db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2407db2a4165SFrank Mandarino  *
2408db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2409db2a4165SFrank Mandarino  */
2410db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
24113056557fSMark Brown 				  void *data, const char *long_name,
2412efb7ac3fSMark Brown 				  const char *prefix)
2413db2a4165SFrank Mandarino {
2414db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2415efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2416efb7ac3fSMark Brown 	char *name = NULL;
2417db2a4165SFrank Mandarino 
2418db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2419db2a4165SFrank Mandarino 	template.index = 0;
2420db2a4165SFrank Mandarino 
2421efb7ac3fSMark Brown 	if (!long_name)
2422efb7ac3fSMark Brown 		long_name = template.name;
2423efb7ac3fSMark Brown 
2424efb7ac3fSMark Brown 	if (prefix) {
24252b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2426efb7ac3fSMark Brown 		if (!name)
2427efb7ac3fSMark Brown 			return NULL;
2428efb7ac3fSMark Brown 
2429efb7ac3fSMark Brown 		template.name = name;
2430efb7ac3fSMark Brown 	} else {
2431efb7ac3fSMark Brown 		template.name = long_name;
2432efb7ac3fSMark Brown 	}
2433efb7ac3fSMark Brown 
2434efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2435efb7ac3fSMark Brown 
2436efb7ac3fSMark Brown 	kfree(name);
2437efb7ac3fSMark Brown 
2438efb7ac3fSMark Brown 	return kcontrol;
2439db2a4165SFrank Mandarino }
2440db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2441db2a4165SFrank Mandarino 
2442022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2443022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2444022658beSLiam Girdwood 	const char *prefix, void *data)
2445022658beSLiam Girdwood {
2446022658beSLiam Girdwood 	int err, i;
2447022658beSLiam Girdwood 
2448022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2449022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2450022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2451022658beSLiam Girdwood 						     control->name, prefix));
2452022658beSLiam Girdwood 		if (err < 0) {
2453f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2454f110bfc7SLiam Girdwood 				control->name, err);
2455022658beSLiam Girdwood 			return err;
2456022658beSLiam Girdwood 		}
2457022658beSLiam Girdwood 	}
2458022658beSLiam Girdwood 
2459022658beSLiam Girdwood 	return 0;
2460022658beSLiam Girdwood }
2461022658beSLiam Girdwood 
24624fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
24634fefd698SDimitris Papastamos 					       const char *name)
24644fefd698SDimitris Papastamos {
24654fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
24664fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
24674fefd698SDimitris Papastamos 
24684fefd698SDimitris Papastamos 	if (unlikely(!name))
24694fefd698SDimitris Papastamos 		return NULL;
24704fefd698SDimitris Papastamos 
24714fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
24724fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
24734fefd698SDimitris Papastamos 			return kctl;
24744fefd698SDimitris Papastamos 	return NULL;
24754fefd698SDimitris Papastamos }
24764fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
24774fefd698SDimitris Papastamos 
2478db2a4165SFrank Mandarino /**
2479022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2480022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
24813e8e1952SIan Molton  * duplicating this code.
24823e8e1952SIan Molton  *
24833e8e1952SIan Molton  * @codec: codec to add controls to
24843e8e1952SIan Molton  * @controls: array of controls to add
24853e8e1952SIan Molton  * @num_controls: number of elements in the array
24863e8e1952SIan Molton  *
24873e8e1952SIan Molton  * Return 0 for success, else error.
24883e8e1952SIan Molton  */
2489022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
24903e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
24913e8e1952SIan Molton {
2492f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
24933e8e1952SIan Molton 
2494022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2495022658beSLiam Girdwood 			codec->name_prefix, codec);
24963e8e1952SIan Molton }
2497022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
24983e8e1952SIan Molton 
24993e8e1952SIan Molton /**
2500a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2501022658beSLiam Girdwood  * Convenience function to add a list of controls.
2502a491a5c8SLiam Girdwood  *
2503a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2504a491a5c8SLiam Girdwood  * @controls: array of controls to add
2505a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2506a491a5c8SLiam Girdwood  *
2507a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2508a491a5c8SLiam Girdwood  */
2509a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2510a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2511a491a5c8SLiam Girdwood {
2512a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2513a491a5c8SLiam Girdwood 
2514022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2515022658beSLiam Girdwood 			NULL, platform);
2516a491a5c8SLiam Girdwood }
2517a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2518a491a5c8SLiam Girdwood 
2519a491a5c8SLiam Girdwood /**
2520022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2521022658beSLiam Girdwood  * Convenience function to add a list of controls.
2522022658beSLiam Girdwood  *
2523022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2524022658beSLiam Girdwood  * @controls: array of controls to add
2525022658beSLiam Girdwood  * @num_controls: number of elements in the array
2526022658beSLiam Girdwood  *
2527022658beSLiam Girdwood  * Return 0 for success, else error.
2528022658beSLiam Girdwood  */
2529022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2530022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2531022658beSLiam Girdwood {
2532022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2533022658beSLiam Girdwood 
2534022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2535022658beSLiam Girdwood 			NULL, soc_card);
2536022658beSLiam Girdwood }
2537022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2538022658beSLiam Girdwood 
2539022658beSLiam Girdwood /**
2540022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2541022658beSLiam Girdwood  * Convienience function to add a list of controls.
2542022658beSLiam Girdwood  *
2543022658beSLiam Girdwood  * @dai: DAI to add controls to
2544022658beSLiam Girdwood  * @controls: array of controls to add
2545022658beSLiam Girdwood  * @num_controls: number of elements in the array
2546022658beSLiam Girdwood  *
2547022658beSLiam Girdwood  * Return 0 for success, else error.
2548022658beSLiam Girdwood  */
2549022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2550022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2551022658beSLiam Girdwood {
2552022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2553022658beSLiam Girdwood 
2554022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2555022658beSLiam Girdwood 			NULL, dai);
2556022658beSLiam Girdwood }
2557022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2558022658beSLiam Girdwood 
2559022658beSLiam Girdwood /**
2560db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2561db2a4165SFrank Mandarino  * @kcontrol: mixer control
2562db2a4165SFrank Mandarino  * @uinfo: control element information
2563db2a4165SFrank Mandarino  *
2564db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2565db2a4165SFrank Mandarino  * mixer control.
2566db2a4165SFrank Mandarino  *
2567db2a4165SFrank Mandarino  * Returns 0 for success.
2568db2a4165SFrank Mandarino  */
2569db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2570db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2571db2a4165SFrank Mandarino {
2572db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2573db2a4165SFrank Mandarino 
2574db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2575db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
25769a8d38dbSTakashi Iwai 	uinfo->value.enumerated.items = e->items;
2577db2a4165SFrank Mandarino 
25789a8d38dbSTakashi Iwai 	if (uinfo->value.enumerated.item >= e->items)
25799a8d38dbSTakashi Iwai 		uinfo->value.enumerated.item = e->items - 1;
2580a19685cbSTakashi Iwai 	strlcpy(uinfo->value.enumerated.name,
2581a19685cbSTakashi Iwai 		e->texts[uinfo->value.enumerated.item],
2582a19685cbSTakashi Iwai 		sizeof(uinfo->value.enumerated.name));
2583db2a4165SFrank Mandarino 	return 0;
2584db2a4165SFrank Mandarino }
2585db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2586db2a4165SFrank Mandarino 
2587db2a4165SFrank Mandarino /**
2588db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2589db2a4165SFrank Mandarino  * @kcontrol: mixer control
2590ac11a2b3SMark Brown  * @ucontrol: control element information
2591db2a4165SFrank Mandarino  *
2592db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2593db2a4165SFrank Mandarino  *
2594db2a4165SFrank Mandarino  * Returns 0 for success.
2595db2a4165SFrank Mandarino  */
2596db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2597db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2598db2a4165SFrank Mandarino {
2599db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2600db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
260129ae2fa5SLars-Peter Clausen 	unsigned int val, item;
260229ae2fa5SLars-Peter Clausen 	unsigned int reg_val;
2603db2a4165SFrank Mandarino 
260429ae2fa5SLars-Peter Clausen 	reg_val = snd_soc_read(codec, e->reg);
260529ae2fa5SLars-Peter Clausen 	val = (reg_val >> e->shift_l) & e->mask;
260629ae2fa5SLars-Peter Clausen 	item = snd_soc_enum_val_to_item(e, val);
260729ae2fa5SLars-Peter Clausen 	ucontrol->value.enumerated.item[0] = item;
260829ae2fa5SLars-Peter Clausen 	if (e->shift_l != e->shift_r) {
260929ae2fa5SLars-Peter Clausen 		val = (reg_val >> e->shift_l) & e->mask;
261029ae2fa5SLars-Peter Clausen 		item = snd_soc_enum_val_to_item(e, val);
261129ae2fa5SLars-Peter Clausen 		ucontrol->value.enumerated.item[1] = item;
261229ae2fa5SLars-Peter Clausen 	}
2613db2a4165SFrank Mandarino 
2614db2a4165SFrank Mandarino 	return 0;
2615db2a4165SFrank Mandarino }
2616db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2617db2a4165SFrank Mandarino 
2618db2a4165SFrank Mandarino /**
2619db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2620db2a4165SFrank Mandarino  * @kcontrol: mixer control
2621ac11a2b3SMark Brown  * @ucontrol: control element information
2622db2a4165SFrank Mandarino  *
2623db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2624db2a4165SFrank Mandarino  *
2625db2a4165SFrank Mandarino  * Returns 0 for success.
2626db2a4165SFrank Mandarino  */
2627db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2628db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2629db2a4165SFrank Mandarino {
2630db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2631db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
263229ae2fa5SLars-Peter Clausen 	unsigned int *item = ucontrol->value.enumerated.item;
263346f5822fSDaniel Ribeiro 	unsigned int val;
263486767b7dSLars-Peter Clausen 	unsigned int mask;
2635db2a4165SFrank Mandarino 
263629ae2fa5SLars-Peter Clausen 	if (item[0] >= e->items)
2637db2a4165SFrank Mandarino 		return -EINVAL;
263829ae2fa5SLars-Peter Clausen 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
263986767b7dSLars-Peter Clausen 	mask = e->mask << e->shift_l;
2640db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
264129ae2fa5SLars-Peter Clausen 		if (item[1] >= e->items)
2642db2a4165SFrank Mandarino 			return -EINVAL;
264329ae2fa5SLars-Peter Clausen 		val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
264486767b7dSLars-Peter Clausen 		mask |= e->mask << e->shift_r;
2645db2a4165SFrank Mandarino 	}
2646db2a4165SFrank Mandarino 
26476c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2648db2a4165SFrank Mandarino }
2649db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2650db2a4165SFrank Mandarino 
2651db2a4165SFrank Mandarino /**
2652f227b88fSMarkus Pargmann  * snd_soc_read_signed - Read a codec register and interprete as signed value
2653f227b88fSMarkus Pargmann  * @codec: codec
2654f227b88fSMarkus Pargmann  * @reg: Register to read
2655f227b88fSMarkus Pargmann  * @mask: Mask to use after shifting the register value
2656f227b88fSMarkus Pargmann  * @shift: Right shift of register value
2657f227b88fSMarkus Pargmann  * @sign_bit: Bit that describes if a number is negative or not.
26582e72f8e3SPeter Ujfalusi  *
2659f227b88fSMarkus Pargmann  * This functions reads a codec register. The register value is shifted right
2660f227b88fSMarkus Pargmann  * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2661f227b88fSMarkus Pargmann  * the given registervalue into a signed integer if sign_bit is non-zero.
26622e72f8e3SPeter Ujfalusi  *
2663f227b88fSMarkus Pargmann  * Returns the register value as signed int.
26642e72f8e3SPeter Ujfalusi  */
2665f227b88fSMarkus Pargmann static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2666f227b88fSMarkus Pargmann 		unsigned int mask, unsigned int shift, unsigned int sign_bit)
26672e72f8e3SPeter Ujfalusi {
2668f227b88fSMarkus Pargmann 	int ret;
266946f5822fSDaniel Ribeiro 	unsigned int val;
26702e72f8e3SPeter Ujfalusi 
2671f227b88fSMarkus Pargmann 	val = (snd_soc_read(codec, reg) >> shift) & mask;
26722e72f8e3SPeter Ujfalusi 
2673f227b88fSMarkus Pargmann 	if (!sign_bit)
2674f227b88fSMarkus Pargmann 		return val;
2675f227b88fSMarkus Pargmann 
2676f227b88fSMarkus Pargmann 	/* non-negative number */
2677f227b88fSMarkus Pargmann 	if (!(val & BIT(sign_bit)))
2678f227b88fSMarkus Pargmann 		return val;
2679f227b88fSMarkus Pargmann 
2680f227b88fSMarkus Pargmann 	ret = val;
2681f227b88fSMarkus Pargmann 
2682f227b88fSMarkus Pargmann 	/*
2683f227b88fSMarkus Pargmann 	 * The register most probably does not contain a full-sized int.
2684f227b88fSMarkus Pargmann 	 * Instead we have an arbitrary number of bits in a signed
2685f227b88fSMarkus Pargmann 	 * representation which has to be translated into a full-sized int.
2686f227b88fSMarkus Pargmann 	 * This is done by filling up all bits above the sign-bit.
2687f227b88fSMarkus Pargmann 	 */
2688f227b88fSMarkus Pargmann 	ret |= ~((int)(BIT(sign_bit) - 1));
2689f227b88fSMarkus Pargmann 
2690f227b88fSMarkus Pargmann 	return ret;
26912e72f8e3SPeter Ujfalusi }
26922e72f8e3SPeter Ujfalusi 
26932e72f8e3SPeter Ujfalusi /**
2694db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2695db2a4165SFrank Mandarino  * @kcontrol: mixer control
2696db2a4165SFrank Mandarino  * @uinfo: control element information
2697db2a4165SFrank Mandarino  *
2698e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2699e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2700db2a4165SFrank Mandarino  *
2701db2a4165SFrank Mandarino  * Returns 0 for success.
2702db2a4165SFrank Mandarino  */
2703db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2704db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2705db2a4165SFrank Mandarino {
27064eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27074eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2708d11bb4a9SPeter Ujfalusi 	int platform_max;
2709db2a4165SFrank Mandarino 
2710d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2711d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2712d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2713d11bb4a9SPeter Ujfalusi 
2714d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2715a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2716a7a4ac86SPhilipp Zabel 	else
2717a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2718a7a4ac86SPhilipp Zabel 
2719e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2720db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2721f227b88fSMarkus Pargmann 	uinfo->value.integer.max = platform_max - mc->min;
2722db2a4165SFrank Mandarino 	return 0;
2723db2a4165SFrank Mandarino }
2724db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2725db2a4165SFrank Mandarino 
2726db2a4165SFrank Mandarino /**
2727db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2728db2a4165SFrank Mandarino  * @kcontrol: mixer control
2729ac11a2b3SMark Brown  * @ucontrol: control element information
2730db2a4165SFrank Mandarino  *
2731f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2732f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2733db2a4165SFrank Mandarino  *
2734db2a4165SFrank Mandarino  * Returns 0 for success.
2735db2a4165SFrank Mandarino  */
2736db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2737db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2738db2a4165SFrank Mandarino {
27394eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27404eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2741db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2742815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2743f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2744815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2745815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
27464eaa9819SJon Smirl 	int max = mc->max;
2747f227b88fSMarkus Pargmann 	int min = mc->min;
2748f227b88fSMarkus Pargmann 	int sign_bit = mc->sign_bit;
2749815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2750815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2751db2a4165SFrank Mandarino 
2752f227b88fSMarkus Pargmann 	if (sign_bit)
2753f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2754f227b88fSMarkus Pargmann 
2755f227b88fSMarkus Pargmann 	ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2756f227b88fSMarkus Pargmann 			shift, sign_bit) - min;
2757f7915d99SPeter Ujfalusi 	if (invert)
2758db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2759a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2760f7915d99SPeter Ujfalusi 
2761f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2762f7915d99SPeter Ujfalusi 		if (reg == reg2)
2763f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2764f227b88fSMarkus Pargmann 				snd_soc_read_signed(codec, reg, mask, rshift,
2765f227b88fSMarkus Pargmann 						sign_bit) - min;
2766f7915d99SPeter Ujfalusi 		else
2767f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2768f227b88fSMarkus Pargmann 				snd_soc_read_signed(codec, reg2, mask, shift,
2769f227b88fSMarkus Pargmann 						sign_bit) - min;
2770f7915d99SPeter Ujfalusi 		if (invert)
2771db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2772a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2773db2a4165SFrank Mandarino 	}
2774db2a4165SFrank Mandarino 
2775db2a4165SFrank Mandarino 	return 0;
2776db2a4165SFrank Mandarino }
2777db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2778db2a4165SFrank Mandarino 
2779db2a4165SFrank Mandarino /**
2780db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2781db2a4165SFrank Mandarino  * @kcontrol: mixer control
2782ac11a2b3SMark Brown  * @ucontrol: control element information
2783db2a4165SFrank Mandarino  *
2784974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2785974815baSPeter Ujfalusi  * control that spans 2 registers.
2786db2a4165SFrank Mandarino  *
2787db2a4165SFrank Mandarino  * Returns 0 for success.
2788db2a4165SFrank Mandarino  */
2789db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2790db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2791db2a4165SFrank Mandarino {
27924eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27934eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2794db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2795815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2796974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2797815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2798815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
27994eaa9819SJon Smirl 	int max = mc->max;
2800f227b88fSMarkus Pargmann 	int min = mc->min;
2801f227b88fSMarkus Pargmann 	unsigned int sign_bit = mc->sign_bit;
2802815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2803815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2804974815baSPeter Ujfalusi 	int err;
2805939d9f16SNenghua Cao 	bool type_2r = false;
2806974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2807974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2808db2a4165SFrank Mandarino 
2809f227b88fSMarkus Pargmann 	if (sign_bit)
2810f227b88fSMarkus Pargmann 		mask = BIT(sign_bit + 1) - 1;
2811f227b88fSMarkus Pargmann 
2812f227b88fSMarkus Pargmann 	val = ((ucontrol->value.integer.value[0] + min) & mask);
2813db2a4165SFrank Mandarino 	if (invert)
2814a7a4ac86SPhilipp Zabel 		val = max - val;
2815db2a4165SFrank Mandarino 	val_mask = mask << shift;
2816db2a4165SFrank Mandarino 	val = val << shift;
2817974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2818f227b88fSMarkus Pargmann 		val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2819db2a4165SFrank Mandarino 		if (invert)
2820a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2821974815baSPeter Ujfalusi 		if (reg == reg2) {
2822db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2823db2a4165SFrank Mandarino 			val |= val2 << rshift;
2824974815baSPeter Ujfalusi 		} else {
2825db2a4165SFrank Mandarino 			val2 = val2 << shift;
2826939d9f16SNenghua Cao 			type_2r = true;
2827974815baSPeter Ujfalusi 		}
2828974815baSPeter Ujfalusi 	}
28296c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
28303ff3f64bSMark Brown 	if (err < 0)
2831db2a4165SFrank Mandarino 		return err;
2832db2a4165SFrank Mandarino 
2833974815baSPeter Ujfalusi 	if (type_2r)
28346c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2835974815baSPeter Ujfalusi 
2836db2a4165SFrank Mandarino 	return err;
2837db2a4165SFrank Mandarino }
2838974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2839db2a4165SFrank Mandarino 
2840e13ac2e9SMark Brown /**
28411d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
28421d99f243SBrian Austin  * @kcontrol: mixer control
28431d99f243SBrian Austin  * @ucontrol: control element information
28441d99f243SBrian Austin  *
28451d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
28461d99f243SBrian Austin  * control that spans 2 registers.
28471d99f243SBrian Austin  *
28481d99f243SBrian Austin  * Returns 0 for success.
28491d99f243SBrian Austin  */
28501d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
28511d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
28521d99f243SBrian Austin {
28531d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
28541d99f243SBrian Austin 	struct soc_mixer_control *mc =
28551d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
28561d99f243SBrian Austin 
28571d99f243SBrian Austin 	unsigned int reg = mc->reg;
28581d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28591d99f243SBrian Austin 	unsigned int shift = mc->shift;
28601d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28611d99f243SBrian Austin 	int max = mc->max;
28621d99f243SBrian Austin 	int min = mc->min;
28631d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
28641d99f243SBrian Austin 
28651d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
28661d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
28671d99f243SBrian Austin 
28681d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
28691d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
28701d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
28711d99f243SBrian Austin 
28721d99f243SBrian Austin 	return 0;
28731d99f243SBrian Austin }
28741d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
28751d99f243SBrian Austin 
28761d99f243SBrian Austin /**
28771d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
28781d99f243SBrian Austin  * @kcontrol: mixer control
28791d99f243SBrian Austin  * @uinfo: control element information
28801d99f243SBrian Austin  *
28811d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
28821d99f243SBrian Austin  *
28831d99f243SBrian Austin  * Returns 0 for success.
28841d99f243SBrian Austin  */
28851d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
28861d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
28871d99f243SBrian Austin {
28881d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
28891d99f243SBrian Austin 	struct soc_mixer_control *mc =
28901d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
28911d99f243SBrian Austin 
28921d99f243SBrian Austin 	unsigned int reg = mc->reg;
28931d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28941d99f243SBrian Austin 	unsigned int shift = mc->shift;
28951d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28961d99f243SBrian Austin 	int max = mc->max;
28971d99f243SBrian Austin 	int min = mc->min;
28981d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
289927f1d759SBrian Austin 	int err = 0;
29001d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
29011d99f243SBrian Austin 
29021d99f243SBrian Austin 	val_mask = mask << shift;
29031d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
29041d99f243SBrian Austin 	val = val << shift;
29051d99f243SBrian Austin 
2906d055852eSMukund Navada 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2907d055852eSMukund Navada 	if (err < 0)
29081d99f243SBrian Austin 		return err;
29091d99f243SBrian Austin 
29101d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
29111d99f243SBrian Austin 		val_mask = mask << rshift;
29121d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
29131d99f243SBrian Austin 		val2 = val2 << rshift;
29141d99f243SBrian Austin 
29151d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
29161d99f243SBrian Austin 			return err;
29171d99f243SBrian Austin 	}
29181d99f243SBrian Austin 	return 0;
29191d99f243SBrian Austin }
29201d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
29211d99f243SBrian Austin 
29221d99f243SBrian Austin /**
2923e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2924e13ac2e9SMark Brown  * @kcontrol: mixer control
2925e13ac2e9SMark Brown  * @uinfo: control element information
2926e13ac2e9SMark Brown  *
2927e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2928e13ac2e9SMark Brown  *
2929e13ac2e9SMark Brown  * Returns 0 for success.
2930e13ac2e9SMark Brown  */
2931e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2932e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2933e13ac2e9SMark Brown {
29344eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29354eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2936d11bb4a9SPeter Ujfalusi 	int platform_max;
29374eaa9819SJon Smirl 	int min = mc->min;
2938e13ac2e9SMark Brown 
2939d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2940d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2941d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2942d11bb4a9SPeter Ujfalusi 
2943e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2944e13ac2e9SMark Brown 	uinfo->count = 2;
2945e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2946d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2947e13ac2e9SMark Brown 	return 0;
2948e13ac2e9SMark Brown }
2949e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2950e13ac2e9SMark Brown 
2951e13ac2e9SMark Brown /**
2952e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2953e13ac2e9SMark Brown  * @kcontrol: mixer control
2954ac11a2b3SMark Brown  * @ucontrol: control element information
2955e13ac2e9SMark Brown  *
2956e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2957e13ac2e9SMark Brown  *
2958e13ac2e9SMark Brown  * Returns 0 for success.
2959e13ac2e9SMark Brown  */
2960e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2961e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2962e13ac2e9SMark Brown {
29634eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29644eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2965e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2966815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
29674eaa9819SJon Smirl 	int min = mc->min;
2968e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2969e13ac2e9SMark Brown 
2970e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2971e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2972e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2973e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2974e13ac2e9SMark Brown 	return 0;
2975e13ac2e9SMark Brown }
2976e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2977e13ac2e9SMark Brown 
2978e13ac2e9SMark Brown /**
2979e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2980e13ac2e9SMark Brown  * @kcontrol: mixer control
2981ac11a2b3SMark Brown  * @ucontrol: control element information
2982e13ac2e9SMark Brown  *
2983e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2984e13ac2e9SMark Brown  *
2985e13ac2e9SMark Brown  * Returns 0 for success.
2986e13ac2e9SMark Brown  */
2987e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2988e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2989e13ac2e9SMark Brown {
29904eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29914eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2992e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2993815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
29944eaa9819SJon Smirl 	int min = mc->min;
299546f5822fSDaniel Ribeiro 	unsigned int val;
2996e13ac2e9SMark Brown 
2997e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2998e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2999e13ac2e9SMark Brown 
30006c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
3001e13ac2e9SMark Brown }
3002e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3003e13ac2e9SMark Brown 
30048c6529dbSLiam Girdwood /**
30056c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
30066c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30076c9d8cf6SAdam Thomson  * @uinfo: control element information
30086c9d8cf6SAdam Thomson  *
30096c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
30106c9d8cf6SAdam Thomson  * mixer control.
30116c9d8cf6SAdam Thomson  *
30126c9d8cf6SAdam Thomson  * returns 0 for success.
30136c9d8cf6SAdam Thomson  */
30146c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
30156c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
30166c9d8cf6SAdam Thomson {
30176c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30186c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
30196c9d8cf6SAdam Thomson 	int platform_max;
30206c9d8cf6SAdam Thomson 	int min = mc->min;
30216c9d8cf6SAdam Thomson 
30226c9d8cf6SAdam Thomson 	if (!mc->platform_max)
30236c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
30246c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
30256c9d8cf6SAdam Thomson 
30266c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
30279bde4f0bSMark Brown 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
30286c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
30296c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
30306c9d8cf6SAdam Thomson 
30316c9d8cf6SAdam Thomson 	return 0;
30326c9d8cf6SAdam Thomson }
30336c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
30346c9d8cf6SAdam Thomson 
30356c9d8cf6SAdam Thomson /**
30366c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
30376c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30386c9d8cf6SAdam Thomson  * @ucontrol: control element information
30396c9d8cf6SAdam Thomson  *
30406c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
30416c9d8cf6SAdam Thomson  *
30426c9d8cf6SAdam Thomson  * Returns 0 for success.
30436c9d8cf6SAdam Thomson  */
30446c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
30456c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30466c9d8cf6SAdam Thomson {
30476c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30486c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
30496c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
30506c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
30519bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
30526c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
30536c9d8cf6SAdam Thomson 	int min = mc->min;
30546c9d8cf6SAdam Thomson 	int max = mc->max;
30556c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
30566c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
30576c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
30589bde4f0bSMark Brown 	int ret;
30596c9d8cf6SAdam Thomson 
30606c9d8cf6SAdam Thomson 	val = ((ucontrol->value.integer.value[0] + min) & mask);
30616c9d8cf6SAdam Thomson 	if (invert)
30626c9d8cf6SAdam Thomson 		val = max - val;
30636c9d8cf6SAdam Thomson 	val_mask = mask << shift;
30646c9d8cf6SAdam Thomson 	val = val << shift;
30656c9d8cf6SAdam Thomson 
30669bde4f0bSMark Brown 	ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
30670eaa6ccaSJoonyoung Shim 	if (ret < 0)
30689bde4f0bSMark Brown 		return ret;
30699bde4f0bSMark Brown 
30709bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
30719bde4f0bSMark Brown 		val = ((ucontrol->value.integer.value[1] + min) & mask);
30729bde4f0bSMark Brown 		if (invert)
30739bde4f0bSMark Brown 			val = max - val;
30749bde4f0bSMark Brown 		val_mask = mask << shift;
30759bde4f0bSMark Brown 		val = val << shift;
30769bde4f0bSMark Brown 
30779bde4f0bSMark Brown 		ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
30789bde4f0bSMark Brown 	}
30799bde4f0bSMark Brown 
30809bde4f0bSMark Brown 	return ret;
30816c9d8cf6SAdam Thomson }
30826c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
30836c9d8cf6SAdam Thomson 
30846c9d8cf6SAdam Thomson /**
30856c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
30866c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30876c9d8cf6SAdam Thomson  * @ucontrol: control element information
30886c9d8cf6SAdam Thomson  *
30896c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
30906c9d8cf6SAdam Thomson  *
30916c9d8cf6SAdam Thomson  * Returns 0 for success.
30926c9d8cf6SAdam Thomson  */
30936c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
30946c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30956c9d8cf6SAdam Thomson {
30966c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30976c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
30986c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
30996c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
31009bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
31016c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
31026c9d8cf6SAdam Thomson 	int min = mc->min;
31036c9d8cf6SAdam Thomson 	int max = mc->max;
31046c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
31056c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
31066c9d8cf6SAdam Thomson 
31076c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
31086c9d8cf6SAdam Thomson 		(snd_soc_read(codec, reg) >> shift) & mask;
31096c9d8cf6SAdam Thomson 	if (invert)
31106c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
31116c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
31126c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
31136c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] - min;
31146c9d8cf6SAdam Thomson 
31159bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
31169bde4f0bSMark Brown 		ucontrol->value.integer.value[1] =
31179bde4f0bSMark Brown 			(snd_soc_read(codec, rreg) >> shift) & mask;
31189bde4f0bSMark Brown 		if (invert)
31199bde4f0bSMark Brown 			ucontrol->value.integer.value[1] =
31209bde4f0bSMark Brown 				max - ucontrol->value.integer.value[1];
31219bde4f0bSMark Brown 		ucontrol->value.integer.value[1] =
31229bde4f0bSMark Brown 			ucontrol->value.integer.value[1] - min;
31239bde4f0bSMark Brown 	}
31249bde4f0bSMark Brown 
31256c9d8cf6SAdam Thomson 	return 0;
31266c9d8cf6SAdam Thomson }
31276c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
31286c9d8cf6SAdam Thomson 
31296c9d8cf6SAdam Thomson /**
3130637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
3131637d3847SPeter Ujfalusi  *
3132637d3847SPeter Ujfalusi  * @codec: where to look for the control
3133637d3847SPeter Ujfalusi  * @name: Name of the control
3134637d3847SPeter Ujfalusi  * @max: new maximum limit
3135637d3847SPeter Ujfalusi  *
3136637d3847SPeter Ujfalusi  * Return 0 for success, else error.
3137637d3847SPeter Ujfalusi  */
3138637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
3139637d3847SPeter Ujfalusi 	const char *name, int max)
3140637d3847SPeter Ujfalusi {
3141f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
3142637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
3143637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
3144637d3847SPeter Ujfalusi 	int found = 0;
3145637d3847SPeter Ujfalusi 	int ret = -EINVAL;
3146637d3847SPeter Ujfalusi 
3147637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
3148637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
3149637d3847SPeter Ujfalusi 		return -EINVAL;
3150637d3847SPeter Ujfalusi 
3151637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
3152637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3153637d3847SPeter Ujfalusi 			found = 1;
3154637d3847SPeter Ujfalusi 			break;
3155637d3847SPeter Ujfalusi 		}
3156637d3847SPeter Ujfalusi 	}
3157637d3847SPeter Ujfalusi 	if (found) {
3158637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3159637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3160d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3161637d3847SPeter Ujfalusi 			ret = 0;
3162637d3847SPeter Ujfalusi 		}
3163637d3847SPeter Ujfalusi 	}
3164637d3847SPeter Ujfalusi 	return ret;
3165637d3847SPeter Ujfalusi }
3166637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3167637d3847SPeter Ujfalusi 
316871d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
316971d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
317071d08516SMark Brown {
317171d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
317271d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
317371d08516SMark Brown 
317471d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
317571d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
317671d08516SMark Brown 
317771d08516SMark Brown 	return 0;
317871d08516SMark Brown }
317971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
318071d08516SMark Brown 
318171d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
318271d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
318371d08516SMark Brown {
318471d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
318571d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
318671d08516SMark Brown 	int ret;
318771d08516SMark Brown 
318871d08516SMark Brown 	if (codec->using_regmap)
318971d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
319071d08516SMark Brown 				      ucontrol->value.bytes.data,
319171d08516SMark Brown 				      params->num_regs * codec->val_bytes);
319271d08516SMark Brown 	else
319371d08516SMark Brown 		ret = -EINVAL;
319471d08516SMark Brown 
3195f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
3196f831b055SMark Brown 	if (ret == 0 && params->mask) {
3197f831b055SMark Brown 		switch (codec->val_bytes) {
3198f831b055SMark Brown 		case 1:
3199f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
3200f831b055SMark Brown 			break;
3201f831b055SMark Brown 		case 2:
3202f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
32038f1ec93aSCharles Keepax 				&= cpu_to_be16(~params->mask);
3204f831b055SMark Brown 			break;
3205f831b055SMark Brown 		case 4:
3206f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
32078f1ec93aSCharles Keepax 				&= cpu_to_be32(~params->mask);
3208f831b055SMark Brown 			break;
3209f831b055SMark Brown 		default:
3210f831b055SMark Brown 			return -EINVAL;
3211f831b055SMark Brown 		}
3212f831b055SMark Brown 	}
3213f831b055SMark Brown 
321471d08516SMark Brown 	return ret;
321571d08516SMark Brown }
321671d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
321771d08516SMark Brown 
321871d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
321971d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
322071d08516SMark Brown {
322171d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
322271d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3223f831b055SMark Brown 	int ret, len;
3224a1a564edSNenghua Cao 	unsigned int val, mask;
3225f831b055SMark Brown 	void *data;
322671d08516SMark Brown 
3227f831b055SMark Brown 	if (!codec->using_regmap)
3228f831b055SMark Brown 		return -EINVAL;
3229f831b055SMark Brown 
3230f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
3231f831b055SMark Brown 
3232b5a8fe43SMark Brown 	data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3233b5a8fe43SMark Brown 	if (!data)
3234b5a8fe43SMark Brown 		return -ENOMEM;
3235b5a8fe43SMark Brown 
3236f831b055SMark Brown 	/*
3237f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3238f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3239f831b055SMark Brown 	 * copy.
3240f831b055SMark Brown 	 */
3241f831b055SMark Brown 	if (params->mask) {
3242f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
3243f831b055SMark Brown 		if (ret != 0)
3244e8b18addSWei Yongjun 			goto out;
3245f831b055SMark Brown 
3246f831b055SMark Brown 		val &= params->mask;
3247f831b055SMark Brown 
3248f831b055SMark Brown 		switch (codec->val_bytes) {
3249f831b055SMark Brown 		case 1:
3250f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3251f831b055SMark Brown 			((u8 *)data)[0] |= val;
3252f831b055SMark Brown 			break;
3253f831b055SMark Brown 		case 2:
3254a1a564edSNenghua Cao 			mask = ~params->mask;
3255a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3256a1a564edSNenghua Cao 							&mask, &mask);
3257a1a564edSNenghua Cao 			if (ret != 0)
3258a1a564edSNenghua Cao 				goto out;
3259a1a564edSNenghua Cao 
3260a1a564edSNenghua Cao 			((u16 *)data)[0] &= mask;
3261a1a564edSNenghua Cao 
3262a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3263a1a564edSNenghua Cao 							&val, &val);
3264a1a564edSNenghua Cao 			if (ret != 0)
3265a1a564edSNenghua Cao 				goto out;
3266a1a564edSNenghua Cao 
3267a1a564edSNenghua Cao 			((u16 *)data)[0] |= val;
3268f831b055SMark Brown 			break;
3269f831b055SMark Brown 		case 4:
3270a1a564edSNenghua Cao 			mask = ~params->mask;
3271a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3272a1a564edSNenghua Cao 							&mask, &mask);
3273a1a564edSNenghua Cao 			if (ret != 0)
3274a1a564edSNenghua Cao 				goto out;
3275a1a564edSNenghua Cao 
3276a1a564edSNenghua Cao 			((u32 *)data)[0] &= mask;
3277a1a564edSNenghua Cao 
3278a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3279a1a564edSNenghua Cao 							&val, &val);
3280a1a564edSNenghua Cao 			if (ret != 0)
3281a1a564edSNenghua Cao 				goto out;
3282a1a564edSNenghua Cao 
3283a1a564edSNenghua Cao 			((u32 *)data)[0] |= val;
3284f831b055SMark Brown 			break;
3285f831b055SMark Brown 		default:
3286e8b18addSWei Yongjun 			ret = -EINVAL;
3287e8b18addSWei Yongjun 			goto out;
3288f831b055SMark Brown 		}
3289f831b055SMark Brown 	}
3290f831b055SMark Brown 
329171d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
3292f831b055SMark Brown 			       data, len);
3293f831b055SMark Brown 
3294e8b18addSWei Yongjun out:
3295f831b055SMark Brown 	kfree(data);
329671d08516SMark Brown 
329771d08516SMark Brown 	return ret;
329871d08516SMark Brown }
329971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
330071d08516SMark Brown 
3301b6f4bb38Sapatard@mandriva.com /**
33024183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
33034183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33044183eed2SKristoffer KARLSSON  * @uinfo: control element information
33054183eed2SKristoffer KARLSSON  *
33064183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
33074183eed2SKristoffer KARLSSON  * span multiple codec registers which together
33084183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
33094183eed2SKristoffer KARLSSON  *
33104183eed2SKristoffer KARLSSON  * Returns 0 for success.
33114183eed2SKristoffer KARLSSON  */
33124183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
33134183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
33144183eed2SKristoffer KARLSSON {
33154183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33164183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33174183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
33184183eed2SKristoffer KARLSSON 	uinfo->count = 1;
33194183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
33204183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
33214183eed2SKristoffer KARLSSON 
33224183eed2SKristoffer KARLSSON 	return 0;
33234183eed2SKristoffer KARLSSON }
33244183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
33254183eed2SKristoffer KARLSSON 
33264183eed2SKristoffer KARLSSON /**
33274183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
33284183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33294183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33304183eed2SKristoffer KARLSSON  *
33314183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
33324183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33334183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33344183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33354183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33364183eed2SKristoffer KARLSSON  *
33374183eed2SKristoffer KARLSSON  * Returns 0 for success.
33384183eed2SKristoffer KARLSSON  */
33394183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
33404183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33414183eed2SKristoffer KARLSSON {
33424183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33434183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33444183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
33454183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33464183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
33474183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
33484183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33494183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33504183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33514183eed2SKristoffer KARLSSON 	long min = mc->min;
33524183eed2SKristoffer KARLSSON 	long max = mc->max;
33534183eed2SKristoffer KARLSSON 	long val = 0;
33544183eed2SKristoffer KARLSSON 	unsigned long regval;
33554183eed2SKristoffer KARLSSON 	unsigned int i;
33564183eed2SKristoffer KARLSSON 
33574183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
33584183eed2SKristoffer KARLSSON 		regval = snd_soc_read(codec, regbase+i) & regwmask;
33594183eed2SKristoffer KARLSSON 		val |= regval << (regwshift*(regcount-i-1));
33604183eed2SKristoffer KARLSSON 	}
33614183eed2SKristoffer KARLSSON 	val &= mask;
33624183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
33634183eed2SKristoffer KARLSSON 		val |= ~mask;
33644183eed2SKristoffer KARLSSON 	if (invert)
33654183eed2SKristoffer KARLSSON 		val = max - val;
33664183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
33674183eed2SKristoffer KARLSSON 
33684183eed2SKristoffer KARLSSON 	return 0;
33694183eed2SKristoffer KARLSSON }
33704183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
33714183eed2SKristoffer KARLSSON 
33724183eed2SKristoffer KARLSSON /**
33734183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
33744183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33754183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33764183eed2SKristoffer KARLSSON  *
33774183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
33784183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33794183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33804183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33814183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33824183eed2SKristoffer KARLSSON  *
33834183eed2SKristoffer KARLSSON  * Returns 0 for success.
33844183eed2SKristoffer KARLSSON  */
33854183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
33864183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33874183eed2SKristoffer KARLSSON {
33884183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33894183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33904183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
33914183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33924183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
33934183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
33944183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33954183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33964183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33974183eed2SKristoffer KARLSSON 	long max = mc->max;
33984183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
33994183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
34004183eed2SKristoffer KARLSSON 	int err;
34014183eed2SKristoffer KARLSSON 
34024183eed2SKristoffer KARLSSON 	if (invert)
34034183eed2SKristoffer KARLSSON 		val = max - val;
34044183eed2SKristoffer KARLSSON 	val &= mask;
34054183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
34064183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
34074183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
34084183eed2SKristoffer KARLSSON 		err = snd_soc_update_bits_locked(codec, regbase+i,
34094183eed2SKristoffer KARLSSON 				regmask, regval);
34104183eed2SKristoffer KARLSSON 		if (err < 0)
34114183eed2SKristoffer KARLSSON 			return err;
34124183eed2SKristoffer KARLSSON 	}
34134183eed2SKristoffer KARLSSON 
34144183eed2SKristoffer KARLSSON 	return 0;
34154183eed2SKristoffer KARLSSON }
34164183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
34174183eed2SKristoffer KARLSSON 
34184183eed2SKristoffer KARLSSON /**
3419dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3420dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3421dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3422dd7b10b3SKristoffer KARLSSON  *
3423dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3424dd7b10b3SKristoffer KARLSSON  *
3425dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3426dd7b10b3SKristoffer KARLSSON  */
3427dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3428dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3429dd7b10b3SKristoffer KARLSSON {
3430dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3431dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3432dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3433dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3434dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3435dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3436dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3437dd7b10b3SKristoffer KARLSSON 	unsigned int val = snd_soc_read(codec, reg) & mask;
3438dd7b10b3SKristoffer KARLSSON 
3439dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3440dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3441dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3442dd7b10b3SKristoffer KARLSSON 
3443dd7b10b3SKristoffer KARLSSON 	return 0;
3444dd7b10b3SKristoffer KARLSSON }
3445dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3446dd7b10b3SKristoffer KARLSSON 
3447dd7b10b3SKristoffer KARLSSON /**
3448dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3449dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3450dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3451dd7b10b3SKristoffer KARLSSON  *
3452dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3453dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3454dd7b10b3SKristoffer KARLSSON  *
3455dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3456dd7b10b3SKristoffer KARLSSON  */
3457dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3458dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3459dd7b10b3SKristoffer KARLSSON {
3460dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3461dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3462dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3463dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3464dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3465dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3466dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3467dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3468dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3469dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3470dd7b10b3SKristoffer KARLSSON 	int err;
3471dd7b10b3SKristoffer KARLSSON 
3472dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3473dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3474dd7b10b3SKristoffer KARLSSON 		return err;
3475dd7b10b3SKristoffer KARLSSON 
3476dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3477dd7b10b3SKristoffer KARLSSON 	return err;
3478dd7b10b3SKristoffer KARLSSON }
3479dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3480dd7b10b3SKristoffer KARLSSON 
3481dd7b10b3SKristoffer KARLSSON /**
34828c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
34838c6529dbSLiam Girdwood  * @dai: DAI
34848c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
34858c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
34868c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
34878c6529dbSLiam Girdwood  *
34888c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
34898c6529dbSLiam Girdwood  */
34908c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
34918c6529dbSLiam Girdwood 	unsigned int freq, int dir)
34928c6529dbSLiam Girdwood {
3493f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3494f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3495ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3496da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3497ec4ee52aSMark Brown 						      freq, dir);
34988c6529dbSLiam Girdwood 	else
34991104a9c8SMark Brown 		return -ENOTSUPP;
35008c6529dbSLiam Girdwood }
35018c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
35028c6529dbSLiam Girdwood 
35038c6529dbSLiam Girdwood /**
3504ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3505ec4ee52aSMark Brown  * @codec: CODEC
3506ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3507da1c6ea6SMark Brown  * @source: Source for the clock
3508ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3509ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3510ec4ee52aSMark Brown  *
3511ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3512ec4ee52aSMark Brown  */
3513ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3514da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3515ec4ee52aSMark Brown {
3516ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3517da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3518da1c6ea6SMark Brown 						 freq, dir);
3519ec4ee52aSMark Brown 	else
35201104a9c8SMark Brown 		return -ENOTSUPP;
3521ec4ee52aSMark Brown }
3522ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3523ec4ee52aSMark Brown 
3524ec4ee52aSMark Brown /**
35258c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
35268c6529dbSLiam Girdwood  * @dai: DAI
3527ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
35288c6529dbSLiam Girdwood  * @div: new clock divisor.
35298c6529dbSLiam Girdwood  *
35308c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
35318c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
35328c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
35338c6529dbSLiam Girdwood  */
35348c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
35358c6529dbSLiam Girdwood 	int div_id, int div)
35368c6529dbSLiam Girdwood {
3537f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3538f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
35398c6529dbSLiam Girdwood 	else
35408c6529dbSLiam Girdwood 		return -EINVAL;
35418c6529dbSLiam Girdwood }
35428c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
35438c6529dbSLiam Girdwood 
35448c6529dbSLiam Girdwood /**
35458c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
35468c6529dbSLiam Girdwood  * @dai: DAI
35478c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
354885488037SMark Brown  * @source: DAI specific source for the PLL
35498c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
35508c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
35518c6529dbSLiam Girdwood  *
35528c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
35538c6529dbSLiam Girdwood  */
355485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
355585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
35568c6529dbSLiam Girdwood {
3557f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3558f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
355985488037SMark Brown 					 freq_in, freq_out);
3560ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3561ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3562ec4ee52aSMark Brown 						   freq_in, freq_out);
35638c6529dbSLiam Girdwood 	else
35648c6529dbSLiam Girdwood 		return -EINVAL;
35658c6529dbSLiam Girdwood }
35668c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
35678c6529dbSLiam Girdwood 
3568ec4ee52aSMark Brown /*
3569ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3570ec4ee52aSMark Brown  * @codec: CODEC
3571ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3572ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3573ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3574ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3575ec4ee52aSMark Brown  *
3576ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3577ec4ee52aSMark Brown  */
3578ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3579ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3580ec4ee52aSMark Brown {
3581ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3582ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3583ec4ee52aSMark Brown 					      freq_in, freq_out);
3584ec4ee52aSMark Brown 	else
3585ec4ee52aSMark Brown 		return -EINVAL;
3586ec4ee52aSMark Brown }
3587ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3588ec4ee52aSMark Brown 
35898c6529dbSLiam Girdwood /**
3590e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3591e54cf76bSLiam Girdwood  * @dai: DAI
3592e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
3593e54cf76bSLiam Girdwood  *
3594e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
3595e54cf76bSLiam Girdwood  */
3596e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3597e54cf76bSLiam Girdwood {
3598e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
3599e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
3600e54cf76bSLiam Girdwood 	else
3601e54cf76bSLiam Girdwood 		return -EINVAL;
3602e54cf76bSLiam Girdwood }
3603e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3604e54cf76bSLiam Girdwood 
3605e54cf76bSLiam Girdwood /**
36068c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
36078c6529dbSLiam Girdwood  * @dai: DAI
36088c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
36098c6529dbSLiam Girdwood  *
36108c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
36118c6529dbSLiam Girdwood  */
36128c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
36138c6529dbSLiam Girdwood {
36145e4ba569SShawn Guo 	if (dai->driver == NULL)
36158c6529dbSLiam Girdwood 		return -EINVAL;
36165e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
36175e4ba569SShawn Guo 		return -ENOTSUPP;
36185e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
36198c6529dbSLiam Girdwood }
36208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
36218c6529dbSLiam Girdwood 
36228c6529dbSLiam Girdwood /**
362389c67857SXiubo Li  * snd_soc_of_xlate_tdm_slot - generate tx/rx slot mask.
362489c67857SXiubo Li  * @slots: Number of slots in use.
362589c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
362689c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
362789c67857SXiubo Li  *
362889c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
362989c67857SXiubo Li  */
363089c67857SXiubo Li static int snd_soc_of_xlate_tdm_slot_mask(unsigned int slots,
363189c67857SXiubo Li 					  unsigned int *tx_mask,
363289c67857SXiubo Li 					  unsigned int *rx_mask)
363389c67857SXiubo Li {
363489c67857SXiubo Li 	if (*tx_mask || *rx_mask)
363589c67857SXiubo Li 		return 0;
363689c67857SXiubo Li 
363789c67857SXiubo Li 	if (!slots)
363889c67857SXiubo Li 		return -EINVAL;
363989c67857SXiubo Li 
364089c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
364189c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
364289c67857SXiubo Li 
364389c67857SXiubo Li 	return 0;
364489c67857SXiubo Li }
364589c67857SXiubo Li 
364689c67857SXiubo Li /**
36478c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
36488c6529dbSLiam Girdwood  * @dai: DAI
3649a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3650a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
36518c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3652a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
36538c6529dbSLiam Girdwood  *
36548c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
36558c6529dbSLiam Girdwood  * specific.
36568c6529dbSLiam Girdwood  */
36578c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3658a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
36598c6529dbSLiam Girdwood {
366089c67857SXiubo Li 	if (dai->driver && dai->driver->ops->of_xlate_tdm_slot_mask)
366189c67857SXiubo Li 		dai->driver->ops->of_xlate_tdm_slot_mask(slots,
366289c67857SXiubo Li 						&tx_mask, &rx_mask);
366389c67857SXiubo Li 	else
366489c67857SXiubo Li 		snd_soc_of_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
366589c67857SXiubo Li 
3666f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3667f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3668a5479e38SDaniel Ribeiro 				slots, slot_width);
36698c6529dbSLiam Girdwood 	else
3670b2cbb6e1SXiubo Li 		return -ENOTSUPP;
36718c6529dbSLiam Girdwood }
36728c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
36738c6529dbSLiam Girdwood 
36748c6529dbSLiam Girdwood /**
3675472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3676472df3cbSBarry Song  * @dai: DAI
3677472df3cbSBarry Song  * @tx_num: how many TX channels
3678472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3679472df3cbSBarry Song  *           0~num-1 uses
3680472df3cbSBarry Song  * @rx_num: how many RX channels
3681472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3682472df3cbSBarry Song  *           0~num-1 uses
3683472df3cbSBarry Song  *
3684472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3685472df3cbSBarry Song  */
3686472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3687472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3688472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3689472df3cbSBarry Song {
3690f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3691f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3692472df3cbSBarry Song 			rx_num, rx_slot);
3693472df3cbSBarry Song 	else
3694472df3cbSBarry Song 		return -EINVAL;
3695472df3cbSBarry Song }
3696472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3697472df3cbSBarry Song 
3698472df3cbSBarry Song /**
36998c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
37008c6529dbSLiam Girdwood  * @dai: DAI
37018c6529dbSLiam Girdwood  * @tristate: tristate enable
37028c6529dbSLiam Girdwood  *
37038c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
37048c6529dbSLiam Girdwood  */
37058c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
37068c6529dbSLiam Girdwood {
3707f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3708f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
37098c6529dbSLiam Girdwood 	else
37108c6529dbSLiam Girdwood 		return -EINVAL;
37118c6529dbSLiam Girdwood }
37128c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
37138c6529dbSLiam Girdwood 
37148c6529dbSLiam Girdwood /**
37158c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
37168c6529dbSLiam Girdwood  * @dai: DAI
37178c6529dbSLiam Girdwood  * @mute: mute enable
3718da18396fSMark Brown  * @direction: stream to mute
37198c6529dbSLiam Girdwood  *
37208c6529dbSLiam Girdwood  * Mutes the DAI DAC.
37218c6529dbSLiam Girdwood  */
3722da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3723da18396fSMark Brown 			     int direction)
37248c6529dbSLiam Girdwood {
3725da18396fSMark Brown 	if (!dai->driver)
3726da18396fSMark Brown 		return -ENOTSUPP;
3727da18396fSMark Brown 
3728da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
3729da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
3730da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3731da18396fSMark Brown 		 dai->driver->ops->digital_mute)
3732f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
37338c6529dbSLiam Girdwood 	else
373404570c62SMark Brown 		return -ENOTSUPP;
37358c6529dbSLiam Girdwood }
37368c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
37378c6529dbSLiam Girdwood 
3738c5af3a2eSMark Brown /**
3739c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3740c5af3a2eSMark Brown  *
3741ac11a2b3SMark Brown  * @card: Card to register
3742c5af3a2eSMark Brown  *
3743c5af3a2eSMark Brown  */
374470a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3745c5af3a2eSMark Brown {
3746b19e6e7bSMark Brown 	int i, ret;
3747f0fba2adSLiam Girdwood 
3748c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3749c5af3a2eSMark Brown 		return -EINVAL;
3750c5af3a2eSMark Brown 
37515a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
37525a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
37535a504963SStephen Warren 
37545a504963SStephen Warren 		/*
37555a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
37565a504963SStephen Warren 		 * not both or neither.
37575a504963SStephen Warren 		 */
37585a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
375910e8aa9aSMichał Mirosław 			dev_err(card->dev,
376010e8aa9aSMichał Mirosław 				"ASoC: Neither/both codec name/of_node are set for %s\n",
376110e8aa9aSMichał Mirosław 				link->name);
37625a504963SStephen Warren 			return -EINVAL;
37635a504963SStephen Warren 		}
3764bc92657aSStephen Warren 		/* Codec DAI name must be specified */
3765bc92657aSStephen Warren 		if (!link->codec_dai_name) {
376610e8aa9aSMichał Mirosław 			dev_err(card->dev,
376710e8aa9aSMichał Mirosław 				"ASoC: codec_dai_name not set for %s\n",
376810e8aa9aSMichał Mirosław 				link->name);
3769bc92657aSStephen Warren 			return -EINVAL;
3770bc92657aSStephen Warren 		}
37715a504963SStephen Warren 
37725a504963SStephen Warren 		/*
37735a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
37745a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
37755a504963SStephen Warren 		 */
37765a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
377710e8aa9aSMichał Mirosław 			dev_err(card->dev,
377810e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
377910e8aa9aSMichał Mirosław 				link->name);
37805a504963SStephen Warren 			return -EINVAL;
37815a504963SStephen Warren 		}
37825a504963SStephen Warren 
37835a504963SStephen Warren 		/*
3784bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3785bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3786bc92657aSStephen Warren 		 * name alone..
37875a504963SStephen Warren 		 */
3788bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
378910e8aa9aSMichał Mirosław 			dev_err(card->dev,
379010e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
379110e8aa9aSMichał Mirosław 				link->name);
3792bc92657aSStephen Warren 			return -EINVAL;
3793bc92657aSStephen Warren 		}
3794bc92657aSStephen Warren 		/*
3795bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3796bc92657aSStephen Warren 		 * specified
3797bc92657aSStephen Warren 		 */
3798bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3799bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
380010e8aa9aSMichał Mirosław 			dev_err(card->dev,
380110e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
380210e8aa9aSMichał Mirosław 				link->name);
38035a504963SStephen Warren 			return -EINVAL;
38045a504963SStephen Warren 		}
38055a504963SStephen Warren 	}
38065a504963SStephen Warren 
3807ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3808ed77cc12SMark Brown 
3809111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3810111c6419SStephen Warren 
3811150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3812150dd2f8SVinod Koul 
3813181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3814181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
38152eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3816f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3817f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3818f0fba2adSLiam Girdwood 		return -ENOMEM;
3819a7dbb603SLiam Girdwood 	card->num_rtd = 0;
38202eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3821f0fba2adSLiam Girdwood 
3822f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3823f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3824f0fba2adSLiam Girdwood 
3825c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3826db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3827c5af3a2eSMark Brown 	card->instantiated = 0;
3828f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3829a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3830c5af3a2eSMark Brown 
3831b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3832b19e6e7bSMark Brown 	if (ret != 0)
3833b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3834c5af3a2eSMark Brown 
3835988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
3836988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
3837988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3838988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3839988e8cc4SNicolin Chen 		if (!codec_dai->active)
3840988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(codec_dai->dev);
3841988e8cc4SNicolin Chen 		if (!cpu_dai->active)
3842988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
3843988e8cc4SNicolin Chen 	}
3844988e8cc4SNicolin Chen 
3845b19e6e7bSMark Brown 	return ret;
3846c5af3a2eSMark Brown }
384770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3848c5af3a2eSMark Brown 
3849c5af3a2eSMark Brown /**
3850c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3851c5af3a2eSMark Brown  *
3852ac11a2b3SMark Brown  * @card: Card to unregister
3853c5af3a2eSMark Brown  *
3854c5af3a2eSMark Brown  */
385570a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3856c5af3a2eSMark Brown {
3857b0e26485SVinod Koul 	if (card->instantiated)
3858b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3859f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3860c5af3a2eSMark Brown 
3861c5af3a2eSMark Brown 	return 0;
3862c5af3a2eSMark Brown }
386370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3864c5af3a2eSMark Brown 
3865f0fba2adSLiam Girdwood /*
3866f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3867f0fba2adSLiam Girdwood  * and sanitizing names.
3868f0fba2adSLiam Girdwood  */
38690b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3870f0fba2adSLiam Girdwood {
3871f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3872f0fba2adSLiam Girdwood 	int id1, id2;
3873f0fba2adSLiam Girdwood 
3874f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3875f0fba2adSLiam Girdwood 		return NULL;
3876f0fba2adSLiam Girdwood 
387758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3878f0fba2adSLiam Girdwood 
3879f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3880f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3881f0fba2adSLiam Girdwood 	if (found) {
3882f0fba2adSLiam Girdwood 		/* get ID */
3883f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3884f0fba2adSLiam Girdwood 
3885f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3886f0fba2adSLiam Girdwood 			if (*id == -1)
3887f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3888f0fba2adSLiam Girdwood 		}
3889f0fba2adSLiam Girdwood 
3890f0fba2adSLiam Girdwood 	} else {
3891f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3892f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3893f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3894f0fba2adSLiam Girdwood 
3895f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
389605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3897f0fba2adSLiam Girdwood 
3898f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3899f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
390058818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3901f0fba2adSLiam Girdwood 		} else
3902f0fba2adSLiam Girdwood 			*id = 0;
3903f0fba2adSLiam Girdwood 	}
3904f0fba2adSLiam Girdwood 
3905f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3906f0fba2adSLiam Girdwood }
3907f0fba2adSLiam Girdwood 
3908f0fba2adSLiam Girdwood /*
3909f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3910f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3911f0fba2adSLiam Girdwood  */
3912f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3913f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3914f0fba2adSLiam Girdwood {
3915f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
391610e8aa9aSMichał Mirosław 		dev_err(dev,
391710e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
391810e8aa9aSMichał Mirosław 			dev_name(dev));
3919f0fba2adSLiam Girdwood 		return NULL;
3920f0fba2adSLiam Girdwood 	}
3921f0fba2adSLiam Girdwood 
3922f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3923f0fba2adSLiam Girdwood }
3924f0fba2adSLiam Girdwood 
39259115171aSMark Brown /**
392632c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
392732c9ba54SLars-Peter Clausen  *
392832c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
392932c9ba54SLars-Peter Clausen  */
393032c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
393132c9ba54SLars-Peter Clausen {
39325c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
393332c9ba54SLars-Peter Clausen 
39345c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
393532c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
393632c9ba54SLars-Peter Clausen 			dai->name);
39375c1d5f09SLars-Peter Clausen 		list_del(&dai->list);
393832c9ba54SLars-Peter Clausen 		kfree(dai->name);
393932c9ba54SLars-Peter Clausen 		kfree(dai);
394032c9ba54SLars-Peter Clausen 	}
394132c9ba54SLars-Peter Clausen }
394232c9ba54SLars-Peter Clausen 
394332c9ba54SLars-Peter Clausen /**
394432c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
39459115171aSMark Brown  *
39466106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
39476cc240f3SLars-Peter Clausen  * @codec: The CODEC that the DAIs are registered for, NULL if the component is
39486cc240f3SLars-Peter Clausen  *         not a CODEC.
39496106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
395032c9ba54SLars-Peter Clausen  * @count: Number of DAIs
395132c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
395232c9ba54SLars-Peter Clausen  *                     parent's name.
39539115171aSMark Brown  */
395432c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
39556cc240f3SLars-Peter Clausen 	struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
39566cc240f3SLars-Peter Clausen 	size_t count, bool legacy_dai_naming)
39579115171aSMark Brown {
39586106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3959f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
396032c9ba54SLars-Peter Clausen 	unsigned int i;
396132c9ba54SLars-Peter Clausen 	int ret;
39629115171aSMark Brown 
396332c9ba54SLars-Peter Clausen 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
396432c9ba54SLars-Peter Clausen 
396532c9ba54SLars-Peter Clausen 	for (i = 0; i < count; i++) {
39669115171aSMark Brown 
3967f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
396832c9ba54SLars-Peter Clausen 		if (dai == NULL) {
396932c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
397032c9ba54SLars-Peter Clausen 			goto err;
397132c9ba54SLars-Peter Clausen 		}
39726335d055SEric Miao 
397332c9ba54SLars-Peter Clausen 		/*
397432c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
397532c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
397632c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
397732c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
397832c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
397932c9ba54SLars-Peter Clausen 		 * component-less anymore.
398032c9ba54SLars-Peter Clausen 		 */
398132c9ba54SLars-Peter Clausen 		if (count == 1 && legacy_dai_naming) {
3982f0fba2adSLiam Girdwood 			dai->name = fmt_single_name(dev, &dai->id);
398332c9ba54SLars-Peter Clausen 		} else {
398432c9ba54SLars-Peter Clausen 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
398532c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
398632c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
398732c9ba54SLars-Peter Clausen 			else
398832c9ba54SLars-Peter Clausen 				dai->id = i;
398932c9ba54SLars-Peter Clausen 		}
3990f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
3991f0fba2adSLiam Girdwood 			kfree(dai);
399232c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
399332c9ba54SLars-Peter Clausen 			goto err;
3994f0fba2adSLiam Girdwood 		}
3995f0fba2adSLiam Girdwood 
39966106d129SLars-Peter Clausen 		dai->component = component;
39976cc240f3SLars-Peter Clausen 		dai->codec = codec;
3998f0fba2adSLiam Girdwood 		dai->dev = dev;
399932c9ba54SLars-Peter Clausen 		dai->driver = &dai_drv[i];
4000be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
4001f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
4002f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
40039115171aSMark Brown 
40045f800080SPeter Ujfalusi 		if (!dai->codec)
40055f800080SPeter Ujfalusi 			dai->dapm.idle_bias_off = 1;
40065f800080SPeter Ujfalusi 
40071438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
40089115171aSMark Brown 
4009f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
4010f0fba2adSLiam Girdwood 	}
4011f0fba2adSLiam Girdwood 
40129115171aSMark Brown 	return 0;
40139115171aSMark Brown 
40149115171aSMark Brown err:
401532c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
40169115171aSMark Brown 
40179115171aSMark Brown 	return ret;
40189115171aSMark Brown }
40199115171aSMark Brown 
40209115171aSMark Brown /**
4021d191bd8dSKuninori Morimoto  * snd_soc_register_component - Register a component with the ASoC core
4022d191bd8dSKuninori Morimoto  *
4023d191bd8dSKuninori Morimoto  */
4024d191bd8dSKuninori Morimoto static int
4025d191bd8dSKuninori Morimoto __snd_soc_register_component(struct device *dev,
4026d191bd8dSKuninori Morimoto 			     struct snd_soc_component *cmpnt,
4027d191bd8dSKuninori Morimoto 			     const struct snd_soc_component_driver *cmpnt_drv,
40286cc240f3SLars-Peter Clausen 			     struct snd_soc_codec *codec,
4029d191bd8dSKuninori Morimoto 			     struct snd_soc_dai_driver *dai_drv,
4030d191bd8dSKuninori Morimoto 			     int num_dai, bool allow_single_dai)
4031d191bd8dSKuninori Morimoto {
4032d191bd8dSKuninori Morimoto 	int ret;
4033d191bd8dSKuninori Morimoto 
4034d191bd8dSKuninori Morimoto 	dev_dbg(dev, "component register %s\n", dev_name(dev));
4035d191bd8dSKuninori Morimoto 
4036d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4037d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to connecting component\n");
4038d191bd8dSKuninori Morimoto 		return -ENOMEM;
4039d191bd8dSKuninori Morimoto 	}
4040d191bd8dSKuninori Morimoto 
4041d191bd8dSKuninori Morimoto 	cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4042d191bd8dSKuninori Morimoto 	if (!cmpnt->name) {
4043d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to simplifying name\n");
4044d191bd8dSKuninori Morimoto 		return -ENOMEM;
4045d191bd8dSKuninori Morimoto 	}
4046d191bd8dSKuninori Morimoto 
4047d191bd8dSKuninori Morimoto 	cmpnt->dev	= dev;
4048d191bd8dSKuninori Morimoto 	cmpnt->driver	= cmpnt_drv;
40496833c452SKuninori Morimoto 	cmpnt->dai_drv	= dai_drv;
4050d191bd8dSKuninori Morimoto 	cmpnt->num_dai	= num_dai;
40511438c2f6SLars-Peter Clausen 	INIT_LIST_HEAD(&cmpnt->dai_list);
4052d191bd8dSKuninori Morimoto 
40536cc240f3SLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
40546cc240f3SLars-Peter Clausen 		allow_single_dai);
4055d191bd8dSKuninori Morimoto 	if (ret < 0) {
4056d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4057d191bd8dSKuninori Morimoto 		goto error_component_name;
4058d191bd8dSKuninori Morimoto 	}
4059d191bd8dSKuninori Morimoto 
4060d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
4061d191bd8dSKuninori Morimoto 	list_add(&cmpnt->list, &component_list);
4062d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
4063d191bd8dSKuninori Morimoto 
4064d191bd8dSKuninori Morimoto 	dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4065d191bd8dSKuninori Morimoto 
4066d191bd8dSKuninori Morimoto 	return ret;
4067d191bd8dSKuninori Morimoto 
4068d191bd8dSKuninori Morimoto error_component_name:
4069d191bd8dSKuninori Morimoto 	kfree(cmpnt->name);
4070d191bd8dSKuninori Morimoto 
4071d191bd8dSKuninori Morimoto 	return ret;
4072d191bd8dSKuninori Morimoto }
4073d191bd8dSKuninori Morimoto 
4074d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
4075d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
4076d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
4077d191bd8dSKuninori Morimoto 			       int num_dai)
4078d191bd8dSKuninori Morimoto {
4079d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4080d191bd8dSKuninori Morimoto 
4081d191bd8dSKuninori Morimoto 	cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4082d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4083d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
4084d191bd8dSKuninori Morimoto 		return -ENOMEM;
4085d191bd8dSKuninori Morimoto 	}
4086d191bd8dSKuninori Morimoto 
40873d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
40883d59400fSLars-Peter Clausen 
40896cc240f3SLars-Peter Clausen 	return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
4090d191bd8dSKuninori Morimoto 					    dai_drv, num_dai, true);
4091d191bd8dSKuninori Morimoto }
4092d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
4093d191bd8dSKuninori Morimoto 
4094d191bd8dSKuninori Morimoto /**
4095d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
4096d191bd8dSKuninori Morimoto  *
4097d191bd8dSKuninori Morimoto  */
4098d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
4099d191bd8dSKuninori Morimoto {
4100d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4101d191bd8dSKuninori Morimoto 
4102d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
4103d191bd8dSKuninori Morimoto 		if (dev == cmpnt->dev)
4104d191bd8dSKuninori Morimoto 			goto found;
4105d191bd8dSKuninori Morimoto 	}
4106d191bd8dSKuninori Morimoto 	return;
4107d191bd8dSKuninori Morimoto 
4108d191bd8dSKuninori Morimoto found:
410932c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(cmpnt);
4110d191bd8dSKuninori Morimoto 
4111d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
4112d191bd8dSKuninori Morimoto 	list_del(&cmpnt->list);
4113d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
4114d191bd8dSKuninori Morimoto 
4115d191bd8dSKuninori Morimoto 	dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4116d191bd8dSKuninori Morimoto 	kfree(cmpnt->name);
4117d191bd8dSKuninori Morimoto }
4118d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4119d191bd8dSKuninori Morimoto 
4120d191bd8dSKuninori Morimoto /**
412171a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
412271a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
412371a45cdaSLars-Peter Clausen  * @platform: The platform to add
412471a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
412512a48a8cSMark Brown  */
412671a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4127d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
412812a48a8cSMark Brown {
4129f0fba2adSLiam Girdwood 	/* create platform component name */
4130f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
4131b5c745fbSDan Carpenter 	if (platform->name == NULL)
4132f0fba2adSLiam Girdwood 		return -ENOMEM;
4133f0fba2adSLiam Girdwood 
4134f0fba2adSLiam Girdwood 	platform->dev = dev;
4135f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
4136b7950641SLiam Girdwood 	platform->dapm.dev = dev;
4137b7950641SLiam Girdwood 	platform->dapm.platform = platform;
413864a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
4139cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
414012a48a8cSMark Brown 
414112a48a8cSMark Brown 	mutex_lock(&client_mutex);
414212a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
414312a48a8cSMark Brown 	mutex_unlock(&client_mutex);
414412a48a8cSMark Brown 
4145f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
414612a48a8cSMark Brown 
414712a48a8cSMark Brown 	return 0;
414812a48a8cSMark Brown }
414971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
415071a45cdaSLars-Peter Clausen 
415171a45cdaSLars-Peter Clausen /**
415271a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
415371a45cdaSLars-Peter Clausen  *
415471a45cdaSLars-Peter Clausen  * @platform: platform to register
415571a45cdaSLars-Peter Clausen  */
415671a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
415771a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
415871a45cdaSLars-Peter Clausen {
415971a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
416071a45cdaSLars-Peter Clausen 	int ret;
416171a45cdaSLars-Peter Clausen 
416271a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
416371a45cdaSLars-Peter Clausen 
416471a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
416571a45cdaSLars-Peter Clausen 	if (platform == NULL)
416671a45cdaSLars-Peter Clausen 		return -ENOMEM;
416771a45cdaSLars-Peter Clausen 
416871a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
416971a45cdaSLars-Peter Clausen 	if (ret)
417071a45cdaSLars-Peter Clausen 		kfree(platform);
417171a45cdaSLars-Peter Clausen 
417271a45cdaSLars-Peter Clausen 	return ret;
417371a45cdaSLars-Peter Clausen }
417412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
417512a48a8cSMark Brown 
417612a48a8cSMark Brown /**
417771a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
417871a45cdaSLars-Peter Clausen  * @platform: the platform to remove
417971a45cdaSLars-Peter Clausen  */
418071a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
418171a45cdaSLars-Peter Clausen {
418271a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
418371a45cdaSLars-Peter Clausen 	list_del(&platform->list);
418471a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
418571a45cdaSLars-Peter Clausen 
418671a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
418771a45cdaSLars-Peter Clausen 		platform->name);
418871a45cdaSLars-Peter Clausen 	kfree(platform->name);
418971a45cdaSLars-Peter Clausen }
419071a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
419171a45cdaSLars-Peter Clausen 
419271a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
419371a45cdaSLars-Peter Clausen {
419471a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
419571a45cdaSLars-Peter Clausen 
419671a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
419771a45cdaSLars-Peter Clausen 		if (dev == platform->dev)
419871a45cdaSLars-Peter Clausen 			return platform;
419971a45cdaSLars-Peter Clausen 	}
420071a45cdaSLars-Peter Clausen 
420171a45cdaSLars-Peter Clausen 	return NULL;
420271a45cdaSLars-Peter Clausen }
420371a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
420471a45cdaSLars-Peter Clausen 
420571a45cdaSLars-Peter Clausen /**
420612a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
420712a48a8cSMark Brown  *
4208ac11a2b3SMark Brown  * @platform: platform to unregister
420912a48a8cSMark Brown  */
4210f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
421112a48a8cSMark Brown {
4212f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
4213f0fba2adSLiam Girdwood 
421471a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
421571a45cdaSLars-Peter Clausen 	if (!platform)
4216f0fba2adSLiam Girdwood 		return;
4217f0fba2adSLiam Girdwood 
421871a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
4219f0fba2adSLiam Girdwood 	kfree(platform);
422012a48a8cSMark Brown }
422112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
422212a48a8cSMark Brown 
4223151ab22cSMark Brown static u64 codec_format_map[] = {
4224151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4225151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4226151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4227151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4228151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4229151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4230151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4231151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4232151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4233151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4234151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4235151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4236151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4237151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4238151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4239151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4240151ab22cSMark Brown };
4241151ab22cSMark Brown 
4242151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
4243151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
4244151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
4245151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
4246151ab22cSMark Brown  */
4247151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4248151ab22cSMark Brown {
4249151ab22cSMark Brown 	int i;
4250151ab22cSMark Brown 
4251151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4252151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
4253151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
4254151ab22cSMark Brown }
4255151ab22cSMark Brown 
42560d0cf00aSMark Brown /**
42570d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
42580d0cf00aSMark Brown  *
4259ac11a2b3SMark Brown  * @codec: codec to register
42600d0cf00aSMark Brown  */
4261f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
4262001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
4263001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
4264001ae4c0SMark Brown 			   int num_dai)
42650d0cf00aSMark Brown {
4266f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4267f0fba2adSLiam Girdwood 	int ret, i;
4268151ab22cSMark Brown 
4269f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
42700d0cf00aSMark Brown 
4271f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4272f0fba2adSLiam Girdwood 	if (codec == NULL)
4273f0fba2adSLiam Girdwood 		return -ENOMEM;
42740d0cf00aSMark Brown 
4275f0fba2adSLiam Girdwood 	/* create CODEC component name */
4276f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
4277f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
4278f790b94dSKuninori Morimoto 		ret = -ENOMEM;
4279f790b94dSKuninori Morimoto 		goto fail_codec;
4280151ab22cSMark Brown 	}
4281151ab22cSMark Brown 
4282c3acec26SMark Brown 	codec->write = codec_drv->write;
4283c3acec26SMark Brown 	codec->read = codec_drv->read;
42841500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
42851500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
42868020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
42873d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4288ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4289ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
4290ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
4291474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
429264a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
4293f0fba2adSLiam Girdwood 	codec->dev = dev;
4294f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
4295f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
4296f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
4297f0fba2adSLiam Girdwood 
4298f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
4299f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
4300f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
4301f0fba2adSLiam Girdwood 	}
4302f0fba2adSLiam Girdwood 
4303054880feSMark Brown 	mutex_lock(&client_mutex);
4304054880feSMark Brown 	list_add(&codec->list, &codec_list);
4305054880feSMark Brown 	mutex_unlock(&client_mutex);
4306054880feSMark Brown 
4307d191bd8dSKuninori Morimoto 	/* register component */
4308d191bd8dSKuninori Morimoto 	ret = __snd_soc_register_component(dev, &codec->component,
4309d191bd8dSKuninori Morimoto 					   &codec_drv->component_driver,
43106cc240f3SLars-Peter Clausen 					   codec, dai_drv, num_dai, false);
43115acd7dfbSKuninori Morimoto 	if (ret < 0) {
4312d191bd8dSKuninori Morimoto 		dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
43135acd7dfbSKuninori Morimoto 		goto fail_codec_name;
431426b01ccdSMark Brown 	}
4315f0fba2adSLiam Girdwood 
4316f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
43170d0cf00aSMark Brown 	return 0;
4318f0fba2adSLiam Girdwood 
4319f790b94dSKuninori Morimoto fail_codec_name:
4320e1328a83SKuninori Morimoto 	mutex_lock(&client_mutex);
4321e1328a83SKuninori Morimoto 	list_del(&codec->list);
4322e1328a83SKuninori Morimoto 	mutex_unlock(&client_mutex);
4323e1328a83SKuninori Morimoto 
4324f0fba2adSLiam Girdwood 	kfree(codec->name);
4325f790b94dSKuninori Morimoto fail_codec:
4326f0fba2adSLiam Girdwood 	kfree(codec);
4327f0fba2adSLiam Girdwood 	return ret;
43280d0cf00aSMark Brown }
43290d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
43300d0cf00aSMark Brown 
43310d0cf00aSMark Brown /**
43320d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
43330d0cf00aSMark Brown  *
4334ac11a2b3SMark Brown  * @codec: codec to unregister
43350d0cf00aSMark Brown  */
4336f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
43370d0cf00aSMark Brown {
4338f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4339f0fba2adSLiam Girdwood 
4340f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
4341f0fba2adSLiam Girdwood 		if (dev == codec->dev)
4342f0fba2adSLiam Girdwood 			goto found;
4343f0fba2adSLiam Girdwood 	}
4344f0fba2adSLiam Girdwood 	return;
4345f0fba2adSLiam Girdwood 
4346f0fba2adSLiam Girdwood found:
4347d191bd8dSKuninori Morimoto 	snd_soc_unregister_component(dev);
4348f0fba2adSLiam Girdwood 
43490d0cf00aSMark Brown 	mutex_lock(&client_mutex);
43500d0cf00aSMark Brown 	list_del(&codec->list);
43510d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
43520d0cf00aSMark Brown 
4353f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4354f0fba2adSLiam Girdwood 
43557a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
43561aafcd4dSDimitris Papastamos 	kfree(codec->name);
4357f0fba2adSLiam Girdwood 	kfree(codec);
43580d0cf00aSMark Brown }
43590d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
43600d0cf00aSMark Brown 
4361bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4362bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4363bec4fa05SStephen Warren 			       const char *propname)
4364bec4fa05SStephen Warren {
4365bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
4366bec4fa05SStephen Warren 	int ret;
4367bec4fa05SStephen Warren 
4368bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4369bec4fa05SStephen Warren 	/*
4370bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4371bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4372bec4fa05SStephen Warren 	 * snd_soc_register_card.
4373bec4fa05SStephen Warren 	 */
4374bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4375bec4fa05SStephen Warren 		dev_err(card->dev,
4376f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
4377bec4fa05SStephen Warren 			propname, ret);
4378bec4fa05SStephen Warren 		return ret;
4379bec4fa05SStephen Warren 	}
4380bec4fa05SStephen Warren 
4381bec4fa05SStephen Warren 	return 0;
4382bec4fa05SStephen Warren }
4383bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4384bec4fa05SStephen Warren 
43859a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
43869a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
43879a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
43889a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
43899a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
43909a6d4860SXiubo Li };
43919a6d4860SXiubo Li 
43929a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
43939a6d4860SXiubo Li 					  const char *propname)
43949a6d4860SXiubo Li {
43959a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
43969a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
43979a6d4860SXiubo Li 	const char *template, *wname;
43989a6d4860SXiubo Li 	int i, j, num_widgets, ret;
43999a6d4860SXiubo Li 
44009a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
44019a6d4860SXiubo Li 	if (num_widgets < 0) {
44029a6d4860SXiubo Li 		dev_err(card->dev,
44039a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
44049a6d4860SXiubo Li 		return -EINVAL;
44059a6d4860SXiubo Li 	}
44069a6d4860SXiubo Li 	if (num_widgets & 1) {
44079a6d4860SXiubo Li 		dev_err(card->dev,
44089a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
44099a6d4860SXiubo Li 		return -EINVAL;
44109a6d4860SXiubo Li 	}
44119a6d4860SXiubo Li 
44129a6d4860SXiubo Li 	num_widgets /= 2;
44139a6d4860SXiubo Li 	if (!num_widgets) {
44149a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
44159a6d4860SXiubo Li 			propname);
44169a6d4860SXiubo Li 		return -EINVAL;
44179a6d4860SXiubo Li 	}
44189a6d4860SXiubo Li 
44199a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
44209a6d4860SXiubo Li 			       GFP_KERNEL);
44219a6d4860SXiubo Li 	if (!widgets) {
44229a6d4860SXiubo Li 		dev_err(card->dev,
44239a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
44249a6d4860SXiubo Li 		return -ENOMEM;
44259a6d4860SXiubo Li 	}
44269a6d4860SXiubo Li 
44279a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
44289a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
44299a6d4860SXiubo Li 			2 * i, &template);
44309a6d4860SXiubo Li 		if (ret) {
44319a6d4860SXiubo Li 			dev_err(card->dev,
44329a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
44339a6d4860SXiubo Li 				propname, 2 * i, ret);
44349a6d4860SXiubo Li 			return -EINVAL;
44359a6d4860SXiubo Li 		}
44369a6d4860SXiubo Li 
44379a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
44389a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
44399a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
44409a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
44419a6d4860SXiubo Li 				break;
44429a6d4860SXiubo Li 			}
44439a6d4860SXiubo Li 		}
44449a6d4860SXiubo Li 
44459a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
44469a6d4860SXiubo Li 			dev_err(card->dev,
44479a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
44489a6d4860SXiubo Li 				template);
44499a6d4860SXiubo Li 			return -EINVAL;
44509a6d4860SXiubo Li 		}
44519a6d4860SXiubo Li 
44529a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
44539a6d4860SXiubo Li 						    (2 * i) + 1,
44549a6d4860SXiubo Li 						    &wname);
44559a6d4860SXiubo Li 		if (ret) {
44569a6d4860SXiubo Li 			dev_err(card->dev,
44579a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
44589a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
44599a6d4860SXiubo Li 			return -EINVAL;
44609a6d4860SXiubo Li 		}
44619a6d4860SXiubo Li 
44629a6d4860SXiubo Li 		widgets[i].name = wname;
44639a6d4860SXiubo Li 	}
44649a6d4860SXiubo Li 
44659a6d4860SXiubo Li 	card->dapm_widgets = widgets;
44669a6d4860SXiubo Li 	card->num_dapm_widgets = num_widgets;
44679a6d4860SXiubo Li 
44689a6d4860SXiubo Li 	return 0;
44699a6d4860SXiubo Li }
44709a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
44719a6d4860SXiubo Li 
447289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
447389c67857SXiubo Li 			      unsigned int *slots,
447489c67857SXiubo Li 			      unsigned int *slot_width)
447589c67857SXiubo Li {
447689c67857SXiubo Li 	u32 val;
447789c67857SXiubo Li 	int ret;
447889c67857SXiubo Li 
447989c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
448089c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
448189c67857SXiubo Li 		if (ret)
448289c67857SXiubo Li 			return ret;
448389c67857SXiubo Li 
448489c67857SXiubo Li 		if (slots)
448589c67857SXiubo Li 			*slots = val;
448689c67857SXiubo Li 	}
448789c67857SXiubo Li 
448889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
448989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
449089c67857SXiubo Li 		if (ret)
449189c67857SXiubo Li 			return ret;
449289c67857SXiubo Li 
449389c67857SXiubo Li 		if (slot_width)
449489c67857SXiubo Li 			*slot_width = val;
449589c67857SXiubo Li 	}
449689c67857SXiubo Li 
449789c67857SXiubo Li 	return 0;
449889c67857SXiubo Li }
449989c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
450089c67857SXiubo Li 
4501a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4502a4a54dd5SStephen Warren 				   const char *propname)
4503a4a54dd5SStephen Warren {
4504a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4505a4a54dd5SStephen Warren 	int num_routes;
4506a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4507a4a54dd5SStephen Warren 	int i, ret;
4508a4a54dd5SStephen Warren 
4509a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4510c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
451110e8aa9aSMichał Mirosław 		dev_err(card->dev,
451210e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
451310e8aa9aSMichał Mirosław 			propname);
4514a4a54dd5SStephen Warren 		return -EINVAL;
4515a4a54dd5SStephen Warren 	}
4516a4a54dd5SStephen Warren 	num_routes /= 2;
4517a4a54dd5SStephen Warren 	if (!num_routes) {
4518f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4519a4a54dd5SStephen Warren 			propname);
4520a4a54dd5SStephen Warren 		return -EINVAL;
4521a4a54dd5SStephen Warren 	}
4522a4a54dd5SStephen Warren 
4523a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4524a4a54dd5SStephen Warren 			      GFP_KERNEL);
4525a4a54dd5SStephen Warren 	if (!routes) {
4526a4a54dd5SStephen Warren 		dev_err(card->dev,
4527f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4528a4a54dd5SStephen Warren 		return -EINVAL;
4529a4a54dd5SStephen Warren 	}
4530a4a54dd5SStephen Warren 
4531a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4532a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4533a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4534a4a54dd5SStephen Warren 		if (ret) {
4535c871bd0bSMark Brown 			dev_err(card->dev,
4536c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4537c871bd0bSMark Brown 				propname, 2 * i, ret);
4538a4a54dd5SStephen Warren 			return -EINVAL;
4539a4a54dd5SStephen Warren 		}
4540a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4541a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4542a4a54dd5SStephen Warren 		if (ret) {
4543a4a54dd5SStephen Warren 			dev_err(card->dev,
4544c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4545c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4546a4a54dd5SStephen Warren 			return -EINVAL;
4547a4a54dd5SStephen Warren 		}
4548a4a54dd5SStephen Warren 	}
4549a4a54dd5SStephen Warren 
4550a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4551a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4552a4a54dd5SStephen Warren 
4553a4a54dd5SStephen Warren 	return 0;
4554a4a54dd5SStephen Warren }
4555a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4556a4a54dd5SStephen Warren 
4557a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4558a7930ed4SKuninori Morimoto 				     const char *prefix)
4559a7930ed4SKuninori Morimoto {
4560a7930ed4SKuninori Morimoto 	int ret, i;
4561a7930ed4SKuninori Morimoto 	char prop[128];
4562a7930ed4SKuninori Morimoto 	unsigned int format = 0;
4563a7930ed4SKuninori Morimoto 	int bit, frame;
4564a7930ed4SKuninori Morimoto 	const char *str;
4565a7930ed4SKuninori Morimoto 	struct {
4566a7930ed4SKuninori Morimoto 		char *name;
4567a7930ed4SKuninori Morimoto 		unsigned int val;
4568a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
4569a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
4570a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
4571a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
4572a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
4573a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
4574a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
4575a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
4576a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
4577a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
4578a7930ed4SKuninori Morimoto 	};
4579a7930ed4SKuninori Morimoto 
4580a7930ed4SKuninori Morimoto 	if (!prefix)
4581a7930ed4SKuninori Morimoto 		prefix = "";
4582a7930ed4SKuninori Morimoto 
4583a7930ed4SKuninori Morimoto 	/*
4584a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
4585a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4586a7930ed4SKuninori Morimoto 	 */
4587a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
4588a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
4589a7930ed4SKuninori Morimoto 	if (ret == 0) {
4590a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4591a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4592a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4593a7930ed4SKuninori Morimoto 				break;
4594a7930ed4SKuninori Morimoto 			}
4595a7930ed4SKuninori Morimoto 		}
4596a7930ed4SKuninori Morimoto 	}
4597a7930ed4SKuninori Morimoto 
4598a7930ed4SKuninori Morimoto 	/*
45998c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4600a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4601a7930ed4SKuninori Morimoto 	 */
46028c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
46038c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
46048c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
46058c2d6a9fSKuninori Morimoto 	else
46068c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4607a7930ed4SKuninori Morimoto 
4608a7930ed4SKuninori Morimoto 	/*
4609a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4610a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4611a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4612a7930ed4SKuninori Morimoto 	 */
4613a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4614a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4615a7930ed4SKuninori Morimoto 
4616a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4617a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4618a7930ed4SKuninori Morimoto 
4619a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4620a7930ed4SKuninori Morimoto 	case 0x11:
4621a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4622a7930ed4SKuninori Morimoto 		break;
4623a7930ed4SKuninori Morimoto 	case 0x10:
4624a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4625a7930ed4SKuninori Morimoto 		break;
4626a7930ed4SKuninori Morimoto 	case 0x01:
4627a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4628a7930ed4SKuninori Morimoto 		break;
4629a7930ed4SKuninori Morimoto 	default:
4630a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4631a7930ed4SKuninori Morimoto 		break;
4632a7930ed4SKuninori Morimoto 	}
4633a7930ed4SKuninori Morimoto 
4634a7930ed4SKuninori Morimoto 	/*
4635a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4636a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4637a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4638a7930ed4SKuninori Morimoto 	 */
4639a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4640a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4641a7930ed4SKuninori Morimoto 
4642a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4643a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4644a7930ed4SKuninori Morimoto 
4645a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4646a7930ed4SKuninori Morimoto 	case 0x11:
4647a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4648a7930ed4SKuninori Morimoto 		break;
4649a7930ed4SKuninori Morimoto 	case 0x10:
4650a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4651a7930ed4SKuninori Morimoto 		break;
4652a7930ed4SKuninori Morimoto 	case 0x01:
4653a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4654a7930ed4SKuninori Morimoto 		break;
4655a7930ed4SKuninori Morimoto 	default:
4656a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4657a7930ed4SKuninori Morimoto 		break;
4658a7930ed4SKuninori Morimoto 	}
4659a7930ed4SKuninori Morimoto 
4660a7930ed4SKuninori Morimoto 	return format;
4661a7930ed4SKuninori Morimoto }
4662a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4663a7930ed4SKuninori Morimoto 
4664cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node,
4665cb470087SKuninori Morimoto 			    const char **dai_name)
4666cb470087SKuninori Morimoto {
4667cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
4668cb470087SKuninori Morimoto 	struct of_phandle_args args;
4669cb470087SKuninori Morimoto 	int ret;
4670cb470087SKuninori Morimoto 
4671cb470087SKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
4672cb470087SKuninori Morimoto 					 "#sound-dai-cells", 0, &args);
4673cb470087SKuninori Morimoto 	if (ret)
4674cb470087SKuninori Morimoto 		return ret;
4675cb470087SKuninori Morimoto 
4676cb470087SKuninori Morimoto 	ret = -EPROBE_DEFER;
4677cb470087SKuninori Morimoto 
4678cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4679cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4680cb470087SKuninori Morimoto 		if (pos->dev->of_node != args.np)
4681cb470087SKuninori Morimoto 			continue;
4682cb470087SKuninori Morimoto 
46836833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
46846833c452SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
46856833c452SKuninori Morimoto 		} else {
46866833c452SKuninori Morimoto 			int id = -1;
46876833c452SKuninori Morimoto 
46886833c452SKuninori Morimoto 			switch (args.args_count) {
46896833c452SKuninori Morimoto 			case 0:
46906833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
46916833c452SKuninori Morimoto 				break;
46926833c452SKuninori Morimoto 			case 1:
46936833c452SKuninori Morimoto 				id = args.args[0];
46946833c452SKuninori Morimoto 				break;
46956833c452SKuninori Morimoto 			default:
46966833c452SKuninori Morimoto 				/* not supported */
4697cb470087SKuninori Morimoto 				break;
4698cb470087SKuninori Morimoto 			}
4699cb470087SKuninori Morimoto 
47006833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
47016833c452SKuninori Morimoto 				ret = -EINVAL;
4702e41975edSXiubo Li 				break;
47036833c452SKuninori Morimoto 			}
4704e41975edSXiubo Li 
4705e41975edSXiubo Li 			ret = 0;
4706e41975edSXiubo Li 
4707e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4708e41975edSXiubo Li 			if (!*dai_name)
4709e41975edSXiubo Li 				*dai_name = pos->name;
47106833c452SKuninori Morimoto 		}
47116833c452SKuninori Morimoto 
4712cb470087SKuninori Morimoto 		break;
4713cb470087SKuninori Morimoto 	}
4714cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
4715cb470087SKuninori Morimoto 
4716cb470087SKuninori Morimoto 	of_node_put(args.np);
4717cb470087SKuninori Morimoto 
4718cb470087SKuninori Morimoto 	return ret;
4719cb470087SKuninori Morimoto }
4720cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4721cb470087SKuninori Morimoto 
4722c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4723db2a4165SFrank Mandarino {
4724384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
47258a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
47268a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
47270837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
47288a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4729384c89e2SMark Brown 	}
4730c3c5a19aSMark Brown 
47318a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4732c3c5a19aSMark Brown 				 &codec_list_fops))
4733c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4734c3c5a19aSMark Brown 
47358a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4736f3208780SMark Brown 				 &dai_list_fops))
4737f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
473819c7ac27SMark Brown 
47398a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
474019c7ac27SMark Brown 				 &platform_list_fops))
474119c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4742384c89e2SMark Brown #endif
4743384c89e2SMark Brown 
4744fb257897SMark Brown 	snd_soc_util_init();
4745fb257897SMark Brown 
4746db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4747db2a4165SFrank Mandarino }
47484abe8e16SMark Brown module_init(snd_soc_init);
4749db2a4165SFrank Mandarino 
47507d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4751db2a4165SFrank Mandarino {
4752fb257897SMark Brown 	snd_soc_util_exit();
4753fb257897SMark Brown 
4754384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
47558a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4756384c89e2SMark Brown #endif
4757db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4758db2a4165SFrank Mandarino }
4759db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4760db2a4165SFrank Mandarino 
4761db2a4165SFrank Mandarino /* Module information */
4762d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4763db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4764db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
47658b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
4766