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