xref: /openbmc/linux/sound/soc/soc-core.c (revision 5164d74d74447895aaa31c094a1b9e666acaa656)
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>
315a0e3ad6STejun Heo #include <linux/slab.h>
32474828a4SMarek Vasut #include <sound/ac97_codec.h>
33db2a4165SFrank Mandarino #include <sound/core.h>
34db2a4165SFrank Mandarino #include <sound/pcm.h>
35db2a4165SFrank Mandarino #include <sound/pcm_params.h>
36db2a4165SFrank Mandarino #include <sound/soc.h>
37db2a4165SFrank Mandarino #include <sound/soc-dapm.h>
38db2a4165SFrank Mandarino #include <sound/initval.h>
39db2a4165SFrank Mandarino 
40db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex);
41db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42db2a4165SFrank Mandarino 
43384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
44384c89e2SMark Brown static struct dentry *debugfs_root;
45384c89e2SMark Brown #endif
46384c89e2SMark Brown 
47c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
48c5af3a2eSMark Brown static LIST_HEAD(card_list);
499115171aSMark Brown static LIST_HEAD(dai_list);
5012a48a8cSMark Brown static LIST_HEAD(platform_list);
510d0cf00aSMark Brown static LIST_HEAD(codec_list);
52c5af3a2eSMark Brown 
53c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card);
54c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card);
55c5af3a2eSMark Brown 
56db2a4165SFrank Mandarino /*
57db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
58db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
59db2a4165SFrank Mandarino  * between two audio tracks.
60db2a4165SFrank Mandarino  */
61db2a4165SFrank Mandarino static int pmdown_time = 5000;
62db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
63db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64db2a4165SFrank Mandarino 
65965ac42cSLiam Girdwood /*
66965ac42cSLiam Girdwood  * This function forces any delayed work to be queued and run.
67965ac42cSLiam Girdwood  */
68965ac42cSLiam Girdwood static int run_delayed_work(struct delayed_work *dwork)
69965ac42cSLiam Girdwood {
70965ac42cSLiam Girdwood 	int ret;
71965ac42cSLiam Girdwood 
72965ac42cSLiam Girdwood 	/* cancel any work waiting to be queued. */
73965ac42cSLiam Girdwood 	ret = cancel_delayed_work(dwork);
74965ac42cSLiam Girdwood 
75965ac42cSLiam Girdwood 	/* if there was any work waiting then we run it now and
76965ac42cSLiam Girdwood 	 * wait for it's completion */
77965ac42cSLiam Girdwood 	if (ret) {
78965ac42cSLiam Girdwood 		schedule_delayed_work(dwork, 0);
79965ac42cSLiam Girdwood 		flush_scheduled_work();
80965ac42cSLiam Girdwood 	}
81965ac42cSLiam Girdwood 	return ret;
82965ac42cSLiam Girdwood }
83965ac42cSLiam Girdwood 
842624d5faSMark Brown /* codec register dump */
852624d5faSMark Brown static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
862624d5faSMark Brown {
87*5164d74dSMark Brown 	int ret, i, step = 1, count = 0;
882624d5faSMark Brown 
892624d5faSMark Brown 	if (!codec->reg_cache_size)
902624d5faSMark Brown 		return 0;
912624d5faSMark Brown 
922624d5faSMark Brown 	if (codec->reg_cache_step)
932624d5faSMark Brown 		step = codec->reg_cache_step;
942624d5faSMark Brown 
952624d5faSMark Brown 	count += sprintf(buf, "%s registers\n", codec->name);
962624d5faSMark Brown 	for (i = 0; i < codec->reg_cache_size; i += step) {
972624d5faSMark Brown 		if (codec->readable_register && !codec->readable_register(i))
982624d5faSMark Brown 			continue;
992624d5faSMark Brown 
1002624d5faSMark Brown 		count += sprintf(buf + count, "%2x: ", i);
1012624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1022624d5faSMark Brown 			break;
1032624d5faSMark Brown 
104*5164d74dSMark Brown 		if (codec->display_register) {
1052624d5faSMark Brown 			count += codec->display_register(codec, buf + count,
1062624d5faSMark Brown 							 PAGE_SIZE - count, i);
107*5164d74dSMark Brown 		} else {
108*5164d74dSMark Brown 			/* If the read fails it's almost certainly due to
109*5164d74dSMark Brown 			 * the register being volatile and the device being
110*5164d74dSMark Brown 			 * powered off.
111*5164d74dSMark Brown 			 */
112*5164d74dSMark Brown 			ret = codec->read(codec, i);
113*5164d74dSMark Brown 			if (ret >= 0)
114*5164d74dSMark Brown 				count += snprintf(buf + count,
115*5164d74dSMark Brown 						  PAGE_SIZE - count,
116*5164d74dSMark Brown 						  "%4x", ret);
1172624d5faSMark Brown 			else
118*5164d74dSMark Brown 				count += snprintf(buf + count,
119*5164d74dSMark Brown 						  PAGE_SIZE - count,
120*5164d74dSMark Brown 						  "<no data: %d>", ret);
121*5164d74dSMark Brown 		}
1222624d5faSMark Brown 
1232624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1242624d5faSMark Brown 			break;
1252624d5faSMark Brown 
1262624d5faSMark Brown 		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1272624d5faSMark Brown 		if (count >= PAGE_SIZE - 1)
1282624d5faSMark Brown 			break;
1292624d5faSMark Brown 	}
1302624d5faSMark Brown 
1312624d5faSMark Brown 	/* Truncate count; min() would cause a warning */
1322624d5faSMark Brown 	if (count >= PAGE_SIZE)
1332624d5faSMark Brown 		count = PAGE_SIZE - 1;
1342624d5faSMark Brown 
1352624d5faSMark Brown 	return count;
1362624d5faSMark Brown }
1372624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev,
1382624d5faSMark Brown 	struct device_attribute *attr, char *buf)
1392624d5faSMark Brown {
1402624d5faSMark Brown 	struct snd_soc_device *devdata = dev_get_drvdata(dev);
1412624d5faSMark Brown 	return soc_codec_reg_show(devdata->card->codec, buf);
1422624d5faSMark Brown }
1432624d5faSMark Brown 
1442624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1452624d5faSMark Brown 
146dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
147dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
148dbe21408SMark Brown {
149dbe21408SMark Brown 	struct snd_soc_device *socdev = dev_get_drvdata(dev);
150dbe21408SMark Brown 	struct snd_soc_card *card = socdev->card;
151dbe21408SMark Brown 
1526c5f1fedSMark Brown 	return sprintf(buf, "%ld\n", card->pmdown_time);
153dbe21408SMark Brown }
154dbe21408SMark Brown 
155dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
156dbe21408SMark Brown 			       struct device_attribute *attr,
157dbe21408SMark Brown 			       const char *buf, size_t count)
158dbe21408SMark Brown {
159dbe21408SMark Brown 	struct snd_soc_device *socdev = dev_get_drvdata(dev);
160dbe21408SMark Brown 	struct snd_soc_card *card = socdev->card;
161dbe21408SMark Brown 
162dbe21408SMark Brown 	strict_strtol(buf, 10, &card->pmdown_time);
163dbe21408SMark Brown 
164dbe21408SMark Brown 	return count;
165dbe21408SMark Brown }
166dbe21408SMark Brown 
167dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
168dbe21408SMark Brown 
1692624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
1702624d5faSMark Brown static int codec_reg_open_file(struct inode *inode, struct file *file)
1712624d5faSMark Brown {
1722624d5faSMark Brown 	file->private_data = inode->i_private;
1732624d5faSMark Brown 	return 0;
1742624d5faSMark Brown }
1752624d5faSMark Brown 
1762624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1772624d5faSMark Brown 			       size_t count, loff_t *ppos)
1782624d5faSMark Brown {
1792624d5faSMark Brown 	ssize_t ret;
1802624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
1812624d5faSMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1822624d5faSMark Brown 	if (!buf)
1832624d5faSMark Brown 		return -ENOMEM;
1842624d5faSMark Brown 	ret = soc_codec_reg_show(codec, buf);
1852624d5faSMark Brown 	if (ret >= 0)
1862624d5faSMark Brown 		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1872624d5faSMark Brown 	kfree(buf);
1882624d5faSMark Brown 	return ret;
1892624d5faSMark Brown }
1902624d5faSMark Brown 
1912624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file,
1922624d5faSMark Brown 		const char __user *user_buf, size_t count, loff_t *ppos)
1932624d5faSMark Brown {
1942624d5faSMark Brown 	char buf[32];
1952624d5faSMark Brown 	int buf_size;
1962624d5faSMark Brown 	char *start = buf;
1972624d5faSMark Brown 	unsigned long reg, value;
1982624d5faSMark Brown 	int step = 1;
1992624d5faSMark Brown 	struct snd_soc_codec *codec = file->private_data;
2002624d5faSMark Brown 
2012624d5faSMark Brown 	buf_size = min(count, (sizeof(buf)-1));
2022624d5faSMark Brown 	if (copy_from_user(buf, user_buf, buf_size))
2032624d5faSMark Brown 		return -EFAULT;
2042624d5faSMark Brown 	buf[buf_size] = 0;
2052624d5faSMark Brown 
2062624d5faSMark Brown 	if (codec->reg_cache_step)
2072624d5faSMark Brown 		step = codec->reg_cache_step;
2082624d5faSMark Brown 
2092624d5faSMark Brown 	while (*start == ' ')
2102624d5faSMark Brown 		start++;
2112624d5faSMark Brown 	reg = simple_strtoul(start, &start, 16);
2122624d5faSMark Brown 	if ((reg >= codec->reg_cache_size) || (reg % step))
2132624d5faSMark Brown 		return -EINVAL;
2142624d5faSMark Brown 	while (*start == ' ')
2152624d5faSMark Brown 		start++;
2162624d5faSMark Brown 	if (strict_strtoul(start, 16, &value))
2172624d5faSMark Brown 		return -EINVAL;
2182624d5faSMark Brown 	codec->write(codec, reg, value);
2192624d5faSMark Brown 	return buf_size;
2202624d5faSMark Brown }
2212624d5faSMark Brown 
2222624d5faSMark Brown static const struct file_operations codec_reg_fops = {
2232624d5faSMark Brown 	.open = codec_reg_open_file,
2242624d5faSMark Brown 	.read = codec_reg_read_file,
2252624d5faSMark Brown 	.write = codec_reg_write_file,
2262624d5faSMark Brown };
2272624d5faSMark Brown 
2282624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2292624d5faSMark Brown {
2302624d5faSMark Brown 	char codec_root[128];
2312624d5faSMark Brown 
2322624d5faSMark Brown 	if (codec->dev)
2332624d5faSMark Brown 		snprintf(codec_root, sizeof(codec_root),
2342624d5faSMark Brown 			"%s.%s", codec->name, dev_name(codec->dev));
2352624d5faSMark Brown 	else
2362624d5faSMark Brown 		snprintf(codec_root, sizeof(codec_root),
2372624d5faSMark Brown 			"%s", codec->name);
2382624d5faSMark Brown 
2392624d5faSMark Brown 	codec->debugfs_codec_root = debugfs_create_dir(codec_root,
2402624d5faSMark Brown 						       debugfs_root);
2412624d5faSMark Brown 	if (!codec->debugfs_codec_root) {
2422624d5faSMark Brown 		printk(KERN_WARNING
2432624d5faSMark Brown 		       "ASoC: Failed to create codec debugfs directory\n");
2442624d5faSMark Brown 		return;
2452624d5faSMark Brown 	}
2462624d5faSMark Brown 
2472624d5faSMark Brown 	codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
2482624d5faSMark Brown 						 codec->debugfs_codec_root,
2492624d5faSMark Brown 						 codec, &codec_reg_fops);
2502624d5faSMark Brown 	if (!codec->debugfs_reg)
2512624d5faSMark Brown 		printk(KERN_WARNING
2522624d5faSMark Brown 		       "ASoC: Failed to create codec register debugfs file\n");
2532624d5faSMark Brown 
2542624d5faSMark Brown 	codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
2552624d5faSMark Brown 						     codec->debugfs_codec_root,
2562624d5faSMark Brown 						     &codec->pop_time);
2572624d5faSMark Brown 	if (!codec->debugfs_pop_time)
2582624d5faSMark Brown 		printk(KERN_WARNING
2592624d5faSMark Brown 		       "Failed to create pop time debugfs file\n");
2602624d5faSMark Brown 
2612624d5faSMark Brown 	codec->debugfs_dapm = debugfs_create_dir("dapm",
2622624d5faSMark Brown 						 codec->debugfs_codec_root);
2632624d5faSMark Brown 	if (!codec->debugfs_dapm)
2642624d5faSMark Brown 		printk(KERN_WARNING
2652624d5faSMark Brown 		       "Failed to create DAPM debugfs directory\n");
2662624d5faSMark Brown 
2672624d5faSMark Brown 	snd_soc_dapm_debugfs_init(codec);
2682624d5faSMark Brown }
2692624d5faSMark Brown 
2702624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2712624d5faSMark Brown {
2722624d5faSMark Brown 	debugfs_remove_recursive(codec->debugfs_codec_root);
2732624d5faSMark Brown }
2742624d5faSMark Brown 
2752624d5faSMark Brown #else
2762624d5faSMark Brown 
2772624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
2782624d5faSMark Brown {
2792624d5faSMark Brown }
2802624d5faSMark Brown 
2812624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
2822624d5faSMark Brown {
2832624d5faSMark Brown }
2842624d5faSMark Brown #endif
2852624d5faSMark Brown 
286db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
287db2a4165SFrank Mandarino /* unregister ac97 codec */
288db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
289db2a4165SFrank Mandarino {
290db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
291db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
292db2a4165SFrank Mandarino 	return 0;
293db2a4165SFrank Mandarino }
294db2a4165SFrank Mandarino 
295db2a4165SFrank Mandarino /* stop no dev release warning */
296db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
297db2a4165SFrank Mandarino 
298db2a4165SFrank Mandarino /* register ac97 codec to bus */
299db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
300db2a4165SFrank Mandarino {
301db2a4165SFrank Mandarino 	int err;
302db2a4165SFrank Mandarino 
303db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
3044ac5c61fSMark Brown 	codec->ac97->dev.parent = codec->card->dev;
305db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
306db2a4165SFrank Mandarino 
307bb072bf0SKay Sievers 	dev_set_name(&codec->ac97->dev, "%d-%d:%s",
308db2a4165SFrank Mandarino 		     codec->card->number, 0, codec->name);
309db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
310db2a4165SFrank Mandarino 	if (err < 0) {
311db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
312db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
313db2a4165SFrank Mandarino 		return err;
314db2a4165SFrank Mandarino 	}
315db2a4165SFrank Mandarino 	return 0;
316db2a4165SFrank Mandarino }
317db2a4165SFrank Mandarino #endif
318db2a4165SFrank Mandarino 
31906f409d7SMark Brown static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
32006f409d7SMark Brown {
32106f409d7SMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
32206f409d7SMark Brown 	struct snd_soc_device *socdev = rtd->socdev;
32306f409d7SMark Brown 	struct snd_soc_card *card = socdev->card;
32406f409d7SMark Brown 	struct snd_soc_dai_link *machine = rtd->dai;
32506f409d7SMark Brown 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
32606f409d7SMark Brown 	struct snd_soc_dai *codec_dai = machine->codec_dai;
32706f409d7SMark Brown 	int ret;
32806f409d7SMark Brown 
32906f409d7SMark Brown 	if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
33006f409d7SMark Brown 	    machine->symmetric_rates) {
33106f409d7SMark Brown 		dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
33206f409d7SMark Brown 			machine->rate);
33306f409d7SMark Brown 
33406f409d7SMark Brown 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
33506f409d7SMark Brown 						   SNDRV_PCM_HW_PARAM_RATE,
33606f409d7SMark Brown 						   machine->rate,
33706f409d7SMark Brown 						   machine->rate);
33806f409d7SMark Brown 		if (ret < 0) {
33906f409d7SMark Brown 			dev_err(card->dev,
34006f409d7SMark Brown 				"Unable to apply rate symmetry constraint: %d\n", ret);
34106f409d7SMark Brown 			return ret;
34206f409d7SMark Brown 		}
34306f409d7SMark Brown 	}
34406f409d7SMark Brown 
34506f409d7SMark Brown 	return 0;
34606f409d7SMark Brown }
34706f409d7SMark Brown 
348db2a4165SFrank Mandarino /*
349db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
350db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
351db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
352db2a4165SFrank Mandarino  */
353db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
354db2a4165SFrank Mandarino {
355db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
356db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
35787689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
358db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
359cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
36087689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
3613c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
3623c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
363db2a4165SFrank Mandarino 	int ret = 0;
364db2a4165SFrank Mandarino 
365db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
366db2a4165SFrank Mandarino 
367db2a4165SFrank Mandarino 	/* startup the audio subsystem */
3686335d055SEric Miao 	if (cpu_dai->ops->startup) {
3696335d055SEric Miao 		ret = cpu_dai->ops->startup(substream, cpu_dai);
370db2a4165SFrank Mandarino 		if (ret < 0) {
371db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
372cb666e5bSLiam Girdwood 				cpu_dai->name);
373db2a4165SFrank Mandarino 			goto out;
374db2a4165SFrank Mandarino 		}
375db2a4165SFrank Mandarino 	}
376db2a4165SFrank Mandarino 
377db2a4165SFrank Mandarino 	if (platform->pcm_ops->open) {
378db2a4165SFrank Mandarino 		ret = platform->pcm_ops->open(substream);
379db2a4165SFrank Mandarino 		if (ret < 0) {
380db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
381db2a4165SFrank Mandarino 			goto platform_err;
382db2a4165SFrank Mandarino 		}
383db2a4165SFrank Mandarino 	}
384db2a4165SFrank Mandarino 
3856335d055SEric Miao 	if (codec_dai->ops->startup) {
3866335d055SEric Miao 		ret = codec_dai->ops->startup(substream, codec_dai);
387cb666e5bSLiam Girdwood 		if (ret < 0) {
388cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: can't open codec %s\n",
389cb666e5bSLiam Girdwood 				codec_dai->name);
390cb666e5bSLiam Girdwood 			goto codec_dai_err;
391cb666e5bSLiam Girdwood 		}
392cb666e5bSLiam Girdwood 	}
393cb666e5bSLiam Girdwood 
394db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->startup) {
395db2a4165SFrank Mandarino 		ret = machine->ops->startup(substream);
396db2a4165SFrank Mandarino 		if (ret < 0) {
397db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
398db2a4165SFrank Mandarino 			goto machine_err;
399db2a4165SFrank Mandarino 		}
400db2a4165SFrank Mandarino 	}
401db2a4165SFrank Mandarino 
402db2a4165SFrank Mandarino 	/* Check that the codec and cpu DAI's are compatible */
403db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
404db2a4165SFrank Mandarino 		runtime->hw.rate_min =
4053ff3f64bSMark Brown 			max(codec_dai->playback.rate_min,
4063ff3f64bSMark Brown 			    cpu_dai->playback.rate_min);
407db2a4165SFrank Mandarino 		runtime->hw.rate_max =
4083ff3f64bSMark Brown 			min(codec_dai->playback.rate_max,
4093ff3f64bSMark Brown 			    cpu_dai->playback.rate_max);
410db2a4165SFrank Mandarino 		runtime->hw.channels_min =
411cb666e5bSLiam Girdwood 			max(codec_dai->playback.channels_min,
412cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_min);
413db2a4165SFrank Mandarino 		runtime->hw.channels_max =
414cb666e5bSLiam Girdwood 			min(codec_dai->playback.channels_max,
415cb666e5bSLiam Girdwood 				cpu_dai->playback.channels_max);
416cb666e5bSLiam Girdwood 		runtime->hw.formats =
417cb666e5bSLiam Girdwood 			codec_dai->playback.formats & cpu_dai->playback.formats;
418cb666e5bSLiam Girdwood 		runtime->hw.rates =
419cb666e5bSLiam Girdwood 			codec_dai->playback.rates & cpu_dai->playback.rates;
420d9ad6296SJassi Brar 		if (codec_dai->playback.rates
421d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
422d9ad6296SJassi Brar 			runtime->hw.rates |= cpu_dai->playback.rates;
423d9ad6296SJassi Brar 		if (cpu_dai->playback.rates
424d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
425d9ad6296SJassi Brar 			runtime->hw.rates |= codec_dai->playback.rates;
426db2a4165SFrank Mandarino 	} else {
427db2a4165SFrank Mandarino 		runtime->hw.rate_min =
4283ff3f64bSMark Brown 			max(codec_dai->capture.rate_min,
4293ff3f64bSMark Brown 			    cpu_dai->capture.rate_min);
430db2a4165SFrank Mandarino 		runtime->hw.rate_max =
4313ff3f64bSMark Brown 			min(codec_dai->capture.rate_max,
4323ff3f64bSMark Brown 			    cpu_dai->capture.rate_max);
433db2a4165SFrank Mandarino 		runtime->hw.channels_min =
434cb666e5bSLiam Girdwood 			max(codec_dai->capture.channels_min,
435cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_min);
436db2a4165SFrank Mandarino 		runtime->hw.channels_max =
437cb666e5bSLiam Girdwood 			min(codec_dai->capture.channels_max,
438cb666e5bSLiam Girdwood 				cpu_dai->capture.channels_max);
439cb666e5bSLiam Girdwood 		runtime->hw.formats =
440cb666e5bSLiam Girdwood 			codec_dai->capture.formats & cpu_dai->capture.formats;
441cb666e5bSLiam Girdwood 		runtime->hw.rates =
442cb666e5bSLiam Girdwood 			codec_dai->capture.rates & cpu_dai->capture.rates;
443d9ad6296SJassi Brar 		if (codec_dai->capture.rates
444d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
445d9ad6296SJassi Brar 			runtime->hw.rates |= cpu_dai->capture.rates;
446d9ad6296SJassi Brar 		if (cpu_dai->capture.rates
447d9ad6296SJassi Brar 			   & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
448d9ad6296SJassi Brar 			runtime->hw.rates |= codec_dai->capture.rates;
449db2a4165SFrank Mandarino 	}
450db2a4165SFrank Mandarino 
451db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
452db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
453db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
454cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
455bb1c0478SJassi Brar 		goto config_err;
456db2a4165SFrank Mandarino 	}
457db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
458db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
459cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
460bb1c0478SJassi Brar 		goto config_err;
461db2a4165SFrank Mandarino 	}
462db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
463db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
464cb666e5bSLiam Girdwood 			codec_dai->name, cpu_dai->name);
465bb1c0478SJassi Brar 		goto config_err;
466db2a4165SFrank Mandarino 	}
467db2a4165SFrank Mandarino 
46806f409d7SMark Brown 	/* Symmetry only applies if we've already got an active stream. */
46906f409d7SMark Brown 	if (cpu_dai->active || codec_dai->active) {
47006f409d7SMark Brown 		ret = soc_pcm_apply_symmetry(substream);
47106f409d7SMark Brown 		if (ret != 0)
472bb1c0478SJassi Brar 			goto config_err;
47306f409d7SMark Brown 	}
47406f409d7SMark Brown 
475f24368c2SMark Brown 	pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
476f24368c2SMark Brown 	pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
477f24368c2SMark Brown 	pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
478b5c5fd24SLiam Girdwood 		 runtime->hw.channels_max);
479f24368c2SMark Brown 	pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
480b5c5fd24SLiam Girdwood 		 runtime->hw.rate_max);
481db2a4165SFrank Mandarino 
48214dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
48314dc5734SJassi Brar 		cpu_dai->playback.active++;
48414dc5734SJassi Brar 		codec_dai->playback.active++;
48514dc5734SJassi Brar 	} else {
48614dc5734SJassi Brar 		cpu_dai->capture.active++;
48714dc5734SJassi Brar 		codec_dai->capture.active++;
48814dc5734SJassi Brar 	}
48914dc5734SJassi Brar 	cpu_dai->active++;
49014dc5734SJassi Brar 	codec_dai->active++;
4916627a653SMark Brown 	card->codec->active++;
492db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
493db2a4165SFrank Mandarino 	return 0;
494db2a4165SFrank Mandarino 
495bb1c0478SJassi Brar config_err:
496db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
497db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
498db2a4165SFrank Mandarino 
499bb1c0478SJassi Brar machine_err:
500bb1c0478SJassi Brar 	if (codec_dai->ops->shutdown)
501bb1c0478SJassi Brar 		codec_dai->ops->shutdown(substream, codec_dai);
502bb1c0478SJassi Brar 
503cb666e5bSLiam Girdwood codec_dai_err:
504db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
505db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
506db2a4165SFrank Mandarino 
507db2a4165SFrank Mandarino platform_err:
5086335d055SEric Miao 	if (cpu_dai->ops->shutdown)
5096335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
510db2a4165SFrank Mandarino out:
511db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
512db2a4165SFrank Mandarino 	return ret;
513db2a4165SFrank Mandarino }
514db2a4165SFrank Mandarino 
515db2a4165SFrank Mandarino /*
5163a4fa0a2SRobert P. J. Day  * Power down the audio subsystem pmdown_time msecs after close is called.
517db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
518db2a4165SFrank Mandarino  * due to DAPM power cycling.
519db2a4165SFrank Mandarino  */
5204484bb2eSAndrew Morton static void close_delayed_work(struct work_struct *work)
521db2a4165SFrank Mandarino {
5226308419aSMark Brown 	struct snd_soc_card *card = container_of(work, struct snd_soc_card,
5236308419aSMark Brown 						 delayed_work.work);
5246627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
5253c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
526db2a4165SFrank Mandarino 	int i;
527db2a4165SFrank Mandarino 
528db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
529db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
530db2a4165SFrank Mandarino 		codec_dai = &codec->dai[i];
531db2a4165SFrank Mandarino 
532f24368c2SMark Brown 		pr_debug("pop wq checking: %s status: %s waiting: %s\n",
533db2a4165SFrank Mandarino 			 codec_dai->playback.stream_name,
534db2a4165SFrank Mandarino 			 codec_dai->playback.active ? "active" : "inactive",
535db2a4165SFrank Mandarino 			 codec_dai->pop_wait ? "yes" : "no");
536db2a4165SFrank Mandarino 
537db2a4165SFrank Mandarino 		/* are we waiting on this codec DAI stream */
538db2a4165SFrank Mandarino 		if (codec_dai->pop_wait == 1) {
539db2a4165SFrank Mandarino 			codec_dai->pop_wait = 0;
5400b4d221bSLiam Girdwood 			snd_soc_dapm_stream_event(codec,
5410b4d221bSLiam Girdwood 				codec_dai->playback.stream_name,
542db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_STOP);
543db2a4165SFrank Mandarino 		}
544db2a4165SFrank Mandarino 	}
545db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
546db2a4165SFrank Mandarino }
547db2a4165SFrank Mandarino 
548db2a4165SFrank Mandarino /*
549db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
550db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
551db2a4165SFrank Mandarino  * shutdown.
552db2a4165SFrank Mandarino  */
553db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
554db2a4165SFrank Mandarino {
555db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
556db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
5576308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
558cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
55987689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
5603c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
5613c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
5626627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
563db2a4165SFrank Mandarino 
564db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
565db2a4165SFrank Mandarino 
56614dc5734SJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
56714dc5734SJassi Brar 		cpu_dai->playback.active--;
56814dc5734SJassi Brar 		codec_dai->playback.active--;
56914dc5734SJassi Brar 	} else {
57014dc5734SJassi Brar 		cpu_dai->capture.active--;
57114dc5734SJassi Brar 		codec_dai->capture.active--;
572db2a4165SFrank Mandarino 	}
57314dc5734SJassi Brar 
57414dc5734SJassi Brar 	cpu_dai->active--;
57514dc5734SJassi Brar 	codec_dai->active--;
576db2a4165SFrank Mandarino 	codec->active--;
577db2a4165SFrank Mandarino 
5786010b2daSMark Brown 	/* Muting the DAC suppresses artifacts caused during digital
5796010b2daSMark Brown 	 * shutdown, for example from stopping clocks.
5806010b2daSMark Brown 	 */
5816010b2daSMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5826010b2daSMark Brown 		snd_soc_dai_digital_mute(codec_dai, 1);
5836010b2daSMark Brown 
5846335d055SEric Miao 	if (cpu_dai->ops->shutdown)
5856335d055SEric Miao 		cpu_dai->ops->shutdown(substream, cpu_dai);
586db2a4165SFrank Mandarino 
5876335d055SEric Miao 	if (codec_dai->ops->shutdown)
5886335d055SEric Miao 		codec_dai->ops->shutdown(substream, codec_dai);
589db2a4165SFrank Mandarino 
590db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
591db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
592db2a4165SFrank Mandarino 
593db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
594db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
595db2a4165SFrank Mandarino 
596db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
598cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 1;
5996308419aSMark Brown 		schedule_delayed_work(&card->delayed_work,
60096dd3622SMark Brown 			msecs_to_jiffies(card->pmdown_time));
601db2a4165SFrank Mandarino 	} else {
602db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
603cb666e5bSLiam Girdwood 		snd_soc_dapm_stream_event(codec,
6040b4d221bSLiam Girdwood 			codec_dai->capture.stream_name,
6050b4d221bSLiam Girdwood 			SND_SOC_DAPM_STREAM_STOP);
606db2a4165SFrank Mandarino 	}
607db2a4165SFrank Mandarino 
608db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
609db2a4165SFrank Mandarino 	return 0;
610db2a4165SFrank Mandarino }
611db2a4165SFrank Mandarino 
612db2a4165SFrank Mandarino /*
613db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
614db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
615db2a4165SFrank Mandarino  * it can refer to the runtime info.
616db2a4165SFrank Mandarino  */
617db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
618db2a4165SFrank Mandarino {
619db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
620db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
6216308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
622cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
62387689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
6243c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
6253c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
6266627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
627db2a4165SFrank Mandarino 	int ret = 0;
628db2a4165SFrank Mandarino 
629db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
630cb666e5bSLiam Girdwood 
631cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->prepare) {
632cb666e5bSLiam Girdwood 		ret = machine->ops->prepare(substream);
633cb666e5bSLiam Girdwood 		if (ret < 0) {
634cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine prepare error\n");
635cb666e5bSLiam Girdwood 			goto out;
636cb666e5bSLiam Girdwood 		}
637cb666e5bSLiam Girdwood 	}
638cb666e5bSLiam Girdwood 
639db2a4165SFrank Mandarino 	if (platform->pcm_ops->prepare) {
640db2a4165SFrank Mandarino 		ret = platform->pcm_ops->prepare(substream);
641a71a468aSLiam Girdwood 		if (ret < 0) {
642a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: platform prepare error\n");
643db2a4165SFrank Mandarino 			goto out;
644db2a4165SFrank Mandarino 		}
645a71a468aSLiam Girdwood 	}
646db2a4165SFrank Mandarino 
6476335d055SEric Miao 	if (codec_dai->ops->prepare) {
6486335d055SEric Miao 		ret = codec_dai->ops->prepare(substream, codec_dai);
649a71a468aSLiam Girdwood 		if (ret < 0) {
650a71a468aSLiam Girdwood 			printk(KERN_ERR "asoc: codec DAI prepare error\n");
651db2a4165SFrank Mandarino 			goto out;
652db2a4165SFrank Mandarino 		}
653a71a468aSLiam Girdwood 	}
654db2a4165SFrank Mandarino 
6556335d055SEric Miao 	if (cpu_dai->ops->prepare) {
6566335d055SEric Miao 		ret = cpu_dai->ops->prepare(substream, cpu_dai);
657cb666e5bSLiam Girdwood 		if (ret < 0) {
658cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: cpu DAI prepare error\n");
659cb666e5bSLiam Girdwood 			goto out;
660cb666e5bSLiam Girdwood 		}
661cb666e5bSLiam Girdwood 	}
662db2a4165SFrank Mandarino 
663d45f6219SMark Brown 	/* cancel any delayed stream shutdown that is pending */
664d45f6219SMark Brown 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
665d45f6219SMark Brown 	    codec_dai->pop_wait) {
666cb666e5bSLiam Girdwood 		codec_dai->pop_wait = 0;
6676308419aSMark Brown 		cancel_delayed_work(&card->delayed_work);
668db2a4165SFrank Mandarino 	}
669db2a4165SFrank Mandarino 
670db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
671db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
672cb666e5bSLiam Girdwood 					  codec_dai->playback.stream_name,
673db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
674db2a4165SFrank Mandarino 	else
675db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec,
676cb666e5bSLiam Girdwood 					  codec_dai->capture.stream_name,
677db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_START);
6788c6529dbSLiam Girdwood 
6798c6529dbSLiam Girdwood 	snd_soc_dai_digital_mute(codec_dai, 0);
680db2a4165SFrank Mandarino 
681db2a4165SFrank Mandarino out:
682db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
683db2a4165SFrank Mandarino 	return ret;
684db2a4165SFrank Mandarino }
685db2a4165SFrank Mandarino 
686db2a4165SFrank Mandarino /*
687db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
688db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
689db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
690db2a4165SFrank Mandarino  */
691db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
692db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
693db2a4165SFrank Mandarino {
694db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
695db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
696cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
69787689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
69887689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
6993c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
7003c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
701db2a4165SFrank Mandarino 	int ret = 0;
702db2a4165SFrank Mandarino 
703db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
704db2a4165SFrank Mandarino 
705cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_params) {
706cb666e5bSLiam Girdwood 		ret = machine->ops->hw_params(substream, params);
707cb666e5bSLiam Girdwood 		if (ret < 0) {
708cb666e5bSLiam Girdwood 			printk(KERN_ERR "asoc: machine hw_params failed\n");
709db2a4165SFrank Mandarino 			goto out;
710db2a4165SFrank Mandarino 		}
711cb666e5bSLiam Girdwood 	}
712db2a4165SFrank Mandarino 
7136335d055SEric Miao 	if (codec_dai->ops->hw_params) {
7146335d055SEric Miao 		ret = codec_dai->ops->hw_params(substream, params, codec_dai);
715db2a4165SFrank Mandarino 		if (ret < 0) {
716db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
717cb666e5bSLiam Girdwood 				codec_dai->name);
718cb666e5bSLiam Girdwood 			goto codec_err;
719db2a4165SFrank Mandarino 		}
720db2a4165SFrank Mandarino 	}
721db2a4165SFrank Mandarino 
7226335d055SEric Miao 	if (cpu_dai->ops->hw_params) {
7236335d055SEric Miao 		ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
724db2a4165SFrank Mandarino 		if (ret < 0) {
7253ff3f64bSMark Brown 			printk(KERN_ERR "asoc: interface %s hw params failed\n",
726cb666e5bSLiam Girdwood 				cpu_dai->name);
727db2a4165SFrank Mandarino 			goto interface_err;
728db2a4165SFrank Mandarino 		}
729db2a4165SFrank Mandarino 	}
730db2a4165SFrank Mandarino 
731db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_params) {
732db2a4165SFrank Mandarino 		ret = platform->pcm_ops->hw_params(substream, params);
733db2a4165SFrank Mandarino 		if (ret < 0) {
7343ff3f64bSMark Brown 			printk(KERN_ERR "asoc: platform %s hw params failed\n",
735db2a4165SFrank Mandarino 				platform->name);
736db2a4165SFrank Mandarino 			goto platform_err;
737db2a4165SFrank Mandarino 		}
738db2a4165SFrank Mandarino 	}
739db2a4165SFrank Mandarino 
74006f409d7SMark Brown 	machine->rate = params_rate(params);
74106f409d7SMark Brown 
742db2a4165SFrank Mandarino out:
743db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
744db2a4165SFrank Mandarino 	return ret;
745db2a4165SFrank Mandarino 
746db2a4165SFrank Mandarino platform_err:
7476335d055SEric Miao 	if (cpu_dai->ops->hw_free)
7486335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
749db2a4165SFrank Mandarino 
750db2a4165SFrank Mandarino interface_err:
7516335d055SEric Miao 	if (codec_dai->ops->hw_free)
7526335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
753cb666e5bSLiam Girdwood 
754cb666e5bSLiam Girdwood codec_err:
755cb666e5bSLiam Girdwood 	if (machine->ops && machine->ops->hw_free)
756cb666e5bSLiam Girdwood 		machine->ops->hw_free(substream);
757db2a4165SFrank Mandarino 
758db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
759db2a4165SFrank Mandarino 	return ret;
760db2a4165SFrank Mandarino }
761db2a4165SFrank Mandarino 
762db2a4165SFrank Mandarino /*
763db2a4165SFrank Mandarino  * Free's resources allocated by hw_params, can be called multiple times
764db2a4165SFrank Mandarino  */
765db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
766db2a4165SFrank Mandarino {
767db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
768db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
769cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
77087689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
77187689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
7723c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
7733c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
7746627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
775db2a4165SFrank Mandarino 
776db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
777db2a4165SFrank Mandarino 
778db2a4165SFrank Mandarino 	/* apply codec digital mute */
7798c6529dbSLiam Girdwood 	if (!codec->active)
7808c6529dbSLiam Girdwood 		snd_soc_dai_digital_mute(codec_dai, 1);
781db2a4165SFrank Mandarino 
782db2a4165SFrank Mandarino 	/* free any machine hw params */
783db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->hw_free)
784db2a4165SFrank Mandarino 		machine->ops->hw_free(substream);
785db2a4165SFrank Mandarino 
786db2a4165SFrank Mandarino 	/* free any DMA resources */
787db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_free)
788db2a4165SFrank Mandarino 		platform->pcm_ops->hw_free(substream);
789db2a4165SFrank Mandarino 
790db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
7916335d055SEric Miao 	if (codec_dai->ops->hw_free)
7926335d055SEric Miao 		codec_dai->ops->hw_free(substream, codec_dai);
793db2a4165SFrank Mandarino 
7946335d055SEric Miao 	if (cpu_dai->ops->hw_free)
7956335d055SEric Miao 		cpu_dai->ops->hw_free(substream, cpu_dai);
796db2a4165SFrank Mandarino 
797db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
798db2a4165SFrank Mandarino 	return 0;
799db2a4165SFrank Mandarino }
800db2a4165SFrank Mandarino 
801db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
802db2a4165SFrank Mandarino {
803db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
804db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
80587689d56SMark Brown 	struct snd_soc_card *card= socdev->card;
806cb666e5bSLiam Girdwood 	struct snd_soc_dai_link *machine = rtd->dai;
80787689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
8083c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
8093c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = machine->codec_dai;
810db2a4165SFrank Mandarino 	int ret;
811db2a4165SFrank Mandarino 
8126335d055SEric Miao 	if (codec_dai->ops->trigger) {
8136335d055SEric Miao 		ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
814db2a4165SFrank Mandarino 		if (ret < 0)
815db2a4165SFrank Mandarino 			return ret;
816db2a4165SFrank Mandarino 	}
817db2a4165SFrank Mandarino 
818db2a4165SFrank Mandarino 	if (platform->pcm_ops->trigger) {
819db2a4165SFrank Mandarino 		ret = platform->pcm_ops->trigger(substream, cmd);
820db2a4165SFrank Mandarino 		if (ret < 0)
821db2a4165SFrank Mandarino 			return ret;
822db2a4165SFrank Mandarino 	}
823db2a4165SFrank Mandarino 
8246335d055SEric Miao 	if (cpu_dai->ops->trigger) {
8256335d055SEric Miao 		ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
826db2a4165SFrank Mandarino 		if (ret < 0)
827db2a4165SFrank Mandarino 			return ret;
828db2a4165SFrank Mandarino 	}
829db2a4165SFrank Mandarino 	return 0;
830db2a4165SFrank Mandarino }
831db2a4165SFrank Mandarino 
832377b6f62SPeter Ujfalusi /*
833377b6f62SPeter Ujfalusi  * soc level wrapper for pointer callback
834258020d0SPeter Ujfalusi  * If cpu_dai, codec_dai, platform driver has the delay callback, than
835258020d0SPeter Ujfalusi  * the runtime->delay will be updated accordingly.
836377b6f62SPeter Ujfalusi  */
837377b6f62SPeter Ujfalusi static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
838377b6f62SPeter Ujfalusi {
839377b6f62SPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
840377b6f62SPeter Ujfalusi 	struct snd_soc_device *socdev = rtd->socdev;
841377b6f62SPeter Ujfalusi 	struct snd_soc_card *card = socdev->card;
842377b6f62SPeter Ujfalusi 	struct snd_soc_platform *platform = card->platform;
843258020d0SPeter Ujfalusi 	struct snd_soc_dai_link *machine = rtd->dai;
844258020d0SPeter Ujfalusi 	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
845258020d0SPeter Ujfalusi 	struct snd_soc_dai *codec_dai = machine->codec_dai;
846258020d0SPeter Ujfalusi 	struct snd_pcm_runtime *runtime = substream->runtime;
847377b6f62SPeter Ujfalusi 	snd_pcm_uframes_t offset = 0;
848258020d0SPeter Ujfalusi 	snd_pcm_sframes_t delay = 0;
849377b6f62SPeter Ujfalusi 
850377b6f62SPeter Ujfalusi 	if (platform->pcm_ops->pointer)
851377b6f62SPeter Ujfalusi 		offset = platform->pcm_ops->pointer(substream);
852377b6f62SPeter Ujfalusi 
853258020d0SPeter Ujfalusi 	if (cpu_dai->ops->delay)
854258020d0SPeter Ujfalusi 		delay += cpu_dai->ops->delay(substream, cpu_dai);
855258020d0SPeter Ujfalusi 
856258020d0SPeter Ujfalusi 	if (codec_dai->ops->delay)
857258020d0SPeter Ujfalusi 		delay += codec_dai->ops->delay(substream, codec_dai);
858258020d0SPeter Ujfalusi 
859258020d0SPeter Ujfalusi 	if (platform->delay)
860258020d0SPeter Ujfalusi 		delay += platform->delay(substream, codec_dai);
861258020d0SPeter Ujfalusi 
862258020d0SPeter Ujfalusi 	runtime->delay = delay;
863258020d0SPeter Ujfalusi 
864377b6f62SPeter Ujfalusi 	return offset;
865377b6f62SPeter Ujfalusi }
866377b6f62SPeter Ujfalusi 
867db2a4165SFrank Mandarino /* ASoC PCM operations */
868db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
869db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
870db2a4165SFrank Mandarino 	.close		= soc_codec_close,
871db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
872db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
873db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
874db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
875377b6f62SPeter Ujfalusi 	.pointer	= soc_pcm_pointer,
876db2a4165SFrank Mandarino };
877db2a4165SFrank Mandarino 
878db2a4165SFrank Mandarino #ifdef CONFIG_PM
879db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
880416356fcSMark Brown static int soc_suspend(struct device *dev)
881db2a4165SFrank Mandarino {
882416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
883db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
88487506549SMark Brown 	struct snd_soc_card *card = socdev->card;
88587689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
886db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
8876627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
888db2a4165SFrank Mandarino 	int i;
889db2a4165SFrank Mandarino 
890e3509ff0SDaniel Mack 	/* If the initialization of this soc device failed, there is no codec
891e3509ff0SDaniel Mack 	 * associated with it. Just bail out in this case.
892e3509ff0SDaniel Mack 	 */
893e3509ff0SDaniel Mack 	if (!codec)
894e3509ff0SDaniel Mack 		return 0;
895e3509ff0SDaniel Mack 
8966ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
8976ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
8986ed25978SAndy Green 	 */
8996ed25978SAndy Green 	snd_power_lock(codec->card);
9006ed25978SAndy Green 	snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
9016ed25978SAndy Green 	snd_power_unlock(codec->card);
9026ed25978SAndy Green 
9036ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
9046ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
9056ed25978SAndy Green 
906db2a4165SFrank Mandarino 	/* mute any active DAC's */
907dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
908dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
9093efab7dcSMark Brown 
9103efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
9113efab7dcSMark Brown 			continue;
9123efab7dcSMark Brown 
9136335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
9146335d055SEric Miao 			dai->ops->digital_mute(dai, 1);
915db2a4165SFrank Mandarino 	}
916db2a4165SFrank Mandarino 
9174ccab3e7SLiam Girdwood 	/* suspend all pcms */
9183efab7dcSMark Brown 	for (i = 0; i < card->num_links; i++) {
9193efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
9203efab7dcSMark Brown 			continue;
9213efab7dcSMark Brown 
92287506549SMark Brown 		snd_pcm_suspend_all(card->dai_link[i].pcm);
9233efab7dcSMark Brown 	}
9244ccab3e7SLiam Girdwood 
92587506549SMark Brown 	if (card->suspend_pre)
926416356fcSMark Brown 		card->suspend_pre(pdev, PMSG_SUSPEND);
927db2a4165SFrank Mandarino 
92887506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
92987506549SMark Brown 		struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
9303efab7dcSMark Brown 
9313efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
9323efab7dcSMark Brown 			continue;
9333efab7dcSMark Brown 
9343ba9e10aSMark Brown 		if (cpu_dai->suspend && !cpu_dai->ac97_control)
935dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
936db2a4165SFrank Mandarino 		if (platform->suspend)
937d273ebe7Sjassi brar 			platform->suspend(&card->dai_link[i]);
938db2a4165SFrank Mandarino 	}
939db2a4165SFrank Mandarino 
940db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
9416308419aSMark Brown 	run_delayed_work(&card->delayed_work);
9420be9898aSMark Brown 	codec->suspend_bias_level = codec->bias_level;
943db2a4165SFrank Mandarino 
944db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
945db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
9463efab7dcSMark Brown 
9473efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
9483efab7dcSMark Brown 			continue;
9493efab7dcSMark Brown 
950db2a4165SFrank Mandarino 		if (stream != NULL)
951db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
952db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
953db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
954db2a4165SFrank Mandarino 		if (stream != NULL)
955db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
956db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
957db2a4165SFrank Mandarino 	}
958db2a4165SFrank Mandarino 
9591547aba9SMark Brown 	/* If there are paths active then the CODEC will be held with
9601547aba9SMark Brown 	 * bias _ON and should not be suspended. */
9611547aba9SMark Brown 	if (codec_dev->suspend) {
9621547aba9SMark Brown 		switch (codec->bias_level) {
9631547aba9SMark Brown 		case SND_SOC_BIAS_STANDBY:
9641547aba9SMark Brown 		case SND_SOC_BIAS_OFF:
965416356fcSMark Brown 			codec_dev->suspend(pdev, PMSG_SUSPEND);
9661547aba9SMark Brown 			break;
9671547aba9SMark Brown 		default:
9681547aba9SMark Brown 			dev_dbg(socdev->dev, "CODEC is on over suspend\n");
9691547aba9SMark Brown 			break;
9701547aba9SMark Brown 		}
9711547aba9SMark Brown 	}
972db2a4165SFrank Mandarino 
97387506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
97487506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
9753efab7dcSMark Brown 
9763efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
9773efab7dcSMark Brown 			continue;
9783efab7dcSMark Brown 
9793ba9e10aSMark Brown 		if (cpu_dai->suspend && cpu_dai->ac97_control)
980dc7d7b83SMark Brown 			cpu_dai->suspend(cpu_dai);
981db2a4165SFrank Mandarino 	}
982db2a4165SFrank Mandarino 
98387506549SMark Brown 	if (card->suspend_post)
984416356fcSMark Brown 		card->suspend_post(pdev, PMSG_SUSPEND);
985db2a4165SFrank Mandarino 
986db2a4165SFrank Mandarino 	return 0;
987db2a4165SFrank Mandarino }
988db2a4165SFrank Mandarino 
9896ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
9906ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
9916ed25978SAndy Green  */
9926ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
993db2a4165SFrank Mandarino {
9946308419aSMark Brown 	struct snd_soc_card *card = container_of(work,
9956308419aSMark Brown 						 struct snd_soc_card,
9966ed25978SAndy Green 						 deferred_resume_work);
9976308419aSMark Brown 	struct snd_soc_device *socdev = card->socdev;
99887689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
999db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
10006627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
10016ed25978SAndy Green 	struct platform_device *pdev = to_platform_device(socdev->dev);
1002db2a4165SFrank Mandarino 	int i;
1003db2a4165SFrank Mandarino 
10046ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
10056ed25978SAndy Green 	 * so userspace apps are blocked from touching us
10066ed25978SAndy Green 	 */
10076ed25978SAndy Green 
1008fde22f27SMark Brown 	dev_dbg(socdev->dev, "starting resume work\n");
10096ed25978SAndy Green 
10109949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
10119949788bSMark Brown 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D2);
10129949788bSMark Brown 
101387506549SMark Brown 	if (card->resume_pre)
101487506549SMark Brown 		card->resume_pre(pdev);
1015db2a4165SFrank Mandarino 
101687506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
101787506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
10183efab7dcSMark Brown 
10193efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
10203efab7dcSMark Brown 			continue;
10213efab7dcSMark Brown 
10223ba9e10aSMark Brown 		if (cpu_dai->resume && cpu_dai->ac97_control)
1023dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
1024db2a4165SFrank Mandarino 	}
1025db2a4165SFrank Mandarino 
10261547aba9SMark Brown 	/* If the CODEC was idle over suspend then it will have been
10271547aba9SMark Brown 	 * left with bias OFF or STANDBY and suspended so we must now
10281547aba9SMark Brown 	 * resume.  Otherwise the suspend was suppressed.
10291547aba9SMark Brown 	 */
10301547aba9SMark Brown 	if (codec_dev->resume) {
10311547aba9SMark Brown 		switch (codec->bias_level) {
10321547aba9SMark Brown 		case SND_SOC_BIAS_STANDBY:
10331547aba9SMark Brown 		case SND_SOC_BIAS_OFF:
1034db2a4165SFrank Mandarino 			codec_dev->resume(pdev);
10351547aba9SMark Brown 			break;
10361547aba9SMark Brown 		default:
10371547aba9SMark Brown 			dev_dbg(socdev->dev, "CODEC was on over suspend\n");
10381547aba9SMark Brown 			break;
10391547aba9SMark Brown 		}
10401547aba9SMark Brown 	}
1041db2a4165SFrank Mandarino 
1042db2a4165SFrank Mandarino 	for (i = 0; i < codec->num_dai; i++) {
1043db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
10443efab7dcSMark Brown 
10453efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
10463efab7dcSMark Brown 			continue;
10473efab7dcSMark Brown 
1048db2a4165SFrank Mandarino 		if (stream != NULL)
1049db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
1050db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1051db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
1052db2a4165SFrank Mandarino 		if (stream != NULL)
1053db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
1054db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1055db2a4165SFrank Mandarino 	}
1056db2a4165SFrank Mandarino 
10573ff3f64bSMark Brown 	/* unmute any active DACs */
1058dee89c4dSMark Brown 	for (i = 0; i < card->num_links; i++) {
1059dee89c4dSMark Brown 		struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
10603efab7dcSMark Brown 
10613efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
10623efab7dcSMark Brown 			continue;
10633efab7dcSMark Brown 
10646335d055SEric Miao 		if (dai->ops->digital_mute && dai->playback.active)
10656335d055SEric Miao 			dai->ops->digital_mute(dai, 0);
1066db2a4165SFrank Mandarino 	}
1067db2a4165SFrank Mandarino 
106887506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
106987506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
10703efab7dcSMark Brown 
10713efab7dcSMark Brown 		if (card->dai_link[i].ignore_suspend)
10723efab7dcSMark Brown 			continue;
10733efab7dcSMark Brown 
10743ba9e10aSMark Brown 		if (cpu_dai->resume && !cpu_dai->ac97_control)
1075dc7d7b83SMark Brown 			cpu_dai->resume(cpu_dai);
1076db2a4165SFrank Mandarino 		if (platform->resume)
1077d273ebe7Sjassi brar 			platform->resume(&card->dai_link[i]);
1078db2a4165SFrank Mandarino 	}
1079db2a4165SFrank Mandarino 
108087506549SMark Brown 	if (card->resume_post)
108187506549SMark Brown 		card->resume_post(pdev);
1082db2a4165SFrank Mandarino 
1083fde22f27SMark Brown 	dev_dbg(socdev->dev, "resume work completed\n");
10846ed25978SAndy Green 
10856ed25978SAndy Green 	/* userspace can access us now we are back as we were before */
10866ed25978SAndy Green 	snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
10876ed25978SAndy Green }
10886ed25978SAndy Green 
10896ed25978SAndy Green /* powers up audio subsystem after a suspend */
1090416356fcSMark Brown static int soc_resume(struct device *dev)
10916ed25978SAndy Green {
1092416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
10936ed25978SAndy Green 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
10946308419aSMark Brown 	struct snd_soc_card *card = socdev->card;
109564ab9baaSMark Brown 	struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
10966ed25978SAndy Green 
1097b9dd94a8SPeter Ujfalusi 	/* If the initialization of this soc device failed, there is no codec
1098b9dd94a8SPeter Ujfalusi 	 * associated with it. Just bail out in this case.
1099b9dd94a8SPeter Ujfalusi 	 */
1100b9dd94a8SPeter Ujfalusi 	if (!card->codec)
1101b9dd94a8SPeter Ujfalusi 		return 0;
1102b9dd94a8SPeter Ujfalusi 
110364ab9baaSMark Brown 	/* AC97 devices might have other drivers hanging off them so
110464ab9baaSMark Brown 	 * need to resume immediately.  Other drivers don't have that
110564ab9baaSMark Brown 	 * problem and may take a substantial amount of time to resume
110664ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
110764ab9baaSMark Brown 	 */
110864ab9baaSMark Brown 	if (cpu_dai->ac97_control) {
110964ab9baaSMark Brown 		dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
111064ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
111164ab9baaSMark Brown 	} else {
111264ab9baaSMark Brown 		dev_dbg(socdev->dev, "Scheduling resume work\n");
11136308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
1114fde22f27SMark Brown 			dev_err(socdev->dev, "resume work item may be lost\n");
111564ab9baaSMark Brown 	}
11166ed25978SAndy Green 
1117db2a4165SFrank Mandarino 	return 0;
1118db2a4165SFrank Mandarino }
1119db2a4165SFrank Mandarino #else
1120db2a4165SFrank Mandarino #define soc_suspend	NULL
1121db2a4165SFrank Mandarino #define soc_resume	NULL
1122db2a4165SFrank Mandarino #endif
1123db2a4165SFrank Mandarino 
112402a06d30SBarry Song static struct snd_soc_dai_ops null_dai_ops = {
112502a06d30SBarry Song };
112602a06d30SBarry Song 
1127435c5e25SMark Brown static void snd_soc_instantiate_card(struct snd_soc_card *card)
1128db2a4165SFrank Mandarino {
1129435c5e25SMark Brown 	struct platform_device *pdev = container_of(card->dev,
1130435c5e25SMark Brown 						    struct platform_device,
1131435c5e25SMark Brown 						    dev);
1132435c5e25SMark Brown 	struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
1133fe3e78e0SMark Brown 	struct snd_soc_codec *codec;
1134435c5e25SMark Brown 	struct snd_soc_platform *platform;
1135435c5e25SMark Brown 	struct snd_soc_dai *dai;
11366b05eda6SMark Brown 	int i, found, ret, ac97;
1137db2a4165SFrank Mandarino 
1138435c5e25SMark Brown 	if (card->instantiated)
1139435c5e25SMark Brown 		return;
11406308419aSMark Brown 
1141435c5e25SMark Brown 	found = 0;
1142435c5e25SMark Brown 	list_for_each_entry(platform, &platform_list, list)
1143435c5e25SMark Brown 		if (card->platform == platform) {
1144435c5e25SMark Brown 			found = 1;
1145435c5e25SMark Brown 			break;
1146435c5e25SMark Brown 		}
1147435c5e25SMark Brown 	if (!found) {
1148435c5e25SMark Brown 		dev_dbg(card->dev, "Platform %s not registered\n",
1149435c5e25SMark Brown 			card->platform->name);
1150435c5e25SMark Brown 		return;
1151c5af3a2eSMark Brown 	}
1152c5af3a2eSMark Brown 
11536b05eda6SMark Brown 	ac97 = 0;
1154435c5e25SMark Brown 	for (i = 0; i < card->num_links; i++) {
1155435c5e25SMark Brown 		found = 0;
1156435c5e25SMark Brown 		list_for_each_entry(dai, &dai_list, list)
1157435c5e25SMark Brown 			if (card->dai_link[i].cpu_dai == dai) {
1158435c5e25SMark Brown 				found = 1;
1159435c5e25SMark Brown 				break;
1160435c5e25SMark Brown 			}
1161435c5e25SMark Brown 		if (!found) {
1162435c5e25SMark Brown 			dev_dbg(card->dev, "DAI %s not registered\n",
1163435c5e25SMark Brown 				card->dai_link[i].cpu_dai->name);
1164435c5e25SMark Brown 			return;
1165435c5e25SMark Brown 		}
11666b05eda6SMark Brown 
11676b05eda6SMark Brown 		if (card->dai_link[i].cpu_dai->ac97_control)
11686b05eda6SMark Brown 			ac97 = 1;
11696b05eda6SMark Brown 	}
11706b05eda6SMark Brown 
117102a06d30SBarry Song 	for (i = 0; i < card->num_links; i++) {
117202a06d30SBarry Song 		if (!card->dai_link[i].codec_dai->ops)
117302a06d30SBarry Song 			card->dai_link[i].codec_dai->ops = &null_dai_ops;
117402a06d30SBarry Song 	}
117502a06d30SBarry Song 
11766b05eda6SMark Brown 	/* If we have AC97 in the system then don't wait for the
11776b05eda6SMark Brown 	 * codec.  This will need revisiting if we have to handle
11786b05eda6SMark Brown 	 * systems with mixed AC97 and non-AC97 parts.  Only check for
11796b05eda6SMark Brown 	 * DAIs currently; we can't do this per link since some AC97
11806b05eda6SMark Brown 	 * codecs have non-AC97 DAIs.
11816b05eda6SMark Brown 	 */
11826b05eda6SMark Brown 	if (!ac97)
11836b05eda6SMark Brown 		for (i = 0; i < card->num_links; i++) {
11846b05eda6SMark Brown 			found = 0;
11856b05eda6SMark Brown 			list_for_each_entry(dai, &dai_list, list)
11866b05eda6SMark Brown 				if (card->dai_link[i].codec_dai == dai) {
11876b05eda6SMark Brown 					found = 1;
11886b05eda6SMark Brown 					break;
11896b05eda6SMark Brown 				}
11906b05eda6SMark Brown 			if (!found) {
11916b05eda6SMark Brown 				dev_dbg(card->dev, "DAI %s not registered\n",
11926b05eda6SMark Brown 					card->dai_link[i].codec_dai->name);
11936b05eda6SMark Brown 				return;
11946b05eda6SMark Brown 			}
1195435c5e25SMark Brown 		}
1196435c5e25SMark Brown 
1197435c5e25SMark Brown 	/* Note that we do not current check for codec components */
1198435c5e25SMark Brown 
1199435c5e25SMark Brown 	dev_dbg(card->dev, "All components present, instantiating\n");
1200435c5e25SMark Brown 
1201435c5e25SMark Brown 	/* Found everything, bring it up */
120296dd3622SMark Brown 	card->pmdown_time = pmdown_time;
120396dd3622SMark Brown 
120487506549SMark Brown 	if (card->probe) {
120587506549SMark Brown 		ret = card->probe(pdev);
1206db2a4165SFrank Mandarino 		if (ret < 0)
1207435c5e25SMark Brown 			return;
1208db2a4165SFrank Mandarino 	}
1209db2a4165SFrank Mandarino 
121087506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
121187506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1212db2a4165SFrank Mandarino 		if (cpu_dai->probe) {
1213bdb92876SMark Brown 			ret = cpu_dai->probe(pdev, cpu_dai);
1214db2a4165SFrank Mandarino 			if (ret < 0)
1215db2a4165SFrank Mandarino 				goto cpu_dai_err;
1216db2a4165SFrank Mandarino 		}
1217db2a4165SFrank Mandarino 	}
1218db2a4165SFrank Mandarino 
1219db2a4165SFrank Mandarino 	if (codec_dev->probe) {
1220db2a4165SFrank Mandarino 		ret = codec_dev->probe(pdev);
1221db2a4165SFrank Mandarino 		if (ret < 0)
1222db2a4165SFrank Mandarino 			goto cpu_dai_err;
1223db2a4165SFrank Mandarino 	}
1224fe3e78e0SMark Brown 	codec = card->codec;
1225db2a4165SFrank Mandarino 
1226db2a4165SFrank Mandarino 	if (platform->probe) {
1227db2a4165SFrank Mandarino 		ret = platform->probe(pdev);
1228db2a4165SFrank Mandarino 		if (ret < 0)
1229db2a4165SFrank Mandarino 			goto platform_err;
1230db2a4165SFrank Mandarino 	}
1231db2a4165SFrank Mandarino 
1232db2a4165SFrank Mandarino 	/* DAPM stream work */
12336308419aSMark Brown 	INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
12341301a964SRandy Dunlap #ifdef CONFIG_PM
12356ed25978SAndy Green 	/* deferred resume work */
12366308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
12371301a964SRandy Dunlap #endif
12386ed25978SAndy Green 
1239fe3e78e0SMark Brown 	for (i = 0; i < card->num_links; i++) {
1240fe3e78e0SMark Brown 		if (card->dai_link[i].init) {
1241fe3e78e0SMark Brown 			ret = card->dai_link[i].init(codec);
1242fe3e78e0SMark Brown 			if (ret < 0) {
1243fe3e78e0SMark Brown 				printk(KERN_ERR "asoc: failed to init %s\n",
1244fe3e78e0SMark Brown 					card->dai_link[i].stream_name);
1245fe3e78e0SMark Brown 				continue;
1246fe3e78e0SMark Brown 			}
1247fe3e78e0SMark Brown 		}
1248f7732053SBarry Song 		if (card->dai_link[i].codec_dai->ac97_control)
1249fe3e78e0SMark Brown 			ac97 = 1;
1250fe3e78e0SMark Brown 	}
1251fe3e78e0SMark Brown 
1252fe3e78e0SMark Brown 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1253fe3e78e0SMark Brown 		 "%s",  card->name);
1254fe3e78e0SMark Brown 	snprintf(codec->card->longname, sizeof(codec->card->longname),
1255fe3e78e0SMark Brown 		 "%s (%s)", card->name, codec->name);
1256fe3e78e0SMark Brown 
1257fe3e78e0SMark Brown 	/* Make sure all DAPM widgets are instantiated */
1258fe3e78e0SMark Brown 	snd_soc_dapm_new_widgets(codec);
1259fe3e78e0SMark Brown 
1260fe3e78e0SMark Brown 	ret = snd_card_register(codec->card);
1261fe3e78e0SMark Brown 	if (ret < 0) {
1262fe3e78e0SMark Brown 		printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1263fe3e78e0SMark Brown 				codec->name);
1264fe3e78e0SMark Brown 		goto card_err;
1265fe3e78e0SMark Brown 	}
1266fe3e78e0SMark Brown 
1267fe3e78e0SMark Brown 	mutex_lock(&codec->mutex);
1268fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS
1269fe3e78e0SMark Brown 	/* Only instantiate AC97 if not already done by the adaptor
1270fe3e78e0SMark Brown 	 * for the generic AC97 subsystem.
1271fe3e78e0SMark Brown 	 */
1272fe3e78e0SMark Brown 	if (ac97 && strcmp(codec->name, "AC97") != 0) {
1273fe3e78e0SMark Brown 		ret = soc_ac97_dev_register(codec);
1274fe3e78e0SMark Brown 		if (ret < 0) {
1275fe3e78e0SMark Brown 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1276fe3e78e0SMark Brown 			snd_card_free(codec->card);
1277fe3e78e0SMark Brown 			mutex_unlock(&codec->mutex);
1278fe3e78e0SMark Brown 			goto card_err;
1279fe3e78e0SMark Brown 		}
1280fe3e78e0SMark Brown 	}
1281fe3e78e0SMark Brown #endif
1282fe3e78e0SMark Brown 
1283fe3e78e0SMark Brown 	ret = snd_soc_dapm_sys_add(card->socdev->dev);
1284fe3e78e0SMark Brown 	if (ret < 0)
1285fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1286fe3e78e0SMark Brown 
1287dbe21408SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1288dbe21408SMark Brown 	if (ret < 0)
1289dbe21408SMark Brown 		printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1290dbe21408SMark Brown 
1291fe3e78e0SMark Brown 	ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1292fe3e78e0SMark Brown 	if (ret < 0)
1293fe3e78e0SMark Brown 		printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1294fe3e78e0SMark Brown 
1295fe3e78e0SMark Brown 	soc_init_codec_debugfs(codec);
1296fe3e78e0SMark Brown 	mutex_unlock(&codec->mutex);
1297fe3e78e0SMark Brown 
1298435c5e25SMark Brown 	card->instantiated = 1;
1299435c5e25SMark Brown 
1300435c5e25SMark Brown 	return;
1301db2a4165SFrank Mandarino 
1302fe3e78e0SMark Brown card_err:
1303fe3e78e0SMark Brown 	if (platform->remove)
1304fe3e78e0SMark Brown 		platform->remove(pdev);
1305fe3e78e0SMark Brown 
1306db2a4165SFrank Mandarino platform_err:
1307db2a4165SFrank Mandarino 	if (codec_dev->remove)
1308db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1309db2a4165SFrank Mandarino 
1310db2a4165SFrank Mandarino cpu_dai_err:
131118b9b3d9SLiam Girdwood 	for (i--; i >= 0; i--) {
131287506549SMark Brown 		struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1313db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1314bdb92876SMark Brown 			cpu_dai->remove(pdev, cpu_dai);
1315db2a4165SFrank Mandarino 	}
1316db2a4165SFrank Mandarino 
131787506549SMark Brown 	if (card->remove)
131887506549SMark Brown 		card->remove(pdev);
1319435c5e25SMark Brown }
1320db2a4165SFrank Mandarino 
1321435c5e25SMark Brown /*
1322435c5e25SMark Brown  * Attempt to initialise any uninitalised cards.  Must be called with
1323435c5e25SMark Brown  * client_mutex.
1324435c5e25SMark Brown  */
1325435c5e25SMark Brown static void snd_soc_instantiate_cards(void)
1326435c5e25SMark Brown {
1327435c5e25SMark Brown 	struct snd_soc_card *card;
1328435c5e25SMark Brown 	list_for_each_entry(card, &card_list, list)
1329435c5e25SMark Brown 		snd_soc_instantiate_card(card);
1330435c5e25SMark Brown }
1331435c5e25SMark Brown 
1332435c5e25SMark Brown /* probes a new socdev */
1333435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
1334435c5e25SMark Brown {
1335435c5e25SMark Brown 	int ret = 0;
1336435c5e25SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1337435c5e25SMark Brown 	struct snd_soc_card *card = socdev->card;
1338435c5e25SMark Brown 
1339435c5e25SMark Brown 	/* Bodge while we push things out of socdev */
1340435c5e25SMark Brown 	card->socdev = socdev;
1341435c5e25SMark Brown 
1342435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
1343435c5e25SMark Brown 	card->dev = &pdev->dev;
1344435c5e25SMark Brown 	ret = snd_soc_register_card(card);
1345435c5e25SMark Brown 	if (ret != 0) {
1346435c5e25SMark Brown 		dev_err(&pdev->dev, "Failed to register card\n");
1347db2a4165SFrank Mandarino 		return ret;
1348db2a4165SFrank Mandarino 	}
1349db2a4165SFrank Mandarino 
1350435c5e25SMark Brown 	return 0;
1351435c5e25SMark Brown }
1352435c5e25SMark Brown 
1353db2a4165SFrank Mandarino /* removes a socdev */
1354db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1355db2a4165SFrank Mandarino {
1356db2a4165SFrank Mandarino 	int i;
1357db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
135887506549SMark Brown 	struct snd_soc_card *card = socdev->card;
135987689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
1360db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1361db2a4165SFrank Mandarino 
1362b2dfa62cSGuennadi Liakhovetski 	if (card->instantiated) {
13636308419aSMark Brown 		run_delayed_work(&card->delayed_work);
1364965ac42cSLiam Girdwood 
1365db2a4165SFrank Mandarino 		if (platform->remove)
1366db2a4165SFrank Mandarino 			platform->remove(pdev);
1367db2a4165SFrank Mandarino 
1368db2a4165SFrank Mandarino 		if (codec_dev->remove)
1369db2a4165SFrank Mandarino 			codec_dev->remove(pdev);
1370db2a4165SFrank Mandarino 
137187506549SMark Brown 		for (i = 0; i < card->num_links; i++) {
137287506549SMark Brown 			struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1373db2a4165SFrank Mandarino 			if (cpu_dai->remove)
1374bdb92876SMark Brown 				cpu_dai->remove(pdev, cpu_dai);
1375db2a4165SFrank Mandarino 		}
1376db2a4165SFrank Mandarino 
137787506549SMark Brown 		if (card->remove)
137887506549SMark Brown 			card->remove(pdev);
1379b2dfa62cSGuennadi Liakhovetski 	}
1380db2a4165SFrank Mandarino 
1381c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
1382c5af3a2eSMark Brown 
1383db2a4165SFrank Mandarino 	return 0;
1384db2a4165SFrank Mandarino }
1385db2a4165SFrank Mandarino 
1386416356fcSMark Brown static int soc_poweroff(struct device *dev)
138751737470SMark Brown {
1388416356fcSMark Brown 	struct platform_device *pdev = to_platform_device(dev);
138951737470SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
139051737470SMark Brown 	struct snd_soc_card *card = socdev->card;
139151737470SMark Brown 
139251737470SMark Brown 	if (!card->instantiated)
1393416356fcSMark Brown 		return 0;
139451737470SMark Brown 
139551737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
139651737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
139751737470SMark Brown 	run_delayed_work(&card->delayed_work);
139851737470SMark Brown 
139951737470SMark Brown 	snd_soc_dapm_shutdown(socdev);
1400416356fcSMark Brown 
1401416356fcSMark Brown 	return 0;
140251737470SMark Brown }
140351737470SMark Brown 
140447145210SAlexey Dobriyan static const struct dev_pm_ops soc_pm_ops = {
1405416356fcSMark Brown 	.suspend = soc_suspend,
1406416356fcSMark Brown 	.resume = soc_resume,
1407416356fcSMark Brown 	.poweroff = soc_poweroff,
1408416356fcSMark Brown };
1409416356fcSMark Brown 
1410db2a4165SFrank Mandarino /* ASoC platform driver */
1411db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1412db2a4165SFrank Mandarino 	.driver		= {
1413db2a4165SFrank Mandarino 		.name		= "soc-audio",
14148b45a209SKay Sievers 		.owner		= THIS_MODULE,
1415416356fcSMark Brown 		.pm		= &soc_pm_ops,
1416db2a4165SFrank Mandarino 	},
1417db2a4165SFrank Mandarino 	.probe		= soc_probe,
1418db2a4165SFrank Mandarino 	.remove		= soc_remove,
1419db2a4165SFrank Mandarino };
1420db2a4165SFrank Mandarino 
1421db2a4165SFrank Mandarino /* create a new pcm */
1422db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev,
1423db2a4165SFrank Mandarino 	struct snd_soc_dai_link *dai_link, int num)
1424db2a4165SFrank Mandarino {
142587689d56SMark Brown 	struct snd_soc_card *card = socdev->card;
14266627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
142787689d56SMark Brown 	struct snd_soc_platform *platform = card->platform;
14283c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai = dai_link->codec_dai;
14293c4b266fSLiam Girdwood 	struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1430db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd;
1431db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1432db2a4165SFrank Mandarino 	char new_name[64];
1433db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1434db2a4165SFrank Mandarino 
1435db2a4165SFrank Mandarino 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1436db2a4165SFrank Mandarino 	if (rtd == NULL)
1437db2a4165SFrank Mandarino 		return -ENOMEM;
1438cb666e5bSLiam Girdwood 
1439cb666e5bSLiam Girdwood 	rtd->dai = dai_link;
1440db2a4165SFrank Mandarino 	rtd->socdev = socdev;
14416627a653SMark Brown 	codec_dai->codec = card->codec;
1442db2a4165SFrank Mandarino 
1443db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
144440ca1142SMark Brown 	snprintf(new_name, sizeof(new_name), "%s %s-%d",
144540ca1142SMark Brown 		 dai_link->stream_name, codec_dai->name, num);
1446db2a4165SFrank Mandarino 
1447db2a4165SFrank Mandarino 	if (codec_dai->playback.channels_min)
1448db2a4165SFrank Mandarino 		playback = 1;
1449db2a4165SFrank Mandarino 	if (codec_dai->capture.channels_min)
1450db2a4165SFrank Mandarino 		capture = 1;
1451db2a4165SFrank Mandarino 
1452db2a4165SFrank Mandarino 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1453db2a4165SFrank Mandarino 		capture, &pcm);
1454db2a4165SFrank Mandarino 	if (ret < 0) {
14553ff3f64bSMark Brown 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
14563ff3f64bSMark Brown 			codec->name);
1457db2a4165SFrank Mandarino 		kfree(rtd);
1458db2a4165SFrank Mandarino 		return ret;
1459db2a4165SFrank Mandarino 	}
1460db2a4165SFrank Mandarino 
14614ccab3e7SLiam Girdwood 	dai_link->pcm = pcm;
1462db2a4165SFrank Mandarino 	pcm->private_data = rtd;
146387689d56SMark Brown 	soc_pcm_ops.mmap = platform->pcm_ops->mmap;
146487689d56SMark Brown 	soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
146587689d56SMark Brown 	soc_pcm_ops.copy = platform->pcm_ops->copy;
146687689d56SMark Brown 	soc_pcm_ops.silence = platform->pcm_ops->silence;
146787689d56SMark Brown 	soc_pcm_ops.ack = platform->pcm_ops->ack;
146887689d56SMark Brown 	soc_pcm_ops.page = platform->pcm_ops->page;
1469db2a4165SFrank Mandarino 
1470db2a4165SFrank Mandarino 	if (playback)
1471db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1472db2a4165SFrank Mandarino 
1473db2a4165SFrank Mandarino 	if (capture)
1474db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1475db2a4165SFrank Mandarino 
147687689d56SMark Brown 	ret = platform->pcm_new(codec->card, codec_dai, pcm);
1477db2a4165SFrank Mandarino 	if (ret < 0) {
1478db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1479db2a4165SFrank Mandarino 		kfree(rtd);
1480db2a4165SFrank Mandarino 		return ret;
1481db2a4165SFrank Mandarino 	}
1482db2a4165SFrank Mandarino 
148387689d56SMark Brown 	pcm->private_free = platform->pcm_free;
1484db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1485db2a4165SFrank Mandarino 		cpu_dai->name);
1486db2a4165SFrank Mandarino 	return ret;
1487db2a4165SFrank Mandarino }
1488db2a4165SFrank Mandarino 
1489096e49d5SMark Brown /**
1490096e49d5SMark Brown  * snd_soc_codec_volatile_register: Report if a register is volatile.
1491096e49d5SMark Brown  *
1492096e49d5SMark Brown  * @codec: CODEC to query.
1493096e49d5SMark Brown  * @reg: Register to query.
1494096e49d5SMark Brown  *
1495096e49d5SMark Brown  * Boolean function indiciating if a CODEC register is volatile.
1496096e49d5SMark Brown  */
1497096e49d5SMark Brown int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1498096e49d5SMark Brown {
1499096e49d5SMark Brown 	if (codec->volatile_register)
1500096e49d5SMark Brown 		return codec->volatile_register(reg);
1501096e49d5SMark Brown 	else
1502096e49d5SMark Brown 		return 0;
1503096e49d5SMark Brown }
1504096e49d5SMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1505096e49d5SMark Brown 
1506db2a4165SFrank Mandarino /**
1507db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1508db2a4165SFrank Mandarino  * @codec: audio codec
1509db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1510db2a4165SFrank Mandarino  * @num: AC97 codec number
1511db2a4165SFrank Mandarino  *
1512db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1513db2a4165SFrank Mandarino  */
1514db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1515db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1516db2a4165SFrank Mandarino {
1517db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1518db2a4165SFrank Mandarino 
1519db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1520db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1521db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1522db2a4165SFrank Mandarino 		return -ENOMEM;
1523db2a4165SFrank Mandarino 	}
1524db2a4165SFrank Mandarino 
1525db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1526db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1527db2a4165SFrank Mandarino 		kfree(codec->ac97);
1528db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1529db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1530db2a4165SFrank Mandarino 		return -ENOMEM;
1531db2a4165SFrank Mandarino 	}
1532db2a4165SFrank Mandarino 
1533db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1534db2a4165SFrank Mandarino 	codec->ac97->num = num;
15352718625fSMark Brown 	codec->dev = &codec->ac97->dev;
1536db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1537db2a4165SFrank Mandarino 	return 0;
1538db2a4165SFrank Mandarino }
1539db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1540db2a4165SFrank Mandarino 
1541db2a4165SFrank Mandarino /**
1542db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1543db2a4165SFrank Mandarino  * @codec: audio codec
1544db2a4165SFrank Mandarino  *
1545db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1546db2a4165SFrank Mandarino  */
1547db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1548db2a4165SFrank Mandarino {
1549db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1550db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1551db2a4165SFrank Mandarino 	kfree(codec->ac97);
1552db2a4165SFrank Mandarino 	codec->ac97 = NULL;
1553db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1554db2a4165SFrank Mandarino }
1555db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1556db2a4165SFrank Mandarino 
1557db2a4165SFrank Mandarino /**
1558db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1559db2a4165SFrank Mandarino  * @codec: audio codec
1560db2a4165SFrank Mandarino  * @reg: codec register
1561db2a4165SFrank Mandarino  * @mask: register mask
1562db2a4165SFrank Mandarino  * @value: new value
1563db2a4165SFrank Mandarino  *
1564db2a4165SFrank Mandarino  * Writes new register value.
1565db2a4165SFrank Mandarino  *
1566db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1567db2a4165SFrank Mandarino  */
1568db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
156946f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1570db2a4165SFrank Mandarino {
1571db2a4165SFrank Mandarino 	int change;
157246f5822fSDaniel Ribeiro 	unsigned int old, new;
1573db2a4165SFrank Mandarino 
1574db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1575db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1576db2a4165SFrank Mandarino 	change = old != new;
1577db2a4165SFrank Mandarino 	if (change)
1578db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1579db2a4165SFrank Mandarino 
1580db2a4165SFrank Mandarino 	return change;
1581db2a4165SFrank Mandarino }
1582db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1583db2a4165SFrank Mandarino 
1584db2a4165SFrank Mandarino /**
15856c508c62SEero Nurkkala  * snd_soc_update_bits_locked - update codec register bits
15866c508c62SEero Nurkkala  * @codec: audio codec
15876c508c62SEero Nurkkala  * @reg: codec register
15886c508c62SEero Nurkkala  * @mask: register mask
15896c508c62SEero Nurkkala  * @value: new value
15906c508c62SEero Nurkkala  *
15916c508c62SEero Nurkkala  * Writes new register value, and takes the codec mutex.
15926c508c62SEero Nurkkala  *
15936c508c62SEero Nurkkala  * Returns 1 for change else 0.
15946c508c62SEero Nurkkala  */
1595dd1b3d53SMark Brown int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
15966c508c62SEero Nurkkala 			       unsigned short reg, unsigned int mask,
15976c508c62SEero Nurkkala 			       unsigned int value)
15986c508c62SEero Nurkkala {
15996c508c62SEero Nurkkala 	int change;
16006c508c62SEero Nurkkala 
16016c508c62SEero Nurkkala 	mutex_lock(&codec->mutex);
16026c508c62SEero Nurkkala 	change = snd_soc_update_bits(codec, reg, mask, value);
16036c508c62SEero Nurkkala 	mutex_unlock(&codec->mutex);
16046c508c62SEero Nurkkala 
16056c508c62SEero Nurkkala 	return change;
16066c508c62SEero Nurkkala }
1607dd1b3d53SMark Brown EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
16086c508c62SEero Nurkkala 
16096c508c62SEero Nurkkala /**
1610db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1611db2a4165SFrank Mandarino  * @codec: audio codec
1612db2a4165SFrank Mandarino  * @reg: codec register
1613db2a4165SFrank Mandarino  * @mask: register mask
1614db2a4165SFrank Mandarino  * @value: new value
1615db2a4165SFrank Mandarino  *
1616db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1617db2a4165SFrank Mandarino  * different from the old value.
1618db2a4165SFrank Mandarino  *
1619db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1620db2a4165SFrank Mandarino  */
1621db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
162246f5822fSDaniel Ribeiro 				unsigned int mask, unsigned int value)
1623db2a4165SFrank Mandarino {
1624db2a4165SFrank Mandarino 	int change;
162546f5822fSDaniel Ribeiro 	unsigned int old, new;
1626db2a4165SFrank Mandarino 
1627db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1628db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1629db2a4165SFrank Mandarino 	change = old != new;
1630db2a4165SFrank Mandarino 
1631db2a4165SFrank Mandarino 	return change;
1632db2a4165SFrank Mandarino }
1633db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1634db2a4165SFrank Mandarino 
1635db2a4165SFrank Mandarino /**
1636db2a4165SFrank Mandarino  * snd_soc_new_pcms - create new sound card and pcms
1637db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1638ac11a2b3SMark Brown  * @idx: ALSA card index
1639ac11a2b3SMark Brown  * @xid: card identification
1640db2a4165SFrank Mandarino  *
1641db2a4165SFrank Mandarino  * Create a new sound card based upon the codec and interface pcms.
1642db2a4165SFrank Mandarino  *
1643db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1644db2a4165SFrank Mandarino  */
1645db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1646db2a4165SFrank Mandarino {
164787506549SMark Brown 	struct snd_soc_card *card = socdev->card;
16486627a653SMark Brown 	struct snd_soc_codec *codec = card->codec;
1649bd7dd77cSTakashi Iwai 	int ret, i;
1650db2a4165SFrank Mandarino 
1651db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1652db2a4165SFrank Mandarino 
1653db2a4165SFrank Mandarino 	/* register a sound card */
1654bd7dd77cSTakashi Iwai 	ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1655bd7dd77cSTakashi Iwai 	if (ret < 0) {
1656db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1657db2a4165SFrank Mandarino 			codec->name);
1658db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1659bd7dd77cSTakashi Iwai 		return ret;
1660db2a4165SFrank Mandarino 	}
1661db2a4165SFrank Mandarino 
1662452c5eaaSMark Brown 	codec->socdev = socdev;
1663db2a4165SFrank Mandarino 	codec->card->dev = socdev->dev;
1664db2a4165SFrank Mandarino 	codec->card->private_data = codec;
1665db2a4165SFrank Mandarino 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1666db2a4165SFrank Mandarino 
1667db2a4165SFrank Mandarino 	/* create the pcms */
166887506549SMark Brown 	for (i = 0; i < card->num_links; i++) {
166987506549SMark Brown 		ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1670db2a4165SFrank Mandarino 		if (ret < 0) {
1671db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't create pcm %s\n",
167287506549SMark Brown 				card->dai_link[i].stream_name);
1673db2a4165SFrank Mandarino 			mutex_unlock(&codec->mutex);
1674db2a4165SFrank Mandarino 			return ret;
1675db2a4165SFrank Mandarino 		}
1676fb48e3c6SGraham Gower 		/* Check for codec->ac97 to handle the ac97.c fun */
1677fb48e3c6SGraham Gower 		if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
1678f7732053SBarry Song 			snd_ac97_dev_add_pdata(codec->ac97,
1679f7732053SBarry Song 				card->dai_link[i].cpu_dai->ac97_pdata);
1680f7732053SBarry Song 		}
1681db2a4165SFrank Mandarino 	}
1682db2a4165SFrank Mandarino 
1683db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1684db2a4165SFrank Mandarino 	return ret;
1685db2a4165SFrank Mandarino }
1686db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1687db2a4165SFrank Mandarino 
1688db2a4165SFrank Mandarino /**
1689db2a4165SFrank Mandarino  * snd_soc_free_pcms - free sound card and pcms
1690db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1691db2a4165SFrank Mandarino  *
1692db2a4165SFrank Mandarino  * Frees sound card and pcms associated with the socdev.
1693db2a4165SFrank Mandarino  * Also unregister the codec if it is an AC97 device.
1694db2a4165SFrank Mandarino  */
1695db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev)
1696db2a4165SFrank Mandarino {
16976627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
1698a68660e0SLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS
16993c4b266fSLiam Girdwood 	struct snd_soc_dai *codec_dai;
1700a68660e0SLiam Girdwood 	int i;
1701a68660e0SLiam Girdwood #endif
1702db2a4165SFrank Mandarino 
1703db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
17046627a653SMark Brown 	soc_cleanup_codec_debugfs(codec);
1705db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1706a68660e0SLiam Girdwood 	for (i = 0; i < codec->num_dai; i++) {
1707a68660e0SLiam Girdwood 		codec_dai = &codec->dai[i];
1708d2314e0eSAtsushi Nemoto 		if (codec_dai->ac97_control && codec->ac97 &&
1709d2314e0eSAtsushi Nemoto 		    strcmp(codec->name, "AC97") != 0) {
1710db2a4165SFrank Mandarino 			soc_ac97_dev_unregister(codec);
1711a68660e0SLiam Girdwood 			goto free_card;
1712a68660e0SLiam Girdwood 		}
1713a68660e0SLiam Girdwood 	}
1714a68660e0SLiam Girdwood free_card:
1715db2a4165SFrank Mandarino #endif
1716db2a4165SFrank Mandarino 
1717db2a4165SFrank Mandarino 	if (codec->card)
1718db2a4165SFrank Mandarino 		snd_card_free(codec->card);
1719db2a4165SFrank Mandarino 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1720db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1721db2a4165SFrank Mandarino }
1722db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1723db2a4165SFrank Mandarino 
1724db2a4165SFrank Mandarino /**
1725db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1726db2a4165SFrank Mandarino  * @substream: the pcm substream
1727db2a4165SFrank Mandarino  * @hw: the hardware parameters
1728db2a4165SFrank Mandarino  *
1729db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1730db2a4165SFrank Mandarino  */
1731db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1732db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1733db2a4165SFrank Mandarino {
1734db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1735db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1736db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1737db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1738db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1739db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1740db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1741db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1742db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1743db2a4165SFrank Mandarino 	return 0;
1744db2a4165SFrank Mandarino }
1745db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1746db2a4165SFrank Mandarino 
1747db2a4165SFrank Mandarino /**
1748db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1749db2a4165SFrank Mandarino  * @_template: control template
1750db2a4165SFrank Mandarino  * @data: control private data
1751ac11a2b3SMark Brown  * @long_name: control long name
1752db2a4165SFrank Mandarino  *
1753db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1754db2a4165SFrank Mandarino  *
1755db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1756db2a4165SFrank Mandarino  */
1757db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1758db2a4165SFrank Mandarino 	void *data, char *long_name)
1759db2a4165SFrank Mandarino {
1760db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1761db2a4165SFrank Mandarino 
1762db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1763db2a4165SFrank Mandarino 	if (long_name)
1764db2a4165SFrank Mandarino 		template.name = long_name;
1765db2a4165SFrank Mandarino 	template.index = 0;
1766db2a4165SFrank Mandarino 
1767db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1768db2a4165SFrank Mandarino }
1769db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1770db2a4165SFrank Mandarino 
1771db2a4165SFrank Mandarino /**
17723e8e1952SIan Molton  * snd_soc_add_controls - add an array of controls to a codec.
17733e8e1952SIan Molton  * Convienience function to add a list of controls. Many codecs were
17743e8e1952SIan Molton  * duplicating this code.
17753e8e1952SIan Molton  *
17763e8e1952SIan Molton  * @codec: codec to add controls to
17773e8e1952SIan Molton  * @controls: array of controls to add
17783e8e1952SIan Molton  * @num_controls: number of elements in the array
17793e8e1952SIan Molton  *
17803e8e1952SIan Molton  * Return 0 for success, else error.
17813e8e1952SIan Molton  */
17823e8e1952SIan Molton int snd_soc_add_controls(struct snd_soc_codec *codec,
17833e8e1952SIan Molton 	const struct snd_kcontrol_new *controls, int num_controls)
17843e8e1952SIan Molton {
17853e8e1952SIan Molton 	struct snd_card *card = codec->card;
17863e8e1952SIan Molton 	int err, i;
17873e8e1952SIan Molton 
17883e8e1952SIan Molton 	for (i = 0; i < num_controls; i++) {
17893e8e1952SIan Molton 		const struct snd_kcontrol_new *control = &controls[i];
17903e8e1952SIan Molton 		err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
17913e8e1952SIan Molton 		if (err < 0) {
17923e8e1952SIan Molton 			dev_err(codec->dev, "%s: Failed to add %s\n",
17933e8e1952SIan Molton 				codec->name, control->name);
17943e8e1952SIan Molton 			return err;
17953e8e1952SIan Molton 		}
17963e8e1952SIan Molton 	}
17973e8e1952SIan Molton 
17983e8e1952SIan Molton 	return 0;
17993e8e1952SIan Molton }
18003e8e1952SIan Molton EXPORT_SYMBOL_GPL(snd_soc_add_controls);
18013e8e1952SIan Molton 
18023e8e1952SIan Molton /**
1803db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1804db2a4165SFrank Mandarino  * @kcontrol: mixer control
1805db2a4165SFrank Mandarino  * @uinfo: control element information
1806db2a4165SFrank Mandarino  *
1807db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
1808db2a4165SFrank Mandarino  * mixer control.
1809db2a4165SFrank Mandarino  *
1810db2a4165SFrank Mandarino  * Returns 0 for success.
1811db2a4165SFrank Mandarino  */
1812db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1813db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1814db2a4165SFrank Mandarino {
1815db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1816db2a4165SFrank Mandarino 
1817db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1818db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1819f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1820db2a4165SFrank Mandarino 
1821f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1822f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1823db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1824db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1825db2a4165SFrank Mandarino 	return 0;
1826db2a4165SFrank Mandarino }
1827db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1828db2a4165SFrank Mandarino 
1829db2a4165SFrank Mandarino /**
1830db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
1831db2a4165SFrank Mandarino  * @kcontrol: mixer control
1832ac11a2b3SMark Brown  * @ucontrol: control element information
1833db2a4165SFrank Mandarino  *
1834db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
1835db2a4165SFrank Mandarino  *
1836db2a4165SFrank Mandarino  * Returns 0 for success.
1837db2a4165SFrank Mandarino  */
1838db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1839db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1840db2a4165SFrank Mandarino {
1841db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1842db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
184346f5822fSDaniel Ribeiro 	unsigned int val, bitmask;
1844db2a4165SFrank Mandarino 
1845f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1846db2a4165SFrank Mandarino 		;
1847db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
18483ff3f64bSMark Brown 	ucontrol->value.enumerated.item[0]
18493ff3f64bSMark Brown 		= (val >> e->shift_l) & (bitmask - 1);
1850db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
1851db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
1852db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
1853db2a4165SFrank Mandarino 
1854db2a4165SFrank Mandarino 	return 0;
1855db2a4165SFrank Mandarino }
1856db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1857db2a4165SFrank Mandarino 
1858db2a4165SFrank Mandarino /**
1859db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
1860db2a4165SFrank Mandarino  * @kcontrol: mixer control
1861ac11a2b3SMark Brown  * @ucontrol: control element information
1862db2a4165SFrank Mandarino  *
1863db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
1864db2a4165SFrank Mandarino  *
1865db2a4165SFrank Mandarino  * Returns 0 for success.
1866db2a4165SFrank Mandarino  */
1867db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1868db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1869db2a4165SFrank Mandarino {
1870db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1871db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
187246f5822fSDaniel Ribeiro 	unsigned int val;
187346f5822fSDaniel Ribeiro 	unsigned int mask, bitmask;
1874db2a4165SFrank Mandarino 
1875f8ba0b7bSJon Smirl 	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1876db2a4165SFrank Mandarino 		;
1877f8ba0b7bSJon Smirl 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
1878db2a4165SFrank Mandarino 		return -EINVAL;
1879db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1880db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
1881db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
1882f8ba0b7bSJon Smirl 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
1883db2a4165SFrank Mandarino 			return -EINVAL;
1884db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1885db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
1886db2a4165SFrank Mandarino 	}
1887db2a4165SFrank Mandarino 
18886c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1889db2a4165SFrank Mandarino }
1890db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1891db2a4165SFrank Mandarino 
1892db2a4165SFrank Mandarino /**
18932e72f8e3SPeter Ujfalusi  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
18942e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
18952e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
18962e72f8e3SPeter Ujfalusi  *
18972e72f8e3SPeter Ujfalusi  * Callback to get the value of a double semi enumerated mixer.
18982e72f8e3SPeter Ujfalusi  *
18992e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
19002e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
19012e72f8e3SPeter Ujfalusi  *
19022e72f8e3SPeter Ujfalusi  * Returns 0 for success.
19032e72f8e3SPeter Ujfalusi  */
19042e72f8e3SPeter Ujfalusi int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
19052e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
19062e72f8e3SPeter Ujfalusi {
19072e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
190874155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
190946f5822fSDaniel Ribeiro 	unsigned int reg_val, val, mux;
19102e72f8e3SPeter Ujfalusi 
19112e72f8e3SPeter Ujfalusi 	reg_val = snd_soc_read(codec, e->reg);
19122e72f8e3SPeter Ujfalusi 	val = (reg_val >> e->shift_l) & e->mask;
19132e72f8e3SPeter Ujfalusi 	for (mux = 0; mux < e->max; mux++) {
19142e72f8e3SPeter Ujfalusi 		if (val == e->values[mux])
19152e72f8e3SPeter Ujfalusi 			break;
19162e72f8e3SPeter Ujfalusi 	}
19172e72f8e3SPeter Ujfalusi 	ucontrol->value.enumerated.item[0] = mux;
19182e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
19192e72f8e3SPeter Ujfalusi 		val = (reg_val >> e->shift_r) & e->mask;
19202e72f8e3SPeter Ujfalusi 		for (mux = 0; mux < e->max; mux++) {
19212e72f8e3SPeter Ujfalusi 			if (val == e->values[mux])
19222e72f8e3SPeter Ujfalusi 				break;
19232e72f8e3SPeter Ujfalusi 		}
19242e72f8e3SPeter Ujfalusi 		ucontrol->value.enumerated.item[1] = mux;
19252e72f8e3SPeter Ujfalusi 	}
19262e72f8e3SPeter Ujfalusi 
19272e72f8e3SPeter Ujfalusi 	return 0;
19282e72f8e3SPeter Ujfalusi }
19292e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
19302e72f8e3SPeter Ujfalusi 
19312e72f8e3SPeter Ujfalusi /**
19322e72f8e3SPeter Ujfalusi  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
19332e72f8e3SPeter Ujfalusi  * @kcontrol: mixer control
19342e72f8e3SPeter Ujfalusi  * @ucontrol: control element information
19352e72f8e3SPeter Ujfalusi  *
19362e72f8e3SPeter Ujfalusi  * Callback to set the value of a double semi enumerated mixer.
19372e72f8e3SPeter Ujfalusi  *
19382e72f8e3SPeter Ujfalusi  * Semi enumerated mixer: the enumerated items are referred as values. Can be
19392e72f8e3SPeter Ujfalusi  * used for handling bitfield coded enumeration for example.
19402e72f8e3SPeter Ujfalusi  *
19412e72f8e3SPeter Ujfalusi  * Returns 0 for success.
19422e72f8e3SPeter Ujfalusi  */
19432e72f8e3SPeter Ujfalusi int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
19442e72f8e3SPeter Ujfalusi 	struct snd_ctl_elem_value *ucontrol)
19452e72f8e3SPeter Ujfalusi {
19462e72f8e3SPeter Ujfalusi 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
194774155556SPeter Ujfalusi 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
194846f5822fSDaniel Ribeiro 	unsigned int val;
194946f5822fSDaniel Ribeiro 	unsigned int mask;
19502e72f8e3SPeter Ujfalusi 
19512e72f8e3SPeter Ujfalusi 	if (ucontrol->value.enumerated.item[0] > e->max - 1)
19522e72f8e3SPeter Ujfalusi 		return -EINVAL;
19532e72f8e3SPeter Ujfalusi 	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
19542e72f8e3SPeter Ujfalusi 	mask = e->mask << e->shift_l;
19552e72f8e3SPeter Ujfalusi 	if (e->shift_l != e->shift_r) {
19562e72f8e3SPeter Ujfalusi 		if (ucontrol->value.enumerated.item[1] > e->max - 1)
19572e72f8e3SPeter Ujfalusi 			return -EINVAL;
19582e72f8e3SPeter Ujfalusi 		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
19592e72f8e3SPeter Ujfalusi 		mask |= e->mask << e->shift_r;
19602e72f8e3SPeter Ujfalusi 	}
19612e72f8e3SPeter Ujfalusi 
19626c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, e->reg, mask, val);
19632e72f8e3SPeter Ujfalusi }
19642e72f8e3SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
19652e72f8e3SPeter Ujfalusi 
19662e72f8e3SPeter Ujfalusi /**
1967db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1968db2a4165SFrank Mandarino  * @kcontrol: mixer control
1969db2a4165SFrank Mandarino  * @uinfo: control element information
1970db2a4165SFrank Mandarino  *
1971db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
1972db2a4165SFrank Mandarino  * single mixer.
1973db2a4165SFrank Mandarino  *
1974db2a4165SFrank Mandarino  * Returns 0 for success.
1975db2a4165SFrank Mandarino  */
1976db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1977db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1978db2a4165SFrank Mandarino {
1979db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1980db2a4165SFrank Mandarino 
1981db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1982db2a4165SFrank Mandarino 	uinfo->count = 1;
1983f8ba0b7bSJon Smirl 	uinfo->value.enumerated.items = e->max;
1984db2a4165SFrank Mandarino 
1985f8ba0b7bSJon Smirl 	if (uinfo->value.enumerated.item > e->max - 1)
1986f8ba0b7bSJon Smirl 		uinfo->value.enumerated.item = e->max - 1;
1987db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1988db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1989db2a4165SFrank Mandarino 	return 0;
1990db2a4165SFrank Mandarino }
1991db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1992db2a4165SFrank Mandarino 
1993db2a4165SFrank Mandarino /**
1994db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
1995db2a4165SFrank Mandarino  * @kcontrol: mixer control
1996db2a4165SFrank Mandarino  * @uinfo: control element information
1997db2a4165SFrank Mandarino  *
1998db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
1999db2a4165SFrank Mandarino  *
2000db2a4165SFrank Mandarino  * Returns 0 for success.
2001db2a4165SFrank Mandarino  */
2002db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2003db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2004db2a4165SFrank Mandarino {
2005a7a4ac86SPhilipp Zabel 	int max = kcontrol->private_value;
2006db2a4165SFrank Mandarino 
2007fd5dfad9SMark Brown 	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2008a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2009a7a4ac86SPhilipp Zabel 	else
2010a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2011a7a4ac86SPhilipp Zabel 
2012db2a4165SFrank Mandarino 	uinfo->count = 1;
2013db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2014a7a4ac86SPhilipp Zabel 	uinfo->value.integer.max = max;
2015db2a4165SFrank Mandarino 	return 0;
2016db2a4165SFrank Mandarino }
2017db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2018db2a4165SFrank Mandarino 
2019db2a4165SFrank Mandarino /**
2020db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
2021db2a4165SFrank Mandarino  * @kcontrol: mixer control
2022db2a4165SFrank Mandarino  * @uinfo: control element information
2023db2a4165SFrank Mandarino  *
2024db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
2025db2a4165SFrank Mandarino  *
2026db2a4165SFrank Mandarino  * Returns 0 for success.
2027db2a4165SFrank Mandarino  */
2028db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2029db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2030db2a4165SFrank Mandarino {
20314eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20324eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2033d11bb4a9SPeter Ujfalusi 	int platform_max;
2034762b8df7SMark Brown 	unsigned int shift = mc->shift;
2035815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
2036db2a4165SFrank Mandarino 
2037d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2038d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2039d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2040d11bb4a9SPeter Ujfalusi 
2041d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2042a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2043a7a4ac86SPhilipp Zabel 	else
2044a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2045a7a4ac86SPhilipp Zabel 
2046db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
2047db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2048d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2049db2a4165SFrank Mandarino 	return 0;
2050db2a4165SFrank Mandarino }
2051db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2052db2a4165SFrank Mandarino 
2053db2a4165SFrank Mandarino /**
2054db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
2055db2a4165SFrank Mandarino  * @kcontrol: mixer control
2056ac11a2b3SMark Brown  * @ucontrol: control element information
2057db2a4165SFrank Mandarino  *
2058db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
2059db2a4165SFrank Mandarino  *
2060db2a4165SFrank Mandarino  * Returns 0 for success.
2061db2a4165SFrank Mandarino  */
2062db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2063db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2064db2a4165SFrank Mandarino {
20654eaa9819SJon Smirl 	struct soc_mixer_control *mc =
20664eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2067db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2068815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2069815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2070815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
20714eaa9819SJon Smirl 	int max = mc->max;
2072815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2073815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2074db2a4165SFrank Mandarino 
2075db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2076db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2077db2a4165SFrank Mandarino 	if (shift != rshift)
2078db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2079db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
2080db2a4165SFrank Mandarino 	if (invert) {
2081db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2082a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2083db2a4165SFrank Mandarino 		if (shift != rshift)
2084db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
2085a7a4ac86SPhilipp Zabel 				max - ucontrol->value.integer.value[1];
2086db2a4165SFrank Mandarino 	}
2087db2a4165SFrank Mandarino 
2088db2a4165SFrank Mandarino 	return 0;
2089db2a4165SFrank Mandarino }
2090db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2091db2a4165SFrank Mandarino 
2092db2a4165SFrank Mandarino /**
2093db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
2094db2a4165SFrank Mandarino  * @kcontrol: mixer control
2095ac11a2b3SMark Brown  * @ucontrol: control element information
2096db2a4165SFrank Mandarino  *
2097db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
2098db2a4165SFrank Mandarino  *
2099db2a4165SFrank Mandarino  * Returns 0 for success.
2100db2a4165SFrank Mandarino  */
2101db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2102db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2103db2a4165SFrank Mandarino {
21044eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21054eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2106db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2107815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2108815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
2109815ecf8dSJon Smirl 	unsigned int rshift = mc->rshift;
21104eaa9819SJon Smirl 	int max = mc->max;
2111815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2112815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
211346f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2114db2a4165SFrank Mandarino 
2115db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2116db2a4165SFrank Mandarino 	if (invert)
2117a7a4ac86SPhilipp Zabel 		val = max - val;
2118db2a4165SFrank Mandarino 	val_mask = mask << shift;
2119db2a4165SFrank Mandarino 	val = val << shift;
2120db2a4165SFrank Mandarino 	if (shift != rshift) {
2121db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
2122db2a4165SFrank Mandarino 		if (invert)
2123a7a4ac86SPhilipp Zabel 			val2 = max - val2;
2124db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
2125db2a4165SFrank Mandarino 		val |= val2 << rshift;
2126db2a4165SFrank Mandarino 	}
21276c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2128db2a4165SFrank Mandarino }
2129db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2130db2a4165SFrank Mandarino 
2131db2a4165SFrank Mandarino /**
2132db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
2133db2a4165SFrank Mandarino  * @kcontrol: mixer control
2134db2a4165SFrank Mandarino  * @uinfo: control element information
2135db2a4165SFrank Mandarino  *
2136db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
2137db2a4165SFrank Mandarino  * spans 2 codec registers.
2138db2a4165SFrank Mandarino  *
2139db2a4165SFrank Mandarino  * Returns 0 for success.
2140db2a4165SFrank Mandarino  */
2141db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2142db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
2143db2a4165SFrank Mandarino {
21444eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21454eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2146d11bb4a9SPeter Ujfalusi 	int platform_max;
2147db2a4165SFrank Mandarino 
2148d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2149d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2150d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2151d11bb4a9SPeter Ujfalusi 
2152d11bb4a9SPeter Ujfalusi 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2153a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2154a7a4ac86SPhilipp Zabel 	else
2155a7a4ac86SPhilipp Zabel 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2156a7a4ac86SPhilipp Zabel 
2157db2a4165SFrank Mandarino 	uinfo->count = 2;
2158db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
2159d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max;
2160db2a4165SFrank Mandarino 	return 0;
2161db2a4165SFrank Mandarino }
2162db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2163db2a4165SFrank Mandarino 
2164db2a4165SFrank Mandarino /**
2165db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
2166db2a4165SFrank Mandarino  * @kcontrol: mixer control
2167ac11a2b3SMark Brown  * @ucontrol: control element information
2168db2a4165SFrank Mandarino  *
2169db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
2170db2a4165SFrank Mandarino  *
2171db2a4165SFrank Mandarino  * Returns 0 for success.
2172db2a4165SFrank Mandarino  */
2173db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2174db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2175db2a4165SFrank Mandarino {
21764eaa9819SJon Smirl 	struct soc_mixer_control *mc =
21774eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2178db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2179815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2180815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2181815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
21824eaa9819SJon Smirl 	int max = mc->max;
2183815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2184815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2185db2a4165SFrank Mandarino 
2186db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
2187db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
2188db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
2189db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
2190db2a4165SFrank Mandarino 	if (invert) {
2191db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
2192a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[0];
2193db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
2194a7a4ac86SPhilipp Zabel 			max - ucontrol->value.integer.value[1];
2195db2a4165SFrank Mandarino 	}
2196db2a4165SFrank Mandarino 
2197db2a4165SFrank Mandarino 	return 0;
2198db2a4165SFrank Mandarino }
2199db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2200db2a4165SFrank Mandarino 
2201db2a4165SFrank Mandarino /**
2202db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
2203db2a4165SFrank Mandarino  * @kcontrol: mixer control
2204ac11a2b3SMark Brown  * @ucontrol: control element information
2205db2a4165SFrank Mandarino  *
2206db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
2207db2a4165SFrank Mandarino  *
2208db2a4165SFrank Mandarino  * Returns 0 for success.
2209db2a4165SFrank Mandarino  */
2210db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2211db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
2212db2a4165SFrank Mandarino {
22134eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22144eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2215db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2216815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
2217815ecf8dSJon Smirl 	unsigned int reg2 = mc->rreg;
2218815ecf8dSJon Smirl 	unsigned int shift = mc->shift;
22194eaa9819SJon Smirl 	int max = mc->max;
2220815ecf8dSJon Smirl 	unsigned int mask = (1 << fls(max)) - 1;
2221815ecf8dSJon Smirl 	unsigned int invert = mc->invert;
2222db2a4165SFrank Mandarino 	int err;
222346f5822fSDaniel Ribeiro 	unsigned int val, val2, val_mask;
2224db2a4165SFrank Mandarino 
2225db2a4165SFrank Mandarino 	val_mask = mask << shift;
2226db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
2227db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
2228db2a4165SFrank Mandarino 
2229db2a4165SFrank Mandarino 	if (invert) {
2230a7a4ac86SPhilipp Zabel 		val = max - val;
2231a7a4ac86SPhilipp Zabel 		val2 = max - val2;
2232db2a4165SFrank Mandarino 	}
2233db2a4165SFrank Mandarino 
2234db2a4165SFrank Mandarino 	val = val << shift;
2235db2a4165SFrank Mandarino 	val2 = val2 << shift;
2236db2a4165SFrank Mandarino 
22376c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
22383ff3f64bSMark Brown 	if (err < 0)
2239db2a4165SFrank Mandarino 		return err;
2240db2a4165SFrank Mandarino 
22416c508c62SEero Nurkkala 	err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2242db2a4165SFrank Mandarino 	return err;
2243db2a4165SFrank Mandarino }
2244db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2245db2a4165SFrank Mandarino 
2246e13ac2e9SMark Brown /**
2247e13ac2e9SMark Brown  * snd_soc_info_volsw_s8 - signed mixer info callback
2248e13ac2e9SMark Brown  * @kcontrol: mixer control
2249e13ac2e9SMark Brown  * @uinfo: control element information
2250e13ac2e9SMark Brown  *
2251e13ac2e9SMark Brown  * Callback to provide information about a signed mixer control.
2252e13ac2e9SMark Brown  *
2253e13ac2e9SMark Brown  * Returns 0 for success.
2254e13ac2e9SMark Brown  */
2255e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2256e13ac2e9SMark Brown 	struct snd_ctl_elem_info *uinfo)
2257e13ac2e9SMark Brown {
22584eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22594eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2260d11bb4a9SPeter Ujfalusi 	int platform_max;
22614eaa9819SJon Smirl 	int min = mc->min;
2262e13ac2e9SMark Brown 
2263d11bb4a9SPeter Ujfalusi 	if (!mc->platform_max)
2264d11bb4a9SPeter Ujfalusi 		mc->platform_max = mc->max;
2265d11bb4a9SPeter Ujfalusi 	platform_max = mc->platform_max;
2266d11bb4a9SPeter Ujfalusi 
2267e13ac2e9SMark Brown 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2268e13ac2e9SMark Brown 	uinfo->count = 2;
2269e13ac2e9SMark Brown 	uinfo->value.integer.min = 0;
2270d11bb4a9SPeter Ujfalusi 	uinfo->value.integer.max = platform_max - min;
2271e13ac2e9SMark Brown 	return 0;
2272e13ac2e9SMark Brown }
2273e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2274e13ac2e9SMark Brown 
2275e13ac2e9SMark Brown /**
2276e13ac2e9SMark Brown  * snd_soc_get_volsw_s8 - signed mixer get callback
2277e13ac2e9SMark Brown  * @kcontrol: mixer control
2278ac11a2b3SMark Brown  * @ucontrol: control element information
2279e13ac2e9SMark Brown  *
2280e13ac2e9SMark Brown  * Callback to get the value of a signed mixer control.
2281e13ac2e9SMark Brown  *
2282e13ac2e9SMark Brown  * Returns 0 for success.
2283e13ac2e9SMark Brown  */
2284e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2285e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2286e13ac2e9SMark Brown {
22874eaa9819SJon Smirl 	struct soc_mixer_control *mc =
22884eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2289e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2290815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
22914eaa9819SJon Smirl 	int min = mc->min;
2292e13ac2e9SMark Brown 	int val = snd_soc_read(codec, reg);
2293e13ac2e9SMark Brown 
2294e13ac2e9SMark Brown 	ucontrol->value.integer.value[0] =
2295e13ac2e9SMark Brown 		((signed char)(val & 0xff))-min;
2296e13ac2e9SMark Brown 	ucontrol->value.integer.value[1] =
2297e13ac2e9SMark Brown 		((signed char)((val >> 8) & 0xff))-min;
2298e13ac2e9SMark Brown 	return 0;
2299e13ac2e9SMark Brown }
2300e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2301e13ac2e9SMark Brown 
2302e13ac2e9SMark Brown /**
2303e13ac2e9SMark Brown  * snd_soc_put_volsw_sgn - signed mixer put callback
2304e13ac2e9SMark Brown  * @kcontrol: mixer control
2305ac11a2b3SMark Brown  * @ucontrol: control element information
2306e13ac2e9SMark Brown  *
2307e13ac2e9SMark Brown  * Callback to set the value of a signed mixer control.
2308e13ac2e9SMark Brown  *
2309e13ac2e9SMark Brown  * Returns 0 for success.
2310e13ac2e9SMark Brown  */
2311e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2312e13ac2e9SMark Brown 	struct snd_ctl_elem_value *ucontrol)
2313e13ac2e9SMark Brown {
23144eaa9819SJon Smirl 	struct soc_mixer_control *mc =
23154eaa9819SJon Smirl 		(struct soc_mixer_control *)kcontrol->private_value;
2316e13ac2e9SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2317815ecf8dSJon Smirl 	unsigned int reg = mc->reg;
23184eaa9819SJon Smirl 	int min = mc->min;
231946f5822fSDaniel Ribeiro 	unsigned int val;
2320e13ac2e9SMark Brown 
2321e13ac2e9SMark Brown 	val = (ucontrol->value.integer.value[0]+min) & 0xff;
2322e13ac2e9SMark Brown 	val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2323e13ac2e9SMark Brown 
23246c508c62SEero Nurkkala 	return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2325e13ac2e9SMark Brown }
2326e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2327e13ac2e9SMark Brown 
23288c6529dbSLiam Girdwood /**
2329637d3847SPeter Ujfalusi  * snd_soc_limit_volume - Set new limit to an existing volume control.
2330637d3847SPeter Ujfalusi  *
2331637d3847SPeter Ujfalusi  * @codec: where to look for the control
2332637d3847SPeter Ujfalusi  * @name: Name of the control
2333637d3847SPeter Ujfalusi  * @max: new maximum limit
2334637d3847SPeter Ujfalusi  *
2335637d3847SPeter Ujfalusi  * Return 0 for success, else error.
2336637d3847SPeter Ujfalusi  */
2337637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec,
2338637d3847SPeter Ujfalusi 	const char *name, int max)
2339637d3847SPeter Ujfalusi {
2340637d3847SPeter Ujfalusi 	struct snd_card *card = codec->card;
2341637d3847SPeter Ujfalusi 	struct snd_kcontrol *kctl;
2342637d3847SPeter Ujfalusi 	struct soc_mixer_control *mc;
2343637d3847SPeter Ujfalusi 	int found = 0;
2344637d3847SPeter Ujfalusi 	int ret = -EINVAL;
2345637d3847SPeter Ujfalusi 
2346637d3847SPeter Ujfalusi 	/* Sanity check for name and max */
2347637d3847SPeter Ujfalusi 	if (unlikely(!name || max <= 0))
2348637d3847SPeter Ujfalusi 		return -EINVAL;
2349637d3847SPeter Ujfalusi 
2350637d3847SPeter Ujfalusi 	list_for_each_entry(kctl, &card->controls, list) {
2351637d3847SPeter Ujfalusi 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2352637d3847SPeter Ujfalusi 			found = 1;
2353637d3847SPeter Ujfalusi 			break;
2354637d3847SPeter Ujfalusi 		}
2355637d3847SPeter Ujfalusi 	}
2356637d3847SPeter Ujfalusi 	if (found) {
2357637d3847SPeter Ujfalusi 		mc = (struct soc_mixer_control *)kctl->private_value;
2358637d3847SPeter Ujfalusi 		if (max <= mc->max) {
2359d11bb4a9SPeter Ujfalusi 			mc->platform_max = max;
2360637d3847SPeter Ujfalusi 			ret = 0;
2361637d3847SPeter Ujfalusi 		}
2362637d3847SPeter Ujfalusi 	}
2363637d3847SPeter Ujfalusi 	return ret;
2364637d3847SPeter Ujfalusi }
2365637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2366637d3847SPeter Ujfalusi 
2367637d3847SPeter Ujfalusi /**
2368b6f4bb38Sapatard@mandriva.com  * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2369b6f4bb38Sapatard@mandriva.com  *  mixer info callback
2370b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2371b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2372b6f4bb38Sapatard@mandriva.com  *
2373b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2374b6f4bb38Sapatard@mandriva.com  */
2375b6f4bb38Sapatard@mandriva.com int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2376b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_info *uinfo)
2377b6f4bb38Sapatard@mandriva.com {
2378b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2379b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2380b6f4bb38Sapatard@mandriva.com 	int max = mc->max;
2381b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2382b6f4bb38Sapatard@mandriva.com 
2383b6f4bb38Sapatard@mandriva.com 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2384b6f4bb38Sapatard@mandriva.com 	uinfo->count = 2;
2385b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.min = 0;
2386b6f4bb38Sapatard@mandriva.com 	uinfo->value.integer.max = max-min;
2387b6f4bb38Sapatard@mandriva.com 
2388b6f4bb38Sapatard@mandriva.com 	return 0;
2389b6f4bb38Sapatard@mandriva.com }
2390b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2391b6f4bb38Sapatard@mandriva.com 
2392b6f4bb38Sapatard@mandriva.com /**
2393b6f4bb38Sapatard@mandriva.com  * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2394b6f4bb38Sapatard@mandriva.com  *  mixer get callback
2395b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2396b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2397b6f4bb38Sapatard@mandriva.com  *
2398b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2399b6f4bb38Sapatard@mandriva.com  */
2400b6f4bb38Sapatard@mandriva.com int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2401b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2402b6f4bb38Sapatard@mandriva.com {
2403b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2404b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2405b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2406b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2407b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2408b6f4bb38Sapatard@mandriva.com 	int val = snd_soc_read(codec, mc->reg) & mask;
2409b6f4bb38Sapatard@mandriva.com 	int valr = snd_soc_read(codec, mc->rreg) & mask;
2410b6f4bb38Sapatard@mandriva.com 
241120630c7fSStuart Longland 	ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
241220630c7fSStuart Longland 	ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2413b6f4bb38Sapatard@mandriva.com 	return 0;
2414b6f4bb38Sapatard@mandriva.com }
2415b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2416b6f4bb38Sapatard@mandriva.com 
2417b6f4bb38Sapatard@mandriva.com /**
2418b6f4bb38Sapatard@mandriva.com  * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2419b6f4bb38Sapatard@mandriva.com  *  mixer put callback
2420b6f4bb38Sapatard@mandriva.com  * @kcontrol: mixer control
2421b6f4bb38Sapatard@mandriva.com  * @uinfo: control element information
2422b6f4bb38Sapatard@mandriva.com  *
2423b6f4bb38Sapatard@mandriva.com  * Returns 0 for success.
2424b6f4bb38Sapatard@mandriva.com  */
2425b6f4bb38Sapatard@mandriva.com int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2426b6f4bb38Sapatard@mandriva.com 			struct snd_ctl_elem_value *ucontrol)
2427b6f4bb38Sapatard@mandriva.com {
2428b6f4bb38Sapatard@mandriva.com 	struct soc_mixer_control *mc =
2429b6f4bb38Sapatard@mandriva.com 		(struct soc_mixer_control *)kcontrol->private_value;
2430b6f4bb38Sapatard@mandriva.com 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2431b6f4bb38Sapatard@mandriva.com 	unsigned int mask = (1<<mc->shift)-1;
2432b6f4bb38Sapatard@mandriva.com 	int min = mc->min;
2433b6f4bb38Sapatard@mandriva.com 	int ret;
2434b6f4bb38Sapatard@mandriva.com 	unsigned int val, valr, oval, ovalr;
2435b6f4bb38Sapatard@mandriva.com 
2436b6f4bb38Sapatard@mandriva.com 	val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2437b6f4bb38Sapatard@mandriva.com 	val &= mask;
2438b6f4bb38Sapatard@mandriva.com 	valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2439b6f4bb38Sapatard@mandriva.com 	valr &= mask;
2440b6f4bb38Sapatard@mandriva.com 
2441b6f4bb38Sapatard@mandriva.com 	oval = snd_soc_read(codec, mc->reg) & mask;
2442b6f4bb38Sapatard@mandriva.com 	ovalr = snd_soc_read(codec, mc->rreg) & mask;
2443b6f4bb38Sapatard@mandriva.com 
2444b6f4bb38Sapatard@mandriva.com 	ret = 0;
2445b6f4bb38Sapatard@mandriva.com 	if (oval != val) {
2446b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->reg, val);
2447b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2448f1df5aecSMark Brown 			return ret;
2449b6f4bb38Sapatard@mandriva.com 	}
2450b6f4bb38Sapatard@mandriva.com 	if (ovalr != valr) {
2451b6f4bb38Sapatard@mandriva.com 		ret = snd_soc_write(codec, mc->rreg, valr);
2452b6f4bb38Sapatard@mandriva.com 		if (ret < 0)
2453f1df5aecSMark Brown 			return ret;
2454b6f4bb38Sapatard@mandriva.com 	}
2455b6f4bb38Sapatard@mandriva.com 
2456b6f4bb38Sapatard@mandriva.com 	return 0;
2457b6f4bb38Sapatard@mandriva.com }
2458b6f4bb38Sapatard@mandriva.com EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2459b6f4bb38Sapatard@mandriva.com 
2460b6f4bb38Sapatard@mandriva.com /**
24618c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
24628c6529dbSLiam Girdwood  * @dai: DAI
24638c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
24648c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
24658c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
24668c6529dbSLiam Girdwood  *
24678c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
24688c6529dbSLiam Girdwood  */
24698c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
24708c6529dbSLiam Girdwood 	unsigned int freq, int dir)
24718c6529dbSLiam Girdwood {
24723f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_sysclk)
24736335d055SEric Miao 		return dai->ops->set_sysclk(dai, clk_id, freq, dir);
24748c6529dbSLiam Girdwood 	else
24758c6529dbSLiam Girdwood 		return -EINVAL;
24768c6529dbSLiam Girdwood }
24778c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
24788c6529dbSLiam Girdwood 
24798c6529dbSLiam Girdwood /**
24808c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
24818c6529dbSLiam Girdwood  * @dai: DAI
2482ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
24838c6529dbSLiam Girdwood  * @div: new clock divisor.
24848c6529dbSLiam Girdwood  *
24858c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
24868c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
24878c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
24888c6529dbSLiam Girdwood  */
24898c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
24908c6529dbSLiam Girdwood 	int div_id, int div)
24918c6529dbSLiam Girdwood {
24923f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_clkdiv)
24936335d055SEric Miao 		return dai->ops->set_clkdiv(dai, div_id, div);
24948c6529dbSLiam Girdwood 	else
24958c6529dbSLiam Girdwood 		return -EINVAL;
24968c6529dbSLiam Girdwood }
24978c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
24988c6529dbSLiam Girdwood 
24998c6529dbSLiam Girdwood /**
25008c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
25018c6529dbSLiam Girdwood  * @dai: DAI
25028c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
250385488037SMark Brown  * @source: DAI specific source for the PLL
25048c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
25058c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
25068c6529dbSLiam Girdwood  *
25078c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
25088c6529dbSLiam Girdwood  */
250985488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
251085488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
25118c6529dbSLiam Girdwood {
25123f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_pll)
251385488037SMark Brown 		return dai->ops->set_pll(dai, pll_id, source,
251485488037SMark Brown 					 freq_in, freq_out);
25158c6529dbSLiam Girdwood 	else
25168c6529dbSLiam Girdwood 		return -EINVAL;
25178c6529dbSLiam Girdwood }
25188c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
25198c6529dbSLiam Girdwood 
25208c6529dbSLiam Girdwood /**
25218c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
25228c6529dbSLiam Girdwood  * @dai: DAI
25238c6529dbSLiam Girdwood  * @fmt: SND_SOC_DAIFMT_ format value.
25248c6529dbSLiam Girdwood  *
25258c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
25268c6529dbSLiam Girdwood  */
25278c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
25288c6529dbSLiam Girdwood {
25293f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_fmt)
25306335d055SEric Miao 		return dai->ops->set_fmt(dai, fmt);
25318c6529dbSLiam Girdwood 	else
25328c6529dbSLiam Girdwood 		return -EINVAL;
25338c6529dbSLiam Girdwood }
25348c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
25358c6529dbSLiam Girdwood 
25368c6529dbSLiam Girdwood /**
25378c6529dbSLiam Girdwood  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
25388c6529dbSLiam Girdwood  * @dai: DAI
2539a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2540a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25418c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2542a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25438c6529dbSLiam Girdwood  *
25448c6529dbSLiam Girdwood  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
25458c6529dbSLiam Girdwood  * specific.
25468c6529dbSLiam Girdwood  */
25478c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2548a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25498c6529dbSLiam Girdwood {
25503f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tdm_slot)
2551a5479e38SDaniel Ribeiro 		return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2552a5479e38SDaniel Ribeiro 				slots, slot_width);
25538c6529dbSLiam Girdwood 	else
25548c6529dbSLiam Girdwood 		return -EINVAL;
25558c6529dbSLiam Girdwood }
25568c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25578c6529dbSLiam Girdwood 
25588c6529dbSLiam Girdwood /**
2559472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2560472df3cbSBarry Song  * @dai: DAI
2561472df3cbSBarry Song  * @tx_num: how many TX channels
2562472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2563472df3cbSBarry Song  *           0~num-1 uses
2564472df3cbSBarry Song  * @rx_num: how many RX channels
2565472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2566472df3cbSBarry Song  *           0~num-1 uses
2567472df3cbSBarry Song  *
2568472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2569472df3cbSBarry Song  */
2570472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2571472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2572472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2573472df3cbSBarry Song {
2574472df3cbSBarry Song 	if (dai->ops && dai->ops->set_channel_map)
2575472df3cbSBarry Song 		return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2576472df3cbSBarry Song 			rx_num, rx_slot);
2577472df3cbSBarry Song 	else
2578472df3cbSBarry Song 		return -EINVAL;
2579472df3cbSBarry Song }
2580472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2581472df3cbSBarry Song 
2582472df3cbSBarry Song /**
25838c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
25848c6529dbSLiam Girdwood  * @dai: DAI
25858c6529dbSLiam Girdwood  * @tristate: tristate enable
25868c6529dbSLiam Girdwood  *
25878c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
25888c6529dbSLiam Girdwood  */
25898c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
25908c6529dbSLiam Girdwood {
25913f1a4d82SMark Brown 	if (dai->ops && dai->ops->set_tristate)
25926335d055SEric Miao 		return dai->ops->set_tristate(dai, tristate);
25938c6529dbSLiam Girdwood 	else
25948c6529dbSLiam Girdwood 		return -EINVAL;
25958c6529dbSLiam Girdwood }
25968c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
25978c6529dbSLiam Girdwood 
25988c6529dbSLiam Girdwood /**
25998c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
26008c6529dbSLiam Girdwood  * @dai: DAI
26018c6529dbSLiam Girdwood  * @mute: mute enable
26028c6529dbSLiam Girdwood  *
26038c6529dbSLiam Girdwood  * Mutes the DAI DAC.
26048c6529dbSLiam Girdwood  */
26058c6529dbSLiam Girdwood int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
26068c6529dbSLiam Girdwood {
26073f1a4d82SMark Brown 	if (dai->ops && dai->ops->digital_mute)
26086335d055SEric Miao 		return dai->ops->digital_mute(dai, mute);
26098c6529dbSLiam Girdwood 	else
26108c6529dbSLiam Girdwood 		return -EINVAL;
26118c6529dbSLiam Girdwood }
26128c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26138c6529dbSLiam Girdwood 
2614c5af3a2eSMark Brown /**
2615c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2616c5af3a2eSMark Brown  *
2617ac11a2b3SMark Brown  * @card: Card to register
2618c5af3a2eSMark Brown  *
2619c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2620c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2621c5af3a2eSMark Brown  * registration APIs.
2622c5af3a2eSMark Brown  */
2623c5af3a2eSMark Brown static int snd_soc_register_card(struct snd_soc_card *card)
2624c5af3a2eSMark Brown {
2625c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2626c5af3a2eSMark Brown 		return -EINVAL;
2627c5af3a2eSMark Brown 
2628c5af3a2eSMark Brown 	INIT_LIST_HEAD(&card->list);
2629c5af3a2eSMark Brown 	card->instantiated = 0;
2630c5af3a2eSMark Brown 
2631c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2632c5af3a2eSMark Brown 	list_add(&card->list, &card_list);
2633435c5e25SMark Brown 	snd_soc_instantiate_cards();
2634c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2635c5af3a2eSMark Brown 
2636c5af3a2eSMark Brown 	dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2637c5af3a2eSMark Brown 
2638c5af3a2eSMark Brown 	return 0;
2639c5af3a2eSMark Brown }
2640c5af3a2eSMark Brown 
2641c5af3a2eSMark Brown /**
2642c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2643c5af3a2eSMark Brown  *
2644ac11a2b3SMark Brown  * @card: Card to unregister
2645c5af3a2eSMark Brown  *
2646c5af3a2eSMark Brown  * Note that currently this is an internal only function: it will be
2647c5af3a2eSMark Brown  * exposed to machine drivers after further backporting of ASoC v2
2648c5af3a2eSMark Brown  * registration APIs.
2649c5af3a2eSMark Brown  */
2650c5af3a2eSMark Brown static int snd_soc_unregister_card(struct snd_soc_card *card)
2651c5af3a2eSMark Brown {
2652c5af3a2eSMark Brown 	mutex_lock(&client_mutex);
2653c5af3a2eSMark Brown 	list_del(&card->list);
2654c5af3a2eSMark Brown 	mutex_unlock(&client_mutex);
2655c5af3a2eSMark Brown 
2656c5af3a2eSMark Brown 	dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2657c5af3a2eSMark Brown 
2658c5af3a2eSMark Brown 	return 0;
2659c5af3a2eSMark Brown }
2660c5af3a2eSMark Brown 
26619115171aSMark Brown /**
26629115171aSMark Brown  * snd_soc_register_dai - Register a DAI with the ASoC core
26639115171aSMark Brown  *
2664ac11a2b3SMark Brown  * @dai: DAI to register
26659115171aSMark Brown  */
26669115171aSMark Brown int snd_soc_register_dai(struct snd_soc_dai *dai)
26679115171aSMark Brown {
26689115171aSMark Brown 	if (!dai->name)
26699115171aSMark Brown 		return -EINVAL;
26709115171aSMark Brown 
26719115171aSMark Brown 	/* The device should become mandatory over time */
26729115171aSMark Brown 	if (!dai->dev)
26739115171aSMark Brown 		printk(KERN_WARNING "No device for DAI %s\n", dai->name);
26749115171aSMark Brown 
26756335d055SEric Miao 	if (!dai->ops)
26766335d055SEric Miao 		dai->ops = &null_dai_ops;
26776335d055SEric Miao 
26789115171aSMark Brown 	INIT_LIST_HEAD(&dai->list);
26799115171aSMark Brown 
26809115171aSMark Brown 	mutex_lock(&client_mutex);
26819115171aSMark Brown 	list_add(&dai->list, &dai_list);
2682435c5e25SMark Brown 	snd_soc_instantiate_cards();
26839115171aSMark Brown 	mutex_unlock(&client_mutex);
26849115171aSMark Brown 
26859115171aSMark Brown 	pr_debug("Registered DAI '%s'\n", dai->name);
26869115171aSMark Brown 
26879115171aSMark Brown 	return 0;
26889115171aSMark Brown }
26899115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dai);
26909115171aSMark Brown 
26919115171aSMark Brown /**
26929115171aSMark Brown  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
26939115171aSMark Brown  *
2694ac11a2b3SMark Brown  * @dai: DAI to unregister
26959115171aSMark Brown  */
26969115171aSMark Brown void snd_soc_unregister_dai(struct snd_soc_dai *dai)
26979115171aSMark Brown {
26989115171aSMark Brown 	mutex_lock(&client_mutex);
26999115171aSMark Brown 	list_del(&dai->list);
27009115171aSMark Brown 	mutex_unlock(&client_mutex);
27019115171aSMark Brown 
27029115171aSMark Brown 	pr_debug("Unregistered DAI '%s'\n", dai->name);
27039115171aSMark Brown }
27049115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
27059115171aSMark Brown 
27069115171aSMark Brown /**
27079115171aSMark Brown  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
27089115171aSMark Brown  *
2709ac11a2b3SMark Brown  * @dai: Array of DAIs to register
2710ac11a2b3SMark Brown  * @count: Number of DAIs
27119115171aSMark Brown  */
27129115171aSMark Brown int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
27139115171aSMark Brown {
27149115171aSMark Brown 	int i, ret;
27159115171aSMark Brown 
27169115171aSMark Brown 	for (i = 0; i < count; i++) {
27179115171aSMark Brown 		ret = snd_soc_register_dai(&dai[i]);
27189115171aSMark Brown 		if (ret != 0)
27199115171aSMark Brown 			goto err;
27209115171aSMark Brown 	}
27219115171aSMark Brown 
27229115171aSMark Brown 	return 0;
27239115171aSMark Brown 
27249115171aSMark Brown err:
27259115171aSMark Brown 	for (i--; i >= 0; i--)
27269115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
27279115171aSMark Brown 
27289115171aSMark Brown 	return ret;
27299115171aSMark Brown }
27309115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_dais);
27319115171aSMark Brown 
27329115171aSMark Brown /**
27339115171aSMark Brown  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
27349115171aSMark Brown  *
2735ac11a2b3SMark Brown  * @dai: Array of DAIs to unregister
2736ac11a2b3SMark Brown  * @count: Number of DAIs
27379115171aSMark Brown  */
27389115171aSMark Brown void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
27399115171aSMark Brown {
27409115171aSMark Brown 	int i;
27419115171aSMark Brown 
27429115171aSMark Brown 	for (i = 0; i < count; i++)
27439115171aSMark Brown 		snd_soc_unregister_dai(&dai[i]);
27449115171aSMark Brown }
27459115171aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
27469115171aSMark Brown 
274712a48a8cSMark Brown /**
274812a48a8cSMark Brown  * snd_soc_register_platform - Register a platform with the ASoC core
274912a48a8cSMark Brown  *
2750ac11a2b3SMark Brown  * @platform: platform to register
275112a48a8cSMark Brown  */
275212a48a8cSMark Brown int snd_soc_register_platform(struct snd_soc_platform *platform)
275312a48a8cSMark Brown {
275412a48a8cSMark Brown 	if (!platform->name)
275512a48a8cSMark Brown 		return -EINVAL;
275612a48a8cSMark Brown 
275712a48a8cSMark Brown 	INIT_LIST_HEAD(&platform->list);
275812a48a8cSMark Brown 
275912a48a8cSMark Brown 	mutex_lock(&client_mutex);
276012a48a8cSMark Brown 	list_add(&platform->list, &platform_list);
2761435c5e25SMark Brown 	snd_soc_instantiate_cards();
276212a48a8cSMark Brown 	mutex_unlock(&client_mutex);
276312a48a8cSMark Brown 
276412a48a8cSMark Brown 	pr_debug("Registered platform '%s'\n", platform->name);
276512a48a8cSMark Brown 
276612a48a8cSMark Brown 	return 0;
276712a48a8cSMark Brown }
276812a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform);
276912a48a8cSMark Brown 
277012a48a8cSMark Brown /**
277112a48a8cSMark Brown  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
277212a48a8cSMark Brown  *
2773ac11a2b3SMark Brown  * @platform: platform to unregister
277412a48a8cSMark Brown  */
277512a48a8cSMark Brown void snd_soc_unregister_platform(struct snd_soc_platform *platform)
277612a48a8cSMark Brown {
277712a48a8cSMark Brown 	mutex_lock(&client_mutex);
277812a48a8cSMark Brown 	list_del(&platform->list);
277912a48a8cSMark Brown 	mutex_unlock(&client_mutex);
278012a48a8cSMark Brown 
278112a48a8cSMark Brown 	pr_debug("Unregistered platform '%s'\n", platform->name);
278212a48a8cSMark Brown }
278312a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
278412a48a8cSMark Brown 
2785151ab22cSMark Brown static u64 codec_format_map[] = {
2786151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2787151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2788151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2789151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2790151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2791151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2792151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2793151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2794151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2795151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2796151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2797151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2798151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2799151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2800151ab22cSMark Brown 	SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2801151ab22cSMark Brown 	| SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2802151ab22cSMark Brown };
2803151ab22cSMark Brown 
2804151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see
2805151ab22cSMark Brown  * the endianness of the data but we're using the CPU format
2806151ab22cSMark Brown  * definitions which do need to include endianness so we ensure that
2807151ab22cSMark Brown  * codec DAIs always have both big and little endian variants set.
2808151ab22cSMark Brown  */
2809151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2810151ab22cSMark Brown {
2811151ab22cSMark Brown 	int i;
2812151ab22cSMark Brown 
2813151ab22cSMark Brown 	for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2814151ab22cSMark Brown 		if (stream->formats & codec_format_map[i])
2815151ab22cSMark Brown 			stream->formats |= codec_format_map[i];
2816151ab22cSMark Brown }
2817151ab22cSMark Brown 
28180d0cf00aSMark Brown /**
28190d0cf00aSMark Brown  * snd_soc_register_codec - Register a codec with the ASoC core
28200d0cf00aSMark Brown  *
2821ac11a2b3SMark Brown  * @codec: codec to register
28220d0cf00aSMark Brown  */
28230d0cf00aSMark Brown int snd_soc_register_codec(struct snd_soc_codec *codec)
28240d0cf00aSMark Brown {
2825151ab22cSMark Brown 	int i;
2826151ab22cSMark Brown 
28270d0cf00aSMark Brown 	if (!codec->name)
28280d0cf00aSMark Brown 		return -EINVAL;
28290d0cf00aSMark Brown 
28300d0cf00aSMark Brown 	/* The device should become mandatory over time */
28310d0cf00aSMark Brown 	if (!codec->dev)
28320d0cf00aSMark Brown 		printk(KERN_WARNING "No device for codec %s\n", codec->name);
28330d0cf00aSMark Brown 
28340d0cf00aSMark Brown 	INIT_LIST_HEAD(&codec->list);
28350d0cf00aSMark Brown 
2836151ab22cSMark Brown 	for (i = 0; i < codec->num_dai; i++) {
2837151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].playback);
2838151ab22cSMark Brown 		fixup_codec_formats(&codec->dai[i].capture);
2839151ab22cSMark Brown 	}
2840151ab22cSMark Brown 
28410d0cf00aSMark Brown 	mutex_lock(&client_mutex);
28420d0cf00aSMark Brown 	list_add(&codec->list, &codec_list);
28430d0cf00aSMark Brown 	snd_soc_instantiate_cards();
28440d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
28450d0cf00aSMark Brown 
28460d0cf00aSMark Brown 	pr_debug("Registered codec '%s'\n", codec->name);
28470d0cf00aSMark Brown 
28480d0cf00aSMark Brown 	return 0;
28490d0cf00aSMark Brown }
28500d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec);
28510d0cf00aSMark Brown 
28520d0cf00aSMark Brown /**
28530d0cf00aSMark Brown  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
28540d0cf00aSMark Brown  *
2855ac11a2b3SMark Brown  * @codec: codec to unregister
28560d0cf00aSMark Brown  */
28570d0cf00aSMark Brown void snd_soc_unregister_codec(struct snd_soc_codec *codec)
28580d0cf00aSMark Brown {
28590d0cf00aSMark Brown 	mutex_lock(&client_mutex);
28600d0cf00aSMark Brown 	list_del(&codec->list);
28610d0cf00aSMark Brown 	mutex_unlock(&client_mutex);
28620d0cf00aSMark Brown 
28630d0cf00aSMark Brown 	pr_debug("Unregistered codec '%s'\n", codec->name);
28640d0cf00aSMark Brown }
28650d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
28660d0cf00aSMark Brown 
2867c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
2868db2a4165SFrank Mandarino {
2869384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2870384c89e2SMark Brown 	debugfs_root = debugfs_create_dir("asoc", NULL);
2871384c89e2SMark Brown 	if (IS_ERR(debugfs_root) || !debugfs_root) {
2872384c89e2SMark Brown 		printk(KERN_WARNING
2873384c89e2SMark Brown 		       "ASoC: Failed to create debugfs directory\n");
2874384c89e2SMark Brown 		debugfs_root = NULL;
2875384c89e2SMark Brown 	}
2876384c89e2SMark Brown #endif
2877384c89e2SMark Brown 
2878db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
2879db2a4165SFrank Mandarino }
2880db2a4165SFrank Mandarino 
28817d8c16a6SMark Brown static void __exit snd_soc_exit(void)
2882db2a4165SFrank Mandarino {
2883384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
2884384c89e2SMark Brown 	debugfs_remove_recursive(debugfs_root);
2885384c89e2SMark Brown #endif
2886db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
2887db2a4165SFrank Mandarino }
2888db2a4165SFrank Mandarino 
2889db2a4165SFrank Mandarino module_init(snd_soc_init);
2890db2a4165SFrank Mandarino module_exit(snd_soc_exit);
2891db2a4165SFrank Mandarino 
2892db2a4165SFrank Mandarino /* Module information */
2893d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2894db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
2895db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
28968b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
2897