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