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