Merge branch 'topic/vmaster-update' into topic/docbook-fix
[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 <lrg@slimlogic.co.uk>
8  *         with code, comments and ideas from :-
9  *         Richard Purdie <richard@openedhand.com>
10  *
11  *  This program is free software; you can redistribute  it and/or modify it
12  *  under  the terms of  the GNU General  Public License as published by the
13  *  Free Software Foundation;  either version 2 of the  License, or (at your
14  *  option) any later version.
15  *
16  *  TODO:
17  *   o Add hw rules to enforce rates, etc.
18  *   o More testing with other codecs/machines.
19  *   o Add more codecs and platforms to ensure good API coverage.
20  *   o Support TDM on PCM and I2S
21  */
22
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/pm.h>
28 #include <linux/bitops.h>
29 #include <linux/debugfs.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 static DEFINE_MUTEX(pcm_mutex);
39 static DEFINE_MUTEX(io_mutex);
40 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
42 #ifdef CONFIG_DEBUG_FS
43 static struct dentry *debugfs_root;
44 #endif
45
46 static DEFINE_MUTEX(client_mutex);
47 static LIST_HEAD(card_list);
48 static LIST_HEAD(dai_list);
49 static LIST_HEAD(platform_list);
50 static LIST_HEAD(codec_list);
51
52 static int snd_soc_register_card(struct snd_soc_card *card);
53 static int snd_soc_unregister_card(struct snd_soc_card *card);
54
55 /*
56  * This is a timeout to do a DAPM powerdown after a stream is closed().
57  * It can be used to eliminate pops between different playback streams, e.g.
58  * between two audio tracks.
59  */
60 static int pmdown_time = 5000;
61 module_param(pmdown_time, int, 0);
62 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
63
64 /*
65  * This function forces any delayed work to be queued and run.
66  */
67 static int run_delayed_work(struct delayed_work *dwork)
68 {
69         int ret;
70
71         /* cancel any work waiting to be queued. */
72         ret = cancel_delayed_work(dwork);
73
74         /* if there was any work waiting then we run it now and
75          * wait for it's completion */
76         if (ret) {
77                 schedule_delayed_work(dwork, 0);
78                 flush_scheduled_work();
79         }
80         return ret;
81 }
82
83 #ifdef CONFIG_SND_SOC_AC97_BUS
84 /* unregister ac97 codec */
85 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
86 {
87         if (codec->ac97->dev.bus)
88                 device_unregister(&codec->ac97->dev);
89         return 0;
90 }
91
92 /* stop no dev release warning */
93 static void soc_ac97_device_release(struct device *dev){}
94
95 /* register ac97 codec to bus */
96 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
97 {
98         int err;
99
100         codec->ac97->dev.bus = &ac97_bus_type;
101         codec->ac97->dev.parent = NULL;
102         codec->ac97->dev.release = soc_ac97_device_release;
103
104         dev_set_name(&codec->ac97->dev, "%d-%d:%s",
105                      codec->card->number, 0, codec->name);
106         err = device_register(&codec->ac97->dev);
107         if (err < 0) {
108                 snd_printk(KERN_ERR "Can't register ac97 bus\n");
109                 codec->ac97->dev.bus = NULL;
110                 return err;
111         }
112         return 0;
113 }
114 #endif
115
116 /*
117  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
118  * then initialized and any private data can be allocated. This also calls
119  * startup for the cpu DAI, platform, machine and codec DAI.
120  */
121 static int soc_pcm_open(struct snd_pcm_substream *substream)
122 {
123         struct snd_soc_pcm_runtime *rtd = substream->private_data;
124         struct snd_soc_device *socdev = rtd->socdev;
125         struct snd_soc_card *card = socdev->card;
126         struct snd_pcm_runtime *runtime = substream->runtime;
127         struct snd_soc_dai_link *machine = rtd->dai;
128         struct snd_soc_platform *platform = card->platform;
129         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
130         struct snd_soc_dai *codec_dai = machine->codec_dai;
131         int ret = 0;
132
133         mutex_lock(&pcm_mutex);
134
135         /* startup the audio subsystem */
136         if (cpu_dai->ops.startup) {
137                 ret = cpu_dai->ops.startup(substream, cpu_dai);
138                 if (ret < 0) {
139                         printk(KERN_ERR "asoc: can't open interface %s\n",
140                                 cpu_dai->name);
141                         goto out;
142                 }
143         }
144
145         if (platform->pcm_ops->open) {
146                 ret = platform->pcm_ops->open(substream);
147                 if (ret < 0) {
148                         printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
149                         goto platform_err;
150                 }
151         }
152
153         if (codec_dai->ops.startup) {
154                 ret = codec_dai->ops.startup(substream, codec_dai);
155                 if (ret < 0) {
156                         printk(KERN_ERR "asoc: can't open codec %s\n",
157                                 codec_dai->name);
158                         goto codec_dai_err;
159                 }
160         }
161
162         if (machine->ops && machine->ops->startup) {
163                 ret = machine->ops->startup(substream);
164                 if (ret < 0) {
165                         printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
166                         goto machine_err;
167                 }
168         }
169
170         /* Check that the codec and cpu DAI's are compatible */
171         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
172                 runtime->hw.rate_min =
173                         max(codec_dai->playback.rate_min,
174                             cpu_dai->playback.rate_min);
175                 runtime->hw.rate_max =
176                         min(codec_dai->playback.rate_max,
177                             cpu_dai->playback.rate_max);
178                 runtime->hw.channels_min =
179                         max(codec_dai->playback.channels_min,
180                                 cpu_dai->playback.channels_min);
181                 runtime->hw.channels_max =
182                         min(codec_dai->playback.channels_max,
183                                 cpu_dai->playback.channels_max);
184                 runtime->hw.formats =
185                         codec_dai->playback.formats & cpu_dai->playback.formats;
186                 runtime->hw.rates =
187                         codec_dai->playback.rates & cpu_dai->playback.rates;
188         } else {
189                 runtime->hw.rate_min =
190                         max(codec_dai->capture.rate_min,
191                             cpu_dai->capture.rate_min);
192                 runtime->hw.rate_max =
193                         min(codec_dai->capture.rate_max,
194                             cpu_dai->capture.rate_max);
195                 runtime->hw.channels_min =
196                         max(codec_dai->capture.channels_min,
197                                 cpu_dai->capture.channels_min);
198                 runtime->hw.channels_max =
199                         min(codec_dai->capture.channels_max,
200                                 cpu_dai->capture.channels_max);
201                 runtime->hw.formats =
202                         codec_dai->capture.formats & cpu_dai->capture.formats;
203                 runtime->hw.rates =
204                         codec_dai->capture.rates & cpu_dai->capture.rates;
205         }
206
207         snd_pcm_limit_hw_rates(runtime);
208         if (!runtime->hw.rates) {
209                 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
210                         codec_dai->name, cpu_dai->name);
211                 goto machine_err;
212         }
213         if (!runtime->hw.formats) {
214                 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
215                         codec_dai->name, cpu_dai->name);
216                 goto machine_err;
217         }
218         if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
219                 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
220                         codec_dai->name, cpu_dai->name);
221                 goto machine_err;
222         }
223
224         pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
225         pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
226         pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
227                  runtime->hw.channels_max);
228         pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
229                  runtime->hw.rate_max);
230
231         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
232                 cpu_dai->playback.active = codec_dai->playback.active = 1;
233         else
234                 cpu_dai->capture.active = codec_dai->capture.active = 1;
235         cpu_dai->active = codec_dai->active = 1;
236         cpu_dai->runtime = runtime;
237         socdev->codec->active++;
238         mutex_unlock(&pcm_mutex);
239         return 0;
240
241 machine_err:
242         if (machine->ops && machine->ops->shutdown)
243                 machine->ops->shutdown(substream);
244
245 codec_dai_err:
246         if (platform->pcm_ops->close)
247                 platform->pcm_ops->close(substream);
248
249 platform_err:
250         if (cpu_dai->ops.shutdown)
251                 cpu_dai->ops.shutdown(substream, cpu_dai);
252 out:
253         mutex_unlock(&pcm_mutex);
254         return ret;
255 }
256
257 /*
258  * Power down the audio subsystem pmdown_time msecs after close is called.
259  * This is to ensure there are no pops or clicks in between any music tracks
260  * due to DAPM power cycling.
261  */
262 static void close_delayed_work(struct work_struct *work)
263 {
264         struct snd_soc_card *card = container_of(work, struct snd_soc_card,
265                                                  delayed_work.work);
266         struct snd_soc_device *socdev = card->socdev;
267         struct snd_soc_codec *codec = socdev->codec;
268         struct snd_soc_dai *codec_dai;
269         int i;
270
271         mutex_lock(&pcm_mutex);
272         for (i = 0; i < codec->num_dai; i++) {
273                 codec_dai = &codec->dai[i];
274
275                 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
276                          codec_dai->playback.stream_name,
277                          codec_dai->playback.active ? "active" : "inactive",
278                          codec_dai->pop_wait ? "yes" : "no");
279
280                 /* are we waiting on this codec DAI stream */
281                 if (codec_dai->pop_wait == 1) {
282
283                         /* Reduce power if no longer active */
284                         if (codec->active == 0) {
285                                 pr_debug("pop wq D1 %s %s\n", codec->name,
286                                          codec_dai->playback.stream_name);
287                                 snd_soc_dapm_set_bias_level(socdev,
288                                         SND_SOC_BIAS_PREPARE);
289                         }
290
291                         codec_dai->pop_wait = 0;
292                         snd_soc_dapm_stream_event(codec,
293                                 codec_dai->playback.stream_name,
294                                 SND_SOC_DAPM_STREAM_STOP);
295
296                         /* Fall into standby if no longer active */
297                         if (codec->active == 0) {
298                                 pr_debug("pop wq D3 %s %s\n", codec->name,
299                                          codec_dai->playback.stream_name);
300                                 snd_soc_dapm_set_bias_level(socdev,
301                                         SND_SOC_BIAS_STANDBY);
302                         }
303                 }
304         }
305         mutex_unlock(&pcm_mutex);
306 }
307
308 /*
309  * Called by ALSA when a PCM substream is closed. Private data can be
310  * freed here. The cpu DAI, codec DAI, machine and platform are also
311  * shutdown.
312  */
313 static int soc_codec_close(struct snd_pcm_substream *substream)
314 {
315         struct snd_soc_pcm_runtime *rtd = substream->private_data;
316         struct snd_soc_device *socdev = rtd->socdev;
317         struct snd_soc_card *card = socdev->card;
318         struct snd_soc_dai_link *machine = rtd->dai;
319         struct snd_soc_platform *platform = card->platform;
320         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
321         struct snd_soc_dai *codec_dai = machine->codec_dai;
322         struct snd_soc_codec *codec = socdev->codec;
323
324         mutex_lock(&pcm_mutex);
325
326         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
327                 cpu_dai->playback.active = codec_dai->playback.active = 0;
328         else
329                 cpu_dai->capture.active = codec_dai->capture.active = 0;
330
331         if (codec_dai->playback.active == 0 &&
332                 codec_dai->capture.active == 0) {
333                 cpu_dai->active = codec_dai->active = 0;
334         }
335         codec->active--;
336
337         /* Muting the DAC suppresses artifacts caused during digital
338          * shutdown, for example from stopping clocks.
339          */
340         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
341                 snd_soc_dai_digital_mute(codec_dai, 1);
342
343         if (cpu_dai->ops.shutdown)
344                 cpu_dai->ops.shutdown(substream, cpu_dai);
345
346         if (codec_dai->ops.shutdown)
347                 codec_dai->ops.shutdown(substream, codec_dai);
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(&card->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_card *card = socdev->card;
386         struct snd_soc_dai_link *machine = rtd->dai;
387         struct snd_soc_platform *platform = card->platform;
388         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
389         struct snd_soc_dai *codec_dai = machine->codec_dai;
390         struct snd_soc_codec *codec = socdev->codec;
391         int ret = 0;
392
393         mutex_lock(&pcm_mutex);
394
395         if (machine->ops && machine->ops->prepare) {
396                 ret = machine->ops->prepare(substream);
397                 if (ret < 0) {
398                         printk(KERN_ERR "asoc: machine prepare error\n");
399                         goto out;
400                 }
401         }
402
403         if (platform->pcm_ops->prepare) {
404                 ret = platform->pcm_ops->prepare(substream);
405                 if (ret < 0) {
406                         printk(KERN_ERR "asoc: platform prepare error\n");
407                         goto out;
408                 }
409         }
410
411         if (codec_dai->ops.prepare) {
412                 ret = codec_dai->ops.prepare(substream, codec_dai);
413                 if (ret < 0) {
414                         printk(KERN_ERR "asoc: codec DAI prepare error\n");
415                         goto out;
416                 }
417         }
418
419         if (cpu_dai->ops.prepare) {
420                 ret = cpu_dai->ops.prepare(substream, cpu_dai);
421                 if (ret < 0) {
422                         printk(KERN_ERR "asoc: cpu DAI prepare error\n");
423                         goto out;
424                 }
425         }
426
427         /* cancel any delayed stream shutdown that is pending */
428         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
429             codec_dai->pop_wait) {
430                 codec_dai->pop_wait = 0;
431                 cancel_delayed_work(&card->delayed_work);
432         }
433
434         /* do we need to power up codec */
435         if (codec->bias_level != SND_SOC_BIAS_ON) {
436                 snd_soc_dapm_set_bias_level(socdev,
437                                             SND_SOC_BIAS_PREPARE);
438
439                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
440                         snd_soc_dapm_stream_event(codec,
441                                         codec_dai->playback.stream_name,
442                                         SND_SOC_DAPM_STREAM_START);
443                 else
444                         snd_soc_dapm_stream_event(codec,
445                                         codec_dai->capture.stream_name,
446                                         SND_SOC_DAPM_STREAM_START);
447
448                 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
449                 snd_soc_dai_digital_mute(codec_dai, 0);
450
451         } else {
452                 /* codec already powered - power on widgets */
453                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
454                         snd_soc_dapm_stream_event(codec,
455                                         codec_dai->playback.stream_name,
456                                         SND_SOC_DAPM_STREAM_START);
457                 else
458                         snd_soc_dapm_stream_event(codec,
459                                         codec_dai->capture.stream_name,
460                                         SND_SOC_DAPM_STREAM_START);
461
462                 snd_soc_dai_digital_mute(codec_dai, 0);
463         }
464
465 out:
466         mutex_unlock(&pcm_mutex);
467         return ret;
468 }
469
470 /*
471  * Called by ALSA when the hardware params are set by application. This
472  * function can also be called multiple times and can allocate buffers
473  * (using snd_pcm_lib_* ). It's non-atomic.
474  */
475 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
476                                 struct snd_pcm_hw_params *params)
477 {
478         struct snd_soc_pcm_runtime *rtd = substream->private_data;
479         struct snd_soc_device *socdev = rtd->socdev;
480         struct snd_soc_dai_link *machine = rtd->dai;
481         struct snd_soc_card *card = socdev->card;
482         struct snd_soc_platform *platform = card->platform;
483         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
484         struct snd_soc_dai *codec_dai = machine->codec_dai;
485         int ret = 0;
486
487         mutex_lock(&pcm_mutex);
488
489         if (machine->ops && machine->ops->hw_params) {
490                 ret = machine->ops->hw_params(substream, params);
491                 if (ret < 0) {
492                         printk(KERN_ERR "asoc: machine hw_params failed\n");
493                         goto out;
494                 }
495         }
496
497         if (codec_dai->ops.hw_params) {
498                 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
499                 if (ret < 0) {
500                         printk(KERN_ERR "asoc: can't set codec %s hw params\n",
501                                 codec_dai->name);
502                         goto codec_err;
503                 }
504         }
505
506         if (cpu_dai->ops.hw_params) {
507                 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
508                 if (ret < 0) {
509                         printk(KERN_ERR "asoc: interface %s hw params failed\n",
510                                 cpu_dai->name);
511                         goto interface_err;
512                 }
513         }
514
515         if (platform->pcm_ops->hw_params) {
516                 ret = platform->pcm_ops->hw_params(substream, params);
517                 if (ret < 0) {
518                         printk(KERN_ERR "asoc: platform %s hw params failed\n",
519                                 platform->name);
520                         goto platform_err;
521                 }
522         }
523
524 out:
525         mutex_unlock(&pcm_mutex);
526         return ret;
527
528 platform_err:
529         if (cpu_dai->ops.hw_free)
530                 cpu_dai->ops.hw_free(substream, cpu_dai);
531
532 interface_err:
533         if (codec_dai->ops.hw_free)
534                 codec_dai->ops.hw_free(substream, codec_dai);
535
536 codec_err:
537         if (machine->ops && machine->ops->hw_free)
538                 machine->ops->hw_free(substream);
539
540         mutex_unlock(&pcm_mutex);
541         return ret;
542 }
543
544 /*
545  * Free's resources allocated by hw_params, can be called multiple times
546  */
547 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
548 {
549         struct snd_soc_pcm_runtime *rtd = substream->private_data;
550         struct snd_soc_device *socdev = rtd->socdev;
551         struct snd_soc_dai_link *machine = rtd->dai;
552         struct snd_soc_card *card = socdev->card;
553         struct snd_soc_platform *platform = card->platform;
554         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
555         struct snd_soc_dai *codec_dai = machine->codec_dai;
556         struct snd_soc_codec *codec = socdev->codec;
557
558         mutex_lock(&pcm_mutex);
559
560         /* apply codec digital mute */
561         if (!codec->active)
562                 snd_soc_dai_digital_mute(codec_dai, 1);
563
564         /* free any machine hw params */
565         if (machine->ops && machine->ops->hw_free)
566                 machine->ops->hw_free(substream);
567
568         /* free any DMA resources */
569         if (platform->pcm_ops->hw_free)
570                 platform->pcm_ops->hw_free(substream);
571
572         /* now free hw params for the DAI's  */
573         if (codec_dai->ops.hw_free)
574                 codec_dai->ops.hw_free(substream, codec_dai);
575
576         if (cpu_dai->ops.hw_free)
577                 cpu_dai->ops.hw_free(substream, cpu_dai);
578
579         mutex_unlock(&pcm_mutex);
580         return 0;
581 }
582
583 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
584 {
585         struct snd_soc_pcm_runtime *rtd = substream->private_data;
586         struct snd_soc_device *socdev = rtd->socdev;
587         struct snd_soc_card *card= socdev->card;
588         struct snd_soc_dai_link *machine = rtd->dai;
589         struct snd_soc_platform *platform = card->platform;
590         struct snd_soc_dai *cpu_dai = machine->cpu_dai;
591         struct snd_soc_dai *codec_dai = machine->codec_dai;
592         int ret;
593
594         if (codec_dai->ops.trigger) {
595                 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
596                 if (ret < 0)
597                         return ret;
598         }
599
600         if (platform->pcm_ops->trigger) {
601                 ret = platform->pcm_ops->trigger(substream, cmd);
602                 if (ret < 0)
603                         return ret;
604         }
605
606         if (cpu_dai->ops.trigger) {
607                 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
608                 if (ret < 0)
609                         return ret;
610         }
611         return 0;
612 }
613
614 /* ASoC PCM operations */
615 static struct snd_pcm_ops soc_pcm_ops = {
616         .open           = soc_pcm_open,
617         .close          = soc_codec_close,
618         .hw_params      = soc_pcm_hw_params,
619         .hw_free        = soc_pcm_hw_free,
620         .prepare        = soc_pcm_prepare,
621         .trigger        = soc_pcm_trigger,
622 };
623
624 #ifdef CONFIG_PM
625 /* powers down audio subsystem for suspend */
626 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
627 {
628         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
629         struct snd_soc_card *card = socdev->card;
630         struct snd_soc_platform *platform = card->platform;
631         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
632         struct snd_soc_codec *codec = socdev->codec;
633         int i;
634
635         /* Due to the resume being scheduled into a workqueue we could
636         * suspend before that's finished - wait for it to complete.
637          */
638         snd_power_lock(codec->card);
639         snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
640         snd_power_unlock(codec->card);
641
642         /* we're going to block userspace touching us until resume completes */
643         snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
644
645         /* mute any active DAC's */
646         for (i = 0; i < card->num_links; i++) {
647                 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
648                 if (dai->ops.digital_mute && dai->playback.active)
649                         dai->ops.digital_mute(dai, 1);
650         }
651
652         /* suspend all pcms */
653         for (i = 0; i < card->num_links; i++)
654                 snd_pcm_suspend_all(card->dai_link[i].pcm);
655
656         if (card->suspend_pre)
657                 card->suspend_pre(pdev, state);
658
659         for (i = 0; i < card->num_links; i++) {
660                 struct snd_soc_dai  *cpu_dai = card->dai_link[i].cpu_dai;
661                 if (cpu_dai->suspend && !cpu_dai->ac97_control)
662                         cpu_dai->suspend(cpu_dai);
663                 if (platform->suspend)
664                         platform->suspend(cpu_dai);
665         }
666
667         /* close any waiting streams and save state */
668         run_delayed_work(&card->delayed_work);
669         codec->suspend_bias_level = codec->bias_level;
670
671         for (i = 0; i < codec->num_dai; i++) {
672                 char *stream = codec->dai[i].playback.stream_name;
673                 if (stream != NULL)
674                         snd_soc_dapm_stream_event(codec, stream,
675                                 SND_SOC_DAPM_STREAM_SUSPEND);
676                 stream = codec->dai[i].capture.stream_name;
677                 if (stream != NULL)
678                         snd_soc_dapm_stream_event(codec, stream,
679                                 SND_SOC_DAPM_STREAM_SUSPEND);
680         }
681
682         if (codec_dev->suspend)
683                 codec_dev->suspend(pdev, state);
684
685         for (i = 0; i < card->num_links; i++) {
686                 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
687                 if (cpu_dai->suspend && cpu_dai->ac97_control)
688                         cpu_dai->suspend(cpu_dai);
689         }
690
691         if (card->suspend_post)
692                 card->suspend_post(pdev, state);
693
694         return 0;
695 }
696
697 /* deferred resume work, so resume can complete before we finished
698  * setting our codec back up, which can be very slow on I2C
699  */
700 static void soc_resume_deferred(struct work_struct *work)
701 {
702         struct snd_soc_card *card = container_of(work,
703                                                  struct snd_soc_card,
704                                                  deferred_resume_work);
705         struct snd_soc_device *socdev = card->socdev;
706         struct snd_soc_platform *platform = card->platform;
707         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
708         struct snd_soc_codec *codec = socdev->codec;
709         struct platform_device *pdev = to_platform_device(socdev->dev);
710         int i;
711
712         /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
713          * so userspace apps are blocked from touching us
714          */
715
716         dev_dbg(socdev->dev, "starting resume work\n");
717
718         if (card->resume_pre)
719                 card->resume_pre(pdev);
720
721         for (i = 0; i < card->num_links; i++) {
722                 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
723                 if (cpu_dai->resume && cpu_dai->ac97_control)
724                         cpu_dai->resume(cpu_dai);
725         }
726
727         if (codec_dev->resume)
728                 codec_dev->resume(pdev);
729
730         for (i = 0; i < codec->num_dai; i++) {
731                 char *stream = codec->dai[i].playback.stream_name;
732                 if (stream != NULL)
733                         snd_soc_dapm_stream_event(codec, stream,
734                                 SND_SOC_DAPM_STREAM_RESUME);
735                 stream = codec->dai[i].capture.stream_name;
736                 if (stream != NULL)
737                         snd_soc_dapm_stream_event(codec, stream,
738                                 SND_SOC_DAPM_STREAM_RESUME);
739         }
740
741         /* unmute any active DACs */
742         for (i = 0; i < card->num_links; i++) {
743                 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
744                 if (dai->ops.digital_mute && dai->playback.active)
745                         dai->ops.digital_mute(dai, 0);
746         }
747
748         for (i = 0; i < card->num_links; i++) {
749                 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
750                 if (cpu_dai->resume && !cpu_dai->ac97_control)
751                         cpu_dai->resume(cpu_dai);
752                 if (platform->resume)
753                         platform->resume(cpu_dai);
754         }
755
756         if (card->resume_post)
757                 card->resume_post(pdev);
758
759         dev_dbg(socdev->dev, "resume work completed\n");
760
761         /* userspace can access us now we are back as we were before */
762         snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
763 }
764
765 /* powers up audio subsystem after a suspend */
766 static int soc_resume(struct platform_device *pdev)
767 {
768         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
769         struct snd_soc_card *card = socdev->card;
770
771         dev_dbg(socdev->dev, "scheduling resume work\n");
772
773         if (!schedule_work(&card->deferred_resume_work))
774                 dev_err(socdev->dev, "resume work item may be lost\n");
775
776         return 0;
777 }
778
779 #else
780 #define soc_suspend     NULL
781 #define soc_resume      NULL
782 #endif
783
784 static void snd_soc_instantiate_card(struct snd_soc_card *card)
785 {
786         struct platform_device *pdev = container_of(card->dev,
787                                                     struct platform_device,
788                                                     dev);
789         struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
790         struct snd_soc_platform *platform;
791         struct snd_soc_dai *dai;
792         int i, found, ret, ac97;
793
794         if (card->instantiated)
795                 return;
796
797         found = 0;
798         list_for_each_entry(platform, &platform_list, list)
799                 if (card->platform == platform) {
800                         found = 1;
801                         break;
802                 }
803         if (!found) {
804                 dev_dbg(card->dev, "Platform %s not registered\n",
805                         card->platform->name);
806                 return;
807         }
808
809         ac97 = 0;
810         for (i = 0; i < card->num_links; i++) {
811                 found = 0;
812                 list_for_each_entry(dai, &dai_list, list)
813                         if (card->dai_link[i].cpu_dai == dai) {
814                                 found = 1;
815                                 break;
816                         }
817                 if (!found) {
818                         dev_dbg(card->dev, "DAI %s not registered\n",
819                                 card->dai_link[i].cpu_dai->name);
820                         return;
821                 }
822
823                 if (card->dai_link[i].cpu_dai->ac97_control)
824                         ac97 = 1;
825         }
826
827         /* If we have AC97 in the system then don't wait for the
828          * codec.  This will need revisiting if we have to handle
829          * systems with mixed AC97 and non-AC97 parts.  Only check for
830          * DAIs currently; we can't do this per link since some AC97
831          * codecs have non-AC97 DAIs.
832          */
833         if (!ac97)
834                 for (i = 0; i < card->num_links; i++) {
835                         found = 0;
836                         list_for_each_entry(dai, &dai_list, list)
837                                 if (card->dai_link[i].codec_dai == dai) {
838                                         found = 1;
839                                         break;
840                                 }
841                         if (!found) {
842                                 dev_dbg(card->dev, "DAI %s not registered\n",
843                                         card->dai_link[i].codec_dai->name);
844                                 return;
845                         }
846                 }
847
848         /* Note that we do not current check for codec components */
849
850         dev_dbg(card->dev, "All components present, instantiating\n");
851
852         /* Found everything, bring it up */
853         if (card->probe) {
854                 ret = card->probe(pdev);
855                 if (ret < 0)
856                         return;
857         }
858
859         for (i = 0; i < card->num_links; i++) {
860                 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
861                 if (cpu_dai->probe) {
862                         ret = cpu_dai->probe(pdev, cpu_dai);
863                         if (ret < 0)
864                                 goto cpu_dai_err;
865                 }
866         }
867
868         if (codec_dev->probe) {
869                 ret = codec_dev->probe(pdev);
870                 if (ret < 0)
871                         goto cpu_dai_err;
872         }
873
874         if (platform->probe) {
875                 ret = platform->probe(pdev);
876                 if (ret < 0)
877                         goto platform_err;
878         }
879
880         /* DAPM stream work */
881         INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
882 #ifdef CONFIG_PM
883         /* deferred resume work */
884         INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
885 #endif
886
887         card->instantiated = 1;
888
889         return;
890
891 platform_err:
892         if (codec_dev->remove)
893                 codec_dev->remove(pdev);
894
895 cpu_dai_err:
896         for (i--; i >= 0; i--) {
897                 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
898                 if (cpu_dai->remove)
899                         cpu_dai->remove(pdev, cpu_dai);
900         }
901
902         if (card->remove)
903                 card->remove(pdev);
904 }
905
906 /*
907  * Attempt to initialise any uninitalised cards.  Must be called with
908  * client_mutex.
909  */
910 static void snd_soc_instantiate_cards(void)
911 {
912         struct snd_soc_card *card;
913         list_for_each_entry(card, &card_list, list)
914                 snd_soc_instantiate_card(card);
915 }
916
917 /* probes a new socdev */
918 static int soc_probe(struct platform_device *pdev)
919 {
920         int ret = 0;
921         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
922         struct snd_soc_card *card = socdev->card;
923
924         /* Bodge while we push things out of socdev */
925         card->socdev = socdev;
926
927         /* Bodge while we unpick instantiation */
928         card->dev = &pdev->dev;
929         ret = snd_soc_register_card(card);
930         if (ret != 0) {
931                 dev_err(&pdev->dev, "Failed to register card\n");
932                 return ret;
933         }
934
935         return 0;
936 }
937
938 /* removes a socdev */
939 static int soc_remove(struct platform_device *pdev)
940 {
941         int i;
942         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
943         struct snd_soc_card *card = socdev->card;
944         struct snd_soc_platform *platform = card->platform;
945         struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
946
947         run_delayed_work(&card->delayed_work);
948
949         if (platform->remove)
950                 platform->remove(pdev);
951
952         if (codec_dev->remove)
953                 codec_dev->remove(pdev);
954
955         for (i = 0; i < card->num_links; i++) {
956                 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
957                 if (cpu_dai->remove)
958                         cpu_dai->remove(pdev, cpu_dai);
959         }
960
961         if (card->remove)
962                 card->remove(pdev);
963
964         snd_soc_unregister_card(card);
965
966         return 0;
967 }
968
969 /* ASoC platform driver */
970 static struct platform_driver soc_driver = {
971         .driver         = {
972                 .name           = "soc-audio",
973                 .owner          = THIS_MODULE,
974         },
975         .probe          = soc_probe,
976         .remove         = soc_remove,
977         .suspend        = soc_suspend,
978         .resume         = soc_resume,
979 };
980
981 /* create a new pcm */
982 static int soc_new_pcm(struct snd_soc_device *socdev,
983         struct snd_soc_dai_link *dai_link, int num)
984 {
985         struct snd_soc_codec *codec = socdev->codec;
986         struct snd_soc_card *card = socdev->card;
987         struct snd_soc_platform *platform = card->platform;
988         struct snd_soc_dai *codec_dai = dai_link->codec_dai;
989         struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
990         struct snd_soc_pcm_runtime *rtd;
991         struct snd_pcm *pcm;
992         char new_name[64];
993         int ret = 0, playback = 0, capture = 0;
994
995         rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
996         if (rtd == NULL)
997                 return -ENOMEM;
998
999         rtd->dai = dai_link;
1000         rtd->socdev = socdev;
1001         codec_dai->codec = socdev->codec;
1002
1003         /* check client and interface hw capabilities */
1004         sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
1005                 num);
1006
1007         if (codec_dai->playback.channels_min)
1008                 playback = 1;
1009         if (codec_dai->capture.channels_min)
1010                 capture = 1;
1011
1012         ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1013                 capture, &pcm);
1014         if (ret < 0) {
1015                 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1016                         codec->name);
1017                 kfree(rtd);
1018                 return ret;
1019         }
1020
1021         dai_link->pcm = pcm;
1022         pcm->private_data = rtd;
1023         soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1024         soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1025         soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1026         soc_pcm_ops.copy = platform->pcm_ops->copy;
1027         soc_pcm_ops.silence = platform->pcm_ops->silence;
1028         soc_pcm_ops.ack = platform->pcm_ops->ack;
1029         soc_pcm_ops.page = platform->pcm_ops->page;
1030
1031         if (playback)
1032                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1033
1034         if (capture)
1035                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1036
1037         ret = platform->pcm_new(codec->card, codec_dai, pcm);
1038         if (ret < 0) {
1039                 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1040                 kfree(rtd);
1041                 return ret;
1042         }
1043
1044         pcm->private_free = platform->pcm_free;
1045         printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1046                 cpu_dai->name);
1047         return ret;
1048 }
1049
1050 /* codec register dump */
1051 static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
1052 {
1053         struct snd_soc_codec *codec = devdata->codec;
1054         int i, step = 1, count = 0;
1055
1056         if (!codec->reg_cache_size)
1057                 return 0;
1058
1059         if (codec->reg_cache_step)
1060                 step = codec->reg_cache_step;
1061
1062         count += sprintf(buf, "%s registers\n", codec->name);
1063         for (i = 0; i < codec->reg_cache_size; i += step) {
1064                 count += sprintf(buf + count, "%2x: ", i);
1065                 if (count >= PAGE_SIZE - 1)
1066                         break;
1067
1068                 if (codec->display_register)
1069                         count += codec->display_register(codec, buf + count,
1070                                                          PAGE_SIZE - count, i);
1071                 else
1072                         count += snprintf(buf + count, PAGE_SIZE - count,
1073                                           "%4x", codec->read(codec, i));
1074
1075                 if (count >= PAGE_SIZE - 1)
1076                         break;
1077
1078                 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1079                 if (count >= PAGE_SIZE - 1)
1080                         break;
1081         }
1082
1083         /* Truncate count; min() would cause a warning */
1084         if (count >= PAGE_SIZE)
1085                 count = PAGE_SIZE - 1;
1086
1087         return count;
1088 }
1089 static ssize_t codec_reg_show(struct device *dev,
1090         struct device_attribute *attr, char *buf)
1091 {
1092         struct snd_soc_device *devdata = dev_get_drvdata(dev);
1093         return soc_codec_reg_show(devdata, buf);
1094 }
1095
1096 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1097
1098 #ifdef CONFIG_DEBUG_FS
1099 static int codec_reg_open_file(struct inode *inode, struct file *file)
1100 {
1101         file->private_data = inode->i_private;
1102         return 0;
1103 }
1104
1105 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1106                                size_t count, loff_t *ppos)
1107 {
1108         ssize_t ret;
1109         struct snd_soc_codec *codec = file->private_data;
1110         struct device *card_dev = codec->card->dev;
1111         struct snd_soc_device *devdata = card_dev->driver_data;
1112         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1113         if (!buf)
1114                 return -ENOMEM;
1115         ret = soc_codec_reg_show(devdata, buf);
1116         if (ret >= 0)
1117                 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1118         kfree(buf);
1119         return ret;
1120 }
1121
1122 static ssize_t codec_reg_write_file(struct file *file,
1123                 const char __user *user_buf, size_t count, loff_t *ppos)
1124 {
1125         char buf[32];
1126         int buf_size;
1127         char *start = buf;
1128         unsigned long reg, value;
1129         int step = 1;
1130         struct snd_soc_codec *codec = file->private_data;
1131
1132         buf_size = min(count, (sizeof(buf)-1));
1133         if (copy_from_user(buf, user_buf, buf_size))
1134                 return -EFAULT;
1135         buf[buf_size] = 0;
1136
1137         if (codec->reg_cache_step)
1138                 step = codec->reg_cache_step;
1139
1140         while (*start == ' ')
1141                 start++;
1142         reg = simple_strtoul(start, &start, 16);
1143         if ((reg >= codec->reg_cache_size) || (reg % step))
1144                 return -EINVAL;
1145         while (*start == ' ')
1146                 start++;
1147         if (strict_strtoul(start, 16, &value))
1148                 return -EINVAL;
1149         codec->write(codec, reg, value);
1150         return buf_size;
1151 }
1152
1153 static const struct file_operations codec_reg_fops = {
1154         .open = codec_reg_open_file,
1155         .read = codec_reg_read_file,
1156         .write = codec_reg_write_file,
1157 };
1158
1159 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
1160 {
1161         codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1162                                                  debugfs_root, codec,
1163                                                  &codec_reg_fops);
1164         if (!codec->debugfs_reg)
1165                 printk(KERN_WARNING
1166                        "ASoC: Failed to create codec register debugfs file\n");
1167
1168         codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1169                                                      debugfs_root,
1170                                                      &codec->pop_time);
1171         if (!codec->debugfs_pop_time)
1172                 printk(KERN_WARNING
1173                        "Failed to create pop time debugfs file\n");
1174 }
1175
1176 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
1177 {
1178         debugfs_remove(codec->debugfs_pop_time);
1179         debugfs_remove(codec->debugfs_reg);
1180 }
1181
1182 #else
1183
1184 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
1185 {
1186 }
1187
1188 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
1189 {
1190 }
1191 #endif
1192
1193 /**
1194  * snd_soc_new_ac97_codec - initailise AC97 device
1195  * @codec: audio codec
1196  * @ops: AC97 bus operations
1197  * @num: AC97 codec number
1198  *
1199  * Initialises AC97 codec resources for use by ad-hoc devices only.
1200  */
1201 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1202         struct snd_ac97_bus_ops *ops, int num)
1203 {
1204         mutex_lock(&codec->mutex);
1205
1206         codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1207         if (codec->ac97 == NULL) {
1208                 mutex_unlock(&codec->mutex);
1209                 return -ENOMEM;
1210         }
1211
1212         codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1213         if (codec->ac97->bus == NULL) {
1214                 kfree(codec->ac97);
1215                 codec->ac97 = NULL;
1216                 mutex_unlock(&codec->mutex);
1217                 return -ENOMEM;
1218         }
1219
1220         codec->ac97->bus->ops = ops;
1221         codec->ac97->num = num;
1222         mutex_unlock(&codec->mutex);
1223         return 0;
1224 }
1225 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1226
1227 /**
1228  * snd_soc_free_ac97_codec - free AC97 codec device
1229  * @codec: audio codec
1230  *
1231  * Frees AC97 codec device resources.
1232  */
1233 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1234 {
1235         mutex_lock(&codec->mutex);
1236         kfree(codec->ac97->bus);
1237         kfree(codec->ac97);
1238         codec->ac97 = NULL;
1239         mutex_unlock(&codec->mutex);
1240 }
1241 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1242
1243 /**
1244  * snd_soc_update_bits - update codec register bits
1245  * @codec: audio codec
1246  * @reg: codec register
1247  * @mask: register mask
1248  * @value: new value
1249  *
1250  * Writes new register value.
1251  *
1252  * Returns 1 for change else 0.
1253  */
1254 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1255                                 unsigned short mask, unsigned short value)
1256 {
1257         int change;
1258         unsigned short old, new;
1259
1260         mutex_lock(&io_mutex);
1261         old = snd_soc_read(codec, reg);
1262         new = (old & ~mask) | value;
1263         change = old != new;
1264         if (change)
1265                 snd_soc_write(codec, reg, new);
1266
1267         mutex_unlock(&io_mutex);
1268         return change;
1269 }
1270 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1271
1272 /**
1273  * snd_soc_test_bits - test register for change
1274  * @codec: audio codec
1275  * @reg: codec register
1276  * @mask: register mask
1277  * @value: new value
1278  *
1279  * Tests a register with a new value and checks if the new value is
1280  * different from the old value.
1281  *
1282  * Returns 1 for change else 0.
1283  */
1284 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1285                                 unsigned short mask, unsigned short value)
1286 {
1287         int change;
1288         unsigned short old, new;
1289
1290         mutex_lock(&io_mutex);
1291         old = snd_soc_read(codec, reg);
1292         new = (old & ~mask) | value;
1293         change = old != new;
1294         mutex_unlock(&io_mutex);
1295
1296         return change;
1297 }
1298 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1299
1300 /**
1301  * snd_soc_new_pcms - create new sound card and pcms
1302  * @socdev: the SoC audio device
1303  * @idx: ALSA card index
1304  * @xid: card identification
1305  *
1306  * Create a new sound card based upon the codec and interface pcms.
1307  *
1308  * Returns 0 for success, else error.
1309  */
1310 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1311 {
1312         struct snd_soc_codec *codec = socdev->codec;
1313         struct snd_soc_card *card = socdev->card;
1314         int ret, i;
1315
1316         mutex_lock(&codec->mutex);
1317
1318         /* register a sound card */
1319         ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1320         if (ret < 0) {
1321                 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1322                         codec->name);
1323                 mutex_unlock(&codec->mutex);
1324                 return ret;
1325         }
1326
1327         codec->card->dev = socdev->dev;
1328         codec->card->private_data = codec;
1329         strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1330
1331         /* create the pcms */
1332         for (i = 0; i < card->num_links; i++) {
1333                 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1334                 if (ret < 0) {
1335                         printk(KERN_ERR "asoc: can't create pcm %s\n",
1336                                 card->dai_link[i].stream_name);
1337                         mutex_unlock(&codec->mutex);
1338                         return ret;
1339                 }
1340         }
1341
1342         mutex_unlock(&codec->mutex);
1343         return ret;
1344 }
1345 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1346
1347 /**
1348  * snd_soc_init_card - register sound card
1349  * @socdev: the SoC audio device
1350  *
1351  * Register a SoC sound card. Also registers an AC97 device if the
1352  * codec is AC97 for ad hoc devices.
1353  *
1354  * Returns 0 for success, else error.
1355  */
1356 int snd_soc_init_card(struct snd_soc_device *socdev)
1357 {
1358         struct snd_soc_codec *codec = socdev->codec;
1359         struct snd_soc_card *card = socdev->card;
1360         int ret = 0, i, ac97 = 0, err = 0;
1361
1362         for (i = 0; i < card->num_links; i++) {
1363                 if (card->dai_link[i].init) {
1364                         err = card->dai_link[i].init(codec);
1365                         if (err < 0) {
1366                                 printk(KERN_ERR "asoc: failed to init %s\n",
1367                                         card->dai_link[i].stream_name);
1368                                 continue;
1369                         }
1370                 }
1371                 if (card->dai_link[i].codec_dai->ac97_control)
1372                         ac97 = 1;
1373         }
1374         snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1375                  "%s",  card->name);
1376         snprintf(codec->card->longname, sizeof(codec->card->longname),
1377                  "%s (%s)", card->name, codec->name);
1378
1379         ret = snd_card_register(codec->card);
1380         if (ret < 0) {
1381                 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1382                                 codec->name);
1383                 goto out;
1384         }
1385
1386         mutex_lock(&codec->mutex);
1387 #ifdef CONFIG_SND_SOC_AC97_BUS
1388         /* Only instantiate AC97 if not already done by the adaptor
1389          * for the generic AC97 subsystem.
1390          */
1391         if (ac97 && strcmp(codec->name, "AC97") != 0) {
1392                 ret = soc_ac97_dev_register(codec);
1393                 if (ret < 0) {
1394                         printk(KERN_ERR "asoc: AC97 device register failed\n");
1395                         snd_card_free(codec->card);
1396                         mutex_unlock(&codec->mutex);
1397                         goto out;
1398                 }
1399         }
1400 #endif
1401
1402         err = snd_soc_dapm_sys_add(socdev->dev);
1403         if (err < 0)
1404                 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1405
1406         err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1407         if (err < 0)
1408                 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1409
1410         soc_init_codec_debugfs(socdev->codec);
1411         mutex_unlock(&codec->mutex);
1412
1413 out:
1414         return ret;
1415 }
1416 EXPORT_SYMBOL_GPL(snd_soc_init_card);
1417
1418 /**
1419  * snd_soc_free_pcms - free sound card and pcms
1420  * @socdev: the SoC audio device
1421  *
1422  * Frees sound card and pcms associated with the socdev.
1423  * Also unregister the codec if it is an AC97 device.
1424  */
1425 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1426 {
1427         struct snd_soc_codec *codec = socdev->codec;
1428 #ifdef CONFIG_SND_SOC_AC97_BUS
1429         struct snd_soc_dai *codec_dai;
1430         int i;
1431 #endif
1432
1433         mutex_lock(&codec->mutex);
1434         soc_cleanup_codec_debugfs(socdev->codec);
1435 #ifdef CONFIG_SND_SOC_AC97_BUS
1436         for (i = 0; i < codec->num_dai; i++) {
1437                 codec_dai = &codec->dai[i];
1438                 if (codec_dai->ac97_control && codec->ac97) {
1439                         soc_ac97_dev_unregister(codec);
1440                         goto free_card;
1441                 }
1442         }
1443 free_card:
1444 #endif
1445
1446         if (codec->card)
1447                 snd_card_free(codec->card);
1448         device_remove_file(socdev->dev, &dev_attr_codec_reg);
1449         mutex_unlock(&codec->mutex);
1450 }
1451 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1452
1453 /**
1454  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1455  * @substream: the pcm substream
1456  * @hw: the hardware parameters
1457  *
1458  * Sets the substream runtime hardware parameters.
1459  */
1460 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1461         const struct snd_pcm_hardware *hw)
1462 {
1463         struct snd_pcm_runtime *runtime = substream->runtime;
1464         runtime->hw.info = hw->info;
1465         runtime->hw.formats = hw->formats;
1466         runtime->hw.period_bytes_min = hw->period_bytes_min;
1467         runtime->hw.period_bytes_max = hw->period_bytes_max;
1468         runtime->hw.periods_min = hw->periods_min;
1469         runtime->hw.periods_max = hw->periods_max;
1470         runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1471         runtime->hw.fifo_size = hw->fifo_size;
1472         return 0;
1473 }
1474 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1475
1476 /**
1477  * snd_soc_cnew - create new control
1478  * @_template: control template
1479  * @data: control private data
1480  * @long_name: control long name
1481  *
1482  * Create a new mixer control from a template control.
1483  *
1484  * Returns 0 for success, else error.
1485  */
1486 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1487         void *data, char *long_name)
1488 {
1489         struct snd_kcontrol_new template;
1490
1491         memcpy(&template, _template, sizeof(template));
1492         if (long_name)
1493                 template.name = long_name;
1494         template.index = 0;
1495
1496         return snd_ctl_new1(&template, data);
1497 }
1498 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1499
1500 /**
1501  * snd_soc_info_enum_double - enumerated double mixer info callback
1502  * @kcontrol: mixer control
1503  * @uinfo: control element information
1504  *
1505  * Callback to provide information about a double enumerated
1506  * mixer control.
1507  *
1508  * Returns 0 for success.
1509  */
1510 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1511         struct snd_ctl_elem_info *uinfo)
1512 {
1513         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1514
1515         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1516         uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1517         uinfo->value.enumerated.items = e->max;
1518
1519         if (uinfo->value.enumerated.item > e->max - 1)
1520                 uinfo->value.enumerated.item = e->max - 1;
1521         strcpy(uinfo->value.enumerated.name,
1522                 e->texts[uinfo->value.enumerated.item]);
1523         return 0;
1524 }
1525 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1526
1527 /**
1528  * snd_soc_get_enum_double - enumerated double mixer get callback
1529  * @kcontrol: mixer control
1530  * @ucontrol: control element information
1531  *
1532  * Callback to get the value of a double enumerated mixer.
1533  *
1534  * Returns 0 for success.
1535  */
1536 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1537         struct snd_ctl_elem_value *ucontrol)
1538 {
1539         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1540         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1541         unsigned short val, bitmask;
1542
1543         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1544                 ;
1545         val = snd_soc_read(codec, e->reg);
1546         ucontrol->value.enumerated.item[0]
1547                 = (val >> e->shift_l) & (bitmask - 1);
1548         if (e->shift_l != e->shift_r)
1549                 ucontrol->value.enumerated.item[1] =
1550                         (val >> e->shift_r) & (bitmask - 1);
1551
1552         return 0;
1553 }
1554 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1555
1556 /**
1557  * snd_soc_put_enum_double - enumerated double mixer put callback
1558  * @kcontrol: mixer control
1559  * @ucontrol: control element information
1560  *
1561  * Callback to set the value of a double enumerated mixer.
1562  *
1563  * Returns 0 for success.
1564  */
1565 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1566         struct snd_ctl_elem_value *ucontrol)
1567 {
1568         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1569         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1570         unsigned short val;
1571         unsigned short mask, bitmask;
1572
1573         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1574                 ;
1575         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1576                 return -EINVAL;
1577         val = ucontrol->value.enumerated.item[0] << e->shift_l;
1578         mask = (bitmask - 1) << e->shift_l;
1579         if (e->shift_l != e->shift_r) {
1580                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1581                         return -EINVAL;
1582                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1583                 mask |= (bitmask - 1) << e->shift_r;
1584         }
1585
1586         return snd_soc_update_bits(codec, e->reg, mask, val);
1587 }
1588 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1589
1590 /**
1591  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1592  * @kcontrol: mixer control
1593  * @ucontrol: control element information
1594  *
1595  * Callback to get the value of a double semi enumerated mixer.
1596  *
1597  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1598  * used for handling bitfield coded enumeration for example.
1599  *
1600  * Returns 0 for success.
1601  */
1602 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1603         struct snd_ctl_elem_value *ucontrol)
1604 {
1605         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1606         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1607         unsigned short reg_val, val, mux;
1608
1609         reg_val = snd_soc_read(codec, e->reg);
1610         val = (reg_val >> e->shift_l) & e->mask;
1611         for (mux = 0; mux < e->max; mux++) {
1612                 if (val == e->values[mux])
1613                         break;
1614         }
1615         ucontrol->value.enumerated.item[0] = mux;
1616         if (e->shift_l != e->shift_r) {
1617                 val = (reg_val >> e->shift_r) & e->mask;
1618                 for (mux = 0; mux < e->max; mux++) {
1619                         if (val == e->values[mux])
1620                                 break;
1621                 }
1622                 ucontrol->value.enumerated.item[1] = mux;
1623         }
1624
1625         return 0;
1626 }
1627 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1628
1629 /**
1630  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1631  * @kcontrol: mixer control
1632  * @ucontrol: control element information
1633  *
1634  * Callback to set the value of a double semi enumerated mixer.
1635  *
1636  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1637  * used for handling bitfield coded enumeration for example.
1638  *
1639  * Returns 0 for success.
1640  */
1641 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1642         struct snd_ctl_elem_value *ucontrol)
1643 {
1644         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1645         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1646         unsigned short val;
1647         unsigned short mask;
1648
1649         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1650                 return -EINVAL;
1651         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1652         mask = e->mask << e->shift_l;
1653         if (e->shift_l != e->shift_r) {
1654                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1655                         return -EINVAL;
1656                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1657                 mask |= e->mask << e->shift_r;
1658         }
1659
1660         return snd_soc_update_bits(codec, e->reg, mask, val);
1661 }
1662 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1663
1664 /**
1665  * snd_soc_info_enum_ext - external enumerated single mixer info callback
1666  * @kcontrol: mixer control
1667  * @uinfo: control element information
1668  *
1669  * Callback to provide information about an external enumerated
1670  * single mixer.
1671  *
1672  * Returns 0 for success.
1673  */
1674 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1675         struct snd_ctl_elem_info *uinfo)
1676 {
1677         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1678
1679         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1680         uinfo->count = 1;
1681         uinfo->value.enumerated.items = e->max;
1682
1683         if (uinfo->value.enumerated.item > e->max - 1)
1684                 uinfo->value.enumerated.item = e->max - 1;
1685         strcpy(uinfo->value.enumerated.name,
1686                 e->texts[uinfo->value.enumerated.item]);
1687         return 0;
1688 }
1689 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1690
1691 /**
1692  * snd_soc_info_volsw_ext - external single mixer info callback
1693  * @kcontrol: mixer control
1694  * @uinfo: control element information
1695  *
1696  * Callback to provide information about a single external mixer control.
1697  *
1698  * Returns 0 for success.
1699  */
1700 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1701         struct snd_ctl_elem_info *uinfo)
1702 {
1703         int max = kcontrol->private_value;
1704
1705         if (max == 1)
1706                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1707         else
1708                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1709
1710         uinfo->count = 1;
1711         uinfo->value.integer.min = 0;
1712         uinfo->value.integer.max = max;
1713         return 0;
1714 }
1715 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1716
1717 /**
1718  * snd_soc_info_volsw - single mixer info callback
1719  * @kcontrol: mixer control
1720  * @uinfo: control element information
1721  *
1722  * Callback to provide information about a single mixer control.
1723  *
1724  * Returns 0 for success.
1725  */
1726 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1727         struct snd_ctl_elem_info *uinfo)
1728 {
1729         struct soc_mixer_control *mc =
1730                 (struct soc_mixer_control *)kcontrol->private_value;
1731         int max = mc->max;
1732         unsigned int shift = mc->shift;
1733         unsigned int rshift = mc->rshift;
1734
1735         if (max == 1)
1736                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1737         else
1738                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1739
1740         uinfo->count = shift == rshift ? 1 : 2;
1741         uinfo->value.integer.min = 0;
1742         uinfo->value.integer.max = max;
1743         return 0;
1744 }
1745 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1746
1747 /**
1748  * snd_soc_get_volsw - single mixer get callback
1749  * @kcontrol: mixer control
1750  * @ucontrol: control element information
1751  *
1752  * Callback to get the value of a single mixer control.
1753  *
1754  * Returns 0 for success.
1755  */
1756 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1757         struct snd_ctl_elem_value *ucontrol)
1758 {
1759         struct soc_mixer_control *mc =
1760                 (struct soc_mixer_control *)kcontrol->private_value;
1761         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1762         unsigned int reg = mc->reg;
1763         unsigned int shift = mc->shift;
1764         unsigned int rshift = mc->rshift;
1765         int max = mc->max;
1766         unsigned int mask = (1 << fls(max)) - 1;
1767         unsigned int invert = mc->invert;
1768
1769         ucontrol->value.integer.value[0] =
1770                 (snd_soc_read(codec, reg) >> shift) & mask;
1771         if (shift != rshift)
1772                 ucontrol->value.integer.value[1] =
1773                         (snd_soc_read(codec, reg) >> rshift) & mask;
1774         if (invert) {
1775                 ucontrol->value.integer.value[0] =
1776                         max - ucontrol->value.integer.value[0];
1777                 if (shift != rshift)
1778                         ucontrol->value.integer.value[1] =
1779                                 max - ucontrol->value.integer.value[1];
1780         }
1781
1782         return 0;
1783 }
1784 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1785
1786 /**
1787  * snd_soc_put_volsw - single mixer put callback
1788  * @kcontrol: mixer control
1789  * @ucontrol: control element information
1790  *
1791  * Callback to set the value of a single mixer control.
1792  *
1793  * Returns 0 for success.
1794  */
1795 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1796         struct snd_ctl_elem_value *ucontrol)
1797 {
1798         struct soc_mixer_control *mc =
1799                 (struct soc_mixer_control *)kcontrol->private_value;
1800         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1801         unsigned int reg = mc->reg;
1802         unsigned int shift = mc->shift;
1803         unsigned int rshift = mc->rshift;
1804         int max = mc->max;
1805         unsigned int mask = (1 << fls(max)) - 1;
1806         unsigned int invert = mc->invert;
1807         unsigned short val, val2, val_mask;
1808
1809         val = (ucontrol->value.integer.value[0] & mask);
1810         if (invert)
1811                 val = max - val;
1812         val_mask = mask << shift;
1813         val = val << shift;
1814         if (shift != rshift) {
1815                 val2 = (ucontrol->value.integer.value[1] & mask);
1816                 if (invert)
1817                         val2 = max - val2;
1818                 val_mask |= mask << rshift;
1819                 val |= val2 << rshift;
1820         }
1821         return snd_soc_update_bits(codec, reg, val_mask, val);
1822 }
1823 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1824
1825 /**
1826  * snd_soc_info_volsw_2r - double mixer info callback
1827  * @kcontrol: mixer control
1828  * @uinfo: control element information
1829  *
1830  * Callback to provide information about a double mixer control that
1831  * spans 2 codec registers.
1832  *
1833  * Returns 0 for success.
1834  */
1835 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1836         struct snd_ctl_elem_info *uinfo)
1837 {
1838         struct soc_mixer_control *mc =
1839                 (struct soc_mixer_control *)kcontrol->private_value;
1840         int max = mc->max;
1841
1842         if (max == 1)
1843                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1844         else
1845                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1846
1847         uinfo->count = 2;
1848         uinfo->value.integer.min = 0;
1849         uinfo->value.integer.max = max;
1850         return 0;
1851 }
1852 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1853
1854 /**
1855  * snd_soc_get_volsw_2r - double mixer get callback
1856  * @kcontrol: mixer control
1857  * @ucontrol: control element information
1858  *
1859  * Callback to get the value of a double mixer control that spans 2 registers.
1860  *
1861  * Returns 0 for success.
1862  */
1863 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1864         struct snd_ctl_elem_value *ucontrol)
1865 {
1866         struct soc_mixer_control *mc =
1867                 (struct soc_mixer_control *)kcontrol->private_value;
1868         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1869         unsigned int reg = mc->reg;
1870         unsigned int reg2 = mc->rreg;
1871         unsigned int shift = mc->shift;
1872         int max = mc->max;
1873         unsigned int mask = (1<<fls(max))-1;
1874         unsigned int invert = mc->invert;
1875
1876         ucontrol->value.integer.value[0] =
1877                 (snd_soc_read(codec, reg) >> shift) & mask;
1878         ucontrol->value.integer.value[1] =
1879                 (snd_soc_read(codec, reg2) >> shift) & mask;
1880         if (invert) {
1881                 ucontrol->value.integer.value[0] =
1882                         max - ucontrol->value.integer.value[0];
1883                 ucontrol->value.integer.value[1] =
1884                         max - ucontrol->value.integer.value[1];
1885         }
1886
1887         return 0;
1888 }
1889 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1890
1891 /**
1892  * snd_soc_put_volsw_2r - double mixer set callback
1893  * @kcontrol: mixer control
1894  * @ucontrol: control element information
1895  *
1896  * Callback to set the value of a double mixer control that spans 2 registers.
1897  *
1898  * Returns 0 for success.
1899  */
1900 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1901         struct snd_ctl_elem_value *ucontrol)
1902 {
1903         struct soc_mixer_control *mc =
1904                 (struct soc_mixer_control *)kcontrol->private_value;
1905         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1906         unsigned int reg = mc->reg;
1907         unsigned int reg2 = mc->rreg;
1908         unsigned int shift = mc->shift;
1909         int max = mc->max;
1910         unsigned int mask = (1 << fls(max)) - 1;
1911         unsigned int invert = mc->invert;
1912         int err;
1913         unsigned short val, val2, val_mask;
1914
1915         val_mask = mask << shift;
1916         val = (ucontrol->value.integer.value[0] & mask);
1917         val2 = (ucontrol->value.integer.value[1] & mask);
1918
1919         if (invert) {
1920                 val = max - val;
1921                 val2 = max - val2;
1922         }
1923
1924         val = val << shift;
1925         val2 = val2 << shift;
1926
1927         err = snd_soc_update_bits(codec, reg, val_mask, val);
1928         if (err < 0)
1929                 return err;
1930
1931         err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1932         return err;
1933 }
1934 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1935
1936 /**
1937  * snd_soc_info_volsw_s8 - signed mixer info callback
1938  * @kcontrol: mixer control
1939  * @uinfo: control element information
1940  *
1941  * Callback to provide information about a signed mixer control.
1942  *
1943  * Returns 0 for success.
1944  */
1945 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1946         struct snd_ctl_elem_info *uinfo)
1947 {
1948         struct soc_mixer_control *mc =
1949                 (struct soc_mixer_control *)kcontrol->private_value;
1950         int max = mc->max;
1951         int min = mc->min;
1952
1953         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1954         uinfo->count = 2;
1955         uinfo->value.integer.min = 0;
1956         uinfo->value.integer.max = max-min;
1957         return 0;
1958 }
1959 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1960
1961 /**
1962  * snd_soc_get_volsw_s8 - signed mixer get callback
1963  * @kcontrol: mixer control
1964  * @ucontrol: control element information
1965  *
1966  * Callback to get the value of a signed mixer control.
1967  *
1968  * Returns 0 for success.
1969  */
1970 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1971         struct snd_ctl_elem_value *ucontrol)
1972 {
1973         struct soc_mixer_control *mc =
1974                 (struct soc_mixer_control *)kcontrol->private_value;
1975         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1976         unsigned int reg = mc->reg;
1977         int min = mc->min;
1978         int val = snd_soc_read(codec, reg);
1979
1980         ucontrol->value.integer.value[0] =
1981                 ((signed char)(val & 0xff))-min;
1982         ucontrol->value.integer.value[1] =
1983                 ((signed char)((val >> 8) & 0xff))-min;
1984         return 0;
1985 }
1986 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1987
1988 /**
1989  * snd_soc_put_volsw_sgn - signed mixer put callback
1990  * @kcontrol: mixer control
1991  * @ucontrol: control element information
1992  *
1993  * Callback to set the value of a signed mixer control.
1994  *
1995  * Returns 0 for success.
1996  */
1997 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1998         struct snd_ctl_elem_value *ucontrol)
1999 {
2000         struct soc_mixer_control *mc =
2001                 (struct soc_mixer_control *)kcontrol->private_value;
2002         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2003         unsigned int reg = mc->reg;
2004         int min = mc->min;
2005         unsigned short val;
2006
2007         val = (ucontrol->value.integer.value[0]+min) & 0xff;
2008         val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2009
2010         return snd_soc_update_bits(codec, reg, 0xffff, val);
2011 }
2012 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2013
2014 /**
2015  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2016  * @dai: DAI
2017  * @clk_id: DAI specific clock ID
2018  * @freq: new clock frequency in Hz
2019  * @dir: new clock direction - input/output.
2020  *
2021  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2022  */
2023 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2024         unsigned int freq, int dir)
2025 {
2026         if (dai->ops.set_sysclk)
2027                 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
2028         else
2029                 return -EINVAL;
2030 }
2031 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2032
2033 /**
2034  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2035  * @dai: DAI
2036  * @div_id: DAI specific clock divider ID
2037  * @div: new clock divisor.
2038  *
2039  * Configures the clock dividers. This is used to derive the best DAI bit and
2040  * frame clocks from the system or master clock. It's best to set the DAI bit
2041  * and frame clocks as low as possible to save system power.
2042  */
2043 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2044         int div_id, int div)
2045 {
2046         if (dai->ops.set_clkdiv)
2047                 return dai->ops.set_clkdiv(dai, div_id, div);
2048         else
2049                 return -EINVAL;
2050 }
2051 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2052
2053 /**
2054  * snd_soc_dai_set_pll - configure DAI PLL.
2055  * @dai: DAI
2056  * @pll_id: DAI specific PLL ID
2057  * @freq_in: PLL input clock frequency in Hz
2058  * @freq_out: requested PLL output clock frequency in Hz
2059  *
2060  * Configures and enables PLL to generate output clock based on input clock.
2061  */
2062 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
2063         int pll_id, unsigned int freq_in, unsigned int freq_out)
2064 {
2065         if (dai->ops.set_pll)
2066                 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
2067         else
2068                 return -EINVAL;
2069 }
2070 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2071
2072 /**
2073  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2074  * @dai: DAI
2075  * @fmt: SND_SOC_DAIFMT_ format value.
2076  *
2077  * Configures the DAI hardware format and clocking.
2078  */
2079 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2080 {
2081         if (dai->ops.set_fmt)
2082                 return dai->ops.set_fmt(dai, fmt);
2083         else
2084                 return -EINVAL;
2085 }
2086 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2087
2088 /**
2089  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2090  * @dai: DAI
2091  * @mask: DAI specific mask representing used slots.
2092  * @slots: Number of slots in use.
2093  *
2094  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2095  * specific.
2096  */
2097 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2098         unsigned int mask, int slots)
2099 {
2100         if (dai->ops.set_sysclk)
2101                 return dai->ops.set_tdm_slot(dai, mask, slots);
2102         else
2103                 return -EINVAL;
2104 }
2105 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2106
2107 /**
2108  * snd_soc_dai_set_tristate - configure DAI system or master clock.
2109  * @dai: DAI
2110  * @tristate: tristate enable
2111  *
2112  * Tristates the DAI so that others can use it.
2113  */
2114 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2115 {
2116         if (dai->ops.set_sysclk)
2117                 return dai->ops.set_tristate(dai, tristate);
2118         else
2119                 return -EINVAL;
2120 }
2121 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2122
2123 /**
2124  * snd_soc_dai_digital_mute - configure DAI system or master clock.
2125  * @dai: DAI
2126  * @mute: mute enable
2127  *
2128  * Mutes the DAI DAC.
2129  */
2130 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2131 {
2132         if (dai->ops.digital_mute)
2133                 return dai->ops.digital_mute(dai, mute);
2134         else
2135                 return -EINVAL;
2136 }
2137 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2138
2139 /**
2140  * snd_soc_register_card - Register a card with the ASoC core
2141  *
2142  * @card: Card to register
2143  *
2144  * Note that currently this is an internal only function: it will be
2145  * exposed to machine drivers after further backporting of ASoC v2
2146  * registration APIs.
2147  */
2148 static int snd_soc_register_card(struct snd_soc_card *card)
2149 {
2150         if (!card->name || !card->dev)
2151                 return -EINVAL;
2152
2153         INIT_LIST_HEAD(&card->list);
2154         card->instantiated = 0;
2155
2156         mutex_lock(&client_mutex);
2157         list_add(&card->list, &card_list);
2158         snd_soc_instantiate_cards();
2159         mutex_unlock(&client_mutex);
2160
2161         dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2162
2163         return 0;
2164 }
2165
2166 /**
2167  * snd_soc_unregister_card - Unregister a card with the ASoC core
2168  *
2169  * @card: Card to unregister
2170  *
2171  * Note that currently this is an internal only function: it will be
2172  * exposed to machine drivers after further backporting of ASoC v2
2173  * registration APIs.
2174  */
2175 static int snd_soc_unregister_card(struct snd_soc_card *card)
2176 {
2177         mutex_lock(&client_mutex);
2178         list_del(&card->list);
2179         mutex_unlock(&client_mutex);
2180
2181         dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2182
2183         return 0;
2184 }
2185
2186 /**
2187  * snd_soc_register_dai - Register a DAI with the ASoC core
2188  *
2189  * @dai: DAI to register
2190  */
2191 int snd_soc_register_dai(struct snd_soc_dai *dai)
2192 {
2193         if (!dai->name)
2194                 return -EINVAL;
2195
2196         /* The device should become mandatory over time */
2197         if (!dai->dev)
2198                 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2199
2200         INIT_LIST_HEAD(&dai->list);
2201
2202         mutex_lock(&client_mutex);
2203         list_add(&dai->list, &dai_list);
2204         snd_soc_instantiate_cards();
2205         mutex_unlock(&client_mutex);
2206
2207         pr_debug("Registered DAI '%s'\n", dai->name);
2208
2209         return 0;
2210 }
2211 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2212
2213 /**
2214  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2215  *
2216  * @dai: DAI to unregister
2217  */
2218 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2219 {
2220         mutex_lock(&client_mutex);
2221         list_del(&dai->list);
2222         mutex_unlock(&client_mutex);
2223
2224         pr_debug("Unregistered DAI '%s'\n", dai->name);
2225 }
2226 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2227
2228 /**
2229  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2230  *
2231  * @dai: Array of DAIs to register
2232  * @count: Number of DAIs
2233  */
2234 int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2235 {
2236         int i, ret;
2237
2238         for (i = 0; i < count; i++) {
2239                 ret = snd_soc_register_dai(&dai[i]);
2240                 if (ret != 0)
2241                         goto err;
2242         }
2243
2244         return 0;
2245
2246 err:
2247         for (i--; i >= 0; i--)
2248                 snd_soc_unregister_dai(&dai[i]);
2249
2250         return ret;
2251 }
2252 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2253
2254 /**
2255  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2256  *
2257  * @dai: Array of DAIs to unregister
2258  * @count: Number of DAIs
2259  */
2260 void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2261 {
2262         int i;
2263
2264         for (i = 0; i < count; i++)
2265                 snd_soc_unregister_dai(&dai[i]);
2266 }
2267 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2268
2269 /**
2270  * snd_soc_register_platform - Register a platform with the ASoC core
2271  *
2272  * @platform: platform to register
2273  */
2274 int snd_soc_register_platform(struct snd_soc_platform *platform)
2275 {
2276         if (!platform->name)
2277                 return -EINVAL;
2278
2279         INIT_LIST_HEAD(&platform->list);
2280
2281         mutex_lock(&client_mutex);
2282         list_add(&platform->list, &platform_list);
2283         snd_soc_instantiate_cards();
2284         mutex_unlock(&client_mutex);
2285
2286         pr_debug("Registered platform '%s'\n", platform->name);
2287
2288         return 0;
2289 }
2290 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2291
2292 /**
2293  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2294  *
2295  * @platform: platform to unregister
2296  */
2297 void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2298 {
2299         mutex_lock(&client_mutex);
2300         list_del(&platform->list);
2301         mutex_unlock(&client_mutex);
2302
2303         pr_debug("Unregistered platform '%s'\n", platform->name);
2304 }
2305 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2306
2307 /**
2308  * snd_soc_register_codec - Register a codec with the ASoC core
2309  *
2310  * @codec: codec to register
2311  */
2312 int snd_soc_register_codec(struct snd_soc_codec *codec)
2313 {
2314         if (!codec->name)
2315                 return -EINVAL;
2316
2317         /* The device should become mandatory over time */
2318         if (!codec->dev)
2319                 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2320
2321         INIT_LIST_HEAD(&codec->list);
2322
2323         mutex_lock(&client_mutex);
2324         list_add(&codec->list, &codec_list);
2325         snd_soc_instantiate_cards();
2326         mutex_unlock(&client_mutex);
2327
2328         pr_debug("Registered codec '%s'\n", codec->name);
2329
2330         return 0;
2331 }
2332 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2333
2334 /**
2335  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2336  *
2337  * @codec: codec to unregister
2338  */
2339 void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2340 {
2341         mutex_lock(&client_mutex);
2342         list_del(&codec->list);
2343         mutex_unlock(&client_mutex);
2344
2345         pr_debug("Unregistered codec '%s'\n", codec->name);
2346 }
2347 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2348
2349 static int __init snd_soc_init(void)
2350 {
2351 #ifdef CONFIG_DEBUG_FS
2352         debugfs_root = debugfs_create_dir("asoc", NULL);
2353         if (IS_ERR(debugfs_root) || !debugfs_root) {
2354                 printk(KERN_WARNING
2355                        "ASoC: Failed to create debugfs directory\n");
2356                 debugfs_root = NULL;
2357         }
2358 #endif
2359
2360         return platform_driver_register(&soc_driver);
2361 }
2362
2363 static void __exit snd_soc_exit(void)
2364 {
2365 #ifdef CONFIG_DEBUG_FS
2366         debugfs_remove_recursive(debugfs_root);
2367 #endif
2368         platform_driver_unregister(&soc_driver);
2369 }
2370
2371 module_init(snd_soc_init);
2372 module_exit(snd_soc_exit);
2373
2374 /* Module information */
2375 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2376 MODULE_DESCRIPTION("ALSA SoC Core");
2377 MODULE_LICENSE("GPL");
2378 MODULE_ALIAS("platform:soc-audio");