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