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