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