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