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