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