]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/soc/soc-core.c
[ALSA] Remove sound/driver.h
[linux-2.6-omap-h63xx.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/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dapm.h>
40 #include <sound/initval.h>
41
42 /* debug */
43 #define SOC_DEBUG 0
44 #if SOC_DEBUG
45 #define dbg(format, arg...) printk(format, ## arg)
46 #else
47 #define dbg(format, arg...)
48 #endif
49
50 static DEFINE_MUTEX(pcm_mutex);
51 static DEFINE_MUTEX(io_mutex);
52 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
53
54 /*
55  * This is a timeout to do a DAPM powerdown after a stream is closed().
56  * It can be used to eliminate pops between different playback streams, e.g.
57  * between two audio tracks.
58  */
59 static int pmdown_time = 5000;
60 module_param(pmdown_time, int, 0);
61 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
62
63 /*
64  * This function forces any delayed work to be queued and run.
65  */
66 static int run_delayed_work(struct delayed_work *dwork)
67 {
68         int ret;
69
70         /* cancel any work waiting to be queued. */
71         ret = cancel_delayed_work(dwork);
72
73         /* if there was any work waiting then we run it now and
74          * wait for it's completion */
75         if (ret) {
76                 schedule_delayed_work(dwork, 0);
77                 flush_scheduled_work();
78         }
79         return ret;
80 }
81
82 #ifdef CONFIG_SND_SOC_AC97_BUS
83 /* unregister ac97 codec */
84 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
85 {
86         if (codec->ac97->dev.bus)
87                 device_unregister(&codec->ac97->dev);
88         return 0;
89 }
90
91 /* stop no dev release warning */
92 static void soc_ac97_device_release(struct device *dev){}
93
94 /* register ac97 codec to bus */
95 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
96 {
97         int err;
98
99         codec->ac97->dev.bus = &ac97_bus_type;
100         codec->ac97->dev.parent = NULL;
101         codec->ac97->dev.release = soc_ac97_device_release;
102
103         snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
104                  codec->card->number, 0, codec->name);
105         err = device_register(&codec->ac97->dev);
106         if (err < 0) {
107                 snd_printk(KERN_ERR "Can't register ac97 bus\n");
108                 codec->ac97->dev.bus = NULL;
109                 return err;
110         }
111         return 0;
112 }
113 #endif
114
115 static inline const char* get_dai_name(int type)
116 {
117         switch(type) {
118         case SND_SOC_DAI_AC97_BUS:
119         case SND_SOC_DAI_AC97:
120                 return "AC97";
121         case SND_SOC_DAI_I2S:
122                 return "I2S";
123         case SND_SOC_DAI_PCM:
124                 return "PCM";
125         }
126         return NULL;
127 }
128
129 /*
130  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
131  * then initialized and any private data can be allocated. This also calls
132  * startup for the cpu DAI, platform, machine and codec DAI.
133  */
134 static int soc_pcm_open(struct snd_pcm_substream *substream)
135 {
136         struct snd_soc_pcm_runtime *rtd = substream->private_data;
137         struct snd_soc_device *socdev = rtd->socdev;
138         struct snd_pcm_runtime *runtime = substream->runtime;
139         struct snd_soc_dai_link *machine = rtd->dai;
140         struct snd_soc_platform *platform = socdev->platform;
141         struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
142         struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
143         int ret = 0;
144
145         mutex_lock(&pcm_mutex);
146
147         /* startup the audio subsystem */
148         if (cpu_dai->ops.startup) {
149                 ret = cpu_dai->ops.startup(substream);
150                 if (ret < 0) {
151                         printk(KERN_ERR "asoc: can't open interface %s\n",
152                                 cpu_dai->name);
153                         goto out;
154                 }
155         }
156
157         if (platform->pcm_ops->open) {
158                 ret = platform->pcm_ops->open(substream);
159                 if (ret < 0) {
160                         printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
161                         goto platform_err;
162                 }
163         }
164
165         if (codec_dai->ops.startup) {
166                 ret = codec_dai->ops.startup(substream);
167                 if (ret < 0) {
168                         printk(KERN_ERR "asoc: can't open codec %s\n",
169                                 codec_dai->name);
170                         goto codec_dai_err;
171                 }
172         }
173
174         if (machine->ops && machine->ops->startup) {
175                 ret = machine->ops->startup(substream);
176                 if (ret < 0) {
177                         printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
178                         goto machine_err;
179                 }
180         }
181
182         /* Check that the codec and cpu DAI's are compatible */
183         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
184                 runtime->hw.rate_min =
185                         max(codec_dai->playback.rate_min, cpu_dai->playback.rate_min);
186                 runtime->hw.rate_max =
187                         min(codec_dai->playback.rate_max, cpu_dai->playback.rate_max);
188                 runtime->hw.channels_min =
189                         max(codec_dai->playback.channels_min,
190                                 cpu_dai->playback.channels_min);
191                 runtime->hw.channels_max =
192                         min(codec_dai->playback.channels_max,
193                                 cpu_dai->playback.channels_max);
194                 runtime->hw.formats =
195                         codec_dai->playback.formats & cpu_dai->playback.formats;
196                 runtime->hw.rates =
197                         codec_dai->playback.rates & cpu_dai->playback.rates;
198         } else {
199                 runtime->hw.rate_min =
200                         max(codec_dai->capture.rate_min, cpu_dai->capture.rate_min);
201                 runtime->hw.rate_max =
202                         min(codec_dai->capture.rate_max, cpu_dai->capture.rate_max);
203                 runtime->hw.channels_min =
204                         max(codec_dai->capture.channels_min,
205                                 cpu_dai->capture.channels_min);
206                 runtime->hw.channels_max =
207                         min(codec_dai->capture.channels_max,
208                                 cpu_dai->capture.channels_max);
209                 runtime->hw.formats =
210                         codec_dai->capture.formats & cpu_dai->capture.formats;
211                 runtime->hw.rates =
212                         codec_dai->capture.rates & cpu_dai->capture.rates;
213         }
214
215         snd_pcm_limit_hw_rates(runtime);
216         if (!runtime->hw.rates) {
217                 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
218                         codec_dai->name, cpu_dai->name);
219                 goto machine_err;
220         }
221         if (!runtime->hw.formats) {
222                 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
223                         codec_dai->name, cpu_dai->name);
224                 goto machine_err;
225         }
226         if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
227                 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
228                         codec_dai->name, cpu_dai->name);
229                 goto machine_err;
230         }
231
232         dbg("asoc: %s <-> %s info:\n",codec_dai->name, cpu_dai->name);
233         dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
234         dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
235                 runtime->hw.channels_max);
236         dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
237                 runtime->hw.rate_max);
238
239         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
240                 cpu_dai->playback.active = codec_dai->playback.active = 1;
241         else
242                 cpu_dai->capture.active = codec_dai->capture.active = 1;
243         cpu_dai->active = codec_dai->active = 1;
244         cpu_dai->runtime = runtime;
245         socdev->codec->active++;
246         mutex_unlock(&pcm_mutex);
247         return 0;
248
249 machine_err:
250         if (machine->ops && machine->ops->shutdown)
251                 machine->ops->shutdown(substream);
252
253 codec_dai_err:
254         if (platform->pcm_ops->close)
255                 platform->pcm_ops->close(substream);
256
257 platform_err:
258         if (cpu_dai->ops.shutdown)
259                 cpu_dai->ops.shutdown(substream);
260 out:
261         mutex_unlock(&pcm_mutex);
262         return ret;
263 }
264
265 /*
266  * Power down the audio subsystem pmdown_time msecs after close is called.
267  * This is to ensure there are no pops or clicks in between any music tracks
268  * due to DAPM power cycling.
269  */
270 static void close_delayed_work(struct work_struct *work)
271 {
272         struct snd_soc_device *socdev =
273                 container_of(work, struct snd_soc_device, delayed_work.work);
274         struct snd_soc_codec *codec = socdev->codec;
275         struct snd_soc_codec_dai *codec_dai;
276         int i;
277
278         mutex_lock(&pcm_mutex);
279         for(i = 0; i < codec->num_dai; i++) {
280                 codec_dai = &codec->dai[i];
281
282                 dbg("pop wq checking: %s status: %s waiting: %s\n",
283                         codec_dai->playback.stream_name,
284                         codec_dai->playback.active ? "active" : "inactive",
285                         codec_dai->pop_wait ? "yes" : "no");
286
287                 /* are we waiting on this codec DAI stream */
288                 if (codec_dai->pop_wait == 1) {
289
290                         codec_dai->pop_wait = 0;
291                         snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
292                                 SND_SOC_DAPM_STREAM_STOP);
293
294                         /* power down the codec power domain if no longer active */
295                         if (codec->active == 0) {
296                                 dbg("pop wq D3 %s %s\n", codec->name,
297                                         codec_dai->playback.stream_name);
298                                 if (codec->dapm_event)
299                                         codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
300                         }
301                 }
302         }
303         mutex_unlock(&pcm_mutex);
304 }
305
306 /*
307  * Called by ALSA when a PCM substream is closed. Private data can be
308  * freed here. The cpu DAI, codec DAI, machine and platform are also
309  * shutdown.
310  */
311 static int soc_codec_close(struct snd_pcm_substream *substream)
312 {
313         struct snd_soc_pcm_runtime *rtd = substream->private_data;
314         struct snd_soc_device *socdev = rtd->socdev;
315         struct snd_soc_dai_link *machine = rtd->dai;
316         struct snd_soc_platform *platform = socdev->platform;
317         struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
318         struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
319         struct snd_soc_codec *codec = socdev->codec;
320
321         mutex_lock(&pcm_mutex);
322
323         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
324                 cpu_dai->playback.active = codec_dai->playback.active = 0;
325         else
326                 cpu_dai->capture.active = codec_dai->capture.active = 0;
327
328         if (codec_dai->playback.active == 0 &&
329                 codec_dai->capture.active == 0) {
330                 cpu_dai->active = codec_dai->active = 0;
331         }
332         codec->active--;
333
334         if (cpu_dai->ops.shutdown)
335                 cpu_dai->ops.shutdown(substream);
336
337         if (codec_dai->ops.shutdown)
338                 codec_dai->ops.shutdown(substream);
339
340         if (machine->ops && machine->ops->shutdown)
341                 machine->ops->shutdown(substream);
342
343         if (platform->pcm_ops->close)
344                 platform->pcm_ops->close(substream);
345         cpu_dai->runtime = NULL;
346
347         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
348                 /* start delayed pop wq here for playback streams */
349                 codec_dai->pop_wait = 1;
350                 schedule_delayed_work(&socdev->delayed_work,
351                         msecs_to_jiffies(pmdown_time));
352         } else {
353                 /* capture streams can be powered down now */
354                 snd_soc_dapm_stream_event(codec,
355                         codec_dai->capture.stream_name, SND_SOC_DAPM_STREAM_STOP);
356
357                 if (codec->active == 0 && codec_dai->pop_wait == 0){
358                         if (codec->dapm_event)
359                                 codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
360                 }
361         }
362
363         mutex_unlock(&pcm_mutex);
364         return 0;
365 }
366
367 /*
368  * Called by ALSA when the PCM substream is prepared, can set format, sample
369  * rate, etc.  This function is non atomic and can be called multiple times,
370  * it can refer to the runtime info.
371  */
372 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
373 {
374         struct snd_soc_pcm_runtime *rtd = substream->private_data;
375         struct snd_soc_device *socdev = rtd->socdev;
376         struct snd_soc_dai_link *machine = rtd->dai;
377         struct snd_soc_platform *platform = socdev->platform;
378         struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
379         struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
380         struct snd_soc_codec *codec = socdev->codec;
381         int ret = 0;
382
383         mutex_lock(&pcm_mutex);
384
385         if (machine->ops && machine->ops->prepare) {
386                 ret = machine->ops->prepare(substream);
387                 if (ret < 0) {
388                         printk(KERN_ERR "asoc: machine prepare error\n");
389                         goto out;
390                 }
391         }
392
393         if (platform->pcm_ops->prepare) {
394                 ret = platform->pcm_ops->prepare(substream);
395                 if (ret < 0) {
396                         printk(KERN_ERR "asoc: platform prepare error\n");
397                         goto out;
398                 }
399         }
400
401         if (codec_dai->ops.prepare) {
402                 ret = codec_dai->ops.prepare(substream);
403                 if (ret < 0) {
404                         printk(KERN_ERR "asoc: codec DAI prepare error\n");
405                         goto out;
406                 }
407         }
408
409         if (cpu_dai->ops.prepare) {
410                 ret = cpu_dai->ops.prepare(substream);
411                 if (ret < 0) {
412                         printk(KERN_ERR "asoc: cpu DAI prepare error\n");
413                         goto out;
414                 }
415         }
416
417         /* we only want to start a DAPM playback stream if we are not waiting
418          * on an existing one stopping */
419         if (codec_dai->pop_wait) {
420                 /* we are waiting for the delayed work to start */
421                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
422                                 snd_soc_dapm_stream_event(socdev->codec,
423                                         codec_dai->capture.stream_name,
424                                         SND_SOC_DAPM_STREAM_START);
425                 else {
426                         codec_dai->pop_wait = 0;
427                         cancel_delayed_work(&socdev->delayed_work);
428                         if (codec_dai->dai_ops.digital_mute)
429                                 codec_dai->dai_ops.digital_mute(codec_dai, 0);
430                 }
431         } else {
432                 /* no delayed work - do we need to power up codec */
433                 if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
434
435                         if (codec->dapm_event)
436                                 codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
437
438                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
439                                 snd_soc_dapm_stream_event(codec,
440                                         codec_dai->playback.stream_name,
441                                         SND_SOC_DAPM_STREAM_START);
442                         else
443                                 snd_soc_dapm_stream_event(codec,
444                                         codec_dai->capture.stream_name,
445                                         SND_SOC_DAPM_STREAM_START);
446
447                         if (codec->dapm_event)
448                                 codec->dapm_event(codec, SNDRV_CTL_POWER_D0);
449                         if (codec_dai->dai_ops.digital_mute)
450                                 codec_dai->dai_ops.digital_mute(codec_dai, 0);
451
452                 } else {
453                         /* codec already powered - power on widgets */
454                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
455                                 snd_soc_dapm_stream_event(codec,
456                                         codec_dai->playback.stream_name,
457                                         SND_SOC_DAPM_STREAM_START);
458                         else
459                                 snd_soc_dapm_stream_event(codec,
460                                         codec_dai->capture.stream_name,
461                                         SND_SOC_DAPM_STREAM_START);
462                         if (codec_dai->dai_ops.digital_mute)
463                                 codec_dai->dai_ops.digital_mute(codec_dai, 0);
464                 }
465         }
466
467 out:
468         mutex_unlock(&pcm_mutex);
469         return ret;
470 }
471
472 /*
473  * Called by ALSA when the hardware params are set by application. This
474  * function can also be called multiple times and can allocate buffers
475  * (using snd_pcm_lib_* ). It's non-atomic.
476  */
477 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
478                                 struct snd_pcm_hw_params *params)
479 {
480         struct snd_soc_pcm_runtime *rtd = substream->private_data;
481         struct snd_soc_device *socdev = rtd->socdev;
482         struct snd_soc_dai_link *machine = rtd->dai;
483         struct snd_soc_platform *platform = socdev->platform;
484         struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
485         struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
486         int ret = 0;
487
488         mutex_lock(&pcm_mutex);
489
490         if (machine->ops && machine->ops->hw_params) {
491                 ret = machine->ops->hw_params(substream, params);
492                 if (ret < 0) {
493                         printk(KERN_ERR "asoc: machine hw_params failed\n");
494                         goto out;
495                 }
496         }
497
498         if (codec_dai->ops.hw_params) {
499                 ret = codec_dai->ops.hw_params(substream, params);
500                 if (ret < 0) {
501                         printk(KERN_ERR "asoc: can't set codec %s hw params\n",
502                                 codec_dai->name);
503                         goto codec_err;
504                 }
505         }
506
507         if (cpu_dai->ops.hw_params) {
508                 ret = cpu_dai->ops.hw_params(substream, params);
509                 if (ret < 0) {
510                         printk(KERN_ERR "asoc: can't set interface %s hw params\n",
511                                 cpu_dai->name);
512                         goto interface_err;
513                 }
514         }
515
516         if (platform->pcm_ops->hw_params) {
517                 ret = platform->pcm_ops->hw_params(substream, params);
518                 if (ret < 0) {
519                         printk(KERN_ERR "asoc: can't set platform %s hw params\n",
520                                 platform->name);
521                         goto platform_err;
522                 }
523         }
524
525 out:
526         mutex_unlock(&pcm_mutex);
527         return ret;
528
529 platform_err:
530         if (cpu_dai->ops.hw_free)
531                 cpu_dai->ops.hw_free(substream);
532
533 interface_err:
534         if (codec_dai->ops.hw_free)
535                 codec_dai->ops.hw_free(substream);
536
537 codec_err:
538         if(machine->ops && machine->ops->hw_free)
539                 machine->ops->hw_free(substream);
540
541         mutex_unlock(&pcm_mutex);
542         return ret;
543 }
544
545 /*
546  * Free's resources allocated by hw_params, can be called multiple times
547  */
548 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
549 {
550         struct snd_soc_pcm_runtime *rtd = substream->private_data;
551         struct snd_soc_device *socdev = rtd->socdev;
552         struct snd_soc_dai_link *machine = rtd->dai;
553         struct snd_soc_platform *platform = socdev->platform;
554         struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
555         struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
556         struct snd_soc_codec *codec = socdev->codec;
557
558         mutex_lock(&pcm_mutex);
559
560         /* apply codec digital mute */
561         if (!codec->active && codec_dai->dai_ops.digital_mute)
562                 codec_dai->dai_ops.digital_mute(codec_dai, 1);
563
564         /* free any machine hw params */
565         if (machine->ops && machine->ops->hw_free)
566                 machine->ops->hw_free(substream);
567
568         /* free any DMA resources */
569         if (platform->pcm_ops->hw_free)
570                 platform->pcm_ops->hw_free(substream);
571
572         /* now free hw params for the DAI's  */
573         if (codec_dai->ops.hw_free)
574                 codec_dai->ops.hw_free(substream);
575
576         if (cpu_dai->ops.hw_free)
577                 cpu_dai->ops.hw_free(substream);
578
579         mutex_unlock(&pcm_mutex);
580         return 0;
581 }
582
583 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
584 {
585         struct snd_soc_pcm_runtime *rtd = substream->private_data;
586         struct snd_soc_device *socdev = rtd->socdev;
587         struct snd_soc_dai_link *machine = rtd->dai;
588         struct snd_soc_platform *platform = socdev->platform;
589         struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
590         struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
591         int ret;
592
593         if (codec_dai->ops.trigger) {
594                 ret = codec_dai->ops.trigger(substream, cmd);
595                 if (ret < 0)
596                         return ret;
597         }
598
599         if (platform->pcm_ops->trigger) {
600                 ret = platform->pcm_ops->trigger(substream, cmd);
601                 if (ret < 0)
602                         return ret;
603         }
604
605         if (cpu_dai->ops.trigger) {
606                 ret = cpu_dai->ops.trigger(substream, cmd);
607                 if (ret < 0)
608                         return ret;
609         }
610         return 0;
611 }
612
613 /* ASoC PCM operations */
614 static struct snd_pcm_ops soc_pcm_ops = {
615         .open           = soc_pcm_open,
616         .close          = soc_codec_close,
617         .hw_params      = soc_pcm_hw_params,
618         .hw_free        = soc_pcm_hw_free,
619         .prepare        = soc_pcm_prepare,
620         .trigger        = soc_pcm_trigger,
621 };
622
623 #ifdef CONFIG_PM
624 /* powers down audio subsystem for suspend */
625 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
626 {
627         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
628         struct snd_soc_machine *machine = socdev->machine;
629         struct snd_soc_platform *platform = socdev->platform;
630         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
631         struct snd_soc_codec *codec = socdev->codec;
632         int i;
633
634         /* mute any active DAC's */
635         for(i = 0; i < machine->num_links; i++) {
636                 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
637                 if (dai->dai_ops.digital_mute && dai->playback.active)
638                         dai->dai_ops.digital_mute(dai, 1);
639         }
640
641         if (machine->suspend_pre)
642                 machine->suspend_pre(pdev, state);
643
644         for(i = 0; i < machine->num_links; i++) {
645                 struct snd_soc_cpu_dai  *cpu_dai = machine->dai_link[i].cpu_dai;
646                 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
647                         cpu_dai->suspend(pdev, cpu_dai);
648                 if (platform->suspend)
649                         platform->suspend(pdev, cpu_dai);
650         }
651
652         /* close any waiting streams and save state */
653         run_delayed_work(&socdev->delayed_work);
654         codec->suspend_dapm_state = codec->dapm_state;
655
656         for(i = 0; i < codec->num_dai; i++) {
657                 char *stream = codec->dai[i].playback.stream_name;
658                 if (stream != NULL)
659                         snd_soc_dapm_stream_event(codec, stream,
660                                 SND_SOC_DAPM_STREAM_SUSPEND);
661                 stream = codec->dai[i].capture.stream_name;
662                 if (stream != NULL)
663                         snd_soc_dapm_stream_event(codec, stream,
664                                 SND_SOC_DAPM_STREAM_SUSPEND);
665         }
666
667         if (codec_dev->suspend)
668                 codec_dev->suspend(pdev, state);
669
670         for(i = 0; i < machine->num_links; i++) {
671                 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
672                 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
673                         cpu_dai->suspend(pdev, cpu_dai);
674         }
675
676         if (machine->suspend_post)
677                 machine->suspend_post(pdev, state);
678
679         return 0;
680 }
681
682 /* powers up audio subsystem after a suspend */
683 static int soc_resume(struct platform_device *pdev)
684 {
685         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
686         struct snd_soc_machine *machine = socdev->machine;
687         struct snd_soc_platform *platform = socdev->platform;
688         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
689         struct snd_soc_codec *codec = socdev->codec;
690         int i;
691
692         if (machine->resume_pre)
693                 machine->resume_pre(pdev);
694
695         for(i = 0; i < machine->num_links; i++) {
696                 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
697                 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
698                         cpu_dai->resume(pdev, cpu_dai);
699         }
700
701         if (codec_dev->resume)
702                 codec_dev->resume(pdev);
703
704         for(i = 0; i < codec->num_dai; i++) {
705                 char* stream = codec->dai[i].playback.stream_name;
706                 if (stream != NULL)
707                         snd_soc_dapm_stream_event(codec, stream,
708                                 SND_SOC_DAPM_STREAM_RESUME);
709                 stream = codec->dai[i].capture.stream_name;
710                 if (stream != NULL)
711                         snd_soc_dapm_stream_event(codec, stream,
712                                 SND_SOC_DAPM_STREAM_RESUME);
713         }
714
715         /* unmute any active DAC's */
716         for(i = 0; i < machine->num_links; i++) {
717                 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
718                 if (dai->dai_ops.digital_mute && dai->playback.active)
719                         dai->dai_ops.digital_mute(dai, 0);
720         }
721
722         for(i = 0; i < machine->num_links; i++) {
723                 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
724                 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
725                         cpu_dai->resume(pdev, cpu_dai);
726                 if (platform->resume)
727                         platform->resume(pdev, cpu_dai);
728         }
729
730         if (machine->resume_post)
731                 machine->resume_post(pdev);
732
733         return 0;
734 }
735
736 #else
737 #define soc_suspend     NULL
738 #define soc_resume      NULL
739 #endif
740
741 /* probes a new socdev */
742 static int soc_probe(struct platform_device *pdev)
743 {
744         int ret = 0, i;
745         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
746         struct snd_soc_machine *machine = socdev->machine;
747         struct snd_soc_platform *platform = socdev->platform;
748         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
749
750         if (machine->probe) {
751                 ret = machine->probe(pdev);
752                 if(ret < 0)
753                         return ret;
754         }
755
756         for (i = 0; i < machine->num_links; i++) {
757                 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
758                 if (cpu_dai->probe) {
759                         ret = cpu_dai->probe(pdev);
760                         if(ret < 0)
761                                 goto cpu_dai_err;
762                 }
763         }
764
765         if (codec_dev->probe) {
766                 ret = codec_dev->probe(pdev);
767                 if(ret < 0)
768                         goto cpu_dai_err;
769         }
770
771         if (platform->probe) {
772                 ret = platform->probe(pdev);
773                 if(ret < 0)
774                         goto platform_err;
775         }
776
777         /* DAPM stream work */
778         INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
779         return 0;
780
781 platform_err:
782         if (codec_dev->remove)
783                 codec_dev->remove(pdev);
784
785 cpu_dai_err:
786         for (i--; i >= 0; i--) {
787                 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
788                 if (cpu_dai->remove)
789                         cpu_dai->remove(pdev);
790         }
791
792         if (machine->remove)
793                 machine->remove(pdev);
794
795         return ret;
796 }
797
798 /* removes a socdev */
799 static int soc_remove(struct platform_device *pdev)
800 {
801         int i;
802         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
803         struct snd_soc_machine *machine = socdev->machine;
804         struct snd_soc_platform *platform = socdev->platform;
805         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
806
807         run_delayed_work(&socdev->delayed_work);
808
809         if (platform->remove)
810                 platform->remove(pdev);
811
812         if (codec_dev->remove)
813                 codec_dev->remove(pdev);
814
815         for (i = 0; i < machine->num_links; i++) {
816                 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
817                 if (cpu_dai->remove)
818                         cpu_dai->remove(pdev);
819         }
820
821         if (machine->remove)
822                 machine->remove(pdev);
823
824         return 0;
825 }
826
827 /* ASoC platform driver */
828 static struct platform_driver soc_driver = {
829         .driver         = {
830                 .name           = "soc-audio",
831         },
832         .probe          = soc_probe,
833         .remove         = soc_remove,
834         .suspend        = soc_suspend,
835         .resume         = soc_resume,
836 };
837
838 /* create a new pcm */
839 static int soc_new_pcm(struct snd_soc_device *socdev,
840         struct snd_soc_dai_link *dai_link, int num)
841 {
842         struct snd_soc_codec *codec = socdev->codec;
843         struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
844         struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
845         struct snd_soc_pcm_runtime *rtd;
846         struct snd_pcm *pcm;
847         char new_name[64];
848         int ret = 0, playback = 0, capture = 0;
849
850         rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
851         if (rtd == NULL)
852                 return -ENOMEM;
853
854         rtd->dai = dai_link;
855         rtd->socdev = socdev;
856         codec_dai->codec = socdev->codec;
857
858         /* check client and interface hw capabilities */
859         sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
860                 get_dai_name(cpu_dai->type), num);
861
862         if (codec_dai->playback.channels_min)
863                 playback = 1;
864         if (codec_dai->capture.channels_min)
865                 capture = 1;
866
867         ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
868                 capture, &pcm);
869         if (ret < 0) {
870                 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
871                 kfree(rtd);
872                 return ret;
873         }
874
875         pcm->private_data = rtd;
876         soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
877         soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
878         soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
879         soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
880         soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
881         soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
882         soc_pcm_ops.page = socdev->platform->pcm_ops->page;
883
884         if (playback)
885                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
886
887         if (capture)
888                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
889
890         ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
891         if (ret < 0) {
892                 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
893                 kfree(rtd);
894                 return ret;
895         }
896
897         pcm->private_free = socdev->platform->pcm_free;
898         printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
899                 cpu_dai->name);
900         return ret;
901 }
902
903 /* codec register dump */
904 static ssize_t codec_reg_show(struct device *dev,
905         struct device_attribute *attr, char *buf)
906 {
907         struct snd_soc_device *devdata = dev_get_drvdata(dev);
908         struct snd_soc_codec *codec = devdata->codec;
909         int i, step = 1, count = 0;
910
911         if (!codec->reg_cache_size)
912                 return 0;
913
914         if (codec->reg_cache_step)
915                 step = codec->reg_cache_step;
916
917         count += sprintf(buf, "%s registers\n", codec->name);
918         for(i = 0; i < codec->reg_cache_size; i += step)
919                 count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
920
921         return count;
922 }
923 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
924
925 /**
926  * snd_soc_new_ac97_codec - initailise AC97 device
927  * @codec: audio codec
928  * @ops: AC97 bus operations
929  * @num: AC97 codec number
930  *
931  * Initialises AC97 codec resources for use by ad-hoc devices only.
932  */
933 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
934         struct snd_ac97_bus_ops *ops, int num)
935 {
936         mutex_lock(&codec->mutex);
937
938         codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
939         if (codec->ac97 == NULL) {
940                 mutex_unlock(&codec->mutex);
941                 return -ENOMEM;
942         }
943
944         codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
945         if (codec->ac97->bus == NULL) {
946                 kfree(codec->ac97);
947                 codec->ac97 = NULL;
948                 mutex_unlock(&codec->mutex);
949                 return -ENOMEM;
950         }
951
952         codec->ac97->bus->ops = ops;
953         codec->ac97->num = num;
954         mutex_unlock(&codec->mutex);
955         return 0;
956 }
957 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
958
959 /**
960  * snd_soc_free_ac97_codec - free AC97 codec device
961  * @codec: audio codec
962  *
963  * Frees AC97 codec device resources.
964  */
965 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
966 {
967         mutex_lock(&codec->mutex);
968         kfree(codec->ac97->bus);
969         kfree(codec->ac97);
970         codec->ac97 = NULL;
971         mutex_unlock(&codec->mutex);
972 }
973 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
974
975 /**
976  * snd_soc_update_bits - update codec register bits
977  * @codec: audio codec
978  * @reg: codec register
979  * @mask: register mask
980  * @value: new value
981  *
982  * Writes new register value.
983  *
984  * Returns 1 for change else 0.
985  */
986 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
987                                 unsigned short mask, unsigned short value)
988 {
989         int change;
990         unsigned short old, new;
991
992         mutex_lock(&io_mutex);
993         old = snd_soc_read(codec, reg);
994         new = (old & ~mask) | value;
995         change = old != new;
996         if (change)
997                 snd_soc_write(codec, reg, new);
998
999         mutex_unlock(&io_mutex);
1000         return change;
1001 }
1002 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1003
1004 /**
1005  * snd_soc_test_bits - test register for change
1006  * @codec: audio codec
1007  * @reg: codec register
1008  * @mask: register mask
1009  * @value: new value
1010  *
1011  * Tests a register with a new value and checks if the new value is
1012  * different from the old value.
1013  *
1014  * Returns 1 for change else 0.
1015  */
1016 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1017                                 unsigned short mask, unsigned short value)
1018 {
1019         int change;
1020         unsigned short old, new;
1021
1022         mutex_lock(&io_mutex);
1023         old = snd_soc_read(codec, reg);
1024         new = (old & ~mask) | value;
1025         change = old != new;
1026         mutex_unlock(&io_mutex);
1027
1028         return change;
1029 }
1030 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1031
1032 /**
1033  * snd_soc_new_pcms - create new sound card and pcms
1034  * @socdev: the SoC audio device
1035  *
1036  * Create a new sound card based upon the codec and interface pcms.
1037  *
1038  * Returns 0 for success, else error.
1039  */
1040 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1041 {
1042         struct snd_soc_codec *codec = socdev->codec;
1043         struct snd_soc_machine *machine = socdev->machine;
1044         int ret = 0, i;
1045
1046         mutex_lock(&codec->mutex);
1047
1048         /* register a sound card */
1049         codec->card = snd_card_new(idx, xid, codec->owner, 0);
1050         if (!codec->card) {
1051                 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1052                         codec->name);
1053                 mutex_unlock(&codec->mutex);
1054                 return -ENODEV;
1055         }
1056
1057         codec->card->dev = socdev->dev;
1058         codec->card->private_data = codec;
1059         strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1060
1061         /* create the pcms */
1062         for(i = 0; i < machine->num_links; i++) {
1063                 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1064                 if (ret < 0) {
1065                         printk(KERN_ERR "asoc: can't create pcm %s\n",
1066                                 machine->dai_link[i].stream_name);
1067                         mutex_unlock(&codec->mutex);
1068                         return ret;
1069                 }
1070         }
1071
1072         mutex_unlock(&codec->mutex);
1073         return ret;
1074 }
1075 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1076
1077 /**
1078  * snd_soc_register_card - register sound card
1079  * @socdev: the SoC audio device
1080  *
1081  * Register a SoC sound card. Also registers an AC97 device if the
1082  * codec is AC97 for ad hoc devices.
1083  *
1084  * Returns 0 for success, else error.
1085  */
1086 int snd_soc_register_card(struct snd_soc_device *socdev)
1087 {
1088         struct snd_soc_codec *codec = socdev->codec;
1089         struct snd_soc_machine *machine = socdev->machine;
1090         int ret = 0, i, ac97 = 0, err = 0;
1091
1092         mutex_lock(&codec->mutex);
1093         for(i = 0; i < machine->num_links; i++) {
1094                 if (socdev->machine->dai_link[i].init) {
1095                         err = socdev->machine->dai_link[i].init(codec);
1096                         if (err < 0) {
1097                                 printk(KERN_ERR "asoc: failed to init %s\n",
1098                                         socdev->machine->dai_link[i].stream_name);
1099                                 continue;
1100                         }
1101                 }
1102                 if (socdev->machine->dai_link[i].codec_dai->type == 
1103                         SND_SOC_DAI_AC97_BUS)
1104                         ac97 = 1;
1105         }
1106         snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1107                  "%s", machine->name);
1108         snprintf(codec->card->longname, sizeof(codec->card->longname),
1109                  "%s (%s)", machine->name, codec->name);
1110
1111         ret = snd_card_register(codec->card);
1112         if (ret < 0) {
1113                 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1114                                 codec->name);
1115                 goto out;
1116         }
1117
1118 #ifdef CONFIG_SND_SOC_AC97_BUS
1119         if (ac97) {
1120                 ret = soc_ac97_dev_register(codec);
1121                 if (ret < 0) {
1122                         printk(KERN_ERR "asoc: AC97 device register failed\n");
1123                         snd_card_free(codec->card);
1124                         goto out;
1125                 }
1126         }
1127 #endif
1128
1129         err = snd_soc_dapm_sys_add(socdev->dev);
1130         if (err < 0)
1131                 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1132
1133         err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1134         if (err < 0)
1135                 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
1136 out:
1137         mutex_unlock(&codec->mutex);
1138         return ret;
1139 }
1140 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1141
1142 /**
1143  * snd_soc_free_pcms - free sound card and pcms
1144  * @socdev: the SoC audio device
1145  *
1146  * Frees sound card and pcms associated with the socdev.
1147  * Also unregister the codec if it is an AC97 device.
1148  */
1149 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1150 {
1151         struct snd_soc_codec *codec = socdev->codec;
1152 #ifdef CONFIG_SND_SOC_AC97_BUS
1153         struct snd_soc_codec_dai *codec_dai;
1154         int i;
1155 #endif
1156
1157         mutex_lock(&codec->mutex);
1158 #ifdef CONFIG_SND_SOC_AC97_BUS
1159         for(i = 0; i < codec->num_dai; i++) {
1160                 codec_dai = &codec->dai[i];
1161                 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1162                         soc_ac97_dev_unregister(codec);
1163                         goto free_card;
1164                 }
1165         }
1166 free_card:
1167 #endif
1168
1169         if (codec->card)
1170                 snd_card_free(codec->card);
1171         device_remove_file(socdev->dev, &dev_attr_codec_reg);
1172         mutex_unlock(&codec->mutex);
1173 }
1174 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1175
1176 /**
1177  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1178  * @substream: the pcm substream
1179  * @hw: the hardware parameters
1180  *
1181  * Sets the substream runtime hardware parameters.
1182  */
1183 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1184         const struct snd_pcm_hardware *hw)
1185 {
1186         struct snd_pcm_runtime *runtime = substream->runtime;
1187         runtime->hw.info = hw->info;
1188         runtime->hw.formats = hw->formats;
1189         runtime->hw.period_bytes_min = hw->period_bytes_min;
1190         runtime->hw.period_bytes_max = hw->period_bytes_max;
1191         runtime->hw.periods_min = hw->periods_min;
1192         runtime->hw.periods_max = hw->periods_max;
1193         runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1194         runtime->hw.fifo_size = hw->fifo_size;
1195         return 0;
1196 }
1197 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1198
1199 /**
1200  * snd_soc_cnew - create new control
1201  * @_template: control template
1202  * @data: control private data
1203  * @lnng_name: control long name
1204  *
1205  * Create a new mixer control from a template control.
1206  *
1207  * Returns 0 for success, else error.
1208  */
1209 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1210         void *data, char *long_name)
1211 {
1212         struct snd_kcontrol_new template;
1213
1214         memcpy(&template, _template, sizeof(template));
1215         if (long_name)
1216                 template.name = long_name;
1217         template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1218         template.index = 0;
1219
1220         return snd_ctl_new1(&template, data);
1221 }
1222 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1223
1224 /**
1225  * snd_soc_info_enum_double - enumerated double mixer info callback
1226  * @kcontrol: mixer control
1227  * @uinfo: control element information
1228  *
1229  * Callback to provide information about a double enumerated
1230  * mixer control.
1231  *
1232  * Returns 0 for success.
1233  */
1234 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1235         struct snd_ctl_elem_info *uinfo)
1236 {
1237         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1238
1239         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1240         uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1241         uinfo->value.enumerated.items = e->mask;
1242
1243         if (uinfo->value.enumerated.item > e->mask - 1)
1244                 uinfo->value.enumerated.item = e->mask - 1;
1245         strcpy(uinfo->value.enumerated.name,
1246                 e->texts[uinfo->value.enumerated.item]);
1247         return 0;
1248 }
1249 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1250
1251 /**
1252  * snd_soc_get_enum_double - enumerated double mixer get callback
1253  * @kcontrol: mixer control
1254  * @uinfo: control element information
1255  *
1256  * Callback to get the value of a double enumerated mixer.
1257  *
1258  * Returns 0 for success.
1259  */
1260 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1261         struct snd_ctl_elem_value *ucontrol)
1262 {
1263         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1264         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1265         unsigned short val, bitmask;
1266
1267         for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1268                 ;
1269         val = snd_soc_read(codec, e->reg);
1270         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1271         if (e->shift_l != e->shift_r)
1272                 ucontrol->value.enumerated.item[1] =
1273                         (val >> e->shift_r) & (bitmask - 1);
1274
1275         return 0;
1276 }
1277 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1278
1279 /**
1280  * snd_soc_put_enum_double - enumerated double mixer put callback
1281  * @kcontrol: mixer control
1282  * @uinfo: control element information
1283  *
1284  * Callback to set the value of a double enumerated mixer.
1285  *
1286  * Returns 0 for success.
1287  */
1288 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1289         struct snd_ctl_elem_value *ucontrol)
1290 {
1291         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1292         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1293         unsigned short val;
1294         unsigned short mask, bitmask;
1295
1296         for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1297                 ;
1298         if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1299                 return -EINVAL;
1300         val = ucontrol->value.enumerated.item[0] << e->shift_l;
1301         mask = (bitmask - 1) << e->shift_l;
1302         if (e->shift_l != e->shift_r) {
1303                 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1304                         return -EINVAL;
1305                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1306                 mask |= (bitmask - 1) << e->shift_r;
1307         }
1308
1309         return snd_soc_update_bits(codec, e->reg, mask, val);
1310 }
1311 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1312
1313 /**
1314  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1315  * @kcontrol: mixer control
1316  * @uinfo: control element information
1317  *
1318  * Callback to provide information about an external enumerated
1319  * single mixer.
1320  *
1321  * Returns 0 for success.
1322  */
1323 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1324         struct snd_ctl_elem_info *uinfo)
1325 {
1326         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1327
1328         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1329         uinfo->count = 1;
1330         uinfo->value.enumerated.items = e->mask;
1331
1332         if (uinfo->value.enumerated.item > e->mask - 1)
1333                 uinfo->value.enumerated.item = e->mask - 1;
1334         strcpy(uinfo->value.enumerated.name,
1335                 e->texts[uinfo->value.enumerated.item]);
1336         return 0;
1337 }
1338 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1339
1340 /**
1341  * snd_soc_info_volsw_ext - external single mixer info callback
1342  * @kcontrol: mixer control
1343  * @uinfo: control element information
1344  *
1345  * Callback to provide information about a single external mixer control.
1346  *
1347  * Returns 0 for success.
1348  */
1349 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1350         struct snd_ctl_elem_info *uinfo)
1351 {
1352         int mask = kcontrol->private_value;
1353
1354         uinfo->type =
1355                 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1356         uinfo->count = 1;
1357         uinfo->value.integer.min = 0;
1358         uinfo->value.integer.max = mask;
1359         return 0;
1360 }
1361 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1362
1363 /**
1364  * snd_soc_info_volsw - single mixer info callback
1365  * @kcontrol: mixer control
1366  * @uinfo: control element information
1367  *
1368  * Callback to provide information about a single mixer control.
1369  *
1370  * Returns 0 for success.
1371  */
1372 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1373         struct snd_ctl_elem_info *uinfo)
1374 {
1375         int mask = (kcontrol->private_value >> 16) & 0xff;
1376         int shift = (kcontrol->private_value >> 8) & 0x0f;
1377         int rshift = (kcontrol->private_value >> 12) & 0x0f;
1378
1379         uinfo->type =
1380                 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1381         uinfo->count = shift == rshift ? 1 : 2;
1382         uinfo->value.integer.min = 0;
1383         uinfo->value.integer.max = mask;
1384         return 0;
1385 }
1386 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1387
1388 /**
1389  * snd_soc_get_volsw - single mixer get callback
1390  * @kcontrol: mixer control
1391  * @uinfo: control element information
1392  *
1393  * Callback to get the value of a single mixer control.
1394  *
1395  * Returns 0 for success.
1396  */
1397 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1398         struct snd_ctl_elem_value *ucontrol)
1399 {
1400         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1401         int reg = kcontrol->private_value & 0xff;
1402         int shift = (kcontrol->private_value >> 8) & 0x0f;
1403         int rshift = (kcontrol->private_value >> 12) & 0x0f;
1404         int mask = (kcontrol->private_value >> 16) & 0xff;
1405         int invert = (kcontrol->private_value >> 24) & 0x01;
1406
1407         ucontrol->value.integer.value[0] =
1408                 (snd_soc_read(codec, reg) >> shift) & mask;
1409         if (shift != rshift)
1410                 ucontrol->value.integer.value[1] =
1411                         (snd_soc_read(codec, reg) >> rshift) & mask;
1412         if (invert) {
1413                 ucontrol->value.integer.value[0] =
1414                         mask - ucontrol->value.integer.value[0];
1415                 if (shift != rshift)
1416                         ucontrol->value.integer.value[1] =
1417                                 mask - ucontrol->value.integer.value[1];
1418         }
1419
1420         return 0;
1421 }
1422 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1423
1424 /**
1425  * snd_soc_put_volsw - single mixer put callback
1426  * @kcontrol: mixer control
1427  * @uinfo: control element information
1428  *
1429  * Callback to set the value of a single mixer control.
1430  *
1431  * Returns 0 for success.
1432  */
1433 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1434         struct snd_ctl_elem_value *ucontrol)
1435 {
1436         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1437         int reg = kcontrol->private_value & 0xff;
1438         int shift = (kcontrol->private_value >> 8) & 0x0f;
1439         int rshift = (kcontrol->private_value >> 12) & 0x0f;
1440         int mask = (kcontrol->private_value >> 16) & 0xff;
1441         int invert = (kcontrol->private_value >> 24) & 0x01;
1442         int err;
1443         unsigned short val, val2, val_mask;
1444
1445         val = (ucontrol->value.integer.value[0] & mask);
1446         if (invert)
1447                 val = mask - val;
1448         val_mask = mask << shift;
1449         val = val << shift;
1450         if (shift != rshift) {
1451                 val2 = (ucontrol->value.integer.value[1] & mask);
1452                 if (invert)
1453                         val2 = mask - val2;
1454                 val_mask |= mask << rshift;
1455                 val |= val2 << rshift;
1456         }
1457         err = snd_soc_update_bits(codec, reg, val_mask, val);
1458         return err;
1459 }
1460 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1461
1462 /**
1463  * snd_soc_info_volsw_2r - double mixer info callback
1464  * @kcontrol: mixer control
1465  * @uinfo: control element information
1466  *
1467  * Callback to provide information about a double mixer control that
1468  * spans 2 codec registers.
1469  *
1470  * Returns 0 for success.
1471  */
1472 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1473         struct snd_ctl_elem_info *uinfo)
1474 {
1475         int mask = (kcontrol->private_value >> 12) & 0xff;
1476
1477         uinfo->type =
1478                 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1479         uinfo->count = 2;
1480         uinfo->value.integer.min = 0;
1481         uinfo->value.integer.max = mask;
1482         return 0;
1483 }
1484 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1485
1486 /**
1487  * snd_soc_get_volsw_2r - double mixer get callback
1488  * @kcontrol: mixer control
1489  * @uinfo: control element information
1490  *
1491  * Callback to get the value of a double mixer control that spans 2 registers.
1492  *
1493  * Returns 0 for success.
1494  */
1495 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1496         struct snd_ctl_elem_value *ucontrol)
1497 {
1498         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1499         int reg = kcontrol->private_value & 0xff;
1500         int reg2 = (kcontrol->private_value >> 24) & 0xff;
1501         int shift = (kcontrol->private_value >> 8) & 0x0f;
1502         int mask = (kcontrol->private_value >> 12) & 0xff;
1503         int invert = (kcontrol->private_value >> 20) & 0x01;
1504
1505         ucontrol->value.integer.value[0] =
1506                 (snd_soc_read(codec, reg) >> shift) & mask;
1507         ucontrol->value.integer.value[1] =
1508                 (snd_soc_read(codec, reg2) >> shift) & mask;
1509         if (invert) {
1510                 ucontrol->value.integer.value[0] =
1511                         mask - ucontrol->value.integer.value[0];
1512                 ucontrol->value.integer.value[1] =
1513                         mask - ucontrol->value.integer.value[1];
1514         }
1515
1516         return 0;
1517 }
1518 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1519
1520 /**
1521  * snd_soc_put_volsw_2r - double mixer set callback
1522  * @kcontrol: mixer control
1523  * @uinfo: control element information
1524  *
1525  * Callback to set the value of a double mixer control that spans 2 registers.
1526  *
1527  * Returns 0 for success.
1528  */
1529 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1530         struct snd_ctl_elem_value *ucontrol)
1531 {
1532         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1533         int reg = kcontrol->private_value & 0xff;
1534         int reg2 = (kcontrol->private_value >> 24) & 0xff;
1535         int shift = (kcontrol->private_value >> 8) & 0x0f;
1536         int mask = (kcontrol->private_value >> 12) & 0xff;
1537         int invert = (kcontrol->private_value >> 20) & 0x01;
1538         int err;
1539         unsigned short val, val2, val_mask;
1540
1541         val_mask = mask << shift;
1542         val = (ucontrol->value.integer.value[0] & mask);
1543         val2 = (ucontrol->value.integer.value[1] & mask);
1544
1545         if (invert) {
1546                 val = mask - val;
1547                 val2 = mask - val2;
1548         }
1549
1550         val = val << shift;
1551         val2 = val2 << shift;
1552
1553         if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
1554                 return err;
1555
1556         err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1557         return err;
1558 }
1559 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1560
1561 static int __devinit snd_soc_init(void)
1562 {
1563         printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1564         return platform_driver_register(&soc_driver);
1565 }
1566
1567 static void snd_soc_exit(void)
1568 {
1569         platform_driver_unregister(&soc_driver);
1570 }
1571
1572 module_init(snd_soc_init);
1573 module_exit(snd_soc_exit);
1574
1575 /* Module information */
1576 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1577 MODULE_DESCRIPTION("ALSA SoC Core");
1578 MODULE_LICENSE("GPL");