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