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