xref: /openbmc/linux/sound/soc/soc-core.c (revision d273ebe77a780d3aba1f5b86615af3b0e87b76bf)
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 
457db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
458cb666e5bSLiam Girdwood 		cpu_dai->playback.active = codec_dai->playback.active = 1;
459db2a4165SFrank Mandarino 	else
460cb666e5bSLiam Girdwood 		cpu_dai->capture.active = codec_dai->capture.active = 1;
461cb666e5bSLiam Girdwood 	cpu_dai->active = codec_dai->active = 1;
462cb666e5bSLiam Girdwood 	cpu_dai->runtime = runtime;
4636627a653SMark Brown 	card->codec->active++;
464db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
465db2a4165SFrank Mandarino 	return 0;
466db2a4165SFrank Mandarino 
467cb666e5bSLiam Girdwood machine_err:
468db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
469db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
470db2a4165SFrank Mandarino 
471cb666e5bSLiam Girdwood codec_dai_err:
472db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
473db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
474db2a4165SFrank Mandarino 
475db2a4165SFrank Mandarino platform_err:
4766335d055SEric Miao 	if (cpu_dai->ops->shutdown)
4776335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
478db2a4165SFrank Mandarino out:
479db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
480db2a4165SFrank Mandarino 	return ret;
481db2a4165SFrank Mandarino }
482db2a4165SFrank Mandarino 
483db2a4165SFrank Mandarino /*
4843a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
485db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
486db2a4165SFrank Mandarino  * due to DAPM power cycling.
487db2a4165SFrank Mandarino  */
4884484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
489db2a4165SFrank Mandarino {
4906308419aSMark Brown 	struct snd_soc_card *card = container_of(work, struct snd_soc_card,
4916308419aSMark Brown 						 delayed_work.work);
4926627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
4933c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
494db2a4165SFrank Mandarino 	int i;
495db2a4165SFrank Mandarino 
496db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
497db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
498db2a4165SFrank Mandarino 		codec_dai = &codec->dai[i];
499db2a4165SFrank Mandarino 
500f24368c2SMark Brown 		pr_debug("pop wq checking: %s status: %s waiting: %s\n",
501db2a4165SFrank Mandarino 			 codec_dai->playback.stream_name,
502db2a4165SFrank Mandarino 			 codec_dai->playback.active ? "active" : "inactive",
503db2a4165SFrank Mandarino 			 codec_dai->pop_wait ? "yes" : "no");
504db2a4165SFrank Mandarino 
505db2a4165SFrank Mandarino 		/* are we waiting on this codec DAI stream */
506db2a4165SFrank Mandarino 		if (codec_dai->pop_wait == 1) {
507db2a4165SFrank Mandarino 			codec_dai->pop_wait = 0;
5080b4d221bSLiam Girdwood 			snd_soc_dapm_stream_event(codec,
5090b4d221bSLiam Girdwood 				codec_dai->playback.stream_name,
510db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_STOP);
511db2a4165SFrank Mandarino 		}
512db2a4165SFrank Mandarino 	}
513db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
514db2a4165SFrank Mandarino }
515db2a4165SFrank Mandarino 
516db2a4165SFrank Mandarino /*
517db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
518db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
519db2a4165SFrank Mandarino  * shutdown.
520db2a4165SFrank Mandarino  */
521db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
522db2a4165SFrank Mandarino {
523db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
524db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
5256308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
526cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
52787689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
5283c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5293c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
5306627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
531db2a4165SFrank Mandarino 
532db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
533db2a4165SFrank Mandarino 
534db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
535cb666e5bSLiam Girdwood 		cpu_dai->playback.active = codec_dai->playback.active = 0;
536db2a4165SFrank Mandarino 	else
537cb666e5bSLiam Girdwood 		cpu_dai->capture.active = codec_dai->capture.active = 0;
538db2a4165SFrank Mandarino 
539cb666e5bSLiam Girdwood 	if (codec_dai->playback.active == 0 &&
540cb666e5bSLiam Girdwood 		codec_dai->capture.active == 0) {
541cb666e5bSLiam Girdwood 		cpu_dai->active = codec_dai->active = 0;
542db2a4165SFrank Mandarino 	}
543db2a4165SFrank Mandarino 	codec->active--;
544db2a4165SFrank Mandarino 
5456010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
5466010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
5476010b2daSMark Brown 	 */
5486010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5496010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
5506010b2daSMark Brown 
5516335d055SEric Miao 	if (cpu_dai->ops->shutdown)
5526335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
553db2a4165SFrank Mandarino 
5546335d055SEric Miao 	if (codec_dai->ops->shutdown)
5556335d055SEric Miao 		codec_dai->ops->shutdown(substream, codec_dai);
556db2a4165SFrank Mandarino 
557db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
558db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
559db2a4165SFrank Mandarino 
560db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
561db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
562cb666e5bSLiam Girdwood 	cpu_dai->runtime = NULL;
563db2a4165SFrank Mandarino 
564db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
565db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
566cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
5676308419aSMark Brown 		schedule_delayed_work(&card->delayed_work,
56896dd3622SMark Brown 			msecs_to_jiffies(card->pmdown_time));
569db2a4165SFrank Mandarino 	} else {
570db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
571cb666e5bSLiam Girdwood 		snd_soc_dapm_stream_event(codec,
5720b4d221bSLiam Girdwood 			codec_dai->capture.stream_name,
5730b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
574db2a4165SFrank Mandarino 	}
575db2a4165SFrank Mandarino 
576db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
577db2a4165SFrank Mandarino 	return 0;
578db2a4165SFrank Mandarino }
579db2a4165SFrank Mandarino 
580db2a4165SFrank Mandarino /*
581db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
582db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
583db2a4165SFrank Mandarino  * it can refer to the runtime info.
584db2a4165SFrank Mandarino  */
585db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
586db2a4165SFrank Mandarino {
587db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
588db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
5896308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
590cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
59187689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
5923c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5933c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
5946627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
595db2a4165SFrank Mandarino 	int ret = 0;
596db2a4165SFrank Mandarino 
597db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
598cb666e5bSLiam Girdwood 
599cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->prepare) {
600cb666e5bSLiam Girdwood 		ret = machine->ops->prepare(substream);
601cb666e5bSLiam Girdwood 		if (ret < 0) {
602cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
603cb666e5bSLiam Girdwood 			goto out;
604cb666e5bSLiam Girdwood 		}
605cb666e5bSLiam Girdwood 	}
606cb666e5bSLiam Girdwood 
607db2a4165SFrank Mandarino 	if (platform->pcm_ops->prepare) {
608db2a4165SFrank Mandarino 		ret = platform->pcm_ops->prepare(substream);
609a71a468aSLiam Girdwood 		if (ret < 0) {
610a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
611db2a4165SFrank Mandarino 			goto out;
612db2a4165SFrank Mandarino 		}
613a71a468aSLiam Girdwood 	}
614db2a4165SFrank Mandarino 
6156335d055SEric Miao 	if (codec_dai->ops->prepare) {
6166335d055SEric Miao 		ret = codec_dai->ops->prepare(substream, codec_dai);
617a71a468aSLiam Girdwood 		if (ret < 0) {
618a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
619db2a4165SFrank Mandarino 			goto out;
620db2a4165SFrank Mandarino 		}
621a71a468aSLiam Girdwood 	}
622db2a4165SFrank Mandarino 
6236335d055SEric Miao 	if (cpu_dai->ops->prepare) {
6246335d055SEric Miao 		ret = cpu_dai->ops->prepare(substream, cpu_dai);
625cb666e5bSLiam Girdwood 		if (ret < 0) {
626cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
627cb666e5bSLiam Girdwood 			goto out;
628cb666e5bSLiam Girdwood 		}
629cb666e5bSLiam Girdwood 	}
630db2a4165SFrank Mandarino 
631d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
632d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
633d45f6219SMark Brown 	    codec_dai->pop_wait) {
634cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
6356308419aSMark Brown 		cancel_delayed_work(&card->delayed_work);
636db2a4165SFrank Mandarino 	}
637db2a4165SFrank Mandarino 
638db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
639db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
640cb666e5bSLiam Girdwood 					  codec_dai->playback.stream_name,
641db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
642db2a4165SFrank Mandarino 	else
643db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
644cb666e5bSLiam Girdwood 					  codec_dai->capture.stream_name,
645db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
6468c6529dbSLiam Girdwood 
6478c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
648db2a4165SFrank Mandarino 
649db2a4165SFrank Mandarino out:
650db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
651db2a4165SFrank Mandarino 	return ret;
652db2a4165SFrank Mandarino }
653db2a4165SFrank Mandarino 
654db2a4165SFrank Mandarino /*
655db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
656db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
657db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
658db2a4165SFrank Mandarino  */
659db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
660db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
661db2a4165SFrank Mandarino {
662db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
663db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
664cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
66587689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
66687689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
6673c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
6683c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
669db2a4165SFrank Mandarino 	int ret = 0;
670db2a4165SFrank Mandarino 
671db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
672db2a4165SFrank Mandarino 
673cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_params) {
674cb666e5bSLiam Girdwood 		ret = machine->ops->hw_params(substream, params);
675cb666e5bSLiam Girdwood 		if (ret < 0) {
676cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
677db2a4165SFrank Mandarino 			goto out;
678db2a4165SFrank Mandarino 		}
679cb666e5bSLiam Girdwood 	}
680db2a4165SFrank Mandarino 
6816335d055SEric Miao 	if (codec_dai->ops->hw_params) {
6826335d055SEric Miao 		ret = codec_dai->ops->hw_params(substream, params, codec_dai);
683db2a4165SFrank Mandarino 		if (ret < 0) {
684db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
685cb666e5bSLiam Girdwood 				codec_dai->name);
686cb666e5bSLiam Girdwood 			goto codec_err;
687db2a4165SFrank Mandarino 		}
688db2a4165SFrank Mandarino 	}
689db2a4165SFrank Mandarino 
6906335d055SEric Miao 	if (cpu_dai->ops->hw_params) {
6916335d055SEric Miao 		ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
692db2a4165SFrank Mandarino 		if (ret < 0) {
6933ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
694cb666e5bSLiam Girdwood 				cpu_dai->name);
695db2a4165SFrank Mandarino 			goto interface_err;
696db2a4165SFrank Mandarino 		}
697db2a4165SFrank Mandarino 	}
698db2a4165SFrank Mandarino 
699db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_params) {
700db2a4165SFrank Mandarino 		ret = platform->pcm_ops->hw_params(substream, params);
701db2a4165SFrank Mandarino 		if (ret < 0) {
7023ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
703db2a4165SFrank Mandarino 				platform->name);
704db2a4165SFrank Mandarino 			goto platform_err;
705db2a4165SFrank Mandarino 		}
706db2a4165SFrank Mandarino 	}
707db2a4165SFrank Mandarino 
70806f409d7SMark Brown 	machine->rate = params_rate(params);
70906f409d7SMark Brown 
710db2a4165SFrank Mandarino out:
711db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
712db2a4165SFrank Mandarino 	return ret;
713db2a4165SFrank Mandarino 
714db2a4165SFrank Mandarino platform_err:
7156335d055SEric Miao 	if (cpu_dai->ops->hw_free)
7166335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
717db2a4165SFrank Mandarino 
718db2a4165SFrank Mandarino interface_err:
7196335d055SEric Miao 	if (codec_dai->ops->hw_free)
7206335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
721cb666e5bSLiam Girdwood 
722cb666e5bSLiam Girdwood codec_err:
723cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_free)
724cb666e5bSLiam Girdwood 		machine->ops->hw_free(substream);
725db2a4165SFrank Mandarino 
726db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
727db2a4165SFrank Mandarino 	return ret;
728db2a4165SFrank Mandarino }
729db2a4165SFrank Mandarino 
730db2a4165SFrank Mandarino /*
731db2a4165SFrank Mandarino  * Free's resources allocated by hw_params, can be called multiple times
732db2a4165SFrank Mandarino  */
733db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
734db2a4165SFrank Mandarino {
735db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
736db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
737cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
73887689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
73987689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
7403c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
7413c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
7426627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
743db2a4165SFrank Mandarino 
744db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
745db2a4165SFrank Mandarino 
746db2a4165SFrank Mandarino 	/* apply codec digital mute */
7478c6529dbSLiam Girdwood 	if (!codec->active)
7488c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
749db2a4165SFrank Mandarino 
750db2a4165SFrank Mandarino 	/* free any machine hw params */
751db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->hw_free)
752db2a4165SFrank Mandarino 		machine->ops->hw_free(substream);
753db2a4165SFrank Mandarino 
754db2a4165SFrank Mandarino 	/* free any DMA resources */
755db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_free)
756db2a4165SFrank Mandarino 		platform->pcm_ops->hw_free(substream);
757db2a4165SFrank Mandarino 
758db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
7596335d055SEric Miao 	if (codec_dai->ops->hw_free)
7606335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
761db2a4165SFrank Mandarino 
7626335d055SEric Miao 	if (cpu_dai->ops->hw_free)
7636335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
764db2a4165SFrank Mandarino 
765db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
766db2a4165SFrank Mandarino 	return 0;
767db2a4165SFrank Mandarino }
768db2a4165SFrank Mandarino 
769db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
770db2a4165SFrank Mandarino {
771db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
772db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
77387689d56SMark Brown 	struct snd_soc_card *card= socdev->card;
774cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
77587689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
7763c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
7773c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
778db2a4165SFrank Mandarino 	int ret;
779db2a4165SFrank Mandarino 
7806335d055SEric Miao 	if (codec_dai->ops->trigger) {
7816335d055SEric Miao 		ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
782db2a4165SFrank Mandarino 		if (ret < 0)
783db2a4165SFrank Mandarino 			return ret;
784db2a4165SFrank Mandarino 	}
785db2a4165SFrank Mandarino 
786db2a4165SFrank Mandarino 	if (platform->pcm_ops->trigger) {
787db2a4165SFrank Mandarino 		ret = platform->pcm_ops->trigger(substream, cmd);
788db2a4165SFrank Mandarino 		if (ret < 0)
789db2a4165SFrank Mandarino 			return ret;
790db2a4165SFrank Mandarino 	}
791db2a4165SFrank Mandarino 
7926335d055SEric Miao 	if (cpu_dai->ops->trigger) {
7936335d055SEric Miao 		ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
794db2a4165SFrank Mandarino 		if (ret < 0)
795db2a4165SFrank Mandarino 			return ret;
796db2a4165SFrank Mandarino 	}
797db2a4165SFrank Mandarino 	return 0;
798db2a4165SFrank Mandarino }
799db2a4165SFrank Mandarino 
800db2a4165SFrank Mandarino /* ASoC PCM operations */
801db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
802db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
803db2a4165SFrank Mandarino 	.close		= soc_codec_close,
804db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
805db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
806db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
807db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
808db2a4165SFrank Mandarino };
809db2a4165SFrank Mandarino 
810db2a4165SFrank Mandarino #ifdef CONFIG_PM
811db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
812416356fcSMark Brown static int soc_suspend(struct device *dev)
813db2a4165SFrank Mandarino {
814416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
815db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
81687506549SMark Brown 	struct snd_soc_card *card = socdev->card;
81787689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
818db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
8196627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
820db2a4165SFrank Mandarino 	int i;
821db2a4165SFrank Mandarino 
822e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
823e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
824e3509ff0SDaniel Mack 	 */
825e3509ff0SDaniel Mack 	if (!codec)
826e3509ff0SDaniel Mack 		return 0;
827e3509ff0SDaniel Mack 
8286ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
8296ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
8306ed25978SAndy Green 	 */
8316ed25978SAndy Green 	snd_power_lock(codec->card);
8326ed25978SAndy Green 	snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
8336ed25978SAndy Green 	snd_power_unlock(codec->card);
8346ed25978SAndy Green 
8356ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
8366ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
8376ed25978SAndy Green 
838db2a4165SFrank Mandarino 	/* mute any active DAC's */
839dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
840dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
8416335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
8426335d055SEric Miao 			dai->ops->digital_mute(dai, 1);
843db2a4165SFrank Mandarino 	}
844db2a4165SFrank Mandarino 
8454ccab3e7SLiam Girdwood 	/* suspend all pcms */
84687506549SMark Brown 	for (i = 0; i < card->num_links; i++)
84787506549SMark Brown 		snd_pcm_suspend_all(card->dai_link[i].pcm);
8484ccab3e7SLiam Girdwood 
84987506549SMark Brown 	if (card->suspend_pre)
850416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
851db2a4165SFrank Mandarino 
85287506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
85387506549SMark Brown 		struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
8543ba9e10aSMark Brown 		if (cpu_dai->suspend && !cpu_dai->ac97_control)
855dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
856db2a4165SFrank Mandarino 		if (platform->suspend)
857*d273ebe7Sjassi brar 			platform->suspend(&card->dai_link[i]);
858db2a4165SFrank Mandarino 	}
859db2a4165SFrank Mandarino 
860db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
8616308419aSMark Brown 	run_delayed_work(&card->delayed_work);
8620be9898aSMark Brown 	codec->suspend_bias_level = codec->bias_level;
863db2a4165SFrank Mandarino 
864db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
865db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
866db2a4165SFrank Mandarino 		if (stream != NULL)
867db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
868db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
869db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
870db2a4165SFrank Mandarino 		if (stream != NULL)
871db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
872db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
873db2a4165SFrank Mandarino 	}
874db2a4165SFrank Mandarino 
875db2a4165SFrank Mandarino 	if (codec_dev->suspend)
876416356fcSMark Brown 		codec_dev->suspend(pdev, PMSG_SUSPEND);
877db2a4165SFrank Mandarino 
87887506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
87987506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
8803ba9e10aSMark Brown 		if (cpu_dai->suspend && cpu_dai->ac97_control)
881dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
882db2a4165SFrank Mandarino 	}
883db2a4165SFrank Mandarino 
88487506549SMark Brown 	if (card->suspend_post)
885416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
886db2a4165SFrank Mandarino 
887db2a4165SFrank Mandarino 	return 0;
888db2a4165SFrank Mandarino }
889db2a4165SFrank Mandarino 
8906ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
8916ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
8926ed25978SAndy Green  */
8936ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
894db2a4165SFrank Mandarino {
8956308419aSMark Brown 	struct snd_soc_card *card = container_of(work,
8966308419aSMark Brown 						 struct snd_soc_card,
8976ed25978SAndy Green 						 deferred_resume_work);
8986308419aSMark Brown 	struct snd_soc_device *socdev = card->socdev;
89987689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
900db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
9016627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
9026ed25978SAndy Green 	struct platform_device *pdev = to_platform_device(socdev->dev);
903db2a4165SFrank Mandarino 	int i;
904db2a4165SFrank Mandarino 
9056ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
9066ed25978SAndy Green 	 * so userspace apps are blocked from touching us
9076ed25978SAndy Green 	 */
9086ed25978SAndy Green 
909fde22f27SMark Brown 	dev_dbg(socdev->dev, "starting resume work\n");
9106ed25978SAndy Green 
91187506549SMark Brown 	if (card->resume_pre)
91287506549SMark Brown 		card->resume_pre(pdev);
913db2a4165SFrank Mandarino 
91487506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
91587506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
9163ba9e10aSMark Brown 		if (cpu_dai->resume && cpu_dai->ac97_control)
917dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
918db2a4165SFrank Mandarino 	}
919db2a4165SFrank Mandarino 
920db2a4165SFrank Mandarino 	if (codec_dev->resume)
921db2a4165SFrank Mandarino 		codec_dev->resume(pdev);
922db2a4165SFrank Mandarino 
923db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
924db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
925db2a4165SFrank Mandarino 		if (stream != NULL)
926db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
927db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
928db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
929db2a4165SFrank Mandarino 		if (stream != NULL)
930db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
931db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
932db2a4165SFrank Mandarino 	}
933db2a4165SFrank Mandarino 
9343ff3f64bSMark Brown 	/* unmute any active DACs */
935dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
936dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
9376335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
9386335d055SEric Miao 			dai->ops->digital_mute(dai, 0);
939db2a4165SFrank Mandarino 	}
940db2a4165SFrank Mandarino 
94187506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
94287506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
9433ba9e10aSMark Brown 		if (cpu_dai->resume && !cpu_dai->ac97_control)
944dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
945db2a4165SFrank Mandarino 		if (platform->resume)
946*d273ebe7Sjassi brar 			platform->resume(&card->dai_link[i]);
947db2a4165SFrank Mandarino 	}
948db2a4165SFrank Mandarino 
94987506549SMark Brown 	if (card->resume_post)
95087506549SMark Brown 		card->resume_post(pdev);
951db2a4165SFrank Mandarino 
952fde22f27SMark Brown 	dev_dbg(socdev->dev, "resume work completed\n");
9536ed25978SAndy Green 
9546ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
9556ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
9566ed25978SAndy Green }
9576ed25978SAndy Green 
9586ed25978SAndy Green /* powers up audio subsystem after a suspend */
959416356fcSMark Brown static int soc_resume(struct device *dev)
9606ed25978SAndy Green {
961416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
9626ed25978SAndy Green 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
9636308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
96464ab9baaSMark Brown 	struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
9656ed25978SAndy Green 
96664ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
96764ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
96864ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
96964ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
97064ab9baaSMark Brown 	 */
97164ab9baaSMark Brown 	if (cpu_dai->ac97_control) {
97264ab9baaSMark Brown 		dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
97364ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
97464ab9baaSMark Brown 	} else {
97564ab9baaSMark Brown 		dev_dbg(socdev->dev, "Scheduling resume work\n");
9766308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
977fde22f27SMark Brown 			dev_err(socdev->dev, "resume work item may be lost\n");
97864ab9baaSMark Brown 	}
9796ed25978SAndy Green 
980db2a4165SFrank Mandarino 	return 0;
981db2a4165SFrank Mandarino }
982db2a4165SFrank Mandarino #else
983db2a4165SFrank Mandarino #define soc_suspend	NULL
984db2a4165SFrank Mandarino #define soc_resume	NULL
985db2a4165SFrank Mandarino #endif
986db2a4165SFrank Mandarino 
98702a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
98802a06d30SBarry Song };
98902a06d30SBarry Song 
990435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card)
991db2a4165SFrank Mandarino {
992435c5e25SMark Brown 	struct platform_device *pdev = container_of(card->dev,
993435c5e25SMark Brown 						    struct platform_device,
994435c5e25SMark Brown 						    dev);
995435c5e25SMark Brown 	struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
996fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
997435c5e25SMark Brown 	struct snd_soc_platform *platform;
998435c5e25SMark Brown 	struct snd_soc_dai *dai;
9996b05eda6SMark Brown 	int i, found, ret, ac97;
1000db2a4165SFrank Mandarino 
1001435c5e25SMark Brown 	if (card->instantiated)
1002435c5e25SMark Brown 		return;
10036308419aSMark Brown 
1004435c5e25SMark Brown 	found = 0;
1005435c5e25SMark Brown 	list_for_each_entry(platform, &platform_list, list)
1006435c5e25SMark Brown 		if (card->platform == platform) {
1007435c5e25SMark Brown 			found = 1;
1008435c5e25SMark Brown 			break;
1009435c5e25SMark Brown 		}
1010435c5e25SMark Brown 	if (!found) {
1011435c5e25SMark Brown 		dev_dbg(card->dev, "Platform %s not registered\n",
1012435c5e25SMark Brown 			card->platform->name);
1013435c5e25SMark Brown 		return;
1014c5af3a2eSMark Brown 	}
1015c5af3a2eSMark Brown 
10166b05eda6SMark Brown 	ac97 = 0;
1017435c5e25SMark Brown 	for (i = 0; i < card->num_links; i++) {
1018435c5e25SMark Brown 		found = 0;
1019435c5e25SMark Brown 		list_for_each_entry(dai, &dai_list, list)
1020435c5e25SMark Brown 			if (card->dai_link[i].cpu_dai == dai) {
1021435c5e25SMark Brown 				found = 1;
1022435c5e25SMark Brown 				break;
1023435c5e25SMark Brown 			}
1024435c5e25SMark Brown 		if (!found) {
1025435c5e25SMark Brown 			dev_dbg(card->dev, "DAI %s not registered\n",
1026435c5e25SMark Brown 				card->dai_link[i].cpu_dai->name);
1027435c5e25SMark Brown 			return;
1028435c5e25SMark Brown 		}
10296b05eda6SMark Brown 
10306b05eda6SMark Brown 		if (card->dai_link[i].cpu_dai->ac97_control)
10316b05eda6SMark Brown 			ac97 = 1;
10326b05eda6SMark Brown 	}
10336b05eda6SMark Brown 
103402a06d30SBarry Song 	for (i = 0; i < card->num_links; i++) {
103502a06d30SBarry Song 		if (!card->dai_link[i].codec_dai->ops)
103602a06d30SBarry Song 			card->dai_link[i].codec_dai->ops = &null_dai_ops;
103702a06d30SBarry Song 	}
103802a06d30SBarry Song 
10396b05eda6SMark Brown 	/* If we have AC97 in the system then don't wait for the
10406b05eda6SMark Brown 	 * codec.  This will need revisiting if we have to handle
10416b05eda6SMark Brown 	 * systems with mixed AC97 and non-AC97 parts.  Only check for
10426b05eda6SMark Brown 	 * DAIs currently; we can't do this per link since some AC97
10436b05eda6SMark Brown 	 * codecs have non-AC97 DAIs.
10446b05eda6SMark Brown 	 */
10456b05eda6SMark Brown 	if (!ac97)
10466b05eda6SMark Brown 		for (i = 0; i < card->num_links; i++) {
10476b05eda6SMark Brown 			found = 0;
10486b05eda6SMark Brown 			list_for_each_entry(dai, &dai_list, list)
10496b05eda6SMark Brown 				if (card->dai_link[i].codec_dai == dai) {
10506b05eda6SMark Brown 					found = 1;
10516b05eda6SMark Brown 					break;
10526b05eda6SMark Brown 				}
10536b05eda6SMark Brown 			if (!found) {
10546b05eda6SMark Brown 				dev_dbg(card->dev, "DAI %s not registered\n",
10556b05eda6SMark Brown 					card->dai_link[i].codec_dai->name);
10566b05eda6SMark Brown 				return;
10576b05eda6SMark Brown 			}
1058435c5e25SMark Brown 		}
1059435c5e25SMark Brown 
1060435c5e25SMark Brown 	/* Note that we do not current check for codec components */
1061435c5e25SMark Brown 
1062435c5e25SMark Brown 	dev_dbg(card->dev, "All components present, instantiating\n");
1063435c5e25SMark Brown 
1064435c5e25SMark Brown 	/* Found everything, bring it up */
106596dd3622SMark Brown 	card->pmdown_time = pmdown_time;
106696dd3622SMark Brown 
106787506549SMark Brown 	if (card->probe) {
106887506549SMark Brown 		ret = card->probe(pdev);
1069db2a4165SFrank Mandarino 		if (ret < 0)
1070435c5e25SMark Brown 			return;
1071db2a4165SFrank Mandarino 	}
1072db2a4165SFrank Mandarino 
107387506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
107487506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1075db2a4165SFrank Mandarino 		if (cpu_dai->probe) {
1076bdb92876SMark Brown 			ret = cpu_dai->probe(pdev, cpu_dai);
1077db2a4165SFrank Mandarino 			if (ret < 0)
1078db2a4165SFrank Mandarino 				goto cpu_dai_err;
1079db2a4165SFrank Mandarino 		}
1080db2a4165SFrank Mandarino 	}
1081db2a4165SFrank Mandarino 
1082db2a4165SFrank Mandarino 	if (codec_dev->probe) {
1083db2a4165SFrank Mandarino 		ret = codec_dev->probe(pdev);
1084db2a4165SFrank Mandarino 		if (ret < 0)
1085db2a4165SFrank Mandarino 			goto cpu_dai_err;
1086db2a4165SFrank Mandarino 	}
1087fe3e78e0SMark Brown 	codec = card->codec;
1088db2a4165SFrank Mandarino 
1089db2a4165SFrank Mandarino 	if (platform->probe) {
1090db2a4165SFrank Mandarino 		ret = platform->probe(pdev);
1091db2a4165SFrank Mandarino 		if (ret < 0)
1092db2a4165SFrank Mandarino 			goto platform_err;
1093db2a4165SFrank Mandarino 	}
1094db2a4165SFrank Mandarino 
1095db2a4165SFrank Mandarino 	/* DAPM stream work */
10966308419aSMark Brown 	INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
10971301a964SRandy Dunlap #ifdef CONFIG_PM
10986ed25978SAndy Green 	/* deferred resume work */
10996308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
11001301a964SRandy Dunlap #endif
11016ed25978SAndy Green 
1102fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1103fe3e78e0SMark Brown 		if (card->dai_link[i].init) {
1104fe3e78e0SMark Brown 			ret = card->dai_link[i].init(codec);
1105fe3e78e0SMark Brown 			if (ret < 0) {
1106fe3e78e0SMark Brown 				printk(KERN_ERR "asoc: failed to init %s\n",
1107fe3e78e0SMark Brown 					card->dai_link[i].stream_name);
1108fe3e78e0SMark Brown 				continue;
1109fe3e78e0SMark Brown 			}
1110fe3e78e0SMark Brown 		}
1111f7732053SBarry Song 		if (card->dai_link[i].codec_dai->ac97_control)
1112fe3e78e0SMark Brown 			ac97 = 1;
1113fe3e78e0SMark Brown 	}
1114fe3e78e0SMark Brown 
1115fe3e78e0SMark Brown 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1116fe3e78e0SMark Brown 		 "%s",  card->name);
1117fe3e78e0SMark Brown 	snprintf(codec->card->longname, sizeof(codec->card->longname),
1118fe3e78e0SMark Brown 		 "%s (%s)", card->name, codec->name);
1119fe3e78e0SMark Brown 
1120fe3e78e0SMark Brown 	/* Make sure all DAPM widgets are instantiated */
1121fe3e78e0SMark Brown 	snd_soc_dapm_new_widgets(codec);
1122fe3e78e0SMark Brown 
1123fe3e78e0SMark Brown 	ret = snd_card_register(codec->card);
1124fe3e78e0SMark Brown 	if (ret < 0) {
1125fe3e78e0SMark Brown 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1126fe3e78e0SMark Brown 				codec->name);
1127fe3e78e0SMark Brown 		goto card_err;
1128fe3e78e0SMark Brown 	}
1129fe3e78e0SMark Brown 
1130fe3e78e0SMark Brown 	mutex_lock(&codec->mutex);
1131fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1132fe3e78e0SMark Brown 	/* Only instantiate AC97 if not already done by the adaptor
1133fe3e78e0SMark Brown 	 * for the generic AC97 subsystem.
1134fe3e78e0SMark Brown 	 */
1135fe3e78e0SMark Brown 	if (ac97 && strcmp(codec->name, "AC97") != 0) {
1136fe3e78e0SMark Brown 		ret = soc_ac97_dev_register(codec);
1137fe3e78e0SMark Brown 		if (ret < 0) {
1138fe3e78e0SMark Brown 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1139fe3e78e0SMark Brown 			snd_card_free(codec->card);
1140fe3e78e0SMark Brown 			mutex_unlock(&codec->mutex);
1141fe3e78e0SMark Brown 			goto card_err;
1142fe3e78e0SMark Brown 		}
1143fe3e78e0SMark Brown 	}
1144fe3e78e0SMark Brown #endif
1145fe3e78e0SMark Brown 
1146fe3e78e0SMark Brown 	ret = snd_soc_dapm_sys_add(card->socdev->dev);
1147fe3e78e0SMark Brown 	if (ret < 0)
1148fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1149fe3e78e0SMark Brown 
1150dbe21408SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1151dbe21408SMark Brown 	if (ret < 0)
1152dbe21408SMark Brown 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1153dbe21408SMark Brown 
1154fe3e78e0SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1155fe3e78e0SMark Brown 	if (ret < 0)
1156fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1157fe3e78e0SMark Brown 
1158fe3e78e0SMark Brown 	soc_init_codec_debugfs(codec);
1159fe3e78e0SMark Brown 	mutex_unlock(&codec->mutex);
1160fe3e78e0SMark Brown 
1161435c5e25SMark Brown 	card->instantiated = 1;
1162435c5e25SMark Brown 
1163435c5e25SMark Brown 	return;
1164db2a4165SFrank Mandarino 
1165fe3e78e0SMark Brown card_err:
1166fe3e78e0SMark Brown 	if (platform->remove)
1167fe3e78e0SMark Brown 		platform->remove(pdev);
1168fe3e78e0SMark Brown 
1169db2a4165SFrank Mandarino platform_err:
1170db2a4165SFrank Mandarino 	if (codec_dev->remove)
1171db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1172db2a4165SFrank Mandarino 
1173db2a4165SFrank Mandarino cpu_dai_err:
117418b9b3d9SLiam Girdwood 	for (i--; i >= 0; i--) {
117587506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1176db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1177bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1178db2a4165SFrank Mandarino 	}
1179db2a4165SFrank Mandarino 
118087506549SMark Brown 	if (card->remove)
118187506549SMark Brown 		card->remove(pdev);
1182435c5e25SMark Brown }
1183db2a4165SFrank Mandarino 
1184435c5e25SMark Brown /*
1185435c5e25SMark Brown  * Attempt to initialise any uninitalised cards.  Must be called with
1186435c5e25SMark Brown  * client_mutex.
1187435c5e25SMark Brown  */
1188435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1189435c5e25SMark Brown {
1190435c5e25SMark Brown 	struct snd_soc_card *card;
1191435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1192435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1193435c5e25SMark Brown }
1194435c5e25SMark Brown 
1195435c5e25SMark Brown /* probes a new socdev */
1196435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1197435c5e25SMark Brown {
1198435c5e25SMark Brown 	int ret = 0;
1199435c5e25SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1200435c5e25SMark Brown 	struct snd_soc_card *card = socdev->card;
1201435c5e25SMark Brown 
1202435c5e25SMark Brown 	/* Bodge while we push things out of socdev */
1203435c5e25SMark Brown 	card->socdev = socdev;
1204435c5e25SMark Brown 
1205435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1206435c5e25SMark Brown 	card->dev = &pdev->dev;
1207435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1208435c5e25SMark Brown 	if (ret != 0) {
1209435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1210db2a4165SFrank Mandarino 		return ret;
1211db2a4165SFrank Mandarino 	}
1212db2a4165SFrank Mandarino 
1213435c5e25SMark Brown 	return 0;
1214435c5e25SMark Brown }
1215435c5e25SMark Brown 
1216db2a4165SFrank Mandarino /* removes a socdev */
1217db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1218db2a4165SFrank Mandarino {
1219db2a4165SFrank Mandarino 	int i;
1220db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
122187506549SMark Brown 	struct snd_soc_card *card = socdev->card;
122287689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
1223db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1224db2a4165SFrank Mandarino 
1225914dc182SMike Rapoport 	if (!card->instantiated)
1226914dc182SMike Rapoport 		return 0;
1227914dc182SMike Rapoport 
12286308419aSMark Brown 	run_delayed_work(&card->delayed_work);
1229965ac42cSLiam Girdwood 
1230db2a4165SFrank Mandarino 	if (platform->remove)
1231db2a4165SFrank Mandarino 		platform->remove(pdev);
1232db2a4165SFrank Mandarino 
1233db2a4165SFrank Mandarino 	if (codec_dev->remove)
1234db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1235db2a4165SFrank Mandarino 
123687506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
123787506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1238db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1239bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1240db2a4165SFrank Mandarino 	}
1241db2a4165SFrank Mandarino 
124287506549SMark Brown 	if (card->remove)
124387506549SMark Brown 		card->remove(pdev);
1244db2a4165SFrank Mandarino 
1245c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1246c5af3a2eSMark Brown 
1247db2a4165SFrank Mandarino 	return 0;
1248db2a4165SFrank Mandarino }
1249db2a4165SFrank Mandarino 
1250416356fcSMark Brown static int soc_poweroff(struct device *dev)
125151737470SMark Brown {
1252416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
125351737470SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
125451737470SMark Brown 	struct snd_soc_card *card = socdev->card;
125551737470SMark Brown 
125651737470SMark Brown 	if (!card->instantiated)
1257416356fcSMark Brown 		return 0;
125851737470SMark Brown 
125951737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
126051737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
126151737470SMark Brown 	run_delayed_work(&card->delayed_work);
126251737470SMark Brown 
126351737470SMark Brown 	snd_soc_dapm_shutdown(socdev);
1264416356fcSMark Brown 
1265416356fcSMark Brown 	return 0;
126651737470SMark Brown }
126751737470SMark Brown 
126847145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1269416356fcSMark Brown 	.suspend = soc_suspend,
1270416356fcSMark Brown 	.resume = soc_resume,
1271416356fcSMark Brown 	.poweroff = soc_poweroff,
1272416356fcSMark Brown };
1273416356fcSMark Brown 
1274db2a4165SFrank Mandarino /* ASoC platform driver */
1275db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1276db2a4165SFrank Mandarino 	.driver		= {
1277db2a4165SFrank Mandarino 		.name		= "soc-audio",
12788b45a209SKay Sievers 		.owner		= THIS_MODULE,
1279416356fcSMark Brown 		.pm		= &soc_pm_ops,
1280db2a4165SFrank Mandarino 	},
1281db2a4165SFrank Mandarino 	.probe		= soc_probe,
1282db2a4165SFrank Mandarino 	.remove		= soc_remove,
1283db2a4165SFrank Mandarino };
1284db2a4165SFrank Mandarino 
1285db2a4165SFrank Mandarino /* create a new pcm */
1286db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev,
1287db2a4165SFrank Mandarino 	struct snd_soc_dai_link *dai_link, int num)
1288db2a4165SFrank Mandarino {
128987689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
12906627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
129187689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
12923c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = dai_link->codec_dai;
12933c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1294db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd;
1295db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1296db2a4165SFrank Mandarino 	char new_name[64];
1297db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1298db2a4165SFrank Mandarino 
1299db2a4165SFrank Mandarino 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1300db2a4165SFrank Mandarino 	if (rtd == NULL)
1301db2a4165SFrank Mandarino 		return -ENOMEM;
1302cb666e5bSLiam Girdwood 
1303cb666e5bSLiam Girdwood 	rtd->dai = dai_link;
1304db2a4165SFrank Mandarino 	rtd->socdev = socdev;
13056627a653SMark Brown 	codec_dai->codec = card->codec;
1306db2a4165SFrank Mandarino 
1307db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
130840ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
130940ca1142SMark Brown 		 dai_link->stream_name, codec_dai->name, num);
1310db2a4165SFrank Mandarino 
1311db2a4165SFrank Mandarino 	if (codec_dai->playback.channels_min)
1312db2a4165SFrank Mandarino 		playback = 1;
1313db2a4165SFrank Mandarino 	if (codec_dai->capture.channels_min)
1314db2a4165SFrank Mandarino 		capture = 1;
1315db2a4165SFrank Mandarino 
1316db2a4165SFrank Mandarino 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1317db2a4165SFrank Mandarino 		capture, &pcm);
1318db2a4165SFrank Mandarino 	if (ret < 0) {
13193ff3f64bSMark Brown 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
13203ff3f64bSMark Brown 			codec->name);
1321db2a4165SFrank Mandarino 		kfree(rtd);
1322db2a4165SFrank Mandarino 		return ret;
1323db2a4165SFrank Mandarino 	}
1324db2a4165SFrank Mandarino 
13254ccab3e7SLiam Girdwood 	dai_link->pcm = pcm;
1326db2a4165SFrank Mandarino 	pcm->private_data = rtd;
132787689d56SMark Brown 	soc_pcm_ops.mmap = platform->pcm_ops->mmap;
132887689d56SMark Brown 	soc_pcm_ops.pointer = platform->pcm_ops->pointer;
132987689d56SMark Brown 	soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
133087689d56SMark Brown 	soc_pcm_ops.copy = platform->pcm_ops->copy;
133187689d56SMark Brown 	soc_pcm_ops.silence = platform->pcm_ops->silence;
133287689d56SMark Brown 	soc_pcm_ops.ack = platform->pcm_ops->ack;
133387689d56SMark Brown 	soc_pcm_ops.page = platform->pcm_ops->page;
1334db2a4165SFrank Mandarino 
1335db2a4165SFrank Mandarino 	if (playback)
1336db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1337db2a4165SFrank Mandarino 
1338db2a4165SFrank Mandarino 	if (capture)
1339db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1340db2a4165SFrank Mandarino 
134187689d56SMark Brown 	ret = platform->pcm_new(codec->card, codec_dai, pcm);
1342db2a4165SFrank Mandarino 	if (ret < 0) {
1343db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1344db2a4165SFrank Mandarino 		kfree(rtd);
1345db2a4165SFrank Mandarino 		return ret;
1346db2a4165SFrank Mandarino 	}
1347db2a4165SFrank Mandarino 
134887689d56SMark Brown 	pcm->private_free = platform->pcm_free;
1349db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1350db2a4165SFrank Mandarino 		cpu_dai->name);
1351db2a4165SFrank Mandarino 	return ret;
1352db2a4165SFrank Mandarino }
1353db2a4165SFrank Mandarino 
1354096e49d5SMark Brown /**
1355096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1356096e49d5SMark Brown  *
1357096e49d5SMark Brown  * @codec: CODEC to query.
1358096e49d5SMark Brown  * @reg: Register to query.
1359096e49d5SMark Brown  *
1360096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1361096e49d5SMark Brown  */
1362096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1363096e49d5SMark Brown {
1364096e49d5SMark Brown 	if (codec->volatile_register)
1365096e49d5SMark Brown 		return codec->volatile_register(reg);
1366096e49d5SMark Brown 	else
1367096e49d5SMark Brown 		return 0;
1368096e49d5SMark Brown }
1369096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1370096e49d5SMark Brown 
1371db2a4165SFrank Mandarino /**
1372db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1373db2a4165SFrank Mandarino  * @codec: audio codec
1374db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1375db2a4165SFrank Mandarino  * @num: AC97 codec number
1376db2a4165SFrank Mandarino  *
1377db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1378db2a4165SFrank Mandarino  */
1379db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1380db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1381db2a4165SFrank Mandarino {
1382db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1383db2a4165SFrank Mandarino 
1384db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1385db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1386db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1387db2a4165SFrank Mandarino 		return -ENOMEM;
1388db2a4165SFrank Mandarino 	}
1389db2a4165SFrank Mandarino 
1390db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1391db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1392db2a4165SFrank Mandarino 		kfree(codec->ac97);
1393db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1394db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1395db2a4165SFrank Mandarino 		return -ENOMEM;
1396db2a4165SFrank Mandarino 	}
1397db2a4165SFrank Mandarino 
1398db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1399db2a4165SFrank Mandarino 	codec->ac97->num = num;
14002718625fSMark Brown 	codec->dev = &codec->ac97->dev;
1401db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1402db2a4165SFrank Mandarino 	return 0;
1403db2a4165SFrank Mandarino }
1404db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1405db2a4165SFrank Mandarino 
1406db2a4165SFrank Mandarino /**
1407db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1408db2a4165SFrank Mandarino  * @codec: audio codec
1409db2a4165SFrank Mandarino  *
1410db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1411db2a4165SFrank Mandarino  */
1412db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1413db2a4165SFrank Mandarino {
1414db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1415db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1416db2a4165SFrank Mandarino 	kfree(codec->ac97);
1417db2a4165SFrank Mandarino 	codec->ac97 = NULL;
1418db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1419db2a4165SFrank Mandarino }
1420db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1421db2a4165SFrank Mandarino 
1422db2a4165SFrank Mandarino /**
1423db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1424db2a4165SFrank Mandarino  * @codec: audio codec
1425db2a4165SFrank Mandarino  * @reg: codec register
1426db2a4165SFrank Mandarino  * @mask: register mask
1427db2a4165SFrank Mandarino  * @value: new value
1428db2a4165SFrank Mandarino  *
1429db2a4165SFrank Mandarino  * Writes new register value.
1430db2a4165SFrank Mandarino  *
1431db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1432db2a4165SFrank Mandarino  */
1433db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
143446f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1435db2a4165SFrank Mandarino {
1436db2a4165SFrank Mandarino 	int change;
143746f5822fSDaniel Ribeiro 	unsigned int old, new;
1438db2a4165SFrank Mandarino 
1439db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1440db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1441db2a4165SFrank Mandarino 	change = old != new;
1442db2a4165SFrank Mandarino 	if (change)
1443db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1444db2a4165SFrank Mandarino 
1445db2a4165SFrank Mandarino 	return change;
1446db2a4165SFrank Mandarino }
1447db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1448db2a4165SFrank Mandarino 
1449db2a4165SFrank Mandarino /**
14506c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
14516c508c62SEero Nurkkala  * @codec: audio codec
14526c508c62SEero Nurkkala  * @reg: codec register
14536c508c62SEero Nurkkala  * @mask: register mask
14546c508c62SEero Nurkkala  * @value: new value
14556c508c62SEero Nurkkala  *
14566c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
14576c508c62SEero Nurkkala  *
14586c508c62SEero Nurkkala  * Returns 1 for change else 0.
14596c508c62SEero Nurkkala  */
1460dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
14616c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
14626c508c62SEero Nurkkala 			       unsigned int value)
14636c508c62SEero Nurkkala {
14646c508c62SEero Nurkkala 	int change;
14656c508c62SEero Nurkkala 
14666c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
14676c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
14686c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
14696c508c62SEero Nurkkala 
14706c508c62SEero Nurkkala 	return change;
14716c508c62SEero Nurkkala }
1472dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
14736c508c62SEero Nurkkala 
14746c508c62SEero Nurkkala /**
1475db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1476db2a4165SFrank Mandarino  * @codec: audio codec
1477db2a4165SFrank Mandarino  * @reg: codec register
1478db2a4165SFrank Mandarino  * @mask: register mask
1479db2a4165SFrank Mandarino  * @value: new value
1480db2a4165SFrank Mandarino  *
1481db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1482db2a4165SFrank Mandarino  * different from the old value.
1483db2a4165SFrank Mandarino  *
1484db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1485db2a4165SFrank Mandarino  */
1486db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
148746f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1488db2a4165SFrank Mandarino {
1489db2a4165SFrank Mandarino 	int change;
149046f5822fSDaniel Ribeiro 	unsigned int old, new;
1491db2a4165SFrank Mandarino 
1492db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1493db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1494db2a4165SFrank Mandarino 	change = old != new;
1495db2a4165SFrank Mandarino 
1496db2a4165SFrank Mandarino 	return change;
1497db2a4165SFrank Mandarino }
1498db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1499db2a4165SFrank Mandarino 
1500db2a4165SFrank Mandarino /**
1501db2a4165SFrank Mandarino  * snd_soc_new_pcms - create new sound card and pcms
1502db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1503ac11a2b3SMark Brown  * @idx: ALSA card index
1504ac11a2b3SMark Brown  * @xid: card identification
1505db2a4165SFrank Mandarino  *
1506db2a4165SFrank Mandarino  * Create a new sound card based upon the codec and interface pcms.
1507db2a4165SFrank Mandarino  *
1508db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1509db2a4165SFrank Mandarino  */
1510db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1511db2a4165SFrank Mandarino {
151287506549SMark Brown 	struct snd_soc_card *card = socdev->card;
15136627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
1514bd7dd77cSTakashi Iwai 	int ret, i;
1515db2a4165SFrank Mandarino 
1516db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1517db2a4165SFrank Mandarino 
1518db2a4165SFrank Mandarino 	/* register a sound card */
1519bd7dd77cSTakashi Iwai 	ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1520bd7dd77cSTakashi Iwai 	if (ret < 0) {
1521db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1522db2a4165SFrank Mandarino 			codec->name);
1523db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1524bd7dd77cSTakashi Iwai 		return ret;
1525db2a4165SFrank Mandarino 	}
1526db2a4165SFrank Mandarino 
1527452c5eaaSMark Brown 	codec->socdev = socdev;
1528db2a4165SFrank Mandarino 	codec->card->dev = socdev->dev;
1529db2a4165SFrank Mandarino 	codec->card->private_data = codec;
1530db2a4165SFrank Mandarino 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1531db2a4165SFrank Mandarino 
1532db2a4165SFrank Mandarino 	/* create the pcms */
153387506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
153487506549SMark Brown 		ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1535db2a4165SFrank Mandarino 		if (ret < 0) {
1536db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't create pcm %s\n",
153787506549SMark Brown 				card->dai_link[i].stream_name);
1538db2a4165SFrank Mandarino 			mutex_unlock(&codec->mutex);
1539db2a4165SFrank Mandarino 			return ret;
1540db2a4165SFrank Mandarino 		}
1541f7732053SBarry Song 		if (card->dai_link[i].codec_dai->ac97_control) {
1542f7732053SBarry Song 			snd_ac97_dev_add_pdata(codec->ac97,
1543f7732053SBarry Song 				card->dai_link[i].cpu_dai->ac97_pdata);
1544f7732053SBarry Song 		}
1545db2a4165SFrank Mandarino 	}
1546db2a4165SFrank Mandarino 
1547db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1548db2a4165SFrank Mandarino 	return ret;
1549db2a4165SFrank Mandarino }
1550db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1551db2a4165SFrank Mandarino 
1552db2a4165SFrank Mandarino /**
1553db2a4165SFrank Mandarino  * snd_soc_free_pcms - free sound card and pcms
1554db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1555db2a4165SFrank Mandarino  *
1556db2a4165SFrank Mandarino  * Frees sound card and pcms associated with the socdev.
1557db2a4165SFrank Mandarino  * Also unregister the codec if it is an AC97 device.
1558db2a4165SFrank Mandarino  */
1559db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev)
1560db2a4165SFrank Mandarino {
15616627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
1562a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
15633c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
1564a68660e0SLiam Girdwood 	int i;
1565a68660e0SLiam Girdwood #endif
1566db2a4165SFrank Mandarino 
1567db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
15686627a653SMark Brown 	soc_cleanup_codec_debugfs(codec);
1569db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1570a68660e0SLiam Girdwood 	for (i = 0; i < codec->num_dai; i++) {
1571a68660e0SLiam Girdwood 		codec_dai = &codec->dai[i];
1572d2314e0eSAtsushi Nemoto 		if (codec_dai->ac97_control && codec->ac97 &&
1573d2314e0eSAtsushi Nemoto 		    strcmp(codec->name, "AC97") != 0) {
1574db2a4165SFrank Mandarino 			soc_ac97_dev_unregister(codec);
1575a68660e0SLiam Girdwood 			goto free_card;
1576a68660e0SLiam Girdwood 		}
1577a68660e0SLiam Girdwood 	}
1578a68660e0SLiam Girdwood free_card:
1579db2a4165SFrank Mandarino #endif
1580db2a4165SFrank Mandarino 
1581db2a4165SFrank Mandarino 	if (codec->card)
1582db2a4165SFrank Mandarino 		snd_card_free(codec->card);
1583db2a4165SFrank Mandarino 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1584db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1585db2a4165SFrank Mandarino }
1586db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1587db2a4165SFrank Mandarino 
1588db2a4165SFrank Mandarino /**
1589db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1590db2a4165SFrank Mandarino  * @substream: the pcm substream
1591db2a4165SFrank Mandarino  * @hw: the hardware parameters
1592db2a4165SFrank Mandarino  *
1593db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1594db2a4165SFrank Mandarino  */
1595db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1596db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1597db2a4165SFrank Mandarino {
1598db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1599db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1600db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1601db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1602db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1603db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1604db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1605db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1606db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1607db2a4165SFrank Mandarino 	return 0;
1608db2a4165SFrank Mandarino }
1609db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1610db2a4165SFrank Mandarino 
1611db2a4165SFrank Mandarino /**
1612db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1613db2a4165SFrank Mandarino  * @_template: control template
1614db2a4165SFrank Mandarino  * @data: control private data
1615ac11a2b3SMark Brown  * @long_name: control long name
1616db2a4165SFrank Mandarino  *
1617db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1618db2a4165SFrank Mandarino  *
1619db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1620db2a4165SFrank Mandarino  */
1621db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1622db2a4165SFrank Mandarino 	void *data, char *long_name)
1623db2a4165SFrank Mandarino {
1624db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1625db2a4165SFrank Mandarino 
1626db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1627db2a4165SFrank Mandarino 	if (long_name)
1628db2a4165SFrank Mandarino 		template.name = long_name;
1629db2a4165SFrank Mandarino 	template.index = 0;
1630db2a4165SFrank Mandarino 
1631db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1632db2a4165SFrank Mandarino }
1633db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1634db2a4165SFrank Mandarino 
1635db2a4165SFrank Mandarino /**
16363e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
16373e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
16383e8e1952SIan Molton  * duplicating this code.
16393e8e1952SIan Molton  *
16403e8e1952SIan Molton  * @codec: codec to add controls to
16413e8e1952SIan Molton  * @controls: array of controls to add
16423e8e1952SIan Molton  * @num_controls: number of elements in the array
16433e8e1952SIan Molton  *
16443e8e1952SIan Molton  * Return 0 for success, else error.
16453e8e1952SIan Molton  */
16463e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
16473e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
16483e8e1952SIan Molton {
16493e8e1952SIan Molton 	struct snd_card *card = codec->card;
16503e8e1952SIan Molton 	int err, i;
16513e8e1952SIan Molton 
16523e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
16533e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
16543e8e1952SIan Molton 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
16553e8e1952SIan Molton 		if (err < 0) {
16563e8e1952SIan Molton 			dev_err(codec->dev, "%s: Failed to add %s\n",
16573e8e1952SIan Molton 				codec->name, control->name);
16583e8e1952SIan Molton 			return err;
16593e8e1952SIan Molton 		}
16603e8e1952SIan Molton 	}
16613e8e1952SIan Molton 
16623e8e1952SIan Molton 	return 0;
16633e8e1952SIan Molton }
16643e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
16653e8e1952SIan Molton 
16663e8e1952SIan Molton /**
1667db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1668db2a4165SFrank Mandarino  * @kcontrol: mixer control
1669db2a4165SFrank Mandarino  * @uinfo: control element information
1670db2a4165SFrank Mandarino  *
1671db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
1672db2a4165SFrank Mandarino  * mixer control.
1673db2a4165SFrank Mandarino  *
1674db2a4165SFrank Mandarino  * Returns 0 for success.
1675db2a4165SFrank Mandarino  */
1676db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1677db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1678db2a4165SFrank Mandarino {
1679db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1680db2a4165SFrank Mandarino 
1681db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1682db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1683f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1684db2a4165SFrank Mandarino 
1685f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1686f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1687db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1688db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1689db2a4165SFrank Mandarino 	return 0;
1690db2a4165SFrank Mandarino }
1691db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1692db2a4165SFrank Mandarino 
1693db2a4165SFrank Mandarino /**
1694db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
1695db2a4165SFrank Mandarino  * @kcontrol: mixer control
1696ac11a2b3SMark Brown  * @ucontrol: control element information
1697db2a4165SFrank Mandarino  *
1698db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
1699db2a4165SFrank Mandarino  *
1700db2a4165SFrank Mandarino  * Returns 0 for success.
1701db2a4165SFrank Mandarino  */
1702db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1703db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1704db2a4165SFrank Mandarino {
1705db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1706db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
170746f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
1708db2a4165SFrank Mandarino 
1709f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1710db2a4165SFrank Mandarino 		;
1711db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
17123ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
17133ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
1714db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
1715db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
1716db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
1717db2a4165SFrank Mandarino 
1718db2a4165SFrank Mandarino 	return 0;
1719db2a4165SFrank Mandarino }
1720db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1721db2a4165SFrank Mandarino 
1722db2a4165SFrank Mandarino /**
1723db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
1724db2a4165SFrank Mandarino  * @kcontrol: mixer control
1725ac11a2b3SMark Brown  * @ucontrol: control element information
1726db2a4165SFrank Mandarino  *
1727db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
1728db2a4165SFrank Mandarino  *
1729db2a4165SFrank Mandarino  * Returns 0 for success.
1730db2a4165SFrank Mandarino  */
1731db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1732db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1733db2a4165SFrank Mandarino {
1734db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1735db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
173646f5822fSDaniel Ribeiro 	unsigned int val;
173746f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
1738db2a4165SFrank Mandarino 
1739f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1740db2a4165SFrank Mandarino 		;
1741f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1742db2a4165SFrank Mandarino 		return -EINVAL;
1743db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1744db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
1745db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
1746f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1747db2a4165SFrank Mandarino 			return -EINVAL;
1748db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1749db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
1750db2a4165SFrank Mandarino 	}
1751db2a4165SFrank Mandarino 
17526c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1753db2a4165SFrank Mandarino }
1754db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1755db2a4165SFrank Mandarino 
1756db2a4165SFrank Mandarino /**
17572e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
17582e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
17592e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
17602e72f8e3SPeter Ujfalusi  *
17612e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
17622e72f8e3SPeter Ujfalusi  *
17632e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
17642e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
17652e72f8e3SPeter Ujfalusi  *
17662e72f8e3SPeter Ujfalusi  * Returns 0 for success.
17672e72f8e3SPeter Ujfalusi  */
17682e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
17692e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
17702e72f8e3SPeter Ujfalusi {
17712e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
177274155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
177346f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
17742e72f8e3SPeter Ujfalusi 
17752e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
17762e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
17772e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
17782e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
17792e72f8e3SPeter Ujfalusi 			break;
17802e72f8e3SPeter Ujfalusi 	}
17812e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
17822e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
17832e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
17842e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
17852e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
17862e72f8e3SPeter Ujfalusi 				break;
17872e72f8e3SPeter Ujfalusi 		}
17882e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
17892e72f8e3SPeter Ujfalusi 	}
17902e72f8e3SPeter Ujfalusi 
17912e72f8e3SPeter Ujfalusi 	return 0;
17922e72f8e3SPeter Ujfalusi }
17932e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
17942e72f8e3SPeter Ujfalusi 
17952e72f8e3SPeter Ujfalusi /**
17962e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
17972e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
17982e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
17992e72f8e3SPeter Ujfalusi  *
18002e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
18012e72f8e3SPeter Ujfalusi  *
18022e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
18032e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
18042e72f8e3SPeter Ujfalusi  *
18052e72f8e3SPeter Ujfalusi  * Returns 0 for success.
18062e72f8e3SPeter Ujfalusi  */
18072e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
18082e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
18092e72f8e3SPeter Ujfalusi {
18102e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
181174155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
181246f5822fSDaniel Ribeiro 	unsigned int val;
181346f5822fSDaniel Ribeiro 	unsigned int mask;
18142e72f8e3SPeter Ujfalusi 
18152e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
18162e72f8e3SPeter Ujfalusi 		return -EINVAL;
18172e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
18182e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
18192e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
18202e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
18212e72f8e3SPeter Ujfalusi 			return -EINVAL;
18222e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
18232e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
18242e72f8e3SPeter Ujfalusi 	}
18252e72f8e3SPeter Ujfalusi 
18266c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
18272e72f8e3SPeter Ujfalusi }
18282e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
18292e72f8e3SPeter Ujfalusi 
18302e72f8e3SPeter Ujfalusi /**
1831db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1832db2a4165SFrank Mandarino  * @kcontrol: mixer control
1833db2a4165SFrank Mandarino  * @uinfo: control element information
1834db2a4165SFrank Mandarino  *
1835db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
1836db2a4165SFrank Mandarino  * single mixer.
1837db2a4165SFrank Mandarino  *
1838db2a4165SFrank Mandarino  * Returns 0 for success.
1839db2a4165SFrank Mandarino  */
1840db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1841db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1842db2a4165SFrank Mandarino {
1843db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1844db2a4165SFrank Mandarino 
1845db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1846db2a4165SFrank Mandarino 	uinfo->count = 1;
1847f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1848db2a4165SFrank Mandarino 
1849f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1850f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1851db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1852db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1853db2a4165SFrank Mandarino 	return 0;
1854db2a4165SFrank Mandarino }
1855db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1856db2a4165SFrank Mandarino 
1857db2a4165SFrank Mandarino /**
1858db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
1859db2a4165SFrank Mandarino  * @kcontrol: mixer control
1860db2a4165SFrank Mandarino  * @uinfo: control element information
1861db2a4165SFrank Mandarino  *
1862db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
1863db2a4165SFrank Mandarino  *
1864db2a4165SFrank Mandarino  * Returns 0 for success.
1865db2a4165SFrank Mandarino  */
1866db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1867db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1868db2a4165SFrank Mandarino {
1869a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
1870db2a4165SFrank Mandarino 
1871fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1872a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1873a7a4ac86SPhilipp Zabel 	else
1874a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1875a7a4ac86SPhilipp Zabel 
1876db2a4165SFrank Mandarino 	uinfo->count = 1;
1877db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1878a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1879db2a4165SFrank Mandarino 	return 0;
1880db2a4165SFrank Mandarino }
1881db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1882db2a4165SFrank Mandarino 
1883db2a4165SFrank Mandarino /**
1884db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
1885db2a4165SFrank Mandarino  * @kcontrol: mixer control
1886db2a4165SFrank Mandarino  * @uinfo: control element information
1887db2a4165SFrank Mandarino  *
1888db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
1889db2a4165SFrank Mandarino  *
1890db2a4165SFrank Mandarino  * Returns 0 for success.
1891db2a4165SFrank Mandarino  */
1892db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1893db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1894db2a4165SFrank Mandarino {
18954eaa9819SJon Smirl 	struct soc_mixer_control *mc =
18964eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
18974eaa9819SJon Smirl 	int max = mc->max;
1898762b8df7SMark Brown 	unsigned int shift = mc->shift;
1899815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
1900db2a4165SFrank Mandarino 
1901fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1902a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1903a7a4ac86SPhilipp Zabel 	else
1904a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1905a7a4ac86SPhilipp Zabel 
1906db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
1907db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1908a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
1909db2a4165SFrank Mandarino 	return 0;
1910db2a4165SFrank Mandarino }
1911db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1912db2a4165SFrank Mandarino 
1913db2a4165SFrank Mandarino /**
1914db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
1915db2a4165SFrank Mandarino  * @kcontrol: mixer control
1916ac11a2b3SMark Brown  * @ucontrol: control element information
1917db2a4165SFrank Mandarino  *
1918db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
1919db2a4165SFrank Mandarino  *
1920db2a4165SFrank Mandarino  * Returns 0 for success.
1921db2a4165SFrank Mandarino  */
1922db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1923db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1924db2a4165SFrank Mandarino {
19254eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19264eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1927db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1928815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1929815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1930815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
19314eaa9819SJon Smirl 	int max = mc->max;
1932815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1933815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
1934db2a4165SFrank Mandarino 
1935db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1936db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1937db2a4165SFrank Mandarino 	if (shift != rshift)
1938db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1939db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
1940db2a4165SFrank Mandarino 	if (invert) {
1941db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1942a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
1943db2a4165SFrank Mandarino 		if (shift != rshift)
1944db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
1945a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
1946db2a4165SFrank Mandarino 	}
1947db2a4165SFrank Mandarino 
1948db2a4165SFrank Mandarino 	return 0;
1949db2a4165SFrank Mandarino }
1950db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1951db2a4165SFrank Mandarino 
1952db2a4165SFrank Mandarino /**
1953db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
1954db2a4165SFrank Mandarino  * @kcontrol: mixer control
1955ac11a2b3SMark Brown  * @ucontrol: control element information
1956db2a4165SFrank Mandarino  *
1957db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
1958db2a4165SFrank Mandarino  *
1959db2a4165SFrank Mandarino  * Returns 0 for success.
1960db2a4165SFrank Mandarino  */
1961db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1962db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1963db2a4165SFrank Mandarino {
19644eaa9819SJon Smirl 	struct soc_mixer_control *mc =
19654eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
1966db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1967815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
1968815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
1969815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
19704eaa9819SJon Smirl 	int max = mc->max;
1971815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
1972815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
197346f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
1974db2a4165SFrank Mandarino 
1975db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
1976db2a4165SFrank Mandarino 	if (invert)
1977a7a4ac86SPhilipp Zabel 		val = max - val;
1978db2a4165SFrank Mandarino 	val_mask = mask << shift;
1979db2a4165SFrank Mandarino 	val = val << shift;
1980db2a4165SFrank Mandarino 	if (shift != rshift) {
1981db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
1982db2a4165SFrank Mandarino 		if (invert)
1983a7a4ac86SPhilipp Zabel 			val2 = max - val2;
1984db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
1985db2a4165SFrank Mandarino 		val |= val2 << rshift;
1986db2a4165SFrank Mandarino 	}
19876c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
1988db2a4165SFrank Mandarino }
1989db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1990db2a4165SFrank Mandarino 
1991db2a4165SFrank Mandarino /**
1992db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
1993db2a4165SFrank Mandarino  * @kcontrol: mixer control
1994db2a4165SFrank Mandarino  * @uinfo: control element information
1995db2a4165SFrank Mandarino  *
1996db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
1997db2a4165SFrank Mandarino  * spans 2 codec registers.
1998db2a4165SFrank Mandarino  *
1999db2a4165SFrank Mandarino  * Returns 0 for success.
2000db2a4165SFrank Mandarino  */
2001db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2002db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2003db2a4165SFrank Mandarino {
20044eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20054eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
20064eaa9819SJon Smirl 	int max = mc->max;
2007db2a4165SFrank Mandarino 
2008fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2009a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2010a7a4ac86SPhilipp Zabel 	else
2011a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2012a7a4ac86SPhilipp Zabel 
2013db2a4165SFrank Mandarino 	uinfo->count = 2;
2014db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2015a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2016db2a4165SFrank Mandarino 	return 0;
2017db2a4165SFrank Mandarino }
2018db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2019db2a4165SFrank Mandarino 
2020db2a4165SFrank Mandarino /**
2021db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2022db2a4165SFrank Mandarino  * @kcontrol: mixer control
2023ac11a2b3SMark Brown  * @ucontrol: control element information
2024db2a4165SFrank Mandarino  *
2025db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2026db2a4165SFrank Mandarino  *
2027db2a4165SFrank Mandarino  * Returns 0 for success.
2028db2a4165SFrank Mandarino  */
2029db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2030db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2031db2a4165SFrank Mandarino {
20324eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20334eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2034db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2035815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2036815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2037815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
20384eaa9819SJon Smirl 	int max = mc->max;
2039815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2040815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2041db2a4165SFrank Mandarino 
2042db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2043db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2044db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2045db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2046db2a4165SFrank Mandarino 	if (invert) {
2047db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2048a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2049db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2050a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2051db2a4165SFrank Mandarino 	}
2052db2a4165SFrank Mandarino 
2053db2a4165SFrank Mandarino 	return 0;
2054db2a4165SFrank Mandarino }
2055db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2056db2a4165SFrank Mandarino 
2057db2a4165SFrank Mandarino /**
2058db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2059db2a4165SFrank Mandarino  * @kcontrol: mixer control
2060ac11a2b3SMark Brown  * @ucontrol: control element information
2061db2a4165SFrank Mandarino  *
2062db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2063db2a4165SFrank Mandarino  *
2064db2a4165SFrank Mandarino  * Returns 0 for success.
2065db2a4165SFrank Mandarino  */
2066db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2067db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2068db2a4165SFrank Mandarino {
20694eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20704eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2071db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2072815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2073815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2074815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
20754eaa9819SJon Smirl 	int max = mc->max;
2076815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2077815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2078db2a4165SFrank Mandarino 	int err;
207946f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2080db2a4165SFrank Mandarino 
2081db2a4165SFrank Mandarino 	val_mask = mask << shift;
2082db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2083db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2084db2a4165SFrank Mandarino 
2085db2a4165SFrank Mandarino 	if (invert) {
2086a7a4ac86SPhilipp Zabel 		val = max - val;
2087a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2088db2a4165SFrank Mandarino 	}
2089db2a4165SFrank Mandarino 
2090db2a4165SFrank Mandarino 	val = val << shift;
2091db2a4165SFrank Mandarino 	val2 = val2 << shift;
2092db2a4165SFrank Mandarino 
20936c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
20943ff3f64bSMark Brown 	if (err < 0)
2095db2a4165SFrank Mandarino 		return err;
2096db2a4165SFrank Mandarino 
20976c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2098db2a4165SFrank Mandarino 	return err;
2099db2a4165SFrank Mandarino }
2100db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2101db2a4165SFrank Mandarino 
2102e13ac2e9SMark Brown /**
2103e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2104e13ac2e9SMark Brown  * @kcontrol: mixer control
2105e13ac2e9SMark Brown  * @uinfo: control element information
2106e13ac2e9SMark Brown  *
2107e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2108e13ac2e9SMark Brown  *
2109e13ac2e9SMark Brown  * Returns 0 for success.
2110e13ac2e9SMark Brown  */
2111e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2112e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2113e13ac2e9SMark Brown {
21144eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21154eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
21164eaa9819SJon Smirl 	int max = mc->max;
21174eaa9819SJon Smirl 	int min = mc->min;
2118e13ac2e9SMark Brown 
2119e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2120e13ac2e9SMark Brown 	uinfo->count = 2;
2121e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2122e13ac2e9SMark Brown 	uinfo->value.integer.max = max-min;
2123e13ac2e9SMark Brown 	return 0;
2124e13ac2e9SMark Brown }
2125e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2126e13ac2e9SMark Brown 
2127e13ac2e9SMark Brown /**
2128e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2129e13ac2e9SMark Brown  * @kcontrol: mixer control
2130ac11a2b3SMark Brown  * @ucontrol: control element information
2131e13ac2e9SMark Brown  *
2132e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2133e13ac2e9SMark Brown  *
2134e13ac2e9SMark Brown  * Returns 0 for success.
2135e13ac2e9SMark Brown  */
2136e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2137e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2138e13ac2e9SMark Brown {
21394eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21404eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2141e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2142815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
21434eaa9819SJon Smirl 	int min = mc->min;
2144e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2145e13ac2e9SMark Brown 
2146e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2147e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2148e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2149e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2150e13ac2e9SMark Brown 	return 0;
2151e13ac2e9SMark Brown }
2152e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2153e13ac2e9SMark Brown 
2154e13ac2e9SMark Brown /**
2155e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2156e13ac2e9SMark Brown  * @kcontrol: mixer control
2157ac11a2b3SMark Brown  * @ucontrol: control element information
2158e13ac2e9SMark Brown  *
2159e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2160e13ac2e9SMark Brown  *
2161e13ac2e9SMark Brown  * Returns 0 for success.
2162e13ac2e9SMark Brown  */
2163e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2164e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2165e13ac2e9SMark Brown {
21664eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21674eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2168e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2169815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
21704eaa9819SJon Smirl 	int min = mc->min;
217146f5822fSDaniel Ribeiro 	unsigned int val;
2172e13ac2e9SMark Brown 
2173e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2174e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2175e13ac2e9SMark Brown 
21766c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2177e13ac2e9SMark Brown }
2178e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2179e13ac2e9SMark Brown 
21808c6529dbSLiam Girdwood /**
21818c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
21828c6529dbSLiam Girdwood  * @dai: DAI
21838c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
21848c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
21858c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
21868c6529dbSLiam Girdwood  *
21878c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
21888c6529dbSLiam Girdwood  */
21898c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
21908c6529dbSLiam Girdwood 	unsigned int freq, int dir)
21918c6529dbSLiam Girdwood {
21923f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_sysclk)
21936335d055SEric Miao 		return dai->ops->set_sysclk(dai, clk_id, freq, dir);
21948c6529dbSLiam Girdwood 	else
21958c6529dbSLiam Girdwood 		return -EINVAL;
21968c6529dbSLiam Girdwood }
21978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
21988c6529dbSLiam Girdwood 
21998c6529dbSLiam Girdwood /**
22008c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
22018c6529dbSLiam Girdwood  * @dai: DAI
2202ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
22038c6529dbSLiam Girdwood  * @div: new clock divisor.
22048c6529dbSLiam Girdwood  *
22058c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
22068c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
22078c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
22088c6529dbSLiam Girdwood  */
22098c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
22108c6529dbSLiam Girdwood 	int div_id, int div)
22118c6529dbSLiam Girdwood {
22123f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_clkdiv)
22136335d055SEric Miao 		return dai->ops->set_clkdiv(dai, div_id, div);
22148c6529dbSLiam Girdwood 	else
22158c6529dbSLiam Girdwood 		return -EINVAL;
22168c6529dbSLiam Girdwood }
22178c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
22188c6529dbSLiam Girdwood 
22198c6529dbSLiam Girdwood /**
22208c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
22218c6529dbSLiam Girdwood  * @dai: DAI
22228c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
222385488037SMark Brown  * @source: DAI specific source for the PLL
22248c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
22258c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
22268c6529dbSLiam Girdwood  *
22278c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
22288c6529dbSLiam Girdwood  */
222985488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
223085488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
22318c6529dbSLiam Girdwood {
22323f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_pll)
223385488037SMark Brown 		return dai->ops->set_pll(dai, pll_id, source,
223485488037SMark Brown 					 freq_in, freq_out);
22358c6529dbSLiam Girdwood 	else
22368c6529dbSLiam Girdwood 		return -EINVAL;
22378c6529dbSLiam Girdwood }
22388c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
22398c6529dbSLiam Girdwood 
22408c6529dbSLiam Girdwood /**
22418c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
22428c6529dbSLiam Girdwood  * @dai: DAI
22438c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
22448c6529dbSLiam Girdwood  *
22458c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
22468c6529dbSLiam Girdwood  */
22478c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
22488c6529dbSLiam Girdwood {
22493f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_fmt)
22506335d055SEric Miao 		return dai->ops->set_fmt(dai, fmt);
22518c6529dbSLiam Girdwood 	else
22528c6529dbSLiam Girdwood 		return -EINVAL;
22538c6529dbSLiam Girdwood }
22548c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
22558c6529dbSLiam Girdwood 
22568c6529dbSLiam Girdwood /**
22578c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
22588c6529dbSLiam Girdwood  * @dai: DAI
2259a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2260a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
22618c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2262a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
22638c6529dbSLiam Girdwood  *
22648c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
22658c6529dbSLiam Girdwood  * specific.
22668c6529dbSLiam Girdwood  */
22678c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2268a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
22698c6529dbSLiam Girdwood {
22703f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tdm_slot)
2271a5479e38SDaniel Ribeiro 		return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2272a5479e38SDaniel Ribeiro 				slots, slot_width);
22738c6529dbSLiam Girdwood 	else
22748c6529dbSLiam Girdwood 		return -EINVAL;
22758c6529dbSLiam Girdwood }
22768c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
22778c6529dbSLiam Girdwood 
22788c6529dbSLiam Girdwood /**
2279472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2280472df3cbSBarry Song  * @dai: DAI
2281472df3cbSBarry Song  * @tx_num: how many TX channels
2282472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2283472df3cbSBarry Song  *           0~num-1 uses
2284472df3cbSBarry Song  * @rx_num: how many RX channels
2285472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2286472df3cbSBarry Song  *           0~num-1 uses
2287472df3cbSBarry Song  *
2288472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2289472df3cbSBarry Song  */
2290472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2291472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2292472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2293472df3cbSBarry Song {
2294472df3cbSBarry Song 	if (dai->ops && dai->ops->set_channel_map)
2295472df3cbSBarry Song 		return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2296472df3cbSBarry Song 			rx_num, rx_slot);
2297472df3cbSBarry Song 	else
2298472df3cbSBarry Song 		return -EINVAL;
2299472df3cbSBarry Song }
2300472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2301472df3cbSBarry Song 
2302472df3cbSBarry Song /**
23038c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
23048c6529dbSLiam Girdwood  * @dai: DAI
23058c6529dbSLiam Girdwood  * @tristate: tristate enable
23068c6529dbSLiam Girdwood  *
23078c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
23088c6529dbSLiam Girdwood  */
23098c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
23108c6529dbSLiam Girdwood {
23113f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tristate)
23126335d055SEric Miao 		return dai->ops->set_tristate(dai, tristate);
23138c6529dbSLiam Girdwood 	else
23148c6529dbSLiam Girdwood 		return -EINVAL;
23158c6529dbSLiam Girdwood }
23168c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
23178c6529dbSLiam Girdwood 
23188c6529dbSLiam Girdwood /**
23198c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
23208c6529dbSLiam Girdwood  * @dai: DAI
23218c6529dbSLiam Girdwood  * @mute: mute enable
23228c6529dbSLiam Girdwood  *
23238c6529dbSLiam Girdwood  * Mutes the DAI DAC.
23248c6529dbSLiam Girdwood  */
23258c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
23268c6529dbSLiam Girdwood {
23273f1a4d82SMark Brown 	if (dai->ops && dai->ops->digital_mute)
23286335d055SEric Miao 		return dai->ops->digital_mute(dai, mute);
23298c6529dbSLiam Girdwood 	else
23308c6529dbSLiam Girdwood 		return -EINVAL;
23318c6529dbSLiam Girdwood }
23328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
23338c6529dbSLiam Girdwood 
2334c5af3a2eSMark Brown /**
2335c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2336c5af3a2eSMark Brown  *
2337ac11a2b3SMark Brown  * @card: Card to register
2338c5af3a2eSMark Brown  *
2339c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2340c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2341c5af3a2eSMark Brown  * registration APIs.
2342c5af3a2eSMark Brown  */
2343c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
2344c5af3a2eSMark Brown {
2345c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2346c5af3a2eSMark Brown 		return -EINVAL;
2347c5af3a2eSMark Brown 
2348c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2349c5af3a2eSMark Brown 	card->instantiated = 0;
2350c5af3a2eSMark Brown 
2351c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2352c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2353435c5e25SMark Brown 	snd_soc_instantiate_cards();
2354c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2355c5af3a2eSMark Brown 
2356c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2357c5af3a2eSMark Brown 
2358c5af3a2eSMark Brown 	return 0;
2359c5af3a2eSMark Brown }
2360c5af3a2eSMark Brown 
2361c5af3a2eSMark Brown /**
2362c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2363c5af3a2eSMark Brown  *
2364ac11a2b3SMark Brown  * @card: Card to unregister
2365c5af3a2eSMark Brown  *
2366c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2367c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2368c5af3a2eSMark Brown  * registration APIs.
2369c5af3a2eSMark Brown  */
2370c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
2371c5af3a2eSMark Brown {
2372c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2373c5af3a2eSMark Brown 	list_del(&card->list);
2374c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2375c5af3a2eSMark Brown 
2376c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2377c5af3a2eSMark Brown 
2378c5af3a2eSMark Brown 	return 0;
2379c5af3a2eSMark Brown }
2380c5af3a2eSMark Brown 
23819115171aSMark Brown /**
23829115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
23839115171aSMark Brown  *
2384ac11a2b3SMark Brown  * @dai: DAI to register
23859115171aSMark Brown  */
23869115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai)
23879115171aSMark Brown {
23889115171aSMark Brown 	if (!dai->name)
23899115171aSMark Brown 		return -EINVAL;
23909115171aSMark Brown 
23919115171aSMark Brown 	/* The device should become mandatory over time */
23929115171aSMark Brown 	if (!dai->dev)
23939115171aSMark Brown 		printk(KERN_WARNING "No device for DAI %s\n", dai->name);
23949115171aSMark Brown 
23956335d055SEric Miao 	if (!dai->ops)
23966335d055SEric Miao 		dai->ops = &null_dai_ops;
23976335d055SEric Miao 
23989115171aSMark Brown 	INIT_LIST_HEAD(&dai->list);
23999115171aSMark Brown 
24009115171aSMark Brown 	mutex_lock(&client_mutex);
24019115171aSMark Brown 	list_add(&dai->list, &dai_list);
2402435c5e25SMark Brown 	snd_soc_instantiate_cards();
24039115171aSMark Brown 	mutex_unlock(&client_mutex);
24049115171aSMark Brown 
24059115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
24069115171aSMark Brown 
24079115171aSMark Brown 	return 0;
24089115171aSMark Brown }
24099115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
24109115171aSMark Brown 
24119115171aSMark Brown /**
24129115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
24139115171aSMark Brown  *
2414ac11a2b3SMark Brown  * @dai: DAI to unregister
24159115171aSMark Brown  */
24169115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai)
24179115171aSMark Brown {
24189115171aSMark Brown 	mutex_lock(&client_mutex);
24199115171aSMark Brown 	list_del(&dai->list);
24209115171aSMark Brown 	mutex_unlock(&client_mutex);
24219115171aSMark Brown 
24229115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
24239115171aSMark Brown }
24249115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
24259115171aSMark Brown 
24269115171aSMark Brown /**
24279115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
24289115171aSMark Brown  *
2429ac11a2b3SMark Brown  * @dai: Array of DAIs to register
2430ac11a2b3SMark Brown  * @count: Number of DAIs
24319115171aSMark Brown  */
24329115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
24339115171aSMark Brown {
24349115171aSMark Brown 	int i, ret;
24359115171aSMark Brown 
24369115171aSMark Brown 	for (i = 0; i < count; i++) {
24379115171aSMark Brown 		ret = snd_soc_register_dai(&dai[i]);
24389115171aSMark Brown 		if (ret != 0)
24399115171aSMark Brown 			goto err;
24409115171aSMark Brown 	}
24419115171aSMark Brown 
24429115171aSMark Brown 	return 0;
24439115171aSMark Brown 
24449115171aSMark Brown err:
24459115171aSMark Brown 	for (i--; i >= 0; i--)
24469115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
24479115171aSMark Brown 
24489115171aSMark Brown 	return ret;
24499115171aSMark Brown }
24509115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
24519115171aSMark Brown 
24529115171aSMark Brown /**
24539115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
24549115171aSMark Brown  *
2455ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
2456ac11a2b3SMark Brown  * @count: Number of DAIs
24579115171aSMark Brown  */
24589115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
24599115171aSMark Brown {
24609115171aSMark Brown 	int i;
24619115171aSMark Brown 
24629115171aSMark Brown 	for (i = 0; i < count; i++)
24639115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
24649115171aSMark Brown }
24659115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
24669115171aSMark Brown 
246712a48a8cSMark Brown /**
246812a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
246912a48a8cSMark Brown  *
2470ac11a2b3SMark Brown  * @platform: platform to register
247112a48a8cSMark Brown  */
247212a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform)
247312a48a8cSMark Brown {
247412a48a8cSMark Brown 	if (!platform->name)
247512a48a8cSMark Brown 		return -EINVAL;
247612a48a8cSMark Brown 
247712a48a8cSMark Brown 	INIT_LIST_HEAD(&platform->list);
247812a48a8cSMark Brown 
247912a48a8cSMark Brown 	mutex_lock(&client_mutex);
248012a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
2481435c5e25SMark Brown 	snd_soc_instantiate_cards();
248212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
248312a48a8cSMark Brown 
248412a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
248512a48a8cSMark Brown 
248612a48a8cSMark Brown 	return 0;
248712a48a8cSMark Brown }
248812a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
248912a48a8cSMark Brown 
249012a48a8cSMark Brown /**
249112a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
249212a48a8cSMark Brown  *
2493ac11a2b3SMark Brown  * @platform: platform to unregister
249412a48a8cSMark Brown  */
249512a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform)
249612a48a8cSMark Brown {
249712a48a8cSMark Brown 	mutex_lock(&client_mutex);
249812a48a8cSMark Brown 	list_del(&platform->list);
249912a48a8cSMark Brown 	mutex_unlock(&client_mutex);
250012a48a8cSMark Brown 
250112a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
250212a48a8cSMark Brown }
250312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
250412a48a8cSMark Brown 
2505151ab22cSMark Brown static u64 codec_format_map[] = {
2506151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2507151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2508151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2509151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2510151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2511151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2512151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2513151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2514151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2515151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2516151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2517151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2518151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2519151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2520151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2521151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2522151ab22cSMark Brown };
2523151ab22cSMark Brown 
2524151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2525151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2526151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2527151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2528151ab22cSMark Brown  */
2529151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2530151ab22cSMark Brown {
2531151ab22cSMark Brown 	int i;
2532151ab22cSMark Brown 
2533151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2534151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2535151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
2536151ab22cSMark Brown }
2537151ab22cSMark Brown 
25380d0cf00aSMark Brown /**
25390d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
25400d0cf00aSMark Brown  *
2541ac11a2b3SMark Brown  * @codec: codec to register
25420d0cf00aSMark Brown  */
25430d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec)
25440d0cf00aSMark Brown {
2545151ab22cSMark Brown 	int i;
2546151ab22cSMark Brown 
25470d0cf00aSMark Brown 	if (!codec->name)
25480d0cf00aSMark Brown 		return -EINVAL;
25490d0cf00aSMark Brown 
25500d0cf00aSMark Brown 	/* The device should become mandatory over time */
25510d0cf00aSMark Brown 	if (!codec->dev)
25520d0cf00aSMark Brown 		printk(KERN_WARNING "No device for codec %s\n", codec->name);
25530d0cf00aSMark Brown 
25540d0cf00aSMark Brown 	INIT_LIST_HEAD(&codec->list);
25550d0cf00aSMark Brown 
2556151ab22cSMark Brown 	for (i = 0; i < codec->num_dai; i++) {
2557151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].playback);
2558151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].capture);
2559151ab22cSMark Brown 	}
2560151ab22cSMark Brown 
25610d0cf00aSMark Brown 	mutex_lock(&client_mutex);
25620d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
25630d0cf00aSMark Brown 	snd_soc_instantiate_cards();
25640d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
25650d0cf00aSMark Brown 
25660d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
25670d0cf00aSMark Brown 
25680d0cf00aSMark Brown 	return 0;
25690d0cf00aSMark Brown }
25700d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
25710d0cf00aSMark Brown 
25720d0cf00aSMark Brown /**
25730d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
25740d0cf00aSMark Brown  *
2575ac11a2b3SMark Brown  * @codec: codec to unregister
25760d0cf00aSMark Brown  */
25770d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec)
25780d0cf00aSMark Brown {
25790d0cf00aSMark Brown 	mutex_lock(&client_mutex);
25800d0cf00aSMark Brown 	list_del(&codec->list);
25810d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
25820d0cf00aSMark Brown 
25830d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
25840d0cf00aSMark Brown }
25850d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
25860d0cf00aSMark Brown 
2587c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
2588db2a4165SFrank Mandarino {
2589384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2590384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
2591384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
2592384c89e2SMark Brown 		printk(KERN_WARNING
2593384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
2594384c89e2SMark Brown 		debugfs_root = NULL;
2595384c89e2SMark Brown 	}
2596384c89e2SMark Brown #endif
2597384c89e2SMark Brown 
2598db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
2599db2a4165SFrank Mandarino }
2600db2a4165SFrank Mandarino 
26017d8c16a6SMark Brown static void __exit snd_soc_exit(void)
2602db2a4165SFrank Mandarino {
2603384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2604384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
2605384c89e2SMark Brown #endif
2606db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
2607db2a4165SFrank Mandarino }
2608db2a4165SFrank Mandarino 
2609db2a4165SFrank Mandarino module_init(snd_soc_init);
2610db2a4165SFrank Mandarino module_exit(snd_soc_exit);
2611db2a4165SFrank Mandarino 
2612db2a4165SFrank Mandarino /* Module information */
2613d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2614db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
2615db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
26168b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
2617