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