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