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