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