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