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