xref: /openbmc/linux/sound/soc/soc-core.c (revision 12e74f7d430655f541b85018ea62bcd669094bd7)
1db2a4165SFrank Mandarino /*
2db2a4165SFrank Mandarino  * soc-core.c  --  ALSA SoC Audio Layer
3db2a4165SFrank Mandarino  *
4db2a4165SFrank Mandarino  * Copyright 2005 Wolfson Microelectronics PLC.
5db2a4165SFrank Mandarino  * Author: Liam Girdwood
6db2a4165SFrank Mandarino  *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7db2a4165SFrank Mandarino  *
8db2a4165SFrank Mandarino  *  This program is free software; you can redistribute  it and/or modify it
9db2a4165SFrank Mandarino  *  under  the terms of  the GNU General  Public License as published by the
10db2a4165SFrank Mandarino  *  Free Software Foundation;  either version 2 of the  License, or (at your
11db2a4165SFrank Mandarino  *  option) any later version.
12db2a4165SFrank Mandarino  *
13db2a4165SFrank Mandarino  *  Revision history
14db2a4165SFrank Mandarino  *    12th Aug 2005   Initial version.
15db2a4165SFrank Mandarino  *    25th Oct 2005   Working Codec, Interface and Platform registration.
16db2a4165SFrank Mandarino  *
17db2a4165SFrank Mandarino  *  TODO:
18db2a4165SFrank Mandarino  *   o Add hw rules to enforce rates, etc.
19db2a4165SFrank Mandarino  *   o More testing with other codecs/machines.
20db2a4165SFrank Mandarino  *   o Add more codecs and platforms to ensure good API coverage.
21db2a4165SFrank Mandarino  *   o Support TDM on PCM and I2S
22db2a4165SFrank Mandarino  */
23db2a4165SFrank Mandarino 
24db2a4165SFrank Mandarino #include <linux/module.h>
25db2a4165SFrank Mandarino #include <linux/moduleparam.h>
26db2a4165SFrank Mandarino #include <linux/init.h>
27db2a4165SFrank Mandarino #include <linux/delay.h>
28db2a4165SFrank Mandarino #include <linux/pm.h>
29db2a4165SFrank Mandarino #include <linux/bitops.h>
30db2a4165SFrank Mandarino #include <linux/platform_device.h>
31db2a4165SFrank Mandarino #include <sound/driver.h>
32db2a4165SFrank Mandarino #include <sound/core.h>
33db2a4165SFrank Mandarino #include <sound/pcm.h>
34db2a4165SFrank Mandarino #include <sound/pcm_params.h>
35db2a4165SFrank Mandarino #include <sound/soc.h>
36db2a4165SFrank Mandarino #include <sound/soc-dapm.h>
37db2a4165SFrank Mandarino #include <sound/initval.h>
38db2a4165SFrank Mandarino 
39db2a4165SFrank Mandarino /* debug */
40db2a4165SFrank Mandarino #define SOC_DEBUG 0
41db2a4165SFrank Mandarino #if SOC_DEBUG
42db2a4165SFrank Mandarino #define dbg(format, arg...) printk(format, ## arg)
43db2a4165SFrank Mandarino #else
44db2a4165SFrank Mandarino #define dbg(format, arg...)
45db2a4165SFrank Mandarino #endif
46db2a4165SFrank Mandarino /* debug DAI capabilities matching */
47db2a4165SFrank Mandarino #define SOC_DEBUG_DAI 0
48db2a4165SFrank Mandarino #if SOC_DEBUG_DAI
49db2a4165SFrank Mandarino #define dbgc(format, arg...) printk(format, ## arg)
50db2a4165SFrank Mandarino #else
51db2a4165SFrank Mandarino #define dbgc(format, arg...)
52db2a4165SFrank Mandarino #endif
53db2a4165SFrank Mandarino 
54db2a4165SFrank Mandarino static DEFINE_MUTEX(pcm_mutex);
55db2a4165SFrank Mandarino static DEFINE_MUTEX(io_mutex);
56db2a4165SFrank Mandarino static struct workqueue_struct *soc_workq;
57db2a4165SFrank Mandarino static struct work_struct soc_stream_work;
58db2a4165SFrank Mandarino static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
59db2a4165SFrank Mandarino 
60db2a4165SFrank Mandarino /* supported sample rates */
61db2a4165SFrank Mandarino /* ATTENTION: these values depend on the definition in pcm.h! */
62db2a4165SFrank Mandarino static const unsigned int rates[] = {
63db2a4165SFrank Mandarino 	5512, 8000, 11025, 16000, 22050, 32000, 44100,
64db2a4165SFrank Mandarino 	48000, 64000, 88200, 96000, 176400, 192000
65db2a4165SFrank Mandarino };
66db2a4165SFrank Mandarino 
67db2a4165SFrank Mandarino /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
76db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
77db2a4165SFrank Mandarino /* unregister ac97 codec */
78db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
79db2a4165SFrank Mandarino {
80db2a4165SFrank Mandarino 	if (codec->ac97->dev.bus)
81db2a4165SFrank Mandarino 		device_unregister(&codec->ac97->dev);
82db2a4165SFrank Mandarino 	return 0;
83db2a4165SFrank Mandarino }
84db2a4165SFrank Mandarino 
85db2a4165SFrank Mandarino /* stop no dev release warning */
86db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){}
87db2a4165SFrank Mandarino 
88db2a4165SFrank Mandarino /* register ac97 codec to bus */
89db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec)
90db2a4165SFrank Mandarino {
91db2a4165SFrank Mandarino 	int err;
92db2a4165SFrank Mandarino 
93db2a4165SFrank Mandarino 	codec->ac97->dev.bus = &ac97_bus_type;
94db2a4165SFrank Mandarino 	codec->ac97->dev.parent = NULL;
95db2a4165SFrank Mandarino 	codec->ac97->dev.release = soc_ac97_device_release;
96db2a4165SFrank Mandarino 
97db2a4165SFrank Mandarino 	snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
98db2a4165SFrank Mandarino 		 codec->card->number, 0, codec->name);
99db2a4165SFrank Mandarino 	err = device_register(&codec->ac97->dev);
100db2a4165SFrank Mandarino 	if (err < 0) {
101db2a4165SFrank Mandarino 		snd_printk(KERN_ERR "Can't register ac97 bus\n");
102db2a4165SFrank Mandarino 		codec->ac97->dev.bus = NULL;
103db2a4165SFrank Mandarino 		return err;
104db2a4165SFrank Mandarino 	}
105db2a4165SFrank Mandarino 	return 0;
106db2a4165SFrank Mandarino }
107db2a4165SFrank Mandarino #endif
108db2a4165SFrank Mandarino 
109db2a4165SFrank Mandarino static inline const char* get_dai_name(int type)
110db2a4165SFrank Mandarino {
111db2a4165SFrank Mandarino 	switch(type) {
112db2a4165SFrank Mandarino 	case SND_SOC_DAI_AC97:
113db2a4165SFrank Mandarino 		return "AC97";
114db2a4165SFrank Mandarino 	case SND_SOC_DAI_I2S:
115db2a4165SFrank Mandarino 		return "I2S";
116db2a4165SFrank Mandarino 	case SND_SOC_DAI_PCM:
117db2a4165SFrank Mandarino 		return "PCM";
118db2a4165SFrank Mandarino 	}
119db2a4165SFrank Mandarino 	return NULL;
120db2a4165SFrank Mandarino }
121db2a4165SFrank Mandarino 
122db2a4165SFrank Mandarino /* get rate format from rate */
123db2a4165SFrank Mandarino static inline int soc_get_rate_format(int rate)
124db2a4165SFrank Mandarino {
125db2a4165SFrank Mandarino 	int i;
126db2a4165SFrank Mandarino 
127db2a4165SFrank Mandarino 	for (i = 0; i < ARRAY_SIZE(rates); i++) {
128db2a4165SFrank Mandarino 		if (rates[i] == rate)
129db2a4165SFrank Mandarino 			return 1 << i;
130db2a4165SFrank Mandarino 	}
131db2a4165SFrank Mandarino 	return 0;
132db2a4165SFrank Mandarino }
133db2a4165SFrank Mandarino 
134db2a4165SFrank Mandarino /* gets the audio system mclk/sysclk for the given parameters */
135db2a4165SFrank Mandarino static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
136db2a4165SFrank Mandarino 	struct snd_soc_clock_info *info)
137db2a4165SFrank Mandarino {
138db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
139db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
140db2a4165SFrank Mandarino 	int i;
141db2a4165SFrank Mandarino 
142db2a4165SFrank Mandarino 	/* find the matching machine config and get it's mclk for the given
143db2a4165SFrank Mandarino 	 * sample rate and hardware format */
144db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
145db2a4165SFrank Mandarino 		if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
146db2a4165SFrank Mandarino 			machine->dai_link[i].config_sysclk)
147db2a4165SFrank Mandarino 			return machine->dai_link[i].config_sysclk(rtd, info);
148db2a4165SFrank Mandarino 	}
149db2a4165SFrank Mandarino 	return 0;
150db2a4165SFrank Mandarino }
151db2a4165SFrank Mandarino 
152db2a4165SFrank Mandarino /* changes a bitclk multiplier mask to a divider mask */
153db2a4165SFrank Mandarino static u16 soc_bfs_mult_to_div(u16 bfs, int rate, unsigned int mclk,
154db2a4165SFrank Mandarino 	unsigned int pcmfmt, unsigned int chn)
155db2a4165SFrank Mandarino {
156db2a4165SFrank Mandarino 	int i, j;
157db2a4165SFrank Mandarino 	u16 bfs_ = 0;
158db2a4165SFrank Mandarino 	int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
159db2a4165SFrank Mandarino 
160db2a4165SFrank Mandarino 	if (size <= 0)
161db2a4165SFrank Mandarino 		return 0;
162db2a4165SFrank Mandarino 
163db2a4165SFrank Mandarino 	/* the minimum bit clock that has enough bandwidth */
164db2a4165SFrank Mandarino 	min = size * rate * chn;
165db2a4165SFrank Mandarino 	dbgc("mult --> div min bclk %d with mclk %d\n", min, mclk);
166db2a4165SFrank Mandarino 
167db2a4165SFrank Mandarino 	for (i = 0; i < 16; i++) {
168db2a4165SFrank Mandarino 		if ((bfs >> i) & 0x1) {
169db2a4165SFrank Mandarino 			j = rate * SND_SOC_FSB_REAL(1<<i);
170db2a4165SFrank Mandarino 
171db2a4165SFrank Mandarino 			if (j >= min) {
172db2a4165SFrank Mandarino 				bfs_ |= SND_SOC_FSBD(mclk/j);
173db2a4165SFrank Mandarino 				dbgc("mult --> div support mult %d\n",
174db2a4165SFrank Mandarino 					SND_SOC_FSB_REAL(1<<i));
175db2a4165SFrank Mandarino 			}
176db2a4165SFrank Mandarino 		}
177db2a4165SFrank Mandarino 	}
178db2a4165SFrank Mandarino 
179db2a4165SFrank Mandarino 	return bfs_;
180db2a4165SFrank Mandarino }
181db2a4165SFrank Mandarino 
182db2a4165SFrank Mandarino /* changes a bitclk divider mask to a multiplier mask */
183db2a4165SFrank Mandarino static u16 soc_bfs_div_to_mult(u16 bfs, int rate, unsigned int mclk,
184db2a4165SFrank Mandarino 	unsigned int pcmfmt, unsigned int chn)
185db2a4165SFrank Mandarino {
186db2a4165SFrank Mandarino 	int i, j;
187db2a4165SFrank Mandarino 	u16 bfs_ = 0;
188db2a4165SFrank Mandarino 
189db2a4165SFrank Mandarino 	int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
190db2a4165SFrank Mandarino 
191db2a4165SFrank Mandarino 	if (size <= 0)
192db2a4165SFrank Mandarino 		return 0;
193db2a4165SFrank Mandarino 
194db2a4165SFrank Mandarino 	/* the minimum bit clock that has enough bandwidth */
195db2a4165SFrank Mandarino 	min = size * rate * chn;
196db2a4165SFrank Mandarino 	dbgc("div to mult min bclk %d with mclk %d\n", min, mclk);
197db2a4165SFrank Mandarino 
198db2a4165SFrank Mandarino 	for (i = 0; i < 16; i++) {
199db2a4165SFrank Mandarino 		if ((bfs >> i) & 0x1) {
200db2a4165SFrank Mandarino 			j = mclk / (SND_SOC_FSBD_REAL(1<<i));
201db2a4165SFrank Mandarino 			if (j >= min) {
202db2a4165SFrank Mandarino 				bfs_ |= SND_SOC_FSB(j/rate);
203db2a4165SFrank Mandarino 				dbgc("div --> mult support div %d\n",
204db2a4165SFrank Mandarino 					SND_SOC_FSBD_REAL(1<<i));
205db2a4165SFrank Mandarino 			}
206db2a4165SFrank Mandarino 		}
207db2a4165SFrank Mandarino 	}
208db2a4165SFrank Mandarino 
209db2a4165SFrank Mandarino 	return bfs_;
210db2a4165SFrank Mandarino }
211db2a4165SFrank Mandarino 
212db2a4165SFrank Mandarino /* Matches codec DAI and SoC CPU DAI hardware parameters */
213db2a4165SFrank Mandarino static int soc_hw_match_params(struct snd_pcm_substream *substream,
214db2a4165SFrank Mandarino 	struct snd_pcm_hw_params *params)
215db2a4165SFrank Mandarino {
216db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
217db2a4165SFrank Mandarino 	struct snd_soc_dai_mode *codec_dai_mode = NULL;
218db2a4165SFrank Mandarino 	struct snd_soc_dai_mode *cpu_dai_mode = NULL;
219db2a4165SFrank Mandarino 	struct snd_soc_clock_info clk_info;
220db2a4165SFrank Mandarino 	unsigned int fs, mclk, codec_bfs, cpu_bfs, rate = params_rate(params),
221db2a4165SFrank Mandarino 		chn, j, k, cpu_bclk, codec_bclk, pcmrate;
222db2a4165SFrank Mandarino 	u16 fmt = 0;
223db2a4165SFrank Mandarino 
224db2a4165SFrank Mandarino 	dbg("asoc: match version %s\n", SND_SOC_VERSION);
225db2a4165SFrank Mandarino 	clk_info.rate = rate;
226db2a4165SFrank Mandarino 	pcmrate = soc_get_rate_format(rate);
227db2a4165SFrank Mandarino 
228db2a4165SFrank Mandarino 	/* try and find a match from the codec and cpu DAI capabilities */
229db2a4165SFrank Mandarino 	for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
230db2a4165SFrank Mandarino 		for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
231db2a4165SFrank Mandarino 			codec_dai_mode = &rtd->codec_dai->caps.mode[j];
232db2a4165SFrank Mandarino 			cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
233db2a4165SFrank Mandarino 
234db2a4165SFrank Mandarino 			if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
235db2a4165SFrank Mandarino 					pcmrate)) {
236db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
237db2a4165SFrank Mandarino 				continue;
238db2a4165SFrank Mandarino 			}
239db2a4165SFrank Mandarino 
240db2a4165SFrank Mandarino 			fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
241db2a4165SFrank Mandarino 			if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
242db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
243db2a4165SFrank Mandarino 				continue;
244db2a4165SFrank Mandarino 			}
245db2a4165SFrank Mandarino 
246db2a4165SFrank Mandarino 			if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
247db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
248db2a4165SFrank Mandarino 					 j, k);
249db2a4165SFrank Mandarino 				continue;
250db2a4165SFrank Mandarino 			}
251db2a4165SFrank Mandarino 
252db2a4165SFrank Mandarino 			if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
253db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
254db2a4165SFrank Mandarino 				continue;
255db2a4165SFrank Mandarino 			}
256db2a4165SFrank Mandarino 
257db2a4165SFrank Mandarino 			if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
258db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
259db2a4165SFrank Mandarino 				continue;
260db2a4165SFrank Mandarino 			}
261db2a4165SFrank Mandarino 
262db2a4165SFrank Mandarino 			if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
263db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
264db2a4165SFrank Mandarino 				continue;
265db2a4165SFrank Mandarino 			}
266db2a4165SFrank Mandarino 
267db2a4165SFrank Mandarino 			/* todo - still need to add tdm selection */
268db2a4165SFrank Mandarino 			rtd->cpu_dai->dai_runtime.fmt =
269db2a4165SFrank Mandarino 			rtd->codec_dai->dai_runtime.fmt =
270db2a4165SFrank Mandarino 				1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
271db2a4165SFrank Mandarino 				1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
272db2a4165SFrank Mandarino 				1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
273db2a4165SFrank Mandarino 			clk_info.bclk_master =
274db2a4165SFrank Mandarino 				rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
275db2a4165SFrank Mandarino 
276db2a4165SFrank Mandarino 			/* make sure the ratio between rate and master
277db2a4165SFrank Mandarino 			 * clock is acceptable*/
278db2a4165SFrank Mandarino 			fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
279db2a4165SFrank Mandarino 			if (fs == 0) {
280db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
281db2a4165SFrank Mandarino 				continue;
282db2a4165SFrank Mandarino 			}
283db2a4165SFrank Mandarino 			clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
284db2a4165SFrank Mandarino 				rtd->codec_dai->dai_runtime.fs = fs;
285db2a4165SFrank Mandarino 
286db2a4165SFrank Mandarino 			/* calculate audio system clocking using slowest clocks possible*/
287db2a4165SFrank Mandarino 			mclk = soc_get_mclk(rtd, &clk_info);
288db2a4165SFrank Mandarino 			if (mclk == 0) {
289db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
290db2a4165SFrank Mandarino 				dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
291db2a4165SFrank Mandarino 					clk_info.bclk_master);
292db2a4165SFrank Mandarino 				continue;
293db2a4165SFrank Mandarino 			}
294db2a4165SFrank Mandarino 
295db2a4165SFrank Mandarino 			/* calculate word size (per channel) and frame size */
296db2a4165SFrank Mandarino 			rtd->codec_dai->dai_runtime.pcmfmt =
297db2a4165SFrank Mandarino 				rtd->cpu_dai->dai_runtime.pcmfmt =
298db2a4165SFrank Mandarino 				1 << params_format(params);
299db2a4165SFrank Mandarino 
300db2a4165SFrank Mandarino 			chn = params_channels(params);
301db2a4165SFrank Mandarino 			/* i2s always has left and right */
302db2a4165SFrank Mandarino 			if (params_channels(params) == 1 &&
303db2a4165SFrank Mandarino 				rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
304db2a4165SFrank Mandarino 					SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
305db2a4165SFrank Mandarino 				chn <<= 1;
306db2a4165SFrank Mandarino 
307db2a4165SFrank Mandarino 			/* Calculate bfs - the ratio between bitclock and the sample rate
308db2a4165SFrank Mandarino 			 * We must take into consideration the dividers and multipliers
309db2a4165SFrank Mandarino 			 * used in the codec and cpu DAI modes. We always choose the
310db2a4165SFrank Mandarino 			 * lowest possible clocks to reduce power.
311db2a4165SFrank Mandarino 			 */
312db2a4165SFrank Mandarino 			if (codec_dai_mode->flags & cpu_dai_mode->flags &
313db2a4165SFrank Mandarino 				SND_SOC_DAI_BFS_DIV) {
314db2a4165SFrank Mandarino 				/* cpu & codec bfs dividers */
315db2a4165SFrank Mandarino 				rtd->cpu_dai->dai_runtime.bfs =
316db2a4165SFrank Mandarino 					rtd->codec_dai->dai_runtime.bfs =
317db2a4165SFrank Mandarino 					1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
318db2a4165SFrank Mandarino 			} else if (codec_dai_mode->flags & SND_SOC_DAI_BFS_DIV) {
319db2a4165SFrank Mandarino 				/* normalise bfs codec divider & cpu mult */
320db2a4165SFrank Mandarino 				codec_bfs = soc_bfs_div_to_mult(codec_dai_mode->bfs, rate,
321db2a4165SFrank Mandarino 					mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
322db2a4165SFrank Mandarino 				rtd->cpu_dai->dai_runtime.bfs =
323db2a4165SFrank Mandarino 					1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
324db2a4165SFrank Mandarino 				cpu_bfs = soc_bfs_mult_to_div(cpu_dai_mode->bfs, rate, mclk,
325db2a4165SFrank Mandarino 						rtd->codec_dai->dai_runtime.pcmfmt, chn);
326db2a4165SFrank Mandarino 				rtd->codec_dai->dai_runtime.bfs =
327db2a4165SFrank Mandarino 					1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
328db2a4165SFrank Mandarino 			} else if (cpu_dai_mode->flags & SND_SOC_DAI_BFS_DIV) {
329db2a4165SFrank Mandarino 				/* normalise bfs codec mult & cpu divider */
330db2a4165SFrank Mandarino 				codec_bfs = soc_bfs_mult_to_div(codec_dai_mode->bfs, rate,
331db2a4165SFrank Mandarino 					mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
332db2a4165SFrank Mandarino 				rtd->cpu_dai->dai_runtime.bfs =
333db2a4165SFrank Mandarino 					1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
334db2a4165SFrank Mandarino 				cpu_bfs = soc_bfs_div_to_mult(cpu_dai_mode->bfs, rate, mclk,
335db2a4165SFrank Mandarino 						rtd->codec_dai->dai_runtime.pcmfmt, chn);
336db2a4165SFrank Mandarino 				rtd->codec_dai->dai_runtime.bfs =
337db2a4165SFrank Mandarino 					1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
338db2a4165SFrank Mandarino 			} else {
339db2a4165SFrank Mandarino 				/* codec & cpu bfs rate multipliers */
340db2a4165SFrank Mandarino 				rtd->cpu_dai->dai_runtime.bfs =
341db2a4165SFrank Mandarino 					rtd->codec_dai->dai_runtime.bfs =
342db2a4165SFrank Mandarino 					1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
343db2a4165SFrank Mandarino 			}
344db2a4165SFrank Mandarino 
345db2a4165SFrank Mandarino 			/* make sure the bit clock speed is acceptable */
346db2a4165SFrank Mandarino 			if (!rtd->cpu_dai->dai_runtime.bfs ||
347db2a4165SFrank Mandarino 				!rtd->codec_dai->dai_runtime.bfs) {
348db2a4165SFrank Mandarino 				dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
349db2a4165SFrank Mandarino 				dbgc("asoc: cpu_dai %x codec %x\n",
350db2a4165SFrank Mandarino 					rtd->cpu_dai->dai_runtime.bfs,
351db2a4165SFrank Mandarino 					rtd->codec_dai->dai_runtime.bfs);
352db2a4165SFrank Mandarino 				dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
353db2a4165SFrank Mandarino 				continue;
354db2a4165SFrank Mandarino 			}
355db2a4165SFrank Mandarino 
356db2a4165SFrank Mandarino 			goto found;
357db2a4165SFrank Mandarino 		}
358db2a4165SFrank Mandarino 	}
359db2a4165SFrank Mandarino 	printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
360db2a4165SFrank Mandarino 	return -EINVAL;
361db2a4165SFrank Mandarino 
362db2a4165SFrank Mandarino found:
363db2a4165SFrank Mandarino 	/* we have matching DAI's, so complete the runtime info */
364db2a4165SFrank Mandarino 	rtd->codec_dai->dai_runtime.pcmrate =
365db2a4165SFrank Mandarino 		rtd->cpu_dai->dai_runtime.pcmrate =
366db2a4165SFrank Mandarino 		soc_get_rate_format(rate);
367db2a4165SFrank Mandarino 
368db2a4165SFrank Mandarino 	rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
369db2a4165SFrank Mandarino 	rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
370db2a4165SFrank Mandarino 	rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
371db2a4165SFrank Mandarino 	rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
372db2a4165SFrank Mandarino 
373db2a4165SFrank Mandarino 	/* for debug atm */
374db2a4165SFrank Mandarino 	dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
375db2a4165SFrank Mandarino 	if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
376db2a4165SFrank Mandarino 		codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
377db2a4165SFrank Mandarino 			SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
378db2a4165SFrank Mandarino 		dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
379db2a4165SFrank Mandarino 			rtd->codec_dai->dai_runtime.fs, mclk,
380db2a4165SFrank Mandarino 			SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs),	codec_bclk);
381db2a4165SFrank Mandarino 	} else {
382db2a4165SFrank Mandarino 		codec_bclk = params_rate(params) *
383db2a4165SFrank Mandarino 			SND_SOC_FSB_REAL(rtd->codec_dai->dai_runtime.bfs);
384db2a4165SFrank Mandarino 		dbg("asoc: codec fs %d mclk %d bfs mult %d bclk %d\n",
385db2a4165SFrank Mandarino 			rtd->codec_dai->dai_runtime.fs, mclk,
386db2a4165SFrank Mandarino 			SND_SOC_FSB_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
387db2a4165SFrank Mandarino 	}
388db2a4165SFrank Mandarino 	if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
389db2a4165SFrank Mandarino 		cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
390db2a4165SFrank Mandarino 			SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
391db2a4165SFrank Mandarino 		dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
392db2a4165SFrank Mandarino 			rtd->cpu_dai->dai_runtime.fs, mclk,
393db2a4165SFrank Mandarino 			SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
394db2a4165SFrank Mandarino 	} else {
395db2a4165SFrank Mandarino 		cpu_bclk = params_rate(params) *
396db2a4165SFrank Mandarino 			SND_SOC_FSB_REAL(rtd->cpu_dai->dai_runtime.bfs);
397db2a4165SFrank Mandarino 		dbg("asoc: cpu fs %d mclk %d bfs mult %d bclk %d\n",
398db2a4165SFrank Mandarino 			rtd->cpu_dai->dai_runtime.fs, mclk,
399db2a4165SFrank Mandarino 			SND_SOC_FSB_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
400db2a4165SFrank Mandarino 	}
401db2a4165SFrank Mandarino 
402db2a4165SFrank Mandarino 	/*
403db2a4165SFrank Mandarino 	 * Check we have matching bitclocks. If we don't then it means the
404db2a4165SFrank Mandarino 	 * sysclock returned by either the codec or cpu DAI (selected by the
405db2a4165SFrank Mandarino 	 * machine sysclock function) is wrong compared with the supported DAI
406db2a4165SFrank Mandarino 	 * modes for the codec or cpu DAI.
407db2a4165SFrank Mandarino 	 */
408db2a4165SFrank Mandarino 	if (cpu_bclk != codec_bclk){
409db2a4165SFrank Mandarino 		printk(KERN_ERR
410db2a4165SFrank Mandarino 			"asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
411db2a4165SFrank Mandarino 			);
412db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
413db2a4165SFrank Mandarino 	}
414db2a4165SFrank Mandarino 
415db2a4165SFrank Mandarino 	switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
416db2a4165SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
417db2a4165SFrank Mandarino 		dbg("asoc: DAI codec BCLK master, LRC master\n");
418db2a4165SFrank Mandarino 		break;
419db2a4165SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFM:
420db2a4165SFrank Mandarino 		dbg("asoc: DAI codec BCLK slave, LRC master\n");
421db2a4165SFrank Mandarino 		break;
422db2a4165SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFS:
423db2a4165SFrank Mandarino 		dbg("asoc: DAI codec BCLK master, LRC slave\n");
424db2a4165SFrank Mandarino 		break;
425db2a4165SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
426db2a4165SFrank Mandarino 		dbg("asoc: DAI codec BCLK slave, LRC slave\n");
427db2a4165SFrank Mandarino 		break;
428db2a4165SFrank Mandarino 	}
429db2a4165SFrank Mandarino 	dbg("asoc: mode %x, invert %x\n",
430db2a4165SFrank Mandarino 		rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
431db2a4165SFrank Mandarino 		rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
432db2a4165SFrank Mandarino 	dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
433db2a4165SFrank Mandarino 		params_channels(params), params_format(params));
434db2a4165SFrank Mandarino 
435db2a4165SFrank Mandarino 	return 0;
436db2a4165SFrank Mandarino }
437db2a4165SFrank Mandarino 
438db2a4165SFrank Mandarino static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
439db2a4165SFrank Mandarino {
440db2a4165SFrank Mandarino 	int i;
441db2a4165SFrank Mandarino 	u32 rates = 0;
442db2a4165SFrank Mandarino 
443db2a4165SFrank Mandarino 	for(i = 0; i < nmodes; i++)
444db2a4165SFrank Mandarino 		rates |= modes[i].pcmrate;
445db2a4165SFrank Mandarino 
446db2a4165SFrank Mandarino 	return rates;
447db2a4165SFrank Mandarino }
448db2a4165SFrank Mandarino 
449db2a4165SFrank Mandarino static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
450db2a4165SFrank Mandarino {
451db2a4165SFrank Mandarino 	int i;
452db2a4165SFrank Mandarino 	u64 formats = 0;
453db2a4165SFrank Mandarino 
454db2a4165SFrank Mandarino 	for(i = 0; i < nmodes; i++)
455db2a4165SFrank Mandarino 		formats |= modes[i].pcmfmt;
456db2a4165SFrank Mandarino 
457db2a4165SFrank Mandarino 	return formats;
458db2a4165SFrank Mandarino }
459db2a4165SFrank Mandarino 
460db2a4165SFrank Mandarino /*
461db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
462db2a4165SFrank Mandarino  * then initialized and any private data can be allocated. This also calls
463db2a4165SFrank Mandarino  * startup for the cpu DAI, platform, machine and codec DAI.
464db2a4165SFrank Mandarino  */
465db2a4165SFrank Mandarino static int soc_pcm_open(struct snd_pcm_substream *substream)
466db2a4165SFrank Mandarino {
467db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
468db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
469db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
470db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
471db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
472db2a4165SFrank Mandarino 	struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
473db2a4165SFrank Mandarino 	struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
474db2a4165SFrank Mandarino 	int ret = 0;
475db2a4165SFrank Mandarino 
476db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
477db2a4165SFrank Mandarino 
478db2a4165SFrank Mandarino 	/* startup the audio subsystem */
479db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.startup) {
480db2a4165SFrank Mandarino 		ret = rtd->cpu_dai->ops.startup(substream);
481db2a4165SFrank Mandarino 		if (ret < 0) {
482db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open interface %s\n",
483db2a4165SFrank Mandarino 				rtd->cpu_dai->name);
484db2a4165SFrank Mandarino 			goto out;
485db2a4165SFrank Mandarino 		}
486db2a4165SFrank Mandarino 	}
487db2a4165SFrank Mandarino 
488db2a4165SFrank Mandarino 	if (platform->pcm_ops->open) {
489db2a4165SFrank Mandarino 		ret = platform->pcm_ops->open(substream);
490db2a4165SFrank Mandarino 		if (ret < 0) {
491db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
492db2a4165SFrank Mandarino 			goto platform_err;
493db2a4165SFrank Mandarino 		}
494db2a4165SFrank Mandarino 	}
495db2a4165SFrank Mandarino 
496db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->startup) {
497db2a4165SFrank Mandarino 		ret = machine->ops->startup(substream);
498db2a4165SFrank Mandarino 		if (ret < 0) {
499db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
500db2a4165SFrank Mandarino 			goto machine_err;
501db2a4165SFrank Mandarino 		}
502db2a4165SFrank Mandarino 	}
503db2a4165SFrank Mandarino 
504db2a4165SFrank Mandarino 	if (rtd->codec_dai->ops.startup) {
505db2a4165SFrank Mandarino 		ret = rtd->codec_dai->ops.startup(substream);
506db2a4165SFrank Mandarino 		if (ret < 0) {
507db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't open codec %s\n",
508db2a4165SFrank Mandarino 				rtd->codec_dai->name);
509db2a4165SFrank Mandarino 			goto codec_dai_err;
510db2a4165SFrank Mandarino 		}
511db2a4165SFrank Mandarino 	}
512db2a4165SFrank Mandarino 
513db2a4165SFrank Mandarino 	/* create runtime params from DMA, codec and cpu DAI */
514db2a4165SFrank Mandarino 	if (runtime->hw.rates)
515db2a4165SFrank Mandarino 		runtime->hw.rates &=
516db2a4165SFrank Mandarino 			get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
517db2a4165SFrank Mandarino 			get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
518db2a4165SFrank Mandarino 	else
519db2a4165SFrank Mandarino 		runtime->hw.rates =
520db2a4165SFrank Mandarino 			get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
521db2a4165SFrank Mandarino 			get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
522db2a4165SFrank Mandarino 	if (runtime->hw.formats)
523db2a4165SFrank Mandarino 		runtime->hw.formats &=
524db2a4165SFrank Mandarino 			get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
525db2a4165SFrank Mandarino 			get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
526db2a4165SFrank Mandarino 	else
527db2a4165SFrank Mandarino 		runtime->hw.formats =
528db2a4165SFrank Mandarino 			get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
529db2a4165SFrank Mandarino 			get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
530db2a4165SFrank Mandarino 
531db2a4165SFrank Mandarino 	/* Check that the codec and cpu DAI's are compatible */
532db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
533db2a4165SFrank Mandarino 		runtime->hw.rate_min =
534db2a4165SFrank Mandarino 			max(rtd->codec_dai->playback.rate_min,
535db2a4165SFrank Mandarino 				rtd->cpu_dai->playback.rate_min);
536db2a4165SFrank Mandarino 		runtime->hw.rate_max =
537db2a4165SFrank Mandarino 			min(rtd->codec_dai->playback.rate_max,
538db2a4165SFrank Mandarino 				rtd->cpu_dai->playback.rate_max);
539db2a4165SFrank Mandarino 		runtime->hw.channels_min =
540db2a4165SFrank Mandarino 			max(rtd->codec_dai->playback.channels_min,
541db2a4165SFrank Mandarino 				rtd->cpu_dai->playback.channels_min);
542db2a4165SFrank Mandarino 		runtime->hw.channels_max =
543db2a4165SFrank Mandarino 			min(rtd->codec_dai->playback.channels_max,
544db2a4165SFrank Mandarino 				rtd->cpu_dai->playback.channels_max);
545db2a4165SFrank Mandarino 	} else {
546db2a4165SFrank Mandarino 		runtime->hw.rate_min =
547db2a4165SFrank Mandarino 			max(rtd->codec_dai->capture.rate_min,
548db2a4165SFrank Mandarino 				rtd->cpu_dai->capture.rate_min);
549db2a4165SFrank Mandarino 		runtime->hw.rate_max =
550db2a4165SFrank Mandarino 			min(rtd->codec_dai->capture.rate_max,
551db2a4165SFrank Mandarino 				rtd->cpu_dai->capture.rate_max);
552db2a4165SFrank Mandarino 		runtime->hw.channels_min =
553db2a4165SFrank Mandarino 			max(rtd->codec_dai->capture.channels_min,
554db2a4165SFrank Mandarino 				rtd->cpu_dai->capture.channels_min);
555db2a4165SFrank Mandarino 		runtime->hw.channels_max =
556db2a4165SFrank Mandarino 			min(rtd->codec_dai->capture.channels_max,
557db2a4165SFrank Mandarino 				rtd->cpu_dai->capture.channels_max);
558db2a4165SFrank Mandarino 	}
559db2a4165SFrank Mandarino 
560db2a4165SFrank Mandarino 	snd_pcm_limit_hw_rates(runtime);
561db2a4165SFrank Mandarino 	if (!runtime->hw.rates) {
562db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
563db2a4165SFrank Mandarino 			rtd->codec_dai->name, rtd->cpu_dai->name);
564db2a4165SFrank Mandarino 		goto codec_dai_err;
565db2a4165SFrank Mandarino 	}
566db2a4165SFrank Mandarino 	if (!runtime->hw.formats) {
567db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
568db2a4165SFrank Mandarino 			rtd->codec_dai->name, rtd->cpu_dai->name);
569db2a4165SFrank Mandarino 		goto codec_dai_err;
570db2a4165SFrank Mandarino 	}
571db2a4165SFrank Mandarino 	if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
572db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
573db2a4165SFrank Mandarino 			rtd->codec_dai->name, rtd->cpu_dai->name);
574db2a4165SFrank Mandarino 		goto codec_dai_err;
575db2a4165SFrank Mandarino 	}
576db2a4165SFrank Mandarino 
577db2a4165SFrank Mandarino 	dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
578b5c5fd24SLiam Girdwood 	dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
579b5c5fd24SLiam Girdwood 	dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
580b5c5fd24SLiam Girdwood 		runtime->hw.channels_max);
581b5c5fd24SLiam Girdwood 	dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
582b5c5fd24SLiam Girdwood 		runtime->hw.rate_max);
583db2a4165SFrank Mandarino 
584db2a4165SFrank Mandarino 
585db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
586db2a4165SFrank Mandarino 		rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
587db2a4165SFrank Mandarino 	else
588db2a4165SFrank Mandarino 		rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
589db2a4165SFrank Mandarino 	rtd->cpu_dai->active = rtd->codec_dai->active = 1;
590db2a4165SFrank Mandarino 	rtd->cpu_dai->runtime = runtime;
591db2a4165SFrank Mandarino 	socdev->codec->active++;
592db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
593db2a4165SFrank Mandarino 	return 0;
594db2a4165SFrank Mandarino 
595db2a4165SFrank Mandarino codec_dai_err:
596db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
597db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
598db2a4165SFrank Mandarino 
599db2a4165SFrank Mandarino machine_err:
600db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
601db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
602db2a4165SFrank Mandarino 
603db2a4165SFrank Mandarino platform_err:
604db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.shutdown)
605db2a4165SFrank Mandarino 		rtd->cpu_dai->ops.shutdown(substream);
606db2a4165SFrank Mandarino out:
607db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
608db2a4165SFrank Mandarino 	return ret;
609db2a4165SFrank Mandarino }
610db2a4165SFrank Mandarino 
611db2a4165SFrank Mandarino /*
612db2a4165SFrank Mandarino  * Power down the audio subsytem pmdown_time msecs after close is called.
613db2a4165SFrank Mandarino  * This is to ensure there are no pops or clicks in between any music tracks
614db2a4165SFrank Mandarino  * due to DAPM power cycling.
615db2a4165SFrank Mandarino  */
616db2a4165SFrank Mandarino static void close_delayed_work(void *data)
617db2a4165SFrank Mandarino {
618db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = data;
619db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
620db2a4165SFrank Mandarino 	struct snd_soc_codec_dai *codec_dai;
621db2a4165SFrank Mandarino 	int i;
622db2a4165SFrank Mandarino 
623db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
624db2a4165SFrank Mandarino 	for(i = 0; i < codec->num_dai; i++) {
625db2a4165SFrank Mandarino 		codec_dai = &codec->dai[i];
626db2a4165SFrank Mandarino 
627db2a4165SFrank Mandarino 		dbg("pop wq checking: %s status: %s waiting: %s\n",
628db2a4165SFrank Mandarino 			codec_dai->playback.stream_name,
629db2a4165SFrank Mandarino 			codec_dai->playback.active ? "active" : "inactive",
630db2a4165SFrank Mandarino 			codec_dai->pop_wait ? "yes" : "no");
631db2a4165SFrank Mandarino 
632db2a4165SFrank Mandarino 		/* are we waiting on this codec DAI stream */
633db2a4165SFrank Mandarino 		if (codec_dai->pop_wait == 1) {
634db2a4165SFrank Mandarino 
635db2a4165SFrank Mandarino 			codec_dai->pop_wait = 0;
636db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
637db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_STOP);
638db2a4165SFrank Mandarino 
639db2a4165SFrank Mandarino 			/* power down the codec power domain if no longer active */
640db2a4165SFrank Mandarino 			if (codec->active == 0) {
641db2a4165SFrank Mandarino 				dbg("pop wq D3 %s %s\n", codec->name,
642db2a4165SFrank Mandarino 					codec_dai->playback.stream_name);
643db2a4165SFrank Mandarino 		 		if (codec->dapm_event)
644db2a4165SFrank Mandarino 					codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
645db2a4165SFrank Mandarino 			}
646db2a4165SFrank Mandarino 		}
647db2a4165SFrank Mandarino 	}
648db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
649db2a4165SFrank Mandarino }
650db2a4165SFrank Mandarino 
651db2a4165SFrank Mandarino /*
652db2a4165SFrank Mandarino  * Called by ALSA when a PCM substream is closed. Private data can be
653db2a4165SFrank Mandarino  * freed here. The cpu DAI, codec DAI, machine and platform are also
654db2a4165SFrank Mandarino  * shutdown.
655db2a4165SFrank Mandarino  */
656db2a4165SFrank Mandarino static int soc_codec_close(struct snd_pcm_substream *substream)
657db2a4165SFrank Mandarino {
658db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
659db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
660db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
661db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
662db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
663db2a4165SFrank Mandarino 
664db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
665db2a4165SFrank Mandarino 
666db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
667db2a4165SFrank Mandarino 		rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0;
668db2a4165SFrank Mandarino 	else
669db2a4165SFrank Mandarino 		rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0;
670db2a4165SFrank Mandarino 
671db2a4165SFrank Mandarino 	if (rtd->codec_dai->playback.active == 0 &&
672db2a4165SFrank Mandarino 		rtd->codec_dai->capture.active == 0) {
673db2a4165SFrank Mandarino 		rtd->cpu_dai->active = rtd->codec_dai->active = 0;
674db2a4165SFrank Mandarino 	}
675db2a4165SFrank Mandarino 	codec->active--;
676db2a4165SFrank Mandarino 
677db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.shutdown)
678db2a4165SFrank Mandarino 		rtd->cpu_dai->ops.shutdown(substream);
679db2a4165SFrank Mandarino 
680db2a4165SFrank Mandarino 	if (rtd->codec_dai->ops.shutdown)
681db2a4165SFrank Mandarino 		rtd->codec_dai->ops.shutdown(substream);
682db2a4165SFrank Mandarino 
683db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->shutdown)
684db2a4165SFrank Mandarino 		machine->ops->shutdown(substream);
685db2a4165SFrank Mandarino 
686db2a4165SFrank Mandarino 	if (platform->pcm_ops->close)
687db2a4165SFrank Mandarino 		platform->pcm_ops->close(substream);
688db2a4165SFrank Mandarino 	rtd->cpu_dai->runtime = NULL;
689db2a4165SFrank Mandarino 
690db2a4165SFrank Mandarino 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
691db2a4165SFrank Mandarino 		/* start delayed pop wq here for playback streams */
692db2a4165SFrank Mandarino 		rtd->codec_dai->pop_wait = 1;
693db2a4165SFrank Mandarino 		queue_delayed_work(soc_workq, &soc_stream_work,
694db2a4165SFrank Mandarino 			msecs_to_jiffies(pmdown_time));
695db2a4165SFrank Mandarino 	} else {
696db2a4165SFrank Mandarino 		/* capture streams can be powered down now */
697db2a4165SFrank Mandarino 		snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name,
698db2a4165SFrank Mandarino 			SND_SOC_DAPM_STREAM_STOP);
699db2a4165SFrank Mandarino 
700db2a4165SFrank Mandarino 		if (codec->active == 0 && rtd->codec_dai->pop_wait == 0){
701db2a4165SFrank Mandarino 			if (codec->dapm_event)
702db2a4165SFrank Mandarino 				codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
703db2a4165SFrank Mandarino 		}
704db2a4165SFrank Mandarino 	}
705db2a4165SFrank Mandarino 
706db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
707db2a4165SFrank Mandarino 	return 0;
708db2a4165SFrank Mandarino }
709db2a4165SFrank Mandarino 
710db2a4165SFrank Mandarino /*
711db2a4165SFrank Mandarino  * Called by ALSA when the PCM substream is prepared, can set format, sample
712db2a4165SFrank Mandarino  * rate, etc.  This function is non atomic and can be called multiple times,
713db2a4165SFrank Mandarino  * it can refer to the runtime info.
714db2a4165SFrank Mandarino  */
715db2a4165SFrank Mandarino static int soc_pcm_prepare(struct snd_pcm_substream *substream)
716db2a4165SFrank Mandarino {
717db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
718db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
719db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
720db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
721db2a4165SFrank Mandarino 	int ret = 0;
722db2a4165SFrank Mandarino 
723db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
724db2a4165SFrank Mandarino 	if (platform->pcm_ops->prepare) {
725db2a4165SFrank Mandarino 		ret = platform->pcm_ops->prepare(substream);
726db2a4165SFrank Mandarino 		if (ret < 0)
727db2a4165SFrank Mandarino 			goto out;
728db2a4165SFrank Mandarino 	}
729db2a4165SFrank Mandarino 
730db2a4165SFrank Mandarino 	if (rtd->codec_dai->ops.prepare) {
731db2a4165SFrank Mandarino 		ret = rtd->codec_dai->ops.prepare(substream);
732db2a4165SFrank Mandarino 		if (ret < 0)
733db2a4165SFrank Mandarino 			goto out;
734db2a4165SFrank Mandarino 	}
735db2a4165SFrank Mandarino 
736db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.prepare)
737db2a4165SFrank Mandarino 		ret = rtd->cpu_dai->ops.prepare(substream);
738db2a4165SFrank Mandarino 
739db2a4165SFrank Mandarino 	/* we only want to start a DAPM playback stream if we are not waiting
740db2a4165SFrank Mandarino 	 * on an existing one stopping */
741db2a4165SFrank Mandarino 	if (rtd->codec_dai->pop_wait) {
742db2a4165SFrank Mandarino 		/* we are waiting for the delayed work to start */
743db2a4165SFrank Mandarino 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
744db2a4165SFrank Mandarino 				snd_soc_dapm_stream_event(codec,
745db2a4165SFrank Mandarino 					rtd->codec_dai->capture.stream_name,
746db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
747db2a4165SFrank Mandarino 		else {
748db2a4165SFrank Mandarino 			rtd->codec_dai->pop_wait = 0;
749db2a4165SFrank Mandarino 			cancel_delayed_work(&soc_stream_work);
750db2a4165SFrank Mandarino 			if (rtd->codec_dai->digital_mute)
751db2a4165SFrank Mandarino 				rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
752db2a4165SFrank Mandarino 		}
753db2a4165SFrank Mandarino 	} else {
754db2a4165SFrank Mandarino 		/* no delayed work - do we need to power up codec */
755db2a4165SFrank Mandarino 		if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
756db2a4165SFrank Mandarino 
757db2a4165SFrank Mandarino 			if (codec->dapm_event)
758db2a4165SFrank Mandarino 				codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
759db2a4165SFrank Mandarino 
760db2a4165SFrank Mandarino 			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
761db2a4165SFrank Mandarino 				snd_soc_dapm_stream_event(codec,
762db2a4165SFrank Mandarino 					rtd->codec_dai->playback.stream_name,
763db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
764db2a4165SFrank Mandarino 			else
765db2a4165SFrank Mandarino 				snd_soc_dapm_stream_event(codec,
766db2a4165SFrank Mandarino 					rtd->codec_dai->capture.stream_name,
767db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
768db2a4165SFrank Mandarino 
769db2a4165SFrank Mandarino 			if (codec->dapm_event)
770db2a4165SFrank Mandarino 				codec->dapm_event(codec, SNDRV_CTL_POWER_D0);
771db2a4165SFrank Mandarino 			if (rtd->codec_dai->digital_mute)
772db2a4165SFrank Mandarino 				rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
773db2a4165SFrank Mandarino 
774db2a4165SFrank Mandarino 		} else {
775db2a4165SFrank Mandarino 			/* codec already powered - power on widgets */
776db2a4165SFrank Mandarino 			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
777db2a4165SFrank Mandarino 				snd_soc_dapm_stream_event(codec,
778db2a4165SFrank Mandarino 					rtd->codec_dai->playback.stream_name,
779db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
780db2a4165SFrank Mandarino 			else
781db2a4165SFrank Mandarino 				snd_soc_dapm_stream_event(codec,
782db2a4165SFrank Mandarino 					rtd->codec_dai->capture.stream_name,
783db2a4165SFrank Mandarino 					SND_SOC_DAPM_STREAM_START);
784db2a4165SFrank Mandarino 			if (rtd->codec_dai->digital_mute)
785db2a4165SFrank Mandarino 				rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
786db2a4165SFrank Mandarino 		}
787db2a4165SFrank Mandarino 	}
788db2a4165SFrank Mandarino 
789db2a4165SFrank Mandarino out:
790db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
791db2a4165SFrank Mandarino 	return ret;
792db2a4165SFrank Mandarino }
793db2a4165SFrank Mandarino 
794db2a4165SFrank Mandarino /*
795db2a4165SFrank Mandarino  * Called by ALSA when the hardware params are set by application. This
796db2a4165SFrank Mandarino  * function can also be called multiple times and can allocate buffers
797db2a4165SFrank Mandarino  * (using snd_pcm_lib_* ). It's non-atomic.
798db2a4165SFrank Mandarino  */
799db2a4165SFrank Mandarino static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
800db2a4165SFrank Mandarino 				struct snd_pcm_hw_params *params)
801db2a4165SFrank Mandarino {
802db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
803db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
804db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
805db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
806db2a4165SFrank Mandarino 	int ret = 0;
807db2a4165SFrank Mandarino 
808db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
809db2a4165SFrank Mandarino 
810db2a4165SFrank Mandarino 	/* we don't need to match any AC97 params */
811db2a4165SFrank Mandarino 	if (rtd->cpu_dai->type != SND_SOC_DAI_AC97) {
812db2a4165SFrank Mandarino 		ret = soc_hw_match_params(substream, params);
813db2a4165SFrank Mandarino 		if (ret < 0)
814db2a4165SFrank Mandarino 			goto out;
815db2a4165SFrank Mandarino 	} else {
816db2a4165SFrank Mandarino 		struct snd_soc_clock_info clk_info;
817db2a4165SFrank Mandarino 		clk_info.rate = params_rate(params);
818db2a4165SFrank Mandarino 		ret = soc_get_mclk(rtd, &clk_info);
819db2a4165SFrank Mandarino 		if (ret < 0)
820db2a4165SFrank Mandarino 			goto out;
821db2a4165SFrank Mandarino 	}
822db2a4165SFrank Mandarino 
823db2a4165SFrank Mandarino 	if (rtd->codec_dai->ops.hw_params) {
824db2a4165SFrank Mandarino 		ret = rtd->codec_dai->ops.hw_params(substream, params);
825db2a4165SFrank Mandarino 		if (ret < 0) {
826db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set codec %s hw params\n",
827db2a4165SFrank Mandarino 				rtd->codec_dai->name);
828db2a4165SFrank Mandarino 			goto out;
829db2a4165SFrank Mandarino 		}
830db2a4165SFrank Mandarino 	}
831db2a4165SFrank Mandarino 
832db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.hw_params) {
833db2a4165SFrank Mandarino 		ret = rtd->cpu_dai->ops.hw_params(substream, params);
834db2a4165SFrank Mandarino 		if (ret < 0) {
835db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set interface %s hw params\n",
836db2a4165SFrank Mandarino 				rtd->cpu_dai->name);
837db2a4165SFrank Mandarino 			goto interface_err;
838db2a4165SFrank Mandarino 		}
839db2a4165SFrank Mandarino 	}
840db2a4165SFrank Mandarino 
841db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_params) {
842db2a4165SFrank Mandarino 		ret = platform->pcm_ops->hw_params(substream, params);
843db2a4165SFrank Mandarino 		if (ret < 0) {
844db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't set platform %s hw params\n",
845db2a4165SFrank Mandarino 				platform->name);
846db2a4165SFrank Mandarino 			goto platform_err;
847db2a4165SFrank Mandarino 		}
848db2a4165SFrank Mandarino 	}
849db2a4165SFrank Mandarino 
850db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->hw_params) {
851db2a4165SFrank Mandarino 		ret = machine->ops->hw_params(substream, params);
852db2a4165SFrank Mandarino 		if (ret < 0) {
853db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: machine hw_params failed\n");
854db2a4165SFrank Mandarino 			goto machine_err;
855db2a4165SFrank Mandarino 		}
856db2a4165SFrank Mandarino 	}
857db2a4165SFrank Mandarino 
858db2a4165SFrank Mandarino out:
859db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
860db2a4165SFrank Mandarino 	return ret;
861db2a4165SFrank Mandarino 
862db2a4165SFrank Mandarino machine_err:
863db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_free)
864db2a4165SFrank Mandarino 		platform->pcm_ops->hw_free(substream);
865db2a4165SFrank Mandarino 
866db2a4165SFrank Mandarino platform_err:
867db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.hw_free)
868db2a4165SFrank Mandarino 		rtd->cpu_dai->ops.hw_free(substream);
869db2a4165SFrank Mandarino 
870db2a4165SFrank Mandarino interface_err:
871db2a4165SFrank Mandarino 	if (rtd->codec_dai->ops.hw_free)
872db2a4165SFrank Mandarino 		rtd->codec_dai->ops.hw_free(substream);
873db2a4165SFrank Mandarino 
874db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
875db2a4165SFrank Mandarino 	return ret;
876db2a4165SFrank Mandarino }
877db2a4165SFrank Mandarino 
878db2a4165SFrank Mandarino /*
879db2a4165SFrank Mandarino  * Free's resources allocated by hw_params, can be called multiple times
880db2a4165SFrank Mandarino  */
881db2a4165SFrank Mandarino static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
882db2a4165SFrank Mandarino {
883db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
884db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
885db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
886db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
887db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
888db2a4165SFrank Mandarino 
889db2a4165SFrank Mandarino 	mutex_lock(&pcm_mutex);
890db2a4165SFrank Mandarino 
891db2a4165SFrank Mandarino 	/* apply codec digital mute */
892db2a4165SFrank Mandarino 	if (!codec->active && rtd->codec_dai->digital_mute)
893db2a4165SFrank Mandarino 		rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 1);
894db2a4165SFrank Mandarino 
895db2a4165SFrank Mandarino 	/* free any machine hw params */
896db2a4165SFrank Mandarino 	if (machine->ops && machine->ops->hw_free)
897db2a4165SFrank Mandarino 		machine->ops->hw_free(substream);
898db2a4165SFrank Mandarino 
899db2a4165SFrank Mandarino 	/* free any DMA resources */
900db2a4165SFrank Mandarino 	if (platform->pcm_ops->hw_free)
901db2a4165SFrank Mandarino 		platform->pcm_ops->hw_free(substream);
902db2a4165SFrank Mandarino 
903db2a4165SFrank Mandarino 	/* now free hw params for the DAI's  */
904db2a4165SFrank Mandarino 	if (rtd->codec_dai->ops.hw_free)
905db2a4165SFrank Mandarino 		rtd->codec_dai->ops.hw_free(substream);
906db2a4165SFrank Mandarino 
907db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.hw_free)
908db2a4165SFrank Mandarino 		rtd->cpu_dai->ops.hw_free(substream);
909db2a4165SFrank Mandarino 
910db2a4165SFrank Mandarino 	mutex_unlock(&pcm_mutex);
911db2a4165SFrank Mandarino 	return 0;
912db2a4165SFrank Mandarino }
913db2a4165SFrank Mandarino 
914db2a4165SFrank Mandarino static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
915db2a4165SFrank Mandarino {
916db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
917db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
918db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
919db2a4165SFrank Mandarino 	int ret;
920db2a4165SFrank Mandarino 
921db2a4165SFrank Mandarino 	if (rtd->codec_dai->ops.trigger) {
922db2a4165SFrank Mandarino 		ret = rtd->codec_dai->ops.trigger(substream, cmd);
923db2a4165SFrank Mandarino 		if (ret < 0)
924db2a4165SFrank Mandarino 			return ret;
925db2a4165SFrank Mandarino 	}
926db2a4165SFrank Mandarino 
927db2a4165SFrank Mandarino 	if (platform->pcm_ops->trigger) {
928db2a4165SFrank Mandarino 		ret = platform->pcm_ops->trigger(substream, cmd);
929db2a4165SFrank Mandarino 		if (ret < 0)
930db2a4165SFrank Mandarino 			return ret;
931db2a4165SFrank Mandarino 	}
932db2a4165SFrank Mandarino 
933db2a4165SFrank Mandarino 	if (rtd->cpu_dai->ops.trigger) {
934db2a4165SFrank Mandarino 		ret = rtd->cpu_dai->ops.trigger(substream, cmd);
935db2a4165SFrank Mandarino 		if (ret < 0)
936db2a4165SFrank Mandarino 			return ret;
937db2a4165SFrank Mandarino 	}
938db2a4165SFrank Mandarino 	return 0;
939db2a4165SFrank Mandarino }
940db2a4165SFrank Mandarino 
941db2a4165SFrank Mandarino /* ASoC PCM operations */
942db2a4165SFrank Mandarino static struct snd_pcm_ops soc_pcm_ops = {
943db2a4165SFrank Mandarino 	.open		= soc_pcm_open,
944db2a4165SFrank Mandarino 	.close		= soc_codec_close,
945db2a4165SFrank Mandarino 	.hw_params	= soc_pcm_hw_params,
946db2a4165SFrank Mandarino 	.hw_free	= soc_pcm_hw_free,
947db2a4165SFrank Mandarino 	.prepare	= soc_pcm_prepare,
948db2a4165SFrank Mandarino 	.trigger	= soc_pcm_trigger,
949db2a4165SFrank Mandarino };
950db2a4165SFrank Mandarino 
951db2a4165SFrank Mandarino #ifdef CONFIG_PM
952db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
953db2a4165SFrank Mandarino static int soc_suspend(struct platform_device *pdev, pm_message_t state)
954db2a4165SFrank Mandarino {
955db2a4165SFrank Mandarino  	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
956db2a4165SFrank Mandarino  	struct snd_soc_machine *machine = socdev->machine;
957db2a4165SFrank Mandarino  	struct snd_soc_platform *platform = socdev->platform;
958db2a4165SFrank Mandarino  	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
959db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
960db2a4165SFrank Mandarino 	int i;
961db2a4165SFrank Mandarino 
962db2a4165SFrank Mandarino 	/* mute any active DAC's */
963db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
964db2a4165SFrank Mandarino 		struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
965db2a4165SFrank Mandarino 		if (dai->digital_mute && dai->playback.active)
966db2a4165SFrank Mandarino 			dai->digital_mute(codec, dai, 1);
967db2a4165SFrank Mandarino 	}
968db2a4165SFrank Mandarino 
969db2a4165SFrank Mandarino 	if (machine->suspend_pre)
970db2a4165SFrank Mandarino 		machine->suspend_pre(pdev, state);
971db2a4165SFrank Mandarino 
972db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
973db2a4165SFrank Mandarino 		struct snd_soc_cpu_dai  *cpu_dai = machine->dai_link[i].cpu_dai;
974db2a4165SFrank Mandarino 		if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
975db2a4165SFrank Mandarino 			cpu_dai->suspend(pdev, cpu_dai);
976db2a4165SFrank Mandarino 		if (platform->suspend)
977db2a4165SFrank Mandarino 			platform->suspend(pdev, cpu_dai);
978db2a4165SFrank Mandarino 	}
979db2a4165SFrank Mandarino 
980db2a4165SFrank Mandarino 	/* close any waiting streams and save state */
981db2a4165SFrank Mandarino 	flush_workqueue(soc_workq);
982db2a4165SFrank Mandarino 	codec->suspend_dapm_state = codec->dapm_state;
983db2a4165SFrank Mandarino 
984db2a4165SFrank Mandarino 	for(i = 0; i < codec->num_dai; i++) {
985db2a4165SFrank Mandarino 		char *stream = codec->dai[i].playback.stream_name;
986db2a4165SFrank Mandarino 		if (stream != NULL)
987db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
988db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
989db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
990db2a4165SFrank Mandarino 		if (stream != NULL)
991db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
992db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_SUSPEND);
993db2a4165SFrank Mandarino 	}
994db2a4165SFrank Mandarino 
995db2a4165SFrank Mandarino 	if (codec_dev->suspend)
996db2a4165SFrank Mandarino 		codec_dev->suspend(pdev, state);
997db2a4165SFrank Mandarino 
998db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
999db2a4165SFrank Mandarino 		struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1000db2a4165SFrank Mandarino 		if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
1001db2a4165SFrank Mandarino 			cpu_dai->suspend(pdev, cpu_dai);
1002db2a4165SFrank Mandarino 	}
1003db2a4165SFrank Mandarino 
1004db2a4165SFrank Mandarino 	if (machine->suspend_post)
1005db2a4165SFrank Mandarino 		machine->suspend_post(pdev, state);
1006db2a4165SFrank Mandarino 
1007db2a4165SFrank Mandarino 	return 0;
1008db2a4165SFrank Mandarino }
1009db2a4165SFrank Mandarino 
1010db2a4165SFrank Mandarino /* powers up audio subsystem after a suspend */
1011db2a4165SFrank Mandarino static int soc_resume(struct platform_device *pdev)
1012db2a4165SFrank Mandarino {
1013db2a4165SFrank Mandarino  	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1014db2a4165SFrank Mandarino  	struct snd_soc_machine *machine = socdev->machine;
1015db2a4165SFrank Mandarino  	struct snd_soc_platform *platform = socdev->platform;
1016db2a4165SFrank Mandarino  	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1017db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
1018db2a4165SFrank Mandarino 	int i;
1019db2a4165SFrank Mandarino 
1020db2a4165SFrank Mandarino 	if (machine->resume_pre)
1021db2a4165SFrank Mandarino 		machine->resume_pre(pdev);
1022db2a4165SFrank Mandarino 
1023db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
1024db2a4165SFrank Mandarino 		struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1025db2a4165SFrank Mandarino 		if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
1026db2a4165SFrank Mandarino 			cpu_dai->resume(pdev, cpu_dai);
1027db2a4165SFrank Mandarino 	}
1028db2a4165SFrank Mandarino 
1029db2a4165SFrank Mandarino 	if (codec_dev->resume)
1030db2a4165SFrank Mandarino 		codec_dev->resume(pdev);
1031db2a4165SFrank Mandarino 
1032db2a4165SFrank Mandarino 	for(i = 0; i < codec->num_dai; i++) {
1033db2a4165SFrank Mandarino 		char* stream = codec->dai[i].playback.stream_name;
1034db2a4165SFrank Mandarino 		if (stream != NULL)
1035db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
1036db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1037db2a4165SFrank Mandarino 		stream = codec->dai[i].capture.stream_name;
1038db2a4165SFrank Mandarino 		if (stream != NULL)
1039db2a4165SFrank Mandarino 			snd_soc_dapm_stream_event(codec, stream,
1040db2a4165SFrank Mandarino 				SND_SOC_DAPM_STREAM_RESUME);
1041db2a4165SFrank Mandarino 	}
1042db2a4165SFrank Mandarino 
1043db2a4165SFrank Mandarino 	/* unmute any active DAC's */
1044db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
1045db2a4165SFrank Mandarino 		struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
1046db2a4165SFrank Mandarino 		if (dai->digital_mute && dai->playback.active)
1047db2a4165SFrank Mandarino 			dai->digital_mute(codec, dai, 0);
1048db2a4165SFrank Mandarino 	}
1049db2a4165SFrank Mandarino 
1050db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
1051db2a4165SFrank Mandarino 		struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1052db2a4165SFrank Mandarino 		if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
1053db2a4165SFrank Mandarino 			cpu_dai->resume(pdev, cpu_dai);
1054db2a4165SFrank Mandarino 		if (platform->resume)
1055db2a4165SFrank Mandarino 			platform->resume(pdev, cpu_dai);
1056db2a4165SFrank Mandarino 	}
1057db2a4165SFrank Mandarino 
1058db2a4165SFrank Mandarino 	if (machine->resume_post)
1059db2a4165SFrank Mandarino 		machine->resume_post(pdev);
1060db2a4165SFrank Mandarino 
1061db2a4165SFrank Mandarino 	return 0;
1062db2a4165SFrank Mandarino }
1063db2a4165SFrank Mandarino 
1064db2a4165SFrank Mandarino #else
1065db2a4165SFrank Mandarino #define soc_suspend	NULL
1066db2a4165SFrank Mandarino #define soc_resume	NULL
1067db2a4165SFrank Mandarino #endif
1068db2a4165SFrank Mandarino 
1069db2a4165SFrank Mandarino /* probes a new socdev */
1070db2a4165SFrank Mandarino static int soc_probe(struct platform_device *pdev)
1071db2a4165SFrank Mandarino {
1072db2a4165SFrank Mandarino 	int ret = 0, i;
1073db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1074db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
1075db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
1076db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1077db2a4165SFrank Mandarino 
1078db2a4165SFrank Mandarino 	if (machine->probe) {
1079db2a4165SFrank Mandarino 		ret = machine->probe(pdev);
1080db2a4165SFrank Mandarino 		if(ret < 0)
1081db2a4165SFrank Mandarino 			return ret;
1082db2a4165SFrank Mandarino 	}
1083db2a4165SFrank Mandarino 
1084db2a4165SFrank Mandarino 	for (i = 0; i < machine->num_links; i++) {
1085db2a4165SFrank Mandarino 		struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1086db2a4165SFrank Mandarino 		if (cpu_dai->probe) {
1087db2a4165SFrank Mandarino 			ret = cpu_dai->probe(pdev);
1088db2a4165SFrank Mandarino 			if(ret < 0)
1089db2a4165SFrank Mandarino 				goto cpu_dai_err;
1090db2a4165SFrank Mandarino 		}
1091db2a4165SFrank Mandarino 	}
1092db2a4165SFrank Mandarino 
1093db2a4165SFrank Mandarino 	if (codec_dev->probe) {
1094db2a4165SFrank Mandarino 		ret = codec_dev->probe(pdev);
1095db2a4165SFrank Mandarino 		if(ret < 0)
1096db2a4165SFrank Mandarino 			goto cpu_dai_err;
1097db2a4165SFrank Mandarino 	}
1098db2a4165SFrank Mandarino 
1099db2a4165SFrank Mandarino 	if (platform->probe) {
1100db2a4165SFrank Mandarino 		ret = platform->probe(pdev);
1101db2a4165SFrank Mandarino 		if(ret < 0)
1102db2a4165SFrank Mandarino 			goto platform_err;
1103db2a4165SFrank Mandarino 	}
1104db2a4165SFrank Mandarino 
1105db2a4165SFrank Mandarino 	/* DAPM stream work */
1106db2a4165SFrank Mandarino 	soc_workq = create_workqueue("kdapm");
1107db2a4165SFrank Mandarino 	if (soc_workq == NULL)
1108db2a4165SFrank Mandarino 		goto work_err;
1109db2a4165SFrank Mandarino 	INIT_WORK(&soc_stream_work, close_delayed_work, socdev);
1110db2a4165SFrank Mandarino 	return 0;
1111db2a4165SFrank Mandarino 
1112db2a4165SFrank Mandarino work_err:
1113db2a4165SFrank Mandarino 	if (platform->remove)
1114db2a4165SFrank Mandarino 		platform->remove(pdev);
1115db2a4165SFrank Mandarino 
1116db2a4165SFrank Mandarino platform_err:
1117db2a4165SFrank Mandarino 	if (codec_dev->remove)
1118db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1119db2a4165SFrank Mandarino 
1120db2a4165SFrank Mandarino cpu_dai_err:
1121db2a4165SFrank Mandarino 	for (i--; i > 0; i--) {
1122db2a4165SFrank Mandarino 		struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1123db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1124db2a4165SFrank Mandarino 			cpu_dai->remove(pdev);
1125db2a4165SFrank Mandarino 	}
1126db2a4165SFrank Mandarino 
1127db2a4165SFrank Mandarino 	if (machine->remove)
1128db2a4165SFrank Mandarino 		machine->remove(pdev);
1129db2a4165SFrank Mandarino 
1130db2a4165SFrank Mandarino 	return ret;
1131db2a4165SFrank Mandarino }
1132db2a4165SFrank Mandarino 
1133db2a4165SFrank Mandarino /* removes a socdev */
1134db2a4165SFrank Mandarino static int soc_remove(struct platform_device *pdev)
1135db2a4165SFrank Mandarino {
1136db2a4165SFrank Mandarino 	int i;
1137db2a4165SFrank Mandarino 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1138db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
1139db2a4165SFrank Mandarino 	struct snd_soc_platform *platform = socdev->platform;
1140db2a4165SFrank Mandarino 	struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1141db2a4165SFrank Mandarino 
1142db2a4165SFrank Mandarino 	if (soc_workq)
1143db2a4165SFrank Mandarino 		destroy_workqueue(soc_workq);
1144db2a4165SFrank Mandarino 
1145db2a4165SFrank Mandarino 	if (platform->remove)
1146db2a4165SFrank Mandarino 		platform->remove(pdev);
1147db2a4165SFrank Mandarino 
1148db2a4165SFrank Mandarino 	if (codec_dev->remove)
1149db2a4165SFrank Mandarino 		codec_dev->remove(pdev);
1150db2a4165SFrank Mandarino 
1151db2a4165SFrank Mandarino 	for (i = 0; i < machine->num_links; i++) {
1152db2a4165SFrank Mandarino 		struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1153db2a4165SFrank Mandarino 		if (cpu_dai->remove)
1154db2a4165SFrank Mandarino 			cpu_dai->remove(pdev);
1155db2a4165SFrank Mandarino 	}
1156db2a4165SFrank Mandarino 
1157db2a4165SFrank Mandarino 	if (machine->remove)
1158db2a4165SFrank Mandarino 		machine->remove(pdev);
1159db2a4165SFrank Mandarino 
1160db2a4165SFrank Mandarino 	return 0;
1161db2a4165SFrank Mandarino }
1162db2a4165SFrank Mandarino 
1163db2a4165SFrank Mandarino /* ASoC platform driver */
1164db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
1165db2a4165SFrank Mandarino 	.driver		= {
1166db2a4165SFrank Mandarino 		.name		= "soc-audio",
1167db2a4165SFrank Mandarino 	},
1168db2a4165SFrank Mandarino 	.probe		= soc_probe,
1169db2a4165SFrank Mandarino 	.remove		= soc_remove,
1170db2a4165SFrank Mandarino 	.suspend	= soc_suspend,
1171db2a4165SFrank Mandarino 	.resume		= soc_resume,
1172db2a4165SFrank Mandarino };
1173db2a4165SFrank Mandarino 
1174db2a4165SFrank Mandarino /* create a new pcm */
1175db2a4165SFrank Mandarino static int soc_new_pcm(struct snd_soc_device *socdev,
1176db2a4165SFrank Mandarino 	struct snd_soc_dai_link *dai_link, int num)
1177db2a4165SFrank Mandarino {
1178db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
1179db2a4165SFrank Mandarino 	struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
1180db2a4165SFrank Mandarino 	struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
1181db2a4165SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd;
1182db2a4165SFrank Mandarino 	struct snd_pcm *pcm;
1183db2a4165SFrank Mandarino 	char new_name[64];
1184db2a4165SFrank Mandarino 	int ret = 0, playback = 0, capture = 0;
1185db2a4165SFrank Mandarino 
1186db2a4165SFrank Mandarino 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1187db2a4165SFrank Mandarino 	if (rtd == NULL)
1188db2a4165SFrank Mandarino 		return -ENOMEM;
1189db2a4165SFrank Mandarino 	rtd->cpu_dai = cpu_dai;
1190db2a4165SFrank Mandarino 	rtd->codec_dai = codec_dai;
1191db2a4165SFrank Mandarino 	rtd->socdev = socdev;
1192db2a4165SFrank Mandarino 
1193db2a4165SFrank Mandarino 	/* check client and interface hw capabilities */
1194db2a4165SFrank Mandarino 	sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
1195db2a4165SFrank Mandarino 		get_dai_name(cpu_dai->type), num);
1196db2a4165SFrank Mandarino 
1197db2a4165SFrank Mandarino 	if (codec_dai->playback.channels_min)
1198db2a4165SFrank Mandarino 		playback = 1;
1199db2a4165SFrank Mandarino 	if (codec_dai->capture.channels_min)
1200db2a4165SFrank Mandarino 		capture = 1;
1201db2a4165SFrank Mandarino 
1202db2a4165SFrank Mandarino 	ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1203db2a4165SFrank Mandarino 		capture, &pcm);
1204db2a4165SFrank Mandarino 	if (ret < 0) {
1205db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1206db2a4165SFrank Mandarino 		kfree(rtd);
1207db2a4165SFrank Mandarino 		return ret;
1208db2a4165SFrank Mandarino 	}
1209db2a4165SFrank Mandarino 
1210db2a4165SFrank Mandarino 	pcm->private_data = rtd;
1211db2a4165SFrank Mandarino 	soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
1212db2a4165SFrank Mandarino 	soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
1213db2a4165SFrank Mandarino 	soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
1214db2a4165SFrank Mandarino 	soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
1215db2a4165SFrank Mandarino 	soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
1216db2a4165SFrank Mandarino 	soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
1217db2a4165SFrank Mandarino 	soc_pcm_ops.page = socdev->platform->pcm_ops->page;
1218db2a4165SFrank Mandarino 
1219db2a4165SFrank Mandarino 	if (playback)
1220db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1221db2a4165SFrank Mandarino 
1222db2a4165SFrank Mandarino 	if (capture)
1223db2a4165SFrank Mandarino 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1224db2a4165SFrank Mandarino 
1225db2a4165SFrank Mandarino 	ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
1226db2a4165SFrank Mandarino 	if (ret < 0) {
1227db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1228db2a4165SFrank Mandarino 		kfree(rtd);
1229db2a4165SFrank Mandarino 		return ret;
1230db2a4165SFrank Mandarino 	}
1231db2a4165SFrank Mandarino 
1232db2a4165SFrank Mandarino 	pcm->private_free = socdev->platform->pcm_free;
1233db2a4165SFrank Mandarino 	printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1234db2a4165SFrank Mandarino 		cpu_dai->name);
1235db2a4165SFrank Mandarino 	return ret;
1236db2a4165SFrank Mandarino }
1237db2a4165SFrank Mandarino 
1238db2a4165SFrank Mandarino /* codec register dump */
1239db2a4165SFrank Mandarino static ssize_t codec_reg_show(struct device *dev,
1240db2a4165SFrank Mandarino 	struct device_attribute *attr, char *buf)
1241db2a4165SFrank Mandarino {
1242db2a4165SFrank Mandarino 	struct snd_soc_device *devdata = dev_get_drvdata(dev);
1243db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = devdata->codec;
1244db2a4165SFrank Mandarino 	int i, step = 1, count = 0;
1245db2a4165SFrank Mandarino 
1246db2a4165SFrank Mandarino 	if (!codec->reg_cache_size)
1247db2a4165SFrank Mandarino 		return 0;
1248db2a4165SFrank Mandarino 
1249db2a4165SFrank Mandarino 	if (codec->reg_cache_step)
1250db2a4165SFrank Mandarino 		step = codec->reg_cache_step;
1251db2a4165SFrank Mandarino 
1252db2a4165SFrank Mandarino 	count += sprintf(buf, "%s registers\n", codec->name);
1253db2a4165SFrank Mandarino 	for(i = 0; i < codec->reg_cache_size; i += step)
1254db2a4165SFrank Mandarino 		count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
1255db2a4165SFrank Mandarino 
1256db2a4165SFrank Mandarino 	return count;
1257db2a4165SFrank Mandarino }
1258db2a4165SFrank Mandarino static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1259db2a4165SFrank Mandarino 
1260db2a4165SFrank Mandarino /**
1261db2a4165SFrank Mandarino  * snd_soc_new_ac97_codec - initailise AC97 device
1262db2a4165SFrank Mandarino  * @codec: audio codec
1263db2a4165SFrank Mandarino  * @ops: AC97 bus operations
1264db2a4165SFrank Mandarino  * @num: AC97 codec number
1265db2a4165SFrank Mandarino  *
1266db2a4165SFrank Mandarino  * Initialises AC97 codec resources for use by ad-hoc devices only.
1267db2a4165SFrank Mandarino  */
1268db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1269db2a4165SFrank Mandarino 	struct snd_ac97_bus_ops *ops, int num)
1270db2a4165SFrank Mandarino {
1271db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1272db2a4165SFrank Mandarino 
1273db2a4165SFrank Mandarino 	codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1274db2a4165SFrank Mandarino 	if (codec->ac97 == NULL) {
1275db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1276db2a4165SFrank Mandarino 		return -ENOMEM;
1277db2a4165SFrank Mandarino 	}
1278db2a4165SFrank Mandarino 
1279db2a4165SFrank Mandarino 	codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1280db2a4165SFrank Mandarino 	if (codec->ac97->bus == NULL) {
1281db2a4165SFrank Mandarino 		kfree(codec->ac97);
1282db2a4165SFrank Mandarino 		codec->ac97 = NULL;
1283db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1284db2a4165SFrank Mandarino 		return -ENOMEM;
1285db2a4165SFrank Mandarino 	}
1286db2a4165SFrank Mandarino 
1287db2a4165SFrank Mandarino 	codec->ac97->bus->ops = ops;
1288db2a4165SFrank Mandarino 	codec->ac97->num = num;
1289db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1290db2a4165SFrank Mandarino 	return 0;
1291db2a4165SFrank Mandarino }
1292db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1293db2a4165SFrank Mandarino 
1294db2a4165SFrank Mandarino /**
1295db2a4165SFrank Mandarino  * snd_soc_free_ac97_codec - free AC97 codec device
1296db2a4165SFrank Mandarino  * @codec: audio codec
1297db2a4165SFrank Mandarino  *
1298db2a4165SFrank Mandarino  * Frees AC97 codec device resources.
1299db2a4165SFrank Mandarino  */
1300db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1301db2a4165SFrank Mandarino {
1302db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1303db2a4165SFrank Mandarino 	kfree(codec->ac97->bus);
1304db2a4165SFrank Mandarino 	kfree(codec->ac97);
1305db2a4165SFrank Mandarino 	codec->ac97 = NULL;
1306db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1307db2a4165SFrank Mandarino }
1308db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1309db2a4165SFrank Mandarino 
1310db2a4165SFrank Mandarino /**
1311db2a4165SFrank Mandarino  * snd_soc_update_bits - update codec register bits
1312db2a4165SFrank Mandarino  * @codec: audio codec
1313db2a4165SFrank Mandarino  * @reg: codec register
1314db2a4165SFrank Mandarino  * @mask: register mask
1315db2a4165SFrank Mandarino  * @value: new value
1316db2a4165SFrank Mandarino  *
1317db2a4165SFrank Mandarino  * Writes new register value.
1318db2a4165SFrank Mandarino  *
1319db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1320db2a4165SFrank Mandarino  */
1321db2a4165SFrank Mandarino int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1322db2a4165SFrank Mandarino 				unsigned short mask, unsigned short value)
1323db2a4165SFrank Mandarino {
1324db2a4165SFrank Mandarino 	int change;
1325db2a4165SFrank Mandarino 	unsigned short old, new;
1326db2a4165SFrank Mandarino 
1327db2a4165SFrank Mandarino 	mutex_lock(&io_mutex);
1328db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1329db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1330db2a4165SFrank Mandarino 	change = old != new;
1331db2a4165SFrank Mandarino 	if (change)
1332db2a4165SFrank Mandarino 		snd_soc_write(codec, reg, new);
1333db2a4165SFrank Mandarino 
1334db2a4165SFrank Mandarino 	mutex_unlock(&io_mutex);
1335db2a4165SFrank Mandarino 	return change;
1336db2a4165SFrank Mandarino }
1337db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1338db2a4165SFrank Mandarino 
1339db2a4165SFrank Mandarino /**
1340db2a4165SFrank Mandarino  * snd_soc_test_bits - test register for change
1341db2a4165SFrank Mandarino  * @codec: audio codec
1342db2a4165SFrank Mandarino  * @reg: codec register
1343db2a4165SFrank Mandarino  * @mask: register mask
1344db2a4165SFrank Mandarino  * @value: new value
1345db2a4165SFrank Mandarino  *
1346db2a4165SFrank Mandarino  * Tests a register with a new value and checks if the new value is
1347db2a4165SFrank Mandarino  * different from the old value.
1348db2a4165SFrank Mandarino  *
1349db2a4165SFrank Mandarino  * Returns 1 for change else 0.
1350db2a4165SFrank Mandarino  */
1351db2a4165SFrank Mandarino int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1352db2a4165SFrank Mandarino 				unsigned short mask, unsigned short value)
1353db2a4165SFrank Mandarino {
1354db2a4165SFrank Mandarino 	int change;
1355db2a4165SFrank Mandarino 	unsigned short old, new;
1356db2a4165SFrank Mandarino 
1357db2a4165SFrank Mandarino 	mutex_lock(&io_mutex);
1358db2a4165SFrank Mandarino 	old = snd_soc_read(codec, reg);
1359db2a4165SFrank Mandarino 	new = (old & ~mask) | value;
1360db2a4165SFrank Mandarino 	change = old != new;
1361db2a4165SFrank Mandarino 	mutex_unlock(&io_mutex);
1362db2a4165SFrank Mandarino 
1363db2a4165SFrank Mandarino 	return change;
1364db2a4165SFrank Mandarino }
1365db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1366db2a4165SFrank Mandarino 
1367db2a4165SFrank Mandarino /**
1368db2a4165SFrank Mandarino  * snd_soc_get_rate - get int sample rate
1369db2a4165SFrank Mandarino  * @hwpcmrate: the hardware pcm rate
1370db2a4165SFrank Mandarino  *
1371db2a4165SFrank Mandarino  * Returns the audio rate integaer value, else 0.
1372db2a4165SFrank Mandarino  */
1373db2a4165SFrank Mandarino int snd_soc_get_rate(int hwpcmrate)
1374db2a4165SFrank Mandarino {
1375db2a4165SFrank Mandarino 	int rate = ffs(hwpcmrate) - 1;
1376db2a4165SFrank Mandarino 
1377db2a4165SFrank Mandarino 	if (rate > ARRAY_SIZE(rates))
1378db2a4165SFrank Mandarino 		return 0;
1379db2a4165SFrank Mandarino 	return rates[rate];
1380db2a4165SFrank Mandarino }
1381db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_rate);
1382db2a4165SFrank Mandarino 
1383db2a4165SFrank Mandarino /**
1384db2a4165SFrank Mandarino  * snd_soc_new_pcms - create new sound card and pcms
1385db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1386db2a4165SFrank Mandarino  *
1387db2a4165SFrank Mandarino  * Create a new sound card based upon the codec and interface pcms.
1388db2a4165SFrank Mandarino  *
1389db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1390db2a4165SFrank Mandarino  */
1391db2a4165SFrank Mandarino int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid)
1392db2a4165SFrank Mandarino {
1393db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
1394db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
1395db2a4165SFrank Mandarino 	int ret = 0, i;
1396db2a4165SFrank Mandarino 
1397db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1398db2a4165SFrank Mandarino 
1399db2a4165SFrank Mandarino 	/* register a sound card */
1400db2a4165SFrank Mandarino 	codec->card = snd_card_new(idx, xid, codec->owner, 0);
1401db2a4165SFrank Mandarino 	if (!codec->card) {
1402db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1403db2a4165SFrank Mandarino 			codec->name);
1404db2a4165SFrank Mandarino 		mutex_unlock(&codec->mutex);
1405db2a4165SFrank Mandarino 		return -ENODEV;
1406db2a4165SFrank Mandarino 	}
1407db2a4165SFrank Mandarino 
1408db2a4165SFrank Mandarino 	codec->card->dev = socdev->dev;
1409db2a4165SFrank Mandarino 	codec->card->private_data = codec;
1410db2a4165SFrank Mandarino 	strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1411db2a4165SFrank Mandarino 
1412db2a4165SFrank Mandarino 	/* create the pcms */
1413db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
1414db2a4165SFrank Mandarino 		ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1415db2a4165SFrank Mandarino 		if (ret < 0) {
1416db2a4165SFrank Mandarino 			printk(KERN_ERR "asoc: can't create pcm %s\n",
1417db2a4165SFrank Mandarino 				machine->dai_link[i].stream_name);
1418db2a4165SFrank Mandarino 			mutex_unlock(&codec->mutex);
1419db2a4165SFrank Mandarino 			return ret;
1420db2a4165SFrank Mandarino 		}
1421db2a4165SFrank Mandarino 	}
1422db2a4165SFrank Mandarino 
1423db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1424db2a4165SFrank Mandarino 	return ret;
1425db2a4165SFrank Mandarino }
1426db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1427db2a4165SFrank Mandarino 
1428db2a4165SFrank Mandarino /**
1429db2a4165SFrank Mandarino  * snd_soc_register_card - register sound card
1430db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1431db2a4165SFrank Mandarino  *
1432db2a4165SFrank Mandarino  * Register a SoC sound card. Also registers an AC97 device if the
1433db2a4165SFrank Mandarino  * codec is AC97 for ad hoc devices.
1434db2a4165SFrank Mandarino  *
1435db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1436db2a4165SFrank Mandarino  */
1437db2a4165SFrank Mandarino int snd_soc_register_card(struct snd_soc_device *socdev)
1438db2a4165SFrank Mandarino {
1439db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
1440db2a4165SFrank Mandarino 	struct snd_soc_machine *machine = socdev->machine;
1441*12e74f7dSLiam Girdwood 	int ret = 0, i, ac97 = 0, err = 0;
1442db2a4165SFrank Mandarino 
1443db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1444db2a4165SFrank Mandarino 	for(i = 0; i < machine->num_links; i++) {
1445*12e74f7dSLiam Girdwood 		if (socdev->machine->dai_link[i].init) {
1446*12e74f7dSLiam Girdwood 			err = socdev->machine->dai_link[i].init(codec);
1447*12e74f7dSLiam Girdwood 			if (err < 0) {
1448*12e74f7dSLiam Girdwood 				printk(KERN_ERR "asoc: failed to init %s\n",
1449*12e74f7dSLiam Girdwood 					socdev->machine->dai_link[i].stream_name);
1450*12e74f7dSLiam Girdwood 				continue;
1451*12e74f7dSLiam Girdwood 			}
1452*12e74f7dSLiam Girdwood 		}
1453db2a4165SFrank Mandarino 		if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
1454db2a4165SFrank Mandarino 			ac97 = 1;
1455db2a4165SFrank Mandarino 	}
1456db2a4165SFrank Mandarino 	snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1457db2a4165SFrank Mandarino 		 "%s", machine->name);
1458db2a4165SFrank Mandarino 	snprintf(codec->card->longname, sizeof(codec->card->longname),
1459db2a4165SFrank Mandarino 		 "%s (%s)", machine->name, codec->name);
1460db2a4165SFrank Mandarino 
1461db2a4165SFrank Mandarino 	ret = snd_card_register(codec->card);
1462db2a4165SFrank Mandarino 	if (ret < 0) {
1463db2a4165SFrank Mandarino 		printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1464db2a4165SFrank Mandarino 				codec->name);
1465*12e74f7dSLiam Girdwood 		goto out;
1466db2a4165SFrank Mandarino 	}
1467db2a4165SFrank Mandarino 
1468db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1469*12e74f7dSLiam Girdwood 	if (ac97) {
1470*12e74f7dSLiam Girdwood 		ret = soc_ac97_dev_register(codec);
1471*12e74f7dSLiam Girdwood 		if (ret < 0) {
1472*12e74f7dSLiam Girdwood 			printk(KERN_ERR "asoc: AC97 device register failed\n");
1473*12e74f7dSLiam Girdwood 			snd_card_free(codec->card);
1474*12e74f7dSLiam Girdwood 			goto out;
1475*12e74f7dSLiam Girdwood 		}
1476*12e74f7dSLiam Girdwood 	}
1477db2a4165SFrank Mandarino #endif
1478db2a4165SFrank Mandarino 
1479*12e74f7dSLiam Girdwood 	err = snd_soc_dapm_sys_add(socdev->dev);
1480*12e74f7dSLiam Girdwood 	if (err < 0)
1481*12e74f7dSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1482*12e74f7dSLiam Girdwood 
1483*12e74f7dSLiam Girdwood 	err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1484*12e74f7dSLiam Girdwood 	if (err < 0)
1485*12e74f7dSLiam Girdwood 		printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
1486*12e74f7dSLiam Girdwood out:
1487db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1488db2a4165SFrank Mandarino 	return ret;
1489db2a4165SFrank Mandarino }
1490db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_register_card);
1491db2a4165SFrank Mandarino 
1492db2a4165SFrank Mandarino /**
1493db2a4165SFrank Mandarino  * snd_soc_free_pcms - free sound card and pcms
1494db2a4165SFrank Mandarino  * @socdev: the SoC audio device
1495db2a4165SFrank Mandarino  *
1496db2a4165SFrank Mandarino  * Frees sound card and pcms associated with the socdev.
1497db2a4165SFrank Mandarino  * Also unregister the codec if it is an AC97 device.
1498db2a4165SFrank Mandarino  */
1499db2a4165SFrank Mandarino void snd_soc_free_pcms(struct snd_soc_device *socdev)
1500db2a4165SFrank Mandarino {
1501db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
1502db2a4165SFrank Mandarino 
1503db2a4165SFrank Mandarino 	mutex_lock(&codec->mutex);
1504db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS
1505db2a4165SFrank Mandarino 	if (codec->ac97)
1506db2a4165SFrank Mandarino 		soc_ac97_dev_unregister(codec);
1507db2a4165SFrank Mandarino #endif
1508db2a4165SFrank Mandarino 
1509db2a4165SFrank Mandarino 	if (codec->card)
1510db2a4165SFrank Mandarino 		snd_card_free(codec->card);
1511db2a4165SFrank Mandarino 	device_remove_file(socdev->dev, &dev_attr_codec_reg);
1512db2a4165SFrank Mandarino 	mutex_unlock(&codec->mutex);
1513db2a4165SFrank Mandarino }
1514db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1515db2a4165SFrank Mandarino 
1516db2a4165SFrank Mandarino /**
1517db2a4165SFrank Mandarino  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1518db2a4165SFrank Mandarino  * @substream: the pcm substream
1519db2a4165SFrank Mandarino  * @hw: the hardware parameters
1520db2a4165SFrank Mandarino  *
1521db2a4165SFrank Mandarino  * Sets the substream runtime hardware parameters.
1522db2a4165SFrank Mandarino  */
1523db2a4165SFrank Mandarino int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1524db2a4165SFrank Mandarino 	const struct snd_pcm_hardware *hw)
1525db2a4165SFrank Mandarino {
1526db2a4165SFrank Mandarino 	struct snd_pcm_runtime *runtime = substream->runtime;
1527db2a4165SFrank Mandarino 	runtime->hw.info = hw->info;
1528db2a4165SFrank Mandarino 	runtime->hw.formats = hw->formats;
1529db2a4165SFrank Mandarino 	runtime->hw.period_bytes_min = hw->period_bytes_min;
1530db2a4165SFrank Mandarino 	runtime->hw.period_bytes_max = hw->period_bytes_max;
1531db2a4165SFrank Mandarino 	runtime->hw.periods_min = hw->periods_min;
1532db2a4165SFrank Mandarino 	runtime->hw.periods_max = hw->periods_max;
1533db2a4165SFrank Mandarino 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1534db2a4165SFrank Mandarino 	runtime->hw.fifo_size = hw->fifo_size;
1535db2a4165SFrank Mandarino 	return 0;
1536db2a4165SFrank Mandarino }
1537db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1538db2a4165SFrank Mandarino 
1539db2a4165SFrank Mandarino /**
1540db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
1541db2a4165SFrank Mandarino  * @_template: control template
1542db2a4165SFrank Mandarino  * @data: control private data
1543db2a4165SFrank Mandarino  * @lnng_name: control long name
1544db2a4165SFrank Mandarino  *
1545db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
1546db2a4165SFrank Mandarino  *
1547db2a4165SFrank Mandarino  * Returns 0 for success, else error.
1548db2a4165SFrank Mandarino  */
1549db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1550db2a4165SFrank Mandarino 	void *data, char *long_name)
1551db2a4165SFrank Mandarino {
1552db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
1553db2a4165SFrank Mandarino 
1554db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
1555db2a4165SFrank Mandarino 	if (long_name)
1556db2a4165SFrank Mandarino 		template.name = long_name;
1557db2a4165SFrank Mandarino 	template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1558db2a4165SFrank Mandarino 	template.index = 0;
1559db2a4165SFrank Mandarino 
1560db2a4165SFrank Mandarino 	return snd_ctl_new1(&template, data);
1561db2a4165SFrank Mandarino }
1562db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
1563db2a4165SFrank Mandarino 
1564db2a4165SFrank Mandarino /**
1565db2a4165SFrank Mandarino  * snd_soc_info_enum_double - enumerated double mixer info callback
1566db2a4165SFrank Mandarino  * @kcontrol: mixer control
1567db2a4165SFrank Mandarino  * @uinfo: control element information
1568db2a4165SFrank Mandarino  *
1569db2a4165SFrank Mandarino  * Callback to provide information about a double enumerated
1570db2a4165SFrank Mandarino  * mixer control.
1571db2a4165SFrank Mandarino  *
1572db2a4165SFrank Mandarino  * Returns 0 for success.
1573db2a4165SFrank Mandarino  */
1574db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1575db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1576db2a4165SFrank Mandarino {
1577db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1578db2a4165SFrank Mandarino 
1579db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1580db2a4165SFrank Mandarino 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1581db2a4165SFrank Mandarino 	uinfo->value.enumerated.items = e->mask;
1582db2a4165SFrank Mandarino 
1583db2a4165SFrank Mandarino 	if (uinfo->value.enumerated.item > e->mask - 1)
1584db2a4165SFrank Mandarino 		uinfo->value.enumerated.item = e->mask - 1;
1585db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1586db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1587db2a4165SFrank Mandarino 	return 0;
1588db2a4165SFrank Mandarino }
1589db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1590db2a4165SFrank Mandarino 
1591db2a4165SFrank Mandarino /**
1592db2a4165SFrank Mandarino  * snd_soc_get_enum_double - enumerated double mixer get callback
1593db2a4165SFrank Mandarino  * @kcontrol: mixer control
1594db2a4165SFrank Mandarino  * @uinfo: control element information
1595db2a4165SFrank Mandarino  *
1596db2a4165SFrank Mandarino  * Callback to get the value of a double enumerated mixer.
1597db2a4165SFrank Mandarino  *
1598db2a4165SFrank Mandarino  * Returns 0 for success.
1599db2a4165SFrank Mandarino  */
1600db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1601db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1602db2a4165SFrank Mandarino {
1603db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1604db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1605db2a4165SFrank Mandarino 	unsigned short val, bitmask;
1606db2a4165SFrank Mandarino 
1607db2a4165SFrank Mandarino 	for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1608db2a4165SFrank Mandarino 		;
1609db2a4165SFrank Mandarino 	val = snd_soc_read(codec, e->reg);
1610db2a4165SFrank Mandarino 	ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1611db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r)
1612db2a4165SFrank Mandarino 		ucontrol->value.enumerated.item[1] =
1613db2a4165SFrank Mandarino 			(val >> e->shift_r) & (bitmask - 1);
1614db2a4165SFrank Mandarino 
1615db2a4165SFrank Mandarino 	return 0;
1616db2a4165SFrank Mandarino }
1617db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1618db2a4165SFrank Mandarino 
1619db2a4165SFrank Mandarino /**
1620db2a4165SFrank Mandarino  * snd_soc_put_enum_double - enumerated double mixer put callback
1621db2a4165SFrank Mandarino  * @kcontrol: mixer control
1622db2a4165SFrank Mandarino  * @uinfo: control element information
1623db2a4165SFrank Mandarino  *
1624db2a4165SFrank Mandarino  * Callback to set the value of a double enumerated mixer.
1625db2a4165SFrank Mandarino  *
1626db2a4165SFrank Mandarino  * Returns 0 for success.
1627db2a4165SFrank Mandarino  */
1628db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1629db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1630db2a4165SFrank Mandarino {
1631db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1632db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1633db2a4165SFrank Mandarino 	unsigned short val;
1634db2a4165SFrank Mandarino 	unsigned short mask, bitmask;
1635db2a4165SFrank Mandarino 
1636db2a4165SFrank Mandarino 	for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1637db2a4165SFrank Mandarino 		;
1638db2a4165SFrank Mandarino 	if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1639db2a4165SFrank Mandarino 		return -EINVAL;
1640db2a4165SFrank Mandarino 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
1641db2a4165SFrank Mandarino 	mask = (bitmask - 1) << e->shift_l;
1642db2a4165SFrank Mandarino 	if (e->shift_l != e->shift_r) {
1643db2a4165SFrank Mandarino 		if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1644db2a4165SFrank Mandarino 			return -EINVAL;
1645db2a4165SFrank Mandarino 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1646db2a4165SFrank Mandarino 		mask |= (bitmask - 1) << e->shift_r;
1647db2a4165SFrank Mandarino 	}
1648db2a4165SFrank Mandarino 
1649db2a4165SFrank Mandarino 	return snd_soc_update_bits(codec, e->reg, mask, val);
1650db2a4165SFrank Mandarino }
1651db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1652db2a4165SFrank Mandarino 
1653db2a4165SFrank Mandarino /**
1654db2a4165SFrank Mandarino  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1655db2a4165SFrank Mandarino  * @kcontrol: mixer control
1656db2a4165SFrank Mandarino  * @uinfo: control element information
1657db2a4165SFrank Mandarino  *
1658db2a4165SFrank Mandarino  * Callback to provide information about an external enumerated
1659db2a4165SFrank Mandarino  * single mixer.
1660db2a4165SFrank Mandarino  *
1661db2a4165SFrank Mandarino  * Returns 0 for success.
1662db2a4165SFrank Mandarino  */
1663db2a4165SFrank Mandarino int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1664db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1665db2a4165SFrank Mandarino {
1666db2a4165SFrank Mandarino 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1667db2a4165SFrank Mandarino 
1668db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1669db2a4165SFrank Mandarino 	uinfo->count = 1;
1670db2a4165SFrank Mandarino 	uinfo->value.enumerated.items = e->mask;
1671db2a4165SFrank Mandarino 
1672db2a4165SFrank Mandarino 	if (uinfo->value.enumerated.item > e->mask - 1)
1673db2a4165SFrank Mandarino 		uinfo->value.enumerated.item = e->mask - 1;
1674db2a4165SFrank Mandarino 	strcpy(uinfo->value.enumerated.name,
1675db2a4165SFrank Mandarino 		e->texts[uinfo->value.enumerated.item]);
1676db2a4165SFrank Mandarino 	return 0;
1677db2a4165SFrank Mandarino }
1678db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1679db2a4165SFrank Mandarino 
1680db2a4165SFrank Mandarino /**
1681db2a4165SFrank Mandarino  * snd_soc_info_volsw_ext - external single mixer info callback
1682db2a4165SFrank Mandarino  * @kcontrol: mixer control
1683db2a4165SFrank Mandarino  * @uinfo: control element information
1684db2a4165SFrank Mandarino  *
1685db2a4165SFrank Mandarino  * Callback to provide information about a single external mixer control.
1686db2a4165SFrank Mandarino  *
1687db2a4165SFrank Mandarino  * Returns 0 for success.
1688db2a4165SFrank Mandarino  */
1689db2a4165SFrank Mandarino int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1690db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1691db2a4165SFrank Mandarino {
1692db2a4165SFrank Mandarino 	int mask = kcontrol->private_value;
1693db2a4165SFrank Mandarino 
1694db2a4165SFrank Mandarino 	uinfo->type =
1695db2a4165SFrank Mandarino 		mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1696db2a4165SFrank Mandarino 	uinfo->count = 1;
1697db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1698db2a4165SFrank Mandarino 	uinfo->value.integer.max = mask;
1699db2a4165SFrank Mandarino 	return 0;
1700db2a4165SFrank Mandarino }
1701db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1702db2a4165SFrank Mandarino 
1703db2a4165SFrank Mandarino /**
1704db2a4165SFrank Mandarino  * snd_soc_info_bool_ext - external single boolean mixer info callback
1705db2a4165SFrank Mandarino  * @kcontrol: mixer control
1706db2a4165SFrank Mandarino  * @uinfo: control element information
1707db2a4165SFrank Mandarino  *
1708db2a4165SFrank Mandarino  * Callback to provide information about a single boolean external mixer control.
1709db2a4165SFrank Mandarino  *
1710db2a4165SFrank Mandarino  * Returns 0 for success.
1711db2a4165SFrank Mandarino  */
1712db2a4165SFrank Mandarino int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
1713db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1714db2a4165SFrank Mandarino {
1715db2a4165SFrank Mandarino 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1716db2a4165SFrank Mandarino 	uinfo->count = 1;
1717db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1718db2a4165SFrank Mandarino 	uinfo->value.integer.max = 1;
1719db2a4165SFrank Mandarino 	return 0;
1720db2a4165SFrank Mandarino }
1721db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
1722db2a4165SFrank Mandarino 
1723db2a4165SFrank Mandarino /**
1724db2a4165SFrank Mandarino  * snd_soc_info_volsw - single mixer info callback
1725db2a4165SFrank Mandarino  * @kcontrol: mixer control
1726db2a4165SFrank Mandarino  * @uinfo: control element information
1727db2a4165SFrank Mandarino  *
1728db2a4165SFrank Mandarino  * Callback to provide information about a single mixer control.
1729db2a4165SFrank Mandarino  *
1730db2a4165SFrank Mandarino  * Returns 0 for success.
1731db2a4165SFrank Mandarino  */
1732db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1733db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1734db2a4165SFrank Mandarino {
1735db2a4165SFrank Mandarino 	int mask = (kcontrol->private_value >> 16) & 0xff;
1736db2a4165SFrank Mandarino 	int shift = (kcontrol->private_value >> 8) & 0x0f;
1737db2a4165SFrank Mandarino 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
1738db2a4165SFrank Mandarino 
1739db2a4165SFrank Mandarino 	uinfo->type =
1740db2a4165SFrank Mandarino 		mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1741db2a4165SFrank Mandarino 	uinfo->count = shift == rshift ? 1 : 2;
1742db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1743db2a4165SFrank Mandarino 	uinfo->value.integer.max = mask;
1744db2a4165SFrank Mandarino 	return 0;
1745db2a4165SFrank Mandarino }
1746db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1747db2a4165SFrank Mandarino 
1748db2a4165SFrank Mandarino /**
1749db2a4165SFrank Mandarino  * snd_soc_get_volsw - single mixer get callback
1750db2a4165SFrank Mandarino  * @kcontrol: mixer control
1751db2a4165SFrank Mandarino  * @uinfo: control element information
1752db2a4165SFrank Mandarino  *
1753db2a4165SFrank Mandarino  * Callback to get the value of a single mixer control.
1754db2a4165SFrank Mandarino  *
1755db2a4165SFrank Mandarino  * Returns 0 for success.
1756db2a4165SFrank Mandarino  */
1757db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1758db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1759db2a4165SFrank Mandarino {
1760db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1761db2a4165SFrank Mandarino 	int reg = kcontrol->private_value & 0xff;
1762db2a4165SFrank Mandarino 	int shift = (kcontrol->private_value >> 8) & 0x0f;
1763db2a4165SFrank Mandarino 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
1764db2a4165SFrank Mandarino 	int mask = (kcontrol->private_value >> 16) & 0xff;
1765db2a4165SFrank Mandarino 	int invert = (kcontrol->private_value >> 24) & 0x01;
1766db2a4165SFrank Mandarino 
1767db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1768db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1769db2a4165SFrank Mandarino 	if (shift != rshift)
1770db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1771db2a4165SFrank Mandarino 			(snd_soc_read(codec, reg) >> rshift) & mask;
1772db2a4165SFrank Mandarino 	if (invert) {
1773db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1774db2a4165SFrank Mandarino 			mask - ucontrol->value.integer.value[0];
1775db2a4165SFrank Mandarino 		if (shift != rshift)
1776db2a4165SFrank Mandarino 			ucontrol->value.integer.value[1] =
1777db2a4165SFrank Mandarino 				mask - ucontrol->value.integer.value[1];
1778db2a4165SFrank Mandarino 	}
1779db2a4165SFrank Mandarino 
1780db2a4165SFrank Mandarino 	return 0;
1781db2a4165SFrank Mandarino }
1782db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1783db2a4165SFrank Mandarino 
1784db2a4165SFrank Mandarino /**
1785db2a4165SFrank Mandarino  * snd_soc_put_volsw - single mixer put callback
1786db2a4165SFrank Mandarino  * @kcontrol: mixer control
1787db2a4165SFrank Mandarino  * @uinfo: control element information
1788db2a4165SFrank Mandarino  *
1789db2a4165SFrank Mandarino  * Callback to set the value of a single mixer control.
1790db2a4165SFrank Mandarino  *
1791db2a4165SFrank Mandarino  * Returns 0 for success.
1792db2a4165SFrank Mandarino  */
1793db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1794db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1795db2a4165SFrank Mandarino {
1796db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1797db2a4165SFrank Mandarino 	int reg = kcontrol->private_value & 0xff;
1798db2a4165SFrank Mandarino 	int shift = (kcontrol->private_value >> 8) & 0x0f;
1799db2a4165SFrank Mandarino 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
1800db2a4165SFrank Mandarino 	int mask = (kcontrol->private_value >> 16) & 0xff;
1801db2a4165SFrank Mandarino 	int invert = (kcontrol->private_value >> 24) & 0x01;
1802db2a4165SFrank Mandarino 	int err;
1803db2a4165SFrank Mandarino 	unsigned short val, val2, val_mask;
1804db2a4165SFrank Mandarino 
1805db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
1806db2a4165SFrank Mandarino 	if (invert)
1807db2a4165SFrank Mandarino 		val = mask - val;
1808db2a4165SFrank Mandarino 	val_mask = mask << shift;
1809db2a4165SFrank Mandarino 	val = val << shift;
1810db2a4165SFrank Mandarino 	if (shift != rshift) {
1811db2a4165SFrank Mandarino 		val2 = (ucontrol->value.integer.value[1] & mask);
1812db2a4165SFrank Mandarino 		if (invert)
1813db2a4165SFrank Mandarino 			val2 = mask - val2;
1814db2a4165SFrank Mandarino 		val_mask |= mask << rshift;
1815db2a4165SFrank Mandarino 		val |= val2 << rshift;
1816db2a4165SFrank Mandarino 	}
1817db2a4165SFrank Mandarino 	err = snd_soc_update_bits(codec, reg, val_mask, val);
1818db2a4165SFrank Mandarino 	return err;
1819db2a4165SFrank Mandarino }
1820db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1821db2a4165SFrank Mandarino 
1822db2a4165SFrank Mandarino /**
1823db2a4165SFrank Mandarino  * snd_soc_info_volsw_2r - double mixer info callback
1824db2a4165SFrank Mandarino  * @kcontrol: mixer control
1825db2a4165SFrank Mandarino  * @uinfo: control element information
1826db2a4165SFrank Mandarino  *
1827db2a4165SFrank Mandarino  * Callback to provide information about a double mixer control that
1828db2a4165SFrank Mandarino  * spans 2 codec registers.
1829db2a4165SFrank Mandarino  *
1830db2a4165SFrank Mandarino  * Returns 0 for success.
1831db2a4165SFrank Mandarino  */
1832db2a4165SFrank Mandarino int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1833db2a4165SFrank Mandarino 	struct snd_ctl_elem_info *uinfo)
1834db2a4165SFrank Mandarino {
1835db2a4165SFrank Mandarino 	int mask = (kcontrol->private_value >> 12) & 0xff;
1836db2a4165SFrank Mandarino 
1837db2a4165SFrank Mandarino 	uinfo->type =
1838db2a4165SFrank Mandarino 		mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1839db2a4165SFrank Mandarino 	uinfo->count = 2;
1840db2a4165SFrank Mandarino 	uinfo->value.integer.min = 0;
1841db2a4165SFrank Mandarino 	uinfo->value.integer.max = mask;
1842db2a4165SFrank Mandarino 	return 0;
1843db2a4165SFrank Mandarino }
1844db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1845db2a4165SFrank Mandarino 
1846db2a4165SFrank Mandarino /**
1847db2a4165SFrank Mandarino  * snd_soc_get_volsw_2r - double mixer get callback
1848db2a4165SFrank Mandarino  * @kcontrol: mixer control
1849db2a4165SFrank Mandarino  * @uinfo: control element information
1850db2a4165SFrank Mandarino  *
1851db2a4165SFrank Mandarino  * Callback to get the value of a double mixer control that spans 2 registers.
1852db2a4165SFrank Mandarino  *
1853db2a4165SFrank Mandarino  * Returns 0 for success.
1854db2a4165SFrank Mandarino  */
1855db2a4165SFrank Mandarino int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1856db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1857db2a4165SFrank Mandarino {
1858db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1859db2a4165SFrank Mandarino 	int reg = kcontrol->private_value & 0xff;
1860db2a4165SFrank Mandarino 	int reg2 = (kcontrol->private_value >> 24) & 0xff;
1861db2a4165SFrank Mandarino 	int shift = (kcontrol->private_value >> 8) & 0x0f;
1862db2a4165SFrank Mandarino 	int mask = (kcontrol->private_value >> 12) & 0xff;
1863db2a4165SFrank Mandarino 	int invert = (kcontrol->private_value >> 20) & 0x01;
1864db2a4165SFrank Mandarino 
1865db2a4165SFrank Mandarino 	ucontrol->value.integer.value[0] =
1866db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg) >> shift) & mask;
1867db2a4165SFrank Mandarino 	ucontrol->value.integer.value[1] =
1868db2a4165SFrank Mandarino 		(snd_soc_read(codec, reg2) >> shift) & mask;
1869db2a4165SFrank Mandarino 	if (invert) {
1870db2a4165SFrank Mandarino 		ucontrol->value.integer.value[0] =
1871db2a4165SFrank Mandarino 			mask - ucontrol->value.integer.value[0];
1872db2a4165SFrank Mandarino 		ucontrol->value.integer.value[1] =
1873db2a4165SFrank Mandarino 			mask - ucontrol->value.integer.value[1];
1874db2a4165SFrank Mandarino 	}
1875db2a4165SFrank Mandarino 
1876db2a4165SFrank Mandarino 	return 0;
1877db2a4165SFrank Mandarino }
1878db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1879db2a4165SFrank Mandarino 
1880db2a4165SFrank Mandarino /**
1881db2a4165SFrank Mandarino  * snd_soc_put_volsw_2r - double mixer set callback
1882db2a4165SFrank Mandarino  * @kcontrol: mixer control
1883db2a4165SFrank Mandarino  * @uinfo: control element information
1884db2a4165SFrank Mandarino  *
1885db2a4165SFrank Mandarino  * Callback to set the value of a double mixer control that spans 2 registers.
1886db2a4165SFrank Mandarino  *
1887db2a4165SFrank Mandarino  * Returns 0 for success.
1888db2a4165SFrank Mandarino  */
1889db2a4165SFrank Mandarino int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1890db2a4165SFrank Mandarino 	struct snd_ctl_elem_value *ucontrol)
1891db2a4165SFrank Mandarino {
1892db2a4165SFrank Mandarino 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1893db2a4165SFrank Mandarino 	int reg = kcontrol->private_value & 0xff;
1894db2a4165SFrank Mandarino 	int reg2 = (kcontrol->private_value >> 24) & 0xff;
1895db2a4165SFrank Mandarino 	int shift = (kcontrol->private_value >> 8) & 0x0f;
1896db2a4165SFrank Mandarino 	int mask = (kcontrol->private_value >> 12) & 0xff;
1897db2a4165SFrank Mandarino 	int invert = (kcontrol->private_value >> 20) & 0x01;
1898db2a4165SFrank Mandarino 	int err;
1899db2a4165SFrank Mandarino 	unsigned short val, val2, val_mask;
1900db2a4165SFrank Mandarino 
1901db2a4165SFrank Mandarino 	val_mask = mask << shift;
1902db2a4165SFrank Mandarino 	val = (ucontrol->value.integer.value[0] & mask);
1903db2a4165SFrank Mandarino 	val2 = (ucontrol->value.integer.value[1] & mask);
1904db2a4165SFrank Mandarino 
1905db2a4165SFrank Mandarino 	if (invert) {
1906db2a4165SFrank Mandarino 		val = mask - val;
1907db2a4165SFrank Mandarino 		val2 = mask - val2;
1908db2a4165SFrank Mandarino 	}
1909db2a4165SFrank Mandarino 
1910db2a4165SFrank Mandarino 	val = val << shift;
1911db2a4165SFrank Mandarino 	val2 = val2 << shift;
1912db2a4165SFrank Mandarino 
1913db2a4165SFrank Mandarino 	if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
1914db2a4165SFrank Mandarino 		return err;
1915db2a4165SFrank Mandarino 
1916db2a4165SFrank Mandarino 	err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1917db2a4165SFrank Mandarino 	return err;
1918db2a4165SFrank Mandarino }
1919db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1920db2a4165SFrank Mandarino 
1921db2a4165SFrank Mandarino static int __devinit snd_soc_init(void)
1922db2a4165SFrank Mandarino {
1923db2a4165SFrank Mandarino 	printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1924db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
1925db2a4165SFrank Mandarino }
1926db2a4165SFrank Mandarino 
1927db2a4165SFrank Mandarino static void snd_soc_exit(void)
1928db2a4165SFrank Mandarino {
1929db2a4165SFrank Mandarino  	platform_driver_unregister(&soc_driver);
1930db2a4165SFrank Mandarino }
1931db2a4165SFrank Mandarino 
1932db2a4165SFrank Mandarino module_init(snd_soc_init);
1933db2a4165SFrank Mandarino module_exit(snd_soc_exit);
1934db2a4165SFrank Mandarino 
1935db2a4165SFrank Mandarino /* Module information */
1936db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1937db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
1938db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
1939