xref: /openbmc/linux/sound/soc/soc-core.c (revision a1a564ed6abf62e05fcffa9ed3f46a826400df3a)
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);
599115171aSMark Brown static LIST_HEAD(dai_list);
6012a48a8cSMark Brown static LIST_HEAD(platform_list);
610d0cf00aSMark Brown static LIST_HEAD(codec_list);
62030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
63c5af3a2eSMark Brown 
64db2a4165SFrank Mandarino /*
65db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
66db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
67db2a4165SFrank Mandarino  * between two audio tracks.
68db2a4165SFrank Mandarino  */
69db2a4165SFrank Mandarino static int pmdown_time = 5000;
70db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
71db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72db2a4165SFrank Mandarino 
73741a509fSMarkus Pargmann struct snd_ac97_reset_cfg {
74741a509fSMarkus Pargmann 	struct pinctrl *pctl;
75741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_reset;
76741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_warm_reset;
77741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_run;
78741a509fSMarkus Pargmann 	int gpio_sdata;
79741a509fSMarkus Pargmann 	int gpio_sync;
80741a509fSMarkus Pargmann 	int gpio_reset;
81741a509fSMarkus Pargmann };
82741a509fSMarkus Pargmann 
832bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
842bc9a81eSDimitris Papastamos  * a particular given value */
852bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
862bc9a81eSDimitris Papastamos {
872bc9a81eSDimitris Papastamos 	int c = 0;
882bc9a81eSDimitris Papastamos 	int i;
892bc9a81eSDimitris Papastamos 
902bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
912bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
922bc9a81eSDimitris Papastamos 			break;
932bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
942bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
952bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
962bc9a81eSDimitris Papastamos 	else
972bc9a81eSDimitris Papastamos 		c /= 8;
982bc9a81eSDimitris Papastamos 	return c;
992bc9a81eSDimitris Papastamos }
1002bc9a81eSDimitris Papastamos 
10113fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
10213fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
10313fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
10413fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
1052624d5faSMark Brown {
10600b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
10700b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
10813fd179fSDimitris Papastamos 	int ret;
10913fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
11013fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
11113fd179fSDimitris Papastamos 
11213fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
11313fd179fSDimitris Papastamos 	 * try to abuse this function */
11413fd179fSDimitris Papastamos 	WARN_ON(len > 63);
11513fd179fSDimitris Papastamos 
11613fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
11713fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
11813fd179fSDimitris Papastamos 		return -EINVAL;
11913fd179fSDimitris Papastamos 
12013fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
12113fd179fSDimitris Papastamos 	if (ret < 0) {
12213fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
12313fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
12413fd179fSDimitris Papastamos 	} else {
12513fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
12613fd179fSDimitris Papastamos 	}
12713fd179fSDimitris Papastamos 
12813fd179fSDimitris Papastamos 	/* prepare the buffer */
12913fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
13013fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
13113fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
13213fd179fSDimitris Papastamos 
13313fd179fSDimitris Papastamos 	return 0;
13413fd179fSDimitris Papastamos }
13513fd179fSDimitris Papastamos 
13613fd179fSDimitris Papastamos /* codec register dump */
13713fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
13813fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
13913fd179fSDimitris Papastamos {
14013fd179fSDimitris Papastamos 	int i, step = 1;
1412bc9a81eSDimitris Papastamos 	int wordsize, regsize;
14213fd179fSDimitris Papastamos 	int len;
14313fd179fSDimitris Papastamos 	size_t total = 0;
14413fd179fSDimitris Papastamos 	loff_t p = 0;
1452bc9a81eSDimitris Papastamos 
14600b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
14700b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1482624d5faSMark Brown 
14913fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
15013fd179fSDimitris Papastamos 
151f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1522624d5faSMark Brown 		return 0;
1532624d5faSMark Brown 
154f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
155f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1562624d5faSMark Brown 
157f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
158b92d150bSLars-Peter Clausen 		if (!snd_soc_codec_readable_register(codec, i))
1592624d5faSMark Brown 			continue;
160f0fba2adSLiam Girdwood 		if (codec->driver->display_register) {
161f0fba2adSLiam Girdwood 			count += codec->driver->display_register(codec, buf + count,
1622624d5faSMark Brown 							 PAGE_SIZE - count, i);
1635164d74dSMark Brown 		} else {
16413fd179fSDimitris Papastamos 			/* only support larger than PAGE_SIZE bytes debugfs
16513fd179fSDimitris Papastamos 			 * entries for the default case */
16613fd179fSDimitris Papastamos 			if (p >= pos) {
16713fd179fSDimitris Papastamos 				if (total + len >= count - 1)
1682624d5faSMark Brown 					break;
16913fd179fSDimitris Papastamos 				format_register_str(codec, i, buf + total, len);
17013fd179fSDimitris Papastamos 				total += len;
17113fd179fSDimitris Papastamos 			}
17213fd179fSDimitris Papastamos 			p += len;
17313fd179fSDimitris Papastamos 		}
1742624d5faSMark Brown 	}
1752624d5faSMark Brown 
17613fd179fSDimitris Papastamos 	total = min(total, count - 1);
1772624d5faSMark Brown 
17813fd179fSDimitris Papastamos 	return total;
1792624d5faSMark Brown }
18013fd179fSDimitris Papastamos 
1812624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1822624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1832624d5faSMark Brown {
18436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
185f0fba2adSLiam Girdwood 
18613fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1872624d5faSMark Brown }
1882624d5faSMark Brown 
1892624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1902624d5faSMark Brown 
191dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
192dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
193dbe21408SMark Brown {
19436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
195dbe21408SMark Brown 
196f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
197dbe21408SMark Brown }
198dbe21408SMark Brown 
199dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
200dbe21408SMark Brown 			       struct device_attribute *attr,
201dbe21408SMark Brown 			       const char *buf, size_t count)
202dbe21408SMark Brown {
20336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
204c593b520SMark Brown 	int ret;
205dbe21408SMark Brown 
206b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
207c593b520SMark Brown 	if (ret)
208c593b520SMark Brown 		return ret;
209dbe21408SMark Brown 
210dbe21408SMark Brown 	return count;
211dbe21408SMark Brown }
212dbe21408SMark Brown 
213dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
214dbe21408SMark Brown 
2152624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2162624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2172624d5faSMark Brown 				   size_t count, loff_t *ppos)
2182624d5faSMark Brown {
2192624d5faSMark Brown 	ssize_t ret;
2202624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
22113fd179fSDimitris Papastamos 	char *buf;
22213fd179fSDimitris Papastamos 
22313fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
22413fd179fSDimitris Papastamos 		return -EINVAL;
22513fd179fSDimitris Papastamos 
22613fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2272624d5faSMark Brown 	if (!buf)
2282624d5faSMark Brown 		return -ENOMEM;
22913fd179fSDimitris Papastamos 
23013fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
23113fd179fSDimitris Papastamos 	if (ret >= 0) {
23213fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
23313fd179fSDimitris Papastamos 			kfree(buf);
23413fd179fSDimitris Papastamos 			return -EFAULT;
23513fd179fSDimitris Papastamos 		}
23613fd179fSDimitris Papastamos 		*ppos += ret;
23713fd179fSDimitris Papastamos 	}
23813fd179fSDimitris Papastamos 
2392624d5faSMark Brown 	kfree(buf);
2402624d5faSMark Brown 	return ret;
2412624d5faSMark Brown }
2422624d5faSMark Brown 
2432624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2442624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2452624d5faSMark Brown {
2462624d5faSMark Brown 	char buf[32];
24734e268d8SStephen Boyd 	size_t buf_size;
2482624d5faSMark Brown 	char *start = buf;
2492624d5faSMark Brown 	unsigned long reg, value;
2502624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
251b785a492SJingoo Han 	int ret;
2522624d5faSMark Brown 
2532624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2542624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2552624d5faSMark Brown 		return -EFAULT;
2562624d5faSMark Brown 	buf[buf_size] = 0;
2572624d5faSMark Brown 
2582624d5faSMark Brown 	while (*start == ' ')
2592624d5faSMark Brown 		start++;
2602624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2612624d5faSMark Brown 	while (*start == ' ')
2622624d5faSMark Brown 		start++;
263b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
264b785a492SJingoo Han 	if (ret)
265b785a492SJingoo Han 		return ret;
2660d51a9cbSMark Brown 
2670d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
268373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2690d51a9cbSMark Brown 
270e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2712624d5faSMark Brown 	return buf_size;
2722624d5faSMark Brown }
2732624d5faSMark Brown 
2742624d5faSMark Brown static const struct file_operations codec_reg_fops = {
275234e3405SStephen Boyd 	.open = simple_open,
2762624d5faSMark Brown 	.read = codec_reg_read_file,
2772624d5faSMark Brown 	.write = codec_reg_write_file,
2786038f373SArnd Bergmann 	.llseek = default_llseek,
2792624d5faSMark Brown };
2802624d5faSMark Brown 
2812624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2822624d5faSMark Brown {
283d6ce4cf3SJarkko Nikula 	struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
284d6ce4cf3SJarkko Nikula 
2856ba6c9c3SMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec->name,
286d6ce4cf3SJarkko Nikula 						       debugfs_card_root);
2872624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
28810e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
28910e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec debugfs directory\n");
2902624d5faSMark Brown 		return;
2912624d5faSMark Brown 	}
2922624d5faSMark Brown 
293aaee8ef1SMark Brown 	debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
294aaee8ef1SMark Brown 			    &codec->cache_sync);
295aaee8ef1SMark Brown 	debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
296aaee8ef1SMark Brown 			    &codec->cache_only);
297aaee8ef1SMark Brown 
2982624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2992624d5faSMark Brown 						 codec->debugfs_codec_root,
3002624d5faSMark Brown 						 codec, &codec_reg_fops);
3012624d5faSMark Brown 	if (!codec->debugfs_reg)
30210e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
30310e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
3042624d5faSMark Brown 
3058eecaf62SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
3062624d5faSMark Brown }
3072624d5faSMark Brown 
3082624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
3092624d5faSMark Brown {
3102624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
3112624d5faSMark Brown }
3122624d5faSMark Brown 
313731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
314731f1ab2SSebastien Guiriec {
315731f1ab2SSebastien Guiriec 	struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
316731f1ab2SSebastien Guiriec 
317731f1ab2SSebastien Guiriec 	platform->debugfs_platform_root = debugfs_create_dir(platform->name,
318731f1ab2SSebastien Guiriec 						       debugfs_card_root);
319731f1ab2SSebastien Guiriec 	if (!platform->debugfs_platform_root) {
320731f1ab2SSebastien Guiriec 		dev_warn(platform->dev,
321f110bfc7SLiam Girdwood 			"ASoC: Failed to create platform debugfs directory\n");
322731f1ab2SSebastien Guiriec 		return;
323731f1ab2SSebastien Guiriec 	}
324731f1ab2SSebastien Guiriec 
325731f1ab2SSebastien Guiriec 	snd_soc_dapm_debugfs_init(&platform->dapm,
326731f1ab2SSebastien Guiriec 		platform->debugfs_platform_root);
327731f1ab2SSebastien Guiriec }
328731f1ab2SSebastien Guiriec 
329731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
330731f1ab2SSebastien Guiriec {
331731f1ab2SSebastien Guiriec 	debugfs_remove_recursive(platform->debugfs_platform_root);
332731f1ab2SSebastien Guiriec }
333731f1ab2SSebastien Guiriec 
334c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
335c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
336c3c5a19aSMark Brown {
337c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3382b194f9dSMark Brown 	ssize_t len, ret = 0;
339c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
340c3c5a19aSMark Brown 
341c3c5a19aSMark Brown 	if (!buf)
342c3c5a19aSMark Brown 		return -ENOMEM;
343c3c5a19aSMark Brown 
3442b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3452b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
346c3c5a19aSMark Brown 			       codec->name);
3472b194f9dSMark Brown 		if (len >= 0)
3482b194f9dSMark Brown 			ret += len;
3492b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3502b194f9dSMark Brown 			ret = PAGE_SIZE;
3512b194f9dSMark Brown 			break;
3522b194f9dSMark Brown 		}
3532b194f9dSMark Brown 	}
354c3c5a19aSMark Brown 
355c3c5a19aSMark Brown 	if (ret >= 0)
356c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
357c3c5a19aSMark Brown 
358c3c5a19aSMark Brown 	kfree(buf);
359c3c5a19aSMark Brown 
360c3c5a19aSMark Brown 	return ret;
361c3c5a19aSMark Brown }
362c3c5a19aSMark Brown 
363c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
364c3c5a19aSMark Brown 	.read = codec_list_read_file,
365c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
366c3c5a19aSMark Brown };
367c3c5a19aSMark Brown 
368f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
369f3208780SMark Brown 				  size_t count, loff_t *ppos)
370f3208780SMark Brown {
371f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3722b194f9dSMark Brown 	ssize_t len, ret = 0;
373f3208780SMark Brown 	struct snd_soc_dai *dai;
374f3208780SMark Brown 
375f3208780SMark Brown 	if (!buf)
376f3208780SMark Brown 		return -ENOMEM;
377f3208780SMark Brown 
3782b194f9dSMark Brown 	list_for_each_entry(dai, &dai_list, list) {
3792b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
3802b194f9dSMark Brown 		if (len >= 0)
3812b194f9dSMark Brown 			ret += len;
3822b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3832b194f9dSMark Brown 			ret = PAGE_SIZE;
3842b194f9dSMark Brown 			break;
3852b194f9dSMark Brown 		}
3862b194f9dSMark Brown 	}
387f3208780SMark Brown 
388f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
389f3208780SMark Brown 
390f3208780SMark Brown 	kfree(buf);
391f3208780SMark Brown 
392f3208780SMark Brown 	return ret;
393f3208780SMark Brown }
394f3208780SMark Brown 
395f3208780SMark Brown static const struct file_operations dai_list_fops = {
396f3208780SMark Brown 	.read = dai_list_read_file,
397f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
398f3208780SMark Brown };
399f3208780SMark Brown 
40019c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
40119c7ac27SMark Brown 				       char __user *user_buf,
40219c7ac27SMark Brown 				       size_t count, loff_t *ppos)
40319c7ac27SMark Brown {
40419c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4052b194f9dSMark Brown 	ssize_t len, ret = 0;
40619c7ac27SMark Brown 	struct snd_soc_platform *platform;
40719c7ac27SMark Brown 
40819c7ac27SMark Brown 	if (!buf)
40919c7ac27SMark Brown 		return -ENOMEM;
41019c7ac27SMark Brown 
4112b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4122b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
41319c7ac27SMark Brown 			       platform->name);
4142b194f9dSMark Brown 		if (len >= 0)
4152b194f9dSMark Brown 			ret += len;
4162b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4172b194f9dSMark Brown 			ret = PAGE_SIZE;
4182b194f9dSMark Brown 			break;
4192b194f9dSMark Brown 		}
4202b194f9dSMark Brown 	}
42119c7ac27SMark Brown 
42219c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
42319c7ac27SMark Brown 
42419c7ac27SMark Brown 	kfree(buf);
42519c7ac27SMark Brown 
42619c7ac27SMark Brown 	return ret;
42719c7ac27SMark Brown }
42819c7ac27SMark Brown 
42919c7ac27SMark Brown static const struct file_operations platform_list_fops = {
43019c7ac27SMark Brown 	.read = platform_list_read_file,
43119c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
43219c7ac27SMark Brown };
43319c7ac27SMark Brown 
434a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
435a6052154SJarkko Nikula {
436a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4378a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4383a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
439a6052154SJarkko Nikula 		dev_warn(card->dev,
4407c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4413a45b867SJarkko Nikula 		return;
4423a45b867SJarkko Nikula 	}
4433a45b867SJarkko Nikula 
4443a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4453a45b867SJarkko Nikula 						    card->debugfs_card_root,
4463a45b867SJarkko Nikula 						    &card->pop_time);
4473a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4483a45b867SJarkko Nikula 		dev_warn(card->dev,
449f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
450a6052154SJarkko Nikula }
451a6052154SJarkko Nikula 
452a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
453a6052154SJarkko Nikula {
454a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
455a6052154SJarkko Nikula }
456a6052154SJarkko Nikula 
4572624d5faSMark Brown #else
4582624d5faSMark Brown 
4592624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
4602624d5faSMark Brown {
4612624d5faSMark Brown }
4622624d5faSMark Brown 
4632624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
4642624d5faSMark Brown {
4652624d5faSMark Brown }
466b95fccbcSAxel Lin 
467731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
468731f1ab2SSebastien Guiriec {
469731f1ab2SSebastien Guiriec }
470731f1ab2SSebastien Guiriec 
471731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
472731f1ab2SSebastien Guiriec {
473731f1ab2SSebastien Guiriec }
474731f1ab2SSebastien Guiriec 
475b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
476b95fccbcSAxel Lin {
477b95fccbcSAxel Lin }
478b95fccbcSAxel Lin 
479b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
480b95fccbcSAxel Lin {
481b95fccbcSAxel Lin }
4822624d5faSMark Brown #endif
4832624d5faSMark Brown 
48447c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
48547c88fffSLiam Girdwood 		const char *dai_link, int stream)
48647c88fffSLiam Girdwood {
48747c88fffSLiam Girdwood 	int i;
48847c88fffSLiam Girdwood 
48947c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
49047c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
49147c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
49247c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
49347c88fffSLiam Girdwood 	}
494f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
49547c88fffSLiam Girdwood 	return NULL;
49647c88fffSLiam Girdwood }
49747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
49847c88fffSLiam Girdwood 
49947c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
50047c88fffSLiam Girdwood 		const char *dai_link)
50147c88fffSLiam Girdwood {
50247c88fffSLiam Girdwood 	int i;
50347c88fffSLiam Girdwood 
50447c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
50547c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
50647c88fffSLiam Girdwood 			return &card->rtd[i];
50747c88fffSLiam Girdwood 	}
508f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
50947c88fffSLiam Girdwood 	return NULL;
51047c88fffSLiam Girdwood }
51147c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
51247c88fffSLiam Girdwood 
513db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
514db2a4165SFrank Mandarino /* unregister ac97 codec */
515db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
516db2a4165SFrank Mandarino {
517db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
518db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
519db2a4165SFrank Mandarino 	return 0;
520db2a4165SFrank Mandarino }
521db2a4165SFrank Mandarino 
522db2a4165SFrank Mandarino /* stop no dev release warning */
523db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
524db2a4165SFrank Mandarino 
525db2a4165SFrank Mandarino /* register ac97 codec to bus */
526db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
527db2a4165SFrank Mandarino {
528db2a4165SFrank Mandarino 	int err;
529db2a4165SFrank Mandarino 
530db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
5314ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
532db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
533db2a4165SFrank Mandarino 
534bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
535f0fba2adSLiam Girdwood 		     codec->card->snd_card->number, 0, codec->name);
536db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
537db2a4165SFrank Mandarino 	if (err < 0) {
538f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
539db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
540db2a4165SFrank Mandarino 		return err;
541db2a4165SFrank Mandarino 	}
542db2a4165SFrank Mandarino 	return 0;
543db2a4165SFrank Mandarino }
544db2a4165SFrank Mandarino #endif
545db2a4165SFrank Mandarino 
5469d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5479d58a077SRichard Fitzgerald {
5489d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5499d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5509d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5519d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5529d58a077SRichard Fitzgerald 	 */
5539d58a077SRichard Fitzgerald }
5549d58a077SRichard Fitzgerald 
5556f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
556db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5576f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
558db2a4165SFrank Mandarino {
5596f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5602eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
561db2a4165SFrank Mandarino 	int i;
562db2a4165SFrank Mandarino 
563e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
564e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
565e3509ff0SDaniel Mack 	 */
566f0fba2adSLiam Girdwood 	if (list_empty(&card->codec_dev_list))
567e3509ff0SDaniel Mack 		return 0;
568e3509ff0SDaniel Mack 
5696ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5706ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5716ed25978SAndy Green 	 */
572f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
573f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
574f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5756ed25978SAndy Green 
5766ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
577f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5786ed25978SAndy Green 
579a00f90f9SMark Brown 	/* mute any active DACs */
580f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
581f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
582f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
5833efab7dcSMark Brown 
584f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5853efab7dcSMark Brown 			continue;
5863efab7dcSMark Brown 
587f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
588f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 1);
589db2a4165SFrank Mandarino 	}
590db2a4165SFrank Mandarino 
5914ccab3e7SLiam Girdwood 	/* suspend all pcms */
592f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
593f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5943efab7dcSMark Brown 			continue;
5953efab7dcSMark Brown 
596f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5973efab7dcSMark Brown 	}
5984ccab3e7SLiam Girdwood 
59987506549SMark Brown 	if (card->suspend_pre)
60070b2ac12SMark Brown 		card->suspend_pre(card);
601db2a4165SFrank Mandarino 
602f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
603f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
604f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
6053efab7dcSMark Brown 
606f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6073efab7dcSMark Brown 			continue;
6083efab7dcSMark Brown 
609f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
610f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
611f0fba2adSLiam Girdwood 		if (platform->driver->suspend && !platform->suspended) {
612f0fba2adSLiam Girdwood 			platform->driver->suspend(cpu_dai);
613f0fba2adSLiam Girdwood 			platform->suspended = 1;
614f0fba2adSLiam Girdwood 		}
615db2a4165SFrank Mandarino 	}
616db2a4165SFrank Mandarino 
617db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
618f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
61943829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
620ce6120ccSLiam Girdwood 		card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
621f0fba2adSLiam Girdwood 	}
622db2a4165SFrank Mandarino 
623f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6243efab7dcSMark Brown 
625f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6263efab7dcSMark Brown 			continue;
6273efab7dcSMark Brown 
6287bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6297bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
630db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
631f0fba2adSLiam Girdwood 
6327bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6337bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
634db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
635db2a4165SFrank Mandarino 	}
636db2a4165SFrank Mandarino 
637e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
638e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
639e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
640e2d32ff6SMark Brown 
641f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6422eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6431547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6441547aba9SMark Brown 		 * bias _ON and should not be suspended. */
645f0fba2adSLiam Girdwood 		if (!codec->suspended && codec->driver->suspend) {
646ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6471547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
648125a25daSMark Brown 				/*
649125a25daSMark Brown 				 * If the CODEC is capable of idle
650125a25daSMark Brown 				 * bias off then being in STANDBY
651125a25daSMark Brown 				 * means it's doing something,
652125a25daSMark Brown 				 * otherwise fall through.
653125a25daSMark Brown 				 */
654125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
655125a25daSMark Brown 					dev_dbg(codec->dev,
65610e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
657125a25daSMark Brown 					break;
658125a25daSMark Brown 				}
6591547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
66084b315eeSLars-Peter Clausen 				codec->driver->suspend(codec);
661f0fba2adSLiam Girdwood 				codec->suspended = 1;
6627be4ba24SMark Brown 				codec->cache_sync = 1;
663da8b8e0fSMark Brown 				if (codec->using_regmap)
664da8b8e0fSMark Brown 					regcache_mark_dirty(codec->control_data);
665988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
666988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6671547aba9SMark Brown 				break;
6681547aba9SMark Brown 			default:
66910e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
67010e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6711547aba9SMark Brown 				break;
6721547aba9SMark Brown 			}
6731547aba9SMark Brown 		}
674f0fba2adSLiam Girdwood 	}
675db2a4165SFrank Mandarino 
676f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
677f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6783efab7dcSMark Brown 
679f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6803efab7dcSMark Brown 			continue;
6813efab7dcSMark Brown 
682f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
683f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
684988e8cc4SNicolin Chen 
685988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
686988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
687db2a4165SFrank Mandarino 	}
688db2a4165SFrank Mandarino 
68987506549SMark Brown 	if (card->suspend_post)
69070b2ac12SMark Brown 		card->suspend_post(card);
691db2a4165SFrank Mandarino 
692db2a4165SFrank Mandarino 	return 0;
693db2a4165SFrank Mandarino }
6946f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
695db2a4165SFrank Mandarino 
6966ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6976ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6986ed25978SAndy Green  */
6996ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
700db2a4165SFrank Mandarino {
701f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
702f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
7032eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
704db2a4165SFrank Mandarino 	int i;
705db2a4165SFrank Mandarino 
7066ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
7076ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7086ed25978SAndy Green 	 */
7096ed25978SAndy Green 
710f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7116ed25978SAndy Green 
7129949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
713f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7149949788bSMark Brown 
71587506549SMark Brown 	if (card->resume_pre)
71670b2ac12SMark Brown 		card->resume_pre(card);
717db2a4165SFrank Mandarino 
718f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
719f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
720f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7213efab7dcSMark Brown 
722f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7233efab7dcSMark Brown 			continue;
7243efab7dcSMark Brown 
725f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
726f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
727db2a4165SFrank Mandarino 	}
728db2a4165SFrank Mandarino 
7292eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7301547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7311547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7321547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7331547aba9SMark Brown 		 */
734f0fba2adSLiam Girdwood 		if (codec->driver->resume && codec->suspended) {
735ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7361547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7371547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
738f0fba2adSLiam Girdwood 				codec->driver->resume(codec);
739f0fba2adSLiam Girdwood 				codec->suspended = 0;
7401547aba9SMark Brown 				break;
7411547aba9SMark Brown 			default:
74210e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
74310e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7441547aba9SMark Brown 				break;
7451547aba9SMark Brown 			}
7461547aba9SMark Brown 		}
747f0fba2adSLiam Girdwood 	}
748db2a4165SFrank Mandarino 
749f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7503efab7dcSMark Brown 
751f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7523efab7dcSMark Brown 			continue;
7533efab7dcSMark Brown 
7547bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
755d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
756db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
757f0fba2adSLiam Girdwood 
7587bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
759d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
760db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
761db2a4165SFrank Mandarino 	}
762db2a4165SFrank Mandarino 
7633ff3f64bSMark Brown 	/* unmute any active DACs */
764f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
765f0fba2adSLiam Girdwood 		struct snd_soc_dai *dai = card->rtd[i].codec_dai;
766f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *drv = dai->driver;
7673efab7dcSMark Brown 
768f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7693efab7dcSMark Brown 			continue;
7703efab7dcSMark Brown 
771f0fba2adSLiam Girdwood 		if (drv->ops->digital_mute && dai->playback_active)
772f0fba2adSLiam Girdwood 			drv->ops->digital_mute(dai, 0);
773db2a4165SFrank Mandarino 	}
774db2a4165SFrank Mandarino 
775f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
776f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
777f0fba2adSLiam Girdwood 		struct snd_soc_platform *platform = card->rtd[i].platform;
7783efab7dcSMark Brown 
779f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7803efab7dcSMark Brown 			continue;
7813efab7dcSMark Brown 
782f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
783f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
784f0fba2adSLiam Girdwood 		if (platform->driver->resume && platform->suspended) {
785f0fba2adSLiam Girdwood 			platform->driver->resume(cpu_dai);
786f0fba2adSLiam Girdwood 			platform->suspended = 0;
787f0fba2adSLiam Girdwood 		}
788db2a4165SFrank Mandarino 	}
789db2a4165SFrank Mandarino 
79087506549SMark Brown 	if (card->resume_post)
79170b2ac12SMark Brown 		card->resume_post(card);
792db2a4165SFrank Mandarino 
793f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7946ed25978SAndy Green 
7956ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
796f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
797e2d32ff6SMark Brown 
798e2d32ff6SMark Brown 	/* Recheck all analogue paths too */
799e2d32ff6SMark Brown 	dapm_mark_io_dirty(&card->dapm);
800e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
8016ed25978SAndy Green }
8026ed25978SAndy Green 
8036ed25978SAndy Green /* powers up audio subsystem after a suspend */
8046f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
8056ed25978SAndy Green {
8066f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
80782e14e8bSStephen Warren 	int i, ac97_control = 0;
808b9dd94a8SPeter Ujfalusi 
8095ff1ddf2SEric Miao 	/* If the initialization of this soc device failed, there is no codec
8105ff1ddf2SEric Miao 	 * associated with it. Just bail out in this case.
8115ff1ddf2SEric Miao 	 */
8125ff1ddf2SEric Miao 	if (list_empty(&card->codec_dev_list))
8135ff1ddf2SEric Miao 		return 0;
8145ff1ddf2SEric Miao 
815988e8cc4SNicolin Chen 	/* activate pins from sleep state */
816988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
817988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
818988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
819988e8cc4SNicolin Chen 		if (cpu_dai->active)
820988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
821988e8cc4SNicolin Chen 		if (codec_dai->active)
822988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(codec_dai->dev);
823988e8cc4SNicolin Chen 	}
824988e8cc4SNicolin Chen 
82564ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
82664ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
82764ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
82864ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
82964ab9baaSMark Brown 	 */
830f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
831f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
83282e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
83382e14e8bSStephen Warren 	}
83482e14e8bSStephen Warren 	if (ac97_control) {
835f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
83664ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
83764ab9baaSMark Brown 	} else {
838f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8396308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
840f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
841f0fba2adSLiam Girdwood 	}
8426ed25978SAndy Green 
843db2a4165SFrank Mandarino 	return 0;
844db2a4165SFrank Mandarino }
8456f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
846db2a4165SFrank Mandarino #else
8476f8ab4acSMark Brown #define snd_soc_suspend NULL
8486f8ab4acSMark Brown #define snd_soc_resume NULL
849db2a4165SFrank Mandarino #endif
850db2a4165SFrank Mandarino 
85185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
85202a06d30SBarry Song };
85302a06d30SBarry Song 
854f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
855db2a4165SFrank Mandarino {
856f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
857f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
858fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
859435c5e25SMark Brown 	struct snd_soc_platform *platform;
860f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai, *cpu_dai;
861848dd8beSMark Brown 	const char *platform_name;
862db2a4165SFrank Mandarino 
863f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
8646308419aSMark Brown 
865b19e6e7bSMark Brown 	/* Find CPU DAI from registered DAIs*/
866f0fba2adSLiam Girdwood 	list_for_each_entry(cpu_dai, &dai_list, list) {
867bc92657aSStephen Warren 		if (dai_link->cpu_of_node &&
868bc92657aSStephen Warren 		    (cpu_dai->dev->of_node != dai_link->cpu_of_node))
8695a504963SStephen Warren 			continue;
870bc92657aSStephen Warren 		if (dai_link->cpu_name &&
871bc92657aSStephen Warren 		    strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
8722610ab77SStephen Warren 			continue;
873bc92657aSStephen Warren 		if (dai_link->cpu_dai_name &&
874bc92657aSStephen Warren 		    strcmp(cpu_dai->name, dai_link->cpu_dai_name))
875bc92657aSStephen Warren 			continue;
8762610ab77SStephen Warren 
877f0fba2adSLiam Girdwood 		rtd->cpu_dai = cpu_dai;
878f0fba2adSLiam Girdwood 	}
879b19e6e7bSMark Brown 
880b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
881f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
882f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
883b19e6e7bSMark Brown 		return -EPROBE_DEFER;
884c5af3a2eSMark Brown 	}
885c5af3a2eSMark Brown 
886b19e6e7bSMark Brown 	/* Find CODEC from registered CODECs */
887f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
8885a504963SStephen Warren 		if (dai_link->codec_of_node) {
8895a504963SStephen Warren 			if (codec->dev->of_node != dai_link->codec_of_node)
8905a504963SStephen Warren 				continue;
8915a504963SStephen Warren 		} else {
8922610ab77SStephen Warren 			if (strcmp(codec->name, dai_link->codec_name))
8932610ab77SStephen Warren 				continue;
8945a504963SStephen Warren 		}
8952610ab77SStephen Warren 
896f0fba2adSLiam Girdwood 		rtd->codec = codec;
897f0fba2adSLiam Girdwood 
8982610ab77SStephen Warren 		/*
8992610ab77SStephen Warren 		 * CODEC found, so find CODEC DAI from registered DAIs from
9002610ab77SStephen Warren 		 * this CODEC
9012610ab77SStephen Warren 		 */
902f0fba2adSLiam Girdwood 		list_for_each_entry(codec_dai, &dai_list, list) {
903f0fba2adSLiam Girdwood 			if (codec->dev == codec_dai->dev &&
9042610ab77SStephen Warren 				!strcmp(codec_dai->name,
9052610ab77SStephen Warren 					dai_link->codec_dai_name)) {
9062610ab77SStephen Warren 
907f0fba2adSLiam Girdwood 				rtd->codec_dai = codec_dai;
908435c5e25SMark Brown 			}
909f0fba2adSLiam Girdwood 		}
910b19e6e7bSMark Brown 
911b19e6e7bSMark Brown 		if (!rtd->codec_dai) {
912f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
913f0fba2adSLiam Girdwood 				dai_link->codec_dai_name);
914b19e6e7bSMark Brown 			return -EPROBE_DEFER;
915f0fba2adSLiam Girdwood 		}
916b19e6e7bSMark Brown 	}
917b19e6e7bSMark Brown 
918b19e6e7bSMark Brown 	if (!rtd->codec) {
919f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CODEC %s not registered\n",
920f0fba2adSLiam Girdwood 			dai_link->codec_name);
921b19e6e7bSMark Brown 		return -EPROBE_DEFER;
922b19e6e7bSMark Brown 	}
923848dd8beSMark Brown 
924848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
925848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9265a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
927848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
928848dd8beSMark Brown 
929b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
930f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9315a504963SStephen Warren 		if (dai_link->platform_of_node) {
9325a504963SStephen Warren 			if (platform->dev->of_node !=
9335a504963SStephen Warren 			    dai_link->platform_of_node)
9345a504963SStephen Warren 				continue;
9355a504963SStephen Warren 		} else {
9362610ab77SStephen Warren 			if (strcmp(platform->name, platform_name))
9372610ab77SStephen Warren 				continue;
9385a504963SStephen Warren 		}
9392610ab77SStephen Warren 
940f0fba2adSLiam Girdwood 		rtd->platform = platform;
941f0fba2adSLiam Girdwood 	}
942b19e6e7bSMark Brown 	if (!rtd->platform) {
943f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
944f0fba2adSLiam Girdwood 			dai_link->platform_name);
945b19e6e7bSMark Brown 		return -EPROBE_DEFER;
946f0fba2adSLiam Girdwood 	}
947b19e6e7bSMark Brown 
948b19e6e7bSMark Brown 	card->num_rtd++;
949b19e6e7bSMark Brown 
950b19e6e7bSMark Brown 	return 0;
9516b05eda6SMark Brown }
9526b05eda6SMark Brown 
953d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform)
954d12cd198SStephen Warren {
955d12cd198SStephen Warren 	int ret;
956d12cd198SStephen Warren 
957d12cd198SStephen Warren 	if (platform->driver->remove) {
958d12cd198SStephen Warren 		ret = platform->driver->remove(platform);
959d12cd198SStephen Warren 		if (ret < 0)
960f110bfc7SLiam Girdwood 			dev_err(platform->dev, "ASoC: failed to remove %d\n",
961f110bfc7SLiam Girdwood 				ret);
962d12cd198SStephen Warren 	}
963d12cd198SStephen Warren 
964d12cd198SStephen Warren 	/* Make sure all DAPM widgets are freed */
965d12cd198SStephen Warren 	snd_soc_dapm_free(&platform->dapm);
966d12cd198SStephen Warren 
967d12cd198SStephen Warren 	soc_cleanup_platform_debugfs(platform);
968d12cd198SStephen Warren 	platform->probed = 0;
969d12cd198SStephen Warren 	list_del(&platform->card_list);
970d12cd198SStephen Warren 	module_put(platform->dev->driver->owner);
971d12cd198SStephen Warren 
972d12cd198SStephen Warren 	return 0;
973d12cd198SStephen Warren }
974d12cd198SStephen Warren 
975589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec)
976589c3563SJarkko Nikula {
977589c3563SJarkko Nikula 	int err;
978589c3563SJarkko Nikula 
979589c3563SJarkko Nikula 	if (codec->driver->remove) {
980589c3563SJarkko Nikula 		err = codec->driver->remove(codec);
981589c3563SJarkko Nikula 		if (err < 0)
982f110bfc7SLiam Girdwood 			dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
983589c3563SJarkko Nikula 	}
984589c3563SJarkko Nikula 
985589c3563SJarkko Nikula 	/* Make sure all DAPM widgets are freed */
986589c3563SJarkko Nikula 	snd_soc_dapm_free(&codec->dapm);
987589c3563SJarkko Nikula 
988589c3563SJarkko Nikula 	soc_cleanup_codec_debugfs(codec);
989589c3563SJarkko Nikula 	codec->probed = 0;
990589c3563SJarkko Nikula 	list_del(&codec->card_list);
991589c3563SJarkko Nikula 	module_put(codec->dev->driver->owner);
992589c3563SJarkko Nikula }
993589c3563SJarkko Nikula 
99462ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
995f0fba2adSLiam Girdwood {
996f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
997f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
998f0fba2adSLiam Girdwood 	int err;
999f0fba2adSLiam Girdwood 
1000f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1001f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
100236ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
100336ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
100436ae1a96SMark Brown 		device_unregister(rtd->dev);
1005f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
100602a06d30SBarry Song 	}
100702a06d30SBarry Song 
1008f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
10090168bf0dSLiam Girdwood 	if (codec_dai && codec_dai->probed &&
10100168bf0dSLiam Girdwood 			codec_dai->driver->remove_order == order) {
1011f0fba2adSLiam Girdwood 		if (codec_dai->driver->remove) {
1012f0fba2adSLiam Girdwood 			err = codec_dai->driver->remove(codec_dai);
1013f0fba2adSLiam Girdwood 			if (err < 0)
1014f110bfc7SLiam Girdwood 				dev_err(codec_dai->dev,
1015f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
10160837fc62SFabio Estevam 					codec_dai->name, err);
1017f0fba2adSLiam Girdwood 		}
1018f0fba2adSLiam Girdwood 		codec_dai->probed = 0;
1019f0fba2adSLiam Girdwood 		list_del(&codec_dai->card_list);
1020f0fba2adSLiam Girdwood 	}
1021f0fba2adSLiam Girdwood 
1022f0fba2adSLiam Girdwood 	/* remove the cpu_dai */
10230168bf0dSLiam Girdwood 	if (cpu_dai && cpu_dai->probed &&
10240168bf0dSLiam Girdwood 			cpu_dai->driver->remove_order == order) {
1025f0fba2adSLiam Girdwood 		if (cpu_dai->driver->remove) {
1026f0fba2adSLiam Girdwood 			err = cpu_dai->driver->remove(cpu_dai);
1027f0fba2adSLiam Girdwood 			if (err < 0)
1028f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1029f110bfc7SLiam Girdwood 					"ASoC: failed to remove %s: %d\n",
10300837fc62SFabio Estevam 					cpu_dai->name, err);
1031f0fba2adSLiam Girdwood 		}
1032f0fba2adSLiam Girdwood 		cpu_dai->probed = 0;
1033f0fba2adSLiam Girdwood 		list_del(&cpu_dai->card_list);
1034a9db7dbeSStephen Warren 
103518d75644SStephen Warren 		if (!cpu_dai->codec) {
103618d75644SStephen Warren 			snd_soc_dapm_free(&cpu_dai->dapm);
1037f0fba2adSLiam Girdwood 			module_put(cpu_dai->dev->driver->owner);
1038f0fba2adSLiam Girdwood 		}
1039f0fba2adSLiam Girdwood 	}
104018d75644SStephen Warren }
1041f0fba2adSLiam Girdwood 
104262ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
104362ae68faSStephen Warren 				       int order)
104462ae68faSStephen Warren {
104562ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
104662ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
104762ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
104862ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
104962ae68faSStephen Warren 	struct snd_soc_codec *codec;
105062ae68faSStephen Warren 
105162ae68faSStephen Warren 	/* remove the platform */
105262ae68faSStephen Warren 	if (platform && platform->probed &&
105362ae68faSStephen Warren 	    platform->driver->remove_order == order) {
105462ae68faSStephen Warren 		soc_remove_platform(platform);
105562ae68faSStephen Warren 	}
105662ae68faSStephen Warren 
105762ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
105862ae68faSStephen Warren 	if (codec_dai) {
105962ae68faSStephen Warren 		codec = codec_dai->codec;
106062ae68faSStephen Warren 		if (codec && codec->probed &&
106162ae68faSStephen Warren 		    codec->driver->remove_order == order)
106262ae68faSStephen Warren 			soc_remove_codec(codec);
106362ae68faSStephen Warren 	}
106462ae68faSStephen Warren 
106562ae68faSStephen Warren 	/* remove any CPU-side CODEC */
106662ae68faSStephen Warren 	if (cpu_dai) {
106762ae68faSStephen Warren 		codec = cpu_dai->codec;
106862ae68faSStephen Warren 		if (codec && codec->probed &&
106962ae68faSStephen Warren 		    codec->driver->remove_order == order)
107062ae68faSStephen Warren 			soc_remove_codec(codec);
107162ae68faSStephen Warren 	}
107262ae68faSStephen Warren }
107362ae68faSStephen Warren 
10740671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10750671fd8eSKuninori Morimoto {
10760168bf0dSLiam Girdwood 	int dai, order;
10770671fd8eSKuninori Morimoto 
10780168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10790168bf0dSLiam Girdwood 			order++) {
10800168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
108162ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10820168bf0dSLiam Girdwood 	}
108362ae68faSStephen Warren 
108462ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
108562ae68faSStephen Warren 			order++) {
108662ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
108762ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
108862ae68faSStephen Warren 	}
108962ae68faSStephen Warren 
10900671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10910671fd8eSKuninori Morimoto }
10920671fd8eSKuninori Morimoto 
1093ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
1094ead9b919SJarkko Nikula 				struct snd_soc_codec *codec)
1095ead9b919SJarkko Nikula {
1096ead9b919SJarkko Nikula 	int i;
1097ead9b919SJarkko Nikula 
1098ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1099ead9b919SJarkko Nikula 		return;
1100ead9b919SJarkko Nikula 
1101ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1102ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1103ead9b919SJarkko Nikula 		if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1104ead9b919SJarkko Nikula 			codec->name_prefix = map->name_prefix;
1105ead9b919SJarkko Nikula 			break;
1106ead9b919SJarkko Nikula 		}
1107ead9b919SJarkko Nikula 	}
1108ead9b919SJarkko Nikula }
1109ead9b919SJarkko Nikula 
1110589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card,
1111589c3563SJarkko Nikula 			   struct snd_soc_codec *codec)
1112589c3563SJarkko Nikula {
1113589c3563SJarkko Nikula 	int ret = 0;
111489b95ac0SMark Brown 	const struct snd_soc_codec_driver *driver = codec->driver;
1115888df395SMark Brown 	struct snd_soc_dai *dai;
1116589c3563SJarkko Nikula 
1117589c3563SJarkko Nikula 	codec->card = card;
1118589c3563SJarkko Nikula 	codec->dapm.card = card;
1119589c3563SJarkko Nikula 	soc_set_name_prefix(card, codec);
1120589c3563SJarkko Nikula 
112170d29331SJarkko Nikula 	if (!try_module_get(codec->dev->driver->owner))
112270d29331SJarkko Nikula 		return -ENODEV;
112370d29331SJarkko Nikula 
1124d5d1e0beSLars-Peter Clausen 	soc_init_codec_debugfs(codec);
1125d5d1e0beSLars-Peter Clausen 
112677530150SLars-Peter Clausen 	if (driver->dapm_widgets)
112777530150SLars-Peter Clausen 		snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
112877530150SLars-Peter Clausen 					  driver->num_dapm_widgets);
112977530150SLars-Peter Clausen 
1130888df395SMark Brown 	/* Create DAPM widgets for each DAI stream */
1131888df395SMark Brown 	list_for_each_entry(dai, &dai_list, list) {
1132888df395SMark Brown 		if (dai->dev != codec->dev)
1133888df395SMark Brown 			continue;
1134888df395SMark Brown 
1135888df395SMark Brown 		snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1136888df395SMark Brown 	}
1137888df395SMark Brown 
113833c5f969SMark Brown 	codec->dapm.idle_bias_off = driver->idle_bias_off;
113933c5f969SMark Brown 
114089b95ac0SMark Brown 	if (driver->probe) {
114189b95ac0SMark Brown 		ret = driver->probe(codec);
1142589c3563SJarkko Nikula 		if (ret < 0) {
1143589c3563SJarkko Nikula 			dev_err(codec->dev,
1144f110bfc7SLiam Girdwood 				"ASoC: failed to probe CODEC %d\n", ret);
114570d29331SJarkko Nikula 			goto err_probe;
1146589c3563SJarkko Nikula 		}
1147ff541f4bSChuansheng Liu 		WARN(codec->dapm.idle_bias_off &&
1148ff541f4bSChuansheng Liu 			codec->dapm.bias_level != SND_SOC_BIAS_OFF,
114910e8aa9aSMichał Mirosław 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
115010e8aa9aSMichał Mirosław 			codec->name);
1151589c3563SJarkko Nikula 	}
1152589c3563SJarkko Nikula 
115338cbf959SMark Brown 	/* If the driver didn't set I/O up try regmap */
115498d3088eSMark Brown 	if (!codec->write && dev_get_regmap(codec->dev, NULL))
115538cbf959SMark Brown 		snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
115638cbf959SMark Brown 
1157b7af1dafSMark Brown 	if (driver->controls)
1158022658beSLiam Girdwood 		snd_soc_add_codec_controls(codec, driver->controls,
1159b7af1dafSMark Brown 				     driver->num_controls);
116089b95ac0SMark Brown 	if (driver->dapm_routes)
116189b95ac0SMark Brown 		snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
116289b95ac0SMark Brown 					driver->num_dapm_routes);
116389b95ac0SMark Brown 
1164589c3563SJarkko Nikula 	/* mark codec as probed and add to card codec list */
1165589c3563SJarkko Nikula 	codec->probed = 1;
1166589c3563SJarkko Nikula 	list_add(&codec->card_list, &card->codec_dev_list);
11677be31be8SJarkko Nikula 	list_add(&codec->dapm.list, &card->dapm_list);
1168589c3563SJarkko Nikula 
116970d29331SJarkko Nikula 	return 0;
117070d29331SJarkko Nikula 
117170d29331SJarkko Nikula err_probe:
1172d5d1e0beSLars-Peter Clausen 	soc_cleanup_codec_debugfs(codec);
117370d29331SJarkko Nikula 	module_put(codec->dev->driver->owner);
117470d29331SJarkko Nikula 
1175589c3563SJarkko Nikula 	return ret;
1176589c3563SJarkko Nikula }
1177589c3563SJarkko Nikula 
1178956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card,
1179956245e9SLiam Girdwood 			   struct snd_soc_platform *platform)
1180956245e9SLiam Girdwood {
1181956245e9SLiam Girdwood 	int ret = 0;
1182956245e9SLiam Girdwood 	const struct snd_soc_platform_driver *driver = platform->driver;
1183be09ad90SLiam Girdwood 	struct snd_soc_dai *dai;
1184956245e9SLiam Girdwood 
1185956245e9SLiam Girdwood 	platform->card = card;
1186b7950641SLiam Girdwood 	platform->dapm.card = card;
1187956245e9SLiam Girdwood 
1188956245e9SLiam Girdwood 	if (!try_module_get(platform->dev->driver->owner))
1189956245e9SLiam Girdwood 		return -ENODEV;
1190956245e9SLiam Girdwood 
1191731f1ab2SSebastien Guiriec 	soc_init_platform_debugfs(platform);
1192731f1ab2SSebastien Guiriec 
1193cb2cf612SLiam Girdwood 	if (driver->dapm_widgets)
1194cb2cf612SLiam Girdwood 		snd_soc_dapm_new_controls(&platform->dapm,
1195cb2cf612SLiam Girdwood 			driver->dapm_widgets, driver->num_dapm_widgets);
1196cb2cf612SLiam Girdwood 
1197be09ad90SLiam Girdwood 	/* Create DAPM widgets for each DAI stream */
1198be09ad90SLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
1199be09ad90SLiam Girdwood 		if (dai->dev != platform->dev)
1200be09ad90SLiam Girdwood 			continue;
1201be09ad90SLiam Girdwood 
1202be09ad90SLiam Girdwood 		snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1203be09ad90SLiam Girdwood 	}
1204be09ad90SLiam Girdwood 
12053fec6b6dSStephen Warren 	platform->dapm.idle_bias_off = 1;
12063fec6b6dSStephen Warren 
1207956245e9SLiam Girdwood 	if (driver->probe) {
1208956245e9SLiam Girdwood 		ret = driver->probe(platform);
1209956245e9SLiam Girdwood 		if (ret < 0) {
1210956245e9SLiam Girdwood 			dev_err(platform->dev,
1211f110bfc7SLiam Girdwood 				"ASoC: failed to probe platform %d\n", ret);
1212956245e9SLiam Girdwood 			goto err_probe;
1213956245e9SLiam Girdwood 		}
1214956245e9SLiam Girdwood 	}
1215956245e9SLiam Girdwood 
1216cb2cf612SLiam Girdwood 	if (driver->controls)
1217cb2cf612SLiam Girdwood 		snd_soc_add_platform_controls(platform, driver->controls,
1218cb2cf612SLiam Girdwood 				     driver->num_controls);
1219cb2cf612SLiam Girdwood 	if (driver->dapm_routes)
1220cb2cf612SLiam Girdwood 		snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1221cb2cf612SLiam Girdwood 					driver->num_dapm_routes);
1222cb2cf612SLiam Girdwood 
1223956245e9SLiam Girdwood 	/* mark platform as probed and add to card platform list */
1224956245e9SLiam Girdwood 	platform->probed = 1;
1225956245e9SLiam Girdwood 	list_add(&platform->card_list, &card->platform_dev_list);
1226b7950641SLiam Girdwood 	list_add(&platform->dapm.list, &card->dapm_list);
1227956245e9SLiam Girdwood 
1228956245e9SLiam Girdwood 	return 0;
1229956245e9SLiam Girdwood 
1230956245e9SLiam Girdwood err_probe:
123102db1103SLiam Girdwood 	soc_cleanup_platform_debugfs(platform);
1232956245e9SLiam Girdwood 	module_put(platform->dev->driver->owner);
1233956245e9SLiam Girdwood 
1234956245e9SLiam Girdwood 	return ret;
1235956245e9SLiam Girdwood }
1236956245e9SLiam Girdwood 
123736ae1a96SMark Brown static void rtd_release(struct device *dev)
123836ae1a96SMark Brown {
123936ae1a96SMark Brown 	kfree(dev);
124036ae1a96SMark Brown }
1241f0fba2adSLiam Girdwood 
1242589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card,
1243589c3563SJarkko Nikula 				   struct snd_soc_codec *codec,
1244589c3563SJarkko Nikula 				   int num, int dailess)
1245589c3563SJarkko Nikula {
1246589c3563SJarkko Nikula 	struct snd_soc_dai_link *dai_link = NULL;
1247589c3563SJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = NULL;
1248589c3563SJarkko Nikula 	struct snd_soc_pcm_runtime *rtd;
1249589c3563SJarkko Nikula 	const char *temp, *name;
1250589c3563SJarkko Nikula 	int ret = 0;
1251589c3563SJarkko Nikula 
1252589c3563SJarkko Nikula 	if (!dailess) {
1253589c3563SJarkko Nikula 		dai_link = &card->dai_link[num];
1254589c3563SJarkko Nikula 		rtd = &card->rtd[num];
1255589c3563SJarkko Nikula 		name = dai_link->name;
1256589c3563SJarkko Nikula 	} else {
1257589c3563SJarkko Nikula 		aux_dev = &card->aux_dev[num];
1258589c3563SJarkko Nikula 		rtd = &card->rtd_aux[num];
1259589c3563SJarkko Nikula 		name = aux_dev->name;
1260589c3563SJarkko Nikula 	}
12610962bb21SJanusz Krzysztofik 	rtd->card = card;
1262589c3563SJarkko Nikula 
1263589c3563SJarkko Nikula 	/* machine controls, routes and widgets are not prefixed */
1264589c3563SJarkko Nikula 	temp = codec->name_prefix;
1265589c3563SJarkko Nikula 	codec->name_prefix = NULL;
1266589c3563SJarkko Nikula 
1267589c3563SJarkko Nikula 	/* do machine specific initialization */
1268589c3563SJarkko Nikula 	if (!dailess && dai_link->init)
1269589c3563SJarkko Nikula 		ret = dai_link->init(rtd);
1270589c3563SJarkko Nikula 	else if (dailess && aux_dev->init)
1271589c3563SJarkko Nikula 		ret = aux_dev->init(&codec->dapm);
1272589c3563SJarkko Nikula 	if (ret < 0) {
1273f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1274589c3563SJarkko Nikula 		return ret;
1275589c3563SJarkko Nikula 	}
1276589c3563SJarkko Nikula 	codec->name_prefix = temp;
1277589c3563SJarkko Nikula 
1278589c3563SJarkko Nikula 	/* register the rtd device */
1279589c3563SJarkko Nikula 	rtd->codec = codec;
128036ae1a96SMark Brown 
128136ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
128236ae1a96SMark Brown 	if (!rtd->dev)
128336ae1a96SMark Brown 		return -ENOMEM;
128436ae1a96SMark Brown 	device_initialize(rtd->dev);
128536ae1a96SMark Brown 	rtd->dev->parent = card->dev;
128636ae1a96SMark Brown 	rtd->dev->release = rtd_release;
128736ae1a96SMark Brown 	rtd->dev->init_name = name;
128836ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1289b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
129001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
129101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
129201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
129301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
129436ae1a96SMark Brown 	ret = device_add(rtd->dev);
1295589c3563SJarkko Nikula 	if (ret < 0) {
1296865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1297865df9cbSChuansheng Liu 		put_device(rtd->dev);
1298589c3563SJarkko Nikula 		dev_err(card->dev,
1299f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1300589c3563SJarkko Nikula 		return ret;
1301589c3563SJarkko Nikula 	}
1302589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1303589c3563SJarkko Nikula 
1304589c3563SJarkko Nikula 	/* add DAPM sysfs entries for this codec */
130536ae1a96SMark Brown 	ret = snd_soc_dapm_sys_add(rtd->dev);
1306589c3563SJarkko Nikula 	if (ret < 0)
1307589c3563SJarkko Nikula 		dev_err(codec->dev,
1308f110bfc7SLiam Girdwood 			"ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1309589c3563SJarkko Nikula 
1310589c3563SJarkko Nikula 	/* add codec sysfs entries */
131136ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1312589c3563SJarkko Nikula 	if (ret < 0)
1313589c3563SJarkko Nikula 		dev_err(codec->dev,
1314f110bfc7SLiam Girdwood 			"ASoC: failed to add codec sysfs files: %d\n", ret);
1315589c3563SJarkko Nikula 
1316f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1317f86dcef8SLiam Girdwood 	/* add DPCM sysfs entries */
1318cd0f8911SLiam Girdwood 	if (!dailess && !dai_link->dynamic)
1319f86dcef8SLiam Girdwood 		goto out;
1320f86dcef8SLiam Girdwood 
1321f86dcef8SLiam Girdwood 	ret = soc_dpcm_debugfs_add(rtd);
1322f86dcef8SLiam Girdwood 	if (ret < 0)
1323f110bfc7SLiam Girdwood 		dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1324f86dcef8SLiam Girdwood 
1325f86dcef8SLiam Girdwood out:
1326f86dcef8SLiam Girdwood #endif
1327589c3563SJarkko Nikula 	return 0;
1328589c3563SJarkko Nikula }
1329589c3563SJarkko Nikula 
133062ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
133162ae68faSStephen Warren 				     int order)
133262ae68faSStephen Warren {
133362ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
133462ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
133562ae68faSStephen Warren 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
133662ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
133762ae68faSStephen Warren 	int ret;
133862ae68faSStephen Warren 
133962ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
134062ae68faSStephen Warren 	if (cpu_dai->codec &&
134162ae68faSStephen Warren 	    !cpu_dai->codec->probed &&
134262ae68faSStephen Warren 	    cpu_dai->codec->driver->probe_order == order) {
134362ae68faSStephen Warren 		ret = soc_probe_codec(card, cpu_dai->codec);
134462ae68faSStephen Warren 		if (ret < 0)
134562ae68faSStephen Warren 			return ret;
134662ae68faSStephen Warren 	}
134762ae68faSStephen Warren 
134862ae68faSStephen Warren 	/* probe the CODEC-side component */
134962ae68faSStephen Warren 	if (!codec_dai->codec->probed &&
135062ae68faSStephen Warren 	    codec_dai->codec->driver->probe_order == order) {
135162ae68faSStephen Warren 		ret = soc_probe_codec(card, codec_dai->codec);
135262ae68faSStephen Warren 		if (ret < 0)
135362ae68faSStephen Warren 			return ret;
135462ae68faSStephen Warren 	}
135562ae68faSStephen Warren 
135662ae68faSStephen Warren 	/* probe the platform */
135762ae68faSStephen Warren 	if (!platform->probed &&
135862ae68faSStephen Warren 	    platform->driver->probe_order == order) {
135962ae68faSStephen Warren 		ret = soc_probe_platform(card, platform);
136062ae68faSStephen Warren 		if (ret < 0)
136162ae68faSStephen Warren 			return ret;
136262ae68faSStephen Warren 	}
136362ae68faSStephen Warren 
136462ae68faSStephen Warren 	return 0;
136562ae68faSStephen Warren }
136662ae68faSStephen Warren 
136762ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1368f0fba2adSLiam Girdwood {
1369f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1370f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1371f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
1372f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1373c74184edSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1374c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1375c74184edSMark Brown 	struct snd_soc_dapm_widget *play_w, *capture_w;
1376f0fba2adSLiam Girdwood 	int ret;
1377f0fba2adSLiam Girdwood 
1378f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
13790168bf0dSLiam Girdwood 			card->name, num, order);
1380f0fba2adSLiam Girdwood 
1381f0fba2adSLiam Girdwood 	/* config components */
1382f0fba2adSLiam Girdwood 	cpu_dai->platform = platform;
1383f0fba2adSLiam Girdwood 	codec_dai->card = card;
1384f0fba2adSLiam Girdwood 	cpu_dai->card = card;
1385f0fba2adSLiam Girdwood 
1386f0fba2adSLiam Girdwood 	/* set default power off timeout */
1387f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1388f0fba2adSLiam Girdwood 
1389f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
13900168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
13910168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1392a9db7dbeSStephen Warren 		if (!cpu_dai->codec) {
1393be09ad90SLiam Girdwood 			cpu_dai->dapm.card = card;
139461b61e3cSLiam Girdwood 			if (!try_module_get(cpu_dai->dev->driver->owner))
139561b61e3cSLiam Girdwood 				return -ENODEV;
139661b61e3cSLiam Girdwood 
139718d75644SStephen Warren 			list_add(&cpu_dai->dapm.list, &card->dapm_list);
1398a9db7dbeSStephen Warren 		}
1399be09ad90SLiam Girdwood 
1400f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1401f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1402f0fba2adSLiam Girdwood 			if (ret < 0) {
1403f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1404f110bfc7SLiam Girdwood 					"ASoC: failed to probe CPU DAI %s: %d\n",
14050837fc62SFabio Estevam 					cpu_dai->name, ret);
140661b61e3cSLiam Girdwood 				module_put(cpu_dai->dev->driver->owner);
1407f0fba2adSLiam Girdwood 				return ret;
1408f0fba2adSLiam Girdwood 			}
1409f0fba2adSLiam Girdwood 		}
1410f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
14111c8371d6SWolfram Sang 		/* mark cpu_dai as probed and add to card dai list */
1412f0fba2adSLiam Girdwood 		list_add(&cpu_dai->card_list, &card->dai_dev_list);
1413f0fba2adSLiam Girdwood 	}
1414f0fba2adSLiam Girdwood 
1415f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14160168bf0dSLiam Girdwood 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1417f0fba2adSLiam Girdwood 		if (codec_dai->driver->probe) {
1418f0fba2adSLiam Girdwood 			ret = codec_dai->driver->probe(codec_dai);
1419f0fba2adSLiam Girdwood 			if (ret < 0) {
1420f110bfc7SLiam Girdwood 				dev_err(codec_dai->dev,
1421f110bfc7SLiam Girdwood 					"ASoC: failed to probe CODEC DAI %s: %d\n",
14220837fc62SFabio Estevam 					codec_dai->name, ret);
1423f0fba2adSLiam Girdwood 				return ret;
1424f0fba2adSLiam Girdwood 			}
1425f0fba2adSLiam Girdwood 		}
1426f0fba2adSLiam Girdwood 
14271c8371d6SWolfram Sang 		/* mark codec_dai as probed and add to card dai list */
1428f0fba2adSLiam Girdwood 		codec_dai->probed = 1;
1429f0fba2adSLiam Girdwood 		list_add(&codec_dai->card_list, &card->dai_dev_list);
1430f0fba2adSLiam Girdwood 	}
1431f0fba2adSLiam Girdwood 
14320168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14330168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14340168bf0dSLiam Girdwood 		return 0;
14350168bf0dSLiam Girdwood 
1436589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 0);
1437589c3563SJarkko Nikula 	if (ret)
1438f0fba2adSLiam Girdwood 		return ret;
1439f0fba2adSLiam Girdwood 
144036ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1441f0fba2adSLiam Girdwood 	if (ret < 0)
1442f110bfc7SLiam Girdwood 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1443f110bfc7SLiam Girdwood 			ret);
1444f0fba2adSLiam Girdwood 
14451245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
14461245b700SNamarta Kohli 		/*create compress_device"*/
14471245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
14481245b700SNamarta Kohli 		if (ret < 0) {
1449f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
14501245b700SNamarta Kohli 					 dai_link->stream_name);
14511245b700SNamarta Kohli 			return ret;
14521245b700SNamarta Kohli 		}
14531245b700SNamarta Kohli 	} else {
14541245b700SNamarta Kohli 
1455c74184edSMark Brown 		if (!dai_link->params) {
1456f0fba2adSLiam Girdwood 			/* create the pcm */
1457f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1458f0fba2adSLiam Girdwood 			if (ret < 0) {
1459f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
14600837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1461f0fba2adSLiam Girdwood 				return ret;
1462f0fba2adSLiam Girdwood 			}
1463c74184edSMark Brown 		} else {
14649d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
14659d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
14669d58a077SRichard Fitzgerald 
1467c74184edSMark Brown 			/* link the DAI widgets */
1468c74184edSMark Brown 			play_w = codec_dai->playback_widget;
1469c74184edSMark Brown 			capture_w = cpu_dai->capture_widget;
1470c74184edSMark Brown 			if (play_w && capture_w) {
1471c74184edSMark Brown 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1472c74184edSMark Brown 						   capture_w, play_w);
1473c74184edSMark Brown 				if (ret != 0) {
1474f110bfc7SLiam Girdwood 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1475c74184edSMark Brown 						play_w->name, capture_w->name, ret);
1476c74184edSMark Brown 					return ret;
1477c74184edSMark Brown 				}
1478c74184edSMark Brown 			}
1479c74184edSMark Brown 
1480c74184edSMark Brown 			play_w = cpu_dai->playback_widget;
1481c74184edSMark Brown 			capture_w = codec_dai->capture_widget;
1482c74184edSMark Brown 			if (play_w && capture_w) {
1483c74184edSMark Brown 				ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1484c74184edSMark Brown 						   capture_w, play_w);
1485c74184edSMark Brown 				if (ret != 0) {
1486f110bfc7SLiam Girdwood 					dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1487c74184edSMark Brown 						play_w->name, capture_w->name, ret);
1488c74184edSMark Brown 					return ret;
1489c74184edSMark Brown 				}
1490c74184edSMark Brown 			}
1491c74184edSMark Brown 		}
14921245b700SNamarta Kohli 	}
1493f0fba2adSLiam Girdwood 
1494f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
1495f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control)
1496f0fba2adSLiam Girdwood 		snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1497f0fba2adSLiam Girdwood 
1498f0fba2adSLiam Girdwood 	return 0;
1499f0fba2adSLiam Girdwood }
1500f0fba2adSLiam Girdwood 
1501f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
1502f0fba2adSLiam Girdwood static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1503f0fba2adSLiam Girdwood {
1504f0fba2adSLiam Girdwood 	int ret;
1505f0fba2adSLiam Girdwood 
1506f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1507f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
15086b05eda6SMark Brown 	 */
1509f0fba2adSLiam Girdwood 	if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
15100562f788SMika Westerberg 		/*
15110562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
15120562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
15130562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
15140562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
15150562f788SMika Westerberg 		 *
15160562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
15170562f788SMika Westerberg 		 */
15180562f788SMika Westerberg 		if (!rtd->codec->ac97_created)
15190562f788SMika Westerberg 			return 0;
1520f0fba2adSLiam Girdwood 
1521f0fba2adSLiam Girdwood 		ret = soc_ac97_dev_register(rtd->codec);
1522f0fba2adSLiam Girdwood 		if (ret < 0) {
1523f110bfc7SLiam Girdwood 			dev_err(rtd->codec->dev,
1524f110bfc7SLiam Girdwood 				"ASoC: AC97 device register failed: %d\n", ret);
1525f0fba2adSLiam Girdwood 			return ret;
1526435c5e25SMark Brown 		}
1527435c5e25SMark Brown 
1528f0fba2adSLiam Girdwood 		rtd->codec->ac97_registered = 1;
1529f0fba2adSLiam Girdwood 	}
1530f0fba2adSLiam Girdwood 	return 0;
1531f0fba2adSLiam Girdwood }
1532435c5e25SMark Brown 
1533f0fba2adSLiam Girdwood static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1534f0fba2adSLiam Girdwood {
1535f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1536f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1537f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1538f0fba2adSLiam Girdwood 	}
1539f0fba2adSLiam Girdwood }
1540f0fba2adSLiam Girdwood #endif
1541435c5e25SMark Brown 
1542b19e6e7bSMark Brown static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1543b19e6e7bSMark Brown {
1544b19e6e7bSMark Brown 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1545b19e6e7bSMark Brown 	struct snd_soc_codec *codec;
1546b19e6e7bSMark Brown 
1547b19e6e7bSMark Brown 	/* find CODEC from registered CODECs*/
1548b19e6e7bSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
1549b19e6e7bSMark Brown 		if (!strcmp(codec->name, aux_dev->codec_name))
1550b19e6e7bSMark Brown 			return 0;
1551b19e6e7bSMark Brown 	}
1552b19e6e7bSMark Brown 
1553f110bfc7SLiam Girdwood 	dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1554fb099cb7SMark Brown 
1555b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1556b19e6e7bSMark Brown }
1557b19e6e7bSMark Brown 
15582eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
15592eea392dSJarkko Nikula {
15602eea392dSJarkko Nikula 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
15612eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
1562676ad98aSJarkko Nikula 	int ret = -ENODEV;
15632eea392dSJarkko Nikula 
15642eea392dSJarkko Nikula 	/* find CODEC from registered CODECs*/
15652eea392dSJarkko Nikula 	list_for_each_entry(codec, &codec_list, list) {
15662eea392dSJarkko Nikula 		if (!strcmp(codec->name, aux_dev->codec_name)) {
15672eea392dSJarkko Nikula 			if (codec->probed) {
15682eea392dSJarkko Nikula 				dev_err(codec->dev,
1569f110bfc7SLiam Girdwood 					"ASoC: codec already probed");
15702eea392dSJarkko Nikula 				ret = -EBUSY;
15712eea392dSJarkko Nikula 				goto out;
15722eea392dSJarkko Nikula 			}
1573676ad98aSJarkko Nikula 			goto found;
15742eea392dSJarkko Nikula 		}
15752eea392dSJarkko Nikula 	}
1576676ad98aSJarkko Nikula 	/* codec not found */
1577f110bfc7SLiam Girdwood 	dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1578b19e6e7bSMark Brown 	return -EPROBE_DEFER;
15792eea392dSJarkko Nikula 
1580676ad98aSJarkko Nikula found:
1581589c3563SJarkko Nikula 	ret = soc_probe_codec(card, codec);
15822eea392dSJarkko Nikula 	if (ret < 0)
1583589c3563SJarkko Nikula 		return ret;
15842eea392dSJarkko Nikula 
1585589c3563SJarkko Nikula 	ret = soc_post_component_init(card, codec, num, 1);
15862eea392dSJarkko Nikula 
15872eea392dSJarkko Nikula out:
15882eea392dSJarkko Nikula 	return ret;
15892eea392dSJarkko Nikula }
15902eea392dSJarkko Nikula 
15912eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
15922eea392dSJarkko Nikula {
15932eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
15942eea392dSJarkko Nikula 	struct snd_soc_codec *codec = rtd->codec;
15952eea392dSJarkko Nikula 
15962eea392dSJarkko Nikula 	/* unregister the rtd device */
15972eea392dSJarkko Nikula 	if (rtd->dev_registered) {
159836ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1599d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
16002eea392dSJarkko Nikula 		rtd->dev_registered = 0;
16012eea392dSJarkko Nikula 	}
16022eea392dSJarkko Nikula 
1603589c3563SJarkko Nikula 	if (codec && codec->probed)
1604589c3563SJarkko Nikula 		soc_remove_codec(codec);
16052eea392dSJarkko Nikula }
16062eea392dSJarkko Nikula 
1607f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1608fdf0f54dSDimitris Papastamos {
1609fdf0f54dSDimitris Papastamos 	int ret;
1610fdf0f54dSDimitris Papastamos 
1611fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1612fdf0f54dSDimitris Papastamos 		return 0;
1613fdf0f54dSDimitris Papastamos 
1614fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1615fdf0f54dSDimitris Papastamos 	if (ret < 0) {
161610e8aa9aSMichał Mirosław 		dev_err(codec->dev,
161710e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
161810e8aa9aSMichał Mirosław 			ret);
1619fdf0f54dSDimitris Papastamos 		return ret;
1620fdf0f54dSDimitris Papastamos 	}
1621fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1622fdf0f54dSDimitris Papastamos 	return 0;
1623fdf0f54dSDimitris Papastamos }
1624fdf0f54dSDimitris Papastamos 
1625b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1626f0fba2adSLiam Girdwood {
1627fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
162875d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1629f04209a7SMark Brown 	int ret, i, order, dai_fmt;
163096dd3622SMark Brown 
163101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1632f0fba2adSLiam Girdwood 
1633b19e6e7bSMark Brown 	/* bind DAIs */
1634b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1635b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1636b19e6e7bSMark Brown 		if (ret != 0)
1637b19e6e7bSMark Brown 			goto base_error;
1638db2a4165SFrank Mandarino 	}
1639db2a4165SFrank Mandarino 
1640b19e6e7bSMark Brown 	/* check aux_devs too */
1641b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
1642b19e6e7bSMark Brown 		ret = soc_check_aux_dev(card, i);
1643b19e6e7bSMark Brown 		if (ret != 0)
1644b19e6e7bSMark Brown 			goto base_error;
1645db2a4165SFrank Mandarino 	}
1646db2a4165SFrank Mandarino 
1647fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1648fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1649fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1650fdf0f54dSDimitris Papastamos 			continue;
1651f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1652b19e6e7bSMark Brown 		if (ret < 0)
1653b19e6e7bSMark Brown 			goto base_error;
1654fdf0f54dSDimitris Papastamos 	}
1655fdf0f54dSDimitris Papastamos 
1656f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1657f0fba2adSLiam Girdwood 	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1658f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1659f0fba2adSLiam Girdwood 	if (ret < 0) {
166010e8aa9aSMichał Mirosław 		dev_err(card->dev,
166110e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
166210e8aa9aSMichał Mirosław 			card->name, ret);
1663b19e6e7bSMark Brown 		goto base_error;
1664db2a4165SFrank Mandarino 	}
1665f0fba2adSLiam Girdwood 	card->snd_card->dev = card->dev;
1666db2a4165SFrank Mandarino 
1667e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1668e37a4970SMark Brown 	card->dapm.dev = card->dev;
1669e37a4970SMark Brown 	card->dapm.card = card;
1670e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1671e37a4970SMark Brown 
1672d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1673d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1674d5d1e0beSLars-Peter Clausen #endif
1675d5d1e0beSLars-Peter Clausen 
167688ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
16776ed25978SAndy Green 	/* deferred resume work */
16786308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16791301a964SRandy Dunlap #endif
16806ed25978SAndy Green 
16819a841ebbSMark Brown 	if (card->dapm_widgets)
16829a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
16839a841ebbSMark Brown 					  card->num_dapm_widgets);
16849a841ebbSMark Brown 
1685f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1686f0fba2adSLiam Girdwood 	if (card->probe) {
1687e7361ec4SMark Brown 		ret = card->probe(card);
1688f0fba2adSLiam Girdwood 		if (ret < 0)
1689f0fba2adSLiam Girdwood 			goto card_probe_error;
1690f0fba2adSLiam Girdwood 	}
1691f0fba2adSLiam Girdwood 
169262ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
16930168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
16940168bf0dSLiam Girdwood 			order++) {
1695fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
169662ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
169762ae68faSStephen Warren 			if (ret < 0) {
1698f110bfc7SLiam Girdwood 				dev_err(card->dev,
1699f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1700f110bfc7SLiam Girdwood 					ret);
170162ae68faSStephen Warren 				goto probe_dai_err;
170262ae68faSStephen Warren 			}
170362ae68faSStephen Warren 		}
170462ae68faSStephen Warren 	}
170562ae68faSStephen Warren 
170662ae68faSStephen Warren 	/* probe all DAI links on this card */
170762ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
170862ae68faSStephen Warren 			order++) {
170962ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
171062ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1711fe3e78e0SMark Brown 			if (ret < 0) {
1712f110bfc7SLiam Girdwood 				dev_err(card->dev,
1713f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1714f110bfc7SLiam Girdwood 					ret);
1715f0fba2adSLiam Girdwood 				goto probe_dai_err;
1716fe3e78e0SMark Brown 			}
1717fe3e78e0SMark Brown 		}
17180168bf0dSLiam Girdwood 	}
1719fe3e78e0SMark Brown 
17202eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
17212eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
17222eea392dSJarkko Nikula 		if (ret < 0) {
1723f110bfc7SLiam Girdwood 			dev_err(card->dev,
1724f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1725f110bfc7SLiam Girdwood 				ret);
17262eea392dSJarkko Nikula 			goto probe_aux_dev_err;
17272eea392dSJarkko Nikula 		}
17282eea392dSJarkko Nikula 	}
17292eea392dSJarkko Nikula 
1730888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1731b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1732888df395SMark Brown 
1733b7af1dafSMark Brown 	if (card->controls)
1734022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1735b7af1dafSMark Brown 
1736b8ad29deSMark Brown 	if (card->dapm_routes)
1737b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1738b8ad29deSMark Brown 					card->num_dapm_routes);
1739b8ad29deSMark Brown 
174075d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
174175d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1742f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
174375d9ac46SMark Brown 
1744f04209a7SMark Brown 		if (dai_fmt) {
174575d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1746f04209a7SMark Brown 						  dai_fmt);
17475e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
174875d9ac46SMark Brown 				dev_warn(card->rtd[i].codec_dai->dev,
1749f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
175075d9ac46SMark Brown 					 ret);
1751f04209a7SMark Brown 		}
1752f04209a7SMark Brown 
1753f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1754fe33d4c5SStephen Warren 		if (dai_fmt &&
1755fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1756f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1757f04209a7SMark Brown 						  dai_fmt);
1758f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1759f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1760f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
1761f04209a7SMark Brown 					 ret);
1762f04209a7SMark Brown 		} else if (dai_fmt) {
1763f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1764f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1765f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1766f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1767f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1768f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1769f04209a7SMark Brown 				break;
1770f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1771f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1772f04209a7SMark Brown 				break;
1773f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1774f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1775f04209a7SMark Brown 				break;
1776f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1777f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1778f04209a7SMark Brown 				break;
1779f04209a7SMark Brown 			}
178075d9ac46SMark Brown 
178175d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1782f04209a7SMark Brown 						  dai_fmt);
17835e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
178475d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1785f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
178675d9ac46SMark Brown 					 ret);
178775d9ac46SMark Brown 		}
178875d9ac46SMark Brown 	}
178975d9ac46SMark Brown 
1790f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1791fe3e78e0SMark Brown 		 "%s", card->name);
1792f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
179322de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1794f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1795f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1796f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1797f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1798f0e8ed85SMark Brown 		case '_':
1799f0e8ed85SMark Brown 		case '-':
1800f0e8ed85SMark Brown 		case '\0':
1801f0e8ed85SMark Brown 			break;
1802f0e8ed85SMark Brown 		default:
1803f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1804f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1805f0e8ed85SMark Brown 			break;
1806f0e8ed85SMark Brown 		}
1807f0e8ed85SMark Brown 	}
1808fe3e78e0SMark Brown 
180928e9ad92SMark Brown 	if (card->late_probe) {
181028e9ad92SMark Brown 		ret = card->late_probe(card);
181128e9ad92SMark Brown 		if (ret < 0) {
1812f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
181328e9ad92SMark Brown 				card->name, ret);
181428e9ad92SMark Brown 			goto probe_aux_dev_err;
181528e9ad92SMark Brown 		}
181628e9ad92SMark Brown 	}
181728e9ad92SMark Brown 
18181633281bSStephen Warren 	if (card->fully_routed)
1819b05d8dc1SMark Brown 		list_for_each_entry(codec, &card->codec_dev_list, card_list)
18201633281bSStephen Warren 			snd_soc_dapm_auto_nc_codec_pins(codec);
18211633281bSStephen Warren 
1822824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
18238c193b8dSLars-Peter Clausen 
1824f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1825fe3e78e0SMark Brown 	if (ret < 0) {
1826f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1827f110bfc7SLiam Girdwood 				ret);
18286b3ed785SAxel Lin 		goto probe_aux_dev_err;
1829fe3e78e0SMark Brown 	}
1830fe3e78e0SMark Brown 
1831fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1832f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1833f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1834f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1835fe3e78e0SMark Brown 		if (ret < 0) {
183610e8aa9aSMichał Mirosław 			dev_err(card->dev,
183710e8aa9aSMichał Mirosław 				"ASoC: failed to register AC97: %d\n", ret);
18386b3ed785SAxel Lin 			while (--i >= 0)
18397d8316dfSMark Brown 				soc_unregister_ac97_dai_link(card->rtd[i].codec);
18406b3ed785SAxel Lin 			goto probe_aux_dev_err;
1841fe3e78e0SMark Brown 		}
1842fe3e78e0SMark Brown 	}
1843fe3e78e0SMark Brown #endif
1844fe3e78e0SMark Brown 
1845435c5e25SMark Brown 	card->instantiated = 1;
18464f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1847f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1848b19e6e7bSMark Brown 
1849b19e6e7bSMark Brown 	return 0;
1850db2a4165SFrank Mandarino 
18512eea392dSJarkko Nikula probe_aux_dev_err:
18522eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18532eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18542eea392dSJarkko Nikula 
1855f0fba2adSLiam Girdwood probe_dai_err:
18560671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1857fe3e78e0SMark Brown 
1858f0fba2adSLiam Girdwood card_probe_error:
185987506549SMark Brown 	if (card->remove)
1860e7361ec4SMark Brown 		card->remove(card);
1861f0fba2adSLiam Girdwood 
1862f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1863f0fba2adSLiam Girdwood 
1864b19e6e7bSMark Brown base_error:
1865f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1866db2a4165SFrank Mandarino 
1867b19e6e7bSMark Brown 	return ret;
1868435c5e25SMark Brown }
1869435c5e25SMark Brown 
1870435c5e25SMark Brown /* probes a new socdev */
1871435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1872435c5e25SMark Brown {
1873f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1874435c5e25SMark Brown 
187570a7ca34SVinod Koul 	/*
187670a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
187770a7ca34SVinod Koul 	 * we should not be here in that case so ret error
187870a7ca34SVinod Koul 	 */
187970a7ca34SVinod Koul 	if (!card)
188070a7ca34SVinod Koul 		return -EINVAL;
188170a7ca34SVinod Koul 
1882fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1883f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1884fe4085e8SMark Brown 		 card->name);
1885fe4085e8SMark Brown 
1886435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1887435c5e25SMark Brown 	card->dev = &pdev->dev;
1888f0fba2adSLiam Girdwood 
188928d528c8SMark Brown 	return snd_soc_register_card(card);
1890435c5e25SMark Brown }
1891435c5e25SMark Brown 
1892b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1893db2a4165SFrank Mandarino {
1894db2a4165SFrank Mandarino 	int i;
1895db2a4165SFrank Mandarino 
1896f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1897f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1898f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
189943829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1900db2a4165SFrank Mandarino 	}
1901db2a4165SFrank Mandarino 
19022eea392dSJarkko Nikula 	/* remove auxiliary devices */
19032eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
19042eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
19052eea392dSJarkko Nikula 
1906f0fba2adSLiam Girdwood 	/* remove and free each DAI */
19070671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1908f0fba2adSLiam Girdwood 
1909a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1910a6052154SJarkko Nikula 
1911f0fba2adSLiam Girdwood 	/* remove the card */
191287506549SMark Brown 	if (card->remove)
1913e7361ec4SMark Brown 		card->remove(card);
1914f0fba2adSLiam Girdwood 
19150aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
19160aaae527SLars-Peter Clausen 
1917f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1918b0e26485SVinod Koul 	return 0;
1919b0e26485SVinod Koul 
1920b2dfa62cSGuennadi Liakhovetski }
1921b0e26485SVinod Koul 
1922b0e26485SVinod Koul /* removes a socdev */
1923b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1924b0e26485SVinod Koul {
1925b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1926b0e26485SVinod Koul 
1927c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1928db2a4165SFrank Mandarino 	return 0;
1929db2a4165SFrank Mandarino }
1930db2a4165SFrank Mandarino 
19316f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
193251737470SMark Brown {
19336f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1934f0fba2adSLiam Girdwood 	int i;
193551737470SMark Brown 
193651737470SMark Brown 	if (!card->instantiated)
1937416356fcSMark Brown 		return 0;
193851737470SMark Brown 
193951737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
194051737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1941f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1942f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
194343829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1944f0fba2adSLiam Girdwood 	}
194551737470SMark Brown 
1946f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1947416356fcSMark Brown 
1948988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
1949988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
1950988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1951988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
1952988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(codec_dai->dev);
1953988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
1954988e8cc4SNicolin Chen 	}
1955988e8cc4SNicolin Chen 
1956416356fcSMark Brown 	return 0;
195751737470SMark Brown }
19586f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
195951737470SMark Brown 
19606f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1961b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1962b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1963b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1964b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
19656f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1966b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1967416356fcSMark Brown };
1968deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1969416356fcSMark Brown 
1970db2a4165SFrank Mandarino /* ASoC platform driver */
1971db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1972db2a4165SFrank Mandarino 	.driver		= {
1973db2a4165SFrank Mandarino 		.name		= "soc-audio",
19748b45a209SKay Sievers 		.owner		= THIS_MODULE,
19756f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1976db2a4165SFrank Mandarino 	},
1977db2a4165SFrank Mandarino 	.probe		= soc_probe,
1978db2a4165SFrank Mandarino 	.remove		= soc_remove,
1979db2a4165SFrank Mandarino };
1980db2a4165SFrank Mandarino 
1981096e49d5SMark Brown /**
1982096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1983096e49d5SMark Brown  *
1984096e49d5SMark Brown  * @codec: CODEC to query.
1985096e49d5SMark Brown  * @reg: Register to query.
1986096e49d5SMark Brown  *
1987096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1988096e49d5SMark Brown  */
1989181e055eSMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1990181e055eSMark Brown 				    unsigned int reg)
1991096e49d5SMark Brown {
19921500b7b5SDimitris Papastamos 	if (codec->volatile_register)
19931500b7b5SDimitris Papastamos 		return codec->volatile_register(codec, reg);
1994096e49d5SMark Brown 	else
1995096e49d5SMark Brown 		return 0;
1996096e49d5SMark Brown }
1997096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1998096e49d5SMark Brown 
1999db2a4165SFrank Mandarino /**
2000239c9706SDimitris Papastamos  * snd_soc_codec_readable_register: Report if a register is readable.
2001239c9706SDimitris Papastamos  *
2002239c9706SDimitris Papastamos  * @codec: CODEC to query.
2003239c9706SDimitris Papastamos  * @reg: Register to query.
2004239c9706SDimitris Papastamos  *
2005239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is readable.
2006239c9706SDimitris Papastamos  */
2007239c9706SDimitris Papastamos int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2008239c9706SDimitris Papastamos 				    unsigned int reg)
2009239c9706SDimitris Papastamos {
2010239c9706SDimitris Papastamos 	if (codec->readable_register)
2011239c9706SDimitris Papastamos 		return codec->readable_register(codec, reg);
2012239c9706SDimitris Papastamos 	else
201363fa0a28SLars-Peter Clausen 		return 1;
2014239c9706SDimitris Papastamos }
2015239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2016239c9706SDimitris Papastamos 
2017239c9706SDimitris Papastamos /**
2018239c9706SDimitris Papastamos  * snd_soc_codec_writable_register: Report if a register is writable.
2019239c9706SDimitris Papastamos  *
2020239c9706SDimitris Papastamos  * @codec: CODEC to query.
2021239c9706SDimitris Papastamos  * @reg: Register to query.
2022239c9706SDimitris Papastamos  *
2023239c9706SDimitris Papastamos  * Boolean function indicating if a CODEC register is writable.
2024239c9706SDimitris Papastamos  */
2025239c9706SDimitris Papastamos int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2026239c9706SDimitris Papastamos 				    unsigned int reg)
2027239c9706SDimitris Papastamos {
2028239c9706SDimitris Papastamos 	if (codec->writable_register)
2029239c9706SDimitris Papastamos 		return codec->writable_register(codec, reg);
2030239c9706SDimitris Papastamos 	else
203163fa0a28SLars-Peter Clausen 		return 1;
2032239c9706SDimitris Papastamos }
2033239c9706SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2034239c9706SDimitris Papastamos 
2035f1442bc1SLiam Girdwood int snd_soc_platform_read(struct snd_soc_platform *platform,
2036f1442bc1SLiam Girdwood 					unsigned int reg)
2037f1442bc1SLiam Girdwood {
2038f1442bc1SLiam Girdwood 	unsigned int ret;
2039f1442bc1SLiam Girdwood 
2040f1442bc1SLiam Girdwood 	if (!platform->driver->read) {
2041f110bfc7SLiam Girdwood 		dev_err(platform->dev, "ASoC: platform has no read back\n");
2042f1442bc1SLiam Girdwood 		return -1;
2043f1442bc1SLiam Girdwood 	}
2044f1442bc1SLiam Girdwood 
2045f1442bc1SLiam Girdwood 	ret = platform->driver->read(platform, reg);
2046f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2047a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_read(platform, reg, ret);
2048f1442bc1SLiam Girdwood 
2049f1442bc1SLiam Girdwood 	return ret;
2050f1442bc1SLiam Girdwood }
2051f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2052f1442bc1SLiam Girdwood 
2053f1442bc1SLiam Girdwood int snd_soc_platform_write(struct snd_soc_platform *platform,
2054f1442bc1SLiam Girdwood 					 unsigned int reg, unsigned int val)
2055f1442bc1SLiam Girdwood {
2056f1442bc1SLiam Girdwood 	if (!platform->driver->write) {
2057f110bfc7SLiam Girdwood 		dev_err(platform->dev, "ASoC: platform has no write back\n");
2058f1442bc1SLiam Girdwood 		return -1;
2059f1442bc1SLiam Girdwood 	}
2060f1442bc1SLiam Girdwood 
2061f1442bc1SLiam Girdwood 	dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2062a82ce2aeSLiam Girdwood 	trace_snd_soc_preg_write(platform, reg, val);
2063f1442bc1SLiam Girdwood 	return platform->driver->write(platform, reg, val);
2064f1442bc1SLiam Girdwood }
2065f1442bc1SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2066f1442bc1SLiam Girdwood 
2067239c9706SDimitris Papastamos /**
2068db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
2069db2a4165SFrank Mandarino  * @codec: audio codec
2070db2a4165SFrank Mandarino  * @ops: AC97 bus operations
2071db2a4165SFrank Mandarino  * @num: AC97 codec number
2072db2a4165SFrank Mandarino  *
2073db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
2074db2a4165SFrank Mandarino  */
2075db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2076db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
2077db2a4165SFrank Mandarino {
2078db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2079db2a4165SFrank Mandarino 
2080db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2081db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
2082db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2083db2a4165SFrank Mandarino 		return -ENOMEM;
2084db2a4165SFrank Mandarino 	}
2085db2a4165SFrank Mandarino 
2086db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2087db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
2088db2a4165SFrank Mandarino 		kfree(codec->ac97);
2089db2a4165SFrank Mandarino 		codec->ac97 = NULL;
2090db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
2091db2a4165SFrank Mandarino 		return -ENOMEM;
2092db2a4165SFrank Mandarino 	}
2093db2a4165SFrank Mandarino 
2094db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
2095db2a4165SFrank Mandarino 	codec->ac97->num = num;
20960562f788SMika Westerberg 
20970562f788SMika Westerberg 	/*
20980562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
20990562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
21000562f788SMika Westerberg 	 */
21010562f788SMika Westerberg 	codec->ac97_created = 1;
21020562f788SMika Westerberg 
2103db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2104db2a4165SFrank Mandarino 	return 0;
2105db2a4165SFrank Mandarino }
2106db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2107db2a4165SFrank Mandarino 
2108741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2109741a509fSMarkus Pargmann 
2110741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2111741a509fSMarkus Pargmann {
2112741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2113741a509fSMarkus Pargmann 
2114741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2115741a509fSMarkus Pargmann 
2116741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2117741a509fSMarkus Pargmann 
2118741a509fSMarkus Pargmann 	udelay(10);
2119741a509fSMarkus Pargmann 
2120741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2121741a509fSMarkus Pargmann 
2122741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2123741a509fSMarkus Pargmann 	msleep(2);
2124741a509fSMarkus Pargmann }
2125741a509fSMarkus Pargmann 
2126741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2127741a509fSMarkus Pargmann {
2128741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2129741a509fSMarkus Pargmann 
2130741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2131741a509fSMarkus Pargmann 
2132741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2133741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2134741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2135741a509fSMarkus Pargmann 
2136741a509fSMarkus Pargmann 	udelay(10);
2137741a509fSMarkus Pargmann 
2138741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2139741a509fSMarkus Pargmann 
2140741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2141741a509fSMarkus Pargmann 	msleep(2);
2142741a509fSMarkus Pargmann }
2143741a509fSMarkus Pargmann 
2144741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev,
2145741a509fSMarkus Pargmann 		struct snd_ac97_reset_cfg *cfg)
2146741a509fSMarkus Pargmann {
2147741a509fSMarkus Pargmann 	struct pinctrl *p;
2148741a509fSMarkus Pargmann 	struct pinctrl_state *state;
2149741a509fSMarkus Pargmann 	int gpio;
2150741a509fSMarkus Pargmann 	int ret;
2151741a509fSMarkus Pargmann 
2152741a509fSMarkus Pargmann 	p = devm_pinctrl_get(dev);
2153741a509fSMarkus Pargmann 	if (IS_ERR(p)) {
2154741a509fSMarkus Pargmann 		dev_err(dev, "Failed to get pinctrl\n");
2155741a509fSMarkus Pargmann 		return PTR_RET(p);
2156741a509fSMarkus Pargmann 	}
2157741a509fSMarkus Pargmann 	cfg->pctl = p;
2158741a509fSMarkus Pargmann 
2159741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-reset");
2160741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2161741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2162741a509fSMarkus Pargmann 		return PTR_RET(state);
2163741a509fSMarkus Pargmann 	}
2164741a509fSMarkus Pargmann 	cfg->pstate_reset = state;
2165741a509fSMarkus Pargmann 
2166741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
2167741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2168741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2169741a509fSMarkus Pargmann 		return PTR_RET(state);
2170741a509fSMarkus Pargmann 	}
2171741a509fSMarkus Pargmann 	cfg->pstate_warm_reset = state;
2172741a509fSMarkus Pargmann 
2173741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-running");
2174741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2175741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
2176741a509fSMarkus Pargmann 		return PTR_RET(state);
2177741a509fSMarkus Pargmann 	}
2178741a509fSMarkus Pargmann 	cfg->pstate_run = state;
2179741a509fSMarkus Pargmann 
2180741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2181741a509fSMarkus Pargmann 	if (gpio < 0) {
2182741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sync gpio\n");
2183741a509fSMarkus Pargmann 		return gpio;
2184741a509fSMarkus Pargmann 	}
2185741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2186741a509fSMarkus Pargmann 	if (ret) {
2187741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
2188741a509fSMarkus Pargmann 		return ret;
2189741a509fSMarkus Pargmann 	}
2190741a509fSMarkus Pargmann 	cfg->gpio_sync = gpio;
2191741a509fSMarkus Pargmann 
2192741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2193741a509fSMarkus Pargmann 	if (gpio < 0) {
2194741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2195741a509fSMarkus Pargmann 		return gpio;
2196741a509fSMarkus Pargmann 	}
2197741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2198741a509fSMarkus Pargmann 	if (ret) {
2199741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2200741a509fSMarkus Pargmann 		return ret;
2201741a509fSMarkus Pargmann 	}
2202741a509fSMarkus Pargmann 	cfg->gpio_sdata = gpio;
2203741a509fSMarkus Pargmann 
2204741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2205741a509fSMarkus Pargmann 	if (gpio < 0) {
2206741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-reset gpio\n");
2207741a509fSMarkus Pargmann 		return gpio;
2208741a509fSMarkus Pargmann 	}
2209741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2210741a509fSMarkus Pargmann 	if (ret) {
2211741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
2212741a509fSMarkus Pargmann 		return ret;
2213741a509fSMarkus Pargmann 	}
2214741a509fSMarkus Pargmann 	cfg->gpio_reset = gpio;
2215741a509fSMarkus Pargmann 
2216741a509fSMarkus Pargmann 	return 0;
2217741a509fSMarkus Pargmann }
2218741a509fSMarkus Pargmann 
2219b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops;
2220f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops);
2221b047e1ccSMark Brown 
2222b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2223b047e1ccSMark Brown {
2224b047e1ccSMark Brown 	if (ops == soc_ac97_ops)
2225b047e1ccSMark Brown 		return 0;
2226b047e1ccSMark Brown 
2227b047e1ccSMark Brown 	if (soc_ac97_ops && ops)
2228b047e1ccSMark Brown 		return -EBUSY;
2229b047e1ccSMark Brown 
2230b047e1ccSMark Brown 	soc_ac97_ops = ops;
2231b047e1ccSMark Brown 
2232b047e1ccSMark Brown 	return 0;
2233b047e1ccSMark Brown }
2234b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2235b047e1ccSMark Brown 
2236db2a4165SFrank Mandarino /**
2237741a509fSMarkus Pargmann  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2238741a509fSMarkus Pargmann  *
2239741a509fSMarkus Pargmann  * This function sets the reset and warm_reset properties of ops and parses
2240741a509fSMarkus Pargmann  * the device node of pdev to get pinctrl states and gpio numbers to use.
2241741a509fSMarkus Pargmann  */
2242741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2243741a509fSMarkus Pargmann 		struct platform_device *pdev)
2244741a509fSMarkus Pargmann {
2245741a509fSMarkus Pargmann 	struct device *dev = &pdev->dev;
2246741a509fSMarkus Pargmann 	struct snd_ac97_reset_cfg cfg;
2247741a509fSMarkus Pargmann 	int ret;
2248741a509fSMarkus Pargmann 
2249741a509fSMarkus Pargmann 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2250741a509fSMarkus Pargmann 	if (ret)
2251741a509fSMarkus Pargmann 		return ret;
2252741a509fSMarkus Pargmann 
2253741a509fSMarkus Pargmann 	ret = snd_soc_set_ac97_ops(ops);
2254741a509fSMarkus Pargmann 	if (ret)
2255741a509fSMarkus Pargmann 		return ret;
2256741a509fSMarkus Pargmann 
2257741a509fSMarkus Pargmann 	ops->warm_reset = snd_soc_ac97_warm_reset;
2258741a509fSMarkus Pargmann 	ops->reset = snd_soc_ac97_reset;
2259741a509fSMarkus Pargmann 
2260741a509fSMarkus Pargmann 	snd_ac97_rst_cfg = cfg;
2261741a509fSMarkus Pargmann 	return 0;
2262741a509fSMarkus Pargmann }
2263741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2264741a509fSMarkus Pargmann 
2265741a509fSMarkus Pargmann /**
2266db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2267db2a4165SFrank Mandarino  * @codec: audio codec
2268db2a4165SFrank Mandarino  *
2269db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2270db2a4165SFrank Mandarino  */
2271db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2272db2a4165SFrank Mandarino {
2273db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
2274f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
2275f0fba2adSLiam Girdwood 	soc_unregister_ac97_dai_link(codec);
2276f0fba2adSLiam Girdwood #endif
2277db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2278db2a4165SFrank Mandarino 	kfree(codec->ac97);
2279db2a4165SFrank Mandarino 	codec->ac97 = NULL;
22800562f788SMika Westerberg 	codec->ac97_created = 0;
2281db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
2282db2a4165SFrank Mandarino }
2283db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2284db2a4165SFrank Mandarino 
2285c3753707SMark Brown unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2286c3753707SMark Brown {
2287c3753707SMark Brown 	unsigned int ret;
2288c3753707SMark Brown 
2289c3acec26SMark Brown 	ret = codec->read(codec, reg);
2290c3753707SMark Brown 	dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2291a8b1d34fSMark Brown 	trace_snd_soc_reg_read(codec, reg, ret);
2292c3753707SMark Brown 
2293c3753707SMark Brown 	return ret;
2294c3753707SMark Brown }
2295c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_read);
2296c3753707SMark Brown 
2297c3753707SMark Brown unsigned int snd_soc_write(struct snd_soc_codec *codec,
2298c3753707SMark Brown 			   unsigned int reg, unsigned int val)
2299c3753707SMark Brown {
2300c3753707SMark Brown 	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2301a8b1d34fSMark Brown 	trace_snd_soc_reg_write(codec, reg, val);
2302c3acec26SMark Brown 	return codec->write(codec, reg, val);
2303c3753707SMark Brown }
2304c3753707SMark Brown EXPORT_SYMBOL_GPL(snd_soc_write);
2305c3753707SMark Brown 
2306db2a4165SFrank Mandarino /**
2307db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
2308db2a4165SFrank Mandarino  * @codec: audio codec
2309db2a4165SFrank Mandarino  * @reg: codec register
2310db2a4165SFrank Mandarino  * @mask: register mask
2311db2a4165SFrank Mandarino  * @value: new value
2312db2a4165SFrank Mandarino  *
2313db2a4165SFrank Mandarino  * Writes new register value.
2314db2a4165SFrank Mandarino  *
2315180c329dSTimur Tabi  * Returns 1 for change, 0 for no change, or negative error code.
2316db2a4165SFrank Mandarino  */
2317db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
231846f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2319db2a4165SFrank Mandarino {
23208a713da8SMark Brown 	bool change;
232146f5822fSDaniel Ribeiro 	unsigned int old, new;
2322180c329dSTimur Tabi 	int ret;
2323db2a4165SFrank Mandarino 
23248a713da8SMark Brown 	if (codec->using_regmap) {
23258a713da8SMark Brown 		ret = regmap_update_bits_check(codec->control_data, reg,
23268a713da8SMark Brown 					       mask, value, &change);
23278a713da8SMark Brown 	} else {
2328180c329dSTimur Tabi 		ret = snd_soc_read(codec, reg);
2329180c329dSTimur Tabi 		if (ret < 0)
2330180c329dSTimur Tabi 			return ret;
2331180c329dSTimur Tabi 
2332180c329dSTimur Tabi 		old = ret;
233378bf3c9aSMark Brown 		new = (old & ~mask) | (value & mask);
2334db2a4165SFrank Mandarino 		change = old != new;
23358a713da8SMark Brown 		if (change)
2336180c329dSTimur Tabi 			ret = snd_soc_write(codec, reg, new);
23378a713da8SMark Brown 	}
23388a713da8SMark Brown 
2339180c329dSTimur Tabi 	if (ret < 0)
2340180c329dSTimur Tabi 		return ret;
2341db2a4165SFrank Mandarino 
2342db2a4165SFrank Mandarino 	return change;
2343db2a4165SFrank Mandarino }
2344db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2345db2a4165SFrank Mandarino 
2346db2a4165SFrank Mandarino /**
23476c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
23486c508c62SEero Nurkkala  * @codec: audio codec
23496c508c62SEero Nurkkala  * @reg: codec register
23506c508c62SEero Nurkkala  * @mask: register mask
23516c508c62SEero Nurkkala  * @value: new value
23526c508c62SEero Nurkkala  *
23536c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
23546c508c62SEero Nurkkala  *
23556c508c62SEero Nurkkala  * Returns 1 for change else 0.
23566c508c62SEero Nurkkala  */
2357dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
23586c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
23596c508c62SEero Nurkkala 			       unsigned int value)
23606c508c62SEero Nurkkala {
23616c508c62SEero Nurkkala 	int change;
23626c508c62SEero Nurkkala 
23636c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
23646c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
23656c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
23666c508c62SEero Nurkkala 
23676c508c62SEero Nurkkala 	return change;
23686c508c62SEero Nurkkala }
2369dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
23706c508c62SEero Nurkkala 
23716c508c62SEero Nurkkala /**
2372db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
2373db2a4165SFrank Mandarino  * @codec: audio codec
2374db2a4165SFrank Mandarino  * @reg: codec register
2375db2a4165SFrank Mandarino  * @mask: register mask
2376db2a4165SFrank Mandarino  * @value: new value
2377db2a4165SFrank Mandarino  *
2378db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
2379db2a4165SFrank Mandarino  * different from the old value.
2380db2a4165SFrank Mandarino  *
2381db2a4165SFrank Mandarino  * Returns 1 for change else 0.
2382db2a4165SFrank Mandarino  */
2383db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
238446f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
2385db2a4165SFrank Mandarino {
2386db2a4165SFrank Mandarino 	int change;
238746f5822fSDaniel Ribeiro 	unsigned int old, new;
2388db2a4165SFrank Mandarino 
2389db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
2390db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
2391db2a4165SFrank Mandarino 	change = old != new;
2392db2a4165SFrank Mandarino 
2393db2a4165SFrank Mandarino 	return change;
2394db2a4165SFrank Mandarino }
2395db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2396db2a4165SFrank Mandarino 
2397db2a4165SFrank Mandarino /**
2398db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2399db2a4165SFrank Mandarino  * @_template: control template
2400db2a4165SFrank Mandarino  * @data: control private data
2401ac11a2b3SMark Brown  * @long_name: control long name
2402efb7ac3fSMark Brown  * @prefix: control name prefix
2403db2a4165SFrank Mandarino  *
2404db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2405db2a4165SFrank Mandarino  *
2406db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2407db2a4165SFrank Mandarino  */
2408db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
24093056557fSMark Brown 				  void *data, const char *long_name,
2410efb7ac3fSMark Brown 				  const char *prefix)
2411db2a4165SFrank Mandarino {
2412db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2413efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2414efb7ac3fSMark Brown 	char *name = NULL;
2415db2a4165SFrank Mandarino 
2416db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2417db2a4165SFrank Mandarino 	template.index = 0;
2418db2a4165SFrank Mandarino 
2419efb7ac3fSMark Brown 	if (!long_name)
2420efb7ac3fSMark Brown 		long_name = template.name;
2421efb7ac3fSMark Brown 
2422efb7ac3fSMark Brown 	if (prefix) {
24232b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2424efb7ac3fSMark Brown 		if (!name)
2425efb7ac3fSMark Brown 			return NULL;
2426efb7ac3fSMark Brown 
2427efb7ac3fSMark Brown 		template.name = name;
2428efb7ac3fSMark Brown 	} else {
2429efb7ac3fSMark Brown 		template.name = long_name;
2430efb7ac3fSMark Brown 	}
2431efb7ac3fSMark Brown 
2432efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2433efb7ac3fSMark Brown 
2434efb7ac3fSMark Brown 	kfree(name);
2435efb7ac3fSMark Brown 
2436efb7ac3fSMark Brown 	return kcontrol;
2437db2a4165SFrank Mandarino }
2438db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2439db2a4165SFrank Mandarino 
2440022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2441022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2442022658beSLiam Girdwood 	const char *prefix, void *data)
2443022658beSLiam Girdwood {
2444022658beSLiam Girdwood 	int err, i;
2445022658beSLiam Girdwood 
2446022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2447022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2448022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2449022658beSLiam Girdwood 						     control->name, prefix));
2450022658beSLiam Girdwood 		if (err < 0) {
2451f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2452f110bfc7SLiam Girdwood 				control->name, err);
2453022658beSLiam Girdwood 			return err;
2454022658beSLiam Girdwood 		}
2455022658beSLiam Girdwood 	}
2456022658beSLiam Girdwood 
2457022658beSLiam Girdwood 	return 0;
2458022658beSLiam Girdwood }
2459022658beSLiam Girdwood 
24604fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
24614fefd698SDimitris Papastamos 					       const char *name)
24624fefd698SDimitris Papastamos {
24634fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
24644fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
24654fefd698SDimitris Papastamos 
24664fefd698SDimitris Papastamos 	if (unlikely(!name))
24674fefd698SDimitris Papastamos 		return NULL;
24684fefd698SDimitris Papastamos 
24694fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
24704fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
24714fefd698SDimitris Papastamos 			return kctl;
24724fefd698SDimitris Papastamos 	return NULL;
24734fefd698SDimitris Papastamos }
24744fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
24754fefd698SDimitris Papastamos 
2476db2a4165SFrank Mandarino /**
2477022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2478022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
24793e8e1952SIan Molton  * duplicating this code.
24803e8e1952SIan Molton  *
24813e8e1952SIan Molton  * @codec: codec to add controls to
24823e8e1952SIan Molton  * @controls: array of controls to add
24833e8e1952SIan Molton  * @num_controls: number of elements in the array
24843e8e1952SIan Molton  *
24853e8e1952SIan Molton  * Return 0 for success, else error.
24863e8e1952SIan Molton  */
2487022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
24883e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
24893e8e1952SIan Molton {
2490f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
24913e8e1952SIan Molton 
2492022658beSLiam Girdwood 	return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2493022658beSLiam Girdwood 			codec->name_prefix, codec);
24943e8e1952SIan Molton }
2495022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
24963e8e1952SIan Molton 
24973e8e1952SIan Molton /**
2498a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2499022658beSLiam Girdwood  * Convenience function to add a list of controls.
2500a491a5c8SLiam Girdwood  *
2501a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2502a491a5c8SLiam Girdwood  * @controls: array of controls to add
2503a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2504a491a5c8SLiam Girdwood  *
2505a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2506a491a5c8SLiam Girdwood  */
2507a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2508a491a5c8SLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2509a491a5c8SLiam Girdwood {
2510a491a5c8SLiam Girdwood 	struct snd_card *card = platform->card->snd_card;
2511a491a5c8SLiam Girdwood 
2512022658beSLiam Girdwood 	return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2513022658beSLiam Girdwood 			NULL, platform);
2514a491a5c8SLiam Girdwood }
2515a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2516a491a5c8SLiam Girdwood 
2517a491a5c8SLiam Girdwood /**
2518022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2519022658beSLiam Girdwood  * Convenience function to add a list of controls.
2520022658beSLiam Girdwood  *
2521022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2522022658beSLiam Girdwood  * @controls: array of controls to add
2523022658beSLiam Girdwood  * @num_controls: number of elements in the array
2524022658beSLiam Girdwood  *
2525022658beSLiam Girdwood  * Return 0 for success, else error.
2526022658beSLiam Girdwood  */
2527022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2528022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2529022658beSLiam Girdwood {
2530022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2531022658beSLiam Girdwood 
2532022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2533022658beSLiam Girdwood 			NULL, soc_card);
2534022658beSLiam Girdwood }
2535022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2536022658beSLiam Girdwood 
2537022658beSLiam Girdwood /**
2538022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2539022658beSLiam Girdwood  * Convienience function to add a list of controls.
2540022658beSLiam Girdwood  *
2541022658beSLiam Girdwood  * @dai: DAI to add controls to
2542022658beSLiam Girdwood  * @controls: array of controls to add
2543022658beSLiam Girdwood  * @num_controls: number of elements in the array
2544022658beSLiam Girdwood  *
2545022658beSLiam Girdwood  * Return 0 for success, else error.
2546022658beSLiam Girdwood  */
2547022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2548022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2549022658beSLiam Girdwood {
2550022658beSLiam Girdwood 	struct snd_card *card = dai->card->snd_card;
2551022658beSLiam Girdwood 
2552022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2553022658beSLiam Girdwood 			NULL, dai);
2554022658beSLiam Girdwood }
2555022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2556022658beSLiam Girdwood 
2557022658beSLiam Girdwood /**
2558db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
2559db2a4165SFrank Mandarino  * @kcontrol: mixer control
2560db2a4165SFrank Mandarino  * @uinfo: control element information
2561db2a4165SFrank Mandarino  *
2562db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
2563db2a4165SFrank Mandarino  * mixer control.
2564db2a4165SFrank Mandarino  *
2565db2a4165SFrank Mandarino  * Returns 0 for success.
2566db2a4165SFrank Mandarino  */
2567db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2568db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2569db2a4165SFrank Mandarino {
2570db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2571db2a4165SFrank Mandarino 
2572db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2573db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2574f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
2575db2a4165SFrank Mandarino 
2576f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
2577f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
2578a19685cbSTakashi Iwai 	strlcpy(uinfo->value.enumerated.name,
2579a19685cbSTakashi Iwai 		e->texts[uinfo->value.enumerated.item],
2580a19685cbSTakashi Iwai 		sizeof(uinfo->value.enumerated.name));
2581db2a4165SFrank Mandarino 	return 0;
2582db2a4165SFrank Mandarino }
2583db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2584db2a4165SFrank Mandarino 
2585db2a4165SFrank Mandarino /**
2586db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
2587db2a4165SFrank Mandarino  * @kcontrol: mixer control
2588ac11a2b3SMark Brown  * @ucontrol: control element information
2589db2a4165SFrank Mandarino  *
2590db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
2591db2a4165SFrank Mandarino  *
2592db2a4165SFrank Mandarino  * Returns 0 for success.
2593db2a4165SFrank Mandarino  */
2594db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2595db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2596db2a4165SFrank Mandarino {
2597db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2598db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
259986767b7dSLars-Peter Clausen 	unsigned int val;
2600db2a4165SFrank Mandarino 
2601db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
26023ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
260386767b7dSLars-Peter Clausen 		= (val >> e->shift_l) & e->mask;
2604db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
2605db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
260686767b7dSLars-Peter Clausen 			(val >> e->shift_r) & e->mask;
2607db2a4165SFrank Mandarino 
2608db2a4165SFrank Mandarino 	return 0;
2609db2a4165SFrank Mandarino }
2610db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2611db2a4165SFrank Mandarino 
2612db2a4165SFrank Mandarino /**
2613db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
2614db2a4165SFrank Mandarino  * @kcontrol: mixer control
2615ac11a2b3SMark Brown  * @ucontrol: control element information
2616db2a4165SFrank Mandarino  *
2617db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
2618db2a4165SFrank Mandarino  *
2619db2a4165SFrank Mandarino  * Returns 0 for success.
2620db2a4165SFrank Mandarino  */
2621db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2622db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2623db2a4165SFrank Mandarino {
2624db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
262646f5822fSDaniel Ribeiro 	unsigned int val;
262786767b7dSLars-Peter Clausen 	unsigned int mask;
2628db2a4165SFrank Mandarino 
2629f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2630db2a4165SFrank Mandarino 		return -EINVAL;
2631db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
263286767b7dSLars-Peter Clausen 	mask = e->mask << e->shift_l;
2633db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
2634f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2635db2a4165SFrank Mandarino 			return -EINVAL;
2636db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
263786767b7dSLars-Peter Clausen 		mask |= e->mask << e->shift_r;
2638db2a4165SFrank Mandarino 	}
2639db2a4165SFrank Mandarino 
26406c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2641db2a4165SFrank Mandarino }
2642db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2643db2a4165SFrank Mandarino 
2644db2a4165SFrank Mandarino /**
26452e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
26462e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
26472e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
26482e72f8e3SPeter Ujfalusi  *
26492e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
26502e72f8e3SPeter Ujfalusi  *
26512e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
26522e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
26532e72f8e3SPeter Ujfalusi  *
26542e72f8e3SPeter Ujfalusi  * Returns 0 for success.
26552e72f8e3SPeter Ujfalusi  */
26562e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
26572e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
26582e72f8e3SPeter Ujfalusi {
26592e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
266074155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
266146f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
26622e72f8e3SPeter Ujfalusi 
26632e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
26642e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
26652e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
26662e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
26672e72f8e3SPeter Ujfalusi 			break;
26682e72f8e3SPeter Ujfalusi 	}
26692e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
26702e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
26712e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
26722e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
26732e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
26742e72f8e3SPeter Ujfalusi 				break;
26752e72f8e3SPeter Ujfalusi 		}
26762e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
26772e72f8e3SPeter Ujfalusi 	}
26782e72f8e3SPeter Ujfalusi 
26792e72f8e3SPeter Ujfalusi 	return 0;
26802e72f8e3SPeter Ujfalusi }
26812e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
26822e72f8e3SPeter Ujfalusi 
26832e72f8e3SPeter Ujfalusi /**
26842e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
26852e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
26862e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
26872e72f8e3SPeter Ujfalusi  *
26882e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
26892e72f8e3SPeter Ujfalusi  *
26902e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
26912e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
26922e72f8e3SPeter Ujfalusi  *
26932e72f8e3SPeter Ujfalusi  * Returns 0 for success.
26942e72f8e3SPeter Ujfalusi  */
26952e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
26962e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
26972e72f8e3SPeter Ujfalusi {
26982e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
269974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
270046f5822fSDaniel Ribeiro 	unsigned int val;
270146f5822fSDaniel Ribeiro 	unsigned int mask;
27022e72f8e3SPeter Ujfalusi 
27032e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
27042e72f8e3SPeter Ujfalusi 		return -EINVAL;
27052e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
27062e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
27072e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
27082e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
27092e72f8e3SPeter Ujfalusi 			return -EINVAL;
27102e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
27112e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
27122e72f8e3SPeter Ujfalusi 	}
27132e72f8e3SPeter Ujfalusi 
27146c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
27152e72f8e3SPeter Ujfalusi }
27162e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
27172e72f8e3SPeter Ujfalusi 
27182e72f8e3SPeter Ujfalusi /**
2719db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2720db2a4165SFrank Mandarino  * @kcontrol: mixer control
2721db2a4165SFrank Mandarino  * @uinfo: control element information
2722db2a4165SFrank Mandarino  *
2723e8f5a103SPeter Ujfalusi  * Callback to provide information about a single mixer control, or a double
2724e8f5a103SPeter Ujfalusi  * mixer control that spans 2 registers.
2725db2a4165SFrank Mandarino  *
2726db2a4165SFrank Mandarino  * Returns 0 for success.
2727db2a4165SFrank Mandarino  */
2728db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2729db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2730db2a4165SFrank Mandarino {
27314eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27324eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2733d11bb4a9SPeter Ujfalusi 	int platform_max;
2734db2a4165SFrank Mandarino 
2735d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2736d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2737d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2738d11bb4a9SPeter Ujfalusi 
2739d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2740a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2741a7a4ac86SPhilipp Zabel 	else
2742a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2743a7a4ac86SPhilipp Zabel 
2744e8f5a103SPeter Ujfalusi 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2745db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2746d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2747db2a4165SFrank Mandarino 	return 0;
2748db2a4165SFrank Mandarino }
2749db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2750db2a4165SFrank Mandarino 
2751db2a4165SFrank Mandarino /**
2752db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2753db2a4165SFrank Mandarino  * @kcontrol: mixer control
2754ac11a2b3SMark Brown  * @ucontrol: control element information
2755db2a4165SFrank Mandarino  *
2756f7915d99SPeter Ujfalusi  * Callback to get the value of a single mixer control, or a double mixer
2757f7915d99SPeter Ujfalusi  * control that spans 2 registers.
2758db2a4165SFrank Mandarino  *
2759db2a4165SFrank Mandarino  * Returns 0 for success.
2760db2a4165SFrank Mandarino  */
2761db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2762db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2763db2a4165SFrank Mandarino {
27644eaa9819SJon Smirl 	struct soc_mixer_control *mc =
27654eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2766db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2767815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2768f7915d99SPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2769815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2770815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
27714eaa9819SJon Smirl 	int max = mc->max;
2772815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2773815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2774db2a4165SFrank Mandarino 
2775db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2776db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2777f7915d99SPeter Ujfalusi 	if (invert)
2778db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2779a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2780f7915d99SPeter Ujfalusi 
2781f7915d99SPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2782f7915d99SPeter Ujfalusi 		if (reg == reg2)
2783f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2784f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg) >> rshift) & mask;
2785f7915d99SPeter Ujfalusi 		else
2786f7915d99SPeter Ujfalusi 			ucontrol->value.integer.value[1] =
2787f7915d99SPeter Ujfalusi 				(snd_soc_read(codec, reg2) >> shift) & mask;
2788f7915d99SPeter Ujfalusi 		if (invert)
2789db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2790a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2791db2a4165SFrank Mandarino 	}
2792db2a4165SFrank Mandarino 
2793db2a4165SFrank Mandarino 	return 0;
2794db2a4165SFrank Mandarino }
2795db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2796db2a4165SFrank Mandarino 
2797db2a4165SFrank Mandarino /**
2798db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2799db2a4165SFrank Mandarino  * @kcontrol: mixer control
2800ac11a2b3SMark Brown  * @ucontrol: control element information
2801db2a4165SFrank Mandarino  *
2802974815baSPeter Ujfalusi  * Callback to set the value of a single mixer control, or a double mixer
2803974815baSPeter Ujfalusi  * control that spans 2 registers.
2804db2a4165SFrank Mandarino  *
2805db2a4165SFrank Mandarino  * Returns 0 for success.
2806db2a4165SFrank Mandarino  */
2807db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2808db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2809db2a4165SFrank Mandarino {
28104eaa9819SJon Smirl 	struct soc_mixer_control *mc =
28114eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2812db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2813815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2814974815baSPeter Ujfalusi 	unsigned int reg2 = mc->rreg;
2815815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2816815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
28174eaa9819SJon Smirl 	int max = mc->max;
2818815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2819815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2820974815baSPeter Ujfalusi 	int err;
2821939d9f16SNenghua Cao 	bool type_2r = false;
2822974815baSPeter Ujfalusi 	unsigned int val2 = 0;
2823974815baSPeter Ujfalusi 	unsigned int val, val_mask;
2824db2a4165SFrank Mandarino 
2825db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2826db2a4165SFrank Mandarino 	if (invert)
2827a7a4ac86SPhilipp Zabel 		val = max - val;
2828db2a4165SFrank Mandarino 	val_mask = mask << shift;
2829db2a4165SFrank Mandarino 	val = val << shift;
2830974815baSPeter Ujfalusi 	if (snd_soc_volsw_is_stereo(mc)) {
2831db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2832db2a4165SFrank Mandarino 		if (invert)
2833a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2834974815baSPeter Ujfalusi 		if (reg == reg2) {
2835db2a4165SFrank Mandarino 			val_mask |= mask << rshift;
2836db2a4165SFrank Mandarino 			val |= val2 << rshift;
2837974815baSPeter Ujfalusi 		} else {
2838db2a4165SFrank Mandarino 			val2 = val2 << shift;
2839939d9f16SNenghua Cao 			type_2r = true;
2840974815baSPeter Ujfalusi 		}
2841974815baSPeter Ujfalusi 	}
28426c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
28433ff3f64bSMark Brown 	if (err < 0)
2844db2a4165SFrank Mandarino 		return err;
2845db2a4165SFrank Mandarino 
2846974815baSPeter Ujfalusi 	if (type_2r)
28476c508c62SEero Nurkkala 		err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2848974815baSPeter Ujfalusi 
2849db2a4165SFrank Mandarino 	return err;
2850db2a4165SFrank Mandarino }
2851974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2852db2a4165SFrank Mandarino 
2853e13ac2e9SMark Brown /**
28541d99f243SBrian Austin  * snd_soc_get_volsw_sx - single mixer get callback
28551d99f243SBrian Austin  * @kcontrol: mixer control
28561d99f243SBrian Austin  * @ucontrol: control element information
28571d99f243SBrian Austin  *
28581d99f243SBrian Austin  * Callback to get the value of a single mixer control, or a double mixer
28591d99f243SBrian Austin  * control that spans 2 registers.
28601d99f243SBrian Austin  *
28611d99f243SBrian Austin  * Returns 0 for success.
28621d99f243SBrian Austin  */
28631d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
28641d99f243SBrian Austin 		      struct snd_ctl_elem_value *ucontrol)
28651d99f243SBrian Austin {
28661d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
28671d99f243SBrian Austin 	struct soc_mixer_control *mc =
28681d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
28691d99f243SBrian Austin 
28701d99f243SBrian Austin 	unsigned int reg = mc->reg;
28711d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
28721d99f243SBrian Austin 	unsigned int shift = mc->shift;
28731d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
28741d99f243SBrian Austin 	int max = mc->max;
28751d99f243SBrian Austin 	int min = mc->min;
28761d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
28771d99f243SBrian Austin 
28781d99f243SBrian Austin 	ucontrol->value.integer.value[0] =
28791d99f243SBrian Austin 	    ((snd_soc_read(codec, reg) >> shift) - min) & mask;
28801d99f243SBrian Austin 
28811d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc))
28821d99f243SBrian Austin 		ucontrol->value.integer.value[1] =
28831d99f243SBrian Austin 			((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
28841d99f243SBrian Austin 
28851d99f243SBrian Austin 	return 0;
28861d99f243SBrian Austin }
28871d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
28881d99f243SBrian Austin 
28891d99f243SBrian Austin /**
28901d99f243SBrian Austin  * snd_soc_put_volsw_sx - double mixer set callback
28911d99f243SBrian Austin  * @kcontrol: mixer control
28921d99f243SBrian Austin  * @uinfo: control element information
28931d99f243SBrian Austin  *
28941d99f243SBrian Austin  * Callback to set the value of a double mixer control that spans 2 registers.
28951d99f243SBrian Austin  *
28961d99f243SBrian Austin  * Returns 0 for success.
28971d99f243SBrian Austin  */
28981d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
28991d99f243SBrian Austin 			 struct snd_ctl_elem_value *ucontrol)
29001d99f243SBrian Austin {
29011d99f243SBrian Austin 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
29021d99f243SBrian Austin 	struct soc_mixer_control *mc =
29031d99f243SBrian Austin 	    (struct soc_mixer_control *)kcontrol->private_value;
29041d99f243SBrian Austin 
29051d99f243SBrian Austin 	unsigned int reg = mc->reg;
29061d99f243SBrian Austin 	unsigned int reg2 = mc->rreg;
29071d99f243SBrian Austin 	unsigned int shift = mc->shift;
29081d99f243SBrian Austin 	unsigned int rshift = mc->rshift;
29091d99f243SBrian Austin 	int max = mc->max;
29101d99f243SBrian Austin 	int min = mc->min;
29111d99f243SBrian Austin 	int mask = (1 << (fls(min + max) - 1)) - 1;
291227f1d759SBrian Austin 	int err = 0;
29131d99f243SBrian Austin 	unsigned short val, val_mask, val2 = 0;
29141d99f243SBrian Austin 
29151d99f243SBrian Austin 	val_mask = mask << shift;
29161d99f243SBrian Austin 	val = (ucontrol->value.integer.value[0] + min) & mask;
29171d99f243SBrian Austin 	val = val << shift;
29181d99f243SBrian Austin 
2919d055852eSMukund Navada 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2920d055852eSMukund Navada 	if (err < 0)
29211d99f243SBrian Austin 		return err;
29221d99f243SBrian Austin 
29231d99f243SBrian Austin 	if (snd_soc_volsw_is_stereo(mc)) {
29241d99f243SBrian Austin 		val_mask = mask << rshift;
29251d99f243SBrian Austin 		val2 = (ucontrol->value.integer.value[1] + min) & mask;
29261d99f243SBrian Austin 		val2 = val2 << rshift;
29271d99f243SBrian Austin 
29281d99f243SBrian Austin 		if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
29291d99f243SBrian Austin 			return err;
29301d99f243SBrian Austin 	}
29311d99f243SBrian Austin 	return 0;
29321d99f243SBrian Austin }
29331d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
29341d99f243SBrian Austin 
29351d99f243SBrian Austin /**
2936e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2937e13ac2e9SMark Brown  * @kcontrol: mixer control
2938e13ac2e9SMark Brown  * @uinfo: control element information
2939e13ac2e9SMark Brown  *
2940e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2941e13ac2e9SMark Brown  *
2942e13ac2e9SMark Brown  * Returns 0 for success.
2943e13ac2e9SMark Brown  */
2944e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2945e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2946e13ac2e9SMark Brown {
29474eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29484eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2949d11bb4a9SPeter Ujfalusi 	int platform_max;
29504eaa9819SJon Smirl 	int min = mc->min;
2951e13ac2e9SMark Brown 
2952d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2953d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2954d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2955d11bb4a9SPeter Ujfalusi 
2956e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2957e13ac2e9SMark Brown 	uinfo->count = 2;
2958e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2959d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2960e13ac2e9SMark Brown 	return 0;
2961e13ac2e9SMark Brown }
2962e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2963e13ac2e9SMark Brown 
2964e13ac2e9SMark Brown /**
2965e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2966e13ac2e9SMark Brown  * @kcontrol: mixer control
2967ac11a2b3SMark Brown  * @ucontrol: control element information
2968e13ac2e9SMark Brown  *
2969e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2970e13ac2e9SMark Brown  *
2971e13ac2e9SMark Brown  * Returns 0 for success.
2972e13ac2e9SMark Brown  */
2973e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2974e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2975e13ac2e9SMark Brown {
29764eaa9819SJon Smirl 	struct soc_mixer_control *mc =
29774eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2978e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2979815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
29804eaa9819SJon Smirl 	int min = mc->min;
2981e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2982e13ac2e9SMark Brown 
2983e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2984e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2985e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2986e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2987e13ac2e9SMark Brown 	return 0;
2988e13ac2e9SMark Brown }
2989e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2990e13ac2e9SMark Brown 
2991e13ac2e9SMark Brown /**
2992e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2993e13ac2e9SMark Brown  * @kcontrol: mixer control
2994ac11a2b3SMark Brown  * @ucontrol: control element information
2995e13ac2e9SMark Brown  *
2996e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2997e13ac2e9SMark Brown  *
2998e13ac2e9SMark Brown  * Returns 0 for success.
2999e13ac2e9SMark Brown  */
3000e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
3001e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
3002e13ac2e9SMark Brown {
30034eaa9819SJon Smirl 	struct soc_mixer_control *mc =
30044eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
3005e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3006815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
30074eaa9819SJon Smirl 	int min = mc->min;
300846f5822fSDaniel Ribeiro 	unsigned int val;
3009e13ac2e9SMark Brown 
3010e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
3011e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
3012e13ac2e9SMark Brown 
30136c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
3014e13ac2e9SMark Brown }
3015e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3016e13ac2e9SMark Brown 
30178c6529dbSLiam Girdwood /**
30186c9d8cf6SAdam Thomson  * snd_soc_info_volsw_range - single mixer info callback with range.
30196c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30206c9d8cf6SAdam Thomson  * @uinfo: control element information
30216c9d8cf6SAdam Thomson  *
30226c9d8cf6SAdam Thomson  * Callback to provide information, within a range, about a single
30236c9d8cf6SAdam Thomson  * mixer control.
30246c9d8cf6SAdam Thomson  *
30256c9d8cf6SAdam Thomson  * returns 0 for success.
30266c9d8cf6SAdam Thomson  */
30276c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
30286c9d8cf6SAdam Thomson 	struct snd_ctl_elem_info *uinfo)
30296c9d8cf6SAdam Thomson {
30306c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30316c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
30326c9d8cf6SAdam Thomson 	int platform_max;
30336c9d8cf6SAdam Thomson 	int min = mc->min;
30346c9d8cf6SAdam Thomson 
30356c9d8cf6SAdam Thomson 	if (!mc->platform_max)
30366c9d8cf6SAdam Thomson 		mc->platform_max = mc->max;
30376c9d8cf6SAdam Thomson 	platform_max = mc->platform_max;
30386c9d8cf6SAdam Thomson 
30396c9d8cf6SAdam Thomson 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
30409bde4f0bSMark Brown 	uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
30416c9d8cf6SAdam Thomson 	uinfo->value.integer.min = 0;
30426c9d8cf6SAdam Thomson 	uinfo->value.integer.max = platform_max - min;
30436c9d8cf6SAdam Thomson 
30446c9d8cf6SAdam Thomson 	return 0;
30456c9d8cf6SAdam Thomson }
30466c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
30476c9d8cf6SAdam Thomson 
30486c9d8cf6SAdam Thomson /**
30496c9d8cf6SAdam Thomson  * snd_soc_put_volsw_range - single mixer put value callback with range.
30506c9d8cf6SAdam Thomson  * @kcontrol: mixer control
30516c9d8cf6SAdam Thomson  * @ucontrol: control element information
30526c9d8cf6SAdam Thomson  *
30536c9d8cf6SAdam Thomson  * Callback to set the value, within a range, for a single mixer control.
30546c9d8cf6SAdam Thomson  *
30556c9d8cf6SAdam Thomson  * Returns 0 for success.
30566c9d8cf6SAdam Thomson  */
30576c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
30586c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
30596c9d8cf6SAdam Thomson {
30606c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
30616c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
30626c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
30636c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
30649bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
30656c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
30666c9d8cf6SAdam Thomson 	int min = mc->min;
30676c9d8cf6SAdam Thomson 	int max = mc->max;
30686c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
30696c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
30706c9d8cf6SAdam Thomson 	unsigned int val, val_mask;
30719bde4f0bSMark Brown 	int ret;
30726c9d8cf6SAdam Thomson 
30736c9d8cf6SAdam Thomson 	val = ((ucontrol->value.integer.value[0] + min) & mask);
30746c9d8cf6SAdam Thomson 	if (invert)
30756c9d8cf6SAdam Thomson 		val = max - val;
30766c9d8cf6SAdam Thomson 	val_mask = mask << shift;
30776c9d8cf6SAdam Thomson 	val = val << shift;
30786c9d8cf6SAdam Thomson 
30799bde4f0bSMark Brown 	ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
30800eaa6ccaSJoonyoung Shim 	if (ret < 0)
30819bde4f0bSMark Brown 		return ret;
30829bde4f0bSMark Brown 
30839bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
30849bde4f0bSMark Brown 		val = ((ucontrol->value.integer.value[1] + min) & mask);
30859bde4f0bSMark Brown 		if (invert)
30869bde4f0bSMark Brown 			val = max - val;
30879bde4f0bSMark Brown 		val_mask = mask << shift;
30889bde4f0bSMark Brown 		val = val << shift;
30899bde4f0bSMark Brown 
30909bde4f0bSMark Brown 		ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
30919bde4f0bSMark Brown 	}
30929bde4f0bSMark Brown 
30939bde4f0bSMark Brown 	return ret;
30946c9d8cf6SAdam Thomson }
30956c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
30966c9d8cf6SAdam Thomson 
30976c9d8cf6SAdam Thomson /**
30986c9d8cf6SAdam Thomson  * snd_soc_get_volsw_range - single mixer get callback with range
30996c9d8cf6SAdam Thomson  * @kcontrol: mixer control
31006c9d8cf6SAdam Thomson  * @ucontrol: control element information
31016c9d8cf6SAdam Thomson  *
31026c9d8cf6SAdam Thomson  * Callback to get the value, within a range, of a single mixer control.
31036c9d8cf6SAdam Thomson  *
31046c9d8cf6SAdam Thomson  * Returns 0 for success.
31056c9d8cf6SAdam Thomson  */
31066c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
31076c9d8cf6SAdam Thomson 	struct snd_ctl_elem_value *ucontrol)
31086c9d8cf6SAdam Thomson {
31096c9d8cf6SAdam Thomson 	struct soc_mixer_control *mc =
31106c9d8cf6SAdam Thomson 		(struct soc_mixer_control *)kcontrol->private_value;
31116c9d8cf6SAdam Thomson 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
31126c9d8cf6SAdam Thomson 	unsigned int reg = mc->reg;
31139bde4f0bSMark Brown 	unsigned int rreg = mc->rreg;
31146c9d8cf6SAdam Thomson 	unsigned int shift = mc->shift;
31156c9d8cf6SAdam Thomson 	int min = mc->min;
31166c9d8cf6SAdam Thomson 	int max = mc->max;
31176c9d8cf6SAdam Thomson 	unsigned int mask = (1 << fls(max)) - 1;
31186c9d8cf6SAdam Thomson 	unsigned int invert = mc->invert;
31196c9d8cf6SAdam Thomson 
31206c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
31216c9d8cf6SAdam Thomson 		(snd_soc_read(codec, reg) >> shift) & mask;
31226c9d8cf6SAdam Thomson 	if (invert)
31236c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] =
31246c9d8cf6SAdam Thomson 			max - ucontrol->value.integer.value[0];
31256c9d8cf6SAdam Thomson 	ucontrol->value.integer.value[0] =
31266c9d8cf6SAdam Thomson 		ucontrol->value.integer.value[0] - min;
31276c9d8cf6SAdam Thomson 
31289bde4f0bSMark Brown 	if (snd_soc_volsw_is_stereo(mc)) {
31299bde4f0bSMark Brown 		ucontrol->value.integer.value[1] =
31309bde4f0bSMark Brown 			(snd_soc_read(codec, rreg) >> shift) & mask;
31319bde4f0bSMark Brown 		if (invert)
31329bde4f0bSMark Brown 			ucontrol->value.integer.value[1] =
31339bde4f0bSMark Brown 				max - ucontrol->value.integer.value[1];
31349bde4f0bSMark Brown 		ucontrol->value.integer.value[1] =
31359bde4f0bSMark Brown 			ucontrol->value.integer.value[1] - min;
31369bde4f0bSMark Brown 	}
31379bde4f0bSMark Brown 
31386c9d8cf6SAdam Thomson 	return 0;
31396c9d8cf6SAdam Thomson }
31406c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
31416c9d8cf6SAdam Thomson 
31426c9d8cf6SAdam Thomson /**
3143637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
3144637d3847SPeter Ujfalusi  *
3145637d3847SPeter Ujfalusi  * @codec: where to look for the control
3146637d3847SPeter Ujfalusi  * @name: Name of the control
3147637d3847SPeter Ujfalusi  * @max: new maximum limit
3148637d3847SPeter Ujfalusi  *
3149637d3847SPeter Ujfalusi  * Return 0 for success, else error.
3150637d3847SPeter Ujfalusi  */
3151637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
3152637d3847SPeter Ujfalusi 	const char *name, int max)
3153637d3847SPeter Ujfalusi {
3154f0fba2adSLiam Girdwood 	struct snd_card *card = codec->card->snd_card;
3155637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
3156637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
3157637d3847SPeter Ujfalusi 	int found = 0;
3158637d3847SPeter Ujfalusi 	int ret = -EINVAL;
3159637d3847SPeter Ujfalusi 
3160637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
3161637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
3162637d3847SPeter Ujfalusi 		return -EINVAL;
3163637d3847SPeter Ujfalusi 
3164637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
3165637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3166637d3847SPeter Ujfalusi 			found = 1;
3167637d3847SPeter Ujfalusi 			break;
3168637d3847SPeter Ujfalusi 		}
3169637d3847SPeter Ujfalusi 	}
3170637d3847SPeter Ujfalusi 	if (found) {
3171637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
3172637d3847SPeter Ujfalusi 		if (max <= mc->max) {
3173d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
3174637d3847SPeter Ujfalusi 			ret = 0;
3175637d3847SPeter Ujfalusi 		}
3176637d3847SPeter Ujfalusi 	}
3177637d3847SPeter Ujfalusi 	return ret;
3178637d3847SPeter Ujfalusi }
3179637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3180637d3847SPeter Ujfalusi 
318171d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
318271d08516SMark Brown 		       struct snd_ctl_elem_info *uinfo)
318371d08516SMark Brown {
318471d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
318571d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
318671d08516SMark Brown 
318771d08516SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
318871d08516SMark Brown 	uinfo->count = params->num_regs * codec->val_bytes;
318971d08516SMark Brown 
319071d08516SMark Brown 	return 0;
319171d08516SMark Brown }
319271d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
319371d08516SMark Brown 
319471d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
319571d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
319671d08516SMark Brown {
319771d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
319871d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
319971d08516SMark Brown 	int ret;
320071d08516SMark Brown 
320171d08516SMark Brown 	if (codec->using_regmap)
320271d08516SMark Brown 		ret = regmap_raw_read(codec->control_data, params->base,
320371d08516SMark Brown 				      ucontrol->value.bytes.data,
320471d08516SMark Brown 				      params->num_regs * codec->val_bytes);
320571d08516SMark Brown 	else
320671d08516SMark Brown 		ret = -EINVAL;
320771d08516SMark Brown 
3208f831b055SMark Brown 	/* Hide any masked bytes to ensure consistent data reporting */
3209f831b055SMark Brown 	if (ret == 0 && params->mask) {
3210f831b055SMark Brown 		switch (codec->val_bytes) {
3211f831b055SMark Brown 		case 1:
3212f831b055SMark Brown 			ucontrol->value.bytes.data[0] &= ~params->mask;
3213f831b055SMark Brown 			break;
3214f831b055SMark Brown 		case 2:
3215f831b055SMark Brown 			((u16 *)(&ucontrol->value.bytes.data))[0]
32168f1ec93aSCharles Keepax 				&= cpu_to_be16(~params->mask);
3217f831b055SMark Brown 			break;
3218f831b055SMark Brown 		case 4:
3219f831b055SMark Brown 			((u32 *)(&ucontrol->value.bytes.data))[0]
32208f1ec93aSCharles Keepax 				&= cpu_to_be32(~params->mask);
3221f831b055SMark Brown 			break;
3222f831b055SMark Brown 		default:
3223f831b055SMark Brown 			return -EINVAL;
3224f831b055SMark Brown 		}
3225f831b055SMark Brown 	}
3226f831b055SMark Brown 
322771d08516SMark Brown 	return ret;
322871d08516SMark Brown }
322971d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
323071d08516SMark Brown 
323171d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
323271d08516SMark Brown 		      struct snd_ctl_elem_value *ucontrol)
323371d08516SMark Brown {
323471d08516SMark Brown 	struct soc_bytes *params = (void *)kcontrol->private_value;
323571d08516SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3236f831b055SMark Brown 	int ret, len;
3237*a1a564edSNenghua Cao 	unsigned int val, mask;
3238f831b055SMark Brown 	void *data;
323971d08516SMark Brown 
3240f831b055SMark Brown 	if (!codec->using_regmap)
3241f831b055SMark Brown 		return -EINVAL;
3242f831b055SMark Brown 
3243f831b055SMark Brown 	len = params->num_regs * codec->val_bytes;
3244f831b055SMark Brown 
3245b5a8fe43SMark Brown 	data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3246b5a8fe43SMark Brown 	if (!data)
3247b5a8fe43SMark Brown 		return -ENOMEM;
3248b5a8fe43SMark Brown 
3249f831b055SMark Brown 	/*
3250f831b055SMark Brown 	 * If we've got a mask then we need to preserve the register
3251f831b055SMark Brown 	 * bits.  We shouldn't modify the incoming data so take a
3252f831b055SMark Brown 	 * copy.
3253f831b055SMark Brown 	 */
3254f831b055SMark Brown 	if (params->mask) {
3255f831b055SMark Brown 		ret = regmap_read(codec->control_data, params->base, &val);
3256f831b055SMark Brown 		if (ret != 0)
3257e8b18addSWei Yongjun 			goto out;
3258f831b055SMark Brown 
3259f831b055SMark Brown 		val &= params->mask;
3260f831b055SMark Brown 
3261f831b055SMark Brown 		switch (codec->val_bytes) {
3262f831b055SMark Brown 		case 1:
3263f831b055SMark Brown 			((u8 *)data)[0] &= ~params->mask;
3264f831b055SMark Brown 			((u8 *)data)[0] |= val;
3265f831b055SMark Brown 			break;
3266f831b055SMark Brown 		case 2:
3267*a1a564edSNenghua Cao 			mask = ~params->mask;
3268*a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3269*a1a564edSNenghua Cao 							&mask, &mask);
3270*a1a564edSNenghua Cao 			if (ret != 0)
3271*a1a564edSNenghua Cao 				goto out;
3272*a1a564edSNenghua Cao 
3273*a1a564edSNenghua Cao 			((u16 *)data)[0] &= mask;
3274*a1a564edSNenghua Cao 
3275*a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3276*a1a564edSNenghua Cao 							&val, &val);
3277*a1a564edSNenghua Cao 			if (ret != 0)
3278*a1a564edSNenghua Cao 				goto out;
3279*a1a564edSNenghua Cao 
3280*a1a564edSNenghua Cao 			((u16 *)data)[0] |= val;
3281f831b055SMark Brown 			break;
3282f831b055SMark Brown 		case 4:
3283*a1a564edSNenghua Cao 			mask = ~params->mask;
3284*a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3285*a1a564edSNenghua Cao 							&mask, &mask);
3286*a1a564edSNenghua Cao 			if (ret != 0)
3287*a1a564edSNenghua Cao 				goto out;
3288*a1a564edSNenghua Cao 
3289*a1a564edSNenghua Cao 			((u32 *)data)[0] &= mask;
3290*a1a564edSNenghua Cao 
3291*a1a564edSNenghua Cao 			ret = regmap_parse_val(codec->control_data,
3292*a1a564edSNenghua Cao 							&val, &val);
3293*a1a564edSNenghua Cao 			if (ret != 0)
3294*a1a564edSNenghua Cao 				goto out;
3295*a1a564edSNenghua Cao 
3296*a1a564edSNenghua Cao 			((u32 *)data)[0] |= val;
3297f831b055SMark Brown 			break;
3298f831b055SMark Brown 		default:
3299e8b18addSWei Yongjun 			ret = -EINVAL;
3300e8b18addSWei Yongjun 			goto out;
3301f831b055SMark Brown 		}
3302f831b055SMark Brown 	}
3303f831b055SMark Brown 
330471d08516SMark Brown 	ret = regmap_raw_write(codec->control_data, params->base,
3305f831b055SMark Brown 			       data, len);
3306f831b055SMark Brown 
3307e8b18addSWei Yongjun out:
3308f831b055SMark Brown 	kfree(data);
330971d08516SMark Brown 
331071d08516SMark Brown 	return ret;
331171d08516SMark Brown }
331271d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
331371d08516SMark Brown 
3314b6f4bb38Sapatard@mandriva.com /**
33154183eed2SKristoffer KARLSSON  * snd_soc_info_xr_sx - signed multi register info callback
33164183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33174183eed2SKristoffer KARLSSON  * @uinfo: control element information
33184183eed2SKristoffer KARLSSON  *
33194183eed2SKristoffer KARLSSON  * Callback to provide information of a control that can
33204183eed2SKristoffer KARLSSON  * span multiple codec registers which together
33214183eed2SKristoffer KARLSSON  * forms a single signed value in a MSB/LSB manner.
33224183eed2SKristoffer KARLSSON  *
33234183eed2SKristoffer KARLSSON  * Returns 0 for success.
33244183eed2SKristoffer KARLSSON  */
33254183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
33264183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_info *uinfo)
33274183eed2SKristoffer KARLSSON {
33284183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33294183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33304183eed2SKristoffer KARLSSON 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
33314183eed2SKristoffer KARLSSON 	uinfo->count = 1;
33324183eed2SKristoffer KARLSSON 	uinfo->value.integer.min = mc->min;
33334183eed2SKristoffer KARLSSON 	uinfo->value.integer.max = mc->max;
33344183eed2SKristoffer KARLSSON 
33354183eed2SKristoffer KARLSSON 	return 0;
33364183eed2SKristoffer KARLSSON }
33374183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
33384183eed2SKristoffer KARLSSON 
33394183eed2SKristoffer KARLSSON /**
33404183eed2SKristoffer KARLSSON  * snd_soc_get_xr_sx - signed multi register get callback
33414183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33424183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33434183eed2SKristoffer KARLSSON  *
33444183eed2SKristoffer KARLSSON  * Callback to get the value of a control that can span
33454183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33464183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33474183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33484183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33494183eed2SKristoffer KARLSSON  *
33504183eed2SKristoffer KARLSSON  * Returns 0 for success.
33514183eed2SKristoffer KARLSSON  */
33524183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
33534183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
33544183eed2SKristoffer KARLSSON {
33554183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
33564183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
33574183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
33584183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
33594183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
33604183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
33614183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
33624183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
33634183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
33644183eed2SKristoffer KARLSSON 	long min = mc->min;
33654183eed2SKristoffer KARLSSON 	long max = mc->max;
33664183eed2SKristoffer KARLSSON 	long val = 0;
33674183eed2SKristoffer KARLSSON 	unsigned long regval;
33684183eed2SKristoffer KARLSSON 	unsigned int i;
33694183eed2SKristoffer KARLSSON 
33704183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
33714183eed2SKristoffer KARLSSON 		regval = snd_soc_read(codec, regbase+i) & regwmask;
33724183eed2SKristoffer KARLSSON 		val |= regval << (regwshift*(regcount-i-1));
33734183eed2SKristoffer KARLSSON 	}
33744183eed2SKristoffer KARLSSON 	val &= mask;
33754183eed2SKristoffer KARLSSON 	if (min < 0 && val > max)
33764183eed2SKristoffer KARLSSON 		val |= ~mask;
33774183eed2SKristoffer KARLSSON 	if (invert)
33784183eed2SKristoffer KARLSSON 		val = max - val;
33794183eed2SKristoffer KARLSSON 	ucontrol->value.integer.value[0] = val;
33804183eed2SKristoffer KARLSSON 
33814183eed2SKristoffer KARLSSON 	return 0;
33824183eed2SKristoffer KARLSSON }
33834183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
33844183eed2SKristoffer KARLSSON 
33854183eed2SKristoffer KARLSSON /**
33864183eed2SKristoffer KARLSSON  * snd_soc_put_xr_sx - signed multi register get callback
33874183eed2SKristoffer KARLSSON  * @kcontrol: mreg control
33884183eed2SKristoffer KARLSSON  * @ucontrol: control element information
33894183eed2SKristoffer KARLSSON  *
33904183eed2SKristoffer KARLSSON  * Callback to set the value of a control that can span
33914183eed2SKristoffer KARLSSON  * multiple codec registers which together forms a single
33924183eed2SKristoffer KARLSSON  * signed value in a MSB/LSB manner. The control supports
33934183eed2SKristoffer KARLSSON  * specifying total no of bits used to allow for bitfields
33944183eed2SKristoffer KARLSSON  * across the multiple codec registers.
33954183eed2SKristoffer KARLSSON  *
33964183eed2SKristoffer KARLSSON  * Returns 0 for success.
33974183eed2SKristoffer KARLSSON  */
33984183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
33994183eed2SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
34004183eed2SKristoffer KARLSSON {
34014183eed2SKristoffer KARLSSON 	struct soc_mreg_control *mc =
34024183eed2SKristoffer KARLSSON 		(struct soc_mreg_control *)kcontrol->private_value;
34034183eed2SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
34044183eed2SKristoffer KARLSSON 	unsigned int regbase = mc->regbase;
34054183eed2SKristoffer KARLSSON 	unsigned int regcount = mc->regcount;
34064183eed2SKristoffer KARLSSON 	unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
34074183eed2SKristoffer KARLSSON 	unsigned int regwmask = (1<<regwshift)-1;
34084183eed2SKristoffer KARLSSON 	unsigned int invert = mc->invert;
34094183eed2SKristoffer KARLSSON 	unsigned long mask = (1UL<<mc->nbits)-1;
34104183eed2SKristoffer KARLSSON 	long max = mc->max;
34114183eed2SKristoffer KARLSSON 	long val = ucontrol->value.integer.value[0];
34124183eed2SKristoffer KARLSSON 	unsigned int i, regval, regmask;
34134183eed2SKristoffer KARLSSON 	int err;
34144183eed2SKristoffer KARLSSON 
34154183eed2SKristoffer KARLSSON 	if (invert)
34164183eed2SKristoffer KARLSSON 		val = max - val;
34174183eed2SKristoffer KARLSSON 	val &= mask;
34184183eed2SKristoffer KARLSSON 	for (i = 0; i < regcount; i++) {
34194183eed2SKristoffer KARLSSON 		regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
34204183eed2SKristoffer KARLSSON 		regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
34214183eed2SKristoffer KARLSSON 		err = snd_soc_update_bits_locked(codec, regbase+i,
34224183eed2SKristoffer KARLSSON 				regmask, regval);
34234183eed2SKristoffer KARLSSON 		if (err < 0)
34244183eed2SKristoffer KARLSSON 			return err;
34254183eed2SKristoffer KARLSSON 	}
34264183eed2SKristoffer KARLSSON 
34274183eed2SKristoffer KARLSSON 	return 0;
34284183eed2SKristoffer KARLSSON }
34294183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
34304183eed2SKristoffer KARLSSON 
34314183eed2SKristoffer KARLSSON /**
3432dd7b10b3SKristoffer KARLSSON  * snd_soc_get_strobe - strobe get callback
3433dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3434dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3435dd7b10b3SKristoffer KARLSSON  *
3436dd7b10b3SKristoffer KARLSSON  * Callback get the value of a strobe mixer control.
3437dd7b10b3SKristoffer KARLSSON  *
3438dd7b10b3SKristoffer KARLSSON  * Returns 0 for success.
3439dd7b10b3SKristoffer KARLSSON  */
3440dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3441dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3442dd7b10b3SKristoffer KARLSSON {
3443dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3444dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3445dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3446dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3447dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3448dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3449dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3450dd7b10b3SKristoffer KARLSSON 	unsigned int val = snd_soc_read(codec, reg) & mask;
3451dd7b10b3SKristoffer KARLSSON 
3452dd7b10b3SKristoffer KARLSSON 	if (shift != 0 && val != 0)
3453dd7b10b3SKristoffer KARLSSON 		val = val >> shift;
3454dd7b10b3SKristoffer KARLSSON 	ucontrol->value.enumerated.item[0] = val ^ invert;
3455dd7b10b3SKristoffer KARLSSON 
3456dd7b10b3SKristoffer KARLSSON 	return 0;
3457dd7b10b3SKristoffer KARLSSON }
3458dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3459dd7b10b3SKristoffer KARLSSON 
3460dd7b10b3SKristoffer KARLSSON /**
3461dd7b10b3SKristoffer KARLSSON  * snd_soc_put_strobe - strobe put callback
3462dd7b10b3SKristoffer KARLSSON  * @kcontrol: mixer control
3463dd7b10b3SKristoffer KARLSSON  * @ucontrol: control element information
3464dd7b10b3SKristoffer KARLSSON  *
3465dd7b10b3SKristoffer KARLSSON  * Callback strobe a register bit to high then low (or the inverse)
3466dd7b10b3SKristoffer KARLSSON  * in one pass of a single mixer enum control.
3467dd7b10b3SKristoffer KARLSSON  *
3468dd7b10b3SKristoffer KARLSSON  * Returns 1 for success.
3469dd7b10b3SKristoffer KARLSSON  */
3470dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3471dd7b10b3SKristoffer KARLSSON 	struct snd_ctl_elem_value *ucontrol)
3472dd7b10b3SKristoffer KARLSSON {
3473dd7b10b3SKristoffer KARLSSON 	struct soc_mixer_control *mc =
3474dd7b10b3SKristoffer KARLSSON 		(struct soc_mixer_control *)kcontrol->private_value;
3475dd7b10b3SKristoffer KARLSSON 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3476dd7b10b3SKristoffer KARLSSON 	unsigned int reg = mc->reg;
3477dd7b10b3SKristoffer KARLSSON 	unsigned int shift = mc->shift;
3478dd7b10b3SKristoffer KARLSSON 	unsigned int mask = 1 << shift;
3479dd7b10b3SKristoffer KARLSSON 	unsigned int invert = mc->invert != 0;
3480dd7b10b3SKristoffer KARLSSON 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3481dd7b10b3SKristoffer KARLSSON 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
3482dd7b10b3SKristoffer KARLSSON 	unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3483dd7b10b3SKristoffer KARLSSON 	int err;
3484dd7b10b3SKristoffer KARLSSON 
3485dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3486dd7b10b3SKristoffer KARLSSON 	if (err < 0)
3487dd7b10b3SKristoffer KARLSSON 		return err;
3488dd7b10b3SKristoffer KARLSSON 
3489dd7b10b3SKristoffer KARLSSON 	err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3490dd7b10b3SKristoffer KARLSSON 	return err;
3491dd7b10b3SKristoffer KARLSSON }
3492dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3493dd7b10b3SKristoffer KARLSSON 
3494dd7b10b3SKristoffer KARLSSON /**
34958c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
34968c6529dbSLiam Girdwood  * @dai: DAI
34978c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
34988c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
34998c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
35008c6529dbSLiam Girdwood  *
35018c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
35028c6529dbSLiam Girdwood  */
35038c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
35048c6529dbSLiam Girdwood 	unsigned int freq, int dir)
35058c6529dbSLiam Girdwood {
3506f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
3507f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3508ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
3509da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3510ec4ee52aSMark Brown 						      freq, dir);
35118c6529dbSLiam Girdwood 	else
35121104a9c8SMark Brown 		return -ENOTSUPP;
35138c6529dbSLiam Girdwood }
35148c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
35158c6529dbSLiam Girdwood 
35168c6529dbSLiam Girdwood /**
3517ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3518ec4ee52aSMark Brown  * @codec: CODEC
3519ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
3520da1c6ea6SMark Brown  * @source: Source for the clock
3521ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
3522ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
3523ec4ee52aSMark Brown  *
3524ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3525ec4ee52aSMark Brown  */
3526ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3527da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
3528ec4ee52aSMark Brown {
3529ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
3530da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
3531da1c6ea6SMark Brown 						 freq, dir);
3532ec4ee52aSMark Brown 	else
35331104a9c8SMark Brown 		return -ENOTSUPP;
3534ec4ee52aSMark Brown }
3535ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3536ec4ee52aSMark Brown 
3537ec4ee52aSMark Brown /**
35388c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
35398c6529dbSLiam Girdwood  * @dai: DAI
3540ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
35418c6529dbSLiam Girdwood  * @div: new clock divisor.
35428c6529dbSLiam Girdwood  *
35438c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
35448c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
35458c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
35468c6529dbSLiam Girdwood  */
35478c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
35488c6529dbSLiam Girdwood 	int div_id, int div)
35498c6529dbSLiam Girdwood {
3550f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
3551f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
35528c6529dbSLiam Girdwood 	else
35538c6529dbSLiam Girdwood 		return -EINVAL;
35548c6529dbSLiam Girdwood }
35558c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
35568c6529dbSLiam Girdwood 
35578c6529dbSLiam Girdwood /**
35588c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
35598c6529dbSLiam Girdwood  * @dai: DAI
35608c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
356185488037SMark Brown  * @source: DAI specific source for the PLL
35628c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
35638c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
35648c6529dbSLiam Girdwood  *
35658c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
35668c6529dbSLiam Girdwood  */
356785488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
356885488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
35698c6529dbSLiam Girdwood {
3570f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
3571f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
357285488037SMark Brown 					 freq_in, freq_out);
3573ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
3574ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3575ec4ee52aSMark Brown 						   freq_in, freq_out);
35768c6529dbSLiam Girdwood 	else
35778c6529dbSLiam Girdwood 		return -EINVAL;
35788c6529dbSLiam Girdwood }
35798c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
35808c6529dbSLiam Girdwood 
3581ec4ee52aSMark Brown /*
3582ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
3583ec4ee52aSMark Brown  * @codec: CODEC
3584ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
3585ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
3586ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
3587ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
3588ec4ee52aSMark Brown  *
3589ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
3590ec4ee52aSMark Brown  */
3591ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3592ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
3593ec4ee52aSMark Brown {
3594ec4ee52aSMark Brown 	if (codec->driver->set_pll)
3595ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
3596ec4ee52aSMark Brown 					      freq_in, freq_out);
3597ec4ee52aSMark Brown 	else
3598ec4ee52aSMark Brown 		return -EINVAL;
3599ec4ee52aSMark Brown }
3600ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3601ec4ee52aSMark Brown 
36028c6529dbSLiam Girdwood /**
3603e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3604e54cf76bSLiam Girdwood  * @dai: DAI
3605e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
3606e54cf76bSLiam Girdwood  *
3607e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
3608e54cf76bSLiam Girdwood  */
3609e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3610e54cf76bSLiam Girdwood {
3611e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
3612e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
3613e54cf76bSLiam Girdwood 	else
3614e54cf76bSLiam Girdwood 		return -EINVAL;
3615e54cf76bSLiam Girdwood }
3616e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3617e54cf76bSLiam Girdwood 
3618e54cf76bSLiam Girdwood /**
36198c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
36208c6529dbSLiam Girdwood  * @dai: DAI
36218c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
36228c6529dbSLiam Girdwood  *
36238c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
36248c6529dbSLiam Girdwood  */
36258c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
36268c6529dbSLiam Girdwood {
36275e4ba569SShawn Guo 	if (dai->driver == NULL)
36288c6529dbSLiam Girdwood 		return -EINVAL;
36295e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
36305e4ba569SShawn Guo 		return -ENOTSUPP;
36315e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
36328c6529dbSLiam Girdwood }
36338c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
36348c6529dbSLiam Girdwood 
36358c6529dbSLiam Girdwood /**
36368c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
36378c6529dbSLiam Girdwood  * @dai: DAI
3638a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
3639a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
36408c6529dbSLiam Girdwood  * @slots: Number of slots in use.
3641a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
36428c6529dbSLiam Girdwood  *
36438c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
36448c6529dbSLiam Girdwood  * specific.
36458c6529dbSLiam Girdwood  */
36468c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3647a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
36488c6529dbSLiam Girdwood {
3649f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
3650f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3651a5479e38SDaniel Ribeiro 				slots, slot_width);
36528c6529dbSLiam Girdwood 	else
3653b2cbb6e1SXiubo Li 		return -ENOTSUPP;
36548c6529dbSLiam Girdwood }
36558c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
36568c6529dbSLiam Girdwood 
36578c6529dbSLiam Girdwood /**
3658472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3659472df3cbSBarry Song  * @dai: DAI
3660472df3cbSBarry Song  * @tx_num: how many TX channels
3661472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
3662472df3cbSBarry Song  *           0~num-1 uses
3663472df3cbSBarry Song  * @rx_num: how many RX channels
3664472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
3665472df3cbSBarry Song  *           0~num-1 uses
3666472df3cbSBarry Song  *
3667472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
3668472df3cbSBarry Song  */
3669472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3670472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
3671472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
3672472df3cbSBarry Song {
3673f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
3674f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3675472df3cbSBarry Song 			rx_num, rx_slot);
3676472df3cbSBarry Song 	else
3677472df3cbSBarry Song 		return -EINVAL;
3678472df3cbSBarry Song }
3679472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3680472df3cbSBarry Song 
3681472df3cbSBarry Song /**
36828c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
36838c6529dbSLiam Girdwood  * @dai: DAI
36848c6529dbSLiam Girdwood  * @tristate: tristate enable
36858c6529dbSLiam Girdwood  *
36868c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
36878c6529dbSLiam Girdwood  */
36888c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
36898c6529dbSLiam Girdwood {
3690f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
3691f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
36928c6529dbSLiam Girdwood 	else
36938c6529dbSLiam Girdwood 		return -EINVAL;
36948c6529dbSLiam Girdwood }
36958c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
36968c6529dbSLiam Girdwood 
36978c6529dbSLiam Girdwood /**
36988c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
36998c6529dbSLiam Girdwood  * @dai: DAI
37008c6529dbSLiam Girdwood  * @mute: mute enable
3701da18396fSMark Brown  * @direction: stream to mute
37028c6529dbSLiam Girdwood  *
37038c6529dbSLiam Girdwood  * Mutes the DAI DAC.
37048c6529dbSLiam Girdwood  */
3705da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3706da18396fSMark Brown 			     int direction)
37078c6529dbSLiam Girdwood {
3708da18396fSMark Brown 	if (!dai->driver)
3709da18396fSMark Brown 		return -ENOTSUPP;
3710da18396fSMark Brown 
3711da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
3712da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
3713da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3714da18396fSMark Brown 		 dai->driver->ops->digital_mute)
3715f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
37168c6529dbSLiam Girdwood 	else
371704570c62SMark Brown 		return -ENOTSUPP;
37188c6529dbSLiam Girdwood }
37198c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
37208c6529dbSLiam Girdwood 
3721c5af3a2eSMark Brown /**
3722c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
3723c5af3a2eSMark Brown  *
3724ac11a2b3SMark Brown  * @card: Card to register
3725c5af3a2eSMark Brown  *
3726c5af3a2eSMark Brown  */
372770a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
3728c5af3a2eSMark Brown {
3729b19e6e7bSMark Brown 	int i, ret;
3730f0fba2adSLiam Girdwood 
3731c5af3a2eSMark Brown 	if (!card->name || !card->dev)
3732c5af3a2eSMark Brown 		return -EINVAL;
3733c5af3a2eSMark Brown 
37345a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
37355a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
37365a504963SStephen Warren 
37375a504963SStephen Warren 		/*
37385a504963SStephen Warren 		 * Codec must be specified by 1 of name or OF node,
37395a504963SStephen Warren 		 * not both or neither.
37405a504963SStephen Warren 		 */
37415a504963SStephen Warren 		if (!!link->codec_name == !!link->codec_of_node) {
374210e8aa9aSMichał Mirosław 			dev_err(card->dev,
374310e8aa9aSMichał Mirosław 				"ASoC: Neither/both codec name/of_node are set for %s\n",
374410e8aa9aSMichał Mirosław 				link->name);
37455a504963SStephen Warren 			return -EINVAL;
37465a504963SStephen Warren 		}
3747bc92657aSStephen Warren 		/* Codec DAI name must be specified */
3748bc92657aSStephen Warren 		if (!link->codec_dai_name) {
374910e8aa9aSMichał Mirosław 			dev_err(card->dev,
375010e8aa9aSMichał Mirosław 				"ASoC: codec_dai_name not set for %s\n",
375110e8aa9aSMichał Mirosław 				link->name);
3752bc92657aSStephen Warren 			return -EINVAL;
3753bc92657aSStephen Warren 		}
37545a504963SStephen Warren 
37555a504963SStephen Warren 		/*
37565a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
37575a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
37585a504963SStephen Warren 		 */
37595a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
376010e8aa9aSMichał Mirosław 			dev_err(card->dev,
376110e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
376210e8aa9aSMichał Mirosław 				link->name);
37635a504963SStephen Warren 			return -EINVAL;
37645a504963SStephen Warren 		}
37655a504963SStephen Warren 
37665a504963SStephen Warren 		/*
3767bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
3768bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
3769bc92657aSStephen Warren 		 * name alone..
37705a504963SStephen Warren 		 */
3771bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
377210e8aa9aSMichał Mirosław 			dev_err(card->dev,
377310e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
377410e8aa9aSMichał Mirosław 				link->name);
3775bc92657aSStephen Warren 			return -EINVAL;
3776bc92657aSStephen Warren 		}
3777bc92657aSStephen Warren 		/*
3778bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
3779bc92657aSStephen Warren 		 * specified
3780bc92657aSStephen Warren 		 */
3781bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
3782bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
378310e8aa9aSMichał Mirosław 			dev_err(card->dev,
378410e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
378510e8aa9aSMichał Mirosław 				link->name);
37865a504963SStephen Warren 			return -EINVAL;
37875a504963SStephen Warren 		}
37885a504963SStephen Warren 	}
37895a504963SStephen Warren 
3790ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
3791ed77cc12SMark Brown 
3792111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
3793111c6419SStephen Warren 
3794150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
3795150dd2f8SVinod Koul 
3796181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
3797181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
37982eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
3799f0fba2adSLiam Girdwood 				 GFP_KERNEL);
3800f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
3801f0fba2adSLiam Girdwood 		return -ENOMEM;
3802a7dbb603SLiam Girdwood 	card->num_rtd = 0;
38032eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
3804f0fba2adSLiam Girdwood 
3805f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_links; i++)
3806f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
3807f0fba2adSLiam Girdwood 
3808c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
3809db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
3810c5af3a2eSMark Brown 	card->instantiated = 0;
3811f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
3812a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
3813c5af3a2eSMark Brown 
3814b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
3815b19e6e7bSMark Brown 	if (ret != 0)
3816b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
3817c5af3a2eSMark Brown 
3818988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
3819988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
3820988e8cc4SNicolin Chen 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3821988e8cc4SNicolin Chen 		struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3822988e8cc4SNicolin Chen 		if (!codec_dai->active)
3823988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(codec_dai->dev);
3824988e8cc4SNicolin Chen 		if (!cpu_dai->active)
3825988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
3826988e8cc4SNicolin Chen 	}
3827988e8cc4SNicolin Chen 
3828b19e6e7bSMark Brown 	return ret;
3829c5af3a2eSMark Brown }
383070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
3831c5af3a2eSMark Brown 
3832c5af3a2eSMark Brown /**
3833c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
3834c5af3a2eSMark Brown  *
3835ac11a2b3SMark Brown  * @card: Card to unregister
3836c5af3a2eSMark Brown  *
3837c5af3a2eSMark Brown  */
383870a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
3839c5af3a2eSMark Brown {
3840b0e26485SVinod Koul 	if (card->instantiated)
3841b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
3842f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3843c5af3a2eSMark Brown 
3844c5af3a2eSMark Brown 	return 0;
3845c5af3a2eSMark Brown }
384670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3847c5af3a2eSMark Brown 
3848f0fba2adSLiam Girdwood /*
3849f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
3850f0fba2adSLiam Girdwood  * and sanitizing names.
3851f0fba2adSLiam Girdwood  */
38520b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
3853f0fba2adSLiam Girdwood {
3854f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
3855f0fba2adSLiam Girdwood 	int id1, id2;
3856f0fba2adSLiam Girdwood 
3857f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
3858f0fba2adSLiam Girdwood 		return NULL;
3859f0fba2adSLiam Girdwood 
386058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
3861f0fba2adSLiam Girdwood 
3862f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
3863f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
3864f0fba2adSLiam Girdwood 	if (found) {
3865f0fba2adSLiam Girdwood 		/* get ID */
3866f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3867f0fba2adSLiam Girdwood 
3868f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
3869f0fba2adSLiam Girdwood 			if (*id == -1)
3870f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
3871f0fba2adSLiam Girdwood 		}
3872f0fba2adSLiam Girdwood 
3873f0fba2adSLiam Girdwood 	} else {
3874f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
3875f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3876f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
3877f0fba2adSLiam Girdwood 
3878f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
387905899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
3880f0fba2adSLiam Girdwood 
3881f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
3882f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
388358818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
3884f0fba2adSLiam Girdwood 		} else
3885f0fba2adSLiam Girdwood 			*id = 0;
3886f0fba2adSLiam Girdwood 	}
3887f0fba2adSLiam Girdwood 
3888f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
3889f0fba2adSLiam Girdwood }
3890f0fba2adSLiam Girdwood 
3891f0fba2adSLiam Girdwood /*
3892f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
3893f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
3894f0fba2adSLiam Girdwood  */
3895f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
3896f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
3897f0fba2adSLiam Girdwood {
3898f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
389910e8aa9aSMichał Mirosław 		dev_err(dev,
390010e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
390110e8aa9aSMichał Mirosław 			dev_name(dev));
3902f0fba2adSLiam Girdwood 		return NULL;
3903f0fba2adSLiam Girdwood 	}
3904f0fba2adSLiam Girdwood 
3905f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
3906f0fba2adSLiam Girdwood }
3907f0fba2adSLiam Girdwood 
39089115171aSMark Brown /**
39099115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
39109115171aSMark Brown  *
3911ac11a2b3SMark Brown  * @dai: DAI to register
39129115171aSMark Brown  */
3913f53179c0SKuninori Morimoto static int snd_soc_register_dai(struct device *dev,
3914f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
39159115171aSMark Brown {
3916054880feSMark Brown 	struct snd_soc_codec *codec;
3917f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
39189115171aSMark Brown 
3919f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
39209115171aSMark Brown 
3921f0fba2adSLiam Girdwood 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3922f0fba2adSLiam Girdwood 	if (dai == NULL)
3923f0fba2adSLiam Girdwood 		return -ENOMEM;
39246335d055SEric Miao 
3925f0fba2adSLiam Girdwood 	/* create DAI component name */
3926f0fba2adSLiam Girdwood 	dai->name = fmt_single_name(dev, &dai->id);
3927f0fba2adSLiam Girdwood 	if (dai->name == NULL) {
3928f0fba2adSLiam Girdwood 		kfree(dai);
3929f0fba2adSLiam Girdwood 		return -ENOMEM;
3930f0fba2adSLiam Girdwood 	}
3931f0fba2adSLiam Girdwood 
3932f0fba2adSLiam Girdwood 	dai->dev = dev;
3933f0fba2adSLiam Girdwood 	dai->driver = dai_drv;
3934be09ad90SLiam Girdwood 	dai->dapm.dev = dev;
3935f0fba2adSLiam Girdwood 	if (!dai->driver->ops)
3936f0fba2adSLiam Girdwood 		dai->driver->ops = &null_dai_ops;
39379115171aSMark Brown 
39389115171aSMark Brown 	mutex_lock(&client_mutex);
3939054880feSMark Brown 
3940054880feSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3941054880feSMark Brown 		if (codec->dev == dev) {
3942f110bfc7SLiam Girdwood 			dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3943054880feSMark Brown 				dai->name, codec->name);
3944054880feSMark Brown 			dai->codec = codec;
3945054880feSMark Brown 			break;
3946054880feSMark Brown 		}
3947054880feSMark Brown 	}
3948054880feSMark Brown 
39495f800080SPeter Ujfalusi 	if (!dai->codec)
39505f800080SPeter Ujfalusi 		dai->dapm.idle_bias_off = 1;
39515f800080SPeter Ujfalusi 
39529115171aSMark Brown 	list_add(&dai->list, &dai_list);
3953054880feSMark Brown 
39549115171aSMark Brown 	mutex_unlock(&client_mutex);
39559115171aSMark Brown 
3956f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
39579115171aSMark Brown 
39589115171aSMark Brown 	return 0;
39599115171aSMark Brown }
39609115171aSMark Brown 
39619115171aSMark Brown /**
39629115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
39639115171aSMark Brown  *
3964ac11a2b3SMark Brown  * @dai: DAI to unregister
39659115171aSMark Brown  */
3966f53179c0SKuninori Morimoto static void snd_soc_unregister_dai(struct device *dev)
39679115171aSMark Brown {
3968f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3969f0fba2adSLiam Girdwood 
3970f0fba2adSLiam Girdwood 	list_for_each_entry(dai, &dai_list, list) {
3971f0fba2adSLiam Girdwood 		if (dev == dai->dev)
3972f0fba2adSLiam Girdwood 			goto found;
3973f0fba2adSLiam Girdwood 	}
3974f0fba2adSLiam Girdwood 	return;
3975f0fba2adSLiam Girdwood 
3976f0fba2adSLiam Girdwood found:
39779115171aSMark Brown 	mutex_lock(&client_mutex);
39789115171aSMark Brown 	list_del(&dai->list);
39799115171aSMark Brown 	mutex_unlock(&client_mutex);
39809115171aSMark Brown 
3981f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3982f0fba2adSLiam Girdwood 	kfree(dai->name);
3983f0fba2adSLiam Girdwood 	kfree(dai);
39849115171aSMark Brown }
39859115171aSMark Brown 
39869115171aSMark Brown /**
39879115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
39889115171aSMark Brown  *
3989ac11a2b3SMark Brown  * @dai: Array of DAIs to register
3990ac11a2b3SMark Brown  * @count: Number of DAIs
39919115171aSMark Brown  */
3992f53179c0SKuninori Morimoto static int snd_soc_register_dais(struct device *dev,
3993f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv, size_t count)
39949115171aSMark Brown {
3995054880feSMark Brown 	struct snd_soc_codec *codec;
3996f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
3997f0fba2adSLiam Girdwood 	int i, ret = 0;
3998f0fba2adSLiam Girdwood 
3999f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
40009115171aSMark Brown 
40019115171aSMark Brown 	for (i = 0; i < count; i++) {
4002f0fba2adSLiam Girdwood 
4003f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
4004c46e0079SAxel Lin 		if (dai == NULL) {
4005c46e0079SAxel Lin 			ret = -ENOMEM;
4006c46e0079SAxel Lin 			goto err;
4007c46e0079SAxel Lin 		}
4008f0fba2adSLiam Girdwood 
4009f0fba2adSLiam Girdwood 		/* create DAI component name */
4010f0fba2adSLiam Girdwood 		dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4011f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
4012f0fba2adSLiam Girdwood 			kfree(dai);
4013f0fba2adSLiam Girdwood 			ret = -EINVAL;
40149115171aSMark Brown 			goto err;
40159115171aSMark Brown 		}
40169115171aSMark Brown 
4017f0fba2adSLiam Girdwood 		dai->dev = dev;
4018f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
40190f9141c9SMark Brown 		if (dai->driver->id)
40200f9141c9SMark Brown 			dai->id = dai->driver->id;
40210f9141c9SMark Brown 		else
40220f9141c9SMark Brown 			dai->id = i;
4023be09ad90SLiam Girdwood 		dai->dapm.dev = dev;
4024f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
4025f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
4026f0fba2adSLiam Girdwood 
4027f0fba2adSLiam Girdwood 		mutex_lock(&client_mutex);
4028054880feSMark Brown 
4029054880feSMark Brown 		list_for_each_entry(codec, &codec_list, list) {
4030054880feSMark Brown 			if (codec->dev == dev) {
403110e8aa9aSMichał Mirosław 				dev_dbg(dev,
403210e8aa9aSMichał Mirosław 					"ASoC: Mapped DAI %s to CODEC %s\n",
403310e8aa9aSMichał Mirosław 					dai->name, codec->name);
4034054880feSMark Brown 				dai->codec = codec;
4035054880feSMark Brown 				break;
4036054880feSMark Brown 			}
4037054880feSMark Brown 		}
4038054880feSMark Brown 
40395f800080SPeter Ujfalusi 		if (!dai->codec)
40405f800080SPeter Ujfalusi 			dai->dapm.idle_bias_off = 1;
40415f800080SPeter Ujfalusi 
4042f0fba2adSLiam Girdwood 		list_add(&dai->list, &dai_list);
4043054880feSMark Brown 
4044f0fba2adSLiam Girdwood 		mutex_unlock(&client_mutex);
4045f0fba2adSLiam Girdwood 
4046f110bfc7SLiam Girdwood 		dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
4047f0fba2adSLiam Girdwood 	}
4048f0fba2adSLiam Girdwood 
40499115171aSMark Brown 	return 0;
40509115171aSMark Brown 
40519115171aSMark Brown err:
40529115171aSMark Brown 	for (i--; i >= 0; i--)
4053f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
40549115171aSMark Brown 
40559115171aSMark Brown 	return ret;
40569115171aSMark Brown }
40579115171aSMark Brown 
40589115171aSMark Brown /**
40599115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
40609115171aSMark Brown  *
4061ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
4062ac11a2b3SMark Brown  * @count: Number of DAIs
40639115171aSMark Brown  */
4064f53179c0SKuninori Morimoto static void snd_soc_unregister_dais(struct device *dev, size_t count)
40659115171aSMark Brown {
40669115171aSMark Brown 	int i;
40679115171aSMark Brown 
40689115171aSMark Brown 	for (i = 0; i < count; i++)
4069f0fba2adSLiam Girdwood 		snd_soc_unregister_dai(dev);
40709115171aSMark Brown }
40719115171aSMark Brown 
407212a48a8cSMark Brown /**
4073d191bd8dSKuninori Morimoto  * snd_soc_register_component - Register a component with the ASoC core
4074d191bd8dSKuninori Morimoto  *
4075d191bd8dSKuninori Morimoto  */
4076d191bd8dSKuninori Morimoto static int
4077d191bd8dSKuninori Morimoto __snd_soc_register_component(struct device *dev,
4078d191bd8dSKuninori Morimoto 			     struct snd_soc_component *cmpnt,
4079d191bd8dSKuninori Morimoto 			     const struct snd_soc_component_driver *cmpnt_drv,
4080d191bd8dSKuninori Morimoto 			     struct snd_soc_dai_driver *dai_drv,
4081d191bd8dSKuninori Morimoto 			     int num_dai, bool allow_single_dai)
4082d191bd8dSKuninori Morimoto {
4083d191bd8dSKuninori Morimoto 	int ret;
4084d191bd8dSKuninori Morimoto 
4085d191bd8dSKuninori Morimoto 	dev_dbg(dev, "component register %s\n", dev_name(dev));
4086d191bd8dSKuninori Morimoto 
4087d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4088d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to connecting component\n");
4089d191bd8dSKuninori Morimoto 		return -ENOMEM;
4090d191bd8dSKuninori Morimoto 	}
4091d191bd8dSKuninori Morimoto 
4092d191bd8dSKuninori Morimoto 	cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4093d191bd8dSKuninori Morimoto 	if (!cmpnt->name) {
4094d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to simplifying name\n");
4095d191bd8dSKuninori Morimoto 		return -ENOMEM;
4096d191bd8dSKuninori Morimoto 	}
4097d191bd8dSKuninori Morimoto 
4098d191bd8dSKuninori Morimoto 	cmpnt->dev	= dev;
4099d191bd8dSKuninori Morimoto 	cmpnt->driver	= cmpnt_drv;
41006833c452SKuninori Morimoto 	cmpnt->dai_drv	= dai_drv;
4101d191bd8dSKuninori Morimoto 	cmpnt->num_dai	= num_dai;
4102d191bd8dSKuninori Morimoto 
4103d191bd8dSKuninori Morimoto 	/*
4104d191bd8dSKuninori Morimoto 	 * snd_soc_register_dai()  uses fmt_single_name(), and
4105d191bd8dSKuninori Morimoto 	 * snd_soc_register_dais() uses fmt_multiple_name()
4106d191bd8dSKuninori Morimoto 	 * for dai->name which is used for name based matching
4107d191bd8dSKuninori Morimoto 	 *
4108d191bd8dSKuninori Morimoto 	 * this function is used from cpu/codec.
4109d191bd8dSKuninori Morimoto 	 * allow_single_dai flag can ignore "codec" driver reworking
4110d191bd8dSKuninori Morimoto 	 * since it had been used snd_soc_register_dais(),
4111d191bd8dSKuninori Morimoto 	 */
4112d191bd8dSKuninori Morimoto 	if ((1 == num_dai) && allow_single_dai)
4113d191bd8dSKuninori Morimoto 		ret = snd_soc_register_dai(dev, dai_drv);
4114d191bd8dSKuninori Morimoto 	else
4115d191bd8dSKuninori Morimoto 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4116d191bd8dSKuninori Morimoto 	if (ret < 0) {
4117d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4118d191bd8dSKuninori Morimoto 		goto error_component_name;
4119d191bd8dSKuninori Morimoto 	}
4120d191bd8dSKuninori Morimoto 
4121d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
4122d191bd8dSKuninori Morimoto 	list_add(&cmpnt->list, &component_list);
4123d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
4124d191bd8dSKuninori Morimoto 
4125d191bd8dSKuninori Morimoto 	dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4126d191bd8dSKuninori Morimoto 
4127d191bd8dSKuninori Morimoto 	return ret;
4128d191bd8dSKuninori Morimoto 
4129d191bd8dSKuninori Morimoto error_component_name:
4130d191bd8dSKuninori Morimoto 	kfree(cmpnt->name);
4131d191bd8dSKuninori Morimoto 
4132d191bd8dSKuninori Morimoto 	return ret;
4133d191bd8dSKuninori Morimoto }
4134d191bd8dSKuninori Morimoto 
4135d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
4136d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
4137d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
4138d191bd8dSKuninori Morimoto 			       int num_dai)
4139d191bd8dSKuninori Morimoto {
4140d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4141d191bd8dSKuninori Morimoto 
4142d191bd8dSKuninori Morimoto 	cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4143d191bd8dSKuninori Morimoto 	if (!cmpnt) {
4144d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
4145d191bd8dSKuninori Morimoto 		return -ENOMEM;
4146d191bd8dSKuninori Morimoto 	}
4147d191bd8dSKuninori Morimoto 
4148d191bd8dSKuninori Morimoto 	return __snd_soc_register_component(dev, cmpnt, cmpnt_drv,
4149d191bd8dSKuninori Morimoto 					    dai_drv, num_dai, true);
4150d191bd8dSKuninori Morimoto }
4151d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
4152d191bd8dSKuninori Morimoto 
4153d191bd8dSKuninori Morimoto /**
4154d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
4155d191bd8dSKuninori Morimoto  *
4156d191bd8dSKuninori Morimoto  */
4157d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
4158d191bd8dSKuninori Morimoto {
4159d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
4160d191bd8dSKuninori Morimoto 
4161d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
4162d191bd8dSKuninori Morimoto 		if (dev == cmpnt->dev)
4163d191bd8dSKuninori Morimoto 			goto found;
4164d191bd8dSKuninori Morimoto 	}
4165d191bd8dSKuninori Morimoto 	return;
4166d191bd8dSKuninori Morimoto 
4167d191bd8dSKuninori Morimoto found:
4168d191bd8dSKuninori Morimoto 	snd_soc_unregister_dais(dev, cmpnt->num_dai);
4169d191bd8dSKuninori Morimoto 
4170d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
4171d191bd8dSKuninori Morimoto 	list_del(&cmpnt->list);
4172d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
4173d191bd8dSKuninori Morimoto 
4174d191bd8dSKuninori Morimoto 	dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4175d191bd8dSKuninori Morimoto 	kfree(cmpnt->name);
4176d191bd8dSKuninori Morimoto }
4177d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4178d191bd8dSKuninori Morimoto 
4179d191bd8dSKuninori Morimoto /**
418071a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
418171a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
418271a45cdaSLars-Peter Clausen  * @platform: The platform to add
418371a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
418412a48a8cSMark Brown  */
418571a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4186d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
418712a48a8cSMark Brown {
4188f0fba2adSLiam Girdwood 	/* create platform component name */
4189f0fba2adSLiam Girdwood 	platform->name = fmt_single_name(dev, &platform->id);
4190b5c745fbSDan Carpenter 	if (platform->name == NULL)
4191f0fba2adSLiam Girdwood 		return -ENOMEM;
4192f0fba2adSLiam Girdwood 
4193f0fba2adSLiam Girdwood 	platform->dev = dev;
4194f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
4195b7950641SLiam Girdwood 	platform->dapm.dev = dev;
4196b7950641SLiam Girdwood 	platform->dapm.platform = platform;
419764a648c2SLiam Girdwood 	platform->dapm.stream_event = platform_drv->stream_event;
4198cc22d37eSLiam Girdwood 	mutex_init(&platform->mutex);
419912a48a8cSMark Brown 
420012a48a8cSMark Brown 	mutex_lock(&client_mutex);
420112a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
420212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
420312a48a8cSMark Brown 
4204f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
420512a48a8cSMark Brown 
420612a48a8cSMark Brown 	return 0;
420712a48a8cSMark Brown }
420871a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
420971a45cdaSLars-Peter Clausen 
421071a45cdaSLars-Peter Clausen /**
421171a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
421271a45cdaSLars-Peter Clausen  *
421371a45cdaSLars-Peter Clausen  * @platform: platform to register
421471a45cdaSLars-Peter Clausen  */
421571a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
421671a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
421771a45cdaSLars-Peter Clausen {
421871a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
421971a45cdaSLars-Peter Clausen 	int ret;
422071a45cdaSLars-Peter Clausen 
422171a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
422271a45cdaSLars-Peter Clausen 
422371a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
422471a45cdaSLars-Peter Clausen 	if (platform == NULL)
422571a45cdaSLars-Peter Clausen 		return -ENOMEM;
422671a45cdaSLars-Peter Clausen 
422771a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
422871a45cdaSLars-Peter Clausen 	if (ret)
422971a45cdaSLars-Peter Clausen 		kfree(platform);
423071a45cdaSLars-Peter Clausen 
423171a45cdaSLars-Peter Clausen 	return ret;
423271a45cdaSLars-Peter Clausen }
423312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
423412a48a8cSMark Brown 
423512a48a8cSMark Brown /**
423671a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
423771a45cdaSLars-Peter Clausen  * @platform: the platform to remove
423871a45cdaSLars-Peter Clausen  */
423971a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
424071a45cdaSLars-Peter Clausen {
424171a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
424271a45cdaSLars-Peter Clausen 	list_del(&platform->list);
424371a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
424471a45cdaSLars-Peter Clausen 
424571a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
424671a45cdaSLars-Peter Clausen 		platform->name);
424771a45cdaSLars-Peter Clausen 	kfree(platform->name);
424871a45cdaSLars-Peter Clausen }
424971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
425071a45cdaSLars-Peter Clausen 
425171a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
425271a45cdaSLars-Peter Clausen {
425371a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
425471a45cdaSLars-Peter Clausen 
425571a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
425671a45cdaSLars-Peter Clausen 		if (dev == platform->dev)
425771a45cdaSLars-Peter Clausen 			return platform;
425871a45cdaSLars-Peter Clausen 	}
425971a45cdaSLars-Peter Clausen 
426071a45cdaSLars-Peter Clausen 	return NULL;
426171a45cdaSLars-Peter Clausen }
426271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
426371a45cdaSLars-Peter Clausen 
426471a45cdaSLars-Peter Clausen /**
426512a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
426612a48a8cSMark Brown  *
4267ac11a2b3SMark Brown  * @platform: platform to unregister
426812a48a8cSMark Brown  */
4269f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
427012a48a8cSMark Brown {
4271f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
4272f0fba2adSLiam Girdwood 
427371a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
427471a45cdaSLars-Peter Clausen 	if (!platform)
4275f0fba2adSLiam Girdwood 		return;
4276f0fba2adSLiam Girdwood 
427771a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
4278f0fba2adSLiam Girdwood 	kfree(platform);
427912a48a8cSMark Brown }
428012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
428112a48a8cSMark Brown 
4282151ab22cSMark Brown static u64 codec_format_map[] = {
4283151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4284151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4285151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4286151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4287151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4288151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4289151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4290151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4291151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4292151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4293151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4294151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4295151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4296151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4297151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4298151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4299151ab22cSMark Brown };
4300151ab22cSMark Brown 
4301151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
4302151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
4303151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
4304151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
4305151ab22cSMark Brown  */
4306151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4307151ab22cSMark Brown {
4308151ab22cSMark Brown 	int i;
4309151ab22cSMark Brown 
4310151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4311151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
4312151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
4313151ab22cSMark Brown }
4314151ab22cSMark Brown 
43150d0cf00aSMark Brown /**
43160d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
43170d0cf00aSMark Brown  *
4318ac11a2b3SMark Brown  * @codec: codec to register
43190d0cf00aSMark Brown  */
4320f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
4321001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
4322001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
4323001ae4c0SMark Brown 			   int num_dai)
43240d0cf00aSMark Brown {
4325f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4326f0fba2adSLiam Girdwood 	int ret, i;
4327151ab22cSMark Brown 
4328f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
43290d0cf00aSMark Brown 
4330f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4331f0fba2adSLiam Girdwood 	if (codec == NULL)
4332f0fba2adSLiam Girdwood 		return -ENOMEM;
43330d0cf00aSMark Brown 
4334f0fba2adSLiam Girdwood 	/* create CODEC component name */
4335f0fba2adSLiam Girdwood 	codec->name = fmt_single_name(dev, &codec->id);
4336f0fba2adSLiam Girdwood 	if (codec->name == NULL) {
4337f790b94dSKuninori Morimoto 		ret = -ENOMEM;
4338f790b94dSKuninori Morimoto 		goto fail_codec;
4339151ab22cSMark Brown 	}
4340151ab22cSMark Brown 
4341c3acec26SMark Brown 	codec->write = codec_drv->write;
4342c3acec26SMark Brown 	codec->read = codec_drv->read;
43431500b7b5SDimitris Papastamos 	codec->volatile_register = codec_drv->volatile_register;
43441500b7b5SDimitris Papastamos 	codec->readable_register = codec_drv->readable_register;
43458020454cSDimitris Papastamos 	codec->writable_register = codec_drv->writable_register;
43465124e69eSMark Brown 	codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4347ce6120ccSLiam Girdwood 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4348ce6120ccSLiam Girdwood 	codec->dapm.dev = dev;
4349ce6120ccSLiam Girdwood 	codec->dapm.codec = codec;
4350474b62d6SMark Brown 	codec->dapm.seq_notifier = codec_drv->seq_notifier;
435164a648c2SLiam Girdwood 	codec->dapm.stream_event = codec_drv->stream_event;
4352f0fba2adSLiam Girdwood 	codec->dev = dev;
4353f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
4354f0fba2adSLiam Girdwood 	codec->num_dai = num_dai;
4355f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
4356f0fba2adSLiam Girdwood 
4357f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
4358f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
4359f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
4360f0fba2adSLiam Girdwood 	}
4361f0fba2adSLiam Girdwood 
4362054880feSMark Brown 	mutex_lock(&client_mutex);
4363054880feSMark Brown 	list_add(&codec->list, &codec_list);
4364054880feSMark Brown 	mutex_unlock(&client_mutex);
4365054880feSMark Brown 
4366d191bd8dSKuninori Morimoto 	/* register component */
4367d191bd8dSKuninori Morimoto 	ret = __snd_soc_register_component(dev, &codec->component,
4368d191bd8dSKuninori Morimoto 					   &codec_drv->component_driver,
4369d191bd8dSKuninori Morimoto 					   dai_drv, num_dai, false);
43705acd7dfbSKuninori Morimoto 	if (ret < 0) {
4371d191bd8dSKuninori Morimoto 		dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
43725acd7dfbSKuninori Morimoto 		goto fail_codec_name;
437326b01ccdSMark Brown 	}
4374f0fba2adSLiam Girdwood 
4375f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
43760d0cf00aSMark Brown 	return 0;
4377f0fba2adSLiam Girdwood 
4378f790b94dSKuninori Morimoto fail_codec_name:
4379e1328a83SKuninori Morimoto 	mutex_lock(&client_mutex);
4380e1328a83SKuninori Morimoto 	list_del(&codec->list);
4381e1328a83SKuninori Morimoto 	mutex_unlock(&client_mutex);
4382e1328a83SKuninori Morimoto 
4383f0fba2adSLiam Girdwood 	kfree(codec->name);
4384f790b94dSKuninori Morimoto fail_codec:
4385f0fba2adSLiam Girdwood 	kfree(codec);
4386f0fba2adSLiam Girdwood 	return ret;
43870d0cf00aSMark Brown }
43880d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
43890d0cf00aSMark Brown 
43900d0cf00aSMark Brown /**
43910d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
43920d0cf00aSMark Brown  *
4393ac11a2b3SMark Brown  * @codec: codec to unregister
43940d0cf00aSMark Brown  */
4395f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
43960d0cf00aSMark Brown {
4397f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
4398f0fba2adSLiam Girdwood 
4399f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
4400f0fba2adSLiam Girdwood 		if (dev == codec->dev)
4401f0fba2adSLiam Girdwood 			goto found;
4402f0fba2adSLiam Girdwood 	}
4403f0fba2adSLiam Girdwood 	return;
4404f0fba2adSLiam Girdwood 
4405f0fba2adSLiam Girdwood found:
4406d191bd8dSKuninori Morimoto 	snd_soc_unregister_component(dev);
4407f0fba2adSLiam Girdwood 
44080d0cf00aSMark Brown 	mutex_lock(&client_mutex);
44090d0cf00aSMark Brown 	list_del(&codec->list);
44100d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
44110d0cf00aSMark Brown 
4412f110bfc7SLiam Girdwood 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4413f0fba2adSLiam Girdwood 
44147a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
44151aafcd4dSDimitris Papastamos 	kfree(codec->name);
4416f0fba2adSLiam Girdwood 	kfree(codec);
44170d0cf00aSMark Brown }
44180d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
44190d0cf00aSMark Brown 
4420bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
4421bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4422bec4fa05SStephen Warren 			       const char *propname)
4423bec4fa05SStephen Warren {
4424bec4fa05SStephen Warren 	struct device_node *np = card->dev->of_node;
4425bec4fa05SStephen Warren 	int ret;
4426bec4fa05SStephen Warren 
4427bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
4428bec4fa05SStephen Warren 	/*
4429bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
4430bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
4431bec4fa05SStephen Warren 	 * snd_soc_register_card.
4432bec4fa05SStephen Warren 	 */
4433bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
4434bec4fa05SStephen Warren 		dev_err(card->dev,
4435f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
4436bec4fa05SStephen Warren 			propname, ret);
4437bec4fa05SStephen Warren 		return ret;
4438bec4fa05SStephen Warren 	}
4439bec4fa05SStephen Warren 
4440bec4fa05SStephen Warren 	return 0;
4441bec4fa05SStephen Warren }
4442bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4443bec4fa05SStephen Warren 
4444a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4445a4a54dd5SStephen Warren 				   const char *propname)
4446a4a54dd5SStephen Warren {
4447a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
4448a4a54dd5SStephen Warren 	int num_routes;
4449a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
4450a4a54dd5SStephen Warren 	int i, ret;
4451a4a54dd5SStephen Warren 
4452a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
4453c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
445410e8aa9aSMichał Mirosław 		dev_err(card->dev,
445510e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
445610e8aa9aSMichał Mirosław 			propname);
4457a4a54dd5SStephen Warren 		return -EINVAL;
4458a4a54dd5SStephen Warren 	}
4459a4a54dd5SStephen Warren 	num_routes /= 2;
4460a4a54dd5SStephen Warren 	if (!num_routes) {
4461f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4462a4a54dd5SStephen Warren 			propname);
4463a4a54dd5SStephen Warren 		return -EINVAL;
4464a4a54dd5SStephen Warren 	}
4465a4a54dd5SStephen Warren 
4466a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4467a4a54dd5SStephen Warren 			      GFP_KERNEL);
4468a4a54dd5SStephen Warren 	if (!routes) {
4469a4a54dd5SStephen Warren 		dev_err(card->dev,
4470f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
4471a4a54dd5SStephen Warren 		return -EINVAL;
4472a4a54dd5SStephen Warren 	}
4473a4a54dd5SStephen Warren 
4474a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
4475a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4476a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
4477a4a54dd5SStephen Warren 		if (ret) {
4478c871bd0bSMark Brown 			dev_err(card->dev,
4479c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4480c871bd0bSMark Brown 				propname, 2 * i, ret);
4481a4a54dd5SStephen Warren 			return -EINVAL;
4482a4a54dd5SStephen Warren 		}
4483a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
4484a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
4485a4a54dd5SStephen Warren 		if (ret) {
4486a4a54dd5SStephen Warren 			dev_err(card->dev,
4487c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
4488c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
4489a4a54dd5SStephen Warren 			return -EINVAL;
4490a4a54dd5SStephen Warren 		}
4491a4a54dd5SStephen Warren 	}
4492a4a54dd5SStephen Warren 
4493a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
4494a4a54dd5SStephen Warren 	card->dapm_routes = routes;
4495a4a54dd5SStephen Warren 
4496a4a54dd5SStephen Warren 	return 0;
4497a4a54dd5SStephen Warren }
4498a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4499a4a54dd5SStephen Warren 
4500a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4501a7930ed4SKuninori Morimoto 				     const char *prefix)
4502a7930ed4SKuninori Morimoto {
4503a7930ed4SKuninori Morimoto 	int ret, i;
4504a7930ed4SKuninori Morimoto 	char prop[128];
4505a7930ed4SKuninori Morimoto 	unsigned int format = 0;
4506a7930ed4SKuninori Morimoto 	int bit, frame;
4507a7930ed4SKuninori Morimoto 	const char *str;
4508a7930ed4SKuninori Morimoto 	struct {
4509a7930ed4SKuninori Morimoto 		char *name;
4510a7930ed4SKuninori Morimoto 		unsigned int val;
4511a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
4512a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
4513a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
4514a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
4515a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
4516a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
4517a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
4518a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
4519a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
4520a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
4521a7930ed4SKuninori Morimoto 	};
4522a7930ed4SKuninori Morimoto 
4523a7930ed4SKuninori Morimoto 	if (!prefix)
4524a7930ed4SKuninori Morimoto 		prefix = "";
4525a7930ed4SKuninori Morimoto 
4526a7930ed4SKuninori Morimoto 	/*
4527a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
4528a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
4529a7930ed4SKuninori Morimoto 	 */
4530a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
4531a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
4532a7930ed4SKuninori Morimoto 	if (ret == 0) {
4533a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4534a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
4535a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
4536a7930ed4SKuninori Morimoto 				break;
4537a7930ed4SKuninori Morimoto 			}
4538a7930ed4SKuninori Morimoto 		}
4539a7930ed4SKuninori Morimoto 	}
4540a7930ed4SKuninori Morimoto 
4541a7930ed4SKuninori Morimoto 	/*
45428c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
4543a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
4544a7930ed4SKuninori Morimoto 	 */
45458c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
45468c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
45478c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
45488c2d6a9fSKuninori Morimoto 	else
45498c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
4550a7930ed4SKuninori Morimoto 
4551a7930ed4SKuninori Morimoto 	/*
4552a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
4553a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
4554a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
4555a7930ed4SKuninori Morimoto 	 */
4556a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4557a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4558a7930ed4SKuninori Morimoto 
4559a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4560a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4561a7930ed4SKuninori Morimoto 
4562a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4563a7930ed4SKuninori Morimoto 	case 0x11:
4564a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
4565a7930ed4SKuninori Morimoto 		break;
4566a7930ed4SKuninori Morimoto 	case 0x10:
4567a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
4568a7930ed4SKuninori Morimoto 		break;
4569a7930ed4SKuninori Morimoto 	case 0x01:
4570a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
4571a7930ed4SKuninori Morimoto 		break;
4572a7930ed4SKuninori Morimoto 	default:
4573a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
4574a7930ed4SKuninori Morimoto 		break;
4575a7930ed4SKuninori Morimoto 	}
4576a7930ed4SKuninori Morimoto 
4577a7930ed4SKuninori Morimoto 	/*
4578a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
4579a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
4580a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
4581a7930ed4SKuninori Morimoto 	 */
4582a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4583a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
4584a7930ed4SKuninori Morimoto 
4585a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4586a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
4587a7930ed4SKuninori Morimoto 
4588a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
4589a7930ed4SKuninori Morimoto 	case 0x11:
4590a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
4591a7930ed4SKuninori Morimoto 		break;
4592a7930ed4SKuninori Morimoto 	case 0x10:
4593a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
4594a7930ed4SKuninori Morimoto 		break;
4595a7930ed4SKuninori Morimoto 	case 0x01:
4596a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
4597a7930ed4SKuninori Morimoto 		break;
4598a7930ed4SKuninori Morimoto 	default:
4599a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
4600a7930ed4SKuninori Morimoto 		break;
4601a7930ed4SKuninori Morimoto 	}
4602a7930ed4SKuninori Morimoto 
4603a7930ed4SKuninori Morimoto 	return format;
4604a7930ed4SKuninori Morimoto }
4605a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4606a7930ed4SKuninori Morimoto 
4607cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node,
4608cb470087SKuninori Morimoto 			    const char **dai_name)
4609cb470087SKuninori Morimoto {
4610cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
4611cb470087SKuninori Morimoto 	struct of_phandle_args args;
4612cb470087SKuninori Morimoto 	int ret;
4613cb470087SKuninori Morimoto 
4614cb470087SKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
4615cb470087SKuninori Morimoto 					 "#sound-dai-cells", 0, &args);
4616cb470087SKuninori Morimoto 	if (ret)
4617cb470087SKuninori Morimoto 		return ret;
4618cb470087SKuninori Morimoto 
4619cb470087SKuninori Morimoto 	ret = -EPROBE_DEFER;
4620cb470087SKuninori Morimoto 
4621cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
4622cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
4623cb470087SKuninori Morimoto 		if (pos->dev->of_node != args.np)
4624cb470087SKuninori Morimoto 			continue;
4625cb470087SKuninori Morimoto 
46266833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
46276833c452SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
46286833c452SKuninori Morimoto 		} else {
46296833c452SKuninori Morimoto 			int id = -1;
46306833c452SKuninori Morimoto 
46316833c452SKuninori Morimoto 			switch (args.args_count) {
46326833c452SKuninori Morimoto 			case 0:
46336833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
46346833c452SKuninori Morimoto 				break;
46356833c452SKuninori Morimoto 			case 1:
46366833c452SKuninori Morimoto 				id = args.args[0];
46376833c452SKuninori Morimoto 				break;
46386833c452SKuninori Morimoto 			default:
46396833c452SKuninori Morimoto 				/* not supported */
4640cb470087SKuninori Morimoto 				break;
4641cb470087SKuninori Morimoto 			}
4642cb470087SKuninori Morimoto 
46436833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
46446833c452SKuninori Morimoto 				ret = -EINVAL;
4645e41975edSXiubo Li 				break;
46466833c452SKuninori Morimoto 			}
4647e41975edSXiubo Li 
4648e41975edSXiubo Li 			ret = 0;
4649e41975edSXiubo Li 
4650e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
4651e41975edSXiubo Li 			if (!*dai_name)
4652e41975edSXiubo Li 				*dai_name = pos->name;
46536833c452SKuninori Morimoto 		}
46546833c452SKuninori Morimoto 
4655cb470087SKuninori Morimoto 		break;
4656cb470087SKuninori Morimoto 	}
4657cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
4658cb470087SKuninori Morimoto 
4659cb470087SKuninori Morimoto 	of_node_put(args.np);
4660cb470087SKuninori Morimoto 
4661cb470087SKuninori Morimoto 	return ret;
4662cb470087SKuninori Morimoto }
4663cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4664cb470087SKuninori Morimoto 
4665c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
4666db2a4165SFrank Mandarino {
4667384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
46688a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
46698a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
46700837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
46718a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
4672384c89e2SMark Brown 	}
4673c3c5a19aSMark Brown 
46748a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4675c3c5a19aSMark Brown 				 &codec_list_fops))
4676c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4677c3c5a19aSMark Brown 
46788a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4679f3208780SMark Brown 				 &dai_list_fops))
4680f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
468119c7ac27SMark Brown 
46828a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
468319c7ac27SMark Brown 				 &platform_list_fops))
468419c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
4685384c89e2SMark Brown #endif
4686384c89e2SMark Brown 
4687fb257897SMark Brown 	snd_soc_util_init();
4688fb257897SMark Brown 
4689db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
4690db2a4165SFrank Mandarino }
46914abe8e16SMark Brown module_init(snd_soc_init);
4692db2a4165SFrank Mandarino 
46937d8c16a6SMark Brown static void __exit snd_soc_exit(void)
4694db2a4165SFrank Mandarino {
4695fb257897SMark Brown 	snd_soc_util_exit();
4696fb257897SMark Brown 
4697384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
46988a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
4699384c89e2SMark Brown #endif
4700db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
4701db2a4165SFrank Mandarino }
4702db2a4165SFrank Mandarino module_exit(snd_soc_exit);
4703db2a4165SFrank Mandarino 
4704db2a4165SFrank Mandarino /* Module information */
4705d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4706db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
4707db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
47088b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
4709