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