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