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