xref: /openbmc/linux/sound/soc/soc-core.c (revision 313665b983fe30af9d0eb274f7e03276e05a1bbf)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
50664d888SLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6f0fba2adSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7f0fba2adSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
80664d888SLiam Girdwood  *
9d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
100664d888SLiam Girdwood  *         with code, comments and ideas from :-
110664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
14db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
15db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
16db2a4165SFrank Mandarino  *  option) any later version.
17db2a4165SFrank Mandarino  *
18db2a4165SFrank Mandarino  *  TODO:
19db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
20db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
21db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
22db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
23db2a4165SFrank Mandarino  */
24db2a4165SFrank Mandarino 
25db2a4165SFrank Mandarino #include <linux/module.h>
26db2a4165SFrank Mandarino #include <linux/moduleparam.h>
27db2a4165SFrank Mandarino #include <linux/init.h>
28db2a4165SFrank Mandarino #include <linux/delay.h>
29db2a4165SFrank Mandarino #include <linux/pm.h>
30db2a4165SFrank Mandarino #include <linux/bitops.h>
3112ef193dSTroy Kisky #include <linux/debugfs.h>
32db2a4165SFrank Mandarino #include <linux/platform_device.h>
33741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
34f0e8ed85SMark Brown #include <linux/ctype.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
36bec4fa05SStephen Warren #include <linux/of.h>
37741a509fSMarkus Pargmann #include <linux/gpio.h>
38741a509fSMarkus Pargmann #include <linux/of_gpio.h>
39474828a4SMarek Vasut #include <sound/ac97_codec.h>
40db2a4165SFrank Mandarino #include <sound/core.h>
413028eb8cSMark Brown #include <sound/jack.h>
42db2a4165SFrank Mandarino #include <sound/pcm.h>
43db2a4165SFrank Mandarino #include <sound/pcm_params.h>
44db2a4165SFrank Mandarino #include <sound/soc.h>
4501d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
46db2a4165SFrank Mandarino #include <sound/initval.h>
47db2a4165SFrank Mandarino 
48a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
49a8b1d34fSMark Brown #include <trace/events/asoc.h>
50a8b1d34fSMark Brown 
51f0fba2adSLiam Girdwood #define NAME_SIZE	32
52f0fba2adSLiam Girdwood 
53384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
548a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
558a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56384c89e2SMark Brown #endif
57384c89e2SMark Brown 
58c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
5912a48a8cSMark Brown static LIST_HEAD(platform_list);
600d0cf00aSMark Brown static LIST_HEAD(codec_list);
61030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
62c5af3a2eSMark Brown 
63db2a4165SFrank Mandarino /*
64db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
65db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
66db2a4165SFrank Mandarino  * between two audio tracks.
67db2a4165SFrank Mandarino  */
68db2a4165SFrank Mandarino static int pmdown_time = 5000;
69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71db2a4165SFrank Mandarino 
72741a509fSMarkus Pargmann struct snd_ac97_reset_cfg {
73741a509fSMarkus Pargmann 	struct pinctrl *pctl;
74741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_reset;
75741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_warm_reset;
76741a509fSMarkus Pargmann 	struct pinctrl_state *pstate_run;
77741a509fSMarkus Pargmann 	int gpio_sdata;
78741a509fSMarkus Pargmann 	int gpio_sync;
79741a509fSMarkus Pargmann 	int gpio_reset;
80741a509fSMarkus Pargmann };
81741a509fSMarkus Pargmann 
822bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent
832bc9a81eSDimitris Papastamos  * a particular given value */
842bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val)
852bc9a81eSDimitris Papastamos {
862bc9a81eSDimitris Papastamos 	int c = 0;
872bc9a81eSDimitris Papastamos 	int i;
882bc9a81eSDimitris Papastamos 
892bc9a81eSDimitris Papastamos 	for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
902bc9a81eSDimitris Papastamos 		if (val & (1UL << i))
912bc9a81eSDimitris Papastamos 			break;
922bc9a81eSDimitris Papastamos 	c = (sizeof val * 8) - c;
932bc9a81eSDimitris Papastamos 	if (!c || (c % 8))
942bc9a81eSDimitris Papastamos 		c = (c + 8) / 8;
952bc9a81eSDimitris Papastamos 	else
962bc9a81eSDimitris Papastamos 		c /= 8;
972bc9a81eSDimitris Papastamos 	return c;
982bc9a81eSDimitris Papastamos }
992bc9a81eSDimitris Papastamos 
10013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted
10113fd179fSDimitris Papastamos  * string of the form 'reg: value\n' */
10213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec,
10313fd179fSDimitris Papastamos 			       unsigned int reg, char *buf, size_t len)
1042624d5faSMark Brown {
10500b317a4SStephen Warren 	int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
10600b317a4SStephen Warren 	int regsize = codec->driver->reg_word_size * 2;
10713fd179fSDimitris Papastamos 	int ret;
10813fd179fSDimitris Papastamos 	char tmpbuf[len + 1];
10913fd179fSDimitris Papastamos 	char regbuf[regsize + 1];
11013fd179fSDimitris Papastamos 
11113fd179fSDimitris Papastamos 	/* since tmpbuf is allocated on the stack, warn the callers if they
11213fd179fSDimitris Papastamos 	 * try to abuse this function */
11313fd179fSDimitris Papastamos 	WARN_ON(len > 63);
11413fd179fSDimitris Papastamos 
11513fd179fSDimitris Papastamos 	/* +2 for ': ' and + 1 for '\n' */
11613fd179fSDimitris Papastamos 	if (wordsize + regsize + 2 + 1 != len)
11713fd179fSDimitris Papastamos 		return -EINVAL;
11813fd179fSDimitris Papastamos 
11913fd179fSDimitris Papastamos 	ret = snd_soc_read(codec, reg);
12013fd179fSDimitris Papastamos 	if (ret < 0) {
12113fd179fSDimitris Papastamos 		memset(regbuf, 'X', regsize);
12213fd179fSDimitris Papastamos 		regbuf[regsize] = '\0';
12313fd179fSDimitris Papastamos 	} else {
12413fd179fSDimitris Papastamos 		snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
12513fd179fSDimitris Papastamos 	}
12613fd179fSDimitris Papastamos 
12713fd179fSDimitris Papastamos 	/* prepare the buffer */
12813fd179fSDimitris Papastamos 	snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
12913fd179fSDimitris Papastamos 	/* copy it back to the caller without the '\0' */
13013fd179fSDimitris Papastamos 	memcpy(buf, tmpbuf, len);
13113fd179fSDimitris Papastamos 
13213fd179fSDimitris Papastamos 	return 0;
13313fd179fSDimitris Papastamos }
13413fd179fSDimitris Papastamos 
13513fd179fSDimitris Papastamos /* codec register dump */
13613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
13713fd179fSDimitris Papastamos 				  size_t count, loff_t pos)
13813fd179fSDimitris Papastamos {
13913fd179fSDimitris Papastamos 	int i, step = 1;
1402bc9a81eSDimitris Papastamos 	int wordsize, regsize;
14113fd179fSDimitris Papastamos 	int len;
14213fd179fSDimitris Papastamos 	size_t total = 0;
14313fd179fSDimitris Papastamos 	loff_t p = 0;
1442bc9a81eSDimitris Papastamos 
14500b317a4SStephen Warren 	wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
14600b317a4SStephen Warren 	regsize = codec->driver->reg_word_size * 2;
1472624d5faSMark Brown 
14813fd179fSDimitris Papastamos 	len = wordsize + regsize + 2 + 1;
14913fd179fSDimitris Papastamos 
150f0fba2adSLiam Girdwood 	if (!codec->driver->reg_cache_size)
1512624d5faSMark Brown 		return 0;
1522624d5faSMark Brown 
153f0fba2adSLiam Girdwood 	if (codec->driver->reg_cache_step)
154f0fba2adSLiam Girdwood 		step = codec->driver->reg_cache_step;
1552624d5faSMark Brown 
156f0fba2adSLiam Girdwood 	for (i = 0; i < codec->driver->reg_cache_size; i += step) {
15713fd179fSDimitris Papastamos 		/* only support larger than PAGE_SIZE bytes debugfs
15813fd179fSDimitris Papastamos 		 * entries for the default case */
15913fd179fSDimitris Papastamos 		if (p >= pos) {
16013fd179fSDimitris Papastamos 			if (total + len >= count - 1)
1612624d5faSMark Brown 				break;
16213fd179fSDimitris Papastamos 			format_register_str(codec, i, buf + total, len);
16313fd179fSDimitris Papastamos 			total += len;
16413fd179fSDimitris Papastamos 		}
16513fd179fSDimitris Papastamos 		p += len;
16613fd179fSDimitris Papastamos 	}
1672624d5faSMark Brown 
16813fd179fSDimitris Papastamos 	total = min(total, count - 1);
1692624d5faSMark Brown 
17013fd179fSDimitris Papastamos 	return total;
1712624d5faSMark Brown }
17213fd179fSDimitris Papastamos 
1732624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1742624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1752624d5faSMark Brown {
17636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
177f0fba2adSLiam Girdwood 
17813fd179fSDimitris Papastamos 	return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
1792624d5faSMark Brown }
1802624d5faSMark Brown 
1812624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1822624d5faSMark Brown 
183dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
184dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
185dbe21408SMark Brown {
18636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
187dbe21408SMark Brown 
188f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
189dbe21408SMark Brown }
190dbe21408SMark Brown 
191dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
192dbe21408SMark Brown 			       struct device_attribute *attr,
193dbe21408SMark Brown 			       const char *buf, size_t count)
194dbe21408SMark Brown {
19536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
196c593b520SMark Brown 	int ret;
197dbe21408SMark Brown 
198b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
199c593b520SMark Brown 	if (ret)
200c593b520SMark Brown 		return ret;
201dbe21408SMark Brown 
202dbe21408SMark Brown 	return count;
203dbe21408SMark Brown }
204dbe21408SMark Brown 
205dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206dbe21408SMark Brown 
2072624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
2082624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
2092624d5faSMark Brown 				   size_t count, loff_t *ppos)
2102624d5faSMark Brown {
2112624d5faSMark Brown 	ssize_t ret;
2122624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
21313fd179fSDimitris Papastamos 	char *buf;
21413fd179fSDimitris Papastamos 
21513fd179fSDimitris Papastamos 	if (*ppos < 0 || !count)
21613fd179fSDimitris Papastamos 		return -EINVAL;
21713fd179fSDimitris Papastamos 
21813fd179fSDimitris Papastamos 	buf = kmalloc(count, GFP_KERNEL);
2192624d5faSMark Brown 	if (!buf)
2202624d5faSMark Brown 		return -ENOMEM;
22113fd179fSDimitris Papastamos 
22213fd179fSDimitris Papastamos 	ret = soc_codec_reg_show(codec, buf, count, *ppos);
22313fd179fSDimitris Papastamos 	if (ret >= 0) {
22413fd179fSDimitris Papastamos 		if (copy_to_user(user_buf, buf, ret)) {
22513fd179fSDimitris Papastamos 			kfree(buf);
22613fd179fSDimitris Papastamos 			return -EFAULT;
22713fd179fSDimitris Papastamos 		}
22813fd179fSDimitris Papastamos 		*ppos += ret;
22913fd179fSDimitris Papastamos 	}
23013fd179fSDimitris Papastamos 
2312624d5faSMark Brown 	kfree(buf);
2322624d5faSMark Brown 	return ret;
2332624d5faSMark Brown }
2342624d5faSMark Brown 
2352624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
2362624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
2372624d5faSMark Brown {
2382624d5faSMark Brown 	char buf[32];
23934e268d8SStephen Boyd 	size_t buf_size;
2402624d5faSMark Brown 	char *start = buf;
2412624d5faSMark Brown 	unsigned long reg, value;
2422624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
243b785a492SJingoo Han 	int ret;
2442624d5faSMark Brown 
2452624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2462624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2472624d5faSMark Brown 		return -EFAULT;
2482624d5faSMark Brown 	buf[buf_size] = 0;
2492624d5faSMark Brown 
2502624d5faSMark Brown 	while (*start == ' ')
2512624d5faSMark Brown 		start++;
2522624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2532624d5faSMark Brown 	while (*start == ' ')
2542624d5faSMark Brown 		start++;
255b785a492SJingoo Han 	ret = kstrtoul(start, 16, &value);
256b785a492SJingoo Han 	if (ret)
257b785a492SJingoo Han 		return ret;
2580d51a9cbSMark Brown 
2590d51a9cbSMark Brown 	/* Userspace has been fiddling around behind the kernel's back */
260373d4d09SRusty Russell 	add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
2610d51a9cbSMark Brown 
262e4f078d8SDimitris Papastamos 	snd_soc_write(codec, reg, value);
2632624d5faSMark Brown 	return buf_size;
2642624d5faSMark Brown }
2652624d5faSMark Brown 
2662624d5faSMark Brown static const struct file_operations codec_reg_fops = {
267234e3405SStephen Boyd 	.open = simple_open,
2682624d5faSMark Brown 	.read = codec_reg_read_file,
2692624d5faSMark Brown 	.write = codec_reg_write_file,
2706038f373SArnd Bergmann 	.llseek = default_llseek,
2712624d5faSMark Brown };
2722624d5faSMark Brown 
27381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
274e73f3de5SRussell King {
27581c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
27681c7cfd1SLars-Peter Clausen 		char *name;
277e73f3de5SRussell King 
27881c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
27981c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
28081c7cfd1SLars-Peter Clausen 		if (name) {
28181c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
28281c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
28381c7cfd1SLars-Peter Clausen 			kfree(name);
28481c7cfd1SLars-Peter Clausen 		}
28581c7cfd1SLars-Peter Clausen 	} else {
28681c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
28781c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
288e73f3de5SRussell King 	}
289e73f3de5SRussell King 
29081c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
29181c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
29281c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
2932624d5faSMark Brown 		return;
2942624d5faSMark Brown 	}
2952624d5faSMark Brown 
29681c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
29781c7cfd1SLars-Peter Clausen 		component->debugfs_root);
29881c7cfd1SLars-Peter Clausen 
29981c7cfd1SLars-Peter Clausen 	if (component->init_debugfs)
30081c7cfd1SLars-Peter Clausen 		component->init_debugfs(component);
30181c7cfd1SLars-Peter Clausen }
30281c7cfd1SLars-Peter Clausen 
30381c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
30481c7cfd1SLars-Peter Clausen {
30581c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
30681c7cfd1SLars-Peter Clausen }
30781c7cfd1SLars-Peter Clausen 
30881c7cfd1SLars-Peter Clausen static void soc_init_codec_debugfs(struct snd_soc_component *component)
30981c7cfd1SLars-Peter Clausen {
31081c7cfd1SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
31181c7cfd1SLars-Peter Clausen 
31281c7cfd1SLars-Peter Clausen 	debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
313aaee8ef1SMark Brown 			    &codec->cache_sync);
314aaee8ef1SMark Brown 
3152624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
31681c7cfd1SLars-Peter Clausen 						 codec->component.debugfs_root,
3172624d5faSMark Brown 						 codec, &codec_reg_fops);
3182624d5faSMark Brown 	if (!codec->debugfs_reg)
31910e8aa9aSMichał Mirosław 		dev_warn(codec->dev,
32010e8aa9aSMichał Mirosław 			"ASoC: Failed to create codec register debugfs file\n");
321731f1ab2SSebastien Guiriec }
322731f1ab2SSebastien Guiriec 
323c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324c3c5a19aSMark Brown 				    size_t count, loff_t *ppos)
325c3c5a19aSMark Brown {
326c3c5a19aSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3272b194f9dSMark Brown 	ssize_t len, ret = 0;
328c3c5a19aSMark Brown 	struct snd_soc_codec *codec;
329c3c5a19aSMark Brown 
330c3c5a19aSMark Brown 	if (!buf)
331c3c5a19aSMark Brown 		return -ENOMEM;
332c3c5a19aSMark Brown 
3332b194f9dSMark Brown 	list_for_each_entry(codec, &codec_list, list) {
3342b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
335f4333203SLars-Peter Clausen 			       codec->component.name);
3362b194f9dSMark Brown 		if (len >= 0)
3372b194f9dSMark Brown 			ret += len;
3382b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
3392b194f9dSMark Brown 			ret = PAGE_SIZE;
3402b194f9dSMark Brown 			break;
3412b194f9dSMark Brown 		}
3422b194f9dSMark Brown 	}
343c3c5a19aSMark Brown 
344c3c5a19aSMark Brown 	if (ret >= 0)
345c3c5a19aSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
346c3c5a19aSMark Brown 
347c3c5a19aSMark Brown 	kfree(buf);
348c3c5a19aSMark Brown 
349c3c5a19aSMark Brown 	return ret;
350c3c5a19aSMark Brown }
351c3c5a19aSMark Brown 
352c3c5a19aSMark Brown static const struct file_operations codec_list_fops = {
353c3c5a19aSMark Brown 	.read = codec_list_read_file,
354c3c5a19aSMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
355c3c5a19aSMark Brown };
356c3c5a19aSMark Brown 
357f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358f3208780SMark Brown 				  size_t count, loff_t *ppos)
359f3208780SMark Brown {
360f3208780SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3612b194f9dSMark Brown 	ssize_t len, ret = 0;
3621438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
363f3208780SMark Brown 	struct snd_soc_dai *dai;
364f3208780SMark Brown 
365f3208780SMark Brown 	if (!buf)
366f3208780SMark Brown 		return -ENOMEM;
367f3208780SMark Brown 
3681438c2f6SLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
3691438c2f6SLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
3701438c2f6SLars-Peter Clausen 			len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
3711438c2f6SLars-Peter Clausen 				dai->name);
3722b194f9dSMark Brown 			if (len >= 0)
3732b194f9dSMark Brown 				ret += len;
3742b194f9dSMark Brown 			if (ret > PAGE_SIZE) {
3752b194f9dSMark Brown 				ret = PAGE_SIZE;
3762b194f9dSMark Brown 				break;
3772b194f9dSMark Brown 			}
3782b194f9dSMark Brown 		}
3791438c2f6SLars-Peter Clausen 	}
380f3208780SMark Brown 
381f3208780SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
382f3208780SMark Brown 
383f3208780SMark Brown 	kfree(buf);
384f3208780SMark Brown 
385f3208780SMark Brown 	return ret;
386f3208780SMark Brown }
387f3208780SMark Brown 
388f3208780SMark Brown static const struct file_operations dai_list_fops = {
389f3208780SMark Brown 	.read = dai_list_read_file,
390f3208780SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
391f3208780SMark Brown };
392f3208780SMark Brown 
39319c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file,
39419c7ac27SMark Brown 				       char __user *user_buf,
39519c7ac27SMark Brown 				       size_t count, loff_t *ppos)
39619c7ac27SMark Brown {
39719c7ac27SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3982b194f9dSMark Brown 	ssize_t len, ret = 0;
39919c7ac27SMark Brown 	struct snd_soc_platform *platform;
40019c7ac27SMark Brown 
40119c7ac27SMark Brown 	if (!buf)
40219c7ac27SMark Brown 		return -ENOMEM;
40319c7ac27SMark Brown 
4042b194f9dSMark Brown 	list_for_each_entry(platform, &platform_list, list) {
4052b194f9dSMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
406f4333203SLars-Peter Clausen 			       platform->component.name);
4072b194f9dSMark Brown 		if (len >= 0)
4082b194f9dSMark Brown 			ret += len;
4092b194f9dSMark Brown 		if (ret > PAGE_SIZE) {
4102b194f9dSMark Brown 			ret = PAGE_SIZE;
4112b194f9dSMark Brown 			break;
4122b194f9dSMark Brown 		}
4132b194f9dSMark Brown 	}
41419c7ac27SMark Brown 
41519c7ac27SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
41619c7ac27SMark Brown 
41719c7ac27SMark Brown 	kfree(buf);
41819c7ac27SMark Brown 
41919c7ac27SMark Brown 	return ret;
42019c7ac27SMark Brown }
42119c7ac27SMark Brown 
42219c7ac27SMark Brown static const struct file_operations platform_list_fops = {
42319c7ac27SMark Brown 	.read = platform_list_read_file,
42419c7ac27SMark Brown 	.llseek = default_llseek,/* read accesses f_pos */
42519c7ac27SMark Brown };
42619c7ac27SMark Brown 
427a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
428a6052154SJarkko Nikula {
429a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
4308a9dab1aSMark Brown 						     snd_soc_debugfs_root);
4313a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
432a6052154SJarkko Nikula 		dev_warn(card->dev,
4337c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
4343a45b867SJarkko Nikula 		return;
4353a45b867SJarkko Nikula 	}
4363a45b867SJarkko Nikula 
4373a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
4383a45b867SJarkko Nikula 						    card->debugfs_card_root,
4393a45b867SJarkko Nikula 						    &card->pop_time);
4403a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
4413a45b867SJarkko Nikula 		dev_warn(card->dev,
442f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
443a6052154SJarkko Nikula }
444a6052154SJarkko Nikula 
445a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
446a6052154SJarkko Nikula {
447a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
448a6052154SJarkko Nikula }
449a6052154SJarkko Nikula 
4502624d5faSMark Brown #else
4512624d5faSMark Brown 
45281c7cfd1SLars-Peter Clausen #define soc_init_codec_debugfs NULL
45381c7cfd1SLars-Peter Clausen 
45481c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
45581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
4562624d5faSMark Brown {
4572624d5faSMark Brown }
4582624d5faSMark Brown 
45981c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
46081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
461731f1ab2SSebastien Guiriec {
462731f1ab2SSebastien Guiriec }
463731f1ab2SSebastien Guiriec 
464b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465b95fccbcSAxel Lin {
466b95fccbcSAxel Lin }
467b95fccbcSAxel Lin 
468b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469b95fccbcSAxel Lin {
470b95fccbcSAxel Lin }
4712624d5faSMark Brown #endif
4722624d5faSMark Brown 
47347c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
47447c88fffSLiam Girdwood 		const char *dai_link, int stream)
47547c88fffSLiam Girdwood {
47647c88fffSLiam Girdwood 	int i;
47747c88fffSLiam Girdwood 
47847c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
47947c88fffSLiam Girdwood 		if (card->rtd[i].dai_link->no_pcm &&
48047c88fffSLiam Girdwood 			!strcmp(card->rtd[i].dai_link->name, dai_link))
48147c88fffSLiam Girdwood 			return card->rtd[i].pcm->streams[stream].substream;
48247c88fffSLiam Girdwood 	}
483f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
48447c88fffSLiam Girdwood 	return NULL;
48547c88fffSLiam Girdwood }
48647c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
48747c88fffSLiam Girdwood 
48847c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
48947c88fffSLiam Girdwood 		const char *dai_link)
49047c88fffSLiam Girdwood {
49147c88fffSLiam Girdwood 	int i;
49247c88fffSLiam Girdwood 
49347c88fffSLiam Girdwood 	for (i = 0; i < card->num_links; i++) {
49447c88fffSLiam Girdwood 		if (!strcmp(card->rtd[i].dai_link->name, dai_link))
49547c88fffSLiam Girdwood 			return &card->rtd[i];
49647c88fffSLiam Girdwood 	}
497f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
49847c88fffSLiam Girdwood 	return NULL;
49947c88fffSLiam Girdwood }
50047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
50147c88fffSLiam Girdwood 
502db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
503db2a4165SFrank Mandarino /* unregister ac97 codec */
504db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
505db2a4165SFrank Mandarino {
506db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
507db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
508db2a4165SFrank Mandarino 	return 0;
509db2a4165SFrank Mandarino }
510db2a4165SFrank Mandarino 
511db2a4165SFrank Mandarino /* stop no dev release warning */
512db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
513db2a4165SFrank Mandarino 
514db2a4165SFrank Mandarino /* register ac97 codec to bus */
515db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
516db2a4165SFrank Mandarino {
517db2a4165SFrank Mandarino 	int err;
518db2a4165SFrank Mandarino 
519db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
52000200107SLars-Peter Clausen 	codec->ac97->dev.parent = codec->component.card->dev;
521db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
522db2a4165SFrank Mandarino 
523bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
52400200107SLars-Peter Clausen 		     codec->component.card->snd_card->number, 0,
52500200107SLars-Peter Clausen 		     codec->component.name);
526db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
527db2a4165SFrank Mandarino 	if (err < 0) {
528f110bfc7SLiam Girdwood 		dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
529db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
530db2a4165SFrank Mandarino 		return err;
531db2a4165SFrank Mandarino 	}
532db2a4165SFrank Mandarino 	return 0;
533db2a4165SFrank Mandarino }
534db2a4165SFrank Mandarino #endif
535db2a4165SFrank Mandarino 
5369d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
5379d58a077SRichard Fitzgerald {
5389d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
5399d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
5409d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
5419d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
5429d58a077SRichard Fitzgerald 	 */
5439d58a077SRichard Fitzgerald }
5449d58a077SRichard Fitzgerald 
5456f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
546db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5476f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
548db2a4165SFrank Mandarino {
5496f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
5502eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
55188bd870fSBenoit Cousson 	int i, j;
552db2a4165SFrank Mandarino 
553c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
554c5599b87SLars-Peter Clausen 	if (!card->instantiated)
555e3509ff0SDaniel Mack 		return 0;
556e3509ff0SDaniel Mack 
5576ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
5586ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
5596ed25978SAndy Green 	 */
560f0fba2adSLiam Girdwood 	snd_power_lock(card->snd_card);
561f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
562f0fba2adSLiam Girdwood 	snd_power_unlock(card->snd_card);
5636ed25978SAndy Green 
5646ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
565f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5666ed25978SAndy Green 
567a00f90f9SMark Brown 	/* mute any active DACs */
568f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
5693efab7dcSMark Brown 
570f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5713efab7dcSMark Brown 			continue;
5723efab7dcSMark Brown 
57388bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
57488bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
57588bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
57688bd870fSBenoit Cousson 
577f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
578f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
579db2a4165SFrank Mandarino 		}
58088bd870fSBenoit Cousson 	}
581db2a4165SFrank Mandarino 
5824ccab3e7SLiam Girdwood 	/* suspend all pcms */
583f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
584f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5853efab7dcSMark Brown 			continue;
5863efab7dcSMark Brown 
587f0fba2adSLiam Girdwood 		snd_pcm_suspend_all(card->rtd[i].pcm);
5883efab7dcSMark Brown 	}
5894ccab3e7SLiam Girdwood 
59087506549SMark Brown 	if (card->suspend_pre)
59170b2ac12SMark Brown 		card->suspend_pre(card);
592db2a4165SFrank Mandarino 
593f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
594f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
5953efab7dcSMark Brown 
596f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
5973efab7dcSMark Brown 			continue;
5983efab7dcSMark Brown 
599f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
600f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
601db2a4165SFrank Mandarino 	}
602db2a4165SFrank Mandarino 
603db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
604f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
60588bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
60643829731STejun Heo 		flush_delayed_work(&card->rtd[i].delayed_work);
60788bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
60888bd870fSBenoit Cousson 			codec_dais[j]->codec->dapm.suspend_bias_level =
60988bd870fSBenoit Cousson 					codec_dais[j]->codec->dapm.bias_level;
61088bd870fSBenoit Cousson 		}
611f0fba2adSLiam Girdwood 	}
612db2a4165SFrank Mandarino 
613f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
6143efab7dcSMark Brown 
615f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6163efab7dcSMark Brown 			continue;
6173efab7dcSMark Brown 
6187bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6197bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
620db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
621f0fba2adSLiam Girdwood 
6227bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
6237bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
624db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
625db2a4165SFrank Mandarino 	}
626db2a4165SFrank Mandarino 
6278be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6288be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
629e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
630e2d32ff6SMark Brown 
631f0fba2adSLiam Girdwood 	/* suspend all CODECs */
6322eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
6331547aba9SMark Brown 		/* If there are paths active then the CODEC will be held with
6341547aba9SMark Brown 		 * bias _ON and should not be suspended. */
635a8093297SLars-Peter Clausen 		if (!codec->suspended) {
636ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
6371547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
638125a25daSMark Brown 				/*
639125a25daSMark Brown 				 * If the CODEC is capable of idle
640125a25daSMark Brown 				 * bias off then being in STANDBY
641125a25daSMark Brown 				 * means it's doing something,
642125a25daSMark Brown 				 * otherwise fall through.
643125a25daSMark Brown 				 */
644125a25daSMark Brown 				if (codec->dapm.idle_bias_off) {
645125a25daSMark Brown 					dev_dbg(codec->dev,
64610e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
647125a25daSMark Brown 					break;
648125a25daSMark Brown 				}
649a8093297SLars-Peter Clausen 
6501547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
651a8093297SLars-Peter Clausen 				if (codec->driver->suspend)
65284b315eeSLars-Peter Clausen 					codec->driver->suspend(codec);
653f0fba2adSLiam Girdwood 				codec->suspended = 1;
6547be4ba24SMark Brown 				codec->cache_sync = 1;
655e2c330b9SLars-Peter Clausen 				if (codec->component.regmap)
656e2c330b9SLars-Peter Clausen 					regcache_mark_dirty(codec->component.regmap);
657988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
658988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec->dev);
6591547aba9SMark Brown 				break;
6601547aba9SMark Brown 			default:
66110e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
66210e8aa9aSMichał Mirosław 					"ASoC: CODEC is on over suspend\n");
6631547aba9SMark Brown 				break;
6641547aba9SMark Brown 			}
6651547aba9SMark Brown 		}
666f0fba2adSLiam Girdwood 	}
667db2a4165SFrank Mandarino 
668f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
669f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
6703efab7dcSMark Brown 
671f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
6723efab7dcSMark Brown 			continue;
6733efab7dcSMark Brown 
674f0fba2adSLiam Girdwood 		if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
675f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
676988e8cc4SNicolin Chen 
677988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
678988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
679db2a4165SFrank Mandarino 	}
680db2a4165SFrank Mandarino 
68187506549SMark Brown 	if (card->suspend_post)
68270b2ac12SMark Brown 		card->suspend_post(card);
683db2a4165SFrank Mandarino 
684db2a4165SFrank Mandarino 	return 0;
685db2a4165SFrank Mandarino }
6866f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
687db2a4165SFrank Mandarino 
6886ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
6896ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6906ed25978SAndy Green  */
6916ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
692db2a4165SFrank Mandarino {
693f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
694f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
6952eea392dSJarkko Nikula 	struct snd_soc_codec *codec;
69688bd870fSBenoit Cousson 	int i, j;
697db2a4165SFrank Mandarino 
6986ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6996ed25978SAndy Green 	 * so userspace apps are blocked from touching us
7006ed25978SAndy Green 	 */
7016ed25978SAndy Green 
702f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
7036ed25978SAndy Green 
7049949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
705f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7069949788bSMark Brown 
70787506549SMark Brown 	if (card->resume_pre)
70870b2ac12SMark Brown 		card->resume_pre(card);
709db2a4165SFrank Mandarino 
710f0fba2adSLiam Girdwood 	/* resume AC97 DAIs */
711f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
712f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7133efab7dcSMark Brown 
714f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7153efab7dcSMark Brown 			continue;
7163efab7dcSMark Brown 
717f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
718f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
719db2a4165SFrank Mandarino 	}
720db2a4165SFrank Mandarino 
7212eea392dSJarkko Nikula 	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
7221547aba9SMark Brown 		/* If the CODEC was idle over suspend then it will have been
7231547aba9SMark Brown 		 * left with bias OFF or STANDBY and suspended so we must now
7241547aba9SMark Brown 		 * resume.  Otherwise the suspend was suppressed.
7251547aba9SMark Brown 		 */
726a8093297SLars-Peter Clausen 		if (codec->suspended) {
727ce6120ccSLiam Girdwood 			switch (codec->dapm.bias_level) {
7281547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
7291547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
730a8093297SLars-Peter Clausen 				if (codec->driver->resume)
731f0fba2adSLiam Girdwood 					codec->driver->resume(codec);
732f0fba2adSLiam Girdwood 				codec->suspended = 0;
7331547aba9SMark Brown 				break;
7341547aba9SMark Brown 			default:
73510e8aa9aSMichał Mirosław 				dev_dbg(codec->dev,
73610e8aa9aSMichał Mirosław 					"ASoC: CODEC was on over suspend\n");
7371547aba9SMark Brown 				break;
7381547aba9SMark Brown 			}
7391547aba9SMark Brown 		}
740f0fba2adSLiam Girdwood 	}
741db2a4165SFrank Mandarino 
742f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7433efab7dcSMark Brown 
744f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7453efab7dcSMark Brown 			continue;
7463efab7dcSMark Brown 
7477bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
748d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
749db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
750f0fba2adSLiam Girdwood 
7517bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(&card->rtd[i],
752d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
753db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
754db2a4165SFrank Mandarino 	}
755db2a4165SFrank Mandarino 
7563ff3f64bSMark Brown 	/* unmute any active DACs */
757f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
7583efab7dcSMark Brown 
759f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7603efab7dcSMark Brown 			continue;
7613efab7dcSMark Brown 
76288bd870fSBenoit Cousson 		for (j = 0; j < card->rtd[i].num_codecs; j++) {
76388bd870fSBenoit Cousson 			struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
76488bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
76588bd870fSBenoit Cousson 
766f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
767f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
768db2a4165SFrank Mandarino 		}
76988bd870fSBenoit Cousson 	}
770db2a4165SFrank Mandarino 
771f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
772f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
7733efab7dcSMark Brown 
774f0fba2adSLiam Girdwood 		if (card->rtd[i].dai_link->ignore_suspend)
7753efab7dcSMark Brown 			continue;
7763efab7dcSMark Brown 
777f0fba2adSLiam Girdwood 		if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
778f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
779db2a4165SFrank Mandarino 	}
780db2a4165SFrank Mandarino 
78187506549SMark Brown 	if (card->resume_post)
78270b2ac12SMark Brown 		card->resume_post(card);
783db2a4165SFrank Mandarino 
784f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7856ed25978SAndy Green 
7866ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
787f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
788e2d32ff6SMark Brown 
7898be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7908be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
791e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7926ed25978SAndy Green }
7936ed25978SAndy Green 
7946ed25978SAndy Green /* powers up audio subsystem after a suspend */
7956f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7966ed25978SAndy Green {
7976f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
79882e14e8bSStephen Warren 	int i, ac97_control = 0;
799b9dd94a8SPeter Ujfalusi 
800c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
801c5599b87SLars-Peter Clausen 	if (!card->instantiated)
8025ff1ddf2SEric Miao 		return 0;
8035ff1ddf2SEric Miao 
804988e8cc4SNicolin Chen 	/* activate pins from sleep state */
805988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
80688bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
80788bd870fSBenoit Cousson 		struct snd_soc_dai **codec_dais = rtd->codec_dais;
80888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
80988bd870fSBenoit Cousson 		int j;
81088bd870fSBenoit Cousson 
811988e8cc4SNicolin Chen 		if (cpu_dai->active)
812988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
81388bd870fSBenoit Cousson 
81488bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
81588bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = codec_dais[j];
816988e8cc4SNicolin Chen 			if (codec_dai->active)
817988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
818988e8cc4SNicolin Chen 		}
81988bd870fSBenoit Cousson 	}
820988e8cc4SNicolin Chen 
82164ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
82264ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
82364ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
82464ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
82564ab9baaSMark Brown 	 */
826f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
827f0fba2adSLiam Girdwood 		struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
82882e14e8bSStephen Warren 		ac97_control |= cpu_dai->driver->ac97_control;
82982e14e8bSStephen Warren 	}
83082e14e8bSStephen Warren 	if (ac97_control) {
831f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
83264ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
83364ab9baaSMark Brown 	} else {
834f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8356308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
836f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
837f0fba2adSLiam Girdwood 	}
8386ed25978SAndy Green 
839db2a4165SFrank Mandarino 	return 0;
840db2a4165SFrank Mandarino }
8416f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
842db2a4165SFrank Mandarino #else
8436f8ab4acSMark Brown #define snd_soc_suspend NULL
8446f8ab4acSMark Brown #define snd_soc_resume NULL
845db2a4165SFrank Mandarino #endif
846db2a4165SFrank Mandarino 
84785e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
84802a06d30SBarry Song };
84902a06d30SBarry Song 
85065d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
85165d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
85212023a9aSMisael Lopez Cruz {
85365d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
85412023a9aSMisael Lopez Cruz 
85565d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
85665d9361fSLars-Peter Clausen 		if (of_node) {
85765d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
85865d9361fSLars-Peter Clausen 				return component;
85965d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
86065d9361fSLars-Peter Clausen 			return component;
86112023a9aSMisael Lopez Cruz 		}
86212023a9aSMisael Lopez Cruz 	}
86312023a9aSMisael Lopez Cruz 
86412023a9aSMisael Lopez Cruz 	return NULL;
86512023a9aSMisael Lopez Cruz }
86612023a9aSMisael Lopez Cruz 
86714621c7eSLars-Peter Clausen static struct snd_soc_dai *snd_soc_find_dai(
86814621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
86912023a9aSMisael Lopez Cruz {
87014621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
87114621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
87212023a9aSMisael Lopez Cruz 
87314621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
87414621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
87514621c7eSLars-Peter Clausen 		if (dlc->of_node && component->dev->of_node != dlc->of_node)
87612023a9aSMisael Lopez Cruz 			continue;
87714621c7eSLars-Peter Clausen 		if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
87812023a9aSMisael Lopez Cruz 			continue;
87914621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
88014621c7eSLars-Peter Clausen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
88114621c7eSLars-Peter Clausen 				continue;
88212023a9aSMisael Lopez Cruz 
88314621c7eSLars-Peter Clausen 			return dai;
88412023a9aSMisael Lopez Cruz 		}
88512023a9aSMisael Lopez Cruz 	}
88612023a9aSMisael Lopez Cruz 
88712023a9aSMisael Lopez Cruz 	return NULL;
88812023a9aSMisael Lopez Cruz }
88912023a9aSMisael Lopez Cruz 
890f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num)
891db2a4165SFrank Mandarino {
892f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
893f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
89488bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
89514621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
89688bd870fSBenoit Cousson 	struct snd_soc_dai **codec_dais = rtd->codec_dais;
897435c5e25SMark Brown 	struct snd_soc_platform *platform;
898848dd8beSMark Brown 	const char *platform_name;
89988bd870fSBenoit Cousson 	int i;
900db2a4165SFrank Mandarino 
901f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
9026308419aSMark Brown 
90314621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
90414621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
90514621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
90614621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
907b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
908f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
909f0fba2adSLiam Girdwood 			dai_link->cpu_dai_name);
910b19e6e7bSMark Brown 		return -EPROBE_DEFER;
911c5af3a2eSMark Brown 	}
912c5af3a2eSMark Brown 
91388bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
91488bd870fSBenoit Cousson 
91588bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
91688bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
91714621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
91888bd870fSBenoit Cousson 		if (!codec_dais[i]) {
91912023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
92088bd870fSBenoit Cousson 				codecs[i].dai_name);
92112023a9aSMisael Lopez Cruz 			return -EPROBE_DEFER;
92212023a9aSMisael Lopez Cruz 		}
92388bd870fSBenoit Cousson 	}
92488bd870fSBenoit Cousson 
92588bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
92688bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
92788bd870fSBenoit Cousson 	rtd->codec = rtd->codec_dai->codec;
92812023a9aSMisael Lopez Cruz 
929848dd8beSMark Brown 	/* if there's no platform we match on the empty platform */
930848dd8beSMark Brown 	platform_name = dai_link->platform_name;
9315a504963SStephen Warren 	if (!platform_name && !dai_link->platform_of_node)
932848dd8beSMark Brown 		platform_name = "snd-soc-dummy";
933848dd8beSMark Brown 
934b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
935f0fba2adSLiam Girdwood 	list_for_each_entry(platform, &platform_list, list) {
9365a504963SStephen Warren 		if (dai_link->platform_of_node) {
9375a504963SStephen Warren 			if (platform->dev->of_node !=
9385a504963SStephen Warren 			    dai_link->platform_of_node)
9395a504963SStephen Warren 				continue;
9405a504963SStephen Warren 		} else {
941f4333203SLars-Peter Clausen 			if (strcmp(platform->component.name, platform_name))
9422610ab77SStephen Warren 				continue;
9435a504963SStephen Warren 		}
9442610ab77SStephen Warren 
945f0fba2adSLiam Girdwood 		rtd->platform = platform;
946f0fba2adSLiam Girdwood 	}
947b19e6e7bSMark Brown 	if (!rtd->platform) {
948f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: platform %s not registered\n",
949f0fba2adSLiam Girdwood 			dai_link->platform_name);
950b19e6e7bSMark Brown 		return -EPROBE_DEFER;
951f0fba2adSLiam Girdwood 	}
952b19e6e7bSMark Brown 
953b19e6e7bSMark Brown 	card->num_rtd++;
954b19e6e7bSMark Brown 
955b19e6e7bSMark Brown 	return 0;
9566b05eda6SMark Brown }
9576b05eda6SMark Brown 
958f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
959d12cd198SStephen Warren {
96070090bbbSLars-Peter Clausen 	if (!component->probed)
96170090bbbSLars-Peter Clausen 		return;
962d12cd198SStephen Warren 
963f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
964f1d45cc3SLars-Peter Clausen 	if (component->codec)
965f1d45cc3SLars-Peter Clausen 		list_del(&component->codec->card_list);
966d12cd198SStephen Warren 
967f1d45cc3SLars-Peter Clausen 	if (component->remove)
968f1d45cc3SLars-Peter Clausen 		component->remove(component);
969d12cd198SStephen Warren 
970f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
971d12cd198SStephen Warren 
972f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
973f1d45cc3SLars-Peter Clausen 	component->probed = 0;
974f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
975d12cd198SStephen Warren }
976d12cd198SStephen Warren 
977e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
978589c3563SJarkko Nikula {
979589c3563SJarkko Nikula 	int err;
980589c3563SJarkko Nikula 
981e60cd14fSLars-Peter Clausen 	if (dai && dai->probed &&
982e60cd14fSLars-Peter Clausen 			dai->driver->remove_order == order) {
983e60cd14fSLars-Peter Clausen 		if (dai->driver->remove) {
984e60cd14fSLars-Peter Clausen 			err = dai->driver->remove(dai);
985589c3563SJarkko Nikula 			if (err < 0)
986e60cd14fSLars-Peter Clausen 				dev_err(dai->dev,
987b0aa88afSMisael Lopez Cruz 					"ASoC: failed to remove %s: %d\n",
988e60cd14fSLars-Peter Clausen 					dai->name, err);
989b0aa88afSMisael Lopez Cruz 		}
990e60cd14fSLars-Peter Clausen 		dai->probed = 0;
991b0aa88afSMisael Lopez Cruz 	}
992b0aa88afSMisael Lopez Cruz }
993b0aa88afSMisael Lopez Cruz 
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];
997e60cd14fSLars-Peter Clausen 	int i;
998f0fba2adSLiam Girdwood 
999f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1000f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
100136ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_pmdown_time);
100236ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
100336ae1a96SMark Brown 		device_unregister(rtd->dev);
1004f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
100502a06d30SBarry Song 	}
100602a06d30SBarry Song 
1007f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
100888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1009e60cd14fSLars-Peter Clausen 		soc_remove_dai(rtd->codec_dais[i], order);
1010f0fba2adSLiam Girdwood 
1011e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1012f0fba2adSLiam Girdwood }
1013f0fba2adSLiam Girdwood 
101462ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num,
101562ae68faSStephen Warren 				       int order)
101662ae68faSStephen Warren {
101762ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
101862ae68faSStephen Warren 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
101962ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
102061aca564SLars-Peter Clausen 	struct snd_soc_component *component;
102188bd870fSBenoit Cousson 	int i;
102262ae68faSStephen Warren 
102362ae68faSStephen Warren 	/* remove the platform */
102470090bbbSLars-Peter Clausen 	if (platform && platform->component.driver->remove_order == order)
1025f1d45cc3SLars-Peter Clausen 		soc_remove_component(&platform->component);
102662ae68faSStephen Warren 
102762ae68faSStephen Warren 	/* remove the CODEC-side CODEC */
102888bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
102961aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
103070090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
103161aca564SLars-Peter Clausen 			soc_remove_component(component);
103262ae68faSStephen Warren 	}
103362ae68faSStephen Warren 
103462ae68faSStephen Warren 	/* remove any CPU-side CODEC */
103562ae68faSStephen Warren 	if (cpu_dai) {
103670090bbbSLars-Peter Clausen 		if (cpu_dai->component->driver->remove_order == order)
103761aca564SLars-Peter Clausen 			soc_remove_component(cpu_dai->component);
103862ae68faSStephen Warren 	}
103962ae68faSStephen Warren }
104062ae68faSStephen Warren 
10410671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10420671fd8eSKuninori Morimoto {
10430168bf0dSLiam Girdwood 	int dai, order;
10440671fd8eSKuninori Morimoto 
10450168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
10460168bf0dSLiam Girdwood 			order++) {
10470168bf0dSLiam Girdwood 		for (dai = 0; dai < card->num_rtd; dai++)
104862ae68faSStephen Warren 			soc_remove_link_dais(card, dai, order);
10490168bf0dSLiam Girdwood 	}
105062ae68faSStephen Warren 
105162ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
105262ae68faSStephen Warren 			order++) {
105362ae68faSStephen Warren 		for (dai = 0; dai < card->num_rtd; dai++)
105462ae68faSStephen Warren 			soc_remove_link_components(card, dai, order);
105562ae68faSStephen Warren 	}
105662ae68faSStephen Warren 
10570671fd8eSKuninori Morimoto 	card->num_rtd = 0;
10580671fd8eSKuninori Morimoto }
10590671fd8eSKuninori Morimoto 
1060ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
106194f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1062ead9b919SJarkko Nikula {
1063ead9b919SJarkko Nikula 	int i;
1064ead9b919SJarkko Nikula 
1065ff819b83SDimitris Papastamos 	if (card->codec_conf == NULL)
1066ead9b919SJarkko Nikula 		return;
1067ead9b919SJarkko Nikula 
1068ff819b83SDimitris Papastamos 	for (i = 0; i < card->num_configs; i++) {
1069ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
107094f99c87SLars-Peter Clausen 		if (map->of_node && component->dev->of_node != map->of_node)
10713ca041edSSebastian Reichel 			continue;
107294f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
10733ca041edSSebastian Reichel 			continue;
107494f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1075ead9b919SJarkko Nikula 		break;
1076ead9b919SJarkko Nikula 	}
1077ead9b919SJarkko Nikula }
1078ead9b919SJarkko Nikula 
1079f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1080f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1081589c3563SJarkko Nikula {
1082f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1083888df395SMark Brown 	struct snd_soc_dai *dai;
1084f1d45cc3SLars-Peter Clausen 	int ret;
1085589c3563SJarkko Nikula 
108670090bbbSLars-Peter Clausen 	if (component->probed)
108770090bbbSLars-Peter Clausen 		return 0;
1088589c3563SJarkko Nikula 
1089f1d45cc3SLars-Peter Clausen 	component->card = card;
1090f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1091f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1092589c3563SJarkko Nikula 
1093f1d45cc3SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
109470d29331SJarkko Nikula 		return -ENODEV;
109570d29331SJarkko Nikula 
1096f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1097d5d1e0beSLars-Peter Clausen 
1098f1d45cc3SLars-Peter Clausen 	if (component->dapm_widgets) {
1099f1d45cc3SLars-Peter Clausen 		ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1100f1d45cc3SLars-Peter Clausen 			component->num_dapm_widgets);
110177530150SLars-Peter Clausen 
1102b318ad50SNariman Poushin 		if (ret != 0) {
1103f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1104b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1105b318ad50SNariman Poushin 			goto err_probe;
1106b318ad50SNariman Poushin 		}
1107b318ad50SNariman Poushin 	}
1108b318ad50SNariman Poushin 
11090634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
11100634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1111261edc70SNariman Poushin 		if (ret != 0) {
1112f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1113261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1114261edc70SNariman Poushin 			goto err_probe;
1115261edc70SNariman Poushin 		}
1116261edc70SNariman Poushin 	}
1117888df395SMark Brown 
1118f1d45cc3SLars-Peter Clausen 	if (component->probe) {
1119f1d45cc3SLars-Peter Clausen 		ret = component->probe(component);
1120589c3563SJarkko Nikula 		if (ret < 0) {
1121f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1122f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
112370d29331SJarkko Nikula 			goto err_probe;
1124589c3563SJarkko Nikula 		}
1125cb2cf612SLiam Girdwood 
1126f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1127f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1128956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1129f1d45cc3SLars-Peter Clausen 			component->name);
1130956245e9SLiam Girdwood 	}
1131956245e9SLiam Girdwood 
1132f1d45cc3SLars-Peter Clausen 	if (component->controls)
1133f1d45cc3SLars-Peter Clausen 		snd_soc_add_component_controls(component, component->controls,
1134f1d45cc3SLars-Peter Clausen 				     component->num_controls);
1135f1d45cc3SLars-Peter Clausen 	if (component->dapm_routes)
1136f1d45cc3SLars-Peter Clausen 		snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1137f1d45cc3SLars-Peter Clausen 					component->num_dapm_routes);
1138956245e9SLiam Girdwood 
1139f1d45cc3SLars-Peter Clausen 	component->probed = 1;
1140f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1141f1d45cc3SLars-Peter Clausen 
1142f1d45cc3SLars-Peter Clausen 	/* This is a HACK and will be removed soon */
1143f1d45cc3SLars-Peter Clausen 	if (component->codec)
1144f1d45cc3SLars-Peter Clausen 		list_add(&component->codec->card_list, &card->codec_dev_list);
1145956245e9SLiam Girdwood 
1146956245e9SLiam Girdwood 	return 0;
1147956245e9SLiam Girdwood 
1148956245e9SLiam Girdwood err_probe:
1149f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1150f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1151956245e9SLiam Girdwood 
1152956245e9SLiam Girdwood 	return ret;
1153956245e9SLiam Girdwood }
1154956245e9SLiam Girdwood 
115536ae1a96SMark Brown static void rtd_release(struct device *dev)
115636ae1a96SMark Brown {
115736ae1a96SMark Brown 	kfree(dev);
115836ae1a96SMark Brown }
1159f0fba2adSLiam Girdwood 
11605f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
11615f3484acSLars-Peter Clausen 	const char *name)
1162503ae5e0SMisael Lopez Cruz {
1163589c3563SJarkko Nikula 	int ret = 0;
1164589c3563SJarkko Nikula 
1165589c3563SJarkko Nikula 	/* register the rtd device */
116636ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
116736ae1a96SMark Brown 	if (!rtd->dev)
116836ae1a96SMark Brown 		return -ENOMEM;
116936ae1a96SMark Brown 	device_initialize(rtd->dev);
11705f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
117136ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1172f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
117336ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1174b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
117501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
117601d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
117701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
117801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
117936ae1a96SMark Brown 	ret = device_add(rtd->dev);
1180589c3563SJarkko Nikula 	if (ret < 0) {
1181865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1182865df9cbSChuansheng Liu 		put_device(rtd->dev);
11835f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1184f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1185589c3563SJarkko Nikula 		return ret;
1186589c3563SJarkko Nikula 	}
1187589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1188589c3563SJarkko Nikula 
118993c3ce76SLars-Peter Clausen 	if (rtd->codec) {
1190589c3563SJarkko Nikula 		/* add DAPM sysfs entries for this codec */
119136ae1a96SMark Brown 		ret = snd_soc_dapm_sys_add(rtd->dev);
1192589c3563SJarkko Nikula 		if (ret < 0)
119344c69bb1SLars-Peter Clausen 			dev_err(rtd->dev,
119493c3ce76SLars-Peter Clausen 				"ASoC: failed to add codec dapm sysfs entries: %d\n",
119593c3ce76SLars-Peter Clausen 				ret);
1196589c3563SJarkko Nikula 
1197589c3563SJarkko Nikula 		/* add codec sysfs entries */
119836ae1a96SMark Brown 		ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1199589c3563SJarkko Nikula 		if (ret < 0)
120044c69bb1SLars-Peter Clausen 			dev_err(rtd->dev,
120193c3ce76SLars-Peter Clausen 				"ASoC: failed to add codec sysfs files: %d\n",
120293c3ce76SLars-Peter Clausen 				ret);
120393c3ce76SLars-Peter Clausen 	}
1204589c3563SJarkko Nikula 
1205589c3563SJarkko Nikula 	return 0;
1206589c3563SJarkko Nikula }
1207589c3563SJarkko Nikula 
120862ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num,
120962ae68faSStephen Warren 				     int order)
121062ae68faSStephen Warren {
121162ae68faSStephen Warren 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
121262ae68faSStephen Warren 	struct snd_soc_platform *platform = rtd->platform;
1213f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
121488bd870fSBenoit Cousson 	int i, ret;
121562ae68faSStephen Warren 
121662ae68faSStephen Warren 	/* probe the CPU-side component, if it is a CODEC */
121761aca564SLars-Peter Clausen 	component = rtd->cpu_dai->component;
121870090bbbSLars-Peter Clausen 	if (component->driver->probe_order == order) {
1219f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, component);
122062ae68faSStephen Warren 		if (ret < 0)
122162ae68faSStephen Warren 			return ret;
122262ae68faSStephen Warren 	}
122362ae68faSStephen Warren 
122488bd870fSBenoit Cousson 	/* probe the CODEC-side components */
122588bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
122661aca564SLars-Peter Clausen 		component = rtd->codec_dais[i]->component;
122770090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1228f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
122962ae68faSStephen Warren 			if (ret < 0)
123062ae68faSStephen Warren 				return ret;
123162ae68faSStephen Warren 		}
123288bd870fSBenoit Cousson 	}
123362ae68faSStephen Warren 
123462ae68faSStephen Warren 	/* probe the platform */
123570090bbbSLars-Peter Clausen 	if (platform->component.driver->probe_order == order) {
1236f1d45cc3SLars-Peter Clausen 		ret = soc_probe_component(card, &platform->component);
123762ae68faSStephen Warren 		if (ret < 0)
123862ae68faSStephen Warren 			return ret;
123962ae68faSStephen Warren 	}
124062ae68faSStephen Warren 
124162ae68faSStephen Warren 	return 0;
124262ae68faSStephen Warren }
124362ae68faSStephen Warren 
1244b0aa88afSMisael Lopez Cruz static int soc_probe_codec_dai(struct snd_soc_card *card,
1245b0aa88afSMisael Lopez Cruz 			       struct snd_soc_dai *codec_dai,
1246b0aa88afSMisael Lopez Cruz 			       int order)
1247b0aa88afSMisael Lopez Cruz {
1248b0aa88afSMisael Lopez Cruz 	int ret;
1249b0aa88afSMisael Lopez Cruz 
1250b0aa88afSMisael Lopez Cruz 	if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1251b0aa88afSMisael Lopez Cruz 		if (codec_dai->driver->probe) {
1252b0aa88afSMisael Lopez Cruz 			ret = codec_dai->driver->probe(codec_dai);
1253b0aa88afSMisael Lopez Cruz 			if (ret < 0) {
1254b0aa88afSMisael Lopez Cruz 				dev_err(codec_dai->dev,
1255b0aa88afSMisael Lopez Cruz 					"ASoC: failed to probe CODEC DAI %s: %d\n",
1256b0aa88afSMisael Lopez Cruz 					codec_dai->name, ret);
1257b0aa88afSMisael Lopez Cruz 				return ret;
1258b0aa88afSMisael Lopez Cruz 			}
1259b0aa88afSMisael Lopez Cruz 		}
1260b0aa88afSMisael Lopez Cruz 
1261b0aa88afSMisael Lopez Cruz 		/* mark codec_dai as probed and add to card dai list */
1262b0aa88afSMisael Lopez Cruz 		codec_dai->probed = 1;
1263b0aa88afSMisael Lopez Cruz 	}
1264b0aa88afSMisael Lopez Cruz 
1265b0aa88afSMisael Lopez Cruz 	return 0;
1266b0aa88afSMisael Lopez Cruz }
1267b0aa88afSMisael Lopez Cruz 
12682436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card,
12692436a723SMisael Lopez Cruz 				struct snd_soc_dai_link *dai_link,
12703f901a02SBenoit Cousson 				struct snd_soc_pcm_runtime *rtd)
12712436a723SMisael Lopez Cruz {
12723f901a02SBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
12733f901a02SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
12742436a723SMisael Lopez Cruz 	struct snd_soc_dapm_widget *play_w, *capture_w;
12752436a723SMisael Lopez Cruz 	int ret;
12762436a723SMisael Lopez Cruz 
127788bd870fSBenoit Cousson 	if (rtd->num_codecs > 1)
127888bd870fSBenoit Cousson 		dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
127988bd870fSBenoit Cousson 
12802436a723SMisael Lopez Cruz 	/* link the DAI widgets */
12812436a723SMisael Lopez Cruz 	play_w = codec_dai->playback_widget;
12822436a723SMisael Lopez Cruz 	capture_w = cpu_dai->capture_widget;
12832436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
12842436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
12852436a723SMisael Lopez Cruz 					   capture_w, play_w);
12862436a723SMisael Lopez Cruz 		if (ret != 0) {
12872436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
12882436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
12892436a723SMisael Lopez Cruz 			return ret;
12902436a723SMisael Lopez Cruz 		}
12912436a723SMisael Lopez Cruz 	}
12922436a723SMisael Lopez Cruz 
12932436a723SMisael Lopez Cruz 	play_w = cpu_dai->playback_widget;
12942436a723SMisael Lopez Cruz 	capture_w = codec_dai->capture_widget;
12952436a723SMisael Lopez Cruz 	if (play_w && capture_w) {
12962436a723SMisael Lopez Cruz 		ret = snd_soc_dapm_new_pcm(card, dai_link->params,
12972436a723SMisael Lopez Cruz 					   capture_w, play_w);
12982436a723SMisael Lopez Cruz 		if (ret != 0) {
12992436a723SMisael Lopez Cruz 			dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
13002436a723SMisael Lopez Cruz 				play_w->name, capture_w->name, ret);
13012436a723SMisael Lopez Cruz 			return ret;
13022436a723SMisael Lopez Cruz 		}
13032436a723SMisael Lopez Cruz 	}
13042436a723SMisael Lopez Cruz 
13052436a723SMisael Lopez Cruz 	return 0;
13062436a723SMisael Lopez Cruz }
13072436a723SMisael Lopez Cruz 
130862ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1309f0fba2adSLiam Girdwood {
1310f0fba2adSLiam Girdwood 	struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1311f0fba2adSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1312c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
131388bd870fSBenoit Cousson 	int i, ret;
1314f0fba2adSLiam Girdwood 
1315f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
13160168bf0dSLiam Girdwood 			card->name, num, order);
1317f0fba2adSLiam Girdwood 
1318f0fba2adSLiam Girdwood 	/* set default power off timeout */
1319f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1320f0fba2adSLiam Girdwood 
1321f0fba2adSLiam Girdwood 	/* probe the cpu_dai */
13220168bf0dSLiam Girdwood 	if (!cpu_dai->probed &&
13230168bf0dSLiam Girdwood 			cpu_dai->driver->probe_order == order) {
1324f0fba2adSLiam Girdwood 		if (cpu_dai->driver->probe) {
1325f0fba2adSLiam Girdwood 			ret = cpu_dai->driver->probe(cpu_dai);
1326f0fba2adSLiam Girdwood 			if (ret < 0) {
1327f110bfc7SLiam Girdwood 				dev_err(cpu_dai->dev,
1328f110bfc7SLiam Girdwood 					"ASoC: failed to probe CPU DAI %s: %d\n",
13290837fc62SFabio Estevam 					cpu_dai->name, ret);
1330f0fba2adSLiam Girdwood 				return ret;
1331f0fba2adSLiam Girdwood 			}
1332f0fba2adSLiam Girdwood 		}
1333f0fba2adSLiam Girdwood 		cpu_dai->probed = 1;
1334f0fba2adSLiam Girdwood 	}
1335f0fba2adSLiam Girdwood 
1336f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
133788bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
133888bd870fSBenoit Cousson 		ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1339b0aa88afSMisael Lopez Cruz 		if (ret)
1340f0fba2adSLiam Girdwood 			return ret;
134188bd870fSBenoit Cousson 	}
1342f0fba2adSLiam Girdwood 
13430168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
13440168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
13450168bf0dSLiam Girdwood 		return 0;
13460168bf0dSLiam Girdwood 
13475f3484acSLars-Peter Clausen 	/* do machine specific initialization */
13485f3484acSLars-Peter Clausen 	if (dai_link->init) {
13495f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
13505f3484acSLars-Peter Clausen 		if (ret < 0) {
13515f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
13525f3484acSLars-Peter Clausen 				dai_link->name, ret);
13535f3484acSLars-Peter Clausen 			return ret;
13545f3484acSLars-Peter Clausen 		}
13555f3484acSLars-Peter Clausen 	}
13565f3484acSLars-Peter Clausen 
13575f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1358589c3563SJarkko Nikula 	if (ret)
1359f0fba2adSLiam Girdwood 		return ret;
1360f0fba2adSLiam Girdwood 
13615f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
13625f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
13635f3484acSLars-Peter Clausen 	if (dai_link->dynamic) {
13645f3484acSLars-Peter Clausen 		ret = soc_dpcm_debugfs_add(rtd);
13655f3484acSLars-Peter Clausen 		if (ret < 0) {
13665f3484acSLars-Peter Clausen 			dev_err(rtd->dev,
13675f3484acSLars-Peter Clausen 				"ASoC: failed to add dpcm sysfs entries: %d\n",
13685f3484acSLars-Peter Clausen 				ret);
13695f3484acSLars-Peter Clausen 			return ret;
13705f3484acSLars-Peter Clausen 		}
13715f3484acSLars-Peter Clausen 	}
13725f3484acSLars-Peter Clausen #endif
13735f3484acSLars-Peter Clausen 
137436ae1a96SMark Brown 	ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1375f0fba2adSLiam Girdwood 	if (ret < 0)
1376f110bfc7SLiam Girdwood 		dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1377f110bfc7SLiam Girdwood 			ret);
1378f0fba2adSLiam Girdwood 
13791245b700SNamarta Kohli 	if (cpu_dai->driver->compress_dai) {
13801245b700SNamarta Kohli 		/*create compress_device"*/
13811245b700SNamarta Kohli 		ret = soc_new_compress(rtd, num);
13821245b700SNamarta Kohli 		if (ret < 0) {
1383f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
13841245b700SNamarta Kohli 					 dai_link->stream_name);
13851245b700SNamarta Kohli 			return ret;
13861245b700SNamarta Kohli 		}
13871245b700SNamarta Kohli 	} else {
13881245b700SNamarta Kohli 
1389c74184edSMark Brown 		if (!dai_link->params) {
1390f0fba2adSLiam Girdwood 			/* create the pcm */
1391f0fba2adSLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1392f0fba2adSLiam Girdwood 			if (ret < 0) {
1393f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
13940837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1395f0fba2adSLiam Girdwood 				return ret;
1396f0fba2adSLiam Girdwood 			}
1397c74184edSMark Brown 		} else {
13989d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
13999d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
14009d58a077SRichard Fitzgerald 
1401c74184edSMark Brown 			/* link the DAI widgets */
14023f901a02SBenoit Cousson 			ret = soc_link_dai_widgets(card, dai_link, rtd);
14032436a723SMisael Lopez Cruz 			if (ret)
1404c74184edSMark Brown 				return ret;
1405c74184edSMark Brown 		}
1406c74184edSMark Brown 	}
1407c74184edSMark Brown 
1408f0fba2adSLiam Girdwood 	/* add platform data for AC97 devices */
140988bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
141088bd870fSBenoit Cousson 		if (rtd->codec_dais[i]->driver->ac97_control)
141188bd870fSBenoit Cousson 			snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
141288bd870fSBenoit Cousson 					       rtd->cpu_dai->ac97_pdata);
141388bd870fSBenoit Cousson 	}
1414f0fba2adSLiam Girdwood 
1415f0fba2adSLiam Girdwood 	return 0;
1416f0fba2adSLiam Girdwood }
1417f0fba2adSLiam Girdwood 
1418f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
141902c9c7b9SMisael Lopez Cruz static int soc_register_ac97_codec(struct snd_soc_codec *codec,
142002c9c7b9SMisael Lopez Cruz 				   struct snd_soc_dai *codec_dai)
1421f0fba2adSLiam Girdwood {
1422f0fba2adSLiam Girdwood 	int ret;
1423f0fba2adSLiam Girdwood 
1424f0fba2adSLiam Girdwood 	/* Only instantiate AC97 if not already done by the adaptor
1425f0fba2adSLiam Girdwood 	 * for the generic AC97 subsystem.
14266b05eda6SMark Brown 	 */
142702c9c7b9SMisael Lopez Cruz 	if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
14280562f788SMika Westerberg 		/*
14290562f788SMika Westerberg 		 * It is possible that the AC97 device is already registered to
14300562f788SMika Westerberg 		 * the device subsystem. This happens when the device is created
14310562f788SMika Westerberg 		 * via snd_ac97_mixer(). Currently only SoC codec that does so
14320562f788SMika Westerberg 		 * is the generic AC97 glue but others migh emerge.
14330562f788SMika Westerberg 		 *
14340562f788SMika Westerberg 		 * In those cases we don't try to register the device again.
14350562f788SMika Westerberg 		 */
143602c9c7b9SMisael Lopez Cruz 		if (!codec->ac97_created)
14370562f788SMika Westerberg 			return 0;
1438f0fba2adSLiam Girdwood 
143902c9c7b9SMisael Lopez Cruz 		ret = soc_ac97_dev_register(codec);
1440f0fba2adSLiam Girdwood 		if (ret < 0) {
144102c9c7b9SMisael Lopez Cruz 			dev_err(codec->dev,
1442f110bfc7SLiam Girdwood 				"ASoC: AC97 device register failed: %d\n", ret);
1443f0fba2adSLiam Girdwood 			return ret;
1444435c5e25SMark Brown 		}
1445435c5e25SMark Brown 
144602c9c7b9SMisael Lopez Cruz 		codec->ac97_registered = 1;
1447f0fba2adSLiam Girdwood 	}
1448f0fba2adSLiam Girdwood 	return 0;
1449f0fba2adSLiam Girdwood }
1450435c5e25SMark Brown 
145102c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1452f0fba2adSLiam Girdwood {
1453f0fba2adSLiam Girdwood 	if (codec->ac97_registered) {
1454f0fba2adSLiam Girdwood 		soc_ac97_dev_unregister(codec);
1455f0fba2adSLiam Girdwood 		codec->ac97_registered = 0;
1456f0fba2adSLiam Girdwood 	}
1457f0fba2adSLiam Girdwood }
145802c9c7b9SMisael Lopez Cruz 
145988bd870fSBenoit Cousson static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
146088bd870fSBenoit Cousson {
146188bd870fSBenoit Cousson 	int i, ret;
146288bd870fSBenoit Cousson 
146388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
146488bd870fSBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
146588bd870fSBenoit Cousson 
146688bd870fSBenoit Cousson 		ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
146788bd870fSBenoit Cousson 		if (ret) {
146888bd870fSBenoit Cousson 			while (--i >= 0)
146988bd870fSBenoit Cousson 				soc_unregister_ac97_codec(codec_dai->codec);
147088bd870fSBenoit Cousson 			return ret;
147188bd870fSBenoit Cousson 		}
147288bd870fSBenoit Cousson 	}
147388bd870fSBenoit Cousson 
147488bd870fSBenoit Cousson 	return 0;
147588bd870fSBenoit Cousson }
147688bd870fSBenoit Cousson 
147702c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
147802c9c7b9SMisael Lopez Cruz {
147988bd870fSBenoit Cousson 	int i;
148088bd870fSBenoit Cousson 
148188bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
148288bd870fSBenoit Cousson 		soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
148302c9c7b9SMisael Lopez Cruz }
1484f0fba2adSLiam Girdwood #endif
1485435c5e25SMark Brown 
148644c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1487b19e6e7bSMark Brown {
148844c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
14893ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
149065d9361fSLars-Peter Clausen 	const char *name = aux_dev->codec_name;
14913ca041edSSebastian Reichel 
149265d9361fSLars-Peter Clausen 	rtd->component = soc_find_component(aux_dev->codec_of_node, name);
149365d9361fSLars-Peter Clausen 	if (!rtd->component) {
14943ca041edSSebastian Reichel 		if (aux_dev->codec_of_node)
149565d9361fSLars-Peter Clausen 			name = of_node_full_name(aux_dev->codec_of_node);
14963ca041edSSebastian Reichel 
149765d9361fSLars-Peter Clausen 		dev_err(card->dev, "ASoC: %s not registered\n", name);
1498b19e6e7bSMark Brown 		return -EPROBE_DEFER;
1499b19e6e7bSMark Brown 	}
1500b19e6e7bSMark Brown 
150165d9361fSLars-Peter Clausen 	/*
150265d9361fSLars-Peter Clausen 	 * Some places still reference rtd->codec, so we have to keep that
150365d9361fSLars-Peter Clausen 	 * initialized if the component is a CODEC. Once all those references
150465d9361fSLars-Peter Clausen 	 * have been removed, this code can be removed as well.
150565d9361fSLars-Peter Clausen 	 */
150665d9361fSLars-Peter Clausen 	 rtd->codec = rtd->component->codec;
150765d9361fSLars-Peter Clausen 
150844c69bb1SLars-Peter Clausen 	return 0;
15093ca041edSSebastian Reichel }
15102eea392dSJarkko Nikula 
151144c69bb1SLars-Peter Clausen static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
151244c69bb1SLars-Peter Clausen {
151344c69bb1SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
15145f3484acSLars-Peter Clausen 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
151544c69bb1SLars-Peter Clausen 	int ret;
151644c69bb1SLars-Peter Clausen 
151765d9361fSLars-Peter Clausen 	ret = soc_probe_component(card, rtd->component);
15182eea392dSJarkko Nikula 	if (ret < 0)
1519589c3563SJarkko Nikula 		return ret;
15202eea392dSJarkko Nikula 
15215f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15225f3484acSLars-Peter Clausen 	if (aux_dev->init) {
152357bf7726SLars-Peter Clausen 		ret = aux_dev->init(rtd->component);
15245f3484acSLars-Peter Clausen 		if (ret < 0) {
15255f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15265f3484acSLars-Peter Clausen 				aux_dev->name, ret);
15272eea392dSJarkko Nikula 			return ret;
15282eea392dSJarkko Nikula 		}
15295f3484acSLars-Peter Clausen 	}
15305f3484acSLars-Peter Clausen 
15315f3484acSLars-Peter Clausen 	return soc_post_component_init(rtd, aux_dev->name);
15322eea392dSJarkko Nikula }
15332eea392dSJarkko Nikula 
15342eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
15352eea392dSJarkko Nikula {
15362eea392dSJarkko Nikula 	struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
153765d9361fSLars-Peter Clausen 	struct snd_soc_component *component = rtd->component;
15382eea392dSJarkko Nikula 
15392eea392dSJarkko Nikula 	/* unregister the rtd device */
15402eea392dSJarkko Nikula 	if (rtd->dev_registered) {
154136ae1a96SMark Brown 		device_remove_file(rtd->dev, &dev_attr_codec_reg);
1542d3bf1561SChuansheng Liu 		device_unregister(rtd->dev);
15432eea392dSJarkko Nikula 		rtd->dev_registered = 0;
15442eea392dSJarkko Nikula 	}
15452eea392dSJarkko Nikula 
154665d9361fSLars-Peter Clausen 	if (component && component->probed)
154765d9361fSLars-Peter Clausen 		soc_remove_component(component);
15482eea392dSJarkko Nikula }
15492eea392dSJarkko Nikula 
1550f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1551fdf0f54dSDimitris Papastamos {
1552fdf0f54dSDimitris Papastamos 	int ret;
1553fdf0f54dSDimitris Papastamos 
1554fdf0f54dSDimitris Papastamos 	if (codec->cache_init)
1555fdf0f54dSDimitris Papastamos 		return 0;
1556fdf0f54dSDimitris Papastamos 
1557fdf0f54dSDimitris Papastamos 	ret = snd_soc_cache_init(codec);
1558fdf0f54dSDimitris Papastamos 	if (ret < 0) {
155910e8aa9aSMichał Mirosław 		dev_err(codec->dev,
156010e8aa9aSMichał Mirosław 			"ASoC: Failed to set cache compression type: %d\n",
156110e8aa9aSMichał Mirosław 			ret);
1562fdf0f54dSDimitris Papastamos 		return ret;
1563fdf0f54dSDimitris Papastamos 	}
1564fdf0f54dSDimitris Papastamos 	codec->cache_init = 1;
1565fdf0f54dSDimitris Papastamos 	return 0;
1566fdf0f54dSDimitris Papastamos }
1567fdf0f54dSDimitris Papastamos 
1568b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1569f0fba2adSLiam Girdwood {
1570fdf0f54dSDimitris Papastamos 	struct snd_soc_codec *codec;
157175d9ac46SMark Brown 	struct snd_soc_dai_link *dai_link;
1572f04209a7SMark Brown 	int ret, i, order, dai_fmt;
157396dd3622SMark Brown 
157401b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1575f0fba2adSLiam Girdwood 
1576b19e6e7bSMark Brown 	/* bind DAIs */
1577b19e6e7bSMark Brown 	for (i = 0; i < card->num_links; i++) {
1578b19e6e7bSMark Brown 		ret = soc_bind_dai_link(card, i);
1579b19e6e7bSMark Brown 		if (ret != 0)
1580b19e6e7bSMark Brown 			goto base_error;
1581db2a4165SFrank Mandarino 	}
1582db2a4165SFrank Mandarino 
158344c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1584b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
158544c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1586b19e6e7bSMark Brown 		if (ret != 0)
1587b19e6e7bSMark Brown 			goto base_error;
1588db2a4165SFrank Mandarino 	}
1589db2a4165SFrank Mandarino 
1590fdf0f54dSDimitris Papastamos 	/* initialize the register cache for each available codec */
1591fdf0f54dSDimitris Papastamos 	list_for_each_entry(codec, &codec_list, list) {
1592fdf0f54dSDimitris Papastamos 		if (codec->cache_init)
1593fdf0f54dSDimitris Papastamos 			continue;
1594f90fb3f7SLars-Peter Clausen 		ret = snd_soc_init_codec_cache(codec);
1595b19e6e7bSMark Brown 		if (ret < 0)
1596b19e6e7bSMark Brown 			goto base_error;
1597fdf0f54dSDimitris Papastamos 	}
1598fdf0f54dSDimitris Papastamos 
1599f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1600102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1601f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1602f0fba2adSLiam Girdwood 	if (ret < 0) {
160310e8aa9aSMichał Mirosław 		dev_err(card->dev,
160410e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
160510e8aa9aSMichał Mirosław 			card->name, ret);
1606b19e6e7bSMark Brown 		goto base_error;
1607db2a4165SFrank Mandarino 	}
1608db2a4165SFrank Mandarino 
1609e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1610e37a4970SMark Brown 	card->dapm.dev = card->dev;
1611e37a4970SMark Brown 	card->dapm.card = card;
1612e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1613e37a4970SMark Brown 
1614d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1615d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1616d5d1e0beSLars-Peter Clausen #endif
1617d5d1e0beSLars-Peter Clausen 
161888ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
16196ed25978SAndy Green 	/* deferred resume work */
16206308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
16211301a964SRandy Dunlap #endif
16226ed25978SAndy Green 
16239a841ebbSMark Brown 	if (card->dapm_widgets)
16249a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
16259a841ebbSMark Brown 					  card->num_dapm_widgets);
16269a841ebbSMark Brown 
1627f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1628f0fba2adSLiam Girdwood 	if (card->probe) {
1629e7361ec4SMark Brown 		ret = card->probe(card);
1630f0fba2adSLiam Girdwood 		if (ret < 0)
1631f0fba2adSLiam Girdwood 			goto card_probe_error;
1632f0fba2adSLiam Girdwood 	}
1633f0fba2adSLiam Girdwood 
163462ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
16350168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
16360168bf0dSLiam Girdwood 			order++) {
1637fe3e78e0SMark Brown 		for (i = 0; i < card->num_links; i++) {
163862ae68faSStephen Warren 			ret = soc_probe_link_components(card, i, order);
163962ae68faSStephen Warren 			if (ret < 0) {
1640f110bfc7SLiam Girdwood 				dev_err(card->dev,
1641f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1642f110bfc7SLiam Girdwood 					ret);
164362ae68faSStephen Warren 				goto probe_dai_err;
164462ae68faSStephen Warren 			}
164562ae68faSStephen Warren 		}
164662ae68faSStephen Warren 	}
164762ae68faSStephen Warren 
164862ae68faSStephen Warren 	/* probe all DAI links on this card */
164962ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
165062ae68faSStephen Warren 			order++) {
165162ae68faSStephen Warren 		for (i = 0; i < card->num_links; i++) {
165262ae68faSStephen Warren 			ret = soc_probe_link_dais(card, i, order);
1653fe3e78e0SMark Brown 			if (ret < 0) {
1654f110bfc7SLiam Girdwood 				dev_err(card->dev,
1655f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1656f110bfc7SLiam Girdwood 					ret);
1657f0fba2adSLiam Girdwood 				goto probe_dai_err;
1658fe3e78e0SMark Brown 			}
1659fe3e78e0SMark Brown 		}
16600168bf0dSLiam Girdwood 	}
1661fe3e78e0SMark Brown 
16622eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++) {
16632eea392dSJarkko Nikula 		ret = soc_probe_aux_dev(card, i);
16642eea392dSJarkko Nikula 		if (ret < 0) {
1665f110bfc7SLiam Girdwood 			dev_err(card->dev,
1666f110bfc7SLiam Girdwood 				"ASoC: failed to add auxiliary devices %d\n",
1667f110bfc7SLiam Girdwood 				ret);
16682eea392dSJarkko Nikula 			goto probe_aux_dev_err;
16692eea392dSJarkko Nikula 		}
16702eea392dSJarkko Nikula 	}
16712eea392dSJarkko Nikula 
1672888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1673b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1674888df395SMark Brown 
1675b7af1dafSMark Brown 	if (card->controls)
1676022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
1677b7af1dafSMark Brown 
1678b8ad29deSMark Brown 	if (card->dapm_routes)
1679b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1680b8ad29deSMark Brown 					card->num_dapm_routes);
1681b8ad29deSMark Brown 
168275d9ac46SMark Brown 	for (i = 0; i < card->num_links; i++) {
168388bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
168475d9ac46SMark Brown 		dai_link = &card->dai_link[i];
1685f04209a7SMark Brown 		dai_fmt = dai_link->dai_fmt;
168675d9ac46SMark Brown 
1687f04209a7SMark Brown 		if (dai_fmt) {
168888bd870fSBenoit Cousson 			struct snd_soc_dai **codec_dais = rtd->codec_dais;
168988bd870fSBenoit Cousson 			int j;
169088bd870fSBenoit Cousson 
169188bd870fSBenoit Cousson 			for (j = 0; j < rtd->num_codecs; j++) {
169288bd870fSBenoit Cousson 				struct snd_soc_dai *codec_dai = codec_dais[j];
169388bd870fSBenoit Cousson 
169488bd870fSBenoit Cousson 				ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
16955e4ba569SShawn Guo 				if (ret != 0 && ret != -ENOTSUPP)
169688bd870fSBenoit Cousson 					dev_warn(codec_dai->dev,
1697f110bfc7SLiam Girdwood 						 "ASoC: Failed to set DAI format: %d\n",
169875d9ac46SMark Brown 						 ret);
1699f04209a7SMark Brown 			}
170088bd870fSBenoit Cousson 		}
1701f04209a7SMark Brown 
1702f04209a7SMark Brown 		/* If this is a regular CPU link there will be a platform */
1703fe33d4c5SStephen Warren 		if (dai_fmt &&
1704fe33d4c5SStephen Warren 		    (dai_link->platform_name || dai_link->platform_of_node)) {
1705f04209a7SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1706f04209a7SMark Brown 						  dai_fmt);
1707f04209a7SMark Brown 			if (ret != 0 && ret != -ENOTSUPP)
1708f04209a7SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1709f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
1710f04209a7SMark Brown 					 ret);
1711f04209a7SMark Brown 		} else if (dai_fmt) {
1712f04209a7SMark Brown 			/* Flip the polarity for the "CPU" end */
1713f04209a7SMark Brown 			dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1714f04209a7SMark Brown 			switch (dai_link->dai_fmt &
1715f04209a7SMark Brown 				SND_SOC_DAIFMT_MASTER_MASK) {
1716f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFM:
1717f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1718f04209a7SMark Brown 				break;
1719f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBM_CFS:
1720f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1721f04209a7SMark Brown 				break;
1722f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFM:
1723f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1724f04209a7SMark Brown 				break;
1725f04209a7SMark Brown 			case SND_SOC_DAIFMT_CBS_CFS:
1726f04209a7SMark Brown 				dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1727f04209a7SMark Brown 				break;
1728f04209a7SMark Brown 			}
172975d9ac46SMark Brown 
173075d9ac46SMark Brown 			ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1731f04209a7SMark Brown 						  dai_fmt);
17325e4ba569SShawn Guo 			if (ret != 0 && ret != -ENOTSUPP)
173375d9ac46SMark Brown 				dev_warn(card->rtd[i].cpu_dai->dev,
1734f110bfc7SLiam Girdwood 					 "ASoC: Failed to set DAI format: %d\n",
173575d9ac46SMark Brown 					 ret);
173675d9ac46SMark Brown 		}
173775d9ac46SMark Brown 	}
173875d9ac46SMark Brown 
1739f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1740fe3e78e0SMark Brown 		 "%s", card->name);
1741f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
174222de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
1743f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1744f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
1745f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1746f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
1747f0e8ed85SMark Brown 		case '_':
1748f0e8ed85SMark Brown 		case '-':
1749f0e8ed85SMark Brown 		case '\0':
1750f0e8ed85SMark Brown 			break;
1751f0e8ed85SMark Brown 		default:
1752f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
1753f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
1754f0e8ed85SMark Brown 			break;
1755f0e8ed85SMark Brown 		}
1756f0e8ed85SMark Brown 	}
1757fe3e78e0SMark Brown 
175828e9ad92SMark Brown 	if (card->late_probe) {
175928e9ad92SMark Brown 		ret = card->late_probe(card);
176028e9ad92SMark Brown 		if (ret < 0) {
1761f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
176228e9ad92SMark Brown 				card->name, ret);
176328e9ad92SMark Brown 			goto probe_aux_dev_err;
176428e9ad92SMark Brown 		}
176528e9ad92SMark Brown 	}
176628e9ad92SMark Brown 
17671633281bSStephen Warren 	if (card->fully_routed)
17687df37884SLars-Peter Clausen 		snd_soc_dapm_auto_nc_pins(card);
17691633281bSStephen Warren 
1770824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
17718c193b8dSLars-Peter Clausen 
1772f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
1773fe3e78e0SMark Brown 	if (ret < 0) {
1774f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1775f110bfc7SLiam Girdwood 				ret);
17766b3ed785SAxel Lin 		goto probe_aux_dev_err;
1777fe3e78e0SMark Brown 	}
1778fe3e78e0SMark Brown 
1779fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1780f0fba2adSLiam Girdwood 	/* register any AC97 codecs */
1781f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1782f0fba2adSLiam Girdwood 		ret = soc_register_ac97_dai_link(&card->rtd[i]);
1783fe3e78e0SMark Brown 		if (ret < 0) {
178410e8aa9aSMichał Mirosław 			dev_err(card->dev,
178510e8aa9aSMichał Mirosław 				"ASoC: failed to register AC97: %d\n", ret);
17866b3ed785SAxel Lin 			while (--i >= 0)
178702c9c7b9SMisael Lopez Cruz 				soc_unregister_ac97_dai_link(&card->rtd[i]);
17886b3ed785SAxel Lin 			goto probe_aux_dev_err;
1789fe3e78e0SMark Brown 		}
1790fe3e78e0SMark Brown 	}
1791fe3e78e0SMark Brown #endif
1792fe3e78e0SMark Brown 
1793435c5e25SMark Brown 	card->instantiated = 1;
17944f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
1795f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1796b19e6e7bSMark Brown 
1797b19e6e7bSMark Brown 	return 0;
1798db2a4165SFrank Mandarino 
17992eea392dSJarkko Nikula probe_aux_dev_err:
18002eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18012eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18022eea392dSJarkko Nikula 
1803f0fba2adSLiam Girdwood probe_dai_err:
18040671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1805fe3e78e0SMark Brown 
1806f0fba2adSLiam Girdwood card_probe_error:
180787506549SMark Brown 	if (card->remove)
1808e7361ec4SMark Brown 		card->remove(card);
1809f0fba2adSLiam Girdwood 
1810f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1811f0fba2adSLiam Girdwood 
1812b19e6e7bSMark Brown base_error:
1813f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
1814db2a4165SFrank Mandarino 
1815b19e6e7bSMark Brown 	return ret;
1816435c5e25SMark Brown }
1817435c5e25SMark Brown 
1818435c5e25SMark Brown /* probes a new socdev */
1819435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1820435c5e25SMark Brown {
1821f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1822435c5e25SMark Brown 
182370a7ca34SVinod Koul 	/*
182470a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
182570a7ca34SVinod Koul 	 * we should not be here in that case so ret error
182670a7ca34SVinod Koul 	 */
182770a7ca34SVinod Koul 	if (!card)
182870a7ca34SVinod Koul 		return -EINVAL;
182970a7ca34SVinod Koul 
1830fe4085e8SMark Brown 	dev_warn(&pdev->dev,
1831f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
1832fe4085e8SMark Brown 		 card->name);
1833fe4085e8SMark Brown 
1834435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1835435c5e25SMark Brown 	card->dev = &pdev->dev;
1836f0fba2adSLiam Girdwood 
183728d528c8SMark Brown 	return snd_soc_register_card(card);
1838435c5e25SMark Brown }
1839435c5e25SMark Brown 
1840b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
1841db2a4165SFrank Mandarino {
1842db2a4165SFrank Mandarino 	int i;
1843db2a4165SFrank Mandarino 
1844f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
1845f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1846f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
184743829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1848db2a4165SFrank Mandarino 	}
1849db2a4165SFrank Mandarino 
18502eea392dSJarkko Nikula 	/* remove auxiliary devices */
18512eea392dSJarkko Nikula 	for (i = 0; i < card->num_aux_devs; i++)
18522eea392dSJarkko Nikula 		soc_remove_aux_dev(card, i);
18532eea392dSJarkko Nikula 
1854f0fba2adSLiam Girdwood 	/* remove and free each DAI */
18550671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
1856f0fba2adSLiam Girdwood 
1857a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
1858a6052154SJarkko Nikula 
1859f0fba2adSLiam Girdwood 	/* remove the card */
186087506549SMark Brown 	if (card->remove)
1861e7361ec4SMark Brown 		card->remove(card);
1862f0fba2adSLiam Girdwood 
18630aaae527SLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
18640aaae527SLars-Peter Clausen 
1865f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
1866b0e26485SVinod Koul 	return 0;
1867b0e26485SVinod Koul 
1868b2dfa62cSGuennadi Liakhovetski }
1869b0e26485SVinod Koul 
1870b0e26485SVinod Koul /* removes a socdev */
1871b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
1872b0e26485SVinod Koul {
1873b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1874b0e26485SVinod Koul 
1875c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1876db2a4165SFrank Mandarino 	return 0;
1877db2a4165SFrank Mandarino }
1878db2a4165SFrank Mandarino 
18796f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
188051737470SMark Brown {
18816f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
1882f0fba2adSLiam Girdwood 	int i;
188351737470SMark Brown 
188451737470SMark Brown 	if (!card->instantiated)
1885416356fcSMark Brown 		return 0;
188651737470SMark Brown 
188751737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
188851737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
1889f0fba2adSLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
1890f0fba2adSLiam Girdwood 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
189143829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
1892f0fba2adSLiam Girdwood 	}
189351737470SMark Brown 
1894f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
1895416356fcSMark Brown 
1896988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
1897988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
189888bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
189988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
190088bd870fSBenoit Cousson 		int j;
190188bd870fSBenoit Cousson 
1902988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
190388bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
190488bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
190588bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
190688bd870fSBenoit Cousson 		}
1907988e8cc4SNicolin Chen 	}
1908988e8cc4SNicolin Chen 
1909416356fcSMark Brown 	return 0;
191051737470SMark Brown }
19116f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
191251737470SMark Brown 
19136f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
1914b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
1915b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
1916b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
1917b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
19186f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
1919b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
1920416356fcSMark Brown };
1921deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1922416356fcSMark Brown 
1923db2a4165SFrank Mandarino /* ASoC platform driver */
1924db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1925db2a4165SFrank Mandarino 	.driver		= {
1926db2a4165SFrank Mandarino 		.name		= "soc-audio",
19278b45a209SKay Sievers 		.owner		= THIS_MODULE,
19286f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
1929db2a4165SFrank Mandarino 	},
1930db2a4165SFrank Mandarino 	.probe		= soc_probe,
1931db2a4165SFrank Mandarino 	.remove		= soc_remove,
1932db2a4165SFrank Mandarino };
1933db2a4165SFrank Mandarino 
1934096e49d5SMark Brown /**
1935db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1936db2a4165SFrank Mandarino  * @codec: audio codec
1937db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1938db2a4165SFrank Mandarino  * @num: AC97 codec number
1939db2a4165SFrank Mandarino  *
1940db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1941db2a4165SFrank Mandarino  */
1942db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1943db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1944db2a4165SFrank Mandarino {
1945db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1946e3f205a7SLars-Peter Clausen 	if (codec->ac97 == NULL)
1947db2a4165SFrank Mandarino 		return -ENOMEM;
1948db2a4165SFrank Mandarino 
1949db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1950db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1951db2a4165SFrank Mandarino 		kfree(codec->ac97);
1952db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1953db2a4165SFrank Mandarino 		return -ENOMEM;
1954db2a4165SFrank Mandarino 	}
1955db2a4165SFrank Mandarino 
1956db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1957db2a4165SFrank Mandarino 	codec->ac97->num = num;
19580562f788SMika Westerberg 
19590562f788SMika Westerberg 	/*
19600562f788SMika Westerberg 	 * Mark the AC97 device to be created by us. This way we ensure that the
19610562f788SMika Westerberg 	 * device will be registered with the device subsystem later on.
19620562f788SMika Westerberg 	 */
19630562f788SMika Westerberg 	codec->ac97_created = 1;
19640562f788SMika Westerberg 
1965db2a4165SFrank Mandarino 	return 0;
1966db2a4165SFrank Mandarino }
1967db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1968db2a4165SFrank Mandarino 
1969741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
1970741a509fSMarkus Pargmann 
1971741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
1972741a509fSMarkus Pargmann {
1973741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1974741a509fSMarkus Pargmann 
1975741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
1976741a509fSMarkus Pargmann 
1977741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
1978741a509fSMarkus Pargmann 
1979741a509fSMarkus Pargmann 	udelay(10);
1980741a509fSMarkus Pargmann 
1981741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1982741a509fSMarkus Pargmann 
1983741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1984741a509fSMarkus Pargmann 	msleep(2);
1985741a509fSMarkus Pargmann }
1986741a509fSMarkus Pargmann 
1987741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
1988741a509fSMarkus Pargmann {
1989741a509fSMarkus Pargmann 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1990741a509fSMarkus Pargmann 
1991741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
1992741a509fSMarkus Pargmann 
1993741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1994741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
1995741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
1996741a509fSMarkus Pargmann 
1997741a509fSMarkus Pargmann 	udelay(10);
1998741a509fSMarkus Pargmann 
1999741a509fSMarkus Pargmann 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2000741a509fSMarkus Pargmann 
2001741a509fSMarkus Pargmann 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2002741a509fSMarkus Pargmann 	msleep(2);
2003741a509fSMarkus Pargmann }
2004741a509fSMarkus Pargmann 
2005741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev,
2006741a509fSMarkus Pargmann 		struct snd_ac97_reset_cfg *cfg)
2007741a509fSMarkus Pargmann {
2008741a509fSMarkus Pargmann 	struct pinctrl *p;
2009741a509fSMarkus Pargmann 	struct pinctrl_state *state;
2010741a509fSMarkus Pargmann 	int gpio;
2011741a509fSMarkus Pargmann 	int ret;
2012741a509fSMarkus Pargmann 
2013741a509fSMarkus Pargmann 	p = devm_pinctrl_get(dev);
2014741a509fSMarkus Pargmann 	if (IS_ERR(p)) {
2015741a509fSMarkus Pargmann 		dev_err(dev, "Failed to get pinctrl\n");
2016b7580cdeSChristoph Jaeger 		return PTR_ERR(p);
2017741a509fSMarkus Pargmann 	}
2018741a509fSMarkus Pargmann 	cfg->pctl = p;
2019741a509fSMarkus Pargmann 
2020741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-reset");
2021741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2022741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2023b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2024741a509fSMarkus Pargmann 	}
2025741a509fSMarkus Pargmann 	cfg->pstate_reset = state;
2026741a509fSMarkus Pargmann 
2027741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
2028741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2029741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2030b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2031741a509fSMarkus Pargmann 	}
2032741a509fSMarkus Pargmann 	cfg->pstate_warm_reset = state;
2033741a509fSMarkus Pargmann 
2034741a509fSMarkus Pargmann 	state = pinctrl_lookup_state(p, "ac97-running");
2035741a509fSMarkus Pargmann 	if (IS_ERR(state)) {
2036741a509fSMarkus Pargmann 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
2037b7580cdeSChristoph Jaeger 		return PTR_ERR(state);
2038741a509fSMarkus Pargmann 	}
2039741a509fSMarkus Pargmann 	cfg->pstate_run = state;
2040741a509fSMarkus Pargmann 
2041741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2042741a509fSMarkus Pargmann 	if (gpio < 0) {
2043741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sync gpio\n");
2044741a509fSMarkus Pargmann 		return gpio;
2045741a509fSMarkus Pargmann 	}
2046741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2047741a509fSMarkus Pargmann 	if (ret) {
2048741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
2049741a509fSMarkus Pargmann 		return ret;
2050741a509fSMarkus Pargmann 	}
2051741a509fSMarkus Pargmann 	cfg->gpio_sync = gpio;
2052741a509fSMarkus Pargmann 
2053741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2054741a509fSMarkus Pargmann 	if (gpio < 0) {
2055741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2056741a509fSMarkus Pargmann 		return gpio;
2057741a509fSMarkus Pargmann 	}
2058741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2059741a509fSMarkus Pargmann 	if (ret) {
2060741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2061741a509fSMarkus Pargmann 		return ret;
2062741a509fSMarkus Pargmann 	}
2063741a509fSMarkus Pargmann 	cfg->gpio_sdata = gpio;
2064741a509fSMarkus Pargmann 
2065741a509fSMarkus Pargmann 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2066741a509fSMarkus Pargmann 	if (gpio < 0) {
2067741a509fSMarkus Pargmann 		dev_err(dev, "Can't find ac97-reset gpio\n");
2068741a509fSMarkus Pargmann 		return gpio;
2069741a509fSMarkus Pargmann 	}
2070741a509fSMarkus Pargmann 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2071741a509fSMarkus Pargmann 	if (ret) {
2072741a509fSMarkus Pargmann 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
2073741a509fSMarkus Pargmann 		return ret;
2074741a509fSMarkus Pargmann 	}
2075741a509fSMarkus Pargmann 	cfg->gpio_reset = gpio;
2076741a509fSMarkus Pargmann 
2077741a509fSMarkus Pargmann 	return 0;
2078741a509fSMarkus Pargmann }
2079741a509fSMarkus Pargmann 
2080b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops;
2081f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops);
2082b047e1ccSMark Brown 
2083b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2084b047e1ccSMark Brown {
2085b047e1ccSMark Brown 	if (ops == soc_ac97_ops)
2086b047e1ccSMark Brown 		return 0;
2087b047e1ccSMark Brown 
2088b047e1ccSMark Brown 	if (soc_ac97_ops && ops)
2089b047e1ccSMark Brown 		return -EBUSY;
2090b047e1ccSMark Brown 
2091b047e1ccSMark Brown 	soc_ac97_ops = ops;
2092b047e1ccSMark Brown 
2093b047e1ccSMark Brown 	return 0;
2094b047e1ccSMark Brown }
2095b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2096b047e1ccSMark Brown 
2097db2a4165SFrank Mandarino /**
2098741a509fSMarkus Pargmann  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2099741a509fSMarkus Pargmann  *
2100741a509fSMarkus Pargmann  * This function sets the reset and warm_reset properties of ops and parses
2101741a509fSMarkus Pargmann  * the device node of pdev to get pinctrl states and gpio numbers to use.
2102741a509fSMarkus Pargmann  */
2103741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2104741a509fSMarkus Pargmann 		struct platform_device *pdev)
2105741a509fSMarkus Pargmann {
2106741a509fSMarkus Pargmann 	struct device *dev = &pdev->dev;
2107741a509fSMarkus Pargmann 	struct snd_ac97_reset_cfg cfg;
2108741a509fSMarkus Pargmann 	int ret;
2109741a509fSMarkus Pargmann 
2110741a509fSMarkus Pargmann 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2111741a509fSMarkus Pargmann 	if (ret)
2112741a509fSMarkus Pargmann 		return ret;
2113741a509fSMarkus Pargmann 
2114741a509fSMarkus Pargmann 	ret = snd_soc_set_ac97_ops(ops);
2115741a509fSMarkus Pargmann 	if (ret)
2116741a509fSMarkus Pargmann 		return ret;
2117741a509fSMarkus Pargmann 
2118741a509fSMarkus Pargmann 	ops->warm_reset = snd_soc_ac97_warm_reset;
2119741a509fSMarkus Pargmann 	ops->reset = snd_soc_ac97_reset;
2120741a509fSMarkus Pargmann 
2121741a509fSMarkus Pargmann 	snd_ac97_rst_cfg = cfg;
2122741a509fSMarkus Pargmann 	return 0;
2123741a509fSMarkus Pargmann }
2124741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2125741a509fSMarkus Pargmann 
2126741a509fSMarkus Pargmann /**
2127db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
2128db2a4165SFrank Mandarino  * @codec: audio codec
2129db2a4165SFrank Mandarino  *
2130db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
2131db2a4165SFrank Mandarino  */
2132db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2133db2a4165SFrank Mandarino {
2134f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
213502c9c7b9SMisael Lopez Cruz 	soc_unregister_ac97_codec(codec);
2136f0fba2adSLiam Girdwood #endif
2137db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
2138db2a4165SFrank Mandarino 	kfree(codec->ac97);
2139db2a4165SFrank Mandarino 	codec->ac97 = NULL;
21400562f788SMika Westerberg 	codec->ac97_created = 0;
2141db2a4165SFrank Mandarino }
2142db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2143db2a4165SFrank Mandarino 
2144db2a4165SFrank Mandarino /**
2145db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2146db2a4165SFrank Mandarino  * @_template: control template
2147db2a4165SFrank Mandarino  * @data: control private data
2148ac11a2b3SMark Brown  * @long_name: control long name
2149efb7ac3fSMark Brown  * @prefix: control name prefix
2150db2a4165SFrank Mandarino  *
2151db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2152db2a4165SFrank Mandarino  *
2153db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2154db2a4165SFrank Mandarino  */
2155db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21563056557fSMark Brown 				  void *data, const char *long_name,
2157efb7ac3fSMark Brown 				  const char *prefix)
2158db2a4165SFrank Mandarino {
2159db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2160efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2161efb7ac3fSMark Brown 	char *name = NULL;
2162db2a4165SFrank Mandarino 
2163db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2164db2a4165SFrank Mandarino 	template.index = 0;
2165db2a4165SFrank Mandarino 
2166efb7ac3fSMark Brown 	if (!long_name)
2167efb7ac3fSMark Brown 		long_name = template.name;
2168efb7ac3fSMark Brown 
2169efb7ac3fSMark Brown 	if (prefix) {
21702b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2171efb7ac3fSMark Brown 		if (!name)
2172efb7ac3fSMark Brown 			return NULL;
2173efb7ac3fSMark Brown 
2174efb7ac3fSMark Brown 		template.name = name;
2175efb7ac3fSMark Brown 	} else {
2176efb7ac3fSMark Brown 		template.name = long_name;
2177efb7ac3fSMark Brown 	}
2178efb7ac3fSMark Brown 
2179efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2180efb7ac3fSMark Brown 
2181efb7ac3fSMark Brown 	kfree(name);
2182efb7ac3fSMark Brown 
2183efb7ac3fSMark Brown 	return kcontrol;
2184db2a4165SFrank Mandarino }
2185db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2186db2a4165SFrank Mandarino 
2187022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2188022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2189022658beSLiam Girdwood 	const char *prefix, void *data)
2190022658beSLiam Girdwood {
2191022658beSLiam Girdwood 	int err, i;
2192022658beSLiam Girdwood 
2193022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2194022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2195022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2196022658beSLiam Girdwood 						     control->name, prefix));
2197022658beSLiam Girdwood 		if (err < 0) {
2198f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2199f110bfc7SLiam Girdwood 				control->name, err);
2200022658beSLiam Girdwood 			return err;
2201022658beSLiam Girdwood 		}
2202022658beSLiam Girdwood 	}
2203022658beSLiam Girdwood 
2204022658beSLiam Girdwood 	return 0;
2205022658beSLiam Girdwood }
2206022658beSLiam Girdwood 
22074fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22084fefd698SDimitris Papastamos 					       const char *name)
22094fefd698SDimitris Papastamos {
22104fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22114fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22124fefd698SDimitris Papastamos 
22134fefd698SDimitris Papastamos 	if (unlikely(!name))
22144fefd698SDimitris Papastamos 		return NULL;
22154fefd698SDimitris Papastamos 
22164fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22174fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22184fefd698SDimitris Papastamos 			return kctl;
22194fefd698SDimitris Papastamos 	return NULL;
22204fefd698SDimitris Papastamos }
22214fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22224fefd698SDimitris Papastamos 
2223db2a4165SFrank Mandarino /**
22240f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22250f2780adSLars-Peter Clausen  *
22260f2780adSLars-Peter Clausen  * @component: Component to add controls to
22270f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22280f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22290f2780adSLars-Peter Clausen  *
22300f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22310f2780adSLars-Peter Clausen  */
22320f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22330f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22340f2780adSLars-Peter Clausen {
22350f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22360f2780adSLars-Peter Clausen 
22370f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22380f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22390f2780adSLars-Peter Clausen }
22400f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22410f2780adSLars-Peter Clausen 
22420f2780adSLars-Peter Clausen /**
2243022658beSLiam Girdwood  * snd_soc_add_codec_controls - add an array of controls to a codec.
2244022658beSLiam Girdwood  * Convenience function to add a list of controls. Many codecs were
22453e8e1952SIan Molton  * duplicating this code.
22463e8e1952SIan Molton  *
22473e8e1952SIan Molton  * @codec: codec to add controls to
22483e8e1952SIan Molton  * @controls: array of controls to add
22493e8e1952SIan Molton  * @num_controls: number of elements in the array
22503e8e1952SIan Molton  *
22513e8e1952SIan Molton  * Return 0 for success, else error.
22523e8e1952SIan Molton  */
2253022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
22540f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22553e8e1952SIan Molton {
22560f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&codec->component, controls,
22570f2780adSLars-Peter Clausen 		num_controls);
22583e8e1952SIan Molton }
2259022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
22603e8e1952SIan Molton 
22613e8e1952SIan Molton /**
2262a491a5c8SLiam Girdwood  * snd_soc_add_platform_controls - add an array of controls to a platform.
2263022658beSLiam Girdwood  * Convenience function to add a list of controls.
2264a491a5c8SLiam Girdwood  *
2265a491a5c8SLiam Girdwood  * @platform: platform to add controls to
2266a491a5c8SLiam Girdwood  * @controls: array of controls to add
2267a491a5c8SLiam Girdwood  * @num_controls: number of elements in the array
2268a491a5c8SLiam Girdwood  *
2269a491a5c8SLiam Girdwood  * Return 0 for success, else error.
2270a491a5c8SLiam Girdwood  */
2271a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
22720f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
2273a491a5c8SLiam Girdwood {
22740f2780adSLars-Peter Clausen 	return snd_soc_add_component_controls(&platform->component, controls,
22750f2780adSLars-Peter Clausen 		num_controls);
2276a491a5c8SLiam Girdwood }
2277a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2278a491a5c8SLiam Girdwood 
2279a491a5c8SLiam Girdwood /**
2280022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2281022658beSLiam Girdwood  * Convenience function to add a list of controls.
2282022658beSLiam Girdwood  *
2283022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2284022658beSLiam Girdwood  * @controls: array of controls to add
2285022658beSLiam Girdwood  * @num_controls: number of elements in the array
2286022658beSLiam Girdwood  *
2287022658beSLiam Girdwood  * Return 0 for success, else error.
2288022658beSLiam Girdwood  */
2289022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2290022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2291022658beSLiam Girdwood {
2292022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2293022658beSLiam Girdwood 
2294022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2295022658beSLiam Girdwood 			NULL, soc_card);
2296022658beSLiam Girdwood }
2297022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2298022658beSLiam Girdwood 
2299022658beSLiam Girdwood /**
2300022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2301022658beSLiam Girdwood  * Convienience function to add a list of controls.
2302022658beSLiam Girdwood  *
2303022658beSLiam Girdwood  * @dai: DAI to add controls to
2304022658beSLiam Girdwood  * @controls: array of controls to add
2305022658beSLiam Girdwood  * @num_controls: number of elements in the array
2306022658beSLiam Girdwood  *
2307022658beSLiam Girdwood  * Return 0 for success, else error.
2308022658beSLiam Girdwood  */
2309022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2310022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2311022658beSLiam Girdwood {
2312*313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2313022658beSLiam Girdwood 
2314022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2315022658beSLiam Girdwood 			NULL, dai);
2316022658beSLiam Girdwood }
2317022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2318022658beSLiam Girdwood 
2319022658beSLiam Girdwood /**
23208c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23218c6529dbSLiam Girdwood  * @dai: DAI
23228c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23238c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23248c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23258c6529dbSLiam Girdwood  *
23268c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
23278c6529dbSLiam Girdwood  */
23288c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
23298c6529dbSLiam Girdwood 	unsigned int freq, int dir)
23308c6529dbSLiam Girdwood {
2331f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_sysclk)
2332f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2333ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_sysclk)
2334da1c6ea6SMark Brown 		return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2335ec4ee52aSMark Brown 						      freq, dir);
23368c6529dbSLiam Girdwood 	else
23371104a9c8SMark Brown 		return -ENOTSUPP;
23388c6529dbSLiam Girdwood }
23398c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
23408c6529dbSLiam Girdwood 
23418c6529dbSLiam Girdwood /**
2342ec4ee52aSMark Brown  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2343ec4ee52aSMark Brown  * @codec: CODEC
2344ec4ee52aSMark Brown  * @clk_id: DAI specific clock ID
2345da1c6ea6SMark Brown  * @source: Source for the clock
2346ec4ee52aSMark Brown  * @freq: new clock frequency in Hz
2347ec4ee52aSMark Brown  * @dir: new clock direction - input/output.
2348ec4ee52aSMark Brown  *
2349ec4ee52aSMark Brown  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2350ec4ee52aSMark Brown  */
2351ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2352da1c6ea6SMark Brown 			     int source, unsigned int freq, int dir)
2353ec4ee52aSMark Brown {
2354ec4ee52aSMark Brown 	if (codec->driver->set_sysclk)
2355da1c6ea6SMark Brown 		return codec->driver->set_sysclk(codec, clk_id, source,
2356da1c6ea6SMark Brown 						 freq, dir);
2357ec4ee52aSMark Brown 	else
23581104a9c8SMark Brown 		return -ENOTSUPP;
2359ec4ee52aSMark Brown }
2360ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2361ec4ee52aSMark Brown 
2362ec4ee52aSMark Brown /**
23638c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
23648c6529dbSLiam Girdwood  * @dai: DAI
2365ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
23668c6529dbSLiam Girdwood  * @div: new clock divisor.
23678c6529dbSLiam Girdwood  *
23688c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
23698c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
23708c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
23718c6529dbSLiam Girdwood  */
23728c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
23738c6529dbSLiam Girdwood 	int div_id, int div)
23748c6529dbSLiam Girdwood {
2375f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_clkdiv)
2376f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
23778c6529dbSLiam Girdwood 	else
23788c6529dbSLiam Girdwood 		return -EINVAL;
23798c6529dbSLiam Girdwood }
23808c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
23818c6529dbSLiam Girdwood 
23828c6529dbSLiam Girdwood /**
23838c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
23848c6529dbSLiam Girdwood  * @dai: DAI
23858c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
238685488037SMark Brown  * @source: DAI specific source for the PLL
23878c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
23888c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
23898c6529dbSLiam Girdwood  *
23908c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
23918c6529dbSLiam Girdwood  */
239285488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
239385488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
23948c6529dbSLiam Girdwood {
2395f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_pll)
2396f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
239785488037SMark Brown 					 freq_in, freq_out);
2398ec4ee52aSMark Brown 	else if (dai->codec && dai->codec->driver->set_pll)
2399ec4ee52aSMark Brown 		return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2400ec4ee52aSMark Brown 						   freq_in, freq_out);
24018c6529dbSLiam Girdwood 	else
24028c6529dbSLiam Girdwood 		return -EINVAL;
24038c6529dbSLiam Girdwood }
24048c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24058c6529dbSLiam Girdwood 
2406ec4ee52aSMark Brown /*
2407ec4ee52aSMark Brown  * snd_soc_codec_set_pll - configure codec PLL.
2408ec4ee52aSMark Brown  * @codec: CODEC
2409ec4ee52aSMark Brown  * @pll_id: DAI specific PLL ID
2410ec4ee52aSMark Brown  * @source: DAI specific source for the PLL
2411ec4ee52aSMark Brown  * @freq_in: PLL input clock frequency in Hz
2412ec4ee52aSMark Brown  * @freq_out: requested PLL output clock frequency in Hz
2413ec4ee52aSMark Brown  *
2414ec4ee52aSMark Brown  * Configures and enables PLL to generate output clock based on input clock.
2415ec4ee52aSMark Brown  */
2416ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2417ec4ee52aSMark Brown 			  unsigned int freq_in, unsigned int freq_out)
2418ec4ee52aSMark Brown {
2419ec4ee52aSMark Brown 	if (codec->driver->set_pll)
2420ec4ee52aSMark Brown 		return codec->driver->set_pll(codec, pll_id, source,
2421ec4ee52aSMark Brown 					      freq_in, freq_out);
2422ec4ee52aSMark Brown 	else
2423ec4ee52aSMark Brown 		return -EINVAL;
2424ec4ee52aSMark Brown }
2425ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2426ec4ee52aSMark Brown 
24278c6529dbSLiam Girdwood /**
2428e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2429e54cf76bSLiam Girdwood  * @dai: DAI
2430e54cf76bSLiam Girdwood  * @ratio Ratio of BCLK to Sample rate.
2431e54cf76bSLiam Girdwood  *
2432e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2433e54cf76bSLiam Girdwood  */
2434e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2435e54cf76bSLiam Girdwood {
2436e54cf76bSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_bclk_ratio)
2437e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2438e54cf76bSLiam Girdwood 	else
2439e54cf76bSLiam Girdwood 		return -EINVAL;
2440e54cf76bSLiam Girdwood }
2441e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2442e54cf76bSLiam Girdwood 
2443e54cf76bSLiam Girdwood /**
24448c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
24458c6529dbSLiam Girdwood  * @dai: DAI
24468c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
24478c6529dbSLiam Girdwood  *
24488c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
24498c6529dbSLiam Girdwood  */
24508c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
24518c6529dbSLiam Girdwood {
24525e4ba569SShawn Guo 	if (dai->driver == NULL)
24538c6529dbSLiam Girdwood 		return -EINVAL;
24545e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
24555e4ba569SShawn Guo 		return -ENOTSUPP;
24565e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
24578c6529dbSLiam Girdwood }
24588c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
24598c6529dbSLiam Girdwood 
24608c6529dbSLiam Girdwood /**
2461e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
246289c67857SXiubo Li  * @slots: Number of slots in use.
246389c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
246489c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
246589c67857SXiubo Li  *
246689c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
246789c67857SXiubo Li  */
2468e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
246989c67857SXiubo Li 					  unsigned int *tx_mask,
247089c67857SXiubo Li 					  unsigned int *rx_mask)
247189c67857SXiubo Li {
247289c67857SXiubo Li 	if (*tx_mask || *rx_mask)
247389c67857SXiubo Li 		return 0;
247489c67857SXiubo Li 
247589c67857SXiubo Li 	if (!slots)
247689c67857SXiubo Li 		return -EINVAL;
247789c67857SXiubo Li 
247889c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
247989c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
248089c67857SXiubo Li 
248189c67857SXiubo Li 	return 0;
248289c67857SXiubo Li }
248389c67857SXiubo Li 
248489c67857SXiubo Li /**
24858c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
24868c6529dbSLiam Girdwood  * @dai: DAI
2487a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2488a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
24898c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2490a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
24918c6529dbSLiam Girdwood  *
24928c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
24938c6529dbSLiam Girdwood  * specific.
24948c6529dbSLiam Girdwood  */
24958c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2496a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
24978c6529dbSLiam Girdwood {
2498e5c21514SXiubo Li 	if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2499e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
250089c67857SXiubo Li 						&tx_mask, &rx_mask);
250189c67857SXiubo Li 	else
2502e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
250389c67857SXiubo Li 
250488bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
250588bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
250688bd870fSBenoit Cousson 
2507f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tdm_slot)
2508f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2509a5479e38SDaniel Ribeiro 				slots, slot_width);
25108c6529dbSLiam Girdwood 	else
2511b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25128c6529dbSLiam Girdwood }
25138c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25148c6529dbSLiam Girdwood 
25158c6529dbSLiam Girdwood /**
2516472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2517472df3cbSBarry Song  * @dai: DAI
2518472df3cbSBarry Song  * @tx_num: how many TX channels
2519472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2520472df3cbSBarry Song  *           0~num-1 uses
2521472df3cbSBarry Song  * @rx_num: how many RX channels
2522472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2523472df3cbSBarry Song  *           0~num-1 uses
2524472df3cbSBarry Song  *
2525472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2526472df3cbSBarry Song  */
2527472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2528472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2529472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2530472df3cbSBarry Song {
2531f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_channel_map)
2532f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2533472df3cbSBarry Song 			rx_num, rx_slot);
2534472df3cbSBarry Song 	else
2535472df3cbSBarry Song 		return -EINVAL;
2536472df3cbSBarry Song }
2537472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2538472df3cbSBarry Song 
2539472df3cbSBarry Song /**
25408c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
25418c6529dbSLiam Girdwood  * @dai: DAI
25428c6529dbSLiam Girdwood  * @tristate: tristate enable
25438c6529dbSLiam Girdwood  *
25448c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
25458c6529dbSLiam Girdwood  */
25468c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
25478c6529dbSLiam Girdwood {
2548f0fba2adSLiam Girdwood 	if (dai->driver && dai->driver->ops->set_tristate)
2549f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
25508c6529dbSLiam Girdwood 	else
25518c6529dbSLiam Girdwood 		return -EINVAL;
25528c6529dbSLiam Girdwood }
25538c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
25548c6529dbSLiam Girdwood 
25558c6529dbSLiam Girdwood /**
25568c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
25578c6529dbSLiam Girdwood  * @dai: DAI
25588c6529dbSLiam Girdwood  * @mute: mute enable
2559da18396fSMark Brown  * @direction: stream to mute
25608c6529dbSLiam Girdwood  *
25618c6529dbSLiam Girdwood  * Mutes the DAI DAC.
25628c6529dbSLiam Girdwood  */
2563da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2564da18396fSMark Brown 			     int direction)
25658c6529dbSLiam Girdwood {
2566da18396fSMark Brown 	if (!dai->driver)
2567da18396fSMark Brown 		return -ENOTSUPP;
2568da18396fSMark Brown 
2569da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2570da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2571da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2572da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2573f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
25748c6529dbSLiam Girdwood 	else
257504570c62SMark Brown 		return -ENOTSUPP;
25768c6529dbSLiam Girdwood }
25778c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
25788c6529dbSLiam Girdwood 
257988bd870fSBenoit Cousson static int snd_soc_init_multicodec(struct snd_soc_card *card,
258088bd870fSBenoit Cousson 				   struct snd_soc_dai_link *dai_link)
258188bd870fSBenoit Cousson {
258288bd870fSBenoit Cousson 	/* Legacy codec/codec_dai link is a single entry in multicodec */
258388bd870fSBenoit Cousson 	if (dai_link->codec_name || dai_link->codec_of_node ||
258488bd870fSBenoit Cousson 	    dai_link->codec_dai_name) {
258588bd870fSBenoit Cousson 		dai_link->num_codecs = 1;
258688bd870fSBenoit Cousson 
258788bd870fSBenoit Cousson 		dai_link->codecs = devm_kzalloc(card->dev,
258888bd870fSBenoit Cousson 				sizeof(struct snd_soc_dai_link_component),
258988bd870fSBenoit Cousson 				GFP_KERNEL);
259088bd870fSBenoit Cousson 		if (!dai_link->codecs)
259188bd870fSBenoit Cousson 			return -ENOMEM;
259288bd870fSBenoit Cousson 
259388bd870fSBenoit Cousson 		dai_link->codecs[0].name = dai_link->codec_name;
259488bd870fSBenoit Cousson 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
259588bd870fSBenoit Cousson 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
259688bd870fSBenoit Cousson 	}
259788bd870fSBenoit Cousson 
259888bd870fSBenoit Cousson 	if (!dai_link->codecs) {
259988bd870fSBenoit Cousson 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
260088bd870fSBenoit Cousson 		return -EINVAL;
260188bd870fSBenoit Cousson 	}
260288bd870fSBenoit Cousson 
260388bd870fSBenoit Cousson 	return 0;
260488bd870fSBenoit Cousson }
260588bd870fSBenoit Cousson 
2606c5af3a2eSMark Brown /**
2607c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2608c5af3a2eSMark Brown  *
2609ac11a2b3SMark Brown  * @card: Card to register
2610c5af3a2eSMark Brown  *
2611c5af3a2eSMark Brown  */
261270a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2613c5af3a2eSMark Brown {
261488bd870fSBenoit Cousson 	int i, j, ret;
2615f0fba2adSLiam Girdwood 
2616c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2617c5af3a2eSMark Brown 		return -EINVAL;
2618c5af3a2eSMark Brown 
26195a504963SStephen Warren 	for (i = 0; i < card->num_links; i++) {
26205a504963SStephen Warren 		struct snd_soc_dai_link *link = &card->dai_link[i];
26215a504963SStephen Warren 
262288bd870fSBenoit Cousson 		ret = snd_soc_init_multicodec(card, link);
262388bd870fSBenoit Cousson 		if (ret) {
262488bd870fSBenoit Cousson 			dev_err(card->dev, "ASoC: failed to init multicodec\n");
262588bd870fSBenoit Cousson 			return ret;
262688bd870fSBenoit Cousson 		}
262788bd870fSBenoit Cousson 
262888bd870fSBenoit Cousson 		for (j = 0; j < link->num_codecs; j++) {
26295a504963SStephen Warren 			/*
26305a504963SStephen Warren 			 * Codec must be specified by 1 of name or OF node,
26315a504963SStephen Warren 			 * not both or neither.
26325a504963SStephen Warren 			 */
263388bd870fSBenoit Cousson 			if (!!link->codecs[j].name ==
263488bd870fSBenoit Cousson 			    !!link->codecs[j].of_node) {
263588bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
263610e8aa9aSMichał Mirosław 					link->name);
26375a504963SStephen Warren 				return -EINVAL;
26385a504963SStephen Warren 			}
2639bc92657aSStephen Warren 			/* Codec DAI name must be specified */
264088bd870fSBenoit Cousson 			if (!link->codecs[j].dai_name) {
264188bd870fSBenoit Cousson 				dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
264210e8aa9aSMichał Mirosław 					link->name);
2643bc92657aSStephen Warren 				return -EINVAL;
2644bc92657aSStephen Warren 			}
264588bd870fSBenoit Cousson 		}
26465a504963SStephen Warren 
26475a504963SStephen Warren 		/*
26485a504963SStephen Warren 		 * Platform may be specified by either name or OF node, but
26495a504963SStephen Warren 		 * can be left unspecified, and a dummy platform will be used.
26505a504963SStephen Warren 		 */
26515a504963SStephen Warren 		if (link->platform_name && link->platform_of_node) {
265210e8aa9aSMichał Mirosław 			dev_err(card->dev,
265310e8aa9aSMichał Mirosław 				"ASoC: Both platform name/of_node are set for %s\n",
265410e8aa9aSMichał Mirosław 				link->name);
26555a504963SStephen Warren 			return -EINVAL;
26565a504963SStephen Warren 		}
26575a504963SStephen Warren 
26585a504963SStephen Warren 		/*
2659bc92657aSStephen Warren 		 * CPU device may be specified by either name or OF node, but
2660bc92657aSStephen Warren 		 * can be left unspecified, and will be matched based on DAI
2661bc92657aSStephen Warren 		 * name alone..
26625a504963SStephen Warren 		 */
2663bc92657aSStephen Warren 		if (link->cpu_name && link->cpu_of_node) {
266410e8aa9aSMichał Mirosław 			dev_err(card->dev,
266510e8aa9aSMichał Mirosław 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
266610e8aa9aSMichał Mirosław 				link->name);
2667bc92657aSStephen Warren 			return -EINVAL;
2668bc92657aSStephen Warren 		}
2669bc92657aSStephen Warren 		/*
2670bc92657aSStephen Warren 		 * At least one of CPU DAI name or CPU device name/node must be
2671bc92657aSStephen Warren 		 * specified
2672bc92657aSStephen Warren 		 */
2673bc92657aSStephen Warren 		if (!link->cpu_dai_name &&
2674bc92657aSStephen Warren 		    !(link->cpu_name || link->cpu_of_node)) {
267510e8aa9aSMichał Mirosław 			dev_err(card->dev,
267610e8aa9aSMichał Mirosław 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
267710e8aa9aSMichał Mirosław 				link->name);
26785a504963SStephen Warren 			return -EINVAL;
26795a504963SStephen Warren 		}
26805a504963SStephen Warren 	}
26815a504963SStephen Warren 
2682ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2683ed77cc12SMark Brown 
2684111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2685111c6419SStephen Warren 
2686150dd2f8SVinod Koul 	soc_init_card_debugfs(card);
2687150dd2f8SVinod Koul 
2688181a6892SMark Brown 	card->rtd = devm_kzalloc(card->dev,
2689181a6892SMark Brown 				 sizeof(struct snd_soc_pcm_runtime) *
26902eea392dSJarkko Nikula 				 (card->num_links + card->num_aux_devs),
2691f0fba2adSLiam Girdwood 				 GFP_KERNEL);
2692f0fba2adSLiam Girdwood 	if (card->rtd == NULL)
2693f0fba2adSLiam Girdwood 		return -ENOMEM;
2694a7dbb603SLiam Girdwood 	card->num_rtd = 0;
26952eea392dSJarkko Nikula 	card->rtd_aux = &card->rtd[card->num_links];
2696f0fba2adSLiam Girdwood 
26975f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_links; i++) {
26985f3484acSLars-Peter Clausen 		card->rtd[i].card = card;
2699f0fba2adSLiam Girdwood 		card->rtd[i].dai_link = &card->dai_link[i];
270088bd870fSBenoit Cousson 		card->rtd[i].codec_dais = devm_kzalloc(card->dev,
270188bd870fSBenoit Cousson 					sizeof(struct snd_soc_dai *) *
270288bd870fSBenoit Cousson 					(card->rtd[i].dai_link->num_codecs),
270388bd870fSBenoit Cousson 					GFP_KERNEL);
270488bd870fSBenoit Cousson 		if (card->rtd[i].codec_dais == NULL)
270588bd870fSBenoit Cousson 			return -ENOMEM;
27065f3484acSLars-Peter Clausen 	}
27075f3484acSLars-Peter Clausen 
27085f3484acSLars-Peter Clausen 	for (i = 0; i < card->num_aux_devs; i++)
27095f3484acSLars-Peter Clausen 		card->rtd_aux[i].card = card;
2710f0fba2adSLiam Girdwood 
2711db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
2712c5af3a2eSMark Brown 	card->instantiated = 0;
2713f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2714a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2715c5af3a2eSMark Brown 
2716b19e6e7bSMark Brown 	ret = snd_soc_instantiate_card(card);
2717b19e6e7bSMark Brown 	if (ret != 0)
2718b19e6e7bSMark Brown 		soc_cleanup_card_debugfs(card);
2719c5af3a2eSMark Brown 
2720988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2721988e8cc4SNicolin Chen 	for (i = 0; i < card->num_rtd; i++) {
272288bd870fSBenoit Cousson 		struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
272388bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
272488bd870fSBenoit Cousson 		int j;
272588bd870fSBenoit Cousson 
272688bd870fSBenoit Cousson 		for (j = 0; j < rtd->num_codecs; j++) {
272788bd870fSBenoit Cousson 			struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2728988e8cc4SNicolin Chen 			if (!codec_dai->active)
2729988e8cc4SNicolin Chen 				pinctrl_pm_select_sleep_state(codec_dai->dev);
273088bd870fSBenoit Cousson 		}
273188bd870fSBenoit Cousson 
2732988e8cc4SNicolin Chen 		if (!cpu_dai->active)
2733988e8cc4SNicolin Chen 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2734988e8cc4SNicolin Chen 	}
2735988e8cc4SNicolin Chen 
2736b19e6e7bSMark Brown 	return ret;
2737c5af3a2eSMark Brown }
273870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2739c5af3a2eSMark Brown 
2740c5af3a2eSMark Brown /**
2741c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2742c5af3a2eSMark Brown  *
2743ac11a2b3SMark Brown  * @card: Card to unregister
2744c5af3a2eSMark Brown  *
2745c5af3a2eSMark Brown  */
274670a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2747c5af3a2eSMark Brown {
274801e0df66SLars-Peter Clausen 	if (card->instantiated) {
274901e0df66SLars-Peter Clausen 		card->instantiated = false;
27501c325f77SLars-Peter Clausen 		snd_soc_dapm_shutdown(card);
2751b0e26485SVinod Koul 		soc_cleanup_card_resources(card);
275201e0df66SLars-Peter Clausen 	}
2753f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2754c5af3a2eSMark Brown 
2755c5af3a2eSMark Brown 	return 0;
2756c5af3a2eSMark Brown }
275770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2758c5af3a2eSMark Brown 
2759f0fba2adSLiam Girdwood /*
2760f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2761f0fba2adSLiam Girdwood  * and sanitizing names.
2762f0fba2adSLiam Girdwood  */
27630b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2764f0fba2adSLiam Girdwood {
2765f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2766f0fba2adSLiam Girdwood 	int id1, id2;
2767f0fba2adSLiam Girdwood 
2768f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2769f0fba2adSLiam Girdwood 		return NULL;
2770f0fba2adSLiam Girdwood 
277158818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2772f0fba2adSLiam Girdwood 
2773f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2774f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2775f0fba2adSLiam Girdwood 	if (found) {
2776f0fba2adSLiam Girdwood 		/* get ID */
2777f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2778f0fba2adSLiam Girdwood 
2779f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2780f0fba2adSLiam Girdwood 			if (*id == -1)
2781f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2782f0fba2adSLiam Girdwood 		}
2783f0fba2adSLiam Girdwood 
2784f0fba2adSLiam Girdwood 	} else {
2785f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr"  */
2786f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2787f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2788f0fba2adSLiam Girdwood 
2789f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
279005899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2791f0fba2adSLiam Girdwood 
2792f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
2793f0fba2adSLiam Girdwood 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
279458818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2795f0fba2adSLiam Girdwood 		} else
2796f0fba2adSLiam Girdwood 			*id = 0;
2797f0fba2adSLiam Girdwood 	}
2798f0fba2adSLiam Girdwood 
2799f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2800f0fba2adSLiam Girdwood }
2801f0fba2adSLiam Girdwood 
2802f0fba2adSLiam Girdwood /*
2803f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2804f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2805f0fba2adSLiam Girdwood  */
2806f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2807f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2808f0fba2adSLiam Girdwood {
2809f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
281010e8aa9aSMichał Mirosław 		dev_err(dev,
281110e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
281210e8aa9aSMichał Mirosław 			dev_name(dev));
2813f0fba2adSLiam Girdwood 		return NULL;
2814f0fba2adSLiam Girdwood 	}
2815f0fba2adSLiam Girdwood 
2816f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2817f0fba2adSLiam Girdwood }
2818f0fba2adSLiam Girdwood 
28199115171aSMark Brown /**
282032c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
28219115171aSMark Brown  *
282232c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
28239115171aSMark Brown  */
282432c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
28259115171aSMark Brown {
28265c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
28279115171aSMark Brown 
28285c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
282932c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
283032c9ba54SLars-Peter Clausen 			dai->name);
28319115171aSMark Brown 		list_del(&dai->list);
2832f0fba2adSLiam Girdwood 		kfree(dai->name);
2833f0fba2adSLiam Girdwood 		kfree(dai);
28349115171aSMark Brown 	}
283532c9ba54SLars-Peter Clausen }
28369115171aSMark Brown 
28379115171aSMark Brown /**
283832c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
28399115171aSMark Brown  *
28406106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
28416106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2842ac11a2b3SMark Brown  * @count: Number of DAIs
284332c9ba54SLars-Peter Clausen  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
284432c9ba54SLars-Peter Clausen  *                     parent's name.
28459115171aSMark Brown  */
284632c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
2847bb13109dSLars-Peter Clausen 	struct snd_soc_dai_driver *dai_drv, size_t count,
2848bb13109dSLars-Peter Clausen 	bool legacy_dai_naming)
28499115171aSMark Brown {
28506106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2851f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
285232c9ba54SLars-Peter Clausen 	unsigned int i;
285332c9ba54SLars-Peter Clausen 	int ret;
2854f0fba2adSLiam Girdwood 
2855f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
28569115171aSMark Brown 
2857bb13109dSLars-Peter Clausen 	component->dai_drv = dai_drv;
2858bb13109dSLars-Peter Clausen 	component->num_dai = count;
2859bb13109dSLars-Peter Clausen 
28609115171aSMark Brown 	for (i = 0; i < count; i++) {
2861f0fba2adSLiam Girdwood 
2862f0fba2adSLiam Girdwood 		dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2863c46e0079SAxel Lin 		if (dai == NULL) {
2864c46e0079SAxel Lin 			ret = -ENOMEM;
2865c46e0079SAxel Lin 			goto err;
2866c46e0079SAxel Lin 		}
2867f0fba2adSLiam Girdwood 
286832c9ba54SLars-Peter Clausen 		/*
286932c9ba54SLars-Peter Clausen 		 * Back in the old days when we still had component-less DAIs,
287032c9ba54SLars-Peter Clausen 		 * instead of having a static name, component-less DAIs would
287132c9ba54SLars-Peter Clausen 		 * inherit the name of the parent device so it is possible to
287232c9ba54SLars-Peter Clausen 		 * register multiple instances of the DAI. We still need to keep
287332c9ba54SLars-Peter Clausen 		 * the same naming style even though those DAIs are not
287432c9ba54SLars-Peter Clausen 		 * component-less anymore.
287532c9ba54SLars-Peter Clausen 		 */
287632c9ba54SLars-Peter Clausen 		if (count == 1 && legacy_dai_naming) {
2877db2a4165SFrank Mandarino 			dai->name = fmt_single_name(dev, &dai->id);
287832c9ba54SLars-Peter Clausen 		} else {
2879f0fba2adSLiam Girdwood 			dai->name = fmt_multiple_name(dev, &dai_drv[i]);
288032c9ba54SLars-Peter Clausen 			if (dai_drv[i].id)
288132c9ba54SLars-Peter Clausen 				dai->id = dai_drv[i].id;
288232c9ba54SLars-Peter Clausen 			else
288332c9ba54SLars-Peter Clausen 				dai->id = i;
288432c9ba54SLars-Peter Clausen 		}
2885f0fba2adSLiam Girdwood 		if (dai->name == NULL) {
2886f0fba2adSLiam Girdwood 			kfree(dai);
288732c9ba54SLars-Peter Clausen 			ret = -ENOMEM;
28889115171aSMark Brown 			goto err;
28899115171aSMark Brown 		}
28909115171aSMark Brown 
28916106d129SLars-Peter Clausen 		dai->component = component;
2892f0fba2adSLiam Girdwood 		dai->dev = dev;
2893f0fba2adSLiam Girdwood 		dai->driver = &dai_drv[i];
2894f0fba2adSLiam Girdwood 		if (!dai->driver->ops)
2895f0fba2adSLiam Girdwood 			dai->driver->ops = &null_dai_ops;
2896f0fba2adSLiam Girdwood 
28971438c2f6SLars-Peter Clausen 		list_add(&dai->list, &component->dai_list);
2898054880feSMark Brown 
28999115171aSMark Brown 		dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2900f0fba2adSLiam Girdwood 	}
2901f0fba2adSLiam Girdwood 
29029115171aSMark Brown 	return 0;
29039115171aSMark Brown 
29049115171aSMark Brown err:
290532c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
29069115171aSMark Brown 
29079115171aSMark Brown 	return ret;
29089115171aSMark Brown }
29099115171aSMark Brown 
291014e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
291114e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2912d191bd8dSKuninori Morimoto {
291314e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2914d191bd8dSKuninori Morimoto 
291514e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
291614e8bdebSLars-Peter Clausen }
2917d191bd8dSKuninori Morimoto 
291814e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
291914e8bdebSLars-Peter Clausen 	int event)
292014e8bdebSLars-Peter Clausen {
292114e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
292214e8bdebSLars-Peter Clausen 
292314e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
292414e8bdebSLars-Peter Clausen }
292514e8bdebSLars-Peter Clausen 
2926bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2927bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2928d191bd8dSKuninori Morimoto {
2929ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2930ce0fc93aSLars-Peter Clausen 
2931bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2932bb13109dSLars-Peter Clausen 	if (!component->name) {
2933bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2934d191bd8dSKuninori Morimoto 		return -ENOMEM;
2935d191bd8dSKuninori Morimoto 	}
2936d191bd8dSKuninori Morimoto 
2937bb13109dSLars-Peter Clausen 	component->dev = dev;
2938bb13109dSLars-Peter Clausen 	component->driver = driver;
293961aca564SLars-Peter Clausen 	component->probe = component->driver->probe;
294061aca564SLars-Peter Clausen 	component->remove = component->driver->remove;
2941e2c330b9SLars-Peter Clausen 
2942ce0fc93aSLars-Peter Clausen 	if (!component->dapm_ptr)
2943ce0fc93aSLars-Peter Clausen 		component->dapm_ptr = &component->dapm;
2944ce0fc93aSLars-Peter Clausen 
2945ce0fc93aSLars-Peter Clausen 	dapm = component->dapm_ptr;
2946ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2947ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2948ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
29495819c2faSLars-Peter Clausen 	dapm->idle_bias_off = true;
295014e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
295114e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
295214e8bdebSLars-Peter Clausen 	if (driver->stream_event)
295314e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
2954ce0fc93aSLars-Peter Clausen 
295561aca564SLars-Peter Clausen 	component->controls = driver->controls;
295661aca564SLars-Peter Clausen 	component->num_controls = driver->num_controls;
295761aca564SLars-Peter Clausen 	component->dapm_widgets = driver->dapm_widgets;
295861aca564SLars-Peter Clausen 	component->num_dapm_widgets = driver->num_dapm_widgets;
295961aca564SLars-Peter Clausen 	component->dapm_routes = driver->dapm_routes;
296061aca564SLars-Peter Clausen 	component->num_dapm_routes = driver->num_dapm_routes;
296161aca564SLars-Peter Clausen 
2962bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2963bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2964bb13109dSLars-Peter Clausen 
2965bb13109dSLars-Peter Clausen 	return 0;
2966d191bd8dSKuninori Morimoto }
2967d191bd8dSKuninori Morimoto 
2968886f5692SLars-Peter Clausen static void snd_soc_component_init_regmap(struct snd_soc_component *component)
2969886f5692SLars-Peter Clausen {
2970886f5692SLars-Peter Clausen 	if (!component->regmap)
2971886f5692SLars-Peter Clausen 		component->regmap = dev_get_regmap(component->dev, NULL);
2972886f5692SLars-Peter Clausen 	if (component->regmap) {
2973886f5692SLars-Peter Clausen 		int val_bytes = regmap_get_val_bytes(component->regmap);
2974886f5692SLars-Peter Clausen 		/* Errors are legitimate for non-integer byte multiples */
2975886f5692SLars-Peter Clausen 		if (val_bytes > 0)
2976886f5692SLars-Peter Clausen 			component->val_bytes = val_bytes;
2977886f5692SLars-Peter Clausen 	}
2978886f5692SLars-Peter Clausen }
2979886f5692SLars-Peter Clausen 
2980bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2981bb13109dSLars-Peter Clausen {
2982886f5692SLars-Peter Clausen 	if (!component->write && !component->read)
2983886f5692SLars-Peter Clausen 		snd_soc_component_init_regmap(component);
2984886f5692SLars-Peter Clausen 
2985bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2986d191bd8dSKuninori Morimoto }
2987d191bd8dSKuninori Morimoto 
2988bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component)
2989bb13109dSLars-Peter Clausen {
2990d191bd8dSKuninori Morimoto 	mutex_lock(&client_mutex);
2991bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(component);
2992d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2993bb13109dSLars-Peter Clausen }
2994d191bd8dSKuninori Morimoto 
2995bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2996bb13109dSLars-Peter Clausen {
2997bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2998bb13109dSLars-Peter Clausen 	kfree(component->name);
2999bb13109dSLars-Peter Clausen }
3000d191bd8dSKuninori Morimoto 
3001bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3002bb13109dSLars-Peter Clausen {
3003bb13109dSLars-Peter Clausen 	list_del(&component->list);
3004bb13109dSLars-Peter Clausen }
3005d191bd8dSKuninori Morimoto 
3006bb13109dSLars-Peter Clausen static void snd_soc_component_del(struct snd_soc_component *component)
3007bb13109dSLars-Peter Clausen {
3008bb13109dSLars-Peter Clausen 	mutex_lock(&client_mutex);
3009bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(component);
3010bb13109dSLars-Peter Clausen 	mutex_unlock(&client_mutex);
3011d191bd8dSKuninori Morimoto }
3012d191bd8dSKuninori Morimoto 
3013d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3014d191bd8dSKuninori Morimoto 			       const struct snd_soc_component_driver *cmpnt_drv,
3015d191bd8dSKuninori Morimoto 			       struct snd_soc_dai_driver *dai_drv,
3016d191bd8dSKuninori Morimoto 			       int num_dai)
3017d191bd8dSKuninori Morimoto {
3018d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3019bb13109dSLars-Peter Clausen 	int ret;
3020d191bd8dSKuninori Morimoto 
3021bb13109dSLars-Peter Clausen 	cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3022d191bd8dSKuninori Morimoto 	if (!cmpnt) {
3023d191bd8dSKuninori Morimoto 		dev_err(dev, "ASoC: Failed to allocate memory\n");
3024d191bd8dSKuninori Morimoto 		return -ENOMEM;
3025d191bd8dSKuninori Morimoto 	}
3026d191bd8dSKuninori Morimoto 
3027bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3028bb13109dSLars-Peter Clausen 	if (ret)
3029bb13109dSLars-Peter Clausen 		goto err_free;
3030bb13109dSLars-Peter Clausen 
30313d59400fSLars-Peter Clausen 	cmpnt->ignore_pmdown_time = true;
303298e639fbSLars-Peter Clausen 	cmpnt->registered_as_component = true;
30333d59400fSLars-Peter Clausen 
3034bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3035bb13109dSLars-Peter Clausen 	if (ret < 0) {
3036bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3037bb13109dSLars-Peter Clausen 		goto err_cleanup;
3038bb13109dSLars-Peter Clausen 	}
3039bb13109dSLars-Peter Clausen 
3040bb13109dSLars-Peter Clausen 	snd_soc_component_add(cmpnt);
3041bb13109dSLars-Peter Clausen 
3042bb13109dSLars-Peter Clausen 	return 0;
3043bb13109dSLars-Peter Clausen 
3044bb13109dSLars-Peter Clausen err_cleanup:
3045bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3046bb13109dSLars-Peter Clausen err_free:
3047bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3048bb13109dSLars-Peter Clausen 	return ret;
3049d191bd8dSKuninori Morimoto }
3050d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3051d191bd8dSKuninori Morimoto 
3052d191bd8dSKuninori Morimoto /**
3053d191bd8dSKuninori Morimoto  * snd_soc_unregister_component - Unregister a component from the ASoC core
3054d191bd8dSKuninori Morimoto  *
3055d191bd8dSKuninori Morimoto  */
3056d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
3057d191bd8dSKuninori Morimoto {
3058d191bd8dSKuninori Morimoto 	struct snd_soc_component *cmpnt;
3059d191bd8dSKuninori Morimoto 
3060d191bd8dSKuninori Morimoto 	list_for_each_entry(cmpnt, &component_list, list) {
306198e639fbSLars-Peter Clausen 		if (dev == cmpnt->dev && cmpnt->registered_as_component)
3062d191bd8dSKuninori Morimoto 			goto found;
3063d191bd8dSKuninori Morimoto 	}
3064d191bd8dSKuninori Morimoto 	return;
3065d191bd8dSKuninori Morimoto 
3066d191bd8dSKuninori Morimoto found:
3067bb13109dSLars-Peter Clausen 	snd_soc_component_del(cmpnt);
3068bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(cmpnt);
3069bb13109dSLars-Peter Clausen 	kfree(cmpnt);
3070d191bd8dSKuninori Morimoto }
3071d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3072d191bd8dSKuninori Morimoto 
3073f1d45cc3SLars-Peter Clausen static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3074e2c330b9SLars-Peter Clausen {
3075e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3076e2c330b9SLars-Peter Clausen 
3077f1d45cc3SLars-Peter Clausen 	return platform->driver->probe(platform);
3078e2c330b9SLars-Peter Clausen }
3079e2c330b9SLars-Peter Clausen 
3080f1d45cc3SLars-Peter Clausen static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3081e2c330b9SLars-Peter Clausen {
3082e2c330b9SLars-Peter Clausen 	struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3083e2c330b9SLars-Peter Clausen 
3084f1d45cc3SLars-Peter Clausen 	platform->driver->remove(platform);
3085e2c330b9SLars-Peter Clausen }
3086e2c330b9SLars-Peter Clausen 
3087d191bd8dSKuninori Morimoto /**
308871a45cdaSLars-Peter Clausen  * snd_soc_add_platform - Add a platform to the ASoC core
308971a45cdaSLars-Peter Clausen  * @dev: The parent device for the platform
309071a45cdaSLars-Peter Clausen  * @platform: The platform to add
309171a45cdaSLars-Peter Clausen  * @platform_driver: The driver for the platform
309212a48a8cSMark Brown  */
309371a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3094d79e57dbSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
309512a48a8cSMark Brown {
3096b37f1d12SLars-Peter Clausen 	int ret;
3097b37f1d12SLars-Peter Clausen 
3098bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&platform->component,
3099bb13109dSLars-Peter Clausen 			&platform_drv->component_driver, dev);
3100bb13109dSLars-Peter Clausen 	if (ret)
3101bb13109dSLars-Peter Clausen 		return ret;
3102f0fba2adSLiam Girdwood 
3103f0fba2adSLiam Girdwood 	platform->dev = dev;
3104f0fba2adSLiam Girdwood 	platform->driver = platform_drv;
3105f1d45cc3SLars-Peter Clausen 
3106f1d45cc3SLars-Peter Clausen 	if (platform_drv->probe)
3107f1d45cc3SLars-Peter Clausen 		platform->component.probe = snd_soc_platform_drv_probe;
3108f1d45cc3SLars-Peter Clausen 	if (platform_drv->remove)
3109f1d45cc3SLars-Peter Clausen 		platform->component.remove = snd_soc_platform_drv_remove;
311012a48a8cSMark Brown 
311181c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
311281c7cfd1SLars-Peter Clausen 	platform->component.debugfs_prefix = "platform";
311381c7cfd1SLars-Peter Clausen #endif
311412a48a8cSMark Brown 
311512a48a8cSMark Brown 	mutex_lock(&client_mutex);
3116bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&platform->component);
311712a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
311812a48a8cSMark Brown 	mutex_unlock(&client_mutex);
311912a48a8cSMark Brown 
3120f4333203SLars-Peter Clausen 	dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3121f4333203SLars-Peter Clausen 		platform->component.name);
312212a48a8cSMark Brown 
312312a48a8cSMark Brown 	return 0;
312412a48a8cSMark Brown }
312571a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform);
312671a45cdaSLars-Peter Clausen 
312771a45cdaSLars-Peter Clausen /**
312871a45cdaSLars-Peter Clausen  * snd_soc_register_platform - Register a platform with the ASoC core
312971a45cdaSLars-Peter Clausen  *
313071a45cdaSLars-Peter Clausen  * @platform: platform to register
313171a45cdaSLars-Peter Clausen  */
313271a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev,
313371a45cdaSLars-Peter Clausen 		const struct snd_soc_platform_driver *platform_drv)
313471a45cdaSLars-Peter Clausen {
313571a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
313671a45cdaSLars-Peter Clausen 	int ret;
313771a45cdaSLars-Peter Clausen 
313871a45cdaSLars-Peter Clausen 	dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
313971a45cdaSLars-Peter Clausen 
314071a45cdaSLars-Peter Clausen 	platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
314171a45cdaSLars-Peter Clausen 	if (platform == NULL)
314271a45cdaSLars-Peter Clausen 		return -ENOMEM;
314371a45cdaSLars-Peter Clausen 
314471a45cdaSLars-Peter Clausen 	ret = snd_soc_add_platform(dev, platform, platform_drv);
314571a45cdaSLars-Peter Clausen 	if (ret)
314671a45cdaSLars-Peter Clausen 		kfree(platform);
314771a45cdaSLars-Peter Clausen 
314871a45cdaSLars-Peter Clausen 	return ret;
314971a45cdaSLars-Peter Clausen }
315012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
315112a48a8cSMark Brown 
315212a48a8cSMark Brown /**
315371a45cdaSLars-Peter Clausen  * snd_soc_remove_platform - Remove a platform from the ASoC core
315471a45cdaSLars-Peter Clausen  * @platform: the platform to remove
315571a45cdaSLars-Peter Clausen  */
315671a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform)
315771a45cdaSLars-Peter Clausen {
3158b37f1d12SLars-Peter Clausen 
315971a45cdaSLars-Peter Clausen 	mutex_lock(&client_mutex);
316071a45cdaSLars-Peter Clausen 	list_del(&platform->list);
3161bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&platform->component);
316271a45cdaSLars-Peter Clausen 	mutex_unlock(&client_mutex);
316371a45cdaSLars-Peter Clausen 
316471a45cdaSLars-Peter Clausen 	dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3165f4333203SLars-Peter Clausen 		platform->component.name);
3166decc27b0SDaniel Mack 
3167decc27b0SDaniel Mack 	snd_soc_component_cleanup(&platform->component);
316871a45cdaSLars-Peter Clausen }
316971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
317071a45cdaSLars-Peter Clausen 
317171a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
317271a45cdaSLars-Peter Clausen {
317371a45cdaSLars-Peter Clausen 	struct snd_soc_platform *platform;
317471a45cdaSLars-Peter Clausen 
317571a45cdaSLars-Peter Clausen 	list_for_each_entry(platform, &platform_list, list) {
317671a45cdaSLars-Peter Clausen 		if (dev == platform->dev)
317771a45cdaSLars-Peter Clausen 			return platform;
317871a45cdaSLars-Peter Clausen 	}
317971a45cdaSLars-Peter Clausen 
318071a45cdaSLars-Peter Clausen 	return NULL;
318171a45cdaSLars-Peter Clausen }
318271a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
318371a45cdaSLars-Peter Clausen 
318471a45cdaSLars-Peter Clausen /**
318512a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
318612a48a8cSMark Brown  *
3187ac11a2b3SMark Brown  * @platform: platform to unregister
318812a48a8cSMark Brown  */
3189f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev)
319012a48a8cSMark Brown {
3191f0fba2adSLiam Girdwood 	struct snd_soc_platform *platform;
3192f0fba2adSLiam Girdwood 
319371a45cdaSLars-Peter Clausen 	platform = snd_soc_lookup_platform(dev);
319471a45cdaSLars-Peter Clausen 	if (!platform)
3195f0fba2adSLiam Girdwood 		return;
3196f0fba2adSLiam Girdwood 
319771a45cdaSLars-Peter Clausen 	snd_soc_remove_platform(platform);
3198f0fba2adSLiam Girdwood 	kfree(platform);
319912a48a8cSMark Brown }
320012a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
320112a48a8cSMark Brown 
3202151ab22cSMark Brown static u64 codec_format_map[] = {
3203151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3204151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3205151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3206151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3207151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3208151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3209151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3210151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3211151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3212151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3213151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3214151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3215151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3216151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3217151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3218151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3219151ab22cSMark Brown };
3220151ab22cSMark Brown 
3221151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
3222151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
3223151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
3224151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
3225151ab22cSMark Brown  */
3226151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3227151ab22cSMark Brown {
3228151ab22cSMark Brown 	int i;
3229151ab22cSMark Brown 
3230151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3231151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
3232151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
3233151ab22cSMark Brown }
3234151ab22cSMark Brown 
3235f1d45cc3SLars-Peter Clausen static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3236f1d45cc3SLars-Peter Clausen {
3237f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3238f1d45cc3SLars-Peter Clausen 
3239f1d45cc3SLars-Peter Clausen 	return codec->driver->probe(codec);
3240f1d45cc3SLars-Peter Clausen }
3241f1d45cc3SLars-Peter Clausen 
3242f1d45cc3SLars-Peter Clausen static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3243f1d45cc3SLars-Peter Clausen {
3244f1d45cc3SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3245f1d45cc3SLars-Peter Clausen 
3246f1d45cc3SLars-Peter Clausen 	codec->driver->remove(codec);
3247f1d45cc3SLars-Peter Clausen }
3248f1d45cc3SLars-Peter Clausen 
3249e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3250e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int val)
3251e2c330b9SLars-Peter Clausen {
3252e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3253e2c330b9SLars-Peter Clausen 
3254e2c330b9SLars-Peter Clausen 	return codec->driver->write(codec, reg, val);
3255e2c330b9SLars-Peter Clausen }
3256e2c330b9SLars-Peter Clausen 
3257e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3258e2c330b9SLars-Peter Clausen 	unsigned int reg, unsigned int *val)
3259e2c330b9SLars-Peter Clausen {
3260e2c330b9SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3261e2c330b9SLars-Peter Clausen 
3262e2c330b9SLars-Peter Clausen 	*val = codec->driver->read(codec, reg);
3263e2c330b9SLars-Peter Clausen 
3264e2c330b9SLars-Peter Clausen 	return 0;
3265e2c330b9SLars-Peter Clausen }
3266e2c330b9SLars-Peter Clausen 
326768f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
326868f831c2SLars-Peter Clausen 	enum snd_soc_bias_level level)
326968f831c2SLars-Peter Clausen {
327068f831c2SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
327168f831c2SLars-Peter Clausen 
327268f831c2SLars-Peter Clausen 	return codec->driver->set_bias_level(codec, level);
327368f831c2SLars-Peter Clausen }
327468f831c2SLars-Peter Clausen 
32750d0cf00aSMark Brown /**
32760d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
32770d0cf00aSMark Brown  *
3278ac11a2b3SMark Brown  * @codec: codec to register
32790d0cf00aSMark Brown  */
3280f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev,
3281001ae4c0SMark Brown 			   const struct snd_soc_codec_driver *codec_drv,
3282001ae4c0SMark Brown 			   struct snd_soc_dai_driver *dai_drv,
3283001ae4c0SMark Brown 			   int num_dai)
32840d0cf00aSMark Brown {
3285f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3286bb13109dSLars-Peter Clausen 	struct snd_soc_dai *dai;
3287f0fba2adSLiam Girdwood 	int ret, i;
3288151ab22cSMark Brown 
3289f0fba2adSLiam Girdwood 	dev_dbg(dev, "codec register %s\n", dev_name(dev));
32900d0cf00aSMark Brown 
3291f0fba2adSLiam Girdwood 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3292f0fba2adSLiam Girdwood 	if (codec == NULL)
3293f0fba2adSLiam Girdwood 		return -ENOMEM;
32940d0cf00aSMark Brown 
3295ce0fc93aSLars-Peter Clausen 	codec->component.dapm_ptr = &codec->dapm;
3296f1d45cc3SLars-Peter Clausen 	codec->component.codec = codec;
3297ce0fc93aSLars-Peter Clausen 
3298bb13109dSLars-Peter Clausen 	ret = snd_soc_component_initialize(&codec->component,
3299bb13109dSLars-Peter Clausen 			&codec_drv->component_driver, dev);
3300bb13109dSLars-Peter Clausen 	if (ret)
3301bb13109dSLars-Peter Clausen 		goto err_free;
3302151ab22cSMark Brown 
3303f1d45cc3SLars-Peter Clausen 	if (codec_drv->controls) {
3304f1d45cc3SLars-Peter Clausen 		codec->component.controls = codec_drv->controls;
3305f1d45cc3SLars-Peter Clausen 		codec->component.num_controls = codec_drv->num_controls;
3306f1d45cc3SLars-Peter Clausen 	}
3307f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_widgets) {
3308f1d45cc3SLars-Peter Clausen 		codec->component.dapm_widgets = codec_drv->dapm_widgets;
3309f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3310f1d45cc3SLars-Peter Clausen 	}
3311f1d45cc3SLars-Peter Clausen 	if (codec_drv->dapm_routes) {
3312f1d45cc3SLars-Peter Clausen 		codec->component.dapm_routes = codec_drv->dapm_routes;
3313f1d45cc3SLars-Peter Clausen 		codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3314f1d45cc3SLars-Peter Clausen 	}
3315f1d45cc3SLars-Peter Clausen 
3316f1d45cc3SLars-Peter Clausen 	if (codec_drv->probe)
3317f1d45cc3SLars-Peter Clausen 		codec->component.probe = snd_soc_codec_drv_probe;
3318f1d45cc3SLars-Peter Clausen 	if (codec_drv->remove)
3319f1d45cc3SLars-Peter Clausen 		codec->component.remove = snd_soc_codec_drv_remove;
3320e2c330b9SLars-Peter Clausen 	if (codec_drv->write)
3321e2c330b9SLars-Peter Clausen 		codec->component.write = snd_soc_codec_drv_write;
3322e2c330b9SLars-Peter Clausen 	if (codec_drv->read)
3323e2c330b9SLars-Peter Clausen 		codec->component.read = snd_soc_codec_drv_read;
33243d59400fSLars-Peter Clausen 	codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
33255819c2faSLars-Peter Clausen 	codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
332686dbf2acSLars-Peter Clausen 	codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
332714e8bdebSLars-Peter Clausen 	if (codec_drv->seq_notifier)
3328474b62d6SMark Brown 		codec->dapm.seq_notifier = codec_drv->seq_notifier;
332968f831c2SLars-Peter Clausen 	if (codec_drv->set_bias_level)
333068f831c2SLars-Peter Clausen 		codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3331f0fba2adSLiam Girdwood 	codec->dev = dev;
3332f0fba2adSLiam Girdwood 	codec->driver = codec_drv;
3333e2c330b9SLars-Peter Clausen 	codec->component.val_bytes = codec_drv->reg_word_size;
3334f0fba2adSLiam Girdwood 	mutex_init(&codec->mutex);
3335f0fba2adSLiam Girdwood 
333681c7cfd1SLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
333781c7cfd1SLars-Peter Clausen 	codec->component.init_debugfs = soc_init_codec_debugfs;
333881c7cfd1SLars-Peter Clausen 	codec->component.debugfs_prefix = "codec";
333981c7cfd1SLars-Peter Clausen #endif
3340a39f75f7SXiubo Li 
3341a39f75f7SXiubo Li 	if (codec_drv->get_regmap)
3342886f5692SLars-Peter Clausen 		codec->component.regmap = codec_drv->get_regmap(dev);
3343a39f75f7SXiubo Li 
3344f0fba2adSLiam Girdwood 	for (i = 0; i < num_dai; i++) {
3345f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].playback);
3346f0fba2adSLiam Girdwood 		fixup_codec_formats(&dai_drv[i].capture);
3347f0fba2adSLiam Girdwood 	}
3348f0fba2adSLiam Girdwood 
3349bb13109dSLars-Peter Clausen 	ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3350bb13109dSLars-Peter Clausen 	if (ret < 0) {
3351bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3352bb13109dSLars-Peter Clausen 		goto err_cleanup;
3353bb13109dSLars-Peter Clausen 	}
3354bb13109dSLars-Peter Clausen 
3355bb13109dSLars-Peter Clausen 	list_for_each_entry(dai, &codec->component.dai_list, list)
3356bb13109dSLars-Peter Clausen 		dai->codec = codec;
3357bb13109dSLars-Peter Clausen 
3358054880feSMark Brown 	mutex_lock(&client_mutex);
3359bb13109dSLars-Peter Clausen 	snd_soc_component_add_unlocked(&codec->component);
3360054880feSMark Brown 	list_add(&codec->list, &codec_list);
3361054880feSMark Brown 	mutex_unlock(&client_mutex);
3362054880feSMark Brown 
3363f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3364f4333203SLars-Peter Clausen 		codec->component.name);
33650d0cf00aSMark Brown 	return 0;
3366f0fba2adSLiam Girdwood 
3367bb13109dSLars-Peter Clausen err_cleanup:
3368bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
3369bb13109dSLars-Peter Clausen err_free:
3370f0fba2adSLiam Girdwood 	kfree(codec);
3371f0fba2adSLiam Girdwood 	return ret;
33720d0cf00aSMark Brown }
33730d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
33740d0cf00aSMark Brown 
33750d0cf00aSMark Brown /**
33760d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
33770d0cf00aSMark Brown  *
3378ac11a2b3SMark Brown  * @codec: codec to unregister
33790d0cf00aSMark Brown  */
3380f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev)
33810d0cf00aSMark Brown {
3382f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec;
3383f0fba2adSLiam Girdwood 
3384f0fba2adSLiam Girdwood 	list_for_each_entry(codec, &codec_list, list) {
3385f0fba2adSLiam Girdwood 		if (dev == codec->dev)
3386f0fba2adSLiam Girdwood 			goto found;
3387f0fba2adSLiam Girdwood 	}
3388f0fba2adSLiam Girdwood 	return;
3389f0fba2adSLiam Girdwood 
3390f0fba2adSLiam Girdwood found:
3391f0fba2adSLiam Girdwood 
33920d0cf00aSMark Brown 	mutex_lock(&client_mutex);
33930d0cf00aSMark Brown 	list_del(&codec->list);
3394bb13109dSLars-Peter Clausen 	snd_soc_component_del_unlocked(&codec->component);
33950d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
33960d0cf00aSMark Brown 
3397f4333203SLars-Peter Clausen 	dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3398f4333203SLars-Peter Clausen 			codec->component.name);
3399f0fba2adSLiam Girdwood 
3400bb13109dSLars-Peter Clausen 	snd_soc_component_cleanup(&codec->component);
34017a30a3dbSDimitris Papastamos 	snd_soc_cache_exit(codec);
3402f0fba2adSLiam Girdwood 	kfree(codec);
34030d0cf00aSMark Brown }
34040d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
34050d0cf00aSMark Brown 
3406bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3407bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3408bec4fa05SStephen Warren 			       const char *propname)
3409bec4fa05SStephen Warren {
34107e07e7c0STushar Behera 	struct device_node *np;
3411bec4fa05SStephen Warren 	int ret;
3412bec4fa05SStephen Warren 
34137e07e7c0STushar Behera 	if (!card->dev) {
34147e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
34157e07e7c0STushar Behera 		return -EINVAL;
34167e07e7c0STushar Behera 	}
34177e07e7c0STushar Behera 
34187e07e7c0STushar Behera 	np = card->dev->of_node;
34197e07e7c0STushar Behera 
3420bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3421bec4fa05SStephen Warren 	/*
3422bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3423bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3424bec4fa05SStephen Warren 	 * snd_soc_register_card.
3425bec4fa05SStephen Warren 	 */
3426bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3427bec4fa05SStephen Warren 		dev_err(card->dev,
3428f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3429bec4fa05SStephen Warren 			propname, ret);
3430bec4fa05SStephen Warren 		return ret;
3431bec4fa05SStephen Warren 	}
3432bec4fa05SStephen Warren 
3433bec4fa05SStephen Warren 	return 0;
3434bec4fa05SStephen Warren }
3435bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3436bec4fa05SStephen Warren 
34379a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
34389a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
34399a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
34409a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
34419a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
34429a6d4860SXiubo Li };
34439a6d4860SXiubo Li 
34449a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
34459a6d4860SXiubo Li 					  const char *propname)
34469a6d4860SXiubo Li {
34479a6d4860SXiubo Li 	struct device_node *np = card->dev->of_node;
34489a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
34499a6d4860SXiubo Li 	const char *template, *wname;
34509a6d4860SXiubo Li 	int i, j, num_widgets, ret;
34519a6d4860SXiubo Li 
34529a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
34539a6d4860SXiubo Li 	if (num_widgets < 0) {
34549a6d4860SXiubo Li 		dev_err(card->dev,
34559a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
34569a6d4860SXiubo Li 		return -EINVAL;
34579a6d4860SXiubo Li 	}
34589a6d4860SXiubo Li 	if (num_widgets & 1) {
34599a6d4860SXiubo Li 		dev_err(card->dev,
34609a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
34619a6d4860SXiubo Li 		return -EINVAL;
34629a6d4860SXiubo Li 	}
34639a6d4860SXiubo Li 
34649a6d4860SXiubo Li 	num_widgets /= 2;
34659a6d4860SXiubo Li 	if (!num_widgets) {
34669a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
34679a6d4860SXiubo Li 			propname);
34689a6d4860SXiubo Li 		return -EINVAL;
34699a6d4860SXiubo Li 	}
34709a6d4860SXiubo Li 
34719a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
34729a6d4860SXiubo Li 			       GFP_KERNEL);
34739a6d4860SXiubo Li 	if (!widgets) {
34749a6d4860SXiubo Li 		dev_err(card->dev,
34759a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
34769a6d4860SXiubo Li 		return -ENOMEM;
34779a6d4860SXiubo Li 	}
34789a6d4860SXiubo Li 
34799a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
34809a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
34819a6d4860SXiubo Li 			2 * i, &template);
34829a6d4860SXiubo Li 		if (ret) {
34839a6d4860SXiubo Li 			dev_err(card->dev,
34849a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
34859a6d4860SXiubo Li 				propname, 2 * i, ret);
34869a6d4860SXiubo Li 			return -EINVAL;
34879a6d4860SXiubo Li 		}
34889a6d4860SXiubo Li 
34899a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
34909a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
34919a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
34929a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
34939a6d4860SXiubo Li 				break;
34949a6d4860SXiubo Li 			}
34959a6d4860SXiubo Li 		}
34969a6d4860SXiubo Li 
34979a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
34989a6d4860SXiubo Li 			dev_err(card->dev,
34999a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
35009a6d4860SXiubo Li 				template);
35019a6d4860SXiubo Li 			return -EINVAL;
35029a6d4860SXiubo Li 		}
35039a6d4860SXiubo Li 
35049a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
35059a6d4860SXiubo Li 						    (2 * i) + 1,
35069a6d4860SXiubo Li 						    &wname);
35079a6d4860SXiubo Li 		if (ret) {
35089a6d4860SXiubo Li 			dev_err(card->dev,
35099a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
35109a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
35119a6d4860SXiubo Li 			return -EINVAL;
35129a6d4860SXiubo Li 		}
35139a6d4860SXiubo Li 
35149a6d4860SXiubo Li 		widgets[i].name = wname;
35159a6d4860SXiubo Li 	}
35169a6d4860SXiubo Li 
35179a6d4860SXiubo Li 	card->dapm_widgets = widgets;
35189a6d4860SXiubo Li 	card->num_dapm_widgets = num_widgets;
35199a6d4860SXiubo Li 
35209a6d4860SXiubo Li 	return 0;
35219a6d4860SXiubo Li }
35229a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
35239a6d4860SXiubo Li 
352489c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
352589c67857SXiubo Li 			      unsigned int *slots,
352689c67857SXiubo Li 			      unsigned int *slot_width)
352789c67857SXiubo Li {
352889c67857SXiubo Li 	u32 val;
352989c67857SXiubo Li 	int ret;
353089c67857SXiubo Li 
353189c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
353289c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
353389c67857SXiubo Li 		if (ret)
353489c67857SXiubo Li 			return ret;
353589c67857SXiubo Li 
353689c67857SXiubo Li 		if (slots)
353789c67857SXiubo Li 			*slots = val;
353889c67857SXiubo Li 	}
353989c67857SXiubo Li 
354089c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
354189c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
354289c67857SXiubo Li 		if (ret)
354389c67857SXiubo Li 			return ret;
354489c67857SXiubo Li 
354589c67857SXiubo Li 		if (slot_width)
354689c67857SXiubo Li 			*slot_width = val;
354789c67857SXiubo Li 	}
354889c67857SXiubo Li 
354989c67857SXiubo Li 	return 0;
355089c67857SXiubo Li }
355189c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
355289c67857SXiubo Li 
3553a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3554a4a54dd5SStephen Warren 				   const char *propname)
3555a4a54dd5SStephen Warren {
3556a4a54dd5SStephen Warren 	struct device_node *np = card->dev->of_node;
3557a4a54dd5SStephen Warren 	int num_routes;
3558a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3559a4a54dd5SStephen Warren 	int i, ret;
3560a4a54dd5SStephen Warren 
3561a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3562c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
356310e8aa9aSMichał Mirosław 		dev_err(card->dev,
356410e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
356510e8aa9aSMichał Mirosław 			propname);
3566a4a54dd5SStephen Warren 		return -EINVAL;
3567a4a54dd5SStephen Warren 	}
3568a4a54dd5SStephen Warren 	num_routes /= 2;
3569a4a54dd5SStephen Warren 	if (!num_routes) {
3570f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3571a4a54dd5SStephen Warren 			propname);
3572a4a54dd5SStephen Warren 		return -EINVAL;
3573a4a54dd5SStephen Warren 	}
3574a4a54dd5SStephen Warren 
3575a4a54dd5SStephen Warren 	routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3576a4a54dd5SStephen Warren 			      GFP_KERNEL);
3577a4a54dd5SStephen Warren 	if (!routes) {
3578a4a54dd5SStephen Warren 		dev_err(card->dev,
3579f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3580a4a54dd5SStephen Warren 		return -EINVAL;
3581a4a54dd5SStephen Warren 	}
3582a4a54dd5SStephen Warren 
3583a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3584a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3585a4a54dd5SStephen Warren 			2 * i, &routes[i].sink);
3586a4a54dd5SStephen Warren 		if (ret) {
3587c871bd0bSMark Brown 			dev_err(card->dev,
3588c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3589c871bd0bSMark Brown 				propname, 2 * i, ret);
3590a4a54dd5SStephen Warren 			return -EINVAL;
3591a4a54dd5SStephen Warren 		}
3592a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3593a4a54dd5SStephen Warren 			(2 * i) + 1, &routes[i].source);
3594a4a54dd5SStephen Warren 		if (ret) {
3595a4a54dd5SStephen Warren 			dev_err(card->dev,
3596c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3597c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3598a4a54dd5SStephen Warren 			return -EINVAL;
3599a4a54dd5SStephen Warren 		}
3600a4a54dd5SStephen Warren 	}
3601a4a54dd5SStephen Warren 
3602a4a54dd5SStephen Warren 	card->num_dapm_routes = num_routes;
3603a4a54dd5SStephen Warren 	card->dapm_routes = routes;
3604a4a54dd5SStephen Warren 
3605a4a54dd5SStephen Warren 	return 0;
3606a4a54dd5SStephen Warren }
3607a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3608a4a54dd5SStephen Warren 
3609a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3610389cb834SJyri Sarha 				     const char *prefix,
3611389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3612389cb834SJyri Sarha 				     struct device_node **framemaster)
3613a7930ed4SKuninori Morimoto {
3614a7930ed4SKuninori Morimoto 	int ret, i;
3615a7930ed4SKuninori Morimoto 	char prop[128];
3616a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3617a7930ed4SKuninori Morimoto 	int bit, frame;
3618a7930ed4SKuninori Morimoto 	const char *str;
3619a7930ed4SKuninori Morimoto 	struct {
3620a7930ed4SKuninori Morimoto 		char *name;
3621a7930ed4SKuninori Morimoto 		unsigned int val;
3622a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3623a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3624a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3625a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3626a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3627a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3628a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3629a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3630a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3631a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3632a7930ed4SKuninori Morimoto 	};
3633a7930ed4SKuninori Morimoto 
3634a7930ed4SKuninori Morimoto 	if (!prefix)
3635a7930ed4SKuninori Morimoto 		prefix = "";
3636a7930ed4SKuninori Morimoto 
3637a7930ed4SKuninori Morimoto 	/*
3638a7930ed4SKuninori Morimoto 	 * check "[prefix]format = xxx"
3639a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3640a7930ed4SKuninori Morimoto 	 */
3641a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sformat", prefix);
3642a7930ed4SKuninori Morimoto 	ret = of_property_read_string(np, prop, &str);
3643a7930ed4SKuninori Morimoto 	if (ret == 0) {
3644a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3645a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3646a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3647a7930ed4SKuninori Morimoto 				break;
3648a7930ed4SKuninori Morimoto 			}
3649a7930ed4SKuninori Morimoto 		}
3650a7930ed4SKuninori Morimoto 	}
3651a7930ed4SKuninori Morimoto 
3652a7930ed4SKuninori Morimoto 	/*
36538c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3654a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3655a7930ed4SKuninori Morimoto 	 */
36568c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
36578c2d6a9fSKuninori Morimoto 	if (of_get_property(np, prop, NULL))
36588c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
36598c2d6a9fSKuninori Morimoto 	else
36608c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3661a7930ed4SKuninori Morimoto 
3662a7930ed4SKuninori Morimoto 	/*
3663a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3664a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3665a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3666a7930ed4SKuninori Morimoto 	 */
3667a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3668a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3669a7930ed4SKuninori Morimoto 
3670a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3671a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3672a7930ed4SKuninori Morimoto 
3673a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3674a7930ed4SKuninori Morimoto 	case 0x11:
3675a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3676a7930ed4SKuninori Morimoto 		break;
3677a7930ed4SKuninori Morimoto 	case 0x10:
3678a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3679a7930ed4SKuninori Morimoto 		break;
3680a7930ed4SKuninori Morimoto 	case 0x01:
3681a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3682a7930ed4SKuninori Morimoto 		break;
3683a7930ed4SKuninori Morimoto 	default:
3684a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3685a7930ed4SKuninori Morimoto 		break;
3686a7930ed4SKuninori Morimoto 	}
3687a7930ed4SKuninori Morimoto 
3688a7930ed4SKuninori Morimoto 	/*
3689a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3690a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3691a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3692a7930ed4SKuninori Morimoto 	 */
3693a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3694a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3695389cb834SJyri Sarha 	if (bit && bitclkmaster)
3696389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3697a7930ed4SKuninori Morimoto 
3698a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3699a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3700389cb834SJyri Sarha 	if (frame && framemaster)
3701389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3702a7930ed4SKuninori Morimoto 
3703a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3704a7930ed4SKuninori Morimoto 	case 0x11:
3705a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3706a7930ed4SKuninori Morimoto 		break;
3707a7930ed4SKuninori Morimoto 	case 0x10:
3708a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3709a7930ed4SKuninori Morimoto 		break;
3710a7930ed4SKuninori Morimoto 	case 0x01:
3711a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3712a7930ed4SKuninori Morimoto 		break;
3713a7930ed4SKuninori Morimoto 	default:
3714a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3715a7930ed4SKuninori Morimoto 		break;
3716a7930ed4SKuninori Morimoto 	}
3717a7930ed4SKuninori Morimoto 
3718a7930ed4SKuninori Morimoto 	return format;
3719a7930ed4SKuninori Morimoto }
3720a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3721a7930ed4SKuninori Morimoto 
3722cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node,
3723cb470087SKuninori Morimoto 			    const char **dai_name)
3724cb470087SKuninori Morimoto {
3725cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
3726cb470087SKuninori Morimoto 	struct of_phandle_args args;
3727cb470087SKuninori Morimoto 	int ret;
3728cb470087SKuninori Morimoto 
3729cb470087SKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
3730cb470087SKuninori Morimoto 					 "#sound-dai-cells", 0, &args);
3731cb470087SKuninori Morimoto 	if (ret)
3732cb470087SKuninori Morimoto 		return ret;
3733cb470087SKuninori Morimoto 
3734cb470087SKuninori Morimoto 	ret = -EPROBE_DEFER;
3735cb470087SKuninori Morimoto 
3736cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3737cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
3738cb470087SKuninori Morimoto 		if (pos->dev->of_node != args.np)
3739cb470087SKuninori Morimoto 			continue;
3740cb470087SKuninori Morimoto 
37416833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
37426833c452SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
37436833c452SKuninori Morimoto 		} else {
37446833c452SKuninori Morimoto 			int id = -1;
37456833c452SKuninori Morimoto 
37466833c452SKuninori Morimoto 			switch (args.args_count) {
37476833c452SKuninori Morimoto 			case 0:
37486833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
37496833c452SKuninori Morimoto 				break;
37506833c452SKuninori Morimoto 			case 1:
37516833c452SKuninori Morimoto 				id = args.args[0];
37526833c452SKuninori Morimoto 				break;
37536833c452SKuninori Morimoto 			default:
37546833c452SKuninori Morimoto 				/* not supported */
3755cb470087SKuninori Morimoto 				break;
3756cb470087SKuninori Morimoto 			}
3757cb470087SKuninori Morimoto 
37586833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
37596833c452SKuninori Morimoto 				ret = -EINVAL;
37603dcba280SNicolin Chen 				continue;
37616833c452SKuninori Morimoto 			}
3762e41975edSXiubo Li 
3763e41975edSXiubo Li 			ret = 0;
3764e41975edSXiubo Li 
3765e41975edSXiubo Li 			*dai_name = pos->dai_drv[id].name;
3766e41975edSXiubo Li 			if (!*dai_name)
3767e41975edSXiubo Li 				*dai_name = pos->name;
37686833c452SKuninori Morimoto 		}
37696833c452SKuninori Morimoto 
3770cb470087SKuninori Morimoto 		break;
3771cb470087SKuninori Morimoto 	}
3772cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
3773cb470087SKuninori Morimoto 
3774cb470087SKuninori Morimoto 	of_node_put(args.np);
3775cb470087SKuninori Morimoto 
3776cb470087SKuninori Morimoto 	return ret;
3777cb470087SKuninori Morimoto }
3778cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3779cb470087SKuninori Morimoto 
3780c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3781db2a4165SFrank Mandarino {
3782db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
37838a9dab1aSMark Brown 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
37848a9dab1aSMark Brown 	if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
37850837fc62SFabio Estevam 		pr_warn("ASoC: Failed to create debugfs directory\n");
37868a9dab1aSMark Brown 		snd_soc_debugfs_root = NULL;
3787db2a4165SFrank Mandarino 	}
3788c3c5a19aSMark Brown 
37898a9dab1aSMark Brown 	if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3790c3c5a19aSMark Brown 				 &codec_list_fops))
3791c3c5a19aSMark Brown 		pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3792c3c5a19aSMark Brown 
37938a9dab1aSMark Brown 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3794f3208780SMark Brown 				 &dai_list_fops))
3795f3208780SMark Brown 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
379619c7ac27SMark Brown 
37978a9dab1aSMark Brown 	if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
379819c7ac27SMark Brown 				 &platform_list_fops))
379919c7ac27SMark Brown 		pr_warn("ASoC: Failed to create platform list debugfs file\n");
3800db2a4165SFrank Mandarino #endif
3801db2a4165SFrank Mandarino 
3802fb257897SMark Brown 	snd_soc_util_init();
3803fb257897SMark Brown 
3804db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3805db2a4165SFrank Mandarino }
38064abe8e16SMark Brown module_init(snd_soc_init);
3807db2a4165SFrank Mandarino 
3808db2a4165SFrank Mandarino static void __exit snd_soc_exit(void)
3809db2a4165SFrank Mandarino {
3810fb257897SMark Brown 	snd_soc_util_exit();
3811fb257897SMark Brown 
3812db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS
38138a9dab1aSMark Brown 	debugfs_remove_recursive(snd_soc_debugfs_root);
3814db2a4165SFrank Mandarino #endif
3815db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3816db2a4165SFrank Mandarino }
3817db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3818db2a4165SFrank Mandarino 
3819db2a4165SFrank Mandarino /* Module information */
3820db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3821db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3822db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3823db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3824