xref: /openbmc/linux/sound/soc/soc-core.c (revision 258020d0882e89c1462800a70eb414b8a4fec78c)
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 
45714dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
45814dc5734SJassi Brar 		cpu_dai->playback.active++;
45914dc5734SJassi Brar 		codec_dai->playback.active++;
46014dc5734SJassi Brar 	} else {
46114dc5734SJassi Brar 		cpu_dai->capture.active++;
46214dc5734SJassi Brar 		codec_dai->capture.active++;
46314dc5734SJassi Brar 	}
46414dc5734SJassi Brar 	cpu_dai->active++;
46514dc5734SJassi 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 
53714dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
53814dc5734SJassi Brar 		cpu_dai->playback.active--;
53914dc5734SJassi Brar 		codec_dai->playback.active--;
54014dc5734SJassi Brar 	} else {
54114dc5734SJassi Brar 		cpu_dai->capture.active--;
54214dc5734SJassi Brar 		codec_dai->capture.active--;
543db2a4165SFrank Mandarino 	}
54414dc5734SJassi Brar 
54514dc5734SJassi Brar 	cpu_dai->active--;
54614dc5734SJassi 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 
803377b6f62SPeter Ujfalusi /*
804377b6f62SPeter Ujfalusi  * soc level wrapper for pointer callback
805*258020d0SPeter Ujfalusi  * If cpu_dai, codec_dai, platform driver has the delay callback, than
806*258020d0SPeter Ujfalusi  * the runtime->delay will be updated accordingly.
807377b6f62SPeter Ujfalusi  */
808377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
809377b6f62SPeter Ujfalusi {
810377b6f62SPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
811377b6f62SPeter Ujfalusi 	struct snd_soc_device *socdev = rtd->socdev;
812377b6f62SPeter Ujfalusi 	struct snd_soc_card *card = socdev->card;
813377b6f62SPeter Ujfalusi 	struct snd_soc_platform *platform = card->platform;
814*258020d0SPeter Ujfalusi 	struct snd_soc_dai_link *machine = rtd->dai;
815*258020d0SPeter Ujfalusi 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
816*258020d0SPeter Ujfalusi 	struct snd_soc_dai *codec_dai = machine->codec_dai;
817*258020d0SPeter Ujfalusi 	struct snd_pcm_runtime *runtime = substream->runtime;
818377b6f62SPeter Ujfalusi 	snd_pcm_uframes_t offset = 0;
819*258020d0SPeter Ujfalusi 	snd_pcm_sframes_t delay = 0;
820377b6f62SPeter Ujfalusi 
821377b6f62SPeter Ujfalusi 	if (platform->pcm_ops->pointer)
822377b6f62SPeter Ujfalusi 		offset = platform->pcm_ops->pointer(substream);
823377b6f62SPeter Ujfalusi 
824*258020d0SPeter Ujfalusi 	if (cpu_dai->ops->delay)
825*258020d0SPeter Ujfalusi 		delay += cpu_dai->ops->delay(substream, cpu_dai);
826*258020d0SPeter Ujfalusi 
827*258020d0SPeter Ujfalusi 	if (codec_dai->ops->delay)
828*258020d0SPeter Ujfalusi 		delay += codec_dai->ops->delay(substream, codec_dai);
829*258020d0SPeter Ujfalusi 
830*258020d0SPeter Ujfalusi 	if (platform->delay)
831*258020d0SPeter Ujfalusi 		delay += platform->delay(substream, codec_dai);
832*258020d0SPeter Ujfalusi 
833*258020d0SPeter Ujfalusi 	runtime->delay = delay;
834*258020d0SPeter Ujfalusi 
835377b6f62SPeter Ujfalusi 	return offset;
836377b6f62SPeter Ujfalusi }
837377b6f62SPeter Ujfalusi 
838db2a4165SFrank Mandarino /* ASoC PCM operations */
839db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
840db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
841db2a4165SFrank Mandarino 	.close		= soc_codec_close,
842db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
843db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
844db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
845db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
846377b6f62SPeter Ujfalusi 	.pointer	= soc_pcm_pointer,
847db2a4165SFrank Mandarino };
848db2a4165SFrank Mandarino 
849db2a4165SFrank Mandarino #ifdef CONFIG_PM
850db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
851416356fcSMark Brown static int soc_suspend(struct device *dev)
852db2a4165SFrank Mandarino {
853416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
854db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
85587506549SMark Brown 	struct snd_soc_card *card = socdev->card;
85687689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
857db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
8586627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
859db2a4165SFrank Mandarino 	int i;
860db2a4165SFrank Mandarino 
861e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
862e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
863e3509ff0SDaniel Mack 	 */
864e3509ff0SDaniel Mack 	if (!codec)
865e3509ff0SDaniel Mack 		return 0;
866e3509ff0SDaniel Mack 
8676ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
8686ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
8696ed25978SAndy Green 	 */
8706ed25978SAndy Green 	snd_power_lock(codec->card);
8716ed25978SAndy Green 	snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
8726ed25978SAndy Green 	snd_power_unlock(codec->card);
8736ed25978SAndy Green 
8746ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
8756ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
8766ed25978SAndy Green 
877db2a4165SFrank Mandarino 	/* mute any active DAC's */
878dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
879dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
8806335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
8816335d055SEric Miao 			dai->ops->digital_mute(dai, 1);
882db2a4165SFrank Mandarino 	}
883db2a4165SFrank Mandarino 
8844ccab3e7SLiam Girdwood 	/* suspend all pcms */
88587506549SMark Brown 	for (i = 0; i < card->num_links; i++)
88687506549SMark Brown 		snd_pcm_suspend_all(card->dai_link[i].pcm);
8874ccab3e7SLiam Girdwood 
88887506549SMark Brown 	if (card->suspend_pre)
889416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
890db2a4165SFrank Mandarino 
89187506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
89287506549SMark Brown 		struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
8933ba9e10aSMark Brown 		if (cpu_dai->suspend && !cpu_dai->ac97_control)
894dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
895db2a4165SFrank Mandarino 		if (platform->suspend)
896d273ebe7Sjassi brar 			platform->suspend(&card->dai_link[i]);
897db2a4165SFrank Mandarino 	}
898db2a4165SFrank Mandarino 
899db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
9006308419aSMark Brown 	run_delayed_work(&card->delayed_work);
9010be9898aSMark Brown 	codec->suspend_bias_level = codec->bias_level;
902db2a4165SFrank Mandarino 
903db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
904db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
905db2a4165SFrank Mandarino 		if (stream != NULL)
906db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
907db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
908db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
909db2a4165SFrank Mandarino 		if (stream != NULL)
910db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
911db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
912db2a4165SFrank Mandarino 	}
913db2a4165SFrank Mandarino 
914db2a4165SFrank Mandarino 	if (codec_dev->suspend)
915416356fcSMark Brown 		codec_dev->suspend(pdev, PMSG_SUSPEND);
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->suspend && cpu_dai->ac97_control)
920dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
921db2a4165SFrank Mandarino 	}
922db2a4165SFrank Mandarino 
92387506549SMark Brown 	if (card->suspend_post)
924416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
925db2a4165SFrank Mandarino 
926db2a4165SFrank Mandarino 	return 0;
927db2a4165SFrank Mandarino }
928db2a4165SFrank Mandarino 
9296ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
9306ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
9316ed25978SAndy Green  */
9326ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
933db2a4165SFrank Mandarino {
9346308419aSMark Brown 	struct snd_soc_card *card = container_of(work,
9356308419aSMark Brown 						 struct snd_soc_card,
9366ed25978SAndy Green 						 deferred_resume_work);
9376308419aSMark Brown 	struct snd_soc_device *socdev = card->socdev;
93887689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
939db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
9406627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
9416ed25978SAndy Green 	struct platform_device *pdev = to_platform_device(socdev->dev);
942db2a4165SFrank Mandarino 	int i;
943db2a4165SFrank Mandarino 
9446ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
9456ed25978SAndy Green 	 * so userspace apps are blocked from touching us
9466ed25978SAndy Green 	 */
9476ed25978SAndy Green 
948fde22f27SMark Brown 	dev_dbg(socdev->dev, "starting resume work\n");
9496ed25978SAndy Green 
95087506549SMark Brown 	if (card->resume_pre)
95187506549SMark Brown 		card->resume_pre(pdev);
952db2a4165SFrank Mandarino 
95387506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
95487506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
9553ba9e10aSMark Brown 		if (cpu_dai->resume && cpu_dai->ac97_control)
956dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
957db2a4165SFrank Mandarino 	}
958db2a4165SFrank Mandarino 
959db2a4165SFrank Mandarino 	if (codec_dev->resume)
960db2a4165SFrank Mandarino 		codec_dev->resume(pdev);
961db2a4165SFrank Mandarino 
962db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
963db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
964db2a4165SFrank Mandarino 		if (stream != NULL)
965db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
966db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
967db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
968db2a4165SFrank Mandarino 		if (stream != NULL)
969db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
970db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
971db2a4165SFrank Mandarino 	}
972db2a4165SFrank Mandarino 
9733ff3f64bSMark Brown 	/* unmute any active DACs */
974dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
975dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
9766335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
9776335d055SEric Miao 			dai->ops->digital_mute(dai, 0);
978db2a4165SFrank Mandarino 	}
979db2a4165SFrank Mandarino 
98087506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
98187506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
9823ba9e10aSMark Brown 		if (cpu_dai->resume && !cpu_dai->ac97_control)
983dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
984db2a4165SFrank Mandarino 		if (platform->resume)
985d273ebe7Sjassi brar 			platform->resume(&card->dai_link[i]);
986db2a4165SFrank Mandarino 	}
987db2a4165SFrank Mandarino 
98887506549SMark Brown 	if (card->resume_post)
98987506549SMark Brown 		card->resume_post(pdev);
990db2a4165SFrank Mandarino 
991fde22f27SMark Brown 	dev_dbg(socdev->dev, "resume work completed\n");
9926ed25978SAndy Green 
9936ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
9946ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
9956ed25978SAndy Green }
9966ed25978SAndy Green 
9976ed25978SAndy Green /* powers up audio subsystem after a suspend */
998416356fcSMark Brown static int soc_resume(struct device *dev)
9996ed25978SAndy Green {
1000416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
10016ed25978SAndy Green 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
10026308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
100364ab9baaSMark Brown 	struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
10046ed25978SAndy Green 
100564ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
100664ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
100764ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
100864ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
100964ab9baaSMark Brown 	 */
101064ab9baaSMark Brown 	if (cpu_dai->ac97_control) {
101164ab9baaSMark Brown 		dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
101264ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
101364ab9baaSMark Brown 	} else {
101464ab9baaSMark Brown 		dev_dbg(socdev->dev, "Scheduling resume work\n");
10156308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
1016fde22f27SMark Brown 			dev_err(socdev->dev, "resume work item may be lost\n");
101764ab9baaSMark Brown 	}
10186ed25978SAndy Green 
1019db2a4165SFrank Mandarino 	return 0;
1020db2a4165SFrank Mandarino }
1021db2a4165SFrank Mandarino #else
1022db2a4165SFrank Mandarino #define soc_suspend	NULL
1023db2a4165SFrank Mandarino #define soc_resume	NULL
1024db2a4165SFrank Mandarino #endif
1025db2a4165SFrank Mandarino 
102602a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
102702a06d30SBarry Song };
102802a06d30SBarry Song 
1029435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card)
1030db2a4165SFrank Mandarino {
1031435c5e25SMark Brown 	struct platform_device *pdev = container_of(card->dev,
1032435c5e25SMark Brown 						    struct platform_device,
1033435c5e25SMark Brown 						    dev);
1034435c5e25SMark Brown 	struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
1035fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1036435c5e25SMark Brown 	struct snd_soc_platform *platform;
1037435c5e25SMark Brown 	struct snd_soc_dai *dai;
10386b05eda6SMark Brown 	int i, found, ret, ac97;
1039db2a4165SFrank Mandarino 
1040435c5e25SMark Brown 	if (card->instantiated)
1041435c5e25SMark Brown 		return;
10426308419aSMark Brown 
1043435c5e25SMark Brown 	found = 0;
1044435c5e25SMark Brown 	list_for_each_entry(platform, &platform_list, list)
1045435c5e25SMark Brown 		if (card->platform == platform) {
1046435c5e25SMark Brown 			found = 1;
1047435c5e25SMark Brown 			break;
1048435c5e25SMark Brown 		}
1049435c5e25SMark Brown 	if (!found) {
1050435c5e25SMark Brown 		dev_dbg(card->dev, "Platform %s not registered\n",
1051435c5e25SMark Brown 			card->platform->name);
1052435c5e25SMark Brown 		return;
1053c5af3a2eSMark Brown 	}
1054c5af3a2eSMark Brown 
10556b05eda6SMark Brown 	ac97 = 0;
1056435c5e25SMark Brown 	for (i = 0; i < card->num_links; i++) {
1057435c5e25SMark Brown 		found = 0;
1058435c5e25SMark Brown 		list_for_each_entry(dai, &dai_list, list)
1059435c5e25SMark Brown 			if (card->dai_link[i].cpu_dai == dai) {
1060435c5e25SMark Brown 				found = 1;
1061435c5e25SMark Brown 				break;
1062435c5e25SMark Brown 			}
1063435c5e25SMark Brown 		if (!found) {
1064435c5e25SMark Brown 			dev_dbg(card->dev, "DAI %s not registered\n",
1065435c5e25SMark Brown 				card->dai_link[i].cpu_dai->name);
1066435c5e25SMark Brown 			return;
1067435c5e25SMark Brown 		}
10686b05eda6SMark Brown 
10696b05eda6SMark Brown 		if (card->dai_link[i].cpu_dai->ac97_control)
10706b05eda6SMark Brown 			ac97 = 1;
10716b05eda6SMark Brown 	}
10726b05eda6SMark Brown 
107302a06d30SBarry Song 	for (i = 0; i < card->num_links; i++) {
107402a06d30SBarry Song 		if (!card->dai_link[i].codec_dai->ops)
107502a06d30SBarry Song 			card->dai_link[i].codec_dai->ops = &null_dai_ops;
107602a06d30SBarry Song 	}
107702a06d30SBarry Song 
10786b05eda6SMark Brown 	/* If we have AC97 in the system then don't wait for the
10796b05eda6SMark Brown 	 * codec.  This will need revisiting if we have to handle
10806b05eda6SMark Brown 	 * systems with mixed AC97 and non-AC97 parts.  Only check for
10816b05eda6SMark Brown 	 * DAIs currently; we can't do this per link since some AC97
10826b05eda6SMark Brown 	 * codecs have non-AC97 DAIs.
10836b05eda6SMark Brown 	 */
10846b05eda6SMark Brown 	if (!ac97)
10856b05eda6SMark Brown 		for (i = 0; i < card->num_links; i++) {
10866b05eda6SMark Brown 			found = 0;
10876b05eda6SMark Brown 			list_for_each_entry(dai, &dai_list, list)
10886b05eda6SMark Brown 				if (card->dai_link[i].codec_dai == dai) {
10896b05eda6SMark Brown 					found = 1;
10906b05eda6SMark Brown 					break;
10916b05eda6SMark Brown 				}
10926b05eda6SMark Brown 			if (!found) {
10936b05eda6SMark Brown 				dev_dbg(card->dev, "DAI %s not registered\n",
10946b05eda6SMark Brown 					card->dai_link[i].codec_dai->name);
10956b05eda6SMark Brown 				return;
10966b05eda6SMark Brown 			}
1097435c5e25SMark Brown 		}
1098435c5e25SMark Brown 
1099435c5e25SMark Brown 	/* Note that we do not current check for codec components */
1100435c5e25SMark Brown 
1101435c5e25SMark Brown 	dev_dbg(card->dev, "All components present, instantiating\n");
1102435c5e25SMark Brown 
1103435c5e25SMark Brown 	/* Found everything, bring it up */
110496dd3622SMark Brown 	card->pmdown_time = pmdown_time;
110596dd3622SMark Brown 
110687506549SMark Brown 	if (card->probe) {
110787506549SMark Brown 		ret = card->probe(pdev);
1108db2a4165SFrank Mandarino 		if (ret < 0)
1109435c5e25SMark Brown 			return;
1110db2a4165SFrank Mandarino 	}
1111db2a4165SFrank Mandarino 
111287506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
111387506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1114db2a4165SFrank Mandarino 		if (cpu_dai->probe) {
1115bdb92876SMark Brown 			ret = cpu_dai->probe(pdev, cpu_dai);
1116db2a4165SFrank Mandarino 			if (ret < 0)
1117db2a4165SFrank Mandarino 				goto cpu_dai_err;
1118db2a4165SFrank Mandarino 		}
1119db2a4165SFrank Mandarino 	}
1120db2a4165SFrank Mandarino 
1121db2a4165SFrank Mandarino 	if (codec_dev->probe) {
1122db2a4165SFrank Mandarino 		ret = codec_dev->probe(pdev);
1123db2a4165SFrank Mandarino 		if (ret < 0)
1124db2a4165SFrank Mandarino 			goto cpu_dai_err;
1125db2a4165SFrank Mandarino 	}
1126fe3e78e0SMark Brown 	codec = card->codec;
1127db2a4165SFrank Mandarino 
1128db2a4165SFrank Mandarino 	if (platform->probe) {
1129db2a4165SFrank Mandarino 		ret = platform->probe(pdev);
1130db2a4165SFrank Mandarino 		if (ret < 0)
1131db2a4165SFrank Mandarino 			goto platform_err;
1132db2a4165SFrank Mandarino 	}
1133db2a4165SFrank Mandarino 
1134db2a4165SFrank Mandarino 	/* DAPM stream work */
11356308419aSMark Brown 	INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
11361301a964SRandy Dunlap #ifdef CONFIG_PM
11376ed25978SAndy Green 	/* deferred resume work */
11386308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
11391301a964SRandy Dunlap #endif
11406ed25978SAndy Green 
1141fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1142fe3e78e0SMark Brown 		if (card->dai_link[i].init) {
1143fe3e78e0SMark Brown 			ret = card->dai_link[i].init(codec);
1144fe3e78e0SMark Brown 			if (ret < 0) {
1145fe3e78e0SMark Brown 				printk(KERN_ERR "asoc: failed to init %s\n",
1146fe3e78e0SMark Brown 					card->dai_link[i].stream_name);
1147fe3e78e0SMark Brown 				continue;
1148fe3e78e0SMark Brown 			}
1149fe3e78e0SMark Brown 		}
1150f7732053SBarry Song 		if (card->dai_link[i].codec_dai->ac97_control)
1151fe3e78e0SMark Brown 			ac97 = 1;
1152fe3e78e0SMark Brown 	}
1153fe3e78e0SMark Brown 
1154fe3e78e0SMark Brown 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1155fe3e78e0SMark Brown 		 "%s",  card->name);
1156fe3e78e0SMark Brown 	snprintf(codec->card->longname, sizeof(codec->card->longname),
1157fe3e78e0SMark Brown 		 "%s (%s)", card->name, codec->name);
1158fe3e78e0SMark Brown 
1159fe3e78e0SMark Brown 	/* Make sure all DAPM widgets are instantiated */
1160fe3e78e0SMark Brown 	snd_soc_dapm_new_widgets(codec);
1161fe3e78e0SMark Brown 
1162fe3e78e0SMark Brown 	ret = snd_card_register(codec->card);
1163fe3e78e0SMark Brown 	if (ret < 0) {
1164fe3e78e0SMark Brown 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1165fe3e78e0SMark Brown 				codec->name);
1166fe3e78e0SMark Brown 		goto card_err;
1167fe3e78e0SMark Brown 	}
1168fe3e78e0SMark Brown 
1169fe3e78e0SMark Brown 	mutex_lock(&codec->mutex);
1170fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1171fe3e78e0SMark Brown 	/* Only instantiate AC97 if not already done by the adaptor
1172fe3e78e0SMark Brown 	 * for the generic AC97 subsystem.
1173fe3e78e0SMark Brown 	 */
1174fe3e78e0SMark Brown 	if (ac97 && strcmp(codec->name, "AC97") != 0) {
1175fe3e78e0SMark Brown 		ret = soc_ac97_dev_register(codec);
1176fe3e78e0SMark Brown 		if (ret < 0) {
1177fe3e78e0SMark Brown 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1178fe3e78e0SMark Brown 			snd_card_free(codec->card);
1179fe3e78e0SMark Brown 			mutex_unlock(&codec->mutex);
1180fe3e78e0SMark Brown 			goto card_err;
1181fe3e78e0SMark Brown 		}
1182fe3e78e0SMark Brown 	}
1183fe3e78e0SMark Brown #endif
1184fe3e78e0SMark Brown 
1185fe3e78e0SMark Brown 	ret = snd_soc_dapm_sys_add(card->socdev->dev);
1186fe3e78e0SMark Brown 	if (ret < 0)
1187fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1188fe3e78e0SMark Brown 
1189dbe21408SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1190dbe21408SMark Brown 	if (ret < 0)
1191dbe21408SMark Brown 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1192dbe21408SMark Brown 
1193fe3e78e0SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1194fe3e78e0SMark Brown 	if (ret < 0)
1195fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1196fe3e78e0SMark Brown 
1197fe3e78e0SMark Brown 	soc_init_codec_debugfs(codec);
1198fe3e78e0SMark Brown 	mutex_unlock(&codec->mutex);
1199fe3e78e0SMark Brown 
1200435c5e25SMark Brown 	card->instantiated = 1;
1201435c5e25SMark Brown 
1202435c5e25SMark Brown 	return;
1203db2a4165SFrank Mandarino 
1204fe3e78e0SMark Brown card_err:
1205fe3e78e0SMark Brown 	if (platform->remove)
1206fe3e78e0SMark Brown 		platform->remove(pdev);
1207fe3e78e0SMark Brown 
1208db2a4165SFrank Mandarino platform_err:
1209db2a4165SFrank Mandarino 	if (codec_dev->remove)
1210db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1211db2a4165SFrank Mandarino 
1212db2a4165SFrank Mandarino cpu_dai_err:
121318b9b3d9SLiam Girdwood 	for (i--; i >= 0; i--) {
121487506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1215db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1216bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1217db2a4165SFrank Mandarino 	}
1218db2a4165SFrank Mandarino 
121987506549SMark Brown 	if (card->remove)
122087506549SMark Brown 		card->remove(pdev);
1221435c5e25SMark Brown }
1222db2a4165SFrank Mandarino 
1223435c5e25SMark Brown /*
1224435c5e25SMark Brown  * Attempt to initialise any uninitalised cards.  Must be called with
1225435c5e25SMark Brown  * client_mutex.
1226435c5e25SMark Brown  */
1227435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1228435c5e25SMark Brown {
1229435c5e25SMark Brown 	struct snd_soc_card *card;
1230435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1231435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1232435c5e25SMark Brown }
1233435c5e25SMark Brown 
1234435c5e25SMark Brown /* probes a new socdev */
1235435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1236435c5e25SMark Brown {
1237435c5e25SMark Brown 	int ret = 0;
1238435c5e25SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1239435c5e25SMark Brown 	struct snd_soc_card *card = socdev->card;
1240435c5e25SMark Brown 
1241435c5e25SMark Brown 	/* Bodge while we push things out of socdev */
1242435c5e25SMark Brown 	card->socdev = socdev;
1243435c5e25SMark Brown 
1244435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1245435c5e25SMark Brown 	card->dev = &pdev->dev;
1246435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1247435c5e25SMark Brown 	if (ret != 0) {
1248435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1249db2a4165SFrank Mandarino 		return ret;
1250db2a4165SFrank Mandarino 	}
1251db2a4165SFrank Mandarino 
1252435c5e25SMark Brown 	return 0;
1253435c5e25SMark Brown }
1254435c5e25SMark Brown 
1255db2a4165SFrank Mandarino /* removes a socdev */
1256db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1257db2a4165SFrank Mandarino {
1258db2a4165SFrank Mandarino 	int i;
1259db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
126087506549SMark Brown 	struct snd_soc_card *card = socdev->card;
126187689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
1262db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1263db2a4165SFrank Mandarino 
1264914dc182SMike Rapoport 	if (!card->instantiated)
1265914dc182SMike Rapoport 		return 0;
1266914dc182SMike Rapoport 
12676308419aSMark Brown 	run_delayed_work(&card->delayed_work);
1268965ac42cSLiam Girdwood 
1269db2a4165SFrank Mandarino 	if (platform->remove)
1270db2a4165SFrank Mandarino 		platform->remove(pdev);
1271db2a4165SFrank Mandarino 
1272db2a4165SFrank Mandarino 	if (codec_dev->remove)
1273db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1274db2a4165SFrank Mandarino 
127587506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
127687506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1277db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1278bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1279db2a4165SFrank Mandarino 	}
1280db2a4165SFrank Mandarino 
128187506549SMark Brown 	if (card->remove)
128287506549SMark Brown 		card->remove(pdev);
1283db2a4165SFrank Mandarino 
1284c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1285c5af3a2eSMark Brown 
1286db2a4165SFrank Mandarino 	return 0;
1287db2a4165SFrank Mandarino }
1288db2a4165SFrank Mandarino 
1289416356fcSMark Brown static int soc_poweroff(struct device *dev)
129051737470SMark Brown {
1291416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
129251737470SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
129351737470SMark Brown 	struct snd_soc_card *card = socdev->card;
129451737470SMark Brown 
129551737470SMark Brown 	if (!card->instantiated)
1296416356fcSMark Brown 		return 0;
129751737470SMark Brown 
129851737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
129951737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
130051737470SMark Brown 	run_delayed_work(&card->delayed_work);
130151737470SMark Brown 
130251737470SMark Brown 	snd_soc_dapm_shutdown(socdev);
1303416356fcSMark Brown 
1304416356fcSMark Brown 	return 0;
130551737470SMark Brown }
130651737470SMark Brown 
130747145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1308416356fcSMark Brown 	.suspend = soc_suspend,
1309416356fcSMark Brown 	.resume = soc_resume,
1310416356fcSMark Brown 	.poweroff = soc_poweroff,
1311416356fcSMark Brown };
1312416356fcSMark Brown 
1313db2a4165SFrank Mandarino /* ASoC platform driver */
1314db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1315db2a4165SFrank Mandarino 	.driver		= {
1316db2a4165SFrank Mandarino 		.name		= "soc-audio",
13178b45a209SKay Sievers 		.owner		= THIS_MODULE,
1318416356fcSMark Brown 		.pm		= &soc_pm_ops,
1319db2a4165SFrank Mandarino 	},
1320db2a4165SFrank Mandarino 	.probe		= soc_probe,
1321db2a4165SFrank Mandarino 	.remove		= soc_remove,
1322db2a4165SFrank Mandarino };
1323db2a4165SFrank Mandarino 
1324db2a4165SFrank Mandarino /* create a new pcm */
1325db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev,
1326db2a4165SFrank Mandarino 	struct snd_soc_dai_link *dai_link, int num)
1327db2a4165SFrank Mandarino {
132887689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
13296627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
133087689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
13313c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = dai_link->codec_dai;
13323c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1333db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd;
1334db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1335db2a4165SFrank Mandarino 	char new_name[64];
1336db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1337db2a4165SFrank Mandarino 
1338db2a4165SFrank Mandarino 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1339db2a4165SFrank Mandarino 	if (rtd == NULL)
1340db2a4165SFrank Mandarino 		return -ENOMEM;
1341cb666e5bSLiam Girdwood 
1342cb666e5bSLiam Girdwood 	rtd->dai = dai_link;
1343db2a4165SFrank Mandarino 	rtd->socdev = socdev;
13446627a653SMark Brown 	codec_dai->codec = card->codec;
1345db2a4165SFrank Mandarino 
1346db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
134740ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
134840ca1142SMark Brown 		 dai_link->stream_name, codec_dai->name, num);
1349db2a4165SFrank Mandarino 
1350db2a4165SFrank Mandarino 	if (codec_dai->playback.channels_min)
1351db2a4165SFrank Mandarino 		playback = 1;
1352db2a4165SFrank Mandarino 	if (codec_dai->capture.channels_min)
1353db2a4165SFrank Mandarino 		capture = 1;
1354db2a4165SFrank Mandarino 
1355db2a4165SFrank Mandarino 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1356db2a4165SFrank Mandarino 		capture, &pcm);
1357db2a4165SFrank Mandarino 	if (ret < 0) {
13583ff3f64bSMark Brown 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
13593ff3f64bSMark Brown 			codec->name);
1360db2a4165SFrank Mandarino 		kfree(rtd);
1361db2a4165SFrank Mandarino 		return ret;
1362db2a4165SFrank Mandarino 	}
1363db2a4165SFrank Mandarino 
13644ccab3e7SLiam Girdwood 	dai_link->pcm = pcm;
1365db2a4165SFrank Mandarino 	pcm->private_data = rtd;
136687689d56SMark Brown 	soc_pcm_ops.mmap = platform->pcm_ops->mmap;
136787689d56SMark Brown 	soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
136887689d56SMark Brown 	soc_pcm_ops.copy = platform->pcm_ops->copy;
136987689d56SMark Brown 	soc_pcm_ops.silence = platform->pcm_ops->silence;
137087689d56SMark Brown 	soc_pcm_ops.ack = platform->pcm_ops->ack;
137187689d56SMark Brown 	soc_pcm_ops.page = platform->pcm_ops->page;
1372db2a4165SFrank Mandarino 
1373db2a4165SFrank Mandarino 	if (playback)
1374db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1375db2a4165SFrank Mandarino 
1376db2a4165SFrank Mandarino 	if (capture)
1377db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1378db2a4165SFrank Mandarino 
137987689d56SMark Brown 	ret = platform->pcm_new(codec->card, codec_dai, pcm);
1380db2a4165SFrank Mandarino 	if (ret < 0) {
1381db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1382db2a4165SFrank Mandarino 		kfree(rtd);
1383db2a4165SFrank Mandarino 		return ret;
1384db2a4165SFrank Mandarino 	}
1385db2a4165SFrank Mandarino 
138687689d56SMark Brown 	pcm->private_free = platform->pcm_free;
1387db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1388db2a4165SFrank Mandarino 		cpu_dai->name);
1389db2a4165SFrank Mandarino 	return ret;
1390db2a4165SFrank Mandarino }
1391db2a4165SFrank Mandarino 
1392096e49d5SMark Brown /**
1393096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1394096e49d5SMark Brown  *
1395096e49d5SMark Brown  * @codec: CODEC to query.
1396096e49d5SMark Brown  * @reg: Register to query.
1397096e49d5SMark Brown  *
1398096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1399096e49d5SMark Brown  */
1400096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1401096e49d5SMark Brown {
1402096e49d5SMark Brown 	if (codec->volatile_register)
1403096e49d5SMark Brown 		return codec->volatile_register(reg);
1404096e49d5SMark Brown 	else
1405096e49d5SMark Brown 		return 0;
1406096e49d5SMark Brown }
1407096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1408096e49d5SMark Brown 
1409db2a4165SFrank Mandarino /**
1410db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1411db2a4165SFrank Mandarino  * @codec: audio codec
1412db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1413db2a4165SFrank Mandarino  * @num: AC97 codec number
1414db2a4165SFrank Mandarino  *
1415db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1416db2a4165SFrank Mandarino  */
1417db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1418db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1419db2a4165SFrank Mandarino {
1420db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1421db2a4165SFrank Mandarino 
1422db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1423db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1424db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1425db2a4165SFrank Mandarino 		return -ENOMEM;
1426db2a4165SFrank Mandarino 	}
1427db2a4165SFrank Mandarino 
1428db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1429db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1430db2a4165SFrank Mandarino 		kfree(codec->ac97);
1431db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1432db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1433db2a4165SFrank Mandarino 		return -ENOMEM;
1434db2a4165SFrank Mandarino 	}
1435db2a4165SFrank Mandarino 
1436db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1437db2a4165SFrank Mandarino 	codec->ac97->num = num;
14382718625fSMark Brown 	codec->dev = &codec->ac97->dev;
1439db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1440db2a4165SFrank Mandarino 	return 0;
1441db2a4165SFrank Mandarino }
1442db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1443db2a4165SFrank Mandarino 
1444db2a4165SFrank Mandarino /**
1445db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1446db2a4165SFrank Mandarino  * @codec: audio codec
1447db2a4165SFrank Mandarino  *
1448db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1449db2a4165SFrank Mandarino  */
1450db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1451db2a4165SFrank Mandarino {
1452db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1453db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1454db2a4165SFrank Mandarino 	kfree(codec->ac97);
1455db2a4165SFrank Mandarino 	codec->ac97 = NULL;
1456db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1457db2a4165SFrank Mandarino }
1458db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1459db2a4165SFrank Mandarino 
1460db2a4165SFrank Mandarino /**
1461db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1462db2a4165SFrank Mandarino  * @codec: audio codec
1463db2a4165SFrank Mandarino  * @reg: codec register
1464db2a4165SFrank Mandarino  * @mask: register mask
1465db2a4165SFrank Mandarino  * @value: new value
1466db2a4165SFrank Mandarino  *
1467db2a4165SFrank Mandarino  * Writes new register value.
1468db2a4165SFrank Mandarino  *
1469db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1470db2a4165SFrank Mandarino  */
1471db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
147246f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1473db2a4165SFrank Mandarino {
1474db2a4165SFrank Mandarino 	int change;
147546f5822fSDaniel Ribeiro 	unsigned int old, new;
1476db2a4165SFrank Mandarino 
1477db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1478db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1479db2a4165SFrank Mandarino 	change = old != new;
1480db2a4165SFrank Mandarino 	if (change)
1481db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1482db2a4165SFrank Mandarino 
1483db2a4165SFrank Mandarino 	return change;
1484db2a4165SFrank Mandarino }
1485db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1486db2a4165SFrank Mandarino 
1487db2a4165SFrank Mandarino /**
14886c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
14896c508c62SEero Nurkkala  * @codec: audio codec
14906c508c62SEero Nurkkala  * @reg: codec register
14916c508c62SEero Nurkkala  * @mask: register mask
14926c508c62SEero Nurkkala  * @value: new value
14936c508c62SEero Nurkkala  *
14946c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
14956c508c62SEero Nurkkala  *
14966c508c62SEero Nurkkala  * Returns 1 for change else 0.
14976c508c62SEero Nurkkala  */
1498dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
14996c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
15006c508c62SEero Nurkkala 			       unsigned int value)
15016c508c62SEero Nurkkala {
15026c508c62SEero Nurkkala 	int change;
15036c508c62SEero Nurkkala 
15046c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
15056c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
15066c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
15076c508c62SEero Nurkkala 
15086c508c62SEero Nurkkala 	return change;
15096c508c62SEero Nurkkala }
1510dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
15116c508c62SEero Nurkkala 
15126c508c62SEero Nurkkala /**
1513db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1514db2a4165SFrank Mandarino  * @codec: audio codec
1515db2a4165SFrank Mandarino  * @reg: codec register
1516db2a4165SFrank Mandarino  * @mask: register mask
1517db2a4165SFrank Mandarino  * @value: new value
1518db2a4165SFrank Mandarino  *
1519db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1520db2a4165SFrank Mandarino  * different from the old value.
1521db2a4165SFrank Mandarino  *
1522db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1523db2a4165SFrank Mandarino  */
1524db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
152546f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1526db2a4165SFrank Mandarino {
1527db2a4165SFrank Mandarino 	int change;
152846f5822fSDaniel Ribeiro 	unsigned int old, new;
1529db2a4165SFrank Mandarino 
1530db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1531db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1532db2a4165SFrank Mandarino 	change = old != new;
1533db2a4165SFrank Mandarino 
1534db2a4165SFrank Mandarino 	return change;
1535db2a4165SFrank Mandarino }
1536db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1537db2a4165SFrank Mandarino 
1538db2a4165SFrank Mandarino /**
1539db2a4165SFrank Mandarino  * snd_soc_new_pcms - create new sound card and pcms
1540db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1541ac11a2b3SMark Brown  * @idx: ALSA card index
1542ac11a2b3SMark Brown  * @xid: card identification
1543db2a4165SFrank Mandarino  *
1544db2a4165SFrank Mandarino  * Create a new sound card based upon the codec and interface pcms.
1545db2a4165SFrank Mandarino  *
1546db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1547db2a4165SFrank Mandarino  */
1548db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1549db2a4165SFrank Mandarino {
155087506549SMark Brown 	struct snd_soc_card *card = socdev->card;
15516627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
1552bd7dd77cSTakashi Iwai 	int ret, i;
1553db2a4165SFrank Mandarino 
1554db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1555db2a4165SFrank Mandarino 
1556db2a4165SFrank Mandarino 	/* register a sound card */
1557bd7dd77cSTakashi Iwai 	ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1558bd7dd77cSTakashi Iwai 	if (ret < 0) {
1559db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1560db2a4165SFrank Mandarino 			codec->name);
1561db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1562bd7dd77cSTakashi Iwai 		return ret;
1563db2a4165SFrank Mandarino 	}
1564db2a4165SFrank Mandarino 
1565452c5eaaSMark Brown 	codec->socdev = socdev;
1566db2a4165SFrank Mandarino 	codec->card->dev = socdev->dev;
1567db2a4165SFrank Mandarino 	codec->card->private_data = codec;
1568db2a4165SFrank Mandarino 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1569db2a4165SFrank Mandarino 
1570db2a4165SFrank Mandarino 	/* create the pcms */
157187506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
157287506549SMark Brown 		ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1573db2a4165SFrank Mandarino 		if (ret < 0) {
1574db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't create pcm %s\n",
157587506549SMark Brown 				card->dai_link[i].stream_name);
1576db2a4165SFrank Mandarino 			mutex_unlock(&codec->mutex);
1577db2a4165SFrank Mandarino 			return ret;
1578db2a4165SFrank Mandarino 		}
1579f7732053SBarry Song 		if (card->dai_link[i].codec_dai->ac97_control) {
1580f7732053SBarry Song 			snd_ac97_dev_add_pdata(codec->ac97,
1581f7732053SBarry Song 				card->dai_link[i].cpu_dai->ac97_pdata);
1582f7732053SBarry Song 		}
1583db2a4165SFrank Mandarino 	}
1584db2a4165SFrank Mandarino 
1585db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1586db2a4165SFrank Mandarino 	return ret;
1587db2a4165SFrank Mandarino }
1588db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1589db2a4165SFrank Mandarino 
1590db2a4165SFrank Mandarino /**
1591db2a4165SFrank Mandarino  * snd_soc_free_pcms - free sound card and pcms
1592db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1593db2a4165SFrank Mandarino  *
1594db2a4165SFrank Mandarino  * Frees sound card and pcms associated with the socdev.
1595db2a4165SFrank Mandarino  * Also unregister the codec if it is an AC97 device.
1596db2a4165SFrank Mandarino  */
1597db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev)
1598db2a4165SFrank Mandarino {
15996627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
1600a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
16013c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
1602a68660e0SLiam Girdwood 	int i;
1603a68660e0SLiam Girdwood #endif
1604db2a4165SFrank Mandarino 
1605db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
16066627a653SMark Brown 	soc_cleanup_codec_debugfs(codec);
1607db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1608a68660e0SLiam Girdwood 	for (i = 0; i < codec->num_dai; i++) {
1609a68660e0SLiam Girdwood 		codec_dai = &codec->dai[i];
1610d2314e0eSAtsushi Nemoto 		if (codec_dai->ac97_control && codec->ac97 &&
1611d2314e0eSAtsushi Nemoto 		    strcmp(codec->name, "AC97") != 0) {
1612db2a4165SFrank Mandarino 			soc_ac97_dev_unregister(codec);
1613a68660e0SLiam Girdwood 			goto free_card;
1614a68660e0SLiam Girdwood 		}
1615a68660e0SLiam Girdwood 	}
1616a68660e0SLiam Girdwood free_card:
1617db2a4165SFrank Mandarino #endif
1618db2a4165SFrank Mandarino 
1619db2a4165SFrank Mandarino 	if (codec->card)
1620db2a4165SFrank Mandarino 		snd_card_free(codec->card);
1621db2a4165SFrank Mandarino 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1622db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1623db2a4165SFrank Mandarino }
1624db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1625db2a4165SFrank Mandarino 
1626db2a4165SFrank Mandarino /**
1627db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1628db2a4165SFrank Mandarino  * @substream: the pcm substream
1629db2a4165SFrank Mandarino  * @hw: the hardware parameters
1630db2a4165SFrank Mandarino  *
1631db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1632db2a4165SFrank Mandarino  */
1633db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1634db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1635db2a4165SFrank Mandarino {
1636db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1637db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1638db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1639db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1640db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1641db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1642db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1643db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1644db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1645db2a4165SFrank Mandarino 	return 0;
1646db2a4165SFrank Mandarino }
1647db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1648db2a4165SFrank Mandarino 
1649db2a4165SFrank Mandarino /**
1650db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1651db2a4165SFrank Mandarino  * @_template: control template
1652db2a4165SFrank Mandarino  * @data: control private data
1653ac11a2b3SMark Brown  * @long_name: control long name
1654db2a4165SFrank Mandarino  *
1655db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1656db2a4165SFrank Mandarino  *
1657db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1658db2a4165SFrank Mandarino  */
1659db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1660db2a4165SFrank Mandarino 	void *data, char *long_name)
1661db2a4165SFrank Mandarino {
1662db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1663db2a4165SFrank Mandarino 
1664db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1665db2a4165SFrank Mandarino 	if (long_name)
1666db2a4165SFrank Mandarino 		template.name = long_name;
1667db2a4165SFrank Mandarino 	template.index = 0;
1668db2a4165SFrank Mandarino 
1669db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1670db2a4165SFrank Mandarino }
1671db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1672db2a4165SFrank Mandarino 
1673db2a4165SFrank Mandarino /**
16743e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
16753e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
16763e8e1952SIan Molton  * duplicating this code.
16773e8e1952SIan Molton  *
16783e8e1952SIan Molton  * @codec: codec to add controls to
16793e8e1952SIan Molton  * @controls: array of controls to add
16803e8e1952SIan Molton  * @num_controls: number of elements in the array
16813e8e1952SIan Molton  *
16823e8e1952SIan Molton  * Return 0 for success, else error.
16833e8e1952SIan Molton  */
16843e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
16853e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
16863e8e1952SIan Molton {
16873e8e1952SIan Molton 	struct snd_card *card = codec->card;
16883e8e1952SIan Molton 	int err, i;
16893e8e1952SIan Molton 
16903e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
16913e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
16923e8e1952SIan Molton 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
16933e8e1952SIan Molton 		if (err < 0) {
16943e8e1952SIan Molton 			dev_err(codec->dev, "%s: Failed to add %s\n",
16953e8e1952SIan Molton 				codec->name, control->name);
16963e8e1952SIan Molton 			return err;
16973e8e1952SIan Molton 		}
16983e8e1952SIan Molton 	}
16993e8e1952SIan Molton 
17003e8e1952SIan Molton 	return 0;
17013e8e1952SIan Molton }
17023e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
17033e8e1952SIan Molton 
17043e8e1952SIan Molton /**
1705db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1706db2a4165SFrank Mandarino  * @kcontrol: mixer control
1707db2a4165SFrank Mandarino  * @uinfo: control element information
1708db2a4165SFrank Mandarino  *
1709db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
1710db2a4165SFrank Mandarino  * mixer control.
1711db2a4165SFrank Mandarino  *
1712db2a4165SFrank Mandarino  * Returns 0 for success.
1713db2a4165SFrank Mandarino  */
1714db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1715db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1716db2a4165SFrank Mandarino {
1717db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1718db2a4165SFrank Mandarino 
1719db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1720db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1721f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1722db2a4165SFrank Mandarino 
1723f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1724f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1725db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1726db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1727db2a4165SFrank Mandarino 	return 0;
1728db2a4165SFrank Mandarino }
1729db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1730db2a4165SFrank Mandarino 
1731db2a4165SFrank Mandarino /**
1732db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
1733db2a4165SFrank Mandarino  * @kcontrol: mixer control
1734ac11a2b3SMark Brown  * @ucontrol: control element information
1735db2a4165SFrank Mandarino  *
1736db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
1737db2a4165SFrank Mandarino  *
1738db2a4165SFrank Mandarino  * Returns 0 for success.
1739db2a4165SFrank Mandarino  */
1740db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1741db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1742db2a4165SFrank Mandarino {
1743db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1744db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
174546f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
1746db2a4165SFrank Mandarino 
1747f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1748db2a4165SFrank Mandarino 		;
1749db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
17503ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
17513ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
1752db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
1753db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
1754db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
1755db2a4165SFrank Mandarino 
1756db2a4165SFrank Mandarino 	return 0;
1757db2a4165SFrank Mandarino }
1758db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1759db2a4165SFrank Mandarino 
1760db2a4165SFrank Mandarino /**
1761db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
1762db2a4165SFrank Mandarino  * @kcontrol: mixer control
1763ac11a2b3SMark Brown  * @ucontrol: control element information
1764db2a4165SFrank Mandarino  *
1765db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
1766db2a4165SFrank Mandarino  *
1767db2a4165SFrank Mandarino  * Returns 0 for success.
1768db2a4165SFrank Mandarino  */
1769db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1770db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1771db2a4165SFrank Mandarino {
1772db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1773db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
177446f5822fSDaniel Ribeiro 	unsigned int val;
177546f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
1776db2a4165SFrank Mandarino 
1777f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1778db2a4165SFrank Mandarino 		;
1779f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1780db2a4165SFrank Mandarino 		return -EINVAL;
1781db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1782db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
1783db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
1784f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1785db2a4165SFrank Mandarino 			return -EINVAL;
1786db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1787db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
1788db2a4165SFrank Mandarino 	}
1789db2a4165SFrank Mandarino 
17906c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1791db2a4165SFrank Mandarino }
1792db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1793db2a4165SFrank Mandarino 
1794db2a4165SFrank Mandarino /**
17952e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
17962e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
17972e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
17982e72f8e3SPeter Ujfalusi  *
17992e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
18002e72f8e3SPeter Ujfalusi  *
18012e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
18022e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
18032e72f8e3SPeter Ujfalusi  *
18042e72f8e3SPeter Ujfalusi  * Returns 0 for success.
18052e72f8e3SPeter Ujfalusi  */
18062e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
18072e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
18082e72f8e3SPeter Ujfalusi {
18092e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
181074155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
181146f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
18122e72f8e3SPeter Ujfalusi 
18132e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
18142e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
18152e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
18162e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
18172e72f8e3SPeter Ujfalusi 			break;
18182e72f8e3SPeter Ujfalusi 	}
18192e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
18202e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
18212e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
18222e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
18232e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
18242e72f8e3SPeter Ujfalusi 				break;
18252e72f8e3SPeter Ujfalusi 		}
18262e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
18272e72f8e3SPeter Ujfalusi 	}
18282e72f8e3SPeter Ujfalusi 
18292e72f8e3SPeter Ujfalusi 	return 0;
18302e72f8e3SPeter Ujfalusi }
18312e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
18322e72f8e3SPeter Ujfalusi 
18332e72f8e3SPeter Ujfalusi /**
18342e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
18352e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
18362e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
18372e72f8e3SPeter Ujfalusi  *
18382e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
18392e72f8e3SPeter Ujfalusi  *
18402e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
18412e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
18422e72f8e3SPeter Ujfalusi  *
18432e72f8e3SPeter Ujfalusi  * Returns 0 for success.
18442e72f8e3SPeter Ujfalusi  */
18452e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
18462e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
18472e72f8e3SPeter Ujfalusi {
18482e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
184974155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
185046f5822fSDaniel Ribeiro 	unsigned int val;
185146f5822fSDaniel Ribeiro 	unsigned int mask;
18522e72f8e3SPeter Ujfalusi 
18532e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
18542e72f8e3SPeter Ujfalusi 		return -EINVAL;
18552e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
18562e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
18572e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
18582e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
18592e72f8e3SPeter Ujfalusi 			return -EINVAL;
18602e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
18612e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
18622e72f8e3SPeter Ujfalusi 	}
18632e72f8e3SPeter Ujfalusi 
18646c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
18652e72f8e3SPeter Ujfalusi }
18662e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
18672e72f8e3SPeter Ujfalusi 
18682e72f8e3SPeter Ujfalusi /**
1869db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1870db2a4165SFrank Mandarino  * @kcontrol: mixer control
1871db2a4165SFrank Mandarino  * @uinfo: control element information
1872db2a4165SFrank Mandarino  *
1873db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
1874db2a4165SFrank Mandarino  * single mixer.
1875db2a4165SFrank Mandarino  *
1876db2a4165SFrank Mandarino  * Returns 0 for success.
1877db2a4165SFrank Mandarino  */
1878db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1879db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1880db2a4165SFrank Mandarino {
1881db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1882db2a4165SFrank Mandarino 
1883db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1884db2a4165SFrank Mandarino 	uinfo->count = 1;
1885f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1886db2a4165SFrank Mandarino 
1887f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1888f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1889db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1890db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1891db2a4165SFrank Mandarino 	return 0;
1892db2a4165SFrank Mandarino }
1893db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1894db2a4165SFrank Mandarino 
1895db2a4165SFrank Mandarino /**
1896db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
1897db2a4165SFrank Mandarino  * @kcontrol: mixer control
1898db2a4165SFrank Mandarino  * @uinfo: control element information
1899db2a4165SFrank Mandarino  *
1900db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
1901db2a4165SFrank Mandarino  *
1902db2a4165SFrank Mandarino  * Returns 0 for success.
1903db2a4165SFrank Mandarino  */
1904db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1905db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1906db2a4165SFrank Mandarino {
1907a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
1908db2a4165SFrank Mandarino 
1909fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1910a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1911a7a4ac86SPhilipp Zabel 	else
1912a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1913a7a4ac86SPhilipp Zabel 
1914db2a4165SFrank Mandarino 	uinfo->count = 1;
1915db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1916a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1917db2a4165SFrank Mandarino 	return 0;
1918db2a4165SFrank Mandarino }
1919db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1920db2a4165SFrank Mandarino 
1921db2a4165SFrank Mandarino /**
1922db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
1923db2a4165SFrank Mandarino  * @kcontrol: mixer control
1924db2a4165SFrank Mandarino  * @uinfo: control element information
1925db2a4165SFrank Mandarino  *
1926db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
1927db2a4165SFrank Mandarino  *
1928db2a4165SFrank Mandarino  * Returns 0 for success.
1929db2a4165SFrank Mandarino  */
1930db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1931db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1932db2a4165SFrank Mandarino {
19334eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19344eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
19354eaa9819SJon Smirl 	int max = mc->max;
1936762b8df7SMark Brown 	unsigned int shift = mc->shift;
1937815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
1938db2a4165SFrank Mandarino 
1939fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1940a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1941a7a4ac86SPhilipp Zabel 	else
1942a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1943a7a4ac86SPhilipp Zabel 
1944db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
1945db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1946a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1947db2a4165SFrank Mandarino 	return 0;
1948db2a4165SFrank Mandarino }
1949db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1950db2a4165SFrank Mandarino 
1951db2a4165SFrank Mandarino /**
1952db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
1953db2a4165SFrank Mandarino  * @kcontrol: mixer control
1954ac11a2b3SMark Brown  * @ucontrol: control element information
1955db2a4165SFrank Mandarino  *
1956db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
1957db2a4165SFrank Mandarino  *
1958db2a4165SFrank Mandarino  * Returns 0 for success.
1959db2a4165SFrank Mandarino  */
1960db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1961db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1962db2a4165SFrank Mandarino {
19634eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19644eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1965db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1966815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1967815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1968815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
19694eaa9819SJon Smirl 	int max = mc->max;
1970815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1971815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1972db2a4165SFrank Mandarino 
1973db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1974db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1975db2a4165SFrank Mandarino 	if (shift != rshift)
1976db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1977db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
1978db2a4165SFrank Mandarino 	if (invert) {
1979db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1980a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
1981db2a4165SFrank Mandarino 		if (shift != rshift)
1982db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
1983a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
1984db2a4165SFrank Mandarino 	}
1985db2a4165SFrank Mandarino 
1986db2a4165SFrank Mandarino 	return 0;
1987db2a4165SFrank Mandarino }
1988db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1989db2a4165SFrank Mandarino 
1990db2a4165SFrank Mandarino /**
1991db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
1992db2a4165SFrank Mandarino  * @kcontrol: mixer control
1993ac11a2b3SMark Brown  * @ucontrol: control element information
1994db2a4165SFrank Mandarino  *
1995db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
1996db2a4165SFrank Mandarino  *
1997db2a4165SFrank Mandarino  * Returns 0 for success.
1998db2a4165SFrank Mandarino  */
1999db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2000db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2001db2a4165SFrank Mandarino {
20024eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20034eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2004db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2005815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2006815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2007815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
20084eaa9819SJon Smirl 	int max = mc->max;
2009815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2010815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
201146f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2012db2a4165SFrank Mandarino 
2013db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2014db2a4165SFrank Mandarino 	if (invert)
2015a7a4ac86SPhilipp Zabel 		val = max - val;
2016db2a4165SFrank Mandarino 	val_mask = mask << shift;
2017db2a4165SFrank Mandarino 	val = val << shift;
2018db2a4165SFrank Mandarino 	if (shift != rshift) {
2019db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2020db2a4165SFrank Mandarino 		if (invert)
2021a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2022db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2023db2a4165SFrank Mandarino 		val |= val2 << rshift;
2024db2a4165SFrank Mandarino 	}
20256c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2026db2a4165SFrank Mandarino }
2027db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2028db2a4165SFrank Mandarino 
2029db2a4165SFrank Mandarino /**
2030db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2031db2a4165SFrank Mandarino  * @kcontrol: mixer control
2032db2a4165SFrank Mandarino  * @uinfo: control element information
2033db2a4165SFrank Mandarino  *
2034db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2035db2a4165SFrank Mandarino  * spans 2 codec registers.
2036db2a4165SFrank Mandarino  *
2037db2a4165SFrank Mandarino  * Returns 0 for success.
2038db2a4165SFrank Mandarino  */
2039db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2040db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2041db2a4165SFrank Mandarino {
20424eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20434eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
20444eaa9819SJon Smirl 	int max = mc->max;
2045db2a4165SFrank Mandarino 
2046fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2047a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2048a7a4ac86SPhilipp Zabel 	else
2049a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2050a7a4ac86SPhilipp Zabel 
2051db2a4165SFrank Mandarino 	uinfo->count = 2;
2052db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2053a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2054db2a4165SFrank Mandarino 	return 0;
2055db2a4165SFrank Mandarino }
2056db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2057db2a4165SFrank Mandarino 
2058db2a4165SFrank Mandarino /**
2059db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2060db2a4165SFrank Mandarino  * @kcontrol: mixer control
2061ac11a2b3SMark Brown  * @ucontrol: control element information
2062db2a4165SFrank Mandarino  *
2063db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2064db2a4165SFrank Mandarino  *
2065db2a4165SFrank Mandarino  * Returns 0 for success.
2066db2a4165SFrank Mandarino  */
2067db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2068db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2069db2a4165SFrank Mandarino {
20704eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20714eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2072db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2073815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2074815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2075815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
20764eaa9819SJon Smirl 	int max = mc->max;
2077815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2078815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2079db2a4165SFrank Mandarino 
2080db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2081db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2082db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2083db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2084db2a4165SFrank Mandarino 	if (invert) {
2085db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2086a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2087db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2088a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2089db2a4165SFrank Mandarino 	}
2090db2a4165SFrank Mandarino 
2091db2a4165SFrank Mandarino 	return 0;
2092db2a4165SFrank Mandarino }
2093db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2094db2a4165SFrank Mandarino 
2095db2a4165SFrank Mandarino /**
2096db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2097db2a4165SFrank Mandarino  * @kcontrol: mixer control
2098ac11a2b3SMark Brown  * @ucontrol: control element information
2099db2a4165SFrank Mandarino  *
2100db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2101db2a4165SFrank Mandarino  *
2102db2a4165SFrank Mandarino  * Returns 0 for success.
2103db2a4165SFrank Mandarino  */
2104db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2105db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2106db2a4165SFrank Mandarino {
21074eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21084eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2109db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2110815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2111815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2112815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
21134eaa9819SJon Smirl 	int max = mc->max;
2114815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2115815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2116db2a4165SFrank Mandarino 	int err;
211746f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2118db2a4165SFrank Mandarino 
2119db2a4165SFrank Mandarino 	val_mask = mask << shift;
2120db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2121db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2122db2a4165SFrank Mandarino 
2123db2a4165SFrank Mandarino 	if (invert) {
2124a7a4ac86SPhilipp Zabel 		val = max - val;
2125a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2126db2a4165SFrank Mandarino 	}
2127db2a4165SFrank Mandarino 
2128db2a4165SFrank Mandarino 	val = val << shift;
2129db2a4165SFrank Mandarino 	val2 = val2 << shift;
2130db2a4165SFrank Mandarino 
21316c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
21323ff3f64bSMark Brown 	if (err < 0)
2133db2a4165SFrank Mandarino 		return err;
2134db2a4165SFrank Mandarino 
21356c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2136db2a4165SFrank Mandarino 	return err;
2137db2a4165SFrank Mandarino }
2138db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2139db2a4165SFrank Mandarino 
2140e13ac2e9SMark Brown /**
2141e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2142e13ac2e9SMark Brown  * @kcontrol: mixer control
2143e13ac2e9SMark Brown  * @uinfo: control element information
2144e13ac2e9SMark Brown  *
2145e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2146e13ac2e9SMark Brown  *
2147e13ac2e9SMark Brown  * Returns 0 for success.
2148e13ac2e9SMark Brown  */
2149e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2150e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2151e13ac2e9SMark Brown {
21524eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21534eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
21544eaa9819SJon Smirl 	int max = mc->max;
21554eaa9819SJon Smirl 	int min = mc->min;
2156e13ac2e9SMark Brown 
2157e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2158e13ac2e9SMark Brown 	uinfo->count = 2;
2159e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2160e13ac2e9SMark Brown 	uinfo->value.integer.max = max-min;
2161e13ac2e9SMark Brown 	return 0;
2162e13ac2e9SMark Brown }
2163e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2164e13ac2e9SMark Brown 
2165e13ac2e9SMark Brown /**
2166e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2167e13ac2e9SMark Brown  * @kcontrol: mixer control
2168ac11a2b3SMark Brown  * @ucontrol: control element information
2169e13ac2e9SMark Brown  *
2170e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2171e13ac2e9SMark Brown  *
2172e13ac2e9SMark Brown  * Returns 0 for success.
2173e13ac2e9SMark Brown  */
2174e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2175e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2176e13ac2e9SMark Brown {
21774eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21784eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2179e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2180815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
21814eaa9819SJon Smirl 	int min = mc->min;
2182e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2183e13ac2e9SMark Brown 
2184e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2185e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2186e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2187e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2188e13ac2e9SMark Brown 	return 0;
2189e13ac2e9SMark Brown }
2190e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2191e13ac2e9SMark Brown 
2192e13ac2e9SMark Brown /**
2193e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2194e13ac2e9SMark Brown  * @kcontrol: mixer control
2195ac11a2b3SMark Brown  * @ucontrol: control element information
2196e13ac2e9SMark Brown  *
2197e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2198e13ac2e9SMark Brown  *
2199e13ac2e9SMark Brown  * Returns 0 for success.
2200e13ac2e9SMark Brown  */
2201e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2202e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2203e13ac2e9SMark Brown {
22044eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22054eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2206e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2207815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
22084eaa9819SJon Smirl 	int min = mc->min;
220946f5822fSDaniel Ribeiro 	unsigned int val;
2210e13ac2e9SMark Brown 
2211e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2212e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2213e13ac2e9SMark Brown 
22146c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2215e13ac2e9SMark Brown }
2216e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2217e13ac2e9SMark Brown 
22188c6529dbSLiam Girdwood /**
22198c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
22208c6529dbSLiam Girdwood  * @dai: DAI
22218c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
22228c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
22238c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
22248c6529dbSLiam Girdwood  *
22258c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
22268c6529dbSLiam Girdwood  */
22278c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
22288c6529dbSLiam Girdwood 	unsigned int freq, int dir)
22298c6529dbSLiam Girdwood {
22303f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_sysclk)
22316335d055SEric Miao 		return dai->ops->set_sysclk(dai, clk_id, freq, dir);
22328c6529dbSLiam Girdwood 	else
22338c6529dbSLiam Girdwood 		return -EINVAL;
22348c6529dbSLiam Girdwood }
22358c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
22368c6529dbSLiam Girdwood 
22378c6529dbSLiam Girdwood /**
22388c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
22398c6529dbSLiam Girdwood  * @dai: DAI
2240ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
22418c6529dbSLiam Girdwood  * @div: new clock divisor.
22428c6529dbSLiam Girdwood  *
22438c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
22448c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
22458c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
22468c6529dbSLiam Girdwood  */
22478c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
22488c6529dbSLiam Girdwood 	int div_id, int div)
22498c6529dbSLiam Girdwood {
22503f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_clkdiv)
22516335d055SEric Miao 		return dai->ops->set_clkdiv(dai, div_id, div);
22528c6529dbSLiam Girdwood 	else
22538c6529dbSLiam Girdwood 		return -EINVAL;
22548c6529dbSLiam Girdwood }
22558c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
22568c6529dbSLiam Girdwood 
22578c6529dbSLiam Girdwood /**
22588c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
22598c6529dbSLiam Girdwood  * @dai: DAI
22608c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
226185488037SMark Brown  * @source: DAI specific source for the PLL
22628c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
22638c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
22648c6529dbSLiam Girdwood  *
22658c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
22668c6529dbSLiam Girdwood  */
226785488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
226885488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
22698c6529dbSLiam Girdwood {
22703f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_pll)
227185488037SMark Brown 		return dai->ops->set_pll(dai, pll_id, source,
227285488037SMark Brown 					 freq_in, freq_out);
22738c6529dbSLiam Girdwood 	else
22748c6529dbSLiam Girdwood 		return -EINVAL;
22758c6529dbSLiam Girdwood }
22768c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
22778c6529dbSLiam Girdwood 
22788c6529dbSLiam Girdwood /**
22798c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
22808c6529dbSLiam Girdwood  * @dai: DAI
22818c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
22828c6529dbSLiam Girdwood  *
22838c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
22848c6529dbSLiam Girdwood  */
22858c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
22868c6529dbSLiam Girdwood {
22873f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_fmt)
22886335d055SEric Miao 		return dai->ops->set_fmt(dai, fmt);
22898c6529dbSLiam Girdwood 	else
22908c6529dbSLiam Girdwood 		return -EINVAL;
22918c6529dbSLiam Girdwood }
22928c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
22938c6529dbSLiam Girdwood 
22948c6529dbSLiam Girdwood /**
22958c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
22968c6529dbSLiam Girdwood  * @dai: DAI
2297a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2298a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
22998c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2300a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
23018c6529dbSLiam Girdwood  *
23028c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
23038c6529dbSLiam Girdwood  * specific.
23048c6529dbSLiam Girdwood  */
23058c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2306a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
23078c6529dbSLiam Girdwood {
23083f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tdm_slot)
2309a5479e38SDaniel Ribeiro 		return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2310a5479e38SDaniel Ribeiro 				slots, slot_width);
23118c6529dbSLiam Girdwood 	else
23128c6529dbSLiam Girdwood 		return -EINVAL;
23138c6529dbSLiam Girdwood }
23148c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
23158c6529dbSLiam Girdwood 
23168c6529dbSLiam Girdwood /**
2317472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2318472df3cbSBarry Song  * @dai: DAI
2319472df3cbSBarry Song  * @tx_num: how many TX channels
2320472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2321472df3cbSBarry Song  *           0~num-1 uses
2322472df3cbSBarry Song  * @rx_num: how many RX channels
2323472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2324472df3cbSBarry Song  *           0~num-1 uses
2325472df3cbSBarry Song  *
2326472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2327472df3cbSBarry Song  */
2328472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2329472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2330472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2331472df3cbSBarry Song {
2332472df3cbSBarry Song 	if (dai->ops && dai->ops->set_channel_map)
2333472df3cbSBarry Song 		return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2334472df3cbSBarry Song 			rx_num, rx_slot);
2335472df3cbSBarry Song 	else
2336472df3cbSBarry Song 		return -EINVAL;
2337472df3cbSBarry Song }
2338472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2339472df3cbSBarry Song 
2340472df3cbSBarry Song /**
23418c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
23428c6529dbSLiam Girdwood  * @dai: DAI
23438c6529dbSLiam Girdwood  * @tristate: tristate enable
23448c6529dbSLiam Girdwood  *
23458c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
23468c6529dbSLiam Girdwood  */
23478c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
23488c6529dbSLiam Girdwood {
23493f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tristate)
23506335d055SEric Miao 		return dai->ops->set_tristate(dai, tristate);
23518c6529dbSLiam Girdwood 	else
23528c6529dbSLiam Girdwood 		return -EINVAL;
23538c6529dbSLiam Girdwood }
23548c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
23558c6529dbSLiam Girdwood 
23568c6529dbSLiam Girdwood /**
23578c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
23588c6529dbSLiam Girdwood  * @dai: DAI
23598c6529dbSLiam Girdwood  * @mute: mute enable
23608c6529dbSLiam Girdwood  *
23618c6529dbSLiam Girdwood  * Mutes the DAI DAC.
23628c6529dbSLiam Girdwood  */
23638c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
23648c6529dbSLiam Girdwood {
23653f1a4d82SMark Brown 	if (dai->ops && dai->ops->digital_mute)
23666335d055SEric Miao 		return dai->ops->digital_mute(dai, mute);
23678c6529dbSLiam Girdwood 	else
23688c6529dbSLiam Girdwood 		return -EINVAL;
23698c6529dbSLiam Girdwood }
23708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
23718c6529dbSLiam Girdwood 
2372c5af3a2eSMark Brown /**
2373c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2374c5af3a2eSMark Brown  *
2375ac11a2b3SMark Brown  * @card: Card to register
2376c5af3a2eSMark Brown  *
2377c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2378c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2379c5af3a2eSMark Brown  * registration APIs.
2380c5af3a2eSMark Brown  */
2381c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
2382c5af3a2eSMark Brown {
2383c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2384c5af3a2eSMark Brown 		return -EINVAL;
2385c5af3a2eSMark Brown 
2386c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2387c5af3a2eSMark Brown 	card->instantiated = 0;
2388c5af3a2eSMark Brown 
2389c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2390c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2391435c5e25SMark Brown 	snd_soc_instantiate_cards();
2392c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2393c5af3a2eSMark Brown 
2394c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2395c5af3a2eSMark Brown 
2396c5af3a2eSMark Brown 	return 0;
2397c5af3a2eSMark Brown }
2398c5af3a2eSMark Brown 
2399c5af3a2eSMark Brown /**
2400c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2401c5af3a2eSMark Brown  *
2402ac11a2b3SMark Brown  * @card: Card to unregister
2403c5af3a2eSMark Brown  *
2404c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2405c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2406c5af3a2eSMark Brown  * registration APIs.
2407c5af3a2eSMark Brown  */
2408c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
2409c5af3a2eSMark Brown {
2410c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2411c5af3a2eSMark Brown 	list_del(&card->list);
2412c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2413c5af3a2eSMark Brown 
2414c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2415c5af3a2eSMark Brown 
2416c5af3a2eSMark Brown 	return 0;
2417c5af3a2eSMark Brown }
2418c5af3a2eSMark Brown 
24199115171aSMark Brown /**
24209115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
24219115171aSMark Brown  *
2422ac11a2b3SMark Brown  * @dai: DAI to register
24239115171aSMark Brown  */
24249115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai)
24259115171aSMark Brown {
24269115171aSMark Brown 	if (!dai->name)
24279115171aSMark Brown 		return -EINVAL;
24289115171aSMark Brown 
24299115171aSMark Brown 	/* The device should become mandatory over time */
24309115171aSMark Brown 	if (!dai->dev)
24319115171aSMark Brown 		printk(KERN_WARNING "No device for DAI %s\n", dai->name);
24329115171aSMark Brown 
24336335d055SEric Miao 	if (!dai->ops)
24346335d055SEric Miao 		dai->ops = &null_dai_ops;
24356335d055SEric Miao 
24369115171aSMark Brown 	INIT_LIST_HEAD(&dai->list);
24379115171aSMark Brown 
24389115171aSMark Brown 	mutex_lock(&client_mutex);
24399115171aSMark Brown 	list_add(&dai->list, &dai_list);
2440435c5e25SMark Brown 	snd_soc_instantiate_cards();
24419115171aSMark Brown 	mutex_unlock(&client_mutex);
24429115171aSMark Brown 
24439115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
24449115171aSMark Brown 
24459115171aSMark Brown 	return 0;
24469115171aSMark Brown }
24479115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
24489115171aSMark Brown 
24499115171aSMark Brown /**
24509115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
24519115171aSMark Brown  *
2452ac11a2b3SMark Brown  * @dai: DAI to unregister
24539115171aSMark Brown  */
24549115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai)
24559115171aSMark Brown {
24569115171aSMark Brown 	mutex_lock(&client_mutex);
24579115171aSMark Brown 	list_del(&dai->list);
24589115171aSMark Brown 	mutex_unlock(&client_mutex);
24599115171aSMark Brown 
24609115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
24619115171aSMark Brown }
24629115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
24639115171aSMark Brown 
24649115171aSMark Brown /**
24659115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
24669115171aSMark Brown  *
2467ac11a2b3SMark Brown  * @dai: Array of DAIs to register
2468ac11a2b3SMark Brown  * @count: Number of DAIs
24699115171aSMark Brown  */
24709115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
24719115171aSMark Brown {
24729115171aSMark Brown 	int i, ret;
24739115171aSMark Brown 
24749115171aSMark Brown 	for (i = 0; i < count; i++) {
24759115171aSMark Brown 		ret = snd_soc_register_dai(&dai[i]);
24769115171aSMark Brown 		if (ret != 0)
24779115171aSMark Brown 			goto err;
24789115171aSMark Brown 	}
24799115171aSMark Brown 
24809115171aSMark Brown 	return 0;
24819115171aSMark Brown 
24829115171aSMark Brown err:
24839115171aSMark Brown 	for (i--; i >= 0; i--)
24849115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
24859115171aSMark Brown 
24869115171aSMark Brown 	return ret;
24879115171aSMark Brown }
24889115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
24899115171aSMark Brown 
24909115171aSMark Brown /**
24919115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
24929115171aSMark Brown  *
2493ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
2494ac11a2b3SMark Brown  * @count: Number of DAIs
24959115171aSMark Brown  */
24969115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
24979115171aSMark Brown {
24989115171aSMark Brown 	int i;
24999115171aSMark Brown 
25009115171aSMark Brown 	for (i = 0; i < count; i++)
25019115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
25029115171aSMark Brown }
25039115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
25049115171aSMark Brown 
250512a48a8cSMark Brown /**
250612a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
250712a48a8cSMark Brown  *
2508ac11a2b3SMark Brown  * @platform: platform to register
250912a48a8cSMark Brown  */
251012a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform)
251112a48a8cSMark Brown {
251212a48a8cSMark Brown 	if (!platform->name)
251312a48a8cSMark Brown 		return -EINVAL;
251412a48a8cSMark Brown 
251512a48a8cSMark Brown 	INIT_LIST_HEAD(&platform->list);
251612a48a8cSMark Brown 
251712a48a8cSMark Brown 	mutex_lock(&client_mutex);
251812a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
2519435c5e25SMark Brown 	snd_soc_instantiate_cards();
252012a48a8cSMark Brown 	mutex_unlock(&client_mutex);
252112a48a8cSMark Brown 
252212a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
252312a48a8cSMark Brown 
252412a48a8cSMark Brown 	return 0;
252512a48a8cSMark Brown }
252612a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
252712a48a8cSMark Brown 
252812a48a8cSMark Brown /**
252912a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
253012a48a8cSMark Brown  *
2531ac11a2b3SMark Brown  * @platform: platform to unregister
253212a48a8cSMark Brown  */
253312a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform)
253412a48a8cSMark Brown {
253512a48a8cSMark Brown 	mutex_lock(&client_mutex);
253612a48a8cSMark Brown 	list_del(&platform->list);
253712a48a8cSMark Brown 	mutex_unlock(&client_mutex);
253812a48a8cSMark Brown 
253912a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
254012a48a8cSMark Brown }
254112a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
254212a48a8cSMark Brown 
2543151ab22cSMark Brown static u64 codec_format_map[] = {
2544151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2545151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2546151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2547151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2548151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2549151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2550151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2551151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2552151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2553151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2554151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2555151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2556151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2557151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2558151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2559151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2560151ab22cSMark Brown };
2561151ab22cSMark Brown 
2562151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2563151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2564151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2565151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2566151ab22cSMark Brown  */
2567151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2568151ab22cSMark Brown {
2569151ab22cSMark Brown 	int i;
2570151ab22cSMark Brown 
2571151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2572151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2573151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
2574151ab22cSMark Brown }
2575151ab22cSMark Brown 
25760d0cf00aSMark Brown /**
25770d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
25780d0cf00aSMark Brown  *
2579ac11a2b3SMark Brown  * @codec: codec to register
25800d0cf00aSMark Brown  */
25810d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec)
25820d0cf00aSMark Brown {
2583151ab22cSMark Brown 	int i;
2584151ab22cSMark Brown 
25850d0cf00aSMark Brown 	if (!codec->name)
25860d0cf00aSMark Brown 		return -EINVAL;
25870d0cf00aSMark Brown 
25880d0cf00aSMark Brown 	/* The device should become mandatory over time */
25890d0cf00aSMark Brown 	if (!codec->dev)
25900d0cf00aSMark Brown 		printk(KERN_WARNING "No device for codec %s\n", codec->name);
25910d0cf00aSMark Brown 
25920d0cf00aSMark Brown 	INIT_LIST_HEAD(&codec->list);
25930d0cf00aSMark Brown 
2594151ab22cSMark Brown 	for (i = 0; i < codec->num_dai; i++) {
2595151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].playback);
2596151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].capture);
2597151ab22cSMark Brown 	}
2598151ab22cSMark Brown 
25990d0cf00aSMark Brown 	mutex_lock(&client_mutex);
26000d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
26010d0cf00aSMark Brown 	snd_soc_instantiate_cards();
26020d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
26030d0cf00aSMark Brown 
26040d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
26050d0cf00aSMark Brown 
26060d0cf00aSMark Brown 	return 0;
26070d0cf00aSMark Brown }
26080d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
26090d0cf00aSMark Brown 
26100d0cf00aSMark Brown /**
26110d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
26120d0cf00aSMark Brown  *
2613ac11a2b3SMark Brown  * @codec: codec to unregister
26140d0cf00aSMark Brown  */
26150d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec)
26160d0cf00aSMark Brown {
26170d0cf00aSMark Brown 	mutex_lock(&client_mutex);
26180d0cf00aSMark Brown 	list_del(&codec->list);
26190d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
26200d0cf00aSMark Brown 
26210d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
26220d0cf00aSMark Brown }
26230d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
26240d0cf00aSMark Brown 
2625c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
2626db2a4165SFrank Mandarino {
2627384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2628384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
2629384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
2630384c89e2SMark Brown 		printk(KERN_WARNING
2631384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
2632384c89e2SMark Brown 		debugfs_root = NULL;
2633384c89e2SMark Brown 	}
2634384c89e2SMark Brown #endif
2635384c89e2SMark Brown 
2636db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
2637db2a4165SFrank Mandarino }
2638db2a4165SFrank Mandarino 
26397d8c16a6SMark Brown static void __exit snd_soc_exit(void)
2640db2a4165SFrank Mandarino {
2641384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2642384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
2643384c89e2SMark Brown #endif
2644db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
2645db2a4165SFrank Mandarino }
2646db2a4165SFrank Mandarino 
2647db2a4165SFrank Mandarino module_init(snd_soc_init);
2648db2a4165SFrank Mandarino module_exit(snd_soc_exit);
2649db2a4165SFrank Mandarino 
2650db2a4165SFrank Mandarino /* Module information */
2651d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2652db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
2653db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
26548b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
2655