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