Merge commit 'v2.6.34-rc1' into for-2.6.35
[linux-2.6/btrfs-unstable.git] / sound / soc / soc-core.c
blob06c38d1502b76510f26cb89cdda51004f70b9304
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
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.
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
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/pm.h>
28 #include <linux/bitops.h>
29 #include <linux/debugfs.h>
30 #include <linux/platform_device.h>
31 #include <sound/ac97_codec.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/soc-dapm.h>
37 #include <sound/initval.h>
39 static DEFINE_MUTEX(pcm_mutex);
40 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42 #ifdef CONFIG_DEBUG_FS
43 static struct dentry *debugfs_root;
44 #endif
46 static DEFINE_MUTEX(client_mutex);
47 static LIST_HEAD(card_list);
48 static LIST_HEAD(dai_list);
49 static LIST_HEAD(platform_list);
50 static LIST_HEAD(codec_list);
52 static int snd_soc_register_card(struct snd_soc_card *card);
53 static int snd_soc_unregister_card(struct snd_soc_card *card);
56 * This is a timeout to do a DAPM powerdown after a stream is closed().
57 * It can be used to eliminate pops between different playback streams, e.g.
58 * between two audio tracks.
60 static int pmdown_time = 5000;
61 module_param(pmdown_time, int, 0);
62 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
65 * This function forces any delayed work to be queued and run.
67 static int run_delayed_work(struct delayed_work *dwork)
69 int ret;
71 /* cancel any work waiting to be queued. */
72 ret = cancel_delayed_work(dwork);
74 /* if there was any work waiting then we run it now and
75 * wait for it's completion */
76 if (ret) {
77 schedule_delayed_work(dwork, 0);
78 flush_scheduled_work();
80 return ret;
83 /* codec register dump */
84 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
86 int i, step = 1, count = 0;
88 if (!codec->reg_cache_size)
89 return 0;
91 if (codec->reg_cache_step)
92 step = codec->reg_cache_step;
94 count += sprintf(buf, "%s registers\n", codec->name);
95 for (i = 0; i < codec->reg_cache_size; i += step) {
96 if (codec->readable_register && !codec->readable_register(i))
97 continue;
99 count += sprintf(buf + count, "%2x: ", i);
100 if (count >= PAGE_SIZE - 1)
101 break;
103 if (codec->display_register)
104 count += codec->display_register(codec, buf + count,
105 PAGE_SIZE - count, i);
106 else
107 count += snprintf(buf + count, PAGE_SIZE - count,
108 "%4x", codec->read(codec, i));
110 if (count >= PAGE_SIZE - 1)
111 break;
113 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
114 if (count >= PAGE_SIZE - 1)
115 break;
118 /* Truncate count; min() would cause a warning */
119 if (count >= PAGE_SIZE)
120 count = PAGE_SIZE - 1;
122 return count;
124 static ssize_t codec_reg_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
127 struct snd_soc_device *devdata = dev_get_drvdata(dev);
128 return soc_codec_reg_show(devdata->card->codec, buf);
131 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
133 static ssize_t pmdown_time_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 struct snd_soc_device *socdev = dev_get_drvdata(dev);
137 struct snd_soc_card *card = socdev->card;
139 return sprintf(buf, "%ld\n", card->pmdown_time);
142 static ssize_t pmdown_time_set(struct device *dev,
143 struct device_attribute *attr,
144 const char *buf, size_t count)
146 struct snd_soc_device *socdev = dev_get_drvdata(dev);
147 struct snd_soc_card *card = socdev->card;
149 strict_strtol(buf, 10, &card->pmdown_time);
151 return count;
154 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
156 #ifdef CONFIG_DEBUG_FS
157 static int codec_reg_open_file(struct inode *inode, struct file *file)
159 file->private_data = inode->i_private;
160 return 0;
163 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
164 size_t count, loff_t *ppos)
166 ssize_t ret;
167 struct snd_soc_codec *codec = file->private_data;
168 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
169 if (!buf)
170 return -ENOMEM;
171 ret = soc_codec_reg_show(codec, buf);
172 if (ret >= 0)
173 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
174 kfree(buf);
175 return ret;
178 static ssize_t codec_reg_write_file(struct file *file,
179 const char __user *user_buf, size_t count, loff_t *ppos)
181 char buf[32];
182 int buf_size;
183 char *start = buf;
184 unsigned long reg, value;
185 int step = 1;
186 struct snd_soc_codec *codec = file->private_data;
188 buf_size = min(count, (sizeof(buf)-1));
189 if (copy_from_user(buf, user_buf, buf_size))
190 return -EFAULT;
191 buf[buf_size] = 0;
193 if (codec->reg_cache_step)
194 step = codec->reg_cache_step;
196 while (*start == ' ')
197 start++;
198 reg = simple_strtoul(start, &start, 16);
199 if ((reg >= codec->reg_cache_size) || (reg % step))
200 return -EINVAL;
201 while (*start == ' ')
202 start++;
203 if (strict_strtoul(start, 16, &value))
204 return -EINVAL;
205 codec->write(codec, reg, value);
206 return buf_size;
209 static const struct file_operations codec_reg_fops = {
210 .open = codec_reg_open_file,
211 .read = codec_reg_read_file,
212 .write = codec_reg_write_file,
215 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
217 char codec_root[128];
219 if (codec->dev)
220 snprintf(codec_root, sizeof(codec_root),
221 "%s.%s", codec->name, dev_name(codec->dev));
222 else
223 snprintf(codec_root, sizeof(codec_root),
224 "%s", codec->name);
226 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
227 debugfs_root);
228 if (!codec->debugfs_codec_root) {
229 printk(KERN_WARNING
230 "ASoC: Failed to create codec debugfs directory\n");
231 return;
234 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
235 codec->debugfs_codec_root,
236 codec, &codec_reg_fops);
237 if (!codec->debugfs_reg)
238 printk(KERN_WARNING
239 "ASoC: Failed to create codec register debugfs file\n");
241 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
242 codec->debugfs_codec_root,
243 &codec->pop_time);
244 if (!codec->debugfs_pop_time)
245 printk(KERN_WARNING
246 "Failed to create pop time debugfs file\n");
248 codec->debugfs_dapm = debugfs_create_dir("dapm",
249 codec->debugfs_codec_root);
250 if (!codec->debugfs_dapm)
251 printk(KERN_WARNING
252 "Failed to create DAPM debugfs directory\n");
254 snd_soc_dapm_debugfs_init(codec);
257 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
259 debugfs_remove_recursive(codec->debugfs_codec_root);
262 #else
264 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
271 #endif
273 #ifdef CONFIG_SND_SOC_AC97_BUS
274 /* unregister ac97 codec */
275 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
277 if (codec->ac97->dev.bus)
278 device_unregister(&codec->ac97->dev);
279 return 0;
282 /* stop no dev release warning */
283 static void soc_ac97_device_release(struct device *dev){}
285 /* register ac97 codec to bus */
286 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
288 int err;
290 codec->ac97->dev.bus = &ac97_bus_type;
291 codec->ac97->dev.parent = codec->card->dev;
292 codec->ac97->dev.release = soc_ac97_device_release;
294 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
295 codec->card->number, 0, codec->name);
296 err = device_register(&codec->ac97->dev);
297 if (err < 0) {
298 snd_printk(KERN_ERR "Can't register ac97 bus\n");
299 codec->ac97->dev.bus = NULL;
300 return err;
302 return 0;
304 #endif
306 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
308 struct snd_soc_pcm_runtime *rtd = substream->private_data;
309 struct snd_soc_device *socdev = rtd->socdev;
310 struct snd_soc_card *card = socdev->card;
311 struct snd_soc_dai_link *machine = rtd->dai;
312 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
313 struct snd_soc_dai *codec_dai = machine->codec_dai;
314 int ret;
316 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
317 machine->symmetric_rates) {
318 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
319 machine->rate);
321 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
322 SNDRV_PCM_HW_PARAM_RATE,
323 machine->rate,
324 machine->rate);
325 if (ret < 0) {
326 dev_err(card->dev,
327 "Unable to apply rate symmetry constraint: %d\n", ret);
328 return ret;
332 return 0;
336 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
337 * then initialized and any private data can be allocated. This also calls
338 * startup for the cpu DAI, platform, machine and codec DAI.
340 static int soc_pcm_open(struct snd_pcm_substream *substream)
342 struct snd_soc_pcm_runtime *rtd = substream->private_data;
343 struct snd_soc_device *socdev = rtd->socdev;
344 struct snd_soc_card *card = socdev->card;
345 struct snd_pcm_runtime *runtime = substream->runtime;
346 struct snd_soc_dai_link *machine = rtd->dai;
347 struct snd_soc_platform *platform = card->platform;
348 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
349 struct snd_soc_dai *codec_dai = machine->codec_dai;
350 int ret = 0;
352 mutex_lock(&pcm_mutex);
354 /* startup the audio subsystem */
355 if (cpu_dai->ops->startup) {
356 ret = cpu_dai->ops->startup(substream, cpu_dai);
357 if (ret < 0) {
358 printk(KERN_ERR "asoc: can't open interface %s\n",
359 cpu_dai->name);
360 goto out;
364 if (platform->pcm_ops->open) {
365 ret = platform->pcm_ops->open(substream);
366 if (ret < 0) {
367 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
368 goto platform_err;
372 if (codec_dai->ops->startup) {
373 ret = codec_dai->ops->startup(substream, codec_dai);
374 if (ret < 0) {
375 printk(KERN_ERR "asoc: can't open codec %s\n",
376 codec_dai->name);
377 goto codec_dai_err;
381 if (machine->ops && machine->ops->startup) {
382 ret = machine->ops->startup(substream);
383 if (ret < 0) {
384 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
385 goto machine_err;
389 /* Check that the codec and cpu DAI's are compatible */
390 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
391 runtime->hw.rate_min =
392 max(codec_dai->playback.rate_min,
393 cpu_dai->playback.rate_min);
394 runtime->hw.rate_max =
395 min(codec_dai->playback.rate_max,
396 cpu_dai->playback.rate_max);
397 runtime->hw.channels_min =
398 max(codec_dai->playback.channels_min,
399 cpu_dai->playback.channels_min);
400 runtime->hw.channels_max =
401 min(codec_dai->playback.channels_max,
402 cpu_dai->playback.channels_max);
403 runtime->hw.formats =
404 codec_dai->playback.formats & cpu_dai->playback.formats;
405 runtime->hw.rates =
406 codec_dai->playback.rates & cpu_dai->playback.rates;
407 } else {
408 runtime->hw.rate_min =
409 max(codec_dai->capture.rate_min,
410 cpu_dai->capture.rate_min);
411 runtime->hw.rate_max =
412 min(codec_dai->capture.rate_max,
413 cpu_dai->capture.rate_max);
414 runtime->hw.channels_min =
415 max(codec_dai->capture.channels_min,
416 cpu_dai->capture.channels_min);
417 runtime->hw.channels_max =
418 min(codec_dai->capture.channels_max,
419 cpu_dai->capture.channels_max);
420 runtime->hw.formats =
421 codec_dai->capture.formats & cpu_dai->capture.formats;
422 runtime->hw.rates =
423 codec_dai->capture.rates & cpu_dai->capture.rates;
426 snd_pcm_limit_hw_rates(runtime);
427 if (!runtime->hw.rates) {
428 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
429 codec_dai->name, cpu_dai->name);
430 goto config_err;
432 if (!runtime->hw.formats) {
433 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
434 codec_dai->name, cpu_dai->name);
435 goto config_err;
437 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
438 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
439 codec_dai->name, cpu_dai->name);
440 goto config_err;
443 /* Symmetry only applies if we've already got an active stream. */
444 if (cpu_dai->active || codec_dai->active) {
445 ret = soc_pcm_apply_symmetry(substream);
446 if (ret != 0)
447 goto config_err;
450 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
451 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
452 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
453 runtime->hw.channels_max);
454 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
455 runtime->hw.rate_max);
457 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
458 cpu_dai->playback.active++;
459 codec_dai->playback.active++;
460 } else {
461 cpu_dai->capture.active++;
462 codec_dai->capture.active++;
464 cpu_dai->active++;
465 codec_dai->active++;
466 card->codec->active++;
467 mutex_unlock(&pcm_mutex);
468 return 0;
470 config_err:
471 if (machine->ops && machine->ops->shutdown)
472 machine->ops->shutdown(substream);
474 machine_err:
475 if (codec_dai->ops->shutdown)
476 codec_dai->ops->shutdown(substream, codec_dai);
478 codec_dai_err:
479 if (platform->pcm_ops->close)
480 platform->pcm_ops->close(substream);
482 platform_err:
483 if (cpu_dai->ops->shutdown)
484 cpu_dai->ops->shutdown(substream, cpu_dai);
485 out:
486 mutex_unlock(&pcm_mutex);
487 return ret;
491 * Power down the audio subsystem pmdown_time msecs after close is called.
492 * This is to ensure there are no pops or clicks in between any music tracks
493 * due to DAPM power cycling.
495 static void close_delayed_work(struct work_struct *work)
497 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
498 delayed_work.work);
499 struct snd_soc_codec *codec = card->codec;
500 struct snd_soc_dai *codec_dai;
501 int i;
503 mutex_lock(&pcm_mutex);
504 for (i = 0; i < codec->num_dai; i++) {
505 codec_dai = &codec->dai[i];
507 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
508 codec_dai->playback.stream_name,
509 codec_dai->playback.active ? "active" : "inactive",
510 codec_dai->pop_wait ? "yes" : "no");
512 /* are we waiting on this codec DAI stream */
513 if (codec_dai->pop_wait == 1) {
514 codec_dai->pop_wait = 0;
515 snd_soc_dapm_stream_event(codec,
516 codec_dai->playback.stream_name,
517 SND_SOC_DAPM_STREAM_STOP);
520 mutex_unlock(&pcm_mutex);
524 * Called by ALSA when a PCM substream is closed. Private data can be
525 * freed here. The cpu DAI, codec DAI, machine and platform are also
526 * shutdown.
528 static int soc_codec_close(struct snd_pcm_substream *substream)
530 struct snd_soc_pcm_runtime *rtd = substream->private_data;
531 struct snd_soc_device *socdev = rtd->socdev;
532 struct snd_soc_card *card = socdev->card;
533 struct snd_soc_dai_link *machine = rtd->dai;
534 struct snd_soc_platform *platform = card->platform;
535 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
536 struct snd_soc_dai *codec_dai = machine->codec_dai;
537 struct snd_soc_codec *codec = card->codec;
539 mutex_lock(&pcm_mutex);
541 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
542 cpu_dai->playback.active--;
543 codec_dai->playback.active--;
544 } else {
545 cpu_dai->capture.active--;
546 codec_dai->capture.active--;
549 cpu_dai->active--;
550 codec_dai->active--;
551 codec->active--;
553 /* Muting the DAC suppresses artifacts caused during digital
554 * shutdown, for example from stopping clocks.
556 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
557 snd_soc_dai_digital_mute(codec_dai, 1);
559 if (cpu_dai->ops->shutdown)
560 cpu_dai->ops->shutdown(substream, cpu_dai);
562 if (codec_dai->ops->shutdown)
563 codec_dai->ops->shutdown(substream, codec_dai);
565 if (machine->ops && machine->ops->shutdown)
566 machine->ops->shutdown(substream);
568 if (platform->pcm_ops->close)
569 platform->pcm_ops->close(substream);
571 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
572 /* start delayed pop wq here for playback streams */
573 codec_dai->pop_wait = 1;
574 schedule_delayed_work(&card->delayed_work,
575 msecs_to_jiffies(card->pmdown_time));
576 } else {
577 /* capture streams can be powered down now */
578 snd_soc_dapm_stream_event(codec,
579 codec_dai->capture.stream_name,
580 SND_SOC_DAPM_STREAM_STOP);
583 mutex_unlock(&pcm_mutex);
584 return 0;
588 * Called by ALSA when the PCM substream is prepared, can set format, sample
589 * rate, etc. This function is non atomic and can be called multiple times,
590 * it can refer to the runtime info.
592 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
594 struct snd_soc_pcm_runtime *rtd = substream->private_data;
595 struct snd_soc_device *socdev = rtd->socdev;
596 struct snd_soc_card *card = socdev->card;
597 struct snd_soc_dai_link *machine = rtd->dai;
598 struct snd_soc_platform *platform = card->platform;
599 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
600 struct snd_soc_dai *codec_dai = machine->codec_dai;
601 struct snd_soc_codec *codec = card->codec;
602 int ret = 0;
604 mutex_lock(&pcm_mutex);
606 if (machine->ops && machine->ops->prepare) {
607 ret = machine->ops->prepare(substream);
608 if (ret < 0) {
609 printk(KERN_ERR "asoc: machine prepare error\n");
610 goto out;
614 if (platform->pcm_ops->prepare) {
615 ret = platform->pcm_ops->prepare(substream);
616 if (ret < 0) {
617 printk(KERN_ERR "asoc: platform prepare error\n");
618 goto out;
622 if (codec_dai->ops->prepare) {
623 ret = codec_dai->ops->prepare(substream, codec_dai);
624 if (ret < 0) {
625 printk(KERN_ERR "asoc: codec DAI prepare error\n");
626 goto out;
630 if (cpu_dai->ops->prepare) {
631 ret = cpu_dai->ops->prepare(substream, cpu_dai);
632 if (ret < 0) {
633 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
634 goto out;
638 /* cancel any delayed stream shutdown that is pending */
639 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
640 codec_dai->pop_wait) {
641 codec_dai->pop_wait = 0;
642 cancel_delayed_work(&card->delayed_work);
645 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
646 snd_soc_dapm_stream_event(codec,
647 codec_dai->playback.stream_name,
648 SND_SOC_DAPM_STREAM_START);
649 else
650 snd_soc_dapm_stream_event(codec,
651 codec_dai->capture.stream_name,
652 SND_SOC_DAPM_STREAM_START);
654 snd_soc_dai_digital_mute(codec_dai, 0);
656 out:
657 mutex_unlock(&pcm_mutex);
658 return ret;
662 * Called by ALSA when the hardware params are set by application. This
663 * function can also be called multiple times and can allocate buffers
664 * (using snd_pcm_lib_* ). It's non-atomic.
666 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
667 struct snd_pcm_hw_params *params)
669 struct snd_soc_pcm_runtime *rtd = substream->private_data;
670 struct snd_soc_device *socdev = rtd->socdev;
671 struct snd_soc_dai_link *machine = rtd->dai;
672 struct snd_soc_card *card = socdev->card;
673 struct snd_soc_platform *platform = card->platform;
674 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
675 struct snd_soc_dai *codec_dai = machine->codec_dai;
676 int ret = 0;
678 mutex_lock(&pcm_mutex);
680 if (machine->ops && machine->ops->hw_params) {
681 ret = machine->ops->hw_params(substream, params);
682 if (ret < 0) {
683 printk(KERN_ERR "asoc: machine hw_params failed\n");
684 goto out;
688 if (codec_dai->ops->hw_params) {
689 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
690 if (ret < 0) {
691 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
692 codec_dai->name);
693 goto codec_err;
697 if (cpu_dai->ops->hw_params) {
698 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
699 if (ret < 0) {
700 printk(KERN_ERR "asoc: interface %s hw params failed\n",
701 cpu_dai->name);
702 goto interface_err;
706 if (platform->pcm_ops->hw_params) {
707 ret = platform->pcm_ops->hw_params(substream, params);
708 if (ret < 0) {
709 printk(KERN_ERR "asoc: platform %s hw params failed\n",
710 platform->name);
711 goto platform_err;
715 machine->rate = params_rate(params);
717 out:
718 mutex_unlock(&pcm_mutex);
719 return ret;
721 platform_err:
722 if (cpu_dai->ops->hw_free)
723 cpu_dai->ops->hw_free(substream, cpu_dai);
725 interface_err:
726 if (codec_dai->ops->hw_free)
727 codec_dai->ops->hw_free(substream, codec_dai);
729 codec_err:
730 if (machine->ops && machine->ops->hw_free)
731 machine->ops->hw_free(substream);
733 mutex_unlock(&pcm_mutex);
734 return ret;
738 * Free's resources allocated by hw_params, can be called multiple times
740 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
742 struct snd_soc_pcm_runtime *rtd = substream->private_data;
743 struct snd_soc_device *socdev = rtd->socdev;
744 struct snd_soc_dai_link *machine = rtd->dai;
745 struct snd_soc_card *card = socdev->card;
746 struct snd_soc_platform *platform = card->platform;
747 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
748 struct snd_soc_dai *codec_dai = machine->codec_dai;
749 struct snd_soc_codec *codec = card->codec;
751 mutex_lock(&pcm_mutex);
753 /* apply codec digital mute */
754 if (!codec->active)
755 snd_soc_dai_digital_mute(codec_dai, 1);
757 /* free any machine hw params */
758 if (machine->ops && machine->ops->hw_free)
759 machine->ops->hw_free(substream);
761 /* free any DMA resources */
762 if (platform->pcm_ops->hw_free)
763 platform->pcm_ops->hw_free(substream);
765 /* now free hw params for the DAI's */
766 if (codec_dai->ops->hw_free)
767 codec_dai->ops->hw_free(substream, codec_dai);
769 if (cpu_dai->ops->hw_free)
770 cpu_dai->ops->hw_free(substream, cpu_dai);
772 mutex_unlock(&pcm_mutex);
773 return 0;
776 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
778 struct snd_soc_pcm_runtime *rtd = substream->private_data;
779 struct snd_soc_device *socdev = rtd->socdev;
780 struct snd_soc_card *card= socdev->card;
781 struct snd_soc_dai_link *machine = rtd->dai;
782 struct snd_soc_platform *platform = card->platform;
783 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
784 struct snd_soc_dai *codec_dai = machine->codec_dai;
785 int ret;
787 if (codec_dai->ops->trigger) {
788 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
789 if (ret < 0)
790 return ret;
793 if (platform->pcm_ops->trigger) {
794 ret = platform->pcm_ops->trigger(substream, cmd);
795 if (ret < 0)
796 return ret;
799 if (cpu_dai->ops->trigger) {
800 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
801 if (ret < 0)
802 return ret;
804 return 0;
808 * soc level wrapper for pointer callback
809 * If cpu_dai, codec_dai, platform driver has the delay callback, than
810 * the runtime->delay will be updated accordingly.
812 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
814 struct snd_soc_pcm_runtime *rtd = substream->private_data;
815 struct snd_soc_device *socdev = rtd->socdev;
816 struct snd_soc_card *card = socdev->card;
817 struct snd_soc_platform *platform = card->platform;
818 struct snd_soc_dai_link *machine = rtd->dai;
819 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
820 struct snd_soc_dai *codec_dai = machine->codec_dai;
821 struct snd_pcm_runtime *runtime = substream->runtime;
822 snd_pcm_uframes_t offset = 0;
823 snd_pcm_sframes_t delay = 0;
825 if (platform->pcm_ops->pointer)
826 offset = platform->pcm_ops->pointer(substream);
828 if (cpu_dai->ops->delay)
829 delay += cpu_dai->ops->delay(substream, cpu_dai);
831 if (codec_dai->ops->delay)
832 delay += codec_dai->ops->delay(substream, codec_dai);
834 if (platform->delay)
835 delay += platform->delay(substream, codec_dai);
837 runtime->delay = delay;
839 return offset;
842 /* ASoC PCM operations */
843 static struct snd_pcm_ops soc_pcm_ops = {
844 .open = soc_pcm_open,
845 .close = soc_codec_close,
846 .hw_params = soc_pcm_hw_params,
847 .hw_free = soc_pcm_hw_free,
848 .prepare = soc_pcm_prepare,
849 .trigger = soc_pcm_trigger,
850 .pointer = soc_pcm_pointer,
853 #ifdef CONFIG_PM
854 /* powers down audio subsystem for suspend */
855 static int soc_suspend(struct device *dev)
857 struct platform_device *pdev = to_platform_device(dev);
858 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
859 struct snd_soc_card *card = socdev->card;
860 struct snd_soc_platform *platform = card->platform;
861 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
862 struct snd_soc_codec *codec = card->codec;
863 int i;
865 /* If the initialization of this soc device failed, there is no codec
866 * associated with it. Just bail out in this case.
868 if (!codec)
869 return 0;
871 /* Due to the resume being scheduled into a workqueue we could
872 * suspend before that's finished - wait for it to complete.
874 snd_power_lock(codec->card);
875 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
876 snd_power_unlock(codec->card);
878 /* we're going to block userspace touching us until resume completes */
879 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
881 /* mute any active DAC's */
882 for (i = 0; i < card->num_links; i++) {
883 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
884 if (dai->ops->digital_mute && dai->playback.active)
885 dai->ops->digital_mute(dai, 1);
888 /* suspend all pcms */
889 for (i = 0; i < card->num_links; i++)
890 snd_pcm_suspend_all(card->dai_link[i].pcm);
892 if (card->suspend_pre)
893 card->suspend_pre(pdev, PMSG_SUSPEND);
895 for (i = 0; i < card->num_links; i++) {
896 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
897 if (cpu_dai->suspend && !cpu_dai->ac97_control)
898 cpu_dai->suspend(cpu_dai);
899 if (platform->suspend)
900 platform->suspend(&card->dai_link[i]);
903 /* close any waiting streams and save state */
904 run_delayed_work(&card->delayed_work);
905 codec->suspend_bias_level = codec->bias_level;
907 for (i = 0; i < codec->num_dai; i++) {
908 char *stream = codec->dai[i].playback.stream_name;
909 if (stream != NULL)
910 snd_soc_dapm_stream_event(codec, stream,
911 SND_SOC_DAPM_STREAM_SUSPEND);
912 stream = codec->dai[i].capture.stream_name;
913 if (stream != NULL)
914 snd_soc_dapm_stream_event(codec, stream,
915 SND_SOC_DAPM_STREAM_SUSPEND);
918 if (codec_dev->suspend)
919 codec_dev->suspend(pdev, PMSG_SUSPEND);
921 for (i = 0; i < card->num_links; i++) {
922 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
923 if (cpu_dai->suspend && cpu_dai->ac97_control)
924 cpu_dai->suspend(cpu_dai);
927 if (card->suspend_post)
928 card->suspend_post(pdev, PMSG_SUSPEND);
930 return 0;
933 /* deferred resume work, so resume can complete before we finished
934 * setting our codec back up, which can be very slow on I2C
936 static void soc_resume_deferred(struct work_struct *work)
938 struct snd_soc_card *card = container_of(work,
939 struct snd_soc_card,
940 deferred_resume_work);
941 struct snd_soc_device *socdev = card->socdev;
942 struct snd_soc_platform *platform = card->platform;
943 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
944 struct snd_soc_codec *codec = card->codec;
945 struct platform_device *pdev = to_platform_device(socdev->dev);
946 int i;
948 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
949 * so userspace apps are blocked from touching us
952 dev_dbg(socdev->dev, "starting resume work\n");
954 if (card->resume_pre)
955 card->resume_pre(pdev);
957 for (i = 0; i < card->num_links; i++) {
958 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
959 if (cpu_dai->resume && cpu_dai->ac97_control)
960 cpu_dai->resume(cpu_dai);
963 if (codec_dev->resume)
964 codec_dev->resume(pdev);
966 for (i = 0; i < codec->num_dai; i++) {
967 char *stream = codec->dai[i].playback.stream_name;
968 if (stream != NULL)
969 snd_soc_dapm_stream_event(codec, stream,
970 SND_SOC_DAPM_STREAM_RESUME);
971 stream = codec->dai[i].capture.stream_name;
972 if (stream != NULL)
973 snd_soc_dapm_stream_event(codec, stream,
974 SND_SOC_DAPM_STREAM_RESUME);
977 /* unmute any active DACs */
978 for (i = 0; i < card->num_links; i++) {
979 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
980 if (dai->ops->digital_mute && dai->playback.active)
981 dai->ops->digital_mute(dai, 0);
984 for (i = 0; i < card->num_links; i++) {
985 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
986 if (cpu_dai->resume && !cpu_dai->ac97_control)
987 cpu_dai->resume(cpu_dai);
988 if (platform->resume)
989 platform->resume(&card->dai_link[i]);
992 if (card->resume_post)
993 card->resume_post(pdev);
995 dev_dbg(socdev->dev, "resume work completed\n");
997 /* userspace can access us now we are back as we were before */
998 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
1001 /* powers up audio subsystem after a suspend */
1002 static int soc_resume(struct device *dev)
1004 struct platform_device *pdev = to_platform_device(dev);
1005 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1006 struct snd_soc_card *card = socdev->card;
1007 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
1009 /* If the initialization of this soc device failed, there is no codec
1010 * associated with it. Just bail out in this case.
1012 if (!card->codec)
1013 return 0;
1015 /* AC97 devices might have other drivers hanging off them so
1016 * need to resume immediately. Other drivers don't have that
1017 * problem and may take a substantial amount of time to resume
1018 * due to I/O costs and anti-pop so handle them out of line.
1020 if (cpu_dai->ac97_control) {
1021 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1022 soc_resume_deferred(&card->deferred_resume_work);
1023 } else {
1024 dev_dbg(socdev->dev, "Scheduling resume work\n");
1025 if (!schedule_work(&card->deferred_resume_work))
1026 dev_err(socdev->dev, "resume work item may be lost\n");
1029 return 0;
1031 #else
1032 #define soc_suspend NULL
1033 #define soc_resume NULL
1034 #endif
1036 static struct snd_soc_dai_ops null_dai_ops = {
1039 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1041 struct platform_device *pdev = container_of(card->dev,
1042 struct platform_device,
1043 dev);
1044 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
1045 struct snd_soc_codec *codec;
1046 struct snd_soc_platform *platform;
1047 struct snd_soc_dai *dai;
1048 int i, found, ret, ac97;
1050 if (card->instantiated)
1051 return;
1053 found = 0;
1054 list_for_each_entry(platform, &platform_list, list)
1055 if (card->platform == platform) {
1056 found = 1;
1057 break;
1059 if (!found) {
1060 dev_dbg(card->dev, "Platform %s not registered\n",
1061 card->platform->name);
1062 return;
1065 ac97 = 0;
1066 for (i = 0; i < card->num_links; i++) {
1067 found = 0;
1068 list_for_each_entry(dai, &dai_list, list)
1069 if (card->dai_link[i].cpu_dai == dai) {
1070 found = 1;
1071 break;
1073 if (!found) {
1074 dev_dbg(card->dev, "DAI %s not registered\n",
1075 card->dai_link[i].cpu_dai->name);
1076 return;
1079 if (card->dai_link[i].cpu_dai->ac97_control)
1080 ac97 = 1;
1083 for (i = 0; i < card->num_links; i++) {
1084 if (!card->dai_link[i].codec_dai->ops)
1085 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1088 /* If we have AC97 in the system then don't wait for the
1089 * codec. This will need revisiting if we have to handle
1090 * systems with mixed AC97 and non-AC97 parts. Only check for
1091 * DAIs currently; we can't do this per link since some AC97
1092 * codecs have non-AC97 DAIs.
1094 if (!ac97)
1095 for (i = 0; i < card->num_links; i++) {
1096 found = 0;
1097 list_for_each_entry(dai, &dai_list, list)
1098 if (card->dai_link[i].codec_dai == dai) {
1099 found = 1;
1100 break;
1102 if (!found) {
1103 dev_dbg(card->dev, "DAI %s not registered\n",
1104 card->dai_link[i].codec_dai->name);
1105 return;
1109 /* Note that we do not current check for codec components */
1111 dev_dbg(card->dev, "All components present, instantiating\n");
1113 /* Found everything, bring it up */
1114 card->pmdown_time = pmdown_time;
1116 if (card->probe) {
1117 ret = card->probe(pdev);
1118 if (ret < 0)
1119 return;
1122 for (i = 0; i < card->num_links; i++) {
1123 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1124 if (cpu_dai->probe) {
1125 ret = cpu_dai->probe(pdev, cpu_dai);
1126 if (ret < 0)
1127 goto cpu_dai_err;
1131 if (codec_dev->probe) {
1132 ret = codec_dev->probe(pdev);
1133 if (ret < 0)
1134 goto cpu_dai_err;
1136 codec = card->codec;
1138 if (platform->probe) {
1139 ret = platform->probe(pdev);
1140 if (ret < 0)
1141 goto platform_err;
1144 /* DAPM stream work */
1145 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1146 #ifdef CONFIG_PM
1147 /* deferred resume work */
1148 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1149 #endif
1151 for (i = 0; i < card->num_links; i++) {
1152 if (card->dai_link[i].init) {
1153 ret = card->dai_link[i].init(codec);
1154 if (ret < 0) {
1155 printk(KERN_ERR "asoc: failed to init %s\n",
1156 card->dai_link[i].stream_name);
1157 continue;
1160 if (card->dai_link[i].codec_dai->ac97_control)
1161 ac97 = 1;
1164 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1165 "%s", card->name);
1166 snprintf(codec->card->longname, sizeof(codec->card->longname),
1167 "%s (%s)", card->name, codec->name);
1169 /* Make sure all DAPM widgets are instantiated */
1170 snd_soc_dapm_new_widgets(codec);
1172 ret = snd_card_register(codec->card);
1173 if (ret < 0) {
1174 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1175 codec->name);
1176 goto card_err;
1179 mutex_lock(&codec->mutex);
1180 #ifdef CONFIG_SND_SOC_AC97_BUS
1181 /* Only instantiate AC97 if not already done by the adaptor
1182 * for the generic AC97 subsystem.
1184 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1185 ret = soc_ac97_dev_register(codec);
1186 if (ret < 0) {
1187 printk(KERN_ERR "asoc: AC97 device register failed\n");
1188 snd_card_free(codec->card);
1189 mutex_unlock(&codec->mutex);
1190 goto card_err;
1193 #endif
1195 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1196 if (ret < 0)
1197 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1199 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1200 if (ret < 0)
1201 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1203 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1204 if (ret < 0)
1205 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1207 soc_init_codec_debugfs(codec);
1208 mutex_unlock(&codec->mutex);
1210 card->instantiated = 1;
1212 return;
1214 card_err:
1215 if (platform->remove)
1216 platform->remove(pdev);
1218 platform_err:
1219 if (codec_dev->remove)
1220 codec_dev->remove(pdev);
1222 cpu_dai_err:
1223 for (i--; i >= 0; i--) {
1224 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1225 if (cpu_dai->remove)
1226 cpu_dai->remove(pdev, cpu_dai);
1229 if (card->remove)
1230 card->remove(pdev);
1234 * Attempt to initialise any uninitalised cards. Must be called with
1235 * client_mutex.
1237 static void snd_soc_instantiate_cards(void)
1239 struct snd_soc_card *card;
1240 list_for_each_entry(card, &card_list, list)
1241 snd_soc_instantiate_card(card);
1244 /* probes a new socdev */
1245 static int soc_probe(struct platform_device *pdev)
1247 int ret = 0;
1248 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1249 struct snd_soc_card *card = socdev->card;
1251 /* Bodge while we push things out of socdev */
1252 card->socdev = socdev;
1254 /* Bodge while we unpick instantiation */
1255 card->dev = &pdev->dev;
1256 ret = snd_soc_register_card(card);
1257 if (ret != 0) {
1258 dev_err(&pdev->dev, "Failed to register card\n");
1259 return ret;
1262 return 0;
1265 /* removes a socdev */
1266 static int soc_remove(struct platform_device *pdev)
1268 int i;
1269 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1270 struct snd_soc_card *card = socdev->card;
1271 struct snd_soc_platform *platform = card->platform;
1272 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1274 if (!card->instantiated)
1275 return 0;
1277 run_delayed_work(&card->delayed_work);
1279 if (platform->remove)
1280 platform->remove(pdev);
1282 if (codec_dev->remove)
1283 codec_dev->remove(pdev);
1285 for (i = 0; i < card->num_links; i++) {
1286 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1287 if (cpu_dai->remove)
1288 cpu_dai->remove(pdev, cpu_dai);
1291 if (card->remove)
1292 card->remove(pdev);
1294 snd_soc_unregister_card(card);
1296 return 0;
1299 static int soc_poweroff(struct device *dev)
1301 struct platform_device *pdev = to_platform_device(dev);
1302 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1303 struct snd_soc_card *card = socdev->card;
1305 if (!card->instantiated)
1306 return 0;
1308 /* Flush out pmdown_time work - we actually do want to run it
1309 * now, we're shutting down so no imminent restart. */
1310 run_delayed_work(&card->delayed_work);
1312 snd_soc_dapm_shutdown(socdev);
1314 return 0;
1317 static const struct dev_pm_ops soc_pm_ops = {
1318 .suspend = soc_suspend,
1319 .resume = soc_resume,
1320 .poweroff = soc_poweroff,
1323 /* ASoC platform driver */
1324 static struct platform_driver soc_driver = {
1325 .driver = {
1326 .name = "soc-audio",
1327 .owner = THIS_MODULE,
1328 .pm = &soc_pm_ops,
1330 .probe = soc_probe,
1331 .remove = soc_remove,
1334 /* create a new pcm */
1335 static int soc_new_pcm(struct snd_soc_device *socdev,
1336 struct snd_soc_dai_link *dai_link, int num)
1338 struct snd_soc_card *card = socdev->card;
1339 struct snd_soc_codec *codec = card->codec;
1340 struct snd_soc_platform *platform = card->platform;
1341 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1342 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1343 struct snd_soc_pcm_runtime *rtd;
1344 struct snd_pcm *pcm;
1345 char new_name[64];
1346 int ret = 0, playback = 0, capture = 0;
1348 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1349 if (rtd == NULL)
1350 return -ENOMEM;
1352 rtd->dai = dai_link;
1353 rtd->socdev = socdev;
1354 codec_dai->codec = card->codec;
1356 /* check client and interface hw capabilities */
1357 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1358 dai_link->stream_name, codec_dai->name, num);
1360 if (codec_dai->playback.channels_min)
1361 playback = 1;
1362 if (codec_dai->capture.channels_min)
1363 capture = 1;
1365 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1366 capture, &pcm);
1367 if (ret < 0) {
1368 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1369 codec->name);
1370 kfree(rtd);
1371 return ret;
1374 dai_link->pcm = pcm;
1375 pcm->private_data = rtd;
1376 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1377 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1378 soc_pcm_ops.copy = platform->pcm_ops->copy;
1379 soc_pcm_ops.silence = platform->pcm_ops->silence;
1380 soc_pcm_ops.ack = platform->pcm_ops->ack;
1381 soc_pcm_ops.page = platform->pcm_ops->page;
1383 if (playback)
1384 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1386 if (capture)
1387 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1389 ret = platform->pcm_new(codec->card, codec_dai, pcm);
1390 if (ret < 0) {
1391 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1392 kfree(rtd);
1393 return ret;
1396 pcm->private_free = platform->pcm_free;
1397 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1398 cpu_dai->name);
1399 return ret;
1403 * snd_soc_codec_volatile_register: Report if a register is volatile.
1405 * @codec: CODEC to query.
1406 * @reg: Register to query.
1408 * Boolean function indiciating if a CODEC register is volatile.
1410 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1412 if (codec->volatile_register)
1413 return codec->volatile_register(reg);
1414 else
1415 return 0;
1417 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1420 * snd_soc_new_ac97_codec - initailise AC97 device
1421 * @codec: audio codec
1422 * @ops: AC97 bus operations
1423 * @num: AC97 codec number
1425 * Initialises AC97 codec resources for use by ad-hoc devices only.
1427 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1428 struct snd_ac97_bus_ops *ops, int num)
1430 mutex_lock(&codec->mutex);
1432 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1433 if (codec->ac97 == NULL) {
1434 mutex_unlock(&codec->mutex);
1435 return -ENOMEM;
1438 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1439 if (codec->ac97->bus == NULL) {
1440 kfree(codec->ac97);
1441 codec->ac97 = NULL;
1442 mutex_unlock(&codec->mutex);
1443 return -ENOMEM;
1446 codec->ac97->bus->ops = ops;
1447 codec->ac97->num = num;
1448 codec->dev = &codec->ac97->dev;
1449 mutex_unlock(&codec->mutex);
1450 return 0;
1452 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1455 * snd_soc_free_ac97_codec - free AC97 codec device
1456 * @codec: audio codec
1458 * Frees AC97 codec device resources.
1460 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1462 mutex_lock(&codec->mutex);
1463 kfree(codec->ac97->bus);
1464 kfree(codec->ac97);
1465 codec->ac97 = NULL;
1466 mutex_unlock(&codec->mutex);
1468 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1471 * snd_soc_update_bits - update codec register bits
1472 * @codec: audio codec
1473 * @reg: codec register
1474 * @mask: register mask
1475 * @value: new value
1477 * Writes new register value.
1479 * Returns 1 for change else 0.
1481 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1482 unsigned int mask, unsigned int value)
1484 int change;
1485 unsigned int old, new;
1487 old = snd_soc_read(codec, reg);
1488 new = (old & ~mask) | value;
1489 change = old != new;
1490 if (change)
1491 snd_soc_write(codec, reg, new);
1493 return change;
1495 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1498 * snd_soc_update_bits_locked - update codec register bits
1499 * @codec: audio codec
1500 * @reg: codec register
1501 * @mask: register mask
1502 * @value: new value
1504 * Writes new register value, and takes the codec mutex.
1506 * Returns 1 for change else 0.
1508 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1509 unsigned short reg, unsigned int mask,
1510 unsigned int value)
1512 int change;
1514 mutex_lock(&codec->mutex);
1515 change = snd_soc_update_bits(codec, reg, mask, value);
1516 mutex_unlock(&codec->mutex);
1518 return change;
1520 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1523 * snd_soc_test_bits - test register for change
1524 * @codec: audio codec
1525 * @reg: codec register
1526 * @mask: register mask
1527 * @value: new value
1529 * Tests a register with a new value and checks if the new value is
1530 * different from the old value.
1532 * Returns 1 for change else 0.
1534 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1535 unsigned int mask, unsigned int value)
1537 int change;
1538 unsigned int old, new;
1540 old = snd_soc_read(codec, reg);
1541 new = (old & ~mask) | value;
1542 change = old != new;
1544 return change;
1546 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1549 * snd_soc_new_pcms - create new sound card and pcms
1550 * @socdev: the SoC audio device
1551 * @idx: ALSA card index
1552 * @xid: card identification
1554 * Create a new sound card based upon the codec and interface pcms.
1556 * Returns 0 for success, else error.
1558 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1560 struct snd_soc_card *card = socdev->card;
1561 struct snd_soc_codec *codec = card->codec;
1562 int ret, i;
1564 mutex_lock(&codec->mutex);
1566 /* register a sound card */
1567 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1568 if (ret < 0) {
1569 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1570 codec->name);
1571 mutex_unlock(&codec->mutex);
1572 return ret;
1575 codec->socdev = socdev;
1576 codec->card->dev = socdev->dev;
1577 codec->card->private_data = codec;
1578 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1580 /* create the pcms */
1581 for (i = 0; i < card->num_links; i++) {
1582 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1583 if (ret < 0) {
1584 printk(KERN_ERR "asoc: can't create pcm %s\n",
1585 card->dai_link[i].stream_name);
1586 mutex_unlock(&codec->mutex);
1587 return ret;
1589 if (card->dai_link[i].codec_dai->ac97_control) {
1590 snd_ac97_dev_add_pdata(codec->ac97,
1591 card->dai_link[i].cpu_dai->ac97_pdata);
1595 mutex_unlock(&codec->mutex);
1596 return ret;
1598 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1601 * snd_soc_free_pcms - free sound card and pcms
1602 * @socdev: the SoC audio device
1604 * Frees sound card and pcms associated with the socdev.
1605 * Also unregister the codec if it is an AC97 device.
1607 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1609 struct snd_soc_codec *codec = socdev->card->codec;
1610 #ifdef CONFIG_SND_SOC_AC97_BUS
1611 struct snd_soc_dai *codec_dai;
1612 int i;
1613 #endif
1615 mutex_lock(&codec->mutex);
1616 soc_cleanup_codec_debugfs(codec);
1617 #ifdef CONFIG_SND_SOC_AC97_BUS
1618 for (i = 0; i < codec->num_dai; i++) {
1619 codec_dai = &codec->dai[i];
1620 if (codec_dai->ac97_control && codec->ac97 &&
1621 strcmp(codec->name, "AC97") != 0) {
1622 soc_ac97_dev_unregister(codec);
1623 goto free_card;
1626 free_card:
1627 #endif
1629 if (codec->card)
1630 snd_card_free(codec->card);
1631 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1632 mutex_unlock(&codec->mutex);
1634 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1637 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1638 * @substream: the pcm substream
1639 * @hw: the hardware parameters
1641 * Sets the substream runtime hardware parameters.
1643 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1644 const struct snd_pcm_hardware *hw)
1646 struct snd_pcm_runtime *runtime = substream->runtime;
1647 runtime->hw.info = hw->info;
1648 runtime->hw.formats = hw->formats;
1649 runtime->hw.period_bytes_min = hw->period_bytes_min;
1650 runtime->hw.period_bytes_max = hw->period_bytes_max;
1651 runtime->hw.periods_min = hw->periods_min;
1652 runtime->hw.periods_max = hw->periods_max;
1653 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1654 runtime->hw.fifo_size = hw->fifo_size;
1655 return 0;
1657 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1660 * snd_soc_cnew - create new control
1661 * @_template: control template
1662 * @data: control private data
1663 * @long_name: control long name
1665 * Create a new mixer control from a template control.
1667 * Returns 0 for success, else error.
1669 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1670 void *data, char *long_name)
1672 struct snd_kcontrol_new template;
1674 memcpy(&template, _template, sizeof(template));
1675 if (long_name)
1676 template.name = long_name;
1677 template.index = 0;
1679 return snd_ctl_new1(&template, data);
1681 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1684 * snd_soc_add_controls - add an array of controls to a codec.
1685 * Convienience function to add a list of controls. Many codecs were
1686 * duplicating this code.
1688 * @codec: codec to add controls to
1689 * @controls: array of controls to add
1690 * @num_controls: number of elements in the array
1692 * Return 0 for success, else error.
1694 int snd_soc_add_controls(struct snd_soc_codec *codec,
1695 const struct snd_kcontrol_new *controls, int num_controls)
1697 struct snd_card *card = codec->card;
1698 int err, i;
1700 for (i = 0; i < num_controls; i++) {
1701 const struct snd_kcontrol_new *control = &controls[i];
1702 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1703 if (err < 0) {
1704 dev_err(codec->dev, "%s: Failed to add %s\n",
1705 codec->name, control->name);
1706 return err;
1710 return 0;
1712 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1715 * snd_soc_info_enum_double - enumerated double mixer info callback
1716 * @kcontrol: mixer control
1717 * @uinfo: control element information
1719 * Callback to provide information about a double enumerated
1720 * mixer control.
1722 * Returns 0 for success.
1724 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_info *uinfo)
1727 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1729 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1730 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1731 uinfo->value.enumerated.items = e->max;
1733 if (uinfo->value.enumerated.item > e->max - 1)
1734 uinfo->value.enumerated.item = e->max - 1;
1735 strcpy(uinfo->value.enumerated.name,
1736 e->texts[uinfo->value.enumerated.item]);
1737 return 0;
1739 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1742 * snd_soc_get_enum_double - enumerated double mixer get callback
1743 * @kcontrol: mixer control
1744 * @ucontrol: control element information
1746 * Callback to get the value of a double enumerated mixer.
1748 * Returns 0 for success.
1750 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_value *ucontrol)
1753 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1754 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1755 unsigned int val, bitmask;
1757 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1759 val = snd_soc_read(codec, e->reg);
1760 ucontrol->value.enumerated.item[0]
1761 = (val >> e->shift_l) & (bitmask - 1);
1762 if (e->shift_l != e->shift_r)
1763 ucontrol->value.enumerated.item[1] =
1764 (val >> e->shift_r) & (bitmask - 1);
1766 return 0;
1768 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1771 * snd_soc_put_enum_double - enumerated double mixer put callback
1772 * @kcontrol: mixer control
1773 * @ucontrol: control element information
1775 * Callback to set the value of a double enumerated mixer.
1777 * Returns 0 for success.
1779 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
1782 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1783 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1784 unsigned int val;
1785 unsigned int mask, bitmask;
1787 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1789 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1790 return -EINVAL;
1791 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1792 mask = (bitmask - 1) << e->shift_l;
1793 if (e->shift_l != e->shift_r) {
1794 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1795 return -EINVAL;
1796 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1797 mask |= (bitmask - 1) << e->shift_r;
1800 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1802 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1805 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1806 * @kcontrol: mixer control
1807 * @ucontrol: control element information
1809 * Callback to get the value of a double semi enumerated mixer.
1811 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1812 * used for handling bitfield coded enumeration for example.
1814 * Returns 0 for success.
1816 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1819 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1820 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1821 unsigned int reg_val, val, mux;
1823 reg_val = snd_soc_read(codec, e->reg);
1824 val = (reg_val >> e->shift_l) & e->mask;
1825 for (mux = 0; mux < e->max; mux++) {
1826 if (val == e->values[mux])
1827 break;
1829 ucontrol->value.enumerated.item[0] = mux;
1830 if (e->shift_l != e->shift_r) {
1831 val = (reg_val >> e->shift_r) & e->mask;
1832 for (mux = 0; mux < e->max; mux++) {
1833 if (val == e->values[mux])
1834 break;
1836 ucontrol->value.enumerated.item[1] = mux;
1839 return 0;
1841 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1844 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1845 * @kcontrol: mixer control
1846 * @ucontrol: control element information
1848 * Callback to set the value of a double semi enumerated mixer.
1850 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1851 * used for handling bitfield coded enumeration for example.
1853 * Returns 0 for success.
1855 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1856 struct snd_ctl_elem_value *ucontrol)
1858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1859 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1860 unsigned int val;
1861 unsigned int mask;
1863 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1864 return -EINVAL;
1865 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1866 mask = e->mask << e->shift_l;
1867 if (e->shift_l != e->shift_r) {
1868 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1869 return -EINVAL;
1870 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1871 mask |= e->mask << e->shift_r;
1874 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1876 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1879 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1880 * @kcontrol: mixer control
1881 * @uinfo: control element information
1883 * Callback to provide information about an external enumerated
1884 * single mixer.
1886 * Returns 0 for success.
1888 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_info *uinfo)
1891 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1893 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1894 uinfo->count = 1;
1895 uinfo->value.enumerated.items = e->max;
1897 if (uinfo->value.enumerated.item > e->max - 1)
1898 uinfo->value.enumerated.item = e->max - 1;
1899 strcpy(uinfo->value.enumerated.name,
1900 e->texts[uinfo->value.enumerated.item]);
1901 return 0;
1903 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1906 * snd_soc_info_volsw_ext - external single mixer info callback
1907 * @kcontrol: mixer control
1908 * @uinfo: control element information
1910 * Callback to provide information about a single external mixer control.
1912 * Returns 0 for success.
1914 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1915 struct snd_ctl_elem_info *uinfo)
1917 int max = kcontrol->private_value;
1919 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1920 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1921 else
1922 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1924 uinfo->count = 1;
1925 uinfo->value.integer.min = 0;
1926 uinfo->value.integer.max = max;
1927 return 0;
1929 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1932 * snd_soc_info_volsw - single mixer info callback
1933 * @kcontrol: mixer control
1934 * @uinfo: control element information
1936 * Callback to provide information about a single mixer control.
1938 * Returns 0 for success.
1940 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1941 struct snd_ctl_elem_info *uinfo)
1943 struct soc_mixer_control *mc =
1944 (struct soc_mixer_control *)kcontrol->private_value;
1945 int max = mc->max;
1946 unsigned int shift = mc->shift;
1947 unsigned int rshift = mc->rshift;
1949 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1950 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1951 else
1952 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1954 uinfo->count = shift == rshift ? 1 : 2;
1955 uinfo->value.integer.min = 0;
1956 uinfo->value.integer.max = max;
1957 return 0;
1959 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1962 * snd_soc_get_volsw - single mixer get callback
1963 * @kcontrol: mixer control
1964 * @ucontrol: control element information
1966 * Callback to get the value of a single mixer control.
1968 * Returns 0 for success.
1970 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1971 struct snd_ctl_elem_value *ucontrol)
1973 struct soc_mixer_control *mc =
1974 (struct soc_mixer_control *)kcontrol->private_value;
1975 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1976 unsigned int reg = mc->reg;
1977 unsigned int shift = mc->shift;
1978 unsigned int rshift = mc->rshift;
1979 int max = mc->max;
1980 unsigned int mask = (1 << fls(max)) - 1;
1981 unsigned int invert = mc->invert;
1983 ucontrol->value.integer.value[0] =
1984 (snd_soc_read(codec, reg) >> shift) & mask;
1985 if (shift != rshift)
1986 ucontrol->value.integer.value[1] =
1987 (snd_soc_read(codec, reg) >> rshift) & mask;
1988 if (invert) {
1989 ucontrol->value.integer.value[0] =
1990 max - ucontrol->value.integer.value[0];
1991 if (shift != rshift)
1992 ucontrol->value.integer.value[1] =
1993 max - ucontrol->value.integer.value[1];
1996 return 0;
1998 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2001 * snd_soc_put_volsw - single mixer put callback
2002 * @kcontrol: mixer control
2003 * @ucontrol: control element information
2005 * Callback to set the value of a single mixer control.
2007 * Returns 0 for success.
2009 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_value *ucontrol)
2012 struct soc_mixer_control *mc =
2013 (struct soc_mixer_control *)kcontrol->private_value;
2014 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2015 unsigned int reg = mc->reg;
2016 unsigned int shift = mc->shift;
2017 unsigned int rshift = mc->rshift;
2018 int max = mc->max;
2019 unsigned int mask = (1 << fls(max)) - 1;
2020 unsigned int invert = mc->invert;
2021 unsigned int val, val2, val_mask;
2023 val = (ucontrol->value.integer.value[0] & mask);
2024 if (invert)
2025 val = max - val;
2026 val_mask = mask << shift;
2027 val = val << shift;
2028 if (shift != rshift) {
2029 val2 = (ucontrol->value.integer.value[1] & mask);
2030 if (invert)
2031 val2 = max - val2;
2032 val_mask |= mask << rshift;
2033 val |= val2 << rshift;
2035 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2037 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2040 * snd_soc_info_volsw_2r - double mixer info callback
2041 * @kcontrol: mixer control
2042 * @uinfo: control element information
2044 * Callback to provide information about a double mixer control that
2045 * spans 2 codec registers.
2047 * Returns 0 for success.
2049 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2050 struct snd_ctl_elem_info *uinfo)
2052 struct soc_mixer_control *mc =
2053 (struct soc_mixer_control *)kcontrol->private_value;
2054 int max = mc->max;
2056 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2057 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2058 else
2059 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2061 uinfo->count = 2;
2062 uinfo->value.integer.min = 0;
2063 uinfo->value.integer.max = max;
2064 return 0;
2066 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2069 * snd_soc_get_volsw_2r - double mixer get callback
2070 * @kcontrol: mixer control
2071 * @ucontrol: control element information
2073 * Callback to get the value of a double mixer control that spans 2 registers.
2075 * Returns 0 for success.
2077 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2080 struct soc_mixer_control *mc =
2081 (struct soc_mixer_control *)kcontrol->private_value;
2082 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2083 unsigned int reg = mc->reg;
2084 unsigned int reg2 = mc->rreg;
2085 unsigned int shift = mc->shift;
2086 int max = mc->max;
2087 unsigned int mask = (1 << fls(max)) - 1;
2088 unsigned int invert = mc->invert;
2090 ucontrol->value.integer.value[0] =
2091 (snd_soc_read(codec, reg) >> shift) & mask;
2092 ucontrol->value.integer.value[1] =
2093 (snd_soc_read(codec, reg2) >> shift) & mask;
2094 if (invert) {
2095 ucontrol->value.integer.value[0] =
2096 max - ucontrol->value.integer.value[0];
2097 ucontrol->value.integer.value[1] =
2098 max - ucontrol->value.integer.value[1];
2101 return 0;
2103 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2106 * snd_soc_put_volsw_2r - double mixer set callback
2107 * @kcontrol: mixer control
2108 * @ucontrol: control element information
2110 * Callback to set the value of a double mixer control that spans 2 registers.
2112 * Returns 0 for success.
2114 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2117 struct soc_mixer_control *mc =
2118 (struct soc_mixer_control *)kcontrol->private_value;
2119 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2120 unsigned int reg = mc->reg;
2121 unsigned int reg2 = mc->rreg;
2122 unsigned int shift = mc->shift;
2123 int max = mc->max;
2124 unsigned int mask = (1 << fls(max)) - 1;
2125 unsigned int invert = mc->invert;
2126 int err;
2127 unsigned int val, val2, val_mask;
2129 val_mask = mask << shift;
2130 val = (ucontrol->value.integer.value[0] & mask);
2131 val2 = (ucontrol->value.integer.value[1] & mask);
2133 if (invert) {
2134 val = max - val;
2135 val2 = max - val2;
2138 val = val << shift;
2139 val2 = val2 << shift;
2141 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2142 if (err < 0)
2143 return err;
2145 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2146 return err;
2148 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2151 * snd_soc_info_volsw_s8 - signed mixer info callback
2152 * @kcontrol: mixer control
2153 * @uinfo: control element information
2155 * Callback to provide information about a signed mixer control.
2157 * Returns 0 for success.
2159 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_info *uinfo)
2162 struct soc_mixer_control *mc =
2163 (struct soc_mixer_control *)kcontrol->private_value;
2164 int max = mc->max;
2165 int min = mc->min;
2167 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2168 uinfo->count = 2;
2169 uinfo->value.integer.min = 0;
2170 uinfo->value.integer.max = max-min;
2171 return 0;
2173 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2176 * snd_soc_get_volsw_s8 - signed mixer get callback
2177 * @kcontrol: mixer control
2178 * @ucontrol: control element information
2180 * Callback to get the value of a signed mixer control.
2182 * Returns 0 for success.
2184 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2187 struct soc_mixer_control *mc =
2188 (struct soc_mixer_control *)kcontrol->private_value;
2189 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2190 unsigned int reg = mc->reg;
2191 int min = mc->min;
2192 int val = snd_soc_read(codec, reg);
2194 ucontrol->value.integer.value[0] =
2195 ((signed char)(val & 0xff))-min;
2196 ucontrol->value.integer.value[1] =
2197 ((signed char)((val >> 8) & 0xff))-min;
2198 return 0;
2200 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2203 * snd_soc_put_volsw_sgn - signed mixer put callback
2204 * @kcontrol: mixer control
2205 * @ucontrol: control element information
2207 * Callback to set the value of a signed mixer control.
2209 * Returns 0 for success.
2211 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2212 struct snd_ctl_elem_value *ucontrol)
2214 struct soc_mixer_control *mc =
2215 (struct soc_mixer_control *)kcontrol->private_value;
2216 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2217 unsigned int reg = mc->reg;
2218 int min = mc->min;
2219 unsigned int val;
2221 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2222 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2224 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2226 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2229 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2230 * @dai: DAI
2231 * @clk_id: DAI specific clock ID
2232 * @freq: new clock frequency in Hz
2233 * @dir: new clock direction - input/output.
2235 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2237 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2238 unsigned int freq, int dir)
2240 if (dai->ops && dai->ops->set_sysclk)
2241 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
2242 else
2243 return -EINVAL;
2245 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2248 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2249 * @dai: DAI
2250 * @div_id: DAI specific clock divider ID
2251 * @div: new clock divisor.
2253 * Configures the clock dividers. This is used to derive the best DAI bit and
2254 * frame clocks from the system or master clock. It's best to set the DAI bit
2255 * and frame clocks as low as possible to save system power.
2257 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2258 int div_id, int div)
2260 if (dai->ops && dai->ops->set_clkdiv)
2261 return dai->ops->set_clkdiv(dai, div_id, div);
2262 else
2263 return -EINVAL;
2265 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2268 * snd_soc_dai_set_pll - configure DAI PLL.
2269 * @dai: DAI
2270 * @pll_id: DAI specific PLL ID
2271 * @source: DAI specific source for the PLL
2272 * @freq_in: PLL input clock frequency in Hz
2273 * @freq_out: requested PLL output clock frequency in Hz
2275 * Configures and enables PLL to generate output clock based on input clock.
2277 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2278 unsigned int freq_in, unsigned int freq_out)
2280 if (dai->ops && dai->ops->set_pll)
2281 return dai->ops->set_pll(dai, pll_id, source,
2282 freq_in, freq_out);
2283 else
2284 return -EINVAL;
2286 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2289 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2290 * @dai: DAI
2291 * @fmt: SND_SOC_DAIFMT_ format value.
2293 * Configures the DAI hardware format and clocking.
2295 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2297 if (dai->ops && dai->ops->set_fmt)
2298 return dai->ops->set_fmt(dai, fmt);
2299 else
2300 return -EINVAL;
2302 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2305 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2306 * @dai: DAI
2307 * @tx_mask: bitmask representing active TX slots.
2308 * @rx_mask: bitmask representing active RX slots.
2309 * @slots: Number of slots in use.
2310 * @slot_width: Width in bits for each slot.
2312 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2313 * specific.
2315 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2316 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2318 if (dai->ops && dai->ops->set_tdm_slot)
2319 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2320 slots, slot_width);
2321 else
2322 return -EINVAL;
2324 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2327 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2328 * @dai: DAI
2329 * @tx_num: how many TX channels
2330 * @tx_slot: pointer to an array which imply the TX slot number channel
2331 * 0~num-1 uses
2332 * @rx_num: how many RX channels
2333 * @rx_slot: pointer to an array which imply the RX slot number channel
2334 * 0~num-1 uses
2336 * configure the relationship between channel number and TDM slot number.
2338 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2339 unsigned int tx_num, unsigned int *tx_slot,
2340 unsigned int rx_num, unsigned int *rx_slot)
2342 if (dai->ops && dai->ops->set_channel_map)
2343 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2344 rx_num, rx_slot);
2345 else
2346 return -EINVAL;
2348 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2351 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2352 * @dai: DAI
2353 * @tristate: tristate enable
2355 * Tristates the DAI so that others can use it.
2357 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2359 if (dai->ops && dai->ops->set_tristate)
2360 return dai->ops->set_tristate(dai, tristate);
2361 else
2362 return -EINVAL;
2364 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2367 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2368 * @dai: DAI
2369 * @mute: mute enable
2371 * Mutes the DAI DAC.
2373 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2375 if (dai->ops && dai->ops->digital_mute)
2376 return dai->ops->digital_mute(dai, mute);
2377 else
2378 return -EINVAL;
2380 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2383 * snd_soc_register_card - Register a card with the ASoC core
2385 * @card: Card to register
2387 * Note that currently this is an internal only function: it will be
2388 * exposed to machine drivers after further backporting of ASoC v2
2389 * registration APIs.
2391 static int snd_soc_register_card(struct snd_soc_card *card)
2393 if (!card->name || !card->dev)
2394 return -EINVAL;
2396 INIT_LIST_HEAD(&card->list);
2397 card->instantiated = 0;
2399 mutex_lock(&client_mutex);
2400 list_add(&card->list, &card_list);
2401 snd_soc_instantiate_cards();
2402 mutex_unlock(&client_mutex);
2404 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2406 return 0;
2410 * snd_soc_unregister_card - Unregister a card with the ASoC core
2412 * @card: Card to unregister
2414 * Note that currently this is an internal only function: it will be
2415 * exposed to machine drivers after further backporting of ASoC v2
2416 * registration APIs.
2418 static int snd_soc_unregister_card(struct snd_soc_card *card)
2420 mutex_lock(&client_mutex);
2421 list_del(&card->list);
2422 mutex_unlock(&client_mutex);
2424 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2426 return 0;
2430 * snd_soc_register_dai - Register a DAI with the ASoC core
2432 * @dai: DAI to register
2434 int snd_soc_register_dai(struct snd_soc_dai *dai)
2436 if (!dai->name)
2437 return -EINVAL;
2439 /* The device should become mandatory over time */
2440 if (!dai->dev)
2441 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2443 if (!dai->ops)
2444 dai->ops = &null_dai_ops;
2446 INIT_LIST_HEAD(&dai->list);
2448 mutex_lock(&client_mutex);
2449 list_add(&dai->list, &dai_list);
2450 snd_soc_instantiate_cards();
2451 mutex_unlock(&client_mutex);
2453 pr_debug("Registered DAI '%s'\n", dai->name);
2455 return 0;
2457 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2460 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2462 * @dai: DAI to unregister
2464 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2466 mutex_lock(&client_mutex);
2467 list_del(&dai->list);
2468 mutex_unlock(&client_mutex);
2470 pr_debug("Unregistered DAI '%s'\n", dai->name);
2472 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2475 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2477 * @dai: Array of DAIs to register
2478 * @count: Number of DAIs
2480 int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2482 int i, ret;
2484 for (i = 0; i < count; i++) {
2485 ret = snd_soc_register_dai(&dai[i]);
2486 if (ret != 0)
2487 goto err;
2490 return 0;
2492 err:
2493 for (i--; i >= 0; i--)
2494 snd_soc_unregister_dai(&dai[i]);
2496 return ret;
2498 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2501 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2503 * @dai: Array of DAIs to unregister
2504 * @count: Number of DAIs
2506 void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2508 int i;
2510 for (i = 0; i < count; i++)
2511 snd_soc_unregister_dai(&dai[i]);
2513 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2516 * snd_soc_register_platform - Register a platform with the ASoC core
2518 * @platform: platform to register
2520 int snd_soc_register_platform(struct snd_soc_platform *platform)
2522 if (!platform->name)
2523 return -EINVAL;
2525 INIT_LIST_HEAD(&platform->list);
2527 mutex_lock(&client_mutex);
2528 list_add(&platform->list, &platform_list);
2529 snd_soc_instantiate_cards();
2530 mutex_unlock(&client_mutex);
2532 pr_debug("Registered platform '%s'\n", platform->name);
2534 return 0;
2536 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2539 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2541 * @platform: platform to unregister
2543 void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2545 mutex_lock(&client_mutex);
2546 list_del(&platform->list);
2547 mutex_unlock(&client_mutex);
2549 pr_debug("Unregistered platform '%s'\n", platform->name);
2551 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2553 static u64 codec_format_map[] = {
2554 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2555 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2556 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2557 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2558 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2559 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2560 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2561 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2562 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2563 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2564 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2565 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2566 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2567 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2568 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2569 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2572 /* Fix up the DAI formats for endianness: codecs don't actually see
2573 * the endianness of the data but we're using the CPU format
2574 * definitions which do need to include endianness so we ensure that
2575 * codec DAIs always have both big and little endian variants set.
2577 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2579 int i;
2581 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2582 if (stream->formats & codec_format_map[i])
2583 stream->formats |= codec_format_map[i];
2587 * snd_soc_register_codec - Register a codec with the ASoC core
2589 * @codec: codec to register
2591 int snd_soc_register_codec(struct snd_soc_codec *codec)
2593 int i;
2595 if (!codec->name)
2596 return -EINVAL;
2598 /* The device should become mandatory over time */
2599 if (!codec->dev)
2600 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2602 INIT_LIST_HEAD(&codec->list);
2604 for (i = 0; i < codec->num_dai; i++) {
2605 fixup_codec_formats(&codec->dai[i].playback);
2606 fixup_codec_formats(&codec->dai[i].capture);
2609 mutex_lock(&client_mutex);
2610 list_add(&codec->list, &codec_list);
2611 snd_soc_instantiate_cards();
2612 mutex_unlock(&client_mutex);
2614 pr_debug("Registered codec '%s'\n", codec->name);
2616 return 0;
2618 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2621 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2623 * @codec: codec to unregister
2625 void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2627 mutex_lock(&client_mutex);
2628 list_del(&codec->list);
2629 mutex_unlock(&client_mutex);
2631 pr_debug("Unregistered codec '%s'\n", codec->name);
2633 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2635 static int __init snd_soc_init(void)
2637 #ifdef CONFIG_DEBUG_FS
2638 debugfs_root = debugfs_create_dir("asoc", NULL);
2639 if (IS_ERR(debugfs_root) || !debugfs_root) {
2640 printk(KERN_WARNING
2641 "ASoC: Failed to create debugfs directory\n");
2642 debugfs_root = NULL;
2644 #endif
2646 return platform_driver_register(&soc_driver);
2649 static void __exit snd_soc_exit(void)
2651 #ifdef CONFIG_DEBUG_FS
2652 debugfs_remove_recursive(debugfs_root);
2653 #endif
2654 platform_driver_unregister(&soc_driver);
2657 module_init(snd_soc_init);
2658 module_exit(snd_soc_exit);
2660 /* Module information */
2661 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2662 MODULE_DESCRIPTION("ALSA SoC Core");
2663 MODULE_LICENSE("GPL");
2664 MODULE_ALIAS("platform:soc-audio");