dumptrace: x86: consistently include loglevel, print stack switch
[pandora-kernel.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_dai *cpu_dai = machine->cpu_dai;
138         struct snd_soc_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_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_dai *cpu_dai = machine->cpu_dai;
327         struct snd_soc_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         /* Muting the DAC suppresses artifacts caused during digital
344          * shutdown, for example from stopping clocks.
345          */
346         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
347                 snd_soc_dai_digital_mute(codec_dai, 1);
348
349         if (cpu_dai->ops.shutdown)
350                 cpu_dai->ops.shutdown(substream);
351
352         if (codec_dai->ops.shutdown)
353                 codec_dai->ops.shutdown(substream);
354
355         if (machine->ops && machine->ops->shutdown)
356                 machine->ops->shutdown(substream);
357
358         if (platform->pcm_ops->close)
359                 platform->pcm_ops->close(substream);
360         cpu_dai->runtime = NULL;
361
362         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
363                 /* start delayed pop wq here for playback streams */
364                 codec_dai->pop_wait = 1;
365                 schedule_delayed_work(&socdev->delayed_work,
366                         msecs_to_jiffies(pmdown_time));
367         } else {
368                 /* capture streams can be powered down now */
369                 snd_soc_dapm_stream_event(codec,
370                         codec_dai->capture.stream_name,
371                         SND_SOC_DAPM_STREAM_STOP);
372
373                 if (codec->active == 0 && codec_dai->pop_wait == 0)
374                         snd_soc_dapm_set_bias_level(socdev,
375                                                 SND_SOC_BIAS_STANDBY);
376         }
377
378         mutex_unlock(&pcm_mutex);
379         return 0;
380 }
381
382 /*
383  * Called by ALSA when the PCM substream is prepared, can set format, sample
384  * rate, etc.  This function is non atomic and can be called multiple times,
385  * it can refer to the runtime info.
386  */
387 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
388 {
389         struct snd_soc_pcm_runtime *rtd = substream->private_data;
390         struct snd_soc_device *socdev = rtd->socdev;
391         struct snd_soc_dai_link *machine = rtd->dai;
392         struct snd_soc_platform *platform = socdev->platform;
393         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
394         struct snd_soc_dai *codec_dai = machine->codec_dai;
395         struct snd_soc_codec *codec = socdev->codec;
396         int ret = 0;
397
398         mutex_lock(&pcm_mutex);
399
400         if (machine->ops && machine->ops->prepare) {
401                 ret = machine->ops->prepare(substream);
402                 if (ret < 0) {
403                         printk(KERN_ERR "asoc: machine prepare error\n");
404                         goto out;
405                 }
406         }
407
408         if (platform->pcm_ops->prepare) {
409                 ret = platform->pcm_ops->prepare(substream);
410                 if (ret < 0) {
411                         printk(KERN_ERR "asoc: platform prepare error\n");
412                         goto out;
413                 }
414         }
415
416         if (codec_dai->ops.prepare) {
417                 ret = codec_dai->ops.prepare(substream);
418                 if (ret < 0) {
419                         printk(KERN_ERR "asoc: codec DAI prepare error\n");
420                         goto out;
421                 }
422         }
423
424         if (cpu_dai->ops.prepare) {
425                 ret = cpu_dai->ops.prepare(substream);
426                 if (ret < 0) {
427                         printk(KERN_ERR "asoc: cpu DAI prepare error\n");
428                         goto out;
429                 }
430         }
431
432         /* we only want to start a DAPM playback stream if we are not waiting
433          * on an existing one stopping */
434         if (codec_dai->pop_wait) {
435                 /* we are waiting for the delayed work to start */
436                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
437                                 snd_soc_dapm_stream_event(socdev->codec,
438                                         codec_dai->capture.stream_name,
439                                         SND_SOC_DAPM_STREAM_START);
440                 else {
441                         codec_dai->pop_wait = 0;
442                         cancel_delayed_work(&socdev->delayed_work);
443                         snd_soc_dai_digital_mute(codec_dai, 0);
444                 }
445         } else {
446                 /* no delayed work - do we need to power up codec */
447                 if (codec->bias_level != SND_SOC_BIAS_ON) {
448
449                         snd_soc_dapm_set_bias_level(socdev,
450                                                     SND_SOC_BIAS_PREPARE);
451
452                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
453                                 snd_soc_dapm_stream_event(codec,
454                                         codec_dai->playback.stream_name,
455                                         SND_SOC_DAPM_STREAM_START);
456                         else
457                                 snd_soc_dapm_stream_event(codec,
458                                         codec_dai->capture.stream_name,
459                                         SND_SOC_DAPM_STREAM_START);
460
461                         snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
462                         snd_soc_dai_digital_mute(codec_dai, 0);
463
464                 } else {
465                         /* codec already powered - power on widgets */
466                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
467                                 snd_soc_dapm_stream_event(codec,
468                                         codec_dai->playback.stream_name,
469                                         SND_SOC_DAPM_STREAM_START);
470                         else
471                                 snd_soc_dapm_stream_event(codec,
472                                         codec_dai->capture.stream_name,
473                                         SND_SOC_DAPM_STREAM_START);
474
475                         snd_soc_dai_digital_mute(codec_dai, 0);
476                 }
477         }
478
479 out:
480         mutex_unlock(&pcm_mutex);
481         return ret;
482 }
483
484 /*
485  * Called by ALSA when the hardware params are set by application. This
486  * function can also be called multiple times and can allocate buffers
487  * (using snd_pcm_lib_* ). It's non-atomic.
488  */
489 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
490                                 struct snd_pcm_hw_params *params)
491 {
492         struct snd_soc_pcm_runtime *rtd = substream->private_data;
493         struct snd_soc_device *socdev = rtd->socdev;
494         struct snd_soc_dai_link *machine = rtd->dai;
495         struct snd_soc_platform *platform = socdev->platform;
496         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
497         struct snd_soc_dai *codec_dai = machine->codec_dai;
498         int ret = 0;
499
500         mutex_lock(&pcm_mutex);
501
502         if (machine->ops && machine->ops->hw_params) {
503                 ret = machine->ops->hw_params(substream, params);
504                 if (ret < 0) {
505                         printk(KERN_ERR "asoc: machine hw_params failed\n");
506                         goto out;
507                 }
508         }
509
510         if (codec_dai->ops.hw_params) {
511                 ret = codec_dai->ops.hw_params(substream, params);
512                 if (ret < 0) {
513                         printk(KERN_ERR "asoc: can't set codec %s hw params\n",
514                                 codec_dai->name);
515                         goto codec_err;
516                 }
517         }
518
519         if (cpu_dai->ops.hw_params) {
520                 ret = cpu_dai->ops.hw_params(substream, params);
521                 if (ret < 0) {
522                         printk(KERN_ERR "asoc: interface %s hw params failed\n",
523                                 cpu_dai->name);
524                         goto interface_err;
525                 }
526         }
527
528         if (platform->pcm_ops->hw_params) {
529                 ret = platform->pcm_ops->hw_params(substream, params);
530                 if (ret < 0) {
531                         printk(KERN_ERR "asoc: platform %s hw params failed\n",
532                                 platform->name);
533                         goto platform_err;
534                 }
535         }
536
537 out:
538         mutex_unlock(&pcm_mutex);
539         return ret;
540
541 platform_err:
542         if (cpu_dai->ops.hw_free)
543                 cpu_dai->ops.hw_free(substream);
544
545 interface_err:
546         if (codec_dai->ops.hw_free)
547                 codec_dai->ops.hw_free(substream);
548
549 codec_err:
550         if (machine->ops && machine->ops->hw_free)
551                 machine->ops->hw_free(substream);
552
553         mutex_unlock(&pcm_mutex);
554         return ret;
555 }
556
557 /*
558  * Free's resources allocated by hw_params, can be called multiple times
559  */
560 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
561 {
562         struct snd_soc_pcm_runtime *rtd = substream->private_data;
563         struct snd_soc_device *socdev = rtd->socdev;
564         struct snd_soc_dai_link *machine = rtd->dai;
565         struct snd_soc_platform *platform = socdev->platform;
566         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
567         struct snd_soc_dai *codec_dai = machine->codec_dai;
568         struct snd_soc_codec *codec = socdev->codec;
569
570         mutex_lock(&pcm_mutex);
571
572         /* apply codec digital mute */
573         if (!codec->active)
574                 snd_soc_dai_digital_mute(codec_dai, 1);
575
576         /* free any machine hw params */
577         if (machine->ops && machine->ops->hw_free)
578                 machine->ops->hw_free(substream);
579
580         /* free any DMA resources */
581         if (platform->pcm_ops->hw_free)
582                 platform->pcm_ops->hw_free(substream);
583
584         /* now free hw params for the DAI's  */
585         if (codec_dai->ops.hw_free)
586                 codec_dai->ops.hw_free(substream);
587
588         if (cpu_dai->ops.hw_free)
589                 cpu_dai->ops.hw_free(substream);
590
591         mutex_unlock(&pcm_mutex);
592         return 0;
593 }
594
595 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
596 {
597         struct snd_soc_pcm_runtime *rtd = substream->private_data;
598         struct snd_soc_device *socdev = rtd->socdev;
599         struct snd_soc_dai_link *machine = rtd->dai;
600         struct snd_soc_platform *platform = socdev->platform;
601         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
602         struct snd_soc_dai *codec_dai = machine->codec_dai;
603         int ret;
604
605         if (codec_dai->ops.trigger) {
606                 ret = codec_dai->ops.trigger(substream, cmd);
607                 if (ret < 0)
608                         return ret;
609         }
610
611         if (platform->pcm_ops->trigger) {
612                 ret = platform->pcm_ops->trigger(substream, cmd);
613                 if (ret < 0)
614                         return ret;
615         }
616
617         if (cpu_dai->ops.trigger) {
618                 ret = cpu_dai->ops.trigger(substream, cmd);
619                 if (ret < 0)
620                         return ret;
621         }
622         return 0;
623 }
624
625 /* ASoC PCM operations */
626 static struct snd_pcm_ops soc_pcm_ops = {
627         .open           = soc_pcm_open,
628         .close          = soc_codec_close,
629         .hw_params      = soc_pcm_hw_params,
630         .hw_free        = soc_pcm_hw_free,
631         .prepare        = soc_pcm_prepare,
632         .trigger        = soc_pcm_trigger,
633 };
634
635 #ifdef CONFIG_PM
636 /* powers down audio subsystem for suspend */
637 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
638 {
639         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
640         struct snd_soc_machine *machine = socdev->machine;
641         struct snd_soc_platform *platform = socdev->platform;
642         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
643         struct snd_soc_codec *codec = socdev->codec;
644         int i;
645
646         /* Due to the resume being scheduled into a workqueue we could
647         * suspend before that's finished - wait for it to complete.
648          */
649         snd_power_lock(codec->card);
650         snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
651         snd_power_unlock(codec->card);
652
653         /* we're going to block userspace touching us until resume completes */
654         snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
655
656         /* mute any active DAC's */
657         for (i = 0; i < machine->num_links; i++) {
658                 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
659                 if (dai->dai_ops.digital_mute && dai->playback.active)
660                         dai->dai_ops.digital_mute(dai, 1);
661         }
662
663         /* suspend all pcms */
664         for (i = 0; i < machine->num_links; i++)
665                 snd_pcm_suspend_all(machine->dai_link[i].pcm);
666
667         if (machine->suspend_pre)
668                 machine->suspend_pre(pdev, state);
669
670         for (i = 0; i < machine->num_links; i++) {
671                 struct snd_soc_dai  *cpu_dai = machine->dai_link[i].cpu_dai;
672                 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
673                         cpu_dai->suspend(pdev, cpu_dai);
674                 if (platform->suspend)
675                         platform->suspend(pdev, cpu_dai);
676         }
677
678         /* close any waiting streams and save state */
679         run_delayed_work(&socdev->delayed_work);
680         codec->suspend_bias_level = codec->bias_level;
681
682         for (i = 0; i < codec->num_dai; i++) {
683                 char *stream = codec->dai[i].playback.stream_name;
684                 if (stream != NULL)
685                         snd_soc_dapm_stream_event(codec, stream,
686                                 SND_SOC_DAPM_STREAM_SUSPEND);
687                 stream = codec->dai[i].capture.stream_name;
688                 if (stream != NULL)
689                         snd_soc_dapm_stream_event(codec, stream,
690                                 SND_SOC_DAPM_STREAM_SUSPEND);
691         }
692
693         if (codec_dev->suspend)
694                 codec_dev->suspend(pdev, state);
695
696         for (i = 0; i < machine->num_links; i++) {
697                 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
698                 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
699                         cpu_dai->suspend(pdev, cpu_dai);
700         }
701
702         if (machine->suspend_post)
703                 machine->suspend_post(pdev, state);
704
705         return 0;
706 }
707
708 /* deferred resume work, so resume can complete before we finished
709  * setting our codec back up, which can be very slow on I2C
710  */
711 static void soc_resume_deferred(struct work_struct *work)
712 {
713         struct snd_soc_device *socdev = container_of(work,
714                                                      struct snd_soc_device,
715                                                      deferred_resume_work);
716         struct snd_soc_machine *machine = socdev->machine;
717         struct snd_soc_platform *platform = socdev->platform;
718         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
719         struct snd_soc_codec *codec = socdev->codec;
720         struct platform_device *pdev = to_platform_device(socdev->dev);
721         int i;
722
723         /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
724          * so userspace apps are blocked from touching us
725          */
726
727         dev_info(socdev->dev, "starting resume work\n");
728
729         if (machine->resume_pre)
730                 machine->resume_pre(pdev);
731
732         for (i = 0; i < machine->num_links; i++) {
733                 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
734                 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
735                         cpu_dai->resume(pdev, cpu_dai);
736         }
737
738         if (codec_dev->resume)
739                 codec_dev->resume(pdev);
740
741         for (i = 0; i < codec->num_dai; i++) {
742                 char *stream = codec->dai[i].playback.stream_name;
743                 if (stream != NULL)
744                         snd_soc_dapm_stream_event(codec, stream,
745                                 SND_SOC_DAPM_STREAM_RESUME);
746                 stream = codec->dai[i].capture.stream_name;
747                 if (stream != NULL)
748                         snd_soc_dapm_stream_event(codec, stream,
749                                 SND_SOC_DAPM_STREAM_RESUME);
750         }
751
752         /* unmute any active DACs */
753         for (i = 0; i < machine->num_links; i++) {
754                 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
755                 if (dai->dai_ops.digital_mute && dai->playback.active)
756                         dai->dai_ops.digital_mute(dai, 0);
757         }
758
759         for (i = 0; i < machine->num_links; i++) {
760                 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
761                 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
762                         cpu_dai->resume(pdev, cpu_dai);
763                 if (platform->resume)
764                         platform->resume(pdev, cpu_dai);
765         }
766
767         if (machine->resume_post)
768                 machine->resume_post(pdev);
769
770         dev_info(socdev->dev, "resume work completed\n");
771
772         /* userspace can access us now we are back as we were before */
773         snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
774 }
775
776 /* powers up audio subsystem after a suspend */
777 static int soc_resume(struct platform_device *pdev)
778 {
779         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
780
781         dev_info(socdev->dev, "scheduling resume work\n");
782
783         if (!schedule_work(&socdev->deferred_resume_work))
784                 dev_err(socdev->dev, "work item may be lost\n");
785
786         return 0;
787 }
788
789 #else
790 #define soc_suspend     NULL
791 #define soc_resume      NULL
792 #endif
793
794 /* probes a new socdev */
795 static int soc_probe(struct platform_device *pdev)
796 {
797         int ret = 0, i;
798         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
799         struct snd_soc_machine *machine = socdev->machine;
800         struct snd_soc_platform *platform = socdev->platform;
801         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
802
803         if (machine->probe) {
804                 ret = machine->probe(pdev);
805                 if (ret < 0)
806                         return ret;
807         }
808
809         for (i = 0; i < machine->num_links; i++) {
810                 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
811                 if (cpu_dai->probe) {
812                         ret = cpu_dai->probe(pdev, cpu_dai);
813                         if (ret < 0)
814                                 goto cpu_dai_err;
815                 }
816         }
817
818         if (codec_dev->probe) {
819                 ret = codec_dev->probe(pdev);
820                 if (ret < 0)
821                         goto cpu_dai_err;
822         }
823
824         if (platform->probe) {
825                 ret = platform->probe(pdev);
826                 if (ret < 0)
827                         goto platform_err;
828         }
829
830         /* DAPM stream work */
831         INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
832 #ifdef CONFIG_PM
833         /* deferred resume work */
834         INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
835 #endif
836
837         return 0;
838
839 platform_err:
840         if (codec_dev->remove)
841                 codec_dev->remove(pdev);
842
843 cpu_dai_err:
844         for (i--; i >= 0; i--) {
845                 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
846                 if (cpu_dai->remove)
847                         cpu_dai->remove(pdev, cpu_dai);
848         }
849
850         if (machine->remove)
851                 machine->remove(pdev);
852
853         return ret;
854 }
855
856 /* removes a socdev */
857 static int soc_remove(struct platform_device *pdev)
858 {
859         int i;
860         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
861         struct snd_soc_machine *machine = socdev->machine;
862         struct snd_soc_platform *platform = socdev->platform;
863         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
864
865         run_delayed_work(&socdev->delayed_work);
866
867         if (platform->remove)
868                 platform->remove(pdev);
869
870         if (codec_dev->remove)
871                 codec_dev->remove(pdev);
872
873         for (i = 0; i < machine->num_links; i++) {
874                 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
875                 if (cpu_dai->remove)
876                         cpu_dai->remove(pdev, cpu_dai);
877         }
878
879         if (machine->remove)
880                 machine->remove(pdev);
881
882         return 0;
883 }
884
885 /* ASoC platform driver */
886 static struct platform_driver soc_driver = {
887         .driver         = {
888                 .name           = "soc-audio",
889                 .owner          = THIS_MODULE,
890         },
891         .probe          = soc_probe,
892         .remove         = soc_remove,
893         .suspend        = soc_suspend,
894         .resume         = soc_resume,
895 };
896
897 /* create a new pcm */
898 static int soc_new_pcm(struct snd_soc_device *socdev,
899         struct snd_soc_dai_link *dai_link, int num)
900 {
901         struct snd_soc_codec *codec = socdev->codec;
902         struct snd_soc_dai *codec_dai = dai_link->codec_dai;
903         struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
904         struct snd_soc_pcm_runtime *rtd;
905         struct snd_pcm *pcm;
906         char new_name[64];
907         int ret = 0, playback = 0, capture = 0;
908
909         rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
910         if (rtd == NULL)
911                 return -ENOMEM;
912
913         rtd->dai = dai_link;
914         rtd->socdev = socdev;
915         codec_dai->codec = socdev->codec;
916
917         /* check client and interface hw capabilities */
918         sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
919                 get_dai_name(cpu_dai->type), num);
920
921         if (codec_dai->playback.channels_min)
922                 playback = 1;
923         if (codec_dai->capture.channels_min)
924                 capture = 1;
925
926         ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
927                 capture, &pcm);
928         if (ret < 0) {
929                 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
930                         codec->name);
931                 kfree(rtd);
932                 return ret;
933         }
934
935         dai_link->pcm = pcm;
936         pcm->private_data = rtd;
937         soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
938         soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
939         soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
940         soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
941         soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
942         soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
943         soc_pcm_ops.page = socdev->platform->pcm_ops->page;
944
945         if (playback)
946                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
947
948         if (capture)
949                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
950
951         ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
952         if (ret < 0) {
953                 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
954                 kfree(rtd);
955                 return ret;
956         }
957
958         pcm->private_free = socdev->platform->pcm_free;
959         printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
960                 cpu_dai->name);
961         return ret;
962 }
963
964 /* codec register dump */
965 static ssize_t codec_reg_show(struct device *dev,
966         struct device_attribute *attr, char *buf)
967 {
968         struct snd_soc_device *devdata = dev_get_drvdata(dev);
969         struct snd_soc_codec *codec = devdata->codec;
970         int i, step = 1, count = 0;
971
972         if (!codec->reg_cache_size)
973                 return 0;
974
975         if (codec->reg_cache_step)
976                 step = codec->reg_cache_step;
977
978         count += sprintf(buf, "%s registers\n", codec->name);
979         for (i = 0; i < codec->reg_cache_size; i += step) {
980                 count += sprintf(buf + count, "%2x: ", i);
981                 if (count >= PAGE_SIZE - 1)
982                         break;
983
984                 if (codec->display_register)
985                         count += codec->display_register(codec, buf + count,
986                                                          PAGE_SIZE - count, i);
987                 else
988                         count += snprintf(buf + count, PAGE_SIZE - count,
989                                           "%4x", codec->read(codec, i));
990
991                 if (count >= PAGE_SIZE - 1)
992                         break;
993
994                 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
995                 if (count >= PAGE_SIZE - 1)
996                         break;
997         }
998
999         /* Truncate count; min() would cause a warning */
1000         if (count >= PAGE_SIZE)
1001                 count = PAGE_SIZE - 1;
1002
1003         return count;
1004 }
1005 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1006
1007 /**
1008  * snd_soc_new_ac97_codec - initailise AC97 device
1009  * @codec: audio codec
1010  * @ops: AC97 bus operations
1011  * @num: AC97 codec number
1012  *
1013  * Initialises AC97 codec resources for use by ad-hoc devices only.
1014  */
1015 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1016         struct snd_ac97_bus_ops *ops, int num)
1017 {
1018         mutex_lock(&codec->mutex);
1019
1020         codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1021         if (codec->ac97 == NULL) {
1022                 mutex_unlock(&codec->mutex);
1023                 return -ENOMEM;
1024         }
1025
1026         codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1027         if (codec->ac97->bus == NULL) {
1028                 kfree(codec->ac97);
1029                 codec->ac97 = NULL;
1030                 mutex_unlock(&codec->mutex);
1031                 return -ENOMEM;
1032         }
1033
1034         codec->ac97->bus->ops = ops;
1035         codec->ac97->num = num;
1036         mutex_unlock(&codec->mutex);
1037         return 0;
1038 }
1039 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1040
1041 /**
1042  * snd_soc_free_ac97_codec - free AC97 codec device
1043  * @codec: audio codec
1044  *
1045  * Frees AC97 codec device resources.
1046  */
1047 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1048 {
1049         mutex_lock(&codec->mutex);
1050         kfree(codec->ac97->bus);
1051         kfree(codec->ac97);
1052         codec->ac97 = NULL;
1053         mutex_unlock(&codec->mutex);
1054 }
1055 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1056
1057 /**
1058  * snd_soc_update_bits - update codec register bits
1059  * @codec: audio codec
1060  * @reg: codec register
1061  * @mask: register mask
1062  * @value: new value
1063  *
1064  * Writes new register value.
1065  *
1066  * Returns 1 for change else 0.
1067  */
1068 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1069                                 unsigned short mask, unsigned short value)
1070 {
1071         int change;
1072         unsigned short old, new;
1073
1074         mutex_lock(&io_mutex);
1075         old = snd_soc_read(codec, reg);
1076         new = (old & ~mask) | value;
1077         change = old != new;
1078         if (change)
1079                 snd_soc_write(codec, reg, new);
1080
1081         mutex_unlock(&io_mutex);
1082         return change;
1083 }
1084 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1085
1086 /**
1087  * snd_soc_test_bits - test register for change
1088  * @codec: audio codec
1089  * @reg: codec register
1090  * @mask: register mask
1091  * @value: new value
1092  *
1093  * Tests a register with a new value and checks if the new value is
1094  * different from the old value.
1095  *
1096  * Returns 1 for change else 0.
1097  */
1098 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1099                                 unsigned short mask, unsigned short value)
1100 {
1101         int change;
1102         unsigned short old, new;
1103
1104         mutex_lock(&io_mutex);
1105         old = snd_soc_read(codec, reg);
1106         new = (old & ~mask) | value;
1107         change = old != new;
1108         mutex_unlock(&io_mutex);
1109
1110         return change;
1111 }
1112 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1113
1114 /**
1115  * snd_soc_new_pcms - create new sound card and pcms
1116  * @socdev: the SoC audio device
1117  *
1118  * Create a new sound card based upon the codec and interface pcms.
1119  *
1120  * Returns 0 for success, else error.
1121  */
1122 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1123 {
1124         struct snd_soc_codec *codec = socdev->codec;
1125         struct snd_soc_machine *machine = socdev->machine;
1126         int ret = 0, i;
1127
1128         mutex_lock(&codec->mutex);
1129
1130         /* register a sound card */
1131         codec->card = snd_card_new(idx, xid, codec->owner, 0);
1132         if (!codec->card) {
1133                 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1134                         codec->name);
1135                 mutex_unlock(&codec->mutex);
1136                 return -ENODEV;
1137         }
1138
1139         codec->card->dev = socdev->dev;
1140         codec->card->private_data = codec;
1141         strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1142
1143         /* create the pcms */
1144         for (i = 0; i < machine->num_links; i++) {
1145                 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1146                 if (ret < 0) {
1147                         printk(KERN_ERR "asoc: can't create pcm %s\n",
1148                                 machine->dai_link[i].stream_name);
1149                         mutex_unlock(&codec->mutex);
1150                         return ret;
1151                 }
1152         }
1153
1154         mutex_unlock(&codec->mutex);
1155         return ret;
1156 }
1157 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1158
1159 /**
1160  * snd_soc_register_card - register sound card
1161  * @socdev: the SoC audio device
1162  *
1163  * Register a SoC sound card. Also registers an AC97 device if the
1164  * codec is AC97 for ad hoc devices.
1165  *
1166  * Returns 0 for success, else error.
1167  */
1168 int snd_soc_register_card(struct snd_soc_device *socdev)
1169 {
1170         struct snd_soc_codec *codec = socdev->codec;
1171         struct snd_soc_machine *machine = socdev->machine;
1172         int ret = 0, i, ac97 = 0, err = 0;
1173
1174         for (i = 0; i < machine->num_links; i++) {
1175                 if (socdev->machine->dai_link[i].init) {
1176                         err = socdev->machine->dai_link[i].init(codec);
1177                         if (err < 0) {
1178                                 printk(KERN_ERR "asoc: failed to init %s\n",
1179                                         socdev->machine->dai_link[i].stream_name);
1180                                 continue;
1181                         }
1182                 }
1183                 if (socdev->machine->dai_link[i].codec_dai->type ==
1184                         SND_SOC_DAI_AC97_BUS)
1185                         ac97 = 1;
1186         }
1187         snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1188                  "%s", machine->name);
1189         snprintf(codec->card->longname, sizeof(codec->card->longname),
1190                  "%s (%s)", machine->name, codec->name);
1191
1192         ret = snd_card_register(codec->card);
1193         if (ret < 0) {
1194                 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1195                                 codec->name);
1196                 goto out;
1197         }
1198
1199         mutex_lock(&codec->mutex);
1200 #ifdef CONFIG_SND_SOC_AC97_BUS
1201         if (ac97) {
1202                 ret = soc_ac97_dev_register(codec);
1203                 if (ret < 0) {
1204                         printk(KERN_ERR "asoc: AC97 device register failed\n");
1205                         snd_card_free(codec->card);
1206                         mutex_unlock(&codec->mutex);
1207                         goto out;
1208                 }
1209         }
1210 #endif
1211
1212         err = snd_soc_dapm_sys_add(socdev->dev);
1213         if (err < 0)
1214                 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1215
1216         err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1217         if (err < 0)
1218                 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1219
1220         mutex_unlock(&codec->mutex);
1221
1222 out:
1223         return ret;
1224 }
1225 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1226
1227 /**
1228  * snd_soc_free_pcms - free sound card and pcms
1229  * @socdev: the SoC audio device
1230  *
1231  * Frees sound card and pcms associated with the socdev.
1232  * Also unregister the codec if it is an AC97 device.
1233  */
1234 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1235 {
1236         struct snd_soc_codec *codec = socdev->codec;
1237 #ifdef CONFIG_SND_SOC_AC97_BUS
1238         struct snd_soc_dai *codec_dai;
1239         int i;
1240 #endif
1241
1242         mutex_lock(&codec->mutex);
1243 #ifdef CONFIG_SND_SOC_AC97_BUS
1244         for (i = 0; i < codec->num_dai; i++) {
1245                 codec_dai = &codec->dai[i];
1246                 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1247                         soc_ac97_dev_unregister(codec);
1248                         goto free_card;
1249                 }
1250         }
1251 free_card:
1252 #endif
1253
1254         if (codec->card)
1255                 snd_card_free(codec->card);
1256         device_remove_file(socdev->dev, &dev_attr_codec_reg);
1257         mutex_unlock(&codec->mutex);
1258 }
1259 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1260
1261 /**
1262  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1263  * @substream: the pcm substream
1264  * @hw: the hardware parameters
1265  *
1266  * Sets the substream runtime hardware parameters.
1267  */
1268 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1269         const struct snd_pcm_hardware *hw)
1270 {
1271         struct snd_pcm_runtime *runtime = substream->runtime;
1272         runtime->hw.info = hw->info;
1273         runtime->hw.formats = hw->formats;
1274         runtime->hw.period_bytes_min = hw->period_bytes_min;
1275         runtime->hw.period_bytes_max = hw->period_bytes_max;
1276         runtime->hw.periods_min = hw->periods_min;
1277         runtime->hw.periods_max = hw->periods_max;
1278         runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1279         runtime->hw.fifo_size = hw->fifo_size;
1280         return 0;
1281 }
1282 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1283
1284 /**
1285  * snd_soc_cnew - create new control
1286  * @_template: control template
1287  * @data: control private data
1288  * @lnng_name: control long name
1289  *
1290  * Create a new mixer control from a template control.
1291  *
1292  * Returns 0 for success, else error.
1293  */
1294 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1295         void *data, char *long_name)
1296 {
1297         struct snd_kcontrol_new template;
1298
1299         memcpy(&template, _template, sizeof(template));
1300         if (long_name)
1301                 template.name = long_name;
1302         template.index = 0;
1303
1304         return snd_ctl_new1(&template, data);
1305 }
1306 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1307
1308 /**
1309  * snd_soc_info_enum_double - enumerated double mixer info callback
1310  * @kcontrol: mixer control
1311  * @uinfo: control element information
1312  *
1313  * Callback to provide information about a double enumerated
1314  * mixer control.
1315  *
1316  * Returns 0 for success.
1317  */
1318 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1319         struct snd_ctl_elem_info *uinfo)
1320 {
1321         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1322
1323         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1324         uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1325         uinfo->value.enumerated.items = e->max;
1326
1327         if (uinfo->value.enumerated.item > e->max - 1)
1328                 uinfo->value.enumerated.item = e->max - 1;
1329         strcpy(uinfo->value.enumerated.name,
1330                 e->texts[uinfo->value.enumerated.item]);
1331         return 0;
1332 }
1333 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1334
1335 /**
1336  * snd_soc_get_enum_double - enumerated double mixer get callback
1337  * @kcontrol: mixer control
1338  * @uinfo: control element information
1339  *
1340  * Callback to get the value of a double enumerated mixer.
1341  *
1342  * Returns 0 for success.
1343  */
1344 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1345         struct snd_ctl_elem_value *ucontrol)
1346 {
1347         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1348         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1349         unsigned short val, bitmask;
1350
1351         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1352                 ;
1353         val = snd_soc_read(codec, e->reg);
1354         ucontrol->value.enumerated.item[0]
1355                 = (val >> e->shift_l) & (bitmask - 1);
1356         if (e->shift_l != e->shift_r)
1357                 ucontrol->value.enumerated.item[1] =
1358                         (val >> e->shift_r) & (bitmask - 1);
1359
1360         return 0;
1361 }
1362 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1363
1364 /**
1365  * snd_soc_put_enum_double - enumerated double mixer put callback
1366  * @kcontrol: mixer control
1367  * @uinfo: control element information
1368  *
1369  * Callback to set the value of a double enumerated mixer.
1370  *
1371  * Returns 0 for success.
1372  */
1373 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1374         struct snd_ctl_elem_value *ucontrol)
1375 {
1376         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1377         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1378         unsigned short val;
1379         unsigned short mask, bitmask;
1380
1381         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1382                 ;
1383         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1384                 return -EINVAL;
1385         val = ucontrol->value.enumerated.item[0] << e->shift_l;
1386         mask = (bitmask - 1) << e->shift_l;
1387         if (e->shift_l != e->shift_r) {
1388                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1389                         return -EINVAL;
1390                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1391                 mask |= (bitmask - 1) << e->shift_r;
1392         }
1393
1394         return snd_soc_update_bits(codec, e->reg, mask, val);
1395 }
1396 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1397
1398 /**
1399  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1400  * @kcontrol: mixer control
1401  * @uinfo: control element information
1402  *
1403  * Callback to provide information about an external enumerated
1404  * single mixer.
1405  *
1406  * Returns 0 for success.
1407  */
1408 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1409         struct snd_ctl_elem_info *uinfo)
1410 {
1411         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1412
1413         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1414         uinfo->count = 1;
1415         uinfo->value.enumerated.items = e->max;
1416
1417         if (uinfo->value.enumerated.item > e->max - 1)
1418                 uinfo->value.enumerated.item = e->max - 1;
1419         strcpy(uinfo->value.enumerated.name,
1420                 e->texts[uinfo->value.enumerated.item]);
1421         return 0;
1422 }
1423 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1424
1425 /**
1426  * snd_soc_info_volsw_ext - external single mixer info callback
1427  * @kcontrol: mixer control
1428  * @uinfo: control element information
1429  *
1430  * Callback to provide information about a single external mixer control.
1431  *
1432  * Returns 0 for success.
1433  */
1434 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1435         struct snd_ctl_elem_info *uinfo)
1436 {
1437         int max = kcontrol->private_value;
1438
1439         if (max == 1)
1440                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1441         else
1442                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1443
1444         uinfo->count = 1;
1445         uinfo->value.integer.min = 0;
1446         uinfo->value.integer.max = max;
1447         return 0;
1448 }
1449 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1450
1451 /**
1452  * snd_soc_info_volsw - single mixer info callback
1453  * @kcontrol: mixer control
1454  * @uinfo: control element information
1455  *
1456  * Callback to provide information about a single mixer control.
1457  *
1458  * Returns 0 for success.
1459  */
1460 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1461         struct snd_ctl_elem_info *uinfo)
1462 {
1463         struct soc_mixer_control *mc =
1464                 (struct soc_mixer_control *)kcontrol->private_value;
1465         int max = mc->max;
1466         unsigned int shift = mc->min;
1467         unsigned int rshift = mc->rshift;
1468
1469         if (max == 1)
1470                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1471         else
1472                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1473
1474         uinfo->count = shift == rshift ? 1 : 2;
1475         uinfo->value.integer.min = 0;
1476         uinfo->value.integer.max = max;
1477         return 0;
1478 }
1479 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1480
1481 /**
1482  * snd_soc_get_volsw - single mixer get callback
1483  * @kcontrol: mixer control
1484  * @uinfo: control element information
1485  *
1486  * Callback to get the value of a single mixer control.
1487  *
1488  * Returns 0 for success.
1489  */
1490 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1491         struct snd_ctl_elem_value *ucontrol)
1492 {
1493         struct soc_mixer_control *mc =
1494                 (struct soc_mixer_control *)kcontrol->private_value;
1495         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1496         unsigned int reg = mc->reg;
1497         unsigned int shift = mc->shift;
1498         unsigned int rshift = mc->rshift;
1499         int max = mc->max;
1500         unsigned int mask = (1 << fls(max)) - 1;
1501         unsigned int invert = mc->invert;
1502
1503         ucontrol->value.integer.value[0] =
1504                 (snd_soc_read(codec, reg) >> shift) & mask;
1505         if (shift != rshift)
1506                 ucontrol->value.integer.value[1] =
1507                         (snd_soc_read(codec, reg) >> rshift) & mask;
1508         if (invert) {
1509                 ucontrol->value.integer.value[0] =
1510                         max - ucontrol->value.integer.value[0];
1511                 if (shift != rshift)
1512                         ucontrol->value.integer.value[1] =
1513                                 max - ucontrol->value.integer.value[1];
1514         }
1515
1516         return 0;
1517 }
1518 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1519
1520 /**
1521  * snd_soc_put_volsw - single mixer put callback
1522  * @kcontrol: mixer control
1523  * @uinfo: control element information
1524  *
1525  * Callback to set the value of a single mixer control.
1526  *
1527  * Returns 0 for success.
1528  */
1529 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1530         struct snd_ctl_elem_value *ucontrol)
1531 {
1532         struct soc_mixer_control *mc =
1533                 (struct soc_mixer_control *)kcontrol->private_value;
1534         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1535         unsigned int reg = mc->reg;
1536         unsigned int shift = mc->shift;
1537         unsigned int rshift = mc->rshift;
1538         int max = mc->max;
1539         unsigned int mask = (1 << fls(max)) - 1;
1540         unsigned int invert = mc->invert;
1541         unsigned short val, val2, val_mask;
1542
1543         val = (ucontrol->value.integer.value[0] & mask);
1544         if (invert)
1545                 val = max - val;
1546         val_mask = mask << shift;
1547         val = val << shift;
1548         if (shift != rshift) {
1549                 val2 = (ucontrol->value.integer.value[1] & mask);
1550                 if (invert)
1551                         val2 = max - val2;
1552                 val_mask |= mask << rshift;
1553                 val |= val2 << rshift;
1554         }
1555         return snd_soc_update_bits(codec, reg, val_mask, val);
1556 }
1557 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1558
1559 /**
1560  * snd_soc_info_volsw_2r - double mixer info callback
1561  * @kcontrol: mixer control
1562  * @uinfo: control element information
1563  *
1564  * Callback to provide information about a double mixer control that
1565  * spans 2 codec registers.
1566  *
1567  * Returns 0 for success.
1568  */
1569 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1570         struct snd_ctl_elem_info *uinfo)
1571 {
1572         struct soc_mixer_control *mc =
1573                 (struct soc_mixer_control *)kcontrol->private_value;
1574         int max = mc->max;
1575
1576         if (max == 1)
1577                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1578         else
1579                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1580
1581         uinfo->count = 2;
1582         uinfo->value.integer.min = 0;
1583         uinfo->value.integer.max = max;
1584         return 0;
1585 }
1586 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1587
1588 /**
1589  * snd_soc_get_volsw_2r - double mixer get callback
1590  * @kcontrol: mixer control
1591  * @uinfo: control element information
1592  *
1593  * Callback to get the value of a double mixer control that spans 2 registers.
1594  *
1595  * Returns 0 for success.
1596  */
1597 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1598         struct snd_ctl_elem_value *ucontrol)
1599 {
1600         struct soc_mixer_control *mc =
1601                 (struct soc_mixer_control *)kcontrol->private_value;
1602         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1603         unsigned int reg = mc->reg;
1604         unsigned int reg2 = mc->rreg;
1605         unsigned int shift = mc->shift;
1606         int max = mc->max;
1607         unsigned int mask = (1<<fls(max))-1;
1608         unsigned int invert = mc->invert;
1609
1610         ucontrol->value.integer.value[0] =
1611                 (snd_soc_read(codec, reg) >> shift) & mask;
1612         ucontrol->value.integer.value[1] =
1613                 (snd_soc_read(codec, reg2) >> shift) & mask;
1614         if (invert) {
1615                 ucontrol->value.integer.value[0] =
1616                         max - ucontrol->value.integer.value[0];
1617                 ucontrol->value.integer.value[1] =
1618                         max - ucontrol->value.integer.value[1];
1619         }
1620
1621         return 0;
1622 }
1623 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1624
1625 /**
1626  * snd_soc_put_volsw_2r - double mixer set callback
1627  * @kcontrol: mixer control
1628  * @uinfo: control element information
1629  *
1630  * Callback to set the value of a double mixer control that spans 2 registers.
1631  *
1632  * Returns 0 for success.
1633  */
1634 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1635         struct snd_ctl_elem_value *ucontrol)
1636 {
1637         struct soc_mixer_control *mc =
1638                 (struct soc_mixer_control *)kcontrol->private_value;
1639         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1640         unsigned int reg = mc->reg;
1641         unsigned int reg2 = mc->rreg;
1642         unsigned int shift = mc->shift;
1643         int max = mc->max;
1644         unsigned int mask = (1 << fls(max)) - 1;
1645         unsigned int invert = mc->invert;
1646         int err;
1647         unsigned short val, val2, val_mask;
1648
1649         val_mask = mask << shift;
1650         val = (ucontrol->value.integer.value[0] & mask);
1651         val2 = (ucontrol->value.integer.value[1] & mask);
1652
1653         if (invert) {
1654                 val = max - val;
1655                 val2 = max - val2;
1656         }
1657
1658         val = val << shift;
1659         val2 = val2 << shift;
1660
1661         err = snd_soc_update_bits(codec, reg, val_mask, val);
1662         if (err < 0)
1663                 return err;
1664
1665         err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1666         return err;
1667 }
1668 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1669
1670 /**
1671  * snd_soc_info_volsw_s8 - signed mixer info callback
1672  * @kcontrol: mixer control
1673  * @uinfo: control element information
1674  *
1675  * Callback to provide information about a signed mixer control.
1676  *
1677  * Returns 0 for success.
1678  */
1679 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1680         struct snd_ctl_elem_info *uinfo)
1681 {
1682         struct soc_mixer_control *mc =
1683                 (struct soc_mixer_control *)kcontrol->private_value;
1684         int max = mc->max;
1685         int min = mc->min;
1686
1687         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1688         uinfo->count = 2;
1689         uinfo->value.integer.min = 0;
1690         uinfo->value.integer.max = max-min;
1691         return 0;
1692 }
1693 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1694
1695 /**
1696  * snd_soc_get_volsw_s8 - signed mixer get callback
1697  * @kcontrol: mixer control
1698  * @uinfo: control element information
1699  *
1700  * Callback to get the value of a signed mixer control.
1701  *
1702  * Returns 0 for success.
1703  */
1704 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1705         struct snd_ctl_elem_value *ucontrol)
1706 {
1707         struct soc_mixer_control *mc =
1708                 (struct soc_mixer_control *)kcontrol->private_value;
1709         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1710         unsigned int reg = mc->reg;
1711         int min = mc->min;
1712         int val = snd_soc_read(codec, reg);
1713
1714         ucontrol->value.integer.value[0] =
1715                 ((signed char)(val & 0xff))-min;
1716         ucontrol->value.integer.value[1] =
1717                 ((signed char)((val >> 8) & 0xff))-min;
1718         return 0;
1719 }
1720 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1721
1722 /**
1723  * snd_soc_put_volsw_sgn - signed mixer put callback
1724  * @kcontrol: mixer control
1725  * @uinfo: control element information
1726  *
1727  * Callback to set the value of a signed mixer control.
1728  *
1729  * Returns 0 for success.
1730  */
1731 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1732         struct snd_ctl_elem_value *ucontrol)
1733 {
1734         struct soc_mixer_control *mc =
1735                 (struct soc_mixer_control *)kcontrol->private_value;
1736         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1737         unsigned int reg = mc->reg;
1738         int min = mc->min;
1739         unsigned short val;
1740
1741         val = (ucontrol->value.integer.value[0]+min) & 0xff;
1742         val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1743
1744         return snd_soc_update_bits(codec, reg, 0xffff, val);
1745 }
1746 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1747
1748 /**
1749  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1750  * @dai: DAI
1751  * @clk_id: DAI specific clock ID
1752  * @freq: new clock frequency in Hz
1753  * @dir: new clock direction - input/output.
1754  *
1755  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1756  */
1757 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1758         unsigned int freq, int dir)
1759 {
1760         if (dai->dai_ops.set_sysclk)
1761                 return dai->dai_ops.set_sysclk(dai, clk_id, freq, dir);
1762         else
1763                 return -EINVAL;
1764 }
1765 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1766
1767 /**
1768  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1769  * @dai: DAI
1770  * @clk_id: DAI specific clock divider ID
1771  * @div: new clock divisor.
1772  *
1773  * Configures the clock dividers. This is used to derive the best DAI bit and
1774  * frame clocks from the system or master clock. It's best to set the DAI bit
1775  * and frame clocks as low as possible to save system power.
1776  */
1777 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1778         int div_id, int div)
1779 {
1780         if (dai->dai_ops.set_clkdiv)
1781                 return dai->dai_ops.set_clkdiv(dai, div_id, div);
1782         else
1783                 return -EINVAL;
1784 }
1785 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1786
1787 /**
1788  * snd_soc_dai_set_pll - configure DAI PLL.
1789  * @dai: DAI
1790  * @pll_id: DAI specific PLL ID
1791  * @freq_in: PLL input clock frequency in Hz
1792  * @freq_out: requested PLL output clock frequency in Hz
1793  *
1794  * Configures and enables PLL to generate output clock based on input clock.
1795  */
1796 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1797         int pll_id, unsigned int freq_in, unsigned int freq_out)
1798 {
1799         if (dai->dai_ops.set_pll)
1800                 return dai->dai_ops.set_pll(dai, pll_id, freq_in, freq_out);
1801         else
1802                 return -EINVAL;
1803 }
1804 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1805
1806 /**
1807  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1808  * @dai: DAI
1809  * @clk_id: DAI specific clock ID
1810  * @fmt: SND_SOC_DAIFMT_ format value.
1811  *
1812  * Configures the DAI hardware format and clocking.
1813  */
1814 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1815 {
1816         if (dai->dai_ops.set_fmt)
1817                 return dai->dai_ops.set_fmt(dai, fmt);
1818         else
1819                 return -EINVAL;
1820 }
1821 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1822
1823 /**
1824  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1825  * @dai: DAI
1826  * @mask: DAI specific mask representing used slots.
1827  * @slots: Number of slots in use.
1828  *
1829  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1830  * specific.
1831  */
1832 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1833         unsigned int mask, int slots)
1834 {
1835         if (dai->dai_ops.set_sysclk)
1836                 return dai->dai_ops.set_tdm_slot(dai, mask, slots);
1837         else
1838                 return -EINVAL;
1839 }
1840 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1841
1842 /**
1843  * snd_soc_dai_set_tristate - configure DAI system or master clock.
1844  * @dai: DAI
1845  * @tristate: tristate enable
1846  *
1847  * Tristates the DAI so that others can use it.
1848  */
1849 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1850 {
1851         if (dai->dai_ops.set_sysclk)
1852                 return dai->dai_ops.set_tristate(dai, tristate);
1853         else
1854                 return -EINVAL;
1855 }
1856 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1857
1858 /**
1859  * snd_soc_dai_digital_mute - configure DAI system or master clock.
1860  * @dai: DAI
1861  * @mute: mute enable
1862  *
1863  * Mutes the DAI DAC.
1864  */
1865 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1866 {
1867         if (dai->dai_ops.digital_mute)
1868                 return dai->dai_ops.digital_mute(dai, mute);
1869         else
1870                 return -EINVAL;
1871 }
1872 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1873
1874 static int __devinit snd_soc_init(void)
1875 {
1876         printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1877         return platform_driver_register(&soc_driver);
1878 }
1879
1880 static void snd_soc_exit(void)
1881 {
1882         platform_driver_unregister(&soc_driver);
1883 }
1884
1885 module_init(snd_soc_init);
1886 module_exit(snd_soc_exit);
1887
1888 /* Module information */
1889 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1890 MODULE_DESCRIPTION("ALSA SoC Core");
1891 MODULE_LICENSE("GPL");
1892 MODULE_ALIAS("platform:soc-audio");