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