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