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