2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/initval.h>
30 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
31 struct snd_soc_dai *soc_dai)
33 struct snd_soc_pcm_runtime *rtd = substream->private_data;
36 if (!soc_dai->driver->symmetric_rates &&
37 !rtd->dai_link->symmetric_rates)
40 /* This can happen if multiple streams are starting simultaneously -
41 * the second can need to get its constraints before the first has
42 * picked a rate. Complain and allow the application to carry on.
45 dev_warn(soc_dai->dev,
46 "Not enforcing symmetric_rates due to race\n");
50 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
52 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
53 SNDRV_PCM_HW_PARAM_RATE,
54 soc_dai->rate, soc_dai->rate);
57 "Unable to apply rate symmetry constraint: %d\n", ret);
65 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
66 * then initialized and any private data can be allocated. This also calls
67 * startup for the cpu DAI, platform, machine and codec DAI.
69 static int soc_pcm_open(struct snd_pcm_substream *substream)
71 struct snd_soc_pcm_runtime *rtd = substream->private_data;
72 struct snd_pcm_runtime *runtime = substream->runtime;
73 struct snd_soc_platform *platform = rtd->platform;
74 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
75 struct snd_soc_dai *codec_dai = rtd->codec_dai;
76 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
77 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
80 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
82 /* startup the audio subsystem */
83 if (cpu_dai->driver->ops->startup) {
84 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
86 printk(KERN_ERR "asoc: can't open interface %s\n",
92 if (platform->driver->ops && platform->driver->ops->open) {
93 ret = platform->driver->ops->open(substream);
95 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
100 if (codec_dai->driver->ops->startup) {
101 ret = codec_dai->driver->ops->startup(substream, codec_dai);
103 printk(KERN_ERR "asoc: can't open codec %s\n",
109 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
110 ret = rtd->dai_link->ops->startup(substream);
112 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
117 /* Check that the codec and cpu DAIs are compatible */
118 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
119 runtime->hw.rate_min =
120 max(codec_dai_drv->playback.rate_min,
121 cpu_dai_drv->playback.rate_min);
122 runtime->hw.rate_max =
123 min(codec_dai_drv->playback.rate_max,
124 cpu_dai_drv->playback.rate_max);
125 runtime->hw.channels_min =
126 max(codec_dai_drv->playback.channels_min,
127 cpu_dai_drv->playback.channels_min);
128 runtime->hw.channels_max =
129 min(codec_dai_drv->playback.channels_max,
130 cpu_dai_drv->playback.channels_max);
131 runtime->hw.formats =
132 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
134 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
135 if (codec_dai_drv->playback.rates
136 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
137 runtime->hw.rates |= cpu_dai_drv->playback.rates;
138 if (cpu_dai_drv->playback.rates
139 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
140 runtime->hw.rates |= codec_dai_drv->playback.rates;
142 runtime->hw.rate_min =
143 max(codec_dai_drv->capture.rate_min,
144 cpu_dai_drv->capture.rate_min);
145 runtime->hw.rate_max =
146 min(codec_dai_drv->capture.rate_max,
147 cpu_dai_drv->capture.rate_max);
148 runtime->hw.channels_min =
149 max(codec_dai_drv->capture.channels_min,
150 cpu_dai_drv->capture.channels_min);
151 runtime->hw.channels_max =
152 min(codec_dai_drv->capture.channels_max,
153 cpu_dai_drv->capture.channels_max);
154 runtime->hw.formats =
155 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
157 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
158 if (codec_dai_drv->capture.rates
159 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
160 runtime->hw.rates |= cpu_dai_drv->capture.rates;
161 if (cpu_dai_drv->capture.rates
162 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
163 runtime->hw.rates |= codec_dai_drv->capture.rates;
167 snd_pcm_limit_hw_rates(runtime);
168 if (!runtime->hw.rates) {
169 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
170 codec_dai->name, cpu_dai->name);
173 if (!runtime->hw.formats) {
174 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
175 codec_dai->name, cpu_dai->name);
178 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
179 runtime->hw.channels_min > runtime->hw.channels_max) {
180 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
181 codec_dai->name, cpu_dai->name);
185 /* Symmetry only applies if we've already got an active stream. */
186 if (cpu_dai->active) {
187 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
192 if (codec_dai->active) {
193 ret = soc_pcm_apply_symmetry(substream, codec_dai);
198 pr_debug("asoc: %s <-> %s info:\n",
199 codec_dai->name, cpu_dai->name);
200 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
201 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
202 runtime->hw.channels_max);
203 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
204 runtime->hw.rate_max);
206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
207 cpu_dai->playback_active++;
208 codec_dai->playback_active++;
210 cpu_dai->capture_active++;
211 codec_dai->capture_active++;
215 rtd->codec->active++;
216 mutex_unlock(&rtd->pcm_mutex);
220 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
221 rtd->dai_link->ops->shutdown(substream);
224 if (codec_dai->driver->ops->shutdown)
225 codec_dai->driver->ops->shutdown(substream, codec_dai);
228 if (platform->driver->ops && platform->driver->ops->close)
229 platform->driver->ops->close(substream);
232 if (cpu_dai->driver->ops->shutdown)
233 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
235 mutex_unlock(&rtd->pcm_mutex);
240 * Power down the audio subsystem pmdown_time msecs after close is called.
241 * This is to ensure there are no pops or clicks in between any music tracks
242 * due to DAPM power cycling.
244 static void close_delayed_work(struct work_struct *work)
246 struct snd_soc_pcm_runtime *rtd =
247 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
248 struct snd_soc_dai *codec_dai = rtd->codec_dai;
250 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
252 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
253 codec_dai->driver->playback.stream_name,
254 codec_dai->playback_active ? "active" : "inactive",
255 codec_dai->pop_wait ? "yes" : "no");
257 /* are we waiting on this codec DAI stream */
258 if (codec_dai->pop_wait == 1) {
259 codec_dai->pop_wait = 0;
260 snd_soc_dapm_stream_event(rtd,
261 codec_dai->driver->playback.stream_name,
262 SND_SOC_DAPM_STREAM_STOP);
265 mutex_unlock(&rtd->pcm_mutex);
269 * Called by ALSA when a PCM substream is closed. Private data can be
270 * freed here. The cpu DAI, codec DAI, machine and platform are also
273 static int soc_pcm_close(struct snd_pcm_substream *substream)
275 struct snd_soc_pcm_runtime *rtd = substream->private_data;
276 struct snd_soc_platform *platform = rtd->platform;
277 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
278 struct snd_soc_dai *codec_dai = rtd->codec_dai;
279 struct snd_soc_codec *codec = rtd->codec;
281 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
284 cpu_dai->playback_active--;
285 codec_dai->playback_active--;
287 cpu_dai->capture_active--;
288 codec_dai->capture_active--;
295 /* clear the corresponding DAIs rate when inactive */
296 if (!cpu_dai->active)
299 if (!codec_dai->active)
302 /* Muting the DAC suppresses artifacts caused during digital
303 * shutdown, for example from stopping clocks.
305 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
306 snd_soc_dai_digital_mute(codec_dai, 1);
308 if (cpu_dai->driver->ops->shutdown)
309 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
311 if (codec_dai->driver->ops->shutdown)
312 codec_dai->driver->ops->shutdown(substream, codec_dai);
314 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
315 rtd->dai_link->ops->shutdown(substream);
317 if (platform->driver->ops && platform->driver->ops->close)
318 platform->driver->ops->close(substream);
319 cpu_dai->runtime = NULL;
321 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
322 if (codec->ignore_pmdown_time ||
323 rtd->dai_link->ignore_pmdown_time) {
324 /* powered down playback stream now */
325 snd_soc_dapm_stream_event(rtd,
326 codec_dai->driver->playback.stream_name,
327 SND_SOC_DAPM_STREAM_STOP);
329 /* start delayed pop wq here for playback streams */
330 codec_dai->pop_wait = 1;
331 schedule_delayed_work(&rtd->delayed_work,
332 msecs_to_jiffies(rtd->pmdown_time));
335 /* capture streams can be powered down now */
336 snd_soc_dapm_stream_event(rtd,
337 codec_dai->driver->capture.stream_name,
338 SND_SOC_DAPM_STREAM_STOP);
341 mutex_unlock(&rtd->pcm_mutex);
346 * Called by ALSA when the PCM substream is prepared, can set format, sample
347 * rate, etc. This function is non atomic and can be called multiple times,
348 * it can refer to the runtime info.
350 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
352 struct snd_soc_pcm_runtime *rtd = substream->private_data;
353 struct snd_soc_platform *platform = rtd->platform;
354 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
355 struct snd_soc_dai *codec_dai = rtd->codec_dai;
358 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
360 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
361 ret = rtd->dai_link->ops->prepare(substream);
363 printk(KERN_ERR "asoc: machine prepare error\n");
368 if (platform->driver->ops && platform->driver->ops->prepare) {
369 ret = platform->driver->ops->prepare(substream);
371 printk(KERN_ERR "asoc: platform prepare error\n");
376 if (codec_dai->driver->ops->prepare) {
377 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
379 printk(KERN_ERR "asoc: codec DAI prepare error\n");
384 if (cpu_dai->driver->ops->prepare) {
385 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
387 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
392 /* cancel any delayed stream shutdown that is pending */
393 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
394 codec_dai->pop_wait) {
395 codec_dai->pop_wait = 0;
396 cancel_delayed_work(&rtd->delayed_work);
399 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
400 snd_soc_dapm_stream_event(rtd,
401 codec_dai->driver->playback.stream_name,
402 SND_SOC_DAPM_STREAM_START);
404 snd_soc_dapm_stream_event(rtd,
405 codec_dai->driver->capture.stream_name,
406 SND_SOC_DAPM_STREAM_START);
408 snd_soc_dai_digital_mute(codec_dai, 0);
411 mutex_unlock(&rtd->pcm_mutex);
416 * Called by ALSA when the hardware params are set by application. This
417 * function can also be called multiple times and can allocate buffers
418 * (using snd_pcm_lib_* ). It's non-atomic.
420 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
421 struct snd_pcm_hw_params *params)
423 struct snd_soc_pcm_runtime *rtd = substream->private_data;
424 struct snd_soc_platform *platform = rtd->platform;
425 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
426 struct snd_soc_dai *codec_dai = rtd->codec_dai;
429 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
431 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
432 ret = rtd->dai_link->ops->hw_params(substream, params);
434 printk(KERN_ERR "asoc: machine hw_params failed\n");
439 if (codec_dai->driver->ops->hw_params) {
440 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
442 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
448 if (cpu_dai->driver->ops->hw_params) {
449 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
451 printk(KERN_ERR "asoc: interface %s hw params failed\n",
457 if (platform->driver->ops && platform->driver->ops->hw_params) {
458 ret = platform->driver->ops->hw_params(substream, params);
460 printk(KERN_ERR "asoc: platform %s hw params failed\n",
466 /* store the rate for each DAIs */
467 cpu_dai->rate = params_rate(params);
468 codec_dai->rate = params_rate(params);
471 mutex_unlock(&rtd->pcm_mutex);
475 if (cpu_dai->driver->ops->hw_free)
476 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
479 if (codec_dai->driver->ops->hw_free)
480 codec_dai->driver->ops->hw_free(substream, codec_dai);
483 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
484 rtd->dai_link->ops->hw_free(substream);
486 mutex_unlock(&rtd->pcm_mutex);
491 * Frees resources allocated by hw_params, can be called multiple times
493 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
495 struct snd_soc_pcm_runtime *rtd = substream->private_data;
496 struct snd_soc_platform *platform = rtd->platform;
497 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
498 struct snd_soc_dai *codec_dai = rtd->codec_dai;
499 struct snd_soc_codec *codec = rtd->codec;
501 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
503 /* apply codec digital mute */
505 snd_soc_dai_digital_mute(codec_dai, 1);
507 /* free any machine hw params */
508 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
509 rtd->dai_link->ops->hw_free(substream);
511 /* free any DMA resources */
512 if (platform->driver->ops && platform->driver->ops->hw_free)
513 platform->driver->ops->hw_free(substream);
515 /* now free hw params for the DAIs */
516 if (codec_dai->driver->ops->hw_free)
517 codec_dai->driver->ops->hw_free(substream, codec_dai);
519 if (cpu_dai->driver->ops->hw_free)
520 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
522 mutex_unlock(&rtd->pcm_mutex);
526 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
528 struct snd_soc_pcm_runtime *rtd = substream->private_data;
529 struct snd_soc_platform *platform = rtd->platform;
530 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
531 struct snd_soc_dai *codec_dai = rtd->codec_dai;
534 if (codec_dai->driver->ops->trigger) {
535 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
540 if (platform->driver->ops && platform->driver->ops->trigger) {
541 ret = platform->driver->ops->trigger(substream, cmd);
546 if (cpu_dai->driver->ops->trigger) {
547 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
555 * soc level wrapper for pointer callback
556 * If cpu_dai, codec_dai, platform driver has the delay callback, than
557 * the runtime->delay will be updated accordingly.
559 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
561 struct snd_soc_pcm_runtime *rtd = substream->private_data;
562 struct snd_soc_platform *platform = rtd->platform;
563 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
564 struct snd_soc_dai *codec_dai = rtd->codec_dai;
565 struct snd_pcm_runtime *runtime = substream->runtime;
566 snd_pcm_uframes_t offset = 0;
567 snd_pcm_sframes_t delay = 0;
569 if (platform->driver->ops && platform->driver->ops->pointer)
570 offset = platform->driver->ops->pointer(substream);
572 if (cpu_dai->driver->ops->delay)
573 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
575 if (codec_dai->driver->ops->delay)
576 delay += codec_dai->driver->ops->delay(substream, codec_dai);
578 if (platform->driver->delay)
579 delay += platform->driver->delay(substream, codec_dai);
581 runtime->delay = delay;
586 /* ASoC PCM operations */
587 static struct snd_pcm_ops soc_pcm_ops = {
588 .open = soc_pcm_open,
589 .close = soc_pcm_close,
590 .hw_params = soc_pcm_hw_params,
591 .hw_free = soc_pcm_hw_free,
592 .prepare = soc_pcm_prepare,
593 .trigger = soc_pcm_trigger,
594 .pointer = soc_pcm_pointer,
597 /* create a new pcm */
598 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
600 struct snd_soc_codec *codec = rtd->codec;
601 struct snd_soc_platform *platform = rtd->platform;
602 struct snd_soc_dai *codec_dai = rtd->codec_dai;
603 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
606 int ret = 0, playback = 0, capture = 0;
608 /* check client and interface hw capabilities */
609 snprintf(new_name, sizeof(new_name), "%s %s-%d",
610 rtd->dai_link->stream_name, codec_dai->name, num);
612 if (codec_dai->driver->playback.channels_min)
614 if (codec_dai->driver->capture.channels_min)
617 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
618 ret = snd_pcm_new(rtd->card->snd_card, new_name,
619 num, playback, capture, &pcm);
621 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
625 /* DAPM dai link stream work */
626 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
629 pcm->private_data = rtd;
630 if (platform->driver->ops) {
631 soc_pcm_ops.mmap = platform->driver->ops->mmap;
632 soc_pcm_ops.pointer = platform->driver->ops->pointer;
633 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
634 soc_pcm_ops.copy = platform->driver->ops->copy;
635 soc_pcm_ops.silence = platform->driver->ops->silence;
636 soc_pcm_ops.ack = platform->driver->ops->ack;
637 soc_pcm_ops.page = platform->driver->ops->page;
641 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
646 if (platform->driver->pcm_new) {
647 ret = platform->driver->pcm_new(rtd);
649 pr_err("asoc: platform pcm constructor failed\n");
654 pcm->private_free = platform->driver->pcm_free;
655 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,