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