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