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