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