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