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