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