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