xref: /openbmc/linux/sound/soc/soc-core.c (revision 14dc5734bdac2629ed4228f3d30662bb440a3982)
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.
60664d888SLiam Girdwood  *
7d331124dSLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
80664d888SLiam Girdwood  *         with code, comments and ideas from :-
90664d888SLiam Girdwood  *         Richard Purdie <richard@openedhand.com>
10db2a4165SFrank Mandarino  *
11db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
12db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
13db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
14db2a4165SFrank Mandarino  *  option) any later version.
15db2a4165SFrank Mandarino  *
16db2a4165SFrank Mandarino  *  TODO:
17db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
18db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
19db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
20db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
21db2a4165SFrank Mandarino  */
22db2a4165SFrank Mandarino 
23db2a4165SFrank Mandarino #include <linux/module.h>
24db2a4165SFrank Mandarino #include <linux/moduleparam.h>
25db2a4165SFrank Mandarino #include <linux/init.h>
26db2a4165SFrank Mandarino #include <linux/delay.h>
27db2a4165SFrank Mandarino #include <linux/pm.h>
28db2a4165SFrank Mandarino #include <linux/bitops.h>
2912ef193dSTroy Kisky #include <linux/debugfs.h>
30db2a4165SFrank Mandarino #include <linux/platform_device.h>
31474828a4SMarek Vasut #include <sound/ac97_codec.h>
32db2a4165SFrank Mandarino #include <sound/core.h>
33db2a4165SFrank Mandarino #include <sound/pcm.h>
34db2a4165SFrank Mandarino #include <sound/pcm_params.h>
35db2a4165SFrank Mandarino #include <sound/soc.h>
36db2a4165SFrank Mandarino #include <sound/soc-dapm.h>
37db2a4165SFrank Mandarino #include <sound/initval.h>
38db2a4165SFrank Mandarino 
39db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex);
40db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41db2a4165SFrank Mandarino 
42384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
43384c89e2SMark Brown static struct dentry *debugfs_root;
44384c89e2SMark Brown #endif
45384c89e2SMark Brown 
46c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
47c5af3a2eSMark Brown static LIST_HEAD(card_list);
489115171aSMark Brown static LIST_HEAD(dai_list);
4912a48a8cSMark Brown static LIST_HEAD(platform_list);
500d0cf00aSMark Brown static LIST_HEAD(codec_list);
51c5af3a2eSMark Brown 
52c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card);
53c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card);
54c5af3a2eSMark Brown 
55db2a4165SFrank Mandarino /*
56db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
57db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
58db2a4165SFrank Mandarino  * between two audio tracks.
59db2a4165SFrank Mandarino  */
60db2a4165SFrank Mandarino static int pmdown_time = 5000;
61db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
62db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
63db2a4165SFrank Mandarino 
64965ac42cSLiam Girdwood /*
65965ac42cSLiam Girdwood  * This function forces any delayed work to be queued and run.
66965ac42cSLiam Girdwood  */
67965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork)
68965ac42cSLiam Girdwood {
69965ac42cSLiam Girdwood 	int ret;
70965ac42cSLiam Girdwood 
71965ac42cSLiam Girdwood 	/* cancel any work waiting to be queued. */
72965ac42cSLiam Girdwood 	ret = cancel_delayed_work(dwork);
73965ac42cSLiam Girdwood 
74965ac42cSLiam Girdwood 	/* if there was any work waiting then we run it now and
75965ac42cSLiam Girdwood 	 * wait for it's completion */
76965ac42cSLiam Girdwood 	if (ret) {
77965ac42cSLiam Girdwood 		schedule_delayed_work(dwork, 0);
78965ac42cSLiam Girdwood 		flush_scheduled_work();
79965ac42cSLiam Girdwood 	}
80965ac42cSLiam Girdwood 	return ret;
81965ac42cSLiam Girdwood }
82965ac42cSLiam Girdwood 
832624d5faSMark Brown /* codec register dump */
842624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
852624d5faSMark Brown {
862624d5faSMark Brown 	int i, step = 1, count = 0;
872624d5faSMark Brown 
882624d5faSMark Brown 	if (!codec->reg_cache_size)
892624d5faSMark Brown 		return 0;
902624d5faSMark Brown 
912624d5faSMark Brown 	if (codec->reg_cache_step)
922624d5faSMark Brown 		step = codec->reg_cache_step;
932624d5faSMark Brown 
942624d5faSMark Brown 	count += sprintf(buf, "%s registers\n", codec->name);
952624d5faSMark Brown 	for (i = 0; i < codec->reg_cache_size; i += step) {
962624d5faSMark Brown 		if (codec->readable_register && !codec->readable_register(i))
972624d5faSMark Brown 			continue;
982624d5faSMark Brown 
992624d5faSMark Brown 		count += sprintf(buf + count, "%2x: ", i);
1002624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1012624d5faSMark Brown 			break;
1022624d5faSMark Brown 
1032624d5faSMark Brown 		if (codec->display_register)
1042624d5faSMark Brown 			count += codec->display_register(codec, buf + count,
1052624d5faSMark Brown 							 PAGE_SIZE - count, i);
1062624d5faSMark Brown 		else
1072624d5faSMark Brown 			count += snprintf(buf + count, PAGE_SIZE - count,
1082624d5faSMark Brown 					  "%4x", codec->read(codec, i));
1092624d5faSMark Brown 
1102624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1112624d5faSMark Brown 			break;
1122624d5faSMark Brown 
1132624d5faSMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1142624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1152624d5faSMark Brown 			break;
1162624d5faSMark Brown 	}
1172624d5faSMark Brown 
1182624d5faSMark Brown 	/* Truncate count; min() would cause a warning */
1192624d5faSMark Brown 	if (count >= PAGE_SIZE)
1202624d5faSMark Brown 		count = PAGE_SIZE - 1;
1212624d5faSMark Brown 
1222624d5faSMark Brown 	return count;
1232624d5faSMark Brown }
1242624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1252624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1262624d5faSMark Brown {
1272624d5faSMark Brown 	struct snd_soc_device *devdata = dev_get_drvdata(dev);
1282624d5faSMark Brown 	return soc_codec_reg_show(devdata->card->codec, buf);
1292624d5faSMark Brown }
1302624d5faSMark Brown 
1312624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1322624d5faSMark Brown 
133dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
134dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
135dbe21408SMark Brown {
136dbe21408SMark Brown 	struct snd_soc_device *socdev = dev_get_drvdata(dev);
137dbe21408SMark Brown 	struct snd_soc_card *card = socdev->card;
138dbe21408SMark Brown 
1396c5f1fedSMark Brown 	return sprintf(buf, "%ld\n", card->pmdown_time);
140dbe21408SMark Brown }
141dbe21408SMark Brown 
142dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
143dbe21408SMark Brown 			       struct device_attribute *attr,
144dbe21408SMark Brown 			       const char *buf, size_t count)
145dbe21408SMark Brown {
146dbe21408SMark Brown 	struct snd_soc_device *socdev = dev_get_drvdata(dev);
147dbe21408SMark Brown 	struct snd_soc_card *card = socdev->card;
148dbe21408SMark Brown 
149dbe21408SMark Brown 	strict_strtol(buf, 10, &card->pmdown_time);
150dbe21408SMark Brown 
151dbe21408SMark Brown 	return count;
152dbe21408SMark Brown }
153dbe21408SMark Brown 
154dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
155dbe21408SMark Brown 
1562624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
1572624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
1582624d5faSMark Brown {
1592624d5faSMark Brown 	file->private_data = inode->i_private;
1602624d5faSMark Brown 	return 0;
1612624d5faSMark Brown }
1622624d5faSMark Brown 
1632624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1642624d5faSMark Brown 			       size_t count, loff_t *ppos)
1652624d5faSMark Brown {
1662624d5faSMark Brown 	ssize_t ret;
1672624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1682624d5faSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1692624d5faSMark Brown 	if (!buf)
1702624d5faSMark Brown 		return -ENOMEM;
1712624d5faSMark Brown 	ret = soc_codec_reg_show(codec, buf);
1722624d5faSMark Brown 	if (ret >= 0)
1732624d5faSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1742624d5faSMark Brown 	kfree(buf);
1752624d5faSMark Brown 	return ret;
1762624d5faSMark Brown }
1772624d5faSMark Brown 
1782624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
1792624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
1802624d5faSMark Brown {
1812624d5faSMark Brown 	char buf[32];
1822624d5faSMark Brown 	int buf_size;
1832624d5faSMark Brown 	char *start = buf;
1842624d5faSMark Brown 	unsigned long reg, value;
1852624d5faSMark Brown 	int step = 1;
1862624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1872624d5faSMark Brown 
1882624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
1892624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
1902624d5faSMark Brown 		return -EFAULT;
1912624d5faSMark Brown 	buf[buf_size] = 0;
1922624d5faSMark Brown 
1932624d5faSMark Brown 	if (codec->reg_cache_step)
1942624d5faSMark Brown 		step = codec->reg_cache_step;
1952624d5faSMark Brown 
1962624d5faSMark Brown 	while (*start == ' ')
1972624d5faSMark Brown 		start++;
1982624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
1992624d5faSMark Brown 	if ((reg >= codec->reg_cache_size) || (reg % step))
2002624d5faSMark Brown 		return -EINVAL;
2012624d5faSMark Brown 	while (*start == ' ')
2022624d5faSMark Brown 		start++;
2032624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2042624d5faSMark Brown 		return -EINVAL;
2052624d5faSMark Brown 	codec->write(codec, reg, value);
2062624d5faSMark Brown 	return buf_size;
2072624d5faSMark Brown }
2082624d5faSMark Brown 
2092624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2102624d5faSMark Brown 	.open = codec_reg_open_file,
2112624d5faSMark Brown 	.read = codec_reg_read_file,
2122624d5faSMark Brown 	.write = codec_reg_write_file,
2132624d5faSMark Brown };
2142624d5faSMark Brown 
2152624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2162624d5faSMark Brown {
2172624d5faSMark Brown 	char codec_root[128];
2182624d5faSMark Brown 
2192624d5faSMark Brown 	if (codec->dev)
2202624d5faSMark Brown 		snprintf(codec_root, sizeof(codec_root),
2212624d5faSMark Brown 			"%s.%s", codec->name, dev_name(codec->dev));
2222624d5faSMark Brown 	else
2232624d5faSMark Brown 		snprintf(codec_root, sizeof(codec_root),
2242624d5faSMark Brown 			"%s", codec->name);
2252624d5faSMark Brown 
2262624d5faSMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec_root,
2272624d5faSMark Brown 						       debugfs_root);
2282624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2292624d5faSMark Brown 		printk(KERN_WARNING
2302624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2312624d5faSMark Brown 		return;
2322624d5faSMark Brown 	}
2332624d5faSMark Brown 
2342624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2352624d5faSMark Brown 						 codec->debugfs_codec_root,
2362624d5faSMark Brown 						 codec, &codec_reg_fops);
2372624d5faSMark Brown 	if (!codec->debugfs_reg)
2382624d5faSMark Brown 		printk(KERN_WARNING
2392624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
2402624d5faSMark Brown 
2412624d5faSMark Brown 	codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
2422624d5faSMark Brown 						     codec->debugfs_codec_root,
2432624d5faSMark Brown 						     &codec->pop_time);
2442624d5faSMark Brown 	if (!codec->debugfs_pop_time)
2452624d5faSMark Brown 		printk(KERN_WARNING
2462624d5faSMark Brown 		       "Failed to create pop time debugfs file\n");
2472624d5faSMark Brown 
2482624d5faSMark Brown 	codec->debugfs_dapm = debugfs_create_dir("dapm",
2492624d5faSMark Brown 						 codec->debugfs_codec_root);
2502624d5faSMark Brown 	if (!codec->debugfs_dapm)
2512624d5faSMark Brown 		printk(KERN_WARNING
2522624d5faSMark Brown 		       "Failed to create DAPM debugfs directory\n");
2532624d5faSMark Brown 
2542624d5faSMark Brown 	snd_soc_dapm_debugfs_init(codec);
2552624d5faSMark Brown }
2562624d5faSMark Brown 
2572624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2582624d5faSMark Brown {
2592624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2602624d5faSMark Brown }
2612624d5faSMark Brown 
2622624d5faSMark Brown #else
2632624d5faSMark Brown 
2642624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2652624d5faSMark Brown {
2662624d5faSMark Brown }
2672624d5faSMark Brown 
2682624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2692624d5faSMark Brown {
2702624d5faSMark Brown }
2712624d5faSMark Brown #endif
2722624d5faSMark Brown 
273db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
274db2a4165SFrank Mandarino /* unregister ac97 codec */
275db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
276db2a4165SFrank Mandarino {
277db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
278db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
279db2a4165SFrank Mandarino 	return 0;
280db2a4165SFrank Mandarino }
281db2a4165SFrank Mandarino 
282db2a4165SFrank Mandarino /* stop no dev release warning */
283db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
284db2a4165SFrank Mandarino 
285db2a4165SFrank Mandarino /* register ac97 codec to bus */
286db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
287db2a4165SFrank Mandarino {
288db2a4165SFrank Mandarino 	int err;
289db2a4165SFrank Mandarino 
290db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
2914ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
292db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
293db2a4165SFrank Mandarino 
294bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
295db2a4165SFrank Mandarino 		     codec->card->number, 0, codec->name);
296db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
297db2a4165SFrank Mandarino 	if (err < 0) {
298db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
299db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
300db2a4165SFrank Mandarino 		return err;
301db2a4165SFrank Mandarino 	}
302db2a4165SFrank Mandarino 	return 0;
303db2a4165SFrank Mandarino }
304db2a4165SFrank Mandarino #endif
305db2a4165SFrank Mandarino 
30606f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
30706f409d7SMark Brown {
30806f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
30906f409d7SMark Brown 	struct snd_soc_device *socdev = rtd->socdev;
31006f409d7SMark Brown 	struct snd_soc_card *card = socdev->card;
31106f409d7SMark Brown 	struct snd_soc_dai_link *machine = rtd->dai;
31206f409d7SMark Brown 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
31306f409d7SMark Brown 	struct snd_soc_dai *codec_dai = machine->codec_dai;
31406f409d7SMark Brown 	int ret;
31506f409d7SMark Brown 
31606f409d7SMark Brown 	if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
31706f409d7SMark Brown 	    machine->symmetric_rates) {
31806f409d7SMark Brown 		dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
31906f409d7SMark Brown 			machine->rate);
32006f409d7SMark Brown 
32106f409d7SMark Brown 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
32206f409d7SMark Brown 						   SNDRV_PCM_HW_PARAM_RATE,
32306f409d7SMark Brown 						   machine->rate,
32406f409d7SMark Brown 						   machine->rate);
32506f409d7SMark Brown 		if (ret < 0) {
32606f409d7SMark Brown 			dev_err(card->dev,
32706f409d7SMark Brown 				"Unable to apply rate symmetry constraint: %d\n", ret);
32806f409d7SMark Brown 			return ret;
32906f409d7SMark Brown 		}
33006f409d7SMark Brown 	}
33106f409d7SMark Brown 
33206f409d7SMark Brown 	return 0;
33306f409d7SMark Brown }
33406f409d7SMark Brown 
335db2a4165SFrank Mandarino /*
336db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
337db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
338db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
339db2a4165SFrank Mandarino  */
340db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
341db2a4165SFrank Mandarino {
342db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
343db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
34487689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
345db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
346cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
34787689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
3483c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
3493c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
350db2a4165SFrank Mandarino 	int ret = 0;
351db2a4165SFrank Mandarino 
352db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
353db2a4165SFrank Mandarino 
354db2a4165SFrank Mandarino 	/* startup the audio subsystem */
3556335d055SEric Miao 	if (cpu_dai->ops->startup) {
3566335d055SEric Miao 		ret = cpu_dai->ops->startup(substream, cpu_dai);
357db2a4165SFrank Mandarino 		if (ret < 0) {
358db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
359cb666e5bSLiam Girdwood 				cpu_dai->name);
360db2a4165SFrank Mandarino 			goto out;
361db2a4165SFrank Mandarino 		}
362db2a4165SFrank Mandarino 	}
363db2a4165SFrank Mandarino 
364db2a4165SFrank Mandarino 	if (platform->pcm_ops->open) {
365db2a4165SFrank Mandarino 		ret = platform->pcm_ops->open(substream);
366db2a4165SFrank Mandarino 		if (ret < 0) {
367db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
368db2a4165SFrank Mandarino 			goto platform_err;
369db2a4165SFrank Mandarino 		}
370db2a4165SFrank Mandarino 	}
371db2a4165SFrank Mandarino 
3726335d055SEric Miao 	if (codec_dai->ops->startup) {
3736335d055SEric Miao 		ret = codec_dai->ops->startup(substream, codec_dai);
374cb666e5bSLiam Girdwood 		if (ret < 0) {
375cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
376cb666e5bSLiam Girdwood 				codec_dai->name);
377cb666e5bSLiam Girdwood 			goto codec_dai_err;
378cb666e5bSLiam Girdwood 		}
379cb666e5bSLiam Girdwood 	}
380cb666e5bSLiam Girdwood 
381db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->startup) {
382db2a4165SFrank Mandarino 		ret = machine->ops->startup(substream);
383db2a4165SFrank Mandarino 		if (ret < 0) {
384db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
385db2a4165SFrank Mandarino 			goto machine_err;
386db2a4165SFrank Mandarino 		}
387db2a4165SFrank Mandarino 	}
388db2a4165SFrank Mandarino 
389db2a4165SFrank Mandarino 	/* Check that the codec and cpu DAI's are compatible */
390db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
391db2a4165SFrank Mandarino 		runtime->hw.rate_min =
3923ff3f64bSMark Brown 			max(codec_dai->playback.rate_min,
3933ff3f64bSMark Brown 			    cpu_dai->playback.rate_min);
394db2a4165SFrank Mandarino 		runtime->hw.rate_max =
3953ff3f64bSMark Brown 			min(codec_dai->playback.rate_max,
3963ff3f64bSMark Brown 			    cpu_dai->playback.rate_max);
397db2a4165SFrank Mandarino 		runtime->hw.channels_min =
398cb666e5bSLiam Girdwood 			max(codec_dai->playback.channels_min,
399cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_min);
400db2a4165SFrank Mandarino 		runtime->hw.channels_max =
401cb666e5bSLiam Girdwood 			min(codec_dai->playback.channels_max,
402cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_max);
403cb666e5bSLiam Girdwood 		runtime->hw.formats =
404cb666e5bSLiam Girdwood 			codec_dai->playback.formats & cpu_dai->playback.formats;
405cb666e5bSLiam Girdwood 		runtime->hw.rates =
406cb666e5bSLiam Girdwood 			codec_dai->playback.rates & cpu_dai->playback.rates;
407db2a4165SFrank Mandarino 	} else {
408db2a4165SFrank Mandarino 		runtime->hw.rate_min =
4093ff3f64bSMark Brown 			max(codec_dai->capture.rate_min,
4103ff3f64bSMark Brown 			    cpu_dai->capture.rate_min);
411db2a4165SFrank Mandarino 		runtime->hw.rate_max =
4123ff3f64bSMark Brown 			min(codec_dai->capture.rate_max,
4133ff3f64bSMark Brown 			    cpu_dai->capture.rate_max);
414db2a4165SFrank Mandarino 		runtime->hw.channels_min =
415cb666e5bSLiam Girdwood 			max(codec_dai->capture.channels_min,
416cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_min);
417db2a4165SFrank Mandarino 		runtime->hw.channels_max =
418cb666e5bSLiam Girdwood 			min(codec_dai->capture.channels_max,
419cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_max);
420cb666e5bSLiam Girdwood 		runtime->hw.formats =
421cb666e5bSLiam Girdwood 			codec_dai->capture.formats & cpu_dai->capture.formats;
422cb666e5bSLiam Girdwood 		runtime->hw.rates =
423cb666e5bSLiam Girdwood 			codec_dai->capture.rates & cpu_dai->capture.rates;
424db2a4165SFrank Mandarino 	}
425db2a4165SFrank Mandarino 
426db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
427db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
428db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
429cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
430cb666e5bSLiam Girdwood 		goto machine_err;
431db2a4165SFrank Mandarino 	}
432db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
433db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
434cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
435cb666e5bSLiam Girdwood 		goto machine_err;
436db2a4165SFrank Mandarino 	}
437db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
438db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
439cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
440cb666e5bSLiam Girdwood 		goto machine_err;
441db2a4165SFrank Mandarino 	}
442db2a4165SFrank Mandarino 
44306f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
44406f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
44506f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
44606f409d7SMark Brown 		if (ret != 0)
44706f409d7SMark Brown 			goto machine_err;
44806f409d7SMark Brown 	}
44906f409d7SMark Brown 
450f24368c2SMark Brown 	pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
451f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
452f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
453b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
454f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
455b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
456db2a4165SFrank Mandarino 
457*14dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
458*14dc5734SJassi Brar 		cpu_dai->playback.active++;
459*14dc5734SJassi Brar 		codec_dai->playback.active++;
460*14dc5734SJassi Brar 	} else {
461*14dc5734SJassi Brar 		cpu_dai->capture.active++;
462*14dc5734SJassi Brar 		codec_dai->capture.active++;
463*14dc5734SJassi Brar 	}
464*14dc5734SJassi Brar 	cpu_dai->active++;
465*14dc5734SJassi Brar 	codec_dai->active++;
4666627a653SMark Brown 	card->codec->active++;
467db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
468db2a4165SFrank Mandarino 	return 0;
469db2a4165SFrank Mandarino 
470cb666e5bSLiam Girdwood machine_err:
471db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
472db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
473db2a4165SFrank Mandarino 
474cb666e5bSLiam Girdwood codec_dai_err:
475db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
476db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
477db2a4165SFrank Mandarino 
478db2a4165SFrank Mandarino platform_err:
4796335d055SEric Miao 	if (cpu_dai->ops->shutdown)
4806335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
481db2a4165SFrank Mandarino out:
482db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
483db2a4165SFrank Mandarino 	return ret;
484db2a4165SFrank Mandarino }
485db2a4165SFrank Mandarino 
486db2a4165SFrank Mandarino /*
4873a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
488db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
489db2a4165SFrank Mandarino  * due to DAPM power cycling.
490db2a4165SFrank Mandarino  */
4914484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
492db2a4165SFrank Mandarino {
4936308419aSMark Brown 	struct snd_soc_card *card = container_of(work, struct snd_soc_card,
4946308419aSMark Brown 						 delayed_work.work);
4956627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
4963c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
497db2a4165SFrank Mandarino 	int i;
498db2a4165SFrank Mandarino 
499db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
500db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
501db2a4165SFrank Mandarino 		codec_dai = &codec->dai[i];
502db2a4165SFrank Mandarino 
503f24368c2SMark Brown 		pr_debug("pop wq checking: %s status: %s waiting: %s\n",
504db2a4165SFrank Mandarino 			 codec_dai->playback.stream_name,
505db2a4165SFrank Mandarino 			 codec_dai->playback.active ? "active" : "inactive",
506db2a4165SFrank Mandarino 			 codec_dai->pop_wait ? "yes" : "no");
507db2a4165SFrank Mandarino 
508db2a4165SFrank Mandarino 		/* are we waiting on this codec DAI stream */
509db2a4165SFrank Mandarino 		if (codec_dai->pop_wait == 1) {
510db2a4165SFrank Mandarino 			codec_dai->pop_wait = 0;
5110b4d221bSLiam Girdwood 			snd_soc_dapm_stream_event(codec,
5120b4d221bSLiam Girdwood 				codec_dai->playback.stream_name,
513db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_STOP);
514db2a4165SFrank Mandarino 		}
515db2a4165SFrank Mandarino 	}
516db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
517db2a4165SFrank Mandarino }
518db2a4165SFrank Mandarino 
519db2a4165SFrank Mandarino /*
520db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
521db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
522db2a4165SFrank Mandarino  * shutdown.
523db2a4165SFrank Mandarino  */
524db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
525db2a4165SFrank Mandarino {
526db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
527db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
5286308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
529cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
53087689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
5313c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5323c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
5336627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
534db2a4165SFrank Mandarino 
535db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
536db2a4165SFrank Mandarino 
537*14dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
538*14dc5734SJassi Brar 		cpu_dai->playback.active--;
539*14dc5734SJassi Brar 		codec_dai->playback.active--;
540*14dc5734SJassi Brar 	} else {
541*14dc5734SJassi Brar 		cpu_dai->capture.active--;
542*14dc5734SJassi Brar 		codec_dai->capture.active--;
543db2a4165SFrank Mandarino 	}
544*14dc5734SJassi Brar 
545*14dc5734SJassi Brar 	cpu_dai->active--;
546*14dc5734SJassi Brar 	codec_dai->active--;
547db2a4165SFrank Mandarino 	codec->active--;
548db2a4165SFrank Mandarino 
5496010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
5506010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
5516010b2daSMark Brown 	 */
5526010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5536010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
5546010b2daSMark Brown 
5556335d055SEric Miao 	if (cpu_dai->ops->shutdown)
5566335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
557db2a4165SFrank Mandarino 
5586335d055SEric Miao 	if (codec_dai->ops->shutdown)
5596335d055SEric Miao 		codec_dai->ops->shutdown(substream, codec_dai);
560db2a4165SFrank Mandarino 
561db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
562db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
563db2a4165SFrank Mandarino 
564db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
565db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
566db2a4165SFrank Mandarino 
567db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
568db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
569cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
5706308419aSMark Brown 		schedule_delayed_work(&card->delayed_work,
57196dd3622SMark Brown 			msecs_to_jiffies(card->pmdown_time));
572db2a4165SFrank Mandarino 	} else {
573db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
574cb666e5bSLiam Girdwood 		snd_soc_dapm_stream_event(codec,
5750b4d221bSLiam Girdwood 			codec_dai->capture.stream_name,
5760b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
577db2a4165SFrank Mandarino 	}
578db2a4165SFrank Mandarino 
579db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
580db2a4165SFrank Mandarino 	return 0;
581db2a4165SFrank Mandarino }
582db2a4165SFrank Mandarino 
583db2a4165SFrank Mandarino /*
584db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
585db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
586db2a4165SFrank Mandarino  * it can refer to the runtime info.
587db2a4165SFrank Mandarino  */
588db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
589db2a4165SFrank Mandarino {
590db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
591db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
5926308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
593cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
59487689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
5953c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5963c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
5976627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
598db2a4165SFrank Mandarino 	int ret = 0;
599db2a4165SFrank Mandarino 
600db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
601cb666e5bSLiam Girdwood 
602cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->prepare) {
603cb666e5bSLiam Girdwood 		ret = machine->ops->prepare(substream);
604cb666e5bSLiam Girdwood 		if (ret < 0) {
605cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
606cb666e5bSLiam Girdwood 			goto out;
607cb666e5bSLiam Girdwood 		}
608cb666e5bSLiam Girdwood 	}
609cb666e5bSLiam Girdwood 
610db2a4165SFrank Mandarino 	if (platform->pcm_ops->prepare) {
611db2a4165SFrank Mandarino 		ret = platform->pcm_ops->prepare(substream);
612a71a468aSLiam Girdwood 		if (ret < 0) {
613a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
614db2a4165SFrank Mandarino 			goto out;
615db2a4165SFrank Mandarino 		}
616a71a468aSLiam Girdwood 	}
617db2a4165SFrank Mandarino 
6186335d055SEric Miao 	if (codec_dai->ops->prepare) {
6196335d055SEric Miao 		ret = codec_dai->ops->prepare(substream, codec_dai);
620a71a468aSLiam Girdwood 		if (ret < 0) {
621a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
622db2a4165SFrank Mandarino 			goto out;
623db2a4165SFrank Mandarino 		}
624a71a468aSLiam Girdwood 	}
625db2a4165SFrank Mandarino 
6266335d055SEric Miao 	if (cpu_dai->ops->prepare) {
6276335d055SEric Miao 		ret = cpu_dai->ops->prepare(substream, cpu_dai);
628cb666e5bSLiam Girdwood 		if (ret < 0) {
629cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
630cb666e5bSLiam Girdwood 			goto out;
631cb666e5bSLiam Girdwood 		}
632cb666e5bSLiam Girdwood 	}
633db2a4165SFrank Mandarino 
634d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
635d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
636d45f6219SMark Brown 	    codec_dai->pop_wait) {
637cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
6386308419aSMark Brown 		cancel_delayed_work(&card->delayed_work);
639db2a4165SFrank Mandarino 	}
640db2a4165SFrank Mandarino 
641db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
642db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
643cb666e5bSLiam Girdwood 					  codec_dai->playback.stream_name,
644db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
645db2a4165SFrank Mandarino 	else
646db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
647cb666e5bSLiam Girdwood 					  codec_dai->capture.stream_name,
648db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
6498c6529dbSLiam Girdwood 
6508c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
651db2a4165SFrank Mandarino 
652db2a4165SFrank Mandarino out:
653db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
654db2a4165SFrank Mandarino 	return ret;
655db2a4165SFrank Mandarino }
656db2a4165SFrank Mandarino 
657db2a4165SFrank Mandarino /*
658db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
659db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
660db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
661db2a4165SFrank Mandarino  */
662db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
663db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
664db2a4165SFrank Mandarino {
665db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
666db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
667cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
66887689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
66987689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
6703c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
6713c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
672db2a4165SFrank Mandarino 	int ret = 0;
673db2a4165SFrank Mandarino 
674db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
675db2a4165SFrank Mandarino 
676cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_params) {
677cb666e5bSLiam Girdwood 		ret = machine->ops->hw_params(substream, params);
678cb666e5bSLiam Girdwood 		if (ret < 0) {
679cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
680db2a4165SFrank Mandarino 			goto out;
681db2a4165SFrank Mandarino 		}
682cb666e5bSLiam Girdwood 	}
683db2a4165SFrank Mandarino 
6846335d055SEric Miao 	if (codec_dai->ops->hw_params) {
6856335d055SEric Miao 		ret = codec_dai->ops->hw_params(substream, params, codec_dai);
686db2a4165SFrank Mandarino 		if (ret < 0) {
687db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
688cb666e5bSLiam Girdwood 				codec_dai->name);
689cb666e5bSLiam Girdwood 			goto codec_err;
690db2a4165SFrank Mandarino 		}
691db2a4165SFrank Mandarino 	}
692db2a4165SFrank Mandarino 
6936335d055SEric Miao 	if (cpu_dai->ops->hw_params) {
6946335d055SEric Miao 		ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
695db2a4165SFrank Mandarino 		if (ret < 0) {
6963ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
697cb666e5bSLiam Girdwood 				cpu_dai->name);
698db2a4165SFrank Mandarino 			goto interface_err;
699db2a4165SFrank Mandarino 		}
700db2a4165SFrank Mandarino 	}
701db2a4165SFrank Mandarino 
702db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_params) {
703db2a4165SFrank Mandarino 		ret = platform->pcm_ops->hw_params(substream, params);
704db2a4165SFrank Mandarino 		if (ret < 0) {
7053ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
706db2a4165SFrank Mandarino 				platform->name);
707db2a4165SFrank Mandarino 			goto platform_err;
708db2a4165SFrank Mandarino 		}
709db2a4165SFrank Mandarino 	}
710db2a4165SFrank Mandarino 
71106f409d7SMark Brown 	machine->rate = params_rate(params);
71206f409d7SMark Brown 
713db2a4165SFrank Mandarino out:
714db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
715db2a4165SFrank Mandarino 	return ret;
716db2a4165SFrank Mandarino 
717db2a4165SFrank Mandarino platform_err:
7186335d055SEric Miao 	if (cpu_dai->ops->hw_free)
7196335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
720db2a4165SFrank Mandarino 
721db2a4165SFrank Mandarino interface_err:
7226335d055SEric Miao 	if (codec_dai->ops->hw_free)
7236335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
724cb666e5bSLiam Girdwood 
725cb666e5bSLiam Girdwood codec_err:
726cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_free)
727cb666e5bSLiam Girdwood 		machine->ops->hw_free(substream);
728db2a4165SFrank Mandarino 
729db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
730db2a4165SFrank Mandarino 	return ret;
731db2a4165SFrank Mandarino }
732db2a4165SFrank Mandarino 
733db2a4165SFrank Mandarino /*
734db2a4165SFrank Mandarino  * Free's resources allocated by hw_params, can be called multiple times
735db2a4165SFrank Mandarino  */
736db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
737db2a4165SFrank Mandarino {
738db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
739db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
740cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
74187689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
74287689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
7433c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
7443c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
7456627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
746db2a4165SFrank Mandarino 
747db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
748db2a4165SFrank Mandarino 
749db2a4165SFrank Mandarino 	/* apply codec digital mute */
7508c6529dbSLiam Girdwood 	if (!codec->active)
7518c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
752db2a4165SFrank Mandarino 
753db2a4165SFrank Mandarino 	/* free any machine hw params */
754db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->hw_free)
755db2a4165SFrank Mandarino 		machine->ops->hw_free(substream);
756db2a4165SFrank Mandarino 
757db2a4165SFrank Mandarino 	/* free any DMA resources */
758db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_free)
759db2a4165SFrank Mandarino 		platform->pcm_ops->hw_free(substream);
760db2a4165SFrank Mandarino 
761db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
7626335d055SEric Miao 	if (codec_dai->ops->hw_free)
7636335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
764db2a4165SFrank Mandarino 
7656335d055SEric Miao 	if (cpu_dai->ops->hw_free)
7666335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
767db2a4165SFrank Mandarino 
768db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
769db2a4165SFrank Mandarino 	return 0;
770db2a4165SFrank Mandarino }
771db2a4165SFrank Mandarino 
772db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
773db2a4165SFrank Mandarino {
774db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
775db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
77687689d56SMark Brown 	struct snd_soc_card *card= socdev->card;
777cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
77887689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
7793c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
7803c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
781db2a4165SFrank Mandarino 	int ret;
782db2a4165SFrank Mandarino 
7836335d055SEric Miao 	if (codec_dai->ops->trigger) {
7846335d055SEric Miao 		ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
785db2a4165SFrank Mandarino 		if (ret < 0)
786db2a4165SFrank Mandarino 			return ret;
787db2a4165SFrank Mandarino 	}
788db2a4165SFrank Mandarino 
789db2a4165SFrank Mandarino 	if (platform->pcm_ops->trigger) {
790db2a4165SFrank Mandarino 		ret = platform->pcm_ops->trigger(substream, cmd);
791db2a4165SFrank Mandarino 		if (ret < 0)
792db2a4165SFrank Mandarino 			return ret;
793db2a4165SFrank Mandarino 	}
794db2a4165SFrank Mandarino 
7956335d055SEric Miao 	if (cpu_dai->ops->trigger) {
7966335d055SEric Miao 		ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
797db2a4165SFrank Mandarino 		if (ret < 0)
798db2a4165SFrank Mandarino 			return ret;
799db2a4165SFrank Mandarino 	}
800db2a4165SFrank Mandarino 	return 0;
801db2a4165SFrank Mandarino }
802db2a4165SFrank Mandarino 
803db2a4165SFrank Mandarino /* ASoC PCM operations */
804db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
805db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
806db2a4165SFrank Mandarino 	.close		= soc_codec_close,
807db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
808db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
809db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
810db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
811db2a4165SFrank Mandarino };
812db2a4165SFrank Mandarino 
813db2a4165SFrank Mandarino #ifdef CONFIG_PM
814db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
815416356fcSMark Brown static int soc_suspend(struct device *dev)
816db2a4165SFrank Mandarino {
817416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
818db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
81987506549SMark Brown 	struct snd_soc_card *card = socdev->card;
82087689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
821db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
8226627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
823db2a4165SFrank Mandarino 	int i;
824db2a4165SFrank Mandarino 
825e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
826e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
827e3509ff0SDaniel Mack 	 */
828e3509ff0SDaniel Mack 	if (!codec)
829e3509ff0SDaniel Mack 		return 0;
830e3509ff0SDaniel Mack 
8316ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
8326ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
8336ed25978SAndy Green 	 */
8346ed25978SAndy Green 	snd_power_lock(codec->card);
8356ed25978SAndy Green 	snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
8366ed25978SAndy Green 	snd_power_unlock(codec->card);
8376ed25978SAndy Green 
8386ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
8396ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
8406ed25978SAndy Green 
841db2a4165SFrank Mandarino 	/* mute any active DAC's */
842dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
843dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
8446335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
8456335d055SEric Miao 			dai->ops->digital_mute(dai, 1);
846db2a4165SFrank Mandarino 	}
847db2a4165SFrank Mandarino 
8484ccab3e7SLiam Girdwood 	/* suspend all pcms */
84987506549SMark Brown 	for (i = 0; i < card->num_links; i++)
85087506549SMark Brown 		snd_pcm_suspend_all(card->dai_link[i].pcm);
8514ccab3e7SLiam Girdwood 
85287506549SMark Brown 	if (card->suspend_pre)
853416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
854db2a4165SFrank Mandarino 
85587506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
85687506549SMark Brown 		struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
8573ba9e10aSMark Brown 		if (cpu_dai->suspend && !cpu_dai->ac97_control)
858dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
859db2a4165SFrank Mandarino 		if (platform->suspend)
860d273ebe7Sjassi brar 			platform->suspend(&card->dai_link[i]);
861db2a4165SFrank Mandarino 	}
862db2a4165SFrank Mandarino 
863db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
8646308419aSMark Brown 	run_delayed_work(&card->delayed_work);
8650be9898aSMark Brown 	codec->suspend_bias_level = codec->bias_level;
866db2a4165SFrank Mandarino 
867db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
868db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
869db2a4165SFrank Mandarino 		if (stream != NULL)
870db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
871db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
872db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
873db2a4165SFrank Mandarino 		if (stream != NULL)
874db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
875db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
876db2a4165SFrank Mandarino 	}
877db2a4165SFrank Mandarino 
878db2a4165SFrank Mandarino 	if (codec_dev->suspend)
879416356fcSMark Brown 		codec_dev->suspend(pdev, PMSG_SUSPEND);
880db2a4165SFrank Mandarino 
88187506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
88287506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
8833ba9e10aSMark Brown 		if (cpu_dai->suspend && cpu_dai->ac97_control)
884dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
885db2a4165SFrank Mandarino 	}
886db2a4165SFrank Mandarino 
88787506549SMark Brown 	if (card->suspend_post)
888416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
889db2a4165SFrank Mandarino 
890db2a4165SFrank Mandarino 	return 0;
891db2a4165SFrank Mandarino }
892db2a4165SFrank Mandarino 
8936ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
8946ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
8956ed25978SAndy Green  */
8966ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
897db2a4165SFrank Mandarino {
8986308419aSMark Brown 	struct snd_soc_card *card = container_of(work,
8996308419aSMark Brown 						 struct snd_soc_card,
9006ed25978SAndy Green 						 deferred_resume_work);
9016308419aSMark Brown 	struct snd_soc_device *socdev = card->socdev;
90287689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
903db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
9046627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
9056ed25978SAndy Green 	struct platform_device *pdev = to_platform_device(socdev->dev);
906db2a4165SFrank Mandarino 	int i;
907db2a4165SFrank Mandarino 
9086ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
9096ed25978SAndy Green 	 * so userspace apps are blocked from touching us
9106ed25978SAndy Green 	 */
9116ed25978SAndy Green 
912fde22f27SMark Brown 	dev_dbg(socdev->dev, "starting resume work\n");
9136ed25978SAndy Green 
91487506549SMark Brown 	if (card->resume_pre)
91587506549SMark Brown 		card->resume_pre(pdev);
916db2a4165SFrank Mandarino 
91787506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
91887506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
9193ba9e10aSMark Brown 		if (cpu_dai->resume && cpu_dai->ac97_control)
920dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
921db2a4165SFrank Mandarino 	}
922db2a4165SFrank Mandarino 
923db2a4165SFrank Mandarino 	if (codec_dev->resume)
924db2a4165SFrank Mandarino 		codec_dev->resume(pdev);
925db2a4165SFrank Mandarino 
926db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
927db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
928db2a4165SFrank Mandarino 		if (stream != NULL)
929db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
930db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
931db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
932db2a4165SFrank Mandarino 		if (stream != NULL)
933db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
934db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
935db2a4165SFrank Mandarino 	}
936db2a4165SFrank Mandarino 
9373ff3f64bSMark Brown 	/* unmute any active DACs */
938dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
939dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
9406335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
9416335d055SEric Miao 			dai->ops->digital_mute(dai, 0);
942db2a4165SFrank Mandarino 	}
943db2a4165SFrank Mandarino 
94487506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
94587506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
9463ba9e10aSMark Brown 		if (cpu_dai->resume && !cpu_dai->ac97_control)
947dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
948db2a4165SFrank Mandarino 		if (platform->resume)
949d273ebe7Sjassi brar 			platform->resume(&card->dai_link[i]);
950db2a4165SFrank Mandarino 	}
951db2a4165SFrank Mandarino 
95287506549SMark Brown 	if (card->resume_post)
95387506549SMark Brown 		card->resume_post(pdev);
954db2a4165SFrank Mandarino 
955fde22f27SMark Brown 	dev_dbg(socdev->dev, "resume work completed\n");
9566ed25978SAndy Green 
9576ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
9586ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
9596ed25978SAndy Green }
9606ed25978SAndy Green 
9616ed25978SAndy Green /* powers up audio subsystem after a suspend */
962416356fcSMark Brown static int soc_resume(struct device *dev)
9636ed25978SAndy Green {
964416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
9656ed25978SAndy Green 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
9666308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
96764ab9baaSMark Brown 	struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
9686ed25978SAndy Green 
96964ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
97064ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
97164ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
97264ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
97364ab9baaSMark Brown 	 */
97464ab9baaSMark Brown 	if (cpu_dai->ac97_control) {
97564ab9baaSMark Brown 		dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
97664ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
97764ab9baaSMark Brown 	} else {
97864ab9baaSMark Brown 		dev_dbg(socdev->dev, "Scheduling resume work\n");
9796308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
980fde22f27SMark Brown 			dev_err(socdev->dev, "resume work item may be lost\n");
98164ab9baaSMark Brown 	}
9826ed25978SAndy Green 
983db2a4165SFrank Mandarino 	return 0;
984db2a4165SFrank Mandarino }
985db2a4165SFrank Mandarino #else
986db2a4165SFrank Mandarino #define soc_suspend	NULL
987db2a4165SFrank Mandarino #define soc_resume	NULL
988db2a4165SFrank Mandarino #endif
989db2a4165SFrank Mandarino 
99002a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
99102a06d30SBarry Song };
99202a06d30SBarry Song 
993435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card)
994db2a4165SFrank Mandarino {
995435c5e25SMark Brown 	struct platform_device *pdev = container_of(card->dev,
996435c5e25SMark Brown 						    struct platform_device,
997435c5e25SMark Brown 						    dev);
998435c5e25SMark Brown 	struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
999fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1000435c5e25SMark Brown 	struct snd_soc_platform *platform;
1001435c5e25SMark Brown 	struct snd_soc_dai *dai;
10026b05eda6SMark Brown 	int i, found, ret, ac97;
1003db2a4165SFrank Mandarino 
1004435c5e25SMark Brown 	if (card->instantiated)
1005435c5e25SMark Brown 		return;
10066308419aSMark Brown 
1007435c5e25SMark Brown 	found = 0;
1008435c5e25SMark Brown 	list_for_each_entry(platform, &platform_list, list)
1009435c5e25SMark Brown 		if (card->platform == platform) {
1010435c5e25SMark Brown 			found = 1;
1011435c5e25SMark Brown 			break;
1012435c5e25SMark Brown 		}
1013435c5e25SMark Brown 	if (!found) {
1014435c5e25SMark Brown 		dev_dbg(card->dev, "Platform %s not registered\n",
1015435c5e25SMark Brown 			card->platform->name);
1016435c5e25SMark Brown 		return;
1017c5af3a2eSMark Brown 	}
1018c5af3a2eSMark Brown 
10196b05eda6SMark Brown 	ac97 = 0;
1020435c5e25SMark Brown 	for (i = 0; i < card->num_links; i++) {
1021435c5e25SMark Brown 		found = 0;
1022435c5e25SMark Brown 		list_for_each_entry(dai, &dai_list, list)
1023435c5e25SMark Brown 			if (card->dai_link[i].cpu_dai == dai) {
1024435c5e25SMark Brown 				found = 1;
1025435c5e25SMark Brown 				break;
1026435c5e25SMark Brown 			}
1027435c5e25SMark Brown 		if (!found) {
1028435c5e25SMark Brown 			dev_dbg(card->dev, "DAI %s not registered\n",
1029435c5e25SMark Brown 				card->dai_link[i].cpu_dai->name);
1030435c5e25SMark Brown 			return;
1031435c5e25SMark Brown 		}
10326b05eda6SMark Brown 
10336b05eda6SMark Brown 		if (card->dai_link[i].cpu_dai->ac97_control)
10346b05eda6SMark Brown 			ac97 = 1;
10356b05eda6SMark Brown 	}
10366b05eda6SMark Brown 
103702a06d30SBarry Song 	for (i = 0; i < card->num_links; i++) {
103802a06d30SBarry Song 		if (!card->dai_link[i].codec_dai->ops)
103902a06d30SBarry Song 			card->dai_link[i].codec_dai->ops = &null_dai_ops;
104002a06d30SBarry Song 	}
104102a06d30SBarry Song 
10426b05eda6SMark Brown 	/* If we have AC97 in the system then don't wait for the
10436b05eda6SMark Brown 	 * codec.  This will need revisiting if we have to handle
10446b05eda6SMark Brown 	 * systems with mixed AC97 and non-AC97 parts.  Only check for
10456b05eda6SMark Brown 	 * DAIs currently; we can't do this per link since some AC97
10466b05eda6SMark Brown 	 * codecs have non-AC97 DAIs.
10476b05eda6SMark Brown 	 */
10486b05eda6SMark Brown 	if (!ac97)
10496b05eda6SMark Brown 		for (i = 0; i < card->num_links; i++) {
10506b05eda6SMark Brown 			found = 0;
10516b05eda6SMark Brown 			list_for_each_entry(dai, &dai_list, list)
10526b05eda6SMark Brown 				if (card->dai_link[i].codec_dai == dai) {
10536b05eda6SMark Brown 					found = 1;
10546b05eda6SMark Brown 					break;
10556b05eda6SMark Brown 				}
10566b05eda6SMark Brown 			if (!found) {
10576b05eda6SMark Brown 				dev_dbg(card->dev, "DAI %s not registered\n",
10586b05eda6SMark Brown 					card->dai_link[i].codec_dai->name);
10596b05eda6SMark Brown 				return;
10606b05eda6SMark Brown 			}
1061435c5e25SMark Brown 		}
1062435c5e25SMark Brown 
1063435c5e25SMark Brown 	/* Note that we do not current check for codec components */
1064435c5e25SMark Brown 
1065435c5e25SMark Brown 	dev_dbg(card->dev, "All components present, instantiating\n");
1066435c5e25SMark Brown 
1067435c5e25SMark Brown 	/* Found everything, bring it up */
106896dd3622SMark Brown 	card->pmdown_time = pmdown_time;
106996dd3622SMark Brown 
107087506549SMark Brown 	if (card->probe) {
107187506549SMark Brown 		ret = card->probe(pdev);
1072db2a4165SFrank Mandarino 		if (ret < 0)
1073435c5e25SMark Brown 			return;
1074db2a4165SFrank Mandarino 	}
1075db2a4165SFrank Mandarino 
107687506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
107787506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1078db2a4165SFrank Mandarino 		if (cpu_dai->probe) {
1079bdb92876SMark Brown 			ret = cpu_dai->probe(pdev, cpu_dai);
1080db2a4165SFrank Mandarino 			if (ret < 0)
1081db2a4165SFrank Mandarino 				goto cpu_dai_err;
1082db2a4165SFrank Mandarino 		}
1083db2a4165SFrank Mandarino 	}
1084db2a4165SFrank Mandarino 
1085db2a4165SFrank Mandarino 	if (codec_dev->probe) {
1086db2a4165SFrank Mandarino 		ret = codec_dev->probe(pdev);
1087db2a4165SFrank Mandarino 		if (ret < 0)
1088db2a4165SFrank Mandarino 			goto cpu_dai_err;
1089db2a4165SFrank Mandarino 	}
1090fe3e78e0SMark Brown 	codec = card->codec;
1091db2a4165SFrank Mandarino 
1092db2a4165SFrank Mandarino 	if (platform->probe) {
1093db2a4165SFrank Mandarino 		ret = platform->probe(pdev);
1094db2a4165SFrank Mandarino 		if (ret < 0)
1095db2a4165SFrank Mandarino 			goto platform_err;
1096db2a4165SFrank Mandarino 	}
1097db2a4165SFrank Mandarino 
1098db2a4165SFrank Mandarino 	/* DAPM stream work */
10996308419aSMark Brown 	INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
11001301a964SRandy Dunlap #ifdef CONFIG_PM
11016ed25978SAndy Green 	/* deferred resume work */
11026308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
11031301a964SRandy Dunlap #endif
11046ed25978SAndy Green 
1105fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1106fe3e78e0SMark Brown 		if (card->dai_link[i].init) {
1107fe3e78e0SMark Brown 			ret = card->dai_link[i].init(codec);
1108fe3e78e0SMark Brown 			if (ret < 0) {
1109fe3e78e0SMark Brown 				printk(KERN_ERR "asoc: failed to init %s\n",
1110fe3e78e0SMark Brown 					card->dai_link[i].stream_name);
1111fe3e78e0SMark Brown 				continue;
1112fe3e78e0SMark Brown 			}
1113fe3e78e0SMark Brown 		}
1114f7732053SBarry Song 		if (card->dai_link[i].codec_dai->ac97_control)
1115fe3e78e0SMark Brown 			ac97 = 1;
1116fe3e78e0SMark Brown 	}
1117fe3e78e0SMark Brown 
1118fe3e78e0SMark Brown 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1119fe3e78e0SMark Brown 		 "%s",  card->name);
1120fe3e78e0SMark Brown 	snprintf(codec->card->longname, sizeof(codec->card->longname),
1121fe3e78e0SMark Brown 		 "%s (%s)", card->name, codec->name);
1122fe3e78e0SMark Brown 
1123fe3e78e0SMark Brown 	/* Make sure all DAPM widgets are instantiated */
1124fe3e78e0SMark Brown 	snd_soc_dapm_new_widgets(codec);
1125fe3e78e0SMark Brown 
1126fe3e78e0SMark Brown 	ret = snd_card_register(codec->card);
1127fe3e78e0SMark Brown 	if (ret < 0) {
1128fe3e78e0SMark Brown 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1129fe3e78e0SMark Brown 				codec->name);
1130fe3e78e0SMark Brown 		goto card_err;
1131fe3e78e0SMark Brown 	}
1132fe3e78e0SMark Brown 
1133fe3e78e0SMark Brown 	mutex_lock(&codec->mutex);
1134fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1135fe3e78e0SMark Brown 	/* Only instantiate AC97 if not already done by the adaptor
1136fe3e78e0SMark Brown 	 * for the generic AC97 subsystem.
1137fe3e78e0SMark Brown 	 */
1138fe3e78e0SMark Brown 	if (ac97 && strcmp(codec->name, "AC97") != 0) {
1139fe3e78e0SMark Brown 		ret = soc_ac97_dev_register(codec);
1140fe3e78e0SMark Brown 		if (ret < 0) {
1141fe3e78e0SMark Brown 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1142fe3e78e0SMark Brown 			snd_card_free(codec->card);
1143fe3e78e0SMark Brown 			mutex_unlock(&codec->mutex);
1144fe3e78e0SMark Brown 			goto card_err;
1145fe3e78e0SMark Brown 		}
1146fe3e78e0SMark Brown 	}
1147fe3e78e0SMark Brown #endif
1148fe3e78e0SMark Brown 
1149fe3e78e0SMark Brown 	ret = snd_soc_dapm_sys_add(card->socdev->dev);
1150fe3e78e0SMark Brown 	if (ret < 0)
1151fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1152fe3e78e0SMark Brown 
1153dbe21408SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1154dbe21408SMark Brown 	if (ret < 0)
1155dbe21408SMark Brown 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1156dbe21408SMark Brown 
1157fe3e78e0SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1158fe3e78e0SMark Brown 	if (ret < 0)
1159fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1160fe3e78e0SMark Brown 
1161fe3e78e0SMark Brown 	soc_init_codec_debugfs(codec);
1162fe3e78e0SMark Brown 	mutex_unlock(&codec->mutex);
1163fe3e78e0SMark Brown 
1164435c5e25SMark Brown 	card->instantiated = 1;
1165435c5e25SMark Brown 
1166435c5e25SMark Brown 	return;
1167db2a4165SFrank Mandarino 
1168fe3e78e0SMark Brown card_err:
1169fe3e78e0SMark Brown 	if (platform->remove)
1170fe3e78e0SMark Brown 		platform->remove(pdev);
1171fe3e78e0SMark Brown 
1172db2a4165SFrank Mandarino platform_err:
1173db2a4165SFrank Mandarino 	if (codec_dev->remove)
1174db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1175db2a4165SFrank Mandarino 
1176db2a4165SFrank Mandarino cpu_dai_err:
117718b9b3d9SLiam Girdwood 	for (i--; i >= 0; i--) {
117887506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1179db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1180bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1181db2a4165SFrank Mandarino 	}
1182db2a4165SFrank Mandarino 
118387506549SMark Brown 	if (card->remove)
118487506549SMark Brown 		card->remove(pdev);
1185435c5e25SMark Brown }
1186db2a4165SFrank Mandarino 
1187435c5e25SMark Brown /*
1188435c5e25SMark Brown  * Attempt to initialise any uninitalised cards.  Must be called with
1189435c5e25SMark Brown  * client_mutex.
1190435c5e25SMark Brown  */
1191435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1192435c5e25SMark Brown {
1193435c5e25SMark Brown 	struct snd_soc_card *card;
1194435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1195435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1196435c5e25SMark Brown }
1197435c5e25SMark Brown 
1198435c5e25SMark Brown /* probes a new socdev */
1199435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1200435c5e25SMark Brown {
1201435c5e25SMark Brown 	int ret = 0;
1202435c5e25SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1203435c5e25SMark Brown 	struct snd_soc_card *card = socdev->card;
1204435c5e25SMark Brown 
1205435c5e25SMark Brown 	/* Bodge while we push things out of socdev */
1206435c5e25SMark Brown 	card->socdev = socdev;
1207435c5e25SMark Brown 
1208435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1209435c5e25SMark Brown 	card->dev = &pdev->dev;
1210435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1211435c5e25SMark Brown 	if (ret != 0) {
1212435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1213db2a4165SFrank Mandarino 		return ret;
1214db2a4165SFrank Mandarino 	}
1215db2a4165SFrank Mandarino 
1216435c5e25SMark Brown 	return 0;
1217435c5e25SMark Brown }
1218435c5e25SMark Brown 
1219db2a4165SFrank Mandarino /* removes a socdev */
1220db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1221db2a4165SFrank Mandarino {
1222db2a4165SFrank Mandarino 	int i;
1223db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
122487506549SMark Brown 	struct snd_soc_card *card = socdev->card;
122587689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
1226db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1227db2a4165SFrank Mandarino 
1228914dc182SMike Rapoport 	if (!card->instantiated)
1229914dc182SMike Rapoport 		return 0;
1230914dc182SMike Rapoport 
12316308419aSMark Brown 	run_delayed_work(&card->delayed_work);
1232965ac42cSLiam Girdwood 
1233db2a4165SFrank Mandarino 	if (platform->remove)
1234db2a4165SFrank Mandarino 		platform->remove(pdev);
1235db2a4165SFrank Mandarino 
1236db2a4165SFrank Mandarino 	if (codec_dev->remove)
1237db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1238db2a4165SFrank Mandarino 
123987506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
124087506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1241db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1242bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1243db2a4165SFrank Mandarino 	}
1244db2a4165SFrank Mandarino 
124587506549SMark Brown 	if (card->remove)
124687506549SMark Brown 		card->remove(pdev);
1247db2a4165SFrank Mandarino 
1248c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1249c5af3a2eSMark Brown 
1250db2a4165SFrank Mandarino 	return 0;
1251db2a4165SFrank Mandarino }
1252db2a4165SFrank Mandarino 
1253416356fcSMark Brown static int soc_poweroff(struct device *dev)
125451737470SMark Brown {
1255416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
125651737470SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
125751737470SMark Brown 	struct snd_soc_card *card = socdev->card;
125851737470SMark Brown 
125951737470SMark Brown 	if (!card->instantiated)
1260416356fcSMark Brown 		return 0;
126151737470SMark Brown 
126251737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
126351737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
126451737470SMark Brown 	run_delayed_work(&card->delayed_work);
126551737470SMark Brown 
126651737470SMark Brown 	snd_soc_dapm_shutdown(socdev);
1267416356fcSMark Brown 
1268416356fcSMark Brown 	return 0;
126951737470SMark Brown }
127051737470SMark Brown 
127147145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1272416356fcSMark Brown 	.suspend = soc_suspend,
1273416356fcSMark Brown 	.resume = soc_resume,
1274416356fcSMark Brown 	.poweroff = soc_poweroff,
1275416356fcSMark Brown };
1276416356fcSMark Brown 
1277db2a4165SFrank Mandarino /* ASoC platform driver */
1278db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1279db2a4165SFrank Mandarino 	.driver		= {
1280db2a4165SFrank Mandarino 		.name		= "soc-audio",
12818b45a209SKay Sievers 		.owner		= THIS_MODULE,
1282416356fcSMark Brown 		.pm		= &soc_pm_ops,
1283db2a4165SFrank Mandarino 	},
1284db2a4165SFrank Mandarino 	.probe		= soc_probe,
1285db2a4165SFrank Mandarino 	.remove		= soc_remove,
1286db2a4165SFrank Mandarino };
1287db2a4165SFrank Mandarino 
1288db2a4165SFrank Mandarino /* create a new pcm */
1289db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev,
1290db2a4165SFrank Mandarino 	struct snd_soc_dai_link *dai_link, int num)
1291db2a4165SFrank Mandarino {
129287689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
12936627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
129487689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
12953c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = dai_link->codec_dai;
12963c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1297db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd;
1298db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1299db2a4165SFrank Mandarino 	char new_name[64];
1300db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1301db2a4165SFrank Mandarino 
1302db2a4165SFrank Mandarino 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1303db2a4165SFrank Mandarino 	if (rtd == NULL)
1304db2a4165SFrank Mandarino 		return -ENOMEM;
1305cb666e5bSLiam Girdwood 
1306cb666e5bSLiam Girdwood 	rtd->dai = dai_link;
1307db2a4165SFrank Mandarino 	rtd->socdev = socdev;
13086627a653SMark Brown 	codec_dai->codec = card->codec;
1309db2a4165SFrank Mandarino 
1310db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
131140ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
131240ca1142SMark Brown 		 dai_link->stream_name, codec_dai->name, num);
1313db2a4165SFrank Mandarino 
1314db2a4165SFrank Mandarino 	if (codec_dai->playback.channels_min)
1315db2a4165SFrank Mandarino 		playback = 1;
1316db2a4165SFrank Mandarino 	if (codec_dai->capture.channels_min)
1317db2a4165SFrank Mandarino 		capture = 1;
1318db2a4165SFrank Mandarino 
1319db2a4165SFrank Mandarino 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1320db2a4165SFrank Mandarino 		capture, &pcm);
1321db2a4165SFrank Mandarino 	if (ret < 0) {
13223ff3f64bSMark Brown 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
13233ff3f64bSMark Brown 			codec->name);
1324db2a4165SFrank Mandarino 		kfree(rtd);
1325db2a4165SFrank Mandarino 		return ret;
1326db2a4165SFrank Mandarino 	}
1327db2a4165SFrank Mandarino 
13284ccab3e7SLiam Girdwood 	dai_link->pcm = pcm;
1329db2a4165SFrank Mandarino 	pcm->private_data = rtd;
133087689d56SMark Brown 	soc_pcm_ops.mmap = platform->pcm_ops->mmap;
133187689d56SMark Brown 	soc_pcm_ops.pointer = platform->pcm_ops->pointer;
133287689d56SMark Brown 	soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
133387689d56SMark Brown 	soc_pcm_ops.copy = platform->pcm_ops->copy;
133487689d56SMark Brown 	soc_pcm_ops.silence = platform->pcm_ops->silence;
133587689d56SMark Brown 	soc_pcm_ops.ack = platform->pcm_ops->ack;
133687689d56SMark Brown 	soc_pcm_ops.page = platform->pcm_ops->page;
1337db2a4165SFrank Mandarino 
1338db2a4165SFrank Mandarino 	if (playback)
1339db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1340db2a4165SFrank Mandarino 
1341db2a4165SFrank Mandarino 	if (capture)
1342db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1343db2a4165SFrank Mandarino 
134487689d56SMark Brown 	ret = platform->pcm_new(codec->card, codec_dai, pcm);
1345db2a4165SFrank Mandarino 	if (ret < 0) {
1346db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1347db2a4165SFrank Mandarino 		kfree(rtd);
1348db2a4165SFrank Mandarino 		return ret;
1349db2a4165SFrank Mandarino 	}
1350db2a4165SFrank Mandarino 
135187689d56SMark Brown 	pcm->private_free = platform->pcm_free;
1352db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1353db2a4165SFrank Mandarino 		cpu_dai->name);
1354db2a4165SFrank Mandarino 	return ret;
1355db2a4165SFrank Mandarino }
1356db2a4165SFrank Mandarino 
1357096e49d5SMark Brown /**
1358096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1359096e49d5SMark Brown  *
1360096e49d5SMark Brown  * @codec: CODEC to query.
1361096e49d5SMark Brown  * @reg: Register to query.
1362096e49d5SMark Brown  *
1363096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1364096e49d5SMark Brown  */
1365096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1366096e49d5SMark Brown {
1367096e49d5SMark Brown 	if (codec->volatile_register)
1368096e49d5SMark Brown 		return codec->volatile_register(reg);
1369096e49d5SMark Brown 	else
1370096e49d5SMark Brown 		return 0;
1371096e49d5SMark Brown }
1372096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1373096e49d5SMark Brown 
1374db2a4165SFrank Mandarino /**
1375db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1376db2a4165SFrank Mandarino  * @codec: audio codec
1377db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1378db2a4165SFrank Mandarino  * @num: AC97 codec number
1379db2a4165SFrank Mandarino  *
1380db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1381db2a4165SFrank Mandarino  */
1382db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1383db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1384db2a4165SFrank Mandarino {
1385db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1386db2a4165SFrank Mandarino 
1387db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1388db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1389db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1390db2a4165SFrank Mandarino 		return -ENOMEM;
1391db2a4165SFrank Mandarino 	}
1392db2a4165SFrank Mandarino 
1393db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1394db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1395db2a4165SFrank Mandarino 		kfree(codec->ac97);
1396db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1397db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1398db2a4165SFrank Mandarino 		return -ENOMEM;
1399db2a4165SFrank Mandarino 	}
1400db2a4165SFrank Mandarino 
1401db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1402db2a4165SFrank Mandarino 	codec->ac97->num = num;
14032718625fSMark Brown 	codec->dev = &codec->ac97->dev;
1404db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1405db2a4165SFrank Mandarino 	return 0;
1406db2a4165SFrank Mandarino }
1407db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1408db2a4165SFrank Mandarino 
1409db2a4165SFrank Mandarino /**
1410db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1411db2a4165SFrank Mandarino  * @codec: audio codec
1412db2a4165SFrank Mandarino  *
1413db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1414db2a4165SFrank Mandarino  */
1415db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1416db2a4165SFrank Mandarino {
1417db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1418db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1419db2a4165SFrank Mandarino 	kfree(codec->ac97);
1420db2a4165SFrank Mandarino 	codec->ac97 = NULL;
1421db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1422db2a4165SFrank Mandarino }
1423db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1424db2a4165SFrank Mandarino 
1425db2a4165SFrank Mandarino /**
1426db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1427db2a4165SFrank Mandarino  * @codec: audio codec
1428db2a4165SFrank Mandarino  * @reg: codec register
1429db2a4165SFrank Mandarino  * @mask: register mask
1430db2a4165SFrank Mandarino  * @value: new value
1431db2a4165SFrank Mandarino  *
1432db2a4165SFrank Mandarino  * Writes new register value.
1433db2a4165SFrank Mandarino  *
1434db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1435db2a4165SFrank Mandarino  */
1436db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
143746f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1438db2a4165SFrank Mandarino {
1439db2a4165SFrank Mandarino 	int change;
144046f5822fSDaniel Ribeiro 	unsigned int old, new;
1441db2a4165SFrank Mandarino 
1442db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1443db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1444db2a4165SFrank Mandarino 	change = old != new;
1445db2a4165SFrank Mandarino 	if (change)
1446db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1447db2a4165SFrank Mandarino 
1448db2a4165SFrank Mandarino 	return change;
1449db2a4165SFrank Mandarino }
1450db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1451db2a4165SFrank Mandarino 
1452db2a4165SFrank Mandarino /**
14536c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
14546c508c62SEero Nurkkala  * @codec: audio codec
14556c508c62SEero Nurkkala  * @reg: codec register
14566c508c62SEero Nurkkala  * @mask: register mask
14576c508c62SEero Nurkkala  * @value: new value
14586c508c62SEero Nurkkala  *
14596c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
14606c508c62SEero Nurkkala  *
14616c508c62SEero Nurkkala  * Returns 1 for change else 0.
14626c508c62SEero Nurkkala  */
1463dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
14646c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
14656c508c62SEero Nurkkala 			       unsigned int value)
14666c508c62SEero Nurkkala {
14676c508c62SEero Nurkkala 	int change;
14686c508c62SEero Nurkkala 
14696c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
14706c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
14716c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
14726c508c62SEero Nurkkala 
14736c508c62SEero Nurkkala 	return change;
14746c508c62SEero Nurkkala }
1475dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
14766c508c62SEero Nurkkala 
14776c508c62SEero Nurkkala /**
1478db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1479db2a4165SFrank Mandarino  * @codec: audio codec
1480db2a4165SFrank Mandarino  * @reg: codec register
1481db2a4165SFrank Mandarino  * @mask: register mask
1482db2a4165SFrank Mandarino  * @value: new value
1483db2a4165SFrank Mandarino  *
1484db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1485db2a4165SFrank Mandarino  * different from the old value.
1486db2a4165SFrank Mandarino  *
1487db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1488db2a4165SFrank Mandarino  */
1489db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
149046f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1491db2a4165SFrank Mandarino {
1492db2a4165SFrank Mandarino 	int change;
149346f5822fSDaniel Ribeiro 	unsigned int old, new;
1494db2a4165SFrank Mandarino 
1495db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1496db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1497db2a4165SFrank Mandarino 	change = old != new;
1498db2a4165SFrank Mandarino 
1499db2a4165SFrank Mandarino 	return change;
1500db2a4165SFrank Mandarino }
1501db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1502db2a4165SFrank Mandarino 
1503db2a4165SFrank Mandarino /**
1504db2a4165SFrank Mandarino  * snd_soc_new_pcms - create new sound card and pcms
1505db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1506ac11a2b3SMark Brown  * @idx: ALSA card index
1507ac11a2b3SMark Brown  * @xid: card identification
1508db2a4165SFrank Mandarino  *
1509db2a4165SFrank Mandarino  * Create a new sound card based upon the codec and interface pcms.
1510db2a4165SFrank Mandarino  *
1511db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1512db2a4165SFrank Mandarino  */
1513db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1514db2a4165SFrank Mandarino {
151587506549SMark Brown 	struct snd_soc_card *card = socdev->card;
15166627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
1517bd7dd77cSTakashi Iwai 	int ret, i;
1518db2a4165SFrank Mandarino 
1519db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1520db2a4165SFrank Mandarino 
1521db2a4165SFrank Mandarino 	/* register a sound card */
1522bd7dd77cSTakashi Iwai 	ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1523bd7dd77cSTakashi Iwai 	if (ret < 0) {
1524db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1525db2a4165SFrank Mandarino 			codec->name);
1526db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1527bd7dd77cSTakashi Iwai 		return ret;
1528db2a4165SFrank Mandarino 	}
1529db2a4165SFrank Mandarino 
1530452c5eaaSMark Brown 	codec->socdev = socdev;
1531db2a4165SFrank Mandarino 	codec->card->dev = socdev->dev;
1532db2a4165SFrank Mandarino 	codec->card->private_data = codec;
1533db2a4165SFrank Mandarino 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1534db2a4165SFrank Mandarino 
1535db2a4165SFrank Mandarino 	/* create the pcms */
153687506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
153787506549SMark Brown 		ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1538db2a4165SFrank Mandarino 		if (ret < 0) {
1539db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't create pcm %s\n",
154087506549SMark Brown 				card->dai_link[i].stream_name);
1541db2a4165SFrank Mandarino 			mutex_unlock(&codec->mutex);
1542db2a4165SFrank Mandarino 			return ret;
1543db2a4165SFrank Mandarino 		}
1544f7732053SBarry Song 		if (card->dai_link[i].codec_dai->ac97_control) {
1545f7732053SBarry Song 			snd_ac97_dev_add_pdata(codec->ac97,
1546f7732053SBarry Song 				card->dai_link[i].cpu_dai->ac97_pdata);
1547f7732053SBarry Song 		}
1548db2a4165SFrank Mandarino 	}
1549db2a4165SFrank Mandarino 
1550db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1551db2a4165SFrank Mandarino 	return ret;
1552db2a4165SFrank Mandarino }
1553db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1554db2a4165SFrank Mandarino 
1555db2a4165SFrank Mandarino /**
1556db2a4165SFrank Mandarino  * snd_soc_free_pcms - free sound card and pcms
1557db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1558db2a4165SFrank Mandarino  *
1559db2a4165SFrank Mandarino  * Frees sound card and pcms associated with the socdev.
1560db2a4165SFrank Mandarino  * Also unregister the codec if it is an AC97 device.
1561db2a4165SFrank Mandarino  */
1562db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev)
1563db2a4165SFrank Mandarino {
15646627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
1565a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
15663c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
1567a68660e0SLiam Girdwood 	int i;
1568a68660e0SLiam Girdwood #endif
1569db2a4165SFrank Mandarino 
1570db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
15716627a653SMark Brown 	soc_cleanup_codec_debugfs(codec);
1572db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1573a68660e0SLiam Girdwood 	for (i = 0; i < codec->num_dai; i++) {
1574a68660e0SLiam Girdwood 		codec_dai = &codec->dai[i];
1575d2314e0eSAtsushi Nemoto 		if (codec_dai->ac97_control && codec->ac97 &&
1576d2314e0eSAtsushi Nemoto 		    strcmp(codec->name, "AC97") != 0) {
1577db2a4165SFrank Mandarino 			soc_ac97_dev_unregister(codec);
1578a68660e0SLiam Girdwood 			goto free_card;
1579a68660e0SLiam Girdwood 		}
1580a68660e0SLiam Girdwood 	}
1581a68660e0SLiam Girdwood free_card:
1582db2a4165SFrank Mandarino #endif
1583db2a4165SFrank Mandarino 
1584db2a4165SFrank Mandarino 	if (codec->card)
1585db2a4165SFrank Mandarino 		snd_card_free(codec->card);
1586db2a4165SFrank Mandarino 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1587db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1588db2a4165SFrank Mandarino }
1589db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1590db2a4165SFrank Mandarino 
1591db2a4165SFrank Mandarino /**
1592db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1593db2a4165SFrank Mandarino  * @substream: the pcm substream
1594db2a4165SFrank Mandarino  * @hw: the hardware parameters
1595db2a4165SFrank Mandarino  *
1596db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1597db2a4165SFrank Mandarino  */
1598db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1599db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1600db2a4165SFrank Mandarino {
1601db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1602db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1603db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1604db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1605db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1606db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1607db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1608db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1609db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1610db2a4165SFrank Mandarino 	return 0;
1611db2a4165SFrank Mandarino }
1612db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1613db2a4165SFrank Mandarino 
1614db2a4165SFrank Mandarino /**
1615db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1616db2a4165SFrank Mandarino  * @_template: control template
1617db2a4165SFrank Mandarino  * @data: control private data
1618ac11a2b3SMark Brown  * @long_name: control long name
1619db2a4165SFrank Mandarino  *
1620db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1621db2a4165SFrank Mandarino  *
1622db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1623db2a4165SFrank Mandarino  */
1624db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1625db2a4165SFrank Mandarino 	void *data, char *long_name)
1626db2a4165SFrank Mandarino {
1627db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1628db2a4165SFrank Mandarino 
1629db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1630db2a4165SFrank Mandarino 	if (long_name)
1631db2a4165SFrank Mandarino 		template.name = long_name;
1632db2a4165SFrank Mandarino 	template.index = 0;
1633db2a4165SFrank Mandarino 
1634db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1635db2a4165SFrank Mandarino }
1636db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1637db2a4165SFrank Mandarino 
1638db2a4165SFrank Mandarino /**
16393e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
16403e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
16413e8e1952SIan Molton  * duplicating this code.
16423e8e1952SIan Molton  *
16433e8e1952SIan Molton  * @codec: codec to add controls to
16443e8e1952SIan Molton  * @controls: array of controls to add
16453e8e1952SIan Molton  * @num_controls: number of elements in the array
16463e8e1952SIan Molton  *
16473e8e1952SIan Molton  * Return 0 for success, else error.
16483e8e1952SIan Molton  */
16493e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
16503e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
16513e8e1952SIan Molton {
16523e8e1952SIan Molton 	struct snd_card *card = codec->card;
16533e8e1952SIan Molton 	int err, i;
16543e8e1952SIan Molton 
16553e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
16563e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
16573e8e1952SIan Molton 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
16583e8e1952SIan Molton 		if (err < 0) {
16593e8e1952SIan Molton 			dev_err(codec->dev, "%s: Failed to add %s\n",
16603e8e1952SIan Molton 				codec->name, control->name);
16613e8e1952SIan Molton 			return err;
16623e8e1952SIan Molton 		}
16633e8e1952SIan Molton 	}
16643e8e1952SIan Molton 
16653e8e1952SIan Molton 	return 0;
16663e8e1952SIan Molton }
16673e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
16683e8e1952SIan Molton 
16693e8e1952SIan Molton /**
1670db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1671db2a4165SFrank Mandarino  * @kcontrol: mixer control
1672db2a4165SFrank Mandarino  * @uinfo: control element information
1673db2a4165SFrank Mandarino  *
1674db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
1675db2a4165SFrank Mandarino  * mixer control.
1676db2a4165SFrank Mandarino  *
1677db2a4165SFrank Mandarino  * Returns 0 for success.
1678db2a4165SFrank Mandarino  */
1679db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1680db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1681db2a4165SFrank Mandarino {
1682db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1683db2a4165SFrank Mandarino 
1684db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1685db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1686f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1687db2a4165SFrank Mandarino 
1688f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1689f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1690db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1691db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1692db2a4165SFrank Mandarino 	return 0;
1693db2a4165SFrank Mandarino }
1694db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1695db2a4165SFrank Mandarino 
1696db2a4165SFrank Mandarino /**
1697db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
1698db2a4165SFrank Mandarino  * @kcontrol: mixer control
1699ac11a2b3SMark Brown  * @ucontrol: control element information
1700db2a4165SFrank Mandarino  *
1701db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
1702db2a4165SFrank Mandarino  *
1703db2a4165SFrank Mandarino  * Returns 0 for success.
1704db2a4165SFrank Mandarino  */
1705db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1706db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1707db2a4165SFrank Mandarino {
1708db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1709db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
171046f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
1711db2a4165SFrank Mandarino 
1712f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1713db2a4165SFrank Mandarino 		;
1714db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
17153ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
17163ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
1717db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
1718db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
1719db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
1720db2a4165SFrank Mandarino 
1721db2a4165SFrank Mandarino 	return 0;
1722db2a4165SFrank Mandarino }
1723db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1724db2a4165SFrank Mandarino 
1725db2a4165SFrank Mandarino /**
1726db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
1727db2a4165SFrank Mandarino  * @kcontrol: mixer control
1728ac11a2b3SMark Brown  * @ucontrol: control element information
1729db2a4165SFrank Mandarino  *
1730db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
1731db2a4165SFrank Mandarino  *
1732db2a4165SFrank Mandarino  * Returns 0 for success.
1733db2a4165SFrank Mandarino  */
1734db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1735db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1736db2a4165SFrank Mandarino {
1737db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1738db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
173946f5822fSDaniel Ribeiro 	unsigned int val;
174046f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
1741db2a4165SFrank Mandarino 
1742f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1743db2a4165SFrank Mandarino 		;
1744f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1745db2a4165SFrank Mandarino 		return -EINVAL;
1746db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1747db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
1748db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
1749f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1750db2a4165SFrank Mandarino 			return -EINVAL;
1751db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1752db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
1753db2a4165SFrank Mandarino 	}
1754db2a4165SFrank Mandarino 
17556c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1756db2a4165SFrank Mandarino }
1757db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1758db2a4165SFrank Mandarino 
1759db2a4165SFrank Mandarino /**
17602e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
17612e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
17622e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
17632e72f8e3SPeter Ujfalusi  *
17642e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
17652e72f8e3SPeter Ujfalusi  *
17662e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
17672e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
17682e72f8e3SPeter Ujfalusi  *
17692e72f8e3SPeter Ujfalusi  * Returns 0 for success.
17702e72f8e3SPeter Ujfalusi  */
17712e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
17722e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
17732e72f8e3SPeter Ujfalusi {
17742e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
177574155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
177646f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
17772e72f8e3SPeter Ujfalusi 
17782e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
17792e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
17802e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
17812e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
17822e72f8e3SPeter Ujfalusi 			break;
17832e72f8e3SPeter Ujfalusi 	}
17842e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
17852e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
17862e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
17872e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
17882e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
17892e72f8e3SPeter Ujfalusi 				break;
17902e72f8e3SPeter Ujfalusi 		}
17912e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
17922e72f8e3SPeter Ujfalusi 	}
17932e72f8e3SPeter Ujfalusi 
17942e72f8e3SPeter Ujfalusi 	return 0;
17952e72f8e3SPeter Ujfalusi }
17962e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
17972e72f8e3SPeter Ujfalusi 
17982e72f8e3SPeter Ujfalusi /**
17992e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
18002e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
18012e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
18022e72f8e3SPeter Ujfalusi  *
18032e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
18042e72f8e3SPeter Ujfalusi  *
18052e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
18062e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
18072e72f8e3SPeter Ujfalusi  *
18082e72f8e3SPeter Ujfalusi  * Returns 0 for success.
18092e72f8e3SPeter Ujfalusi  */
18102e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
18112e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
18122e72f8e3SPeter Ujfalusi {
18132e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
181474155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
181546f5822fSDaniel Ribeiro 	unsigned int val;
181646f5822fSDaniel Ribeiro 	unsigned int mask;
18172e72f8e3SPeter Ujfalusi 
18182e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
18192e72f8e3SPeter Ujfalusi 		return -EINVAL;
18202e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
18212e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
18222e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
18232e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
18242e72f8e3SPeter Ujfalusi 			return -EINVAL;
18252e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
18262e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
18272e72f8e3SPeter Ujfalusi 	}
18282e72f8e3SPeter Ujfalusi 
18296c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
18302e72f8e3SPeter Ujfalusi }
18312e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
18322e72f8e3SPeter Ujfalusi 
18332e72f8e3SPeter Ujfalusi /**
1834db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1835db2a4165SFrank Mandarino  * @kcontrol: mixer control
1836db2a4165SFrank Mandarino  * @uinfo: control element information
1837db2a4165SFrank Mandarino  *
1838db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
1839db2a4165SFrank Mandarino  * single mixer.
1840db2a4165SFrank Mandarino  *
1841db2a4165SFrank Mandarino  * Returns 0 for success.
1842db2a4165SFrank Mandarino  */
1843db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1844db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1845db2a4165SFrank Mandarino {
1846db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1847db2a4165SFrank Mandarino 
1848db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1849db2a4165SFrank Mandarino 	uinfo->count = 1;
1850f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1851db2a4165SFrank Mandarino 
1852f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1853f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1854db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1855db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1856db2a4165SFrank Mandarino 	return 0;
1857db2a4165SFrank Mandarino }
1858db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1859db2a4165SFrank Mandarino 
1860db2a4165SFrank Mandarino /**
1861db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
1862db2a4165SFrank Mandarino  * @kcontrol: mixer control
1863db2a4165SFrank Mandarino  * @uinfo: control element information
1864db2a4165SFrank Mandarino  *
1865db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
1866db2a4165SFrank Mandarino  *
1867db2a4165SFrank Mandarino  * Returns 0 for success.
1868db2a4165SFrank Mandarino  */
1869db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1870db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1871db2a4165SFrank Mandarino {
1872a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
1873db2a4165SFrank Mandarino 
1874fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1875a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1876a7a4ac86SPhilipp Zabel 	else
1877a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1878a7a4ac86SPhilipp Zabel 
1879db2a4165SFrank Mandarino 	uinfo->count = 1;
1880db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1881a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1882db2a4165SFrank Mandarino 	return 0;
1883db2a4165SFrank Mandarino }
1884db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1885db2a4165SFrank Mandarino 
1886db2a4165SFrank Mandarino /**
1887db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
1888db2a4165SFrank Mandarino  * @kcontrol: mixer control
1889db2a4165SFrank Mandarino  * @uinfo: control element information
1890db2a4165SFrank Mandarino  *
1891db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
1892db2a4165SFrank Mandarino  *
1893db2a4165SFrank Mandarino  * Returns 0 for success.
1894db2a4165SFrank Mandarino  */
1895db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1896db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1897db2a4165SFrank Mandarino {
18984eaa9819SJon Smirl 	struct soc_mixer_control *mc =
18994eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
19004eaa9819SJon Smirl 	int max = mc->max;
1901762b8df7SMark Brown 	unsigned int shift = mc->shift;
1902815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
1903db2a4165SFrank Mandarino 
1904fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1905a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1906a7a4ac86SPhilipp Zabel 	else
1907a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1908a7a4ac86SPhilipp Zabel 
1909db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
1910db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1911a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1912db2a4165SFrank Mandarino 	return 0;
1913db2a4165SFrank Mandarino }
1914db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1915db2a4165SFrank Mandarino 
1916db2a4165SFrank Mandarino /**
1917db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
1918db2a4165SFrank Mandarino  * @kcontrol: mixer control
1919ac11a2b3SMark Brown  * @ucontrol: control element information
1920db2a4165SFrank Mandarino  *
1921db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
1922db2a4165SFrank Mandarino  *
1923db2a4165SFrank Mandarino  * Returns 0 for success.
1924db2a4165SFrank Mandarino  */
1925db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1926db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1927db2a4165SFrank Mandarino {
19284eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19294eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1930db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1931815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1932815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1933815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
19344eaa9819SJon Smirl 	int max = mc->max;
1935815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1936815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1937db2a4165SFrank Mandarino 
1938db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1939db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1940db2a4165SFrank Mandarino 	if (shift != rshift)
1941db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1942db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
1943db2a4165SFrank Mandarino 	if (invert) {
1944db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1945a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
1946db2a4165SFrank Mandarino 		if (shift != rshift)
1947db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
1948a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
1949db2a4165SFrank Mandarino 	}
1950db2a4165SFrank Mandarino 
1951db2a4165SFrank Mandarino 	return 0;
1952db2a4165SFrank Mandarino }
1953db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1954db2a4165SFrank Mandarino 
1955db2a4165SFrank Mandarino /**
1956db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
1957db2a4165SFrank Mandarino  * @kcontrol: mixer control
1958ac11a2b3SMark Brown  * @ucontrol: control element information
1959db2a4165SFrank Mandarino  *
1960db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
1961db2a4165SFrank Mandarino  *
1962db2a4165SFrank Mandarino  * Returns 0 for success.
1963db2a4165SFrank Mandarino  */
1964db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1965db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1966db2a4165SFrank Mandarino {
19674eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19684eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1969db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1970815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1971815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1972815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
19734eaa9819SJon Smirl 	int max = mc->max;
1974815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1975815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
197646f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
1977db2a4165SFrank Mandarino 
1978db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
1979db2a4165SFrank Mandarino 	if (invert)
1980a7a4ac86SPhilipp Zabel 		val = max - val;
1981db2a4165SFrank Mandarino 	val_mask = mask << shift;
1982db2a4165SFrank Mandarino 	val = val << shift;
1983db2a4165SFrank Mandarino 	if (shift != rshift) {
1984db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
1985db2a4165SFrank Mandarino 		if (invert)
1986a7a4ac86SPhilipp Zabel 			val2 = max - val2;
1987db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
1988db2a4165SFrank Mandarino 		val |= val2 << rshift;
1989db2a4165SFrank Mandarino 	}
19906c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
1991db2a4165SFrank Mandarino }
1992db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1993db2a4165SFrank Mandarino 
1994db2a4165SFrank Mandarino /**
1995db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
1996db2a4165SFrank Mandarino  * @kcontrol: mixer control
1997db2a4165SFrank Mandarino  * @uinfo: control element information
1998db2a4165SFrank Mandarino  *
1999db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2000db2a4165SFrank Mandarino  * spans 2 codec registers.
2001db2a4165SFrank Mandarino  *
2002db2a4165SFrank Mandarino  * Returns 0 for success.
2003db2a4165SFrank Mandarino  */
2004db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2005db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2006db2a4165SFrank Mandarino {
20074eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20084eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
20094eaa9819SJon Smirl 	int max = mc->max;
2010db2a4165SFrank Mandarino 
2011fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2012a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2013a7a4ac86SPhilipp Zabel 	else
2014a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2015a7a4ac86SPhilipp Zabel 
2016db2a4165SFrank Mandarino 	uinfo->count = 2;
2017db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2018a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2019db2a4165SFrank Mandarino 	return 0;
2020db2a4165SFrank Mandarino }
2021db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2022db2a4165SFrank Mandarino 
2023db2a4165SFrank Mandarino /**
2024db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2025db2a4165SFrank Mandarino  * @kcontrol: mixer control
2026ac11a2b3SMark Brown  * @ucontrol: control element information
2027db2a4165SFrank Mandarino  *
2028db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2029db2a4165SFrank Mandarino  *
2030db2a4165SFrank Mandarino  * Returns 0 for success.
2031db2a4165SFrank Mandarino  */
2032db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2033db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2034db2a4165SFrank Mandarino {
20354eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20364eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2037db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2038815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2039815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2040815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
20414eaa9819SJon Smirl 	int max = mc->max;
2042815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2043815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2044db2a4165SFrank Mandarino 
2045db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2046db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2047db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2048db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2049db2a4165SFrank Mandarino 	if (invert) {
2050db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2051a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2052db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2053a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2054db2a4165SFrank Mandarino 	}
2055db2a4165SFrank Mandarino 
2056db2a4165SFrank Mandarino 	return 0;
2057db2a4165SFrank Mandarino }
2058db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2059db2a4165SFrank Mandarino 
2060db2a4165SFrank Mandarino /**
2061db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2062db2a4165SFrank Mandarino  * @kcontrol: mixer control
2063ac11a2b3SMark Brown  * @ucontrol: control element information
2064db2a4165SFrank Mandarino  *
2065db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2066db2a4165SFrank Mandarino  *
2067db2a4165SFrank Mandarino  * Returns 0 for success.
2068db2a4165SFrank Mandarino  */
2069db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2070db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2071db2a4165SFrank Mandarino {
20724eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20734eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2074db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2075815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2076815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2077815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
20784eaa9819SJon Smirl 	int max = mc->max;
2079815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2080815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2081db2a4165SFrank Mandarino 	int err;
208246f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2083db2a4165SFrank Mandarino 
2084db2a4165SFrank Mandarino 	val_mask = mask << shift;
2085db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2086db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2087db2a4165SFrank Mandarino 
2088db2a4165SFrank Mandarino 	if (invert) {
2089a7a4ac86SPhilipp Zabel 		val = max - val;
2090a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2091db2a4165SFrank Mandarino 	}
2092db2a4165SFrank Mandarino 
2093db2a4165SFrank Mandarino 	val = val << shift;
2094db2a4165SFrank Mandarino 	val2 = val2 << shift;
2095db2a4165SFrank Mandarino 
20966c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
20973ff3f64bSMark Brown 	if (err < 0)
2098db2a4165SFrank Mandarino 		return err;
2099db2a4165SFrank Mandarino 
21006c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2101db2a4165SFrank Mandarino 	return err;
2102db2a4165SFrank Mandarino }
2103db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2104db2a4165SFrank Mandarino 
2105e13ac2e9SMark Brown /**
2106e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2107e13ac2e9SMark Brown  * @kcontrol: mixer control
2108e13ac2e9SMark Brown  * @uinfo: control element information
2109e13ac2e9SMark Brown  *
2110e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2111e13ac2e9SMark Brown  *
2112e13ac2e9SMark Brown  * Returns 0 for success.
2113e13ac2e9SMark Brown  */
2114e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2115e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2116e13ac2e9SMark Brown {
21174eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21184eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
21194eaa9819SJon Smirl 	int max = mc->max;
21204eaa9819SJon Smirl 	int min = mc->min;
2121e13ac2e9SMark Brown 
2122e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2123e13ac2e9SMark Brown 	uinfo->count = 2;
2124e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2125e13ac2e9SMark Brown 	uinfo->value.integer.max = max-min;
2126e13ac2e9SMark Brown 	return 0;
2127e13ac2e9SMark Brown }
2128e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2129e13ac2e9SMark Brown 
2130e13ac2e9SMark Brown /**
2131e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2132e13ac2e9SMark Brown  * @kcontrol: mixer control
2133ac11a2b3SMark Brown  * @ucontrol: control element information
2134e13ac2e9SMark Brown  *
2135e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2136e13ac2e9SMark Brown  *
2137e13ac2e9SMark Brown  * Returns 0 for success.
2138e13ac2e9SMark Brown  */
2139e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2140e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2141e13ac2e9SMark Brown {
21424eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21434eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2144e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2145815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
21464eaa9819SJon Smirl 	int min = mc->min;
2147e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2148e13ac2e9SMark Brown 
2149e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2150e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2151e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2152e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2153e13ac2e9SMark Brown 	return 0;
2154e13ac2e9SMark Brown }
2155e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2156e13ac2e9SMark Brown 
2157e13ac2e9SMark Brown /**
2158e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2159e13ac2e9SMark Brown  * @kcontrol: mixer control
2160ac11a2b3SMark Brown  * @ucontrol: control element information
2161e13ac2e9SMark Brown  *
2162e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2163e13ac2e9SMark Brown  *
2164e13ac2e9SMark Brown  * Returns 0 for success.
2165e13ac2e9SMark Brown  */
2166e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2167e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2168e13ac2e9SMark Brown {
21694eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21704eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2171e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2172815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
21734eaa9819SJon Smirl 	int min = mc->min;
217446f5822fSDaniel Ribeiro 	unsigned int val;
2175e13ac2e9SMark Brown 
2176e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2177e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2178e13ac2e9SMark Brown 
21796c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2180e13ac2e9SMark Brown }
2181e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2182e13ac2e9SMark Brown 
21838c6529dbSLiam Girdwood /**
21848c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
21858c6529dbSLiam Girdwood  * @dai: DAI
21868c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
21878c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
21888c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
21898c6529dbSLiam Girdwood  *
21908c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
21918c6529dbSLiam Girdwood  */
21928c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
21938c6529dbSLiam Girdwood 	unsigned int freq, int dir)
21948c6529dbSLiam Girdwood {
21953f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_sysclk)
21966335d055SEric Miao 		return dai->ops->set_sysclk(dai, clk_id, freq, dir);
21978c6529dbSLiam Girdwood 	else
21988c6529dbSLiam Girdwood 		return -EINVAL;
21998c6529dbSLiam Girdwood }
22008c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
22018c6529dbSLiam Girdwood 
22028c6529dbSLiam Girdwood /**
22038c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
22048c6529dbSLiam Girdwood  * @dai: DAI
2205ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
22068c6529dbSLiam Girdwood  * @div: new clock divisor.
22078c6529dbSLiam Girdwood  *
22088c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
22098c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
22108c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
22118c6529dbSLiam Girdwood  */
22128c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
22138c6529dbSLiam Girdwood 	int div_id, int div)
22148c6529dbSLiam Girdwood {
22153f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_clkdiv)
22166335d055SEric Miao 		return dai->ops->set_clkdiv(dai, div_id, div);
22178c6529dbSLiam Girdwood 	else
22188c6529dbSLiam Girdwood 		return -EINVAL;
22198c6529dbSLiam Girdwood }
22208c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
22218c6529dbSLiam Girdwood 
22228c6529dbSLiam Girdwood /**
22238c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
22248c6529dbSLiam Girdwood  * @dai: DAI
22258c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
222685488037SMark Brown  * @source: DAI specific source for the PLL
22278c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
22288c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
22298c6529dbSLiam Girdwood  *
22308c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
22318c6529dbSLiam Girdwood  */
223285488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
223385488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
22348c6529dbSLiam Girdwood {
22353f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_pll)
223685488037SMark Brown 		return dai->ops->set_pll(dai, pll_id, source,
223785488037SMark Brown 					 freq_in, freq_out);
22388c6529dbSLiam Girdwood 	else
22398c6529dbSLiam Girdwood 		return -EINVAL;
22408c6529dbSLiam Girdwood }
22418c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
22428c6529dbSLiam Girdwood 
22438c6529dbSLiam Girdwood /**
22448c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
22458c6529dbSLiam Girdwood  * @dai: DAI
22468c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
22478c6529dbSLiam Girdwood  *
22488c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
22498c6529dbSLiam Girdwood  */
22508c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
22518c6529dbSLiam Girdwood {
22523f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_fmt)
22536335d055SEric Miao 		return dai->ops->set_fmt(dai, fmt);
22548c6529dbSLiam Girdwood 	else
22558c6529dbSLiam Girdwood 		return -EINVAL;
22568c6529dbSLiam Girdwood }
22578c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
22588c6529dbSLiam Girdwood 
22598c6529dbSLiam Girdwood /**
22608c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
22618c6529dbSLiam Girdwood  * @dai: DAI
2262a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2263a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
22648c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2265a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
22668c6529dbSLiam Girdwood  *
22678c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
22688c6529dbSLiam Girdwood  * specific.
22698c6529dbSLiam Girdwood  */
22708c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2271a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
22728c6529dbSLiam Girdwood {
22733f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tdm_slot)
2274a5479e38SDaniel Ribeiro 		return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2275a5479e38SDaniel Ribeiro 				slots, slot_width);
22768c6529dbSLiam Girdwood 	else
22778c6529dbSLiam Girdwood 		return -EINVAL;
22788c6529dbSLiam Girdwood }
22798c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
22808c6529dbSLiam Girdwood 
22818c6529dbSLiam Girdwood /**
2282472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2283472df3cbSBarry Song  * @dai: DAI
2284472df3cbSBarry Song  * @tx_num: how many TX channels
2285472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2286472df3cbSBarry Song  *           0~num-1 uses
2287472df3cbSBarry Song  * @rx_num: how many RX channels
2288472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2289472df3cbSBarry Song  *           0~num-1 uses
2290472df3cbSBarry Song  *
2291472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2292472df3cbSBarry Song  */
2293472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2294472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2295472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2296472df3cbSBarry Song {
2297472df3cbSBarry Song 	if (dai->ops && dai->ops->set_channel_map)
2298472df3cbSBarry Song 		return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2299472df3cbSBarry Song 			rx_num, rx_slot);
2300472df3cbSBarry Song 	else
2301472df3cbSBarry Song 		return -EINVAL;
2302472df3cbSBarry Song }
2303472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2304472df3cbSBarry Song 
2305472df3cbSBarry Song /**
23068c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
23078c6529dbSLiam Girdwood  * @dai: DAI
23088c6529dbSLiam Girdwood  * @tristate: tristate enable
23098c6529dbSLiam Girdwood  *
23108c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
23118c6529dbSLiam Girdwood  */
23128c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
23138c6529dbSLiam Girdwood {
23143f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tristate)
23156335d055SEric Miao 		return dai->ops->set_tristate(dai, tristate);
23168c6529dbSLiam Girdwood 	else
23178c6529dbSLiam Girdwood 		return -EINVAL;
23188c6529dbSLiam Girdwood }
23198c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
23208c6529dbSLiam Girdwood 
23218c6529dbSLiam Girdwood /**
23228c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
23238c6529dbSLiam Girdwood  * @dai: DAI
23248c6529dbSLiam Girdwood  * @mute: mute enable
23258c6529dbSLiam Girdwood  *
23268c6529dbSLiam Girdwood  * Mutes the DAI DAC.
23278c6529dbSLiam Girdwood  */
23288c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
23298c6529dbSLiam Girdwood {
23303f1a4d82SMark Brown 	if (dai->ops && dai->ops->digital_mute)
23316335d055SEric Miao 		return dai->ops->digital_mute(dai, mute);
23328c6529dbSLiam Girdwood 	else
23338c6529dbSLiam Girdwood 		return -EINVAL;
23348c6529dbSLiam Girdwood }
23358c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
23368c6529dbSLiam Girdwood 
2337c5af3a2eSMark Brown /**
2338c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2339c5af3a2eSMark Brown  *
2340ac11a2b3SMark Brown  * @card: Card to register
2341c5af3a2eSMark Brown  *
2342c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2343c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2344c5af3a2eSMark Brown  * registration APIs.
2345c5af3a2eSMark Brown  */
2346c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
2347c5af3a2eSMark Brown {
2348c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2349c5af3a2eSMark Brown 		return -EINVAL;
2350c5af3a2eSMark Brown 
2351c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2352c5af3a2eSMark Brown 	card->instantiated = 0;
2353c5af3a2eSMark Brown 
2354c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2355c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2356435c5e25SMark Brown 	snd_soc_instantiate_cards();
2357c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2358c5af3a2eSMark Brown 
2359c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2360c5af3a2eSMark Brown 
2361c5af3a2eSMark Brown 	return 0;
2362c5af3a2eSMark Brown }
2363c5af3a2eSMark Brown 
2364c5af3a2eSMark Brown /**
2365c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2366c5af3a2eSMark Brown  *
2367ac11a2b3SMark Brown  * @card: Card to unregister
2368c5af3a2eSMark Brown  *
2369c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2370c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2371c5af3a2eSMark Brown  * registration APIs.
2372c5af3a2eSMark Brown  */
2373c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
2374c5af3a2eSMark Brown {
2375c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2376c5af3a2eSMark Brown 	list_del(&card->list);
2377c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2378c5af3a2eSMark Brown 
2379c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2380c5af3a2eSMark Brown 
2381c5af3a2eSMark Brown 	return 0;
2382c5af3a2eSMark Brown }
2383c5af3a2eSMark Brown 
23849115171aSMark Brown /**
23859115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
23869115171aSMark Brown  *
2387ac11a2b3SMark Brown  * @dai: DAI to register
23889115171aSMark Brown  */
23899115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai)
23909115171aSMark Brown {
23919115171aSMark Brown 	if (!dai->name)
23929115171aSMark Brown 		return -EINVAL;
23939115171aSMark Brown 
23949115171aSMark Brown 	/* The device should become mandatory over time */
23959115171aSMark Brown 	if (!dai->dev)
23969115171aSMark Brown 		printk(KERN_WARNING "No device for DAI %s\n", dai->name);
23979115171aSMark Brown 
23986335d055SEric Miao 	if (!dai->ops)
23996335d055SEric Miao 		dai->ops = &null_dai_ops;
24006335d055SEric Miao 
24019115171aSMark Brown 	INIT_LIST_HEAD(&dai->list);
24029115171aSMark Brown 
24039115171aSMark Brown 	mutex_lock(&client_mutex);
24049115171aSMark Brown 	list_add(&dai->list, &dai_list);
2405435c5e25SMark Brown 	snd_soc_instantiate_cards();
24069115171aSMark Brown 	mutex_unlock(&client_mutex);
24079115171aSMark Brown 
24089115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
24099115171aSMark Brown 
24109115171aSMark Brown 	return 0;
24119115171aSMark Brown }
24129115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
24139115171aSMark Brown 
24149115171aSMark Brown /**
24159115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
24169115171aSMark Brown  *
2417ac11a2b3SMark Brown  * @dai: DAI to unregister
24189115171aSMark Brown  */
24199115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai)
24209115171aSMark Brown {
24219115171aSMark Brown 	mutex_lock(&client_mutex);
24229115171aSMark Brown 	list_del(&dai->list);
24239115171aSMark Brown 	mutex_unlock(&client_mutex);
24249115171aSMark Brown 
24259115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
24269115171aSMark Brown }
24279115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
24289115171aSMark Brown 
24299115171aSMark Brown /**
24309115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
24319115171aSMark Brown  *
2432ac11a2b3SMark Brown  * @dai: Array of DAIs to register
2433ac11a2b3SMark Brown  * @count: Number of DAIs
24349115171aSMark Brown  */
24359115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
24369115171aSMark Brown {
24379115171aSMark Brown 	int i, ret;
24389115171aSMark Brown 
24399115171aSMark Brown 	for (i = 0; i < count; i++) {
24409115171aSMark Brown 		ret = snd_soc_register_dai(&dai[i]);
24419115171aSMark Brown 		if (ret != 0)
24429115171aSMark Brown 			goto err;
24439115171aSMark Brown 	}
24449115171aSMark Brown 
24459115171aSMark Brown 	return 0;
24469115171aSMark Brown 
24479115171aSMark Brown err:
24489115171aSMark Brown 	for (i--; i >= 0; i--)
24499115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
24509115171aSMark Brown 
24519115171aSMark Brown 	return ret;
24529115171aSMark Brown }
24539115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
24549115171aSMark Brown 
24559115171aSMark Brown /**
24569115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
24579115171aSMark Brown  *
2458ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
2459ac11a2b3SMark Brown  * @count: Number of DAIs
24609115171aSMark Brown  */
24619115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
24629115171aSMark Brown {
24639115171aSMark Brown 	int i;
24649115171aSMark Brown 
24659115171aSMark Brown 	for (i = 0; i < count; i++)
24669115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
24679115171aSMark Brown }
24689115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
24699115171aSMark Brown 
247012a48a8cSMark Brown /**
247112a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
247212a48a8cSMark Brown  *
2473ac11a2b3SMark Brown  * @platform: platform to register
247412a48a8cSMark Brown  */
247512a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform)
247612a48a8cSMark Brown {
247712a48a8cSMark Brown 	if (!platform->name)
247812a48a8cSMark Brown 		return -EINVAL;
247912a48a8cSMark Brown 
248012a48a8cSMark Brown 	INIT_LIST_HEAD(&platform->list);
248112a48a8cSMark Brown 
248212a48a8cSMark Brown 	mutex_lock(&client_mutex);
248312a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
2484435c5e25SMark Brown 	snd_soc_instantiate_cards();
248512a48a8cSMark Brown 	mutex_unlock(&client_mutex);
248612a48a8cSMark Brown 
248712a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
248812a48a8cSMark Brown 
248912a48a8cSMark Brown 	return 0;
249012a48a8cSMark Brown }
249112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
249212a48a8cSMark Brown 
249312a48a8cSMark Brown /**
249412a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
249512a48a8cSMark Brown  *
2496ac11a2b3SMark Brown  * @platform: platform to unregister
249712a48a8cSMark Brown  */
249812a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform)
249912a48a8cSMark Brown {
250012a48a8cSMark Brown 	mutex_lock(&client_mutex);
250112a48a8cSMark Brown 	list_del(&platform->list);
250212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
250312a48a8cSMark Brown 
250412a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
250512a48a8cSMark Brown }
250612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
250712a48a8cSMark Brown 
2508151ab22cSMark Brown static u64 codec_format_map[] = {
2509151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2510151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2511151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2512151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2513151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2514151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2515151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2516151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2517151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2518151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2519151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2520151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2521151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2522151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2523151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2524151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2525151ab22cSMark Brown };
2526151ab22cSMark Brown 
2527151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2528151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2529151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2530151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2531151ab22cSMark Brown  */
2532151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2533151ab22cSMark Brown {
2534151ab22cSMark Brown 	int i;
2535151ab22cSMark Brown 
2536151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2537151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2538151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
2539151ab22cSMark Brown }
2540151ab22cSMark Brown 
25410d0cf00aSMark Brown /**
25420d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
25430d0cf00aSMark Brown  *
2544ac11a2b3SMark Brown  * @codec: codec to register
25450d0cf00aSMark Brown  */
25460d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec)
25470d0cf00aSMark Brown {
2548151ab22cSMark Brown 	int i;
2549151ab22cSMark Brown 
25500d0cf00aSMark Brown 	if (!codec->name)
25510d0cf00aSMark Brown 		return -EINVAL;
25520d0cf00aSMark Brown 
25530d0cf00aSMark Brown 	/* The device should become mandatory over time */
25540d0cf00aSMark Brown 	if (!codec->dev)
25550d0cf00aSMark Brown 		printk(KERN_WARNING "No device for codec %s\n", codec->name);
25560d0cf00aSMark Brown 
25570d0cf00aSMark Brown 	INIT_LIST_HEAD(&codec->list);
25580d0cf00aSMark Brown 
2559151ab22cSMark Brown 	for (i = 0; i < codec->num_dai; i++) {
2560151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].playback);
2561151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].capture);
2562151ab22cSMark Brown 	}
2563151ab22cSMark Brown 
25640d0cf00aSMark Brown 	mutex_lock(&client_mutex);
25650d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
25660d0cf00aSMark Brown 	snd_soc_instantiate_cards();
25670d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
25680d0cf00aSMark Brown 
25690d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
25700d0cf00aSMark Brown 
25710d0cf00aSMark Brown 	return 0;
25720d0cf00aSMark Brown }
25730d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
25740d0cf00aSMark Brown 
25750d0cf00aSMark Brown /**
25760d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
25770d0cf00aSMark Brown  *
2578ac11a2b3SMark Brown  * @codec: codec to unregister
25790d0cf00aSMark Brown  */
25800d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec)
25810d0cf00aSMark Brown {
25820d0cf00aSMark Brown 	mutex_lock(&client_mutex);
25830d0cf00aSMark Brown 	list_del(&codec->list);
25840d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
25850d0cf00aSMark Brown 
25860d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
25870d0cf00aSMark Brown }
25880d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
25890d0cf00aSMark Brown 
2590c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
2591db2a4165SFrank Mandarino {
2592384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2593384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
2594384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
2595384c89e2SMark Brown 		printk(KERN_WARNING
2596384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
2597384c89e2SMark Brown 		debugfs_root = NULL;
2598384c89e2SMark Brown 	}
2599384c89e2SMark Brown #endif
2600384c89e2SMark Brown 
2601db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
2602db2a4165SFrank Mandarino }
2603db2a4165SFrank Mandarino 
26047d8c16a6SMark Brown static void __exit snd_soc_exit(void)
2605db2a4165SFrank Mandarino {
2606384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2607384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
2608384c89e2SMark Brown #endif
2609db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
2610db2a4165SFrank Mandarino }
2611db2a4165SFrank Mandarino 
2612db2a4165SFrank Mandarino module_init(snd_soc_init);
2613db2a4165SFrank Mandarino module_exit(snd_soc_exit);
2614db2a4165SFrank Mandarino 
2615db2a4165SFrank Mandarino /* Module information */
2616d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2617db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
2618db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
26198b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
2620